mmu_context.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. static inline void switch_new_context(struct mm_struct *mm)
  59. {
  60. unsigned long flags;
  61. __new_context(mm);
  62. local_irq_save(flags);
  63. cpu_switch_mm(mm->pgd, mm);
  64. local_irq_restore(flags);
  65. }
  66. static inline void check_and_switch_context(struct mm_struct *mm,
  67. struct task_struct *tsk)
  68. {
  69. /*
  70. * Required during context switch to avoid speculative page table
  71. * walking with the wrong TTBR.
  72. */
  73. cpu_set_reserved_ttbr0();
  74. if (!((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS))
  75. /*
  76. * The ASID is from the current generation, just switch to the
  77. * new pgd. This condition is only true for calls from
  78. * context_switch() and interrupts are already disabled.
  79. */
  80. cpu_switch_mm(mm->pgd, mm);
  81. else if (irqs_disabled())
  82. /*
  83. * Defer the new ASID allocation until after the context
  84. * switch critical region since __new_context() cannot be
  85. * called with interrupts disabled.
  86. */
  87. set_ti_thread_flag(task_thread_info(tsk), TIF_SWITCH_MM);
  88. else
  89. /*
  90. * That is a direct call to switch_mm() or activate_mm() with
  91. * interrupts enabled and a new context.
  92. */
  93. switch_new_context(mm);
  94. }
  95. #define init_new_context(tsk,mm) (__init_new_context(tsk,mm),0)
  96. #define destroy_context(mm) do { } while(0)
  97. #define finish_arch_post_lock_switch \
  98. finish_arch_post_lock_switch
  99. static inline void finish_arch_post_lock_switch(void)
  100. {
  101. if (test_and_clear_thread_flag(TIF_SWITCH_MM)) {
  102. struct mm_struct *mm = current->mm;
  103. unsigned long flags;
  104. __new_context(mm);
  105. local_irq_save(flags);
  106. cpu_switch_mm(mm->pgd, mm);
  107. local_irq_restore(flags);
  108. }
  109. }
  110. /*
  111. * This is called when "tsk" is about to enter lazy TLB mode.
  112. *
  113. * mm: describes the currently active mm context
  114. * tsk: task which is entering lazy tlb
  115. * cpu: cpu number which is entering lazy tlb
  116. *
  117. * tsk->mm will be NULL
  118. */
  119. static inline void
  120. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  121. {
  122. }
  123. /*
  124. * This is the actual mm switch as far as the scheduler
  125. * is concerned. No registers are touched. We avoid
  126. * calling the CPU specific function when the mm hasn't
  127. * actually changed.
  128. */
  129. static inline void
  130. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  131. struct task_struct *tsk)
  132. {
  133. unsigned int cpu = smp_processor_id();
  134. #ifdef CONFIG_SMP
  135. /* check for possible thread migration */
  136. if (!cpumask_empty(mm_cpumask(next)) &&
  137. !cpumask_test_cpu(cpu, mm_cpumask(next)))
  138. __flush_icache_all();
  139. #endif
  140. if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)) || prev != next)
  141. check_and_switch_context(next, tsk);
  142. }
  143. #define deactivate_mm(tsk,mm) do { } while (0)
  144. #define activate_mm(prev,next) switch_mm(prev, next, NULL)
  145. #endif