mmu_context.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_MMU_CONTEXT_H
  3. #define _ASM_X86_MMU_CONTEXT_H
  4. #include <asm/desc.h>
  5. #include <linux/atomic.h>
  6. #include <linux/mm_types.h>
  7. #include <linux/pkeys.h>
  8. #include <trace/events/tlb.h>
  9. #include <asm/pgalloc.h>
  10. #include <asm/tlbflush.h>
  11. #include <asm/paravirt.h>
  12. #include <asm/mpx.h>
  13. extern atomic64_t last_mm_ctx_id;
  14. #ifndef CONFIG_PARAVIRT
  15. static inline void paravirt_activate_mm(struct mm_struct *prev,
  16. struct mm_struct *next)
  17. {
  18. }
  19. #endif /* !CONFIG_PARAVIRT */
  20. #ifdef CONFIG_PERF_EVENTS
  21. extern struct static_key rdpmc_always_available;
  22. static inline void load_mm_cr4(struct mm_struct *mm)
  23. {
  24. if (static_key_false(&rdpmc_always_available) ||
  25. atomic_read(&mm->context.perf_rdpmc_allowed))
  26. cr4_set_bits(X86_CR4_PCE);
  27. else
  28. cr4_clear_bits(X86_CR4_PCE);
  29. }
  30. #else
  31. static inline void load_mm_cr4(struct mm_struct *mm) {}
  32. #endif
  33. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  34. /*
  35. * ldt_structs can be allocated, used, and freed, but they are never
  36. * modified while live.
  37. */
  38. struct ldt_struct {
  39. /*
  40. * Xen requires page-aligned LDTs with special permissions. This is
  41. * needed to prevent us from installing evil descriptors such as
  42. * call gates. On native, we could merge the ldt_struct and LDT
  43. * allocations, but it's not worth trying to optimize.
  44. */
  45. struct desc_struct *entries;
  46. unsigned int nr_entries;
  47. };
  48. /*
  49. * Used for LDT copy/destruction.
  50. */
  51. int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm);
  52. void destroy_context_ldt(struct mm_struct *mm);
  53. #else /* CONFIG_MODIFY_LDT_SYSCALL */
  54. static inline int init_new_context_ldt(struct task_struct *tsk,
  55. struct mm_struct *mm)
  56. {
  57. return 0;
  58. }
  59. static inline void destroy_context_ldt(struct mm_struct *mm) {}
  60. #endif
  61. static inline void load_mm_ldt(struct mm_struct *mm)
  62. {
  63. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  64. struct ldt_struct *ldt;
  65. /* READ_ONCE synchronizes with smp_store_release */
  66. ldt = READ_ONCE(mm->context.ldt);
  67. /*
  68. * Any change to mm->context.ldt is followed by an IPI to all
  69. * CPUs with the mm active. The LDT will not be freed until
  70. * after the IPI is handled by all such CPUs. This means that,
  71. * if the ldt_struct changes before we return, the values we see
  72. * will be safe, and the new values will be loaded before we run
  73. * any user code.
  74. *
  75. * NB: don't try to convert this to use RCU without extreme care.
  76. * We would still need IRQs off, because we don't want to change
  77. * the local LDT after an IPI loaded a newer value than the one
  78. * that we can see.
  79. */
  80. if (unlikely(ldt))
  81. set_ldt(ldt->entries, ldt->nr_entries);
  82. else
  83. clear_LDT();
  84. #else
  85. clear_LDT();
  86. #endif
  87. }
  88. static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
  89. {
  90. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  91. /*
  92. * Load the LDT if either the old or new mm had an LDT.
  93. *
  94. * An mm will never go from having an LDT to not having an LDT. Two
  95. * mms never share an LDT, so we don't gain anything by checking to
  96. * see whether the LDT changed. There's also no guarantee that
  97. * prev->context.ldt actually matches LDTR, but, if LDTR is non-NULL,
  98. * then prev->context.ldt will also be non-NULL.
  99. *
  100. * If we really cared, we could optimize the case where prev == next
  101. * and we're exiting lazy mode. Most of the time, if this happens,
  102. * we don't actually need to reload LDTR, but modify_ldt() is mostly
  103. * used by legacy code and emulators where we don't need this level of
  104. * performance.
  105. *
  106. * This uses | instead of || because it generates better code.
  107. */
  108. if (unlikely((unsigned long)prev->context.ldt |
  109. (unsigned long)next->context.ldt))
  110. load_mm_ldt(next);
  111. #endif
  112. DEBUG_LOCKS_WARN_ON(preemptible());
  113. }
  114. void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk);
  115. static inline int init_new_context(struct task_struct *tsk,
  116. struct mm_struct *mm)
  117. {
  118. mm->context.ctx_id = atomic64_inc_return(&last_mm_ctx_id);
  119. atomic64_set(&mm->context.tlb_gen, 0);
  120. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  121. if (cpu_feature_enabled(X86_FEATURE_OSPKE)) {
  122. /* pkey 0 is the default and always allocated */
  123. mm->context.pkey_allocation_map = 0x1;
  124. /* -1 means unallocated or invalid */
  125. mm->context.execute_only_pkey = -1;
  126. }
  127. #endif
  128. return init_new_context_ldt(tsk, mm);
  129. }
  130. static inline void destroy_context(struct mm_struct *mm)
  131. {
  132. destroy_context_ldt(mm);
  133. }
  134. extern void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  135. struct task_struct *tsk);
  136. extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  137. struct task_struct *tsk);
  138. #define switch_mm_irqs_off switch_mm_irqs_off
  139. #define activate_mm(prev, next) \
  140. do { \
  141. paravirt_activate_mm((prev), (next)); \
  142. switch_mm((prev), (next), NULL); \
  143. } while (0);
  144. #ifdef CONFIG_X86_32
  145. #define deactivate_mm(tsk, mm) \
  146. do { \
  147. lazy_load_gs(0); \
  148. } while (0)
  149. #else
  150. #define deactivate_mm(tsk, mm) \
  151. do { \
  152. load_gs_index(0); \
  153. loadsegment(fs, 0); \
  154. } while (0)
  155. #endif
  156. static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  157. {
  158. paravirt_arch_dup_mmap(oldmm, mm);
  159. return 0;
  160. }
  161. static inline void arch_exit_mmap(struct mm_struct *mm)
  162. {
  163. paravirt_arch_exit_mmap(mm);
  164. }
  165. #ifdef CONFIG_X86_64
  166. static inline bool is_64bit_mm(struct mm_struct *mm)
  167. {
  168. return !IS_ENABLED(CONFIG_IA32_EMULATION) ||
  169. !(mm->context.ia32_compat == TIF_IA32);
  170. }
  171. #else
  172. static inline bool is_64bit_mm(struct mm_struct *mm)
  173. {
  174. return false;
  175. }
  176. #endif
  177. static inline void arch_bprm_mm_init(struct mm_struct *mm,
  178. struct vm_area_struct *vma)
  179. {
  180. mpx_mm_init(mm);
  181. }
  182. static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
  183. unsigned long start, unsigned long end)
  184. {
  185. /*
  186. * mpx_notify_unmap() goes and reads a rarely-hot
  187. * cacheline in the mm_struct. That can be expensive
  188. * enough to be seen in profiles.
  189. *
  190. * The mpx_notify_unmap() call and its contents have been
  191. * observed to affect munmap() performance on hardware
  192. * where MPX is not present.
  193. *
  194. * The unlikely() optimizes for the fast case: no MPX
  195. * in the CPU, or no MPX use in the process. Even if
  196. * we get this wrong (in the unlikely event that MPX
  197. * is widely enabled on some system) the overhead of
  198. * MPX itself (reading bounds tables) is expected to
  199. * overwhelm the overhead of getting this unlikely()
  200. * consistently wrong.
  201. */
  202. if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX)))
  203. mpx_notify_unmap(mm, vma, start, end);
  204. }
  205. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  206. static inline int vma_pkey(struct vm_area_struct *vma)
  207. {
  208. unsigned long vma_pkey_mask = VM_PKEY_BIT0 | VM_PKEY_BIT1 |
  209. VM_PKEY_BIT2 | VM_PKEY_BIT3;
  210. return (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT;
  211. }
  212. #else
  213. static inline int vma_pkey(struct vm_area_struct *vma)
  214. {
  215. return 0;
  216. }
  217. #endif
  218. /*
  219. * We only want to enforce protection keys on the current process
  220. * because we effectively have no access to PKRU for other
  221. * processes or any way to tell *which * PKRU in a threaded
  222. * process we could use.
  223. *
  224. * So do not enforce things if the VMA is not from the current
  225. * mm, or if we are in a kernel thread.
  226. */
  227. static inline bool vma_is_foreign(struct vm_area_struct *vma)
  228. {
  229. if (!current->mm)
  230. return true;
  231. /*
  232. * Should PKRU be enforced on the access to this VMA? If
  233. * the VMA is from another process, then PKRU has no
  234. * relevance and should not be enforced.
  235. */
  236. if (current->mm != vma->vm_mm)
  237. return true;
  238. return false;
  239. }
  240. static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
  241. bool write, bool execute, bool foreign)
  242. {
  243. /* pkeys never affect instruction fetches */
  244. if (execute)
  245. return true;
  246. /* allow access if the VMA is not one from this process */
  247. if (foreign || vma_is_foreign(vma))
  248. return true;
  249. return __pkru_allows_pkey(vma_pkey(vma), write);
  250. }
  251. /*
  252. * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID
  253. * bits. This serves two purposes. It prevents a nasty situation in
  254. * which PCID-unaware code saves CR3, loads some other value (with PCID
  255. * == 0), and then restores CR3, thus corrupting the TLB for ASID 0 if
  256. * the saved ASID was nonzero. It also means that any bugs involving
  257. * loading a PCID-enabled CR3 with CR4.PCIDE off will trigger
  258. * deterministically.
  259. */
  260. static inline unsigned long build_cr3(struct mm_struct *mm, u16 asid)
  261. {
  262. if (static_cpu_has(X86_FEATURE_PCID)) {
  263. VM_WARN_ON_ONCE(asid > 4094);
  264. return __sme_pa(mm->pgd) | (asid + 1);
  265. } else {
  266. VM_WARN_ON_ONCE(asid != 0);
  267. return __sme_pa(mm->pgd);
  268. }
  269. }
  270. static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
  271. {
  272. VM_WARN_ON_ONCE(asid > 4094);
  273. return __sme_pa(mm->pgd) | (asid + 1) | CR3_NOFLUSH;
  274. }
  275. /*
  276. * This can be used from process context to figure out what the value of
  277. * CR3 is without needing to do a (slow) __read_cr3().
  278. *
  279. * It's intended to be used for code like KVM that sneakily changes CR3
  280. * and needs to restore it. It needs to be used very carefully.
  281. */
  282. static inline unsigned long __get_current_cr3_fast(void)
  283. {
  284. unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm),
  285. this_cpu_read(cpu_tlbstate.loaded_mm_asid));
  286. /* For now, be very restrictive about when this can be called. */
  287. VM_WARN_ON(in_nmi() || preemptible());
  288. VM_BUG_ON(cr3 != __read_cr3());
  289. return cr3;
  290. }
  291. #endif /* _ASM_X86_MMU_CONTEXT_H */