tlbflush.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/special_insns.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. struct tlb_state {
  15. #ifdef CONFIG_SMP
  16. struct mm_struct *active_mm;
  17. int state;
  18. #endif
  19. /*
  20. * Access to this CR4 shadow and to H/W CR4 is protected by
  21. * disabling interrupts when modifying either one.
  22. */
  23. unsigned long cr4;
  24. };
  25. DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
  26. /* Initialize cr4 shadow for this CPU. */
  27. static inline void cr4_init_shadow(void)
  28. {
  29. this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
  30. }
  31. /* Set in this cpu's CR4. */
  32. static inline void cr4_set_bits(unsigned long mask)
  33. {
  34. unsigned long cr4;
  35. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  36. if ((cr4 | mask) != cr4) {
  37. cr4 |= mask;
  38. this_cpu_write(cpu_tlbstate.cr4, cr4);
  39. __write_cr4(cr4);
  40. }
  41. }
  42. /* Clear in this cpu's CR4. */
  43. static inline void cr4_clear_bits(unsigned long mask)
  44. {
  45. unsigned long cr4;
  46. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  47. if ((cr4 & ~mask) != cr4) {
  48. cr4 &= ~mask;
  49. this_cpu_write(cpu_tlbstate.cr4, cr4);
  50. __write_cr4(cr4);
  51. }
  52. }
  53. /* Read the CR4 shadow. */
  54. static inline unsigned long cr4_read_shadow(void)
  55. {
  56. return this_cpu_read(cpu_tlbstate.cr4);
  57. }
  58. /*
  59. * Save some of cr4 feature set we're using (e.g. Pentium 4MB
  60. * enable and PPro Global page enable), so that any CPU's that boot
  61. * up after us can get the correct flags. This should only be used
  62. * during boot on the boot cpu.
  63. */
  64. extern unsigned long mmu_cr4_features;
  65. extern u32 *trampoline_cr4_features;
  66. static inline void cr4_set_bits_and_update_boot(unsigned long mask)
  67. {
  68. mmu_cr4_features |= mask;
  69. if (trampoline_cr4_features)
  70. *trampoline_cr4_features = mmu_cr4_features;
  71. cr4_set_bits(mask);
  72. }
  73. static inline void __native_flush_tlb(void)
  74. {
  75. native_write_cr3(native_read_cr3());
  76. }
  77. static inline void __native_flush_tlb_global_irq_disabled(void)
  78. {
  79. unsigned long cr4;
  80. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  81. /* clear PGE */
  82. native_write_cr4(cr4 & ~X86_CR4_PGE);
  83. /* write old PGE again and flush TLBs */
  84. native_write_cr4(cr4);
  85. }
  86. static inline void __native_flush_tlb_global(void)
  87. {
  88. unsigned long flags;
  89. /*
  90. * Read-modify-write to CR4 - protect it from preemption and
  91. * from interrupts. (Use the raw variant because this code can
  92. * be called from deep inside debugging code.)
  93. */
  94. raw_local_irq_save(flags);
  95. __native_flush_tlb_global_irq_disabled();
  96. raw_local_irq_restore(flags);
  97. }
  98. static inline void __native_flush_tlb_single(unsigned long addr)
  99. {
  100. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  101. }
  102. static inline void __flush_tlb_all(void)
  103. {
  104. if (cpu_has_pge)
  105. __flush_tlb_global();
  106. else
  107. __flush_tlb();
  108. }
  109. static inline void __flush_tlb_one(unsigned long addr)
  110. {
  111. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  112. __flush_tlb_single(addr);
  113. }
  114. #define TLB_FLUSH_ALL -1UL
  115. /*
  116. * TLB flushing:
  117. *
  118. * - flush_tlb() flushes the current mm struct TLBs
  119. * - flush_tlb_all() flushes all processes TLBs
  120. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  121. * - flush_tlb_page(vma, vmaddr) flushes one page
  122. * - flush_tlb_range(vma, start, end) flushes a range of pages
  123. * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
  124. * - flush_tlb_others(cpumask, mm, start, end) flushes TLBs on other cpus
  125. *
  126. * ..but the i386 has somewhat limited tlb flushing capabilities,
  127. * and page-granular flushes are available only on i486 and up.
  128. */
  129. #ifndef CONFIG_SMP
  130. /* "_up" is for UniProcessor.
  131. *
  132. * This is a helper for other header functions. *Not* intended to be called
  133. * directly. All global TLB flushes need to either call this, or to bump the
  134. * vm statistics themselves.
  135. */
  136. static inline void __flush_tlb_up(void)
  137. {
  138. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  139. __flush_tlb();
  140. }
  141. static inline void flush_tlb_all(void)
  142. {
  143. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  144. __flush_tlb_all();
  145. }
  146. static inline void flush_tlb(void)
  147. {
  148. __flush_tlb_up();
  149. }
  150. static inline void local_flush_tlb(void)
  151. {
  152. __flush_tlb_up();
  153. }
  154. static inline void flush_tlb_mm(struct mm_struct *mm)
  155. {
  156. if (mm == current->active_mm)
  157. __flush_tlb_up();
  158. }
  159. static inline void flush_tlb_page(struct vm_area_struct *vma,
  160. unsigned long addr)
  161. {
  162. if (vma->vm_mm == current->active_mm)
  163. __flush_tlb_one(addr);
  164. }
  165. static inline void flush_tlb_range(struct vm_area_struct *vma,
  166. unsigned long start, unsigned long end)
  167. {
  168. if (vma->vm_mm == current->active_mm)
  169. __flush_tlb_up();
  170. }
  171. static inline void flush_tlb_mm_range(struct mm_struct *mm,
  172. unsigned long start, unsigned long end, unsigned long vmflag)
  173. {
  174. if (mm == current->active_mm)
  175. __flush_tlb_up();
  176. }
  177. static inline void native_flush_tlb_others(const struct cpumask *cpumask,
  178. struct mm_struct *mm,
  179. unsigned long start,
  180. unsigned long end)
  181. {
  182. }
  183. static inline void reset_lazy_tlbstate(void)
  184. {
  185. }
  186. static inline void flush_tlb_kernel_range(unsigned long start,
  187. unsigned long end)
  188. {
  189. flush_tlb_all();
  190. }
  191. #else /* SMP */
  192. #include <asm/smp.h>
  193. #define local_flush_tlb() __flush_tlb()
  194. #define flush_tlb_mm(mm) flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
  195. #define flush_tlb_range(vma, start, end) \
  196. flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
  197. extern void flush_tlb_all(void);
  198. extern void flush_tlb_current_task(void);
  199. extern void flush_tlb_page(struct vm_area_struct *, unsigned long);
  200. extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  201. unsigned long end, unsigned long vmflag);
  202. extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  203. #define flush_tlb() flush_tlb_current_task()
  204. void native_flush_tlb_others(const struct cpumask *cpumask,
  205. struct mm_struct *mm,
  206. unsigned long start, unsigned long end);
  207. #define TLBSTATE_OK 1
  208. #define TLBSTATE_LAZY 2
  209. static inline void reset_lazy_tlbstate(void)
  210. {
  211. this_cpu_write(cpu_tlbstate.state, 0);
  212. this_cpu_write(cpu_tlbstate.active_mm, &init_mm);
  213. }
  214. #endif /* SMP */
  215. #ifndef CONFIG_PARAVIRT
  216. #define flush_tlb_others(mask, mm, start, end) \
  217. native_flush_tlb_others(mask, mm, start, end)
  218. #endif
  219. #endif /* _ASM_X86_TLBFLUSH_H */