xref: /openbmc/linux/arch/arm64/include/asm/mmu_context.h (revision 5383cc6efed13784ddb3cff2cc183b6b8c50c8db)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2b3901d54SCatalin Marinas /*
3b3901d54SCatalin Marinas  * Based on arch/arm/include/asm/mmu_context.h
4b3901d54SCatalin Marinas  *
5b3901d54SCatalin Marinas  * Copyright (C) 1996 Russell King.
6b3901d54SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
7b3901d54SCatalin Marinas  */
8b3901d54SCatalin Marinas #ifndef __ASM_MMU_CONTEXT_H
9b3901d54SCatalin Marinas #define __ASM_MMU_CONTEXT_H
10b3901d54SCatalin Marinas 
1138fd94b0SChristopher Covington #ifndef __ASSEMBLY__
1238fd94b0SChristopher Covington 
13b3901d54SCatalin Marinas #include <linux/compiler.h>
14b3901d54SCatalin Marinas #include <linux/sched.h>
15ef8bd77fSIngo Molnar #include <linux/sched/hotplug.h>
16589ee628SIngo Molnar #include <linux/mm_types.h>
17b3901d54SCatalin Marinas 
18b3901d54SCatalin Marinas #include <asm/cacheflush.h>
1939bc88e5SCatalin Marinas #include <asm/cpufeature.h>
20b3901d54SCatalin Marinas #include <asm/proc-fns.h>
21b3901d54SCatalin Marinas #include <asm-generic/mm_hooks.h>
22b3901d54SCatalin Marinas #include <asm/cputype.h>
23b3901d54SCatalin Marinas #include <asm/pgtable.h>
24adf75899SMark Rutland #include <asm/sysreg.h>
259e8e865bSMark Rutland #include <asm/tlbflush.h>
26b3901d54SCatalin Marinas 
27c55191e9SArd Biesheuvel extern bool rodata_full;
28c55191e9SArd Biesheuvel 
29ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
30ec45d1cfSWill Deacon {
31d3ea42aaSMark Rutland 	if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
32d3ea42aaSMark Rutland 		return;
33d3ea42aaSMark Rutland 
34adf75899SMark Rutland 	write_sysreg(task_pid_nr(next), contextidr_el1);
35adf75899SMark Rutland 	isb();
36ec45d1cfSWill Deacon }
37ec45d1cfSWill Deacon 
38b3901d54SCatalin Marinas /*
39b3901d54SCatalin Marinas  * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
40b3901d54SCatalin Marinas  */
41b3901d54SCatalin Marinas static inline void cpu_set_reserved_ttbr0(void)
42b3901d54SCatalin Marinas {
43529c4b05SKristina Martsenko 	unsigned long ttbr = phys_to_ttbr(__pa_symbol(empty_zero_page));
44b3901d54SCatalin Marinas 
45adf75899SMark Rutland 	write_sysreg(ttbr, ttbr0_el1);
46adf75899SMark Rutland 	isb();
47b3901d54SCatalin Marinas }
48b3901d54SCatalin Marinas 
497655abb9SWill Deacon static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
507655abb9SWill Deacon {
517655abb9SWill Deacon 	BUG_ON(pgd == swapper_pg_dir);
527655abb9SWill Deacon 	cpu_set_reserved_ttbr0();
537655abb9SWill Deacon 	cpu_do_switch_mm(virt_to_phys(pgd),mm);
547655abb9SWill Deacon }
557655abb9SWill Deacon 
56dd006da2SArd Biesheuvel /*
57dd006da2SArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active. Usually equals
58dd006da2SArd Biesheuvel  * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
59dd006da2SArd Biesheuvel  * physical memory, in which case it will be smaller.
60dd006da2SArd Biesheuvel  */
61dd006da2SArd Biesheuvel extern u64 idmap_t0sz;
62fa2a8445SKristina Martsenko extern u64 idmap_ptrs_per_pgd;
63dd006da2SArd Biesheuvel 
64dd006da2SArd Biesheuvel static inline bool __cpu_uses_extended_idmap(void)
65dd006da2SArd Biesheuvel {
6668d23da4SWill Deacon 	if (IS_ENABLED(CONFIG_ARM64_USER_VA_BITS_52))
6767e7fdfcSSteve Capper 		return false;
6867e7fdfcSSteve Capper 
696a205420SKristina Martsenko 	return unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS));
70dd006da2SArd Biesheuvel }
71dd006da2SArd Biesheuvel 
72c51e97d8SWill Deacon /*
73fa2a8445SKristina Martsenko  * True if the extended ID map requires an extra level of translation table
74fa2a8445SKristina Martsenko  * to be configured.
75fa2a8445SKristina Martsenko  */
76fa2a8445SKristina Martsenko static inline bool __cpu_uses_extended_idmap_level(void)
77fa2a8445SKristina Martsenko {
786a205420SKristina Martsenko 	return ARM64_HW_PGTABLE_LEVELS(64 - idmap_t0sz) > CONFIG_PGTABLE_LEVELS;
79fa2a8445SKristina Martsenko }
80fa2a8445SKristina Martsenko 
81fa2a8445SKristina Martsenko /*
82c51e97d8SWill Deacon  * Set TCR.T0SZ to its default value (based on VA_BITS)
83c51e97d8SWill Deacon  */
84609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
85dd006da2SArd Biesheuvel {
86dd006da2SArd Biesheuvel 	unsigned long tcr;
87dd006da2SArd Biesheuvel 
88c51e97d8SWill Deacon 	if (!__cpu_uses_extended_idmap())
89c51e97d8SWill Deacon 		return;
90c51e97d8SWill Deacon 
91adf75899SMark Rutland 	tcr = read_sysreg(tcr_el1);
92adf75899SMark Rutland 	tcr &= ~TCR_T0SZ_MASK;
93adf75899SMark Rutland 	tcr |= t0sz << TCR_T0SZ_OFFSET;
94adf75899SMark Rutland 	write_sysreg(tcr, tcr_el1);
95adf75899SMark Rutland 	isb();
96dd006da2SArd Biesheuvel }
97dd006da2SArd Biesheuvel 
98*5383cc6eSSteve Capper #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(vabits_actual))
99609116d2SMark Rutland #define cpu_set_idmap_tcr_t0sz()	__cpu_set_tcr_t0sz(idmap_t0sz)
100609116d2SMark Rutland 
101b3901d54SCatalin Marinas /*
1029e8e865bSMark Rutland  * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
1039e8e865bSMark Rutland  *
1049e8e865bSMark Rutland  * The idmap lives in the same VA range as userspace, but uses global entries
1059e8e865bSMark Rutland  * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
1069e8e865bSMark Rutland  * speculative TLB fetches, we must temporarily install the reserved page
1079e8e865bSMark Rutland  * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
1089e8e865bSMark Rutland  *
1099e8e865bSMark Rutland  * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
1109e8e865bSMark Rutland  * which should not be installed in TTBR0_EL1. In this case we can leave the
1119e8e865bSMark Rutland  * reserved page tables in place.
1129e8e865bSMark Rutland  */
1139e8e865bSMark Rutland static inline void cpu_uninstall_idmap(void)
1149e8e865bSMark Rutland {
1159e8e865bSMark Rutland 	struct mm_struct *mm = current->active_mm;
1169e8e865bSMark Rutland 
1179e8e865bSMark Rutland 	cpu_set_reserved_ttbr0();
1189e8e865bSMark Rutland 	local_flush_tlb_all();
1199e8e865bSMark Rutland 	cpu_set_default_tcr_t0sz();
1209e8e865bSMark Rutland 
12139bc88e5SCatalin Marinas 	if (mm != &init_mm && !system_uses_ttbr0_pan())
1229e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1239e8e865bSMark Rutland }
1249e8e865bSMark Rutland 
125609116d2SMark Rutland static inline void cpu_install_idmap(void)
126609116d2SMark Rutland {
127609116d2SMark Rutland 	cpu_set_reserved_ttbr0();
128609116d2SMark Rutland 	local_flush_tlb_all();
129609116d2SMark Rutland 	cpu_set_idmap_tcr_t0sz();
130609116d2SMark Rutland 
1312077be67SLaura Abbott 	cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
132609116d2SMark Rutland }
133609116d2SMark Rutland 
1349e8e865bSMark Rutland /*
13550e1881dSMark Rutland  * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
13650e1881dSMark Rutland  * avoiding the possibility of conflicting TLB entries being allocated.
13750e1881dSMark Rutland  */
13820a004e7SWill Deacon static inline void cpu_replace_ttbr1(pgd_t *pgdp)
13950e1881dSMark Rutland {
14050e1881dSMark Rutland 	typedef void (ttbr_replace_func)(phys_addr_t);
14150e1881dSMark Rutland 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
14250e1881dSMark Rutland 	ttbr_replace_func *replace_phys;
14350e1881dSMark Rutland 
1445ffdfaedSVladimir Murzin 	/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
1455ffdfaedSVladimir Murzin 	phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
1465ffdfaedSVladimir Murzin 
1475ffdfaedSVladimir Murzin 	if (system_supports_cnp() && !WARN_ON(pgdp != lm_alias(swapper_pg_dir))) {
1485ffdfaedSVladimir Murzin 		/*
1495ffdfaedSVladimir Murzin 		 * cpu_replace_ttbr1() is used when there's a boot CPU
1505ffdfaedSVladimir Murzin 		 * up (i.e. cpufeature framework is not up yet) and
1515ffdfaedSVladimir Murzin 		 * latter only when we enable CNP via cpufeature's
1525ffdfaedSVladimir Murzin 		 * enable() callback.
1535ffdfaedSVladimir Murzin 		 * Also we rely on the cpu_hwcap bit being set before
1545ffdfaedSVladimir Murzin 		 * calling the enable() function.
1555ffdfaedSVladimir Murzin 		 */
1565ffdfaedSVladimir Murzin 		ttbr1 |= TTBR_CNP_BIT;
1575ffdfaedSVladimir Murzin 	}
15850e1881dSMark Rutland 
1592077be67SLaura Abbott 	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
16050e1881dSMark Rutland 
16150e1881dSMark Rutland 	cpu_install_idmap();
1625ffdfaedSVladimir Murzin 	replace_phys(ttbr1);
16350e1881dSMark Rutland 	cpu_uninstall_idmap();
16450e1881dSMark Rutland }
16550e1881dSMark Rutland 
16650e1881dSMark Rutland /*
1675aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1685aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1695aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1705aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1715aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1725aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1735aec715dSWill Deacon  * take CPU migration into account.
174b3901d54SCatalin Marinas  */
175b3901d54SCatalin Marinas #define destroy_context(mm)		do { } while(0)
1765aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
177b3901d54SCatalin Marinas 
17865da0a8eSArd Biesheuvel #define init_new_context(tsk,mm)	({ atomic64_set(&(mm)->context.id, 0); 0; })
179b3901d54SCatalin Marinas 
18039bc88e5SCatalin Marinas #ifdef CONFIG_ARM64_SW_TTBR0_PAN
18139bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
18239bc88e5SCatalin Marinas 				      struct mm_struct *mm)
18339bc88e5SCatalin Marinas {
1840adbdfdeSWill Deacon 	u64 ttbr;
1850adbdfdeSWill Deacon 
1860adbdfdeSWill Deacon 	if (!system_uses_ttbr0_pan())
1870adbdfdeSWill Deacon 		return;
1880adbdfdeSWill Deacon 
1890adbdfdeSWill Deacon 	if (mm == &init_mm)
1900adbdfdeSWill Deacon 		ttbr = __pa_symbol(empty_zero_page);
1910adbdfdeSWill Deacon 	else
1920adbdfdeSWill Deacon 		ttbr = virt_to_phys(mm->pgd) | ASID(mm) << 48;
1930adbdfdeSWill Deacon 
1946b88a32cSCatalin Marinas 	WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
19539bc88e5SCatalin Marinas }
19639bc88e5SCatalin Marinas #else
19739bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
19839bc88e5SCatalin Marinas 				      struct mm_struct *mm)
19939bc88e5SCatalin Marinas {
20039bc88e5SCatalin Marinas }
20139bc88e5SCatalin Marinas #endif
20239bc88e5SCatalin Marinas 
203d96cc49bSWill Deacon static inline void
204d96cc49bSWill Deacon enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
205d96cc49bSWill Deacon {
206d96cc49bSWill Deacon 	/*
207d96cc49bSWill Deacon 	 * We don't actually care about the ttbr0 mapping, so point it at the
208d96cc49bSWill Deacon 	 * zero page.
209d96cc49bSWill Deacon 	 */
210d96cc49bSWill Deacon 	update_saved_ttbr0(tsk, &init_mm);
211d96cc49bSWill Deacon }
212d96cc49bSWill Deacon 
21339bc88e5SCatalin Marinas static inline void __switch_mm(struct mm_struct *next)
214b3901d54SCatalin Marinas {
215b3901d54SCatalin Marinas 	unsigned int cpu = smp_processor_id();
216b3901d54SCatalin Marinas 
217e53f21bcSCatalin Marinas 	/*
218e53f21bcSCatalin Marinas 	 * init_mm.pgd does not contain any user mappings and it is always
219e53f21bcSCatalin Marinas 	 * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
220e53f21bcSCatalin Marinas 	 */
221e53f21bcSCatalin Marinas 	if (next == &init_mm) {
222e53f21bcSCatalin Marinas 		cpu_set_reserved_ttbr0();
223e53f21bcSCatalin Marinas 		return;
224e53f21bcSCatalin Marinas 	}
225e53f21bcSCatalin Marinas 
226c2775b2eSWill Deacon 	check_and_switch_context(next, cpu);
227b3901d54SCatalin Marinas }
228b3901d54SCatalin Marinas 
22939bc88e5SCatalin Marinas static inline void
23039bc88e5SCatalin Marinas switch_mm(struct mm_struct *prev, struct mm_struct *next,
23139bc88e5SCatalin Marinas 	  struct task_struct *tsk)
23239bc88e5SCatalin Marinas {
23339bc88e5SCatalin Marinas 	if (prev != next)
23439bc88e5SCatalin Marinas 		__switch_mm(next);
23539bc88e5SCatalin Marinas 
23639bc88e5SCatalin Marinas 	/*
23739bc88e5SCatalin Marinas 	 * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
23839bc88e5SCatalin Marinas 	 * value may have not been initialised yet (activate_mm caller) or the
23939bc88e5SCatalin Marinas 	 * ASID has changed since the last run (following the context switch
2400adbdfdeSWill Deacon 	 * of another thread of the same process).
24139bc88e5SCatalin Marinas 	 */
24239bc88e5SCatalin Marinas 	update_saved_ttbr0(tsk, next);
24339bc88e5SCatalin Marinas }
24439bc88e5SCatalin Marinas 
245b3901d54SCatalin Marinas #define deactivate_mm(tsk,mm)	do { } while (0)
24639bc88e5SCatalin Marinas #define activate_mm(prev,next)	switch_mm(prev, next, current)
247b3901d54SCatalin Marinas 
24813f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void);
2496b88a32cSCatalin Marinas void post_ttbr_update_workaround(void);
25013f417f3SSuzuki K Poulose 
25138fd94b0SChristopher Covington #endif /* !__ASSEMBLY__ */
25238fd94b0SChristopher Covington 
25338fd94b0SChristopher Covington #endif /* !__ASM_MMU_CONTEXT_H */
254