tlbflush.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef ASM_X86__TLBFLUSH_H
  2. #define ASM_X86__TLBFLUSH_H
  3. #include <linux/mm.h>
  4. #include <linux/sched.h>
  5. #include <asm/processor.h>
  6. #include <asm/system.h>
  7. #ifdef CONFIG_PARAVIRT
  8. #include <asm/paravirt.h>
  9. #else
  10. #define __flush_tlb() __native_flush_tlb()
  11. #define __flush_tlb_global() __native_flush_tlb_global()
  12. #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
  13. #endif
  14. static inline void __native_flush_tlb(void)
  15. {
  16. write_cr3(read_cr3());
  17. }
  18. static inline void __native_flush_tlb_global(void)
  19. {
  20. unsigned long flags;
  21. unsigned long cr4;
  22. /*
  23. * Read-modify-write to CR4 - protect it from preemption and
  24. * from interrupts. (Use the raw variant because this code can
  25. * be called from deep inside debugging code.)
  26. */
  27. raw_local_irq_save(flags);
  28. cr4 = read_cr4();
  29. /* clear PGE */
  30. write_cr4(cr4 & ~X86_CR4_PGE);
  31. /* write old PGE again and flush TLBs */
  32. write_cr4(cr4);
  33. raw_local_irq_restore(flags);
  34. }
  35. static inline void __native_flush_tlb_single(unsigned long addr)
  36. {
  37. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  38. }
  39. static inline void __flush_tlb_all(void)
  40. {
  41. if (cpu_has_pge)
  42. __flush_tlb_global();
  43. else
  44. __flush_tlb();
  45. }
  46. static inline void __flush_tlb_one(unsigned long addr)
  47. {
  48. if (cpu_has_invlpg)
  49. __flush_tlb_single(addr);
  50. else
  51. __flush_tlb();
  52. }
  53. #ifdef CONFIG_X86_32
  54. # define TLB_FLUSH_ALL 0xffffffff
  55. #else
  56. # define TLB_FLUSH_ALL -1ULL
  57. #endif
  58. /*
  59. * TLB flushing:
  60. *
  61. * - flush_tlb() flushes the current mm struct TLBs
  62. * - flush_tlb_all() flushes all processes TLBs
  63. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  64. * - flush_tlb_page(vma, vmaddr) flushes one page
  65. * - flush_tlb_range(vma, start, end) flushes a range of pages
  66. * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
  67. * - flush_tlb_others(cpumask, mm, va) flushes TLBs on other cpus
  68. *
  69. * ..but the i386 has somewhat limited tlb flushing capabilities,
  70. * and page-granular flushes are available only on i486 and up.
  71. *
  72. * x86-64 can only flush individual pages or full VMs. For a range flush
  73. * we always do the full VM. Might be worth trying if for a small
  74. * range a few INVLPGs in a row are a win.
  75. */
  76. #ifndef CONFIG_SMP
  77. #define flush_tlb() __flush_tlb()
  78. #define flush_tlb_all() __flush_tlb_all()
  79. #define local_flush_tlb() __flush_tlb()
  80. static inline void flush_tlb_mm(struct mm_struct *mm)
  81. {
  82. if (mm == current->active_mm)
  83. __flush_tlb();
  84. }
  85. static inline void flush_tlb_page(struct vm_area_struct *vma,
  86. unsigned long addr)
  87. {
  88. if (vma->vm_mm == current->active_mm)
  89. __flush_tlb_one(addr);
  90. }
  91. static inline void flush_tlb_range(struct vm_area_struct *vma,
  92. unsigned long start, unsigned long end)
  93. {
  94. if (vma->vm_mm == current->active_mm)
  95. __flush_tlb();
  96. }
  97. static inline void native_flush_tlb_others(const cpumask_t *cpumask,
  98. struct mm_struct *mm,
  99. unsigned long va)
  100. {
  101. }
  102. #else /* SMP */
  103. #include <asm/smp.h>
  104. #define local_flush_tlb() __flush_tlb()
  105. extern void flush_tlb_all(void);
  106. extern void flush_tlb_current_task(void);
  107. extern void flush_tlb_mm(struct mm_struct *);
  108. extern void flush_tlb_page(struct vm_area_struct *, unsigned long);
  109. #define flush_tlb() flush_tlb_current_task()
  110. static inline void flush_tlb_range(struct vm_area_struct *vma,
  111. unsigned long start, unsigned long end)
  112. {
  113. flush_tlb_mm(vma->vm_mm);
  114. }
  115. void native_flush_tlb_others(const cpumask_t *cpumask, struct mm_struct *mm,
  116. unsigned long va);
  117. #define TLBSTATE_OK 1
  118. #define TLBSTATE_LAZY 2
  119. #ifdef CONFIG_X86_32
  120. struct tlb_state {
  121. struct mm_struct *active_mm;
  122. int state;
  123. char __cacheline_padding[L1_CACHE_BYTES-8];
  124. };
  125. DECLARE_PER_CPU(struct tlb_state, cpu_tlbstate);
  126. #endif
  127. #endif /* SMP */
  128. #ifndef CONFIG_PARAVIRT
  129. #define flush_tlb_others(mask, mm, va) native_flush_tlb_others(&mask, mm, va)
  130. #endif
  131. static inline void flush_tlb_kernel_range(unsigned long start,
  132. unsigned long end)
  133. {
  134. flush_tlb_all();
  135. }
  136. #endif /* ASM_X86__TLBFLUSH_H */