efi: Expose loaded image properties via info struct
Subsystems (8)
v4 cover efi: Expose loaded image properties via info struct
From: Ard Biesheuvel <[email protected]>
Clean up the arm64 build script and linker hacks that zboot uses to obtain properties of the loaded image (which it cannot access directly via a relative reference like the ordinary EFI stub).
Instead, define a generic struct efi_image_info, and expose its offset in the loaded image in a similar manner, but without relying on explicit linker script exports.
Populate the struct with section markers that all architecture use for loading the kernel image at boot, so that all the linker exports can be dropped for them.
Changes since v3:
- Populate efi_image_info for ARM correctly
- Fix a pre-existing issue in the ARM allocation logic
- Simplify the prototype of handle_kernel_image()
- Pass image_info pointer down to efi_kaslr_relocate_kernel() and simplify the callers
- Align RISC-V's __inittext_end correctly so it can be remapped.
- Add some more acks from Jason
Changes since v2:
- Add some additional fixes for RISC-V, which deviates from other architectures wrt its section markers. These patches have been sent out separately as well [0] as they will likely be taken through the RISC-V tree.
- Pass an efi_image_pointer to efi_stub_common() that points to the version inside the loaded image, so that its relative references accurately describe vmlinux in memory before boot.
- Avoid weak linkage hacks to derive the kernel entrypoint, and instead, always use the entrypoint passed via efi_image_info
- Use efi_image_info for sysfb_display_info so that efifb earlycon works much earlier on zboot
- Drop changes related to the previous bullet that are now no longer needed
- Add acks from Jason
Changes since v1:
- drop arm64 image header changes
- move efi_image_info out of __efistub_ scope so it can access vmlinux symbols directly
- implement in a generic manner for all architectures that support zboot
- address Sashiko comments
[0] https://lore.kernel.org/all/[email protected]/
Ard Biesheuvel (19):
efi/zboot: arm64: Add missing barrier after cache maintenance
riscv: Use _stext not _text to denote the start of kernel code
riscv: Make _text point to the start of the kernel image efi/riscv: Rename __init_text_end to __inittext_end like other archs efi/libstub: Avoid _end section marker in library code efi/libstub: arm: Size the decompressed kernel allocation precisely efi/libstub: Avoid reserve_addr/_size in efi_kaslr_relocate_kernel() efi/libstub: Capture loaded image information in struct efi_image_info efi/libstub: Pass kernel entrypoint down to efi_enter_kernel() efi/libstub: arm: Simplify handle_kernel_image() efi/libstub: Drop reserve_addr/_size params from handle_kernel_image() efi/libstub: arm64: Use struct efi_image_info to access image
properties
efi/libstub: loongarch: Use struct efi_image_info to access image
properties
efi/libstub: riscv: Use struct efi_image_info to access image
properties
efi/libstub: Make efi_remap_image() return efi_status_t efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image() efi/libstub: arm64: Drop zboot code_size hack
efi: Make config table based primary display handover ARM-only again Revert "efi: earlycon: Reprobe after parsing config tables"
arch/arm/boot/compressed/head.S | 3 +- arch/arm/include/asm/efi.h | 15 +--- arch/arm/kernel/efi.c | 27 ++++++ arch/arm64/boot/Makefile | 3 - arch/arm64/include/asm/efi.h | 2 - arch/arm64/kernel/efi-header.S | 2 + arch/arm64/kernel/image-vars.h | 14 --- arch/loongarch/kernel/efi.c | 20 +---- arch/loongarch/kernel/head.S | 4 +- arch/loongarch/kernel/image-vars.h | 6 -- arch/loongarch/kernel/vmlinux.lds.S | 1 - arch/riscv/include/asm/efi.h | 2 - arch/riscv/include/asm/sections.h | 2 +- arch/riscv/kernel/head.S | 1 + arch/riscv/kernel/image-vars.h | 8 -- arch/riscv/kernel/setup.c | 2 +- arch/riscv/kernel/vmlinux.lds.S | 6 +- drivers/firmware/efi/earlycon.c | 16 +--- drivers/firmware/efi/efi-init.c | 24 +----- drivers/firmware/efi/efi.c | 5 -- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/Makefile.zboot | 11 ++- drivers/firmware/efi/libstub/arm32-stub.c | 90 +++++++++++++++----- drivers/firmware/efi/libstub/arm64-stub.c | 37 ++------ drivers/firmware/efi/libstub/arm64.c | 23 +---- drivers/firmware/efi/libstub/efi-stub-entry.c | 33 +------ drivers/firmware/efi/libstub/efi-stub-helper.c | 15 ++-- drivers/firmware/efi/libstub/efi-stub.c | 26 ++++-- drivers/firmware/efi/libstub/efistub.h | 84 +++++++++++++----- drivers/firmware/efi/libstub/fdt.c | 5 +- drivers/firmware/efi/libstub/image_info.S | 19 +++++ drivers/firmware/efi/libstub/kaslr.c | 45 +++++----- drivers/firmware/efi/libstub/loongarch-stub.c | 21 ++--- drivers/firmware/efi/libstub/loongarch-stub.h | 4 - drivers/firmware/efi/libstub/loongarch.c | 11 +-- drivers/firmware/efi/libstub/primary_display.c | 56 ------------ drivers/firmware/efi/libstub/riscv-stub.c | 29 +------ drivers/firmware/efi/libstub/riscv.c | 16 +--- drivers/firmware/efi/libstub/zboot-decompress-gzip.c | 2 - drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 2 - drivers/firmware/efi/libstub/zboot.c | 28 +++++- drivers/firmware/efi/libstub/zboot.lds | 6 +- include/linux/efi.h | 1 - 43 files changed, 324 insertions(+), 406 deletions(-) create mode 100644 drivers/firmware/efi/libstub/image_info.S delete mode 100644 drivers/firmware/efi/libstub/loongarch-stub.h delete mode 100644 drivers/firmware/efi/libstub/primary_display.c
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 1/19 efi/zboot: arm64: Add missing barrier after cache maintenance
From: Ard Biesheuvel <[email protected]>
Prevent the I-cache from fetching stale data by issuing a barrier after the data cache maintenance completes but before performing the I-cache maintenance.
--- drivers/firmware/efi/libstub/arm64.c | 1 + 1 file changed, 1 insertion(+)
drivers/firmware/efi/libstub/arm64.c +1 -0
@@ -109,6 +109,7 @@ void efi_cache_sync_image(unsigned long image_base, }while (size >= lsize); } +dsb (ish); asm("ic ialluis" );dsb (ish);isb ();
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 2/19 riscv: Use _stext not _text to denote the start of kernel code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
On RISC-V, _text and _stext currently refer to the same location, i.e., the start of the .text output section, which follows the image header (.head.text) emitted by HEAD_TEXT_SECTION. _start is the symbol that refers to the actual start of the image.
On other architectures such as arm64, _text refers to the start of the image, and _stext to the start of the kernel code, and generic code (such as the EFI stub) may start relying on this distinction.
So switch the only user of _text in arch/riscv, i.e., the "Kernel code" resource that is exposed via /proc/iomem (and from which the "Kernel image" resource inherits its start address), over to _stext. This is a no-op given that both symbols currently resolve to the same address, but it permits _text to be moved to the start of the image, making it an alias of _start.
--- arch/riscv/kernel/setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/setup.c +1 -1
@@ -91,7 +91,7 @@ static int __init add_kernel_resources(void)* child nodes. */ - code_res.start =__pa_symbol (_text); + code_res.start =__pa_symbol (_stext); code_res.end =__pa_symbol (_etext) -1 ; code_res.flags =IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY ;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 3/19 riscv: Make _text point to the start of the kernel image
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
On RISC-V, _text currently refers to the start of the .text output section, which is placed after the image header (.head.text), and is therefore equal to _stext. On other architectures such as arm64, _text refers to the start of the image, and is used as such by generic code, e.g., to describe the kernel mapping in /proc/kcore and in the ELF headers of the vmcore, and by the EFI stub.
Now that arch/riscv no longer uses _text to refer to the start of the kernel code, move it to the start of the image so that it is equal to _start, and the distinction between _text and _stext is the same as on other architectures.
This also means that the image header is no longer omitted from the /proc/kcore and vmcore kernel text mappings. Other users of _text, such as kallsyms and ftrace, only use it as a reference point to calculate relative offsets, and are not affected by this change.
--- arch/riscv/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/vmlinux.lds.S +1 -1
@@ -28,11 +28,11 @@ SECTIONS /* Beginning of code and text segment */ . = LOAD_OFFSET; _start = .; + _text = .; HEAD_TEXT_SECTION . = ALIGN(PAGE_SIZE); .text : { - _text = .; _stext = .; TEXT_TEXT SCHED_TEXT
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 4/19 efi/riscv: Rename __init_text_end to __inittext_end like other archs
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Align RISC-V with arm64, LoongArch and x86, and drop the underscore between 'init' and 'text' in the section markers. This allows the value to be captured by generic EFI stub code that can be shared between all those architectures.
--- arch/riscv/include/asm/sections.h | 2 +- arch/riscv/kernel/image-vars.h | 2 +- arch/riscv/kernel/vmlinux.lds.S | 4 ++-- drivers/firmware/efi/libstub/riscv-stub.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
arch/riscv/include/asm/sections.h +1 -1
@@ -11,7 +11,7 @@extern char _start[];extern char _start_kernel[];extern char __init_data_begin[], __init_data_end[]; -extern char __init_text_begin[], __init_text_end[]; +extern char __inittext_begin[], __inittext_end[];extern char __alt_start[], __alt_end[];extern char __exittext_begin[], __exittext_end[];
arch/riscv/kernel/image-vars.h +1 -1
@@ -27,7 +27,7 @@ __efistub__start = _start; __efistub__start_kernel = _start_kernel; __efistub__end = _end; __efistub__edata = _edata; -__efistub___init_text_end = __init_text_end; +__efistub___inittext_end = __inittext_end;#if defined(CONFIG_EFI_EARLYCON ) || defined(CONFIG_SYSFB ) __efistub_sysfb_primary_display = sysfb_primary_display;#endif
arch/riscv/kernel/vmlinux.lds.S +2 -2
@@ -46,7 +46,7 @@ SECTIONS . = ALIGN(SECTION_ALIGN); __init_begin = .; - __init_text_begin = .; + __inittext_begin = .; .init.text : AT(ADDR(.init.text) - LOAD_OFFSET) ALIGN(SECTION_ALIGN) { \ _sinittext = .; \ INIT_TEXT \
@@ -72,12 +72,12 @@ SECTIONS } __exittext_end = .; - __init_text_end = .; . = ALIGN(SECTION_ALIGN); #ifdef CONFIG_EFI . = ALIGN(PECOFF_SECTION_ALIGNMENT); __pecoff_text_end = .; #endif + __inittext_end = .; /* Start of init data section */ __init_data_begin = .; INIT_DATA_SECTION(16)
drivers/firmware/efi/libstub/riscv-stub.c +1 -1
@@ -34,7 +34,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_status_t status; kernel_size = _edata - _start; - kernel_codesize = __init_text_end - _start; + kernel_codesize = __inittext_end - _start; kernel_memsize = kernel_size + (_end - _edata); *image_addr = (unsigned long )_start; *reserve_size = kernel_memsize;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 5/19 efi/libstub: Avoid _end section marker in library code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The _end marker could point to the end of the kernel image being loaded, or to the _end of the zboot decompressor image, and so it should only be used in code that is obviously only intended for the former case.
So instead, use kernel_memsize and the image address to obtain the same quantity.
--- drivers/firmware/efi/libstub/kaslr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/libstub/kaslr.c +1 -1
@@ -125,7 +125,7 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,if (!check_image_region (*image_addr, kernel_memsize)) {efi_err ("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n" ); }else if (IS_ALIGNED (*image_addr, min_kimg_align) && - (unsigned long )_end <EFI_ALLOC_LIMIT ) { + (*image_addr + kernel_memsize) <EFI_ALLOC_LIMIT ) {/* * Just execute from wherever we were loaded by the * UEFI PE/COFF loader if the placement is suitable.
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 6/19 efi/libstub: arm: Size the decompressed kernel allocation precisely
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The ARM EFI stub reserves a window for the decompressed kernel, as the decompressor writes it to memory without regard for other allocations made while running under the firmware. Currently, this window is MAX_UNCOMP_KERNEL_SIZE (32 MiB) in size, but it starts 5 pages below the kernel's start address, to cover the initial page tables. This leaves 32 MiB minus 5 pages for the kernel itself, and nothing checks whether that is sufficient. Large kernels (e.g., multiplatform builds with debug options enabled) can exceed this, in which case the decompressed image and its BSS overrun the reservation into memory that the firmware regards as free, and which may have been handed out for the FDT, the initrd or the EFI memory map.
So instead, size the window based on the actual memory footprint of the
kernel: the uncompressed image size, which is stored in the last 4 bytes of the compressed payload, plus the size of BSS, which is recorded in the zImage tag table, plus the size of the initial page tables, which is 4 pages, or 5 pages when LPAE is enabled.
While at it, drop the requirement that the window is aligned to a power of two that is at least TEXT_OFFSET, which means 4 MiB when TEXT_OFFSET is 0x208000 or 0x308000. The only actual requirement is that the physical start of the kernel minus TEXT_OFFSET, which becomes PHYS_OFFSET, is a multiple of 2 MiB, as required by the phys-to-virt patching code. The reason for the larger alignment was that the existing logic breaks down when the slack below the kernel exceeds the alignment, and so this is fixed by making the additional space that is allocated to accommodate the alignment the larger of 2 MiB and the size of the slack. This also ensures that PHYS_OFFSET never ends up below the 2 MiB aligned block containing the start of the allocation, which only requires DRAM to be 2 MiB aligned rather than 4 MiB aligned.
Finally, free the unused parts at either end of the allocation without updating alloc_base and alloc_size in place, so that both frees are derived directly from the original allocation, and return the reserved window to the caller as is.
--- arch/arm/include/asm/efi.h | 15 ++-------- drivers/firmware/efi/libstub/arm32-stub.c | 31 ++++++++++++-------- 2 files changed, 20 insertions(+), 26 deletions(-)
arch/arm/include/asm/efi.h +2 -13
@@ -56,22 +56,11 @@ void efi_virtmap_unload(void);/* arch specific definitions used by the stub code */ -/* -* A reasonable upper bound for the uncompressed kernel size is 32 MBytes, -* so we will reserve that amount of memory. We have no easy way to tell what -* the actuall size of code + data the uncompressed kernel will use. -* If this is insufficient, the decompressor will relocate itself out of the -* way before performing the decompression. -*/ -#define MAX_UNCOMP_KERNEL_SIZE SZ_32M -/* * phys-to-virt patching requires that the physical to virtual offset is a -* multiple of 2 MiB. However, using an alignment smaller than TEXT_OFFSET -* here throws off the memory allocation logic, so let's use the lowest power -* of two greater than 2 MiB and greater than TEXT_OFFSET. +* multiple of 2 MiB. */ -#define EFI_PHYS_ALIGN max(UL(SZ_2M), roundup_pow_of_two(TEXT_OFFSET)) +#define EFI_PHYS_ALIGN SZ_2M/* on ARM, the initrd should be loaded in a lowmem region */ static inline unsigned long efi_get_max_initrd_addr (unsigned long image_addr)
drivers/firmware/efi/libstub/arm32-stub.c +18 -13
@@ -76,6 +76,12 @@ void efi_handle_post_ebs_state(void) &efi_entry_state->sctlr_after_ebs); } +/* last unaligned u32 in the image carries the uncompressed size */ +extern u32 input_data_end[]__aligned(1) ; + +/* entry #3 in the zimage magic table carries the kernel's BSS size */ +extern u32 zimage_magic_table[] asm("_table_start" ); + efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr,
@@ -83,18 +89,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efi_loaded_image_t *image, efi_handle_t image_handle) { -const int slack =TEXT_OFFSET -5 *PAGE_SIZE ; -int alloc_size =MAX_UNCOMP_KERNEL_SIZE +EFI_PHYS_ALIGN ; +unsigned long kernel_memsize = input_data_end[-1 ] + zimage_magic_table[3 ]; +const int pgdir_size = (4 +IS_ENABLED (CONFIG_ARM_LPAE )) *PAGE_SIZE ; +int alloc_size = pgdir_size +ALIGN (kernel_memsize,EFI_PAGE_SIZE ); +const int slack =TEXT_OFFSET - pgdir_size; +const unsigned long margin =max (EFI_PHYS_ALIGN , slack);unsigned long alloc_base, kernel_base;efi_status_t status;/* * Allocate space for the decompressed kernel as low as possible. -* The region should be 16 MiB aligned, but the first 'slack' bytes +* The region should be 2 MiB aligned, but the first 'slack' bytes * are not used by Linux, so we allow those to be occupied by the * firmware. */ - status =efi_low_alloc_above (alloc_size,EFI_PAGE_SIZE , &alloc_base,0x0 ); + status =efi_low_alloc_above (alloc_size + margin,EFI_PAGE_SIZE , + &alloc_base,0x0 );if (status !=EFI_SUCCESS ) {efi_err ("Unable to allocate memory for uncompressed kernel.\n" );return status;
@@ -103,7 +113,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,if ((alloc_base %EFI_PHYS_ALIGN ) > slack) {/* * More than 'slack' bytes are already occupied at the base of -* the allocation, so we need to advance to the next 16 MiB block. +* the allocation, so we need to advance to the next 2 MiB block. */ kernel_base =round_up (alloc_base,EFI_PHYS_ALIGN ); efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
@@ -113,16 +123,11 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, } *reserve_addr = kernel_base + slack; - *reserve_size =MAX_UNCOMP_KERNEL_SIZE ; + *reserve_size = alloc_size;/* now free the parts that we will not use */ -if (*reserve_addr > alloc_base) { -efi_bs_call (free_pages, alloc_base, - (*reserve_addr - alloc_base) /EFI_PAGE_SIZE ); - alloc_size -= *reserve_addr - alloc_base; - } -efi_bs_call (free_pages, *reserve_addr +MAX_UNCOMP_KERNEL_SIZE , - (alloc_size -MAX_UNCOMP_KERNEL_SIZE ) /EFI_PAGE_SIZE ); +efi_free (*reserve_addr - alloc_base, alloc_base);/* no-op if 0 */ +efi_free (alloc_base + margin - *reserve_addr, *reserve_addr + alloc_size); *image_addr = kernel_base +TEXT_OFFSET ; *image_size =0 ;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 7/19 efi/libstub: Avoid reserve_addr/_size in efi_kaslr_relocate_kernel()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Avoid reserve_addr and reserve_size to carry the information about the allocated space for the relocated kernel, and instead, use a single alloc_size output parameter that is wired to image_size.
While at it, don't use the alloc_size as an in/out parameter, but only use it to return the allocated size (and leave it as 0x0 if the image is going to execute in place)
--- drivers/firmware/efi/libstub/arm64-stub.c | 3 +- drivers/firmware/efi/libstub/efistub.h | 3 +- drivers/firmware/efi/libstub/kaslr.c | 30 +++++++++----------- drivers/firmware/efi/libstub/riscv-stub.c | 4 +-- 4 files changed, 17 insertions(+), 23 deletions(-)
drivers/firmware/efi/libstub/arm64-stub.c +1 -2
@@ -35,10 +35,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, kernel_size = _edata - _text; kernel_codesize = __inittext_end - _text; kernel_memsize = kernel_size + (_end - _edata); - *reserve_size = kernel_memsize; *image_addr = (unsigned long )_text; -return efi_kaslr_relocate_kernel (image_addr, reserve_addr, reserve_size, +return efi_kaslr_relocate_kernel (image_addr, image_size, kernel_size, kernel_codesize, kernel_memsize,efi_kaslr_get_phys_seed (image_handle)); }
drivers/firmware/efi/libstub/efistub.h +1 -2
@@ -1244,8 +1244,7 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,void efi_remap_image (unsigned long image_base,unsigned alloc_size,unsigned long code_size); efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, - unsigned long *reserve_addr, - unsigned long *reserve_size, + unsigned long *alloc_size, unsigned long kernel_size, unsigned long kernel_codesize, unsigned long kernel_memsize,
drivers/firmware/efi/libstub/kaslr.c +14 -16
@@ -82,9 +82,8 @@ static bool check_image_region(u64 base, u64 size)/** * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled) -* @image_addr: Pointer to the current kernel location -* @reserve_addr: Pointer to the relocated kernel location -* @reserve_size: Size of the relocated kernel +* @image_addr: Pointer to variable carrying the image base address +* @alloc_size: Pointer to variable carrying the allocated size * @kernel_size: Size of the text + data * @kernel_codesize: Size of the text * @kernel_memsize: Size of the text + data + bss
@@ -97,13 +96,13 @@ static bool check_image_region(u64 base, u64 size)* Return: status code, EFI_SUCCESS if relocation is successful */ efi_status_t efi_kaslr_relocate_kernel (unsigned long *image_addr, -unsigned long *reserve_addr, -unsigned long *reserve_size, +unsigned long *alloc_size,unsigned long kernel_size,unsigned long kernel_codesize,unsigned long kernel_memsize,u32 phys_seed) { +unsigned long kernel_base = *image_addr;efi_status_t status;u64 min_kimg_align =efi_get_kimg_min_align ();
@@ -112,8 +111,8 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,* If KASLR is enabled, and we have some randomness available, * locate the kernel at a randomized offset in physical memory. */ - status =efi_random_alloc (*reserve_size, min_kimg_align, - reserve_addr, phys_seed, + status =efi_random_alloc (kernel_memsize, min_kimg_align, + image_addr, phys_seed,EFI_LOADER_CODE ,0 ,EFI_ALLOC_LIMIT );if (status !=EFI_SUCCESS )efi_warn ("efi_random_alloc() failed: 0x%lx\n" , status);
@@ -122,33 +121,32 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, }if (status !=EFI_SUCCESS ) { -if (!check_image_region (*image_addr, kernel_memsize)) { +if (!check_image_region (kernel_base, kernel_memsize)) {efi_err ("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n" ); - }else if (IS_ALIGNED (*image_addr, min_kimg_align) && - (*image_addr + kernel_memsize) <EFI_ALLOC_LIMIT ) { + }else if (IS_ALIGNED (kernel_base, min_kimg_align) && + (kernel_base + kernel_memsize) <EFI_ALLOC_LIMIT ) {/* * Just execute from wherever we were loaded by the * UEFI PE/COFF loader if the placement is suitable. */ - *reserve_size =0 ;return EFI_SUCCESS ; } - status =efi_allocate_pages_aligned (*reserve_size, reserve_addr, + status =efi_allocate_pages_aligned (kernel_memsize, image_addr,ULONG_MAX , min_kimg_align,EFI_LOADER_CODE );if (status !=EFI_SUCCESS ) {efi_err ("Failed to relocate kernel\n" ); - *reserve_size =0 ;return status; } } -memcpy ((void *)*reserve_addr, (void *)*image_addr, kernel_size); - *image_addr = *reserve_addr; +memcpy ((void *)*image_addr, (void *)kernel_base, kernel_size); + *alloc_size = kernel_memsize; +efi_icache_sync (*image_addr, *image_addr + kernel_codesize); -efi_remap_image (*image_addr, *reserve_size, kernel_codesize); +efi_remap_image (*image_addr, kernel_memsize, kernel_codesize);return status; }
drivers/firmware/efi/libstub/riscv-stub.c +1 -3
@@ -37,10 +37,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, kernel_codesize = __inittext_end - _start; kernel_memsize = kernel_size + (_end - _edata); *image_addr = (unsigned long )_start; - *reserve_size = kernel_memsize; - status =efi_kaslr_relocate_kernel (image_addr, - reserve_addr, reserve_size, + status =efi_kaslr_relocate_kernel (image_addr, image_size, kernel_size, kernel_codesize, kernel_memsize,efi_kaslr_get_phys_seed (image_handle));if (status !=EFI_SUCCESS )
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 8/19 efi/libstub: Capture loaded image information in struct efi_image_info
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Define a struct efi_image_info that carries properties of the loaded kernel image, and pass a pointer to it to efi_stub_common() so that EFI stub code can access these properties in a consistent manner for both the uncompressed kernel and the EFI zboot image.
Note that zboot can only access this information after decompressing the image, whereas code that is part of the EFI stub that gets integrated into Linux could just grab the efi_image_info pointer as needed.
However, in order to avoid future surprises, pass the efi_image_info pointer down via the calls into the shared stub code, replacing the image base address field, whose value can be derived from image_info as well as long as it is taken from the copy of the image that will ultimately be used to boot the kernel.
ARM implements neither, but -like x86- incorporates the EFI stub into its own decompressor and cannot incorporate struct efi_image_info as defined here, as it refers to global symbols in vmlinux. Assign the efi_image_info._text_offset field at runtime so that generic code can use efi_image_info to derive the base address of the loaded image. Other fields are not used on ARM so ignore those.
--- arch/arm64/kernel/efi-header.S | 2 + arch/loongarch/kernel/head.S | 1 + arch/riscv/kernel/head.S | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/firmware/efi/libstub/Makefile.zboot | 11 +++- drivers/firmware/efi/libstub/arm32-stub.c | 8 ++- drivers/firmware/efi/libstub/arm64-stub.c | 13 +---- drivers/firmware/efi/libstub/efi-stub-entry.c | 22 +++---- drivers/firmware/efi/libstub/efi-stub.c | 10 ++-- drivers/firmware/efi/libstub/efistub.h | 61 ++++++++++++++++++-- drivers/firmware/efi/libstub/image_info.S | 19 ++++++ drivers/firmware/efi/libstub/kaslr.c | 17 +++--- drivers/firmware/efi/libstub/loongarch-stub.c | 4 +- drivers/firmware/efi/libstub/riscv-stub.c | 12 +--- drivers/firmware/efi/libstub/zboot.c | 22 +++++-- drivers/firmware/efi/libstub/zboot.lds | 2 + 16 files changed, 148 insertions(+), 58 deletions(-)
arch/arm64/kernel/efi-header.S +2 -0
@@ -7,6 +7,8 @@ #include <linux/pe.h> #include <linux/sizes.h> +SYM_FUNC_ALIAS(efi_primary_entry, primary_entry) + .macro efi_signature_nop #ifdef CONFIG_EFI .L_head:
arch/loongarch/kernel/head.S +1 -0
@@ -35,6 +35,7 @@ pe_header: SYM_DATA(kernel_asize, .long _kernel_asize); SYM_DATA(kernel_fsize, .long _kernel_fsize); +SYM_FUNC_ALIAS(efi_primary_entry, kernel_entry) #endif __REF
arch/riscv/kernel/head.S +1 -0
@@ -28,6 +28,7 @@ SYM_CODE_START(_start) * that expects this header format!! */ #ifdef CONFIG_EFI +SYM_FUNC_ALIAS(efi_primary_entry, _start_kernel) /* * This instruction decodes to "MZ" ASCII required by UEFI. */
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -107,6 +107,7 @@ lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o targets := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y)) +lib-$(CONFIG_EFI_GENERIC_STUB) += image_info.o # Even when -mbranch-protection=none is set, Clang will generate a # .note.gnu.property for code-less object files (like lib/ctype.c),
drivers/firmware/efi/libstub/Makefile.zboot +10 -1
@@ -26,9 +26,18 @@ zboot-size-len-$(CONFIG_KERNEL_ZSTD) := 4 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE +$(call if_changed,$(zboot-method-y)) +# subtract the two symbol values to obtain the offset of struct efi_image_info +get_image_info_offset = $(shell $(NM) -n $(objtree)/vmlinux |\ + grep -E ' _text$$| __efistub_efistub_image_info$$' |\ + awk 'NR == 1 { a = toupper($$1) } \ + NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\ + bc) + # avoid eager evaluation to prevent references to non-existent build artifacts OBJCOPYFLAGS_vmlinuz.o = -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \ - --rename-section .data=.gzdata,load,alloc,readonly,contents + --rename-section .data=.gzdata,load,alloc,readonly,contents \ + --add-symbol efistub_image_info_offset=0x$(get_image_info_offset) + $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE $(call if_changed,objcopy)
drivers/firmware/efi/libstub/arm32-stub.c +7 -1
@@ -82,12 +82,15 @@ extern u32 input_data_end[] __aligned(1);/* entry #3 in the zimage magic table carries the kernel's BSS size */ extern u32 zimage_magic_table[] asm("_table_start" ); +struct efi_image_info efistub_image_info = {}; +efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size,unsigned long *reserve_addr,unsigned long *reserve_size,efi_loaded_image_t *image, -efi_handle_t image_handle) +efi_handle_t image_handle, +const struct efi_image_info **image_info) {unsigned long kernel_memsize = input_data_end[-1 ] + zimage_magic_table[3 ];const int pgdir_size = (4 +IS_ENABLED (CONFIG_ARM_LPAE )) *PAGE_SIZE ;
@@ -122,6 +125,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, kernel_base =round_down (alloc_base,EFI_PHYS_ALIGN ); } + efistub_image_info._text_offset = + kernel_base +TEXT_OFFSET - (u32)&efistub_image_info; + *reserve_addr = kernel_base + slack; *reserve_size = alloc_size;
drivers/firmware/efi/libstub/arm64-stub.c +3 -10
@@ -19,10 +19,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *reserve_addr, unsigned long *reserve_size, efi_loaded_image_t *image, - efi_handle_t image_handle) + efi_handle_t image_handle, + const struct efi_image_info **image_info) { - unsigned long kernel_size, kernel_codesize, kernel_memsize; - if (image->image_base != _text) { efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n"); image->image_base = _text;
@@ -32,13 +31,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_err ("FIRMWARE BUG: kernel image not aligned on %dk boundary\n" ,SEGMENT_ALIGN >>10 ); - kernel_size = _edata - _text; - kernel_codesize = __inittext_end - _text; - kernel_memsize = kernel_size + (_end - _edata); - *image_addr = (unsigned long )_text; - -return efi_kaslr_relocate_kernel (image_addr, image_size, - kernel_size, kernel_codesize, kernel_memsize, +return efi_kaslr_relocate_kernel (image_addr, image_size, image_info,efi_kaslr_get_phys_seed (image_handle)); }
drivers/firmware/efi/libstub/efi-stub-entry.c +8 -14
@@ -7,22 +7,17 @@#include "efistub.h" -static unsigned long kernel_image_offset; +extern const struct efi_image_info efistub_image_info; -static void *kernel_image_addr (void *addr) -{ -return addr + kernel_image_offset; -} - -struct sysfb_display_info *alloc_primary_display (void ) +struct sysfb_display_info * +alloc_primary_display (const struct efi_image_info *image_info) {if (IS_ENABLED (CONFIG_ARM ))return __alloc_primary_display (); -if (IS_ENABLED (CONFIG_X86 ) || -IS_ENABLED (CONFIG_EFI_EARLYCON ) || +if (IS_ENABLED (CONFIG_EFI_EARLYCON ) ||IS_ENABLED (CONFIG_SYSFB )) -return kernel_image_addr (&sysfb_primary_display); +return efistub_sysfb_display_info (image_info);return NULL ; }
@@ -36,6 +31,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, efi_system_table_t *systab) {static efi_guid_t loaded_image_proto =LOADED_IMAGE_PROTOCOL_GUID ; +const struct efi_image_info *image_info = &efistub_image_info;efi_loaded_image_t *image;efi_status_t status;unsigned long image_addr;
@@ -72,15 +68,13 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, status =handle_kernel_image (&image_addr, &image_size, &reserve_addr, &reserve_size, - image, handle); + image, handle, &image_info);if (status !=EFI_SUCCESS ) {efi_err ("Failed to relocate kernel\n" );return status; } - kernel_image_offset = image_addr - (unsigned long )image->image_base; - - status =efi_stub_common (handle, image, image_addr, cmdline_ptr); + status =efi_stub_common (handle, image, image_info, cmdline_ptr);efi_free (image_size, image_addr);efi_free (reserve_size, reserve_addr);
drivers/firmware/efi/libstub/efi-stub.c +6 -4
@@ -51,14 +51,15 @@ static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);void __weak free_primary_display (struct sysfb_display_info *dpy) { } -static struct sysfb_display_info *setup_primary_display (void ) +static struct sysfb_display_info * +setup_primary_display (const struct efi_image_info *image_info) {struct sysfb_display_info *dpy;struct screen_info *screen =NULL ;struct edid_info *edid =NULL ;efi_status_t status; - dpy =alloc_primary_display (); + dpy =alloc_primary_display (image_info);if (!dpy)return NULL ; screen = &dpy->screen;
@@ -160,9 +161,10 @@ efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)efi_status_t efi_stub_common (efi_handle_t handle,efi_loaded_image_t *image, -unsigned long image_addr, +const struct efi_image_info *image_info,char *cmdline_ptr) { +unsigned long image_addr =efistub_kernel_base (image_info);struct sysfb_display_info *dpy;efi_status_t status;
@@ -170,7 +172,7 @@ efi_status_t efi_stub_common(efi_handle_t handle,if (status !=EFI_SUCCESS )return status; - dpy =setup_primary_display (); + dpy =setup_primary_display (image_info);efi_retrieve_eventlog ();efi_bli_set_variables (image);
drivers/firmware/efi/libstub/efistub.h +55 -6
89 lines not on this page · raw message
drivers/firmware/efi/libstub/image_info.S new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <linux/init.h> + + __INITRODATA + + .p2align 3 + .globl __efistub_efistub_image_info +__efistub_efistub_image_info: +0: .long _text - . + .long __inittext_end - 0b + .long _edata - 0b + .long _end - 0b + .long efi_primary_entry - . +#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB) + .long sysfb_primary_display - . +#else + .long 0 +#endif
drivers/firmware/efi/libstub/kaslr.c +9 -8
@@ -84,9 +84,7 @@ static bool check_image_region(u64 base, u64 size)* efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled) * @image_addr: Pointer to variable carrying the image base address * @alloc_size: Pointer to variable carrying the allocated size -* @kernel_size: Size of the text + data -* @kernel_codesize: Size of the text -* @kernel_memsize: Size of the text + data + bss +* @image_info: Pointer to struct efi_image_info pointer * @phys_seed: Random seed used for the relocation * * If KASLR is not enabled, this function relocates the kernel to a fixed
@@ -97,14 +95,15 @@ static bool check_image_region(u64 base, u64 size)*/ efi_status_t efi_kaslr_relocate_kernel (unsigned long *image_addr,unsigned long *alloc_size, -unsigned long kernel_size, -unsigned long kernel_codesize, -unsigned long kernel_memsize, +const struct efi_image_info **image_info,u32 phys_seed) { -unsigned long kernel_base = *image_addr; +unsigned long kernel_base =efistub_kernel_base (*image_info); +unsigned long kernel_size =efistub_kernel_size (*image_info); +unsigned long kernel_codesize =efistub_kernel_codesize (*image_info); +unsigned long kernel_memsize =efistub_kernel_memsize (*image_info); +u64 min_kimg_align =efi_get_kimg_min_align ();efi_status_t status; -u64 min_kimg_align =efi_get_kimg_min_align ();if (IS_ENABLED (CONFIG_RANDOMIZE_BASE ) && phys_seed !=0 ) {/*
@@ -148,5 +147,7 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,efi_icache_sync (*image_addr, *image_addr + kernel_codesize);efi_remap_image (*image_addr, kernel_memsize, kernel_codesize); + *(unsigned long *)image_info += *image_addr - kernel_base; +return status; }
drivers/firmware/efi/libstub/loongarch-stub.c +3 -1
@@ -99,7 +99,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,unsigned long *reserve_addr, unsigned long *reserve_size, efi_loaded_image_t *image, - efi_handle_t image_handle) + efi_handle_t image_handle, + const struct efi_image_info **image_info) { efi_status_t status;unsigned long kernel_addr =0 ;
@@ -112,6 +113,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, *image_addr = kernel_addr; *image_size = kernel_asize; + *(unsigned long *)image_info += *image_addr - (unsigned long )image->image_base;return status; }
drivers/firmware/efi/libstub/riscv-stub.c +3 -9
@@ -28,18 +28,12 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,unsigned long *reserve_addr, unsigned long *reserve_size, efi_loaded_image_t *image, - efi_handle_t image_handle) + efi_handle_t image_handle, + const struct efi_image_info **image_info) { - unsigned long kernel_size, kernel_codesize, kernel_memsize; efi_status_t status; - kernel_size = _edata - _start; - kernel_codesize = __inittext_end - _start; - kernel_memsize = kernel_size + (_end - _edata); - *image_addr = (unsigned long )_start; - - status =efi_kaslr_relocate_kernel (image_addr, image_size, - kernel_size, kernel_codesize, kernel_memsize, + status =efi_kaslr_relocate_kernel (image_addr, image_size, image_info,efi_kaslr_get_phys_seed (image_handle));if (status !=EFI_SUCCESS )efi_err ("Failed to relocate kernel\n" );
drivers/firmware/efi/libstub/zboot.c +18 -4
@@ -26,9 +26,18 @@ void __weak efi_cache_sync_image(unsigned long image_base,// executable code loaded into memory to be safe for execution. } -struct sysfb_display_info *alloc_primary_display (void ) +struct sysfb_display_info * +alloc_primary_display (const struct efi_image_info *image_info) { -return __alloc_primary_display (); +return efistub_sysfb_display_info (image_info); +} + +static +const struct efi_image_info *zboot_get_image_info (unsigned long image_base) +{ +extern const u32 efistub_image_info_offset; + +return (void *)image_base + efistub_image_info_offset; } asmlinkage efi_status_t __efiapi
@@ -93,9 +102,14 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) }// Decompress the payload into the newly allocated buffer - status =efi_zboot_decompress ((void *)image_base, alloc_size) ?: -efi_stub_common (handle, image, image_base, cmdline_ptr); + status =efi_zboot_decompress ((void *)image_base, alloc_size); +if (status !=EFI_SUCCESS ) + goto error; + + auto image_info = zboot_get_image_info(image_base); + status =efi_stub_common (handle, image, image_info, cmdline_ptr); +error:efi_free (alloc_size, image_base);return status; }
drivers/firmware/efi/libstub/zboot.lds +2 -0
@@ -24,6 +24,8 @@ SECTIONS . = ALIGN(4); __efistub_code_size = .; LONG(zboot_code_size); + __efistub_efistub_image_info_offset = .; + LONG(efistub_image_info_offset); _etext = ALIGN(4096); . = _etext;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 9/19 efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Update the EFI stub code shared by arm64 and RISC-V so that the kernel entry point is taken from struct efi_image_info rather than from opaque arithmetic performed on linker symbols.
Notably, this results in EFI zboot to enter via the actual kernel entrypoint rather than byte 0x0 of the loaded image on these architectures. Beyond that, it is mostly a cleanup that will allow special linker exports for global symbols to be dropped in subsequent patches.
--- arch/arm/boot/compressed/head.S | 3 ++- arch/arm64/include/asm/efi.h | 2 -- arch/riscv/include/asm/efi.h | 2 -- drivers/firmware/efi/libstub/arm64-stub.c | 15 --------------- drivers/firmware/efi/libstub/arm64.c | 16 ++-------------- drivers/firmware/efi/libstub/efi-stub.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 13 +++++++------ drivers/firmware/efi/libstub/fdt.c | 5 +++-- drivers/firmware/efi/libstub/loongarch.c | 3 ++- drivers/firmware/efi/libstub/riscv-stub.c | 13 ------------- drivers/firmware/efi/libstub/riscv.c | 16 +++------------- 11 files changed, 20 insertions(+), 70 deletions(-)
arch/arm/boot/compressed/head.S +2 -1
@@ -1450,7 +1450,8 @@ __enter_kernel_from_hyp: b __enter_kernel ENTRY(efi_enter_kernel) - mov r4, r0 @ preserve image base + ldr r4, [r0] @ preserve image base + add r4, r4, r0 mov r8, r1 @ preserve DT pointer adr_l r0, call_cache_fn
arch/arm64/include/asm/efi.h +0 -2
@@ -103,8 +103,6 @@ static inline unsigned long efi_get_kimg_min_align(void)#define EFI_ALLOC_ALIGN SZ_64K#define EFI_ALLOC_LIMIT ((1UL << 48) - 1) -extern unsigned long primary_entry_offset (void ); -/* * On ARM systems, virtually remapped UEFI runtime services are set up in two * distinct stages:
arch/riscv/include/asm/efi.h +0 -2
@@ -43,8 +43,6 @@ static inline unsigned long efi_get_kimg_min_align(void)void arch_efi_call_virt_setup (void );void arch_efi_call_virt_teardown (void ); -unsigned long stext_offset (void ); -void efi_icache_sync (unsigned long start,unsigned long end);#endif /* _ASM_EFI_H */
drivers/firmware/efi/libstub/arm64-stub.c +0 -15
@@ -10,7 +10,6 @@#include <linux/efi.h> #include <asm/efi.h> #include <asm/memory.h> -#include <asm/sections.h> #include "efistub.h"
@@ -35,20 +34,6 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efi_kaslr_get_phys_seed(image_handle)); } -asmlinkage void primary_entry(void); - -unsigned long primary_entry_offset (void ) -{ -/* -* When built as part of the kernel, the EFI stub cannot branch to the -* kernel proper via the image header, as the PE/COFF header is -* strictly not part of the in-memory presentation of the image, only -* of the file representation. So instead, we need to jump to the -* actual entrypoint in the .text region of the image. -*/ -return (char *)primary_entry - _text; -} -void efi_icache_sync (unsigned long start,unsigned long end) {caches_clean_inval_pou (start, end);
drivers/firmware/efi/libstub/arm64.c +2 -14
@@ -117,24 +117,12 @@ void efi_cache_sync_image(unsigned long image_base,efi_remap_image (image_base, alloc_size, code_size); } -unsigned long __weak primary_entry_offset (void ) -{ -/* -* By default, we can invoke the kernel via the branch instruction in -* the image header, so offset #0. This will be overridden by the EFI -* stub build that is linked into the core kernel, as in that case, the -* image header may not have been loaded into memory, or may be mapped -* with non-executable permissions. -*/ -return 0 ; -} - -void __noreturn efi_enter_kernel (unsigned long entrypoint, +void __noreturn efi_enter_kernel (const struct efi_image_info *image_info,unsigned long fdt_addr,unsigned long fdt_size) {void (*__noreturn enter_kernel)(u64 ,u64 ,u64 ,u64 ); - enter_kernel = (void *)entrypoint +primary_entry_offset (); + enter_kernel =efistub_kernel_entry (image_info);enter_kernel (fdt_addr,0 ,0 ,0 ); }
drivers/firmware/efi/libstub/efi-stub.c +1 -1
@@ -191,7 +191,7 @@ efi_status_t efi_stub_common(efi_handle_t handle,install_memreserve_table (); - status =efi_boot_kernel (handle, image, image_addr, cmdline_ptr); + status =efi_boot_kernel (handle, image, image_info, cmdline_ptr);free_primary_display (dpy);
drivers/firmware/efi/libstub/efistub.h +7 -6
@@ -1054,9 +1054,6 @@ typedef efi_status_t (*efi_exit_boot_map_processing)(efi_status_t efi_exit_boot_services (void *handle,void *priv,efi_exit_boot_map_processing priv_func); -efi_status_t efi_boot_kernel (void *handle,efi_loaded_image_t *image, -unsigned long kernel_addr,char *cmdline_ptr); -void *get_fdt (unsigned long *fdt_size); efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
@@ -1189,6 +1186,10 @@ void *efistub_kernel_entry(const struct efi_image_info *image_info)return offset_to_ptr (&image_info->entry_offset); } +efi_status_t efi_boot_kernel (void *handle,efi_loaded_image_t *image, +const struct efi_image_info *image_info, +char *cmdline_ptr); +static inline void *efistub_sysfb_display_info (const struct efi_image_info *image_info) {
@@ -1205,9 +1206,9 @@ efi_status_t efi_stub_common(efi_handle_t handle,efi_status_t efi_handle_cmdline (efi_loaded_image_t *image,char **cmdline_ptr); -asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint, -unsigned long fdt_addr, -unsigned long fdt_size); +void __noreturn efi_enter_kernel (const struct efi_image_info *image_info, +unsigned long fdt_addr, +unsigned long fdt_size);void efi_handle_post_ebs_state (void );
drivers/firmware/efi/libstub/fdt.c +3 -2
@@ -344,7 +344,8 @@ efi_status_t allocate_new_fdt_and_exit_boot(void *handle, }efi_status_t efi_boot_kernel (void *handle,efi_loaded_image_t *image, -unsigned long kernel_addr,char *cmdline_ptr) +const struct efi_image_info *image_info, +char *cmdline_ptr) {unsigned long fdt_addr;efi_status_t status;
@@ -359,7 +360,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,if (IS_ENABLED (CONFIG_ARM ))efi_handle_post_ebs_state (); -efi_enter_kernel (kernel_addr, fdt_addr,fdt_totalsize ((void *)fdt_addr)); +efi_enter_kernel (image_info, fdt_addr,fdt_totalsize ((void *)fdt_addr));/* not reached */ }
drivers/firmware/efi/libstub/loongarch.c +2 -1
@@ -66,8 +66,9 @@ unsigned long __weak kernel_entry_address(unsigned long kernel_addr, }efi_status_t efi_boot_kernel (void *handle,efi_loaded_image_t *image, -unsigned long kernel_addr,char *cmdline_ptr) +const struct efi_image_info *image_info,char *cmdline_ptr) { +unsigned long kernel_addr =efistub_kernel_base (image_info);kernel_entry_t real_kernel_entry;struct exit_boot_struct priv;unsigned long desc_size;
drivers/firmware/efi/libstub/riscv-stub.c +0 -13
@@ -6,23 +6,10 @@#include <linux/efi.h> #include <asm/efi.h> -#include <asm/sections.h> #include <linux/unaligned.h> #include "efistub.h" -unsigned long stext_offset (void ) -{ -/* -* When built as part of the kernel, the EFI stub cannot branch to the -* kernel proper via the image header, as the PE/COFF header is -* strictly not part of the in-memory presentation of the image, only -* of the file representation. So instead, we need to jump to the -* actual entrypoint in the .text region of the image. -*/ -return _start_kernel - _start; -} - efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr,
drivers/firmware/efi/libstub/riscv.c +3 -13
@@ -72,20 +72,10 @@ efi_status_t check_platform_features(void)return EFI_SUCCESS ; } -unsigned long __weak stext_offset (void ) +void __noreturn efi_enter_kernel (const struct efi_image_info *image_info, +unsigned long fdt,unsigned long fdt_size) { -/* -* This fallback definition is used by the EFI zboot stub, which loads -* the entire image so it can branch via the image header at offset #0. -*/ -return 0 ; -} - -void __noreturn efi_enter_kernel (unsigned long entrypoint,unsigned long fdt, -unsigned long fdt_size) -{ -unsigned long kernel_entry = entrypoint +stext_offset (); -jump_kernel_func jump_kernel = (jump_kernel_func )kernel_entry; +jump_kernel_func jump_kernel =efistub_kernel_entry (image_info);/* * Jump to real kernel here with following constraints.
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 10/19 efi/libstub: arm: Simplify handle_kernel_image()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that ARM moved to efi_image_info for obtaining the base of the decompressed kernel image in efi_enter_kernel(), it no longer needs to distinguish between the image address (which might point into an unrelated allocation below the decompressed image) and the reservation address (which is what can be freed upon an error).
So use image_addr/image_size rather than reserve_addr/reserve_size in handle_kernel_image() to return the base and size of the allocated region to the caller. This will allow those parameters to be dropped entirely from the prototype in a subsequent patch.
--- drivers/firmware/efi/libstub/arm32-stub.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/firmware/efi/libstub/arm32-stub.c +5 -9
@@ -128,18 +128,14 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efistub_image_info._text_offset = kernel_base +TEXT_OFFSET - (u32)&efistub_image_info; - *reserve_addr = kernel_base + slack; - *reserve_size = alloc_size; + kernel_base += slack;/* now free the parts that we will not use */ -efi_free (*reserve_addr - alloc_base, alloc_base);/* no-op if 0 */ -efi_free (alloc_base + margin - *reserve_addr, *reserve_addr + alloc_size); +efi_free (kernel_base - alloc_base, alloc_base);/* no-op if 0 */ +efi_free (alloc_base + margin - kernel_base, kernel_base + alloc_size); - *image_addr = kernel_base +TEXT_OFFSET ; - *image_size =0 ; - -efi_debug ("image addr == 0x%lx, reserve_addr == 0x%lx\n" , - *image_addr, *reserve_addr); + *image_addr = kernel_base; + *image_size = alloc_size;return EFI_SUCCESS ; }
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 11/19 efi/libstub: Drop reserve_addr/_size params from handle_kernel_image()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The reserve_addr and reserve_size variable pointers passed to handle_kernel_image() are no longer used, so drop them from the caller and from the prototype.
--- drivers/firmware/efi/libstub/arm32-stub.c | 2 -- drivers/firmware/efi/libstub/arm64-stub.c | 2 -- drivers/firmware/efi/libstub/efi-stub-entry.c | 6 ------ drivers/firmware/efi/libstub/efistub.h | 2 -- drivers/firmware/efi/libstub/loongarch-stub.c | 2 -- drivers/firmware/efi/libstub/riscv-stub.c | 2 -- 6 files changed, 16 deletions(-)
drivers/firmware/efi/libstub/arm32-stub.c +0 -2
@@ -86,8 +86,6 @@ struct efi_image_info efistub_image_info = {};efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size, -unsigned long *reserve_addr, -unsigned long *reserve_size,efi_loaded_image_t *image,efi_handle_t image_handle,const struct efi_image_info **image_info)
drivers/firmware/efi/libstub/arm64-stub.c +0 -2
@@ -15,8 +15,6 @@efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size, -unsigned long *reserve_addr, -unsigned long *reserve_size,efi_loaded_image_t *image,efi_handle_t image_handle,const struct efi_image_info **image_info)
drivers/firmware/efi/libstub/efi-stub-entry.c +0 -6
@@ -36,9 +36,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,efi_status_t status;unsigned long image_addr;unsigned long image_size =0 ; -/* addr/point and size pairs for memory management*/ -unsigned long reserve_addr =0 ; -unsigned long reserve_size =0 ;WRITE_ONCE (efi_system_table, systab);
@@ -66,8 +63,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,efi_info ("Booting Linux Kernel...\n" ); status =handle_kernel_image (&image_addr, &image_size, - &reserve_addr, - &reserve_size, image, handle, &image_info);if (status !=EFI_SUCCESS ) {efi_err ("Failed to relocate kernel\n" );
@@ -77,7 +72,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, status =efi_stub_common (handle, image, image_info, cmdline_ptr);efi_free (image_size, image_addr); -efi_free (reserve_size, reserve_addr);return status; }
drivers/firmware/efi/libstub/efistub.h +0 -2
@@ -1150,8 +1150,6 @@ struct efi_image_info {*/ efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size, -unsigned long *reserve_addr, -unsigned long *reserve_size,efi_loaded_image_t *image,efi_handle_t image_handle,const struct efi_image_info **image_info);
drivers/firmware/efi/libstub/loongarch-stub.c +0 -2
@@ -96,8 +96,6 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size, -unsigned long *reserve_addr, -unsigned long *reserve_size,efi_loaded_image_t *image,efi_handle_t image_handle,const struct efi_image_info **image_info)
drivers/firmware/efi/libstub/riscv-stub.c +0 -2
@@ -12,8 +12,6 @@efi_status_t handle_kernel_image (unsigned long *image_addr,unsigned long *image_size, -unsigned long *reserve_addr, -unsigned long *reserve_size,efi_loaded_image_t *image,efi_handle_t image_handle,const struct efi_image_info **image_info)
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 12/19 efi/libstub: arm64: Use struct efi_image_info to access image properties
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.
This improves code reuse across architectures, and makes it easier to add new references in a generic manner.
--- arch/arm64/kernel/image-vars.h | 10 ---------- drivers/firmware/efi/libstub/arm64-stub.c | 8 +++++--- 2 files changed, 5 insertions(+), 13 deletions(-)
arch/arm64/kernel/image-vars.h +0 -10
@@ -20,8 +20,6 @@PROVIDE (pisym = sym); \ ASSERT((sym - KIMAGE_VADDR) < (__bss_start - KIMAGE_VADDR), #msg) -PROVIDE(__efistub_primary_entry = primary_entry); -/* * The EFI stub has its own symbol namespace prefixed by __efistub_, to * isolate it from the kernel proper. The following symbols are legally
@@ -32,14 +30,6 @@ PROVIDE(__efistub_primary_entry = primary_entry);* position independent manner */ PROVIDE (__efistub_caches_clean_inval_pou = __pi_caches_clean_inval_pou); - -PROVIDE (__efistub__text = _text); -PROVIDE (__efistub__end = _end); -PROVIDE (__efistub___inittext_end = __inittext_end); -PROVIDE (__efistub__edata = _edata); -#if defined(CONFIG_EFI_EARLYCON ) || defined(CONFIG_SYSFB ) -PROVIDE (__efistub_sysfb_primary_display = sysfb_primary_display); -#endif PROVIDE (__efistub__ctype = _ctype);PROVIDE (__pi___memcpy = __pi_memcpy);
drivers/firmware/efi/libstub/arm64-stub.c +5 -3
@@ -19,12 +19,14 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_handle_t image_handle, const struct efi_image_info **image_info) { -if (image->image_base != _text) { + unsigned long kernel_base = efistub_kernel_base(*image_info); + +if (image->image_base != (void *)kernel_base) {efi_err ("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n" ); - image->image_base = _text; + image->image_base = (void *)kernel_base; } -if (!IS_ALIGNED ((u64 )_text,SEGMENT_ALIGN )) +if (!IS_ALIGNED (kernel_base,SEGMENT_ALIGN ))efi_err ("FIRMWARE BUG: kernel image not aligned on %dk boundary\n" ,SEGMENT_ALIGN >>10 );
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 13/19 efi/libstub: loongarch: Use struct efi_image_info to access image properties
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.
This improves code reuse across architectures, and makes it easier to add new references in a generic manner.
--- arch/loongarch/kernel/head.S | 3 --- arch/loongarch/kernel/image-vars.h | 6 ------ arch/loongarch/kernel/vmlinux.lds.S | 1 - drivers/firmware/efi/libstub/loongarch-stub.c | 15 ++------------- drivers/firmware/efi/libstub/loongarch-stub.h | 4 ---- drivers/firmware/efi/libstub/loongarch.c | 10 +--------- 6 files changed, 3 insertions(+), 36 deletions(-)
arch/loongarch/kernel/head.S +0 -3
@@ -32,9 +32,6 @@ _head: pe_header: __EFI_PE_HEADER -SYM_DATA(kernel_asize, .long _kernel_asize); -SYM_DATA(kernel_fsize, .long _kernel_fsize); - SYM_FUNC_ALIAS(efi_primary_entry, kernel_entry) #endif
arch/loongarch/kernel/image-vars.h +0 -6
@@ -8,12 +8,6 @@#ifdef CONFIG_EFI_STUB __efistub_strcmp = strcmp; -__efistub_kernel_entry = kernel_entry; -__efistub_kernel_asize = kernel_asize; -__efistub_kernel_fsize = kernel_fsize; -#if defined(CONFIG_EFI_EARLYCON ) || defined(CONFIG_SYSFB ) -__efistub_sysfb_primary_display = sysfb_primary_display; -#endif #endif
arch/loongarch/kernel/vmlinux.lds.S +0 -1
@@ -159,7 +159,6 @@ SECTIONS /* header symbols */ _kernel_entry = ABSOLUTE(kernel_entry & PHYSADDR_MASK); _kernel_asize = ABSOLUTE(_end - _text); - _kernel_fsize = ABSOLUTE(_edata - _text); _kernel_vsize = ABSOLUTE(_end - __initdata_begin); _kernel_rsize = ABSOLUTE(_edata - __initdata_begin); #endif
drivers/firmware/efi/libstub/loongarch-stub.c +2 -13
@@ -8,11 +8,6 @@#include <asm/efi.h> #include <asm/addrspace.h> #include "efistub.h" -#include "loongarch-stub.h" - -extern int kernel_asize; -extern int kernel_fsize; -extern int kernel_entry;/** * efi_relocate_kernel() - copy memory area
@@ -100,6 +95,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_handle_t image_handle, const struct efi_image_info **image_info) { + int kernel_asize = efistub_kernel_memsize(*image_info); +int kernel_fsize =efistub_kernel_size (*image_info);efi_status_t status;unsigned long kernel_addr =0 ;
@@ -114,11 +111,3 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, *(unsigned long *)image_info += *image_addr - (unsigned long )image->image_base;return status; } - -unsigned long kernel_entry_address (unsigned long kernel_addr, -efi_loaded_image_t *image) -{ -unsigned long base = (unsigned long )image->image_base; - -return (unsigned long )&kernel_entry - base + kernel_addr; -}
drivers/firmware/efi/libstub/loongarch-stub.h deleted +0 -4
@@ -1,4 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ - -unsigned long kernel_entry_address (unsigned long kernel_addr, -efi_loaded_image_t *image);
drivers/firmware/efi/libstub/loongarch.c +1 -9
@@ -8,7 +8,6 @@#include <asm/efi.h> #include <asm/addrspace.h> #include "efistub.h" -#include "loongarch-stub.h" typedef void __noreturn (*kernel_entry_t )(bool efi,unsigned long cmdline,unsigned long systab);
@@ -59,16 +58,9 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)return EFI_SUCCESS ; } -unsigned long __weak kernel_entry_address (unsigned long kernel_addr, -efi_loaded_image_t *image) -{ -return *(unsigned long *)(kernel_addr +8 ) -PHYSADDR (VMLINUX_LOAD_ADDRESS ) + kernel_addr; -} -efi_status_t efi_boot_kernel (void *handle,efi_loaded_image_t *image,const struct efi_image_info *image_info,char *cmdline_ptr) { -unsigned long kernel_addr =efistub_kernel_base (image_info);kernel_entry_t real_kernel_entry;struct exit_boot_struct priv;unsigned long desc_size;
@@ -99,7 +91,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,csr_write (CSR_DMW2_INIT ,LOONGARCH_CSR_DMWIN2 );csr_write (CSR_DMW3_INIT ,LOONGARCH_CSR_DMWIN3 ); - real_kernel_entry = (void *)kernel_entry_address (kernel_addr, image); + real_kernel_entry =efistub_kernel_entry (image_info);real_kernel_entry (true, (unsigned long )cmdline_ptr, (unsigned long )efi_system_table);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 14/19 efi/libstub: riscv: Use struct efi_image_info to access image properties
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.
This improves code reuse across architectures, and makes it easier to add new references in a generic manner.
--- arch/riscv/kernel/image-vars.h | 8 -------- 1 file changed, 8 deletions(-)
arch/riscv/kernel/image-vars.h +0 -8
@@ -23,14 +23,6 @@* linked at. The routines below are all implemented in assembler in a * position independent manner */ -__efistub__start = _start; -__efistub__start_kernel = _start_kernel; -__efistub__end = _end; -__efistub__edata = _edata; -__efistub___inittext_end = __inittext_end; -#if defined(CONFIG_EFI_EARLYCON ) || defined(CONFIG_SYSFB ) -__efistub_sysfb_primary_display = sysfb_primary_display; -#endif /* * These double-word integer shifts are used by the library code, and
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 15/19 efi/libstub: Make efi_remap_image() return efi_status_t
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
efi_remap_image() uses the EFI Memory Attributes Protocol to remap code and data regions with the appropriate permissions. This could fail for a variety of regions, all of which are currently ignored.
So relay the failure status code if any occur when invoking the protocol. Locating the protocol itself is allowed to fail, as remapping the image is likely unnecessary in the first place if the protocol does not exist.
--- drivers/firmware/efi/libstub/efi-stub-helper.c | 15 ++++++++++----- drivers/firmware/efi/libstub/efistub.h | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +10 -5
@@ -697,9 +697,11 @@ efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)* region of the loaded image read-only/executable, and the remainder * read-write/non-executable. The code region is assumed to start at the base * of the image, and will therefore cover the PE/COFF header as well. +* +* Return: EFI_SUCCESS on success, other EFI status codes on failure. */ -void efi_remap_image (unsigned long image_base,unsigned alloc_size, -unsigned long code_size) +efi_status_t efi_remap_image (unsigned long image_base,unsigned alloc_size, +unsigned long code_size) {static efi_guid_t guid =EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID ;efi_memory_attribute_protocol_t *memattr;
@@ -710,10 +712,12 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,* If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's * invoke it to remap the text/rodata region of the decompressed image * as read-only and the data/bss region as non-executable. +* Don't bother if the protocol does not exist: it means all memory is +* read-write-exec anyway. */ status =efi_bs_call (locate_protocol, &guid,NULL , (void **)&memattr);if (status !=EFI_SUCCESS ) -return ; +return EFI_SUCCESS ;// Get the current attributes for the entire region status = memattr->get_memory_attributes(memattr, image_base,
@@ -721,7 +725,7 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,if (status !=EFI_SUCCESS ) {efi_warn ("Failed to retrieve memory attributes for image region: 0x%lx\n" , status); -return ; +return status; }// Mark the code region as read-only
@@ -729,7 +733,7 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size, EFI_MEMORY_RO);if (status !=EFI_SUCCESS ) {efi_warn ("Failed to remap code region read-only\n" ); -return ; +return status; }// If the entire region was already mapped as non-exec, clear the
@@ -749,4 +753,5 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,if (status !=EFI_SUCCESS )efi_warn ("Failed to remap data region non-executable\n" ); } +return status; }
drivers/firmware/efi/libstub/efistub.h +2 -2
@@ -1291,8 +1291,8 @@ struct efi_smbios_type4_record {const u8 *__efi_get_smbios_string (const struct efi_smbios_record *record,const u8 *offset); -void efi_remap_image (unsigned long image_base,unsigned alloc_size, -unsigned long code_size); +efi_status_t efi_remap_image (unsigned long image_base,unsigned alloc_size, +unsigned long code_size); efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, unsigned long *alloc_size, const struct efi_image_info **image_info,
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 16/19 efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Refactor efi_cache_sync_image() so that it is not in charge of calling efi_remap_image(), which is really the wrong way around.
Instead, call efi_remap_image() from the primary zboot procedure, where it is more straight-forward to obtain the code size from the loaded image.
--- drivers/firmware/efi/libstub/arm64.c | 2 -- drivers/firmware/efi/libstub/zboot-decompress-gzip.c | 2 -- drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 2 -- drivers/firmware/efi/libstub/zboot.c | 14 +++++++++++++- 4 files changed, 13 insertions(+), 7 deletions(-)
drivers/firmware/efi/libstub/arm64.c +0 -2
@@ -113,8 +113,6 @@ void efi_cache_sync_image(unsigned long image_base, asm("ic ialluis" );dsb (ish);isb (); - -efi_remap_image (image_base, alloc_size, code_size); } void __noreturn efi_enter_kernel(const struct efi_image_info *image_info,
drivers/firmware/efi/libstub/zboot-decompress-gzip.c +0 -2
@@ -62,7 +62,5 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)return EFI_LOAD_ERROR ; } -efi_cache_sync_image ((unsigned long )out, outlen); -return EFI_SUCCESS ; }
drivers/firmware/efi/libstub/zboot-decompress-zstd.c +0 -2
@@ -43,7 +43,5 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)return EFI_LOAD_ERROR ; } -efi_cache_sync_image ((unsigned long )out, outlen); -return EFI_SUCCESS ; }
drivers/firmware/efi/libstub/zboot.c +13 -1
@@ -40,6 +40,17 @@ const struct efi_image_info *zboot_get_image_info(unsigned long image_base)return (void *)image_base + efistub_image_info_offset; } +static efi_status_t efi_zboot_remap_image (const struct efi_image_info *image_info, +unsigned long image_base, +unsigned long image_size) +{ +unsigned long code_size =efistub_kernel_codesize (image_info); + +efi_cache_sync_image (image_base, code_size); + +return efi_remap_image (image_base, image_size, code_size); +} + asmlinkage efi_status_t __efiapiefi_zboot_entry (efi_handle_t handle,efi_system_table_t *systab) {
@@ -107,7 +118,8 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) goto error; auto image_info = zboot_get_image_info(image_base); - status =efi_stub_common (handle, image, image_info, cmdline_ptr); + status =efi_zboot_remap_image (image_info, image_base, alloc_size) ?: +efi_stub_common (handle, image, image_info, cmdline_ptr); error:efi_free (alloc_size, image_base);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 17/19 efi/libstub: arm64: Drop zboot code_size hack
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that the code size can be obtained via the generic image_info struct, there is no longer a need to inject it explicitly during the build.
--- arch/arm64/boot/Makefile | 3 --- arch/arm64/kernel/image-vars.h | 4 ---- drivers/firmware/efi/libstub/arm64.c | 4 +--- drivers/firmware/efi/libstub/zboot.lds | 4 ---- 4 files changed, 1 insertion(+), 14 deletions(-)
arch/arm64/boot/Makefile +0 -3
@@ -51,7 +51,4 @@ EFI_ZBOOT_BFD_TARGET := elf64-littleaarch64 EFI_ZBOOT_MACH_TYPE := ARM64 EFI_ZBOOT_FORWARD_CFI := $(CONFIG_ARM64_BTI_KERNEL) -EFI_ZBOOT_OBJCOPY_FLAGS = --add-symbol zboot_code_size=0x$$( \ - $(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1) - include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
arch/arm64/kernel/image-vars.h +0 -4
@@ -140,10 +140,6 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);#endif /* CONFIG_KVM */ -#ifdef CONFIG_EFI_ZBOOT -_kernel_codesize =ABSOLUTE (__inittext_end - _text); -#endif -/* * LLD will occasionally error out with a '__init_end does not converge' error * if INIT_IDMAP_DIR_SIZE is defined in terms of _end, as this results in a
drivers/firmware/efi/libstub/arm64.c +1 -3
@@ -88,8 +88,6 @@ efi_status_t check_platform_features(void)#define DCTYPE "cvau"#endif -u32 __weak code_size; -void efi_cache_sync_image (unsigned long image_base,unsigned long alloc_size) {
@@ -100,7 +98,7 @@ void efi_cache_sync_image(unsigned long image_base,/* only perform the cache maintenance if needed for I/D coherency */ if (!(ctr &BIT (CTR_EL0_IDC_SHIFT ))) {unsigned long base = image_base; -unsigned long size = code_size; +unsigned long size = alloc_size; do { asm("dc " DCTYPE ", %0" :: "r"(base));
drivers/firmware/efi/libstub/zboot.lds +0 -4
@@ -2,8 +2,6 @@ ENTRY(__efistub_efi_zboot_header); -PROVIDE(zboot_code_size = ABSOLUTE(0)); - SECTIONS { .head : ALIGN(4096) {
@@ -22,8 +20,6 @@ SECTIONS *(.rodata* .init.rodata* .srodata*) . = ALIGN(4); - __efistub_code_size = .; - LONG(zboot_code_size); __efistub_efistub_image_info_offset = .; LONG(efistub_image_info_offset);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 18/19 efi: Make config table based primary display handover ARM-only again
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
This reverts the effect of commit 732ea9db9d8a ("efi: libstub: Move screen_info handling to common code"), taking into account the changes that were made to the code since.
That commit moved all architectures to the ARM approach of passing the primary display information (formerly struct screen_info) from the EFI stub to the core kernel via an EFI configuration table, so that the zboot decompressor, which is not linked into the core kernel image, could pass this information as well.
Now that the EFI stub and the zboot decompressor both have access to the location of sysfb_primary_display inside the loaded kernel image via struct efi_image_info, they can populate it directly, and the configuration table is only needed on ARM, whose decompressor has no such information.
So make the configuration table based handover ARM-only again:
- move the config table based alloc/free routines into the ARM stub, and provide __weak defaults in the generic stub that return a pointer to the kernel image's copy of sysfb_primary_display;
- drop the primary display table from the common config table list, and move the code that copies it into sysfb_primary_display into the ARM arch code;
- on the other architectures, revert to simply reserving the framebuffer memory if the stub populated sysfb_primary_display.
This makes the EFI framebuffer earlycon available right away when booting via zboot, rather than only after the EFI config tables have been parsed.
--- arch/arm/kernel/efi.c | 27 ++++++++++ arch/loongarch/kernel/efi.c | 20 ++----- drivers/firmware/efi/efi-init.c | 24 ++------- drivers/firmware/efi/efi.c | 5 -- drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/arm32-stub.c | 41 ++++++++++++++ drivers/firmware/efi/libstub/efi-stub-entry.c | 13 ----- drivers/firmware/efi/libstub/efi-stub.c | 14 +++++ drivers/firmware/efi/libstub/efistub.h | 1 - drivers/firmware/efi/libstub/primary_display.c | 56 -------------------- drivers/firmware/efi/libstub/zboot.c | 6 --- 11 files changed, 90 insertions(+), 119 deletions(-)
arch/arm/kernel/efi.c +27 -0
@@ -7,6 +7,7 @@#include <linux/memblock.h> #include <linux/screen_info.h> #include <linux/sizes.h> +#include <linux/sysfb.h> #include <asm/efi.h> #include <asm/mach/map.h>
@@ -91,13 +92,36 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)return 0 ; } +static unsigned long __initdata primary_display_table =EFI_INVALID_TABLE_ADDR ;static unsigned long __initdata cpu_state_table =EFI_INVALID_TABLE_ADDR ;const efi_config_table_type_t efi_arch_tables[]__initconst = { + {LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID , &primary_display_table}, {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID , &cpu_state_table}, {} }; +static void __init load_primary_display_table (void ) +{ +struct sysfb_display_info *dpy; + +if (primary_display_table ==EFI_INVALID_TABLE_ADDR ) +return ; + + dpy =early_memremap (primary_display_table,sizeof (*dpy)); +if (!dpy) { +pr_err ("Could not map primary_display config table\n" ); +return ; + } + sysfb_primary_display = *dpy; +memset (dpy,0 ,sizeof (*dpy)); +early_memunmap (dpy,sizeof (*dpy)); + +if (memblock_is_map_memory (sysfb_primary_display.screen.lfb_base)) +memblock_mark_nomap (sysfb_primary_display.screen.lfb_base, + sysfb_primary_display.screen.lfb_size); +} +static void __init load_cpu_state_table (void ) {if (cpu_state_table !=EFI_INVALID_TABLE_ADDR ) {
@@ -136,6 +160,9 @@ void __init arm_efi_init(void) {efi_init (); +if (IS_ENABLED (CONFIG_SYSFB ) ||IS_ENABLED (CONFIG_FIRMWARE_EDID )) +load_primary_display_table (); +/* ARM does not permit early mappings to persist across paging_init() */ efi_memmap_unmap ();
arch/loongarch/kernel/efi.c +3 -17
@@ -72,8 +72,6 @@ bool efi_poweroff_required(void) (acpi_gbl_reduced_hardware || acpi_no_s5); } -unsigned long __initdata primary_display_table =EFI_INVALID_TABLE_ADDR ; -#if defined(CONFIG_SYSFB ) || defined(CONFIG_EFI_EARLYCON ) struct sysfb_display_info sysfb_primary_display __section(".data");EXPORT_SYMBOL_GPL (sysfb_primary_display);
@@ -81,22 +79,10 @@ EXPORT_SYMBOL_GPL(sysfb_primary_display);static void __init init_primary_display (void ) { -struct sysfb_display_info *dpy; - -if (primary_display_table ==EFI_INVALID_TABLE_ADDR ) -return ; - - dpy =early_memremap (primary_display_table,sizeof (*dpy)); -if (!dpy) { -pr_err ("Could not map primary_display config table\n" ); -return ; - } - sysfb_primary_display = *dpy; -memset (dpy,0 ,sizeof (*dpy)); -early_memunmap (dpy,sizeof (*dpy)); +struct screen_info *si = &sysfb_primary_display.screen; -memblock_reserve (__screen_info_lfb_base (&sysfb_primary_display.screen), - sysfb_primary_display.screen.lfb_size); +if (si->orig_video_isVGA ==VIDEO_TYPE_EFI ) +memblock_reserve (__screen_info_lfb_base (si), si->lfb_size); }void __init efi_init (void )
drivers/firmware/efi/efi-init.c +4 -20
@@ -23,8 +23,6 @@#include <asm/efi.h> -unsigned long __initdata primary_display_table =EFI_INVALID_TABLE_ADDR ; -static int __init is_memory (efi_memory_desc_t *md) {if (md->attribute & (EFI_MEMORY_WB |EFI_MEMORY_WT |EFI_MEMORY_WC ))
@@ -67,25 +65,11 @@ EXPORT_SYMBOL_GPL(sysfb_primary_display);static void __init init_primary_display (void ) { -struct sysfb_display_info *dpy; - -if (primary_display_table !=EFI_INVALID_TABLE_ADDR ) { - dpy =early_memremap (primary_display_table,sizeof (*dpy)); -if (!dpy) { -pr_err ("Could not map primary_display config table\n" ); -return ; - } - sysfb_primary_display = *dpy; -memset (dpy,0 ,sizeof (*dpy)); -early_memunmap (dpy,sizeof (*dpy)); +struct screen_info *si = &sysfb_primary_display.screen; -if (memblock_is_map_memory (sysfb_primary_display.screen.lfb_base)) -memblock_mark_nomap (sysfb_primary_display.screen.lfb_base, - sysfb_primary_display.screen.lfb_size); - -if (IS_ENABLED (CONFIG_EFI_EARLYCON )) -efi_earlycon_reprobe (); - } +if (si->orig_video_isVGA ==VIDEO_TYPE_EFI && +memblock_is_map_memory (si->lfb_base)) +memblock_mark_nomap (si->lfb_base, si->lfb_size); }static int __init uefi_init (u64 efi_system_table)
drivers/firmware/efi/efi.c +0 -5
@@ -57,8 +57,6 @@ unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;static unsigned long __initdata rt_prop =EFI_INVALID_TABLE_ADDR ;static unsigned long __initdata initrd =EFI_INVALID_TABLE_ADDR ; -extern unsigned long primary_display_table; -struct mm_struct efi_mm = { .mm_mt =MTREE_INIT_EXT (mm_mt,MM_MT_FLAGS , efi_mm.mmap_lock), .mm_users =ATOMIC_INIT (2 ),
@@ -674,9 +672,6 @@ static const efi_config_table_type_t common_tables[] __initconst = {#endif #ifdef CONFIG_UNACCEPTED_MEMORY {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" }, -#endif -#ifdef CONFIG_EFI_GENERIC_STUB - {LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID, &primary_display_table }, #endif {}, };
drivers/firmware/efi/libstub/Makefile +1 -1
@@ -81,7 +81,7 @@ $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE $(call if_changed_rule,cc_o_c) lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ - primary_display.o efi-stub-entry.o + efi-stub-entry.o lib-$(CONFIG_ARM) += arm32-stub.o lib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o
drivers/firmware/efi/libstub/arm32-stub.c +41 -0
53 lines not on this page · raw message
drivers/firmware/efi/libstub/efi-stub-entry.c +0 -13
@@ -9,19 +9,6 @@extern const struct efi_image_info efistub_image_info; -struct sysfb_display_info * -alloc_primary_display (const struct efi_image_info *image_info) -{ -if (IS_ENABLED (CONFIG_ARM )) -return __alloc_primary_display (); - -if (IS_ENABLED (CONFIG_EFI_EARLYCON ) || -IS_ENABLED (CONFIG_SYSFB )) -return efistub_sysfb_display_info (image_info); - -return NULL ; -} -/* * EFI entry point for the generic EFI stub used by ARM, arm64, RISC-V and * LoongArch. This is the entrypoint that is described in the PE/COFF header
drivers/firmware/efi/libstub/efi-stub.c +14 -0
@@ -48,6 +48,20 @@static u64 virtmap_base =EFI_RT_VIRTUAL_BASE ;static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET !=0 ); +/* +* By default, populate the core kernel's copy of sysfb_primary_display +* directly. This works for both the EFI stub that is incorporated into the +* core kernel and for the zboot decompressor, as both have access to the +* location of the variable inside the loaded kernel image via image_info. +* +* ARM overrides these, as it uses a configuration table instead. +*/ +struct sysfb_display_info *__weak +alloc_primary_display (const struct efi_image_info *image_info) +{ +return efistub_sysfb_display_info (image_info); +} +void __weak free_primary_display (struct sysfb_display_info *dpy) { }
drivers/firmware/efi/libstub/efistub.h +0 -1
@@ -1224,7 +1224,6 @@ void efi_retrieve_eventlog(void);struct sysfb_display_info *alloc_primary_display (const struct efi_image_info *image_info); -struct sysfb_display_info *__alloc_primary_display (void );void free_primary_display (struct sysfb_display_info *dpy); void efi_cache_sync_image(unsigned long image_base,
drivers/firmware/efi/libstub/primary_display.c deleted +0 -56
56 lines not on this page · raw message
drivers/firmware/efi/libstub/zboot.c +0 -6
@@ -26,12 +26,6 @@ void __weak efi_cache_sync_image(unsigned long image_base,// executable code loaded into memory to be safe for execution. } -struct sysfb_display_info * -alloc_primary_display (const struct efi_image_info *image_info) -{ -return efistub_sysfb_display_info (image_info); -} -static const struct efi_image_info *zboot_get_image_info (unsigned long image_base) {
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v4 19/19 Revert "efi: earlycon: Reprobe after parsing config tables"
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
This reverts commit 8b3a149db461d3286d1e211112de3b44ccaeaf71.
That commit added logic to probe the EFI framebuffer earlycon again after the EFI configuration tables have been parsed, as the primary display information may not have been available yet when the earlycon= early parameter was processed.
Now that only ARM uses a configuration table to pass the primary display information to the core kernel, and the EFI framebuffer earlycon is not supported on ARM, the reprobe logic no longer has any callers. So revert it.
--- drivers/firmware/efi/earlycon.c | 16 +++------------- include/linux/efi.h | 1 - 2 files changed, 3 insertions(+), 14 deletions(-)
drivers/firmware/efi/earlycon.c +3 -13
@@ -216,14 +216,6 @@ efi_earlycon_write(struct console *con, const char *str, unsigned int num) } } -static bool __initdata fb_probed; - -void __init efi_earlycon_reprobe (void ) -{ -if (fb_probed) -setup_earlycon ("efifb" ); -} -static int __init efi_earlycon_setup (struct earlycon_device *device,const char *opt) {
@@ -231,17 +223,15 @@ static int __init efi_earlycon_setup(struct earlycon_device *device,u16 xres, yres;u32 i; - fb_wb = opt && !strcmp (opt,"ram" ); - -if (si->orig_video_isVGA !=VIDEO_TYPE_EFI ) { - fb_probed = true; +if (si->orig_video_isVGA !=VIDEO_TYPE_EFI )return -ENODEV ; - } fb_base = si->lfb_base;if (si->capabilities &VIDEO_CAPABILITY_64BIT_BASE ) fb_base |= (u64 )si->ext_lfb_base <<32 ; + fb_wb = opt && !strcmp (opt,"ram" ); + xres = si->lfb_width; yres = si->lfb_height;
include/linux/efi.h +0 -1
@@ -719,7 +719,6 @@ efi_guid_to_str(const efi_guid_t *guid, char *out) }extern void efi_init (void ); -extern void efi_earlycon_reprobe (void );#ifdef CONFIG_EFI extern void efi_enter_virtual_mode (void );/* switch EFI to virtual mode, if possible */ #else
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
[email protected]
AI reviewRe: [PATCH v4 01/19] efi/zboot: arm64: Add missing barrier after cache maintenance
In reply to Ard BiesheuvelThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] efi/zboot: arm64: missing Fixes trailer on efi_cache_sync_image() fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] efi/zboot: arm64: missing Fixes trailer on efi_cache_sync_image() fix
--
commit 67b9c92c21821b6c29157da1899e6b84aef3ebfa
Author: Ard Biesheuvel <[email protected]>
Quoted 8 lines
efi/zboot: arm64: Add missing barrier after cache maintenance
Prevent the I-cache from fetching stale data by issuing a barrier after the data cache maintenance completes but before performing the I-cache maintenance.
Cc <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
[Severity: Low]efi_cache_sync_image() was introduced without this barrier in d9ffe524a538 ("efi/arm64: libstub: Split off kernel image relocation for builtin stub"). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v4 06/19] efi/libstub: arm: Size the decompressed kernel allocation precisely
In reply to Ard BiesheuvelThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/libstub: arm: unaligned u32 read in handle_kernel_image() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/libstub: arm: unaligned u32 read in handle_kernel_image() --
commit b84ef299db1b32d7220e230864d358882c52170f
Author: Ard Biesheuvel <[email protected]>
efi/libstub: arm: Size the decompressed kernel allocation precisely
This commit sizes the ARM EFI stub's reserved memory window for the decompressed kernel using the uncompressed image size, BSS size, and initial page table size instead of a fixed 32 MiB limit, and reduces EFI_PHYS_ALIGN to 2 MiB.
Quoted 18 lines
drivers/firmware/efi/libstub/arm32-stub.c +6
-0@@ -76,6 +76,12 @@ void efi_handle_post_ebs_state(void) &efi_entry_state->sctlr_after_ebs); } +/* last unaligned u32 in the image carries the uncompressed size */ +extern u32 input_data_end[]__aligned(1) ; + +/* entry #3 in the zimage magic table carries the kernel's BSS size */ +extern u32 zimage_magic_table[] asm("_table_start" ); + efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr,
drivers/firmware/efi/libstub/arm32-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 92
Quoted 6 lines
drivers/firmware/efi/libstub/arm32-stub.c +1
-2@@ -83,18 +89,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efi_loaded_image_t *image, efi_handle_t image_handle) { -const int slack =TEXT_OFFSET -5 *PAGE_SIZE ; -int alloc_size =MAX_UNCOMP_KERNEL_SIZE +EFI_PHYS_ALIGN ; +unsigned long kernel_memsize = input_data_end[-1 ] + zimage_magic_table[3 ];
[Severity: High]
Does input_data_end[-1] still perform a 4-byte-aligned word load when the compressed payload length is not a multiple of 4?
In C, __aligned(1) on an array variable declaration applies to the array symbol rather than the element type, so input_data_end decays to u32 * and compilers emit a 32-bit ldr instruction even under -malignment-traps. In arch/arm/boot/compressed/vmlinux.lds.S, input_data_end immediately follows *(.piggydata) before . = ALIGN(4), so input_data_end - 4 has only 1-byte alignment and triggers an alignment fault when the stub runs with the MMU disabled or alignment traps enabled. Reading the size with get_unaligned_le32((u8 *)input_data_end - 4) avoids the aligned u32 dereference.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v4 06/19] efi/libstub: arm: Size the decompressed kernel allocation precisely
Hi Sashiko-bot, Ard,
A few points in arm32-stub.c in your patch:
Quoted 7 lines
Quoted 7 lines
...
...
+ extern u32 input_data_end[]__aligned(1);
...
...
+ unsigned longkernel_memsize= ...
...
I agree with Sashiko Bot Review. In my current tree, I verified:
In vmlinux.lds.S:
.piggydata : {
(.piggydata)
__piggy_size_addr = . - 4;
}
...
. = ALIGN(4);
_etext = .;
In misc.c:
...
ret = do_decompress(input_data, input_data_end - input_data,
output_data, error);
if (ret)
error("decompressor returned an error");
else
putstr(" done, booting the kernel.\n");...
In arm32-stub.c:
...efi_status_t handle_kernel_image(unsigned long *image_addr,
unsigned long *image_size, unsigned long *reserve_addr, unsigned long *reserve_size, efi_loaded_image_t *image, efi_handle_t image_handle) const int slack = TEXT_OFFSET - 5 * PAGE_SIZE; int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN; unsigned long alloc_base, kernel_base; efi_status_t status;
...
I. Analysis
input_data_end sits immediately after .piggydata and right before . = ALIGN(4); in vmlinux.lds.S, so its address is only 1-byte aligned.
Applying __aligned(1) to extern u32 input_data_end[] does not prevent an unaligned 32-bit word load when input_data_end[-1] decays to
u32 *, which can trigger an alignment fault on ARM32 during early stub execution.
II. Suggestion
Declare extern u8 input_data_end[]; (byte array, naturally 1-byte aligned without attributes) and read the uncompressed size safely
via get_unaligned_le32(input_data_end - 4) (from <linux/unaligned.h>).
Thanks,
João Guilherme da Silva Freitas Lima <[email protected]>
Re: [PATCH v4 10/19] efi/libstub: arm: Simplify handle_kernel_image()
In reply to Ard BiesheuvelQuoted 6 lines · Ard Biesheuvel
Now that ARM moved to
efi_image_infofor obtaining the base of the decompressed kernel image inefi_enter_kernel(), it no longer needs to distinguish between the image address (which might point into an unrelated allocation below the decompressed image) and the reservation address (which is what can be freed upon an error).
Quoted 6 lines
So use image_addr/image_size rather than reserve_addr/reserve_size in
handle_kernel_image()to return the base and size of the allocated region to the caller. This will allow those parameters to be dropped entirely from the prototype in a subsequent patch.[...]
Signature · 1 line
Jason
Quoted 6 lines
From: Ard Biesheuvel <[email protected]>
Clean up the arm64 build script and linker hacks that zboot uses to obtain properties of the loaded image (which it cannot access directly via a relative reference like the ordinary EFI stub).
Quoted 7 lines
Instead, define a generic struct
efi_image_info, and expose its offset in the loaded image in a similar manner, but without relying on explicit linker script exports.Populate the struct with section markers that all architecture use for loading the kernel image at boot, so that all the linker exports can be dropped for them.
This one looks fine to me, other than the minor sashiko note. Still works
Signature · 1 line
Jason
Re: [PATCH v4 07/19] efi/libstub: Avoid reserve_addr/_size in efi_kaslr_relocate_kernel()
In reply to Ard BiesheuvelQuoted 3 lines
Avoid
reserve_addrandreserve_sizeto carry the information about the allocated space for the relocated kernel, and instead, use a singlealloc_sizeoutput parameter that is wired toimage_size.
I was a bit worried about this patch since the DRTM series uses reserved for the in place trailing allocation
But after all the changes I made a patch that just renames the variables in the entire caller chain:
efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
unsigned long *free_size,
And nothing looks at free_addr/size except to free it. It is all now described by image_info. The DRTM case uses it to free the trailing allocation..
So this turned into a nice cleanup
Signature · 1 line
Jason
Re: [PATCH v4 11/19] efi/libstub: Drop reserve_addr/_size params from handle_kernel_image()
In reply to Ard BiesheuvelQuoted 3 lines · Ard Biesheuvel
The
reserve_addrandreserve_sizevariable pointers passed tohandle_kernel_image()are no longer used, so drop them from the caller and from the prototype.
Signature · 1 line
Jason
Ard Biesheuvel
maintainerQuoted 58 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Clean up the arm64 build script and linker hacks that zboot uses to obtain properties of the loaded image (which it cannot access directly via a relative reference like the ordinary EFI stub).
Instead, define a generic struct
efi_image_info, and expose its offset in the loaded image in a similar manner, but without relying on explicit linker script exports.Populate the struct with section markers that all architecture use for loading the kernel image at boot, so that all the linker exports can be dropped for them.
Changes since v3:
- Populateefi_image_infofor ARM correctly
- Fix a pre-existing issue in the ARM allocation logic
- Simplify the prototype ofhandle_kernel_image()
- Passimage_infopointer down toefi_kaslr_relocate_kernel()and simplify the callers
- Align RISC-V's__inittext_endcorrectly so it can be remapped.
- Add some more acks from JasonChanges since v2:
- Add some additional fixes for RISC-V, which deviates from other architectures wrt its section markers. These patches have been sent out separately as well [0] as they will likely be taken through the RISC-V tree.
- Pass anefi_image_pointertoefi_stub_common()that points to the version inside the loaded image, so that its relative references accurately describe vmlinux in memory before boot.
- Avoid weak linkage hacks to derive the kernel entrypoint, and instead, always use the entrypoint passed viaefi_image_info
- Useefi_image_infoforsysfb_display_infoso that efifb earlycon works much earlier on zboot
- Drop changes related to the previous bullet that are now no longer needed
- Add acks from JasonChanges since v1:
- drop arm64 image header changes
- moveefi_image_infoout of__efistub_scope so it can access vmlinux symbols directly
- implement in a generic manner for all architectures that support zboot
- address Sashiko comments[0] https://lore.kernel.org/all/[email protected]/
Cc: Jason Gunthorpe <[email protected]>
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: [email protected]
Cc: [email protected]
Link:
https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Ard Biesheuvel (19):
efi/zboot: arm64: Add missing barrier after cache maintenance
riscv: Use_stextnot_textto denote the start of kernel code
riscv: Make_textpoint to the start of the kernel image efi/riscv: Rename__init_text_endto__inittext_endlike other archs
Ping to the RISC-V maintainers: I'd like to incorporate these RISC-V changes for the next cycle using a stable shared branch.
I'd like to queue up this series this week. I'll put the RISC-V changes on a separate branch so I'll be able to swap in another one later, if needed.
Thanks,
Quoted 71 lines
efi/libstub: Avoid
_endsection marker in library code efi/libstub: arm: Size the decompressed kernel allocation precisely efi/libstub: Avoid reserve_addr/_size inefi_kaslr_relocate_kernel()efi/libstub: Capture loaded image information in structefi_image_infoefi/libstub: Pass kernel entrypoint down toefi_enter_kernel()efi/libstub: arm: Simplifyhandle_kernel_image()efi/libstub: Drop reserve_addr/_size params fromhandle_kernel_image()efi/libstub: arm64: Use structefi_image_infoto access image
properties
efi/libstub: loongarch: Use structefi_image_infoto access image
properties
efi/libstub: riscv: Use structefi_image_infoto access image
properties
efi/libstub: Makeefi_remap_image()returnefi_status_tefi/zboot: Disentangleefi_remap_image()fromefi_cache_sync_image()efi/libstub: arm64: Drop zbootcode_sizehack
efi: Make config table based primary display handover ARM-only again Revert "efi: earlycon: Reprobe after parsing config tables"arch/arm/boot/compressed/head.S | 3 +- arch/arm/include/asm/efi.h | 15 +--- arch/arm/kernel/efi.c | 27 ++++++ arch/arm64/boot/Makefile | 3 - arch/arm64/include/asm/efi.h | 2 - arch/arm64/kernel/efi-header.S | 2 + arch/arm64/kernel/image-vars.h | 14 --- arch/loongarch/kernel/efi.c | 20 +---- arch/loongarch/kernel/head.S | 4 +- arch/loongarch/kernel/image-vars.h | 6 -- arch/loongarch/kernel/vmlinux.lds.S | 1 - arch/riscv/include/asm/efi.h | 2 - arch/riscv/include/asm/sections.h | 2 +- arch/riscv/kernel/head.S | 1 + arch/riscv/kernel/image-vars.h | 8 -- arch/riscv/kernel/setup.c | 2 +- arch/riscv/kernel/vmlinux.lds.S | 6 +- drivers/firmware/efi/earlycon.c | 16 +--- drivers/firmware/efi/efi-init.c | 24 +----- drivers/firmware/efi/efi.c | 5 -- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/Makefile.zboot | 11 ++- drivers/firmware/efi/libstub/arm32-stub.c | 90 +++++++++++++++----- drivers/firmware/efi/libstub/arm64-stub.c | 37 ++------ drivers/firmware/efi/libstub/arm64.c | 23 +---- drivers/firmware/efi/libstub/efi-stub-entry.c | 33 +------ drivers/firmware/efi/libstub/efi-stub-helper.c | 15 ++-- drivers/firmware/efi/libstub/efi-stub.c | 26 ++++-- drivers/firmware/efi/libstub/efistub.h | 84 +++++++++++++----- drivers/firmware/efi/libstub/fdt.c | 5 +- drivers/firmware/efi/libstub/image_info.S | 19 +++++ drivers/firmware/efi/libstub/kaslr.c | 45 +++++----- drivers/firmware/efi/libstub/loongarch-stub.c | 21 ++--- drivers/firmware/efi/libstub/loongarch-stub.h | 4 - drivers/firmware/efi/libstub/loongarch.c | 11 +-- drivers/firmware/efi/libstub/primary_display.c | 56 ------------ drivers/firmware/efi/libstub/riscv-stub.c | 29 +------ drivers/firmware/efi/libstub/riscv.c | 16 +--- drivers/firmware/efi/libstub/zboot-decompress-gzip.c | 2 - drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 2 - drivers/firmware/efi/libstub/zboot.c | 28 +++++- drivers/firmware/efi/libstub/zboot.lds | 6 +- include/linux/efi.h | 1 - 43 files changed, 324 insertions(+), 406 deletions(-) create mode 100644 drivers/firmware/efi/libstub/image_info.S delete mode 100644 drivers/firmware/efi/libstub/loongarch-stub.h delete mode 100644 drivers/firmware/efi/libstub/primary_display.cbase-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Re: [PATCH v4 02/19] riscv: Use _stext not _text to denote the start of kernel code
In reply to Ard BiesheuvelQuoted 29 lines · On Wed, 18 Nov 2026 10:21:08 PST (-0800), [email protected] wrote:
From: Ard Biesheuvel <[email protected]>
On RISC-V,
_textand_stextcurrently refer to the same location, i.e., the start of the .text output section, which follows the image header (.head.text) emitted byHEAD_TEXT_SECTION._startis the symbol that refers to the actual start of the image.On other architectures such as arm64,
_textrefers to the start of the image, and_stextto the start of the kernel code, and generic code (such as the EFI stub) may start relying on this distinction.So switch the only user of
_textinarch/riscv, i.e., the "Kernel code" resource that is exposed via/proc/iomem(and from which the "Kernel image" resource inherits its start address), over to_stext. This is a no-op given that both symbols currently resolve to the same address, but it permits_textto be moved to the start of the image, making it an alias of_start.Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/riscv/kernel/setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kernel/setup.c +0
-0
arch/riscv/kernel/setup.c static int __init add_kernel_resources(void) · line 96
Quoted 7 lines
arch/riscv/kernel/setup.c +1
-1@@ -91,7 +91,7 @@ static int __init add_kernel_resources(void)* child nodes. */ - code_res.start =__pa_symbol (_text); + code_res.start =__pa_symbol (_stext); code_res.end =__pa_symbol (_etext) -1 ; code_res.flags =IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY ;
Re: [PATCH v4 03/19] riscv: Make _text point to the start of the kernel image
In reply to Ard BiesheuvelQuoted 32 lines · On Wed, 18 Nov 2026 10:21:09 PST (-0800), [email protected] wrote:
From: Ard Biesheuvel <[email protected]>
On RISC-V,
_textcurrently refers to the start of the .text output section, which is placed after the image header (.head.text), and is therefore equal to_stext. On other architectures such as arm64,_textrefers to the start of the image, and is used as such by generic code, e.g., to describe the kernel mapping in/proc/kcoreand in the ELF headers of the vmcore, and by the EFI stub.Now that
arch/riscvno longer uses_textto refer to the start of the kernel code, move it to the start of the image so that it is equal to_start, and the distinction between_textand_stextis the same as on other architectures.This also means that the image header is no longer omitted from the
/proc/kcoreand vmcore kernel text mappings. Other users of_text, such as kallsyms and ftrace, only use it as a reference point to calculate relative offsets, and are not affected by this change.Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/riscv/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kernel/vmlinux.lds.S +0
-0@@ -28,11 +28,11 @@ SECTIONS /* Beginning of code and text segment */ . = LOAD_OFFSET;
arch/riscv/kernel/vmlinux.lds.S SECTIONS · line 38
Quoted 10 lines
arch/riscv/kernel/vmlinux.lds.S +1
-1_start = .; + _text = .; HEAD_TEXT_SECTION . = ALIGN(PAGE_SIZE); .text : { - _text = .; _stext = .; TEXT_TEXT SCHED_TEXT
Re: [PATCH v4 04/19] efi/riscv: Rename __init_text_end to __inittext_end like other archs
In reply to Ard BiesheuvelQuoted 74 lines · On Wed, 18 Nov 2026 10:21:10 PST (-0800), [email protected] wrote:
From: Ard Biesheuvel <[email protected]>
Align RISC-V with arm64, LoongArch and x86, and drop the underscore between 'init' and 'text' in the section markers. This allows the value to be captured by generic EFI stub code that can be shared between all those architectures.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/riscv/include/asm/sections.h | 2 +- arch/riscv/kernel/image-vars.h | 2 +- arch/riscv/kernel/vmlinux.lds.S | 4 ++-- drivers/firmware/efi/libstub/riscv-stub.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)arch/riscv/include/asm/sections.h +1
-1@@ -11,7 +11,7 @@extern char _start[];extern char _start_kernel[];extern char __init_data_begin[], __init_data_end[]; -extern char __init_text_begin[], __init_text_end[]; +extern char __inittext_begin[], __inittext_end[];extern char __alt_start[], __alt_end[];extern char __exittext_begin[], __exittext_end[];arch/riscv/kernel/image-vars.h +1
-1@@ -27,7 +27,7 @@ __efistub__start = _start; __efistub__start_kernel = _start_kernel; __efistub__end = _end; __efistub__edata = _edata; -__efistub___init_text_end = __init_text_end; +__efistub___inittext_end = __inittext_end;#if defined(CONFIG_EFI_EARLYCON ) || defined(CONFIG_SYSFB ) __efistub_sysfb_primary_display = sysfb_primary_display;#endif arch/riscv/kernel/vmlinux.lds.S +2
-2@@ -46,7 +46,7 @@ SECTIONS . = ALIGN(SECTION_ALIGN); __init_begin = .; - __init_text_begin = .; + __inittext_begin = .; .init.text : AT(ADDR(.init.text) - LOAD_OFFSET) ALIGN(SECTION_ALIGN) { \ _sinittext = .; \ INIT_TEXT \@@ -72,12 +72,12 @@ SECTIONS } __exittext_end = .; - __init_text_end = .; . = ALIGN(SECTION_ALIGN); #ifdef CONFIG_EFI . = ALIGN(PECOFF_SECTION_ALIGNMENT); __pecoff_text_end = .; #endif + __inittext_end = .; /* Start of init data section */ __init_data_begin = .; INIT_DATA_SECTION(16)drivers/firmware/efi/libstub/riscv-stub.c +0
-0
drivers/firmware/efi/libstub/riscv-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 40
Quoted 8 lines
drivers/firmware/efi/libstub/riscv-stub.c +1
-1@@ -34,7 +34,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_status_t status; kernel_size = _edata - _start; - kernel_codesize = __init_text_end - _start; + kernel_codesize = __inittext_end - _start; kernel_memsize = kernel_size + (_end - _edata); *image_addr = (unsigned long )_start; *reserve_size = kernel_memsize;