mmu_context.h 3.9 KB

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