Re: [PATCH] x86/efi: Map firmware runtime regions before boot services regions

From: dev12124

Date: Sat Dec 05 2026 - 07:07:57 EST


Hi,

Analyzing the code, have a few points:

I. The good side

[ x ] - Why it would break during S4 (Hibernation)
During the resumption from hibernation, the new kernel must map the EFI Runtime Services regions (RTCode and RTData)
to the exact
same virtual addresses used in the previous session, ensuring that
`SetVirtualAddressMap()` and the firmware pointers remain valid.
[ x ] - The failure of the old algorithm
Since efi_map_regions() traversed the memory table from top to bottom, mapping everything in a single pass,
any change in the size of Boot Services regions (BSCode/BSData)—which the firmware might
allocate differently between a normal boot and an S4 wake—would shift all subsequent virtual addresses
of the Runtime regions, causing an immediate page fault in efi_call_rts().
[ x ] - The two-pass correction (pass 0 and pass 1)
Separating the loop to first map all pure runtime regions (pass == 0)
and only subsequently mapping the reserved boot services regions (pass == 1)
isolates the virtual layout of the runtime services.
II. The bad side
[ ] - Confusing and hard-to-read Boolean condition in the loop (pass == 0)
In line:

if (efi_is_firmware_runtime_region(md) != (pass == 0))
continue;

Comparing the return value of a `bool` function—or
an equality expression like `(pass == 0)`—using `!=` works in C (since both evaluate to 0 or 1), but it makes the code unnecessarily dense
and prone to errors during future maintenance. Using a clear `enum` (e.g., `PASS_RUNTIME`, `PASS_BOOT_SERVICES`)
or a direct check makes the intent
immediately obvious.
[ ] - Incompatibility of hibernation layout without a bump in RESTORE_MAGIC (Risk of silent regression)
Regarding your notes: why shouldn't the x86 `RESTORE_MAGIC` be incremented, or, alternatively, shouldn't a consistency
check be performed before calling `efi.runtime`?
[ ] - Check the impact of ordering when EFI_PROPERTIES_TABLE is present
Is this validated on firmwares that expose the EFI_PROPERTIES_TABLE? Given that the original interleaving between BS
(Boot Services) and RT (Runtime Services) can alter the 2M page alignment(EFI_VAR_START).

This is a good solution (but, with a `small bugs`).

Cheers,
João.