Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: CI
on: [push, pull_request]
on:
push:
branches: [master]
pull_request:

concurrency:
group: ${{ github.head_ref || format('{0}-{1}', github.ref, github.run_number) }}
Expand Down Expand Up @@ -37,15 +40,34 @@ jobs:
submodules: true

- if: ${{ matrix.toolchain == 'gcc' }}
name: Restore cache
uses: actions/cache@v4.2.2
env:
gcc-version: 13.2.0
id: toolchain-version
name: Get the toolchain version
run: |
python3 -c 'import build_utils.toolchain_builder as tb
print(f"key=gcc-{tb.GCC_VERSION}-binutils-{tb.BINUTILS_VERSION}")' \
>> "$GITHUB_OUTPUT"

- if: ${{ matrix.toolchain == 'gcc' }}
id: toolchain-cache
name: Restore the toolchain cache
uses: actions/cache/restore@v4.2.2
with:
path: "${{ github.workspace }}/toolchain/tools_${{ matrix.arch }}_${{ matrix.platform }}_gcc"
key: "${{ runner.os }}-${{ runner.arch }}-${{ matrix.arch }}-${{ matrix.platform }}-${{ steps.toolchain-version.outputs.key }}"

- name: Build the platform toolchain
run: "${{ github.workspace }}/build.py --arch ${{ matrix.arch }} --platform ${{ matrix.platform }} --toolchain ${{ matrix.toolchain }} --no-tune-native --toolchain-only"

# Save right away rather than in a post step, which only runs if the
# whole job succeeds and would throw the toolchain away on a failed test
- if: ${{ matrix.toolchain == 'gcc' && steps.toolchain-cache.outputs.cache-hit != 'true' }}
name: Save the toolchain cache
uses: actions/cache/save@v4.2.2
with:
path: "${{ github.workspace }}/toolchain/tools_${{ matrix.arch }}_${{ matrix.platform }}_gcc"
key: "${{ runner.os }}-${{ runner.arch }}-${{ matrix.arch }}-${{ matrix.platform }}-gcc-${{ env.gcc-version }}"
key: ${{ steps.toolchain-cache.outputs.cache-primary-key }}

- name: Build platform toolchain & Hyper
- name: Build Hyper
run: "${{ github.workspace }}/build.py --arch ${{ matrix.arch }} --platform ${{ matrix.platform }} --toolchain ${{ matrix.toolchain }} --fetch-test-dependencies --no-tune-native --e9-debug-log=on"

- name: Build test kernel
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
cp build-clang-amd64-uefi/loader/hyper_uefi release/BOOTX64.EFI
cp build-clang-aarch64-uefi/loader/hyper_uefi release/BOOTAA64.EFI
cp build-clang-i686-bios/loader/hyper_iso_boot release/hyper_iso_boot
cp build-clang-i686-bios/loader/hyper_pxe release/hyper_pxe
cp build-clang-i686-bios/loader/hyper.pxe release/hyper.pxe
cp installer/hyper_install release/hyper_install

- name: Resolve release version
Expand Down
27 changes: 24 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3

