- supported.conf: Added sparse_keymap (eeepc_laptop depends on it)
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / include / mach-xen / asm / mmu_context.h
1 #ifndef _ASM_X86_MMU_CONTEXT_H
2 #define _ASM_X86_MMU_CONTEXT_H
3
4 #include <asm/desc.h>
5 #include <asm/atomic.h>
6 #include <asm/pgalloc.h>
7 #include <asm/tlbflush.h>
8
9 void arch_exit_mmap(struct mm_struct *mm);
10 void arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm);
11
12 void mm_pin(struct mm_struct *mm);
13 void mm_unpin(struct mm_struct *mm);
14 void mm_pin_all(void);
15
16 static inline void xen_activate_mm(struct mm_struct *prev,
17                                    struct mm_struct *next)
18 {
19         if (!PagePinned(virt_to_page(next->pgd)))
20                 mm_pin(next);
21 }
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
29
30 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
31 {
32 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN) /* XEN: no lazy tlb */
33         if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
34                 percpu_write(cpu_tlbstate.state, TLBSTATE_LAZY);
35 #endif
36 }
37
38 #define prepare_arch_switch(next)       __prepare_arch_switch()
39
40 static inline void __prepare_arch_switch(void)
41 {
42 #ifdef CONFIG_X86_32
43         /*
44          * Save away %gs. No need to save %fs, as it was saved on the
45          * stack on entry.  No need to save %es and %ds, as those are
46          * always kernel segments while inside the kernel.
47          */
48         lazy_save_gs(current->thread.gs);
49         lazy_load_gs(__KERNEL_STACK_CANARY);
50 #else
51         /*
52          * Save away %es, %ds, %fs and %gs. Must happen before reload
53          * of cr3/ldt (i.e., not in __switch_to).
54          */
55         __asm__ __volatile__ (
56                 "mov %%es,%0 ; mov %%ds,%1 ; mov %%fs,%2 ; mov %%gs,%3"
57                 : "=m" (current->thread.es),
58                   "=m" (current->thread.ds),
59                   "=m" (current->thread.fsindex),
60                   "=m" (current->thread.gsindex) );
61
62         if (current->thread.ds)
63                 __asm__ __volatile__ ( "movl %0,%%ds" : : "r" (0) );
64
65         if (current->thread.es)
66                 __asm__ __volatile__ ( "movl %0,%%es" : : "r" (0) );
67
68         if (current->thread.fsindex) {
69                 __asm__ __volatile__ ( "movl %0,%%fs" : : "r" (0) );
70                 current->thread.fs = 0;
71         }
72
73         if (current->thread.gsindex) {
74                 load_gs_index(0);
75                 current->thread.gs = 0;
76         }
77 #endif
78 }
79
80 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
81                              struct task_struct *tsk)
82 {
83         unsigned cpu = smp_processor_id();
84         struct mmuext_op _op[2 + (sizeof(long) > 4)], *op = _op;
85 #ifdef CONFIG_X86_64
86         pgd_t *upgd;
87 #endif
88
89         if (likely(prev != next)) {
90                 BUG_ON(!xen_feature(XENFEAT_writable_page_tables) &&
91                        !PagePinned(virt_to_page(next->pgd)));
92
93                 /* stop flush ipis for the previous mm */
94                 cpumask_clear_cpu(cpu, mm_cpumask(prev));
95 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN) /* XEN: no lazy tlb */
96                 percpu_write(cpu_tlbstate.state, TLBSTATE_OK);
97                 percpu_write(cpu_tlbstate.active_mm, next);
98 #endif
99                 cpumask_set_cpu(cpu, mm_cpumask(next));
100
101                 /* Re-load page tables: load_cr3(next->pgd) */
102                 op->cmd = MMUEXT_NEW_BASEPTR;
103                 op->arg1.mfn = virt_to_mfn(next->pgd);
104                 op++;
105
106                 /* xen_new_user_pt(next->pgd) */
107 #ifdef CONFIG_X86_64
108                 op->cmd = MMUEXT_NEW_USER_BASEPTR;
109                 upgd = __user_pgd(next->pgd);
110                 op->arg1.mfn = likely(upgd) ? virt_to_mfn(upgd) : 0;
111                 op++;
112 #endif
113
114                 /*
115                  * load the LDT, if the LDT is different:
116                  */
117                 if (unlikely(prev->context.ldt != next->context.ldt)) {
118                         /* load_LDT_nolock(&next->context) */
119                         op->cmd = MMUEXT_SET_LDT;
120                         op->arg1.linear_addr = (unsigned long)next->context.ldt;
121                         op->arg2.nr_ents     = next->context.size;
122                         op++;
123                 }
124
125                 BUG_ON(HYPERVISOR_mmuext_op(_op, op-_op, NULL, DOMID_SELF));
126         }
127 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN) /* XEN: no lazy tlb */
128         else {
129                 percpu_write(cpu_tlbstate.state, TLBSTATE_OK);
130                 BUG_ON(percpu_read(cpu_tlbstate.active_mm) != next);
131
132                 if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next))) {
133                         /* We were in lazy tlb mode and leave_mm disabled
134                          * tlb flush IPI delivery. We must reload CR3
135                          * to make sure to use no freed page tables.
136                          */
137                         load_cr3(next->pgd);
138                         xen_new_user_pt(next->pgd);
139                         load_LDT_nolock(&next->context);
140                 }
141         }
142 #endif
143 }
144
145 #define activate_mm(prev, next)                 \
146 do {                                            \
147         xen_activate_mm(prev, next);            \
148         switch_mm((prev), (next), NULL);        \
149 } while (0);
150
151 #ifdef CONFIG_X86_32
152 #define deactivate_mm(tsk, mm)                  \
153 do {                                            \
154         lazy_load_gs(0);                        \
155 } while (0)
156 #else
157 #define deactivate_mm(tsk, mm)                  \
158 do {                                            \
159         load_gs_index(0);                       \
160         loadsegment(fs, 0);                     \
161 } while (0)
162 #endif
163
164 #endif /* _ASM_X86_MMU_CONTEXT_H */