1 #ifndef __X86_64_MMU_CONTEXT_H
2 #define __X86_64_MMU_CONTEXT_H
4 #include <linux/config.h>
6 #include <asm/atomic.h>
7 #include <asm/pgalloc.h>
10 #include <asm/pgtable.h>
11 #include <asm/tlbflush.h>
14 * possibly do the LDT unload here?
16 int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
17 void destroy_context(struct mm_struct *mm);
19 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
21 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN)
22 if (read_pda(mmu_state) == TLBSTATE_OK)
23 write_pda(mmu_state, TLBSTATE_LAZY);
27 #define prepare_arch_switch(next) __prepare_arch_switch()
29 static inline void __prepare_arch_switch(void)
32 * Save away %es, %ds, %fs and %gs. Must happen before reload
33 * of cr3/ldt (i.e., not in __switch_to).
35 __asm__ __volatile__ (
36 "mov %%es,%0 ; mov %%ds,%1 ; mov %%fs,%2 ; mov %%gs,%3"
37 : "=m" (current->thread.es),
38 "=m" (current->thread.ds),
39 "=m" (current->thread.fsindex),
40 "=m" (current->thread.gsindex) );
42 if (current->thread.ds)
43 __asm__ __volatile__ ( "movl %0,%%ds" : : "r" (0) );
45 if (current->thread.es)
46 __asm__ __volatile__ ( "movl %0,%%es" : : "r" (0) );
48 if (current->thread.fsindex) {
49 __asm__ __volatile__ ( "movl %0,%%fs" : : "r" (0) );
50 current->thread.fs = 0;
53 if (current->thread.gsindex) {
55 current->thread.gs = 0;
59 extern void mm_pin(struct mm_struct *mm);
60 extern void mm_unpin(struct mm_struct *mm);
61 void mm_pin_all(void);
63 static inline void load_cr3(pgd_t *pgd)
65 asm volatile("movq %0,%%cr3" :: "r" (phys_to_machine(__pa(pgd))) :
69 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
70 struct task_struct *tsk)
72 unsigned cpu = smp_processor_id();
73 struct mmuext_op _op[3], *op = _op;
75 if (likely(prev != next)) {
76 BUG_ON(!next->context.pinned);
78 /* stop flush ipis for the previous mm */
79 cpu_clear(cpu, prev->cpu_vm_mask);
80 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN)
81 write_pda(mmu_state, TLBSTATE_OK);
82 write_pda(active_mm, next);
84 cpu_set(cpu, next->cpu_vm_mask);
86 /* load_cr3(next->pgd) */
87 op->cmd = MMUEXT_NEW_BASEPTR;
88 op->arg1.mfn = pfn_to_mfn(__pa(next->pgd) >> PAGE_SHIFT);
91 /* xen_new_user_pt(__pa(__user_pgd(next->pgd))) */
92 op->cmd = MMUEXT_NEW_USER_BASEPTR;
93 op->arg1.mfn = pfn_to_mfn(__pa(__user_pgd(next->pgd)) >> PAGE_SHIFT);
96 if (unlikely(next->context.ldt != prev->context.ldt)) {
97 /* load_LDT_nolock(&next->context, cpu) */
98 op->cmd = MMUEXT_SET_LDT;
99 op->arg1.linear_addr = (unsigned long)next->context.ldt;
100 op->arg2.nr_ents = next->context.size;
104 BUG_ON(HYPERVISOR_mmuext_op(_op, op-_op, NULL, DOMID_SELF));
106 #if defined(CONFIG_SMP) && !defined(CONFIG_XEN)
108 write_pda(mmu_state, TLBSTATE_OK);
109 if (read_pda(active_mm) != next)
111 if (!cpu_test_and_set(cpu, next->cpu_vm_mask)) {
112 /* We were in lazy tlb mode and leave_mm disabled
113 * tlb flush IPI delivery. We must reload CR3
114 * to make sure to use no freed page tables.
117 xen_new_user_pt(__pa(__user_pgd(next->pgd)));
118 load_LDT_nolock(&next->context, cpu);
124 #define deactivate_mm(tsk,mm) do { \
126 asm volatile("movl %0,%%fs"::"r"(0)); \
129 static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
131 if (!next->context.pinned)
133 switch_mm(prev, next, NULL);