xen: disable preempt for leave_lazy_mmu
[linux-2.6/mini2440.git] / arch / x86 / xen / mmu.c
blob0e572380413bbc6506cda063fbcf7da3ebf03894
1 /*
2 * Xen mmu operations
4 * This file contains the various mmu fetch and update operations.
5 * The most important job they must perform is the mapping between the
6 * domain's pfn and the overall machine mfns.
8 * Xen allows guests to directly update the pagetable, in a controlled
9 * fashion. In other words, the guest modifies the same pagetable
10 * that the CPU actually uses, which eliminates the overhead of having
11 * a separate shadow pagetable.
13 * In order to allow this, it falls on the guest domain to map its
14 * notion of a "physical" pfn - which is just a domain-local linear
15 * address - into a real "machine address" which the CPU's MMU can
16 * use.
18 * A pgd_t/pmd_t/pte_t will typically contain an mfn, and so can be
19 * inserted directly into the pagetable. When creating a new
20 * pte/pmd/pgd, it converts the passed pfn into an mfn. Conversely,
21 * when reading the content back with __(pgd|pmd|pte)_val, it converts
22 * the mfn back into a pfn.
24 * The other constraint is that all pages which make up a pagetable
25 * must be mapped read-only in the guest. This prevents uncontrolled
26 * guest updates to the pagetable. Xen strictly enforces this, and
27 * will disallow any pagetable update which will end up mapping a
28 * pagetable page RW, and will disallow using any writable page as a
29 * pagetable.
31 * Naively, when loading %cr3 with the base of a new pagetable, Xen
32 * would need to validate the whole pagetable before going on.
33 * Naturally, this is quite slow. The solution is to "pin" a
34 * pagetable, which enforces all the constraints on the pagetable even
35 * when it is not actively in use. This menas that Xen can be assured
36 * that it is still valid when you do load it into %cr3, and doesn't
37 * need to revalidate it.
39 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
41 #include <linux/sched.h>
42 #include <linux/highmem.h>
43 #include <linux/debugfs.h>
44 #include <linux/bug.h>
46 #include <asm/pgtable.h>
47 #include <asm/tlbflush.h>
48 #include <asm/fixmap.h>
49 #include <asm/mmu_context.h>
50 #include <asm/setup.h>
51 #include <asm/paravirt.h>
52 #include <asm/linkage.h>
54 #include <asm/xen/hypercall.h>
55 #include <asm/xen/hypervisor.h>
57 #include <xen/page.h>
58 #include <xen/interface/xen.h>
59 #include <xen/interface/version.h>
60 #include <xen/hvc-console.h>
62 #include "multicalls.h"
63 #include "mmu.h"
64 #include "debugfs.h"
66 #define MMU_UPDATE_HISTO 30
68 #ifdef CONFIG_XEN_DEBUG_FS
70 static struct {
71 u32 pgd_update;
72 u32 pgd_update_pinned;
73 u32 pgd_update_batched;
75 u32 pud_update;
76 u32 pud_update_pinned;
77 u32 pud_update_batched;
79 u32 pmd_update;
80 u32 pmd_update_pinned;
81 u32 pmd_update_batched;
83 u32 pte_update;
84 u32 pte_update_pinned;
85 u32 pte_update_batched;
87 u32 mmu_update;
88 u32 mmu_update_extended;
89 u32 mmu_update_histo[MMU_UPDATE_HISTO];
91 u32 prot_commit;
92 u32 prot_commit_batched;
94 u32 set_pte_at;
95 u32 set_pte_at_batched;
96 u32 set_pte_at_pinned;
97 u32 set_pte_at_current;
98 u32 set_pte_at_kernel;
99 } mmu_stats;
101 static u8 zero_stats;
103 static inline void check_zero(void)
105 if (unlikely(zero_stats)) {
106 memset(&mmu_stats, 0, sizeof(mmu_stats));
107 zero_stats = 0;
111 #define ADD_STATS(elem, val) \
112 do { check_zero(); mmu_stats.elem += (val); } while(0)
114 #else /* !CONFIG_XEN_DEBUG_FS */
116 #define ADD_STATS(elem, val) do { (void)(val); } while(0)
118 #endif /* CONFIG_XEN_DEBUG_FS */
122 * Identity map, in addition to plain kernel map. This needs to be
123 * large enough to allocate page table pages to allocate the rest.
124 * Each page can map 2MB.
126 static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
128 #ifdef CONFIG_X86_64
129 /* l3 pud for userspace vsyscall mapping */
130 static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
131 #endif /* CONFIG_X86_64 */
134 * Note about cr3 (pagetable base) values:
136 * xen_cr3 contains the current logical cr3 value; it contains the
137 * last set cr3. This may not be the current effective cr3, because
138 * its update may be being lazily deferred. However, a vcpu looking
139 * at its own cr3 can use this value knowing that it everything will
140 * be self-consistent.
142 * xen_current_cr3 contains the actual vcpu cr3; it is set once the
143 * hypercall to set the vcpu cr3 is complete (so it may be a little
144 * out of date, but it will never be set early). If one vcpu is
145 * looking at another vcpu's cr3 value, it should use this variable.
147 DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
148 DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
152 * Just beyond the highest usermode address. STACK_TOP_MAX has a
153 * redzone above it, so round it up to a PGD boundary.
155 #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
158 #define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
159 #define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE)
161 /* Placeholder for holes in the address space */
162 static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] __page_aligned_data =
163 { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL };
165 /* Array of pointers to pages containing p2m entries */
166 static unsigned long *p2m_top[TOP_ENTRIES] __page_aligned_data =
167 { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] };
169 /* Arrays of p2m arrays expressed in mfns used for save/restore */
170 static unsigned long p2m_top_mfn[TOP_ENTRIES] __page_aligned_bss;
172 static unsigned long p2m_top_mfn_list[TOP_ENTRIES / P2M_ENTRIES_PER_PAGE]
173 __page_aligned_bss;
175 static inline unsigned p2m_top_index(unsigned long pfn)
177 BUG_ON(pfn >= MAX_DOMAIN_PAGES);
178 return pfn / P2M_ENTRIES_PER_PAGE;
181 static inline unsigned p2m_index(unsigned long pfn)
183 return pfn % P2M_ENTRIES_PER_PAGE;
186 /* Build the parallel p2m_top_mfn structures */
187 void xen_setup_mfn_list_list(void)
189 unsigned pfn, idx;
191 for (pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) {
192 unsigned topidx = p2m_top_index(pfn);
194 p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]);
197 for (idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) {
198 unsigned topidx = idx * P2M_ENTRIES_PER_PAGE;
199 p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]);
202 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
204 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
205 virt_to_mfn(p2m_top_mfn_list);
206 HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages;
209 /* Set up p2m_top to point to the domain-builder provided p2m pages */
210 void __init xen_build_dynamic_phys_to_machine(void)
212 unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
213 unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
214 unsigned pfn;
216 for (pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) {
217 unsigned topidx = p2m_top_index(pfn);
219 p2m_top[topidx] = &mfn_list[pfn];
223 unsigned long get_phys_to_machine(unsigned long pfn)
225 unsigned topidx, idx;
227 if (unlikely(pfn >= MAX_DOMAIN_PAGES))
228 return INVALID_P2M_ENTRY;
230 topidx = p2m_top_index(pfn);
231 idx = p2m_index(pfn);
232 return p2m_top[topidx][idx];
234 EXPORT_SYMBOL_GPL(get_phys_to_machine);
236 static void alloc_p2m(unsigned long **pp, unsigned long *mfnp)
238 unsigned long *p;
239 unsigned i;
241 p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL);
242 BUG_ON(p == NULL);
244 for (i = 0; i < P2M_ENTRIES_PER_PAGE; i++)
245 p[i] = INVALID_P2M_ENTRY;
247 if (cmpxchg(pp, p2m_missing, p) != p2m_missing)
248 free_page((unsigned long)p);
249 else
250 *mfnp = virt_to_mfn(p);
253 void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
255 unsigned topidx, idx;
257 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
258 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
259 return;
262 if (unlikely(pfn >= MAX_DOMAIN_PAGES)) {
263 BUG_ON(mfn != INVALID_P2M_ENTRY);
264 return;
267 topidx = p2m_top_index(pfn);
268 if (p2m_top[topidx] == p2m_missing) {
269 /* no need to allocate a page to store an invalid entry */
270 if (mfn == INVALID_P2M_ENTRY)
271 return;
272 alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]);
275 idx = p2m_index(pfn);
276 p2m_top[topidx][idx] = mfn;
279 unsigned long arbitrary_virt_to_mfn(void *vaddr)
281 xmaddr_t maddr = arbitrary_virt_to_machine(vaddr);
283 return PFN_DOWN(maddr.maddr);
286 xmaddr_t arbitrary_virt_to_machine(void *vaddr)
288 unsigned long address = (unsigned long)vaddr;
289 unsigned int level;
290 pte_t *pte;
291 unsigned offset;
294 * if the PFN is in the linear mapped vaddr range, we can just use
295 * the (quick) virt_to_machine() p2m lookup
297 if (virt_addr_valid(vaddr))
298 return virt_to_machine(vaddr);
300 /* otherwise we have to do a (slower) full page-table walk */
302 pte = lookup_address(address, &level);
303 BUG_ON(pte == NULL);
304 offset = address & ~PAGE_MASK;
305 return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
308 void make_lowmem_page_readonly(void *vaddr)
310 pte_t *pte, ptev;
311 unsigned long address = (unsigned long)vaddr;
312 unsigned int level;
314 pte = lookup_address(address, &level);
315 BUG_ON(pte == NULL);
317 ptev = pte_wrprotect(*pte);
319 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
320 BUG();
323 void make_lowmem_page_readwrite(void *vaddr)
325 pte_t *pte, ptev;
326 unsigned long address = (unsigned long)vaddr;
327 unsigned int level;
329 pte = lookup_address(address, &level);
330 BUG_ON(pte == NULL);
332 ptev = pte_mkwrite(*pte);
334 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
335 BUG();
339 static bool xen_page_pinned(void *ptr)
341 struct page *page = virt_to_page(ptr);
343 return PagePinned(page);
346 static void xen_extend_mmu_update(const struct mmu_update *update)
348 struct multicall_space mcs;
349 struct mmu_update *u;
351 mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
353 if (mcs.mc != NULL) {
354 ADD_STATS(mmu_update_extended, 1);
355 ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1);
357 mcs.mc->args[1]++;
359 if (mcs.mc->args[1] < MMU_UPDATE_HISTO)
360 ADD_STATS(mmu_update_histo[mcs.mc->args[1]], 1);
361 else
362 ADD_STATS(mmu_update_histo[0], 1);
363 } else {
364 ADD_STATS(mmu_update, 1);
365 mcs = __xen_mc_entry(sizeof(*u));
366 MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
367 ADD_STATS(mmu_update_histo[1], 1);
370 u = mcs.args;
371 *u = *update;
374 void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
376 struct mmu_update u;
378 preempt_disable();
380 xen_mc_batch();
382 /* ptr may be ioremapped for 64-bit pagetable setup */
383 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
384 u.val = pmd_val_ma(val);
385 xen_extend_mmu_update(&u);
387 ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
389 xen_mc_issue(PARAVIRT_LAZY_MMU);
391 preempt_enable();
394 void xen_set_pmd(pmd_t *ptr, pmd_t val)
396 ADD_STATS(pmd_update, 1);
398 /* If page is not pinned, we can just update the entry
399 directly */
400 if (!xen_page_pinned(ptr)) {
401 *ptr = val;
402 return;
405 ADD_STATS(pmd_update_pinned, 1);
407 xen_set_pmd_hyper(ptr, val);
411 * Associate a virtual page frame with a given physical page frame
412 * and protection flags for that frame.
414 void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
416 set_pte_vaddr(vaddr, mfn_pte(mfn, flags));
419 void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
420 pte_t *ptep, pte_t pteval)
422 ADD_STATS(set_pte_at, 1);
423 // ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep));
424 ADD_STATS(set_pte_at_current, mm == current->mm);
425 ADD_STATS(set_pte_at_kernel, mm == &init_mm);
427 if (mm == current->mm || mm == &init_mm) {
428 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
429 struct multicall_space mcs;
430 mcs = xen_mc_entry(0);
432 MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
433 ADD_STATS(set_pte_at_batched, 1);
434 xen_mc_issue(PARAVIRT_LAZY_MMU);
435 goto out;
436 } else
437 if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
438 goto out;
440 xen_set_pte(ptep, pteval);
442 out: return;
445 pte_t xen_ptep_modify_prot_start(struct mm_struct *mm,
446 unsigned long addr, pte_t *ptep)
448 /* Just return the pte as-is. We preserve the bits on commit */
449 return *ptep;
452 void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
453 pte_t *ptep, pte_t pte)
455 struct mmu_update u;
457 xen_mc_batch();
459 u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
460 u.val = pte_val_ma(pte);
461 xen_extend_mmu_update(&u);
463 ADD_STATS(prot_commit, 1);
464 ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
466 xen_mc_issue(PARAVIRT_LAZY_MMU);
469 /* Assume pteval_t is equivalent to all the other *val_t types. */
470 static pteval_t pte_mfn_to_pfn(pteval_t val)
472 if (val & _PAGE_PRESENT) {
473 unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
474 pteval_t flags = val & PTE_FLAGS_MASK;
475 val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags;
478 return val;
481 static pteval_t pte_pfn_to_mfn(pteval_t val)
483 if (val & _PAGE_PRESENT) {
484 unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
485 pteval_t flags = val & PTE_FLAGS_MASK;
486 val = ((pteval_t)pfn_to_mfn(pfn) << PAGE_SHIFT) | flags;
489 return val;
492 pteval_t xen_pte_val(pte_t pte)
494 return pte_mfn_to_pfn(pte.pte);
496 PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val);
498 pgdval_t xen_pgd_val(pgd_t pgd)
500 return pte_mfn_to_pfn(pgd.pgd);
502 PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);
504 pte_t xen_make_pte(pteval_t pte)
506 pte = pte_pfn_to_mfn(pte);
507 return native_make_pte(pte);
509 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
511 pgd_t xen_make_pgd(pgdval_t pgd)
513 pgd = pte_pfn_to_mfn(pgd);
514 return native_make_pgd(pgd);
516 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd);
518 pmdval_t xen_pmd_val(pmd_t pmd)
520 return pte_mfn_to_pfn(pmd.pmd);
522 PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val);
524 void xen_set_pud_hyper(pud_t *ptr, pud_t val)
526 struct mmu_update u;
528 preempt_disable();
530 xen_mc_batch();
532 /* ptr may be ioremapped for 64-bit pagetable setup */
533 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
534 u.val = pud_val_ma(val);
535 xen_extend_mmu_update(&u);
537 ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
539 xen_mc_issue(PARAVIRT_LAZY_MMU);
541 preempt_enable();
544 void xen_set_pud(pud_t *ptr, pud_t val)
546 ADD_STATS(pud_update, 1);
548 /* If page is not pinned, we can just update the entry
549 directly */
550 if (!xen_page_pinned(ptr)) {
551 *ptr = val;
552 return;
555 ADD_STATS(pud_update_pinned, 1);
557 xen_set_pud_hyper(ptr, val);
560 void xen_set_pte(pte_t *ptep, pte_t pte)
562 ADD_STATS(pte_update, 1);
563 // ADD_STATS(pte_update_pinned, xen_page_pinned(ptep));
564 ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
566 #ifdef CONFIG_X86_PAE
567 ptep->pte_high = pte.pte_high;
568 smp_wmb();
569 ptep->pte_low = pte.pte_low;
570 #else
571 *ptep = pte;
572 #endif
575 #ifdef CONFIG_X86_PAE
576 void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
578 set_64bit((u64 *)ptep, native_pte_val(pte));
581 void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
583 ptep->pte_low = 0;
584 smp_wmb(); /* make sure low gets written first */
585 ptep->pte_high = 0;
588 void xen_pmd_clear(pmd_t *pmdp)
590 set_pmd(pmdp, __pmd(0));
592 #endif /* CONFIG_X86_PAE */
594 pmd_t xen_make_pmd(pmdval_t pmd)
596 pmd = pte_pfn_to_mfn(pmd);
597 return native_make_pmd(pmd);
599 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd);
601 #if PAGETABLE_LEVELS == 4
602 pudval_t xen_pud_val(pud_t pud)
604 return pte_mfn_to_pfn(pud.pud);
606 PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val);
608 pud_t xen_make_pud(pudval_t pud)
610 pud = pte_pfn_to_mfn(pud);
612 return native_make_pud(pud);
614 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud);
616 pgd_t *xen_get_user_pgd(pgd_t *pgd)
618 pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
619 unsigned offset = pgd - pgd_page;
620 pgd_t *user_ptr = NULL;
622 if (offset < pgd_index(USER_LIMIT)) {
623 struct page *page = virt_to_page(pgd_page);
624 user_ptr = (pgd_t *)page->private;
625 if (user_ptr)
626 user_ptr += offset;
629 return user_ptr;
632 static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
634 struct mmu_update u;
636 u.ptr = virt_to_machine(ptr).maddr;
637 u.val = pgd_val_ma(val);
638 xen_extend_mmu_update(&u);
642 * Raw hypercall-based set_pgd, intended for in early boot before
643 * there's a page structure. This implies:
644 * 1. The only existing pagetable is the kernel's
645 * 2. It is always pinned
646 * 3. It has no user pagetable attached to it
648 void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
650 preempt_disable();
652 xen_mc_batch();
654 __xen_set_pgd_hyper(ptr, val);
656 xen_mc_issue(PARAVIRT_LAZY_MMU);
658 preempt_enable();
661 void xen_set_pgd(pgd_t *ptr, pgd_t val)
663 pgd_t *user_ptr = xen_get_user_pgd(ptr);
665 ADD_STATS(pgd_update, 1);
667 /* If page is not pinned, we can just update the entry
668 directly */
669 if (!xen_page_pinned(ptr)) {
670 *ptr = val;
671 if (user_ptr) {
672 WARN_ON(xen_page_pinned(user_ptr));
673 *user_ptr = val;
675 return;
678 ADD_STATS(pgd_update_pinned, 1);
679 ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
681 /* If it's pinned, then we can at least batch the kernel and
682 user updates together. */
683 xen_mc_batch();
685 __xen_set_pgd_hyper(ptr, val);
686 if (user_ptr)
687 __xen_set_pgd_hyper(user_ptr, val);
689 xen_mc_issue(PARAVIRT_LAZY_MMU);
691 #endif /* PAGETABLE_LEVELS == 4 */
694 * (Yet another) pagetable walker. This one is intended for pinning a
695 * pagetable. This means that it walks a pagetable and calls the
696 * callback function on each page it finds making up the page table,
697 * at every level. It walks the entire pagetable, but it only bothers
698 * pinning pte pages which are below limit. In the normal case this
699 * will be STACK_TOP_MAX, but at boot we need to pin up to
700 * FIXADDR_TOP.
702 * For 32-bit the important bit is that we don't pin beyond there,
703 * because then we start getting into Xen's ptes.
705 * For 64-bit, we must skip the Xen hole in the middle of the address
706 * space, just after the big x86-64 virtual hole.
708 static int __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd,
709 int (*func)(struct mm_struct *mm, struct page *,
710 enum pt_level),
711 unsigned long limit)
713 int flush = 0;
714 unsigned hole_low, hole_high;
715 unsigned pgdidx_limit, pudidx_limit, pmdidx_limit;
716 unsigned pgdidx, pudidx, pmdidx;
718 /* The limit is the last byte to be touched */
719 limit--;
720 BUG_ON(limit >= FIXADDR_TOP);
722 if (xen_feature(XENFEAT_auto_translated_physmap))
723 return 0;
726 * 64-bit has a great big hole in the middle of the address
727 * space, which contains the Xen mappings. On 32-bit these
728 * will end up making a zero-sized hole and so is a no-op.
730 hole_low = pgd_index(USER_LIMIT);
731 hole_high = pgd_index(PAGE_OFFSET);
733 pgdidx_limit = pgd_index(limit);
734 #if PTRS_PER_PUD > 1
735 pudidx_limit = pud_index(limit);
736 #else
737 pudidx_limit = 0;
738 #endif
739 #if PTRS_PER_PMD > 1
740 pmdidx_limit = pmd_index(limit);
741 #else
742 pmdidx_limit = 0;
743 #endif
745 for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) {
746 pud_t *pud;
748 if (pgdidx >= hole_low && pgdidx < hole_high)
749 continue;
751 if (!pgd_val(pgd[pgdidx]))
752 continue;
754 pud = pud_offset(&pgd[pgdidx], 0);
756 if (PTRS_PER_PUD > 1) /* not folded */
757 flush |= (*func)(mm, virt_to_page(pud), PT_PUD);
759 for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) {
760 pmd_t *pmd;
762 if (pgdidx == pgdidx_limit &&
763 pudidx > pudidx_limit)
764 goto out;
766 if (pud_none(pud[pudidx]))
767 continue;
769 pmd = pmd_offset(&pud[pudidx], 0);
771 if (PTRS_PER_PMD > 1) /* not folded */
772 flush |= (*func)(mm, virt_to_page(pmd), PT_PMD);
774 for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) {
775 struct page *pte;
777 if (pgdidx == pgdidx_limit &&
778 pudidx == pudidx_limit &&
779 pmdidx > pmdidx_limit)
780 goto out;
782 if (pmd_none(pmd[pmdidx]))
783 continue;
785 pte = pmd_page(pmd[pmdidx]);
786 flush |= (*func)(mm, pte, PT_PTE);
791 out:
792 /* Do the top level last, so that the callbacks can use it as
793 a cue to do final things like tlb flushes. */
794 flush |= (*func)(mm, virt_to_page(pgd), PT_PGD);
796 return flush;
799 static int xen_pgd_walk(struct mm_struct *mm,
800 int (*func)(struct mm_struct *mm, struct page *,
801 enum pt_level),
802 unsigned long limit)
804 return __xen_pgd_walk(mm, mm->pgd, func, limit);
807 /* If we're using split pte locks, then take the page's lock and
808 return a pointer to it. Otherwise return NULL. */
809 static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
811 spinlock_t *ptl = NULL;
813 #if USE_SPLIT_PTLOCKS
814 ptl = __pte_lockptr(page);
815 spin_lock_nest_lock(ptl, &mm->page_table_lock);
816 #endif
818 return ptl;
821 static void xen_pte_unlock(void *v)
823 spinlock_t *ptl = v;
824 spin_unlock(ptl);
827 static void xen_do_pin(unsigned level, unsigned long pfn)
829 struct mmuext_op *op;
830 struct multicall_space mcs;
832 mcs = __xen_mc_entry(sizeof(*op));
833 op = mcs.args;
834 op->cmd = level;
835 op->arg1.mfn = pfn_to_mfn(pfn);
836 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
839 static int xen_pin_page(struct mm_struct *mm, struct page *page,
840 enum pt_level level)
842 unsigned pgfl = TestSetPagePinned(page);
843 int flush;
845 if (pgfl)
846 flush = 0; /* already pinned */
847 else if (PageHighMem(page))
848 /* kmaps need flushing if we found an unpinned
849 highpage */
850 flush = 1;
851 else {
852 void *pt = lowmem_page_address(page);
853 unsigned long pfn = page_to_pfn(page);
854 struct multicall_space mcs = __xen_mc_entry(0);
855 spinlock_t *ptl;
857 flush = 0;
860 * We need to hold the pagetable lock between the time
861 * we make the pagetable RO and when we actually pin
862 * it. If we don't, then other users may come in and
863 * attempt to update the pagetable by writing it,
864 * which will fail because the memory is RO but not
865 * pinned, so Xen won't do the trap'n'emulate.
867 * If we're using split pte locks, we can't hold the
868 * entire pagetable's worth of locks during the
869 * traverse, because we may wrap the preempt count (8
870 * bits). The solution is to mark RO and pin each PTE
871 * page while holding the lock. This means the number
872 * of locks we end up holding is never more than a
873 * batch size (~32 entries, at present).
875 * If we're not using split pte locks, we needn't pin
876 * the PTE pages independently, because we're
877 * protected by the overall pagetable lock.
879 ptl = NULL;
880 if (level == PT_PTE)
881 ptl = xen_pte_lock(page, mm);
883 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
884 pfn_pte(pfn, PAGE_KERNEL_RO),
885 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
887 if (ptl) {
888 xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
890 /* Queue a deferred unlock for when this batch
891 is completed. */
892 xen_mc_callback(xen_pte_unlock, ptl);
896 return flush;
899 /* This is called just after a mm has been created, but it has not
900 been used yet. We need to make sure that its pagetable is all
901 read-only, and can be pinned. */
902 static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
904 vm_unmap_aliases();
906 xen_mc_batch();
908 if (__xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT)) {
909 /* re-enable interrupts for flushing */
910 xen_mc_issue(0);
912 kmap_flush_unused();
914 xen_mc_batch();
917 #ifdef CONFIG_X86_64
919 pgd_t *user_pgd = xen_get_user_pgd(pgd);
921 xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
923 if (user_pgd) {
924 xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
925 xen_do_pin(MMUEXT_PIN_L4_TABLE,
926 PFN_DOWN(__pa(user_pgd)));
929 #else /* CONFIG_X86_32 */
930 #ifdef CONFIG_X86_PAE
931 /* Need to make sure unshared kernel PMD is pinnable */
932 xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
933 PT_PMD);
934 #endif
935 xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
936 #endif /* CONFIG_X86_64 */
937 xen_mc_issue(0);
940 static void xen_pgd_pin(struct mm_struct *mm)
942 __xen_pgd_pin(mm, mm->pgd);
946 * On save, we need to pin all pagetables to make sure they get their
947 * mfns turned into pfns. Search the list for any unpinned pgds and pin
948 * them (unpinned pgds are not currently in use, probably because the
949 * process is under construction or destruction).
951 * Expected to be called in stop_machine() ("equivalent to taking
952 * every spinlock in the system"), so the locking doesn't really
953 * matter all that much.
955 void xen_mm_pin_all(void)
957 unsigned long flags;
958 struct page *page;
960 spin_lock_irqsave(&pgd_lock, flags);
962 list_for_each_entry(page, &pgd_list, lru) {
963 if (!PagePinned(page)) {
964 __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
965 SetPageSavePinned(page);
969 spin_unlock_irqrestore(&pgd_lock, flags);
973 * The init_mm pagetable is really pinned as soon as its created, but
974 * that's before we have page structures to store the bits. So do all
975 * the book-keeping now.
977 static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
978 enum pt_level level)
980 SetPagePinned(page);
981 return 0;
984 void __init xen_mark_init_mm_pinned(void)
986 xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
989 static int xen_unpin_page(struct mm_struct *mm, struct page *page,
990 enum pt_level level)
992 unsigned pgfl = TestClearPagePinned(page);
994 if (pgfl && !PageHighMem(page)) {
995 void *pt = lowmem_page_address(page);
996 unsigned long pfn = page_to_pfn(page);
997 spinlock_t *ptl = NULL;
998 struct multicall_space mcs;
1001 * Do the converse to pin_page. If we're using split
1002 * pte locks, we must be holding the lock for while
1003 * the pte page is unpinned but still RO to prevent
1004 * concurrent updates from seeing it in this
1005 * partially-pinned state.
1007 if (level == PT_PTE) {
1008 ptl = xen_pte_lock(page, mm);
1010 if (ptl)
1011 xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
1014 mcs = __xen_mc_entry(0);
1016 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
1017 pfn_pte(pfn, PAGE_KERNEL),
1018 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
1020 if (ptl) {
1021 /* unlock when batch completed */
1022 xen_mc_callback(xen_pte_unlock, ptl);
1026 return 0; /* never need to flush on unpin */
1029 /* Release a pagetables pages back as normal RW */
1030 static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
1032 xen_mc_batch();
1034 xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1036 #ifdef CONFIG_X86_64
1038 pgd_t *user_pgd = xen_get_user_pgd(pgd);
1040 if (user_pgd) {
1041 xen_do_pin(MMUEXT_UNPIN_TABLE,
1042 PFN_DOWN(__pa(user_pgd)));
1043 xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
1046 #endif
1048 #ifdef CONFIG_X86_PAE
1049 /* Need to make sure unshared kernel PMD is unpinned */
1050 xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
1051 PT_PMD);
1052 #endif
1054 __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT);
1056 xen_mc_issue(0);
1059 static void xen_pgd_unpin(struct mm_struct *mm)
1061 __xen_pgd_unpin(mm, mm->pgd);
1065 * On resume, undo any pinning done at save, so that the rest of the
1066 * kernel doesn't see any unexpected pinned pagetables.
1068 void xen_mm_unpin_all(void)
1070 unsigned long flags;
1071 struct page *page;
1073 spin_lock_irqsave(&pgd_lock, flags);
1075 list_for_each_entry(page, &pgd_list, lru) {
1076 if (PageSavePinned(page)) {
1077 BUG_ON(!PagePinned(page));
1078 __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
1079 ClearPageSavePinned(page);
1083 spin_unlock_irqrestore(&pgd_lock, flags);
1086 void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
1088 spin_lock(&next->page_table_lock);
1089 xen_pgd_pin(next);
1090 spin_unlock(&next->page_table_lock);
1093 void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
1095 spin_lock(&mm->page_table_lock);
1096 xen_pgd_pin(mm);
1097 spin_unlock(&mm->page_table_lock);
1101 #ifdef CONFIG_SMP
1102 /* Another cpu may still have their %cr3 pointing at the pagetable, so
1103 we need to repoint it somewhere else before we can unpin it. */
1104 static void drop_other_mm_ref(void *info)
1106 struct mm_struct *mm = info;
1107 struct mm_struct *active_mm;
1109 active_mm = percpu_read(cpu_tlbstate.active_mm);
1111 if (active_mm == mm)
1112 leave_mm(smp_processor_id());
1114 /* If this cpu still has a stale cr3 reference, then make sure
1115 it has been flushed. */
1116 if (percpu_read(xen_current_cr3) == __pa(mm->pgd))
1117 load_cr3(swapper_pg_dir);
1120 static void xen_drop_mm_ref(struct mm_struct *mm)
1122 cpumask_var_t mask;
1123 unsigned cpu;
1125 if (current->active_mm == mm) {
1126 if (current->mm == mm)
1127 load_cr3(swapper_pg_dir);
1128 else
1129 leave_mm(smp_processor_id());
1132 /* Get the "official" set of cpus referring to our pagetable. */
1133 if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) {
1134 for_each_online_cpu(cpu) {
1135 if (!cpumask_test_cpu(cpu, &mm->cpu_vm_mask)
1136 && per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd))
1137 continue;
1138 smp_call_function_single(cpu, drop_other_mm_ref, mm, 1);
1140 return;
1142 cpumask_copy(mask, &mm->cpu_vm_mask);
1144 /* It's possible that a vcpu may have a stale reference to our
1145 cr3, because its in lazy mode, and it hasn't yet flushed
1146 its set of pending hypercalls yet. In this case, we can
1147 look at its actual current cr3 value, and force it to flush
1148 if needed. */
1149 for_each_online_cpu(cpu) {
1150 if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
1151 cpumask_set_cpu(cpu, mask);
1154 if (!cpumask_empty(mask))
1155 smp_call_function_many(mask, drop_other_mm_ref, mm, 1);
1156 free_cpumask_var(mask);
1158 #else
1159 static void xen_drop_mm_ref(struct mm_struct *mm)
1161 if (current->active_mm == mm)
1162 load_cr3(swapper_pg_dir);
1164 #endif
1167 * While a process runs, Xen pins its pagetables, which means that the
1168 * hypervisor forces it to be read-only, and it controls all updates
1169 * to it. This means that all pagetable updates have to go via the
1170 * hypervisor, which is moderately expensive.
1172 * Since we're pulling the pagetable down, we switch to use init_mm,
1173 * unpin old process pagetable and mark it all read-write, which
1174 * allows further operations on it to be simple memory accesses.
1176 * The only subtle point is that another CPU may be still using the
1177 * pagetable because of lazy tlb flushing. This means we need need to
1178 * switch all CPUs off this pagetable before we can unpin it.
1180 void xen_exit_mmap(struct mm_struct *mm)
1182 get_cpu(); /* make sure we don't move around */
1183 xen_drop_mm_ref(mm);
1184 put_cpu();
1186 spin_lock(&mm->page_table_lock);
1188 /* pgd may not be pinned in the error exit path of execve */
1189 if (xen_page_pinned(mm->pgd))
1190 xen_pgd_unpin(mm);
1192 spin_unlock(&mm->page_table_lock);
1195 static __init void xen_pagetable_setup_start(pgd_t *base)
1199 static __init void xen_pagetable_setup_done(pgd_t *base)
1201 xen_setup_shared_info();
1204 static void xen_write_cr2(unsigned long cr2)
1206 percpu_read(xen_vcpu)->arch.cr2 = cr2;
1209 static unsigned long xen_read_cr2(void)
1211 return percpu_read(xen_vcpu)->arch.cr2;
1214 unsigned long xen_read_cr2_direct(void)
1216 return percpu_read(xen_vcpu_info.arch.cr2);
1219 static void xen_flush_tlb(void)
1221 struct mmuext_op *op;
1222 struct multicall_space mcs;
1224 preempt_disable();
1226 mcs = xen_mc_entry(sizeof(*op));
1228 op = mcs.args;
1229 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
1230 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1232 xen_mc_issue(PARAVIRT_LAZY_MMU);
1234 preempt_enable();
1237 static void xen_flush_tlb_single(unsigned long addr)
1239 struct mmuext_op *op;
1240 struct multicall_space mcs;
1242 preempt_disable();
1244 mcs = xen_mc_entry(sizeof(*op));
1245 op = mcs.args;
1246 op->cmd = MMUEXT_INVLPG_LOCAL;
1247 op->arg1.linear_addr = addr & PAGE_MASK;
1248 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1250 xen_mc_issue(PARAVIRT_LAZY_MMU);
1252 preempt_enable();
1255 static void xen_flush_tlb_others(const struct cpumask *cpus,
1256 struct mm_struct *mm, unsigned long va)
1258 struct {
1259 struct mmuext_op op;
1260 DECLARE_BITMAP(mask, NR_CPUS);
1261 } *args;
1262 struct multicall_space mcs;
1264 BUG_ON(cpumask_empty(cpus));
1265 BUG_ON(!mm);
1267 mcs = xen_mc_entry(sizeof(*args));
1268 args = mcs.args;
1269 args->op.arg2.vcpumask = to_cpumask(args->mask);
1271 /* Remove us, and any offline CPUS. */
1272 cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask);
1273 cpumask_clear_cpu(smp_processor_id(), to_cpumask(args->mask));
1275 if (va == TLB_FLUSH_ALL) {
1276 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
1277 } else {
1278 args->op.cmd = MMUEXT_INVLPG_MULTI;
1279 args->op.arg1.linear_addr = va;
1282 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
1284 xen_mc_issue(PARAVIRT_LAZY_MMU);
1287 static unsigned long xen_read_cr3(void)
1289 return percpu_read(xen_cr3);
1292 static void set_current_cr3(void *v)
1294 percpu_write(xen_current_cr3, (unsigned long)v);
1297 static void __xen_write_cr3(bool kernel, unsigned long cr3)
1299 struct mmuext_op *op;
1300 struct multicall_space mcs;
1301 unsigned long mfn;
1303 if (cr3)
1304 mfn = pfn_to_mfn(PFN_DOWN(cr3));
1305 else
1306 mfn = 0;
1308 WARN_ON(mfn == 0 && kernel);
1310 mcs = __xen_mc_entry(sizeof(*op));
1312 op = mcs.args;
1313 op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
1314 op->arg1.mfn = mfn;
1316 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1318 if (kernel) {
1319 percpu_write(xen_cr3, cr3);
1321 /* Update xen_current_cr3 once the batch has actually
1322 been submitted. */
1323 xen_mc_callback(set_current_cr3, (void *)cr3);
1327 static void xen_write_cr3(unsigned long cr3)
1329 BUG_ON(preemptible());
1331 xen_mc_batch(); /* disables interrupts */
1333 /* Update while interrupts are disabled, so its atomic with
1334 respect to ipis */
1335 percpu_write(xen_cr3, cr3);
1337 __xen_write_cr3(true, cr3);
1339 #ifdef CONFIG_X86_64
1341 pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
1342 if (user_pgd)
1343 __xen_write_cr3(false, __pa(user_pgd));
1344 else
1345 __xen_write_cr3(false, 0);
1347 #endif
1349 xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
1352 static int xen_pgd_alloc(struct mm_struct *mm)
1354 pgd_t *pgd = mm->pgd;
1355 int ret = 0;
1357 BUG_ON(PagePinned(virt_to_page(pgd)));
1359 #ifdef CONFIG_X86_64
1361 struct page *page = virt_to_page(pgd);
1362 pgd_t *user_pgd;
1364 BUG_ON(page->private != 0);
1366 ret = -ENOMEM;
1368 user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1369 page->private = (unsigned long)user_pgd;
1371 if (user_pgd != NULL) {
1372 user_pgd[pgd_index(VSYSCALL_START)] =
1373 __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
1374 ret = 0;
1377 BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
1379 #endif
1381 return ret;
1384 static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
1386 #ifdef CONFIG_X86_64
1387 pgd_t *user_pgd = xen_get_user_pgd(pgd);
1389 if (user_pgd)
1390 free_page((unsigned long)user_pgd);
1391 #endif
1394 #ifdef CONFIG_HIGHPTE
1395 static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
1397 pgprot_t prot = PAGE_KERNEL;
1399 if (PagePinned(page))
1400 prot = PAGE_KERNEL_RO;
1402 if (0 && PageHighMem(page))
1403 printk("mapping highpte %lx type %d prot %s\n",
1404 page_to_pfn(page), type,
1405 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
1407 return kmap_atomic_prot(page, type, prot);
1409 #endif
1411 #ifdef CONFIG_X86_32
1412 static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
1414 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
1415 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
1416 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
1417 pte_val_ma(pte));
1419 return pte;
1422 /* Init-time set_pte while constructing initial pagetables, which
1423 doesn't allow RO pagetable pages to be remapped RW */
1424 static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
1426 pte = mask_rw_pte(ptep, pte);
1428 xen_set_pte(ptep, pte);
1430 #endif
1432 /* Early in boot, while setting up the initial pagetable, assume
1433 everything is pinned. */
1434 static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
1436 #ifdef CONFIG_FLATMEM
1437 BUG_ON(mem_map); /* should only be used early */
1438 #endif
1439 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
1442 /* Early release_pte assumes that all pts are pinned, since there's
1443 only init_mm and anything attached to that is pinned. */
1444 static void xen_release_pte_init(unsigned long pfn)
1446 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1449 static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
1451 struct mmuext_op op;
1452 op.cmd = cmd;
1453 op.arg1.mfn = pfn_to_mfn(pfn);
1454 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
1455 BUG();
1458 /* This needs to make sure the new pte page is pinned iff its being
1459 attached to a pinned pagetable. */
1460 static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level)
1462 struct page *page = pfn_to_page(pfn);
1464 if (PagePinned(virt_to_page(mm->pgd))) {
1465 SetPagePinned(page);
1467 vm_unmap_aliases();
1468 if (!PageHighMem(page)) {
1469 make_lowmem_page_readonly(__va(PFN_PHYS((unsigned long)pfn)));
1470 if (level == PT_PTE && USE_SPLIT_PTLOCKS)
1471 pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
1472 } else {
1473 /* make sure there are no stray mappings of
1474 this page */
1475 kmap_flush_unused();
1480 static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
1482 xen_alloc_ptpage(mm, pfn, PT_PTE);
1485 static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
1487 xen_alloc_ptpage(mm, pfn, PT_PMD);
1490 /* This should never happen until we're OK to use struct page */
1491 static void xen_release_ptpage(unsigned long pfn, unsigned level)
1493 struct page *page = pfn_to_page(pfn);
1495 if (PagePinned(page)) {
1496 if (!PageHighMem(page)) {
1497 if (level == PT_PTE && USE_SPLIT_PTLOCKS)
1498 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
1499 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1501 ClearPagePinned(page);
1505 static void xen_release_pte(unsigned long pfn)
1507 xen_release_ptpage(pfn, PT_PTE);
1510 static void xen_release_pmd(unsigned long pfn)
1512 xen_release_ptpage(pfn, PT_PMD);
1515 #if PAGETABLE_LEVELS == 4
1516 static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
1518 xen_alloc_ptpage(mm, pfn, PT_PUD);
1521 static void xen_release_pud(unsigned long pfn)
1523 xen_release_ptpage(pfn, PT_PUD);
1525 #endif
1527 void __init xen_reserve_top(void)
1529 #ifdef CONFIG_X86_32
1530 unsigned long top = HYPERVISOR_VIRT_START;
1531 struct xen_platform_parameters pp;
1533 if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
1534 top = pp.virt_start;
1536 reserve_top_address(-top);
1537 #endif /* CONFIG_X86_32 */
1541 * Like __va(), but returns address in the kernel mapping (which is
1542 * all we have until the physical memory mapping has been set up.
1544 static void *__ka(phys_addr_t paddr)
1546 #ifdef CONFIG_X86_64
1547 return (void *)(paddr + __START_KERNEL_map);
1548 #else
1549 return __va(paddr);
1550 #endif
1553 /* Convert a machine address to physical address */
1554 static unsigned long m2p(phys_addr_t maddr)
1556 phys_addr_t paddr;
1558 maddr &= PTE_PFN_MASK;
1559 paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
1561 return paddr;
1564 /* Convert a machine address to kernel virtual */
1565 static void *m2v(phys_addr_t maddr)
1567 return __ka(m2p(maddr));
1570 static void set_page_prot(void *addr, pgprot_t prot)
1572 unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
1573 pte_t pte = pfn_pte(pfn, prot);
1575 if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
1576 BUG();
1579 static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
1581 unsigned pmdidx, pteidx;
1582 unsigned ident_pte;
1583 unsigned long pfn;
1585 ident_pte = 0;
1586 pfn = 0;
1587 for (pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
1588 pte_t *pte_page;
1590 /* Reuse or allocate a page of ptes */
1591 if (pmd_present(pmd[pmdidx]))
1592 pte_page = m2v(pmd[pmdidx].pmd);
1593 else {
1594 /* Check for free pte pages */
1595 if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
1596 break;
1598 pte_page = &level1_ident_pgt[ident_pte];
1599 ident_pte += PTRS_PER_PTE;
1601 pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
1604 /* Install mappings */
1605 for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
1606 pte_t pte;
1608 if (pfn > max_pfn_mapped)
1609 max_pfn_mapped = pfn;
1611 if (!pte_none(pte_page[pteidx]))
1612 continue;
1614 pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
1615 pte_page[pteidx] = pte;
1619 for (pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
1620 set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
1622 set_page_prot(pmd, PAGE_KERNEL_RO);
1625 #ifdef CONFIG_X86_64
1626 static void convert_pfn_mfn(void *v)
1628 pte_t *pte = v;
1629 int i;
1631 /* All levels are converted the same way, so just treat them
1632 as ptes. */
1633 for (i = 0; i < PTRS_PER_PTE; i++)
1634 pte[i] = xen_make_pte(pte[i].pte);
1638 * Set up the inital kernel pagetable.
1640 * We can construct this by grafting the Xen provided pagetable into
1641 * head_64.S's preconstructed pagetables. We copy the Xen L2's into
1642 * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
1643 * means that only the kernel has a physical mapping to start with -
1644 * but that's enough to get __va working. We need to fill in the rest
1645 * of the physical mapping once some sort of allocator has been set
1646 * up.
1648 __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
1649 unsigned long max_pfn)
1651 pud_t *l3;
1652 pmd_t *l2;
1654 /* Zap identity mapping */
1655 init_level4_pgt[0] = __pgd(0);
1657 /* Pre-constructed entries are in pfn, so convert to mfn */
1658 convert_pfn_mfn(init_level4_pgt);
1659 convert_pfn_mfn(level3_ident_pgt);
1660 convert_pfn_mfn(level3_kernel_pgt);
1662 l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
1663 l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
1665 memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1666 memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1668 l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
1669 l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
1670 memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1672 /* Set up identity map */
1673 xen_map_identity_early(level2_ident_pgt, max_pfn);
1675 /* Make pagetable pieces RO */
1676 set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
1677 set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
1678 set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
1679 set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
1680 set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
1681 set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
1683 /* Pin down new L4 */
1684 pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
1685 PFN_DOWN(__pa_symbol(init_level4_pgt)));
1687 /* Unpin Xen-provided one */
1688 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1690 /* Switch over */
1691 pgd = init_level4_pgt;
1694 * At this stage there can be no user pgd, and no page
1695 * structure to attach it to, so make sure we just set kernel
1696 * pgd.
1698 xen_mc_batch();
1699 __xen_write_cr3(true, __pa(pgd));
1700 xen_mc_issue(PARAVIRT_LAZY_CPU);
1702 reserve_early(__pa(xen_start_info->pt_base),
1703 __pa(xen_start_info->pt_base +
1704 xen_start_info->nr_pt_frames * PAGE_SIZE),
1705 "XEN PAGETABLES");
1707 return pgd;
1709 #else /* !CONFIG_X86_64 */
1710 static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
1712 __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
1713 unsigned long max_pfn)
1715 pmd_t *kernel_pmd;
1717 init_pg_tables_start = __pa(pgd);
1718 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
1719 max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
1721 kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
1722 memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
1724 xen_map_identity_early(level2_kernel_pgt, max_pfn);
1726 memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
1727 set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
1728 __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
1730 set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
1731 set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
1732 set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
1734 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1736 xen_write_cr3(__pa(swapper_pg_dir));
1738 pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
1740 return swapper_pg_dir;
1742 #endif /* CONFIG_X86_64 */
1744 static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
1746 pte_t pte;
1748 phys >>= PAGE_SHIFT;
1750 switch (idx) {
1751 case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
1752 #ifdef CONFIG_X86_F00F_BUG
1753 case FIX_F00F_IDT:
1754 #endif
1755 #ifdef CONFIG_X86_32
1756 case FIX_WP_TEST:
1757 case FIX_VDSO:
1758 # ifdef CONFIG_HIGHMEM
1759 case FIX_KMAP_BEGIN ... FIX_KMAP_END:
1760 # endif
1761 #else
1762 case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
1763 #endif
1764 #ifdef CONFIG_X86_LOCAL_APIC
1765 case FIX_APIC_BASE: /* maps dummy local APIC */
1766 #endif
1767 pte = pfn_pte(phys, prot);
1768 break;
1770 default:
1771 pte = mfn_pte(phys, prot);
1772 break;
1775 __native_set_fixmap(idx, pte);
1777 #ifdef CONFIG_X86_64
1778 /* Replicate changes to map the vsyscall page into the user
1779 pagetable vsyscall mapping. */
1780 if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
1781 unsigned long vaddr = __fix_to_virt(idx);
1782 set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
1784 #endif
1787 __init void xen_post_allocator_init(void)
1789 pv_mmu_ops.set_pte = xen_set_pte;
1790 pv_mmu_ops.set_pmd = xen_set_pmd;
1791 pv_mmu_ops.set_pud = xen_set_pud;
1792 #if PAGETABLE_LEVELS == 4
1793 pv_mmu_ops.set_pgd = xen_set_pgd;
1794 #endif
1796 /* This will work as long as patching hasn't happened yet
1797 (which it hasn't) */
1798 pv_mmu_ops.alloc_pte = xen_alloc_pte;
1799 pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
1800 pv_mmu_ops.release_pte = xen_release_pte;
1801 pv_mmu_ops.release_pmd = xen_release_pmd;
1802 #if PAGETABLE_LEVELS == 4
1803 pv_mmu_ops.alloc_pud = xen_alloc_pud;
1804 pv_mmu_ops.release_pud = xen_release_pud;
1805 #endif
1807 #ifdef CONFIG_X86_64
1808 SetPagePinned(virt_to_page(level3_user_vsyscall));
1809 #endif
1810 xen_mark_init_mm_pinned();
1813 static void xen_leave_lazy_mmu(void)
1815 preempt_disable();
1816 xen_mc_flush();
1817 paravirt_leave_lazy_mmu();
1818 preempt_enable();
1821 const struct pv_mmu_ops xen_mmu_ops __initdata = {
1822 .pagetable_setup_start = xen_pagetable_setup_start,
1823 .pagetable_setup_done = xen_pagetable_setup_done,
1825 .read_cr2 = xen_read_cr2,
1826 .write_cr2 = xen_write_cr2,
1828 .read_cr3 = xen_read_cr3,
1829 .write_cr3 = xen_write_cr3,
1831 .flush_tlb_user = xen_flush_tlb,
1832 .flush_tlb_kernel = xen_flush_tlb,
1833 .flush_tlb_single = xen_flush_tlb_single,
1834 .flush_tlb_others = xen_flush_tlb_others,
1836 .pte_update = paravirt_nop,
1837 .pte_update_defer = paravirt_nop,
1839 .pgd_alloc = xen_pgd_alloc,
1840 .pgd_free = xen_pgd_free,
1842 .alloc_pte = xen_alloc_pte_init,
1843 .release_pte = xen_release_pte_init,
1844 .alloc_pmd = xen_alloc_pte_init,
1845 .alloc_pmd_clone = paravirt_nop,
1846 .release_pmd = xen_release_pte_init,
1848 #ifdef CONFIG_HIGHPTE
1849 .kmap_atomic_pte = xen_kmap_atomic_pte,
1850 #endif
1852 #ifdef CONFIG_X86_64
1853 .set_pte = xen_set_pte,
1854 #else
1855 .set_pte = xen_set_pte_init,
1856 #endif
1857 .set_pte_at = xen_set_pte_at,
1858 .set_pmd = xen_set_pmd_hyper,
1860 .ptep_modify_prot_start = __ptep_modify_prot_start,
1861 .ptep_modify_prot_commit = __ptep_modify_prot_commit,
1863 .pte_val = PV_CALLEE_SAVE(xen_pte_val),
1864 .pgd_val = PV_CALLEE_SAVE(xen_pgd_val),
1866 .make_pte = PV_CALLEE_SAVE(xen_make_pte),
1867 .make_pgd = PV_CALLEE_SAVE(xen_make_pgd),
1869 #ifdef CONFIG_X86_PAE
1870 .set_pte_atomic = xen_set_pte_atomic,
1871 .set_pte_present = xen_set_pte_at,
1872 .pte_clear = xen_pte_clear,
1873 .pmd_clear = xen_pmd_clear,
1874 #endif /* CONFIG_X86_PAE */
1875 .set_pud = xen_set_pud_hyper,
1877 .make_pmd = PV_CALLEE_SAVE(xen_make_pmd),
1878 .pmd_val = PV_CALLEE_SAVE(xen_pmd_val),
1880 #if PAGETABLE_LEVELS == 4
1881 .pud_val = PV_CALLEE_SAVE(xen_pud_val),
1882 .make_pud = PV_CALLEE_SAVE(xen_make_pud),
1883 .set_pgd = xen_set_pgd_hyper,
1885 .alloc_pud = xen_alloc_pte_init,
1886 .release_pud = xen_release_pte_init,
1887 #endif /* PAGETABLE_LEVELS == 4 */
1889 .activate_mm = xen_activate_mm,
1890 .dup_mmap = xen_dup_mmap,
1891 .exit_mmap = xen_exit_mmap,
1893 .lazy_mode = {
1894 .enter = paravirt_enter_lazy_mmu,
1895 .leave = xen_leave_lazy_mmu,
1898 .set_fixmap = xen_set_fixmap,
1902 #ifdef CONFIG_XEN_DEBUG_FS
1904 static struct dentry *d_mmu_debug;
1906 static int __init xen_mmu_debugfs(void)
1908 struct dentry *d_xen = xen_init_debugfs();
1910 if (d_xen == NULL)
1911 return -ENOMEM;
1913 d_mmu_debug = debugfs_create_dir("mmu", d_xen);
1915 debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats);
1917 debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update);
1918 debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug,
1919 &mmu_stats.pgd_update_pinned);
1920 debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug,
1921 &mmu_stats.pgd_update_pinned);
1923 debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update);
1924 debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug,
1925 &mmu_stats.pud_update_pinned);
1926 debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug,
1927 &mmu_stats.pud_update_pinned);
1929 debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update);
1930 debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug,
1931 &mmu_stats.pmd_update_pinned);
1932 debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug,
1933 &mmu_stats.pmd_update_pinned);
1935 debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update);
1936 // debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug,
1937 // &mmu_stats.pte_update_pinned);
1938 debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug,
1939 &mmu_stats.pte_update_pinned);
1941 debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update);
1942 debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug,
1943 &mmu_stats.mmu_update_extended);
1944 xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug,
1945 mmu_stats.mmu_update_histo, 20);
1947 debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at);
1948 debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug,
1949 &mmu_stats.set_pte_at_batched);
1950 debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug,
1951 &mmu_stats.set_pte_at_current);
1952 debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug,
1953 &mmu_stats.set_pte_at_kernel);
1955 debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit);
1956 debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
1957 &mmu_stats.prot_commit_batched);
1959 return 0;
1961 fs_initcall(xen_mmu_debugfs);
1963 #endif /* CONFIG_XEN_DEBUG_FS */