Skip to content

arm: apple: keep Linux inside the CTXR executable window - #32

Merged
RyanTheTide merged 22 commits into
aurora-wipfrom
feat/ctxr-exec-window
Nov 7, 2026
Merged

RyanTheTide merged 22 commits into
aurora-wipfrom
feat/ctxr-exec-window

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Merged at 7e540a04d0. Two later commits (a revert and a doc note) are in #36.

A CONFIG_ARM64_CTXR_EXECMEM kernel (aurora-silicon/linux#985) confines its executable code to the window in /chosen/aurora,ctxr-exec-window (aurora-silicon/aura#77). If its image is outside the window, or the window is unusable, it keeps the secondary CPUs offline and boots on the boot CPU. It turns EFI runtime services off if any runtime code is outside the window or not aligned to its pages. Through U-Boot and GRUB, the image used to go wherever the EFI allocator or the stub's physical KASLR put it, and U-Boot's runtime code sat at the top of RAM.

This PR puts the kernel and U-Boot's runtime code inside the window, on every way U-Boot starts Linux here: bootefi, GRUB's LoadImage, systemd-stub UKIs, booti and bootm.

Ryan booted 50d879ac on a Neo with a real 128 MiB window, through GRUB, on linux#985 kernels a1a2ac0 and f6b1cbf (report). The commits after 50d879ac haven't booted on hardware, only under QEMU. sithglan booted an M5 on linux#985 (report) through the aura#77 chain. That report doesn't mention U-Boot and its pool is in one range, where a boot with this PR's runtime code in the window has two, so it isn't counted as a run of this PR. The QEMU results are below, and the list of what to measure on hardware is at the end.

Commits

The first six are the original series:

  • arm: apple: keep Linux inside the CTXR executable window places the kernel.
  • boot: relocate U-Boot's image into the CTXR window moves runtime services in.
  • efi_loader: allocate runtime code inside the CTXR window covers runtime code an EFI application allocates.
  • efi_loader: free all of a kernel placed in the CTXR window fixes the failure paths.
  • lmb: set the CTXR window aside until something needs it stops other allocations using it up.
  • efi_loader: keep a kernel a UKI loads itself in the CTXR window covers systemd-stub, which skips LoadImage.

The rest answer the review:

  • boot: ctxr: follow the kernel's window checks and warn instead of refusing matches linux#985: no aurora,ctxr-exec-enforce, and a window counts only with a non-zero, 16 KiB aligned base and size that don't wrap. U-Boot no longer refuses a kernel it can't place. Every path prints the same warning and starts it.
  • boot: ctxr: place U-Boot's image once the rest of its memory is reserved replaces the estimate of U-Boot's memory with the real bottom of its stack, and refuses a destination that overlaps the running image or the device tree.
  • lmb: keep the CTXR window in struct lmb and test it makes the window part of the state lmb_push() saves, and adds sandbox tests.
  • efi_loader: keep device-tree reservations out of the CTXR window stops a kernel being loaded over a reservation that only the tree handed to bootefi has.
  • boot: ctxr: give back the window place a failed booti took stops failed booti calls using up the window.
  • efi_loader: serve only a UKI's kernel from the CTXR window keys that path on the image asking having a .linux section, not on the size of the request.
  • efi_loader: give runtime code in the CTXR window whole 64 KiB blocks keeps runtime code aligned for any kernel page size.
  • efi_loader: free a PE image's pages when measuring it fails closes a leak.
  • doc: apple: describe the CTXR window support as it is.

A second review found these, fixed in:

  • efi_loader: warn for a UKI's zboot kernel and its runtime code: a UKI whose .linux is a zboot image got its loader code from the window and a success line, then its decompressor put the kernel elsewhere without a word. The UKI path also didn't say when U-Boot's runtime code is outside the window.
  • boot: bootm: don't reserve a load address the window holds: a uImage that loads inside the window failed with Unable to allocate memory.
  • boot: ctxr: treat an image larger than the window as not fitting: a huge size in an image header wrapped the fit checks.
  • boot: ctxr: keep U-Boot's image clear of the early gd and malloc area.
  • test: lmb: check that lmb_push() saves the CTXR window, which the old test couldn't catch, and a claim over several held regions.
  • doc: apple: say when U-Boot's image stays out of the CTXR window.
  • Revert "efi_loader: free only what runtime code in the CTXR window asked for" and doc: apple: note what freeing part of CTXR runtime code does, below.

efi_loader: free only what runtime code in the CTXR window asked for added a table of runtime code allocations so that a partial free stayed exact. It is in this merge (7e540a04d0); #36 reverts it and documents the limit, since nothing in the tree frees part of such an allocation.

What U-Boot takes as a window

The same as ctxr_parse_dt() and ctxr_window_valid() in linux#985 for a 16K-page kernel: four cells, base and size non-zero and page aligned, no wrap. U-Boot can't know the kernel's page size before it relocates itself, and Apple kernels use 16K pages. A 4K-page kernel takes every window U-Boot takes. A 64K-page kernel may reject one, and then boots on the boot CPU as with any window it rejects. Without a window none of the following happens, so M1 to M3 take the old path.

How the kernel gets placed

  • efi_load_pe(), which serves bootefi and the LoadImage call GRUB makes, recognises an arm64 Image by its header. It loads it at the lowest free 2 MiB boundary in the window and installs LINUX_EFI_LOADED_IMAGE_FIXED_GUID on the handle, the stub's existing way for a loader to say "run it where I put it". The EFI map stays accurate and the stub never looks for a new home.
  • booti_setup(), which booti and bootm both call, does the same search and reserves the place itself. A booti or bootm that succeeds never returns, so the next kernel placement, by booti, bootm or the EFI loader, first gives back a place an earlier one took.
  • systemd-stub loads a UKI's kernel into loader code it allocates and starts it without LoadImage. U-Boot serves loader code from the window when the image asking for it is a PE image with a .linux section, whatever the size. That is the UKI when systemd-boot or U-Boot started it, and also when shim runs it itself, since shim points its own loaded image protocol at the image it starts. U-Boot reads the Linux header at the start of .linux: for a zboot image it warns and allocates as usual, since the decompressor puts the real kernel wherever it likes.

Physical KASLR is off in this mode. The stub logs Image placement fixed by loader, or, for a UKI, EFI_RNG_PROTOCOL unavailable, and runs the image in place. Apple U-Boot has no DM_RNG, so it has no EFI_RNG_PROTOCOL. Virtual KASLR still works.

When the kernel can't be placed

U-Boot prints, on every path:

WARNING: CTXR exec window [<base>, <end>): <reason>
WARNING: Linux will run outside it, on the boot CPU only

and starts the kernel as it would without a window. The kernel then keeps its secondary CPUs offline and boots on the boot CPU. That happens when the window has no room or isn't RAM, for a UKI whose kernel doesn't fit, and for a zboot image, on its own or in a UKI, whose decompressor picks the kernel's address from the whole memory map. A size larger than the window counts as no room, so a huge size in a header can't wrap the checks. Refusing used to turn a boot on one CPU into no boot at all. Because booti_setup() no longer fails for the window, bootm can't turn it into a board reset either. bootm also no longer reserves a load address inside the window once the image has moved out of it.

How runtime services get in

A confined core can only fetch inside the window at EL2, and efi_rts_wq runs on any core. CPU0 isn't confined and EFI applications only run on it before ExitBootServices(), so only the kernel and EFI_RUNTIME_SERVICES_CODE need the window.

U-Boot's runtime code is inside its image, so the image relocates to the top of the window. That is decided in its own init call after reserve_stacks(), once every reservation below U-Boot's usual place is made, and only if the window ends below the bottom of the EFI stack. Only the image moves: its malloc area, EFI stack, device tree copy, MMU tables and globals stay where they would be without a window. It also stays where it is if its new place would overlap the running image, its relocations, the device tree in use, or the early gd and malloc area, if the window is smaller than the image or starts below RAM, or if the device tree reserves the top of the window. Its runtime code is then outside, U-Boot says so when it places a kernel, and Linux turns runtime services off.

In the EFI map, the first 64 KiB of the image is Runtime Code and the rest is Boot Services Code, so the kernel gets all but 64 KiB back for its pool. The image starts on a 64 KiB boundary and .efi_runtime is at 0x1b0 to 0xfd8 of it. add_u_boot_and_runtime() rounds the section to 64 KiB, so the region stays inside the image if the section grows.

Runtime code an EFI application allocates comes from the top of the window too, in whole 64 KiB blocks, as UEFI wants for AArch64. That region ends where U-Boot's begins, and the EFI map merges the two, so the merged region has to stay aligned too. When such memory is freed, U-Boot frees the block the range ends in, but not one it starts partway into, which is how efi_alloc_aligned_pages() trims an allocation. If the window has no room, or the caller's limit is below it, the code goes outside with a warning, and Linux turns runtime services off.

Keeping the window free

When LMB starts, it sets aside what is free of the window, after U-Boot's image and the device tree's reservations, with a flag of its own. Allocations go around it: board_late_init() allocates about 2.4 GiB top-down, and GRUB builds its heap from the largest free ranges in the EFI map. The EFI map reports the window as Boot Services Data, which an application can't allocate and the OS gets back at ExitBootServices().

Only kernel placement and runtime code take from it, each the range it needs. Freed pages go back, and so does a range whose reservation fails afterwards.

efi_carve_out_dt_rsv() adds the reservations of the tree an EFI payload gets to the EFI map but not to LMB. Giving up a window range rewrote that part of the EFI map to conventional memory, so the kernel could load on top of such a reservation. Each one now comes out of the window's free space as well.

Testing

Builds with no warnings: apple_m1_defconfig, and qemu_arm64_defconfig with the option on and off. Sandbox builds with only the warnings it had before. Two builds of apple_m1_defconfig with the same SOURCE_DATE_EPOCH are byte-identical: u-boot-nodtb.bin at 7791f7a1d6 (#36) has sha256 6de1f52ad92077f3145e455d6a9ebc31734144fc1097a411b5a0c1c6006abab9.

Sandbox unit tests, ut lib: all 10 existing LMB tests and the 4 new ones pass. The new ones set the window aside around existing reservations, check that allocations go around it, give up a range, set it aside again and see the regions merge back, try releases that don't fit or whose end wraps, find the top of it, set aside a window with no RAM behind part of it, check that lmb_push() saves and clears it, and take a reservation out of it, including one that spans several held regions. Removing the reset from lmb_setup(), or the wrap checks from the LMB functions, makes them fail.

QEMU virt (TCG, 2 CPUs, 8 GiB RAM, virtualization=on) booting linux#985 at its final head f6b1cbf1bc1, built with RANDOMIZE_BASE=y and 16K pages, with CONFIG_EFI_RNG_PROTOCOL=y in U-Boot so the stub would randomise if allowed. U-Boot and Linux run at EL2, because that kernel only takes a window at EL2. U-Boot has Apple's malloc size. The window is [0x40200000, +128 MiB).

All 61 scenarios pass: the 45 from before, with their expectations moved to the new kernel contract, and 16 new ones.

The 45 changed in these ways:

  • before-efi, before-grub, before-booti and before-uki44, U-Boot without the option or from before U-Boot moved into the window, now expect does not fit window, then unusable window: keeping the secondary CPUs offline, then init, where they expected a panic.
  • small-*, enforce-absent-efi, not-ram-* and zboot-enforce expect U-Boot's warning, the kernel outside the window on CPU0, and init, where they expected a refusal. The small-report-* trees still set aurora,ctxr-exec-enforce = <0>, and U-Boot ignores it.
  • base0* and malformed expect the kernel's malformed aurora,ctxr-exec-window line.
  • before-rt-secondary needed the kernel to leave runtime services on with runtime code outside an active window, which the kernel no longer does. It is now fetch-control, the plugin's negative control: the plugin is told a window 2 MiB short at the top, and has to count the secondary's fetches of U-Boot's runtime code as outside.
  • rt-alloc expects a runtime code request limited to below the window to land below it with a warning, and runtime code in the window to be 64 KiB aligned.
  • The kernel dropped its debugfs pool report, so the pool checks read its boot log: executable pool: 84 MiB usable in 2 range(s) and boot verify: ... 0 violation(s): PASS.
Path Result on this head
U-Boot to stub, no option Kernel at 0x234c00000, does not fit window, secondaries offline, reaches init
U-Boot to stub Kernel at 0x40200000, enforcing, pool 84 MiB in 2 ranges, 0 violations
U-Boot to GRUB 2.16, Fedora 44 and 45 GRUB, shim then GRUB, systemd-boot Same
Fedora 44 and 45 UKIs, and one with a 100 MiB initrd Same, after bytes of loader code at 0x40200000
booti Moved to 0x40200000, pool 84 MiB in 1 range
A UKI with a 4 MiB window Warning, kernel outside, CPU0 only, reaches init
Window base 4 KiB but not 16 KiB aligned, or a window that wraps U-Boot ignores it, the kernel calls it malformed

The new scenarios for the review findings, against the first six commits:

Scenario First six commits This head
bootefi with a tree that reserves the first 8 MiB of the window, which U-Boot's own doesn't Kernel at 0x40200000, on the reservation Kernel at 0x40a00000, the EFI map shows Reserved at 0x40200000
Two booti calls fail after placing the image, then a third 0x40200000, 0x42e00000, then no free 2 MiB aligned range All three at 0x40200000, the third boots
A booti fails after placing the image, then bootefi Not run Kernel at 0x40200000
Fedora 44 shim runs a 93 MiB UKI as its second stage The UKI takes the window, the kernel goes to 0x22bc00000, does not fit window Only the kernel's 43 MiB of loader code is in the window, at 0x40200000
An application keeps one page of runtime code [0x4809f000, 0x480b0000), runtime services off [0x48090000, 0x480b0000), runtime services on

And for the second review, against bb6e1e3005 where that was run:

Scenario bb6e1e3005 This head
A UKI whose .linux has a zboot header bytes of loader code at 0x40200000, no warning The warning, allocated outside the window, CPU0 only, reaches init
bootm of a uImage that loads at 0x44000000 Unable to allocate memory 0x44000000 for loading OS Moved to 0x40200000, enforcing, reaches init
Free the first page of a 128 KiB runtime code allocation, then allocate 64 KiB The new allocation lands on pages still in use The same, as the documented limit; freeing the rest gives the whole allocation back
An Image header with a huge image_size, through bootefi Not run The warning, kernel outside, CPU0 only, reaches init
The same, through booti Not run The warning, then booti's own Failed to allocate memory for the image, back to the prompt
A UKI with the top of the window reserved Not run Kernel in the window, U-Boot's runtime code is outside it, runtime services off
A 2 MiB window whose top holds the early gd and malloc area Not run U-Boot's image stays out, the warning, CPU0 only, reaches init
systemd-boot entry with a devicetree that reserves the window's start Not run See the limit below

The zboot UKI uses an Image with a patched zboot header, so it covers U-Boot's detection and warning. A real decompressor hasn't run through this path. That the decompressor allocates with EFI_ALLOCATE_ADDRESS comes from reading zboot.c and randomalloc.c in the kernel.

The Image comes from a build whose own after-build check the shared build VM cut off, and which the host's disk space then kept from being retried. It boots across all of these scenarios, and it is only the test vehicle. The U-Boot builds ran between clean checks of the guest.

On this head, init reads 10 variables through efivarfs on CPU1, with CPU0 offline and the efi_runtime workqueue limited to CPU1, and CPU1 fetches runtime code only from 0x480a0000, inside the window. With U-Boot from before it moved into the window, the kernel logs EFI runtime services off.

Other scenarios that still pass as before: a window high in a 3 GiB machine after board_late_init()'s allocations, a 100 MiB initrd through GRUB, bootefi, booti and a UKI, no property at all, a reservation at the start or the top of the window, a window at the top of RAM, and a window base that isn't 2 MiB aligned.

QEMU has no CTXR, so a TCG plugin records which physical pages each vCPU fetches privileged code from. It stands in for the hardware check on the secondary. QEMU proves the allocator paths and the kernel contract. Its plugin only compares addresses, so it can't show that the Neo lets a confined core run U-Boot's runtime code.

On the Neo at 50d879ac, Ryan's report has reserved exec window [0x10007200000, +128 MiB), enforcing, a pool of 53 MiB in 2 ranges with 74 MiB already reserved, boot verify: 1696 executable mapping(s), 0 violation(s): PASS, all 6 CPUs at EL2 through spin-table, kernel code and data inside the window, and 19 efivars. That is 50d879ac, not this head.

Known limits

  • efi_runtime.c accepts a SetVirtualAddressMap only with exactly one runtime code region. Runtime code an application keeps in the window ends where U-Boot's begins and merges with it in the EFI map, so the call succeeds, but U-Boot doesn't relocate the application's code. Outside the window it is a second region and the call fails. Nothing in the boot chain keeps runtime code.
  • A fixed-address allocation inside the window fails.
  • Kernel placement only steps around the device tree reservations U-Boot knows of when it places the kernel. booti places it before it reads the tree it boots with. systemd-boot installs the tree an entry's devicetree key names after LoadImage: under QEMU the kernel goes to 0x40200000, the reservation then lands on it, the stub logs FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region and moves it out of the window, and it boots on CPU0 with no warning from U-Boot.
  • After efi_loader: drop the CTXR runtime-code allocation table #36: freeing the start of a runtime code allocation in the window frees its whole first 64 KiB block. Nothing in the tree frees part of an allocation that way.
  • Runtime services on means what U-Boot's runtime offers. GetTime returns EFI_UNSUPPORTED. apple_m1_defconfig has EFI_RT_VOLATILE_STORE off, so U-Boot doesn't advertise SetVariable, and efivarfs mounts read-only. GetVariable works, 500 of 500 in Ryan's Neo run at 50d879ac. None of this comes from the window: the series doesn't touch efi_runtime.c.
  • A loader that loads the kernel as loader data, or from an image without a .linux section, misses the window with no warning, and the kernel boots on the boot CPU. An EFI_RNG_PROTOCOL in U-Boot would let the stub move a UKI's kernel to a random address outside it. Neither happens today. The complete fix would be for the kernel's EFI stub to move itself into the window.

To measure on hardware

Needs a padded boot object (aurora-installer#104), aura#77 with aura-ctxr-exec, and a kernel from linux#985. Don't pass efi=noruntime.

None of this has run on the commits after 50d879ac, so every item stands for this head. "Seen at 50d879ac" marks what Ryan's Neo report already shows for that commit.

  • U-Boot prints CTXR exec window [...]: Linux image at ... and the address is inside the armed CTXR_D. No WARNING: CTXR exec window line and no U-Boot's runtime code is outside it.
  • At the prompt, bdinfo shows relocaddr in the last MiB of the window, and sp start and TLB addr near the top of RAM. There is no out of memory.
  • In the lmb_dump_all part of bdinfo, the window is reserved[n] [<window base>-<relocaddr - 1>], flags: ctxr-window, no-overwrite then reserved[n+1] [<relocaddr>-<window end - 1>], flags: no-overwrite, and no other region starts inside it. kernel_addr_r, ramdisk_addr_r and loadaddr are outside it, and late_init: Failed to set run time variables does not appear.
  • The kernel logs reserved exec window ... enforcing, executable pool: ... and boot verify: ... 0 violation(s): PASS, /proc/iomem shows Kernel code in the window, and no line says it keeps the secondary CPUs offline. Seen at 50d879ac.
  • With efi=debug, the window at ExitBootServices holds Loader Code for the kernel, one 64 KiB Runtime Code, Boot Code for the rest of U-Boot's image and Boot Data for the free remainder, and nothing else. The initrd's Loader Data is outside the window.
  • No EFI runtime services off line, and /sys/firmware/efi/efivars lists variables, read-only. Seen at 50d879ac, without the per-CPU part that follows. Write each of CPU1 to CPU5 in turn to /sys/devices/virtual/workqueue/efi_runtime/cpumask and read a variable, with no oops. This is the check QEMU can't do.
  • All 6 CPUs come up at EL2 through spin-table into the window, and a module loads. Seen at 50d879ac.
  • Fedora's GRUB from the installed ESP reaches LoadImage, with a real initramfs and BLS entries. Linux image at in the CTXR exec window line shows it did. 50d879ac booted through GRUB; the report doesn't say which build or whether the entries were BLS.
  • A UKI logs bytes of loader code at with an address at the window's start, and boots. A kernel larger than the window's free space gets the warning and boots on the boot CPU.
  • The boot chain works when aura enters U-Boot at the window base at EL2 with no monitor. U-Boot relocates to the top and LMB does not reserve the old copy, so the kernel can load over it. This chain booted at 50d879ac.
  • Reboot and poweroff still work with runtime services on.
  • aurora_fallback boots through booti into the window.
  • All of the above at 128 MiB, which is what 50d879ac ran.

🤖 Generated with Claude Code

A kernel built with CONFIG_ARM64_CTXR_EXECMEM confines its executable
code to the physical window in /chosen/aurora,ctxr-exec-window and panics
early if its image is outside it. Only a kernel aura loads itself lands
there today. Through EFI, the image goes where the allocator or the
stub's physical KASLR puts it, and booti leaves it at its load address.

efi_load_pe() serves both bootefi and the LoadImage call GRUB makes, so
load an arm64 Image from there, at the lowest free 2 MiB boundary in the
window, and install LINUX_EFI_LOADED_IMAGE_FIXED_GUID on its handle. The
stub then skips physical randomisation and runs the image in place. The
EFI memory map stays accurate and other allocations are untouched.

booti_setup() does the same search for the aurora_fallback path. It
checks that the range is free RAM itself, because booti moves the image
before it reserves the memory.

A zboot image cannot be placed this way: its decompressor picks the
kernel's address from the whole memory map and ignores the protocol. No
Aurora kernel is built with EFI_ZBOOT, so refuse it.

If the image cannot be placed, print an error and refuse to start it
rather than boot a kernel that will panic. With
/chosen/aurora,ctxr-exec-enforce set to 0, warn and place it as usual.

Nothing changes when the property is absent or malformed. A base of 0
also changes nothing, because the kernel anchors that window on its own
image.

Signed-off-by: Diljit Singh <[email protected]>
DjDeveloperr added a commit to aurora-silicon/aura that referenced this pull request Nov 5, 2026
U-Boot can now place the kernel inside the window (aurora-silicon/u-boot#32),
so booting through U-Boot and GRUB no longer requires a kernel aura loads
itself. Record that, and that a zboot kernel is refused while enforcing.

Signed-off-by: Diljit Singh <[email protected]>
A core the window confines can only fetch inside it at EL2, and Linux calls
EFI runtime services from any core. U-Boot's runtime code is part of its image,
so relocate the image to the top of the window. A kernel that finds runtime
code outside it turns runtime services off.

Only the image moves. U-Boot's malloc area and EFI stack reserve about 80 MiB,
which with a 128 MiB window leaves little or no room for the kernel, and none of
that memory is executed once the OS runs. It stays where it would be without a
window, at the same addresses. Keep the end of the malloc area in gd, since
initr_malloc() can no longer find it from relocaddr.

The image's memory is Boot Services Code in the EFI map except for runtime
services, so the kernel gets it back for its executable pool at
ExitBootServices().

Leave the image where it is if the window isn't in RAM, can't hold it, runs into
the rest of U-Boot's memory or has a reservation at its top. Warn when placing
a kernel with runtime code outside the window. Nothing changes when the window
property is absent.

Signed-off-by: Diljit Singh <[email protected]>
An EFI runtime driver's image, or any other EFI_RUNTIME_SERVICES_CODE that an
application asks for, is code a confined core may have to run. Ask LMB for the
highest address inside the window for it, and fail rather than go below the
window when one is enforced. An application that names the address gets that
address.

Everything else stays out of the window: loader code and data, boot services
memory, an initrd and GRUB only run or get read on the unconfined boot core
before ExitBootServices(), so they can go anywhere.

Signed-off-by: Diljit Singh <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Adversarial review and regression run (CTXR lane, 06/11). Not merging yet: the series hasn't booted with a window on our Neo, see the end.

Tested

  • apple_m1_defconfig and apple_j873_defconfig, base aurora-wip vs this head, with -Wextra: both build, no new warnings. CTXR_EXEC_WINDOW=y on both.
  • Read every changed line against ctxr_parse_dt() in linux#985: same cell layout, same length check, size 0 rejected, enforce on when the property is missing, the wrong length or non-zero. With the property absent both paths return before doing anything, so M1 to M3 and every boot without a window are unchanged.

Should fix

  1. EFI runtime services end up off on every confined boot through U-Boot. Only the kernel image is placed in the window. U-Boot relocates itself, and its EFI runtime section, to the top of RAM. linux#985 d9f915b turns runtime services off when any EFI_RUNTIME_SERVICES_CODE region is outside the window, so efivarfs, efibootmgr, bootctl and systemd's Loader* variables stop working on these Macs. aura#77's comment in kboot.c ("U-Boot places itself and the kernel inside it") says otherwise. Either relocate the runtime section into the window, 16K-aligned, or document the loss and fix that comment.
  2. Other allocations can use up the window. board_late_init reserves about 2.4 GiB top-down with lmb_alloc, and U-Boot relocates to the top of RAM. Where the window sits high on a small-RAM Mac, these can leave no free 2 MiB range and the boot is refused. Reserving the whole window in lmb as soon as the property is seen, and releasing it only for the Image placement, would close this. Worth an lmb dump on the Neo.
  3. Loaders that don't call LoadImage bypass the placement: systemd-stub UKIs that load the inner kernel themselves, and possibly Fedora's patched GRUB. The kernel then lands outside the window and an enforcing kernel panics. The PR lists the general limit. It needs a concrete Fedora GRUB and UKI run on the Neo.

Nits

  • efi_image_loader.c: if efi_add_protocol() fails, the window pages leak. On a relocation failure the code frees virt_size pages but allocated max(virt_size, image_size).
  • Any PE with ARM\x64 at offset 0x38 gets window placement and FIXED_GUID. No privilege is gained, since every EFI image already runs at EL2 on the boot CPU; it only spends window space.

Merge order stays kernel, then this, then aura#77, then aurora-installer#104. Nothing here is unsafe without a window, but the lab Neo can't test it with one: both of its installs have the unpadded 1.7 MB Mach-O Stage 1 enrolled, so CTXR_D is far under 128 MiB, and enrolling a padded object is a persistent Stage 1 change that needs Ryan in Recovery.

A kernel placed in the window can be allocated larger than its PE size,
because its header's image_size counts too. A relocation failure freed only
the PE size, and unloading the image frees what loaded_image reports. Keep the
allocated size and use it for both.

If installing LINUX_EFI_LOADED_IMAGE_FIXED_GUID fails, free the window pages too.

Signed-off-by: Diljit Singh <[email protected]>
Top-down allocations reach the window if it sits high enough, such as
board_late_init()'s 2.4 GiB on a small-RAM Mac, or GRUB's heap on an EFI map
that shows the window as the largest free range. That can leave no 2 MiB range
for the kernel, and the boot is refused.

Reserve what's free of the window in LMB when LMB starts, with a flag of its
own so that a dump shows it. U-Boot's image and the device tree's reservations
are already there by then and stay as they are. The EFI map follows LMB and
reports the window as boot services data, which an application can't allocate
and the OS gets back at ExitBootServices().

Only three things take from it, each giving up just what it needs: the EFI
loader and booti for the kernel, and an allocation of runtime code. Freed pages
go back. Nothing changes when the window property is absent.

Signed-off-by: Diljit Singh <[email protected]>
systemd-stub loads the kernel in a UKI into loader code pages it allocates and
starts it without LoadImage, so it misses the placement in efi_load_pe(). The
kernel then lands outside the window, and an enforcing one panics.

A kernel is the only loader code that large an application asks for at an
address of U-Boot's choosing, so allocate those from the window. efi_load_pe()
doesn't come through here, so a UKI's own image stays outside. The EFI stub runs
the kernel in place because Apple U-Boot has no RNG, and it logs the placement.

Signed-off-by: Diljit Singh <[email protected]>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Thanks. Status at 50d879ac4154, a fast-forward. All 45 QEMU scenarios pass on it. Nothing has run on Apple hardware.

1. Stale. That describes 34832c1e1a39. From 37f6ad66d7, reserve_uboot() relocates U-Boot's image to the top of the window, and on apple_m1_defconfig board_init_f calls ctxr_exec_relocate_uboot. Runtime code is the image's first 64 KiB on a 64 KiB boundary, so the 16K check holds. In QEMU with a 16K kernel, efivars read on CPU1 with CPU0 offline, and a fetch-recording plugin saw no fetch outside the window on the secondary. The Apple figures come from reading the code and the binary. This also answers item 3 on aura#77.

2. Fixed in 51867ea612. LMB sets the window aside at startup with a ctxr-window flag. Only kernel placement and runtime code take from it. With board_late_init's allocation sizes, 3 GiB RAM and the window at 0x60000000, the old build refused the kernel and the new one boots. QEMU doesn't run board_late_init() itself: an EFI app made the same allocations. The bdinfo readings are in the checklist. A fixed-address allocation inside the window now fails.

3. Fedora's GRUB isn't a bypass, UKIs were. Fedora 44 and 45 GRUB, with or without shim, call LoadImage and boot in QEMU, no Secure Boot and a hand-written grub.cfg. Real BLS entries and a real Fedora kernel weren't run. systemd-stub 259.10 and 262.1 panicked, fixed in 50d879ac41 by serving large loader-code allocations from the window. That's a heuristic and relies on Apple U-Boot having no RNG. A loader that loads the kernel as loader data still bypasses it. The complete fix is the kernel stub moving itself into the window, which would be a linux#985 change. Limits are in the PR.

Nits. Both fixed in d510822f7e, read but not fault-injected. I left the ARM\x64 one, since you noted it gains no privilege.

…using

The kernel side of the CTXR window has changed. A kernel that finds its
image outside the window, or a window it can't use, no longer panics: it
keeps its secondary CPUs offline and boots on the boot CPU. It no longer
reads aurora,ctxr-exec-enforce, it treats a base of 0 as malformed, and
it takes only a page-aligned window that doesn't wrap.

Read the window the same way. Drop the enforce property, and take a
window only if its base and size are non-zero and 16K aligned, which is
the page size of Apple kernels, and it doesn't wrap. U-Boot can't know
the kernel's page size before it relocates itself. A 4K-page kernel takes
every window this takes. A 64K-page kernel may reject one, and then boots
on the boot CPU, as it would with any window it rejects.

Stop refusing to start a kernel U-Boot couldn't place. Refusing turned a
boot on one CPU into no boot at all, and only some paths did it: a UKI's
kernel already fell back silently. Every path now warns the same way,
twice and on the console, and carries on: the EFI loader and booti when
the window has no room, the EFI loader for a zboot image, the UKI path,
and runtime code an application allocates when the window has no room
for it. booti_setup() no longer fails for the window, so bootm can no
longer turn it into a board reset.

Signed-off-by: Diljit Singh <[email protected]>
ctxr_exec_relocate_uboot() ran from reserve_uboot() and guessed how much
memory would be reserved below U-Boot's usual place: malloc, the EFI
stack, the device tree and 1 MiB. That missed noncached memory and the
bloblist, which other boards reserve there.

Run it as its own init call after reserve_stacks() instead, when
gd->start_addr_sp is final and nothing has used gd->relocaddr yet, and
move the image only if the window ends below the bottom of the EFI
stack. The image's usual place stays reserved either way.

Also leave the image where it is if its new place overlaps what
relocate_code() reads while it copies, which is the running image and
its relocations, or the device tree U-Boot is using. The copy runs
forwards, so a destination above an overlapping source would corrupt it.

Signed-off-by: Diljit Singh <[email protected]>
The window LMB sets aside was a static of its own, so lmb_push() and
lmb_pop() didn't save it, and a unit test would have run against the
board's window. Keep it in struct lmb instead, cleared by lmb_setup(),
and set it with lmb_ctxr_window_init(). It only takes effect where the
window code or the unit tests are built in, so other boards still
compile the hold away.

Split lmb_release_ctxr_window_top() into lmb_ctxr_window_top(), which
finds the highest aligned range set aside and takes an alignment, and
lmb_release_ctxr_window(), so that a caller can check a candidate before
giving it up. If giving up a range frees it in LMB but the EFI map can't
follow, set it aside again.

Add sandbox tests for setting the window aside around what is already
reserved, allocations going around it, giving up and setting aside a
range with the regions merging back, releases that don't fit, finding
the top of it, and a window with no RAM behind part of it.

CTXR_EXEC_WINDOW itself stays ARM64 only: it relocates U-Boot and places
an arm64 Image, and the LMB part doesn't need it to be tested.

Signed-off-by: Diljit Singh <[email protected]>
LMB sets the window aside when it starts, from what the control device
tree reserves. The tree an EFI payload gets can reserve more:
efi_carve_out_dt_rsv() adds its reservations to the EFI map but not to
LMB. Placing the kernel then gives up the window range in LMB, which
rewrites that range of the EFI map to conventional memory, and the
check in efi_allocate_pages() never sees the reservation.

Under QEMU, bootefi with a tree whose /memreserve/ covers the first
8 MiB of the window, which U-Boot's own tree doesn't, loaded the kernel
at the window's base, on top of it.

Have efi_reserve_memory() take the reservation's pages out of the
window's free space with lmb_ctxr_window_claim(), which reserves them in
LMB without telling the EFI map, so that the type it was given stays.
Kernel placement and runtime code then step around it, and the same
boot loads the kernel 8 MiB in.

booti still only steps around what U-Boot's own tree reserves: it places
the kernel before it reads the tree it boots with.

Signed-off-by: Diljit Singh <[email protected]>
booti_setup() gave up a range of the window and booti_start() reserved
the image there. When the boot then failed, in bootm_find_images() or a
later state, the reservation stayed, and the next booti or EFI kernel
went to the next free 2 MiB boundary. Under QEMU, two booti calls with a
bad device tree address took 0x40200000 and 0x42e00000 of a 128 MiB
window, and a third, valid one found no room for a 43 MiB kernel.

Reserve the place in booti_setup() itself, which bootm also calls, and
remember it. A successful booti or bootm never returns, so whenever a
kernel is placed again, by booti, bootm or the EFI loader, the earlier
boot didn't start: give its place back to the window first. booti's own
reservation of the image merges with this one, and bootm, which only
reserves the load address, now has the moved image reserved too.

Signed-off-by: Diljit Singh <[email protected]>
The window took any loader code allocation of 16 MiB or more an
application asked for, as a stand-in for systemd-stub loading a UKI's
kernel. shim's own PE loader asks for its second stage that way too.
Under QEMU, Fedora's shim 16.1 loading a 93 MiB UKI put the UKI itself
in the window, left no room for systemd-stub's kernel, which U-Boot
then placed outside it without a word, and the kernel booted outside
its window.

Key on the image asking instead: serve the allocation from the window
only if the image its handle describes is a PE with a .linux section,
whatever its size. That is the UKI when systemd-boot or U-Boot started
it, and also when shim runs it itself, as shim points its own loaded
image protocol at the image it starts. A shim loading a UKI no longer
puts the UKI in the window, so systemd-stub's kernel goes there.

If the window can't take the kernel, say so with the same warning as
the other paths before falling back.

Signed-off-by: Diljit Singh <[email protected]>
Runtime code from the window was placed at the top of what the window
had free, 4 KiB aligned, which is directly below U-Boot's own runtime
code. The EFI map then merges the two into one region that starts on a
4 KiB boundary, and a 16K-page kernel turns runtime services off. Under
QEMU an application keeping one page of runtime code did exactly that.

Allocate runtime code in the window in whole 64 KiB blocks, as UEFI
wants for AArch64 runtime memory, so the region stays aligned for a
kernel of any page size, merged or not. When such memory is freed,
free the 64 KiB block the range ends in too, but not one it starts
partway into, which is how efi_alloc_aligned_pages() trims an aligned
allocation.

If the window has no room, or the caller's limit is below it, the
code still goes outside with a warning, as before.

If reserving the range the window gave up fails, or the EFI map can't
take it, set the range aside again rather than leave it free.

Signed-off-by: Diljit Singh <[email protected]>
efi_load_pe() returned the error from a failed TCG2 measurement without
freeing the image's pages. For a kernel in the CTXR window that was
window space lost until reset. Free them as the relocation failure
does, which also sets window pages aside again.

Signed-off-by: Diljit Singh <[email protected]>
The section still described the first commit of the series: only the
kernel in the window, a kernel that panics, an enforce property, a
window with base 0 that the kernel anchors on its image, and nothing
keeping other allocations out.

Describe what the window holds now: the kernel from the EFI loader,
booti and bootm, a UKI's kernel, and U-Boot's image with its runtime
code. Say how LMB and the EFI map keep the rest of it free, what U-Boot
takes as a window, what happens when the kernel doesn't fit, and what
it still misses. Update the Kconfig help the same way.

Signed-off-by: Diljit Singh <[email protected]>
A UKI whose kernel is an EFI zboot image got its loader code from the
window and the "bytes of loader code at" line, then its decompressor
allocated the real kernel with EFI_ALLOCATE_ADDRESS wherever it liked,
outside the window, with no warning.

Read the Linux header at the start of the UKI's .linux section. For a
zboot image, give the warning efi_load_pe() gives for one, and allocate
as usual.

The UKI path also didn't warn when U-Boot's runtime code is outside the
window, which only efi_load_pe() checked. Share that check, and test the
64 KiB region the EFI map gives runtime code rather than the section.

Signed-off-by: Diljit Singh <[email protected]>
bootm_load_os() copies a uImage to its load address, booti_setup() then
moves an arm64 Image into the CTXR window and reserves that place, and
bootm_load_os() goes on to reserve the load address. If the load address
is inside the window, LMB holds it and the reservation fails. Under
QEMU, a uImage loading at 0x44000000 with the window at
[0x40200000, 0x48200000) printed "Moving Image from 0x44000000 to
0x40200000" and then "Unable to allocate memory 0x44000000 for loading
OS".

The image no longer lives at its load address once it has moved, so
skip that reservation when booti_setup() put it in a place in the
window. Without the window nothing changes.

Signed-off-by: Diljit Singh <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Neo (J700) boot through this head (50d879ac): the kernel was placed in the CTXR window (code and data inside it), and EFI runtime stayed on. GetVariable passed 500/500. GetTime returns EFI_UNSUPPORTED, and efivarfs mounts read-only. Details: aurora-silicon/linux#985 (comment)

🤖 Generated with Claude Code

The searches for a place in the window compared base + size against the
window's end. A huge size from an image header, image_size in a Linux
header or text_offset + image_size for booti, wraps that sum and passes
the check, and LMB is then asked to give up or free a range that wraps
too.

Treat a size larger than the window, or than the held region being
tested, as no room: in efi_alloc_in_ctxr_window(), booti_setup(),
ctxr_exec_place_booti(), lmb_release_ctxr_window(),
lmb_ctxr_window_top(), and for runtime code. The LMB tests cover the
two LMB functions with such a size.

Signed-off-by: Diljit Singh <[email protected]>
Freeing runtime code in the window rounded the end of the range up to a
64 KiB block. That is right for the last block of an allocation, but
efi_free_pages(A, 1) on a larger allocation that starts at A freed the
whole first block, pages the caller still owned.

The EFI map merges neighbouring allocations, and this one with U-Boot's
own runtime code, so it can't say where an allocation ends. Keep a small
table of runtime code allocations in the window instead: where each
still starts, where what was asked for ends, and where its blocks end.
Free the blocks a range covers whole, and an allocation's last block
only once the range reaches the end of what was asked for. Trimming the
end of an aligned allocation, a whole free and a free in pieces then
each free exactly the blocks nobody uses. A free in the middle splits
the entry. With the table full, runtime code goes outside the window
with the usual warning.

Signed-off-by: Diljit Singh <[email protected]>
ctxr_exec_relocate_uboot() left U-Boot's image where it was if its new
place overlapped the running image, its relocations or the device tree,
but not the early global data and the pre-relocation malloc() area.
relocate_code() runs on the new stack and global data and doesn't read
them, but they are in use right up to it. Leave the image where it is
if it would overlap them too, from gd to the end of the early malloc()
area, rather than depend on that.

Signed-off-by: Diljit Singh <[email protected]>
The check that an LMB test starts with no window popped back to the
real LMB and pushed again, so it passed whether or not lmb_setup()
clears the window. Set a window in the test LMB instead, push, check it
is gone, pop, and check it is back.

Add a test of a claim that spans three regions set aside, split by two
reservations, and check that it takes each part and leaves the rest.

Signed-off-by: Diljit Singh <[email protected]>
The section said U-Boot's image relocates to the top of the window, but
ctxr_exec_relocate_uboot() leaves it where it is in several cases. List
them, and what follows for runtime services.

Say that a zboot kernel in a UKI is warned about too, that systemd-boot
installs an entry's devicetree after loading the kernel, as booti reads
its tree after placing it, and when freeing part of runtime code in the
window keeps a block.

Signed-off-by: Diljit Singh <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Neo (J700) boot through this head (bb6e1e3), 07/11. It ran as a one-shot aura-try trial under the padded Stage 1: aura#77 cda450d Stage 2, the fleet j700 DTB and this U-Boot. The live boot-ctxr.bin (50d879ac) stayed in place as the fallback.

  • Chain: Stage 1 spent the marker and booted the trial (chosen/aurora,trial set). SMBIOS reads aura 1.0.0b5-gcda450df / U-Boot 2026.07-gbb6e1e30053a, then GRUB.
  • Kernel: linux#985 273b9e8. Window [0x10007e00000, +128 MiB) enforcing, all 6 CPUs at EL2, boot verify 1696 mappings / 0 violations. The image is inside the window and the pool has 53 MiB.
  • EFI: runtime services on, 19 variables, efi: EFI v2.11 by Das U-Boot.
  • Regression: boot-time kernel warnings are the fleet baseline only. 0 AVC (enforcing), 0 failed units. Wi-Fi, eDP, GPU, KVM (nv) and Touch ID (enrolment restored) work.
  • Under load: the module and BPF stress phases passed on this boot (details on arm64: run Linux at EL2 with its code confined to a CTXR window linux#985).

Afterwards the Neo went back to the normal path (50d879ac) and the trial files were removed. The build has 0 warnings (apple_m1 config, CONFIG_CTXR_EXEC_WINDOW=y).

🤖 Generated with Claude Code

@RyanTheTide
RyanTheTide merged commit 62ee470 into aurora-wip Nov 7, 2026
2 checks passed
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Thanks for merging. Two commits landed on the branch after the merge, so they're in #36.

  • They revert the runtime-code allocation table (faafafd67a), since nothing frees part of such an allocation.
  • They document that limit: freeing the start of a runtime-code allocation in the window frees its whole first 64 KiB block.

QEMU 61/61, sandbox LMB 14/14.

🤖 Generated with Claude Code

scottjones added a commit to scottjones/omarchy-pkgs that referenced this pull request Nov 21, 2026
… window

On an M4 booting aura's Mach-O Stage 1, U-Boot at d30213e can place the
kernel outside the CTXR executable window, and the secondary CPUs stay
parked. aurora-silicon/u-boot#32 keeps it inside. Tested on a fresh
16-inch M4 Pro (J616s) install: 14 cores, Touch ID, external display.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scottjones added a commit to omacom/omarchy-mac-installer that referenced this pull request Nov 21, 2026
An image with the confined Stage 2 (esp/m1n1/boot-ctxr.bin) boots M4-and-later
Macs, whose kernel must run inside the CTXR window. U-Boot before
aurora-silicon/u-boot#32 places it wherever its allocator likes, and the kernel
then keeps every CPU but the boot CPU offline, as a J616s did on
uboot-aurora d30213e. Inspection's new ctxr-uboot check requires such an
image's U-Boot to read aurora,ctxr-exec-window; an image without the confined
Stage 2 passes it as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scottjones added a commit to omacom/omarchy-mac-installer that referenced this pull request Nov 21, 2026
An image with the confined Stage 2 (esp/m1n1/boot-ctxr.bin) boots M4-and-later
Macs, whose kernel must run inside the CTXR window. U-Boot before
aurora-silicon/u-boot#32 places it wherever its allocator likes, and the kernel
then keeps every CPU but the boot CPU offline, as a J616s did on
uboot-aurora d30213e. Inspection's new ctxr-uboot check requires such an
image's U-Boot to read aurora,ctxr-exec-window; an image without the confined
Stage 2 passes it as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scottjones added a commit to omacom/omarchy-mac-installer that referenced this pull request Nov 21, 2026
An image with the confined Stage 2 (esp/m1n1/boot-ctxr.bin) boots M4-and-later
Macs, whose kernel must run inside the CTXR window. U-Boot before
aurora-silicon/u-boot#32 places it wherever its allocator likes, and the kernel
then keeps every CPU but the boot CPU offline, as a J616s did on
uboot-aurora d30213e. Inspection's new ctxr-uboot check requires such an
image's U-Boot to read aurora,ctxr-exec-window; an image without the confined
Stage 2 passes it as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants