mmu_context.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include <linux/compiler.h>
  22. #include <linux/sched.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/proc-fns.h>
  25. #include <asm-generic/mm_hooks.h>
  26. #include <asm/cputype.h>
  27. #include <asm/pgtable.h>
  28. #define MAX_ASID_BITS 16
  29. extern unsigned int cpu_last_asid;
  30. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm);
  31. void __new_context(struct mm_struct *mm);
  32. #ifdef CONFIG_PID_IN_CONTEXTIDR
  33. static inline void contextidr_thread_switch(struct task_struct *next)
  34. {
  35. asm(
  36. " msr contextidr_el1, %0\n"
  37. " isb"
  38. :
  39. : "r" (task_pid_nr(next)));
  40. }
  41. #else
  42. static inline void contextidr_thread_switch(struct task_struct *next)
  43. {
  44. }
  45. #endif
  46. /*
  47. * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
  48. */
  49. static inline void cpu_set_reserved_ttbr0(void)
  50. {
  51. unsigned long ttbr = page_to_phys(empty_zero_page);
  52. asm(
  53. " msr ttbr0_el1, %0 // set TTBR0\n"
  54. " isb"
  55. :
  56. : "r" (ttbr));
  57. }
  58. /*
  59. * TCR.T0SZ value to use when the ID map is active. Usually equals
  60. * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
  61. * physical memory, in which case it will be smaller.
  62. */
  63. extern u64 idmap_t0sz;
  64. static inline bool __cpu_uses_extended_idmap(void)
  65. {
  66. return (!IS_ENABLED(CONFIG_ARM64_VA_BITS_48) &&
  67. unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS)));
  68. }
  69. static inline void __cpu_set_tcr_t0sz(u64 t0sz)
  70. {
  71. unsigned long tcr;
  72. if (__cpu_uses_extended_idmap())
  73. asm volatile (
  74. " mrs %0, tcr_el1 ;"
  75. " bfi %0, %1, %2, %3 ;"
  76. " msr tcr_el1, %0 ;"
  77. " isb"
  78. : "=&r" (tcr)
  79. : "r"(t0sz), "I"(TCR_T0SZ_OFFSET), "I"(TCR_TxSZ_WIDTH));
  80. }
  81. /*
  82. * Set TCR.T0SZ to the value appropriate for activating the identity map.
  83. */
  84. static inline void cpu_set_idmap_tcr_t0sz(void)
  85. {
  86. __cpu_set_tcr_t0sz(idmap_t0sz);
  87. }
  88. /*
  89. * Set TCR.T0SZ to its default value (based on VA_BITS)
  90. */
  91. static inline void cpu_set_default_tcr_t0sz(void)
  92. {
  93. __cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS));
  94. }
  95. static inline void switch_new_context(struct mm_struct *mm)
  96. {
  97. unsigned long flags;
  98. __new_context(mm);
  99. local_irq_save(flags);
  100. cpu_switch_mm(mm->pgd, mm);
  101. local_irq_restore(flags);
  102. }
  103. static inline void check_and_switch_context(struct mm_struct *mm,
  104. struct task_struct *tsk)
  105. {
  106. /*
  107. * Required during context switch to avoid speculative page table
  108. * walking with the wrong TTBR.
  109. */
  110. cpu_set_reserved_ttbr0();
  111. if (!((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS))
  112. /*
  113. * The ASID is from the current generation, just switch to the
  114. * new pgd. This condition is only true for calls from
  115. * context_switch() and interrupts are already disabled.
  116. */
  117. cpu_switch_mm(mm->pgd, mm);
  118. else if (irqs_disabled())
  119. /*
  120. * Defer the new ASID allocation until after the context
  121. * switch critical region since __new_context() cannot be
  122. * called with interrupts disabled.
  123. */
  124. set_ti_thread_flag(task_thread_info(tsk), TIF_SWITCH_MM);
  125. else
  126. /*
  127. * That is a direct call to switch_mm() or activate_mm() with
  128. * interrupts enabled and a new context.
  129. */
  130. switch_new_context(mm);
  131. }
  132. #define init_new_context(tsk,mm) (__init_new_context(tsk,mm),0)
  133. #define destroy_context(mm) do { } while(0)
  134. #define finish_arch_post_lock_switch \
  135. finish_arch_post_lock_switch
  136. static inline void finish_arch_post_lock_switch(void)
  137. {
  138. if (test_and_clear_thread_flag(TIF_SWITCH_MM)) {
  139. struct mm_struct *mm = current->mm;
  140. unsigned long flags;
  141. __new_context(mm);
  142. local_irq_save(flags);
  143. cpu_switch_mm(mm->pgd, mm);
  144. local_irq_restore(flags);
  145. }
  146. }
  147. /*
  148. * This is called when "tsk" is about to enter lazy TLB mode.
  149. *
  150. * mm: describes the currently active mm context
  151. * tsk: task which is entering lazy tlb
  152. * cpu: cpu number which is entering lazy tlb
  153. *
  154. * tsk->mm will be NULL
  155. */
  156. static inline void
  157. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  158. {
  159. }
  160. /*
  161. * This is the actual mm switch as far as the scheduler
  162. * is concerned. No registers are touched. We avoid
  163. * calling the CPU specific function when the mm hasn't
  164. * actually changed.
  165. */
  166. static inline void
  167. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  168. struct task_struct *tsk)
  169. {
  170. unsigned int cpu = smp_processor_id();
  171. /*
  172. * init_mm.pgd does not contain any user mappings and it is always
  173. * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
  174. */
  175. if (next == &init_mm) {
  176. cpu_set_reserved_ttbr0();
  177. return;
  178. }
  179. if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)) || prev != next)
  180. check_and_switch_context(next, tsk);
  181. }
  182. #define deactivate_mm(tsk,mm) do { } while (0)
  183. #define activate_mm(prev,next) switch_mm(prev, next, NULL)
  184. #endif