xref: /openbmc/linux/arch/x86/kernel/head64.c (revision e639c869)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  prepare to run common code
4  *
5  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6  */
7 
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>
16 #include <linux/io.h>
17 #include <linux/memblock.h>
18 #include <linux/mem_encrypt.h>
19 
20 #include <asm/processor.h>
21 #include <asm/proto.h>
22 #include <asm/smp.h>
23 #include <asm/setup.h>
24 #include <asm/desc.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>
34 
35 /*
36  * Manage page tables very early on.
37  */
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);
41 
42 #define __head	__section(.head.text)
43 
44 static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
45 {
46 	return ptr - (void *)_text + (void *)physaddr;
47 }
48 
49 unsigned long __head __startup_64(unsigned long physaddr,
50 				  struct boot_params *bp)
51 {
52 	unsigned long load_delta, *p;
53 	unsigned long pgtable_flags;
54 	pgdval_t *pgd;
55 	p4dval_t *p4d;
56 	pudval_t *pud;
57 	pmdval_t *pmd, pmd_entry;
58 	int i;
59 	unsigned int *next_pgt_ptr;
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 
75 	/* Activate Secure Memory Encryption (SME) if supported and enabled */
76 	sme_enable(bp);
77 
78 	/* Include the SME encryption mask in the fixup value */
79 	load_delta += sme_get_me_mask();
80 
81 	/* Fixup the physical addresses in the page table */
82 
83 	pgd = fixup_pointer(&early_top_pgt, physaddr);
84 	pgd[pgd_index(__START_KERNEL_map)] += load_delta;
85 
86 	if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
87 		p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
88 		p4d[511] += load_delta;
89 	}
90 
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 
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);
108 
109 	pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
110 
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;
115 		pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
116 		pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
117 
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;
121 	} else {
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;
125 	}
126 
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;
130 
131 	pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
132 	pmd_entry += sme_get_me_mask();
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 
153 	/*
154 	 * Fixup phys_base - remove the memory encryption mask to obtain
155 	 * the true physical address.
156 	 */
157 	p = fixup_pointer(&phys_base, physaddr);
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 
170 unsigned 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();
177 }
178 
179 /* Wipe all early page tables except for the kernel symbol map */
180 static void __init reset_early_page_tables(void)
181 {
182 	memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
183 	next_early_pgt = 0;
184 	write_cr3(__sme_pa_nodebug(early_top_pgt));
185 }
186 
187 /* Create a new PMD entry */
188 int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
189 {
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;
194 	pmdval_t *pmd_p;
195 
196 	/* Invalid address or early pgt is done ?  */
197 	if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
198 		return -1;
199 
200 again:
201 	pgd_p = &early_top_pgt[pgd_index(address)].pgd;
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 	 */
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);
228 	else {
229 		if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
230 			reset_early_page_tables();
231 			goto again;
232 		}
233 
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;
237 	}
238 	pud_p += pud_index(address);
239 	pud = *pud_p;
240 
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++];
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;
252 	}
253 	pmd_p[pmd_index(address)] = pmd;
254 
255 	return 0;
256 }
257 
258 int __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 
268 /* Don't add a printk in there. printk relies on the PDA which is not initialized
269    yet. */
270 static void __init clear_bss(void)
271 {
272 	memset(__bss_start, 0,
273 	       (unsigned long) __bss_stop - (unsigned long) __bss_start);
274 }
275 
276 static unsigned long get_cmd_line_ptr(void)
277 {
278 	unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
279 
280 	cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
281 
282 	return cmd_line_ptr;
283 }
284 
285 static void __init copy_bootdata(char *real_mode_data)
286 {
287 	char * command_line;
288 	unsigned long cmd_line_ptr;
289 
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 
296 	memcpy(&boot_params, real_mode_data, sizeof boot_params);
297 	sanitize_boot_params(&boot_params);
298 	cmd_line_ptr = get_cmd_line_ptr();
299 	if (cmd_line_ptr) {
300 		command_line = __va(cmd_line_ptr);
301 		memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
302 	}
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);
311 }
312 
313 asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
314 {
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 	 */
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);
328 
329 	cr4_init_shadow();
330 
331 	/* Kill off the identity-map trampoline */
332 	reset_early_page_tables();
333 
334 	clear_bss();
335 
336 	clear_page(init_top_pgt);
337 
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 
345 	kasan_early_init();
346 
347 	idt_setup_early_handler();
348 
349 	copy_bootdata(__va(real_mode_data));
350 
351 	/*
352 	 * Load microcode early on BSP.
353 	 */
354 	load_ucode_bsp();
355 
356 	/* set init_top_pgt kernel high mapping*/
357 	init_top_pgt[511] = early_top_pgt[511];
358 
359 	x86_64_start_reservations(real_mode_data);
360 }
361 
362 void __init x86_64_start_reservations(char *real_mode_data)
363 {
364 	/* version is always not zero if it is copied */
365 	if (!boot_params.hdr.version)
366 		copy_bootdata(__va(real_mode_data));
367 
368 	x86_early_init_platform_quirks();
369 
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 
378 	start_kernel();
379 }
380