x86: Always permit the kernel to be placed at any physical address
PATCH v2 1/1 x86: Always permit the kernel to be placed at any physical address
From: Ard Biesheuvel <[email protected]>
On i386, the virtual displacement relative to PAGE_OFFSET must equal the physical displacement relative to LOAD_OFFSET, and so relocations need to be applied to all virtual symbol references at boot if the actual physical load address deviates from the build-time default.
This requires CONFIG_RELOCATABLE=y, otherwise the decompressor will place the kernel at its build-time default physical load address, so that no relocations need to be applied.
On x86_64, this logic was inherited, even though the virtual placement of the kernel is independent from the physical load address, and so the kernel can always be placed at its build-time virtual default address regardless of where it was placed in the physical space, requiring no relocation fixups to be applied.
This means that for the 64-bit kernel, the CONFIG_RELOCATABLE dependency can be dropped. Instead, always present it as 'relocatable' via the bzImage metadata as well as to the EFI stub, and only depend on CONFIG_RELOCATABLE when enabling KASLR (CONFIG_RANDOMIZE_BASE=y).
For symmetry, do the same for the 32-bit kernel. This makes CONFIG_RELOCATABLE a hard dependency there, as the relocation metadata is always required, but this is already the default in practice.
While at it, tidy up the Kconfig help texts.
---
Changes since v1: [0]
- make i386 kernels always relocatable as well
- clean up Kconfig help texts a bit more
[0] https://lore.kernel.org/all/[email protected]/
arch/x86/Kconfig | 94 ++++++-------------- arch/x86/boot/compressed/head_32.S | 2 - arch/x86/boot/compressed/head_64.S | 4 - arch/x86/boot/compressed/misc.c | 4 - arch/x86/boot/header.S | 10 +-- 5 files changed, 28 insertions(+), 86 deletions(-)
arch/x86/Kconfig +27 -67
@@ -19,6 +19,7 @@ config X86_32 select MODULES_USE_ELF_REL select OLD_SIGACTION select ARCH_SPLIT_ARG64 + select RELOCATABLE config X86_64 def_bool y
@@ -1898,7 +1899,6 @@ config EFI config EFI_STUB bool "EFI stub support" depends on EFI - select RELOCATABLE help This kernel feature allows a bzImage to be loaded directly by EFI firmware without the use of a bootloader.
@@ -1990,66 +1990,37 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION def_bool CRASH_RESERVE config PHYSICAL_START - hex "Physical address where the kernel is loaded" if (EXPERT || CRASH_DUMP) + hex "Physical load address of the kernel" if (EXPERT || CRASH_DUMP) && X86_32 default "0x1000000" help - This gives the physical address where the kernel is loaded. - - If the kernel is not relocatable (CONFIG_RELOCATABLE=n) then bzImage - will decompress itself to above physical address and run from there. - Otherwise, bzImage will run from the address where it has been loaded - by the boot loader. The only exception is if it is loaded below the - above physical address, in which case it will relocate itself there. - - In normal kdump cases one does not have to set/change this option - as now bzImage can be compiled as a completely relocatable image - (CONFIG_RELOCATABLE=y) and be used to load and run from a different - address. This option is mainly useful for the folks who don't want - to use a bzImage for capturing the crash dump and want to use a - vmlinux instead. vmlinux is not relocatable hence a kernel needs - to be specifically compiled to run from a specific memory area - (normally a reserved region) and this option comes handy. - - So if you are using bzImage for capturing the crash dump, - leave the value here unchanged to 0x1000000 and set - CONFIG_RELOCATABLE=y. Otherwise if you plan to use vmlinux - for capturing the crash dump change this value to start of - the reserved region. In other words, it can be set based on - the "X" value as specified in the "crashkernel=YM@XM" - command line boot parameter passed to the panic-ed - kernel. Please take a look at Documentation/admin-guide/kdump/kdump.rst - for more details about crash dumps. - - Usage of bzImage for capturing the crash dump is recommended as - one does not have to build two kernels. Same kernel can be used - as production kernel and capture kernel. Above option should have - gone away after relocatable bzImage support is introduced. But it - is present because there are users out there who continue to use - vmlinux for dump capture. This option should go away down the - line. + The minimum physical load address of the decompressed image. The + kernel will be placed here by the decompressor if it was loaded below + this address. If it was loaded at or above this address, the kernel + will be placed at the actual load address rounded up to + PHYSICAL_ALIGN. - Don't change this unless you know what you are doing. + Legacy i386 kdump use cases based on vmlinux rather than bzImage + might need to override this, otherwise leave at its default. config RELOCATABLE - bool "Build a relocatable kernel" - default y + def_bool y help - This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. - The relocations tend to make the kernel binary about 10% larger, - but are discarded at runtime. + This builds a kernel image that retains relocation information, + allowing its placement in the virtual address space to deviate from + the build time default. The relocations are applied at boot and then + discarded, so their runtime memory footprint is zero. - One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary - kernel. + On i386, the virtual displacement relative to PAGE_OFFSET must equal + the physical displacement relative to LOAD_OFFSET, and so this option + must always be set. - Note: If CONFIG_RELOCATABLE=y, then the kernel runs from the address - it has been loaded at and the compile time physical address - (CONFIG_PHYSICAL_START) is used as the minimum location. + On x86_64, the virtual displacement is independent from the physical + placement, and the relocations are only used when randomizing the + virtual address space (KASLR). config RANDOMIZE_BASE bool "Randomize the address of the kernel image (KASLR)" - depends on RELOCATABLE + select RELOCATABLE default y help In support of Kernel Address Space Layout Randomization (KASLR),
@@ -2085,30 +2056,19 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build support config X86_NEED_RELOCS def_bool y - depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLE select ARCH_VMLINUX_NEEDS_RELOCS config PHYSICAL_ALIGN - hex "Alignment value to which kernel should be aligned" + hex "Minimum alignment of the physical load address" default "0x200000" range 0x2000 0x1000000 if X86_32 range 0x200000 0x1000000 if X86_64 help - This value puts the alignment restrictions on physical address - where kernel is loaded and run from. Kernel is compiled for an - address which meets above alignment restriction. - - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is set, kernel will move itself to nearest - address aligned to above value and run from there. - - If bootloader loads the kernel at a non-aligned address and - CONFIG_RELOCATABLE is not set, kernel will ignore the run time - load address and decompress itself to the address it has been - compiled for and run from there. The address for which kernel is - compiled already meets above alignment restrictions. Hence the - end result is that kernel runs from a physical address meeting - above alignment restrictions. + The minimum alignment of the kernel's physical load address. If the + decompressor was loaded at a physical address > PHYSICAL_START but + not aligned to this value, it will round it up to the next multiple + and decompress the kernel there. On 32-bit this value must be a multiple of 0x2000. On 64-bit this value must be a multiple of 0x200000.
arch/x86/boot/compressed/head_32.S +0 -2
@@ -82,7 +82,6 @@ SYM_FUNC_START(startup_32) * %ebp is calculated to be the address that the kernel will be decompressed to. */ -#ifdef CONFIG_RELOCATABLE leal startup_32@GOTOFF(%edx), %ebx movl BP_kernel_alignment(%esi), %eax decl %eax
@@ -91,7 +90,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx 1:
arch/x86/boot/compressed/head_64.S +0 -4
@@ -143,7 +143,6 @@ SYM_FUNC_START(startup_32) * for safe in-place decompression. */ -#ifdef CONFIG_RELOCATABLE movl %ebp, %ebx movl BP_kernel_alignment(%esi), %eax decl %eax
@@ -152,7 +151,6 @@ SYM_FUNC_START(startup_32) andl %eax, %ebx cmpl $LOAD_PHYSICAL_ADDR, %ebx jae 1f -#endif movl $LOAD_PHYSICAL_ADDR, %ebx 1:
@@ -316,7 +314,6 @@ SYM_CODE_START(startup_64) */ /* Start with the delta to where the kernel will run at. */ -#ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp movl BP_kernel_alignment(%rsi), %eax decl %eax
@@ -325,7 +322,6 @@ SYM_CODE_START(startup_64) andq %rax, %rbp cmpq $LOAD_PHYSICAL_ADDR, %rbp jae 1f -#endif movq $LOAD_PHYSICAL_ADDR, %rbp 1:
arch/x86/boot/compressed/misc.c +0 -4
@@ -314,12 +314,8 @@ static size_t parse_elf(void *output)if ((phdr->p_align %0x200000 ) !=0 )error ("Alignment of LOAD segment isn't multiple of 2MB" );#endif -#ifdef CONFIG_RELOCATABLE dest = output; dest += (phdr->p_paddr -LOAD_PHYSICAL_ADDR ); -#else - dest = (void *)(phdr->p_paddr); -#endif memmove (dest, output + phdr->p_offset, phdr->p_filesz);break ;default :/* Ignore other PT_* */ break ;
arch/x86/boot/header.S +1 -9
@@ -332,24 +332,16 @@ initrd_addr_max: .long 0x7fffffff kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment #required for protected mode #kernel -#ifdef CONFIG_RELOCATABLE relocatable_kernel: .byte 1 -#else -relocatable_kernel: .byte 0 -#endif min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment xloadflags: #ifdef CONFIG_X86_64 # define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ -#else -# define XLF0 0 -#endif - -#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) /* kernel/boot_param/ramdisk could be loaded above 4g */ # define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G #else +# define XLF0 0 # define XLF1 0 #endif
Signature · 1 line
2.56.1.831.g720f3f5937-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] x86/Kconfig: config RELOCATABLE uses def_bool y instead of bool --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] x86/Kconfig: config RELOCATABLE uses def_bool y instead of bool
--
commit c734084ffe5143387f2c8e1cef5938ed63060e83
Author: Ard Biesheuvel <[email protected]>
x86: Always permit the kernel to be placed at any physical address
This commit removes the CONFIG_RELOCATABLE gating around physical load address handling in the x86 decompressor and boot header, and restricts CONFIG_RELOCATABLE to 32-bit kernels and KASLR builds.
Quoted 4 lines
arch/x86/Kconfig +0
-0
[ ... ]
Quoted 41 lines
arch/x86/Kconfig +14
-13@@ -1994,66 +1994,37 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION Don't change this unless you know what you are doing. + Legacy i386 kdump use cases based on vmlinux rather than bzImage + might need to override this, otherwise leave at its default. config RELOCATABLE - bool "Build a relocatable kernel" - default y + def_bool y help - This builds a kernel image that retains relocation information - so it can be loaded someplace besides the default 1MB. - The relocations tend to make the kernel binary about 10% larger, - but are discarded at runtime. + This builds a kernel image that retains relocation information, + allowing its placement in the virtual address space to deviate from + the build time default. The relocations are applied at boot and then + discarded, so their runtime memory footprint is zero. - One use is for the kexec on panic case where the recovery kernel - must live at a different physical address than the primary - kernel. + On i386, the virtual displacement relative to PAGE_OFFSET must equal + the physical displacement relative to LOAD_OFFSET, and so this option + must always be set. - Note: If CONFIG_RELOCATABLE=y, then the kernel runs from the address - it has been loaded at and the compile time physical address - (CONFIG_PHYSICAL_START) is used as the minimum location. + On x86_64, the virtual displacement is independent from the physical + placement, and the relocations are only used when randomizing the + virtual address space (KASLR). config RANDOMIZE_BASE bool "Randomize the address of the kernel image (KASLR)" - depends on RELOCATABLE + select RELOCATABLE default y help In support of Kernel Address Space Layout Randomization (KASLR),
arch/x86/Kconfig config RANDOMIZE_BASE · line 2064
Quoted 6 lines
arch/x86/Kconfig +1
-1@@ -2089,30 +2060,19 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build support config X86_NEED_RELOCS def_bool y - depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on RELOCATABLE select ARCH_VMLINUX_NEEDS_RELOCS
[Severity: Medium]
Should config RELOCATABLE be declared as bool rather than def_bool y?
With def_bool y, CONFIG_RELOCATABLE is unconditionally enabled even when neither X86_32 nor RANDOMIZE_BASE selects it. Because X86_NEED_RELOCS now depends only on RELOCATABLE, an x86_64 build with CONFIG_RANDOMIZE_BASE=n has CONFIG_RELOCATABLE=y and CONFIG_X86_NEED_RELOCS=y forced on, still emitting and compressing vmlinux.relocs into the image and running handle_relocations() at boot.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1