mmu_context.h 8.3 KB

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