xref: /openbmc/linux/arch/arm64/mm/context.c (revision 4fc92254)
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 
954fc92254SJean-Philippe Brucker #define asid_gen_match(asid) \
964fc92254SJean-Philippe Brucker 	(!(((asid) ^ atomic64_read(&asid_generation)) >> asid_bits))
974fc92254SJean-Philippe Brucker 
98742fafa5SShaokun Zhang static void flush_context(void)
995aec715dSWill Deacon {
1005aec715dSWill Deacon 	int i;
1015aec715dSWill Deacon 	u64 asid;
1025aec715dSWill Deacon 
1035aec715dSWill Deacon 	/* Update the list of reserved ASIDs and the ASID bitmap. */
104f88f42f8SVladimir Murzin 	set_reserved_asid_bits();
105b3901d54SCatalin Marinas 
1065aec715dSWill Deacon 	for_each_possible_cpu(i) {
1075aec715dSWill Deacon 		asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
1085aec715dSWill Deacon 		/*
1095aec715dSWill Deacon 		 * If this CPU has already been through a
1105aec715dSWill Deacon 		 * rollover, but hasn't run another task in
1115aec715dSWill Deacon 		 * the meantime, we must preserve its reserved
1125aec715dSWill Deacon 		 * ASID, as this is the only trace we have of
1135aec715dSWill Deacon 		 * the process it is still running.
1145aec715dSWill Deacon 		 */
1155aec715dSWill Deacon 		if (asid == 0)
1165aec715dSWill Deacon 			asid = per_cpu(reserved_asids, i);
1170c8ea531SWill Deacon 		__set_bit(asid2idx(asid), asid_map);
1185aec715dSWill Deacon 		per_cpu(reserved_asids, i) = asid;
119b3901d54SCatalin Marinas 	}
120b3901d54SCatalin Marinas 
121f81a3487SMark Rutland 	/*
122f81a3487SMark Rutland 	 * Queue a TLB invalidation for each CPU to perform on next
123f81a3487SMark Rutland 	 * context-switch
124f81a3487SMark Rutland 	 */
1255aec715dSWill Deacon 	cpumask_setall(&tlb_flush_pending);
126b3901d54SCatalin Marinas }
127b3901d54SCatalin Marinas 
1280ebea808SWill Deacon static bool check_update_reserved_asid(u64 asid, u64 newasid)
1295aec715dSWill Deacon {
1305aec715dSWill Deacon 	int cpu;
1310ebea808SWill Deacon 	bool hit = false;
1320ebea808SWill Deacon 
1330ebea808SWill Deacon 	/*
1340ebea808SWill Deacon 	 * Iterate over the set of reserved ASIDs looking for a match.
1350ebea808SWill Deacon 	 * If we find one, then we can update our mm to use newasid
1360ebea808SWill Deacon 	 * (i.e. the same ASID in the current generation) but we can't
1370ebea808SWill Deacon 	 * exit the loop early, since we need to ensure that all copies
1380ebea808SWill Deacon 	 * of the old ASID are updated to reflect the mm. Failure to do
1390ebea808SWill Deacon 	 * so could result in us missing the reserved ASID in a future
1400ebea808SWill Deacon 	 * generation.
1410ebea808SWill Deacon 	 */
1420ebea808SWill Deacon 	for_each_possible_cpu(cpu) {
1430ebea808SWill Deacon 		if (per_cpu(reserved_asids, cpu) == asid) {
1440ebea808SWill Deacon 			hit = true;
1450ebea808SWill Deacon 			per_cpu(reserved_asids, cpu) = newasid;
1460ebea808SWill Deacon 		}
1470ebea808SWill Deacon 	}
1480ebea808SWill Deacon 
1490ebea808SWill Deacon 	return hit;
1505aec715dSWill Deacon }
1515aec715dSWill Deacon 
152742fafa5SShaokun Zhang static u64 new_context(struct mm_struct *mm)
1535aec715dSWill Deacon {
1545aec715dSWill Deacon 	static u32 cur_idx = 1;
1555aec715dSWill Deacon 	u64 asid = atomic64_read(&mm->context.id);
1565aec715dSWill Deacon 	u64 generation = atomic64_read(&asid_generation);
1575aec715dSWill Deacon 
1585aec715dSWill Deacon 	if (asid != 0) {
1590ebea808SWill Deacon 		u64 newasid = generation | (asid & ~ASID_MASK);
1600ebea808SWill Deacon 
1615aec715dSWill Deacon 		/*
1625aec715dSWill Deacon 		 * If our current ASID was active during a rollover, we
1635aec715dSWill Deacon 		 * can continue to use it and this was just a false alarm.
1645aec715dSWill Deacon 		 */
1650ebea808SWill Deacon 		if (check_update_reserved_asid(asid, newasid))
1660ebea808SWill Deacon 			return newasid;
1675aec715dSWill Deacon 
1685aec715dSWill Deacon 		/*
1695aec715dSWill Deacon 		 * We had a valid ASID in a previous life, so try to re-use
1705aec715dSWill Deacon 		 * it if possible.
1715aec715dSWill Deacon 		 */
1720c8ea531SWill Deacon 		if (!__test_and_set_bit(asid2idx(asid), asid_map))
1730ebea808SWill Deacon 			return newasid;
1745aec715dSWill Deacon 	}
1755aec715dSWill Deacon 
1765aec715dSWill Deacon 	/*
1775aec715dSWill Deacon 	 * Allocate a free ASID. If we can't find one, take a note of the
1780c8ea531SWill Deacon 	 * currently active ASIDs and mark the TLBs as requiring flushes.  We
1790c8ea531SWill Deacon 	 * always count from ASID #2 (index 1), as we use ASID #0 when setting
1800c8ea531SWill Deacon 	 * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
1810c8ea531SWill Deacon 	 * pairs.
1825aec715dSWill Deacon 	 */
1835aec715dSWill Deacon 	asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
1845aec715dSWill Deacon 	if (asid != NUM_USER_ASIDS)
1855aec715dSWill Deacon 		goto set_asid;
1865aec715dSWill Deacon 
1875aec715dSWill Deacon 	/* We're out of ASIDs, so increment the global generation count */
1885aec715dSWill Deacon 	generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
1895aec715dSWill Deacon 						 &asid_generation);
190742fafa5SShaokun Zhang 	flush_context();
1915aec715dSWill Deacon 
192f7e0efc9SJean-Philippe Brucker 	/* We have more ASIDs than CPUs, so this will always succeed */
1935aec715dSWill Deacon 	asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
1945aec715dSWill Deacon 
1955aec715dSWill Deacon set_asid:
1965aec715dSWill Deacon 	__set_bit(asid, asid_map);
1975aec715dSWill Deacon 	cur_idx = asid;
1980c8ea531SWill Deacon 	return idx2asid(asid) | generation;
1995aec715dSWill Deacon }
2005aec715dSWill Deacon 
2015aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
202b3901d54SCatalin Marinas {
203b3901d54SCatalin Marinas 	unsigned long flags;
204a8ffaaa0SCatalin Marinas 	u64 asid, old_active_asid;
2055aec715dSWill Deacon 
2065ffdfaedSVladimir Murzin 	if (system_supports_cnp())
2075ffdfaedSVladimir Murzin 		cpu_set_reserved_ttbr0();
2085ffdfaedSVladimir Murzin 
2095aec715dSWill Deacon 	asid = atomic64_read(&mm->context.id);
210b3901d54SCatalin Marinas 
211b3901d54SCatalin Marinas 	/*
2123a33c760SWill Deacon 	 * The memory ordering here is subtle.
213a8ffaaa0SCatalin Marinas 	 * If our active_asids is non-zero and the ASID matches the current
214a8ffaaa0SCatalin Marinas 	 * generation, then we update the active_asids entry with a relaxed
215a8ffaaa0SCatalin Marinas 	 * cmpxchg. Racing with a concurrent rollover means that either:
2163a33c760SWill Deacon 	 *
217a8ffaaa0SCatalin Marinas 	 * - We get a zero back from the cmpxchg and end up waiting on the
2183a33c760SWill Deacon 	 *   lock. Taking the lock synchronises with the rollover and so
2193a33c760SWill Deacon 	 *   we are forced to see the updated generation.
2203a33c760SWill Deacon 	 *
221a8ffaaa0SCatalin Marinas 	 * - We get a valid ASID back from the cmpxchg, which means the
2223a33c760SWill Deacon 	 *   relaxed xchg in flush_context will treat us as reserved
2233a33c760SWill Deacon 	 *   because atomic RmWs are totally ordered for a given location.
224b3901d54SCatalin Marinas 	 */
225a8ffaaa0SCatalin Marinas 	old_active_asid = atomic64_read(&per_cpu(active_asids, cpu));
2264fc92254SJean-Philippe Brucker 	if (old_active_asid && asid_gen_match(asid) &&
227a8ffaaa0SCatalin Marinas 	    atomic64_cmpxchg_relaxed(&per_cpu(active_asids, cpu),
228a8ffaaa0SCatalin Marinas 				     old_active_asid, asid))
2295aec715dSWill Deacon 		goto switch_mm_fastpath;
230b3901d54SCatalin Marinas 
2315aec715dSWill Deacon 	raw_spin_lock_irqsave(&cpu_asid_lock, flags);
2325aec715dSWill Deacon 	/* Check that our ASID belongs to the current generation. */
2335aec715dSWill Deacon 	asid = atomic64_read(&mm->context.id);
2344fc92254SJean-Philippe Brucker 	if (!asid_gen_match(asid)) {
235742fafa5SShaokun Zhang 		asid = new_context(mm);
2365aec715dSWill Deacon 		atomic64_set(&mm->context.id, asid);
237b3901d54SCatalin Marinas 	}
238b3901d54SCatalin Marinas 
2395aec715dSWill Deacon 	if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
2405aec715dSWill Deacon 		local_flush_tlb_all();
241b3901d54SCatalin Marinas 
2425aec715dSWill Deacon 	atomic64_set(&per_cpu(active_asids, cpu), asid);
2435aec715dSWill Deacon 	raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
244565630d5SCatalin Marinas 
2455aec715dSWill Deacon switch_mm_fastpath:
246a8e4c0a9SMarc Zyngier 
247a8e4c0a9SMarc Zyngier 	arm64_apply_bp_hardening();
248a8e4c0a9SMarc Zyngier 
24939bc88e5SCatalin Marinas 	/*
25039bc88e5SCatalin Marinas 	 * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
25139bc88e5SCatalin Marinas 	 * emulating PAN.
25239bc88e5SCatalin Marinas 	 */
25339bc88e5SCatalin Marinas 	if (!system_uses_ttbr0_pan())
254b3901d54SCatalin Marinas 		cpu_switch_mm(mm->pgd, mm);
255b3901d54SCatalin Marinas }
256b3901d54SCatalin Marinas 
25795e3de35SMarc Zyngier /* Errata workaround post TTBRx_EL1 update. */
25895e3de35SMarc Zyngier asmlinkage void post_ttbr_update_workaround(void)
25995e3de35SMarc Zyngier {
26025b92693SMark Rutland 	if (!IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456))
26125b92693SMark Rutland 		return;
26225b92693SMark Rutland 
26395e3de35SMarc Zyngier 	asm(ALTERNATIVE("nop; nop; nop",
26495e3de35SMarc Zyngier 			"ic iallu; dsb nsh; isb",
26525b92693SMark Rutland 			ARM64_WORKAROUND_CAVIUM_27456));
26625b92693SMark Rutland }
26725b92693SMark Rutland 
26825b92693SMark Rutland void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm)
26925b92693SMark Rutland {
27025b92693SMark Rutland 	unsigned long ttbr1 = read_sysreg(ttbr1_el1);
27125b92693SMark Rutland 	unsigned long asid = ASID(mm);
27225b92693SMark Rutland 	unsigned long ttbr0 = phys_to_ttbr(pgd_phys);
27325b92693SMark Rutland 
27425b92693SMark Rutland 	/* Skip CNP for the reserved ASID */
27525b92693SMark Rutland 	if (system_supports_cnp() && asid)
27625b92693SMark Rutland 		ttbr0 |= TTBR_CNP_BIT;
27725b92693SMark Rutland 
27825b92693SMark Rutland 	/* SW PAN needs a copy of the ASID in TTBR0 for entry */
27925b92693SMark Rutland 	if (IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN))
28025b92693SMark Rutland 		ttbr0 |= FIELD_PREP(TTBR_ASID_MASK, asid);
28125b92693SMark Rutland 
28225b92693SMark Rutland 	/* Set ASID in TTBR1 since TCR.A1 is set */
28325b92693SMark Rutland 	ttbr1 &= ~TTBR_ASID_MASK;
28425b92693SMark Rutland 	ttbr1 |= FIELD_PREP(TTBR_ASID_MASK, asid);
28525b92693SMark Rutland 
28625b92693SMark Rutland 	write_sysreg(ttbr1, ttbr1_el1);
28725b92693SMark Rutland 	isb();
28825b92693SMark Rutland 	write_sysreg(ttbr0, ttbr0_el1);
28925b92693SMark Rutland 	isb();
29025b92693SMark Rutland 	post_ttbr_update_workaround();
29195e3de35SMarc Zyngier }
29295e3de35SMarc Zyngier 
2939abd515aSJean-Philippe Brucker static int asids_update_limit(void)
294b3901d54SCatalin Marinas {
2959abd515aSJean-Philippe Brucker 	unsigned long num_available_asids = NUM_USER_ASIDS;
2969abd515aSJean-Philippe Brucker 
2979abd515aSJean-Philippe Brucker 	if (arm64_kernel_unmapped_at_el0())
2989abd515aSJean-Philippe Brucker 		num_available_asids /= 2;
299f7e0efc9SJean-Philippe Brucker 	/*
300f7e0efc9SJean-Philippe Brucker 	 * Expect allocation after rollover to fail if we don't have at least
301f7e0efc9SJean-Philippe Brucker 	 * one more ASID than CPUs. ASID #0 is reserved for init_mm.
302f7e0efc9SJean-Philippe Brucker 	 */
3039abd515aSJean-Philippe Brucker 	WARN_ON(num_available_asids - 1 <= num_possible_cpus());
3049abd515aSJean-Philippe Brucker 	pr_info("ASID allocator initialised with %lu entries\n",
3059abd515aSJean-Philippe Brucker 		num_available_asids);
3069abd515aSJean-Philippe Brucker 	return 0;
3079abd515aSJean-Philippe Brucker }
3089abd515aSJean-Philippe Brucker arch_initcall(asids_update_limit);
3099abd515aSJean-Philippe Brucker 
3109abd515aSJean-Philippe Brucker static int asids_init(void)
3119abd515aSJean-Philippe Brucker {
3129abd515aSJean-Philippe Brucker 	asid_bits = get_cpu_asid_bits();
3135aec715dSWill Deacon 	atomic64_set(&asid_generation, ASID_FIRST_VERSION);
3146396bb22SKees Cook 	asid_map = kcalloc(BITS_TO_LONGS(NUM_USER_ASIDS), sizeof(*asid_map),
3155aec715dSWill Deacon 			   GFP_KERNEL);
3165aec715dSWill Deacon 	if (!asid_map)
3175aec715dSWill Deacon 		panic("Failed to allocate bitmap for %lu ASIDs\n",
3185aec715dSWill Deacon 		      NUM_USER_ASIDS);
3195aec715dSWill Deacon 
320f88f42f8SVladimir Murzin 	/*
321f88f42f8SVladimir Murzin 	 * We cannot call set_reserved_asid_bits() here because CPU
322f88f42f8SVladimir Murzin 	 * caps are not finalized yet, so it is safer to assume KPTI
323f88f42f8SVladimir Murzin 	 * and reserve kernel ASID's from beginning.
324f88f42f8SVladimir Murzin 	 */
325f88f42f8SVladimir Murzin 	if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0))
326f88f42f8SVladimir Murzin 		set_kpti_asid_bits();
3275aec715dSWill Deacon 	return 0;
328b3901d54SCatalin Marinas }
3295aec715dSWill Deacon early_initcall(asids_init);
330