1 // SPDX-License-Identifier: GPL-2.0
3 * prepare to run common code
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
8 #define DISABLE_BRANCH_PROFILING
9 #include <linux/init.h>
10 #include <linux/linkage.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/percpu.h>
15 #include <linux/start_kernel.h>
17 #include <linux/memblock.h>
18 #include <linux/mem_encrypt.h>
20 #include <asm/processor.h>
21 #include <asm/proto.h>
23 #include <asm/setup.h>
25 #include <asm/pgtable.h>
26 #include <asm/tlbflush.h>
27 #include <asm/sections.h>
28 #include <asm/kdebug.h>
29 #include <asm/e820/api.h>
30 #include <asm/bios_ebda.h>
31 #include <asm/bootparam_utils.h>
32 #include <asm/microcode.h>
33 #include <asm/kasan.h>
36 * Manage page tables very early on.
38 extern pmd_t early_dynamic_pgts
[EARLY_DYNAMIC_PAGE_TABLES
][PTRS_PER_PMD
];
39 static unsigned int __initdata next_early_pgt
;
40 pmdval_t early_pmd_flags
= __PAGE_KERNEL_LARGE
& ~(_PAGE_GLOBAL
| _PAGE_NX
);
42 #define __head __section(.head.text)
44 static void __head
*fixup_pointer(void *ptr
, unsigned long physaddr
)
46 return ptr
- (void *)_text
+ (void *)physaddr
;
49 unsigned long __head
__startup_64(unsigned long physaddr
,
50 struct boot_params
*bp
)
52 unsigned long load_delta
, *p
;
53 unsigned long pgtable_flags
;
57 pmdval_t
*pmd
, pmd_entry
;
59 unsigned int *next_pgt_ptr
;
61 /* Is the address too large? */
62 if (physaddr
>> MAX_PHYSMEM_BITS
)
66 * Compute the delta between the address I am compiled to run at
67 * and the address I am actually running at.
69 load_delta
= physaddr
- (unsigned long)(_text
- __START_KERNEL_map
);
71 /* Is the address not 2M aligned? */
72 if (load_delta
& ~PMD_PAGE_MASK
)
75 /* Activate Secure Memory Encryption (SME) if supported and enabled */
78 /* Include the SME encryption mask in the fixup value */
79 load_delta
+= sme_get_me_mask();
81 /* Fixup the physical addresses in the page table */
83 pgd
= fixup_pointer(&early_top_pgt
, physaddr
);
84 pgd
[pgd_index(__START_KERNEL_map
)] += load_delta
;
86 if (IS_ENABLED(CONFIG_X86_5LEVEL
)) {
87 p4d
= fixup_pointer(&level4_kernel_pgt
, physaddr
);
88 p4d
[511] += load_delta
;
91 pud
= fixup_pointer(&level3_kernel_pgt
, physaddr
);
92 pud
[510] += load_delta
;
93 pud
[511] += load_delta
;
95 pmd
= fixup_pointer(level2_fixmap_pgt
, physaddr
);
96 pmd
[506] += load_delta
;
99 * Set up the identity mapping for the switchover. These
100 * entries should *NOT* have the global bit set! This also
101 * creates a bunch of nonsense entries but that is fine --
102 * it avoids problems around wraparound.
105 next_pgt_ptr
= fixup_pointer(&next_early_pgt
, physaddr
);
106 pud
= fixup_pointer(early_dynamic_pgts
[(*next_pgt_ptr
)++], physaddr
);
107 pmd
= fixup_pointer(early_dynamic_pgts
[(*next_pgt_ptr
)++], physaddr
);
109 pgtable_flags
= _KERNPG_TABLE_NOENC
+ sme_get_me_mask();
111 if (IS_ENABLED(CONFIG_X86_5LEVEL
)) {
112 p4d
= fixup_pointer(early_dynamic_pgts
[next_early_pgt
++], physaddr
);
114 i
= (physaddr
>> PGDIR_SHIFT
) % PTRS_PER_PGD
;
115 pgd
[i
+ 0] = (pgdval_t
)p4d
+ pgtable_flags
;
116 pgd
[i
+ 1] = (pgdval_t
)p4d
+ pgtable_flags
;
118 i
= (physaddr
>> P4D_SHIFT
) % PTRS_PER_P4D
;
119 p4d
[i
+ 0] = (pgdval_t
)pud
+ pgtable_flags
;
120 p4d
[i
+ 1] = (pgdval_t
)pud
+ pgtable_flags
;
122 i
= (physaddr
>> PGDIR_SHIFT
) % PTRS_PER_PGD
;
123 pgd
[i
+ 0] = (pgdval_t
)pud
+ pgtable_flags
;
124 pgd
[i
+ 1] = (pgdval_t
)pud
+ pgtable_flags
;
127 i
= (physaddr
>> PUD_SHIFT
) % PTRS_PER_PUD
;
128 pud
[i
+ 0] = (pudval_t
)pmd
+ pgtable_flags
;
129 pud
[i
+ 1] = (pudval_t
)pmd
+ pgtable_flags
;
131 pmd_entry
= __PAGE_KERNEL_LARGE_EXEC
& ~_PAGE_GLOBAL
;
132 pmd_entry
+= sme_get_me_mask();
133 pmd_entry
+= physaddr
;
135 for (i
= 0; i
< DIV_ROUND_UP(_end
- _text
, PMD_SIZE
); i
++) {
136 int idx
= i
+ (physaddr
>> PMD_SHIFT
) % PTRS_PER_PMD
;
137 pmd
[idx
] = pmd_entry
+ i
* PMD_SIZE
;
141 * Fixup the kernel text+data virtual addresses. Note that
142 * we might write invalid pmds, when the kernel is relocated
143 * cleanup_highmap() fixes this up along with the mappings
147 pmd
= fixup_pointer(level2_kernel_pgt
, physaddr
);
148 for (i
= 0; i
< PTRS_PER_PMD
; i
++) {
149 if (pmd
[i
] & _PAGE_PRESENT
)
150 pmd
[i
] += load_delta
;
154 * Fixup phys_base - remove the memory encryption mask to obtain
155 * the true physical address.
157 p
= fixup_pointer(&phys_base
, physaddr
);
158 *p
+= load_delta
- sme_get_me_mask();
160 /* Encrypt the kernel and related (if SME is active) */
161 sme_encrypt_kernel(bp
);
164 * Return the SME encryption mask (if SME is active) to be used as a
165 * modifier for the initial pgdir entry programmed into CR3.
167 return sme_get_me_mask();
170 unsigned long __startup_secondary_64(void)
173 * Return the SME encryption mask (if SME is active) to be used as a
174 * modifier for the initial pgdir entry programmed into CR3.
176 return sme_get_me_mask();
179 /* Wipe all early page tables except for the kernel symbol map */
180 static void __init
reset_early_page_tables(void)
182 memset(early_top_pgt
, 0, sizeof(pgd_t
)*(PTRS_PER_PGD
-1));
184 write_cr3(__sme_pa_nodebug(early_top_pgt
));
187 /* Create a new PMD entry */
188 int __init
__early_make_pgtable(unsigned long address
, pmdval_t pmd
)
190 unsigned long physaddr
= address
- __PAGE_OFFSET
;
191 pgdval_t pgd
, *pgd_p
;
192 p4dval_t p4d
, *p4d_p
;
193 pudval_t pud
, *pud_p
;
196 /* Invalid address or early pgt is done ? */
197 if (physaddr
>= MAXMEM
|| read_cr3_pa() != __pa_nodebug(early_top_pgt
))
201 pgd_p
= &early_top_pgt
[pgd_index(address
)].pgd
;
205 * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
206 * critical -- __PAGE_OFFSET would point us back into the dynamic
207 * range and we might end up looping forever...
209 if (!IS_ENABLED(CONFIG_X86_5LEVEL
))
212 p4d_p
= (p4dval_t
*)((pgd
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
214 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
215 reset_early_page_tables();
219 p4d_p
= (p4dval_t
*)early_dynamic_pgts
[next_early_pgt
++];
220 memset(p4d_p
, 0, sizeof(*p4d_p
) * PTRS_PER_P4D
);
221 *pgd_p
= (pgdval_t
)p4d_p
- __START_KERNEL_map
+ phys_base
+ _KERNPG_TABLE
;
223 p4d_p
+= p4d_index(address
);
227 pud_p
= (pudval_t
*)((p4d
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
229 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
230 reset_early_page_tables();
234 pud_p
= (pudval_t
*)early_dynamic_pgts
[next_early_pgt
++];
235 memset(pud_p
, 0, sizeof(*pud_p
) * PTRS_PER_PUD
);
236 *p4d_p
= (p4dval_t
)pud_p
- __START_KERNEL_map
+ phys_base
+ _KERNPG_TABLE
;
238 pud_p
+= pud_index(address
);
242 pmd_p
= (pmdval_t
*)((pud
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
244 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
245 reset_early_page_tables();
249 pmd_p
= (pmdval_t
*)early_dynamic_pgts
[next_early_pgt
++];
250 memset(pmd_p
, 0, sizeof(*pmd_p
) * PTRS_PER_PMD
);
251 *pud_p
= (pudval_t
)pmd_p
- __START_KERNEL_map
+ phys_base
+ _KERNPG_TABLE
;
253 pmd_p
[pmd_index(address
)] = pmd
;
258 int __init
early_make_pgtable(unsigned long address
)
260 unsigned long physaddr
= address
- __PAGE_OFFSET
;
263 pmd
= (physaddr
& PMD_MASK
) + early_pmd_flags
;
265 return __early_make_pgtable(address
, pmd
);
268 /* Don't add a printk in there. printk relies on the PDA which is not initialized
270 static void __init
clear_bss(void)
272 memset(__bss_start
, 0,
273 (unsigned long) __bss_stop
- (unsigned long) __bss_start
);
276 static unsigned long get_cmd_line_ptr(void)
278 unsigned long cmd_line_ptr
= boot_params
.hdr
.cmd_line_ptr
;
280 cmd_line_ptr
|= (u64
)boot_params
.ext_cmd_line_ptr
<< 32;
285 static void __init
copy_bootdata(char *real_mode_data
)
288 unsigned long cmd_line_ptr
;
291 * If SME is active, this will create decrypted mappings of the
292 * boot data in advance of the copy operations.
294 sme_map_bootdata(real_mode_data
);
296 memcpy(&boot_params
, real_mode_data
, sizeof boot_params
);
297 sanitize_boot_params(&boot_params
);
298 cmd_line_ptr
= get_cmd_line_ptr();
300 command_line
= __va(cmd_line_ptr
);
301 memcpy(boot_command_line
, command_line
, COMMAND_LINE_SIZE
);
305 * The old boot data is no longer needed and won't be reserved,
306 * freeing up that memory for use by the system. If SME is active,
307 * we need to remove the mappings that were created so that the
308 * memory doesn't remain mapped as decrypted.
310 sme_unmap_bootdata(real_mode_data
);
313 asmlinkage __visible
void __init
x86_64_start_kernel(char * real_mode_data
)
316 * Build-time sanity checks on the kernel image and module
317 * area mappings. (these are purely build-time and produce no code)
319 BUILD_BUG_ON(MODULES_VADDR
< __START_KERNEL_map
);
320 BUILD_BUG_ON(MODULES_VADDR
- __START_KERNEL_map
< KERNEL_IMAGE_SIZE
);
321 BUILD_BUG_ON(MODULES_LEN
+ KERNEL_IMAGE_SIZE
> 2*PUD_SIZE
);
322 BUILD_BUG_ON((__START_KERNEL_map
& ~PMD_MASK
) != 0);
323 BUILD_BUG_ON((MODULES_VADDR
& ~PMD_MASK
) != 0);
324 BUILD_BUG_ON(!(MODULES_VADDR
> __START_KERNEL
));
325 BUILD_BUG_ON(!(((MODULES_END
- 1) & PGDIR_MASK
) ==
326 (__START_KERNEL
& PGDIR_MASK
)));
327 BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses
) <= MODULES_END
);
331 /* Kill off the identity-map trampoline */
332 reset_early_page_tables();
336 clear_page(init_top_pgt
);
339 * SME support may update early_pmd_flags to include the memory
340 * encryption mask, so it needs to be called before anything
341 * that may generate a page fault.
347 idt_setup_early_handler();
349 copy_bootdata(__va(real_mode_data
));
352 * Load microcode early on BSP.
356 /* set init_top_pgt kernel high mapping*/
357 init_top_pgt
[511] = early_top_pgt
[511];
359 x86_64_start_reservations(real_mode_data
);
362 void __init
x86_64_start_reservations(char *real_mode_data
)
364 /* version is always not zero if it is copied */
365 if (!boot_params
.hdr
.version
)
366 copy_bootdata(__va(real_mode_data
));
368 x86_early_init_platform_quirks();
370 switch (boot_params
.hdr
.hardware_subarch
) {
371 case X86_SUBARCH_INTEL_MID
:
372 x86_intel_mid_early_setup();