context.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(unsigned int cpu)
  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, unsigned int cpu)
  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(cpu);
  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. asid = atomic64_read(&mm->context.id);
  171. /*
  172. * The memory ordering here is subtle.
  173. * If our active_asids is non-zero and the ASID matches the current
  174. * generation, then we update the active_asids entry with a relaxed
  175. * cmpxchg. Racing with a concurrent rollover means that either:
  176. *
  177. * - We get a zero back from the cmpxchg and end up waiting on the
  178. * lock. Taking the lock synchronises with the rollover and so
  179. * we are forced to see the updated generation.
  180. *
  181. * - We get a valid ASID back from the cmpxchg, which means the
  182. * relaxed xchg in flush_context will treat us as reserved
  183. * because atomic RmWs are totally ordered for a given location.
  184. */
  185. old_active_asid = atomic64_read(&per_cpu(active_asids, cpu));
  186. if (old_active_asid &&
  187. !((asid ^ atomic64_read(&asid_generation)) >> asid_bits) &&
  188. atomic64_cmpxchg_relaxed(&per_cpu(active_asids, cpu),
  189. old_active_asid, asid))
  190. goto switch_mm_fastpath;
  191. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  192. /* Check that our ASID belongs to the current generation. */
  193. asid = atomic64_read(&mm->context.id);
  194. if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
  195. asid = new_context(mm, cpu);
  196. atomic64_set(&mm->context.id, asid);
  197. }
  198. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
  199. local_flush_tlb_all();
  200. atomic64_set(&per_cpu(active_asids, cpu), asid);
  201. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  202. switch_mm_fastpath:
  203. arm64_apply_bp_hardening();
  204. /*
  205. * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
  206. * emulating PAN.
  207. */
  208. if (!system_uses_ttbr0_pan())
  209. cpu_switch_mm(mm->pgd, mm);
  210. }
  211. /* Errata workaround post TTBRx_EL1 update. */
  212. asmlinkage void post_ttbr_update_workaround(void)
  213. {
  214. asm(ALTERNATIVE("nop; nop; nop",
  215. "ic iallu; dsb nsh; isb",
  216. ARM64_WORKAROUND_CAVIUM_27456,
  217. CONFIG_CAVIUM_ERRATUM_27456));
  218. }
  219. static int asids_init(void)
  220. {
  221. asid_bits = get_cpu_asid_bits();
  222. /*
  223. * Expect allocation after rollover to fail if we don't have at least
  224. * one more ASID than CPUs. ASID #0 is reserved for init_mm.
  225. */
  226. WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
  227. atomic64_set(&asid_generation, ASID_FIRST_VERSION);
  228. asid_map = kcalloc(BITS_TO_LONGS(NUM_USER_ASIDS), sizeof(*asid_map),
  229. GFP_KERNEL);
  230. if (!asid_map)
  231. panic("Failed to allocate bitmap for %lu ASIDs\n",
  232. NUM_USER_ASIDS);
  233. pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
  234. return 0;
  235. }
  236. early_initcall(asids_init);