x86: strict separation of startup code
Subsystems (3)
cover x86: strict separation of startup code
From: Ard Biesheuvel <[email protected]>
This is a proof-of-concept series that implements a strict separation between startup code and ordinary code, where startup code is built in a way that tolerates being invoked from the initial 1:1 mapping of memory.
The current approach of emitting this code into .head.text and checking for absolute relocations in that section is not 100% safe, and produces diagnostics that are sometimes difficult to interpret.
Instead, rely on symbol prefixes, similar to how this is implemented for the EFI stub and for the startup code in the arm64 port. This ensures that startup code can only call other startup code, unless a special symbol alias is emitted that exposes a non-startup routine to the startup code.
This is somewhat intrusive, as there are many data objects that are referenced both by startup code and by ordinary code, and an alias needs to be emitted for each of those.
This ultimately allows the .head.text section to be dropped entirely, as it no longer has a special significance. Instead, code that only executes at boot is emitted into .init.text as it should.
This series is presented for discussion only - defconfig should build and run correctly, but allmodconfig will likely need the last patch omitted.
Ard Biesheuvel (11):
x86/linkage: Add SYM_PI_ALIAS() macro helper to emit symbol aliases x86/boot: Move early_setup_gdt() back into head64.c x86/boot: Disregard __supported_pte_mask in __startup_64() x86/boot: Add a bunch of PI aliases
HACK: provide __pti_set_user_pgtbl() to startup code x86/boot: Created a confined code area for startup code
HACK: work around sev-startup.c being omitted for now x86/boot: Move startup code out of __head section x86/boot: Disallow absolute symbol references in startup code x86/boot: Revert "Reject absolute references in .head.text" x86/boot: Get rid of the .head.text section
arch/x86/boot/startup/Makefile | 26 ++++++++++++++-- arch/x86/boot/startup/gdt_idt.c | 17 ++--------- arch/x86/boot/startup/map_kernel.c | 6 ++-- arch/x86/boot/startup/sev-startup.c | 3 ++ arch/x86/boot/startup/sme.c | 31 ++++++++++++-------- arch/x86/coco/core.c | 2 ++ arch/x86/include/asm/linkage.h | 6 ++++ arch/x86/include/asm/setup.h | 2 ++ arch/x86/include/asm/sev.h | 2 +- arch/x86/kernel/cpu/common.c | 1 + arch/x86/kernel/head64.c | 19 ++++++++++++ arch/x86/kernel/head_32.S | 2 +- arch/x86/kernel/head_64.S | 16 +++++++--- arch/x86/kernel/setup.c | 1 + arch/x86/kernel/vmlinux.lds.S | 9 +++--- arch/x86/lib/retpoline.S | 1 + arch/x86/mm/mem_encrypt_amd.c | 2 ++ arch/x86/mm/mem_encrypt_boot.S | 6 ++-- arch/x86/mm/pgtable.c | 1 + arch/x86/platform/pvh/head.S | 2 +- arch/x86/tools/relocs.c | 8 +---- 21 files changed, 107 insertions(+), 56 deletions(-)
base-commit: 121c335b36e02d6aefb72501186e060474fdf33c
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 1/11 x86/linkage: Add SYM_PI_ALIAS() macro helper to emit symbol aliases
From: Ard Biesheuvel <[email protected]>
Startup code that may execute from the early 1:1 mapping of memory will be confined into its own address space, and only be permitted to access ordinary kernel symbols if this is known to be safe.
Introduce a macro helper PI_ALIAS() that emits a __pi_ prefixed alias for a symbol, which allows startup code to access it.
--- arch/x86/include/asm/linkage.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/linkage.h +6 -0
@@ -141,5 +141,11 @@#define SYM_FUNC_START_WEAK_NOALIGN (name) \ SYM_START(name, SYM_L_WEAK, SYM_A_NONE) +#ifdef __ASSEMBLER__ +#define SYM_PI_ALIAS (sym) SYM_ALIAS(__pi_ ## sym, sym, SYM_L_GLOBAL) +#else +#define SYM_PI_ALIAS (sym) extern typeof(sym) __PASTE(__pi_, sym) __alias(sym) +#endif +#endif /* _ASM_X86_LINKAGE_H */
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH PoC 01/11] x86/linkage: Add SYM_PI_ALIAS() macro helper to emit symbol aliases
Quoted 8 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Startup code that may execute from the early 1:1 mapping of memory will be confined into its own address space, and only be permitted to access ordinary kernel symbols if this is known to be safe.
Introduce a macro helper
PI_ALIAS()that emits a__pi_prefixed alias for a symbol, which allows startup code to access it.
s/PI_ALIAS
/SYM_PI_ALIAS
What does 'PI' stand for? 'Physical memory Identity' map?
Thanks,
Ingo
Re: [RFC PATCH PoC 01/11] x86/linkage: Add SYM_PI_ALIAS() macro helper to emit symbol aliases
Quoted 10 lines · Ingo Molnar
Quoted 6 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Startup code that may execute from the early 1:1 mapping of memory will be confined into its own address space, and only be permitted to access ordinary kernel symbols if this is known to be safe.
Quoted 8 lines
Quoted 2 lines
Introduce a macro helper
PI_ALIAS()that emits a__pi_prefixed alias for a symbol, which allows startup code to access it.s/PI_ALIAS
/SYM_PI_ALIASWhat does 'PI' stand for? 'Physical memory Identity' map?
'position independent' - it's what we ended up with on arm64, but I'm not attached to it so happy to switch to something better.
Re: [RFC PATCH PoC 01/11] x86/linkage: Add SYM_PI_ALIAS() macro helper to emit symbol aliases
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
Quoted 8 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Startup code that may execute from the early 1:1 mapping of memory will be confined into its own address space, and only be permitted to access ordinary kernel symbols if this is known to be safe.
Introduce a macro helper
PI_ALIAS()that emits a__pi_prefixed alias for a symbol, which allows startup code to access it.
Quoted 7 lines
Quoted 5 lines
s/PI_ALIAS
/SYM_PI_ALIASWhat does 'PI' stand for? 'Physical memory Identity' map?
'position independent'
/facepalm
Clearly it's getting late here :)
Quoted 2 lines
- it's what we ended up with on arm64, but I'm not attached to it so happy to switch to something better.
Could we make it something like SYM_PIC_ALIAS() at least? Because 'PIC' is something most people will recognize in this context. PI goes for 3.1415. ;-)
Thanks,
Ingo
RFC PATCH 2/11 x86/boot: Move early_setup_gdt() back into head64.c
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move early_setup_gdt() out of the startup code that is callable from the 1:1 mapping - this is not needed, and instead, it is better to expose the helper that does reside in __head directly. This reduces the amount of code that needs special checks for 1:1 execution suitability.
--- arch/x86/boot/startup/gdt_idt.c | 15 +-------------- arch/x86/include/asm/setup.h | 1 + arch/x86/kernel/head64.c | 12 ++++++++++++ 3 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/boot/startup/gdt_idt.c +1 -14
@@ -24,7 +24,7 @@ static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;/* This may run while still in the direct mapping */ -static void __head startup_64_load_idt(void *vc_handler) +void __head startup_64_load_idt(void *vc_handler) {struct desc_ptr desc = { .address = (unsigned long )rip_rel_ptr (bringup_idt_table),
@@ -43,19 +43,6 @@ static void __head startup_64_load_idt(void *vc_handler)native_load_idt (&desc); } -/* This is used when running on kernel addresses */ -void early_setup_idt (void ) -{ -void *handler =NULL ; - -if (IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) { -setup_ghcb (); - handler = vc_boot_ghcb; - } - -startup_64_load_idt (handler); -} -/* * Setup boot CPU state needed before kernel switches to virtual addresses. */
arch/x86/include/asm/setup.h +1 -0
@@ -52,6 +52,7 @@ extern void reserve_standard_io_resources(void);extern void i386_reserve_resources (void );extern unsigned long __startup_64 (unsigned long p2v_offset,struct boot_params *bp);extern void startup_64_setup_gdt_idt (void ); +extern void startup_64_load_idt (void *vc_handler);extern void early_setup_idt (void );extern void __init do_early_exception (struct pt_regs *regs,int trapnr);
arch/x86/kernel/head64.c +12 -0
@@ -303,3 +303,15 @@ void __init __noreturn x86_64_start_reservations(char *real_mode_data)start_kernel (); } + +void early_setup_idt (void ) +{ +void *handler =NULL ; + +if (IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) { +setup_ghcb (); + handler = vc_boot_ghcb; + } + +startup_64_load_idt (handler); +}
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 3/11 x86/boot: Disregard __supported_pte_mask in __startup_64()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
__supported_pte_mask is statically initialized to U64_MAX and never assigned until long after the startup code executes that creates the initial page tables. So applying the mask is unnecessary, and can be avoided.
--- arch/x86/boot/startup/map_kernel.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/boot/startup/map_kernel.c +0 -2
@@ -179,8 +179,6 @@ unsigned long __head __startup_64(unsigned long p2v_offset, pud[(i +1 ) %PTRS_PER_PUD ] = (pudval_t )pmd + pgtable_flags; pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL; -/* Filter out unsupported __PAGE_KERNEL_* bits: */ - pmd_entry &= __supported_pte_mask; pmd_entry +=sme_get_me_mask (); pmd_entry += physaddr;
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 4/11 x86/boot: Add a bunch of PI aliases
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Add aliases for all the data objects that the startup code references - this is needed so that this code can be moved into its own confined area where it can only access symbols that have a __pi_ prefix.
--- arch/x86/coco/core.c | 2 ++ arch/x86/kernel/cpu/common.c | 1 + arch/x86/kernel/head64.c | 7 +++++++ arch/x86/kernel/head_64.S | 8 ++++++++ arch/x86/kernel/setup.c | 1 + arch/x86/kernel/vmlinux.lds.S | 4 ++++ arch/x86/lib/retpoline.S | 1 + arch/x86/mm/mem_encrypt_amd.c | 2 ++ arch/x86/mm/pgtable.c | 1 + 9 files changed, 27 insertions(+)
arch/x86/coco/core.c +2 -0
@@ -18,7 +18,9 @@#include <asm/processor.h> enum cc_vendor cc_vendor__ro_after_init =CC_VENDOR_NONE ; +SYM_PI_ALIAS (cc_vendor);u64 cc_mask__ro_after_init ; +SYM_PI_ALIAS (cc_mask); static struct cc_attr_flags { __u64 host_sev_snp : 1,
arch/x86/kernel/cpu/common.c +1 -0
@@ -242,6 +242,7 @@ DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {#endif } };EXPORT_PER_CPU_SYMBOL_GPL (gdt_page); +SYM_PI_ALIAS (gdt_page);#ifdef CONFIG_X86_64 static int __init x86_nopcid_setup (char *s)
arch/x86/kernel/head64.c +7 -0
@@ -48,23 +48,30 @@*/ extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES ][PTRS_PER_PMD ];unsigned int __initdata next_early_pgt; +SYM_PI_ALIAS (next_early_pgt);pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);#ifdef CONFIG_X86_5LEVEL unsigned int __pgtable_l5_enabled__ro_after_init ; +SYM_PI_ALIAS (__pgtable_l5_enabled);unsigned int pgdir_shift__ro_after_init =39 ;EXPORT_SYMBOL (pgdir_shift); +SYM_PI_ALIAS (pgdir_shift);unsigned int ptrs_per_p4d__ro_after_init =1 ;EXPORT_SYMBOL (ptrs_per_p4d); +SYM_PI_ALIAS (ptrs_per_p4d);#endif #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT unsigned long page_offset_base__ro_after_init = __PAGE_OFFSET_BASE_L4;EXPORT_SYMBOL (page_offset_base); +SYM_PI_ALIAS (page_offset_base);unsigned long vmalloc_base__ro_after_init = __VMALLOC_BASE_L4;EXPORT_SYMBOL (vmalloc_base); +SYM_PI_ALIAS (vmalloc_base);unsigned long vmemmap_base__ro_after_init = __VMEMMAP_BASE_L4;EXPORT_SYMBOL (vmemmap_base); +SYM_PI_ALIAS (vmemmap_base);#endif /* Wipe all early page tables except for the kernel symbol map */
arch/x86/kernel/head_64.S +8 -0
@@ -573,6 +573,7 @@ SYM_CODE_START_NOALIGN(vc_no_ghcb) /* Pure iret required here - don't use INTERRUPT_RETURN */ iretq SYM_CODE_END(vc_no_ghcb) +SYM_PI_ALIAS(vc_no_ghcb); #endif #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
@@ -604,10 +605,12 @@ SYM_DATA_START_PTI_ALIGNED(early_top_pgt) .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC .fill PTI_USER_PGD_FILL,8,0 SYM_DATA_END(early_top_pgt) +SYM_PI_ALIAS(early_top_pgt) SYM_DATA_START_PAGE_ALIGNED(early_dynamic_pgts) .fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0 SYM_DATA_END(early_dynamic_pgts) +SYM_PI_ALIAS(early_dynamic_pgts); SYM_DATA(early_recursion_flag, .long 0)
@@ -651,6 +654,7 @@ SYM_DATA_START_PAGE_ALIGNED(level4_kernel_pgt) .fill 511,8,0 .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC SYM_DATA_END(level4_kernel_pgt) +SYM_PI_ALIAS(level4_kernel_pgt) #endif SYM_DATA_START_PAGE_ALIGNED(level3_kernel_pgt)
@@ -659,6 +663,7 @@ SYM_DATA_START_PAGE_ALIGNED(level3_kernel_pgt) .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC SYM_DATA_END(level3_kernel_pgt) +SYM_PI_ALIAS(level3_kernel_pgt) SYM_DATA_START_PAGE_ALIGNED(level2_kernel_pgt) /*
@@ -676,6 +681,7 @@ SYM_DATA_START_PAGE_ALIGNED(level2_kernel_pgt) */ PMDS(0, __PAGE_KERNEL_LARGE_EXEC, KERNEL_IMAGE_SIZE/PMD_SIZE) SYM_DATA_END(level2_kernel_pgt) +SYM_PI_ALIAS(level2_kernel_pgt) SYM_DATA_START_PAGE_ALIGNED(level2_fixmap_pgt) .fill (512 - 4 - FIXMAP_PMD_NUM),8,0
@@ -688,6 +694,7 @@ SYM_DATA_START_PAGE_ALIGNED(level2_fixmap_pgt) /* 6 MB reserved space + a 2MB hole */ .fill 4,8,0 SYM_DATA_END(level2_fixmap_pgt) +SYM_PI_ALIAS(level2_fixmap_pgt) SYM_DATA_START_PAGE_ALIGNED(level1_fixmap_pgt) .rept (FIXMAP_PMD_NUM)
@@ -703,6 +710,7 @@ SYM_DATA(smpboot_control, .long 0) .align 16 /* This must match the first entry in level2_kernel_pgt */ SYM_DATA(phys_base, .quad 0x0) +SYM_PI_ALIAS(phys_base); EXPORT_SYMBOL(phys_base) #include "../xen/xen-head.S"
arch/x86/kernel/setup.c +1 -0
@@ -134,6 +134,7 @@ struct ist_info ist_info;struct cpuinfo_x86 boot_cpu_data__read_mostly ;EXPORT_SYMBOL (boot_cpu_data); +SYM_PI_ALIAS (boot_cpu_data);#if !defined(CONFIG_X86_PAE ) || defined(CONFIG_X86_64 )__visible unsigned long mmu_cr4_features__ro_after_init ;
arch/x86/kernel/vmlinux.lds.S +4 -0
@@ -79,11 +79,13 @@ const_cpu_current_top_of_stack = cpu_current_top_of_stack; #define BSS_DECRYPTED \ . = ALIGN(PMD_SIZE); \ __start_bss_decrypted = .; \ + __pi___start_bss_decrypted = .; \ *(.bss..decrypted); \ . = ALIGN(PAGE_SIZE); \ __start_bss_decrypted_unused = .; \ . = ALIGN(PMD_SIZE); \ __end_bss_decrypted = .; \ + __pi___end_bss_decrypted = .; \ #else
@@ -128,6 +130,7 @@ SECTIONS /* Text and read-only data */ .text : AT(ADDR(.text) - LOAD_OFFSET) { _text = .; + __pi__text = .; _stext = .; ALIGN_ENTRY_TEXT_BEGIN *(.text..__x86.rethunk_untrain)
@@ -391,6 +394,7 @@ SECTIONS . = ALIGN(PAGE_SIZE); /* keep VO_INIT_SIZE page aligned */ _end = .; + __pi__end = .; #ifdef CONFIG_AMD_MEM_ENCRYPT /*
arch/x86/lib/retpoline.S +1 -0
@@ -394,6 +394,7 @@ SYM_CODE_START(__x86_return_thunk) #endif int3 SYM_CODE_END(__x86_return_thunk) +SYM_PI_ALIAS(__x86_return_thunk) EXPORT_SYMBOL(__x86_return_thunk) #endif /* CONFIG_MITIGATION_RETHUNK */
arch/x86/mm/mem_encrypt_amd.c +2 -0
@@ -40,7 +40,9 @@* section is later cleared. */ u64 sme_me_mask __section(".data") = 0; +SYM_PI_ALIAS (sme_me_mask); u64 sev_status __section(".data") = 0; +SYM_PI_ALIAS (sev_status); u64 sev_check_data __section(".data") = 0;EXPORT_SYMBOL (sme_me_mask);
arch/x86/mm/pgtable.c +1 -0
@@ -10,6 +10,7 @@#ifdef CONFIG_DYNAMIC_PHYSICAL_MASK phys_addr_t physical_mask__ro_after_init = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ;EXPORT_SYMBOL (physical_mask); +SYM_PI_ALIAS (physical_mask);#endif pgtable_t pte_alloc_one (struct mm_struct *mm)
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 5/11 HACK: provide __pti_set_user_pgtbl() to startup code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The SME startup code may call out to pti_set_user_pgtbl(), which is not part of the code corpus that is explicitly built to tolerate execution from the 1:1 mapping of memory.
Hack around this for now by providing an alternative that just returns the pgd.
--- arch/x86/boot/startup/sme.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/boot/startup/sme.c +5 -0
@@ -564,3 +564,8 @@ void __head sme_enable(struct boot_params *bp) cc_vendor =CC_VENDOR_AMD ;cc_set_mask (me_mask); } + +pgd_t __pti_set_user_pgtbl (pgd_t *pgdp,pgd_t pgd) +{ +return pgd; +}
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 6/11 x86/boot: Created a confined code area for startup code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
In order to be able to have tight control over which code may execute from the early 1:1 mapping of memory, but still link vmlinux as a single executable, prefix all symbol references in startup code with __pi_, and invoke it from outside using the __pi_ prefix.
HACK: omit sev-status.c for the time being - disentangling that is rather challenging, and not necessary for a proof of concept implementation.
--- arch/x86/boot/startup/Makefile | 18 ++++++++++++++++-- arch/x86/include/asm/setup.h | 1 + arch/x86/kernel/head64.c | 2 +- arch/x86/kernel/head_64.S | 6 +++--- arch/x86/mm/mem_encrypt_boot.S | 6 +++--- 5 files changed, 24 insertions(+), 9 deletions(-)
arch/x86/boot/startup/Makefile +16 -2
@@ -17,8 +17,9 @@ KMSAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n -obj-$(CONFIG_X86_64) += gdt_idt.o map_kernel.o -obj-$(CONFIG_AMD_MEM_ENCRYPT) += sme.o sev-startup.o +pi-obj-$(CONFIG_X86_64) += gdt_idt.o map_kernel.o +pi-obj-$(CONFIG_AMD_MEM_ENCRYPT) += sme.o #sev-startup.o +obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev-startup.o lib-$(CONFIG_X86_64) += la57toggle.o lib-$(CONFIG_EFI_MIXED) += efi-mixed.o
@@ -28,3 +29,16 @@ lib-$(CONFIG_EFI_MIXED) += efi-mixed.o # to be linked into the decompressor or the EFI stub but not vmlinux # $(patsubst %.o,$(obj)/%.o,$(lib-y)): OBJECT_FILES_NON_STANDARD := y + +# +# Confine the startup code by prefixing all symbols with __pi_ (for position +# independent). This ensures that startup code can only call other startup +# code, or code that has explicitly been made accessible to it via a symbol +# alias. +# +$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ +$(obj)/%.pi.o: $(obj)/%.o FORCE + $(call if_changed,objcopy) + +extra-y := $(pi-obj-y) +obj-y += $(patsubst %.o,%.pi.o,$(pi-obj-y))
arch/x86/include/asm/setup.h +1 -0
@@ -53,6 +53,7 @@ extern void i386_reserve_resources(void);extern unsigned long __startup_64 (unsigned long p2v_offset,struct boot_params *bp);extern void startup_64_setup_gdt_idt (void );extern void startup_64_load_idt (void *vc_handler); +extern void __pi_startup_64_load_idt (void *vc_handler);extern void early_setup_idt (void );extern void __init do_early_exception (struct pt_regs *regs,int trapnr);
arch/x86/kernel/head64.c +1 -1
@@ -320,5 +320,5 @@ void early_setup_idt(void) handler = vc_boot_ghcb; } -startup_64_load_idt (handler); +__pi_startup_64_load_idt (handler); }
arch/x86/kernel/head_64.S +3 -3
@@ -71,7 +71,7 @@ SYM_CODE_START_NOALIGN(startup_64) xorl %edx, %edx wrmsr - call startup_64_setup_gdt_idt + call __pi_startup_64_setup_gdt_idt /* Now switch to __KERNEL_CS so IRET works reliably */ pushq $__KERNEL_CS
@@ -91,7 +91,7 @@ SYM_CODE_START_NOALIGN(startup_64) * subsequent code. Pass the boot_params pointer as the first argument. */ movq %r15, %rdi - call sme_enable + call __pi_sme_enable #endif /* Sanitize CPU configuration */
@@ -111,7 +111,7 @@ SYM_CODE_START_NOALIGN(startup_64) * programmed into CR3. */ movq %r15, %rsi - call __startup_64 + call __pi___startup_64 /* Form the CR3 value being sure to include the CR3 modifier */ leaq early_top_pgt(%rip), %rcx
arch/x86/mm/mem_encrypt_boot.S +3 -3
@@ -16,7 +16,7 @@ .text .code64 -SYM_FUNC_START(sme_encrypt_execute) +SYM_FUNC_START(__pi_sme_encrypt_execute) /* * Entry parameters:
@@ -69,9 +69,9 @@ SYM_FUNC_START(sme_encrypt_execute) ANNOTATE_UNRET_SAFE ret int3 -SYM_FUNC_END(sme_encrypt_execute) +SYM_FUNC_END(__pi_sme_encrypt_execute) -SYM_FUNC_START(__enc_copy) +SYM_FUNC_START_LOCAL(__enc_copy) ANNOTATE_NOENDBR /* * Routine used to encrypt memory in place.
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 7/11 HACK: work around sev-startup.c being omitted for now
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Add some PI aliases that shouldn't be needed once sev-startup.c is also built with __pi_ aliases.
--- arch/x86/boot/startup/sev-startup.c | 3 +++ arch/x86/include/asm/sev.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/boot/startup/sev-startup.c +3 -0
@@ -562,6 +562,7 @@ void __head early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr/* Ask hypervisor to mark the memory pages shared in the RMP table. */ early_set_pages_state (vaddr, paddr, npages,SNP_PAGE_STATE_SHARED ); } +SYM_PI_ALIAS (early_snp_set_memory_shared);/* Writes to the SVSM CAA MSR are ignored */ static enum es_result __vc_handle_msr_caa (struct pt_regs *regs,bool write)
@@ -1383,8 +1384,10 @@ bool __head snp_init(struct boot_params *bp)return true; } +SYM_PI_ALIAS (snp_init); void __head __noreturn snp_abort(void) {sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED ); } +SYM_PI_ALIAS (snp_abort);
arch/x86/include/asm/sev.h +1 -1
@@ -494,6 +494,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);void snp_set_wakeup_secondary_cpu (void );bool snp_init (struct boot_params *bp);void __noreturn snp_abort (void ); +void __noreturn __pi_snp_abort (void );void snp_dmi_setup (void );int snp_issue_svsm_attest_req (u64 call_id,struct svsm_call *call,struct svsm_attest_call *input);void snp_accept_memory (phys_addr_t start,phys_addr_t end);
@@ -541,7 +542,6 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npagstatic inline void snp_set_memory_private (unsigned long vaddr,unsigned long npages) { }static inline void snp_set_wakeup_secondary_cpu (void ) { }static inline bool snp_init (struct boot_params *bp) {return false; } -static inline void snp_abort (void ) { }static inline void snp_dmi_setup (void ) { }static inline int snp_issue_svsm_attest_req (u64 call_id,struct svsm_call *call,struct svsm_attest_call *input) {
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 8/11 x86/boot: Move startup code out of __head section
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move startup code out of the __head section, now that this no longer has a special significance. Move everything into .text or .init.text as appropriate, so that startup code is not kept around unnecessarily.
--- arch/x86/boot/startup/gdt_idt.c | 4 +-- arch/x86/boot/startup/map_kernel.c | 4 +-- arch/x86/boot/startup/sme.c | 26 ++++++++++---------- arch/x86/kernel/head_32.S | 2 +- arch/x86/kernel/head_64.S | 2 +- arch/x86/platform/pvh/head.S | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-)
arch/x86/boot/startup/gdt_idt.c +2 -2
@@ -24,7 +24,7 @@ static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;/* This may run while still in the direct mapping */ -void __head startup_64_load_idt(void *vc_handler) +void startup_64_load_idt (void *vc_handler) {struct desc_ptr desc = { .address = (unsigned long )rip_rel_ptr (bringup_idt_table),
@@ -46,7 +46,7 @@ void __head startup_64_load_idt(void *vc_handler)/* * Setup boot CPU state needed before kernel switches to virtual addresses. */ -void __head startup_64_setup_gdt_idt(void) +void __init startup_64_setup_gdt_idt (void ) {struct gdt_page *gp =rip_rel_ptr ((void *)(__force unsigned long )&gdt_page);void *handler =NULL ;
arch/x86/boot/startup/map_kernel.c +2 -2
@@ -36,7 +36,7 @@ static inline bool check_la57_support(void)return true; } -static unsigned long __head sme_postprocess_startup (struct boot_params *bp, +static unsigned long __init sme_postprocess_startup (struct boot_params *bp,pmdval_t *pmd,unsigned long p2v_offset) {
@@ -90,7 +90,7 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp,* the 1:1 mapping of memory. Kernel virtual addresses can be determined by * subtracting p2v_offset from the RIP-relative address. */ -unsigned long __head __startup_64 (unsigned long p2v_offset, +unsigned long __init __startup_64 (unsigned long p2v_offset,struct boot_params *bp) {pmd_t (*early_pgts)[PTRS_PER_PMD ] =rip_rel_ptr (early_dynamic_pgts);
arch/x86/boot/startup/sme.c +13 -13
@@ -91,7 +91,7 @@ struct sme_populate_pgd_data {*/ static char sme_workarea[2 * PMD_SIZE] __section(".init.scratch"); -static void __head sme_clear_pgd(struct sme_populate_pgd_data *ppd) +static void __init sme_clear_pgd (struct sme_populate_pgd_data *ppd) {unsigned long pgd_start, pgd_end, pgd_size;pgd_t *pgd_p;
@@ -106,7 +106,7 @@ static void __head sme_clear_pgd(struct sme_populate_pgd_data *ppd)memset (pgd_p,0 , pgd_size); } -static pud_t __head *sme_prepare_pgd(struct sme_populate_pgd_data *ppd) +static pud_t __init *sme_prepare_pgd (struct sme_populate_pgd_data *ppd) {pgd_t *pgd;p4d_t *p4d;
@@ -143,7 +143,7 @@ static pud_t __head *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)return pud; } -static void __head sme_populate_pgd_large(struct sme_populate_pgd_data *ppd) +static void __init sme_populate_pgd_large (struct sme_populate_pgd_data *ppd) {pud_t *pud;pmd_t *pmd;
@@ -159,7 +159,7 @@ static void __head sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)set_pmd (pmd,__pmd (ppd->paddr | ppd->pmd_flags)); } -static void __head sme_populate_pgd(struct sme_populate_pgd_data *ppd) +static void __init sme_populate_pgd (struct sme_populate_pgd_data *ppd) {pud_t *pud;pmd_t *pmd;
@@ -185,7 +185,7 @@ static void __head sme_populate_pgd(struct sme_populate_pgd_data *ppd)set_pte (pte,__pte (ppd->paddr | ppd->pte_flags)); } -static void __head __sme_map_range_pmd(struct sme_populate_pgd_data *ppd) +static void __init __sme_map_range_pmd (struct sme_populate_pgd_data *ppd) {while (ppd->vaddr < ppd->vaddr_end) {sme_populate_pgd_large (ppd);
@@ -195,7 +195,7 @@ static void __head __sme_map_range_pmd(struct sme_populate_pgd_data *ppd) } } -static void __head __sme_map_range_pte(struct sme_populate_pgd_data *ppd) +static void __init __sme_map_range_pte (struct sme_populate_pgd_data *ppd) {while (ppd->vaddr < ppd->vaddr_end) {sme_populate_pgd (ppd);
@@ -205,7 +205,7 @@ static void __head __sme_map_range_pte(struct sme_populate_pgd_data *ppd) } } -static void __head __sme_map_range(struct sme_populate_pgd_data *ppd, +static void __init __sme_map_range (struct sme_populate_pgd_data *ppd,pmdval_t pmd_flags,pteval_t pte_flags) {unsigned long vaddr_end;
@@ -229,22 +229,22 @@ static void __head __sme_map_range(struct sme_populate_pgd_data *ppd,__sme_map_range_pte (ppd); } -static void __head sme_map_range_encrypted(struct sme_populate_pgd_data *ppd) +static void __init sme_map_range_encrypted (struct sme_populate_pgd_data *ppd) {__sme_map_range (ppd,PMD_FLAGS_ENC ,PTE_FLAGS_ENC ); } -static void __head sme_map_range_decrypted(struct sme_populate_pgd_data *ppd) +static void __init sme_map_range_decrypted (struct sme_populate_pgd_data *ppd) {__sme_map_range (ppd,PMD_FLAGS_DEC ,PTE_FLAGS_DEC ); } -static void __head sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd) +static void __init sme_map_range_decrypted_wp (struct sme_populate_pgd_data *ppd) {__sme_map_range (ppd,PMD_FLAGS_DEC_WP ,PTE_FLAGS_DEC_WP ); } -static unsigned long __head sme_pgtable_calc (unsigned long len) +static unsigned long __init sme_pgtable_calc (unsigned long len) {unsigned long entries =0 , tables =0 ;
@@ -281,7 +281,7 @@ static unsigned long __head sme_pgtable_calc(unsigned long len)return entries + tables; } -void __head sme_encrypt_kernel(struct boot_params *bp) +void __init sme_encrypt_kernel (struct boot_params *bp) {unsigned long workarea_start, workarea_end, workarea_len;unsigned long execute_start, execute_end, execute_len;
@@ -485,7 +485,7 @@ void __head sme_encrypt_kernel(struct boot_params *bp)native_write_cr3 (__native_read_cr3 ()); } -void __head sme_enable(struct boot_params *bp) +void __init sme_enable (struct boot_params *bp) {unsigned int eax, ebx, ecx, edx;unsigned long feature_mask;
arch/x86/kernel/head_32.S +1 -1
@@ -61,7 +61,7 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE) * any particular GDT layout, because we load our own as soon as we * can. */ -__HEAD + __INIT SYM_CODE_START(startup_32) movl pa(initial_stack),%ecx
arch/x86/kernel/head_64.S +1 -1
@@ -33,7 +33,7 @@ * because we need identity-mapped pages. */ - __HEAD + __INIT .code64 SYM_CODE_START_NOALIGN(startup_64) UNWIND_HINT_END_OF_STACK
arch/x86/platform/pvh/head.S +1 -1
@@ -24,7 +24,7 @@ #include <asm/nospec-branch.h> #include <xen/interface/elfnote.h> - __HEAD + __INIT /* * Entry point for PVH guests.
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
kernel test robot
build botRe: [RFC PATCH PoC 08/11] x86/boot: Move startup code out of __head section
kernel test robot noticed the following build errors:
Hi Ard,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on 121c335b36e02d6aefb72501186e060474fdf33c]
url: https://github.com/intel-lab-lkp/linux/commits/Ard-Biesheuvel/x86-linkage-Add-SYM_PI_ALIAS-macro-helper-to-emit-symbol-aliases/20250423-191433 base: 121c335b36e02d6aefb72501186e060474fdf33c patch link: https://lore.kernel.org/r/20250423110948.1103030-21-ardb%2Bgit%40google.com patch subject: [RFC PATCH PoC 08/11] x86/boot: Move startup code out of __head section config: x86_64-buildonly-randconfig-006-20250424 (https://download.01.org/0day-ci/archive/20250424/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250424/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> ld: Unexpected GOT entries detected! ld: vmlinux.o: in function `__pi_startup_64_load_idt': >> (.text+0x1525c3): undefined reference to `__pi_memset' >> ld: (.text+0x15262d): undefined reference to `__pi_memcpy' ld: vmlinux.o: in function `__pi_startup_64_setup_gdt_idt': >> (.init.text+0x42254): undefined reference to `__pi_latent_entropy'
ld: vmlinux.o: in function `__pi___startup_64':
(.init.text+0x422d5): undefined reference to `__pi_latent_entropy'
ld: .tmp_vmlinux1: hidden symbol `__pi_memcpy' isn't defined
ld: final link failed: bad value
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH 9/11 x86/boot: Disallow absolute symbol references in startup code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Check that the objects built under arch/x86/boot/startup do not contain any absolute symbol reference. Given that the code is built with -fPIC, such references can only be emitted using R_X86_64_64 relocations, so checking that those are absent is sufficient.
Note that debug sections and __patchable_funtion_entries section may contain such relocations nonetheless, but these are unnecessary in the startup code, so they can be dropped first.
--- arch/x86/boot/startup/Makefile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/boot/startup/Makefile +10 -2
@@ -36,9 +36,17 @@ $(patsubst %.o,$(obj)/%.o,$(lib-y)): OBJECT_FILES_NON_STANDARD := y # code, or code that has explicitly been made accessible to it via a symbol # alias. # -$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ +$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ --strip-debug \ + --remove-section=.rela__patchable_function_entries $(obj)/%.pi.o: $(obj)/%.o FORCE - $(call if_changed,objcopy) + $(call if_changed,piobjcopy) + +quiet_cmd_piobjcopy = $(quiet_cmd_objcopy) + cmd_piobjcopy = $(cmd_objcopy); \ + if $(READELF) -r $(@) | grep R_X86_64_64; then \ + echo "$@: R_X86_64_64 references not allowed in startup code" >&2; \ + /bin/false; \ + fi extra-y := $(pi-obj-y) obj-y += $(patsubst %.o,%.pi.o,$(pi-obj-y))
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 10/11 x86/boot: Revert "Reject absolute references in .head.text"
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
This reverts commit faf0ed487415f76fe4acf7980ce360901f5e1698.
The startup code is checked directly for the absence of absolute symbol references, so checking the .head.text section in the relocs tool is no longer needed.
--- arch/x86/tools/relocs.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
arch/x86/tools/relocs.c +1 -7
@@ -740,10 +740,10 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,static int do_reloc64 (struct section *sec,Elf_Rel *rel,ElfW (Sym) *sym,const char *symname) { -int headtext = !strcmp (sec_name (sec->shdr.sh_info),".head.text" );unsigned r_type =ELF64_R_TYPE (rel->r_info);ElfW (Addr) offset = rel->r_offset;int shn_abs = (sym->st_shndx ==SHN_ABS ) && !is_reloc (S_REL , symname); +if (sym->st_shndx ==SHN_UNDEF )return 0 ;
@@ -783,12 +783,6 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,break ; } -if (headtext) { -die ("Absolute reference to symbol '%s' not permitted in .head.text\n" , - symname); -break ; - } -/* * Relocation offsets for 64 bit kernels are output * as 32 bits and sign extended back to 64 bits when
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH 11/11 x86/boot: Get rid of the .head.text section
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The .head.text section is now empty, so it can be dropped from the linker script.
--- arch/x86/kernel/vmlinux.lds.S | 5 ----- 1 file changed, 5 deletions(-)
arch/x86/kernel/vmlinux.lds.S +0 -5
@@ -160,11 +160,6 @@ SECTIONS } :text = 0xcccccccc - /* bootstrapping code */ - .head.text : AT(ADDR(.head.text) - LOAD_OFFSET) { - HEAD_TEXT - } :text = 0xcccccccc - /* End of text section, which should occupy whole number of pages */ _etext = .; . = ALIGN(PAGE_SIZE);
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Quoted 10 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
This is a proof-of-concept series that implements a strict separation between startup code and ordinary code, where startup code is built in a way that tolerates being invoked from the initial 1:1 mapping of memory.
The current approach of emitting this code into .head.text and checking for absolute relocations in that section is not 100% safe, and produces diagnostics that are sometimes difficult to interpret.
Quoted 5 lines
Instead, rely on symbol prefixes, similar to how this is implemented for the EFI stub and for the startup code in the arm64 port. This ensures that startup code can only call other startup code, unless a special symbol alias is emitted that exposes a non-startup routine to the startup code.
So when startup code accidentally references non-startup symbols outside the __pi namespace, we get a build/link error, right?
Quoted 3 lines
This is somewhat intrusive, as there are many data objects that are referenced both by startup code and by ordinary code, and an alias needs to be emitted for each of those.
Yeah, but this should make it ultimately safe(r): every object is either local to the startup code, or has been 'exported' intentionally to the startup code.
Quoted 7 lines
This ultimately allows the .head.text section to be dropped entirely, as it no longer has a special significance. Instead, code that only executes at boot is emitted into .init.text as it should.
This series is presented for discussion only - defconfig should build and run correctly, but allmodconfig will likely need the last patch omitted.
No fundamental objections from me.
Thanks,
Ingo
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
This is a proof-of-concept series that implements a strict separation between startup code and ordinary code, where startup code is built in a way that tolerates being invoked from the initial 1:1 mapping of memory.
The current approach of emitting this code into .head.text and checking for absolute relocations in that section is not 100% safe, and produces diagnostics that are sometimes difficult to interpret.
Quoted 9 lines
Quoted 5 lines
Instead, rely on symbol prefixes, similar to how this is implemented for the EFI stub and for the startup code in the arm64 port. This ensures that startup code can only call other startup code, unless a special symbol alias is emitted that exposes a non-startup routine to the startup code.
So when startup code accidentally references non-startup symbols outside the
__pinamespace, we get a build/link error, right?
Yes.
Quoted 8 lines
Quoted 3 lines
This is somewhat intrusive, as there are many data objects that are referenced both by startup code and by ordinary code, and an alias needs to be emitted for each of those.
Yeah, but this should make it ultimately safe(r): every object is either local to the startup code, or has been 'exported' intentionally to the startup code.
Indeed.
Quoted 10 lines
Quoted 7 lines
This ultimately allows the .head.text section to be dropped entirely, as it no longer has a special significance. Instead, code that only executes at boot is emitted into .init.text as it should.
This series is presented for discussion only - defconfig should build and run correctly, but allmodconfig will likely need the last patch omitted.
No fundamental objections from me.
Good to know - thanks.