mmu_context.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * arch/arm/include/asm/mmu_context.h
  3. *
  4. * Copyright (C) 1996 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Changelog:
  11. * 27-06-1996 RMK Created
  12. */
  13. #ifndef __ASM_ARM_MMU_CONTEXT_H
  14. #define __ASM_ARM_MMU_CONTEXT_H
  15. #include <linux/compiler.h>
  16. #include <linux/sched.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cachetype.h>
  19. #include <asm/proc-fns.h>
  20. #include <asm-generic/mm_hooks.h>
  21. void __check_kvm_seq(struct mm_struct *mm);
  22. #ifdef CONFIG_CPU_HAS_ASID
  23. /*
  24. * On ARMv6, we have the following structure in the Context ID:
  25. *
  26. * 31 7 0
  27. * +-------------------------+-----------+
  28. * | process ID | ASID |
  29. * +-------------------------+-----------+
  30. * | context ID |
  31. * +-------------------------------------+
  32. *
  33. * The ASID is used to tag entries in the CPU caches and TLBs.
  34. * The context ID is used by debuggers and trace logic, and
  35. * should be unique within all running processes.
  36. */
  37. #define ASID_BITS 8
  38. #define ASID_MASK ((~0) << ASID_BITS)
  39. #define ASID_FIRST_VERSION (1 << ASID_BITS)
  40. extern unsigned int cpu_last_asid;
  41. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm);
  42. void __new_context(struct mm_struct *mm);
  43. void cpu_set_reserved_ttbr0(void);
  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. if (unlikely(mm->context.kvm_seq != init_mm.context.kvm_seq))
  56. __check_kvm_seq(mm);
  57. /*
  58. * Required during context switch to avoid speculative page table
  59. * walking with the wrong TTBR.
  60. */
  61. cpu_set_reserved_ttbr0();
  62. if (!((mm->context.id ^ cpu_last_asid) >> ASID_BITS))
  63. /*
  64. * The ASID is from the current generation, just switch to the
  65. * new pgd. This condition is only true for calls from
  66. * context_switch() and interrupts are already disabled.
  67. */
  68. cpu_switch_mm(mm->pgd, mm);
  69. else if (irqs_disabled())
  70. /*
  71. * Defer the new ASID allocation until after the context
  72. * switch critical region since __new_context() cannot be
  73. * called with interrupts disabled (it sends IPIs).
  74. */
  75. set_ti_thread_flag(task_thread_info(tsk), TIF_SWITCH_MM);
  76. else
  77. /*
  78. * That is a direct call to switch_mm() or activate_mm() with
  79. * interrupts enabled and a new context.
  80. */
  81. switch_new_context(mm);
  82. }
  83. #define init_new_context(tsk,mm) (__init_new_context(tsk,mm),0)
  84. #define finish_arch_post_lock_switch \
  85. finish_arch_post_lock_switch
  86. static inline void finish_arch_post_lock_switch(void)
  87. {
  88. if (test_and_clear_thread_flag(TIF_SWITCH_MM))
  89. switch_new_context(current->mm);
  90. }
  91. #else /* !CONFIG_CPU_HAS_ASID */
  92. #ifdef CONFIG_MMU
  93. static inline void check_and_switch_context(struct mm_struct *mm,
  94. struct task_struct *tsk)
  95. {
  96. if (unlikely(mm->context.kvm_seq != init_mm.context.kvm_seq))
  97. __check_kvm_seq(mm);
  98. if (irqs_disabled())
  99. /*
  100. * cpu_switch_mm() needs to flush the VIVT caches. To avoid
  101. * high interrupt latencies, defer the call and continue
  102. * running with the old mm. Since we only support UP systems
  103. * on non-ASID CPUs, the old mm will remain valid until the
  104. * finish_arch_post_lock_switch() call.
  105. */
  106. set_ti_thread_flag(task_thread_info(tsk), TIF_SWITCH_MM);
  107. else
  108. cpu_switch_mm(mm->pgd, mm);
  109. }
  110. #define finish_arch_post_lock_switch \
  111. finish_arch_post_lock_switch
  112. static inline void finish_arch_post_lock_switch(void)
  113. {
  114. if (test_and_clear_thread_flag(TIF_SWITCH_MM)) {
  115. struct mm_struct *mm = current->mm;
  116. cpu_switch_mm(mm->pgd, mm);
  117. }
  118. }
  119. #endif /* CONFIG_MMU */
  120. #define init_new_context(tsk,mm) 0
  121. #endif /* CONFIG_CPU_HAS_ASID */
  122. #define destroy_context(mm) do { } while(0)
  123. /*
  124. * This is called when "tsk" is about to enter lazy TLB mode.
  125. *
  126. * mm: describes the currently active mm context
  127. * tsk: task which is entering lazy tlb
  128. * cpu: cpu number which is entering lazy tlb
  129. *
  130. * tsk->mm will be NULL
  131. */
  132. static inline void
  133. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  134. {
  135. }
  136. /*
  137. * This is the actual mm switch as far as the scheduler
  138. * is concerned. No registers are touched. We avoid
  139. * calling the CPU specific function when the mm hasn't
  140. * actually changed.
  141. */
  142. static inline void
  143. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  144. struct task_struct *tsk)
  145. {
  146. #ifdef CONFIG_MMU
  147. unsigned int cpu = smp_processor_id();
  148. #ifdef CONFIG_SMP
  149. /* check for possible thread migration */
  150. if (!cpumask_empty(mm_cpumask(next)) &&
  151. !cpumask_test_cpu(cpu, mm_cpumask(next)))
  152. __flush_icache_all();
  153. #endif
  154. if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)) || prev != next) {
  155. check_and_switch_context(next, tsk);
  156. if (cache_is_vivt())
  157. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  158. }
  159. #endif
  160. }
  161. #define deactivate_mm(tsk,mm) do { } while (0)
  162. #define activate_mm(prev,next) switch_mm(prev, next, NULL)
  163. #endif