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