H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_MMU_CONTEXT_H |
| 2 | #define _ASM_X86_MMU_CONTEXT_H |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 3 | |
| 4 | #include <asm/desc.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 6 | #include <linux/mm_types.h> |
| 7 | |
| 8 | #include <trace/events/tlb.h> |
| 9 | |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 10 | #include <asm/pgalloc.h> |
| 11 | #include <asm/tlbflush.h> |
| 12 | #include <asm/paravirt.h> |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame^] | 13 | #include <asm/mpx.h> |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 14 | #ifndef CONFIG_PARAVIRT |
| 15 | #include <asm-generic/mm_hooks.h> |
| 16 | |
| 17 | static inline void paravirt_activate_mm(struct mm_struct *prev, |
| 18 | struct mm_struct *next) |
| 19 | { |
| 20 | } |
| 21 | #endif /* !CONFIG_PARAVIRT */ |
| 22 | |
| 23 | /* |
| 24 | * Used for LDT copy/destruction. |
| 25 | */ |
| 26 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm); |
| 27 | void destroy_context(struct mm_struct *mm); |
| 28 | |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 29 | |
| 30 | static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) |
| 31 | { |
| 32 | #ifdef CONFIG_SMP |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 33 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) |
| 34 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_LAZY); |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 35 | #endif |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 39 | struct task_struct *tsk) |
| 40 | { |
| 41 | unsigned cpu = smp_processor_id(); |
| 42 | |
| 43 | if (likely(prev != next)) { |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 44 | #ifdef CONFIG_SMP |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 45 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 46 | this_cpu_write(cpu_tlbstate.active_mm, next); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 47 | #endif |
Rusty Russell | 78f1c4d | 2009-09-24 09:34:51 -0600 | [diff] [blame] | 48 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 49 | |
| 50 | /* Re-load page tables */ |
| 51 | load_cr3(next->pgd); |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 52 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 53 | |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 54 | /* Stop flush ipis for the previous mm */ |
Suresh Siddha | 831d52bc1 | 2011-02-03 12:20:04 -0800 | [diff] [blame] | 55 | cpumask_clear_cpu(cpu, mm_cpumask(prev)); |
| 56 | |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 57 | /* Load the LDT, if the LDT is different: */ |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 58 | if (unlikely(prev->context.ldt != next->context.ldt)) |
| 59 | load_LDT_nolock(&next->context); |
| 60 | } |
| 61 | #ifdef CONFIG_SMP |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 62 | else { |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 63 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 64 | BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 65 | |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 66 | if (!cpumask_test_cpu(cpu, mm_cpumask(next))) { |
| 67 | /* |
| 68 | * On established mms, the mm_cpumask is only changed |
| 69 | * from irq context, from ptep_clear_flush() while in |
| 70 | * lazy tlb mode, and here. Irqs are blocked during |
| 71 | * schedule, protecting us from simultaneous changes. |
| 72 | */ |
| 73 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 74 | /* |
| 75 | * We were in lazy tlb mode and leave_mm disabled |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 76 | * tlb flush IPI delivery. We must reload CR3 |
| 77 | * to make sure to use no freed page tables. |
| 78 | */ |
| 79 | load_cr3(next->pgd); |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 80 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 81 | load_LDT_nolock(&next->context); |
| 82 | } |
| 83 | } |
| 84 | #endif |
| 85 | } |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 86 | |
| 87 | #define activate_mm(prev, next) \ |
| 88 | do { \ |
| 89 | paravirt_activate_mm((prev), (next)); \ |
| 90 | switch_mm((prev), (next), NULL); \ |
| 91 | } while (0); |
| 92 | |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 93 | #ifdef CONFIG_X86_32 |
| 94 | #define deactivate_mm(tsk, mm) \ |
| 95 | do { \ |
Tejun Heo | ccbeed3 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 96 | lazy_load_gs(0); \ |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 97 | } while (0) |
| 98 | #else |
| 99 | #define deactivate_mm(tsk, mm) \ |
| 100 | do { \ |
| 101 | load_gs_index(0); \ |
| 102 | loadsegment(fs, 0); \ |
| 103 | } while (0) |
| 104 | #endif |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 105 | |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame^] | 106 | static inline void arch_bprm_mm_init(struct mm_struct *mm, |
| 107 | struct vm_area_struct *vma) |
| 108 | { |
| 109 | mpx_mm_init(mm); |
| 110 | } |
| 111 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 112 | #endif /* _ASM_X86_MMU_CONTEXT_H */ |