Skip to content

Commit e5ceb9a

Browse files
nivedita76suryasaimadhu
authored andcommitted
x86/boot/64: Initialize 5-level paging variables earlier
Commit ca0e22d ("x86/boot/compressed/64: Always switch to own page table") started using a new set of pagetables even without KASLR. After that commit, initialize_identity_maps() is called before the 5-level paging variables are setup in choose_random_location(), which will not work if 5-level paging is actually enabled. Fix this by moving the initialization of __pgtable_l5_enabled, pgdir_shift and ptrs_per_p4d into cleanup_trampoline(), which is called immediately after the finalization of whether the kernel is executing with 4- or 5-level paging. This will be earlier than anything that might require those variables, and keeps the 4- vs 5-level paging code all in one place. Fixes: ca0e22d ("x86/boot/compressed/64: Always switch to own page table") Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Joerg Roedel <jroedel@suse.de> Tested-by: Joerg Roedel <jroedel@suse.de> Tested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Link: https://lkml.kernel.org/r/20201010191110.4060905-1-nivedita@alum.mit.edu
1 parent da9803d commit e5ceb9a

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

arch/x86/boot/compressed/ident_map_64.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
#define __PAGE_OFFSET __PAGE_OFFSET_BASE
3434
#include "../../mm/ident_map.c"
3535

36-
#ifdef CONFIG_X86_5LEVEL
37-
unsigned int __pgtable_l5_enabled;
38-
unsigned int pgdir_shift = 39;
39-
unsigned int ptrs_per_p4d = 1;
40-
#endif
41-
4236
/* Used by PAGE_KERN* macros: */
4337
pteval_t __default_kernel_pte_mask __read_mostly = ~0;
4438

arch/x86/boot/compressed/kaslr.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,6 @@ void choose_random_location(unsigned long input,
840840
return;
841841
}
842842

843-
#ifdef CONFIG_X86_5LEVEL
844-
if (__read_cr4() & X86_CR4_LA57) {
845-
__pgtable_l5_enabled = 1;
846-
pgdir_shift = 48;
847-
ptrs_per_p4d = 512;
848-
}
849-
#endif
850-
851843
boot_params->hdr.loadflags |= KASLR_FLAG;
852844

853845
if (IS_ENABLED(CONFIG_X86_32))

arch/x86/boot/compressed/pgtable_64.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
#define BIOS_START_MIN 0x20000U /* 128K, less than this is insane */
99
#define BIOS_START_MAX 0x9f000U /* 640K, absolute maximum */
1010

11+
#ifdef CONFIG_X86_5LEVEL
12+
/* __pgtable_l5_enabled needs to be in .data to avoid being cleared along with .bss */
13+
unsigned int __section(.data) __pgtable_l5_enabled;
14+
unsigned int __section(.data) pgdir_shift = 39;
15+
unsigned int __section(.data) ptrs_per_p4d = 1;
16+
#endif
17+
1118
struct paging_config {
1219
unsigned long trampoline_start;
1320
unsigned long l5_required;
@@ -198,4 +205,13 @@ void cleanup_trampoline(void *pgtable)
198205

199206
/* Restore trampoline memory */
200207
memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE);
208+
209+
/* Initialize variables for 5-level paging */
210+
#ifdef CONFIG_X86_5LEVEL
211+
if (__read_cr4() & X86_CR4_LA57) {
212+
__pgtable_l5_enabled = 1;
213+
pgdir_shift = 48;
214+
ptrs_per_p4d = 512;
215+
}
216+
#endif
201217
}

0 commit comments

Comments
 (0)