tlbflush.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #include <asm/smp.h>
  9. static inline void __invpcid(unsigned long pcid, unsigned long addr,
  10. unsigned long type)
  11. {
  12. struct { u64 d[2]; } desc = { { pcid, addr } };
  13. /*
  14. * The memory clobber is because the whole point is to invalidate
  15. * stale TLB entries and, especially if we're flushing global
  16. * mappings, we don't want the compiler to reorder any subsequent
  17. * memory accesses before the TLB flush.
  18. *
  19. * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
  20. * invpcid (%rcx), %rax in long mode.
  21. */
  22. asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
  23. : : "m" (desc), "a" (type), "c" (&desc) : "memory");
  24. }
  25. #define INVPCID_TYPE_INDIV_ADDR 0
  26. #define INVPCID_TYPE_SINGLE_CTXT 1
  27. #define INVPCID_TYPE_ALL_INCL_GLOBAL 2
  28. #define INVPCID_TYPE_ALL_NON_GLOBAL 3
  29. /* Flush all mappings for a given pcid and addr, not including globals. */
  30. static inline void invpcid_flush_one(unsigned long pcid,
  31. unsigned long addr)
  32. {
  33. __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
  34. }
  35. /* Flush all mappings for a given PCID, not including globals. */
  36. static inline void invpcid_flush_single_context(unsigned long pcid)
  37. {
  38. __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
  39. }
  40. /* Flush all mappings, including globals, for all PCIDs. */
  41. static inline void invpcid_flush_all(void)
  42. {
  43. __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
  44. }
  45. /* Flush all mappings for all PCIDs except globals. */
  46. static inline void invpcid_flush_all_nonglobals(void)
  47. {
  48. __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
  49. }
  50. static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  51. {
  52. u64 new_tlb_gen;
  53. /*
  54. * Bump the generation count. This also serves as a full barrier
  55. * that synchronizes with switch_mm(): callers are required to order
  56. * their read of mm_cpumask after their writes to the paging
  57. * structures.
  58. */
  59. smp_mb__before_atomic();
  60. new_tlb_gen = atomic64_inc_return(&mm->context.tlb_gen);
  61. smp_mb__after_atomic();
  62. return new_tlb_gen;
  63. }
  64. #ifdef CONFIG_PARAVIRT
  65. #include <asm/paravirt.h>
  66. #else
  67. #define __flush_tlb() __native_flush_tlb()
  68. #define __flush_tlb_global() __native_flush_tlb_global()
  69. #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
  70. #endif
  71. static inline bool tlb_defer_switch_to_init_mm(void)
  72. {
  73. /*
  74. * If we have PCID, then switching to init_mm is reasonably
  75. * fast. If we don't have PCID, then switching to init_mm is
  76. * quite slow, so we try to defer it in the hopes that we can
  77. * avoid it entirely. The latter approach runs the risk of
  78. * receiving otherwise unnecessary IPIs.
  79. *
  80. * This choice is just a heuristic. The tlb code can handle this
  81. * function returning true or false regardless of whether we have
  82. * PCID.
  83. */
  84. return !static_cpu_has(X86_FEATURE_PCID);
  85. }
  86. /*
  87. * 6 because 6 should be plenty and struct tlb_state will fit in
  88. * two cache lines.
  89. */
  90. #define TLB_NR_DYN_ASIDS 6
  91. struct tlb_context {
  92. u64 ctx_id;
  93. u64 tlb_gen;
  94. };
  95. struct tlb_state {
  96. /*
  97. * cpu_tlbstate.loaded_mm should match CR3 whenever interrupts
  98. * are on. This means that it may not match current->active_mm,
  99. * which will contain the previous user mm when we're in lazy TLB
  100. * mode even if we've already switched back to swapper_pg_dir.
  101. */
  102. struct mm_struct *loaded_mm;
  103. u16 loaded_mm_asid;
  104. u16 next_asid;
  105. /*
  106. * We can be in one of several states:
  107. *
  108. * - Actively using an mm. Our CPU's bit will be set in
  109. * mm_cpumask(loaded_mm) and is_lazy == false;
  110. *
  111. * - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit
  112. * will not be set in mm_cpumask(&init_mm) and is_lazy == false.
  113. *
  114. * - Lazily using a real mm. loaded_mm != &init_mm, our bit
  115. * is set in mm_cpumask(loaded_mm), but is_lazy == true.
  116. * We're heuristically guessing that the CR3 load we
  117. * skipped more than makes up for the overhead added by
  118. * lazy mode.
  119. */
  120. bool is_lazy;
  121. /*
  122. * Access to this CR4 shadow and to H/W CR4 is protected by
  123. * disabling interrupts when modifying either one.
  124. */
  125. unsigned long cr4;
  126. /*
  127. * This is a list of all contexts that might exist in the TLB.
  128. * There is one per ASID that we use, and the ASID (what the
  129. * CPU calls PCID) is the index into ctxts.
  130. *
  131. * For each context, ctx_id indicates which mm the TLB's user
  132. * entries came from. As an invariant, the TLB will never
  133. * contain entries that are out-of-date as when that mm reached
  134. * the tlb_gen in the list.
  135. *
  136. * To be clear, this means that it's legal for the TLB code to
  137. * flush the TLB without updating tlb_gen. This can happen
  138. * (for now, at least) due to paravirt remote flushes.
  139. *
  140. * NB: context 0 is a bit special, since it's also used by
  141. * various bits of init code. This is fine -- code that
  142. * isn't aware of PCID will end up harmlessly flushing
  143. * context 0.
  144. */
  145. struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
  146. };
  147. DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
  148. /* Initialize cr4 shadow for this CPU. */
  149. static inline void cr4_init_shadow(void)
  150. {
  151. this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
  152. }
  153. /* Set in this cpu's CR4. */
  154. static inline void cr4_set_bits(unsigned long mask)
  155. {
  156. unsigned long cr4;
  157. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  158. if ((cr4 | mask) != cr4) {
  159. cr4 |= mask;
  160. this_cpu_write(cpu_tlbstate.cr4, cr4);
  161. __write_cr4(cr4);
  162. }
  163. }
  164. /* Clear in this cpu's CR4. */
  165. static inline void cr4_clear_bits(unsigned long mask)
  166. {
  167. unsigned long cr4;
  168. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  169. if ((cr4 & ~mask) != cr4) {
  170. cr4 &= ~mask;
  171. this_cpu_write(cpu_tlbstate.cr4, cr4);
  172. __write_cr4(cr4);
  173. }
  174. }
  175. static inline void cr4_toggle_bits(unsigned long mask)
  176. {
  177. unsigned long cr4;
  178. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  179. cr4 ^= mask;
  180. this_cpu_write(cpu_tlbstate.cr4, cr4);
  181. __write_cr4(cr4);
  182. }
  183. /* Read the CR4 shadow. */
  184. static inline unsigned long cr4_read_shadow(void)
  185. {
  186. return this_cpu_read(cpu_tlbstate.cr4);
  187. }
  188. /*
  189. * Save some of cr4 feature set we're using (e.g. Pentium 4MB
  190. * enable and PPro Global page enable), so that any CPU's that boot
  191. * up after us can get the correct flags. This should only be used
  192. * during boot on the boot cpu.
  193. */
  194. extern unsigned long mmu_cr4_features;
  195. extern u32 *trampoline_cr4_features;
  196. static inline void cr4_set_bits_and_update_boot(unsigned long mask)
  197. {
  198. mmu_cr4_features |= mask;
  199. if (trampoline_cr4_features)
  200. *trampoline_cr4_features = mmu_cr4_features;
  201. cr4_set_bits(mask);
  202. }
  203. extern void initialize_tlbstate_and_flush(void);
  204. static inline void __native_flush_tlb(void)
  205. {
  206. /*
  207. * If current->mm == NULL then we borrow a mm which may change during a
  208. * task switch and therefore we must not be preempted while we write CR3
  209. * back:
  210. */
  211. preempt_disable();
  212. native_write_cr3(__native_read_cr3());
  213. preempt_enable();
  214. }
  215. static inline void __native_flush_tlb_global_irq_disabled(void)
  216. {
  217. unsigned long cr4;
  218. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  219. /* clear PGE */
  220. native_write_cr4(cr4 & ~X86_CR4_PGE);
  221. /* write old PGE again and flush TLBs */
  222. native_write_cr4(cr4);
  223. }
  224. static inline void __native_flush_tlb_global(void)
  225. {
  226. unsigned long flags;
  227. if (static_cpu_has(X86_FEATURE_INVPCID)) {
  228. /*
  229. * Using INVPCID is considerably faster than a pair of writes
  230. * to CR4 sandwiched inside an IRQ flag save/restore.
  231. */
  232. invpcid_flush_all();
  233. return;
  234. }
  235. /*
  236. * Read-modify-write to CR4 - protect it from preemption and
  237. * from interrupts. (Use the raw variant because this code can
  238. * be called from deep inside debugging code.)
  239. */
  240. raw_local_irq_save(flags);
  241. __native_flush_tlb_global_irq_disabled();
  242. raw_local_irq_restore(flags);
  243. }
  244. static inline void __native_flush_tlb_single(unsigned long addr)
  245. {
  246. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  247. }
  248. static inline void __flush_tlb_all(void)
  249. {
  250. if (boot_cpu_has(X86_FEATURE_PGE))
  251. __flush_tlb_global();
  252. else
  253. __flush_tlb();
  254. /*
  255. * Note: if we somehow had PCID but not PGE, then this wouldn't work --
  256. * we'd end up flushing kernel translations for the current ASID but
  257. * we might fail to flush kernel translations for other cached ASIDs.
  258. *
  259. * To avoid this issue, we force PCID off if PGE is off.
  260. */
  261. }
  262. static inline void __flush_tlb_one(unsigned long addr)
  263. {
  264. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  265. __flush_tlb_single(addr);
  266. }
  267. #define TLB_FLUSH_ALL -1UL
  268. /*
  269. * TLB flushing:
  270. *
  271. * - flush_tlb_all() flushes all processes TLBs
  272. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  273. * - flush_tlb_page(vma, vmaddr) flushes one page
  274. * - flush_tlb_range(vma, start, end) flushes a range of pages
  275. * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
  276. * - flush_tlb_others(cpumask, info) flushes TLBs on other cpus
  277. *
  278. * ..but the i386 has somewhat limited tlb flushing capabilities,
  279. * and page-granular flushes are available only on i486 and up.
  280. */
  281. struct flush_tlb_info {
  282. /*
  283. * We support several kinds of flushes.
  284. *
  285. * - Fully flush a single mm. .mm will be set, .end will be
  286. * TLB_FLUSH_ALL, and .new_tlb_gen will be the tlb_gen to
  287. * which the IPI sender is trying to catch us up.
  288. *
  289. * - Partially flush a single mm. .mm will be set, .start and
  290. * .end will indicate the range, and .new_tlb_gen will be set
  291. * such that the changes between generation .new_tlb_gen-1 and
  292. * .new_tlb_gen are entirely contained in the indicated range.
  293. *
  294. * - Fully flush all mms whose tlb_gens have been updated. .mm
  295. * will be NULL, .end will be TLB_FLUSH_ALL, and .new_tlb_gen
  296. * will be zero.
  297. */
  298. struct mm_struct *mm;
  299. unsigned long start;
  300. unsigned long end;
  301. u64 new_tlb_gen;
  302. };
  303. #define local_flush_tlb() __flush_tlb()
  304. #define flush_tlb_mm(mm) flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
  305. #define flush_tlb_range(vma, start, end) \
  306. flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
  307. extern void flush_tlb_all(void);
  308. extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  309. unsigned long end, unsigned long vmflag);
  310. extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  311. static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
  312. {
  313. flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, VM_NONE);
  314. }
  315. void native_flush_tlb_others(const struct cpumask *cpumask,
  316. const struct flush_tlb_info *info);
  317. static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
  318. struct mm_struct *mm)
  319. {
  320. inc_mm_tlb_gen(mm);
  321. cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
  322. }
  323. extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
  324. #ifndef CONFIG_PARAVIRT
  325. #define flush_tlb_others(mask, info) \
  326. native_flush_tlb_others(mask, info)
  327. #endif
  328. #endif /* _ASM_X86_TLBFLUSH_H */