context.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Based on arch/arm/mm/context.c
  3. *
  4. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <asm/cpufeature.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/smp.h>
  26. #include <asm/tlbflush.h>
  27. static u32 asid_bits;
  28. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  29. static atomic64_t asid_generation;
  30. static unsigned long *asid_map;
  31. static DEFINE_PER_CPU(atomic64_t, active_asids);
  32. static DEFINE_PER_CPU(u64, reserved_asids);
  33. static cpumask_t tlb_flush_pending;
  34. #define ASID_MASK (~GENMASK(asid_bits - 1, 0))
  35. #define ASID_FIRST_VERSION (1UL << asid_bits)
  36. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  37. #define NUM_USER_ASIDS (ASID_FIRST_VERSION >> 1)
  38. #define asid2idx(asid) (((asid) & ~ASID_MASK) >> 1)
  39. #define idx2asid(idx) (((idx) << 1) & ~ASID_MASK)
  40. #else
  41. #define NUM_USER_ASIDS (ASID_FIRST_VERSION)
  42. #define asid2idx(asid) ((asid) & ~ASID_MASK)
  43. #define idx2asid(idx) asid2idx(idx)
  44. #endif
  45. /* Get the ASIDBits supported by the current CPU */
  46. static u32 get_cpu_asid_bits(void)
  47. {
  48. u32 asid;
  49. int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
  50. ID_AA64MMFR0_ASID_SHIFT);
  51. switch (fld) {
  52. default:
  53. pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n",
  54. smp_processor_id(), fld);
  55. /* Fallthrough */
  56. case 0:
  57. asid = 8;
  58. break;
  59. case 2:
  60. asid = 16;
  61. }
  62. return asid;
  63. }
  64. /* Check if the current cpu's ASIDBits is compatible with asid_bits */
  65. void verify_cpu_asid_bits(void)
  66. {
  67. u32 asid = get_cpu_asid_bits();
  68. if (asid < asid_bits) {
  69. /*
  70. * We cannot decrease the ASID size at runtime, so panic if we support
  71. * fewer ASID bits than the boot CPU.
  72. */
  73. pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
  74. smp_processor_id(), asid, asid_bits);
  75. cpu_panic_kernel();
  76. }
  77. }
  78. static void flush_context(void)
  79. {
  80. int i;
  81. u64 asid;
  82. /* Update the list of reserved ASIDs and the ASID bitmap. */
  83. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  84. for_each_possible_cpu(i) {
  85. asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
  86. /*
  87. * If this CPU has already been through a
  88. * rollover, but hasn't run another task in
  89. * the meantime, we must preserve its reserved
  90. * ASID, as this is the only trace we have of
  91. * the process it is still running.
  92. */
  93. if (asid == 0)
  94. asid = per_cpu(reserved_asids, i);
  95. __set_bit(asid2idx(asid), asid_map);
  96. per_cpu(reserved_asids, i) = asid;
  97. }
  98. /*
  99. * Queue a TLB invalidation for each CPU to perform on next
  100. * context-switch
  101. */
  102. cpumask_setall(&tlb_flush_pending);
  103. }
  104. static bool check_update_reserved_asid(u64 asid, u64 newasid)
  105. {
  106. int cpu;
  107. bool hit = false;
  108. /*
  109. * Iterate over the set of reserved ASIDs looking for a match.
  110. * If we find one, then we can update our mm to use newasid
  111. * (i.e. the same ASID in the current generation) but we can't
  112. * exit the loop early, since we need to ensure that all copies
  113. * of the old ASID are updated to reflect the mm. Failure to do
  114. * so could result in us missing the reserved ASID in a future
  115. * generation.
  116. */
  117. for_each_possible_cpu(cpu) {
  118. if (per_cpu(reserved_asids, cpu) == asid) {
  119. hit = true;
  120. per_cpu(reserved_asids, cpu) = newasid;
  121. }
  122. }
  123. return hit;
  124. }
  125. static u64 new_context(struct mm_struct *mm)
  126. {
  127. static u32 cur_idx = 1;
  128. u64 asid = atomic64_read(&mm->context.id);
  129. u64 generation = atomic64_read(&asid_generation);
  130. if (asid != 0) {
  131. u64 newasid = generation | (asid & ~ASID_MASK);
  132. /*
  133. * If our current ASID was active during a rollover, we
  134. * can continue to use it and this was just a false alarm.
  135. */
  136. if (check_update_reserved_asid(asid, newasid))
  137. return newasid;
  138. /*
  139. * We had a valid ASID in a previous life, so try to re-use
  140. * it if possible.
  141. */
  142. if (!__test_and_set_bit(asid2idx(asid), asid_map))
  143. return newasid;
  144. }
  145. /*
  146. * Allocate a free ASID. If we can't find one, take a note of the
  147. * currently active ASIDs and mark the TLBs as requiring flushes. We
  148. * always count from ASID #2 (index 1), as we use ASID #0 when setting
  149. * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
  150. * pairs.
  151. */
  152. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
  153. if (asid != NUM_USER_ASIDS)
  154. goto set_asid;
  155. /* We're out of ASIDs, so increment the global generation count */
  156. generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
  157. &asid_generation);
  158. flush_context();
  159. /* We have more ASIDs than CPUs, so this will always succeed */
  160. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  161. set_asid:
  162. __set_bit(asid, asid_map);
  163. cur_idx = asid;
  164. return idx2asid(asid) | generation;
  165. }
  166. void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
  167. {
  168. unsigned long flags;
  169. u64 asid, old_active_asid;
  170. if (system_supports_cnp())
  171. cpu_set_reserved_ttbr0();
  172. asid = atomic64_read(&mm->context.id);
  173. /*
  174. * The memory ordering here is subtle.
  175. * If our active_asids is non-zero and the ASID matches the current
  176. * generation, then we update the active_asids entry with a relaxed
  177. * cmpxchg. Racing with a concurrent rollover means that either:
  178. *
  179. * - We get a zero back from the cmpxchg and end up waiting on the
  180. * lock. Taking the lock synchronises with the rollover and so
  181. * we are forced to see the updated generation.
  182. *
  183. * - We get a valid ASID back from the cmpxchg, which means the
  184. * relaxed xchg in flush_context will treat us as reserved
  185. * because atomic RmWs are totally ordered for a given location.
  186. */
  187. old_active_asid = atomic64_read(&per_cpu(active_asids, cpu));
  188. if (old_active_asid &&
  189. !((asid ^ atomic64_read(&asid_generation)) >> asid_bits) &&
  190. atomic64_cmpxchg_relaxed(&per_cpu(active_asids, cpu),
  191. old_active_asid, asid))
  192. goto switch_mm_fastpath;
  193. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  194. /* Check that our ASID belongs to the current generation. */
  195. asid = atomic64_read(&mm->context.id);
  196. if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
  197. asid = new_context(mm);
  198. atomic64_set(&mm->context.id, asid);
  199. }
  200. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
  201. local_flush_tlb_all();
  202. atomic64_set(&per_cpu(active_asids, cpu), asid);
  203. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  204. switch_mm_fastpath:
  205. arm64_apply_bp_hardening();
  206. /*
  207. * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
  208. * emulating PAN.
  209. */
  210. if (!system_uses_ttbr0_pan())
  211. cpu_switch_mm(mm->pgd, mm);
  212. }
  213. /* Errata workaround post TTBRx_EL1 update. */
  214. asmlinkage void post_ttbr_update_workaround(void)
  215. {
  216. asm(ALTERNATIVE("nop; nop; nop",
  217. "ic iallu; dsb nsh; isb",
  218. ARM64_WORKAROUND_CAVIUM_27456,
  219. CONFIG_CAVIUM_ERRATUM_27456));
  220. }
  221. static int asids_init(void)
  222. {
  223. asid_bits = get_cpu_asid_bits();
  224. /*
  225. * Expect allocation after rollover to fail if we don't have at least
  226. * one more ASID than CPUs. ASID #0 is reserved for init_mm.
  227. */
  228. WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
  229. atomic64_set(&asid_generation, ASID_FIRST_VERSION);
  230. asid_map = kcalloc(BITS_TO_LONGS(NUM_USER_ASIDS), sizeof(*asid_map),
  231. GFP_KERNEL);
  232. if (!asid_map)
  233. panic("Failed to allocate bitmap for %lu ASIDs\n",
  234. NUM_USER_ASIDS);
  235. pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
  236. return 0;
  237. }
  238. early_initcall(asids_init);