x86: 5-level related changes into decompression code
Subsystems (2)
cover x86: 5-level related changes into decompression code
Hi Ingo,
While we haven't yet closed on how to handle MAX_PHYSMEM_BITS situation, could you look on changes into kernel decompression code?
These changes prepare the code to boot-time switching between paging modes and handle booting in 5-level mode when bootloader put kernel image above 4G, but haven't enabled 5-level paging for us.
Please review and consider applying.
Kirill A. Shutemov (4):
x86/boot/compressed/64: Compile pagetable.c unconditionally x86/boot/compressed/64: Detect and handle 5-level paging at boot-time x86/boot/compressed/64: Introduce place_trampoline() x86/boot/compressed/64: Handle 5-level paging boot if kernel is above
4G
arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/head_64.S | 99 +++++++++++++++++++++++++----------- arch/x86/boot/compressed/pagetable.c | 66 ++++++++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 +++++++ 4 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 arch/x86/boot/compressed/pagetable.h
Signature · 1 line
2.14.2
PATCH 1/4 x86/boot/compressed/64: Compile pagetable.c unconditionally
We are going to put few helpers into pagetable.c that are not specific to KASLR.
Let's make compilation of the file independent of KASLR and wrap KASLR-depended code into ifdef.
--- arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/pagetable.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/Makefile +1 -1
@@ -77,7 +77,7 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o ifdef CONFIG_X86_64 - vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o + vmlinux-objs-y += $(obj)/pagetable.o endif $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
arch/x86/boot/compressed/pagetable.c +5 -0
@@ -27,6 +27,9 @@/* These actually do the work of building the kernel identity maps. */ #include <asm/init.h> #include <asm/pgtable.h> + +#ifdef CONFIG_RANDOMIZE_BASE +/* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET#define __PAGE_OFFSET __PAGE_OFFSET_BASE
@@ -149,3 +152,5 @@ void finalize_identity_maps(void) {write_cr3 (top_level_pgt); } + +#endif /* CONFIG_RANDOMIZE_BASE */
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] x86/boot/compressed/64: Compile pagetable.c unconditionally
Quoted 40 lines · Kirill A. Shutemov
We are going to put few helpers into
pagetable.cthat are not specific to KASLR.Let's make compilation of the file independent of KASLR and wrap KASLR-depended code into ifdef.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/pagetable.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)arch/x86/boot/compressed/Makefile +1
-1@@ -77,7 +77,7 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o ifdef CONFIG_X86_64 - vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o + vmlinux-objs-y += $(obj)/pagetable.o endif $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zonearch/x86/boot/compressed/pagetable.c +3
-0@@ -27,6 +27,9 @@/* These actually do the work of building the kernel identity maps. */ #include <asm/init.h> #include <asm/pgtable.h> + +#ifdef CONFIG_RANDOMIZE_BASE +/* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET#define __PAGE_OFFSET __PAGE_OFFSET_BASE
arch/x86/boot/compressed/pagetable.c void finalize_identity_maps(void) · line 156
Quoted 5 lines
arch/x86/boot/compressed/pagetable.c +2
-0@@ -149,3 +152,5 @@ void finalize_identity_maps(void) {write_cr3 (top_level_pgt); } + +#endif /* CONFIG_RANDOMIZE_BASE */
The #ifdeffery becomes really ugly in this file. I think we should split these into separate .c files:
arch/x86/boot/compressed/kaslr.c
arch/x86/boot/compressed/5-level-paging.c
With core data structures and code and a well defined interface:
arch/x86/boot/compressed/pagetable.c
arch/x86/boot/compressed/pagetable.h
or so.
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] x86/boot/compressed/64: Compile pagetable.c unconditionally
Quoted 55 lines · Ingo Molnar
Quoted 40 lines · Kirill A. Shutemov
We are going to put few helpers into
pagetable.cthat are not specific to KASLR.Let's make compilation of the file independent of KASLR and wrap KASLR-depended code into ifdef.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/pagetable.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)arch/x86/boot/compressed/Makefile +1
-1@@ -77,7 +77,7 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o ifdef CONFIG_X86_64 - vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o + vmlinux-objs-y += $(obj)/pagetable.o endif $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zonearch/x86/boot/compressed/pagetable.c +3
-0@@ -27,6 +27,9 @@/* These actually do the work of building the kernel identity maps. */ #include <asm/init.h> #include <asm/pgtable.h> + +#ifdef CONFIG_RANDOMIZE_BASE +/* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET#define __PAGE_OFFSET __PAGE_OFFSET_BASEarch/x86/boot/compressed/pagetable.c void finalize_identity_maps(void) · line 156
Quoted 5 lines
arch/x86/boot/compressed/pagetable.c +2
-0@@ -149,3 +152,5 @@ void finalize_identity_maps(void) {write_cr3 (top_level_pgt); } + +#endif /* CONFIG_RANDOMIZE_BASE */ The #ifdeffery becomes really ugly in this file. I think we should split these into separate .c files:
arch/x86/boot/compressed/kaslr.c
arch/x86/boot/compressed/5-level-paging.c
Quoted 6 lines
With core data structures and code and a well defined interface:
arch/x86/boot/compressed/pagetable.c
arch/x86/boot/compressed/pagetable.hor so.
Okay, I'll do this.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/4 x86/boot/compressed/64: Detect and handle 5-level paging at boot-time
In reply to Kirill A. ShutemovThis patch prepare decompression code to boot-time switching between 4- and 5-level paging.
--- arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pagetable.c | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -288,10 +288,18 @@ ENTRY(startup_64) leaq boot_stack_end(%rbx), %rsp #ifdef CONFIG_X86_5LEVEL - /* Check if 5-level paging has already enabled */ - movq %cr4, %rax - testl $X86_CR4_LA57, %eax - jnz lvl5 + /* + * Check if we need to enable 5-level paging. + * RSI holds real mode data and need to be preserved across + * a function call. + */ + pushq %rsi + call need_to_enabled_l5 + popq %rsi + + /* If need_to_enabled_l5() returned zero, we're done here. */ + cmpq $0, %rax + je lvl5 /* * At this point we are in long mode with 4-level paging enabled,
arch/x86/boot/compressed/pagetable.c +19 -0
@@ -154,3 +154,22 @@ void finalize_identity_maps(void) }#endif /* CONFIG_RANDOMIZE_BASE */ + +#ifdef CONFIG_X86_5LEVEL +int need_to_enabled_l5 (void ) +{ +/* Check i leaf 7 is supported. */ +if (native_cpuid_eax (0 ) <7 ) +return 0 ; + +/* Check if la57 is supported. */ +if (!(native_cpuid_ecx (7 ) & (1 << (X86_FEATURE_LA57 &31 )))) +return 0 ; + +/* Check if 5-level paging has already been enabled. */ +if (native_read_cr4 () &X86_CR4_LA57 ) +return 0 ; + +return 1 ; +} +#endif
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/4] x86/boot/compressed/64: Detect and handle 5-level paging at boot-time
Quoted 53 lines · Kirill A. Shutemov
This patch prepare decompression code to boot-time switching between 4- and 5-level paging.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pagetable.c | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-)arch/x86/boot/compressed/head_64.S +12
-4@@ -288,10 +288,18 @@ ENTRY(startup_64) leaq boot_stack_end(%rbx), %rsp #ifdef CONFIG_X86_5LEVEL - /* Check if 5-level paging has already enabled */ - movq %cr4, %rax - testl $X86_CR4_LA57, %eax - jnz lvl5 + /* + * Check if we need to enable 5-level paging. + * RSI holds real mode data and need to be preserved across + * a function call. + */ + pushq %rsi + call need_to_enabled_l5 + popq %rsi + + /* If need_to_enabled_l5() returned zero, we're done here. */ + cmpq $0, %rax + je lvl5 /* * At this point we are in long mode with 4-level paging enabled,arch/x86/boot/compressed/pagetable.c +9
-0@@ -154,3 +154,22 @@ void finalize_identity_maps(void) }#endif /* CONFIG_RANDOMIZE_BASE */ + +#ifdef CONFIG_X86_5LEVEL +int need_to_enabled_l5 (void ) +{ +/* Check i leaf 7 is supported. */ +if (native_cpuid_eax (0 ) <7 ) +return 0 ; + +/* Check if la57 is supported. */
arch/x86/boot/compressed/pagetable.c void finalize_identity_maps(void) · line 175
Quoted 10 lines
arch/x86/boot/compressed/pagetable.c +10
-0+if (!(native_cpuid_ecx (7 ) & (1 << (X86_FEATURE_LA57 &31 )))) +return 0 ; + +/* Check if 5-level paging has already been enabled. */ +if (native_read_cr4 () &X86_CR4_LA57 ) +return 0 ; + + return 1; +} +#endif
Ok, I like this a lot better than doing this at the assembly level - and this could provide a model for how to further reduce assembly code.
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/4 x86/boot/compressed/64: Introduce place_trampoline()
In reply to Kirill A. ShutemovIf bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
To handle the situation, we need a trampoline in lower memory that would take care about switching on 5-level paging.
Apart from trampoline itself we also need place to store top level page table in lower memory as we don't have a way to load 64-bit value into CR3 from 32-bit mode. We only really need 8-bytes there as we only use the very first entry of the page table. But we allocate whole page anyway. We cannot have the code in the same because, there's hazard that a CPU would read page table speculatively and get confused seeing garbage.
This patch introduces place_trampoline() that finds right spot in lower memory for trampoline, copies trampoline code there and setups new top level page table for 5-level paging.
At this point we do all the preparation, but not yet use trampoline. It will be done in following patch.
--- arch/x86/boot/compressed/head_64.S | 13 +++++++++++ arch/x86/boot/compressed/pagetable.c | 42 ++++++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 ++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 arch/x86/boot/compressed/pagetable.h
arch/x86/boot/compressed/head_64.S +13 -0
@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved across + * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %rax
@@ -474,6 +486,7 @@ relocated: .code32 #ifdef CONFIG_X86_5LEVEL +ENTRY(lvl5_trampoline_src) compatible_mode: /* Setup data and stack segments */ movl $__KERNEL_DS, %eax
arch/x86/boot/compressed/pagetable.c +42 -0
52 lines not on this page · raw message
arch/x86/boot/compressed/pagetable.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef BOOT_COMPRESSED_PAGETABLE_H +#define BOOT_COMPRESSED_PAGETABLE_H + +#define LVL5_TRAMPOLINE_SIZE (2 * PAGE_SIZE) + +#define LVL5_TRAMPOLINE_PGTABLE_OFF 0 + +#define LVL5_TRAMPOLINE_CODE_OFF PAGE_SIZE +#define LVL5_TRAMPOLINE_CODE_SIZE 0x40 + +#define LVL5_TRAMPOLINE_STACK_END LVL5_TRAMPOLINE_SIZE + +#ifndef __ASSEMBLER__ + +extern void (*lvl5_trampoline_src)(void *return_ptr); + +#endif /* __ASSEMBLER__ */ +#endif /* BOOT_COMPRESSED_PAGETABLE_H */
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 26 lines · Kirill A. Shutemov
If bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
To handle the situation, we need a trampoline in lower memory that would take care about switching on 5-level paging.
Apart from trampoline itself we also need place to store top level page table in lower memory as we don't have a way to load 64-bit value into CR3 from 32-bit mode. We only really need 8-bytes there as we only use the very first entry of the page table. But we allocate whole page anyway. We cannot have the code in the same because, there's hazard that a CPU would read page table speculatively and get confused seeing garbage.
This patch introduces
place_trampoline()that finds right spot in lower memory for trampoline, copies trampoline code there and setups new top level page table for 5-level paging.At this point we do all the preparation, but not yet use trampoline. It will be done in following patch.
Quoted 7 lines
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/boot/compressed/head_64.S | 13 +++++++++++ arch/x86/boot/compressed/pagetable.c | 42 ++++++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 ++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 arch/x86/boot/compressed/pagetable.h
Ok, I like it how almost all of the complexity is now at the .c level.
I'm wondering, how did you test it if no current bootloader puts the kernel to above addresses of 4G?
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 2 lines · Ingo Molnar
I'm wondering, how did you test it if no current bootloader puts the kernel to above addresses of 4G?
I didn't test it directly.
I've checked with debugger that there's no memory accesses above 1M between disabling paging in the trampoline and returning back from trampoline to the main kernel image code.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
In reply to Kirill A. ShutemovQuoted 47 lines · Kirill A. Shutemov
If bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
To handle the situation, we need a trampoline in lower memory that would take care about switching on 5-level paging.
Apart from trampoline itself we also need place to store top level page table in lower memory as we don't have a way to load 64-bit value into CR3 from 32-bit mode. We only really need 8-bytes there as we only use the very first entry of the page table. But we allocate whole page anyway. We cannot have the code in the same because, there's hazard that a CPU would read page table speculatively and get confused seeing garbage.
This patch introduces
place_trampoline()that finds right spot in lower memory for trampoline, copies trampoline code there and setups new top level page table for 5-level paging.At this point we do all the preparation, but not yet use trampoline. It will be done in following patch.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/boot/compressed/head_64.S | 13 +++++++++++ arch/x86/boot/compressed/pagetable.c | 42 ++++++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 ++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 arch/x86/boot/compressed/pagetable.harch/x86/boot/compressed/head_64.S +5
-0@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved across
arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 332
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %rax
One request: it's always going to be fragile if the _only_ thing that uses the trampoline is the 5-level paging code.
Could we use the trampoline in the 4-level paging case too? It's not required, but would test much of the trampoline allocation and copying machinery - and the performance cost is negligible.
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 25 lines · Ingo Molnar
Quoted 11 lines
arch/x86/boot/compressed/head_64.S +5
-0@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved acrossarch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 332
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %raxOne request: it's always going to be fragile if the _only_ thing that uses the trampoline is the 5-level paging code.
Quoted 3 lines
Could we use the trampoline in the 4-level paging case too? It's not required, but would test much of the trampoline allocation and copying machinery - and the performance cost is negligible.
Note that right now the trampoline is pointless on 4-level setups, so there's nothing to copy - but we could perhaps make it meaningful. But maybe it's not a good idea.
One other detail I noticed:
/* Bound size of trampoline code */
.org lvl5_trampoline_src + LVL5_TRAMPOLINE_CODE_SIZE
will this generate a build error if the trampoline code exceeds 0x40?
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 28 lines · Ingo Molnar
Quoted 25 lines · Ingo Molnar
Quoted 11 lines
arch/x86/boot/compressed/head_64.S +5
-0@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved acrossarch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 332
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %raxOne request: it's always going to be fragile if the _only_ thing that uses the trampoline is the 5-level paging code.
Quoted 7 lines
Quoted 3 lines
Could we use the trampoline in the 4-level paging case too? It's not required, but would test much of the trampoline allocation and copying machinery - and the performance cost is negligible.
Note that right now the trampoline is pointless on 4-level setups, so there's nothing to copy - but we could perhaps make it meaningful. But maybe it's not a good idea.
Let me see how it will play out.
Quoted 6 lines
One other detail I noticed:
/* Bound size of trampoline code */
.orglvl5_trampoline_src+LVL5_TRAMPOLINE_CODE_SIZEwill this generate a build error if the trampoline code exceeds
0x40?
Yes, this is the point. Just a failsafe if trampoline code would grew too much.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 9 lines · Kirill A. Shutemov
Quoted 6 lines
One other detail I noticed:
/* Bound size of trampoline code */
.orglvl5_trampoline_src+LVL5_TRAMPOLINE_CODE_SIZEwill this generate a build error if the trampoline code exceeds
0x40?Yes, this is the point. Just a failsafe if trampoline code would grew too much.
Ok, good!
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
In reply to Kirill A. ShutemovQuoted 11 lines · Kirill A. Shutemov
arch/x86/boot/compressed/head_64.S +5
-0@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved across
arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 332
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %rax
So in the final version of this code we now have:
pushq %rsi call need_to_enabled_l5 popq %rsi /* If need_to_enabled_l5() returned zero, we're done here. */ cmpq $0, %rax je lvl5 /* * At this point we are in long mode with 4-level paging enabled, * but we want to enable 5-level paging. * * The problem is that we cannot do it directly. Setting LA57 in * long mode would trigger #GP. So we need to switch off long mode * first. * * We use trampoline in lower memory to handle situation when * bootloader put the kernel image above 4G. * * The first step is go into compatibility mode. */ /* * Find suitable place for trampoline and populate it. * The address will be stored in RCX. * * RSI holds real mode data and need to be preserved across * a function call. */ pushq %rsi call place_trampoline popq %rsi movq %rax, %rcx
Firstly, the 'need_to_enabled_l5' name sucks because it includes a typo, but also because the prefix is way too generic.
Something like:
l5_paging_required()
would read a lot better - and would also provide a namespace for all L5 paging related functions.
Secondly, couldn't this be combined into a single .c function, named accordingly:
l5_paging_prepare()
which would return true if L5 paging is available and should be enabled. In this case the trampoline copying function would be called in C, by l5_paging_prepare().
This further reduces the amount of assembly code.
Thanks,
Ingo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/4] x86/boot/compressed/64: Introduce place_trampoline()
Quoted 71 lines · Ingo Molnar
Quoted 11 lines · Kirill A. Shutemov
arch/x86/boot/compressed/head_64.S +5
-0@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved acrossarch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 332
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %raxSo in the final version of this code we now have:
pushq %rsi call need_to_enabled_l5 popq %rsi /* If need_to_enabled_l5() returned zero, we're done here. */ cmpq $0, %rax je lvl5 /* * At this point we are in long mode with 4-level paging enabled, * but we want to enable 5-level paging. * * The problem is that we cannot do it directly. Setting LA57 in * long mode would trigger #GP. So we need to switch off long mode * first. * * We use trampoline in lower memory to handle situation when * bootloader put the kernel image above 4G. * * The first step is go into compatibility mode. */ /* * Find suitable place for trampoline and populate it. * The address will be stored in RCX. * * RSI holds real mode data and need to be preserved across * a function call. */ pushq %rsi call place_trampoline popq %rsi movq %rax, %rcxFirstly, the '
need_to_enabled_l5' name sucks because it includes a typo, but also because the prefix is way too generic.Something like:
l5_paging_required()would read a lot better - and would also provide a namespace for all L5 paging related functions.
Quoted 8 lines
Secondly, couldn't this be combined into a single .c function, named accordingly:
l5_paging_prepare()which would return true if L5 paging is available and should be enabled. In this case the trampoline copying function would be called in C, by
l5_paging_prepare().This further reduces the amount of assembly code.
Makes sense.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/4 x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G
In reply to Kirill A. ShutemovThis patch addresses shortcoming in current boot process on machines that supports 5-level paging.
If bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
This patch implements trampoline in lower memory to handle this situation.
We only need the memory for very short time, until main kernel image setup its own page tables.
--- arch/x86/boot/compressed/head_64.S | 72 ++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 27 deletions(-)
arch/x86/boot/compressed/head_64.S +45 -27
@@ -32,6 +32,7 @@ #include <asm/processor-flags.h> #include <asm/asm-offsets.h> #include <asm/bootparam.h> +#include "pagetable.h" /* * Locally defined symbols should be marked hidden:
@@ -288,6 +289,19 @@ ENTRY(startup_64) leaq boot_stack_end(%rbx), %rsp #ifdef CONFIG_X86_5LEVEL +/* + * We need trampoline in lower memory switch from 4- to 5-level paging for + * cases when bootloader put kernel above 4G, but didn't enable 5-level paging + * for us. + * + * We also have to have top page table in lower memory as we don't have a way + * to load 64-bit value into CR3 from 32-bit mode. We only need 8-bytes there + * as we only use the very first entry of the page table, but we allocate whole + * page anyway. We cannot have the code in the same because, there's hazard + * that a CPU would read page table speculatively and get confused seeing + * garbage. + */ + /* * Check if we need to enable 5-level paging. * RSI holds real mode data and need to be preserved across
@@ -309,8 +323,8 @@ ENTRY(startup_64) * long mode would trigger #GP. So we need to switch off long mode * first. * - * NOTE: This is not going to work if bootloader put us above 4G - * limit. + * We use trampoline in lower memory to handle situation when + * bootloader put the kernel image above 4G. * * The first step is go into compatibility mode. */
@@ -327,26 +341,20 @@ ENTRY(startup_64) popq %rsi movq %rax, %rcx - /* Clear additional page table */ - leaq lvl5_pgtable(%rbx), %rdi - xorq %rax, %rax - movq $(PAGE_SIZE/8), %rcx - rep stosq - /* - * Setup current CR3 as the first and only entry in a new top level - * page table. + * Load address of lvl5 into RDI. + * It will be used to return address from trampoline. */ - movq %cr3, %rdi - leaq 0x7 (%rdi), %rax - movq %rax, lvl5_pgtable(%rbx) + leaq lvl5(%rip), %rdi /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ pushq $__KERNEL32_CS - leaq compatible_mode(%rip), %rax + leaq LVL5_TRAMPOLINE_CODE_OFF(%rcx), %rax pushq %rax lretq lvl5: + /* Restore stack, 32-bit trampoline uses own stack */ + leaq boot_stack_end(%rbx), %rsp #endif /* Zero EFLAGS */
@@ -484,22 +492,30 @@ relocated: */ jmp *%rax - .code32 #ifdef CONFIG_X86_5LEVEL + .code32 +/* + * This is 32-bit trampoline that will be copied over to low memory. + * + * RDI contains return address (might be above 4G). + * ECX contains the base address of trampoline memory. + */ ENTRY(lvl5_trampoline_src) -compatible_mode: /* Setup data and stack segments */ movl $__KERNEL_DS, %eax movl %eax, %ds movl %eax, %ss + /* Setup new stack at the end of trampoline memory */ + leal LVL5_TRAMPOLINE_STACK_END (%ecx), %esp + /* Disable paging */ movl %cr0, %eax btrl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 /* Point CR3 to 5-level paging */ - leal lvl5_pgtable(%ebx), %eax + leal LVL5_TRAMPOLINE_PGTABLE_OFF (%ecx), %eax movl %eax, %cr3 /* Enable PAE and LA57 mode */
@@ -507,23 +523,29 @@ compatible_mode: orl $(X86_CR4_PAE | X86_CR4_LA57), %eax movl %eax, %cr4 - /* Calculate address we are running at */ - call 1f -1: popl %edi - subl $1b, %edi + /* Calculate address of lvl5_enabled once we are in trampoline */ + leal lvl5_enabled - lvl5_trampoline_src + LVL5_TRAMPOLINE_CODE_OFF (%ecx), %eax /* Prepare stack for far return to Long Mode */ pushl $__KERNEL_CS - leal lvl5(%edi), %eax - push %eax + pushl %eax /* Enable paging back */ movl $(X86_CR0_PG | X86_CR0_PE), %eax movl %eax, %cr0 lret + + .code64 +lvl5_enabled: + /* Return from trampoline */ + jmp *%rdi + + /* Bound size of trampoline code */ + .org lvl5_trampoline_src + LVL5_TRAMPOLINE_CODE_SIZE #endif + .code32 no_longmode: /* This isn't an x86-64 CPU so hang */ 1:
@@ -581,7 +603,3 @@ boot_stack_end: .balign 4096 pgtable: .fill BOOT_PGT_SIZE, 1, 0 -#ifdef CONFIG_X86_5LEVEL -lvl5_pgtable: - .fill PAGE_SIZE, 1, 0 -#endif
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>