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