xref: /openbmc/linux/arch/arm64/include/asm/mmu_context.h (revision 39bc88e5e38e9b213bd7d833ce0df6ec029761ad)
1b3901d54SCatalin Marinas /*
2b3901d54SCatalin Marinas  * Based on arch/arm/include/asm/mmu_context.h
3b3901d54SCatalin Marinas  *
4b3901d54SCatalin Marinas  * Copyright (C) 1996 Russell King.
5b3901d54SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6b3901d54SCatalin Marinas  *
7b3901d54SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8b3901d54SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9b3901d54SCatalin Marinas  * published by the Free Software Foundation.
10b3901d54SCatalin Marinas  *
11b3901d54SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12b3901d54SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13b3901d54SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b3901d54SCatalin Marinas  * GNU General Public License for more details.
15b3901d54SCatalin Marinas  *
16b3901d54SCatalin Marinas  * You should have received a copy of the GNU General Public License
17b3901d54SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18b3901d54SCatalin Marinas  */
19b3901d54SCatalin Marinas #ifndef __ASM_MMU_CONTEXT_H
20b3901d54SCatalin Marinas #define __ASM_MMU_CONTEXT_H
21b3901d54SCatalin Marinas 
22b3901d54SCatalin Marinas #include <linux/compiler.h>
23b3901d54SCatalin Marinas #include <linux/sched.h>
24b3901d54SCatalin Marinas 
25b3901d54SCatalin Marinas #include <asm/cacheflush.h>
26*39bc88e5SCatalin Marinas #include <asm/cpufeature.h>
27b3901d54SCatalin Marinas #include <asm/proc-fns.h>
28b3901d54SCatalin Marinas #include <asm-generic/mm_hooks.h>
29b3901d54SCatalin Marinas #include <asm/cputype.h>
30b3901d54SCatalin Marinas #include <asm/pgtable.h>
31adf75899SMark Rutland #include <asm/sysreg.h>
329e8e865bSMark Rutland #include <asm/tlbflush.h>
33b3901d54SCatalin Marinas 
34ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
35ec45d1cfSWill Deacon {
36d3ea42aaSMark Rutland 	if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
37d3ea42aaSMark Rutland 		return;
38d3ea42aaSMark Rutland 
39adf75899SMark Rutland 	write_sysreg(task_pid_nr(next), contextidr_el1);
40adf75899SMark Rutland 	isb();
41ec45d1cfSWill Deacon }
42ec45d1cfSWill Deacon 
43b3901d54SCatalin Marinas /*
44b3901d54SCatalin Marinas  * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
45b3901d54SCatalin Marinas  */
46b3901d54SCatalin Marinas static inline void cpu_set_reserved_ttbr0(void)
47b3901d54SCatalin Marinas {
485227cfa7SMark Rutland 	unsigned long ttbr = virt_to_phys(empty_zero_page);
49b3901d54SCatalin Marinas 
50adf75899SMark Rutland 	write_sysreg(ttbr, ttbr0_el1);
51adf75899SMark Rutland 	isb();
52b3901d54SCatalin Marinas }
53b3901d54SCatalin Marinas 
54dd006da2SArd Biesheuvel /*
55dd006da2SArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active. Usually equals
56dd006da2SArd Biesheuvel  * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
57dd006da2SArd Biesheuvel  * physical memory, in which case it will be smaller.
58dd006da2SArd Biesheuvel  */
59dd006da2SArd Biesheuvel extern u64 idmap_t0sz;
60dd006da2SArd Biesheuvel 
61dd006da2SArd Biesheuvel static inline bool __cpu_uses_extended_idmap(void)
62dd006da2SArd Biesheuvel {
63dd006da2SArd Biesheuvel 	return (!IS_ENABLED(CONFIG_ARM64_VA_BITS_48) &&
64dd006da2SArd Biesheuvel 		unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS)));
65dd006da2SArd Biesheuvel }
66dd006da2SArd Biesheuvel 
67c51e97d8SWill Deacon /*
68c51e97d8SWill Deacon  * Set TCR.T0SZ to its default value (based on VA_BITS)
69c51e97d8SWill Deacon  */
70609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
71dd006da2SArd Biesheuvel {
72dd006da2SArd Biesheuvel 	unsigned long tcr;
73dd006da2SArd Biesheuvel 
74c51e97d8SWill Deacon 	if (!__cpu_uses_extended_idmap())
75c51e97d8SWill Deacon 		return;
76c51e97d8SWill Deacon 
77adf75899SMark Rutland 	tcr = read_sysreg(tcr_el1);
78adf75899SMark Rutland 	tcr &= ~TCR_T0SZ_MASK;
79adf75899SMark Rutland 	tcr |= t0sz << TCR_T0SZ_OFFSET;
80adf75899SMark Rutland 	write_sysreg(tcr, tcr_el1);
81adf75899SMark Rutland 	isb();
82dd006da2SArd Biesheuvel }
83dd006da2SArd Biesheuvel 
84609116d2SMark Rutland #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS))
85609116d2SMark Rutland #define cpu_set_idmap_tcr_t0sz()	__cpu_set_tcr_t0sz(idmap_t0sz)
86609116d2SMark Rutland 
87b3901d54SCatalin Marinas /*
889e8e865bSMark Rutland  * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
899e8e865bSMark Rutland  *
909e8e865bSMark Rutland  * The idmap lives in the same VA range as userspace, but uses global entries
919e8e865bSMark Rutland  * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
929e8e865bSMark Rutland  * speculative TLB fetches, we must temporarily install the reserved page
939e8e865bSMark Rutland  * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
949e8e865bSMark Rutland  *
959e8e865bSMark Rutland  * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
969e8e865bSMark Rutland  * which should not be installed in TTBR0_EL1. In this case we can leave the
979e8e865bSMark Rutland  * reserved page tables in place.
989e8e865bSMark Rutland  */
999e8e865bSMark Rutland static inline void cpu_uninstall_idmap(void)
1009e8e865bSMark Rutland {
1019e8e865bSMark Rutland 	struct mm_struct *mm = current->active_mm;
1029e8e865bSMark Rutland 
1039e8e865bSMark Rutland 	cpu_set_reserved_ttbr0();
1049e8e865bSMark Rutland 	local_flush_tlb_all();
1059e8e865bSMark Rutland 	cpu_set_default_tcr_t0sz();
1069e8e865bSMark Rutland 
107*39bc88e5SCatalin Marinas 	if (mm != &init_mm && !system_uses_ttbr0_pan())
1089e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1099e8e865bSMark Rutland }
1109e8e865bSMark Rutland 
111609116d2SMark Rutland static inline void cpu_install_idmap(void)
112609116d2SMark Rutland {
113609116d2SMark Rutland 	cpu_set_reserved_ttbr0();
114609116d2SMark Rutland 	local_flush_tlb_all();
115609116d2SMark Rutland 	cpu_set_idmap_tcr_t0sz();
116609116d2SMark Rutland 
117609116d2SMark Rutland 	cpu_switch_mm(idmap_pg_dir, &init_mm);
118609116d2SMark Rutland }
119609116d2SMark Rutland 
1209e8e865bSMark Rutland /*
12150e1881dSMark Rutland  * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
12250e1881dSMark Rutland  * avoiding the possibility of conflicting TLB entries being allocated.
12350e1881dSMark Rutland  */
12450e1881dSMark Rutland static inline void cpu_replace_ttbr1(pgd_t *pgd)
12550e1881dSMark Rutland {
12650e1881dSMark Rutland 	typedef void (ttbr_replace_func)(phys_addr_t);
12750e1881dSMark Rutland 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
12850e1881dSMark Rutland 	ttbr_replace_func *replace_phys;
12950e1881dSMark Rutland 
13050e1881dSMark Rutland 	phys_addr_t pgd_phys = virt_to_phys(pgd);
13150e1881dSMark Rutland 
13250e1881dSMark Rutland 	replace_phys = (void *)virt_to_phys(idmap_cpu_replace_ttbr1);
13350e1881dSMark Rutland 
13450e1881dSMark Rutland 	cpu_install_idmap();
13550e1881dSMark Rutland 	replace_phys(pgd_phys);
13650e1881dSMark Rutland 	cpu_uninstall_idmap();
13750e1881dSMark Rutland }
13850e1881dSMark Rutland 
13950e1881dSMark Rutland /*
1405aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1415aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1425aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1435aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1445aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1455aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1465aec715dSWill Deacon  * take CPU migration into account.
147b3901d54SCatalin Marinas  */
148b3901d54SCatalin Marinas #define destroy_context(mm)		do { } while(0)
1495aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
150b3901d54SCatalin Marinas 
15165da0a8eSArd Biesheuvel #define init_new_context(tsk,mm)	({ atomic64_set(&(mm)->context.id, 0); 0; })
152b3901d54SCatalin Marinas 
153b3901d54SCatalin Marinas /*
154b3901d54SCatalin Marinas  * This is called when "tsk" is about to enter lazy TLB mode.
155b3901d54SCatalin Marinas  *
156b3901d54SCatalin Marinas  * mm:  describes the currently active mm context
157b3901d54SCatalin Marinas  * tsk: task which is entering lazy tlb
158b3901d54SCatalin Marinas  * cpu: cpu number which is entering lazy tlb
159b3901d54SCatalin Marinas  *
160b3901d54SCatalin Marinas  * tsk->mm will be NULL
161b3901d54SCatalin Marinas  */
162b3901d54SCatalin Marinas static inline void
163b3901d54SCatalin Marinas enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
164b3901d54SCatalin Marinas {
165b3901d54SCatalin Marinas }
166b3901d54SCatalin Marinas 
167*39bc88e5SCatalin Marinas #ifdef CONFIG_ARM64_SW_TTBR0_PAN
168*39bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
169*39bc88e5SCatalin Marinas 				      struct mm_struct *mm)
170*39bc88e5SCatalin Marinas {
171*39bc88e5SCatalin Marinas 	if (system_uses_ttbr0_pan()) {
172*39bc88e5SCatalin Marinas 		BUG_ON(mm->pgd == swapper_pg_dir);
173*39bc88e5SCatalin Marinas 		task_thread_info(tsk)->ttbr0 =
174*39bc88e5SCatalin Marinas 			virt_to_phys(mm->pgd) | ASID(mm) << 48;
175*39bc88e5SCatalin Marinas 	}
176*39bc88e5SCatalin Marinas }
177*39bc88e5SCatalin Marinas #else
178*39bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
179*39bc88e5SCatalin Marinas 				      struct mm_struct *mm)
180*39bc88e5SCatalin Marinas {
181*39bc88e5SCatalin Marinas }
182*39bc88e5SCatalin Marinas #endif
183*39bc88e5SCatalin Marinas 
184*39bc88e5SCatalin Marinas static inline void __switch_mm(struct mm_struct *next)
185b3901d54SCatalin Marinas {
186b3901d54SCatalin Marinas 	unsigned int cpu = smp_processor_id();
187b3901d54SCatalin Marinas 
188e53f21bcSCatalin Marinas 	/*
189e53f21bcSCatalin Marinas 	 * init_mm.pgd does not contain any user mappings and it is always
190e53f21bcSCatalin Marinas 	 * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
191e53f21bcSCatalin Marinas 	 */
192e53f21bcSCatalin Marinas 	if (next == &init_mm) {
193e53f21bcSCatalin Marinas 		cpu_set_reserved_ttbr0();
194e53f21bcSCatalin Marinas 		return;
195e53f21bcSCatalin Marinas 	}
196e53f21bcSCatalin Marinas 
197c2775b2eSWill Deacon 	check_and_switch_context(next, cpu);
198b3901d54SCatalin Marinas }
199b3901d54SCatalin Marinas 
200*39bc88e5SCatalin Marinas static inline void
201*39bc88e5SCatalin Marinas switch_mm(struct mm_struct *prev, struct mm_struct *next,
202*39bc88e5SCatalin Marinas 	  struct task_struct *tsk)
203*39bc88e5SCatalin Marinas {
204*39bc88e5SCatalin Marinas 	if (prev != next)
205*39bc88e5SCatalin Marinas 		__switch_mm(next);
206*39bc88e5SCatalin Marinas 
207*39bc88e5SCatalin Marinas 	/*
208*39bc88e5SCatalin Marinas 	 * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
209*39bc88e5SCatalin Marinas 	 * value may have not been initialised yet (activate_mm caller) or the
210*39bc88e5SCatalin Marinas 	 * ASID has changed since the last run (following the context switch
211*39bc88e5SCatalin Marinas 	 * of another thread of the same process). Avoid setting the reserved
212*39bc88e5SCatalin Marinas 	 * TTBR0_EL1 to swapper_pg_dir (init_mm; e.g. via idle_task_exit).
213*39bc88e5SCatalin Marinas 	 */
214*39bc88e5SCatalin Marinas 	if (next != &init_mm)
215*39bc88e5SCatalin Marinas 		update_saved_ttbr0(tsk, next);
216*39bc88e5SCatalin Marinas }
217*39bc88e5SCatalin Marinas 
218b3901d54SCatalin Marinas #define deactivate_mm(tsk,mm)	do { } while (0)
219*39bc88e5SCatalin Marinas #define activate_mm(prev,next)	switch_mm(prev, next, current)
220b3901d54SCatalin Marinas 
22113f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void);
22213f417f3SSuzuki K Poulose 
223b3901d54SCatalin Marinas #endif
224