xref: /openbmc/linux/arch/arm64/mm/context.c (revision 25b92693)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b3901d54SCatalin Marinas /*
3b3901d54SCatalin Marinas  * Based on arch/arm/mm/context.c
4b3901d54SCatalin Marinas  *
5b3901d54SCatalin Marinas  * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
6b3901d54SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
7b3901d54SCatalin Marinas  */
8b3901d54SCatalin Marinas 
925b92693SMark Rutland #include <linux/bitfield.h>
105aec715dSWill Deacon #include <linux/bitops.h>
11b3901d54SCatalin Marinas #include <linux/sched.h>
125aec715dSWill Deacon #include <linux/slab.h>
13b3901d54SCatalin Marinas #include <linux/mm.h>
14b3901d54SCatalin Marinas 
155aec715dSWill Deacon #include <asm/cpufeature.h>
16b3901d54SCatalin Marinas #include <asm/mmu_context.h>
1713f417f3SSuzuki K Poulose #include <asm/smp.h>
18b3901d54SCatalin Marinas #include <asm/tlbflush.h>
19b3901d54SCatalin Marinas 
205aec715dSWill Deacon static u32 asid_bits;
21b3901d54SCatalin Marinas static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
225aec715dSWill Deacon 
235aec715dSWill Deacon static atomic64_t asid_generation;
245aec715dSWill Deacon static unsigned long *asid_map;
255aec715dSWill Deacon 
265aec715dSWill Deacon static DEFINE_PER_CPU(atomic64_t, active_asids);
275aec715dSWill Deacon static DEFINE_PER_CPU(u64, reserved_asids);
285aec715dSWill Deacon static cpumask_t tlb_flush_pending;
295aec715dSWill Deacon 
305aec715dSWill Deacon #define ASID_MASK		(~GENMASK(asid_bits - 1, 0))
315aec715dSWill Deacon #define ASID_FIRST_VERSION	(1UL << asid_bits)
320c8ea531SWill Deacon 
33f88f42f8SVladimir Murzin #define NUM_USER_ASIDS		ASID_FIRST_VERSION
340c8ea531SWill Deacon #define asid2idx(asid)		((asid) & ~ASID_MASK)
350c8ea531SWill Deacon #define idx2asid(idx)		asid2idx(idx)
365aec715dSWill Deacon 
37038dc9c6SSuzuki K Poulose /* Get the ASIDBits supported by the current CPU */
38038dc9c6SSuzuki K Poulose static u32 get_cpu_asid_bits(void)
39038dc9c6SSuzuki K Poulose {
40038dc9c6SSuzuki K Poulose 	u32 asid;
411cc6ed90SMark Rutland 	int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
42038dc9c6SSuzuki K Poulose 						ID_AA64MMFR0_ASID_SHIFT);
43038dc9c6SSuzuki K Poulose 
44038dc9c6SSuzuki K Poulose 	switch (fld) {
45038dc9c6SSuzuki K Poulose 	default:
46038dc9c6SSuzuki K Poulose 		pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n",
47038dc9c6SSuzuki K Poulose 					smp_processor_id(),  fld);
48038dc9c6SSuzuki K Poulose 		/* Fallthrough */
49038dc9c6SSuzuki K Poulose 	case 0:
50038dc9c6SSuzuki K Poulose 		asid = 8;
51038dc9c6SSuzuki K Poulose 		break;
52038dc9c6SSuzuki K Poulose 	case 2:
53038dc9c6SSuzuki K Poulose 		asid = 16;
54038dc9c6SSuzuki K Poulose 	}
55038dc9c6SSuzuki K Poulose 
56038dc9c6SSuzuki K Poulose 	return asid;
57038dc9c6SSuzuki K Poulose }
58038dc9c6SSuzuki K Poulose 
5913f417f3SSuzuki K Poulose /* Check if the current cpu's ASIDBits is compatible with asid_bits */
6013f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void)
6113f417f3SSuzuki K Poulose {
6213f417f3SSuzuki K Poulose 	u32 asid = get_cpu_asid_bits();
6313f417f3SSuzuki K Poulose 
6413f417f3SSuzuki K Poulose 	if (asid < asid_bits) {
6513f417f3SSuzuki K Poulose 		/*
6613f417f3SSuzuki K Poulose 		 * We cannot decrease the ASID size at runtime, so panic if we support
6713f417f3SSuzuki K Poulose 		 * fewer ASID bits than the boot CPU.
6813f417f3SSuzuki K Poulose 		 */
6913f417f3SSuzuki K Poulose 		pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
7013f417f3SSuzuki K Poulose 				smp_processor_id(), asid, asid_bits);
7117eebd1aSSuzuki K Poulose 		cpu_panic_kernel();
7213f417f3SSuzuki K Poulose 	}
7313f417f3SSuzuki K Poulose }
7413f417f3SSuzuki K Poulose 
75f88f42f8SVladimir Murzin static void set_kpti_asid_bits(void)
76f88f42f8SVladimir Murzin {
77f88f42f8SVladimir Murzin 	unsigned int len = BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(unsigned long);
78f88f42f8SVladimir Murzin 	/*
79f88f42f8SVladimir Murzin 	 * In case of KPTI kernel/user ASIDs are allocated in
80f88f42f8SVladimir Murzin 	 * pairs, the bottom bit distinguishes the two: if it
81f88f42f8SVladimir Murzin 	 * is set, then the ASID will map only userspace. Thus
82f88f42f8SVladimir Murzin 	 * mark even as reserved for kernel.
83f88f42f8SVladimir Murzin 	 */
84f88f42f8SVladimir Murzin 	memset(asid_map, 0xaa, len);
85f88f42f8SVladimir Murzin }
86f88f42f8SVladimir Murzin 
87f88f42f8SVladimir Murzin static void set_reserved_asid_bits(void)
88f88f42f8SVladimir Murzin {
89f88f42f8SVladimir Murzin 	if (arm64_kernel_unmapped_at_el0())
90f88f42f8SVladimir Murzin 		set_kpti_asid_bits();
91f88f42f8SVladimir Murzin 	else
92f88f42f8SVladimir Murzin 		bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
93f88f42f8SVladimir Murzin }
94f88f42f8SVladimir Murzin 
95742fafa5SShaokun Zhang static void flush_context(void)
965aec715dSWill Deacon {
975aec715dSWill Deacon 	int i;
985aec715dSWill Deacon 	u64 asid;
995aec715dSWill Deacon 
1005aec715dSWill Deacon 	/* Update the list of reserved ASIDs and the ASID bitmap. */
101f88f42f8SVladimir Murzin 	set_reserved_asid_bits();
102b3901d54SCatalin Marinas 
1035aec715dSWill Deacon 	for_each_possible_cpu(i) {
1045aec715dSWill Deacon 		asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
1055aec715dSWill Deacon 		/*
1065aec715dSWill Deacon 		 * If this CPU has already been through a
1075aec715dSWill Deacon 		 * rollover, but hasn't run another task in
1085aec715dSWill Deacon 		 * the meantime, we must preserve its reserved
1095aec715dSWill Deacon 		 * ASID, as this is the only trace we have of
1105aec715dSWill Deacon 		 * the process it is still running.
1115aec715dSWill Deacon 		 */
1125aec715dSWill Deacon 		if (asid == 0)
1135aec715dSWill Deacon 			asid = per_cpu(reserved_asids, i);
1140c8ea531SWill Deacon 		__set_bit(asid2idx(asid), asid_map);
1155aec715dSWill Deacon 		per_cpu(reserved_asids, i) = asid;
116b3901d54SCatalin Marinas 	}
117b3901d54SCatalin Marinas 
118f81a3487SMark Rutland 	/*
119f81a3487SMark Rutland 	 * Queue a TLB invalidation for each CPU to perform on next
120f81a3487SMark Rutland 	 * context-switch
121f81a3487SMark Rutland 	 */
1225aec715dSWill Deacon 	cpumask_setall(&tlb_flush_pending);
123b3901d54SCatalin Marinas }
124b3901d54SCatalin Marinas 
1250ebea808SWill Deacon static bool check_update_reserved_asid(u64 asid, u64 newasid)
1265aec715dSWill Deacon {
1275aec715dSWill Deacon 	int cpu;
1280ebea808SWill Deacon 	bool hit = false;
1290ebea808SWill Deacon 
1300ebea808SWill Deacon 	/*
1310ebea808SWill Deacon 	 * Iterate over the set of reserved ASIDs looking for a match.
1320ebea808SWill Deacon 	 * If we find one, then we can update our mm to use newasid
1330ebea808SWill Deacon 	 * (i.e. the same ASID in the current generation) but we can't
1340ebea808SWill Deacon 	 * exit the loop early, since we need to ensure that all copies
1350ebea808SWill Deacon 	 * of the old ASID are updated to reflect the mm. Failure to do
1360ebea808SWill Deacon 	 * so could result in us missing the reserved ASID in a future
1370ebea808SWill Deacon 	 * generation.
1380ebea808SWill Deacon 	 */
1390ebea808SWill Deacon 	for_each_possible_cpu(cpu) {
1400ebea808SWill Deacon 		if (per_cpu(reserved_asids, cpu) == asid) {
1410ebea808SWill Deacon 			hit = true;
1420ebea808SWill Deacon 			per_cpu(reserved_asids, cpu) = newasid;
1430ebea808SWill Deacon 		}
1440ebea808SWill Deacon 	}
1450ebea808SWill Deacon 
1460ebea808SWill Deacon 	return hit;
1475aec715dSWill Deacon }
1485aec715dSWill Deacon 
149742fafa5SShaokun Zhang static u64 new_context(struct mm_struct *mm)
1505aec715dSWill Deacon {
1515aec715dSWill Deacon 	static u32 cur_idx = 1;
1525aec715dSWill Deacon 	u64 asid = atomic64_read(&mm->context.id);
1535aec715dSWill Deacon 	u64 generation = atomic64_read(&asid_generation);
1545aec715dSWill Deacon 
1555aec715dSWill Deacon 	if (asid != 0) {
1560ebea808SWill Deacon 		u64 newasid = generation | (asid & ~ASID_MASK);
1570ebea808SWill Deacon 
1585aec715dSWill Deacon 		/*
1595aec715dSWill Deacon 		 * If our current ASID was active during a rollover, we
1605aec715dSWill Deacon 		 * can continue to use it and this was just a false alarm.
1615aec715dSWill Deacon 		 */
1620ebea808SWill Deacon 		if (check_update_reserved_asid(asid, newasid))
1630ebea808SWill Deacon 			return newasid;
1645aec715dSWill Deacon 
1655aec715dSWill Deacon 		/*
1665aec715dSWill Deacon 		 * We had a valid ASID in a previous life, so try to re-use
1675aec715dSWill Deacon 		 * it if possible.
1685aec715dSWill Deacon 		 */
1690c8ea531SWill Deacon 		if (!__test_and_set_bit(asid2idx(asid), asid_map))
1700ebea808SWill Deacon 			return newasid;
1715aec715dSWill Deacon 	}
1725aec715dSWill Deacon 
1735aec715dSWill Deacon 	/*
1745aec715dSWill Deacon 	 * Allocate a free ASID. If we can't find one, take a note of the
1750c8ea531SWill Deacon 	 * currently active ASIDs and mark the TLBs as requiring flushes.  We
1760c8ea531SWill Deacon 	 * always count from ASID #2 (index 1), as we use ASID #0 when setting
1770c8ea531SWill Deacon 	 * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
1780c8ea531SWill Deacon 	 * pairs.
1795aec715dSWill Deacon 	 */
1805aec715dSWill Deacon 	asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
1815aec715dSWill Deacon 	if (asid != NUM_USER_ASIDS)
1825aec715dSWill Deacon 		goto set_asid;
1835aec715dSWill Deacon 
1845aec715dSWill Deacon 	/* We're out of ASIDs, so increment the global generation count */
1855aec715dSWill Deacon 	generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
1865aec715dSWill Deacon 						 &asid_generation);
187742fafa5SShaokun Zhang 	flush_context();
1885aec715dSWill Deacon 
189f7e0efc9SJean-Philippe Brucker 	/* We have more ASIDs than CPUs, so this will always succeed */
1905aec715dSWill Deacon 	asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
1915aec715dSWill Deacon 
1925aec715dSWill Deacon set_asid:
1935aec715dSWill Deacon 	__set_bit(asid, asid_map);
1945aec715dSWill Deacon 	cur_idx = asid;
1950c8ea531SWill Deacon 	return idx2asid(asid) | generation;
1965aec715dSWill Deacon }
1975aec715dSWill Deacon 
1985aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
199b3901d54SCatalin Marinas {
200b3901d54SCatalin Marinas 	unsigned long flags;
201a8ffaaa0SCatalin Marinas 	u64 asid, old_active_asid;
2025aec715dSWill Deacon 
2035ffdfaedSVladimir Murzin 	if (system_supports_cnp())
2045ffdfaedSVladimir Murzin 		cpu_set_reserved_ttbr0();
2055ffdfaedSVladimir Murzin 
2065aec715dSWill Deacon 	asid = atomic64_read(&mm->context.id);
207b3901d54SCatalin Marinas 
208b3901d54SCatalin Marinas 	/*
2093a33c760SWill Deacon 	 * The memory ordering here is subtle.
210a8ffaaa0SCatalin Marinas 	 * If our active_asids is non-zero and the ASID matches the current
211a8ffaaa0SCatalin Marinas 	 * generation, then we update the active_asids entry with a relaxed
212a8ffaaa0SCatalin Marinas 	 * cmpxchg. Racing with a concurrent rollover means that either:
2133a33c760SWill Deacon 	 *
214a8ffaaa0SCatalin Marinas 	 * - We get a zero back from the cmpxchg and end up waiting on the
2153a33c760SWill Deacon 	 *   lock. Taking the lock synchronises with the rollover and so
2163a33c760SWill Deacon 	 *   we are forced to see the updated generation.
2173a33c760SWill Deacon 	 *
218a8ffaaa0SCatalin Marinas 	 * - We get a valid ASID back from the cmpxchg, which means the
2193a33c760SWill Deacon 	 *   relaxed xchg in flush_context will treat us as reserved
2203a33c760SWill Deacon 	 *   because atomic RmWs are totally ordered for a given location.
221b3901d54SCatalin Marinas 	 */
222a8ffaaa0SCatalin Marinas 	old_active_asid = atomic64_read(&per_cpu(active_asids, cpu));
223a8ffaaa0SCatalin Marinas 	if (old_active_asid &&
224a8ffaaa0SCatalin Marinas 	    !((asid ^ atomic64_read(&asid_generation)) >> asid_bits) &&
225a8ffaaa0SCatalin Marinas 	    atomic64_cmpxchg_relaxed(&per_cpu(active_asids, cpu),
226a8ffaaa0SCatalin Marinas 				     old_active_asid, asid))
2275aec715dSWill Deacon 		goto switch_mm_fastpath;
228b3901d54SCatalin Marinas 
2295aec715dSWill Deacon 	raw_spin_lock_irqsave(&cpu_asid_lock, flags);
2305aec715dSWill Deacon 	/* Check that our ASID belongs to the current generation. */
2315aec715dSWill Deacon 	asid = atomic64_read(&mm->context.id);
2325aec715dSWill Deacon 	if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
233742fafa5SShaokun Zhang 		asid = new_context(mm);
2345aec715dSWill Deacon 		atomic64_set(&mm->context.id, asid);
235b3901d54SCatalin Marinas 	}
236b3901d54SCatalin Marinas 
2375aec715dSWill Deacon 	if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
2385aec715dSWill Deacon 		local_flush_tlb_all();
239b3901d54SCatalin Marinas 
2405aec715dSWill Deacon 	atomic64_set(&per_cpu(active_asids, cpu), asid);
2415aec715dSWill Deacon 	raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
242565630d5SCatalin Marinas 
2435aec715dSWill Deacon switch_mm_fastpath:
244a8e4c0a9SMarc Zyngier 
245a8e4c0a9SMarc Zyngier 	arm64_apply_bp_hardening();
246a8e4c0a9SMarc Zyngier 
24739bc88e5SCatalin Marinas 	/*
24839bc88e5SCatalin Marinas 	 * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
24939bc88e5SCatalin Marinas 	 * emulating PAN.
25039bc88e5SCatalin Marinas 	 */
25139bc88e5SCatalin Marinas 	if (!system_uses_ttbr0_pan())
252b3901d54SCatalin Marinas 		cpu_switch_mm(mm->pgd, mm);
253b3901d54SCatalin Marinas }
254b3901d54SCatalin Marinas 
25595e3de35SMarc Zyngier /* Errata workaround post TTBRx_EL1 update. */
25695e3de35SMarc Zyngier asmlinkage void post_ttbr_update_workaround(void)
25795e3de35SMarc Zyngier {
25825b92693SMark Rutland 	if (!IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456))
25925b92693SMark Rutland 		return;
26025b92693SMark Rutland 
26195e3de35SMarc Zyngier 	asm(ALTERNATIVE("nop; nop; nop",
26295e3de35SMarc Zyngier 			"ic iallu; dsb nsh; isb",
26325b92693SMark Rutland 			ARM64_WORKAROUND_CAVIUM_27456));
26425b92693SMark Rutland }
26525b92693SMark Rutland 
26625b92693SMark Rutland void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm)
26725b92693SMark Rutland {
26825b92693SMark Rutland 	unsigned long ttbr1 = read_sysreg(ttbr1_el1);
26925b92693SMark Rutland 	unsigned long asid = ASID(mm);
27025b92693SMark Rutland 	unsigned long ttbr0 = phys_to_ttbr(pgd_phys);
27125b92693SMark Rutland 
27225b92693SMark Rutland 	/* Skip CNP for the reserved ASID */
27325b92693SMark Rutland 	if (system_supports_cnp() && asid)
27425b92693SMark Rutland 		ttbr0 |= TTBR_CNP_BIT;
27525b92693SMark Rutland 
27625b92693SMark Rutland 	/* SW PAN needs a copy of the ASID in TTBR0 for entry */
27725b92693SMark Rutland 	if (IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN))
27825b92693SMark Rutland 		ttbr0 |= FIELD_PREP(TTBR_ASID_MASK, asid);
27925b92693SMark Rutland 
28025b92693SMark Rutland 	/* Set ASID in TTBR1 since TCR.A1 is set */
28125b92693SMark Rutland 	ttbr1 &= ~TTBR_ASID_MASK;
28225b92693SMark Rutland 	ttbr1 |= FIELD_PREP(TTBR_ASID_MASK, asid);
28325b92693SMark Rutland 
28425b92693SMark Rutland 	write_sysreg(ttbr1, ttbr1_el1);
28525b92693SMark Rutland 	isb();
28625b92693SMark Rutland 	write_sysreg(ttbr0, ttbr0_el1);
28725b92693SMark Rutland 	isb();
28825b92693SMark Rutland 	post_ttbr_update_workaround();
28995e3de35SMarc Zyngier }
29095e3de35SMarc Zyngier 
2915aec715dSWill Deacon static int asids_init(void)
292b3901d54SCatalin Marinas {
293038dc9c6SSuzuki K Poulose 	asid_bits = get_cpu_asid_bits();
294f7e0efc9SJean-Philippe Brucker 	/*
295f7e0efc9SJean-Philippe Brucker 	 * Expect allocation after rollover to fail if we don't have at least
296f7e0efc9SJean-Philippe Brucker 	 * one more ASID than CPUs. ASID #0 is reserved for init_mm.
297f7e0efc9SJean-Philippe Brucker 	 */
298f7e0efc9SJean-Philippe Brucker 	WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
2995aec715dSWill Deacon 	atomic64_set(&asid_generation, ASID_FIRST_VERSION);
3006396bb22SKees Cook 	asid_map = kcalloc(BITS_TO_LONGS(NUM_USER_ASIDS), sizeof(*asid_map),
3015aec715dSWill Deacon 			   GFP_KERNEL);
3025aec715dSWill Deacon 	if (!asid_map)
3035aec715dSWill Deacon 		panic("Failed to allocate bitmap for %lu ASIDs\n",
3045aec715dSWill Deacon 		      NUM_USER_ASIDS);
3055aec715dSWill Deacon 
306f88f42f8SVladimir Murzin 	/*
307f88f42f8SVladimir Murzin 	 * We cannot call set_reserved_asid_bits() here because CPU
308f88f42f8SVladimir Murzin 	 * caps are not finalized yet, so it is safer to assume KPTI
309f88f42f8SVladimir Murzin 	 * and reserve kernel ASID's from beginning.
310f88f42f8SVladimir Murzin 	 */
311f88f42f8SVladimir Murzin 	if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0))
312f88f42f8SVladimir Murzin 		set_kpti_asid_bits();
313f88f42f8SVladimir Murzin 
3145aec715dSWill Deacon 	pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
3155aec715dSWill Deacon 	return 0;
316b3901d54SCatalin Marinas }
3175aec715dSWill Deacon early_initcall(asids_init);
318