head64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #define DISABLE_BRANCH_PROFILING
  8. /* cpu_feature_enabled() cannot be used this early */
  9. #define USE_EARLY_PGTABLE_L5
  10. #include <linux/init.h>
  11. #include <linux/linkage.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/percpu.h>
  16. #include <linux/start_kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/memblock.h>
  19. #include <linux/mem_encrypt.h>
  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. #include <asm/fixmap.h>
  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. #ifdef CONFIG_X86_5LEVEL
  42. unsigned int __pgtable_l5_enabled __ro_after_init;
  43. unsigned int pgdir_shift __ro_after_init = 39;
  44. EXPORT_SYMBOL(pgdir_shift);
  45. unsigned int ptrs_per_p4d __ro_after_init = 1;
  46. EXPORT_SYMBOL(ptrs_per_p4d);
  47. #endif
  48. #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT
  49. unsigned long page_offset_base __ro_after_init = __PAGE_OFFSET_BASE_L4;
  50. EXPORT_SYMBOL(page_offset_base);
  51. unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4;
  52. EXPORT_SYMBOL(vmalloc_base);
  53. unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4;
  54. EXPORT_SYMBOL(vmemmap_base);
  55. #endif
  56. #define __head __section(.head.text)
  57. static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
  58. {
  59. return ptr - (void *)_text + (void *)physaddr;
  60. }
  61. static unsigned long __head *fixup_long(void *ptr, unsigned long physaddr)
  62. {
  63. return fixup_pointer(ptr, physaddr);
  64. }
  65. #ifdef CONFIG_X86_5LEVEL
  66. static unsigned int __head *fixup_int(void *ptr, unsigned long physaddr)
  67. {
  68. return fixup_pointer(ptr, physaddr);
  69. }
  70. static bool __head check_la57_support(unsigned long physaddr)
  71. {
  72. /*
  73. * 5-level paging is detected and enabled at kernel decomression
  74. * stage. Only check if it has been enabled there.
  75. */
  76. if (!(native_read_cr4() & X86_CR4_LA57))
  77. return false;
  78. *fixup_int(&__pgtable_l5_enabled, physaddr) = 1;
  79. *fixup_int(&pgdir_shift, physaddr) = 48;
  80. *fixup_int(&ptrs_per_p4d, physaddr) = 512;
  81. *fixup_long(&page_offset_base, physaddr) = __PAGE_OFFSET_BASE_L5;
  82. *fixup_long(&vmalloc_base, physaddr) = __VMALLOC_BASE_L5;
  83. *fixup_long(&vmemmap_base, physaddr) = __VMEMMAP_BASE_L5;
  84. return true;
  85. }
  86. #else
  87. static bool __head check_la57_support(unsigned long physaddr)
  88. {
  89. return false;
  90. }
  91. #endif
  92. /* Code in __startup_64() can be relocated during execution, but the compiler
  93. * doesn't have to generate PC-relative relocations when accessing globals from
  94. * that function. Clang actually does not generate them, which leads to
  95. * boot-time crashes. To work around this problem, every global pointer must
  96. * be adjusted using fixup_pointer().
  97. */
  98. unsigned long __head __startup_64(unsigned long physaddr,
  99. struct boot_params *bp)
  100. {
  101. unsigned long vaddr, vaddr_end;
  102. unsigned long load_delta, *p;
  103. unsigned long pgtable_flags;
  104. pgdval_t *pgd;
  105. p4dval_t *p4d;
  106. pudval_t *pud;
  107. pmdval_t *pmd, pmd_entry;
  108. pteval_t *mask_ptr;
  109. bool la57;
  110. int i;
  111. unsigned int *next_pgt_ptr;
  112. la57 = check_la57_support(physaddr);
  113. /* Is the address too large? */
  114. if (physaddr >> MAX_PHYSMEM_BITS)
  115. for (;;);
  116. /*
  117. * Compute the delta between the address I am compiled to run at
  118. * and the address I am actually running at.
  119. */
  120. load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
  121. /* Is the address not 2M aligned? */
  122. if (load_delta & ~PMD_PAGE_MASK)
  123. for (;;);
  124. /* Activate Secure Memory Encryption (SME) if supported and enabled */
  125. sme_enable(bp);
  126. /* Include the SME encryption mask in the fixup value */
  127. load_delta += sme_get_me_mask();
  128. /* Fixup the physical addresses in the page table */
  129. pgd = fixup_pointer(&early_top_pgt, physaddr);
  130. p = pgd + pgd_index(__START_KERNEL_map);
  131. if (la57)
  132. *p = (unsigned long)level4_kernel_pgt;
  133. else
  134. *p = (unsigned long)level3_kernel_pgt;
  135. *p += _PAGE_TABLE_NOENC - __START_KERNEL_map + load_delta;
  136. if (la57) {
  137. p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
  138. p4d[511] += load_delta;
  139. }
  140. pud = fixup_pointer(&level3_kernel_pgt, physaddr);
  141. pud[510] += load_delta;
  142. pud[511] += load_delta;
  143. pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
  144. for (i = FIXMAP_PMD_TOP; i > FIXMAP_PMD_TOP - FIXMAP_PMD_NUM; i--)
  145. pmd[i] += load_delta;
  146. /*
  147. * Set up the identity mapping for the switchover. These
  148. * entries should *NOT* have the global bit set! This also
  149. * creates a bunch of nonsense entries but that is fine --
  150. * it avoids problems around wraparound.
  151. */
  152. next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr);
  153. pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
  154. pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
  155. pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
  156. if (la57) {
  157. p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
  158. i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
  159. pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
  160. pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
  161. i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D;
  162. p4d[i + 0] = (pgdval_t)pud + pgtable_flags;
  163. p4d[i + 1] = (pgdval_t)pud + pgtable_flags;
  164. } else {
  165. i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
  166. pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
  167. pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
  168. }
  169. i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
  170. pud[i + 0] = (pudval_t)pmd + pgtable_flags;
  171. pud[i + 1] = (pudval_t)pmd + pgtable_flags;
  172. pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
  173. /* Filter out unsupported __PAGE_KERNEL_* bits: */
  174. mask_ptr = fixup_pointer(&__supported_pte_mask, physaddr);
  175. pmd_entry &= *mask_ptr;
  176. pmd_entry += sme_get_me_mask();
  177. pmd_entry += physaddr;
  178. for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
  179. int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD;
  180. pmd[idx] = pmd_entry + i * PMD_SIZE;
  181. }
  182. /*
  183. * Fixup the kernel text+data virtual addresses. Note that
  184. * we might write invalid pmds, when the kernel is relocated
  185. * cleanup_highmap() fixes this up along with the mappings
  186. * beyond _end.
  187. */
  188. pmd = fixup_pointer(level2_kernel_pgt, physaddr);
  189. for (i = 0; i < PTRS_PER_PMD; i++) {
  190. if (pmd[i] & _PAGE_PRESENT)
  191. pmd[i] += load_delta;
  192. }
  193. /*
  194. * Fixup phys_base - remove the memory encryption mask to obtain
  195. * the true physical address.
  196. */
  197. *fixup_long(&phys_base, physaddr) += load_delta - sme_get_me_mask();
  198. /* Encrypt the kernel and related (if SME is active) */
  199. sme_encrypt_kernel(bp);
  200. /*
  201. * Clear the memory encryption mask from the .bss..decrypted section.
  202. * The bss section will be memset to zero later in the initialization so
  203. * there is no need to zero it after changing the memory encryption
  204. * attribute.
  205. */
  206. if (mem_encrypt_active()) {
  207. vaddr = (unsigned long)__start_bss_decrypted;
  208. vaddr_end = (unsigned long)__end_bss_decrypted;
  209. for (; vaddr < vaddr_end; vaddr += PMD_SIZE) {
  210. i = pmd_index(vaddr);
  211. pmd[i] -= sme_get_me_mask();
  212. }
  213. }
  214. /*
  215. * Return the SME encryption mask (if SME is active) to be used as a
  216. * modifier for the initial pgdir entry programmed into CR3.
  217. */
  218. return sme_get_me_mask();
  219. }
  220. unsigned long __startup_secondary_64(void)
  221. {
  222. /*
  223. * Return the SME encryption mask (if SME is active) to be used as a
  224. * modifier for the initial pgdir entry programmed into CR3.
  225. */
  226. return sme_get_me_mask();
  227. }
  228. /* Wipe all early page tables except for the kernel symbol map */
  229. static void __init reset_early_page_tables(void)
  230. {
  231. memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
  232. next_early_pgt = 0;
  233. write_cr3(__sme_pa_nodebug(early_top_pgt));
  234. }
  235. /* Create a new PMD entry */
  236. int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
  237. {
  238. unsigned long physaddr = address - __PAGE_OFFSET;
  239. pgdval_t pgd, *pgd_p;
  240. p4dval_t p4d, *p4d_p;
  241. pudval_t pud, *pud_p;
  242. pmdval_t *pmd_p;
  243. /* Invalid address or early pgt is done ? */
  244. if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
  245. return -1;
  246. again:
  247. pgd_p = &early_top_pgt[pgd_index(address)].pgd;
  248. pgd = *pgd_p;
  249. /*
  250. * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
  251. * critical -- __PAGE_OFFSET would point us back into the dynamic
  252. * range and we might end up looping forever...
  253. */
  254. if (!pgtable_l5_enabled())
  255. p4d_p = pgd_p;
  256. else if (pgd)
  257. p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  258. else {
  259. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  260. reset_early_page_tables();
  261. goto again;
  262. }
  263. p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
  264. memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
  265. *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  266. }
  267. p4d_p += p4d_index(address);
  268. p4d = *p4d_p;
  269. if (p4d)
  270. pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  271. else {
  272. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  273. reset_early_page_tables();
  274. goto again;
  275. }
  276. pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
  277. memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
  278. *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  279. }
  280. pud_p += pud_index(address);
  281. pud = *pud_p;
  282. if (pud)
  283. pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  284. else {
  285. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  286. reset_early_page_tables();
  287. goto again;
  288. }
  289. pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
  290. memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
  291. *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  292. }
  293. pmd_p[pmd_index(address)] = pmd;
  294. return 0;
  295. }
  296. int __init early_make_pgtable(unsigned long address)
  297. {
  298. unsigned long physaddr = address - __PAGE_OFFSET;
  299. pmdval_t pmd;
  300. pmd = (physaddr & PMD_MASK) + early_pmd_flags;
  301. return __early_make_pgtable(address, pmd);
  302. }
  303. /* Don't add a printk in there. printk relies on the PDA which is not initialized
  304. yet. */
  305. static void __init clear_bss(void)
  306. {
  307. memset(__bss_start, 0,
  308. (unsigned long) __bss_stop - (unsigned long) __bss_start);
  309. }
  310. static unsigned long get_cmd_line_ptr(void)
  311. {
  312. unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
  313. cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
  314. return cmd_line_ptr;
  315. }
  316. static void __init copy_bootdata(char *real_mode_data)
  317. {
  318. char * command_line;
  319. unsigned long cmd_line_ptr;
  320. /*
  321. * If SME is active, this will create decrypted mappings of the
  322. * boot data in advance of the copy operations.
  323. */
  324. sme_map_bootdata(real_mode_data);
  325. memcpy(&boot_params, real_mode_data, sizeof(boot_params));
  326. sanitize_boot_params(&boot_params);
  327. cmd_line_ptr = get_cmd_line_ptr();
  328. if (cmd_line_ptr) {
  329. command_line = __va(cmd_line_ptr);
  330. memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  331. }
  332. /*
  333. * The old boot data is no longer needed and won't be reserved,
  334. * freeing up that memory for use by the system. If SME is active,
  335. * we need to remove the mappings that were created so that the
  336. * memory doesn't remain mapped as decrypted.
  337. */
  338. sme_unmap_bootdata(real_mode_data);
  339. }
  340. asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
  341. {
  342. /*
  343. * Build-time sanity checks on the kernel image and module
  344. * area mappings. (these are purely build-time and produce no code)
  345. */
  346. BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
  347. BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
  348. BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
  349. BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
  350. BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
  351. BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
  352. MAYBE_BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
  353. (__START_KERNEL & PGDIR_MASK)));
  354. BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
  355. cr4_init_shadow();
  356. /* Kill off the identity-map trampoline */
  357. reset_early_page_tables();
  358. clear_bss();
  359. clear_page(init_top_pgt);
  360. /*
  361. * SME support may update early_pmd_flags to include the memory
  362. * encryption mask, so it needs to be called before anything
  363. * that may generate a page fault.
  364. */
  365. sme_early_init();
  366. kasan_early_init();
  367. idt_setup_early_handler();
  368. copy_bootdata(__va(real_mode_data));
  369. /*
  370. * Load microcode early on BSP.
  371. */
  372. load_ucode_bsp();
  373. /* set init_top_pgt kernel high mapping*/
  374. init_top_pgt[511] = early_top_pgt[511];
  375. x86_64_start_reservations(real_mode_data);
  376. }
  377. void __init x86_64_start_reservations(char *real_mode_data)
  378. {
  379. /* version is always not zero if it is copied */
  380. if (!boot_params.hdr.version)
  381. copy_bootdata(__va(real_mode_data));
  382. x86_early_init_platform_quirks();
  383. switch (boot_params.hdr.hardware_subarch) {
  384. case X86_SUBARCH_INTEL_MID:
  385. x86_intel_mid_early_setup();
  386. break;
  387. default:
  388. break;
  389. }
  390. start_kernel();
  391. }