cpu_entry_area.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/spinlock.h>
  3. #include <linux/percpu.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/kcore.h>
  6. #include <linux/pgtable.h>
  7. #include <asm/cpu_entry_area.h>
  8. #include <asm/fixmap.h>
  9. #include <asm/desc.h>
  10. #include <asm/kasan.h>
  11. #include <asm/setup.h>
  12. static DEFINE_PER_CPU_PAGE_ALIGNED(struct entry_stack_page, entry_stack_storage);
  13. #ifdef CONFIG_X86_64
  14. static DEFINE_PER_CPU_PAGE_ALIGNED(struct exception_stacks, exception_stacks);
  15. DEFINE_PER_CPU(struct cea_exception_stacks*, cea_exception_stacks);
  16. static DEFINE_PER_CPU_READ_MOSTLY(unsigned long, _cea_offset);
  17. static __always_inline unsigned int cea_offset(unsigned int cpu)
  18. {
  19. return per_cpu(_cea_offset, cpu);
  20. }
  21. static __init void init_cea_offsets(void)
  22. {
  23. unsigned int max_cea;
  24. unsigned int i, j;
  25. if (!kaslr_enabled()) {
  26. for_each_possible_cpu(i)
  27. per_cpu(_cea_offset, i) = i;
  28. return;
  29. }
  30. max_cea = (CPU_ENTRY_AREA_MAP_SIZE - PAGE_SIZE) / CPU_ENTRY_AREA_SIZE;
  31. /* O(sodding terrible) */
  32. for_each_possible_cpu(i) {
  33. unsigned int cea;
  34. again:
  35. cea = get_random_u32_below(max_cea);
  36. for_each_possible_cpu(j) {
  37. if (cea_offset(j) == cea)
  38. goto again;
  39. if (i == j)
  40. break;
  41. }
  42. per_cpu(_cea_offset, i) = cea;
  43. }
  44. }
  45. #else /* !X86_64 */
  46. DECLARE_PER_CPU_PAGE_ALIGNED(struct doublefault_stack, doublefault_stack);
  47. static __always_inline unsigned int cea_offset(unsigned int cpu)
  48. {
  49. return cpu;
  50. }
  51. static inline void init_cea_offsets(void) { }
  52. #endif
  53. /* Is called from entry code, so must be noinstr */
  54. noinstr struct cpu_entry_area *get_cpu_entry_area(int cpu)
  55. {
  56. unsigned long va = CPU_ENTRY_AREA_PER_CPU + cea_offset(cpu) * CPU_ENTRY_AREA_SIZE;
  57. BUILD_BUG_ON(sizeof(struct cpu_entry_area) % PAGE_SIZE != 0);
  58. return (struct cpu_entry_area *) va;
  59. }
  60. EXPORT_SYMBOL(get_cpu_entry_area);
  61. void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags)
  62. {
  63. unsigned long va = (unsigned long) cea_vaddr;
  64. pte_t pte = pfn_pte(pa >> PAGE_SHIFT, flags);
  65. /*
  66. * The cpu_entry_area is shared between the user and kernel
  67. * page tables. All of its ptes can safely be global.
  68. * _PAGE_GLOBAL gets reused to help indicate PROT_NONE for
  69. * non-present PTEs, so be careful not to set it in that
  70. * case to avoid confusion.
  71. */
  72. if (boot_cpu_has(X86_FEATURE_PGE) &&
  73. (pgprot_val(flags) & _PAGE_PRESENT))
  74. pte = pte_set_flags(pte, _PAGE_GLOBAL);
  75. set_pte_vaddr(va, pte);
  76. }
  77. static void __init
  78. cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)
  79. {
  80. for ( ; pages; pages--, cea_vaddr+= PAGE_SIZE, ptr += PAGE_SIZE)
  81. cea_set_pte(cea_vaddr, per_cpu_ptr_to_phys(ptr), prot);
  82. }
  83. static void __init percpu_setup_debug_store(unsigned int cpu)
  84. {
  85. #ifdef CONFIG_CPU_SUP_INTEL
  86. unsigned int npages;
  87. void *cea;
  88. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  89. return;
  90. cea = &get_cpu_entry_area(cpu)->cpu_debug_store;
  91. npages = sizeof(struct debug_store) / PAGE_SIZE;
  92. BUILD_BUG_ON(sizeof(struct debug_store) % PAGE_SIZE != 0);
  93. cea_map_percpu_pages(cea, &per_cpu(cpu_debug_store, cpu), npages,
  94. PAGE_KERNEL);
  95. cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers;
  96. /*
  97. * Force the population of PMDs for not yet allocated per cpu
  98. * memory like debug store buffers.
  99. */
  100. npages = sizeof(struct debug_store_buffers) / PAGE_SIZE;
  101. for (; npages; npages--, cea += PAGE_SIZE)
  102. cea_set_pte(cea, 0, PAGE_NONE);
  103. #endif
  104. }
  105. #ifdef CONFIG_X86_64
  106. #define cea_map_stack(name) do { \
  107. npages = sizeof(estacks->name## _stack) / PAGE_SIZE; \
  108. cea_map_percpu_pages(cea->estacks.name## _stack, \
  109. estacks->name## _stack, npages, PAGE_KERNEL); \
  110. } while (0)
  111. static void __init percpu_setup_exception_stacks(unsigned int cpu)
  112. {
  113. struct exception_stacks *estacks = per_cpu_ptr(&exception_stacks, cpu);
  114. struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
  115. unsigned int npages;
  116. BUILD_BUG_ON(sizeof(exception_stacks) % PAGE_SIZE != 0);
  117. per_cpu(cea_exception_stacks, cpu) = &cea->estacks;
  118. /*
  119. * The exceptions stack mappings in the per cpu area are protected
  120. * by guard pages so each stack must be mapped separately. DB2 is
  121. * not mapped; it just exists to catch triple nesting of #DB.
  122. */
  123. cea_map_stack(DF);
  124. cea_map_stack(NMI);
  125. cea_map_stack(DB);
  126. cea_map_stack(MCE);
  127. if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
  128. if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) {
  129. cea_map_stack(VC);
  130. cea_map_stack(VC2);
  131. }
  132. }
  133. }
  134. #else
  135. static void __init percpu_setup_exception_stacks(unsigned int cpu)
  136. {
  137. struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
  138. cea_map_percpu_pages(&cea->doublefault_stack,
  139. &per_cpu(doublefault_stack, cpu), 1, PAGE_KERNEL);
  140. }
  141. #endif
  142. /* Setup the fixmap mappings only once per-processor */
  143. static void __init setup_cpu_entry_area(unsigned int cpu)
  144. {
  145. struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
  146. #ifdef CONFIG_X86_64
  147. /* On 64-bit systems, we use a read-only fixmap GDT and TSS. */
  148. pgprot_t gdt_prot = PAGE_KERNEL_RO;
  149. pgprot_t tss_prot = PAGE_KERNEL_RO;
  150. #else
  151. /*
  152. * On 32-bit systems, the GDT cannot be read-only because
  153. * our double fault handler uses a task gate, and entering through
  154. * a task gate needs to change an available TSS to busy. If the
  155. * GDT is read-only, that will triple fault. The TSS cannot be
  156. * read-only because the CPU writes to it on task switches.
  157. */
  158. pgprot_t gdt_prot = PAGE_KERNEL;
  159. pgprot_t tss_prot = PAGE_KERNEL;
  160. #endif
  161. kasan_populate_shadow_for_vaddr(cea, CPU_ENTRY_AREA_SIZE,
  162. early_cpu_to_node(cpu));
  163. cea_set_pte(&cea->gdt, get_cpu_gdt_paddr(cpu), gdt_prot);
  164. cea_map_percpu_pages(&cea->entry_stack_page,
  165. per_cpu_ptr(&entry_stack_storage, cpu), 1,
  166. PAGE_KERNEL);
  167. /*
  168. * The Intel SDM says (Volume 3, 7.2.1):
  169. *
  170. * Avoid placing a page boundary in the part of the TSS that the
  171. * processor reads during a task switch (the first 104 bytes). The
  172. * processor may not correctly perform address translations if a
  173. * boundary occurs in this area. During a task switch, the processor
  174. * reads and writes into the first 104 bytes of each TSS (using
  175. * contiguous physical addresses beginning with the physical address
  176. * of the first byte of the TSS). So, after TSS access begins, if
  177. * part of the 104 bytes is not physically contiguous, the processor
  178. * will access incorrect information without generating a page-fault
  179. * exception.
  180. *
  181. * There are also a lot of errata involving the TSS spanning a page
  182. * boundary. Assert that we're not doing that.
  183. */
  184. BUILD_BUG_ON((offsetof(struct tss_struct, x86_tss) ^
  185. offsetofend(struct tss_struct, x86_tss)) & PAGE_MASK);
  186. BUILD_BUG_ON(sizeof(struct tss_struct) % PAGE_SIZE != 0);
  187. /*
  188. * VMX changes the host TR limit to 0x67 after a VM exit. This is
  189. * okay, since 0x67 covers the size of struct x86_hw_tss. Make sure
  190. * that this is correct.
  191. */
  192. BUILD_BUG_ON(offsetof(struct tss_struct, x86_tss) != 0);
  193. BUILD_BUG_ON(sizeof(struct x86_hw_tss) != 0x68);
  194. cea_map_percpu_pages(&cea->tss, &per_cpu(cpu_tss_rw, cpu),
  195. sizeof(struct tss_struct) / PAGE_SIZE, tss_prot);
  196. #ifdef CONFIG_X86_32
  197. per_cpu(cpu_entry_area, cpu) = cea;
  198. #endif
  199. percpu_setup_exception_stacks(cpu);
  200. percpu_setup_debug_store(cpu);
  201. }
  202. static __init void setup_cpu_entry_area_ptes(void)
  203. {
  204. #ifdef CONFIG_X86_32
  205. unsigned long start, end;
  206. /* The +1 is for the readonly IDT: */
  207. BUILD_BUG_ON((CPU_ENTRY_AREA_PAGES+1)*PAGE_SIZE != CPU_ENTRY_AREA_MAP_SIZE);
  208. BUG_ON(CPU_ENTRY_AREA_BASE & ~PMD_MASK);
  209. start = CPU_ENTRY_AREA_BASE;
  210. end = start + CPU_ENTRY_AREA_MAP_SIZE;
  211. /* Careful here: start + PMD_SIZE might wrap around */
  212. for (; start < end && start >= CPU_ENTRY_AREA_BASE; start += PMD_SIZE)
  213. populate_extra_pte(start);
  214. #endif
  215. }
  216. void __init setup_cpu_entry_areas(void)
  217. {
  218. unsigned int cpu;
  219. init_cea_offsets();
  220. setup_cpu_entry_area_ptes();
  221. for_each_possible_cpu(cpu)
  222. setup_cpu_entry_area(cpu);
  223. /*
  224. * This is the last essential update to swapper_pgdir which needs
  225. * to be synchronized to initial_page_table on 32bit.
  226. */
  227. sync_initial_page_table();
  228. }