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 static unsigned int __initdata next_early_pgt; 51 pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX); 52 53 #ifdef CONFIG_X86_5LEVEL 54 unsigned int __pgtable_l5_enabled __ro_after_init; 55 unsigned int pgdir_shift __ro_after_init = 39; 56 EXPORT_SYMBOL(pgdir_shift); 57 unsigned int ptrs_per_p4d __ro_after_init = 1; 58 EXPORT_SYMBOL(ptrs_per_p4d); 59 #endif 60 61 #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT 62 unsigned long page_offset_base __ro_after_init = __PAGE_OFFSET_BASE_L4; 63 EXPORT_SYMBOL(page_offset_base); 64 unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4; 65 EXPORT_SYMBOL(vmalloc_base); 66 unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4; 67 EXPORT_SYMBOL(vmemmap_base); 68 #endif 69 70 static inline bool check_la57_support(void) 71 { 72 if (!IS_ENABLED(CONFIG_X86_5LEVEL)) 73 return false; 74 75 /* 76 * 5-level paging is detected and enabled at kernel decompression 77 * stage. Only check if it has been enabled there. 78 */ 79 if (!(native_read_cr4() & X86_CR4_LA57)) 80 return false; 81 82 RIP_REL_REF(__pgtable_l5_enabled) = 1; 83 RIP_REL_REF(pgdir_shift) = 48; 84 RIP_REL_REF(ptrs_per_p4d) = 512; 85 RIP_REL_REF(page_offset_base) = __PAGE_OFFSET_BASE_L5; 86 RIP_REL_REF(vmalloc_base) = __VMALLOC_BASE_L5; 87 RIP_REL_REF(vmemmap_base) = __VMEMMAP_BASE_L5; 88 89 return true; 90 } 91 92 static unsigned long __head sme_postprocess_startup(struct boot_params *bp, 93 pmdval_t *pmd, 94 unsigned long p2v_offset) 95 { 96 unsigned long paddr, paddr_end; 97 int i; 98 99 /* Encrypt the kernel and related (if SME is active) */ 100 sme_encrypt_kernel(bp); 101 102 /* 103 * Clear the memory encryption mask from the .bss..decrypted section. 104 * The bss section will be memset to zero later in the initialization so 105 * there is no need to zero it after changing the memory encryption 106 * attribute. 107 */ 108 if (sme_get_me_mask()) { 109 paddr = (unsigned long)&RIP_REL_REF(__start_bss_decrypted); 110 paddr_end = (unsigned long)&RIP_REL_REF(__end_bss_decrypted); 111 112 for (; paddr < paddr_end; paddr += PMD_SIZE) { 113 /* 114 * On SNP, transition the page to shared in the RMP table so that 115 * it is consistent with the page table attribute change. 116 * 117 * __start_bss_decrypted has a virtual address in the high range 118 * mapping (kernel .text). PVALIDATE, by way of 119 * early_snp_set_memory_shared(), requires a valid virtual 120 * address but the kernel is currently running off of the identity 121 * mapping so use the PA to get a *currently* valid virtual address. 122 */ 123 early_snp_set_memory_shared(paddr, paddr, PTRS_PER_PMD); 124 125 i = pmd_index(paddr - p2v_offset); 126 pmd[i] -= sme_get_me_mask(); 127 } 128 } 129 130 /* 131 * Return the SME encryption mask (if SME is active) to be used as a 132 * modifier for the initial pgdir entry programmed into CR3. 133 */ 134 return sme_get_me_mask(); 135 } 136 137 /* Code in __startup_64() can be relocated during execution, but the compiler 138 * doesn't have to generate PC-relative relocations when accessing globals from 139 * that function. Clang actually does not generate them, which leads to 140 * boot-time crashes. To work around this problem, every global pointer must 141 * be accessed using RIP_REL_REF(). Kernel virtual addresses can be determined 142 * by subtracting p2v_offset from the RIP-relative address. 143 */ 144 unsigned long __head __startup_64(unsigned long p2v_offset, 145 struct boot_params *bp) 146 { 147 pmd_t (*early_pgts)[PTRS_PER_PMD] = RIP_REL_REF(early_dynamic_pgts); 148 unsigned long physaddr = (unsigned long)&RIP_REL_REF(_text); 149 unsigned long va_text, va_end; 150 unsigned long pgtable_flags; 151 unsigned long load_delta; 152 pgdval_t *pgd; 153 p4dval_t *p4d; 154 pudval_t *pud; 155 pmdval_t *pmd, pmd_entry; 156 bool la57; 157 int i; 158 159 la57 = check_la57_support(); 160 161 /* Is the address too large? */ 162 if (physaddr >> MAX_PHYSMEM_BITS) 163 for (;;); 164 165 /* 166 * Compute the delta between the address I am compiled to run at 167 * and the address I am actually running at. 168 */ 169 load_delta = __START_KERNEL_map + p2v_offset; 170 RIP_REL_REF(phys_base) = load_delta; 171 172 /* Is the address not 2M aligned? */ 173 if (load_delta & ~PMD_MASK) 174 for (;;); 175 176 va_text = physaddr - p2v_offset; 177 va_end = (unsigned long)&RIP_REL_REF(_end) - p2v_offset; 178 179 /* Include the SME encryption mask in the fixup value */ 180 load_delta += sme_get_me_mask(); 181 182 /* Fixup the physical addresses in the page table */ 183 184 pgd = &RIP_REL_REF(early_top_pgt)->pgd; 185 pgd[pgd_index(__START_KERNEL_map)] += load_delta; 186 187 if (IS_ENABLED(CONFIG_X86_5LEVEL) && la57) { 188 p4d = (p4dval_t *)&RIP_REL_REF(level4_kernel_pgt); 189 p4d[MAX_PTRS_PER_P4D - 1] += load_delta; 190 191 pgd[pgd_index(__START_KERNEL_map)] = (pgdval_t)p4d | _PAGE_TABLE; 192 } 193 194 RIP_REL_REF(level3_kernel_pgt)[PTRS_PER_PUD - 2].pud += load_delta; 195 RIP_REL_REF(level3_kernel_pgt)[PTRS_PER_PUD - 1].pud += load_delta; 196 197 for (i = FIXMAP_PMD_TOP; i > FIXMAP_PMD_TOP - FIXMAP_PMD_NUM; i--) 198 RIP_REL_REF(level2_fixmap_pgt)[i].pmd += load_delta; 199 200 /* 201 * Set up the identity mapping for the switchover. These 202 * entries should *NOT* have the global bit set! This also 203 * creates a bunch of nonsense entries but that is fine -- 204 * it avoids problems around wraparound. 205 */ 206 207 pud = &early_pgts[0]->pmd; 208 pmd = &early_pgts[1]->pmd; 209 RIP_REL_REF(next_early_pgt) = 2; 210 211 pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask(); 212 213 if (la57) { 214 p4d = &early_pgts[RIP_REL_REF(next_early_pgt)++]->pmd; 215 216 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD; 217 pgd[i + 0] = (pgdval_t)p4d + pgtable_flags; 218 pgd[i + 1] = (pgdval_t)p4d + pgtable_flags; 219 220 i = physaddr >> P4D_SHIFT; 221 p4d[(i + 0) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags; 222 p4d[(i + 1) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags; 223 } else { 224 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD; 225 pgd[i + 0] = (pgdval_t)pud + pgtable_flags; 226 pgd[i + 1] = (pgdval_t)pud + pgtable_flags; 227 } 228 229 i = physaddr >> PUD_SHIFT; 230 pud[(i + 0) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags; 231 pud[(i + 1) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags; 232 233 pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL; 234 /* Filter out unsupported __PAGE_KERNEL_* bits: */ 235 pmd_entry &= RIP_REL_REF(__supported_pte_mask); 236 pmd_entry += sme_get_me_mask(); 237 pmd_entry += physaddr; 238 239 for (i = 0; i < DIV_ROUND_UP(va_end - va_text, PMD_SIZE); i++) { 240 int idx = i + (physaddr >> PMD_SHIFT); 241 242 pmd[idx % PTRS_PER_PMD] = pmd_entry + i * PMD_SIZE; 243 } 244 245 /* 246 * Fixup the kernel text+data virtual addresses. Note that 247 * we might write invalid pmds, when the kernel is relocated 248 * cleanup_highmap() fixes this up along with the mappings 249 * beyond _end. 250 * 251 * Only the region occupied by the kernel image has so far 252 * been checked against the table of usable memory regions 253 * provided by the firmware, so invalidate pages outside that 254 * region. A page table entry that maps to a reserved area of 255 * memory would allow processor speculation into that area, 256 * and on some hardware (particularly the UV platform) even 257 * speculative access to some reserved areas is caught as an 258 * error, causing the BIOS to halt the system. 259 */ 260 261 pmd = &RIP_REL_REF(level2_kernel_pgt)->pmd; 262 263 /* invalidate pages before the kernel image */ 264 for (i = 0; i < pmd_index(va_text); i++) 265 pmd[i] &= ~_PAGE_PRESENT; 266 267 /* fixup pages that are part of the kernel image */ 268 for (; i <= pmd_index(va_end); i++) 269 if (pmd[i] & _PAGE_PRESENT) 270 pmd[i] += load_delta; 271 272 /* invalidate pages after the kernel image */ 273 for (; i < PTRS_PER_PMD; i++) 274 pmd[i] &= ~_PAGE_PRESENT; 275 276 return sme_postprocess_startup(bp, pmd, p2v_offset); 277 } 278 279 /* Wipe all early page tables except for the kernel symbol map */ 280 static void __init reset_early_page_tables(void) 281 { 282 memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1)); 283 next_early_pgt = 0; 284 write_cr3(__sme_pa_nodebug(early_top_pgt)); 285 } 286 287 /* Create a new PMD entry */ 288 bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd) 289 { 290 unsigned long physaddr = address - __PAGE_OFFSET; 291 pgdval_t pgd, *pgd_p; 292 p4dval_t p4d, *p4d_p; 293 pudval_t pud, *pud_p; 294 pmdval_t *pmd_p; 295 296 /* Invalid address or early pgt is done ? */ 297 if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt)) 298 return false; 299 300 again: 301 pgd_p = &early_top_pgt[pgd_index(address)].pgd; 302 pgd = *pgd_p; 303 304 /* 305 * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is 306 * critical -- __PAGE_OFFSET would point us back into the dynamic 307 * range and we might end up looping forever... 308 */ 309 if (!pgtable_l5_enabled()) 310 p4d_p = pgd_p; 311 else if (pgd) 312 p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); 313 else { 314 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { 315 reset_early_page_tables(); 316 goto again; 317 } 318 319 p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++]; 320 memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D); 321 *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; 322 } 323 p4d_p += p4d_index(address); 324 p4d = *p4d_p; 325 326 if (p4d) 327 pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); 328 else { 329 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { 330 reset_early_page_tables(); 331 goto again; 332 } 333 334 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++]; 335 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD); 336 *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; 337 } 338 pud_p += pud_index(address); 339 pud = *pud_p; 340 341 if (pud) 342 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); 343 else { 344 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { 345 reset_early_page_tables(); 346 goto again; 347 } 348 349 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++]; 350 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD); 351 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; 352 } 353 pmd_p[pmd_index(address)] = pmd; 354 355 return true; 356 } 357 358 static bool __init early_make_pgtable(unsigned long address) 359 { 360 unsigned long physaddr = address - __PAGE_OFFSET; 361 pmdval_t pmd; 362 363 pmd = (physaddr & PMD_MASK) + early_pmd_flags; 364 365 return __early_make_pgtable(address, pmd); 366 } 367 368 void __init do_early_exception(struct pt_regs *regs, int trapnr) 369 { 370 if (trapnr == X86_TRAP_PF && 371 early_make_pgtable(native_read_cr2())) 372 return; 373 374 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT) && 375 trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs)) 376 return; 377 378 if (trapnr == X86_TRAP_VE && tdx_early_handle_ve(regs)) 379 return; 380 381 early_fixup_exception(regs, trapnr); 382 } 383 384 /* Don't add a printk in there. printk relies on the PDA which is not initialized 385 yet. */ 386 void __init clear_bss(void) 387 { 388 memset(__bss_start, 0, 389 (unsigned long) __bss_stop - (unsigned long) __bss_start); 390 memset(__brk_base, 0, 391 (unsigned long) __brk_limit - (unsigned long) __brk_base); 392 } 393 394 static unsigned long get_cmd_line_ptr(void) 395 { 396 unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr; 397 398 cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32; 399 400 return cmd_line_ptr; 401 } 402 403 static void __init copy_bootdata(char *real_mode_data) 404 { 405 char * command_line; 406 unsigned long cmd_line_ptr; 407 408 /* 409 * If SME is active, this will create decrypted mappings of the 410 * boot data in advance of the copy operations. 411 */ 412 sme_map_bootdata(real_mode_data); 413 414 memcpy(&boot_params, real_mode_data, sizeof(boot_params)); 415 sanitize_boot_params(&boot_params); 416 cmd_line_ptr = get_cmd_line_ptr(); 417 if (cmd_line_ptr) { 418 command_line = __va(cmd_line_ptr); 419 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE); 420 } 421 422 /* 423 * The old boot data is no longer needed and won't be reserved, 424 * freeing up that memory for use by the system. If SME is active, 425 * we need to remove the mappings that were created so that the 426 * memory doesn't remain mapped as decrypted. 427 */ 428 sme_unmap_bootdata(real_mode_data); 429 } 430 431 asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode_data) 432 { 433 /* 434 * Build-time sanity checks on the kernel image and module 435 * area mappings. (these are purely build-time and produce no code) 436 */ 437 BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map); 438 BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE); 439 BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE); 440 BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0); 441 BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0); 442 BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL)); 443 MAYBE_BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) == 444 (__START_KERNEL & PGDIR_MASK))); 445 BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END); 446 447 cr4_init_shadow(); 448 449 /* Kill off the identity-map trampoline */ 450 reset_early_page_tables(); 451 452 clear_bss(); 453 454 /* 455 * This needs to happen *before* kasan_early_init() because latter maps stuff 456 * into that page. 457 */ 458 clear_page(init_top_pgt); 459 460 /* 461 * SME support may update early_pmd_flags to include the memory 462 * encryption mask, so it needs to be called before anything 463 * that may generate a page fault. 464 */ 465 sme_early_init(); 466 467 kasan_early_init(); 468 469 /* 470 * Flush global TLB entries which could be left over from the trampoline page 471 * table. 472 * 473 * This needs to happen *after* kasan_early_init() as KASAN-enabled .configs 474 * instrument native_write_cr4() so KASAN must be initialized for that 475 * instrumentation to work. 476 */ 477 __native_tlb_flush_global(this_cpu_read(cpu_tlbstate.cr4)); 478 479 idt_setup_early_handler(); 480 481 /* Needed before cc_platform_has() can be used for TDX */ 482 tdx_early_init(); 483 484 copy_bootdata(__va(real_mode_data)); 485 486 /* 487 * Load microcode early on BSP. 488 */ 489 load_ucode_bsp(); 490 491 /* set init_top_pgt kernel high mapping*/ 492 init_top_pgt[511] = early_top_pgt[511]; 493 494 x86_64_start_reservations(real_mode_data); 495 } 496 497 void __init __noreturn x86_64_start_reservations(char *real_mode_data) 498 { 499 /* version is always not zero if it is copied */ 500 if (!boot_params.hdr.version) 501 copy_bootdata(__va(real_mode_data)); 502 503 x86_early_init_platform_quirks(); 504 505 switch (boot_params.hdr.hardware_subarch) { 506 case X86_SUBARCH_INTEL_MID: 507 x86_intel_mid_early_setup(); 508 break; 509 default: 510 break; 511 } 512 513 start_kernel(); 514 } 515 516 /* 517 * Data structures and code used for IDT setup in head_64.S. The bringup-IDT is 518 * used until the idt_table takes over. On the boot CPU this happens in 519 * x86_64_start_kernel(), on secondary CPUs in start_secondary(). In both cases 520 * this happens in the functions called from head_64.S. 521 * 522 * The idt_table can't be used that early because all the code modifying it is 523 * in idt.c and can be instrumented by tracing or KASAN, which both don't work 524 * during early CPU bringup. Also the idt_table has the runtime vectors 525 * configured which require certain CPU state to be setup already (like TSS), 526 * which also hasn't happened yet in early CPU bringup. 527 */ 528 static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data; 529 530 /* This may run while still in the direct mapping */ 531 static void __head startup_64_load_idt(void *vc_handler) 532 { 533 struct desc_ptr desc = { 534 .address = (unsigned long)&RIP_REL_REF(bringup_idt_table), 535 .size = sizeof(bringup_idt_table) - 1, 536 }; 537 struct idt_data data; 538 gate_desc idt_desc; 539 540 /* @vc_handler is set only for a VMM Communication Exception */ 541 if (vc_handler) { 542 init_idt_data(&data, X86_TRAP_VC, vc_handler); 543 idt_init_desc(&idt_desc, &data); 544 native_write_idt_entry((gate_desc *)desc.address, X86_TRAP_VC, &idt_desc); 545 } 546 547 native_load_idt(&desc); 548 } 549 550 /* This is used when running on kernel addresses */ 551 void early_setup_idt(void) 552 { 553 void *handler = NULL; 554 555 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { 556 setup_ghcb(); 557 handler = vc_boot_ghcb; 558 } 559 560 startup_64_load_idt(handler); 561 } 562 563 /* 564 * Setup boot CPU state needed before kernel switches to virtual addresses. 565 */ 566 void __head startup_64_setup_gdt_idt(void) 567 { 568 struct desc_struct *gdt = (void *)(__force unsigned long)gdt_page.gdt; 569 void *handler = NULL; 570 571 struct desc_ptr startup_gdt_descr = { 572 .address = (unsigned long)&RIP_REL_REF(*gdt), 573 .size = GDT_SIZE - 1, 574 }; 575 576 /* Load GDT */ 577 native_load_gdt(&startup_gdt_descr); 578 579 /* New GDT is live - reload data segment registers */ 580 asm volatile("movl %%eax, %%ds\n" 581 "movl %%eax, %%ss\n" 582 "movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory"); 583 584 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) 585 handler = &RIP_REL_REF(vc_no_ghcb); 586 587 startup_64_load_idt(handler); 588 } 589