init.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/io.h>
  3. #include <linux/slab.h>
  4. #include <linux/memblock.h>
  5. #include <linux/cc_platform.h>
  6. #include <linux/pgtable.h>
  7. #include <asm/set_memory.h>
  8. #include <asm/realmode.h>
  9. #include <asm/tlbflush.h>
  10. #include <asm/crash.h>
  11. #include <asm/sev.h>
  12. struct real_mode_header *real_mode_header;
  13. u32 *trampoline_cr4_features;
  14. /* Hold the pgd entry used on booting additional CPUs */
  15. pgd_t trampoline_pgd_entry;
  16. void load_trampoline_pgtable(void)
  17. {
  18. #ifdef CONFIG_X86_32
  19. load_cr3(initial_page_table);
  20. #else
  21. /*
  22. * This function is called before exiting to real-mode and that will
  23. * fail with CR4.PCIDE still set.
  24. */
  25. if (boot_cpu_has(X86_FEATURE_PCID))
  26. cr4_clear_bits(X86_CR4_PCIDE);
  27. write_cr3(real_mode_header->trampoline_pgd);
  28. #endif
  29. /*
  30. * The CR3 write above will not flush global TLB entries.
  31. * Stale, global entries from previous page tables may still be
  32. * present. Flush those stale entries.
  33. *
  34. * This ensures that memory accessed while running with
  35. * trampoline_pgd is *actually* mapped into trampoline_pgd.
  36. */
  37. __flush_tlb_all();
  38. }
  39. void __init reserve_real_mode(void)
  40. {
  41. phys_addr_t mem;
  42. size_t size = real_mode_size_needed();
  43. if (!size)
  44. return;
  45. WARN_ON(slab_is_available());
  46. /* Has to be under 1M so we can execute real-mode AP code. */
  47. mem = memblock_phys_alloc_range(size, PAGE_SIZE, 0, 1<<20);
  48. if (!mem)
  49. pr_info("No sub-1M memory is available for the trampoline\n");
  50. else
  51. set_real_mode_mem(mem);
  52. /*
  53. * Unconditionally reserve the entire fisrt 1M, see comment in
  54. * setup_arch().
  55. */
  56. memblock_reserve(0, SZ_1M);
  57. }
  58. static void __init sme_sev_setup_real_mode(struct trampoline_header *th)
  59. {
  60. #ifdef CONFIG_AMD_MEM_ENCRYPT
  61. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  62. th->flags |= TH_FLAGS_SME_ACTIVE;
  63. if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) {
  64. /*
  65. * Skip the call to verify_cpu() in secondary_startup_64 as it
  66. * will cause #VC exceptions when the AP can't handle them yet.
  67. */
  68. th->start = (u64) secondary_startup_64_no_verify;
  69. if (sev_es_setup_ap_jump_table(real_mode_header))
  70. panic("Failed to get/update SEV-ES AP Jump Table");
  71. }
  72. #endif
  73. }
  74. static void __init setup_real_mode(void)
  75. {
  76. u16 real_mode_seg;
  77. const u32 *rel;
  78. u32 count;
  79. unsigned char *base;
  80. unsigned long phys_base;
  81. struct trampoline_header *trampoline_header;
  82. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  83. #ifdef CONFIG_X86_64
  84. u64 *trampoline_pgd;
  85. u64 efer;
  86. int i;
  87. #endif
  88. base = (unsigned char *)real_mode_header;
  89. /*
  90. * If SME is active, the trampoline area will need to be in
  91. * decrypted memory in order to bring up other processors
  92. * successfully. This is not needed for SEV.
  93. */
  94. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  95. set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
  96. memcpy(base, real_mode_blob, size);
  97. phys_base = __pa(base);
  98. real_mode_seg = phys_base >> 4;
  99. rel = (u32 *) real_mode_relocs;
  100. /* 16-bit segment relocations. */
  101. count = *rel++;
  102. while (count--) {
  103. u16 *seg = (u16 *) (base + *rel++);
  104. *seg = real_mode_seg;
  105. }
  106. /* 32-bit linear relocations. */
  107. count = *rel++;
  108. while (count--) {
  109. u32 *ptr = (u32 *) (base + *rel++);
  110. *ptr += phys_base;
  111. }
  112. /* Must be performed *after* relocation. */
  113. trampoline_header = (struct trampoline_header *)
  114. __va(real_mode_header->trampoline_header);
  115. #ifdef CONFIG_X86_32
  116. trampoline_header->start = __pa_symbol(startup_32_smp);
  117. trampoline_header->gdt_limit = __BOOT_DS + 7;
  118. trampoline_header->gdt_base = __pa_symbol(boot_gdt);
  119. #else
  120. /*
  121. * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
  122. * so we need to mask it out.
  123. */
  124. rdmsrl(MSR_EFER, efer);
  125. trampoline_header->efer = efer & ~EFER_LMA;
  126. trampoline_header->start = (u64) secondary_startup_64;
  127. trampoline_cr4_features = &trampoline_header->cr4;
  128. *trampoline_cr4_features = mmu_cr4_features;
  129. trampoline_header->flags = 0;
  130. trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
  131. /* Map the real mode stub as virtual == physical */
  132. trampoline_pgd[0] = trampoline_pgd_entry.pgd;
  133. /*
  134. * Include the entirety of the kernel mapping into the trampoline
  135. * PGD. This way, all mappings present in the normal kernel page
  136. * tables are usable while running on trampoline_pgd.
  137. */
  138. for (i = pgd_index(__PAGE_OFFSET); i < PTRS_PER_PGD; i++)
  139. trampoline_pgd[i] = init_top_pgt[i].pgd;
  140. #endif
  141. sme_sev_setup_real_mode(trampoline_header);
  142. }
  143. /*
  144. * reserve_real_mode() gets called very early, to guarantee the
  145. * availability of low memory. This is before the proper kernel page
  146. * tables are set up, so we cannot set page permissions in that
  147. * function. Also trampoline code will be executed by APs so we
  148. * need to mark it executable at do_pre_smp_initcalls() at least,
  149. * thus run it as a early_initcall().
  150. */
  151. static void __init set_real_mode_permissions(void)
  152. {
  153. unsigned char *base = (unsigned char *) real_mode_header;
  154. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  155. size_t ro_size =
  156. PAGE_ALIGN(real_mode_header->ro_end) -
  157. __pa(base);
  158. size_t text_size =
  159. PAGE_ALIGN(real_mode_header->ro_end) -
  160. real_mode_header->text_start;
  161. unsigned long text_start =
  162. (unsigned long) __va(real_mode_header->text_start);
  163. set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
  164. set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
  165. set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
  166. }
  167. void __init init_real_mode(void)
  168. {
  169. if (!real_mode_header)
  170. panic("Real mode trampoline was not allocated");
  171. setup_real_mode();
  172. set_real_mode_permissions();
  173. }
  174. static int __init do_init_real_mode(void)
  175. {
  176. x86_platform.realmode_init();
  177. return 0;
  178. }
  179. early_initcall(do_init_real_mode);