]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/x86/kernel/head64.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / arch / x86 / kernel / head64.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
835c34a1 3 * prepare to run common code
1da177e4
LT
4 *
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
1da177e4
LT
6 */
7
be3606ff 8#define DISABLE_BRANCH_PROFILING
1da177e4
LT
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>
eaf76e8b 15#include <linux/start_kernel.h>
8b664aa6 16#include <linux/io.h>
72d7c3b3 17#include <linux/memblock.h>
5868f365 18#include <linux/mem_encrypt.h>
1da177e4
LT
19
20#include <asm/processor.h>
21#include <asm/proto.h>
22#include <asm/smp.h>
1da177e4
LT
23#include <asm/setup.h>
24#include <asm/desc.h>
f6c2e333 25#include <asm/pgtable.h>
cfd243d4 26#include <asm/tlbflush.h>
2bc0414e 27#include <asm/sections.h>
718fc13b 28#include <asm/kdebug.h>
66441bd3 29#include <asm/e820/api.h>
47a3d5da 30#include <asm/bios_ebda.h>
5dcd14ec 31#include <asm/bootparam_utils.h>
feddc9de 32#include <asm/microcode.h>
ef7f0d6a 33#include <asm/kasan.h>
1da177e4 34
8170e6be
PA
35/*
36 * Manage page tables very early on.
37 */
8170e6be 38extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
c88d7150 39static unsigned int __initdata next_early_pgt;
5e427ec2 40pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
8170e6be 41
26179670
KS
42#define __head __section(.head.text)
43
44static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
c88d7150
KS
45{
46 return ptr - (void *)_text + (void *)physaddr;
47}
48
aca20d54
TL
49unsigned long __head __startup_64(unsigned long physaddr,
50 struct boot_params *bp)
c88d7150
KS
51{
52 unsigned long load_delta, *p;
5868f365 53 unsigned long pgtable_flags;
c88d7150 54 pgdval_t *pgd;
032370b9 55 p4dval_t *p4d;
c88d7150
KS
56 pudval_t *pud;
57 pmdval_t *pmd, pmd_entry;
58 int i;
187e91fe 59 unsigned int *next_pgt_ptr;
c88d7150
KS
60
61 /* Is the address too large? */
62 if (physaddr >> MAX_PHYSMEM_BITS)
63 for (;;);
64
65 /*
66 * Compute the delta between the address I am compiled to run at
67 * and the address I am actually running at.
68 */
69 load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
70
71 /* Is the address not 2M aligned? */
72 if (load_delta & ~PMD_PAGE_MASK)
73 for (;;);
74
5868f365 75 /* Activate Secure Memory Encryption (SME) if supported and enabled */
aca20d54 76 sme_enable(bp);
5868f365
TL
77
78 /* Include the SME encryption mask in the fixup value */
79 load_delta += sme_get_me_mask();
80
c88d7150
KS
81 /* Fixup the physical addresses in the page table */
82
65ade2f8 83 pgd = fixup_pointer(&early_top_pgt, physaddr);
c88d7150
KS
84 pgd[pgd_index(__START_KERNEL_map)] += load_delta;
85
032370b9
KS
86 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
87 p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
88 p4d[511] += load_delta;
89 }
90
c88d7150
KS
91 pud = fixup_pointer(&level3_kernel_pgt, physaddr);
92 pud[510] += load_delta;
93 pud[511] += load_delta;
94
95 pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
96 pmd[506] += load_delta;
97
98 /*
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.
103 */
104
187e91fe
AP
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);
c88d7150 108
21729f81 109 pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
c88d7150 110
032370b9
KS
111 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
112 p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
113
114 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
5868f365
TL
115 pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
116 pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
032370b9
KS
117
118 i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D;
5868f365
TL
119 p4d[i + 0] = (pgdval_t)pud + pgtable_flags;
120 p4d[i + 1] = (pgdval_t)pud + pgtable_flags;
032370b9
KS
121 } else {
122 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
5868f365
TL
123 pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
124 pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
032370b9 125 }
c88d7150
KS
126
127 i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
5868f365
TL
128 pud[i + 0] = (pudval_t)pmd + pgtable_flags;
129 pud[i + 1] = (pudval_t)pmd + pgtable_flags;
c88d7150
KS
130
131 pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
5868f365 132 pmd_entry += sme_get_me_mask();
c88d7150
KS
133 pmd_entry += physaddr;
134
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;
138 }
139
140 /*
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
144 * beyond _end.
145 */
146
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;
151 }
152
5868f365
TL
153 /*
154 * Fixup phys_base - remove the memory encryption mask to obtain
155 * the true physical address.
156 */
c88d7150 157 p = fixup_pointer(&phys_base, physaddr);
5868f365
TL
158 *p += load_delta - sme_get_me_mask();
159
160 /* Encrypt the kernel (if SME is active) */
161 sme_encrypt_kernel();
162
163 /*
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.
166 */
167 return sme_get_me_mask();
168}
169
170unsigned long __startup_secondary_64(void)
171{
172 /*
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.
175 */
176 return sme_get_me_mask();
c88d7150
KS
177}
178
8170e6be
PA
179/* Wipe all early page tables except for the kernel symbol map */
180static void __init reset_early_page_tables(void)
cfd243d4 181{
65ade2f8 182 memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
8170e6be 183 next_early_pgt = 0;
21729f81 184 write_cr3(__sme_pa_nodebug(early_top_pgt));
8170e6be
PA
185}
186
187/* Create a new PMD entry */
b9d05200 188int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
8170e6be
PA
189{
190 unsigned long physaddr = address - __PAGE_OFFSET;
8170e6be 191 pgdval_t pgd, *pgd_p;
032370b9 192 p4dval_t p4d, *p4d_p;
6b9c75ac 193 pudval_t pud, *pud_p;
b9d05200 194 pmdval_t *pmd_p;
8170e6be
PA
195
196 /* Invalid address or early pgt is done ? */
65ade2f8 197 if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
8170e6be
PA
198 return -1;
199
6b9c75ac 200again:
65ade2f8 201 pgd_p = &early_top_pgt[pgd_index(address)].pgd;
8170e6be
PA
202 pgd = *pgd_p;
203
204 /*
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...
208 */
032370b9
KS
209 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
210 p4d_p = pgd_p;
211 else if (pgd)
212 p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
213 else {
214 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
215 reset_early_page_tables();
216 goto again;
217 }
218
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;
222 }
223 p4d_p += p4d_index(address);
224 p4d = *p4d_p;
225
226 if (p4d)
227 pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
6b9c75ac
YL
228 else {
229 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
8170e6be 230 reset_early_page_tables();
6b9c75ac
YL
231 goto again;
232 }
8170e6be
PA
233
234 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
a91bbe01 235 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
032370b9 236 *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
8170e6be 237 }
6b9c75ac
YL
238 pud_p += pud_index(address);
239 pud = *pud_p;
8170e6be 240
6b9c75ac
YL
241 if (pud)
242 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
243 else {
244 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
245 reset_early_page_tables();
246 goto again;
247 }
248
249 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
a91bbe01 250 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
6b9c75ac
YL
251 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
252 }
6b9c75ac 253 pmd_p[pmd_index(address)] = pmd;
8170e6be
PA
254
255 return 0;
cfd243d4
VG
256}
257
b9d05200
TL
258int __init early_make_pgtable(unsigned long address)
259{
260 unsigned long physaddr = address - __PAGE_OFFSET;
261 pmdval_t pmd;
262
263 pmd = (physaddr & PMD_MASK) + early_pmd_flags;
264
265 return __early_make_pgtable(address, pmd);
266}
267
1da177e4
LT
268/* Don't add a printk in there. printk relies on the PDA which is not initialized
269 yet. */
270static void __init clear_bss(void)
271{
1da177e4 272 memset(__bss_start, 0,
2bc0414e 273 (unsigned long) __bss_stop - (unsigned long) __bss_start);
1da177e4
LT
274}
275
f1da834c
YL
276static unsigned long get_cmd_line_ptr(void)
277{
278 unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
279
ee92d815
YL
280 cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
281
f1da834c
YL
282 return cmd_line_ptr;
283}
284
1da177e4
LT
285static void __init copy_bootdata(char *real_mode_data)
286{
1da177e4 287 char * command_line;
f1da834c 288 unsigned long cmd_line_ptr;
1da177e4 289
b9d05200
TL
290 /*
291 * If SME is active, this will create decrypted mappings of the
292 * boot data in advance of the copy operations.
293 */
294 sme_map_bootdata(real_mode_data);
295
30c82645 296 memcpy(&boot_params, real_mode_data, sizeof boot_params);
5dcd14ec 297 sanitize_boot_params(&boot_params);
f1da834c
YL
298 cmd_line_ptr = get_cmd_line_ptr();
299 if (cmd_line_ptr) {
300 command_line = __va(cmd_line_ptr);
30c82645 301 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
1da177e4 302 }
b9d05200
TL
303
304 /*
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.
309 */
310 sme_unmap_bootdata(real_mode_data);
1da177e4
LT
311}
312
2605fc21 313asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
1da177e4 314{
b4e0409a
IM
315 /*
316 * Build-time sanity checks on the kernel image and module
317 * area mappings. (these are purely build-time and produce no code)
318 */
8e3c2a8c
BP
319 BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
320 BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
b4e0409a 321 BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
8e3c2a8c 322 BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
b4e0409a
IM
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)));
66d4bdf2 327 BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
b4e0409a 328
1e02ce4c
AL
329 cr4_init_shadow();
330
8170e6be
PA
331 /* Kill off the identity-map trampoline */
332 reset_early_page_tables();
333
3df0af0e
YL
334 clear_bss();
335
65ade2f8 336 clear_page(init_top_pgt);
d0f77d4d 337
21729f81
TL
338 /*
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.
342 */
343 sme_early_init();
344
5d5aa3cf
AP
345 kasan_early_init();
346
588787fd 347 idt_setup_early_handler();
f6c2e333 348
fa2bbce9
YL
349 copy_bootdata(__va(real_mode_data));
350
feddc9de
FY
351 /*
352 * Load microcode early on BSP.
353 */
354 load_ucode_bsp();
355
65ade2f8
KS
356 /* set init_top_pgt kernel high mapping*/
357 init_top_pgt[511] = early_top_pgt[511];
8170e6be 358
f97013fd
JF
359 x86_64_start_reservations(real_mode_data);
360}
361
362void __init x86_64_start_reservations(char *real_mode_data)
363{
fa2bbce9
YL
364 /* version is always not zero if it is copied */
365 if (!boot_params.hdr.version)
366 copy_bootdata(__va(real_mode_data));
9de819fe 367
8d152e7a 368 x86_early_init_platform_quirks();
75175278 369
3fda5bb4
AS
370 switch (boot_params.hdr.hardware_subarch) {
371 case X86_SUBARCH_INTEL_MID:
372 x86_intel_mid_early_setup();
373 break;
374 default:
375 break;
376 }
377
1da177e4
LT
378 start_kernel();
379}