context.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #define NUM_USER_ASIDS ASID_FIRST_VERSION
  37. /* Get the ASIDBits supported by the current CPU */
  38. static u32 get_cpu_asid_bits(void)
  39. {
  40. u32 asid;
  41. int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
  42. ID_AA64MMFR0_ASID_SHIFT);
  43. switch (fld) {
  44. default:
  45. pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n",
  46. smp_processor_id(), fld);
  47. /* Fallthrough */
  48. case 0:
  49. asid = 8;
  50. break;
  51. case 2:
  52. asid = 16;
  53. }
  54. return asid;
  55. }
  56. /* Check if the current cpu's ASIDBits is compatible with asid_bits */
  57. void verify_cpu_asid_bits(void)
  58. {
  59. u32 asid = get_cpu_asid_bits();
  60. if (asid < asid_bits) {
  61. /*
  62. * We cannot decrease the ASID size at runtime, so panic if we support
  63. * fewer ASID bits than the boot CPU.
  64. */
  65. pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
  66. smp_processor_id(), asid, asid_bits);
  67. cpu_panic_kernel();
  68. }
  69. }
  70. static void flush_context(unsigned int cpu)
  71. {
  72. int i;
  73. u64 asid;
  74. /* Update the list of reserved ASIDs and the ASID bitmap. */
  75. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  76. /*
  77. * Ensure the generation bump is observed before we xchg the
  78. * active_asids.
  79. */
  80. smp_wmb();
  81. for_each_possible_cpu(i) {
  82. asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
  83. /*
  84. * If this CPU has already been through a
  85. * rollover, but hasn't run another task in
  86. * the meantime, we must preserve its reserved
  87. * ASID, as this is the only trace we have of
  88. * the process it is still running.
  89. */
  90. if (asid == 0)
  91. asid = per_cpu(reserved_asids, i);
  92. __set_bit(asid & ~ASID_MASK, asid_map);
  93. per_cpu(reserved_asids, i) = asid;
  94. }
  95. /* Queue a TLB invalidate and flush the I-cache if necessary. */
  96. cpumask_setall(&tlb_flush_pending);
  97. if (icache_is_aivivt())
  98. __flush_icache_all();
  99. }
  100. static bool check_update_reserved_asid(u64 asid, u64 newasid)
  101. {
  102. int cpu;
  103. bool hit = false;
  104. /*
  105. * Iterate over the set of reserved ASIDs looking for a match.
  106. * If we find one, then we can update our mm to use newasid
  107. * (i.e. the same ASID in the current generation) but we can't
  108. * exit the loop early, since we need to ensure that all copies
  109. * of the old ASID are updated to reflect the mm. Failure to do
  110. * so could result in us missing the reserved ASID in a future
  111. * generation.
  112. */
  113. for_each_possible_cpu(cpu) {
  114. if (per_cpu(reserved_asids, cpu) == asid) {
  115. hit = true;
  116. per_cpu(reserved_asids, cpu) = newasid;
  117. }
  118. }
  119. return hit;
  120. }
  121. static u64 new_context(struct mm_struct *mm, unsigned int cpu)
  122. {
  123. static u32 cur_idx = 1;
  124. u64 asid = atomic64_read(&mm->context.id);
  125. u64 generation = atomic64_read(&asid_generation);
  126. if (asid != 0) {
  127. u64 newasid = generation | (asid & ~ASID_MASK);
  128. /*
  129. * If our current ASID was active during a rollover, we
  130. * can continue to use it and this was just a false alarm.
  131. */
  132. if (check_update_reserved_asid(asid, newasid))
  133. return newasid;
  134. /*
  135. * We had a valid ASID in a previous life, so try to re-use
  136. * it if possible.
  137. */
  138. asid &= ~ASID_MASK;
  139. if (!__test_and_set_bit(asid, asid_map))
  140. return newasid;
  141. }
  142. /*
  143. * Allocate a free ASID. If we can't find one, take a note of the
  144. * currently active ASIDs and mark the TLBs as requiring flushes.
  145. * We always count from ASID #1, as we use ASID #0 when setting a
  146. * reserved TTBR0 for the init_mm.
  147. */
  148. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
  149. if (asid != NUM_USER_ASIDS)
  150. goto set_asid;
  151. /* We're out of ASIDs, so increment the global generation count */
  152. generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
  153. &asid_generation);
  154. flush_context(cpu);
  155. /* We have more ASIDs than CPUs, so this will always succeed */
  156. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  157. set_asid:
  158. __set_bit(asid, asid_map);
  159. cur_idx = asid;
  160. return asid | generation;
  161. }
  162. void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
  163. {
  164. unsigned long flags;
  165. u64 asid;
  166. asid = atomic64_read(&mm->context.id);
  167. /*
  168. * The memory ordering here is subtle. We rely on the control
  169. * dependency between the generation read and the update of
  170. * active_asids to ensure that we are synchronised with a
  171. * parallel rollover (i.e. this pairs with the smp_wmb() in
  172. * flush_context).
  173. */
  174. if (!((asid ^ atomic64_read(&asid_generation)) >> asid_bits)
  175. && atomic64_xchg_relaxed(&per_cpu(active_asids, cpu), asid))
  176. goto switch_mm_fastpath;
  177. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  178. /* Check that our ASID belongs to the current generation. */
  179. asid = atomic64_read(&mm->context.id);
  180. if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
  181. asid = new_context(mm, cpu);
  182. atomic64_set(&mm->context.id, asid);
  183. }
  184. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
  185. local_flush_tlb_all();
  186. atomic64_set(&per_cpu(active_asids, cpu), asid);
  187. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  188. switch_mm_fastpath:
  189. /*
  190. * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
  191. * emulating PAN.
  192. */
  193. if (!system_uses_ttbr0_pan())
  194. cpu_switch_mm(mm->pgd, mm);
  195. }
  196. static int asids_init(void)
  197. {
  198. asid_bits = get_cpu_asid_bits();
  199. /*
  200. * Expect allocation after rollover to fail if we don't have at least
  201. * one more ASID than CPUs. ASID #0 is reserved for init_mm.
  202. */
  203. WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
  204. atomic64_set(&asid_generation, ASID_FIRST_VERSION);
  205. asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map),
  206. GFP_KERNEL);
  207. if (!asid_map)
  208. panic("Failed to allocate bitmap for %lu ASIDs\n",
  209. NUM_USER_ASIDS);
  210. pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
  211. return 0;
  212. }
  213. early_initcall(asids_init);