tlbflush.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/cpufeature.h>
  7. #include <asm/special_insns.h>
  8. static inline void __invpcid(unsigned long pcid, unsigned long addr,
  9. unsigned long type)
  10. {
  11. struct { u64 d[2]; } desc = { { pcid, addr } };
  12. /*
  13. * The memory clobber is because the whole point is to invalidate
  14. * stale TLB entries and, especially if we're flushing global
  15. * mappings, we don't want the compiler to reorder any subsequent
  16. * memory accesses before the TLB flush.
  17. *
  18. * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
  19. * invpcid (%rcx), %rax in long mode.
  20. */
  21. asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
  22. : : "m" (desc), "a" (type), "c" (&desc) : "memory");
  23. }
  24. #define INVPCID_TYPE_INDIV_ADDR 0
  25. #define INVPCID_TYPE_SINGLE_CTXT 1
  26. #define INVPCID_TYPE_ALL_INCL_GLOBAL 2
  27. #define INVPCID_TYPE_ALL_NON_GLOBAL 3
  28. /* Flush all mappings for a given pcid and addr, not including globals. */
  29. static inline void invpcid_flush_one(unsigned long pcid,
  30. unsigned long addr)
  31. {
  32. __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
  33. }
  34. /* Flush all mappings for a given PCID, not including globals. */
  35. static inline void invpcid_flush_single_context(unsigned long pcid)
  36. {
  37. __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
  38. }
  39. /* Flush all mappings, including globals, for all PCIDs. */
  40. static inline void invpcid_flush_all(void)
  41. {
  42. __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
  43. }
  44. /* Flush all mappings for all PCIDs except globals. */
  45. static inline void invpcid_flush_all_nonglobals(void)
  46. {
  47. __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
  48. }
  49. #ifdef CONFIG_PARAVIRT
  50. #include <asm/paravirt.h>
  51. #else
  52. #define __flush_tlb() __native_flush_tlb()
  53. #define __flush_tlb_global() __native_flush_tlb_global()
  54. #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
  55. #endif
  56. struct tlb_state {
  57. #ifdef CONFIG_SMP
  58. struct mm_struct *active_mm;
  59. int state;
  60. #endif
  61. /*
  62. * Access to this CR4 shadow and to H/W CR4 is protected by
  63. * disabling interrupts when modifying either one.
  64. */
  65. unsigned long cr4;
  66. };
  67. DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
  68. /* Initialize cr4 shadow for this CPU. */
  69. static inline void cr4_init_shadow(void)
  70. {
  71. this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
  72. }
  73. /* Set in this cpu's CR4. */
  74. static inline void cr4_set_bits(unsigned long mask)
  75. {
  76. unsigned long cr4;
  77. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  78. if ((cr4 | mask) != cr4) {
  79. cr4 |= mask;
  80. this_cpu_write(cpu_tlbstate.cr4, cr4);
  81. __write_cr4(cr4);
  82. }
  83. }
  84. /* Clear in this cpu's CR4. */
  85. static inline void cr4_clear_bits(unsigned long mask)
  86. {
  87. unsigned long cr4;
  88. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  89. if ((cr4 & ~mask) != cr4) {
  90. cr4 &= ~mask;
  91. this_cpu_write(cpu_tlbstate.cr4, cr4);
  92. __write_cr4(cr4);
  93. }
  94. }
  95. static inline void cr4_toggle_bits(unsigned long mask)
  96. {
  97. unsigned long cr4;
  98. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  99. cr4 ^= mask;
  100. this_cpu_write(cpu_tlbstate.cr4, cr4);
  101. __write_cr4(cr4);
  102. }
  103. /* Read the CR4 shadow. */
  104. static inline unsigned long cr4_read_shadow(void)
  105. {
  106. return this_cpu_read(cpu_tlbstate.cr4);
  107. }
  108. /*
  109. * Save some of cr4 feature set we're using (e.g. Pentium 4MB
  110. * enable and PPro Global page enable), so that any CPU's that boot
  111. * up after us can get the correct flags. This should only be used
  112. * during boot on the boot cpu.
  113. */
  114. extern unsigned long mmu_cr4_features;
  115. extern u32 *trampoline_cr4_features;
  116. static inline void cr4_set_bits_and_update_boot(unsigned long mask)
  117. {
  118. mmu_cr4_features |= mask;
  119. if (trampoline_cr4_features)
  120. *trampoline_cr4_features = mmu_cr4_features;
  121. cr4_set_bits(mask);
  122. }
  123. static inline void __native_flush_tlb(void)
  124. {
  125. /*
  126. * If current->mm == NULL then we borrow a mm which may change during a
  127. * task switch and therefore we must not be preempted while we write CR3
  128. * back:
  129. */
  130. preempt_disable();
  131. native_write_cr3(native_read_cr3());
  132. preempt_enable();
  133. }
  134. static inline void __native_flush_tlb_global_irq_disabled(void)
  135. {
  136. unsigned long cr4;
  137. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  138. /* clear PGE */
  139. native_write_cr4(cr4 & ~X86_CR4_PGE);
  140. /* write old PGE again and flush TLBs */
  141. native_write_cr4(cr4);
  142. }
  143. static inline void __native_flush_tlb_global(void)
  144. {
  145. unsigned long flags;
  146. if (static_cpu_has(X86_FEATURE_INVPCID)) {
  147. /*
  148. * Using INVPCID is considerably faster than a pair of writes
  149. * to CR4 sandwiched inside an IRQ flag save/restore.
  150. */
  151. invpcid_flush_all();
  152. return;
  153. }
  154. /*
  155. * Read-modify-write to CR4 - protect it from preemption and
  156. * from interrupts. (Use the raw variant because this code can
  157. * be called from deep inside debugging code.)
  158. */
  159. raw_local_irq_save(flags);
  160. __native_flush_tlb_global_irq_disabled();
  161. raw_local_irq_restore(flags);
  162. }
  163. static inline void __native_flush_tlb_single(unsigned long addr)
  164. {
  165. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  166. }
  167. static inline void __flush_tlb_all(void)
  168. {
  169. if (boot_cpu_has(X86_FEATURE_PGE))
  170. __flush_tlb_global();
  171. else
  172. __flush_tlb();
  173. }
  174. static inline void __flush_tlb_one(unsigned long addr)
  175. {
  176. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  177. __flush_tlb_single(addr);
  178. }
  179. #define TLB_FLUSH_ALL -1UL
  180. /*
  181. * TLB flushing:
  182. *
  183. * - flush_tlb() flushes the current mm struct TLBs
  184. * - flush_tlb_all() flushes all processes TLBs
  185. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  186. * - flush_tlb_page(vma, vmaddr) flushes one page
  187. * - flush_tlb_range(vma, start, end) flushes a range of pages
  188. * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
  189. * - flush_tlb_others(cpumask, mm, start, end) flushes TLBs on other cpus
  190. *
  191. * ..but the i386 has somewhat limited tlb flushing capabilities,
  192. * and page-granular flushes are available only on i486 and up.
  193. */
  194. #ifndef CONFIG_SMP
  195. /* "_up" is for UniProcessor.
  196. *
  197. * This is a helper for other header functions. *Not* intended to be called
  198. * directly. All global TLB flushes need to either call this, or to bump the
  199. * vm statistics themselves.
  200. */
  201. static inline void __flush_tlb_up(void)
  202. {
  203. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  204. __flush_tlb();
  205. }
  206. static inline void flush_tlb_all(void)
  207. {
  208. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  209. __flush_tlb_all();
  210. }
  211. static inline void flush_tlb(void)
  212. {
  213. __flush_tlb_up();
  214. }
  215. static inline void local_flush_tlb(void)
  216. {
  217. __flush_tlb_up();
  218. }
  219. static inline void flush_tlb_mm(struct mm_struct *mm)
  220. {
  221. if (mm == current->active_mm)
  222. __flush_tlb_up();
  223. }
  224. static inline void flush_tlb_page(struct vm_area_struct *vma,
  225. unsigned long addr)
  226. {
  227. if (vma->vm_mm == current->active_mm)
  228. __flush_tlb_one(addr);
  229. }
  230. static inline void flush_tlb_range(struct vm_area_struct *vma,
  231. unsigned long start, unsigned long end)
  232. {
  233. if (vma->vm_mm == current->active_mm)
  234. __flush_tlb_up();
  235. }
  236. static inline void flush_tlb_mm_range(struct mm_struct *mm,
  237. unsigned long start, unsigned long end, unsigned long vmflag)
  238. {
  239. if (mm == current->active_mm)
  240. __flush_tlb_up();
  241. }
  242. static inline void native_flush_tlb_others(const struct cpumask *cpumask,
  243. struct mm_struct *mm,
  244. unsigned long start,
  245. unsigned long end)
  246. {
  247. }
  248. static inline void reset_lazy_tlbstate(void)
  249. {
  250. }
  251. static inline void flush_tlb_kernel_range(unsigned long start,
  252. unsigned long end)
  253. {
  254. flush_tlb_all();
  255. }
  256. #else /* SMP */
  257. #include <asm/smp.h>
  258. #define local_flush_tlb() __flush_tlb()
  259. #define flush_tlb_mm(mm) flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
  260. #define flush_tlb_range(vma, start, end) \
  261. flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
  262. extern void flush_tlb_all(void);
  263. extern void flush_tlb_current_task(void);
  264. extern void flush_tlb_page(struct vm_area_struct *, unsigned long);
  265. extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  266. unsigned long end, unsigned long vmflag);
  267. extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  268. #define flush_tlb() flush_tlb_current_task()
  269. void native_flush_tlb_others(const struct cpumask *cpumask,
  270. struct mm_struct *mm,
  271. unsigned long start, unsigned long end);
  272. #define TLBSTATE_OK 1
  273. #define TLBSTATE_LAZY 2
  274. static inline void reset_lazy_tlbstate(void)
  275. {
  276. this_cpu_write(cpu_tlbstate.state, 0);
  277. this_cpu_write(cpu_tlbstate.active_mm, &init_mm);
  278. }
  279. #endif /* SMP */
  280. #ifndef CONFIG_PARAVIRT
  281. #define flush_tlb_others(mask, mm, start, end) \
  282. native_flush_tlb_others(mask, mm, start, end)
  283. #endif
  284. #endif /* _ASM_X86_TLBFLUSH_H */