x86: Refactor and consolidate startup code
v2 cover x86: Refactor and consolidate startup code
From: Ard Biesheuvel <[email protected]>
v2:
- drop queued patches #1 - #4
- keep exported symbols out of startup/
- rebase onto tip/x86/boot
Ard Biesheuvel (2):
x86/boot: Move early kernel mapping code into startup/ x86/boot: Move early SME init code into startup/
arch/x86/boot/startup/Makefile | 3 +-
arch/x86/boot/startup/map_kernel.c | 215 ++++++++++++++++++++
arch/x86/{mm/mem_encrypt_identity.c => boot/startup/sme.c} | 45 ++--
arch/x86/include/asm/mem_encrypt.h | 2 +-
arch/x86/kernel/head64.c | 211 +------------------
arch/x86/mm/Makefile | 6 -
6 files changed, 240 insertions(+), 242 deletions(-)
create mode 100644 arch/x86/boot/startup/map_kernel.c
rename arch/x86/{mm/mem_encrypt_identity.c => boot/startup/sme.c} (92%)base-commit: cc34e658c6db493c1524077e95b42d478de58f2b
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
PATCH v2 1/2 x86/boot: Move early kernel mapping code into startup/
From: Ard Biesheuvel <[email protected]>
The startup code that constructs the kernel virtual mapping runs from the 1:1 mapping of memory itself, and therefore, cannot use absolute symbol references. Move this code into a separate source file under arch/x86/boot/startup/ where all such code will be kept from now on.
Since all code here is constructed in a manner that ensures that it tolerates running from the 1:1 mapping of memory, any uses of the RIP_REL_REF() macro can be dropped, along with __head annotations for placing this code in a dedicated startup section.
--- arch/x86/boot/startup/Makefile | 2 +- arch/x86/boot/startup/map_kernel.c | 215 ++++++++++++++++++++ arch/x86/kernel/head64.c | 211 +------------------ 3 files changed, 217 insertions(+), 211 deletions(-)
arch/x86/boot/startup/Makefile +1 -1
@@ -15,7 +15,7 @@ KMSAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n -obj-$(CONFIG_X86_64) += gdt_idt.o +obj-$(CONFIG_X86_64) += gdt_idt.o map_kernel.o lib-$(CONFIG_X86_64) += la57toggle.o lib-$(CONFIG_EFI_MIXED) += efi-mixed.o
arch/x86/boot/startup/map_kernel.c new file +215 -0
215 lines not on this page · raw message
arch/x86/kernel/head64.c +1 -210
223 lines not on this page · raw message
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
PATCH v2 2/2 x86/boot: Move early SME init code into startup/
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move the SME initialization code, which runs from the 1:1 mapping of memory as it operates on the kernel virtual mapping, into the new sub-directory arch/x86/boot/startup/ where all startup code will reside that needs to tolerate executing from the 1:1 mapping.
This allows RIP_REL_REF() macro invocations and __head annotations to be dropped.
---
arch/x86/boot/startup/Makefile | 1 +
arch/x86/{mm/mem_encrypt_identity.c => boot/startup/sme.c} | 45 +++++++++-----------
arch/x86/include/asm/mem_encrypt.h | 2 +-
arch/x86/mm/Makefile | 6 ---
4 files changed, 23 insertions(+), 31 deletions(-)arch/x86/boot/startup/Makefile +1 -0
@@ -16,6 +16,7 @@ UBSAN_SANITIZE := n KCOV_INSTRUMENT := n obj-$(CONFIG_X86_64) += gdt_idt.o map_kernel.o +obj-$(CONFIG_AMD_MEM_ENCRYPT) += sme.o lib-$(CONFIG_X86_64) += la57toggle.o lib-$(CONFIG_EFI_MIXED) += efi-mixed.o
arch/x86/boot/startup/sme.c renamed from arch/x86/mm/mem_encrypt_identity.c +21 -24
152 lines not on this page · raw message
arch/x86/include/asm/mem_encrypt.h +1 -1
@@ -61,7 +61,7 @@ void __init sev_es_init_vc_handling(void);static inline u64 sme_get_me_mask (void ) { -return RIP_REL_REF (sme_me_mask); +return sme_me_mask; }#define __bss_decrypted __section(".bss..decrypted")
arch/x86/mm/Makefile +0 -6
@@ -3,12 +3,10 @@ KCOV_INSTRUMENT_tlb.o := n KCOV_INSTRUMENT_mem_encrypt.o := n KCOV_INSTRUMENT_mem_encrypt_amd.o := n -KCOV_INSTRUMENT_mem_encrypt_identity.o := n KCOV_INSTRUMENT_pgprot.o := n KASAN_SANITIZE_mem_encrypt.o := n KASAN_SANITIZE_mem_encrypt_amd.o := n -KASAN_SANITIZE_mem_encrypt_identity.o := n KASAN_SANITIZE_pgprot.o := n # Disable KCSAN entirely, because otherwise we get warnings that some functions
@@ -16,12 +14,10 @@ KASAN_SANITIZE_pgprot.o := n KCSAN_SANITIZE := n # Avoid recursion by not calling KMSAN hooks for CEA code. KMSAN_SANITIZE_cpu_entry_area.o := n -KMSAN_SANITIZE_mem_encrypt_identity.o := n ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_mem_encrypt.o = -pg CFLAGS_REMOVE_mem_encrypt_amd.o = -pg -CFLAGS_REMOVE_mem_encrypt_identity.o = -pg CFLAGS_REMOVE_pgprot.o = -pg endif
@@ -32,7 +28,6 @@ obj-y += pat/ # Make sure __phys_addr has no stackprotector CFLAGS_physaddr.o := -fno-stack-protector -CFLAGS_mem_encrypt_identity.o := -fno-stack-protector CFLAGS_fault.o := -I $(src)/../include/asm/trace
@@ -63,5 +58,4 @@ obj-$(CONFIG_MITIGATION_PAGE_TABLE_ISOLATION) += pti.o obj-$(CONFIG_X86_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_amd.o -obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Re: [PATCH v2 1/2] x86/boot: Move early kernel mapping code into startup/
In reply to Ard BiesheuvelQuoted 11 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The startup code that constructs the kernel virtual mapping runs from the 1:1 mapping of memory itself, and therefore, cannot use absolute symbol references. Move this code into a separate source file under
arch/x86/boot/startup/where all such code will be kept from now on.Since all code here is constructed in a manner that ensures that it tolerates running from the 1:1 mapping of memory, any uses of the
RIP_REL_REF()macro can be dropped, along with__headannotations for placing this code in a dedicated startup section.
So would it be possible to do this in ~3 steps: first the mechanic movement of code, with very few changes (if the result builds & boots), then drop the RIP_REL_REF() uses and __head annotations in two separate patches?
Bisectability, ease of review, etc.
(The tiny bird gets the worm, but I might have butchered that proverb.)
Thanks,
Ingo
Ard Biesheuvel
maintainerRe: [PATCH v2 1/2] x86/boot: Move early kernel mapping code into startup/
Quoted 16 lines · Ingo Molnar
Quoted 11 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The startup code that constructs the kernel virtual mapping runs from the 1:1 mapping of memory itself, and therefore, cannot use absolute symbol references. Move this code into a separate source file under
arch/x86/boot/startup/where all such code will be kept from now on.Since all code here is constructed in a manner that ensures that it tolerates running from the 1:1 mapping of memory, any uses of the
RIP_REL_REF()macro can be dropped, along with__headannotations for placing this code in a dedicated startup section.
Quoted 9 lines
So would it be possible to do this in ~3 steps: first the mechanic movement of code, with very few changes (if the result builds & boots), then drop the
RIP_REL_REF()uses and__headannotations in two separate patches?Bisectability, ease of review, etc.
(The tiny bird gets the worm, but I might have butchered that proverb.)
Yes.
And actually, the Clang boot regression that was reported indicates that this statement it not 100% true to begin with. While it is no longer necessary to use RIP_REL_REF() for accesses to global variables, it may still be needed when explicitly taking the address of a global variable and storing it in a stack allocated struct, e.g.,
void __init startup_64_setup_gdt_idt(void)
{
void *handler = NULL;
struct desc_ptr startup_gdt_descr = {
.address = (__force unsigned long)gdt_page.gdt,
.size = GDT_SIZE - 1,
};In this case, even -fPIC may produce an absolute reference to gdt_page.gdt, but from .rodata not from .text, and this is equally broken at early boot.
Once all this code has been moved into place, I'll propose the validation (similar to arm64 and EFI stub) which just greps the output of readelf -r and checks for occurrences of R_X86_64_64; that way, we will detect early and precisely whether the codegen is ok.
Please let me know which of these patches you are intending to keep in tip/x86/boot, and I will respin on top of that.
Re: [PATCH v2 1/2] x86/boot: Move early kernel mapping code into startup/
Quoted 45 lines · Ard Biesheuvel
Quoted 16 lines · Ingo Molnar
Quoted 11 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The startup code that constructs the kernel virtual mapping runs from the 1:1 mapping of memory itself, and therefore, cannot use absolute symbol references. Move this code into a separate source file under
arch/x86/boot/startup/where all such code will be kept from now on.Since all code here is constructed in a manner that ensures that it tolerates running from the 1:1 mapping of memory, any uses of the
RIP_REL_REF()macro can be dropped, along with__headannotations for placing this code in a dedicated startup section.Quoted 9 lines
So would it be possible to do this in ~3 steps: first the mechanic movement of code, with very few changes (if the result builds & boots), then drop the
RIP_REL_REF()uses and__headannotations in two separate patches?Bisectability, ease of review, etc.
(The tiny bird gets the worm, but I might have butchered that proverb.)
Yes.
And actually, the Clang boot regression that was reported indicates that this statement it not 100% true to begin with. While it is no longer necessary to use
RIP_REL_REF()for accesses to global variables, it may still be needed when explicitly taking the address of a global variable and storing it in a stack allocated struct, e.g.,void
__initstartup_64_setup_gdt_idt(void){ void *handler = NULL; struct desc_ptr startup_gdt_descr = { .address = (__force unsigned long)gdt_page.gdt, .size = GDT_SIZE - 1, };
Quoted 3 lines
In this case, even -fPIC may produce an absolute reference to gdt_page.gdt, but from .rodata not from .text, and this is equally broken at early boot.
OK.
Quoted 4 lines
Once all this code has been moved into place, I'll propose the validation (similar to arm64 and EFI stub) which just greps the output of readelf -r and checks for occurrences of
R_X86_64_64; that way, we will detect early and precisely whether the codegen is ok.
Yeah, that sounds good!
Quoted 2 lines
Please let me know which of these patches you are intending to keep in tip/x86/boot, and I will respin on top of that.
I'd like to merge all of them as long as they don't intentionally regress. All of this seemed like a step forward, and having them in one place will enable a new type of debugging check - which is a win too in my book.
So no fundamental worries from me, just the request to have more careful iterations.
Thanks,
Ingo