x86/boot: Early boot cleanups
Subsystems (4)
v2 cover x86/boot: Early boot cleanups
This series moves clearing BSS and copying boot parameters and the command line to the early boot code. Clearing BSS early prevents using unitialized data making the code more robust. Copying the boot parameters early simplifies the code by not needing to pass around the orignal source pointer and does not need special handling when encryption is enabled.
v2 changes:
- Use sizeof(*boot_params) (Tom Lendacky)
- Always clear decrypted BSS section even if encryption is not active (Tom Lendacky)
Brian Gerst (5):
x86/sme: Clear decrypted BSS separately x86/boot/64: Clear BSS as early as possible x86/boot: Remove hardcoded boot_param constants x86/boot/64: Remove copy_bootdata() call x86/boot/64: Copy boot parameters and command line earlier
arch/x86/boot/startup/map_kernel.c | 10 ++-- arch/x86/boot/startup/sme.c | 6 ++- arch/x86/include/asm/mem_encrypt.h | 14 ++---- arch/x86/include/asm/setup.h | 11 ++--- arch/x86/kernel/asm-offsets.c | 3 ++ arch/x86/kernel/head64.c | 61 ++--------------------- arch/x86/kernel/head_32.S | 4 +- arch/x86/kernel/head_64.S | 47 ++++++++++++++---- arch/x86/kernel/setup.c | 1 + arch/x86/kernel/smpboot.c | 2 +- arch/x86/kernel/vmlinux.lds.S | 2 +- arch/x86/mm/mem_encrypt_amd.c | 65 ++----------------------- arch/x86/xen/enlighten_pv.c | 4 +- arch/x86/xen/xen-head.S | 5 +- drivers/firmware/efi/libstub/x86-stub.c | 6 +-- 15 files changed, 77 insertions(+), 164 deletions(-)
base-commit: e811152a1de6a33cf5a933770821477757f69f99
Signature · 1 line
2.55.0
PATCH v2 1/5 x86/sme: Clear decrypted BSS separately
The decrypted BSS section needs to be cleared after it is remapped as decrypted memory. Separate it so that the normal BSS section can be cleared earlier.
--- arch/x86/kernel/vmlinux.lds.S | 2 +- arch/x86/mm/mem_encrypt_amd.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/kernel/vmlinux.lds.S +1 -1
@@ -365,9 +365,9 @@ SECTIONS *(.bss..page_aligned) . = ALIGN(PAGE_SIZE); *(BSS_MAIN) - BSS_DECRYPTED . = ALIGN(PAGE_SIZE); __bss_stop = .; + BSS_DECRYPTED } /*
arch/x86/mm/mem_encrypt_amd.c +4 -0
@@ -476,6 +476,10 @@ void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, unsigned long svoid __init sme_early_init (void ) { +/* Needs to be cleared even if encryption is not enabled */ +memset (__start_bss_decrypted,0 , + (unsigned long ) __end_bss_decrypted - (unsigned long ) __start_bss_decrypted); +if (!sme_me_mask)return ;
Signature · 1 line
2.55.0
PATCH v2 2/5 x86/boot/64: Clear BSS as early as possible
In reply to Brian GerstCurrently, the BSS section is not cleared until x86_64_start_kernel(). Using unitialized BSS data before that point leads to difficult to debug problems. Fix this by moving clear_bss() to as early as possible.
--- arch/x86/include/asm/setup.h | 2 -- arch/x86/kernel/head64.c | 12 ------------ arch/x86/kernel/head_64.S | 20 ++++++++++++++++++++ arch/x86/xen/enlighten_pv.c | 2 -- arch/x86/xen/xen-head.S | 5 ++++- 5 files changed, 24 insertions(+), 17 deletions(-)
arch/x86/include/asm/setup.h +0 -2
@@ -126,8 +126,6 @@ void *extend_brk(size_t size, size_t align);extern void probe_roms (void ); -void clear_bss (void ); -#ifdef __i386__ asmlinkage void __init __noreturn i386_start_kernel(void);
arch/x86/kernel/head64.c +0 -12
@@ -172,16 +172,6 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr)early_fixup_exception (regs, trapnr); } -/* Don't add a printk in there. printk relies on the PDA which is not initialized -yet. */ -void __init clear_bss (void ) -{ -memset (__bss_start,0 , - (unsigned long ) __bss_stop - (unsigned long ) __bss_start); -memset (__brk_base,0 , - (unsigned long ) __brk_limit - (unsigned long ) __brk_base); -} -static unsigned long get_cmd_line_ptr (void ) {unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
@@ -246,8 +236,6 @@ asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode vmemmap_base = __VMEMMAP_BASE_L5; } -clear_bss (); -/* * This needs to happen *before* kasan_early_init() because latter maps stuff * into that page.
arch/x86/kernel/head_64.S +20 -0
@@ -37,6 +37,8 @@ .code64 SYM_CODE_START_NOALIGN(startup_64) UNWIND_HINT_END_OF_STACK + cld + /* * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, * and someone has loaded an identity mapped page table
@@ -61,6 +63,8 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu() */ leaq __top_init_kernel_stack(%rip), %rsp + call clear_bss + /* * Set up GSBASE. * Note that on SMP the boot CPU uses the init data section until
@@ -140,6 +144,22 @@ SYM_CODE_START_NOALIGN(startup_64) jmp *.Lcommon_startup_64(%rip) SYM_CODE_END(startup_64) +SYM_FUNC_START(clear_bss) + xorl %eax, %eax + + leaq __bss_start(%rip), %rdi + leaq __bss_stop(%rip), %rcx + subq %rdi, %rcx + rep stosb + + leaq __brk_base(%rip), %rdi + leaq __brk_limit(%rip), %rcx + subq %rdi, %rcx + rep stosb + + RET +SYM_FUNC_END(clear_bss) + __INITRODATA SYM_DATA_LOCAL(.Lcommon_startup_64, .quad common_startup_64)
arch/x86/xen/enlighten_pv.c +0 -2
@@ -1334,8 +1334,6 @@ asmlinkage __visible void __init xen_start_kernel(struct start_info *si)if (!si)return ; -clear_bss (); - xen_start_info = si; __text_gen_insn(&early_xen_iret_patch,
arch/x86/xen/xen-head.S +4 -1
@@ -31,6 +31,9 @@ SYM_CODE_START(startup_xen) leaq __top_init_kernel_stack(%rip), %rsp + movq %rsi, %r15 + call clear_bss + /* * Set up GSBASE. * Note that, on SMP, the boot cpu uses init data section until
@@ -41,7 +44,7 @@ SYM_CODE_START(startup_xen) xorl %edx, %edx wrmsr - mov %rsi, %rdi + mov %r15, %rdi call xen_start_kernel SYM_CODE_END(startup_xen) __FINIT
Signature · 1 line
2.55.0
PATCH v2 3/5 x86/boot: Remove hardcoded boot_param constants
In reply to Brian GerstUse generated constants or sizeof() instead of hardcoded values.
No functional change.
--- arch/x86/include/asm/setup.h | 3 --- arch/x86/kernel/asm-offsets.c | 2 ++ arch/x86/kernel/head_32.S | 4 ++-- drivers/firmware/efi/libstub/x86-stub.c | 6 +++--- 4 files changed, 7 insertions(+), 8 deletions(-)
arch/x86/include/asm/setup.h +0 -3
@@ -21,11 +21,8 @@#endif /* __i386__ */ -#define PARAM_SIZE 4096/* sizeof(struct boot_params) */ -#define OLD_CL_MAGIC 0xA33F#define OLD_CL_ADDRESS 0x020/* Relative to real mode data */ -#define NEW_CL_POINTER 0x228/* Relative to real mode data */ #ifndef __ASSEMBLER__#include <linux/cache.h>
arch/x86/kernel/asm-offsets.c +2 -0
@@ -105,6 +105,8 @@ static void __used common(void)OFFSET (BP_kernel_alignment, boot_params, hdr.kernel_alignment);OFFSET (BP_init_size, boot_params, hdr.init_size);OFFSET (BP_pref_address, boot_params, hdr.pref_address); +OFFSET (BP_cmd_line_ptr, boot_params, hdr.cmd_line_ptr); +DEFINE (SIZEOF_boot_params,sizeof (struct boot_params ));BLANK ();DEFINE (PTREGS_SIZE ,sizeof (struct pt_regs ));
arch/x86/kernel/head_32.S +2 -2
@@ -96,10 +96,10 @@ SYM_CODE_START(startup_32) * page tables. */ movl $pa(boot_params),%edi - movl $(PARAM_SIZE/4),%ecx + movl $(SIZEOF_boot_params/4),%ecx cld rep movsl - movl pa(boot_params) + NEW_CL_POINTER,%esi + movl pa(boot_params) + BP_cmd_line_ptr,%esi andl %esi,%esi jz 1f # No command line movl $pa(boot_command_line),%edi
drivers/firmware/efi/libstub/x86-stub.c +3 -3
@@ -520,11 +520,11 @@ static efi_status_t efi_allocate_bootparams(efi_handle_t handle,return status; } - status =efi_allocate_pages (PARAM_SIZE , &alloc,ULONG_MAX ); + status =efi_allocate_pages (sizeof (*boot_params), &alloc,ULONG_MAX );if (status !=EFI_SUCCESS )return status; - boot_params =memset ((void *)alloc,0x0 ,PARAM_SIZE ); + boot_params =memset ((void *)alloc,0x0 ,sizeof (*boot_params)); hdr = &boot_params->hdr;/* Assign the setup_header fields that the kernel actually cares about */
@@ -537,7 +537,7 @@ static efi_status_t efi_allocate_bootparams(efi_handle_t handle,/* Convert unicode cmdline to ascii */ cmdline_ptr =efi_convert_cmdline (image);if (!cmdline_ptr) { -efi_free (PARAM_SIZE , alloc); +efi_free (sizeof (*boot_params), alloc);return EFI_OUT_OF_RESOURCES ; }
Signature · 1 line
2.55.0
PATCH v2 4/5 x86/boot/64: Remove copy_bootdata() call
In reply to Brian GerstOn a normal boot, copy_bootdata() is first called in x86_64_start_kernel(). The second call in x86_64_start_reservations() is effectively a no-op.
Xen PV directly initializes boot_params, so it does not need to call copy_bootdata() at all.
Remove the unnecessary call to copy_bootdata() from x86_64_start_reservations().
--- arch/x86/include/asm/setup.h | 2 +- arch/x86/kernel/head64.c | 8 ++------ arch/x86/xen/enlighten_pv.c | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-)
arch/x86/include/asm/setup.h +1 -1
@@ -130,7 +130,7 @@ void __init mk_early_pgtbl_32(void);#else asmlinkage void __init __noreturn x86_64_start_kernel(char *real_mode); -asmlinkage void __init __noreturn x86_64_start_reservations(char *real_mode_data); +asmlinkage void __init __noreturn x86_64_start_reservations(void);#endif /* __i386__ */ #endif /* _SETUP */
arch/x86/kernel/head64.c +2 -6
@@ -276,15 +276,11 @@ asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode/* set init_top_pgt kernel high mapping*/ init_top_pgt[511 ] = early_top_pgt[511 ]; -x86_64_start_reservations (real_mode_data); +x86_64_start_reservations (); } -void __init __noreturn x86_64_start_reservations (char *real_mode_data) +void __init __noreturn x86_64_start_reservations (void ) { -/* version is always not zero if it is copied */ -if (!boot_params.hdr.version) -copy_bootdata (__va (real_mode_data)); -x86_early_init_platform_quirks ();switch (boot_params.hdr.hardware_subarch) {
arch/x86/xen/enlighten_pv.c +1 -1
@@ -1556,7 +1556,7 @@ asmlinkage __visible void __init xen_start_kernel(struct start_info *si)/* Start the world */ cr4_init_shadow ();/* 32b kernel does this in i386_start_kernel() */ -x86_64_start_reservations ((char *)__pa_symbol (&boot_params)); +x86_64_start_reservations (); }static int xen_cpu_up_prepare_pv (unsigned int cpu)
Signature · 1 line
2.55.0
PATCH v2 5/5 x86/boot/64: Copy boot parameters and command line earlier
In reply to Brian GerstMove the copy of the boot parameters and command line to the early setup code. This eliminates the need to pass the original pointer to several other functions.
By copying the data into kernel memory before encryption is enabled, the original location no longer needs to be remapped as decrypted.
--- arch/x86/boot/startup/map_kernel.c | 10 ++--- arch/x86/boot/startup/sme.c | 6 ++- arch/x86/include/asm/mem_encrypt.h | 14 ++----- arch/x86/include/asm/setup.h | 4 +- arch/x86/kernel/asm-offsets.c | 1 + arch/x86/kernel/head64.c | 41 +------------------- arch/x86/kernel/head_64.S | 27 ++++++++----- arch/x86/kernel/setup.c | 1 + arch/x86/kernel/smpboot.c | 2 +- arch/x86/mm/mem_encrypt_amd.c | 61 ------------------------------ 10 files changed, 37 insertions(+), 130 deletions(-)
arch/x86/boot/startup/map_kernel.c +4 -6
@@ -30,15 +30,14 @@ static inline bool check_la57_support(void)return true; } -static unsigned long __init sme_postprocess_startup (struct boot_params *bp, -pmdval_t *pmd, +static unsigned long __init sme_postprocess_startup (pmdval_t *pmd,unsigned long p2v_offset) {unsigned long paddr, paddr_end;int i;/* Encrypt the kernel and related (if SME is active) */ -sme_encrypt_kernel (bp); +sme_encrypt_kernel ();/* * Clear the memory encryption mask from the .bss..decrypted section.
@@ -84,8 +83,7 @@ static unsigned long __init 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 __init __startup_64 (unsigned long p2v_offset, -struct boot_params *bp) +unsigned long __init __startup_64 (unsigned long p2v_offset) {pmd_t (*early_pgts)[PTRS_PER_PMD ] =rip_rel_ptr (early_dynamic_pgts);unsigned long physaddr = (unsigned long )rip_rel_ptr (_text);
@@ -213,5 +211,5 @@ unsigned long __init __startup_64(unsigned long p2v_offset,for (; i <PTRS_PER_PMD ; i++) pmd[i] &= ~_PAGE_PRESENT; -return sme_postprocess_startup (bp, pmd, p2v_offset); +return sme_postprocess_startup (pmd, p2v_offset); }
arch/x86/boot/startup/sme.c +4 -2
@@ -283,8 +283,9 @@ static unsigned long __init sme_pgtable_calc(unsigned long len)return entries + tables; } -void __init sme_encrypt_kernel (struct boot_params *bp) +void __init sme_encrypt_kernel (void ) { +struct boot_params *bp =rip_rel_ptr (&boot_params);unsigned long workarea_start, workarea_end, workarea_len;unsigned long execute_start, execute_end, execute_len;unsigned long kernel_start, kernel_end, kernel_len;
@@ -487,8 +488,9 @@ void __init sme_encrypt_kernel(struct boot_params *bp)native_write_cr3 (__native_read_cr3 ()); } -void __init sme_enable (struct boot_params *bp) +void __init sme_enable (void ) { +struct boot_params *bp =rip_rel_ptr (&boot_params);unsigned int eax, ebx, ecx, edx;unsigned long feature_mask;unsigned long me_mask;
arch/x86/include/asm/mem_encrypt.h +4 -10
@@ -42,13 +42,10 @@ void __init sme_early_encrypt(resource_size_t paddr,void __init sme_early_decrypt (resource_size_t paddr,unsigned long size); -void __init sme_map_bootdata (char *real_mode_data); -void __init sme_unmap_bootdata (char *real_mode_data); -void __init sme_early_init (void ); -void sme_encrypt_kernel (struct boot_params *bp); -void sme_enable (struct boot_params *bp); +void sme_encrypt_kernel (void ); +void sme_enable (void );int __init early_set_memory_decrypted (unsigned long vaddr,unsigned long size);int __init early_set_memory_encrypted (unsigned long vaddr,unsigned long size);
@@ -76,13 +73,10 @@ static inline void __init sme_early_encrypt(resource_size_t paddr,static inline void __init sme_early_decrypt (resource_size_t paddr,unsigned long size) { } -static inline void __init sme_map_bootdata (char *real_mode_data) { } -static inline void __init sme_unmap_bootdata (char *real_mode_data) { } -static inline void __init sme_early_init (void ) { } -static inline void sme_encrypt_kernel (struct boot_params *bp) { } -static inline void sme_enable (struct boot_params *bp) { } +static inline void sme_encrypt_kernel (void ) { } +static inline void sme_enable (void ) { }static inline void sev_es_init_vc_handling (void ) { }
arch/x86/include/asm/setup.h +2 -2
@@ -47,7 +47,7 @@ extern unsigned long acpi_realmode_flags;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 unsigned long __startup_64 (unsigned long p2v_offset);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);
@@ -129,7 +129,7 @@ asmlinkage void __init __noreturn i386_start_kernel(void);void __init mk_early_pgtbl_32 (void );#else -asmlinkage void __init __noreturn x86_64_start_kernel(char *real_mode); +asmlinkage void __init __noreturn x86_64_start_kernel(void); asmlinkage void __init __noreturn x86_64_start_reservations(void);#endif /* __i386__ */
arch/x86/kernel/asm-offsets.c +1 -0
@@ -106,6 +106,7 @@ static void __used common(void)OFFSET (BP_init_size, boot_params, hdr.init_size);OFFSET (BP_pref_address, boot_params, hdr.pref_address);OFFSET (BP_cmd_line_ptr, boot_params, hdr.cmd_line_ptr); +OFFSET (BP_ext_cmd_line_ptr, boot_params, ext_cmd_line_ptr);DEFINE (SIZEOF_boot_params,sizeof (struct boot_params ));BLANK ();
arch/x86/kernel/head64.c +2 -39
53 lines not on this page · raw message
arch/x86/kernel/head_64.S +18 -9
@@ -27,6 +27,7 @@ #include <asm/fixmap.h> #include <asm/smp.h> #include <asm/thread_info.h> +#include <asm/setup.h> /* * We are not able to switch in one step to the final KERNEL ADDRESS SPACE
@@ -65,6 +66,22 @@ SYM_CODE_START_NOALIGN(startup_64) call clear_bss + /* Copy the boot parameters and command line into kernel memory. */ + movq %r15, %rsi + leaq boot_params(%rip), %rdi + movl $(SIZEOF_boot_params / 4), %ecx + rep movsl + + movl boot_params + BP_cmd_line_ptr(%rip), %esi + movl boot_params + BP_ext_cmd_line_ptr(%rip), %eax + shlq $32, %rax + orq %rax, %rsi + jz .Lno_command_line + leaq boot_command_line(%rip), %rdi + movl $(COMMAND_LINE_SIZE / 4), %ecx + rep movsl +.Lno_command_line: + /* * Set up GSBASE. * Note that on SMP the boot CPU uses the init data section until
@@ -92,9 +109,8 @@ SYM_CODE_START_NOALIGN(startup_64) * Activate SEV/SME memory encryption if supported/enabled. This needs to * be done now, since this also includes setup of the SEV-SNP CPUID table, * which needs to be done before any CPUID instructions are executed in - * subsequent code. Pass the boot_params pointer as the first argument. + * subsequent code. */ - movq %r15, %rdi call __pi_sme_enable #endif
@@ -114,7 +130,6 @@ SYM_CODE_START_NOALIGN(startup_64) * is active) to be added to the initial pgdir entry that will be * programmed into CR3. */ - movq %r15, %rsi call __pi___startup_64 /* Form the CR3 value being sure to include the CR3 modifier */
@@ -195,9 +210,6 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR - /* Clear %R15 which holds the boot_params pointer on the boot CPU */ - xorl %r15d, %r15d - /* Derive the runtime physical address of init_top_pgt[] */ movq phys_base(%rip), %rax addq $(init_top_pgt - __START_KERNEL_map), %rax
@@ -429,9 +441,6 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) pushq $0 popfq - /* Pass the boot_params pointer as first argument */ - movq %r15, %rdi - .Ljump_to_C_code: xorl %ebp, %ebp # clear frame pointer ANNOTATE_RETPOLINE_SAFE
arch/x86/kernel/setup.c +1 -0
@@ -77,6 +77,7 @@ unsigned long _brk_start = (unsigned long)__brk_base;unsigned long _brk_end = (unsigned long )__brk_base;struct boot_params boot_params; +SYM_PIC_ALIAS (boot_params);/* * These are the four main kernel memory regions, we put them into
arch/x86/kernel/smpboot.c +1 -1
@@ -226,7 +226,7 @@ static void ap_calibrate_delay(void)/* * Activate a secondary processor. */ -static void notrace __noendbr start_secondary(void *unused) +static void notrace __noendbr start_secondary(void) {/* * Don't put *anything* except direct CPU state initialization
arch/x86/mm/mem_encrypt_amd.c +0 -61
67 lines not on this page · raw message
Signature · 1 line
2.55.0