H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_TLBFLUSH_H |
| 2 | #define _ASM_X86_TLBFLUSH_H |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 3 | |
| 4 | #include <linux/mm.h> |
| 5 | #include <linux/sched.h> |
| 6 | |
| 7 | #include <asm/processor.h> |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 8 | #include <asm/special_insns.h> |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 9 | |
| 10 | #ifdef CONFIG_PARAVIRT |
| 11 | #include <asm/paravirt.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 12 | #else |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 13 | #define __flush_tlb() __native_flush_tlb() |
| 14 | #define __flush_tlb_global() __native_flush_tlb_global() |
| 15 | #define __flush_tlb_single(addr) __native_flush_tlb_single(addr) |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 16 | #endif |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 17 | |
| 18 | static inline void __native_flush_tlb(void) |
| 19 | { |
Chris Wright | d7285c6 | 2009-04-23 10:21:38 -0700 | [diff] [blame] | 20 | native_write_cr3(native_read_cr3()); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static inline void __native_flush_tlb_global(void) |
| 24 | { |
Ingo Molnar | b1979a5 | 2008-05-12 21:21:15 +0200 | [diff] [blame] | 25 | unsigned long flags; |
| 26 | unsigned long cr4; |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 27 | |
Ingo Molnar | b1979a5 | 2008-05-12 21:21:15 +0200 | [diff] [blame] | 28 | /* |
| 29 | * Read-modify-write to CR4 - protect it from preemption and |
| 30 | * from interrupts. (Use the raw variant because this code can |
| 31 | * be called from deep inside debugging code.) |
| 32 | */ |
| 33 | raw_local_irq_save(flags); |
| 34 | |
Chris Wright | d7285c6 | 2009-04-23 10:21:38 -0700 | [diff] [blame] | 35 | cr4 = native_read_cr4(); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 36 | /* clear PGE */ |
Chris Wright | d7285c6 | 2009-04-23 10:21:38 -0700 | [diff] [blame] | 37 | native_write_cr4(cr4 & ~X86_CR4_PGE); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 38 | /* write old PGE again and flush TLBs */ |
Chris Wright | d7285c6 | 2009-04-23 10:21:38 -0700 | [diff] [blame] | 39 | native_write_cr4(cr4); |
Ingo Molnar | b1979a5 | 2008-05-12 21:21:15 +0200 | [diff] [blame] | 40 | |
| 41 | raw_local_irq_restore(flags); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static inline void __native_flush_tlb_single(unsigned long addr) |
| 45 | { |
Joe Perches | 94cf8de | 2008-03-23 01:03:45 -0700 | [diff] [blame] | 46 | asm volatile("invlpg (%0)" ::"r" (addr) : "memory"); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static inline void __flush_tlb_all(void) |
| 50 | { |
| 51 | if (cpu_has_pge) |
| 52 | __flush_tlb_global(); |
| 53 | else |
| 54 | __flush_tlb(); |
| 55 | } |
| 56 | |
| 57 | static inline void __flush_tlb_one(unsigned long addr) |
| 58 | { |
| 59 | if (cpu_has_invlpg) |
| 60 | __flush_tlb_single(addr); |
| 61 | else |
| 62 | __flush_tlb(); |
| 63 | } |
| 64 | |
Alex Shi | 3e7f3db | 2012-05-10 18:01:59 +0800 | [diff] [blame] | 65 | #define TLB_FLUSH_ALL -1UL |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * TLB flushing: |
| 69 | * |
| 70 | * - flush_tlb() flushes the current mm struct TLBs |
| 71 | * - flush_tlb_all() flushes all processes TLBs |
| 72 | * - flush_tlb_mm(mm) flushes the specified mm context TLB's |
| 73 | * - flush_tlb_page(vma, vmaddr) flushes one page |
| 74 | * - flush_tlb_range(vma, start, end) flushes a range of pages |
| 75 | * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages |
| 76 | * - flush_tlb_others(cpumask, mm, va) flushes TLBs on other cpus |
| 77 | * |
| 78 | * ..but the i386 has somewhat limited tlb flushing capabilities, |
| 79 | * and page-granular flushes are available only on i486 and up. |
| 80 | * |
| 81 | * x86-64 can only flush individual pages or full VMs. For a range flush |
| 82 | * we always do the full VM. Might be worth trying if for a small |
| 83 | * range a few INVLPGs in a row are a win. |
| 84 | */ |
| 85 | |
| 86 | #ifndef CONFIG_SMP |
| 87 | |
| 88 | #define flush_tlb() __flush_tlb() |
| 89 | #define flush_tlb_all() __flush_tlb_all() |
| 90 | #define local_flush_tlb() __flush_tlb() |
| 91 | |
| 92 | static inline void flush_tlb_mm(struct mm_struct *mm) |
| 93 | { |
| 94 | if (mm == current->active_mm) |
| 95 | __flush_tlb(); |
| 96 | } |
| 97 | |
| 98 | static inline void flush_tlb_page(struct vm_area_struct *vma, |
| 99 | unsigned long addr) |
| 100 | { |
| 101 | if (vma->vm_mm == current->active_mm) |
| 102 | __flush_tlb_one(addr); |
| 103 | } |
| 104 | |
| 105 | static inline void flush_tlb_range(struct vm_area_struct *vma, |
| 106 | unsigned long start, unsigned long end) |
| 107 | { |
| 108 | if (vma->vm_mm == current->active_mm) |
| 109 | __flush_tlb(); |
| 110 | } |
| 111 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 112 | static inline void native_flush_tlb_others(const struct cpumask *cpumask, |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 113 | struct mm_struct *mm, |
| 114 | unsigned long va) |
| 115 | { |
| 116 | } |
| 117 | |
Alex Nixon | 913da64 | 2008-09-03 14:30:23 +0100 | [diff] [blame] | 118 | static inline void reset_lazy_tlbstate(void) |
| 119 | { |
| 120 | } |
| 121 | |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 122 | #else /* SMP */ |
| 123 | |
| 124 | #include <asm/smp.h> |
| 125 | |
| 126 | #define local_flush_tlb() __flush_tlb() |
| 127 | |
| 128 | extern void flush_tlb_all(void); |
| 129 | extern void flush_tlb_current_task(void); |
| 130 | extern void flush_tlb_mm(struct mm_struct *); |
| 131 | extern void flush_tlb_page(struct vm_area_struct *, unsigned long); |
| 132 | |
| 133 | #define flush_tlb() flush_tlb_current_task() |
| 134 | |
| 135 | static inline void flush_tlb_range(struct vm_area_struct *vma, |
| 136 | unsigned long start, unsigned long end) |
| 137 | { |
| 138 | flush_tlb_mm(vma->vm_mm); |
| 139 | } |
| 140 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 141 | void native_flush_tlb_others(const struct cpumask *cpumask, |
| 142 | struct mm_struct *mm, unsigned long va); |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 143 | |
| 144 | #define TLBSTATE_OK 1 |
| 145 | #define TLBSTATE_LAZY 2 |
| 146 | |
Joe Perches | 94cf8de | 2008-03-23 01:03:45 -0700 | [diff] [blame] | 147 | struct tlb_state { |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 148 | struct mm_struct *active_mm; |
| 149 | int state; |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 150 | }; |
David Howells | 9b8de74 | 2009-04-21 23:00:24 +0100 | [diff] [blame] | 151 | DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate); |
Alex Nixon | 913da64 | 2008-09-03 14:30:23 +0100 | [diff] [blame] | 152 | |
Alex Nixon | 913da64 | 2008-09-03 14:30:23 +0100 | [diff] [blame] | 153 | static inline void reset_lazy_tlbstate(void) |
| 154 | { |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 155 | this_cpu_write(cpu_tlbstate.state, 0); |
| 156 | this_cpu_write(cpu_tlbstate.active_mm, &init_mm); |
Alex Nixon | 913da64 | 2008-09-03 14:30:23 +0100 | [diff] [blame] | 157 | } |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 158 | |
| 159 | #endif /* SMP */ |
| 160 | |
| 161 | #ifndef CONFIG_PARAVIRT |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 162 | #define flush_tlb_others(mask, mm, va) native_flush_tlb_others(mask, mm, va) |
Thomas Gleixner | d291cf8 | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 163 | #endif |
| 164 | |
| 165 | static inline void flush_tlb_kernel_range(unsigned long start, |
| 166 | unsigned long end) |
| 167 | { |
| 168 | flush_tlb_all(); |
| 169 | } |
| 170 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 171 | #endif /* _ASM_X86_TLBFLUSH_H */ |