mmu_context.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Based on arch/arm/include/asm/mmu_context.h
  3. *
  4. * Copyright (C) 1996 Russell King.
  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. #ifndef __ASM_MMU_CONTEXT_H
  20. #define __ASM_MMU_CONTEXT_H
  21. #ifndef __ASSEMBLY__
  22. #include <linux/compiler.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/hotplug.h>
  25. #include <linux/mm_types.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/cpufeature.h>
  28. #include <asm/proc-fns.h>
  29. #include <asm-generic/mm_hooks.h>
  30. #include <asm/cputype.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/sysreg.h>
  33. #include <asm/tlbflush.h>
  34. static inline void contextidr_thread_switch(struct task_struct *next)
  35. {
  36. if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
  37. return;
  38. write_sysreg(task_pid_nr(next), contextidr_el1);
  39. isb();
  40. }
  41. /*
  42. * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
  43. */
  44. static inline void cpu_set_reserved_ttbr0(void)
  45. {
  46. unsigned long ttbr = phys_to_ttbr(__pa_symbol(empty_zero_page));
  47. write_sysreg(ttbr, ttbr0_el1);
  48. isb();
  49. }
  50. static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
  51. {
  52. BUG_ON(pgd == swapper_pg_dir);
  53. cpu_set_reserved_ttbr0();
  54. cpu_do_switch_mm(virt_to_phys(pgd),mm);
  55. }
  56. /*
  57. * TCR.T0SZ value to use when the ID map is active. Usually equals
  58. * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
  59. * physical memory, in which case it will be smaller.
  60. */
  61. extern u64 idmap_t0sz;
  62. extern u64 idmap_ptrs_per_pgd;
  63. static inline bool __cpu_uses_extended_idmap(void)
  64. {
  65. return unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS));
  66. }
  67. /*
  68. * True if the extended ID map requires an extra level of translation table
  69. * to be configured.
  70. */
  71. static inline bool __cpu_uses_extended_idmap_level(void)
  72. {
  73. return ARM64_HW_PGTABLE_LEVELS(64 - idmap_t0sz) > CONFIG_PGTABLE_LEVELS;
  74. }
  75. /*
  76. * Set TCR.T0SZ to its default value (based on VA_BITS)
  77. */
  78. static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
  79. {
  80. unsigned long tcr;
  81. if (!__cpu_uses_extended_idmap())
  82. return;
  83. tcr = read_sysreg(tcr_el1);
  84. tcr &= ~TCR_T0SZ_MASK;
  85. tcr |= t0sz << TCR_T0SZ_OFFSET;
  86. write_sysreg(tcr, tcr_el1);
  87. isb();
  88. }
  89. #define cpu_set_default_tcr_t0sz() __cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS))
  90. #define cpu_set_idmap_tcr_t0sz() __cpu_set_tcr_t0sz(idmap_t0sz)
  91. /*
  92. * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
  93. *
  94. * The idmap lives in the same VA range as userspace, but uses global entries
  95. * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
  96. * speculative TLB fetches, we must temporarily install the reserved page
  97. * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
  98. *
  99. * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
  100. * which should not be installed in TTBR0_EL1. In this case we can leave the
  101. * reserved page tables in place.
  102. */
  103. static inline void cpu_uninstall_idmap(void)
  104. {
  105. struct mm_struct *mm = current->active_mm;
  106. cpu_set_reserved_ttbr0();
  107. local_flush_tlb_all();
  108. cpu_set_default_tcr_t0sz();
  109. if (mm != &init_mm && !system_uses_ttbr0_pan())
  110. cpu_switch_mm(mm->pgd, mm);
  111. }
  112. static inline void cpu_install_idmap(void)
  113. {
  114. cpu_set_reserved_ttbr0();
  115. local_flush_tlb_all();
  116. cpu_set_idmap_tcr_t0sz();
  117. cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
  118. }
  119. /*
  120. * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
  121. * avoiding the possibility of conflicting TLB entries being allocated.
  122. */
  123. static inline void cpu_replace_ttbr1(pgd_t *pgdp)
  124. {
  125. typedef void (ttbr_replace_func)(phys_addr_t);
  126. extern ttbr_replace_func idmap_cpu_replace_ttbr1;
  127. ttbr_replace_func *replace_phys;
  128. phys_addr_t pgd_phys = virt_to_phys(pgdp);
  129. replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
  130. cpu_install_idmap();
  131. replace_phys(pgd_phys);
  132. cpu_uninstall_idmap();
  133. }
  134. /*
  135. * It would be nice to return ASIDs back to the allocator, but unfortunately
  136. * that introduces a race with a generation rollover where we could erroneously
  137. * free an ASID allocated in a future generation. We could workaround this by
  138. * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
  139. * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
  140. * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
  141. * take CPU migration into account.
  142. */
  143. #define destroy_context(mm) do { } while(0)
  144. void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
  145. #define init_new_context(tsk,mm) ({ atomic64_set(&(mm)->context.id, 0); 0; })
  146. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  147. static inline void update_saved_ttbr0(struct task_struct *tsk,
  148. struct mm_struct *mm)
  149. {
  150. u64 ttbr;
  151. if (!system_uses_ttbr0_pan())
  152. return;
  153. if (mm == &init_mm)
  154. ttbr = __pa_symbol(empty_zero_page);
  155. else
  156. ttbr = virt_to_phys(mm->pgd) | ASID(mm) << 48;
  157. WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
  158. }
  159. #else
  160. static inline void update_saved_ttbr0(struct task_struct *tsk,
  161. struct mm_struct *mm)
  162. {
  163. }
  164. #endif
  165. static inline void
  166. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  167. {
  168. /*
  169. * We don't actually care about the ttbr0 mapping, so point it at the
  170. * zero page.
  171. */
  172. update_saved_ttbr0(tsk, &init_mm);
  173. }
  174. static inline void __switch_mm(struct mm_struct *next)
  175. {
  176. unsigned int cpu = smp_processor_id();
  177. /*
  178. * init_mm.pgd does not contain any user mappings and it is always
  179. * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
  180. */
  181. if (next == &init_mm) {
  182. cpu_set_reserved_ttbr0();
  183. return;
  184. }
  185. check_and_switch_context(next, cpu);
  186. }
  187. static inline void
  188. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  189. struct task_struct *tsk)
  190. {
  191. if (prev != next)
  192. __switch_mm(next);
  193. /*
  194. * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
  195. * value may have not been initialised yet (activate_mm caller) or the
  196. * ASID has changed since the last run (following the context switch
  197. * of another thread of the same process).
  198. */
  199. update_saved_ttbr0(tsk, next);
  200. }
  201. #define deactivate_mm(tsk,mm) do { } while (0)
  202. #define activate_mm(prev,next) switch_mm(prev, next, current)
  203. void verify_cpu_asid_bits(void);
  204. void post_ttbr_update_workaround(void);
  205. #endif /* !__ASSEMBLY__ */
  206. #endif /* !__ASM_MMU_CONTEXT_H */