1 #ifndef _ASM_X86_TLBFLUSH_H
2 #define _ASM_X86_TLBFLUSH_H
5 #include <linux/sched.h>
7 #include <asm/processor.h>
8 #include <asm/cpufeature.h>
9 #include <asm/special_insns.h>
12 static inline void __invpcid(unsigned long pcid, unsigned long addr,
15 struct { u64 d[2]; } desc = { { pcid, addr } };
18 * The memory clobber is because the whole point is to invalidate
19 * stale TLB entries and, especially if we're flushing global
20 * mappings, we don't want the compiler to reorder any subsequent
21 * memory accesses before the TLB flush.
23 * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
24 * invpcid (%rcx), %rax in long mode.
26 asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
27 : : "m" (desc), "a" (type), "c" (&desc) : "memory");
30 #define INVPCID_TYPE_INDIV_ADDR 0
31 #define INVPCID_TYPE_SINGLE_CTXT 1
32 #define INVPCID_TYPE_ALL_INCL_GLOBAL 2
33 #define INVPCID_TYPE_ALL_NON_GLOBAL 3
35 /* Flush all mappings for a given pcid and addr, not including globals. */
36 static inline void invpcid_flush_one(unsigned long pcid,
39 __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
42 /* Flush all mappings for a given PCID, not including globals. */
43 static inline void invpcid_flush_single_context(unsigned long pcid)
45 __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
48 /* Flush all mappings, including globals, for all PCIDs. */
49 static inline void invpcid_flush_all(void)
51 __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
54 /* Flush all mappings for all PCIDs except globals. */
55 static inline void invpcid_flush_all_nonglobals(void)
57 __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
60 static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
65 * Bump the generation count. This also serves as a full barrier
66 * that synchronizes with switch_mm(): callers are required to order
67 * their read of mm_cpumask after their writes to the paging
70 smp_mb__before_atomic();
71 new_tlb_gen = atomic64_inc_return(&mm->context.tlb_gen);
72 smp_mb__after_atomic();
77 #ifdef CONFIG_PARAVIRT
78 #include <asm/paravirt.h>
80 #define __flush_tlb() __native_flush_tlb()
81 #define __flush_tlb_global() __native_flush_tlb_global()
82 #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
86 * 6 because 6 should be plenty and struct tlb_state will fit in
89 #define TLB_NR_DYN_ASIDS 6
98 * cpu_tlbstate.loaded_mm should match CR3 whenever interrupts
99 * are on. This means that it may not match current->active_mm,
100 * which will contain the previous user mm when we're in lazy TLB
101 * mode even if we've already switched back to swapper_pg_dir.
103 struct mm_struct *loaded_mm;
108 * Access to this CR4 shadow and to H/W CR4 is protected by
109 * disabling interrupts when modifying either one.
114 * This is a list of all contexts that might exist in the TLB.
115 * There is one per ASID that we use, and the ASID (what the
116 * CPU calls PCID) is the index into ctxts.
118 * For each context, ctx_id indicates which mm the TLB's user
119 * entries came from. As an invariant, the TLB will never
120 * contain entries that are out-of-date as when that mm reached
121 * the tlb_gen in the list.
123 * To be clear, this means that it's legal for the TLB code to
124 * flush the TLB without updating tlb_gen. This can happen
125 * (for now, at least) due to paravirt remote flushes.
127 * NB: context 0 is a bit special, since it's also used by
128 * various bits of init code. This is fine -- code that
129 * isn't aware of PCID will end up harmlessly flushing
132 struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
134 DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
136 /* Initialize cr4 shadow for this CPU. */
137 static inline void cr4_init_shadow(void)
139 this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
142 /* Set in this cpu's CR4. */
143 static inline void cr4_set_bits(unsigned long mask)
147 cr4 = this_cpu_read(cpu_tlbstate.cr4);
148 if ((cr4 | mask) != cr4) {
150 this_cpu_write(cpu_tlbstate.cr4, cr4);
155 /* Clear in this cpu's CR4. */
156 static inline void cr4_clear_bits(unsigned long mask)
160 cr4 = this_cpu_read(cpu_tlbstate.cr4);
161 if ((cr4 & ~mask) != cr4) {
163 this_cpu_write(cpu_tlbstate.cr4, cr4);
168 static inline void cr4_toggle_bits(unsigned long mask)
172 cr4 = this_cpu_read(cpu_tlbstate.cr4);
174 this_cpu_write(cpu_tlbstate.cr4, cr4);
178 /* Read the CR4 shadow. */
179 static inline unsigned long cr4_read_shadow(void)
181 return this_cpu_read(cpu_tlbstate.cr4);
185 * Save some of cr4 feature set we're using (e.g. Pentium 4MB
186 * enable and PPro Global page enable), so that any CPU's that boot
187 * up after us can get the correct flags. This should only be used
188 * during boot on the boot cpu.
190 extern unsigned long mmu_cr4_features;
191 extern u32 *trampoline_cr4_features;
193 static inline void cr4_set_bits_and_update_boot(unsigned long mask)
195 mmu_cr4_features |= mask;
196 if (trampoline_cr4_features)
197 *trampoline_cr4_features = mmu_cr4_features;
201 static inline void __native_flush_tlb(void)
204 * If current->mm == NULL then we borrow a mm which may change during a
205 * task switch and therefore we must not be preempted while we write CR3
209 native_write_cr3(__native_read_cr3());
213 static inline void __native_flush_tlb_global_irq_disabled(void)
217 cr4 = this_cpu_read(cpu_tlbstate.cr4);
219 native_write_cr4(cr4 & ~X86_CR4_PGE);
220 /* write old PGE again and flush TLBs */
221 native_write_cr4(cr4);
224 static inline void __native_flush_tlb_global(void)
228 if (static_cpu_has(X86_FEATURE_INVPCID)) {
230 * Using INVPCID is considerably faster than a pair of writes
231 * to CR4 sandwiched inside an IRQ flag save/restore.
238 * Read-modify-write to CR4 - protect it from preemption and
239 * from interrupts. (Use the raw variant because this code can
240 * be called from deep inside debugging code.)
242 raw_local_irq_save(flags);
244 __native_flush_tlb_global_irq_disabled();
246 raw_local_irq_restore(flags);
249 static inline void __native_flush_tlb_single(unsigned long addr)
251 asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
254 static inline void __flush_tlb_all(void)
256 if (boot_cpu_has(X86_FEATURE_PGE))
257 __flush_tlb_global();
262 * Note: if we somehow had PCID but not PGE, then this wouldn't work --
263 * we'd end up flushing kernel translations for the current ASID but
264 * we might fail to flush kernel translations for other cached ASIDs.
266 * To avoid this issue, we force PCID off if PGE is off.
270 static inline void __flush_tlb_one(unsigned long addr)
272 count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
273 __flush_tlb_single(addr);
276 #define TLB_FLUSH_ALL -1UL
281 * - flush_tlb_all() flushes all processes TLBs
282 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
283 * - flush_tlb_page(vma, vmaddr) flushes one page
284 * - flush_tlb_range(vma, start, end) flushes a range of pages
285 * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
286 * - flush_tlb_others(cpumask, info) flushes TLBs on other cpus
288 * ..but the i386 has somewhat limited tlb flushing capabilities,
289 * and page-granular flushes are available only on i486 and up.
291 struct flush_tlb_info {
293 * We support several kinds of flushes.
295 * - Fully flush a single mm. .mm will be set, .end will be
296 * TLB_FLUSH_ALL, and .new_tlb_gen will be the tlb_gen to
297 * which the IPI sender is trying to catch us up.
299 * - Partially flush a single mm. .mm will be set, .start and
300 * .end will indicate the range, and .new_tlb_gen will be set
301 * such that the changes between generation .new_tlb_gen-1 and
302 * .new_tlb_gen are entirely contained in the indicated range.
304 * - Fully flush all mms whose tlb_gens have been updated. .mm
305 * will be NULL, .end will be TLB_FLUSH_ALL, and .new_tlb_gen
308 struct mm_struct *mm;
314 #define local_flush_tlb() __flush_tlb()
316 #define flush_tlb_mm(mm) flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
318 #define flush_tlb_range(vma, start, end) \
319 flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
321 extern void flush_tlb_all(void);
322 extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
323 unsigned long end, unsigned long vmflag);
324 extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
326 static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
328 flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, VM_NONE);
331 void native_flush_tlb_others(const struct cpumask *cpumask,
332 const struct flush_tlb_info *info);
334 static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
335 struct mm_struct *mm)
338 cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
341 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
343 #ifndef CONFIG_PARAVIRT
344 #define flush_tlb_others(mask, info) \
345 native_flush_tlb_others(mask, info)
348 #endif /* _ASM_X86_TLBFLUSH_H */