init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/mem_encrypt.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-es.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_find_in_range(0, 1<<20, size, PAGE_SIZE);
  48. if (!mem) {
  49. pr_info("No sub-1M memory is available for the trampoline\n");
  50. return;
  51. }
  52. memblock_reserve(mem, size);
  53. set_real_mode_mem(mem);
  54. crash_reserve_low_1M();
  55. }
  56. static void sme_sev_setup_real_mode(struct trampoline_header *th)
  57. {
  58. #ifdef CONFIG_AMD_MEM_ENCRYPT
  59. if (sme_active())
  60. th->flags |= TH_FLAGS_SME_ACTIVE;
  61. if (sev_es_active()) {
  62. /*
  63. * Skip the call to verify_cpu() in secondary_startup_64 as it
  64. * will cause #VC exceptions when the AP can't handle them yet.
  65. */
  66. th->start = (u64) secondary_startup_64_no_verify;
  67. if (sev_es_setup_ap_jump_table(real_mode_header))
  68. panic("Failed to get/update SEV-ES AP Jump Table");
  69. }
  70. #endif
  71. }
  72. static void __init setup_real_mode(void)
  73. {
  74. u16 real_mode_seg;
  75. const u32 *rel;
  76. u32 count;
  77. unsigned char *base;
  78. unsigned long phys_base;
  79. struct trampoline_header *trampoline_header;
  80. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  81. #ifdef CONFIG_X86_64
  82. u64 *trampoline_pgd;
  83. u64 efer;
  84. int i;
  85. #endif
  86. base = (unsigned char *)real_mode_header;
  87. /*
  88. * If SME is active, the trampoline area will need to be in
  89. * decrypted memory in order to bring up other processors
  90. * successfully. This is not needed for SEV.
  91. */
  92. if (sme_active())
  93. set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
  94. memcpy(base, real_mode_blob, size);
  95. phys_base = __pa(base);
  96. real_mode_seg = phys_base >> 4;
  97. rel = (u32 *) real_mode_relocs;
  98. /* 16-bit segment relocations. */
  99. count = *rel++;
  100. while (count--) {
  101. u16 *seg = (u16 *) (base + *rel++);
  102. *seg = real_mode_seg;
  103. }
  104. /* 32-bit linear relocations. */
  105. count = *rel++;
  106. while (count--) {
  107. u32 *ptr = (u32 *) (base + *rel++);
  108. *ptr += phys_base;
  109. }
  110. /* Must be perfomed *after* relocation. */
  111. trampoline_header = (struct trampoline_header *)
  112. __va(real_mode_header->trampoline_header);
  113. #ifdef CONFIG_X86_32
  114. trampoline_header->start = __pa_symbol(startup_32_smp);
  115. trampoline_header->gdt_limit = __BOOT_DS + 7;
  116. trampoline_header->gdt_base = __pa_symbol(boot_gdt);
  117. #else
  118. /*
  119. * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
  120. * so we need to mask it out.
  121. */
  122. rdmsrl(MSR_EFER, efer);
  123. trampoline_header->efer = efer & ~EFER_LMA;
  124. trampoline_header->start = (u64) secondary_startup_64;
  125. trampoline_cr4_features = &trampoline_header->cr4;
  126. *trampoline_cr4_features = mmu_cr4_features;
  127. trampoline_header->flags = 0;
  128. trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
  129. /* Map the real mode stub as virtual == physical */
  130. trampoline_pgd[0] = trampoline_pgd_entry.pgd;
  131. /*
  132. * Include the entirety of the kernel mapping into the trampoline
  133. * PGD. This way, all mappings present in the normal kernel page
  134. * tables are usable while running on trampoline_pgd.
  135. */
  136. for (i = pgd_index(__PAGE_OFFSET); i < PTRS_PER_PGD; i++)
  137. trampoline_pgd[i] = init_top_pgt[i].pgd;
  138. #endif
  139. sme_sev_setup_real_mode(trampoline_header);
  140. }
  141. /*
  142. * reserve_real_mode() gets called very early, to guarantee the
  143. * availability of low memory. This is before the proper kernel page
  144. * tables are set up, so we cannot set page permissions in that
  145. * function. Also trampoline code will be executed by APs so we
  146. * need to mark it executable at do_pre_smp_initcalls() at least,
  147. * thus run it as a early_initcall().
  148. */
  149. static void __init set_real_mode_permissions(void)
  150. {
  151. unsigned char *base = (unsigned char *) real_mode_header;
  152. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  153. size_t ro_size =
  154. PAGE_ALIGN(real_mode_header->ro_end) -
  155. __pa(base);
  156. size_t text_size =
  157. PAGE_ALIGN(real_mode_header->ro_end) -
  158. real_mode_header->text_start;
  159. unsigned long text_start =
  160. (unsigned long) __va(real_mode_header->text_start);
  161. set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
  162. set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
  163. set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
  164. }
  165. static int __init init_real_mode(void)
  166. {
  167. if (!real_mode_header)
  168. panic("Real mode trampoline was not allocated");
  169. setup_real_mode();
  170. set_real_mode_permissions();
  171. return 0;
  172. }
  173. early_initcall(init_real_mode);