xref: /linux/arch/arm64/include/asm/mmu_context.h (revision 69ebc018)
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>
1765fddcfcSMike Rapoport #include <linux/pgtable.h>
18b3901d54SCatalin Marinas 
19b3901d54SCatalin Marinas #include <asm/cacheflush.h>
2039bc88e5SCatalin Marinas #include <asm/cpufeature.h>
21a8bf2fc4SMark Brown #include <asm/daifflags.h>
22b3901d54SCatalin Marinas #include <asm/proc-fns.h>
23*69ebc018SCatalin Marinas #include <asm-generic/mm_hooks.h>
24b3901d54SCatalin Marinas #include <asm/cputype.h>
25adf75899SMark Rutland #include <asm/sysreg.h>
269e8e865bSMark Rutland #include <asm/tlbflush.h>
27b3901d54SCatalin Marinas 
28c55191e9SArd Biesheuvel extern bool rodata_full;
29c55191e9SArd Biesheuvel 
contextidr_thread_switch(struct task_struct * next)30ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
31ec45d1cfSWill Deacon {
32d3ea42aaSMark Rutland 	if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
33d3ea42aaSMark Rutland 		return;
34d3ea42aaSMark Rutland 
35adf75899SMark Rutland 	write_sysreg(task_pid_nr(next), contextidr_el1);
36adf75899SMark Rutland 	isb();
37ec45d1cfSWill Deacon }
38ec45d1cfSWill Deacon 
39b3901d54SCatalin Marinas /*
40833be850SMark Rutland  * Set TTBR0 to reserved_pg_dir. No translations will be possible via TTBR0.
41b3901d54SCatalin Marinas  */
cpu_set_reserved_ttbr0_nosync(void)42b9293d45SJamie Iles static inline void cpu_set_reserved_ttbr0_nosync(void)
43b3901d54SCatalin Marinas {
44833be850SMark Rutland 	unsigned long ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
45b3901d54SCatalin Marinas 
46adf75899SMark Rutland 	write_sysreg(ttbr, ttbr0_el1);
47b9293d45SJamie Iles }
48b9293d45SJamie Iles 
cpu_set_reserved_ttbr0(void)49b9293d45SJamie Iles static inline void cpu_set_reserved_ttbr0(void)
50b9293d45SJamie Iles {
51b9293d45SJamie Iles 	cpu_set_reserved_ttbr0_nosync();
52adf75899SMark Rutland 	isb();
53b3901d54SCatalin Marinas }
54b3901d54SCatalin Marinas 
5525b92693SMark Rutland void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
5625b92693SMark Rutland 
cpu_switch_mm(pgd_t * pgd,struct mm_struct * mm)577655abb9SWill Deacon static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
587655abb9SWill Deacon {
597655abb9SWill Deacon 	BUG_ON(pgd == swapper_pg_dir);
607655abb9SWill Deacon 	cpu_do_switch_mm(virt_to_phys(pgd),mm);
617655abb9SWill Deacon }
627655abb9SWill Deacon 
63dd006da2SArd Biesheuvel /*
6484b04d3eSArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active.
65dd006da2SArd Biesheuvel  */
6684b04d3eSArd Biesheuvel #define idmap_t0sz	TCR_T0SZ(IDMAP_VA_BITS)
67dd006da2SArd Biesheuvel 
68fa2a8445SKristina Martsenko /*
691401bef7SJames Morse  * Ensure TCR.T0SZ is set to the provided value.
70c51e97d8SWill Deacon  */
__cpu_set_tcr_t0sz(unsigned long t0sz)71609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
72dd006da2SArd Biesheuvel {
731401bef7SJames Morse 	unsigned long tcr = read_sysreg(tcr_el1);
74dd006da2SArd Biesheuvel 
751401bef7SJames Morse 	if ((tcr & TCR_T0SZ_MASK) >> TCR_T0SZ_OFFSET == t0sz)
76c51e97d8SWill Deacon 		return;
77c51e97d8SWill Deacon 
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 
845383cc6eSSteve Capper #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(vabits_actual))
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  */
cpu_uninstall_idmap(void)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 
10739bc88e5SCatalin Marinas 	if (mm != &init_mm && !system_uses_ttbr0_pan())
1089e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1099e8e865bSMark Rutland }
1109e8e865bSMark Rutland 
cpu_install_idmap(void)111e0f92f0dSArd Biesheuvel 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 
117e0f92f0dSArd Biesheuvel 	cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
118609116d2SMark Rutland }
119609116d2SMark Rutland 
1209e8e865bSMark Rutland /*
121a347f601SPasha Tatashin  * Load our new page tables. A strict BBM approach requires that we ensure that
122a347f601SPasha Tatashin  * TLBs are free of any entries that may overlap with the global mappings we are
123a347f601SPasha Tatashin  * about to install.
124a347f601SPasha Tatashin  *
125a347f601SPasha Tatashin  * For a real hibernate/resume/kexec cycle TTBR0 currently points to a zero
126a347f601SPasha Tatashin  * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI runtime
127a347f601SPasha Tatashin  * services), while for a userspace-driven test_resume cycle it points to
128a347f601SPasha Tatashin  * userspace page tables (and we must point it at a zero page ourselves).
129a347f601SPasha Tatashin  *
130a347f601SPasha Tatashin  * We change T0SZ as part of installing the idmap. This is undone by
131a347f601SPasha Tatashin  * cpu_uninstall_idmap() in __cpu_suspend_exit().
132a347f601SPasha Tatashin  */
cpu_install_ttbr0(phys_addr_t ttbr0,unsigned long t0sz)133a347f601SPasha Tatashin static inline void cpu_install_ttbr0(phys_addr_t ttbr0, unsigned long t0sz)
134a347f601SPasha Tatashin {
135a347f601SPasha Tatashin 	cpu_set_reserved_ttbr0();
136a347f601SPasha Tatashin 	local_flush_tlb_all();
137a347f601SPasha Tatashin 	__cpu_set_tcr_t0sz(t0sz);
138a347f601SPasha Tatashin 
139a347f601SPasha Tatashin 	/* avoid cpu_switch_mm() and its SW-PAN and CNP interactions */
140a347f601SPasha Tatashin 	write_sysreg(ttbr0, ttbr0_el1);
141a347f601SPasha Tatashin 	isb();
142a347f601SPasha Tatashin }
143a347f601SPasha Tatashin 
144e0f92f0dSArd Biesheuvel void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp);
14550e1881dSMark Rutland 
cpu_enable_swapper_cnp(void)14654c8818aSMark Rutland static inline void cpu_enable_swapper_cnp(void)
14754c8818aSMark Rutland {
148e0f92f0dSArd Biesheuvel 	__cpu_replace_ttbr1(lm_alias(swapper_pg_dir), true);
14954c8818aSMark Rutland }
15054c8818aSMark Rutland 
cpu_replace_ttbr1(pgd_t * pgdp)151e0f92f0dSArd Biesheuvel static inline void cpu_replace_ttbr1(pgd_t *pgdp)
15254c8818aSMark Rutland {
15354c8818aSMark Rutland 	/*
15454c8818aSMark Rutland 	 * Only for early TTBR1 replacement before cpucaps are finalized and
15554c8818aSMark Rutland 	 * before we've decided whether to use CNP.
15654c8818aSMark Rutland 	 */
15754c8818aSMark Rutland 	WARN_ON(system_capabilities_finalized());
158e0f92f0dSArd Biesheuvel 	__cpu_replace_ttbr1(pgdp, false);
15954c8818aSMark Rutland }
16054c8818aSMark Rutland 
16150e1881dSMark Rutland /*
1625aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1635aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1645aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1655aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1665aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1675aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1685aec715dSWill Deacon  * take CPU migration into account.
169b3901d54SCatalin Marinas  */
170c4885bbbSPingfan Liu void check_and_switch_context(struct mm_struct *mm);
171b3901d54SCatalin Marinas 
172d98295d3SNicholas Piggin #define init_new_context(tsk, mm) init_new_context(tsk, mm)
17348118151SJean-Philippe Brucker static inline int
init_new_context(struct task_struct * tsk,struct mm_struct * mm)17448118151SJean-Philippe Brucker init_new_context(struct task_struct *tsk, struct mm_struct *mm)
17548118151SJean-Philippe Brucker {
17648118151SJean-Philippe Brucker 	atomic64_set(&mm->context.id, 0);
17748118151SJean-Philippe Brucker 	refcount_set(&mm->context.pinned, 0);
17848118151SJean-Philippe Brucker 	return 0;
17948118151SJean-Philippe Brucker }
180b3901d54SCatalin Marinas 
18139bc88e5SCatalin Marinas #ifdef CONFIG_ARM64_SW_TTBR0_PAN
update_saved_ttbr0(struct task_struct * tsk,struct mm_struct * mm)18239bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
18339bc88e5SCatalin Marinas 				      struct mm_struct *mm)
18439bc88e5SCatalin Marinas {
1850adbdfdeSWill Deacon 	u64 ttbr;
1860adbdfdeSWill Deacon 
1870adbdfdeSWill Deacon 	if (!system_uses_ttbr0_pan())
1880adbdfdeSWill Deacon 		return;
1890adbdfdeSWill Deacon 
1900adbdfdeSWill Deacon 	if (mm == &init_mm)
1919163f011SAnshuman Khandual 		ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
1920adbdfdeSWill Deacon 	else
1939163f011SAnshuman Khandual 		ttbr = phys_to_ttbr(virt_to_phys(mm->pgd)) | ASID(mm) << 48;
1940adbdfdeSWill Deacon 
1956b88a32cSCatalin Marinas 	WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
19639bc88e5SCatalin Marinas }
19739bc88e5SCatalin Marinas #else
update_saved_ttbr0(struct task_struct * tsk,struct mm_struct * mm)19839bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
19939bc88e5SCatalin Marinas 				      struct mm_struct *mm)
20039bc88e5SCatalin Marinas {
20139bc88e5SCatalin Marinas }
20239bc88e5SCatalin Marinas #endif
20339bc88e5SCatalin Marinas 
204d98295d3SNicholas Piggin #define enter_lazy_tlb enter_lazy_tlb
205d96cc49bSWill Deacon static inline void
enter_lazy_tlb(struct mm_struct * mm,struct task_struct * tsk)206d96cc49bSWill Deacon enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
207d96cc49bSWill Deacon {
208d96cc49bSWill Deacon 	/*
209d96cc49bSWill Deacon 	 * We don't actually care about the ttbr0 mapping, so point it at the
210d96cc49bSWill Deacon 	 * zero page.
211d96cc49bSWill Deacon 	 */
212d96cc49bSWill Deacon 	update_saved_ttbr0(tsk, &init_mm);
213d96cc49bSWill Deacon }
214d96cc49bSWill Deacon 
__switch_mm(struct mm_struct * next)21539bc88e5SCatalin Marinas static inline void __switch_mm(struct mm_struct *next)
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 
226c4885bbbSPingfan Liu 	check_and_switch_context(next);
227b3901d54SCatalin Marinas }
228b3901d54SCatalin Marinas 
22939bc88e5SCatalin Marinas static inline void
switch_mm(struct mm_struct * prev,struct mm_struct * next,struct task_struct * tsk)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 
245d82158faSWill Deacon static inline const struct cpumask *
task_cpu_possible_mask(struct task_struct * p)246d82158faSWill Deacon task_cpu_possible_mask(struct task_struct *p)
247d82158faSWill Deacon {
248d82158faSWill Deacon 	if (!static_branch_unlikely(&arm64_mismatched_32bit_el0))
249d82158faSWill Deacon 		return cpu_possible_mask;
250d82158faSWill Deacon 
251d82158faSWill Deacon 	if (!is_compat_thread(task_thread_info(p)))
252d82158faSWill Deacon 		return cpu_possible_mask;
253d82158faSWill Deacon 
254d82158faSWill Deacon 	return system_32bit_el0_cpumask();
255d82158faSWill Deacon }
256d82158faSWill Deacon #define task_cpu_possible_mask	task_cpu_possible_mask
257d82158faSWill Deacon 
25813f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void);
2596b88a32cSCatalin Marinas void post_ttbr_update_workaround(void);
26013f417f3SSuzuki K Poulose 
26148118151SJean-Philippe Brucker unsigned long arm64_mm_context_get(struct mm_struct *mm);
26248118151SJean-Philippe Brucker void arm64_mm_context_put(struct mm_struct *mm);
26348118151SJean-Philippe Brucker 
264f7d30434SKirill A. Shutemov #define mm_untag_mask mm_untag_mask
mm_untag_mask(struct mm_struct * mm)265f7d30434SKirill A. Shutemov static inline unsigned long mm_untag_mask(struct mm_struct *mm)
266f7d30434SKirill A. Shutemov {
267f7d30434SKirill A. Shutemov 	return -1UL >> 8;
268f7d30434SKirill A. Shutemov }
269f7d30434SKirill A. Shutemov 
270d98295d3SNicholas Piggin #include <asm-generic/mmu_context.h>
271d98295d3SNicholas Piggin 
27238fd94b0SChristopher Covington #endif /* !__ASSEMBLY__ */
27338fd94b0SChristopher Covington 
27438fd94b0SChristopher Covington #endif /* !__ASM_MMU_CONTEXT_H */
275