context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/mm/context.c
  4. *
  5. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/bitops.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/mm.h>
  13. #include <asm/cpufeature.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/smp.h>
  16. #include <asm/tlbflush.h>
  17. static u32 asid_bits;
  18. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  19. static atomic64_t asid_generation;
  20. static unsigned long *asid_map;
  21. static DEFINE_PER_CPU(atomic64_t, active_asids);
  22. static DEFINE_PER_CPU(u64, reserved_asids);
  23. static cpumask_t tlb_flush_pending;
  24. static unsigned long max_pinned_asids;
  25. static unsigned long nr_pinned_asids;
  26. static unsigned long *pinned_asid_map;
  27. #define ASID_MASK (~GENMASK(asid_bits - 1, 0))
  28. #define ASID_FIRST_VERSION (1UL << asid_bits)
  29. #define NUM_USER_ASIDS ASID_FIRST_VERSION
  30. #define ctxid2asid(asid) ((asid) & ~ASID_MASK)
  31. #define asid2ctxid(asid, genid) ((asid) | (genid))
  32. /* Get the ASIDBits supported by the current CPU */
  33. static u32 get_cpu_asid_bits(void)
  34. {
  35. u32 asid;
  36. int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
  37. ID_AA64MMFR0_EL1_ASIDBITS_SHIFT);
  38. switch (fld) {
  39. default:
  40. pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n",
  41. smp_processor_id(), fld);
  42. fallthrough;
  43. case ID_AA64MMFR0_EL1_ASIDBITS_8:
  44. asid = 8;
  45. break;
  46. case ID_AA64MMFR0_EL1_ASIDBITS_16:
  47. asid = 16;
  48. }
  49. return asid;
  50. }
  51. /* Check if the current cpu's ASIDBits is compatible with asid_bits */
  52. void verify_cpu_asid_bits(void)
  53. {
  54. u32 asid = get_cpu_asid_bits();
  55. if (asid < asid_bits) {
  56. /*
  57. * We cannot decrease the ASID size at runtime, so panic if we support
  58. * fewer ASID bits than the boot CPU.
  59. */
  60. pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
  61. smp_processor_id(), asid, asid_bits);
  62. cpu_panic_kernel();
  63. }
  64. }
  65. static void set_kpti_asid_bits(unsigned long *map)
  66. {
  67. unsigned int len = BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(unsigned long);
  68. /*
  69. * In case of KPTI kernel/user ASIDs are allocated in
  70. * pairs, the bottom bit distinguishes the two: if it
  71. * is set, then the ASID will map only userspace. Thus
  72. * mark even as reserved for kernel.
  73. */
  74. memset(map, 0xaa, len);
  75. }
  76. static void set_reserved_asid_bits(void)
  77. {
  78. if (pinned_asid_map)
  79. bitmap_copy(asid_map, pinned_asid_map, NUM_USER_ASIDS);
  80. else if (arm64_kernel_unmapped_at_el0())
  81. set_kpti_asid_bits(asid_map);
  82. else
  83. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  84. }
  85. #define asid_gen_match(asid) \
  86. (!(((asid) ^ atomic64_read(&asid_generation)) >> asid_bits))
  87. static void flush_context(void)
  88. {
  89. int i;
  90. u64 asid;
  91. /* Update the list of reserved ASIDs and the ASID bitmap. */
  92. set_reserved_asid_bits();
  93. for_each_possible_cpu(i) {
  94. asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
  95. /*
  96. * If this CPU has already been through a
  97. * rollover, but hasn't run another task in
  98. * the meantime, we must preserve its reserved
  99. * ASID, as this is the only trace we have of
  100. * the process it is still running.
  101. */
  102. if (asid == 0)
  103. asid = per_cpu(reserved_asids, i);
  104. __set_bit(ctxid2asid(asid), asid_map);
  105. per_cpu(reserved_asids, i) = asid;
  106. }
  107. /*
  108. * Queue a TLB invalidation for each CPU to perform on next
  109. * context-switch
  110. */
  111. cpumask_setall(&tlb_flush_pending);
  112. }
  113. static bool check_update_reserved_asid(u64 asid, u64 newasid)
  114. {
  115. int cpu;
  116. bool hit = false;
  117. /*
  118. * Iterate over the set of reserved ASIDs looking for a match.
  119. * If we find one, then we can update our mm to use newasid
  120. * (i.e. the same ASID in the current generation) but we can't
  121. * exit the loop early, since we need to ensure that all copies
  122. * of the old ASID are updated to reflect the mm. Failure to do
  123. * so could result in us missing the reserved ASID in a future
  124. * generation.
  125. */
  126. for_each_possible_cpu(cpu) {
  127. if (per_cpu(reserved_asids, cpu) == asid) {
  128. hit = true;
  129. per_cpu(reserved_asids, cpu) = newasid;
  130. }
  131. }
  132. return hit;
  133. }
  134. static u64 new_context(struct mm_struct *mm)
  135. {
  136. static u32 cur_idx = 1;
  137. u64 asid = atomic64_read(&mm->context.id);
  138. u64 generation = atomic64_read(&asid_generation);
  139. if (asid != 0) {
  140. u64 newasid = asid2ctxid(ctxid2asid(asid), generation);
  141. /*
  142. * If our current ASID was active during a rollover, we
  143. * can continue to use it and this was just a false alarm.
  144. */
  145. if (check_update_reserved_asid(asid, newasid))
  146. return newasid;
  147. /*
  148. * If it is pinned, we can keep using it. Note that reserved
  149. * takes priority, because even if it is also pinned, we need to
  150. * update the generation into the reserved_asids.
  151. */
  152. if (refcount_read(&mm->context.pinned))
  153. return newasid;
  154. /*
  155. * We had a valid ASID in a previous life, so try to re-use
  156. * it if possible.
  157. */
  158. if (!__test_and_set_bit(ctxid2asid(asid), asid_map))
  159. return newasid;
  160. }
  161. /*
  162. * Allocate a free ASID. If we can't find one, take a note of the
  163. * currently active ASIDs and mark the TLBs as requiring flushes. We
  164. * always count from ASID #2 (index 1), as we use ASID #0 when setting
  165. * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
  166. * pairs.
  167. */
  168. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
  169. if (asid != NUM_USER_ASIDS)
  170. goto set_asid;
  171. /* We're out of ASIDs, so increment the global generation count */
  172. generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
  173. &asid_generation);
  174. flush_context();
  175. /* We have more ASIDs than CPUs, so this will always succeed */
  176. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  177. set_asid:
  178. __set_bit(asid, asid_map);
  179. cur_idx = asid;
  180. return asid2ctxid(asid, generation);
  181. }
  182. void check_and_switch_context(struct mm_struct *mm)
  183. {
  184. unsigned long flags;
  185. unsigned int cpu;
  186. u64 asid, old_active_asid;
  187. if (system_supports_cnp())
  188. cpu_set_reserved_ttbr0();
  189. asid = atomic64_read(&mm->context.id);
  190. /*
  191. * The memory ordering here is subtle.
  192. * If our active_asids is non-zero and the ASID matches the current
  193. * generation, then we update the active_asids entry with a relaxed
  194. * cmpxchg. Racing with a concurrent rollover means that either:
  195. *
  196. * - We get a zero back from the cmpxchg and end up waiting on the
  197. * lock. Taking the lock synchronises with the rollover and so
  198. * we are forced to see the updated generation.
  199. *
  200. * - We get a valid ASID back from the cmpxchg, which means the
  201. * relaxed xchg in flush_context will treat us as reserved
  202. * because atomic RmWs are totally ordered for a given location.
  203. */
  204. old_active_asid = atomic64_read(this_cpu_ptr(&active_asids));
  205. if (old_active_asid && asid_gen_match(asid) &&
  206. atomic64_cmpxchg_relaxed(this_cpu_ptr(&active_asids),
  207. old_active_asid, asid))
  208. goto switch_mm_fastpath;
  209. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  210. /* Check that our ASID belongs to the current generation. */
  211. asid = atomic64_read(&mm->context.id);
  212. if (!asid_gen_match(asid)) {
  213. asid = new_context(mm);
  214. atomic64_set(&mm->context.id, asid);
  215. }
  216. cpu = smp_processor_id();
  217. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
  218. local_flush_tlb_all();
  219. atomic64_set(this_cpu_ptr(&active_asids), asid);
  220. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  221. switch_mm_fastpath:
  222. arm64_apply_bp_hardening();
  223. /*
  224. * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
  225. * emulating PAN.
  226. */
  227. if (!system_uses_ttbr0_pan())
  228. cpu_switch_mm(mm->pgd, mm);
  229. }
  230. unsigned long arm64_mm_context_get(struct mm_struct *mm)
  231. {
  232. unsigned long flags;
  233. u64 asid;
  234. if (!pinned_asid_map)
  235. return 0;
  236. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  237. asid = atomic64_read(&mm->context.id);
  238. if (refcount_inc_not_zero(&mm->context.pinned))
  239. goto out_unlock;
  240. if (nr_pinned_asids >= max_pinned_asids) {
  241. asid = 0;
  242. goto out_unlock;
  243. }
  244. if (!asid_gen_match(asid)) {
  245. /*
  246. * We went through one or more rollover since that ASID was
  247. * used. Ensure that it is still valid, or generate a new one.
  248. */
  249. asid = new_context(mm);
  250. atomic64_set(&mm->context.id, asid);
  251. }
  252. nr_pinned_asids++;
  253. __set_bit(ctxid2asid(asid), pinned_asid_map);
  254. refcount_set(&mm->context.pinned, 1);
  255. out_unlock:
  256. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  257. asid = ctxid2asid(asid);
  258. /* Set the equivalent of USER_ASID_BIT */
  259. if (asid && arm64_kernel_unmapped_at_el0())
  260. asid |= 1;
  261. return asid;
  262. }
  263. EXPORT_SYMBOL_GPL(arm64_mm_context_get);
  264. void arm64_mm_context_put(struct mm_struct *mm)
  265. {
  266. unsigned long flags;
  267. u64 asid = atomic64_read(&mm->context.id);
  268. if (!pinned_asid_map)
  269. return;
  270. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  271. if (refcount_dec_and_test(&mm->context.pinned)) {
  272. __clear_bit(ctxid2asid(asid), pinned_asid_map);
  273. nr_pinned_asids--;
  274. }
  275. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  276. }
  277. EXPORT_SYMBOL_GPL(arm64_mm_context_put);
  278. /* Errata workaround post TTBRx_EL1 update. */
  279. asmlinkage void post_ttbr_update_workaround(void)
  280. {
  281. if (!IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456))
  282. return;
  283. asm(ALTERNATIVE("nop; nop; nop",
  284. "ic iallu; dsb nsh; isb",
  285. ARM64_WORKAROUND_CAVIUM_27456));
  286. }
  287. void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm)
  288. {
  289. unsigned long ttbr1 = read_sysreg(ttbr1_el1);
  290. unsigned long asid = ASID(mm);
  291. unsigned long ttbr0 = phys_to_ttbr(pgd_phys);
  292. /* Skip CNP for the reserved ASID */
  293. if (system_supports_cnp() && asid)
  294. ttbr0 |= TTBR_CNP_BIT;
  295. /* SW PAN needs a copy of the ASID in TTBR0 for entry */
  296. if (IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN))
  297. ttbr0 |= FIELD_PREP(TTBR_ASID_MASK, asid);
  298. /* Set ASID in TTBR1 since TCR.A1 is set */
  299. ttbr1 &= ~TTBR_ASID_MASK;
  300. ttbr1 |= FIELD_PREP(TTBR_ASID_MASK, asid);
  301. write_sysreg(ttbr1, ttbr1_el1);
  302. isb();
  303. write_sysreg(ttbr0, ttbr0_el1);
  304. isb();
  305. post_ttbr_update_workaround();
  306. }
  307. static int asids_update_limit(void)
  308. {
  309. unsigned long num_available_asids = NUM_USER_ASIDS;
  310. if (arm64_kernel_unmapped_at_el0()) {
  311. num_available_asids /= 2;
  312. if (pinned_asid_map)
  313. set_kpti_asid_bits(pinned_asid_map);
  314. }
  315. /*
  316. * Expect allocation after rollover to fail if we don't have at least
  317. * one more ASID than CPUs. ASID #0 is reserved for init_mm.
  318. */
  319. WARN_ON(num_available_asids - 1 <= num_possible_cpus());
  320. pr_info("ASID allocator initialised with %lu entries\n",
  321. num_available_asids);
  322. /*
  323. * There must always be an ASID available after rollover. Ensure that,
  324. * even if all CPUs have a reserved ASID and the maximum number of ASIDs
  325. * are pinned, there still is at least one empty slot in the ASID map.
  326. */
  327. max_pinned_asids = num_available_asids - num_possible_cpus() - 2;
  328. return 0;
  329. }
  330. arch_initcall(asids_update_limit);
  331. static int asids_init(void)
  332. {
  333. asid_bits = get_cpu_asid_bits();
  334. atomic64_set(&asid_generation, ASID_FIRST_VERSION);
  335. asid_map = bitmap_zalloc(NUM_USER_ASIDS, GFP_KERNEL);
  336. if (!asid_map)
  337. panic("Failed to allocate bitmap for %lu ASIDs\n",
  338. NUM_USER_ASIDS);
  339. pinned_asid_map = bitmap_zalloc(NUM_USER_ASIDS, GFP_KERNEL);
  340. nr_pinned_asids = 0;
  341. /*
  342. * We cannot call set_reserved_asid_bits() here because CPU
  343. * caps are not finalized yet, so it is safer to assume KPTI
  344. * and reserve kernel ASID's from beginning.
  345. */
  346. if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0))
  347. set_kpti_asid_bits(asid_map);
  348. return 0;
  349. }
  350. early_initcall(asids_init);