import argparse
import os
Expand All @@ -18,6 +18,10 @@
"nasm",
"cmake",
],
"dnf": [
"nasm",
"cmake",
],
"brew": [
"nasm",
"cmake",
Expand All @@ -34,7 +38,8 @@
"xorriso",
"mtools",
"qemu-system-x86",
"qemu-system-aarch64",
# qemu-system-aarch64 is only a virtual package here
"qemu-system-arm",
],
"pacman": [
"parted",
Expand All @@ -47,12 +52,22 @@
"qemu-system-x86",
"qemu-system-aarch64",
],
"dnf": [
"parted",
"gdisk",
"python3-pytest",
"clang",
"lld",
"xorriso",
"mtools",
"qemu-system-x86",
"qemu-system-aarch64",
],
"brew": [
"llvm",
"lld",
"xorriso",
"mtools",
"gdisk",
"gptfdisk",
"qemu",
],
Expand Down Expand Up @@ -190,6 +205,8 @@ def main():
help="skip base dependencies")
parser.add_argument("--fetch-test-dependencies", action="store_true",
help="also fetch packages used for running the loader tests")
parser.add_argument("--toolchain-only", action="store_true",
help="build the toolchain and exit")
parser.add_argument("--reconfigure", action="store_true",
help="Reconfigure cmake before building")
parser.add_argument("--e9-debug-log", choices=["on", "off"],
Expand All @@ -206,6 +223,10 @@ def main():

args.arch, args.platform = pick_arch_and_platform(args.arch, args.platform)

if args.toolchain_only:
build_toolchain(args)
return

build_hyper(args)


Expand Down
2 changes: 1 addition & 1 deletion build_utils
2 changes: 1 addition & 1 deletion loader/arch/aarch64/handover_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void handover_prepare_for(struct handover_info *hi)

#define TCR_DS (1ull << 59)
#define TCR_HA (1ull << 39)
#define TCR_TG1_4K_GRANULE (0b10 << 30)
#define TCR_TG1_4K_GRANULE (0b10ull << 30)
#define TCR_TG0_4K_GRANULE (0b00 << 14)
#define TCR_SH1_SHIFT 28
#define TCR_ORGN1_SHIFT 26
Expand Down
2 changes: 1 addition & 1 deletion loader/arch/x86/bios/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ set(ISO_BOOT_REC_FULL_PATH "${MBR_PATH}/${ISO_BOOT_REC_BINARY}")
set(PXE_BOOT_REC_FULL_PATH "${MBR_PATH}/${PXE_BOOT_REC_BINARY}")
set(STAGE2_FULL_PATH "${STAGE2_PATH}/${STAGE2_BINARY}")
set(ISO_STAGE2_FULL_PATH "${STAGE2_PATH}/hyper_iso_boot")
set(PXE_STAGE2_FULL_PATH "${STAGE2_PATH}/hyper_pxe")
set(PXE_STAGE2_FULL_PATH "${STAGE2_PATH}/hyper.pxe")

add_custom_command(
OUTPUT
Expand Down
18 changes: 10 additions & 8 deletions loader/memory_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ void mme_insert(struct memory_map_entry *buf, struct memory_map_entry *me,

struct fixup_result {
bool lhs_type_changed;

// RHS was absorbed into LHS, its slot is now stale and must be skipped
bool rhs_consumed;

// Number of entries physically present in the buffer after the fixup
size_t new_count;
};

Expand Down Expand Up @@ -337,17 +342,14 @@ static void mm_overlap_fixup(
print_info("\n");
}

res->new_count = count - 1;

if (or.entries[0].type != lhs->type)
res->lhs_type_changed = true;
res->new_count = count;
res->lhs_type_changed = or.entries[0].type != lhs->type;
res->rhs_consumed = or.entry_count == 1;

*lhs = or.entries[0];

if (or.entry_count >= 2) {
if (or.entry_count >= 2)
*rhs = or.entries[1];
res->new_count++;
}

if (or.entry_count == 3) {
res->new_count += mme_insert_try_merge(buf + rhs_idx, &or.entries[2],
Expand Down Expand Up @@ -388,7 +390,7 @@ static size_t mm_do_fixup(
mm_overlap_fixup(buf, j, i, count, buf_cap, &fr, is_intentional);

// Both ranges collapsed into one, skip rhs
if (fr.new_count < count)
if (fr.rhs_consumed)
++i;

count = fr.new_count;
Expand Down
113 changes: 80 additions & 33 deletions loader/uefi/uefi_memory_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,61 +343,108 @@ const void *services_get_captured_uefi_map(size_t *out_size)
return uefi_map_capture_buf;
}

/*
* The UEFI firmware allows drivers to register an event listener for
* ExitBootServices() events via EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES.
*
* For example, EDK2 after 12c5ded ("FatPkg: Clean any volume caches during
* exit boot services") registers such a handler and flushes dirty FAT blocks
* back to disk, which causes a few allocations and frees to take place, thus
* invalidating the memory map key we're holding.
*
* This means we must retry ExitBootServices() in a loop, 2 should be enough
* for EDK2 specifically, but let's give some leeway for other implementations
* as well.
*/
#define EXIT_BOOT_SERVICES_MAX_ATTEMPTS 5

size_t services_release_resources(void *buf, size_t capacity, size_t elem_size,
mme_convert_t entry_convert)
{
SERVICE_FUNCTION();
struct string_view err_msg;
EFI_STATUS ret;
size_t i;
size_t i, attempt;

/*
* Only log errors after first call to GetMemoryMap,
* as WriteString() is allowed to allocate.
*/
logger_set_level(LOG_LEVEL_ERR);

/*
* Hand the kernel the exact map used for ExitBootServices() below: capture
* the raw EFI descriptors before converting the buffer to the native format
* in place. Probing calls (buf == NULL) never reach ExitBootServices(), so
* skip the copy for those. No allocation happens between here and
* ExitBootServices(), so the map key stays valid.
*/
acquire_efi_memory_map();
if (buf)
capture_raw_efi_memory_map();
efi_memory_map_fixup();
for (attempt = 1;; attempt++) {
void *out = buf;

/*
* Hand the kernel the exact map used for ExitBootServices() below:
* capture the raw EFI descriptors before converting the buffer to the
* native format in place. Probing calls (buf == NULL) never reach
* ExitBootServices(), so skip the copy for those. No allocation happens
* between here and ExitBootServices(), so the map key stays valid.
*/
acquire_efi_memory_map();
if (buf)
capture_raw_efi_memory_map();
efi_memory_map_fixup();

if (capacity < buf_entry_count) {
/*
* The firmware changed the map behind our back after a failed
* ExitBootServices(). Growing the buffer would still be allowed
* (it only takes memory allocation services), but the caller has
* already sized and laid out its buffer by now and doesn't expect
* to be asked again, so there's no recovering from this.
*/
if (unlikely(attempt > 1)) {
panic(
"memory map grew to %zu entries (capacity %zu) after "
"a failed ExitBootServices()\n", buf_entry_count, capacity
);
}

return buf_entry_count;
}

if (capacity < buf_entry_count)
return buf_entry_count;
/*
* The buffer is finally large enough, we can now destroy loader
* reclaimable memory if the protocol doesn't support it and
* transform it into MEMORY_TYPE_FREE safely as services are now
* disabled.
*/
buf_entry_count = mm_fixup(
memory_map_buf, buf_entry_count,
buf_byte_capacity / sizeof(struct memory_map_entry),
FIXUP_NO_PRESERVE_LOADER_RECLAIM
);

/*
* The buffer is finally large enough, we can now destroy loader
* reclaimable memory if the protocol doesn't support it and
* transform it into MEMORY_TYPE_FREE safely as services are now
* disabled.
*/
buf_entry_count = mm_fixup(memory_map_buf, buf_entry_count,
buf_byte_capacity / sizeof(struct memory_map_entry),
FIXUP_NO_PRESERVE_LOADER_RECLAIM);
for (i = 0; i < buf_entry_count; ++i) {
struct memory_map_entry *me = mm_entry_at(i);

for (i = 0; i < buf_entry_count; ++i) {
struct memory_map_entry *me = mm_entry_at(i);
if (entry_convert) {
entry_convert(me, out);
} else {
memcpy(out, me, sizeof(struct memory_map_entry));
}

if (entry_convert) {
entry_convert(me, buf);
} else {
memcpy(buf, me, sizeof(struct memory_map_entry));
out += elem_size;
}

buf += elem_size;
ret = g_st->BootServices->ExitBootServices(g_img, map_key);
if (!EFI_ERROR(ret))
break;

if (ret != EFI_INVALID_PARAMETER ||
attempt == EXIT_BOOT_SERVICES_MAX_ATTEMPTS)
goto ebs_failed;
}

ret = g_st->BootServices->ExitBootServices(g_img, map_key);
BUG_ON(EFI_ERROR(ret));
services_offline = true;

return buf_entry_count;

ebs_failed:
err_msg = uefi_status_to_string(ret);
panic("ExitBootServices() failed after %zu attempt(s): %pSV\n",
attempt, &err_msg);
}

void mm_foreach_entry(mme_foreach_t func, void *user)
Expand Down
2 changes: 1 addition & 1 deletion tests/disk_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3
import shutil
import os
import options
Expand Down
2 changes: 1 addition & 1 deletion tests/image_utils
Submodule image_utils updated 3 files
+1 −1 multipart.py
+1 −1 pxe.py
+54 −16 uefi.py
2 changes: 1 addition & 1 deletion tests/make_disk_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3
import argparse
import options
import disk_image as di
Expand Down
2 changes: 1 addition & 1 deletion tests/path_guesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def guess_path_to_hyper_iso_br():
def guess_path_to_hyper_pxe():
return _hyper_build_or_none(
["build-clang-i686-bios", "build-gcc-i686-bios"],
os.path.join("loader", "hyper_pxe"))
os.path.join("loader", "hyper.pxe"))


def guess_path_to_uefi_firmware(arch):
Expand Down
2 changes: 1 addition & 1 deletion tests/pxe_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3
"""
Hyper-specific wiring for a (simulated) PXE boot: assembles the normal boot
config plus the same module set the disk tests use, then hands it to the generic
Expand Down
2 changes: 1 addition & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ def _pxe_boot_image(getopt, is_uefi):
"""(source path, TFTP file name) of the image the firmware fetches."""
if is_uefi:
return getopt(options.X64_HYPER_UEFI_OPT), "BOOTX64.EFI"
return getopt(options.HYPER_PXE_OPT), "hyper_pxe"
return getopt(options.HYPER_PXE_OPT), "hyper.pxe"


@pytest.mark.parametrize("kernel_type,is_uefi", _PXE_PARAMS)
Expand Down
Loading