efi: Expose loaded image properties via info struct
Subsystems (7)
v2 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 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
Ard Biesheuvel (10):
efi/zboot: arm64: Add missing barrier after cache maintenance efi/riscv: Rename __init_text_end to __inittext_end like other archs efi/libstub: Avoid _end section marker in library code efi/libstub: Capture loaded image information in struct efi_image_info 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
arch/arm64/boot/Makefile | 3 -- arch/arm64/kernel/efi-header.S | 2 + arch/arm64/kernel/image-vars.h | 10 ---- arch/loongarch/kernel/head.S | 1 + arch/loongarch/kernel/image-vars.h | 1 - arch/riscv/include/asm/sections.h | 2 +- arch/riscv/kernel/head.S | 1 + arch/riscv/kernel/image-vars.h | 5 -- arch/riscv/kernel/vmlinux.lds.S | 4 +- drivers/firmware/efi/libstub/Makefile | 1 + drivers/firmware/efi/libstub/Makefile.zboot | 11 ++++- drivers/firmware/efi/libstub/arm32-stub.c | 3 ++ drivers/firmware/efi/libstub/arm64-stub.c | 20 ++++---- drivers/firmware/efi/libstub/arm64.c | 8 ++-- drivers/firmware/efi/libstub/efi-stub-entry.c | 10 +++- drivers/firmware/efi/libstub/efi-stub-helper.c | 15 ++++-- drivers/firmware/efi/libstub/efi-stub.c | 3 +- drivers/firmware/efi/libstub/efistub.h | 48 ++++++++++++++++++-- drivers/firmware/efi/libstub/image_info.S | 12 +++++ drivers/firmware/efi/libstub/kaslr.c | 2 +- drivers/firmware/efi/libstub/loongarch-stub.c | 4 +- drivers/firmware/efi/libstub/riscv-stub.c | 14 +++--- drivers/firmware/efi/libstub/zboot-decompress-gzip.c | 2 - drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 2 - drivers/firmware/efi/libstub/zboot.c | 29 +++++++++++- drivers/firmware/efi/libstub/zboot.lds | 6 +-- 26 files changed, 150 insertions(+), 69 deletions(-) create mode 100644 drivers/firmware/efi/libstub/image_info.S
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
PATCH v2 1/10 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 | 2 ++ 1 file changed, 2 insertions(+)
drivers/firmware/efi/libstub/arm64.c +2 -0
@@ -107,6 +107,8 @@ void efi_cache_sync_image(unsigned long image_base, base += lsize; size -= lsize; }while (size >= lsize); + +dsb (ish); } asm("ic ialluis" );
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v2 01/10] efi/zboot: arm64: Add missing barrier after cache maintenance
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] efi/zboot: arm64: missing dsb(ish) in efi_cache_sync_image() when IDC is set - [Low] efi/zboot…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] efi/zboot: arm64: missing dsb(ish) in efi_cache_sync_image() when IDC is set
- [Low] efi/zboot: arm64: missing Fixes trailer for efi_cache_sync_image() barrier fix
--
commit e6570e5cb733b9a306b82caaabc5d43037fdb9b0
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]
Should this bug fix include a Fixes tag for the commit that introduced efi_cache_sync_image()?
drivers/firmware/efi/libstub/arm64.c void efi_cache_sync_image(unsigned long image_base, · line 114
Quoted 13 lines
drivers/firmware/efi/libstub/arm64.c +2
-0@@ -107,6 +107,8 @@ void efi_cache_sync_image(unsigned long image_base, base += lsize; size -= lsize; }while (size >= lsize); + +dsb (ish); } asm("ic ialluis" );
[Severity: Medium]
Should dsb(ish) be placed outside the !(ctr & BIT(CTR_EL0_IDC_SHIFT)) block, immediately before asm("ic ialluis")?
When CTR_EL0.IDC is set, explicit data cache clean instructions are not needed, but a DSB is still required to ensure the prior stores from decompressing the kernel image have completed before "ic ialluis" invalidates the instruction cache (matching caches_clean_inval_pou_macro in arch/arm64/mm/cache.S). Otherwise the I-cache invalidation can complete before those stores drain, allowing stale instructions to be fetched.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/10 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,7 +72,7 @@ SECTIONS } __exittext_end = .; - __init_text_end = .; + __inittext_end = .; . = ALIGN(SECTION_ALIGN); #ifdef CONFIG_EFI . = ALIGN(PECOFF_SECTION_ALIGNMENT);
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.0.542.g3ef89b7020-goog
Re: [PATCH v2 02/10] efi/riscv: Rename __init_text_end to __inittext_end like other archs
Quoted 4 lines · Ard Biesheuvel
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.
Signature · 1 line
Jason
PATCH v2 3/10 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.0.542.g3ef89b7020-goog
Re: [PATCH v2 03/10] efi/libstub: Avoid _end section marker in library code
Quoted 8 lines · Ard Biesheuvel
The
_endmarker could point to the end of the kernel image being loaded, or to the_endof 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_memsizeand the image address to obtain the same quantity.[...]
Signature · 1 line
Jason
PATCH v2 4/10 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 there 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 can just grab the efi_image_info pointer as needed.
ARM implements neither, but -like x86- incorporates the EFI stub into its own decompressor. It has no need for this functionality, so emit a dummy struct to satisfy the linker.
--- 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 | 3 ++ drivers/firmware/efi/libstub/efi-stub-entry.c | 10 ++++- drivers/firmware/efi/libstub/efi-stub.c | 3 +- drivers/firmware/efi/libstub/efistub.h | 44 +++++++++++++++++++- drivers/firmware/efi/libstub/image_info.S | 12 ++++++ drivers/firmware/efi/libstub/zboot.c | 17 +++++++- drivers/firmware/efi/libstub/zboot.lds | 2 + 12 files changed, 101 insertions(+), 6 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 +3 -0
@@ -132,3 +132,6 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,return EFI_SUCCESS ; } + +/* expose a dummy struct - ARM does not use this anyway */ +struct efi_image_info efistub_image_info = {};
drivers/firmware/efi/libstub/efi-stub-entry.c +9 -1
@@ -27,6 +27,13 @@ struct sysfb_display_info *alloc_primary_display(void)return NULL ; } +const struct efi_image_info *efistub_get_image_info (void ) +{ +extern const struct efi_image_info efistub_image_info; + +return &efistub_image_info; +} +/* * 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
@@ -80,7 +87,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 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_addr, cmdline_ptr, +efistub_get_image_info ());efi_free (image_size, image_addr);efi_free (reserve_size, reserve_addr);
drivers/firmware/efi/libstub/efi-stub.c +2 -1
@@ -161,7 +161,8 @@ 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, -char *cmdline_ptr) +char *cmdline_ptr, +const struct efi_image_info *image_info) {struct sysfb_display_info *dpy;efi_status_t status;
drivers/firmware/efi/libstub/efistub.h +43 -1
54 lines not on this page · raw message
drivers/firmware/efi/libstub/image_info.S new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#define REL_REF(sym) .long sym - 0b + + .globl __efistub_efistub_image_info + .p2align 3 +__efistub_efistub_image_info: +0: REL_REF(_text) + REL_REF(__inittext_end) + REL_REF(_edata) + REL_REF(_end) + REL_REF(efi_primary_entry)
drivers/firmware/efi/libstub/zboot.c +15 -2
@@ -31,6 +31,13 @@ struct sysfb_display_info *alloc_primary_display(void)return __alloc_primary_display (); } +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 __efiapiefi_zboot_entry (efi_handle_t handle,efi_system_table_t *systab) {
@@ -93,9 +100,15 @@ 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_base, cmdline_ptr, + image_info); +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.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v2 04/10] efi/libstub: Capture loaded image information in struct efi_image_info
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/libstub: use of _text instead of _start breaks RISC-V image offsets --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/libstub: use of _text instead of _start breaks RISC-V image offsets
--
commit 753bce0ac7226eaedf6904088805b59f9e240934
Author: Ard Biesheuvel <[email protected]>
efi/libstub: Capture loaded image information in struct efi_image_info
This commit introduces struct efi_image_info to record the loaded kernel image's layout and entry point offsets, passing it to efi_stub_common() for both the uncompressed EFI stub and EFI zboot.
Quoted 33 lines
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.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) +
[ ... ]
Quoted 48 lines
drivers/firmware/efi/libstub/efistub.h +40
-0@@ -1148,11 +1148,53 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle); +struct efi_image_info { +s32 _text_offset; +s32 __inittext_end_offset; +s32 _edata_offset; +s32 _end_offset; +s32 entry_offset; +}; + +const struct efi_image_info *efistub_get_image_info (void ); +const struct efi_image_info *zboot_get_image_info (unsigned long image_base); + +static inline +unsigned long efistub_kernel_base (const struct efi_image_info *image_info) +{ +return (unsigned long )offset_to_ptr (&image_info->_text_offset); +} + +static inline +unsigned long efistub_kernel_size (const struct efi_image_info *image_info) +{ +return image_info->_edata_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_memsize (const struct efi_image_info *image_info) +{ +return image_info->_end_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_codesize (const struct efi_image_info *image_info) +{ +return image_info->__inittext_end_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_entry_offset (const struct efi_image_info *image_info) +{ +return image_info->entry_offset - image_info->_text_offset; +}
[ ... ]
Quoted 8 lines
drivers/firmware/efi/libstub/image_info.S new file +2
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +
drivers/firmware/efi/libstub/image_info.S line 12
Quoted 10 lines
drivers/firmware/efi/libstub/image_info.S new file +10
-0+#define REL_REF(sym) .long sym - 0b + + .globl __efistub_efistub_image_info + .p2align 3 +__efistub_efistub_image_info: +0: REL_REF(_text) + REL_REF(__inittext_end) + REL_REF(_edata) + REL_REF(_end) + REL_REF(efi_primary_entry)
[Severity: High]
Does using _text as the image base reference break these offset calculations on RISC-V?
Unlike arm64 and LoongArch, RISC-V places _start and HEAD_TEXT_SECTION before _text:
arch/riscv/kernel/vmlinux.lds.S:
. = LOAD_OFFSET;
_start = .;
HEAD_TEXT_SECTION
. = ALIGN(PAGE_SIZE);
.text : {
_text = .;
_stext = .;Because arch/riscv/kernel/head.S is in __HEAD (.head.text), the binary Image starts at _start and efi_primary_entry (_start_kernel) sits below _text.
As a result, get_image_info_offset in Makefile.zboot computes efistub_image_info_offset relative to _text instead of the start of the decompressed image (_start), so zboot_get_image_info() points (_text - _start) bytes before struct efi_image_info. In addition, efistub_kernel_base() returns _text rather than _start, the size helpers exclude .head.text, and efistub_kernel_entry_offset() computes a negative s32 (_start_kernel - _text) that sign-extends to a bogus unsigned long.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 04/10] efi/libstub: Capture loaded image information in struct efi_image_info
In reply to Ard Biesheuveldrivers/firmware/efi/libstub/efi-stub-entry.c efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, · line 91
Quoted 8 lines
[ ... 131 lines skipped ... ]
drivers/firmware/efi/libstub/efi-stub-entry.c +2
-1@@ -80,7 +87,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 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_addr, cmdline_ptr, +efistub_get_image_info ());
efistub_get_image_info() always returns the pointer from the EFI stubs location, but at this point the kernel has been relocated and now we are passing an info pointer that is outside image to everything else while the zboot path passes an info inside the image.
It would be better if info is always pointing inside the final image. Then you can use info to always get a pointer inside the ultimate kernel, which is what all the DRTM values need.
Having efistub_get_image_info() take in image_base, and then using weak to overlay it with zboot's version would give a consistent interface.
The adjustment can be calculated like this:
return (const void *)(image_base - efistub_image_info._text_offset);
That would allow cleaning up the primary_entry() flow to just use info, and remove the weak functions:
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) =
efistub_kernel_ptr(image_info, entry);
enter_kernel(fdt_addr, 0, 0, 0);
}(see below for macro)
You could do the same by passing image_base and image_info everywhere, but that seems less nice.
drivers/firmware/efi/libstub/efistub.h efi_status_t handle_kernel_image(unsigned long *image_addr, · line 1157
Quoted 13 lines
[ ... 20 lines skipped ... ]
+++b/drivers/firmware/efi/libstub/efistub.hdrivers/firmware/efi/libstub/efistub.h +7
-0@@ -1148,11 +1148,53 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle); +struct efi_image_info { +s32 _text_offset; +s32 __inittext_end_offset; +s32 _edata_offset; +s32 _end_offset; +s32 entry_offset; +};
This ends up in a cross-arch header.. It seems OK, the x86 voffset.h parts can fit cross-arch like this too.
For DRTM I will do it like this:
#ifdef CONFIG_EFI_STUB_DRTM s32 __drtm_measured_start_offset; s32 __drtm_measured_end_offset; s32 drtm_entry_offset; s32 drtm_handoff_offset;
#endif
Which should be fine, every DRTM will need those four things at least.
Quoted 23 lines
drivers/firmware/efi/libstub/efistub.h +23
-0+ +const struct efi_image_info *efistub_get_image_info (void ); +const struct efi_image_info *zboot_get_image_info (unsigned long image_base); + +static inline +unsigned long efistub_kernel_base (const struct efi_image_info *image_info) +{ +return (unsigned long )offset_to_ptr (&image_info->_text_offset); +} + +static inline +unsigned long efistub_kernel_size (const struct efi_image_info *image_info) +{ +return image_info->_edata_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_memsize (const struct efi_image_info *image_info) +{ +return image_info->_end_offset - image_info->_text_offset; +} + +static inline
drivers/firmware/efi/libstub/efistub.h efi_status_t handle_kernel_image(unsigned long *image_addr, · line 1190
Quoted 10 lines
drivers/firmware/efi/libstub/efistub.h +10
-0+unsigned long efistub_kernel_codesize (const struct efi_image_info *image_info) +{ +return image_info->__inittext_end_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_entry_offset (const struct efi_image_info *image_info) +{ +return image_info->entry_offset - image_info->_text_offset; +}
As above a little helper to get a void * would be useful:
static inline void *
__efistub_kernel_entry_ptr(const struct efi_image_info *image_info, s32 offset)
{
return (void *)((offset - image_info->_text_offset) +
efistub_kernel_base(image_info));
}
#define efistub_kernel_ptr(image_info, ptr) \
__efistub_kernel_entry_ptr(image_info, (image_info)->ptr##_offset)If the rework above is done this would replaceefistub_kernel_entry_offset()
drivers/firmware/efi/libstub/image_info.S line 3
Quoted 6 lines
[ ... 15 lines skipped ... ]
+++b/drivers/firmware/efi/libstub/image_info.Sdrivers/firmware/efi/libstub/image_info.S new file +3
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#define REL_REF(sym) .long sym - 0b
The code could consistently use the offset_to_ptr() macro if the label was included in the macro?
#define REL_REF(sym) 0: .long sym - 0b
It would make the efistub_kernel_ptr() simpler, and the three size calculations would look like:
return efistub_kernel_ptr(image_info, __inittext_end) - efistub_kernel_ptr(image_info, _text);
drivers/firmware/efi/libstub/image_info.S line 12
Quoted 9 lines
drivers/firmware/efi/libstub/image_info.S new file +9
-0+ + .globl __efistub_efistub_image_info + .p2align 3 +__efistub_efistub_image_info: +0: REL_REF(_text) + REL_REF(__inittext_end) + REL_REF(_edata) + REL_REF(_end) + REL_REF(efi_primary_entry)
Can you put a
+ .section ".init.rodata", "a"
Here?
The DRTM patch points this to various things in .init. sections and modpost will complain. I think nothing outside the efi stub should touch this stuct and it should be freed as init data right?
drivers/firmware/efi/libstub/zboot.c efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) · line 107
Quoted 12 lines
[ ... 18 lines skipped ... ]
drivers/firmware/efi/libstub/zboot.c +4
-2@@ -93,9 +100,15 @@ 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);
auto is allowed in the kernel now?
Signature · 1 line
Jason
Ard Biesheuvel
maintainerRe: [PATCH v2 04/10] efi/libstub: Capture loaded image information in struct efi_image_info
Quoted 38 lines · Jason Gunthorpe
drivers/firmware/efi/libstub/efi-stub-entry.c efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, · line 91
Quoted 8 lines
[ ... 131 lines skipped ... ]
drivers/firmware/efi/libstub/efi-stub-entry.c +2
-1@@ -80,7 +87,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 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_addr, cmdline_ptr, +efistub_get_image_info ());
efistub_get_image_info()always returns the pointer from the EFI stubs location, but at this point the kernel has been relocated and now we are passing an info pointer that is outside image to everything else while the zboot path passes an info inside the image.It would be better if info is always pointing inside the final image. Then you can use info to always get a pointer inside the ultimate kernel, which is what all the DRTM values need.
Having
efistub_get_image_info()take inimage_base, and then using weak to overlay it with zboot's version would give a consistent interface.The adjustment can be calculated like this:
return (const void *)(image_base - efistub_image_info._text_offset);That would allow cleaning up the
primary_entry()flow to just use info, and remove the weak functions:void
__noreturnefi_enter_kernel(const structefi_image_info*image_info,
unsigned longfdt_addr, unsigned longfdt_size){ void (* __noreturn enter_kernel)(u64, u64, u64, u64) = efistub_kernel_ptr(image_info, entry); enter_kernel(fdt_addr, 0, 0, 0); }
Quoted 5 lines
(see below for macro)
You could do the same by passing
image_baseandimage_infoeverywhere, but that seems less nice.
I'd prefer to handle this by adding another offset, and an API to update it after moving the image. That way, the EFI stub code can still grab the info struct pointer without threading it through the API, while it will always remain valid.
Quoted 19 lines
drivers/firmware/efi/libstub/efistub.h efi_status_t handle_kernel_image(unsigned long *image_addr, · line 1157
Quoted 13 lines
[ ... 20 lines skipped ... ]
+++b/drivers/firmware/efi/libstub/efistub.hdrivers/firmware/efi/libstub/efistub.h +7
-0@@ -1148,11 +1148,53 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle); +struct efi_image_info { +s32 _text_offset; +s32 __inittext_end_offset; +s32 _edata_offset; +s32 _end_offset; +s32 entry_offset; +};This ends up in a cross-arch header.. It seems OK, the x86
voffset.hparts can fit cross-arch like this too.For DRTM I will do it like this:
Quoted 9 lines
#ifdef CONFIG_EFI_STUB_DRTM s32 __drtm_measured_start_offset; s32 __drtm_measured_end_offset; s32 drtm_entry_offset; s32 drtm_handoff_offset;#endif
Which should be fine, every DRTM will need those four things at least.
I think that is fine. We could add an arch substruct once we need it but that seems premature at this point.
Quoted 59 lines
Quoted 23 lines
drivers/firmware/efi/libstub/efistub.h +23
-0+ +const struct efi_image_info *efistub_get_image_info (void ); +const struct efi_image_info *zboot_get_image_info (unsigned long image_base); + +static inline +unsigned long efistub_kernel_base (const struct efi_image_info *image_info) +{ +return (unsigned long )offset_to_ptr (&image_info->_text_offset); +} + +static inline +unsigned long efistub_kernel_size (const struct efi_image_info *image_info) +{ +return image_info->_edata_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_memsize (const struct efi_image_info *image_info) +{ +return image_info->_end_offset - image_info->_text_offset; +} + +static inlinedrivers/firmware/efi/libstub/efistub.h efi_status_t handle_kernel_image(unsigned long *image_addr, · line 1190
Quoted 10 lines
drivers/firmware/efi/libstub/efistub.h +10
-0+unsigned long efistub_kernel_codesize (const struct efi_image_info *image_info) +{ +return image_info->__inittext_end_offset - image_info->_text_offset; +} + +static inline +unsigned long efistub_kernel_entry_offset (const struct efi_image_info *image_info) +{ +return image_info->entry_offset - image_info->_text_offset; +}As above a little helper to get a void * would be useful:
static inline void *
__efistub_kernel_entry_ptr(const structefi_image_info*image_info,
s32 offset){ return (void *)((offset - image_info->_text_offset) + efistub_kernel_base(image_info)); } #define efistub_kernel_ptr(image_info, ptr) \ __efistub_kernel_entry_ptr(image_info, (image_info)->ptr##_offset)If the rework above is done this would replace
efistub_kernel_entry_offset()drivers/firmware/efi/libstub/image_info.S line 3
Quoted 6 lines
[ ... 15 lines skipped ... ]
+++b/drivers/firmware/efi/libstub/image_info.Sdrivers/firmware/efi/libstub/image_info.S new file +3
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#define REL_REF(sym) .long sym - 0bThe code could consistently use the
offset_to_ptr()macro if the label was included in the macro?
Quoted 8 lines
#define REL_REF(sym) 0: .long sym - 0bIt would make the
efistub_kernel_ptr()simpler, and the three size calculations would look like:return efistub_kernel_ptr(image_info, __inittext_end) - efistub_kernel_ptr(image_info, _text);
Again, probably cleaner to just refer to the copy of the struct once it exists.
Quoted 14 lines
drivers/firmware/efi/libstub/image_info.S line 12
Quoted 9 lines
drivers/firmware/efi/libstub/image_info.S new file +9
-0+ + .globl __efistub_efistub_image_info + .p2align 3 +__efistub_efistub_image_info: +0: REL_REF(_text) + REL_REF(__inittext_end) + REL_REF(_edata) + REL_REF(_end) + REL_REF(efi_primary_entry)Can you put a
(quoted diff) +1
-0+ .section ".init.rodata", "a"
Quoted 2 lines
Here?
Ack
Quoted 4 lines
The DRTM patch points this to various things in .init. sections and modpost will complain. I think nothing outside the efi stub should touch this stuct and it should be freed as init data right?
Yes.
Quoted 11 lines
Quoted 11 lines
[ ... 18 lines skipped ... ]
drivers/firmware/efi/libstub/zboot.c +3
-2@@ -93,9 +100,15 @@ 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;
Quoted 4 lines
drivers/firmware/efi/libstub/zboot.c efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) · line 107
Quoted 1 line
drivers/firmware/efi/libstub/zboot.c +1
-0+ auto image_info = zboot_get_image_info(image_base);auto is allowed in the kernel now?
Yes.
Re: [PATCH v2 04/10] efi/libstub: Capture loaded image information in struct efi_image_info
Quoted 10 lines · Ard Biesheuvel
Quoted 3 lines · Jason Gunthorpe
You could do the same by passing
image_baseandimage_infoeverywhere, but that seems less nice.I'd prefer to handle this by adding another offset, and an API to update it after moving the image. That way, the EFI stub code can still grab the info struct pointer without threading it through the API, while it will always remain valid.
I did draft threading it throughout the API and it was a net improvement, IMHO. There wasn't much and it removed __weak function shenanigans because info is always available and you can just always jump to entry even for zboot.
But yeah, you can adjust the function to return the corrected version too.
Quoted 8 lines
Quoted 8 lines
The code could consistently use the
offset_to_ptr()macro if the label was included in the macro?#define REL_REF(sym) 0: .long sym - 0bIt would make the
efistub_kernel_ptr()simpler, and the three size calculations would look like:
Quoted 6 lines
Quoted 3 lines
return efistub_kernel_ptr(image_info, __inittext_end) - efistub_kernel_ptr(image_info, _text);Again, probably cleaner to just refer to the copy of the struct once it exists.
Well, we need pointers, like for entry, so as long as there is some way to get them that always works I'm fine.
Converting directly from image_info without the image_base argument was convenient..
Jason
PATCH v2 5/10 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 | 6 ------ drivers/firmware/efi/libstub/arm64-stub.c | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 16 deletions(-)
arch/arm64/kernel/image-vars.h +0 -6
@@ -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
@@ -33,10 +31,6 @@ PROVIDE(__efistub_primary_entry = primary_entry);*/ 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
drivers/firmware/efi/libstub/arm64-stub.c +10 -10
@@ -10,7 +10,6 @@#include <linux/efi.h> #include <asm/efi.h> #include <asm/memory.h> -#include <asm/sections.h> #include "efistub.h"
@@ -21,22 +20,23 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle) { -unsigned long kernel_size, kernel_codesize, kernel_memsize; + auto image_info = efistub_get_image_info(); +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); -if (image->image_base != _text) { +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 ); - kernel_size = _edata - _text; - kernel_codesize = __inittext_end - _text; - kernel_memsize = kernel_size + (_end - _edata); *reserve_size = kernel_memsize; - *image_addr = (unsigned long )_text; + *image_addr = kernel_base; return efi_kaslr_relocate_kernel(image_addr, reserve_addr, reserve_size, kernel_size, kernel_codesize, kernel_memsize,
@@ -54,7 +54,7 @@ unsigned long primary_entry_offset(void)* 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; +return efistub_kernel_entry_offset (efistub_get_image_info ()); }void efi_icache_sync (unsigned long start,unsigned long end)
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Re: [PATCH v2 05/10] efi/libstub: arm64: Use struct efi_image_info to access image properties
Quoted 8 lines · Ard Biesheuvel
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.
[...]
Signature · 1 line
Jason
PATCH v2 6/10 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/image-vars.h | 1 - drivers/firmware/efi/libstub/loongarch-stub.c | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-)
arch/loongarch/kernel/image-vars.h +0 -1
@@ -8,7 +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 )
drivers/firmware/efi/libstub/loongarch-stub.c +1 -3
@@ -118,7 +118,5 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,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; +return kernel_addr +efistub_kernel_entry_offset (efistub_get_image_info ()); }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Re: [PATCH v2 06/10] efi/libstub: loongarch: Use struct efi_image_info to access image properties
drivers/firmware/efi/libstub/loongarch-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 122
Quoted 10 lines
[ ... 29 lines skipped ... ]
drivers/firmware/efi/libstub/loongarch-stub.c +1
-3@@ -118,7 +118,5 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,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; +return kernel_addr +efistub_kernel_entry_offset (efistub_get_image_info ()); }
This can be cleaned up the same way as primary_entry:
real_kernel_entry = efistub_kernel_ptr(image_info, entry); real_kernel_entry(true, (unsigned long)cmdline_ptr, (unsigned long)efi_system_table);
Signature · 1 line
Jason
PATCH v2 7/10 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 | 5 ----- drivers/firmware/efi/libstub/riscv-stub.c | 14 +++++++------- 2 files changed, 7 insertions(+), 12 deletions(-)
arch/riscv/kernel/image-vars.h +0 -5
@@ -23,11 +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
drivers/firmware/efi/libstub/riscv-stub.c +7 -7
@@ -6,7 +6,6 @@#include <linux/efi.h> #include <asm/efi.h> -#include <asm/sections.h> #include <linux/unaligned.h> #include "efistub.h"
@@ -20,7 +19,7 @@ unsigned long stext_offset(void)* 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; +return efistub_kernel_entry_offset (efistub_get_image_info ()); } efi_status_t handle_kernel_image(unsigned long *image_addr,
@@ -30,13 +29,14 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle) { -unsigned long kernel_size, kernel_codesize, kernel_memsize; + auto image_info = efistub_get_image_info(); +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);efi_status_t status; - kernel_size = _edata - _start; - kernel_codesize = __inittext_end - _start; - kernel_memsize = kernel_size + (_end - _edata); - *image_addr = (unsigned long )_start; + *image_addr = kernel_base; *reserve_size = kernel_memsize; status = efi_kaslr_relocate_kernel(image_addr,
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v2 07/10] efi/libstub: riscv: Use struct efi_image_info to access image properties
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Critical] efi/libstub: riscv: wrong image base and entry offset from _text --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Critical] efi/libstub: riscv: wrong image base and entry offset from _text
--
commit 66c3f5bb26141f8dc9e6a6b6eea665f0e9187c52
Author: Ard Biesheuvel <[email protected]>
efi/libstub: riscv: Use struct efi_image_info to access image properties
This commit converts the RISC-V EFI stub to use struct efi_image_info helpers instead of linker script symbol aliases to determine the kernel image base, sizes, and entry offset.
Quoted 21 lines
drivers/firmware/efi/libstub/riscv-stub.c +4
-2@@ -20,7 +19,7 @@ unsigned long stext_offset(void)* 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; +return efistub_kernel_entry_offset (efistub_get_image_info ()); } efi_status_t handle_kernel_image(unsigned long *image_addr,@@ -30,13 +29,14 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,efi_loaded_image_t *image, efi_handle_t image_handle) { -unsigned long kernel_size, kernel_codesize, kernel_memsize; + auto image_info = efistub_get_image_info(); +unsigned long kernel_base =efistub_kernel_base (image_info); +unsigned long kernel_size =efistub_kernel_size (image_info);
drivers/firmware/efi/libstub/riscv-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 40
Quoted 10 lines
drivers/firmware/efi/libstub/riscv-stub.c +3
-4+unsigned long kernel_codesize =efistub_kernel_codesize (image_info); +unsigned long kernel_memsize =efistub_kernel_memsize (image_info);efi_status_t status; - kernel_size = _edata - _start; - kernel_codesize = __inittext_end - _start; - kernel_memsize = kernel_size + (_end - _edata); - *image_addr = (unsigned long )_start; + *image_addr = kernel_base; *reserve_size = kernel_memsize;
[Severity: Critical]
Does using struct efi_image_info here base the RISC-V image calculations on _text instead of _start?
struct efi_image_info records _text_offset using REL_REF(_text), and the efistub_kernel_*() helpers compute the base address, image sizes, and efi_primary_entry (_start_kernel) offset relative to _text. On RISC-V, however, _start and the .head.text section (which contains _start_kernel) are placed before _text:
arch/riscv/kernel/vmlinux.lds.S:
_start = .;
HEAD_TEXT_SECTION
. = ALIGN(PAGE_SIZE);
.text : {
_text = .;
_stext = .;As a result, handle_kernel_image() sets *image_addr to _text and omits .head.text when efi_kaslr_relocate_kernel() checks alignment and copies the image, while stext_offset() returns _start_kernel - _text (a negative s32 offset that sign-extends to unsigned long), causing efi_enter_kernel() to jump before the start of the image buffer and crash at boot.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v2 07/10] efi/libstub: riscv: Use struct efi_image_info to access image properties
In reply to Ard Biesheuveldrivers/firmware/efi/libstub/riscv-stub.c unsigned long stext_offset(void) · line 23
Quoted 8 lines
[ ... 41 lines skipped ... ]
drivers/firmware/efi/libstub/riscv-stub.c +1
-1@@ -20,7 +19,7 @@ unsigned long stext_offset(void)* 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; +return efistub_kernel_entry_offset (efistub_get_image_info ()); }
Same primary_entry() transformation is possible here
Signature · 1 line
Jason
PATCH v2 8/10 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
@@ -1283,8 +1283,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 *reserve_addr, unsigned long *reserve_size,
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Re: [PATCH v2 08/10] efi/libstub: Make efi_remap_image() return efi_status_t
Quoted 10 lines · Ard Biesheuvel
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.
[...]
Signature · 1 line
Jason
PATCH v2 9/10 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
@@ -114,8 +114,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); }unsigned long __weak primary_entry_offset (void )
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
@@ -38,6 +38,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) {
@@ -105,7 +116,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_base, cmdline_ptr, + status =efi_zboot_remap_image (image_info, image_base, alloc_size) ?: +efi_stub_common (handle, image, image_base, cmdline_ptr, image_info); error:
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v2 09/10] efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/zboot: broken image_info offset and code_size in efi_zboot_remap_image() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/zboot: broken image_info offset and code_size in efi_zboot_remap_image()
--
commit 51947c84bec76536d55cfa9120873713a58ae31b
Author: Ard Biesheuvel <[email protected]>
efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()
This commit moves the efi_cache_sync_image() and efi_remap_image() calls out of the architecture-specific and decompressor paths into a new efi_zboot_remap_image() helper invoked from efi_zboot_entry().
Quoted 23 lines
drivers/firmware/efi/libstub/zboot.c +11
-0@@ -38,6 +38,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) {
drivers/firmware/efi/libstub/zboot.c efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) · line 121
Quoted 7 lines
drivers/firmware/efi/libstub/zboot.c +2
-1@@ -105,7 +116,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_base, cmdline_ptr, + status =efi_zboot_remap_image (image_info, image_base, alloc_size) ?: +efi_stub_common (handle, image, image_base, cmdline_ptr, image_info);
[Severity: High]
Does calling efi_zboot_remap_image() here cause a boot regression on RISC-V CONFIG_EFI_ZBOOT kernels when EFI_MEMORY_ATTRIBUTE_PROTOCOL is present?
Previously, efi_remap_image() in the zboot path was only called on arm64, and zboot.c never dereferenced image_info. Now efi_zboot_entry() calls efi_zboot_remap_image() for all zboot architectures and aborts the boot if it fails.
On RISC-V, the binary Image decompressed to image_base starts at _start, while _text is placed PAGE_SIZE bytes later, and __inittext_end is defined before section and PE/COFF alignment:
arch/riscv/kernel/vmlinux.lds.S:
_start = .;
HEAD_TEXT_SECTION
. = ALIGN(PAGE_SIZE);
.text : {
_text = .;
...
__inittext_end = .;
. = ALIGN(SECTION_ALIGN);
#ifdef CONFIG_EFI
. = ALIGN(PECOFF_SECTION_ALIGNMENT);
__pecoff_text_end = .;#endif
Because get_image_info_offset in drivers/firmware/efi/libstub/Makefile.zboot computes efistub_image_info_offset relative to _text, zboot_get_image_info(image_base) points PAGE_SIZE bytes before the actual struct efi_image_info, so efistub_kernel_codesize(image_info) reads unrelated image bytes as code_size. Even with the offset fixed, __inittext_end - _text omits the leading PAGE_SIZE bytes between _start and _text and is not page-aligned, causing efi_remap_image() to fail with EFI_INVALID_PARAMETER or remap the wrong range.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v2 09/10] efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()
In reply to Ard BiesheuvelQuoted 8 lines · Ard Biesheuvel
Refactor
efi_cache_sync_image()so that it is not in charge of callingefi_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.[...]
Signature · 1 line
Jason
PATCH v2 10/10 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
@@ -144,10 +144,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.0.542.g3ef89b7020-goog
Re: [PATCH v2 10/10] efi/libstub: arm64: Drop zboot code_size hack
Quoted 3 lines · Ard Biesheuvel
Now that the code size can be obtained via the generic
image_infostruct, there is no longer a need to inject it explicitly during the build.
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.
DRTM still works after being rebased on top of this.
I left some ideas for further improvement, but other than the .init.rodata marking nothing is critical.
Thanks for doing this
Signature · 1 line
Jason