Fix two crashes in the decomression code
Subsystems (2)
v2 cover Fix two crashes in the decomression code
Here's an updated version of two crash fixes in early boot code.
Kirill A. Shutemov (2):
x86/boot/compressed/64: Set up GOT for paging_prepare() and
cleanup_trampoline()
x86/boot/compressed/64: Fix moving page table out of trampoline memory
arch/x86/boot/compressed/head_64.S | 79 ++++++++++++++++++++++----- arch/x86/boot/compressed/pgtable_64.c | 14 +---- 2 files changed, 69 insertions(+), 24 deletions(-)
Signature · 1 line
2.17.0
PATCH v2 1/2 x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()
Eric and Hugh have reported instant reboot due to my recent changes in decompression code.
The root cause is that I didn't realize that we need to adjust GOT to be able to run C code that early.
The problem is only visible with an older toolchain. Binutils >= 2.24 is able to eliminate GOT references by replacing them with RIP-relative address loads[1].
We need to adjust GOT two times:
- before calling paging_prepare() using the initial load address
- before calling C code from the relocated kernel
[1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=80d873266dec
--- arch/x86/boot/compressed/head_64.S | 68 ++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-)
arch/x86/boot/compressed/head_64.S +55 -13
@@ -305,6 +305,25 @@ ENTRY(startup_64) /* Set up the stack */ leaq boot_stack_end(%rbx), %rsp + /* + * paging_prepare() and cleanup_trampoline() below can have GOT + * references. Adjust the table with address we are running at. + * + * Zero RAX for adjust_got: the GOT was not adjusted before; + * there's no adjustment to undo. + */ + xorq %rax, %rax + + /* + * Calculate the address the binary is loaded at and use it as + * a GOT adjustment. + */ + call 1f +1: popq %rdi + subq $1b, %rdi + + call adjust_got + /* * At this point we are in long mode with 4-level paging enabled, * but we might want to enable 5-level paging or vice versa.
@@ -381,6 +400,21 @@ trampoline_return: pushq $0 popfq + /* + * Previously we've adjusted the GOT with address the binary was + * loaded at. Now we need to re-adjust for relocation address. + * + * Calculate the address the binary is loaded at, so that we can + * undo the previous GOT adjustment. + */ + call 1f +1: popq %rax + subq $1b, %rax + + /* The new adjustment is the relocation address */ + movq %rbx, %rdi + call adjust_got + /* * Copy the compressed kernel to the end of our buffer * where decompression in place becomes safe.
@@ -481,19 +515,6 @@ relocated: shrq $3, %rcx rep stosq -/* - * Adjust our own GOT - */ - leaq _got(%rip), %rdx - leaq _egot(%rip), %rcx -1: - cmpq %rcx, %rdx - jae 2f - addq %rbx, (%rdx) - addq $8, %rdx - jmp 1b -2: - /* * Do the extraction, and jump to the new kernel.. */
@@ -512,6 +533,27 @@ relocated: */ jmp *%rax +/* + * Adjust the global offset table + * + * RAX is the previous adjustment of the table to undo (use 0 if it's the + * first time we touch GOT). + * RDI is the new adjustment to apply. + */ +adjust_got: + /* Walk through the GOT adding the address to the entries */ + leaq _got(%rip), %rdx + leaq _egot(%rip), %rcx +1: + cmpq %rcx, %rdx + jae 2f + subq %rax, (%rdx) /* Undo previous adjustment */ + addq %rdi, (%rdx) /* Apply the new adjustment */ + addq $8, %rdx + jmp 1b +2: + ret + .code32 /* * This is the 32-bit trampoline that will be copied over to low memory.
Signature · 1 line
2.17.0
Re: [PATCHv2 1/2] x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()
Quoted 14 lines · Kirill A. Shutemov
Eric and Hugh have reported instant reboot due to my recent changes in decompression code.
The root cause is that I didn't realize that we need to adjust GOT to be able to run C code that early.
The problem is only visible with an older toolchain. Binutils >= 2.24 is able to eliminate GOT references by replacing them with RIP-relative address loads[1].
We need to adjust GOT two times:
- before callingpaging_prepare()using the initial load address
- before calling C code from the relocated kernel
Quoted 6 lines
[1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=80d873266dec
Signed-off-by Kirill A. Shutemov <[email protected]>Fixes 194a9749c73d ("x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G")Reported-by Eric Dumazet <[email protected]>Reported-by Hugh Dickins <[email protected]>
Very nice and well done!
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/urgent] x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()
In reply to Kirill A. Shutemovx86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()
Commit-ID: 5c9b0b1c49881c680d4a56b9d9e03dfb3160fd4d Gitweb: https://git.kernel.org/tip/5c9b0b1c49881c680d4a56b9d9e03dfb3160fd4d Author: Kirill A. Shutemov <[email protected]> AuthorDate: Wed, 16 May 2018 11:01:28 +0300 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 16 May 2018 12:15:13 +0200
x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()
Eric and Hugh have reported instant reboot due to my recent changes in decompression code.
The root cause is that I didn't realize that we need to adjust GOT to be able to run C code that early.
The problem is only visible with an older toolchain. Binutils >= 2.24 is able to eliminate GOT references by replacing them with RIP-relative address loads:
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=80d873266dec
We need to adjust GOT two times:
- before calling paging_prepare() using the initial load address
- before calling C code from the relocated kernel
--- arch/x86/boot/compressed/head_64.S | 68 ++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 13 deletions(-)
arch/x86/boot/compressed/head_64.S +55 -13
@@ -305,6 +305,25 @@ ENTRY(startup_64) /* Set up the stack */ leaq boot_stack_end(%rbx), %rsp + /* + * paging_prepare() and cleanup_trampoline() below can have GOT + * references. Adjust the table with address we are running at. + * + * Zero RAX for adjust_got: the GOT was not adjusted before; + * there's no adjustment to undo. + */ + xorq %rax, %rax + + /* + * Calculate the address the binary is loaded at and use it as + * a GOT adjustment. + */ + call 1f +1: popq %rdi + subq $1b, %rdi + + call adjust_got + /* * At this point we are in long mode with 4-level paging enabled, * but we might want to enable 5-level paging or vice versa.
@@ -381,6 +400,21 @@ trampoline_return: pushq $0 popfq + /* + * Previously we've adjusted the GOT with address the binary was + * loaded at. Now we need to re-adjust for relocation address. + * + * Calculate the address the binary is loaded at, so that we can + * undo the previous GOT adjustment. + */ + call 1f +1: popq %rax + subq $1b, %rax + + /* The new adjustment is the relocation address */ + movq %rbx, %rdi + call adjust_got + /* * Copy the compressed kernel to the end of our buffer * where decompression in place becomes safe.
@@ -481,19 +515,6 @@ relocated: shrq $3, %rcx rep stosq -/* - * Adjust our own GOT - */ - leaq _got(%rip), %rdx - leaq _egot(%rip), %rcx -1: - cmpq %rcx, %rdx - jae 2f - addq %rbx, (%rdx) - addq $8, %rdx - jmp 1b -2: - /* * Do the extraction, and jump to the new kernel.. */
@@ -512,6 +533,27 @@ relocated: */ jmp *%rax +/* + * Adjust the global offset table + * + * RAX is the previous adjustment of the table to undo (use 0 if it's the + * first time we touch GOT). + * RDI is the new adjustment to apply. + */ +adjust_got: + /* Walk through the GOT adding the address to the entries */ + leaq _got(%rip), %rdx + leaq _egot(%rip), %rcx +1: + cmpq %rcx, %rdx + jae 2f + subq %rax, (%rdx) /* Undo previous adjustment */ + addq %rdi, (%rdx) /* Apply the new adjustment */ + addq $8, %rdx + jmp 1b +2: + ret + .code32 /* * This is the 32-bit trampoline that will be copied over to low memory.
PATCH v2 2/2 x86/boot/compressed/64: Fix moving page table out of trampoline memory
In reply to Kirill A. Shutemovcleanup_trampoline() relocates the top-level page table out of trampoline memory. We use 'top_pgtable' as our new top-level page table.
But if the 'top_pgtable' would be referenced from C in a usual way, the address of the table will be calculated relative to RIP. After kernel gets relocated, the address will be in the middle of decompression buffer and the page table may get overwritten. This leads to a crash.
We calculate the address of other page tables relative to the relocation address. It makes them safe. We should do the same for 'top_pgtable'.
Calculate the address of 'top_pgtable' in assembly and pass down to cleanup_trampoline().
Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.
--- arch/x86/boot/compressed/head_64.S | 11 +++++++++++ arch/x86/boot/compressed/pgtable_64.c | 14 +++----------- 2 files changed, 14 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +11 -0
@@ -389,10 +389,14 @@ trampoline_return: /* * cleanup_trampoline() would restore trampoline memory. * + * RDI is address of the page table to use instead of page table + * in trampoline memory (if required). + * * RSI holds real mode data and needs to be preserved across * this function call. */ pushq %rsi + leaq top_pgtable(%rbx), %rdi call cleanup_trampoline popq %rsi
@@ -691,3 +695,10 @@ boot_stack_end: .balign 4096 pgtable: .fill BOOT_PGT_SIZE, 1, 0 + +/* + * The page table is going to be used instead of page table in the trampoline + * memory. + */ +top_pgtable: + .fill PAGE_SIZE, 1, 0
arch/x86/boot/compressed/pgtable_64.c +3 -11
@@ -22,14 +22,6 @@ struct paging_config {/* Buffer to preserve trampoline memory */ static char trampoline_save[TRAMPOLINE_32BIT_SIZE ]; -/* -* The page table is going to be used instead of page table in the trampoline -* memory. -* -* It must not be in BSS as BSS is cleared after cleanup_trampoline(). -*/ -static char top_pgtable[PAGE_SIZE] __aligned(PAGE_SIZE) __section(.data); -/* * Trampoline address will be printed by extract_kernel() for debugging * purposes.
@@ -134,7 +126,7 @@ struct paging_config paging_prepare(void)return paging_config; } -void cleanup_trampoline (void ) +void cleanup_trampoline (void *pgtable) {void *trampoline_pgtable;
@@ -145,8 +137,8 @@ void cleanup_trampoline(void)* if it's there. */ if ((void *)__native_read_cr3 () == trampoline_pgtable) { -memcpy (top_pgtable, trampoline_pgtable,PAGE_SIZE ); -native_write_cr3 ((unsigned long )top_pgtable); +memcpy (pgtable, trampoline_pgtable,PAGE_SIZE ); +native_write_cr3 ((unsigned long )pgtable); }/* Restore trampoline memory */
Signature · 1 line
2.17.0
Re: [PATCHv2 2/2] x86/boot/compressed/64: Fix moving page table out of trampoline memory
Quoted 12 lines · Kirill A. Shutemov
cleanup_trampoline()relocates the top-level page table out of trampoline memory. We use 'top_pgtable' as our new top-level page table.But if the '
top_pgtable' would be referenced from C in a usual way, the address of the table will be calculated relative to RIP. After kernel gets relocated, the address will be in the middle of decompression buffer and the page table may get overwritten. This leads to a crash.We calculate the address of other page tables relative to the relocation address. It makes them safe. We should do the same for '
top_pgtable'.
Quoted 8 lines
Calculate the address of '
top_pgtable' in assembly and pass down tocleanup_trampoline().Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.
Signed-off-by Kirill A. Shutemov <[email protected]>Fixes e9d0e6330eb8 ("x86/boot/compressed/64: Prepare new top-level page table for trampoline")
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/urgent] x86/boot/compressed/64: Fix moving page table out of trampoline memory
In reply to Kirill A. Shutemovx86/boot/compressed/64: Fix moving page table out of trampoline memory
Commit-ID: 589bb62be316401603453c7d2d3c60ad8b9c3cf3 Gitweb: https://git.kernel.org/tip/589bb62be316401603453c7d2d3c60ad8b9c3cf3 Author: Kirill A. Shutemov <[email protected]> AuthorDate: Wed, 16 May 2018 11:01:29 +0300 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 16 May 2018 12:15:13 +0200
x86/boot/compressed/64: Fix moving page table out of trampoline memory
cleanup_trampoline() relocates the top-level page table out of trampoline memory. We use 'top_pgtable' as our new top-level page table.
But if the 'top_pgtable' would be referenced from C in a usual way, the address of the table will be calculated relative to RIP. After kernel gets relocated, the address will be in the middle of decompression buffer and the page table may get overwritten. This leads to a crash.
We calculate the address of other page tables relative to the relocation address. It makes them safe. We should do the same for 'top_pgtable'.
Calculate the address of 'top_pgtable' in assembly and pass down to cleanup_trampoline().
Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.
--- arch/x86/boot/compressed/head_64.S | 11 +++++++++++ arch/x86/boot/compressed/pgtable_64.c | 14 +++----------- 2 files changed, 14 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +11 -0
@@ -389,10 +389,14 @@ trampoline_return: /* * cleanup_trampoline() would restore trampoline memory. * + * RDI is address of the page table to use instead of page table + * in trampoline memory (if required). + * * RSI holds real mode data and needs to be preserved across * this function call. */ pushq %rsi + leaq top_pgtable(%rbx), %rdi call cleanup_trampoline popq %rsi
@@ -691,3 +695,10 @@ boot_stack_end: .balign 4096 pgtable: .fill BOOT_PGT_SIZE, 1, 0 + +/* + * The page table is going to be used instead of page table in the trampoline + * memory. + */ +top_pgtable: + .fill PAGE_SIZE, 1, 0
arch/x86/boot/compressed/pgtable_64.c +3 -11
@@ -22,14 +22,6 @@ struct paging_config {/* Buffer to preserve trampoline memory */ static char trampoline_save[TRAMPOLINE_32BIT_SIZE ]; -/* -* The page table is going to be used instead of page table in the trampoline -* memory. -* -* It must not be in BSS as BSS is cleared after cleanup_trampoline(). -*/ -static char top_pgtable[PAGE_SIZE] __aligned(PAGE_SIZE) __section(.data); -/* * Trampoline address will be printed by extract_kernel() for debugging * purposes.
@@ -134,7 +126,7 @@ out:return paging_config; } -void cleanup_trampoline (void ) +void cleanup_trampoline (void *pgtable) {void *trampoline_pgtable;
@@ -145,8 +137,8 @@ void cleanup_trampoline(void)* if it's there. */ if ((void *)__native_read_cr3 () == trampoline_pgtable) { -memcpy (top_pgtable, trampoline_pgtable,PAGE_SIZE ); -native_write_cr3 ((unsigned long )top_pgtable); +memcpy (pgtable, trampoline_pgtable,PAGE_SIZE ); +native_write_cr3 ((unsigned long )pgtable); }/* Restore trampoline memory */
PATCH 3/4 x86/boot/compressed/64: Fix trampoline page table address calculation
In reply to Kirill A. ShutemovHugh noticied that I calculate address of trampoline page table wrong in cleanup_trampoline(). TRAMPOLINE_32BIT_PGTABLE_OFFSET has to be divided by sizeof(unsigned long) since trampoline_32bit is unsigned long pointer.
TRAMPOLINE_32BIT_PGTABLE_OFFSET is zero so the bug doesn't have a visible effect.
--- arch/x86/boot/compressed/pgtable_64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/compressed/pgtable_64.c +1 -1
@@ -130,7 +130,7 @@ void cleanup_trampoline(void *pgtable) {void *trampoline_pgtable; - trampoline_pgtable = trampoline_32bit +TRAMPOLINE_32BIT_PGTABLE_OFFSET ; + trampoline_pgtable = trampoline_32bit +TRAMPOLINE_32BIT_PGTABLE_OFFSET /sizeof (unsigned long );/* * Move the top level page table out of trampoline memory,
Signature · 1 line
2.17.0
Re: [PATCH 3/4] x86/boot/compressed/64: Fix trampoline page table address calculation
Ouch. Please ignore this and the next patch. It was sent by mistake. Sorry.
Signature · 1 line
Kirill A. Shutemov
PATCH 4/4 x86/mm: Introduce 'no5lvl' kernel parameter
In reply to Kirill A. ShutemovThe kernel parameter allows to force kernel to use 4-level paging even if hardware and kernel support 5-level paging.
The option may be useful to workaround regressions related to 5-level paging.
--- Documentation/admin-guide/kernel-parameters.txt | 3 +++ arch/x86/boot/compressed/cmdline.c | 2 +- arch/x86/boot/compressed/head_64.S | 1 + arch/x86/boot/compressed/pgtable_64.c | 12 ++++++++++-- arch/x86/kernel/cpu/common.c | 6 ++++++ arch/x86/kernel/head64.c | 10 ++++++---- 6 files changed, 27 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -2600,6 +2600,9 @@ emulation library even if a 387 maths coprocessor is present. + no5lvl [X86-64] Disable 5-level paging mode. Forces + kernel to use 4-level paging instead. + no_console_suspend [HW] Never suspend the console Disable suspending of consoles during suspend and
arch/x86/boot/compressed/cmdline.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include "misc.h" -#if CONFIG_EARLY_PRINTK ||CONFIG_RANDOMIZE_BASE +#if CONFIG_EARLY_PRINTK ||CONFIG_RANDOMIZE_BASE ||CONFIG_X86_5LEVEL static unsigned long fs;static inline void set_fs (unsigned long seg)
arch/x86/boot/compressed/head_64.S +1 -0
@@ -365,6 +365,7 @@ ENTRY(startup_64) * this function call. */ pushq %rsi + movq %rsi, %rdi /* real mode address */ call paging_prepare popq %rsi
arch/x86/boot/compressed/pgtable_64.c +10 -2
@@ -31,16 +31,23 @@ static char trampoline_save[TRAMPOLINE_32BIT_SIZE];*/ unsigned long *trampoline_32bit __section(.data); -struct paging_config paging_prepare (void ) +extern struct boot_params *boot_params; +int cmdline_find_option_bool (const char *option); + +struct paging_config paging_prepare (void *rmode) {struct paging_config paging_config = {};unsigned long bios_start, ebda_start; +/* Initialize boot_params. Required for cmdline_find_option_bool(). */ + boot_params = rmode; +/* * Check if LA57 is desired and supported. * -* There are two parts to the check: +* There are several parts to the check: * - if the kernel supports 5-level paging: CONFIG_X86_5LEVEL=y +* - if user asked to disable 5-level paging: no5lvl in cmdline * - if the machine supports 5-level paging: * + CPUID leaf 7 is supported * + the leaf has the feature bit set
@@ -48,6 +55,7 @@ struct paging_config paging_prepare(void)* That's substitute for boot_cpu_has() in early boot code. */ if (IS_ENABLED (CONFIG_X86_5LEVEL ) && + !cmdline_find_option_bool ("no5lvl" ) &&native_cpuid_eax (0 ) >=7 && (native_cpuid_ecx (7 ) & (1 << (X86_FEATURE_LA57 &31 )))) { paging_config.l5_required =1 ;
arch/x86/kernel/cpu/common.c +6 -0
@@ -1008,6 +1008,12 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)*/ setup_clear_cpu_cap (X86_FEATURE_PCID );#endif + +#ifdef CONFIG_X86_5LEVEL +/* Clear the 5-level paging feature if user asked for 'no5lvl' */ +if (!__pgtable_l5_enabled) +setup_clear_cpu_cap (X86_FEATURE_LA57 ); +#endif }void __init early_cpu_init (void )
arch/x86/kernel/head64.c +6 -4
@@ -82,10 +82,12 @@ static unsigned int __head *fixup_int(void *ptr, unsigned long physaddr) static bool __head check_la57_support(unsigned long physaddr) { -if (native_cpuid_eax (0 ) <7 ) -return false; - -if (!(native_cpuid_ecx (7 ) & (1 << (X86_FEATURE_LA57 &31 )))) +/* +* 5-level paging is detected and enabled on kernel decomression +* stage. Back off if 5-level paging mode has not yet enabled by +* this point. +*/ +if (!(native_read_cr4 () &X86_CR4_LA57 ))return false; *fixup_int (&pgtable_l5_enabled, physaddr) =1 ;
Signature · 1 line
2.17.0