Commit | Line | Data |
---|---|---|
caab277b | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
b3901d54 CM |
2 | /* |
3 | * Based on arch/arm/include/asm/mmu_context.h | |
4 | * | |
5 | * Copyright (C) 1996 Russell King. | |
6 | * Copyright (C) 2012 ARM Ltd. | |
b3901d54 CM |
7 | */ |
8 | #ifndef __ASM_MMU_CONTEXT_H | |
9 | #define __ASM_MMU_CONTEXT_H | |
10 | ||
38fd94b0 CC |
11 | #ifndef __ASSEMBLY__ |
12 | ||
b3901d54 CM |
13 | #include <linux/compiler.h> |
14 | #include <linux/sched.h> | |
ef8bd77f | 15 | #include <linux/sched/hotplug.h> |
589ee628 | 16 | #include <linux/mm_types.h> |
65fddcfc | 17 | #include <linux/pgtable.h> |
7f955be9 | 18 | #include <linux/pkeys.h> |
b3901d54 CM |
19 | |
20 | #include <asm/cacheflush.h> | |
39bc88e5 | 21 | #include <asm/cpufeature.h> |
a8bf2fc4 | 22 | #include <asm/daifflags.h> |
506496bc | 23 | #include <asm/gcs.h> |
b3901d54 | 24 | #include <asm/proc-fns.h> |
b3901d54 | 25 | #include <asm/cputype.h> |
adf75899 | 26 | #include <asm/sysreg.h> |
9e8e865b | 27 | #include <asm/tlbflush.h> |
b3901d54 | 28 | |
c55191e9 AB |
29 | extern bool rodata_full; |
30 | ||
ec45d1cf WD |
31 | static inline void contextidr_thread_switch(struct task_struct *next) |
32 | { | |
d3ea42aa MR |
33 | if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR)) |
34 | return; | |
35 | ||
adf75899 MR |
36 | write_sysreg(task_pid_nr(next), contextidr_el1); |
37 | isb(); | |
ec45d1cf | 38 | } |
ec45d1cf | 39 | |
b3901d54 | 40 | /* |
833be850 | 41 | * Set TTBR0 to reserved_pg_dir. No translations will be possible via TTBR0. |
b3901d54 | 42 | */ |
b9293d45 | 43 | static inline void cpu_set_reserved_ttbr0_nosync(void) |
b3901d54 | 44 | { |
833be850 | 45 | unsigned long ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir)); |
b3901d54 | 46 | |
adf75899 | 47 | write_sysreg(ttbr, ttbr0_el1); |
b9293d45 JI |
48 | } |
49 | ||
50 | static inline void cpu_set_reserved_ttbr0(void) | |
51 | { | |
52 | cpu_set_reserved_ttbr0_nosync(); | |
adf75899 | 53 | isb(); |
b3901d54 CM |
54 | } |
55 | ||
25b92693 MR |
56 | void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm); |
57 | ||
7655abb9 WD |
58 | static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm) |
59 | { | |
60 | BUG_ON(pgd == swapper_pg_dir); | |
7655abb9 WD |
61 | cpu_do_switch_mm(virt_to_phys(pgd),mm); |
62 | } | |
63 | ||
dd006da2 | 64 | /* |
84b04d3e | 65 | * TCR.T0SZ value to use when the ID map is active. |
dd006da2 | 66 | */ |
84b04d3e | 67 | #define idmap_t0sz TCR_T0SZ(IDMAP_VA_BITS) |
dd006da2 | 68 | |
dd006da2 | 69 | /* |
1401bef7 | 70 | * Ensure TCR.T0SZ is set to the provided value. |
dd006da2 | 71 | */ |
609116d2 | 72 | static inline void __cpu_set_tcr_t0sz(unsigned long t0sz) |
dd006da2 | 73 | { |
1401bef7 | 74 | unsigned long tcr = read_sysreg(tcr_el1); |
c51e97d8 | 75 | |
cf938f91 | 76 | if ((tcr & TCR_T0SZ_MASK) == t0sz) |
c51e97d8 WD |
77 | return; |
78 | ||
adf75899 | 79 | tcr &= ~TCR_T0SZ_MASK; |
cf938f91 | 80 | tcr |= t0sz; |
adf75899 MR |
81 | write_sysreg(tcr, tcr_el1); |
82 | isb(); | |
dd006da2 AB |
83 | } |
84 | ||
5383cc6e | 85 | #define cpu_set_default_tcr_t0sz() __cpu_set_tcr_t0sz(TCR_T0SZ(vabits_actual)) |
609116d2 MR |
86 | #define cpu_set_idmap_tcr_t0sz() __cpu_set_tcr_t0sz(idmap_t0sz) |
87 | ||
9e8e865b MR |
88 | /* |
89 | * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm. | |
90 | * | |
91 | * The idmap lives in the same VA range as userspace, but uses global entries | |
92 | * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from | |
93 | * speculative TLB fetches, we must temporarily install the reserved page | |
94 | * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ. | |
95 | * | |
96 | * If current is a not a user task, the mm covers the TTBR1_EL1 page tables, | |
97 | * which should not be installed in TTBR0_EL1. In this case we can leave the | |
98 | * reserved page tables in place. | |
99 | */ | |
100 | static inline void cpu_uninstall_idmap(void) | |
101 | { | |
102 | struct mm_struct *mm = current->active_mm; | |
103 | ||
104 | cpu_set_reserved_ttbr0(); | |
105 | local_flush_tlb_all(); | |
106 | cpu_set_default_tcr_t0sz(); | |
107 | ||
39bc88e5 | 108 | if (mm != &init_mm && !system_uses_ttbr0_pan()) |
9e8e865b MR |
109 | cpu_switch_mm(mm->pgd, mm); |
110 | } | |
111 | ||
e0f92f0d | 112 | static inline void cpu_install_idmap(void) |
609116d2 MR |
113 | { |
114 | cpu_set_reserved_ttbr0(); | |
115 | local_flush_tlb_all(); | |
116 | cpu_set_idmap_tcr_t0sz(); | |
117 | ||
e0f92f0d | 118 | cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm); |
609116d2 MR |
119 | } |
120 | ||
a347f601 PT |
121 | /* |
122 | * Load our new page tables. A strict BBM approach requires that we ensure that | |
123 | * TLBs are free of any entries that may overlap with the global mappings we are | |
124 | * about to install. | |
125 | * | |
126 | * For a real hibernate/resume/kexec cycle TTBR0 currently points to a zero | |
127 | * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI runtime | |
128 | * services), while for a userspace-driven test_resume cycle it points to | |
129 | * userspace page tables (and we must point it at a zero page ourselves). | |
130 | * | |
131 | * We change T0SZ as part of installing the idmap. This is undone by | |
132 | * cpu_uninstall_idmap() in __cpu_suspend_exit(). | |
133 | */ | |
134 | static inline void cpu_install_ttbr0(phys_addr_t ttbr0, unsigned long t0sz) | |
135 | { | |
136 | cpu_set_reserved_ttbr0(); | |
137 | local_flush_tlb_all(); | |
138 | __cpu_set_tcr_t0sz(t0sz); | |
139 | ||
140 | /* avoid cpu_switch_mm() and its SW-PAN and CNP interactions */ | |
141 | write_sysreg(ttbr0, ttbr0_el1); | |
142 | isb(); | |
143 | } | |
144 | ||
e0f92f0d | 145 | void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp); |
50e1881d | 146 | |
54c8818a MR |
147 | static inline void cpu_enable_swapper_cnp(void) |
148 | { | |
e0f92f0d | 149 | __cpu_replace_ttbr1(lm_alias(swapper_pg_dir), true); |
54c8818a MR |
150 | } |
151 | ||
e0f92f0d | 152 | static inline void cpu_replace_ttbr1(pgd_t *pgdp) |
54c8818a MR |
153 | { |
154 | /* | |
155 | * Only for early TTBR1 replacement before cpucaps are finalized and | |
156 | * before we've decided whether to use CNP. | |
157 | */ | |
158 | WARN_ON(system_capabilities_finalized()); | |
e0f92f0d | 159 | __cpu_replace_ttbr1(pgdp, false); |
54c8818a MR |
160 | } |
161 | ||
5aec715d WD |
162 | /* |
163 | * It would be nice to return ASIDs back to the allocator, but unfortunately | |
164 | * that introduces a race with a generation rollover where we could erroneously | |
165 | * free an ASID allocated in a future generation. We could workaround this by | |
166 | * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap), | |
167 | * but we'd then need to make sure that we didn't dirty any TLBs afterwards. | |
168 | * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you | |
169 | * take CPU migration into account. | |
170 | */ | |
c4885bbb | 171 | void check_and_switch_context(struct mm_struct *mm); |
b3901d54 | 172 | |
d98295d3 | 173 | #define init_new_context(tsk, mm) init_new_context(tsk, mm) |
48118151 JPB |
174 | static inline int |
175 | init_new_context(struct task_struct *tsk, struct mm_struct *mm) | |
176 | { | |
177 | atomic64_set(&mm->context.id, 0); | |
178 | refcount_set(&mm->context.pinned, 0); | |
7f955be9 JG |
179 | |
180 | /* pkey 0 is the default, so always reserve it. */ | |
181 | mm->context.pkey_allocation_map = BIT(0); | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
186 | static inline void arch_dup_pkeys(struct mm_struct *oldmm, | |
187 | struct mm_struct *mm) | |
188 | { | |
189 | /* Duplicate the oldmm pkey state in mm: */ | |
190 | mm->context.pkey_allocation_map = oldmm->context.pkey_allocation_map; | |
191 | } | |
192 | ||
193 | static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) | |
194 | { | |
195 | arch_dup_pkeys(oldmm, mm); | |
196 | ||
48118151 JPB |
197 | return 0; |
198 | } | |
b3901d54 | 199 | |
7f955be9 JG |
200 | static inline void arch_exit_mmap(struct mm_struct *mm) |
201 | { | |
202 | } | |
203 | ||
204 | static inline void arch_unmap(struct mm_struct *mm, | |
205 | unsigned long start, unsigned long end) | |
206 | { | |
207 | } | |
208 | ||
39bc88e5 CM |
209 | #ifdef CONFIG_ARM64_SW_TTBR0_PAN |
210 | static inline void update_saved_ttbr0(struct task_struct *tsk, | |
211 | struct mm_struct *mm) | |
b3901d54 | 212 | { |
0adbdfde WD |
213 | u64 ttbr; |
214 | ||
215 | if (!system_uses_ttbr0_pan()) | |
216 | return; | |
217 | ||
218 | if (mm == &init_mm) | |
9163f011 | 219 | ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir)); |
0adbdfde | 220 | else |
9163f011 | 221 | ttbr = phys_to_ttbr(virt_to_phys(mm->pgd)) | ASID(mm) << 48; |
0adbdfde | 222 | |
6b88a32c | 223 | WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr); |
39bc88e5 CM |
224 | } |
225 | #else | |
226 | static inline void update_saved_ttbr0(struct task_struct *tsk, | |
227 | struct mm_struct *mm) | |
228 | { | |
229 | } | |
230 | #endif | |
b3901d54 | 231 | |
d98295d3 | 232 | #define enter_lazy_tlb enter_lazy_tlb |
d96cc49b WD |
233 | static inline void |
234 | enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) | |
235 | { | |
236 | /* | |
237 | * We don't actually care about the ttbr0 mapping, so point it at the | |
238 | * zero page. | |
239 | */ | |
240 | update_saved_ttbr0(tsk, &init_mm); | |
241 | } | |
242 | ||
39bc88e5 CM |
243 | static inline void __switch_mm(struct mm_struct *next) |
244 | { | |
e53f21bc CM |
245 | /* |
246 | * init_mm.pgd does not contain any user mappings and it is always | |
247 | * active for kernel addresses in TTBR1. Just set the reserved TTBR0. | |
248 | */ | |
249 | if (next == &init_mm) { | |
250 | cpu_set_reserved_ttbr0(); | |
251 | return; | |
252 | } | |
253 | ||
c4885bbb | 254 | check_and_switch_context(next); |
b3901d54 CM |
255 | } |
256 | ||
39bc88e5 CM |
257 | static inline void |
258 | switch_mm(struct mm_struct *prev, struct mm_struct *next, | |
259 | struct task_struct *tsk) | |
260 | { | |
261 | if (prev != next) | |
262 | __switch_mm(next); | |
263 | ||
264 | /* | |
265 | * Update the saved TTBR0_EL1 of the scheduled-in task as the previous | |
266 | * value may have not been initialised yet (activate_mm caller) or the | |
267 | * ASID has changed since the last run (following the context switch | |
0adbdfde | 268 | * of another thread of the same process). |
39bc88e5 | 269 | */ |
0adbdfde | 270 | update_saved_ttbr0(tsk, next); |
39bc88e5 CM |
271 | } |
272 | ||
d82158fa | 273 | static inline const struct cpumask * |
3a544661 | 274 | __task_cpu_possible_mask(struct task_struct *p, const struct cpumask *mask) |
d82158fa WD |
275 | { |
276 | if (!static_branch_unlikely(&arm64_mismatched_32bit_el0)) | |
3a544661 | 277 | return mask; |
d82158fa WD |
278 | |
279 | if (!is_compat_thread(task_thread_info(p))) | |
3a544661 | 280 | return mask; |
d82158fa WD |
281 | |
282 | return system_32bit_el0_cpumask(); | |
283 | } | |
3a544661 FW |
284 | |
285 | static inline const struct cpumask * | |
286 | task_cpu_possible_mask(struct task_struct *p) | |
287 | { | |
288 | return __task_cpu_possible_mask(p, cpu_possible_mask); | |
289 | } | |
d82158fa WD |
290 | #define task_cpu_possible_mask task_cpu_possible_mask |
291 | ||
3a544661 FW |
292 | const struct cpumask *task_cpu_fallback_mask(struct task_struct *p); |
293 | ||
13f417f3 | 294 | void verify_cpu_asid_bits(void); |
6b88a32c | 295 | void post_ttbr_update_workaround(void); |
13f417f3 | 296 | |
48118151 JPB |
297 | unsigned long arm64_mm_context_get(struct mm_struct *mm); |
298 | void arm64_mm_context_put(struct mm_struct *mm); | |
299 | ||
f7d30434 KS |
300 | #define mm_untag_mask mm_untag_mask |
301 | static inline unsigned long mm_untag_mask(struct mm_struct *mm) | |
302 | { | |
303 | return -1UL >> 8; | |
304 | } | |
305 | ||
7f955be9 JG |
306 | /* |
307 | * Only enforce protection keys on the current process, because there is no | |
308 | * user context to access POR_EL0 for another address space. | |
309 | */ | |
310 | static inline bool arch_vma_access_permitted(struct vm_area_struct *vma, | |
311 | bool write, bool execute, bool foreign) | |
312 | { | |
313 | if (!system_supports_poe()) | |
314 | return true; | |
315 | ||
316 | /* allow access if the VMA is not one from this process */ | |
317 | if (foreign || vma_is_foreign(vma)) | |
318 | return true; | |
319 | ||
320 | return por_el0_allows_pkey(vma_pkey(vma), write, execute); | |
321 | } | |
322 | ||
506496bc MB |
323 | #define deactivate_mm deactivate_mm |
324 | static inline void deactivate_mm(struct task_struct *tsk, | |
325 | struct mm_struct *mm) | |
326 | { | |
327 | gcs_free(tsk); | |
328 | } | |
329 | ||
330 | ||
d98295d3 NP |
331 | #include <asm-generic/mmu_context.h> |
332 | ||
38fd94b0 CC |
333 | #endif /* !__ASSEMBLY__ */ |
334 | ||
335 | #endif /* !__ASM_MMU_CONTEXT_H */ |