diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e63ee095..aa23e1e3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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) }} @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index df081cdf..3d5d95d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/build.py b/build.py index eb115b82..6754d1d0 100755 --- a/build.py +++ b/build.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import argparse import os @@ -18,6 +18,10 @@ "nasm", "cmake", ], + "dnf": [ + "nasm", + "cmake", + ], "brew": [ "nasm", "cmake", @@ -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", @@ -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", ], @@ -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"], @@ -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) diff --git a/build_utils b/build_utils index b68bd798..3f9222b2 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit b68bd798ada9e05fe5104b2dce4c78e996ef18c0 +Subproject commit 3f9222b292eb0e6c983445cd7e335eb4c4819de8 diff --git a/loader/arch/aarch64/handover_impl.c b/loader/arch/aarch64/handover_impl.c index f18d8094..57e87fcf 100644 --- a/loader/arch/aarch64/handover_impl.c +++ b/loader/arch/aarch64/handover_impl.c @@ -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 diff --git a/loader/arch/x86/bios/CMakeLists.txt b/loader/arch/x86/bios/CMakeLists.txt index 03389bde..1d562545 100644 --- a/loader/arch/x86/bios/CMakeLists.txt +++ b/loader/arch/x86/bios/CMakeLists.txt @@ -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 diff --git a/loader/memory_services.c b/loader/memory_services.c index fb931dab..f051ec31 100644 --- a/loader/memory_services.c +++ b/loader/memory_services.c @@ -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; }; @@ -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], @@ -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; diff --git a/loader/uefi/uefi_memory_services.c b/loader/uefi/uefi_memory_services.c index b6f1dac7..c8863af1 100644 --- a/loader/uefi/uefi_memory_services.c +++ b/loader/uefi/uefi_memory_services.c @@ -343,12 +343,28 @@ 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, @@ -356,48 +372,79 @@ size_t services_release_resources(void *buf, size_t capacity, size_t elem_size, */ 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) diff --git a/tests/disk_image.py b/tests/disk_image.py index 5fa26d72..b2dcb348 100644 --- a/tests/disk_image.py +++ b/tests/disk_image.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import shutil import os import options diff --git a/tests/image_utils b/tests/image_utils index 7951e768..291f7566 160000 --- a/tests/image_utils +++ b/tests/image_utils @@ -1 +1 @@ -Subproject commit 7951e768952ff5968ce51a1ceb09ca1d2fe25ab9 +Subproject commit 291f7566bfee42583f02d3aabd90046f3101b66e diff --git a/tests/make_disk_image.py b/tests/make_disk_image.py index 467955c6..63da921b 100755 --- a/tests/make_disk_image.py +++ b/tests/make_disk_image.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import argparse import options import disk_image as di diff --git a/tests/path_guesser.py b/tests/path_guesser.py index 33ea554e..6a2b27ba 100644 --- a/tests/path_guesser.py +++ b/tests/path_guesser.py @@ -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): diff --git a/tests/pxe_image.py b/tests/pxe_image.py index 9bf51c4a..b266c008 100644 --- a/tests/pxe_image.py +++ b/tests/pxe_image.py @@ -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 diff --git a/tests/test_loader.py b/tests/test_loader.py index de6a2f3a..097494e1 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -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)