tlbflush.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_TLBFLUSH_H
  3. #define _ASM_X86_TLBFLUSH_H
  4. #include <linux/mm.h>
  5. #include <linux/sched.h>
  6. #include <asm/processor.h>
  7. #include <asm/cpufeature.h>
  8. #include <asm/special_insns.h>
  9. #include <asm/smp.h>
  10. #include <asm/invpcid.h>
  11. #include <asm/pti.h>
  12. #include <asm/processor-flags.h>
  13. /*
  14. * The x86 feature is called PCID (Process Context IDentifier). It is similar
  15. * to what is traditionally called ASID on the RISC processors.
  16. *
  17. * We don't use the traditional ASID implementation, where each process/mm gets
  18. * its own ASID and flush/restart when we run out of ASID space.
  19. *
  20. * Instead we have a small per-cpu array of ASIDs and cache the last few mm's
  21. * that came by on this CPU, allowing cheaper switch_mm between processes on
  22. * this CPU.
  23. *
  24. * We end up with different spaces for different things. To avoid confusion we
  25. * use different names for each of them:
  26. *
  27. * ASID - [0, TLB_NR_DYN_ASIDS-1]
  28. * the canonical identifier for an mm
  29. *
  30. * kPCID - [1, TLB_NR_DYN_ASIDS]
  31. * the value we write into the PCID part of CR3; corresponds to the
  32. * ASID+1, because PCID 0 is special.
  33. *
  34. * uPCID - [2048 + 1, 2048 + TLB_NR_DYN_ASIDS]
  35. * for KPTI each mm has two address spaces and thus needs two
  36. * PCID values, but we can still do with a single ASID denomination
  37. * for each mm. Corresponds to kPCID + 2048.
  38. *
  39. */
  40. /* There are 12 bits of space for ASIDS in CR3 */
  41. #define CR3_HW_ASID_BITS 12
  42. /*
  43. * When enabled, PAGE_TABLE_ISOLATION consumes a single bit for
  44. * user/kernel switches
  45. */
  46. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  47. # define PTI_CONSUMED_PCID_BITS 1
  48. #else
  49. # define PTI_CONSUMED_PCID_BITS 0
  50. #endif
  51. #define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS)
  52. /*
  53. * ASIDs are zero-based: 0->MAX_AVAIL_ASID are valid. -1 below to account
  54. * for them being zero-based. Another -1 is because PCID 0 is reserved for
  55. * use by non-PCID-aware users.
  56. */
  57. #define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2)
  58. /*
  59. * 6 because 6 should be plenty and struct tlb_state will fit in two cache
  60. * lines.
  61. */
  62. #define TLB_NR_DYN_ASIDS 6
  63. /*
  64. * Given @asid, compute kPCID
  65. */
  66. static inline u16 kern_pcid(u16 asid)
  67. {
  68. VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
  69. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  70. /*
  71. * Make sure that the dynamic ASID space does not confict with the
  72. * bit we are using to switch between user and kernel ASIDs.
  73. */
  74. BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_SWITCH_BIT));
  75. /*
  76. * The ASID being passed in here should have respected the
  77. * MAX_ASID_AVAILABLE and thus never have the switch bit set.
  78. */
  79. VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_SWITCH_BIT));
  80. #endif
  81. /*
  82. * The dynamically-assigned ASIDs that get passed in are small
  83. * (<TLB_NR_DYN_ASIDS). They never have the high switch bit set,
  84. * so do not bother to clear it.
  85. *
  86. * If PCID is on, ASID-aware code paths put the ASID+1 into the
  87. * PCID bits. This serves two purposes. It prevents a nasty
  88. * situation in which PCID-unaware code saves CR3, loads some other
  89. * value (with PCID == 0), and then restores CR3, thus corrupting
  90. * the TLB for ASID 0 if the saved ASID was nonzero. It also means
  91. * that any bugs involving loading a PCID-enabled CR3 with
  92. * CR4.PCIDE off will trigger deterministically.
  93. */
  94. return asid + 1;
  95. }
  96. /*
  97. * Given @asid, compute uPCID
  98. */
  99. static inline u16 user_pcid(u16 asid)
  100. {
  101. u16 ret = kern_pcid(asid);
  102. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  103. ret |= 1 << X86_CR3_PTI_SWITCH_BIT;
  104. #endif
  105. return ret;
  106. }
  107. struct pgd_t;
  108. static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
  109. {
  110. if (static_cpu_has(X86_FEATURE_PCID)) {
  111. return __sme_pa(pgd) | kern_pcid(asid);
  112. } else {
  113. VM_WARN_ON_ONCE(asid != 0);
  114. return __sme_pa(pgd);
  115. }
  116. }
  117. static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
  118. {
  119. VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
  120. VM_WARN_ON_ONCE(!this_cpu_has(X86_FEATURE_PCID));
  121. return __sme_pa(pgd) | kern_pcid(asid) | CR3_NOFLUSH;
  122. }
  123. #ifdef CONFIG_PARAVIRT
  124. #include <asm/paravirt.h>
  125. #else
  126. #define __flush_tlb() __native_flush_tlb()
  127. #define __flush_tlb_global() __native_flush_tlb_global()
  128. #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
  129. #endif
  130. static inline bool tlb_defer_switch_to_init_mm(void)
  131. {
  132. /*
  133. * If we have PCID, then switching to init_mm is reasonably
  134. * fast. If we don't have PCID, then switching to init_mm is
  135. * quite slow, so we try to defer it in the hopes that we can
  136. * avoid it entirely. The latter approach runs the risk of
  137. * receiving otherwise unnecessary IPIs.
  138. *
  139. * This choice is just a heuristic. The tlb code can handle this
  140. * function returning true or false regardless of whether we have
  141. * PCID.
  142. */
  143. return !static_cpu_has(X86_FEATURE_PCID);
  144. }
  145. struct tlb_context {
  146. u64 ctx_id;
  147. u64 tlb_gen;
  148. };
  149. struct tlb_state {
  150. /*
  151. * cpu_tlbstate.loaded_mm should match CR3 whenever interrupts
  152. * are on. This means that it may not match current->active_mm,
  153. * which will contain the previous user mm when we're in lazy TLB
  154. * mode even if we've already switched back to swapper_pg_dir.
  155. */
  156. struct mm_struct *loaded_mm;
  157. u16 loaded_mm_asid;
  158. u16 next_asid;
  159. /*
  160. * We can be in one of several states:
  161. *
  162. * - Actively using an mm. Our CPU's bit will be set in
  163. * mm_cpumask(loaded_mm) and is_lazy == false;
  164. *
  165. * - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit
  166. * will not be set in mm_cpumask(&init_mm) and is_lazy == false.
  167. *
  168. * - Lazily using a real mm. loaded_mm != &init_mm, our bit
  169. * is set in mm_cpumask(loaded_mm), but is_lazy == true.
  170. * We're heuristically guessing that the CR3 load we
  171. * skipped more than makes up for the overhead added by
  172. * lazy mode.
  173. */
  174. bool is_lazy;
  175. /*
  176. * If set we changed the page tables in such a way that we
  177. * needed an invalidation of all contexts (aka. PCIDs / ASIDs).
  178. * This tells us to go invalidate all the non-loaded ctxs[]
  179. * on the next context switch.
  180. *
  181. * The current ctx was kept up-to-date as it ran and does not
  182. * need to be invalidated.
  183. */
  184. bool invalidate_other;
  185. /*
  186. * Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate
  187. * the corresponding user PCID needs a flush next time we
  188. * switch to it; see SWITCH_TO_USER_CR3.
  189. */
  190. unsigned short user_pcid_flush_mask;
  191. /*
  192. * Access to this CR4 shadow and to H/W CR4 is protected by
  193. * disabling interrupts when modifying either one.
  194. */
  195. unsigned long cr4;
  196. /*
  197. * This is a list of all contexts that might exist in the TLB.
  198. * There is one per ASID that we use, and the ASID (what the
  199. * CPU calls PCID) is the index into ctxts.
  200. *
  201. * For each context, ctx_id indicates which mm the TLB's user
  202. * entries came from. As an invariant, the TLB will never
  203. * contain entries that are out-of-date as when that mm reached
  204. * the tlb_gen in the list.
  205. *
  206. * To be clear, this means that it's legal for the TLB code to
  207. * flush the TLB without updating tlb_gen. This can happen
  208. * (for now, at least) due to paravirt remote flushes.
  209. *
  210. * NB: context 0 is a bit special, since it's also used by
  211. * various bits of init code. This is fine -- code that
  212. * isn't aware of PCID will end up harmlessly flushing
  213. * context 0.
  214. */
  215. struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
  216. };
  217. DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
  218. /* Initialize cr4 shadow for this CPU. */
  219. static inline void cr4_init_shadow(void)
  220. {
  221. this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
  222. }
  223. static inline void __cr4_set(unsigned long cr4)
  224. {
  225. lockdep_assert_irqs_disabled();
  226. this_cpu_write(cpu_tlbstate.cr4, cr4);
  227. __write_cr4(cr4);
  228. }
  229. /* Set in this cpu's CR4. */
  230. static inline void cr4_set_bits(unsigned long mask)
  231. {
  232. unsigned long cr4, flags;
  233. local_irq_save(flags);
  234. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  235. if ((cr4 | mask) != cr4)
  236. __cr4_set(cr4 | mask);
  237. local_irq_restore(flags);
  238. }
  239. /* Clear in this cpu's CR4. */
  240. static inline void cr4_clear_bits(unsigned long mask)
  241. {
  242. unsigned long cr4, flags;
  243. local_irq_save(flags);
  244. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  245. if ((cr4 & ~mask) != cr4)
  246. __cr4_set(cr4 & ~mask);
  247. local_irq_restore(flags);
  248. }
  249. static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
  250. {
  251. unsigned long cr4;
  252. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  253. __cr4_set(cr4 ^ mask);
  254. }
  255. /* Read the CR4 shadow. */
  256. static inline unsigned long cr4_read_shadow(void)
  257. {
  258. return this_cpu_read(cpu_tlbstate.cr4);
  259. }
  260. /*
  261. * Mark all other ASIDs as invalid, preserves the current.
  262. */
  263. static inline void invalidate_other_asid(void)
  264. {
  265. this_cpu_write(cpu_tlbstate.invalidate_other, true);
  266. }
  267. /*
  268. * Save some of cr4 feature set we're using (e.g. Pentium 4MB
  269. * enable and PPro Global page enable), so that any CPU's that boot
  270. * up after us can get the correct flags. This should only be used
  271. * during boot on the boot cpu.
  272. */
  273. extern unsigned long mmu_cr4_features;
  274. extern u32 *trampoline_cr4_features;
  275. static inline void cr4_set_bits_and_update_boot(unsigned long mask)
  276. {
  277. mmu_cr4_features |= mask;
  278. if (trampoline_cr4_features)
  279. *trampoline_cr4_features = mmu_cr4_features;
  280. cr4_set_bits(mask);
  281. }
  282. extern void initialize_tlbstate_and_flush(void);
  283. /*
  284. * Given an ASID, flush the corresponding user ASID. We can delay this
  285. * until the next time we switch to it.
  286. *
  287. * See SWITCH_TO_USER_CR3.
  288. */
  289. static inline void invalidate_user_asid(u16 asid)
  290. {
  291. /* There is no user ASID if address space separation is off */
  292. if (!IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
  293. return;
  294. /*
  295. * We only have a single ASID if PCID is off and the CR3
  296. * write will have flushed it.
  297. */
  298. if (!cpu_feature_enabled(X86_FEATURE_PCID))
  299. return;
  300. if (!static_cpu_has(X86_FEATURE_PTI))
  301. return;
  302. __set_bit(kern_pcid(asid),
  303. (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask));
  304. }
  305. /*
  306. * flush the entire current user mapping
  307. */
  308. static inline void __native_flush_tlb(void)
  309. {
  310. /*
  311. * Preemption or interrupts must be disabled to protect the access
  312. * to the per CPU variable and to prevent being preempted between
  313. * read_cr3() and write_cr3().
  314. */
  315. WARN_ON_ONCE(preemptible());
  316. invalidate_user_asid(this_cpu_read(cpu_tlbstate.loaded_mm_asid));
  317. /* If current->mm == NULL then the read_cr3() "borrows" an mm */
  318. native_write_cr3(__native_read_cr3());
  319. }
  320. /*
  321. * flush everything
  322. */
  323. static inline void __native_flush_tlb_global(void)
  324. {
  325. unsigned long cr4, flags;
  326. if (static_cpu_has(X86_FEATURE_INVPCID)) {
  327. /*
  328. * Using INVPCID is considerably faster than a pair of writes
  329. * to CR4 sandwiched inside an IRQ flag save/restore.
  330. *
  331. * Note, this works with CR4.PCIDE=0 or 1.
  332. */
  333. invpcid_flush_all();
  334. return;
  335. }
  336. /*
  337. * Read-modify-write to CR4 - protect it from preemption and
  338. * from interrupts. (Use the raw variant because this code can
  339. * be called from deep inside debugging code.)
  340. */
  341. raw_local_irq_save(flags);
  342. cr4 = this_cpu_read(cpu_tlbstate.cr4);
  343. /* toggle PGE */
  344. native_write_cr4(cr4 ^ X86_CR4_PGE);
  345. /* write old PGE again and flush TLBs */
  346. native_write_cr4(cr4);
  347. raw_local_irq_restore(flags);
  348. }
  349. /*
  350. * flush one page in the user mapping
  351. */
  352. static inline void __native_flush_tlb_single(unsigned long addr)
  353. {
  354. u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
  355. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  356. if (!static_cpu_has(X86_FEATURE_PTI))
  357. return;
  358. /*
  359. * Some platforms #GP if we call invpcid(type=1/2) before CR4.PCIDE=1.
  360. * Just use invalidate_user_asid() in case we are called early.
  361. */
  362. if (!this_cpu_has(X86_FEATURE_INVPCID_SINGLE))
  363. invalidate_user_asid(loaded_mm_asid);
  364. else
  365. invpcid_flush_one(user_pcid(loaded_mm_asid), addr);
  366. }
  367. /*
  368. * flush everything
  369. */
  370. static inline void __flush_tlb_all(void)
  371. {
  372. if (boot_cpu_has(X86_FEATURE_PGE)) {
  373. __flush_tlb_global();
  374. } else {
  375. /*
  376. * !PGE -> !PCID (setup_pcid()), thus every flush is total.
  377. */
  378. __flush_tlb();
  379. }
  380. }
  381. /*
  382. * flush one page in the kernel mapping
  383. */
  384. static inline void __flush_tlb_one(unsigned long addr)
  385. {
  386. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  387. __flush_tlb_single(addr);
  388. if (!static_cpu_has(X86_FEATURE_PTI))
  389. return;
  390. /*
  391. * __flush_tlb_single() will have cleared the TLB entry for this ASID,
  392. * but since kernel space is replicated across all, we must also
  393. * invalidate all others.
  394. */
  395. invalidate_other_asid();
  396. }
  397. #define TLB_FLUSH_ALL -1UL
  398. /*
  399. * TLB flushing:
  400. *
  401. * - flush_tlb_all() flushes all processes TLBs
  402. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  403. * - flush_tlb_page(vma, vmaddr) flushes one page
  404. * - flush_tlb_range(vma, start, end) flushes a range of pages
  405. * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
  406. * - flush_tlb_others(cpumask, info) flushes TLBs on other cpus
  407. *
  408. * ..but the i386 has somewhat limited tlb flushing capabilities,
  409. * and page-granular flushes are available only on i486 and up.
  410. */
  411. struct flush_tlb_info {
  412. /*
  413. * We support several kinds of flushes.
  414. *
  415. * - Fully flush a single mm. .mm will be set, .end will be
  416. * TLB_FLUSH_ALL, and .new_tlb_gen will be the tlb_gen to
  417. * which the IPI sender is trying to catch us up.
  418. *
  419. * - Partially flush a single mm. .mm will be set, .start and
  420. * .end will indicate the range, and .new_tlb_gen will be set
  421. * such that the changes between generation .new_tlb_gen-1 and
  422. * .new_tlb_gen are entirely contained in the indicated range.
  423. *
  424. * - Fully flush all mms whose tlb_gens have been updated. .mm
  425. * will be NULL, .end will be TLB_FLUSH_ALL, and .new_tlb_gen
  426. * will be zero.
  427. */
  428. struct mm_struct *mm;
  429. unsigned long start;
  430. unsigned long end;
  431. u64 new_tlb_gen;
  432. };
  433. #define local_flush_tlb() __flush_tlb()
  434. #define flush_tlb_mm(mm) flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
  435. #define flush_tlb_range(vma, start, end) \
  436. flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
  437. extern void flush_tlb_all(void);
  438. extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  439. unsigned long end, unsigned long vmflag);
  440. extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  441. static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
  442. {
  443. flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, VM_NONE);
  444. }
  445. void native_flush_tlb_others(const struct cpumask *cpumask,
  446. const struct flush_tlb_info *info);
  447. static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  448. {
  449. /*
  450. * Bump the generation count. This also serves as a full barrier
  451. * that synchronizes with switch_mm(): callers are required to order
  452. * their read of mm_cpumask after their writes to the paging
  453. * structures.
  454. */
  455. return atomic64_inc_return(&mm->context.tlb_gen);
  456. }
  457. static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
  458. struct mm_struct *mm)
  459. {
  460. inc_mm_tlb_gen(mm);
  461. cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
  462. }
  463. extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
  464. #ifndef CONFIG_PARAVIRT
  465. #define flush_tlb_others(mask, info) \
  466. native_flush_tlb_others(mask, info)
  467. #endif
  468. #endif /* _ASM_X86_TLBFLUSH_H */