mmu_context.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef _ASM_X86_MMU_CONTEXT_H
  2. #define _ASM_X86_MMU_CONTEXT_H
  3. #include <asm/desc.h>
  4. #include <linux/atomic.h>
  5. #include <asm/pgalloc.h>
  6. #include <asm/tlbflush.h>
  7. #include <asm/paravirt.h>
  8. #ifndef CONFIG_PARAVIRT
  9. #include <asm-generic/mm_hooks.h>
  10. static inline void paravirt_activate_mm(struct mm_struct *prev,
  11. struct mm_struct *next)
  12. {
  13. }
  14. #endif /* !CONFIG_PARAVIRT */
  15. /*
  16. * Used for LDT copy/destruction.
  17. */
  18. int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
  19. void destroy_context(struct mm_struct *mm);
  20. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  21. {
  22. #ifdef CONFIG_SMP
  23. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  24. this_cpu_write(cpu_tlbstate.state, TLBSTATE_LAZY);
  25. #endif
  26. }
  27. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  28. struct task_struct *tsk)
  29. {
  30. unsigned cpu = smp_processor_id();
  31. if (likely(prev != next)) {
  32. #ifdef CONFIG_SMP
  33. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  34. this_cpu_write(cpu_tlbstate.active_mm, next);
  35. #endif
  36. cpumask_set_cpu(cpu, mm_cpumask(next));
  37. /* Re-load page tables */
  38. load_cr3(next->pgd);
  39. /* Stop flush ipis for the previous mm */
  40. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  41. /* Load the LDT, if the LDT is different: */
  42. if (unlikely(prev->context.ldt != next->context.ldt))
  43. load_LDT_nolock(&next->context);
  44. }
  45. #ifdef CONFIG_SMP
  46. else {
  47. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  48. BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next);
  49. if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
  50. /*
  51. * On established mms, the mm_cpumask is only changed
  52. * from irq context, from ptep_clear_flush() while in
  53. * lazy tlb mode, and here. Irqs are blocked during
  54. * schedule, protecting us from simultaneous changes.
  55. */
  56. cpumask_set_cpu(cpu, mm_cpumask(next));
  57. /*
  58. * We were in lazy tlb mode and leave_mm disabled
  59. * tlb flush IPI delivery. We must reload CR3
  60. * to make sure to use no freed page tables.
  61. */
  62. load_cr3(next->pgd);
  63. load_LDT_nolock(&next->context);
  64. }
  65. }
  66. #endif
  67. }
  68. #define activate_mm(prev, next) \
  69. do { \
  70. paravirt_activate_mm((prev), (next)); \
  71. switch_mm((prev), (next), NULL); \
  72. } while (0);
  73. #ifdef CONFIG_X86_32
  74. #define deactivate_mm(tsk, mm) \
  75. do { \
  76. lazy_load_gs(0); \
  77. } while (0)
  78. #else
  79. #define deactivate_mm(tsk, mm) \
  80. do { \
  81. load_gs_index(0); \
  82. loadsegment(fs, 0); \
  83. } while (0)
  84. #endif
  85. #endif /* _ASM_X86_MMU_CONTEXT_H */