blob: cb6afa4ec95c524ce8e95fa20b47b88a2f65dac9 [file] [log] [blame]
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001/*
2 * Xen mmu operations
3 *
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.
7 *
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.
12 *
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.
17 *
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.
23 *
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.
30 *
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.
38 *
39 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
40 */
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070041#include <linux/sched.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070042#include <linux/highmem.h>
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070043#include <linux/debugfs.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070044#include <linux/bug.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070045
46#include <asm/pgtable.h>
47#include <asm/tlbflush.h>
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -070048#include <asm/fixmap.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070049#include <asm/mmu_context.h>
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -080050#include <asm/setup.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070051#include <asm/paravirt.h>
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -070052#include <asm/linkage.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070053
54#include <asm/xen/hypercall.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070055#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070056
57#include <xen/page.h>
58#include <xen/interface/xen.h>
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -080059#include <xen/interface/version.h>
60#include <xen/hvc-console.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070061
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070062#include "multicalls.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070063#include "mmu.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070064#include "debugfs.h"
65
66#define MMU_UPDATE_HISTO 30
67
68#ifdef CONFIG_XEN_DEBUG_FS
69
70static struct {
71 u32 pgd_update;
72 u32 pgd_update_pinned;
73 u32 pgd_update_batched;
74
75 u32 pud_update;
76 u32 pud_update_pinned;
77 u32 pud_update_batched;
78
79 u32 pmd_update;
80 u32 pmd_update_pinned;
81 u32 pmd_update_batched;
82
83 u32 pte_update;
84 u32 pte_update_pinned;
85 u32 pte_update_batched;
86
87 u32 mmu_update;
88 u32 mmu_update_extended;
89 u32 mmu_update_histo[MMU_UPDATE_HISTO];
90
91 u32 prot_commit;
92 u32 prot_commit_batched;
93
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;
100
101static u8 zero_stats;
102
103static inline void check_zero(void)
104{
105 if (unlikely(zero_stats)) {
106 memset(&mmu_stats, 0, sizeof(mmu_stats));
107 zero_stats = 0;
108 }
109}
110
111#define ADD_STATS(elem, val) \
112 do { check_zero(); mmu_stats.elem += (val); } while(0)
113
114#else /* !CONFIG_XEN_DEBUG_FS */
115
116#define ADD_STATS(elem, val) do { (void)(val); } while(0)
117
118#endif /* CONFIG_XEN_DEBUG_FS */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700119
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -0800120
121/*
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.
125 */
126static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
127
128#ifdef CONFIG_X86_64
129/* l3 pud for userspace vsyscall mapping */
130static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
131#endif /* CONFIG_X86_64 */
132
133/*
134 * Note about cr3 (pagetable base) values:
135 *
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.
141 *
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.
146 */
147DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
148DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
149
150
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700151/*
152 * Just beyond the highest usermode address. STACK_TOP_MAX has a
153 * redzone above it, so round it up to a PGD boundary.
154 */
155#define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
156
157
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100158#define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100159#define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100160
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100161/* Placeholder for holes in the address space */
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -0700162static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] __page_aligned_data =
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100163 { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL };
164
165 /* Array of pointers to pages containing p2m entries */
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -0700166static unsigned long *p2m_top[TOP_ENTRIES] __page_aligned_data =
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100167 { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] };
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100168
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100169/* Arrays of p2m arrays expressed in mfns used for save/restore */
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -0700170static unsigned long p2m_top_mfn[TOP_ENTRIES] __page_aligned_bss;
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100171
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -0700172static unsigned long p2m_top_mfn_list[TOP_ENTRIES / P2M_ENTRIES_PER_PAGE]
173 __page_aligned_bss;
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100174
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100175static inline unsigned p2m_top_index(unsigned long pfn)
176{
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100177 BUG_ON(pfn >= MAX_DOMAIN_PAGES);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100178 return pfn / P2M_ENTRIES_PER_PAGE;
179}
180
181static inline unsigned p2m_index(unsigned long pfn)
182{
183 return pfn % P2M_ENTRIES_PER_PAGE;
184}
185
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100186/* Build the parallel p2m_top_mfn structures */
187void xen_setup_mfn_list_list(void)
188{
189 unsigned pfn, idx;
190
Tejf63c2f22008-12-16 11:56:06 -0800191 for (pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) {
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100192 unsigned topidx = p2m_top_index(pfn);
193
194 p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]);
195 }
196
Tejf63c2f22008-12-16 11:56:06 -0800197 for (idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) {
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100198 unsigned topidx = idx * P2M_ENTRIES_PER_PAGE;
199 p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]);
200 }
201
202 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
203
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;
207}
208
209/* Set up p2m_top to point to the domain-builder provided p2m pages */
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100210void __init xen_build_dynamic_phys_to_machine(void)
211{
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100212 unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100213 unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100214 unsigned pfn;
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100215
Tejf63c2f22008-12-16 11:56:06 -0800216 for (pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) {
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100217 unsigned topidx = p2m_top_index(pfn);
218
219 p2m_top[topidx] = &mfn_list[pfn];
220 }
221}
222
223unsigned long get_phys_to_machine(unsigned long pfn)
224{
225 unsigned topidx, idx;
226
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100227 if (unlikely(pfn >= MAX_DOMAIN_PAGES))
228 return INVALID_P2M_ENTRY;
229
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100230 topidx = p2m_top_index(pfn);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100231 idx = p2m_index(pfn);
232 return p2m_top[topidx][idx];
233}
Ingo Molnar15ce60052008-06-02 13:20:11 +0200234EXPORT_SYMBOL_GPL(get_phys_to_machine);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100235
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100236static void alloc_p2m(unsigned long **pp, unsigned long *mfnp)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100237{
238 unsigned long *p;
239 unsigned i;
240
241 p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL);
242 BUG_ON(p == NULL);
243
Tejf63c2f22008-12-16 11:56:06 -0800244 for (i = 0; i < P2M_ENTRIES_PER_PAGE; i++)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100245 p[i] = INVALID_P2M_ENTRY;
246
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100247 if (cmpxchg(pp, p2m_missing, p) != p2m_missing)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100248 free_page((unsigned long)p);
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100249 else
250 *mfnp = virt_to_mfn(p);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100251}
252
253void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
254{
255 unsigned topidx, idx;
256
257 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
258 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
259 return;
260 }
261
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100262 if (unlikely(pfn >= MAX_DOMAIN_PAGES)) {
263 BUG_ON(mfn != INVALID_P2M_ENTRY);
264 return;
265 }
266
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100267 topidx = p2m_top_index(pfn);
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100268 if (p2m_top[topidx] == p2m_missing) {
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100269 /* no need to allocate a page to store an invalid entry */
270 if (mfn == INVALID_P2M_ENTRY)
271 return;
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100272 alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100273 }
274
275 idx = p2m_index(pfn);
276 p2m_top[topidx][idx] = mfn;
277}
278
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800279unsigned long arbitrary_virt_to_mfn(void *vaddr)
280{
281 xmaddr_t maddr = arbitrary_virt_to_machine(vaddr);
282
283 return PFN_DOWN(maddr.maddr);
284}
285
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700286xmaddr_t arbitrary_virt_to_machine(void *vaddr)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700287{
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700288 unsigned long address = (unsigned long)vaddr;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100289 unsigned int level;
Chris Lalancette9f32d212008-10-23 17:40:25 -0700290 pte_t *pte;
291 unsigned offset;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700292
Chris Lalancette9f32d212008-10-23 17:40:25 -0700293 /*
294 * if the PFN is in the linear mapped vaddr range, we can just use
295 * the (quick) virt_to_machine() p2m lookup
296 */
297 if (virt_addr_valid(vaddr))
298 return virt_to_machine(vaddr);
299
300 /* otherwise we have to do a (slower) full page-table walk */
301
302 pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700303 BUG_ON(pte == NULL);
Chris Lalancette9f32d212008-10-23 17:40:25 -0700304 offset = address & ~PAGE_MASK;
Jeremy Fitzhardingeebd879e2008-07-08 15:06:54 -0700305 return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700306}
307
308void make_lowmem_page_readonly(void *vaddr)
309{
310 pte_t *pte, ptev;
311 unsigned long address = (unsigned long)vaddr;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100312 unsigned int level;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700313
Ingo Molnarf0646e42008-01-30 13:33:43 +0100314 pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700315 BUG_ON(pte == NULL);
316
317 ptev = pte_wrprotect(*pte);
318
319 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
320 BUG();
321}
322
323void make_lowmem_page_readwrite(void *vaddr)
324{
325 pte_t *pte, ptev;
326 unsigned long address = (unsigned long)vaddr;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100327 unsigned int level;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700328
Ingo Molnarf0646e42008-01-30 13:33:43 +0100329 pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700330 BUG_ON(pte == NULL);
331
332 ptev = pte_mkwrite(*pte);
333
334 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
335 BUG();
336}
337
338
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700339static bool xen_page_pinned(void *ptr)
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100340{
341 struct page *page = virt_to_page(ptr);
342
343 return PagePinned(page);
344}
345
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700346static void xen_extend_mmu_update(const struct mmu_update *update)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700347{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700348 struct multicall_space mcs;
349 struct mmu_update *u;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700350
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700351 mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
352
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700353 if (mcs.mc != NULL) {
354 ADD_STATS(mmu_update_extended, 1);
355 ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1);
356
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700357 mcs.mc->args[1]++;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700358
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);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700365 mcs = __xen_mc_entry(sizeof(*u));
366 MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700367 ADD_STATS(mmu_update_histo[1], 1);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700368 }
369
370 u = mcs.args;
371 *u = *update;
372}
373
374void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
375{
376 struct mmu_update u;
377
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700378 preempt_disable();
379
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700380 xen_mc_batch();
381
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700382 /* ptr may be ioremapped for 64-bit pagetable setup */
383 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700384 u.val = pmd_val_ma(val);
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700385 xen_extend_mmu_update(&u);
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700386
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700387 ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
388
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700389 xen_mc_issue(PARAVIRT_LAZY_MMU);
390
391 preempt_enable();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700392}
393
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100394void xen_set_pmd(pmd_t *ptr, pmd_t val)
395{
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700396 ADD_STATS(pmd_update, 1);
397
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100398 /* If page is not pinned, we can just update the entry
399 directly */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700400 if (!xen_page_pinned(ptr)) {
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100401 *ptr = val;
402 return;
403 }
404
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700405 ADD_STATS(pmd_update_pinned, 1);
406
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100407 xen_set_pmd_hyper(ptr, val);
408}
409
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700410/*
411 * Associate a virtual page frame with a given physical page frame
412 * and protection flags for that frame.
413 */
414void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
415{
Jeremy Fitzhardinge836fe2f2008-07-08 15:06:58 -0700416 set_pte_vaddr(vaddr, mfn_pte(mfn, flags));
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700417}
418
419void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
420 pte_t *ptep, pte_t pteval)
421{
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700422 /* updates to init_mm may be done without lock */
423 if (mm == &init_mm)
424 preempt_disable();
425
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700426 ADD_STATS(set_pte_at, 1);
427// ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep));
428 ADD_STATS(set_pte_at_current, mm == current->mm);
429 ADD_STATS(set_pte_at_kernel, mm == &init_mm);
430
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700431 if (mm == current->mm || mm == &init_mm) {
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700432 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700433 struct multicall_space mcs;
434 mcs = xen_mc_entry(0);
435
436 MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700437 ADD_STATS(set_pte_at_batched, 1);
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700438 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700439 goto out;
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700440 } else
441 if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700442 goto out;
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700443 }
444 xen_set_pte(ptep, pteval);
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700445
446out:
447 if (mm == &init_mm)
448 preempt_enable();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700449}
450
Tejf63c2f22008-12-16 11:56:06 -0800451pte_t xen_ptep_modify_prot_start(struct mm_struct *mm,
452 unsigned long addr, pte_t *ptep)
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700453{
454 /* Just return the pte as-is. We preserve the bits on commit */
455 return *ptep;
456}
457
458void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
459 pte_t *ptep, pte_t pte)
460{
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700461 struct mmu_update u;
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700462
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700463 xen_mc_batch();
464
Chris Lalancette9f32d212008-10-23 17:40:25 -0700465 u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700466 u.val = pte_val_ma(pte);
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700467 xen_extend_mmu_update(&u);
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700468
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700469 ADD_STATS(prot_commit, 1);
470 ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
471
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700472 xen_mc_issue(PARAVIRT_LAZY_MMU);
473}
474
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700475/* Assume pteval_t is equivalent to all the other *val_t types. */
476static pteval_t pte_mfn_to_pfn(pteval_t val)
477{
478 if (val & _PAGE_PRESENT) {
Jeremy Fitzhardinge59438c92008-07-21 22:59:42 -0700479 unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
Jeremy Fitzhardinge77be1fa2008-07-21 22:59:56 -0700480 pteval_t flags = val & PTE_FLAGS_MASK;
Jeremy Fitzhardinged8355ac2008-07-03 22:10:18 -0700481 val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags;
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700482 }
483
484 return val;
485}
486
487static pteval_t pte_pfn_to_mfn(pteval_t val)
488{
489 if (val & _PAGE_PRESENT) {
Jeremy Fitzhardinge59438c92008-07-21 22:59:42 -0700490 unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
Jeremy Fitzhardinge77be1fa2008-07-21 22:59:56 -0700491 pteval_t flags = val & PTE_FLAGS_MASK;
Jeremy Fitzhardinged8355ac2008-07-03 22:10:18 -0700492 val = ((pteval_t)pfn_to_mfn(pfn) << PAGE_SHIFT) | flags;
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700493 }
494
495 return val;
496}
497
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700498pteval_t xen_pte_val(pte_t pte)
499{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700500 return pte_mfn_to_pfn(pte.pte);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700501}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800502PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700503
504pgdval_t xen_pgd_val(pgd_t pgd)
505{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700506 return pte_mfn_to_pfn(pgd.pgd);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700507}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800508PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700509
510pte_t xen_make_pte(pteval_t pte)
511{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700512 pte = pte_pfn_to_mfn(pte);
513 return native_make_pte(pte);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700514}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800515PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700516
517pgd_t xen_make_pgd(pgdval_t pgd)
518{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700519 pgd = pte_pfn_to_mfn(pgd);
520 return native_make_pgd(pgd);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700521}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800522PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700523
524pmdval_t xen_pmd_val(pmd_t pmd)
525{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700526 return pte_mfn_to_pfn(pmd.pmd);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700527}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800528PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val);
Jeremy Fitzhardinge28499142008-05-09 12:05:57 +0100529
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100530void xen_set_pud_hyper(pud_t *ptr, pud_t val)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700531{
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700532 struct mmu_update u;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700533
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700534 preempt_disable();
535
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700536 xen_mc_batch();
537
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700538 /* ptr may be ioremapped for 64-bit pagetable setup */
539 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700540 u.val = pud_val_ma(val);
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700541 xen_extend_mmu_update(&u);
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700542
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700543 ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
544
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700545 xen_mc_issue(PARAVIRT_LAZY_MMU);
546
547 preempt_enable();
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700548}
549
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100550void xen_set_pud(pud_t *ptr, pud_t val)
551{
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700552 ADD_STATS(pud_update, 1);
553
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100554 /* If page is not pinned, we can just update the entry
555 directly */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700556 if (!xen_page_pinned(ptr)) {
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100557 *ptr = val;
558 return;
559 }
560
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700561 ADD_STATS(pud_update_pinned, 1);
562
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100563 xen_set_pud_hyper(ptr, val);
564}
565
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700566void xen_set_pte(pte_t *ptep, pte_t pte)
567{
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700568 ADD_STATS(pte_update, 1);
569// ADD_STATS(pte_update_pinned, xen_page_pinned(ptep));
570 ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
571
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700572#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700573 ptep->pte_high = pte.pte_high;
574 smp_wmb();
575 ptep->pte_low = pte.pte_low;
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700576#else
577 *ptep = pte;
578#endif
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700579}
580
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700581#ifdef CONFIG_X86_PAE
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700582void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
583{
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700584 set_64bit((u64 *)ptep, native_pte_val(pte));
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700585}
586
587void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
588{
589 ptep->pte_low = 0;
590 smp_wmb(); /* make sure low gets written first */
591 ptep->pte_high = 0;
592}
593
594void xen_pmd_clear(pmd_t *pmdp)
595{
Jeremy Fitzhardingee2426cf2008-05-31 01:24:27 +0100596 set_pmd(pmdp, __pmd(0));
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700597}
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700598#endif /* CONFIG_X86_PAE */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700599
Jeremy Fitzhardingeabf33032008-03-17 16:37:07 -0700600pmd_t xen_make_pmd(pmdval_t pmd)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700601{
Jeremy Fitzhardingeebb9cfe2008-06-16 15:01:56 -0700602 pmd = pte_pfn_to_mfn(pmd);
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700603 return native_make_pmd(pmd);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700604}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800605PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700606
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700607#if PAGETABLE_LEVELS == 4
608pudval_t xen_pud_val(pud_t pud)
609{
610 return pte_mfn_to_pfn(pud.pud);
611}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800612PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val);
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700613
614pud_t xen_make_pud(pudval_t pud)
615{
616 pud = pte_pfn_to_mfn(pud);
617
618 return native_make_pud(pud);
619}
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -0800620PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud);
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700621
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700622pgd_t *xen_get_user_pgd(pgd_t *pgd)
623{
624 pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
625 unsigned offset = pgd - pgd_page;
626 pgd_t *user_ptr = NULL;
627
628 if (offset < pgd_index(USER_LIMIT)) {
629 struct page *page = virt_to_page(pgd_page);
630 user_ptr = (pgd_t *)page->private;
631 if (user_ptr)
632 user_ptr += offset;
633 }
634
635 return user_ptr;
636}
637
638static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700639{
640 struct mmu_update u;
641
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700642 u.ptr = virt_to_machine(ptr).maddr;
643 u.val = pgd_val_ma(val);
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700644 xen_extend_mmu_update(&u);
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700645}
646
647/*
648 * Raw hypercall-based set_pgd, intended for in early boot before
649 * there's a page structure. This implies:
650 * 1. The only existing pagetable is the kernel's
651 * 2. It is always pinned
652 * 3. It has no user pagetable attached to it
653 */
654void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
655{
656 preempt_disable();
657
658 xen_mc_batch();
659
660 __xen_set_pgd_hyper(ptr, val);
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700661
662 xen_mc_issue(PARAVIRT_LAZY_MMU);
663
664 preempt_enable();
665}
666
667void xen_set_pgd(pgd_t *ptr, pgd_t val)
668{
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700669 pgd_t *user_ptr = xen_get_user_pgd(ptr);
670
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700671 ADD_STATS(pgd_update, 1);
672
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700673 /* If page is not pinned, we can just update the entry
674 directly */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700675 if (!xen_page_pinned(ptr)) {
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700676 *ptr = val;
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700677 if (user_ptr) {
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700678 WARN_ON(xen_page_pinned(user_ptr));
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700679 *user_ptr = val;
680 }
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700681 return;
682 }
683
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700684 ADD_STATS(pgd_update_pinned, 1);
685 ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
686
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700687 /* If it's pinned, then we can at least batch the kernel and
688 user updates together. */
689 xen_mc_batch();
690
691 __xen_set_pgd_hyper(ptr, val);
692 if (user_ptr)
693 __xen_set_pgd_hyper(user_ptr, val);
694
695 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700696}
697#endif /* PAGETABLE_LEVELS == 4 */
698
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700699/*
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700700 * (Yet another) pagetable walker. This one is intended for pinning a
701 * pagetable. This means that it walks a pagetable and calls the
702 * callback function on each page it finds making up the page table,
703 * at every level. It walks the entire pagetable, but it only bothers
704 * pinning pte pages which are below limit. In the normal case this
705 * will be STACK_TOP_MAX, but at boot we need to pin up to
706 * FIXADDR_TOP.
707 *
708 * For 32-bit the important bit is that we don't pin beyond there,
709 * because then we start getting into Xen's ptes.
710 *
711 * For 64-bit, we must skip the Xen hole in the middle of the address
712 * space, just after the big x86-64 virtual hole.
713 */
Ian Campbell86bbc2c2008-11-21 10:21:33 +0000714static int __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd,
715 int (*func)(struct mm_struct *mm, struct page *,
716 enum pt_level),
717 unsigned long limit)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700718{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700719 int flush = 0;
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700720 unsigned hole_low, hole_high;
721 unsigned pgdidx_limit, pudidx_limit, pmdidx_limit;
722 unsigned pgdidx, pudidx, pmdidx;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700723
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700724 /* The limit is the last byte to be touched */
725 limit--;
726 BUG_ON(limit >= FIXADDR_TOP);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700727
728 if (xen_feature(XENFEAT_auto_translated_physmap))
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700729 return 0;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700730
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700731 /*
732 * 64-bit has a great big hole in the middle of the address
733 * space, which contains the Xen mappings. On 32-bit these
734 * will end up making a zero-sized hole and so is a no-op.
735 */
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700736 hole_low = pgd_index(USER_LIMIT);
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700737 hole_high = pgd_index(PAGE_OFFSET);
738
739 pgdidx_limit = pgd_index(limit);
740#if PTRS_PER_PUD > 1
741 pudidx_limit = pud_index(limit);
742#else
743 pudidx_limit = 0;
744#endif
745#if PTRS_PER_PMD > 1
746 pmdidx_limit = pmd_index(limit);
747#else
748 pmdidx_limit = 0;
749#endif
750
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700751 for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) {
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700752 pud_t *pud;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700753
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700754 if (pgdidx >= hole_low && pgdidx < hole_high)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700755 continue;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700756
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700757 if (!pgd_val(pgd[pgdidx]))
758 continue;
759
760 pud = pud_offset(&pgd[pgdidx], 0);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700761
762 if (PTRS_PER_PUD > 1) /* not folded */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700763 flush |= (*func)(mm, virt_to_page(pud), PT_PUD);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700764
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700765 for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) {
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700766 pmd_t *pmd;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700767
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700768 if (pgdidx == pgdidx_limit &&
769 pudidx > pudidx_limit)
770 goto out;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700771
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700772 if (pud_none(pud[pudidx]))
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700773 continue;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700774
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700775 pmd = pmd_offset(&pud[pudidx], 0);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700776
777 if (PTRS_PER_PMD > 1) /* not folded */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700778 flush |= (*func)(mm, virt_to_page(pmd), PT_PMD);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700779
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700780 for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) {
781 struct page *pte;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700782
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700783 if (pgdidx == pgdidx_limit &&
784 pudidx == pudidx_limit &&
785 pmdidx > pmdidx_limit)
786 goto out;
787
788 if (pmd_none(pmd[pmdidx]))
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700789 continue;
790
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700791 pte = pmd_page(pmd[pmdidx]);
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700792 flush |= (*func)(mm, pte, PT_PTE);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700793 }
794 }
795 }
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -0700796
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700797out:
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -0700798 /* Do the top level last, so that the callbacks can use it as
799 a cue to do final things like tlb flushes. */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700800 flush |= (*func)(mm, virt_to_page(pgd), PT_PGD);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700801
802 return flush;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700803}
804
Ian Campbell86bbc2c2008-11-21 10:21:33 +0000805static int xen_pgd_walk(struct mm_struct *mm,
806 int (*func)(struct mm_struct *mm, struct page *,
807 enum pt_level),
808 unsigned long limit)
809{
810 return __xen_pgd_walk(mm, mm->pgd, func, limit);
811}
812
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700813/* If we're using split pte locks, then take the page's lock and
814 return a pointer to it. Otherwise return NULL. */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700815static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700816{
817 spinlock_t *ptl = NULL;
818
Jeremy Fitzhardingef7d0b922008-09-09 15:43:22 -0700819#if USE_SPLIT_PTLOCKS
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700820 ptl = __pte_lockptr(page);
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700821 spin_lock_nest_lock(ptl, &mm->page_table_lock);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700822#endif
823
824 return ptl;
825}
826
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700827static void xen_pte_unlock(void *v)
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700828{
829 spinlock_t *ptl = v;
830 spin_unlock(ptl);
831}
832
833static void xen_do_pin(unsigned level, unsigned long pfn)
834{
835 struct mmuext_op *op;
836 struct multicall_space mcs;
837
838 mcs = __xen_mc_entry(sizeof(*op));
839 op = mcs.args;
840 op->cmd = level;
841 op->arg1.mfn = pfn_to_mfn(pfn);
842 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
843}
844
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700845static int xen_pin_page(struct mm_struct *mm, struct page *page,
846 enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700847{
Christoph Lameterd60cd462008-04-28 02:12:51 -0700848 unsigned pgfl = TestSetPagePinned(page);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700849 int flush;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700850
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700851 if (pgfl)
852 flush = 0; /* already pinned */
853 else if (PageHighMem(page))
854 /* kmaps need flushing if we found an unpinned
855 highpage */
856 flush = 1;
857 else {
858 void *pt = lowmem_page_address(page);
859 unsigned long pfn = page_to_pfn(page);
860 struct multicall_space mcs = __xen_mc_entry(0);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700861 spinlock_t *ptl;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700862
863 flush = 0;
864
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -0700865 /*
866 * We need to hold the pagetable lock between the time
867 * we make the pagetable RO and when we actually pin
868 * it. If we don't, then other users may come in and
869 * attempt to update the pagetable by writing it,
870 * which will fail because the memory is RO but not
871 * pinned, so Xen won't do the trap'n'emulate.
872 *
873 * If we're using split pte locks, we can't hold the
874 * entire pagetable's worth of locks during the
875 * traverse, because we may wrap the preempt count (8
876 * bits). The solution is to mark RO and pin each PTE
877 * page while holding the lock. This means the number
878 * of locks we end up holding is never more than a
879 * batch size (~32 entries, at present).
880 *
881 * If we're not using split pte locks, we needn't pin
882 * the PTE pages independently, because we're
883 * protected by the overall pagetable lock.
884 */
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700885 ptl = NULL;
886 if (level == PT_PTE)
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700887 ptl = xen_pte_lock(page, mm);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700888
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700889 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
890 pfn_pte(pfn, PAGE_KERNEL_RO),
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700891 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
892
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -0700893 if (ptl) {
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700894 xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
895
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700896 /* Queue a deferred unlock for when this batch
897 is completed. */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -0700898 xen_mc_callback(xen_pte_unlock, ptl);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700899 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700900 }
901
902 return flush;
903}
904
905/* This is called just after a mm has been created, but it has not
906 been used yet. We need to make sure that its pagetable is all
907 read-only, and can be pinned. */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700908static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700909{
Jeremy Fitzhardinged05fdf32008-10-28 19:23:06 +1100910 vm_unmap_aliases();
911
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700912 xen_mc_batch();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700913
Ian Campbell86bbc2c2008-11-21 10:21:33 +0000914 if (__xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT)) {
Jeremy Fitzhardinged05fdf32008-10-28 19:23:06 +1100915 /* re-enable interrupts for flushing */
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700916 xen_mc_issue(0);
Jeremy Fitzhardinged05fdf32008-10-28 19:23:06 +1100917
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700918 kmap_flush_unused();
Jeremy Fitzhardinged05fdf32008-10-28 19:23:06 +1100919
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700920 xen_mc_batch();
921 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700922
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700923#ifdef CONFIG_X86_64
924 {
925 pgd_t *user_pgd = xen_get_user_pgd(pgd);
926
927 xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
928
929 if (user_pgd) {
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700930 xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
Tejf63c2f22008-12-16 11:56:06 -0800931 xen_do_pin(MMUEXT_PIN_L4_TABLE,
932 PFN_DOWN(__pa(user_pgd)));
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700933 }
934 }
935#else /* CONFIG_X86_32 */
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700936#ifdef CONFIG_X86_PAE
937 /* Need to make sure unshared kernel PMD is pinnable */
Jeremy Fitzhardinge47cb2ed2008-11-06 13:48:24 -0800938 xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700939 PT_PMD);
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -0700940#endif
Jeremy Fitzhardinge28499142008-05-09 12:05:57 +0100941 xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -0700942#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700943 xen_mc_issue(0);
944}
945
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700946static void xen_pgd_pin(struct mm_struct *mm)
947{
948 __xen_pgd_pin(mm, mm->pgd);
949}
950
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100951/*
952 * On save, we need to pin all pagetables to make sure they get their
953 * mfns turned into pfns. Search the list for any unpinned pgds and pin
954 * them (unpinned pgds are not currently in use, probably because the
955 * process is under construction or destruction).
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700956 *
957 * Expected to be called in stop_machine() ("equivalent to taking
958 * every spinlock in the system"), so the locking doesn't really
959 * matter all that much.
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100960 */
961void xen_mm_pin_all(void)
962{
963 unsigned long flags;
964 struct page *page;
965
966 spin_lock_irqsave(&pgd_lock, flags);
967
968 list_for_each_entry(page, &pgd_list, lru) {
969 if (!PagePinned(page)) {
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700970 __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100971 SetPageSavePinned(page);
972 }
973 }
974
975 spin_unlock_irqrestore(&pgd_lock, flags);
976}
977
Eduardo Habkostc1f2f092008-07-08 15:06:24 -0700978/*
979 * The init_mm pagetable is really pinned as soon as its created, but
980 * that's before we have page structures to store the bits. So do all
981 * the book-keeping now.
982 */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700983static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
984 enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700985{
986 SetPagePinned(page);
987 return 0;
988}
989
990void __init xen_mark_init_mm_pinned(void)
991{
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700992 xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700993}
994
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -0700995static int xen_unpin_page(struct mm_struct *mm, struct page *page,
996 enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700997{
Christoph Lameterd60cd462008-04-28 02:12:51 -0700998 unsigned pgfl = TestClearPagePinned(page);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700999
1000 if (pgfl && !PageHighMem(page)) {
1001 void *pt = lowmem_page_address(page);
1002 unsigned long pfn = page_to_pfn(page);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001003 spinlock_t *ptl = NULL;
1004 struct multicall_space mcs;
1005
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -07001006 /*
1007 * Do the converse to pin_page. If we're using split
1008 * pte locks, we must be holding the lock for while
1009 * the pte page is unpinned but still RO to prevent
1010 * concurrent updates from seeing it in this
1011 * partially-pinned state.
1012 */
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001013 if (level == PT_PTE) {
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001014 ptl = xen_pte_lock(page, mm);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001015
Jeremy Fitzhardinge11ad93e2008-08-19 13:32:51 -07001016 if (ptl)
1017 xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001018 }
1019
1020 mcs = __xen_mc_entry(0);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001021
1022 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
1023 pfn_pte(pfn, PAGE_KERNEL),
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001024 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
1025
1026 if (ptl) {
1027 /* unlock when batch completed */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -07001028 xen_mc_callback(xen_pte_unlock, ptl);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001029 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001030 }
1031
1032 return 0; /* never need to flush on unpin */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001033}
1034
1035/* Release a pagetables pages back as normal RW */
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001036static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001037{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001038 xen_mc_batch();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001039
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001040 xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001041
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -07001042#ifdef CONFIG_X86_64
1043 {
1044 pgd_t *user_pgd = xen_get_user_pgd(pgd);
1045
1046 if (user_pgd) {
Tejf63c2f22008-12-16 11:56:06 -08001047 xen_do_pin(MMUEXT_UNPIN_TABLE,
1048 PFN_DOWN(__pa(user_pgd)));
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001049 xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -07001050 }
1051 }
1052#endif
1053
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -07001054#ifdef CONFIG_X86_PAE
1055 /* Need to make sure unshared kernel PMD is unpinned */
Jeremy Fitzhardinge47cb2ed2008-11-06 13:48:24 -08001056 xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001057 PT_PMD);
Jeremy Fitzhardinge5deb30d2008-07-08 15:07:06 -07001058#endif
Jeremy Fitzhardinged6182fb2008-07-08 15:07:13 -07001059
Ian Campbell86bbc2c2008-11-21 10:21:33 +00001060 __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001061
1062 xen_mc_issue(0);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001063}
1064
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001065static void xen_pgd_unpin(struct mm_struct *mm)
1066{
1067 __xen_pgd_unpin(mm, mm->pgd);
1068}
1069
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001070/*
1071 * On resume, undo any pinning done at save, so that the rest of the
1072 * kernel doesn't see any unexpected pinned pagetables.
1073 */
1074void xen_mm_unpin_all(void)
1075{
1076 unsigned long flags;
1077 struct page *page;
1078
1079 spin_lock_irqsave(&pgd_lock, flags);
1080
1081 list_for_each_entry(page, &pgd_list, lru) {
1082 if (PageSavePinned(page)) {
1083 BUG_ON(!PagePinned(page));
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001084 __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001085 ClearPageSavePinned(page);
1086 }
1087 }
1088
1089 spin_unlock_irqrestore(&pgd_lock, flags);
1090}
1091
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001092void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
1093{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001094 spin_lock(&next->page_table_lock);
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001095 xen_pgd_pin(next);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001096 spin_unlock(&next->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001097}
1098
1099void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
1100{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001101 spin_lock(&mm->page_table_lock);
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001102 xen_pgd_pin(mm);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -07001103 spin_unlock(&mm->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001104}
1105
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001106
1107#ifdef CONFIG_SMP
1108/* Another cpu may still have their %cr3 pointing at the pagetable, so
1109 we need to repoint it somewhere else before we can unpin it. */
1110static void drop_other_mm_ref(void *info)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001111{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001112 struct mm_struct *mm = info;
Jeremy Fitzhardingece87b3d2008-07-08 15:06:40 -07001113 struct mm_struct *active_mm;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001114
Brian Gerst9eb912d2009-01-19 00:38:57 +09001115 active_mm = percpu_read(cpu_tlbstate.active_mm);
Jeremy Fitzhardingece87b3d2008-07-08 15:06:40 -07001116
1117 if (active_mm == mm)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001118 leave_mm(smp_processor_id());
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001119
1120 /* If this cpu still has a stale cr3 reference, then make sure
1121 it has been flushed. */
Ingo Molnar6dbde352009-01-15 22:15:53 +09001122 if (percpu_read(xen_current_cr3) == __pa(mm->pgd)) {
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001123 load_cr3(swapper_pg_dir);
1124 arch_flush_lazy_cpu_mode();
1125 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001126}
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001127
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -07001128static void xen_drop_mm_ref(struct mm_struct *mm)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001129{
Mike Travise4d98202008-12-16 17:34:05 -08001130 cpumask_var_t mask;
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001131 unsigned cpu;
1132
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001133 if (current->active_mm == mm) {
1134 if (current->mm == mm)
1135 load_cr3(swapper_pg_dir);
1136 else
1137 leave_mm(smp_processor_id());
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001138 arch_flush_lazy_cpu_mode();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001139 }
1140
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001141 /* Get the "official" set of cpus referring to our pagetable. */
Mike Travise4d98202008-12-16 17:34:05 -08001142 if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) {
1143 for_each_online_cpu(cpu) {
1144 if (!cpumask_test_cpu(cpu, &mm->cpu_vm_mask)
1145 && per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd))
1146 continue;
1147 smp_call_function_single(cpu, drop_other_mm_ref, mm, 1);
1148 }
1149 return;
1150 }
1151 cpumask_copy(mask, &mm->cpu_vm_mask);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001152
1153 /* It's possible that a vcpu may have a stale reference to our
1154 cr3, because its in lazy mode, and it hasn't yet flushed
1155 its set of pending hypercalls yet. In this case, we can
1156 look at its actual current cr3 value, and force it to flush
1157 if needed. */
1158 for_each_online_cpu(cpu) {
1159 if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
Mike Travise4d98202008-12-16 17:34:05 -08001160 cpumask_set_cpu(cpu, mask);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -07001161 }
1162
Mike Travise4d98202008-12-16 17:34:05 -08001163 if (!cpumask_empty(mask))
1164 smp_call_function_many(mask, drop_other_mm_ref, mm, 1);
1165 free_cpumask_var(mask);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001166}
1167#else
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -07001168static void xen_drop_mm_ref(struct mm_struct *mm)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001169{
1170 if (current->active_mm == mm)
1171 load_cr3(swapper_pg_dir);
1172}
1173#endif
1174
1175/*
1176 * While a process runs, Xen pins its pagetables, which means that the
1177 * hypervisor forces it to be read-only, and it controls all updates
1178 * to it. This means that all pagetable updates have to go via the
1179 * hypervisor, which is moderately expensive.
1180 *
1181 * Since we're pulling the pagetable down, we switch to use init_mm,
1182 * unpin old process pagetable and mark it all read-write, which
1183 * allows further operations on it to be simple memory accesses.
1184 *
1185 * The only subtle point is that another CPU may be still using the
1186 * pagetable because of lazy tlb flushing. This means we need need to
1187 * switch all CPUs off this pagetable before we can unpin it.
1188 */
1189void xen_exit_mmap(struct mm_struct *mm)
1190{
1191 get_cpu(); /* make sure we don't move around */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -07001192 xen_drop_mm_ref(mm);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001193 put_cpu();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001194
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -07001195 spin_lock(&mm->page_table_lock);
Jeremy Fitzhardingedf912ea2007-09-25 11:50:00 -07001196
1197 /* pgd may not be pinned in the error exit path of execve */
Jeremy Fitzhardinge7708ad62008-08-19 13:34:22 -07001198 if (xen_page_pinned(mm->pgd))
Jeremy Fitzhardingeeefb47f2008-10-08 13:01:39 -07001199 xen_pgd_unpin(mm);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -07001200
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -07001201 spin_unlock(&mm->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -07001202}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -07001203
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001204static __init void xen_pagetable_setup_start(pgd_t *base)
1205{
1206}
1207
1208static __init void xen_pagetable_setup_done(pgd_t *base)
1209{
1210 xen_setup_shared_info();
1211}
1212
1213static void xen_write_cr2(unsigned long cr2)
1214{
1215 percpu_read(xen_vcpu)->arch.cr2 = cr2;
1216}
1217
1218static unsigned long xen_read_cr2(void)
1219{
1220 return percpu_read(xen_vcpu)->arch.cr2;
1221}
1222
1223unsigned long xen_read_cr2_direct(void)
1224{
1225 return percpu_read(xen_vcpu_info.arch.cr2);
1226}
1227
1228static void xen_flush_tlb(void)
1229{
1230 struct mmuext_op *op;
1231 struct multicall_space mcs;
1232
1233 preempt_disable();
1234
1235 mcs = xen_mc_entry(sizeof(*op));
1236
1237 op = mcs.args;
1238 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
1239 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1240
1241 xen_mc_issue(PARAVIRT_LAZY_MMU);
1242
1243 preempt_enable();
1244}
1245
1246static void xen_flush_tlb_single(unsigned long addr)
1247{
1248 struct mmuext_op *op;
1249 struct multicall_space mcs;
1250
1251 preempt_disable();
1252
1253 mcs = xen_mc_entry(sizeof(*op));
1254 op = mcs.args;
1255 op->cmd = MMUEXT_INVLPG_LOCAL;
1256 op->arg1.linear_addr = addr & PAGE_MASK;
1257 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1258
1259 xen_mc_issue(PARAVIRT_LAZY_MMU);
1260
1261 preempt_enable();
1262}
1263
1264static void xen_flush_tlb_others(const struct cpumask *cpus,
1265 struct mm_struct *mm, unsigned long va)
1266{
1267 struct {
1268 struct mmuext_op op;
1269 DECLARE_BITMAP(mask, NR_CPUS);
1270 } *args;
1271 struct multicall_space mcs;
1272
1273 BUG_ON(cpumask_empty(cpus));
1274 BUG_ON(!mm);
1275
1276 mcs = xen_mc_entry(sizeof(*args));
1277 args = mcs.args;
1278 args->op.arg2.vcpumask = to_cpumask(args->mask);
1279
1280 /* Remove us, and any offline CPUS. */
1281 cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask);
1282 cpumask_clear_cpu(smp_processor_id(), to_cpumask(args->mask));
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001283
1284 if (va == TLB_FLUSH_ALL) {
1285 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
1286 } else {
1287 args->op.cmd = MMUEXT_INVLPG_MULTI;
1288 args->op.arg1.linear_addr = va;
1289 }
1290
1291 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
1292
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001293 xen_mc_issue(PARAVIRT_LAZY_MMU);
1294}
1295
1296static unsigned long xen_read_cr3(void)
1297{
1298 return percpu_read(xen_cr3);
1299}
1300
1301static void set_current_cr3(void *v)
1302{
1303 percpu_write(xen_current_cr3, (unsigned long)v);
1304}
1305
1306static void __xen_write_cr3(bool kernel, unsigned long cr3)
1307{
1308 struct mmuext_op *op;
1309 struct multicall_space mcs;
1310 unsigned long mfn;
1311
1312 if (cr3)
1313 mfn = pfn_to_mfn(PFN_DOWN(cr3));
1314 else
1315 mfn = 0;
1316
1317 WARN_ON(mfn == 0 && kernel);
1318
1319 mcs = __xen_mc_entry(sizeof(*op));
1320
1321 op = mcs.args;
1322 op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
1323 op->arg1.mfn = mfn;
1324
1325 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1326
1327 if (kernel) {
1328 percpu_write(xen_cr3, cr3);
1329
1330 /* Update xen_current_cr3 once the batch has actually
1331 been submitted. */
1332 xen_mc_callback(set_current_cr3, (void *)cr3);
1333 }
1334}
1335
1336static void xen_write_cr3(unsigned long cr3)
1337{
1338 BUG_ON(preemptible());
1339
1340 xen_mc_batch(); /* disables interrupts */
1341
1342 /* Update while interrupts are disabled, so its atomic with
1343 respect to ipis */
1344 percpu_write(xen_cr3, cr3);
1345
1346 __xen_write_cr3(true, cr3);
1347
1348#ifdef CONFIG_X86_64
1349 {
1350 pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
1351 if (user_pgd)
1352 __xen_write_cr3(false, __pa(user_pgd));
1353 else
1354 __xen_write_cr3(false, 0);
1355 }
1356#endif
1357
1358 xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
1359}
1360
1361static int xen_pgd_alloc(struct mm_struct *mm)
1362{
1363 pgd_t *pgd = mm->pgd;
1364 int ret = 0;
1365
1366 BUG_ON(PagePinned(virt_to_page(pgd)));
1367
1368#ifdef CONFIG_X86_64
1369 {
1370 struct page *page = virt_to_page(pgd);
1371 pgd_t *user_pgd;
1372
1373 BUG_ON(page->private != 0);
1374
1375 ret = -ENOMEM;
1376
1377 user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1378 page->private = (unsigned long)user_pgd;
1379
1380 if (user_pgd != NULL) {
1381 user_pgd[pgd_index(VSYSCALL_START)] =
1382 __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
1383 ret = 0;
1384 }
1385
1386 BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
1387 }
1388#endif
1389
1390 return ret;
1391}
1392
1393static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
1394{
1395#ifdef CONFIG_X86_64
1396 pgd_t *user_pgd = xen_get_user_pgd(pgd);
1397
1398 if (user_pgd)
1399 free_page((unsigned long)user_pgd);
1400#endif
1401}
1402
Jeremy Fitzhardinge1f4f9312009-02-02 13:58:06 -08001403#ifdef CONFIG_HIGHPTE
1404static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
1405{
1406 pgprot_t prot = PAGE_KERNEL;
1407
1408 if (PagePinned(page))
1409 prot = PAGE_KERNEL_RO;
1410
1411 if (0 && PageHighMem(page))
1412 printk("mapping highpte %lx type %d prot %s\n",
1413 page_to_pfn(page), type,
1414 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
1415
1416 return kmap_atomic_prot(page, type, prot);
1417}
1418#endif
1419
1420#ifdef CONFIG_X86_32
1421static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
1422{
1423 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
1424 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
1425 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
1426 pte_val_ma(pte));
1427
1428 return pte;
1429}
1430
1431/* Init-time set_pte while constructing initial pagetables, which
1432 doesn't allow RO pagetable pages to be remapped RW */
1433static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
1434{
1435 pte = mask_rw_pte(ptep, pte);
1436
1437 xen_set_pte(ptep, pte);
1438}
1439#endif
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001440
1441/* Early in boot, while setting up the initial pagetable, assume
1442 everything is pinned. */
1443static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
1444{
1445#ifdef CONFIG_FLATMEM
1446 BUG_ON(mem_map); /* should only be used early */
1447#endif
1448 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
1449}
1450
1451/* Early release_pte assumes that all pts are pinned, since there's
1452 only init_mm and anything attached to that is pinned. */
1453static void xen_release_pte_init(unsigned long pfn)
1454{
1455 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1456}
1457
1458static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
1459{
1460 struct mmuext_op op;
1461 op.cmd = cmd;
1462 op.arg1.mfn = pfn_to_mfn(pfn);
1463 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
1464 BUG();
1465}
1466
1467/* This needs to make sure the new pte page is pinned iff its being
1468 attached to a pinned pagetable. */
1469static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level)
1470{
1471 struct page *page = pfn_to_page(pfn);
1472
1473 if (PagePinned(virt_to_page(mm->pgd))) {
1474 SetPagePinned(page);
1475
1476 vm_unmap_aliases();
1477 if (!PageHighMem(page)) {
1478 make_lowmem_page_readonly(__va(PFN_PHYS((unsigned long)pfn)));
1479 if (level == PT_PTE && USE_SPLIT_PTLOCKS)
1480 pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
1481 } else {
1482 /* make sure there are no stray mappings of
1483 this page */
1484 kmap_flush_unused();
1485 }
1486 }
1487}
1488
1489static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
1490{
1491 xen_alloc_ptpage(mm, pfn, PT_PTE);
1492}
1493
1494static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
1495{
1496 xen_alloc_ptpage(mm, pfn, PT_PMD);
1497}
1498
1499/* This should never happen until we're OK to use struct page */
1500static void xen_release_ptpage(unsigned long pfn, unsigned level)
1501{
1502 struct page *page = pfn_to_page(pfn);
1503
1504 if (PagePinned(page)) {
1505 if (!PageHighMem(page)) {
1506 if (level == PT_PTE && USE_SPLIT_PTLOCKS)
1507 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
1508 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1509 }
1510 ClearPagePinned(page);
1511 }
1512}
1513
1514static void xen_release_pte(unsigned long pfn)
1515{
1516 xen_release_ptpage(pfn, PT_PTE);
1517}
1518
1519static void xen_release_pmd(unsigned long pfn)
1520{
1521 xen_release_ptpage(pfn, PT_PMD);
1522}
1523
1524#if PAGETABLE_LEVELS == 4
1525static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
1526{
1527 xen_alloc_ptpage(mm, pfn, PT_PUD);
1528}
1529
1530static void xen_release_pud(unsigned long pfn)
1531{
1532 xen_release_ptpage(pfn, PT_PUD);
1533}
1534#endif
1535
1536void __init xen_reserve_top(void)
1537{
1538#ifdef CONFIG_X86_32
1539 unsigned long top = HYPERVISOR_VIRT_START;
1540 struct xen_platform_parameters pp;
1541
1542 if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
1543 top = pp.virt_start;
1544
1545 reserve_top_address(-top);
1546#endif /* CONFIG_X86_32 */
1547}
1548
1549/*
1550 * Like __va(), but returns address in the kernel mapping (which is
1551 * all we have until the physical memory mapping has been set up.
1552 */
1553static void *__ka(phys_addr_t paddr)
1554{
1555#ifdef CONFIG_X86_64
1556 return (void *)(paddr + __START_KERNEL_map);
1557#else
1558 return __va(paddr);
1559#endif
1560}
1561
1562/* Convert a machine address to physical address */
1563static unsigned long m2p(phys_addr_t maddr)
1564{
1565 phys_addr_t paddr;
1566
1567 maddr &= PTE_PFN_MASK;
1568 paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
1569
1570 return paddr;
1571}
1572
1573/* Convert a machine address to kernel virtual */
1574static void *m2v(phys_addr_t maddr)
1575{
1576 return __ka(m2p(maddr));
1577}
1578
1579static void set_page_prot(void *addr, pgprot_t prot)
1580{
1581 unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
1582 pte_t pte = pfn_pte(pfn, prot);
1583
1584 if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
1585 BUG();
1586}
1587
1588static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
1589{
1590 unsigned pmdidx, pteidx;
1591 unsigned ident_pte;
1592 unsigned long pfn;
1593
1594 ident_pte = 0;
1595 pfn = 0;
1596 for (pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
1597 pte_t *pte_page;
1598
1599 /* Reuse or allocate a page of ptes */
1600 if (pmd_present(pmd[pmdidx]))
1601 pte_page = m2v(pmd[pmdidx].pmd);
1602 else {
1603 /* Check for free pte pages */
1604 if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
1605 break;
1606
1607 pte_page = &level1_ident_pgt[ident_pte];
1608 ident_pte += PTRS_PER_PTE;
1609
1610 pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
1611 }
1612
1613 /* Install mappings */
1614 for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
1615 pte_t pte;
1616
1617 if (pfn > max_pfn_mapped)
1618 max_pfn_mapped = pfn;
1619
1620 if (!pte_none(pte_page[pteidx]))
1621 continue;
1622
1623 pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
1624 pte_page[pteidx] = pte;
1625 }
1626 }
1627
1628 for (pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
1629 set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
1630
1631 set_page_prot(pmd, PAGE_KERNEL_RO);
1632}
1633
1634#ifdef CONFIG_X86_64
1635static void convert_pfn_mfn(void *v)
1636{
1637 pte_t *pte = v;
1638 int i;
1639
1640 /* All levels are converted the same way, so just treat them
1641 as ptes. */
1642 for (i = 0; i < PTRS_PER_PTE; i++)
1643 pte[i] = xen_make_pte(pte[i].pte);
1644}
1645
1646/*
1647 * Set up the inital kernel pagetable.
1648 *
1649 * We can construct this by grafting the Xen provided pagetable into
1650 * head_64.S's preconstructed pagetables. We copy the Xen L2's into
1651 * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
1652 * means that only the kernel has a physical mapping to start with -
1653 * but that's enough to get __va working. We need to fill in the rest
1654 * of the physical mapping once some sort of allocator has been set
1655 * up.
1656 */
1657__init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
1658 unsigned long max_pfn)
1659{
1660 pud_t *l3;
1661 pmd_t *l2;
1662
1663 /* Zap identity mapping */
1664 init_level4_pgt[0] = __pgd(0);
1665
1666 /* Pre-constructed entries are in pfn, so convert to mfn */
1667 convert_pfn_mfn(init_level4_pgt);
1668 convert_pfn_mfn(level3_ident_pgt);
1669 convert_pfn_mfn(level3_kernel_pgt);
1670
1671 l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
1672 l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
1673
1674 memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1675 memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1676
1677 l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
1678 l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
1679 memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
1680
1681 /* Set up identity map */
1682 xen_map_identity_early(level2_ident_pgt, max_pfn);
1683
1684 /* Make pagetable pieces RO */
1685 set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
1686 set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
1687 set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
1688 set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
1689 set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
1690 set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
1691
1692 /* Pin down new L4 */
1693 pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
1694 PFN_DOWN(__pa_symbol(init_level4_pgt)));
1695
1696 /* Unpin Xen-provided one */
1697 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1698
1699 /* Switch over */
1700 pgd = init_level4_pgt;
1701
1702 /*
1703 * At this stage there can be no user pgd, and no page
1704 * structure to attach it to, so make sure we just set kernel
1705 * pgd.
1706 */
1707 xen_mc_batch();
1708 __xen_write_cr3(true, __pa(pgd));
1709 xen_mc_issue(PARAVIRT_LAZY_CPU);
1710
1711 reserve_early(__pa(xen_start_info->pt_base),
1712 __pa(xen_start_info->pt_base +
1713 xen_start_info->nr_pt_frames * PAGE_SIZE),
1714 "XEN PAGETABLES");
1715
1716 return pgd;
1717}
1718#else /* !CONFIG_X86_64 */
1719static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
1720
1721__init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
1722 unsigned long max_pfn)
1723{
1724 pmd_t *kernel_pmd;
1725
1726 init_pg_tables_start = __pa(pgd);
1727 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
1728 max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
1729
1730 kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
1731 memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
1732
1733 xen_map_identity_early(level2_kernel_pgt, max_pfn);
1734
1735 memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
1736 set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
1737 __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
1738
1739 set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
1740 set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
1741 set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
1742
1743 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1744
1745 xen_write_cr3(__pa(swapper_pg_dir));
1746
1747 pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
1748
1749 return swapper_pg_dir;
1750}
1751#endif /* CONFIG_X86_64 */
1752
1753static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
1754{
1755 pte_t pte;
1756
1757 phys >>= PAGE_SHIFT;
1758
1759 switch (idx) {
1760 case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
1761#ifdef CONFIG_X86_F00F_BUG
1762 case FIX_F00F_IDT:
1763#endif
1764#ifdef CONFIG_X86_32
1765 case FIX_WP_TEST:
1766 case FIX_VDSO:
1767# ifdef CONFIG_HIGHMEM
1768 case FIX_KMAP_BEGIN ... FIX_KMAP_END:
1769# endif
1770#else
1771 case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
1772#endif
1773#ifdef CONFIG_X86_LOCAL_APIC
1774 case FIX_APIC_BASE: /* maps dummy local APIC */
1775#endif
1776 pte = pfn_pte(phys, prot);
1777 break;
1778
1779 default:
1780 pte = mfn_pte(phys, prot);
1781 break;
1782 }
1783
1784 __native_set_fixmap(idx, pte);
1785
1786#ifdef CONFIG_X86_64
1787 /* Replicate changes to map the vsyscall page into the user
1788 pagetable vsyscall mapping. */
1789 if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
1790 unsigned long vaddr = __fix_to_virt(idx);
1791 set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
1792 }
1793#endif
1794}
1795
1796__init void xen_post_allocator_init(void)
1797{
1798 pv_mmu_ops.set_pte = xen_set_pte;
1799 pv_mmu_ops.set_pmd = xen_set_pmd;
1800 pv_mmu_ops.set_pud = xen_set_pud;
1801#if PAGETABLE_LEVELS == 4
1802 pv_mmu_ops.set_pgd = xen_set_pgd;
1803#endif
1804
1805 /* This will work as long as patching hasn't happened yet
1806 (which it hasn't) */
1807 pv_mmu_ops.alloc_pte = xen_alloc_pte;
1808 pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
1809 pv_mmu_ops.release_pte = xen_release_pte;
1810 pv_mmu_ops.release_pmd = xen_release_pmd;
1811#if PAGETABLE_LEVELS == 4
1812 pv_mmu_ops.alloc_pud = xen_alloc_pud;
1813 pv_mmu_ops.release_pud = xen_release_pud;
1814#endif
1815
1816#ifdef CONFIG_X86_64
1817 SetPagePinned(virt_to_page(level3_user_vsyscall));
1818#endif
1819 xen_mark_init_mm_pinned();
1820}
1821
1822
1823const struct pv_mmu_ops xen_mmu_ops __initdata = {
1824 .pagetable_setup_start = xen_pagetable_setup_start,
1825 .pagetable_setup_done = xen_pagetable_setup_done,
1826
1827 .read_cr2 = xen_read_cr2,
1828 .write_cr2 = xen_write_cr2,
1829
1830 .read_cr3 = xen_read_cr3,
1831 .write_cr3 = xen_write_cr3,
1832
1833 .flush_tlb_user = xen_flush_tlb,
1834 .flush_tlb_kernel = xen_flush_tlb,
1835 .flush_tlb_single = xen_flush_tlb_single,
1836 .flush_tlb_others = xen_flush_tlb_others,
1837
1838 .pte_update = paravirt_nop,
1839 .pte_update_defer = paravirt_nop,
1840
1841 .pgd_alloc = xen_pgd_alloc,
1842 .pgd_free = xen_pgd_free,
1843
1844 .alloc_pte = xen_alloc_pte_init,
1845 .release_pte = xen_release_pte_init,
1846 .alloc_pmd = xen_alloc_pte_init,
1847 .alloc_pmd_clone = paravirt_nop,
1848 .release_pmd = xen_release_pte_init,
1849
1850#ifdef CONFIG_HIGHPTE
1851 .kmap_atomic_pte = xen_kmap_atomic_pte,
1852#endif
1853
1854#ifdef CONFIG_X86_64
1855 .set_pte = xen_set_pte,
1856#else
1857 .set_pte = xen_set_pte_init,
1858#endif
1859 .set_pte_at = xen_set_pte_at,
1860 .set_pmd = xen_set_pmd_hyper,
1861
1862 .ptep_modify_prot_start = __ptep_modify_prot_start,
1863 .ptep_modify_prot_commit = __ptep_modify_prot_commit,
1864
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -08001865 .pte_val = PV_CALLEE_SAVE(xen_pte_val),
1866 .pgd_val = PV_CALLEE_SAVE(xen_pgd_val),
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001867
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -08001868 .make_pte = PV_CALLEE_SAVE(xen_make_pte),
1869 .make_pgd = PV_CALLEE_SAVE(xen_make_pgd),
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001870
1871#ifdef CONFIG_X86_PAE
1872 .set_pte_atomic = xen_set_pte_atomic,
1873 .set_pte_present = xen_set_pte_at,
1874 .pte_clear = xen_pte_clear,
1875 .pmd_clear = xen_pmd_clear,
1876#endif /* CONFIG_X86_PAE */
1877 .set_pud = xen_set_pud_hyper,
1878
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -08001879 .make_pmd = PV_CALLEE_SAVE(xen_make_pmd),
1880 .pmd_val = PV_CALLEE_SAVE(xen_pmd_val),
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001881
1882#if PAGETABLE_LEVELS == 4
Jeremy Fitzhardingeda5de7c2009-01-28 14:35:07 -08001883 .pud_val = PV_CALLEE_SAVE(xen_pud_val),
1884 .make_pud = PV_CALLEE_SAVE(xen_make_pud),
Jeremy Fitzhardinge319f3ba2009-01-28 14:35:01 -08001885 .set_pgd = xen_set_pgd_hyper,
1886
1887 .alloc_pud = xen_alloc_pte_init,
1888 .release_pud = xen_release_pte_init,
1889#endif /* PAGETABLE_LEVELS == 4 */
1890
1891 .activate_mm = xen_activate_mm,
1892 .dup_mmap = xen_dup_mmap,
1893 .exit_mmap = xen_exit_mmap,
1894
1895 .lazy_mode = {
1896 .enter = paravirt_enter_lazy_mmu,
1897 .leave = xen_leave_lazy,
1898 },
1899
1900 .set_fixmap = xen_set_fixmap,
1901};
1902
1903
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -07001904#ifdef CONFIG_XEN_DEBUG_FS
1905
1906static struct dentry *d_mmu_debug;
1907
1908static int __init xen_mmu_debugfs(void)
1909{
1910 struct dentry *d_xen = xen_init_debugfs();
1911
1912 if (d_xen == NULL)
1913 return -ENOMEM;
1914
1915 d_mmu_debug = debugfs_create_dir("mmu", d_xen);
1916
1917 debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats);
1918
1919 debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update);
1920 debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug,
1921 &mmu_stats.pgd_update_pinned);
1922 debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug,
1923 &mmu_stats.pgd_update_pinned);
1924
1925 debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update);
1926 debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug,
1927 &mmu_stats.pud_update_pinned);
1928 debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug,
1929 &mmu_stats.pud_update_pinned);
1930
1931 debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update);
1932 debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug,
1933 &mmu_stats.pmd_update_pinned);
1934 debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug,
1935 &mmu_stats.pmd_update_pinned);
1936
1937 debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update);
1938// debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug,
1939// &mmu_stats.pte_update_pinned);
1940 debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug,
1941 &mmu_stats.pte_update_pinned);
1942
1943 debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update);
1944 debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug,
1945 &mmu_stats.mmu_update_extended);
1946 xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug,
1947 mmu_stats.mmu_update_histo, 20);
1948
1949 debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at);
1950 debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug,
1951 &mmu_stats.set_pte_at_batched);
1952 debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug,
1953 &mmu_stats.set_pte_at_current);
1954 debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug,
1955 &mmu_stats.set_pte_at_kernel);
1956
1957 debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit);
1958 debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
1959 &mmu_stats.prot_commit_batched);
1960
1961 return 0;
1962}
1963fs_initcall(xen_mmu_debugfs);
1964
1965#endif /* CONFIG_XEN_DEBUG_FS */