context.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mm/context.c
  4. *
  5. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  6. * Copyright (C) 2012 ARM Limited
  7. *
  8. * Author: Will Deacon <[email protected]>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/percpu.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/smp_plat.h>
  17. #include <asm/thread_notify.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/proc-fns.h>
  20. /*
  21. * On ARMv6, we have the following structure in the Context ID:
  22. *
  23. * 31 7 0
  24. * +-------------------------+-----------+
  25. * | process ID | ASID |
  26. * +-------------------------+-----------+
  27. * | context ID |
  28. * +-------------------------------------+
  29. *
  30. * The ASID is used to tag entries in the CPU caches and TLBs.
  31. * The context ID is used by debuggers and trace logic, and
  32. * should be unique within all running processes.
  33. *
  34. * In big endian operation, the two 32 bit words are swapped if accessed
  35. * by non-64-bit operations.
  36. */
  37. #define ASID_FIRST_VERSION (1ULL << ASID_BITS)
  38. #define NUM_USER_ASIDS ASID_FIRST_VERSION
  39. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  40. static atomic64_t asid_generation = ATOMIC64_INIT(ASID_FIRST_VERSION);
  41. static DECLARE_BITMAP(asid_map, NUM_USER_ASIDS);
  42. static DEFINE_PER_CPU(atomic64_t, active_asids);
  43. static DEFINE_PER_CPU(u64, reserved_asids);
  44. static cpumask_t tlb_flush_pending;
  45. #ifdef CONFIG_ARM_ERRATA_798181
  46. void a15_erratum_get_cpumask(int this_cpu, struct mm_struct *mm,
  47. cpumask_t *mask)
  48. {
  49. int cpu;
  50. unsigned long flags;
  51. u64 context_id, asid;
  52. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  53. context_id = mm->context.id.counter;
  54. for_each_online_cpu(cpu) {
  55. if (cpu == this_cpu)
  56. continue;
  57. /*
  58. * We only need to send an IPI if the other CPUs are
  59. * running the same ASID as the one being invalidated.
  60. */
  61. asid = per_cpu(active_asids, cpu).counter;
  62. if (asid == 0)
  63. asid = per_cpu(reserved_asids, cpu);
  64. if (context_id == asid)
  65. cpumask_set_cpu(cpu, mask);
  66. }
  67. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  68. }
  69. #endif
  70. #ifdef CONFIG_ARM_LPAE
  71. /*
  72. * With LPAE, the ASID and page tables are updated atomicly, so there is
  73. * no need for a reserved set of tables (the active ASID tracking prevents
  74. * any issues across a rollover).
  75. */
  76. #define cpu_set_reserved_ttbr0()
  77. #else
  78. static void cpu_set_reserved_ttbr0(void)
  79. {
  80. u32 ttb;
  81. /*
  82. * Copy TTBR1 into TTBR0.
  83. * This points at swapper_pg_dir, which contains only global
  84. * entries so any speculative walks are perfectly safe.
  85. */
  86. asm volatile(
  87. " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
  88. " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
  89. : "=r" (ttb));
  90. isb();
  91. }
  92. #endif
  93. #ifdef CONFIG_PID_IN_CONTEXTIDR
  94. static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
  95. void *t)
  96. {
  97. u32 contextidr;
  98. pid_t pid;
  99. struct thread_info *thread = t;
  100. if (cmd != THREAD_NOTIFY_SWITCH)
  101. return NOTIFY_DONE;
  102. pid = task_pid_nr(thread_task(thread)) << ASID_BITS;
  103. asm volatile(
  104. " mrc p15, 0, %0, c13, c0, 1\n"
  105. " and %0, %0, %2\n"
  106. " orr %0, %0, %1\n"
  107. " mcr p15, 0, %0, c13, c0, 1\n"
  108. : "=r" (contextidr), "+r" (pid)
  109. : "I" (~ASID_MASK));
  110. isb();
  111. return NOTIFY_OK;
  112. }
  113. static struct notifier_block contextidr_notifier_block = {
  114. .notifier_call = contextidr_notifier,
  115. };
  116. static int __init contextidr_notifier_init(void)
  117. {
  118. return thread_register_notifier(&contextidr_notifier_block);
  119. }
  120. arch_initcall(contextidr_notifier_init);
  121. #endif
  122. static void flush_context(unsigned int cpu)
  123. {
  124. int i;
  125. u64 asid;
  126. /* Update the list of reserved ASIDs and the ASID bitmap. */
  127. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  128. for_each_possible_cpu(i) {
  129. asid = atomic64_xchg(&per_cpu(active_asids, i), 0);
  130. /*
  131. * If this CPU has already been through a
  132. * rollover, but hasn't run another task in
  133. * the meantime, we must preserve its reserved
  134. * ASID, as this is the only trace we have of
  135. * the process it is still running.
  136. */
  137. if (asid == 0)
  138. asid = per_cpu(reserved_asids, i);
  139. __set_bit(asid & ~ASID_MASK, asid_map);
  140. per_cpu(reserved_asids, i) = asid;
  141. }
  142. /* Queue a TLB invalidate and flush the I-cache if necessary. */
  143. cpumask_setall(&tlb_flush_pending);
  144. if (icache_is_vivt_asid_tagged())
  145. __flush_icache_all();
  146. }
  147. static bool check_update_reserved_asid(u64 asid, u64 newasid)
  148. {
  149. int cpu;
  150. bool hit = false;
  151. /*
  152. * Iterate over the set of reserved ASIDs looking for a match.
  153. * If we find one, then we can update our mm to use newasid
  154. * (i.e. the same ASID in the current generation) but we can't
  155. * exit the loop early, since we need to ensure that all copies
  156. * of the old ASID are updated to reflect the mm. Failure to do
  157. * so could result in us missing the reserved ASID in a future
  158. * generation.
  159. */
  160. for_each_possible_cpu(cpu) {
  161. if (per_cpu(reserved_asids, cpu) == asid) {
  162. hit = true;
  163. per_cpu(reserved_asids, cpu) = newasid;
  164. }
  165. }
  166. return hit;
  167. }
  168. static u64 new_context(struct mm_struct *mm, unsigned int cpu)
  169. {
  170. static u32 cur_idx = 1;
  171. u64 asid = atomic64_read(&mm->context.id);
  172. u64 generation = atomic64_read(&asid_generation);
  173. if (asid != 0) {
  174. u64 newasid = generation | (asid & ~ASID_MASK);
  175. /*
  176. * If our current ASID was active during a rollover, we
  177. * can continue to use it and this was just a false alarm.
  178. */
  179. if (check_update_reserved_asid(asid, newasid))
  180. return newasid;
  181. /*
  182. * We had a valid ASID in a previous life, so try to re-use
  183. * it if possible.,
  184. */
  185. asid &= ~ASID_MASK;
  186. if (!__test_and_set_bit(asid, asid_map))
  187. return newasid;
  188. }
  189. /*
  190. * Allocate a free ASID. If we can't find one, take a note of the
  191. * currently active ASIDs and mark the TLBs as requiring flushes.
  192. * We always count from ASID #1, as we reserve ASID #0 to switch
  193. * via TTBR0 and to avoid speculative page table walks from hitting
  194. * in any partial walk caches, which could be populated from
  195. * overlapping level-1 descriptors used to map both the module
  196. * area and the userspace stack.
  197. */
  198. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
  199. if (asid == NUM_USER_ASIDS) {
  200. generation = atomic64_add_return(ASID_FIRST_VERSION,
  201. &asid_generation);
  202. flush_context(cpu);
  203. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  204. }
  205. __set_bit(asid, asid_map);
  206. cur_idx = asid;
  207. cpumask_clear(mm_cpumask(mm));
  208. return asid | generation;
  209. }
  210. void check_and_switch_context(struct mm_struct *mm, struct task_struct *tsk)
  211. {
  212. unsigned long flags;
  213. unsigned int cpu = smp_processor_id();
  214. u64 asid;
  215. check_vmalloc_seq(mm);
  216. /*
  217. * We cannot update the pgd and the ASID atomicly with classic
  218. * MMU, so switch exclusively to global mappings to avoid
  219. * speculative page table walking with the wrong TTBR.
  220. */
  221. cpu_set_reserved_ttbr0();
  222. asid = atomic64_read(&mm->context.id);
  223. if (!((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS)
  224. && atomic64_xchg(&per_cpu(active_asids, cpu), asid))
  225. goto switch_mm_fastpath;
  226. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  227. /* Check that our ASID belongs to the current generation. */
  228. asid = atomic64_read(&mm->context.id);
  229. if ((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS) {
  230. asid = new_context(mm, cpu);
  231. atomic64_set(&mm->context.id, asid);
  232. }
  233. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) {
  234. local_flush_bp_all();
  235. local_flush_tlb_all();
  236. }
  237. atomic64_set(&per_cpu(active_asids, cpu), asid);
  238. cpumask_set_cpu(cpu, mm_cpumask(mm));
  239. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  240. switch_mm_fastpath:
  241. cpu_switch_mm(mm->pgd, mm);
  242. }