head64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #include <linux/init.h>
  9. #include <linux/linkage.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/percpu.h>
  14. #include <linux/start_kernel.h>
  15. #include <linux/io.h>
  16. #include <linux/memblock.h>
  17. #include <linux/mem_encrypt.h>
  18. #include <asm/processor.h>
  19. #include <asm/proto.h>
  20. #include <asm/smp.h>
  21. #include <asm/setup.h>
  22. #include <asm/desc.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/sections.h>
  26. #include <asm/kdebug.h>
  27. #include <asm/e820/api.h>
  28. #include <asm/bios_ebda.h>
  29. #include <asm/bootparam_utils.h>
  30. #include <asm/microcode.h>
  31. #include <asm/kasan.h>
  32. /*
  33. * Manage page tables very early on.
  34. */
  35. extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
  36. static unsigned int __initdata next_early_pgt;
  37. pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
  38. #define __head __section(.head.text)
  39. static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
  40. {
  41. return ptr - (void *)_text + (void *)physaddr;
  42. }
  43. unsigned long __head __startup_64(unsigned long physaddr,
  44. struct boot_params *bp)
  45. {
  46. unsigned long load_delta, *p;
  47. unsigned long pgtable_flags;
  48. pgdval_t *pgd;
  49. p4dval_t *p4d;
  50. pudval_t *pud;
  51. pmdval_t *pmd, pmd_entry;
  52. int i;
  53. unsigned int *next_pgt_ptr;
  54. /* Is the address too large? */
  55. if (physaddr >> MAX_PHYSMEM_BITS)
  56. for (;;);
  57. /*
  58. * Compute the delta between the address I am compiled to run at
  59. * and the address I am actually running at.
  60. */
  61. load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
  62. /* Is the address not 2M aligned? */
  63. if (load_delta & ~PMD_PAGE_MASK)
  64. for (;;);
  65. /* Activate Secure Memory Encryption (SME) if supported and enabled */
  66. sme_enable(bp);
  67. /* Include the SME encryption mask in the fixup value */
  68. load_delta += sme_get_me_mask();
  69. /* Fixup the physical addresses in the page table */
  70. pgd = fixup_pointer(&early_top_pgt, physaddr);
  71. pgd[pgd_index(__START_KERNEL_map)] += load_delta;
  72. if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
  73. p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
  74. p4d[511] += load_delta;
  75. }
  76. pud = fixup_pointer(&level3_kernel_pgt, physaddr);
  77. pud[510] += load_delta;
  78. pud[511] += load_delta;
  79. pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
  80. pmd[506] += load_delta;
  81. /*
  82. * Set up the identity mapping for the switchover. These
  83. * entries should *NOT* have the global bit set! This also
  84. * creates a bunch of nonsense entries but that is fine --
  85. * it avoids problems around wraparound.
  86. */
  87. next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr);
  88. pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
  89. pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
  90. pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
  91. if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
  92. p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
  93. i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
  94. pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
  95. pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
  96. i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D;
  97. p4d[i + 0] = (pgdval_t)pud + pgtable_flags;
  98. p4d[i + 1] = (pgdval_t)pud + pgtable_flags;
  99. } else {
  100. i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
  101. pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
  102. pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
  103. }
  104. i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
  105. pud[i + 0] = (pudval_t)pmd + pgtable_flags;
  106. pud[i + 1] = (pudval_t)pmd + pgtable_flags;
  107. pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
  108. pmd_entry += sme_get_me_mask();
  109. pmd_entry += physaddr;
  110. for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
  111. int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD;
  112. pmd[idx] = pmd_entry + i * PMD_SIZE;
  113. }
  114. /*
  115. * Fixup the kernel text+data virtual addresses. Note that
  116. * we might write invalid pmds, when the kernel is relocated
  117. * cleanup_highmap() fixes this up along with the mappings
  118. * beyond _end.
  119. */
  120. pmd = fixup_pointer(level2_kernel_pgt, physaddr);
  121. for (i = 0; i < PTRS_PER_PMD; i++) {
  122. if (pmd[i] & _PAGE_PRESENT)
  123. pmd[i] += load_delta;
  124. }
  125. /*
  126. * Fixup phys_base - remove the memory encryption mask to obtain
  127. * the true physical address.
  128. */
  129. p = fixup_pointer(&phys_base, physaddr);
  130. *p += load_delta - sme_get_me_mask();
  131. /* Encrypt the kernel and related (if SME is active) */
  132. sme_encrypt_kernel(bp);
  133. /*
  134. * Return the SME encryption mask (if SME is active) to be used as a
  135. * modifier for the initial pgdir entry programmed into CR3.
  136. */
  137. return sme_get_me_mask();
  138. }
  139. unsigned long __startup_secondary_64(void)
  140. {
  141. /*
  142. * Return the SME encryption mask (if SME is active) to be used as a
  143. * modifier for the initial pgdir entry programmed into CR3.
  144. */
  145. return sme_get_me_mask();
  146. }
  147. /* Wipe all early page tables except for the kernel symbol map */
  148. static void __init reset_early_page_tables(void)
  149. {
  150. memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
  151. next_early_pgt = 0;
  152. write_cr3(__sme_pa_nodebug(early_top_pgt));
  153. }
  154. /* Create a new PMD entry */
  155. int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
  156. {
  157. unsigned long physaddr = address - __PAGE_OFFSET;
  158. pgdval_t pgd, *pgd_p;
  159. p4dval_t p4d, *p4d_p;
  160. pudval_t pud, *pud_p;
  161. pmdval_t *pmd_p;
  162. /* Invalid address or early pgt is done ? */
  163. if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
  164. return -1;
  165. again:
  166. pgd_p = &early_top_pgt[pgd_index(address)].pgd;
  167. pgd = *pgd_p;
  168. /*
  169. * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
  170. * critical -- __PAGE_OFFSET would point us back into the dynamic
  171. * range and we might end up looping forever...
  172. */
  173. if (!IS_ENABLED(CONFIG_X86_5LEVEL))
  174. p4d_p = pgd_p;
  175. else if (pgd)
  176. p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  177. else {
  178. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  179. reset_early_page_tables();
  180. goto again;
  181. }
  182. p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
  183. memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
  184. *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  185. }
  186. p4d_p += p4d_index(address);
  187. p4d = *p4d_p;
  188. if (p4d)
  189. pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  190. else {
  191. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  192. reset_early_page_tables();
  193. goto again;
  194. }
  195. pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
  196. memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
  197. *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  198. }
  199. pud_p += pud_index(address);
  200. pud = *pud_p;
  201. if (pud)
  202. pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
  203. else {
  204. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  205. reset_early_page_tables();
  206. goto again;
  207. }
  208. pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
  209. memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
  210. *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
  211. }
  212. pmd_p[pmd_index(address)] = pmd;
  213. return 0;
  214. }
  215. int __init early_make_pgtable(unsigned long address)
  216. {
  217. unsigned long physaddr = address - __PAGE_OFFSET;
  218. pmdval_t pmd;
  219. pmd = (physaddr & PMD_MASK) + early_pmd_flags;
  220. return __early_make_pgtable(address, pmd);
  221. }
  222. /* Don't add a printk in there. printk relies on the PDA which is not initialized
  223. yet. */
  224. static void __init clear_bss(void)
  225. {
  226. memset(__bss_start, 0,
  227. (unsigned long) __bss_stop - (unsigned long) __bss_start);
  228. }
  229. static unsigned long get_cmd_line_ptr(void)
  230. {
  231. unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
  232. cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
  233. return cmd_line_ptr;
  234. }
  235. static void __init copy_bootdata(char *real_mode_data)
  236. {
  237. char * command_line;
  238. unsigned long cmd_line_ptr;
  239. /*
  240. * If SME is active, this will create decrypted mappings of the
  241. * boot data in advance of the copy operations.
  242. */
  243. sme_map_bootdata(real_mode_data);
  244. memcpy(&boot_params, real_mode_data, sizeof boot_params);
  245. sanitize_boot_params(&boot_params);
  246. cmd_line_ptr = get_cmd_line_ptr();
  247. if (cmd_line_ptr) {
  248. command_line = __va(cmd_line_ptr);
  249. memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  250. }
  251. /*
  252. * The old boot data is no longer needed and won't be reserved,
  253. * freeing up that memory for use by the system. If SME is active,
  254. * we need to remove the mappings that were created so that the
  255. * memory doesn't remain mapped as decrypted.
  256. */
  257. sme_unmap_bootdata(real_mode_data);
  258. }
  259. asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
  260. {
  261. /*
  262. * Build-time sanity checks on the kernel image and module
  263. * area mappings. (these are purely build-time and produce no code)
  264. */
  265. BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
  266. BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
  267. BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
  268. BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
  269. BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
  270. BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
  271. BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
  272. (__START_KERNEL & PGDIR_MASK)));
  273. BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
  274. cr4_init_shadow();
  275. /* Kill off the identity-map trampoline */
  276. reset_early_page_tables();
  277. clear_bss();
  278. clear_page(init_top_pgt);
  279. /*
  280. * SME support may update early_pmd_flags to include the memory
  281. * encryption mask, so it needs to be called before anything
  282. * that may generate a page fault.
  283. */
  284. sme_early_init();
  285. kasan_early_init();
  286. idt_setup_early_handler();
  287. copy_bootdata(__va(real_mode_data));
  288. /*
  289. * Load microcode early on BSP.
  290. */
  291. load_ucode_bsp();
  292. /* set init_top_pgt kernel high mapping*/
  293. init_top_pgt[511] = early_top_pgt[511];
  294. x86_64_start_reservations(real_mode_data);
  295. }
  296. void __init x86_64_start_reservations(char *real_mode_data)
  297. {
  298. /* version is always not zero if it is copied */
  299. if (!boot_params.hdr.version)
  300. copy_bootdata(__va(real_mode_data));
  301. x86_early_init_platform_quirks();
  302. switch (boot_params.hdr.hardware_subarch) {
  303. case X86_SUBARCH_INTEL_MID:
  304. x86_intel_mid_early_setup();
  305. break;
  306. default:
  307. break;
  308. }
  309. start_kernel();
  310. }