]>
Commit | Line | Data |
---|---|---|
1965aae3 PA |
1 | #ifndef _ASM_X86_MMU_CONTEXT_H |
2 | #define _ASM_X86_MMU_CONTEXT_H | |
c3c2fee3 JF |
3 | |
4 | #include <asm/desc.h> | |
60063497 | 5 | #include <linux/atomic.h> |
d17d8f9d | 6 | #include <linux/mm_types.h> |
7d06d9c9 | 7 | #include <linux/pkeys.h> |
d17d8f9d DH |
8 | |
9 | #include <trace/events/tlb.h> | |
10 | ||
c3c2fee3 JF |
11 | #include <asm/pgalloc.h> |
12 | #include <asm/tlbflush.h> | |
13 | #include <asm/paravirt.h> | |
fe3d197f | 14 | #include <asm/mpx.h> |
e566a0df AL |
15 | |
16 | extern atomic64_t last_mm_ctx_id; | |
17 | ||
c3c2fee3 | 18 | #ifndef CONFIG_PARAVIRT |
c3c2fee3 JF |
19 | static inline void paravirt_activate_mm(struct mm_struct *prev, |
20 | struct mm_struct *next) | |
21 | { | |
22 | } | |
23 | #endif /* !CONFIG_PARAVIRT */ | |
24 | ||
7911d3f7 | 25 | #ifdef CONFIG_PERF_EVENTS |
a6673429 AL |
26 | extern struct static_key rdpmc_always_available; |
27 | ||
7911d3f7 AL |
28 | static inline void load_mm_cr4(struct mm_struct *mm) |
29 | { | |
a833581e | 30 | if (static_key_false(&rdpmc_always_available) || |
a6673429 | 31 | atomic_read(&mm->context.perf_rdpmc_allowed)) |
7911d3f7 AL |
32 | cr4_set_bits(X86_CR4_PCE); |
33 | else | |
34 | cr4_clear_bits(X86_CR4_PCE); | |
35 | } | |
36 | #else | |
37 | static inline void load_mm_cr4(struct mm_struct *mm) {} | |
38 | #endif | |
39 | ||
a5b9e5a2 | 40 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
37868fe1 AL |
41 | /* |
42 | * ldt_structs can be allocated, used, and freed, but they are never | |
43 | * modified while live. | |
44 | */ | |
45 | struct ldt_struct { | |
46 | /* | |
47 | * Xen requires page-aligned LDTs with special permissions. This is | |
48 | * needed to prevent us from installing evil descriptors such as | |
49 | * call gates. On native, we could merge the ldt_struct and LDT | |
50 | * allocations, but it's not worth trying to optimize. | |
51 | */ | |
c2506438 AL |
52 | struct desc_struct *entries; |
53 | unsigned int nr_entries; | |
54 | ||
55 | /* | |
56 | * If PTI is in use, then the entries array is not mapped while we're | |
57 | * in user mode. The whole array will be aliased at the addressed | |
58 | * given by ldt_slot_va(slot). We use two slots so that we can allocate | |
59 | * and map, and enable a new LDT without invalidating the mapping | |
60 | * of an older, still-in-use LDT. | |
61 | * | |
62 | * slot will be -1 if this LDT doesn't have an alias mapping. | |
63 | */ | |
64 | int slot; | |
37868fe1 AL |
65 | }; |
66 | ||
c2506438 AL |
67 | /* This is a multiple of PAGE_SIZE. */ |
68 | #define LDT_SLOT_STRIDE (LDT_ENTRIES * LDT_ENTRY_SIZE) | |
69 | ||
70 | static inline void *ldt_slot_va(int slot) | |
71 | { | |
72 | #ifdef CONFIG_X86_64 | |
73 | return (void *)(LDT_BASE_ADDR + LDT_SLOT_STRIDE * slot); | |
74 | #else | |
75 | BUG(); | |
76 | #endif | |
77 | } | |
78 | ||
a5b9e5a2 AL |
79 | /* |
80 | * Used for LDT copy/destruction. | |
81 | */ | |
f90d2542 TG |
82 | static inline void init_new_context_ldt(struct mm_struct *mm) |
83 | { | |
84 | mm->context.ldt = NULL; | |
85 | init_rwsem(&mm->context.ldt_usr_sem); | |
86 | } | |
87 | int ldt_dup_context(struct mm_struct *oldmm, struct mm_struct *mm); | |
39a0526f | 88 | void destroy_context_ldt(struct mm_struct *mm); |
c2506438 | 89 | void ldt_arch_exit_mmap(struct mm_struct *mm); |
a5b9e5a2 | 90 | #else /* CONFIG_MODIFY_LDT_SYSCALL */ |
f90d2542 TG |
91 | static inline void init_new_context_ldt(struct mm_struct *mm) { } |
92 | static inline int ldt_dup_context(struct mm_struct *oldmm, | |
93 | struct mm_struct *mm) | |
a5b9e5a2 AL |
94 | { |
95 | return 0; | |
96 | } | |
c2506438 AL |
97 | static inline void destroy_context_ldt(struct mm_struct *mm) { } |
98 | static inline void ldt_arch_exit_mmap(struct mm_struct *mm) { } | |
a5b9e5a2 AL |
99 | #endif |
100 | ||
37868fe1 AL |
101 | static inline void load_mm_ldt(struct mm_struct *mm) |
102 | { | |
a5b9e5a2 | 103 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
37868fe1 AL |
104 | struct ldt_struct *ldt; |
105 | ||
7252704b WD |
106 | /* READ_ONCE synchronizes with smp_store_release */ |
107 | ldt = READ_ONCE(mm->context.ldt); | |
37868fe1 AL |
108 | |
109 | /* | |
110 | * Any change to mm->context.ldt is followed by an IPI to all | |
111 | * CPUs with the mm active. The LDT will not be freed until | |
112 | * after the IPI is handled by all such CPUs. This means that, | |
113 | * if the ldt_struct changes before we return, the values we see | |
114 | * will be safe, and the new values will be loaded before we run | |
115 | * any user code. | |
116 | * | |
117 | * NB: don't try to convert this to use RCU without extreme care. | |
118 | * We would still need IRQs off, because we don't want to change | |
119 | * the local LDT after an IPI loaded a newer value than the one | |
120 | * that we can see. | |
121 | */ | |
122 | ||
c2506438 AL |
123 | if (unlikely(ldt)) { |
124 | if (static_cpu_has(X86_FEATURE_PTI)) { | |
125 | if (WARN_ON_ONCE((unsigned long)ldt->slot > 1)) { | |
126 | /* | |
127 | * Whoops -- either the new LDT isn't mapped | |
128 | * (if slot == -1) or is mapped into a bogus | |
129 | * slot (if slot > 1). | |
130 | */ | |
131 | clear_LDT(); | |
132 | return; | |
133 | } | |
134 | ||
135 | /* | |
136 | * If page table isolation is enabled, ldt->entries | |
137 | * will not be mapped in the userspace pagetables. | |
138 | * Tell the CPU to access the LDT through the alias | |
139 | * at ldt_slot_va(ldt->slot). | |
140 | */ | |
141 | set_ldt(ldt_slot_va(ldt->slot), ldt->nr_entries); | |
142 | } else { | |
143 | set_ldt(ldt->entries, ldt->nr_entries); | |
144 | } | |
145 | } else { | |
37868fe1 | 146 | clear_LDT(); |
c2506438 | 147 | } |
a5b9e5a2 AL |
148 | #else |
149 | clear_LDT(); | |
150 | #endif | |
73534258 AL |
151 | } |
152 | ||
153 | static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) | |
154 | { | |
155 | #ifdef CONFIG_MODIFY_LDT_SYSCALL | |
156 | /* | |
157 | * Load the LDT if either the old or new mm had an LDT. | |
158 | * | |
159 | * An mm will never go from having an LDT to not having an LDT. Two | |
160 | * mms never share an LDT, so we don't gain anything by checking to | |
161 | * see whether the LDT changed. There's also no guarantee that | |
162 | * prev->context.ldt actually matches LDTR, but, if LDTR is non-NULL, | |
163 | * then prev->context.ldt will also be non-NULL. | |
164 | * | |
165 | * If we really cared, we could optimize the case where prev == next | |
166 | * and we're exiting lazy mode. Most of the time, if this happens, | |
167 | * we don't actually need to reload LDTR, but modify_ldt() is mostly | |
168 | * used by legacy code and emulators where we don't need this level of | |
169 | * performance. | |
170 | * | |
171 | * This uses | instead of || because it generates better code. | |
172 | */ | |
173 | if (unlikely((unsigned long)prev->context.ldt | | |
174 | (unsigned long)next->context.ldt)) | |
175 | load_mm_ldt(next); | |
176 | #endif | |
37868fe1 AL |
177 | |
178 | DEBUG_LOCKS_WARN_ON(preemptible()); | |
179 | } | |
180 | ||
a4bb9409 | 181 | void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk); |
6826c8ff | 182 | |
39a0526f DH |
183 | static inline int init_new_context(struct task_struct *tsk, |
184 | struct mm_struct *mm) | |
185 | { | |
bf7ee649 PZ |
186 | mutex_init(&mm->context.lock); |
187 | ||
e566a0df AL |
188 | mm->context.ctx_id = atomic64_inc_return(&last_mm_ctx_id); |
189 | atomic64_set(&mm->context.tlb_gen, 0); | |
190 | ||
f90d2542 | 191 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
e8c24d3a DH |
192 | if (cpu_feature_enabled(X86_FEATURE_OSPKE)) { |
193 | /* pkey 0 is the default and always allocated */ | |
194 | mm->context.pkey_allocation_map = 0x1; | |
195 | /* -1 means unallocated or invalid */ | |
196 | mm->context.execute_only_pkey = -1; | |
197 | } | |
f90d2542 TG |
198 | #endif |
199 | init_new_context_ldt(mm); | |
200 | return 0; | |
39a0526f DH |
201 | } |
202 | static inline void destroy_context(struct mm_struct *mm) | |
203 | { | |
204 | destroy_context_ldt(mm); | |
205 | } | |
206 | ||
69c0319a AL |
207 | extern void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
208 | struct task_struct *tsk); | |
6826c8ff | 209 | |
078194f8 AL |
210 | extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, |
211 | struct task_struct *tsk); | |
212 | #define switch_mm_irqs_off switch_mm_irqs_off | |
c3c2fee3 JF |
213 | |
214 | #define activate_mm(prev, next) \ | |
215 | do { \ | |
216 | paravirt_activate_mm((prev), (next)); \ | |
217 | switch_mm((prev), (next), NULL); \ | |
218 | } while (0); | |
219 | ||
6826c8ff BG |
220 | #ifdef CONFIG_X86_32 |
221 | #define deactivate_mm(tsk, mm) \ | |
222 | do { \ | |
ccbeed3a | 223 | lazy_load_gs(0); \ |
6826c8ff BG |
224 | } while (0) |
225 | #else | |
226 | #define deactivate_mm(tsk, mm) \ | |
227 | do { \ | |
228 | load_gs_index(0); \ | |
229 | loadsegment(fs, 0); \ | |
230 | } while (0) | |
231 | #endif | |
c3c2fee3 | 232 | |
b812abb6 | 233 | static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) |
a1ea1c03 DH |
234 | { |
235 | paravirt_arch_dup_mmap(oldmm, mm); | |
f90d2542 | 236 | return ldt_dup_context(oldmm, mm); |
a1ea1c03 DH |
237 | } |
238 | ||
239 | static inline void arch_exit_mmap(struct mm_struct *mm) | |
240 | { | |
241 | paravirt_arch_exit_mmap(mm); | |
c2506438 | 242 | ldt_arch_exit_mmap(mm); |
a1ea1c03 DH |
243 | } |
244 | ||
b0e9b09b DH |
245 | #ifdef CONFIG_X86_64 |
246 | static inline bool is_64bit_mm(struct mm_struct *mm) | |
247 | { | |
97f2645f | 248 | return !IS_ENABLED(CONFIG_IA32_EMULATION) || |
b0e9b09b DH |
249 | !(mm->context.ia32_compat == TIF_IA32); |
250 | } | |
251 | #else | |
252 | static inline bool is_64bit_mm(struct mm_struct *mm) | |
253 | { | |
254 | return false; | |
255 | } | |
256 | #endif | |
257 | ||
fe3d197f DH |
258 | static inline void arch_bprm_mm_init(struct mm_struct *mm, |
259 | struct vm_area_struct *vma) | |
260 | { | |
261 | mpx_mm_init(mm); | |
262 | } | |
263 | ||
1de4fa14 DH |
264 | static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma, |
265 | unsigned long start, unsigned long end) | |
266 | { | |
c922228e DH |
267 | /* |
268 | * mpx_notify_unmap() goes and reads a rarely-hot | |
269 | * cacheline in the mm_struct. That can be expensive | |
270 | * enough to be seen in profiles. | |
271 | * | |
272 | * The mpx_notify_unmap() call and its contents have been | |
273 | * observed to affect munmap() performance on hardware | |
274 | * where MPX is not present. | |
275 | * | |
276 | * The unlikely() optimizes for the fast case: no MPX | |
277 | * in the CPU, or no MPX use in the process. Even if | |
278 | * we get this wrong (in the unlikely event that MPX | |
279 | * is widely enabled on some system) the overhead of | |
280 | * MPX itself (reading bounds tables) is expected to | |
281 | * overwhelm the overhead of getting this unlikely() | |
282 | * consistently wrong. | |
283 | */ | |
284 | if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX))) | |
285 | mpx_notify_unmap(mm, vma, start, end); | |
1de4fa14 DH |
286 | } |
287 | ||
7d06d9c9 | 288 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
8f62c883 DH |
289 | static inline int vma_pkey(struct vm_area_struct *vma) |
290 | { | |
8f62c883 DH |
291 | unsigned long vma_pkey_mask = VM_PKEY_BIT0 | VM_PKEY_BIT1 | |
292 | VM_PKEY_BIT2 | VM_PKEY_BIT3; | |
7d06d9c9 DH |
293 | |
294 | return (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT; | |
295 | } | |
296 | #else | |
297 | static inline int vma_pkey(struct vm_area_struct *vma) | |
298 | { | |
299 | return 0; | |
8f62c883 | 300 | } |
7d06d9c9 | 301 | #endif |
8f62c883 | 302 | |
33a709b2 DH |
303 | /* |
304 | * We only want to enforce protection keys on the current process | |
305 | * because we effectively have no access to PKRU for other | |
306 | * processes or any way to tell *which * PKRU in a threaded | |
307 | * process we could use. | |
308 | * | |
309 | * So do not enforce things if the VMA is not from the current | |
310 | * mm, or if we are in a kernel thread. | |
311 | */ | |
312 | static inline bool vma_is_foreign(struct vm_area_struct *vma) | |
313 | { | |
314 | if (!current->mm) | |
315 | return true; | |
316 | /* | |
317 | * Should PKRU be enforced on the access to this VMA? If | |
318 | * the VMA is from another process, then PKRU has no | |
319 | * relevance and should not be enforced. | |
320 | */ | |
321 | if (current->mm != vma->vm_mm) | |
322 | return true; | |
323 | ||
324 | return false; | |
325 | } | |
326 | ||
1b2ee126 | 327 | static inline bool arch_vma_access_permitted(struct vm_area_struct *vma, |
d61172b4 | 328 | bool write, bool execute, bool foreign) |
33a709b2 | 329 | { |
d61172b4 DH |
330 | /* pkeys never affect instruction fetches */ |
331 | if (execute) | |
332 | return true; | |
33a709b2 | 333 | /* allow access if the VMA is not one from this process */ |
1b2ee126 | 334 | if (foreign || vma_is_foreign(vma)) |
33a709b2 DH |
335 | return true; |
336 | return __pkru_allows_pkey(vma_pkey(vma), write); | |
337 | } | |
338 | ||
d6e41f11 AL |
339 | /* |
340 | * This can be used from process context to figure out what the value of | |
6c690ee1 | 341 | * CR3 is without needing to do a (slow) __read_cr3(). |
d6e41f11 AL |
342 | * |
343 | * It's intended to be used for code like KVM that sneakily changes CR3 | |
344 | * and needs to restore it. It needs to be used very carefully. | |
345 | */ | |
346 | static inline unsigned long __get_current_cr3_fast(void) | |
347 | { | |
f741923a | 348 | unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd, |
72be211b | 349 | this_cpu_read(cpu_tlbstate.loaded_mm_asid)); |
d833a976 | 350 | |
d6e41f11 | 351 | /* For now, be very restrictive about when this can be called. */ |
4c07f904 | 352 | VM_WARN_ON(in_nmi() || preemptible()); |
d6e41f11 | 353 | |
6c690ee1 | 354 | VM_BUG_ON(cr3 != __read_cr3()); |
d6e41f11 AL |
355 | return cr3; |
356 | } | |
357 | ||
1965aae3 | 358 | #endif /* _ASM_X86_MMU_CONTEXT_H */ |