xref: /linux/arch/x86/kernel/head64.c (revision cdc8be31cb324a0c52529f192e39a44abcfff513)
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 /* cpu_feature_enabled() cannot be used this early */
9 #define USE_EARLY_PGTABLE_L5
10 
11 #include <linux/init.h>
12 #include <linux/linkage.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/percpu.h>
17 #include <linux/start_kernel.h>
18 #include <linux/io.h>
19 #include <linux/memblock.h>
20 #include <linux/cc_platform.h>
21 #include <linux/pgtable.h>
22 
23 #include <asm/asm.h>
24 #include <asm/page_64.h>
25 #include <asm/processor.h>
26 #include <asm/proto.h>
27 #include <asm/smp.h>
28 #include <asm/setup.h>
29 #include <asm/desc.h>
30 #include <asm/tlbflush.h>
31 #include <asm/sections.h>
32 #include <asm/kdebug.h>
33 #include <asm/e820/api.h>
34 #include <asm/bios_ebda.h>
35 #include <asm/bootparam_utils.h>
36 #include <asm/microcode.h>
37 #include <asm/kasan.h>
38 #include <asm/fixmap.h>
39 #include <asm/realmode.h>
40 #include <asm/extable.h>
41 #include <asm/trapnr.h>
42 #include <asm/sev.h>
43 #include <asm/tdx.h>
44 #include <asm/init.h>
45 
46 /*
47  * Manage page tables very early on.
48  */
49 extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
50 unsigned int __initdata next_early_pgt;
51 SYM_PIC_ALIAS(next_early_pgt);
52 pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
53 
54 #ifdef CONFIG_X86_5LEVEL
55 unsigned int __pgtable_l5_enabled __ro_after_init;
56 unsigned int pgdir_shift __ro_after_init = 39;
57 EXPORT_SYMBOL(pgdir_shift);
58 unsigned int ptrs_per_p4d __ro_after_init = 1;
59 EXPORT_SYMBOL(ptrs_per_p4d);
60 #endif
61 
62 #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT
63 unsigned long page_offset_base __ro_after_init = __PAGE_OFFSET_BASE_L4;
64 EXPORT_SYMBOL(page_offset_base);
65 unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4;
66 EXPORT_SYMBOL(vmalloc_base);
67 unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4;
68 EXPORT_SYMBOL(vmemmap_base);
69 #endif
70 
71 /* Wipe all early page tables except for the kernel symbol map */
72 static void __init reset_early_page_tables(void)
73 {
74 	memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
75 	next_early_pgt = 0;
76 	write_cr3(__sme_pa_nodebug(early_top_pgt));
77 }
78 
79 /* Create a new PMD entry */
80 bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
81 {
82 	unsigned long physaddr = address - __PAGE_OFFSET;
83 	pgdval_t pgd, *pgd_p;
84 	p4dval_t p4d, *p4d_p;
85 	pudval_t pud, *pud_p;
86 	pmdval_t *pmd_p;
87 
88 	/* Invalid address or early pgt is done ?  */
89 	if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
90 		return false;
91 
92 again:
93 	pgd_p = &early_top_pgt[pgd_index(address)].pgd;
94 	pgd = *pgd_p;
95 
96 	/*
97 	 * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
98 	 * critical -- __PAGE_OFFSET would point us back into the dynamic
99 	 * range and we might end up looping forever...
100 	 */
101 	if (!pgtable_l5_enabled())
102 		p4d_p = pgd_p;
103 	else if (pgd)
104 		p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
105 	else {
106 		if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
107 			reset_early_page_tables();
108 			goto again;
109 		}
110 
111 		p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
112 		memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
113 		*pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
114 	}
115 	p4d_p += p4d_index(address);
116 	p4d = *p4d_p;
117 
118 	if (p4d)
119 		pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
120 	else {
121 		if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
122 			reset_early_page_tables();
123 			goto again;
124 		}
125 
126 		pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
127 		memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
128 		*p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
129 	}
130 	pud_p += pud_index(address);
131 	pud = *pud_p;
132 
133 	if (pud)
134 		pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
135 	else {
136 		if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
137 			reset_early_page_tables();
138 			goto again;
139 		}
140 
141 		pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
142 		memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
143 		*pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
144 	}
145 	pmd_p[pmd_index(address)] = pmd;
146 
147 	return true;
148 }
149 
150 static bool __init early_make_pgtable(unsigned long address)
151 {
152 	unsigned long physaddr = address - __PAGE_OFFSET;
153 	pmdval_t pmd;
154 
155 	pmd = (physaddr & PMD_MASK) + early_pmd_flags;
156 
157 	return __early_make_pgtable(address, pmd);
158 }
159 
160 void __init do_early_exception(struct pt_regs *regs, int trapnr)
161 {
162 	if (trapnr == X86_TRAP_PF &&
163 	    early_make_pgtable(native_read_cr2()))
164 		return;
165 
166 	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT) &&
167 	    trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs))
168 		return;
169 
170 	if (trapnr == X86_TRAP_VE && tdx_early_handle_ve(regs))
171 		return;
172 
173 	early_fixup_exception(regs, trapnr);
174 }
175 
176 /* Don't add a printk in there. printk relies on the PDA which is not initialized
177    yet. */
178 void __init clear_bss(void)
179 {
180 	memset(__bss_start, 0,
181 	       (unsigned long) __bss_stop - (unsigned long) __bss_start);
182 	memset(__brk_base, 0,
183 	       (unsigned long) __brk_limit - (unsigned long) __brk_base);
184 }
185 
186 static unsigned long get_cmd_line_ptr(void)
187 {
188 	unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
189 
190 	cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
191 
192 	return cmd_line_ptr;
193 }
194 
195 static void __init copy_bootdata(char *real_mode_data)
196 {
197 	char * command_line;
198 	unsigned long cmd_line_ptr;
199 
200 	/*
201 	 * If SME is active, this will create decrypted mappings of the
202 	 * boot data in advance of the copy operations.
203 	 */
204 	sme_map_bootdata(real_mode_data);
205 
206 	memcpy(&boot_params, real_mode_data, sizeof(boot_params));
207 	sanitize_boot_params(&boot_params);
208 	cmd_line_ptr = get_cmd_line_ptr();
209 	if (cmd_line_ptr) {
210 		command_line = __va(cmd_line_ptr);
211 		memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
212 	}
213 
214 	/*
215 	 * The old boot data is no longer needed and won't be reserved,
216 	 * freeing up that memory for use by the system. If SME is active,
217 	 * we need to remove the mappings that were created so that the
218 	 * memory doesn't remain mapped as decrypted.
219 	 */
220 	sme_unmap_bootdata(real_mode_data);
221 }
222 
223 asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode_data)
224 {
225 	/*
226 	 * Build-time sanity checks on the kernel image and module
227 	 * area mappings. (these are purely build-time and produce no code)
228 	 */
229 	BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
230 	BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
231 	BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
232 	BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
233 	BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
234 	BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
235 	MAYBE_BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
236 				(__START_KERNEL & PGDIR_MASK)));
237 	BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
238 
239 	cr4_init_shadow();
240 
241 	/* Kill off the identity-map trampoline */
242 	reset_early_page_tables();
243 
244 	if (pgtable_l5_enabled()) {
245 		page_offset_base	= __PAGE_OFFSET_BASE_L5;
246 		vmalloc_base		= __VMALLOC_BASE_L5;
247 		vmemmap_base		= __VMEMMAP_BASE_L5;
248 	}
249 
250 	clear_bss();
251 
252 	/*
253 	 * This needs to happen *before* kasan_early_init() because latter maps stuff
254 	 * into that page.
255 	 */
256 	clear_page(init_top_pgt);
257 
258 	/*
259 	 * SME support may update early_pmd_flags to include the memory
260 	 * encryption mask, so it needs to be called before anything
261 	 * that may generate a page fault.
262 	 */
263 	sme_early_init();
264 
265 	kasan_early_init();
266 
267 	/*
268 	 * Flush global TLB entries which could be left over from the trampoline page
269 	 * table.
270 	 *
271 	 * This needs to happen *after* kasan_early_init() as KASAN-enabled .configs
272 	 * instrument native_write_cr4() so KASAN must be initialized for that
273 	 * instrumentation to work.
274 	 */
275 	__native_tlb_flush_global(this_cpu_read(cpu_tlbstate.cr4));
276 
277 	idt_setup_early_handler();
278 
279 	/* Needed before cc_platform_has() can be used for TDX */
280 	tdx_early_init();
281 
282 	copy_bootdata(__va(real_mode_data));
283 
284 	/*
285 	 * Load microcode early on BSP.
286 	 */
287 	load_ucode_bsp();
288 
289 	/* set init_top_pgt kernel high mapping*/
290 	init_top_pgt[511] = early_top_pgt[511];
291 
292 	x86_64_start_reservations(real_mode_data);
293 }
294 
295 void __init __noreturn x86_64_start_reservations(char *real_mode_data)
296 {
297 	/* version is always not zero if it is copied */
298 	if (!boot_params.hdr.version)
299 		copy_bootdata(__va(real_mode_data));
300 
301 	x86_early_init_platform_quirks();
302 
303 	switch (boot_params.hdr.hardware_subarch) {
304 	case X86_SUBARCH_INTEL_MID:
305 		x86_intel_mid_early_setup();
306 		break;
307 	default:
308 		break;
309 	}
310 
311 	start_kernel();
312 }
313 
314 void early_setup_idt(void)
315 {
316 	void *handler = NULL;
317 
318 	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
319 		setup_ghcb();
320 		handler = vc_boot_ghcb;
321 	}
322 
323 	startup_64_load_idt(handler);
324 }
325