mmu_context.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/tlbflush.h>
  10. #include <asm/paravirt.h>
  11. #include <asm/debugreg.h>
  12. extern atomic64_t last_mm_ctx_id;
  13. #ifndef CONFIG_PARAVIRT_XXL
  14. static inline void paravirt_activate_mm(struct mm_struct *prev,
  15. struct mm_struct *next)
  16. {
  17. }
  18. #endif /* !CONFIG_PARAVIRT_XXL */
  19. #ifdef CONFIG_PERF_EVENTS
  20. DECLARE_STATIC_KEY_FALSE(rdpmc_never_available_key);
  21. DECLARE_STATIC_KEY_FALSE(rdpmc_always_available_key);
  22. void cr4_update_pce(void *ignored);
  23. #endif
  24. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  25. /*
  26. * ldt_structs can be allocated, used, and freed, but they are never
  27. * modified while live.
  28. */
  29. struct ldt_struct {
  30. /*
  31. * Xen requires page-aligned LDTs with special permissions. This is
  32. * needed to prevent us from installing evil descriptors such as
  33. * call gates. On native, we could merge the ldt_struct and LDT
  34. * allocations, but it's not worth trying to optimize.
  35. */
  36. struct desc_struct *entries;
  37. unsigned int nr_entries;
  38. /*
  39. * If PTI is in use, then the entries array is not mapped while we're
  40. * in user mode. The whole array will be aliased at the addressed
  41. * given by ldt_slot_va(slot). We use two slots so that we can allocate
  42. * and map, and enable a new LDT without invalidating the mapping
  43. * of an older, still-in-use LDT.
  44. *
  45. * slot will be -1 if this LDT doesn't have an alias mapping.
  46. */
  47. int slot;
  48. };
  49. /*
  50. * Used for LDT copy/destruction.
  51. */
  52. static inline void init_new_context_ldt(struct mm_struct *mm)
  53. {
  54. mm->context.ldt = NULL;
  55. init_rwsem(&mm->context.ldt_usr_sem);
  56. }
  57. int ldt_dup_context(struct mm_struct *oldmm, struct mm_struct *mm);
  58. void destroy_context_ldt(struct mm_struct *mm);
  59. void ldt_arch_exit_mmap(struct mm_struct *mm);
  60. #else /* CONFIG_MODIFY_LDT_SYSCALL */
  61. static inline void init_new_context_ldt(struct mm_struct *mm) { }
  62. static inline int ldt_dup_context(struct mm_struct *oldmm,
  63. struct mm_struct *mm)
  64. {
  65. return 0;
  66. }
  67. static inline void destroy_context_ldt(struct mm_struct *mm) { }
  68. static inline void ldt_arch_exit_mmap(struct mm_struct *mm) { }
  69. #endif
  70. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  71. extern void load_mm_ldt(struct mm_struct *mm);
  72. extern void switch_ldt(struct mm_struct *prev, struct mm_struct *next);
  73. #else
  74. static inline void load_mm_ldt(struct mm_struct *mm)
  75. {
  76. clear_LDT();
  77. }
  78. static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
  79. {
  80. DEBUG_LOCKS_WARN_ON(preemptible());
  81. }
  82. #endif
  83. #define enter_lazy_tlb enter_lazy_tlb
  84. extern void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk);
  85. /*
  86. * Init a new mm. Used on mm copies, like at fork()
  87. * and on mm's that are brand-new, like at execve().
  88. */
  89. #define init_new_context init_new_context
  90. static inline int init_new_context(struct task_struct *tsk,
  91. struct mm_struct *mm)
  92. {
  93. mutex_init(&mm->context.lock);
  94. mm->context.ctx_id = atomic64_inc_return(&last_mm_ctx_id);
  95. atomic64_set(&mm->context.tlb_gen, 0);
  96. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  97. if (cpu_feature_enabled(X86_FEATURE_OSPKE)) {
  98. /* pkey 0 is the default and allocated implicitly */
  99. mm->context.pkey_allocation_map = 0x1;
  100. /* -1 means unallocated or invalid */
  101. mm->context.execute_only_pkey = -1;
  102. }
  103. #endif
  104. init_new_context_ldt(mm);
  105. return 0;
  106. }
  107. #define destroy_context destroy_context
  108. static inline void destroy_context(struct mm_struct *mm)
  109. {
  110. destroy_context_ldt(mm);
  111. }
  112. extern void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  113. struct task_struct *tsk);
  114. extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  115. struct task_struct *tsk);
  116. #define switch_mm_irqs_off switch_mm_irqs_off
  117. #define activate_mm(prev, next) \
  118. do { \
  119. paravirt_activate_mm((prev), (next)); \
  120. switch_mm((prev), (next), NULL); \
  121. } while (0);
  122. #ifdef CONFIG_X86_32
  123. #define deactivate_mm(tsk, mm) \
  124. do { \
  125. loadsegment(gs, 0); \
  126. } while (0)
  127. #else
  128. #define deactivate_mm(tsk, mm) \
  129. do { \
  130. load_gs_index(0); \
  131. loadsegment(fs, 0); \
  132. } while (0)
  133. #endif
  134. static inline void arch_dup_pkeys(struct mm_struct *oldmm,
  135. struct mm_struct *mm)
  136. {
  137. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  138. if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
  139. return;
  140. /* Duplicate the oldmm pkey state in mm: */
  141. mm->context.pkey_allocation_map = oldmm->context.pkey_allocation_map;
  142. mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
  143. #endif
  144. }
  145. static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  146. {
  147. arch_dup_pkeys(oldmm, mm);
  148. paravirt_arch_dup_mmap(oldmm, mm);
  149. return ldt_dup_context(oldmm, mm);
  150. }
  151. static inline void arch_exit_mmap(struct mm_struct *mm)
  152. {
  153. paravirt_arch_exit_mmap(mm);
  154. ldt_arch_exit_mmap(mm);
  155. }
  156. #ifdef CONFIG_X86_64
  157. static inline bool is_64bit_mm(struct mm_struct *mm)
  158. {
  159. return !IS_ENABLED(CONFIG_IA32_EMULATION) ||
  160. !(mm->context.flags & MM_CONTEXT_UPROBE_IA32);
  161. }
  162. #else
  163. static inline bool is_64bit_mm(struct mm_struct *mm)
  164. {
  165. return false;
  166. }
  167. #endif
  168. static inline void arch_unmap(struct mm_struct *mm, unsigned long start,
  169. unsigned long end)
  170. {
  171. }
  172. /*
  173. * We only want to enforce protection keys on the current process
  174. * because we effectively have no access to PKRU for other
  175. * processes or any way to tell *which * PKRU in a threaded
  176. * process we could use.
  177. *
  178. * So do not enforce things if the VMA is not from the current
  179. * mm, or if we are in a kernel thread.
  180. */
  181. static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
  182. bool write, bool execute, bool foreign)
  183. {
  184. /* pkeys never affect instruction fetches */
  185. if (execute)
  186. return true;
  187. /* allow access if the VMA is not one from this process */
  188. if (foreign || vma_is_foreign(vma))
  189. return true;
  190. return __pkru_allows_pkey(vma_pkey(vma), write);
  191. }
  192. unsigned long __get_current_cr3_fast(void);
  193. #include <asm-generic/mmu_context.h>
  194. #endif /* _ASM_X86_MMU_CONTEXT_H */