mmu_context.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/mmu_context.h
  4. *
  5. * Copyright (C) 1996 Russell King.
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #ifndef __ASM_MMU_CONTEXT_H
  9. #define __ASM_MMU_CONTEXT_H
  10. #ifndef __ASSEMBLY__
  11. #include <linux/compiler.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/hotplug.h>
  14. #include <linux/mm_types.h>
  15. #include <linux/pgtable.h>
  16. #include <linux/pkeys.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/daifflags.h>
  20. #include <asm/proc-fns.h>
  21. #include <asm/cputype.h>
  22. #include <asm/sysreg.h>
  23. #include <asm/tlbflush.h>
  24. extern bool rodata_full;
  25. static inline void contextidr_thread_switch(struct task_struct *next)
  26. {
  27. if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
  28. return;
  29. write_sysreg(task_pid_nr(next), contextidr_el1);
  30. isb();
  31. }
  32. /*
  33. * Set TTBR0 to reserved_pg_dir. No translations will be possible via TTBR0.
  34. */
  35. static inline void cpu_set_reserved_ttbr0_nosync(void)
  36. {
  37. unsigned long ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
  38. write_sysreg(ttbr, ttbr0_el1);
  39. }
  40. static inline void cpu_set_reserved_ttbr0(void)
  41. {
  42. cpu_set_reserved_ttbr0_nosync();
  43. isb();
  44. }
  45. void cpu_do_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
  46. static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
  47. {
  48. BUG_ON(pgd == swapper_pg_dir);
  49. cpu_do_switch_mm(virt_to_phys(pgd),mm);
  50. }
  51. /*
  52. * TCR.T0SZ value to use when the ID map is active.
  53. */
  54. #define idmap_t0sz TCR_T0SZ(IDMAP_VA_BITS)
  55. /*
  56. * Ensure TCR.T0SZ is set to the provided value.
  57. */
  58. static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
  59. {
  60. unsigned long tcr = read_sysreg(tcr_el1);
  61. if ((tcr & TCR_T0SZ_MASK) == t0sz)
  62. return;
  63. tcr &= ~TCR_T0SZ_MASK;
  64. tcr |= t0sz;
  65. write_sysreg(tcr, tcr_el1);
  66. isb();
  67. }
  68. #define cpu_set_default_tcr_t0sz() __cpu_set_tcr_t0sz(TCR_T0SZ(vabits_actual))
  69. #define cpu_set_idmap_tcr_t0sz() __cpu_set_tcr_t0sz(idmap_t0sz)
  70. /*
  71. * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
  72. *
  73. * The idmap lives in the same VA range as userspace, but uses global entries
  74. * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
  75. * speculative TLB fetches, we must temporarily install the reserved page
  76. * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
  77. *
  78. * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
  79. * which should not be installed in TTBR0_EL1. In this case we can leave the
  80. * reserved page tables in place.
  81. */
  82. static inline void cpu_uninstall_idmap(void)
  83. {
  84. struct mm_struct *mm = current->active_mm;
  85. cpu_set_reserved_ttbr0();
  86. local_flush_tlb_all();
  87. cpu_set_default_tcr_t0sz();
  88. if (mm != &init_mm && !system_uses_ttbr0_pan())
  89. cpu_switch_mm(mm->pgd, mm);
  90. }
  91. static inline void cpu_install_idmap(void)
  92. {
  93. cpu_set_reserved_ttbr0();
  94. local_flush_tlb_all();
  95. cpu_set_idmap_tcr_t0sz();
  96. cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
  97. }
  98. /*
  99. * Load our new page tables. A strict BBM approach requires that we ensure that
  100. * TLBs are free of any entries that may overlap with the global mappings we are
  101. * about to install.
  102. *
  103. * For a real hibernate/resume/kexec cycle TTBR0 currently points to a zero
  104. * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI runtime
  105. * services), while for a userspace-driven test_resume cycle it points to
  106. * userspace page tables (and we must point it at a zero page ourselves).
  107. *
  108. * We change T0SZ as part of installing the idmap. This is undone by
  109. * cpu_uninstall_idmap() in __cpu_suspend_exit().
  110. */
  111. static inline void cpu_install_ttbr0(phys_addr_t ttbr0, unsigned long t0sz)
  112. {
  113. cpu_set_reserved_ttbr0();
  114. local_flush_tlb_all();
  115. __cpu_set_tcr_t0sz(t0sz);
  116. /* avoid cpu_switch_mm() and its SW-PAN and CNP interactions */
  117. write_sysreg(ttbr0, ttbr0_el1);
  118. isb();
  119. }
  120. void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp);
  121. static inline void cpu_enable_swapper_cnp(void)
  122. {
  123. __cpu_replace_ttbr1(lm_alias(swapper_pg_dir), true);
  124. }
  125. static inline void cpu_replace_ttbr1(pgd_t *pgdp)
  126. {
  127. /*
  128. * Only for early TTBR1 replacement before cpucaps are finalized and
  129. * before we've decided whether to use CNP.
  130. */
  131. WARN_ON(system_capabilities_finalized());
  132. __cpu_replace_ttbr1(pgdp, false);
  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. void check_and_switch_context(struct mm_struct *mm);
  144. #define init_new_context(tsk, mm) init_new_context(tsk, mm)
  145. static inline int
  146. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  147. {
  148. atomic64_set(&mm->context.id, 0);
  149. refcount_set(&mm->context.pinned, 0);
  150. /* pkey 0 is the default, so always reserve it. */
  151. mm->context.pkey_allocation_map = BIT(0);
  152. return 0;
  153. }
  154. static inline void arch_dup_pkeys(struct mm_struct *oldmm,
  155. struct mm_struct *mm)
  156. {
  157. /* Duplicate the oldmm pkey state in mm: */
  158. mm->context.pkey_allocation_map = oldmm->context.pkey_allocation_map;
  159. }
  160. static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  161. {
  162. arch_dup_pkeys(oldmm, mm);
  163. return 0;
  164. }
  165. static inline void arch_exit_mmap(struct mm_struct *mm)
  166. {
  167. }
  168. static inline void arch_unmap(struct mm_struct *mm,
  169. unsigned long start, unsigned long end)
  170. {
  171. }
  172. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  173. static inline void update_saved_ttbr0(struct task_struct *tsk,
  174. struct mm_struct *mm)
  175. {
  176. u64 ttbr;
  177. if (!system_uses_ttbr0_pan())
  178. return;
  179. if (mm == &init_mm)
  180. ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
  181. else
  182. ttbr = phys_to_ttbr(virt_to_phys(mm->pgd)) | ASID(mm) << 48;
  183. WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
  184. }
  185. #else
  186. static inline void update_saved_ttbr0(struct task_struct *tsk,
  187. struct mm_struct *mm)
  188. {
  189. }
  190. #endif
  191. #define enter_lazy_tlb enter_lazy_tlb
  192. static inline void
  193. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  194. {
  195. /*
  196. * We don't actually care about the ttbr0 mapping, so point it at the
  197. * zero page.
  198. */
  199. update_saved_ttbr0(tsk, &init_mm);
  200. }
  201. static inline void __switch_mm(struct mm_struct *next)
  202. {
  203. /*
  204. * init_mm.pgd does not contain any user mappings and it is always
  205. * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
  206. */
  207. if (next == &init_mm) {
  208. cpu_set_reserved_ttbr0();
  209. return;
  210. }
  211. check_and_switch_context(next);
  212. }
  213. static inline void
  214. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  215. struct task_struct *tsk)
  216. {
  217. if (prev != next)
  218. __switch_mm(next);
  219. /*
  220. * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
  221. * value may have not been initialised yet (activate_mm caller) or the
  222. * ASID has changed since the last run (following the context switch
  223. * of another thread of the same process).
  224. */
  225. update_saved_ttbr0(tsk, next);
  226. }
  227. static inline const struct cpumask *
  228. task_cpu_possible_mask(struct task_struct *p)
  229. {
  230. if (!static_branch_unlikely(&arm64_mismatched_32bit_el0))
  231. return cpu_possible_mask;
  232. if (!is_compat_thread(task_thread_info(p)))
  233. return cpu_possible_mask;
  234. return system_32bit_el0_cpumask();
  235. }
  236. #define task_cpu_possible_mask task_cpu_possible_mask
  237. void verify_cpu_asid_bits(void);
  238. void post_ttbr_update_workaround(void);
  239. unsigned long arm64_mm_context_get(struct mm_struct *mm);
  240. void arm64_mm_context_put(struct mm_struct *mm);
  241. #define mm_untag_mask mm_untag_mask
  242. static inline unsigned long mm_untag_mask(struct mm_struct *mm)
  243. {
  244. return -1UL >> 8;
  245. }
  246. /*
  247. * Only enforce protection keys on the current process, because there is no
  248. * user context to access POR_EL0 for another address space.
  249. */
  250. static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
  251. bool write, bool execute, bool foreign)
  252. {
  253. if (!system_supports_poe())
  254. return true;
  255. /* allow access if the VMA is not one from this process */
  256. if (foreign || vma_is_foreign(vma))
  257. return true;
  258. return por_el0_allows_pkey(vma_pkey(vma), write, execute);
  259. }
  260. #include <asm-generic/mmu_context.h>
  261. #endif /* !__ASSEMBLY__ */
  262. #endif /* !__ASM_MMU_CONTEXT_H */