blob: 17f374eb1faa4abbe077c24beb98de92651fe818 [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 Fitzhardinge3b827c12007-07-17 18:37:04 -070043#include <linux/bug.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070044
45#include <asm/pgtable.h>
46#include <asm/tlbflush.h>
47#include <asm/mmu_context.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070048#include <asm/paravirt.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070049
50#include <asm/xen/hypercall.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070051#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070052
53#include <xen/page.h>
54#include <xen/interface/xen.h>
55
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070056#include "multicalls.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070057#include "mmu.h"
58
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010059#define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +010060#define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010061
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +010062/* Placeholder for holes in the address space */
63static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE]
64 __attribute__((section(".data.page_aligned"))) =
65 { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL };
66
67 /* Array of pointers to pages containing p2m entries */
68static unsigned long *p2m_top[TOP_ENTRIES]
69 __attribute__((section(".data.page_aligned"))) =
70 { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] };
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010071
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +010072/* Arrays of p2m arrays expressed in mfns used for save/restore */
73static unsigned long p2m_top_mfn[TOP_ENTRIES]
74 __attribute__((section(".bss.page_aligned")));
75
Ingo Molnarb20aecc2008-05-28 14:24:38 +020076static unsigned long p2m_top_mfn_list[
77 PAGE_ALIGN(TOP_ENTRIES / P2M_ENTRIES_PER_PAGE)]
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +010078 __attribute__((section(".bss.page_aligned")));
79
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010080static inline unsigned p2m_top_index(unsigned long pfn)
81{
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010082 BUG_ON(pfn >= MAX_DOMAIN_PAGES);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010083 return pfn / P2M_ENTRIES_PER_PAGE;
84}
85
86static inline unsigned p2m_index(unsigned long pfn)
87{
88 return pfn % P2M_ENTRIES_PER_PAGE;
89}
90
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +010091/* Build the parallel p2m_top_mfn structures */
92void xen_setup_mfn_list_list(void)
93{
94 unsigned pfn, idx;
95
96 for(pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) {
97 unsigned topidx = p2m_top_index(pfn);
98
99 p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]);
100 }
101
102 for(idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) {
103 unsigned topidx = idx * P2M_ENTRIES_PER_PAGE;
104 p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]);
105 }
106
107 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
108
109 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
110 virt_to_mfn(p2m_top_mfn_list);
111 HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages;
112}
113
114/* Set up p2m_top to point to the domain-builder provided p2m pages */
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100115void __init xen_build_dynamic_phys_to_machine(void)
116{
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100117 unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100118 unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100119 unsigned pfn;
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100120
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100121 for(pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) {
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100122 unsigned topidx = p2m_top_index(pfn);
123
124 p2m_top[topidx] = &mfn_list[pfn];
125 }
126}
127
128unsigned long get_phys_to_machine(unsigned long pfn)
129{
130 unsigned topidx, idx;
131
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100132 if (unlikely(pfn >= MAX_DOMAIN_PAGES))
133 return INVALID_P2M_ENTRY;
134
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100135 topidx = p2m_top_index(pfn);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100136 idx = p2m_index(pfn);
137 return p2m_top[topidx][idx];
138}
Ingo Molnar15ce60052008-06-02 13:20:11 +0200139EXPORT_SYMBOL_GPL(get_phys_to_machine);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100140
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100141static void alloc_p2m(unsigned long **pp, unsigned long *mfnp)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100142{
143 unsigned long *p;
144 unsigned i;
145
146 p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL);
147 BUG_ON(p == NULL);
148
149 for(i = 0; i < P2M_ENTRIES_PER_PAGE; i++)
150 p[i] = INVALID_P2M_ENTRY;
151
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100152 if (cmpxchg(pp, p2m_missing, p) != p2m_missing)
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100153 free_page((unsigned long)p);
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100154 else
155 *mfnp = virt_to_mfn(p);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100156}
157
158void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
159{
160 unsigned topidx, idx;
161
162 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
163 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
164 return;
165 }
166
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100167 if (unlikely(pfn >= MAX_DOMAIN_PAGES)) {
168 BUG_ON(mfn != INVALID_P2M_ENTRY);
169 return;
170 }
171
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100172 topidx = p2m_top_index(pfn);
Jeremy Fitzhardingecf0923e2008-05-26 23:31:20 +0100173 if (p2m_top[topidx] == p2m_missing) {
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100174 /* no need to allocate a page to store an invalid entry */
175 if (mfn == INVALID_P2M_ENTRY)
176 return;
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100177 alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]);
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +0100178 }
179
180 idx = p2m_index(pfn);
181 p2m_top[topidx][idx] = mfn;
182}
183
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700184xmaddr_t arbitrary_virt_to_machine(unsigned long address)
185{
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100186 unsigned int level;
Ingo Molnarf0646e42008-01-30 13:33:43 +0100187 pte_t *pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700188 unsigned offset = address & PAGE_MASK;
189
190 BUG_ON(pte == NULL);
191
192 return XMADDR((pte_mfn(*pte) << PAGE_SHIFT) + offset);
193}
194
195void make_lowmem_page_readonly(void *vaddr)
196{
197 pte_t *pte, ptev;
198 unsigned long address = (unsigned long)vaddr;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100199 unsigned int level;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700200
Ingo Molnarf0646e42008-01-30 13:33:43 +0100201 pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700202 BUG_ON(pte == NULL);
203
204 ptev = pte_wrprotect(*pte);
205
206 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
207 BUG();
208}
209
210void make_lowmem_page_readwrite(void *vaddr)
211{
212 pte_t *pte, ptev;
213 unsigned long address = (unsigned long)vaddr;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100214 unsigned int level;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700215
Ingo Molnarf0646e42008-01-30 13:33:43 +0100216 pte = lookup_address(address, &level);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700217 BUG_ON(pte == NULL);
218
219 ptev = pte_mkwrite(*pte);
220
221 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
222 BUG();
223}
224
225
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700226void xen_set_pmd(pmd_t *ptr, pmd_t val)
227{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700228 struct multicall_space mcs;
229 struct mmu_update *u;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700230
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700231 preempt_disable();
232
233 mcs = xen_mc_entry(sizeof(*u));
234 u = mcs.args;
235 u->ptr = virt_to_machine(ptr).maddr;
236 u->val = pmd_val_ma(val);
237 MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF);
238
239 xen_mc_issue(PARAVIRT_LAZY_MMU);
240
241 preempt_enable();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700242}
243
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700244/*
245 * Associate a virtual page frame with a given physical page frame
246 * and protection flags for that frame.
247 */
248void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
249{
250 pgd_t *pgd;
251 pud_t *pud;
252 pmd_t *pmd;
253 pte_t *pte;
254
255 pgd = swapper_pg_dir + pgd_index(vaddr);
256 if (pgd_none(*pgd)) {
257 BUG();
258 return;
259 }
260 pud = pud_offset(pgd, vaddr);
261 if (pud_none(*pud)) {
262 BUG();
263 return;
264 }
265 pmd = pmd_offset(pud, vaddr);
266 if (pmd_none(*pmd)) {
267 BUG();
268 return;
269 }
270 pte = pte_offset_kernel(pmd, vaddr);
271 /* <mfn,flags> stored as-is, to permit clearing entries */
272 xen_set_pte(pte, mfn_pte(mfn, flags));
273
274 /*
275 * It's enough to flush this one mapping.
276 * (PGE mappings get flushed as well)
277 */
278 __flush_tlb_one(vaddr);
279}
280
281void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
282 pte_t *ptep, pte_t pteval)
283{
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700284 /* updates to init_mm may be done without lock */
285 if (mm == &init_mm)
286 preempt_disable();
287
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700288 if (mm == current->mm || mm == &init_mm) {
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700289 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700290 struct multicall_space mcs;
291 mcs = xen_mc_entry(0);
292
293 MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
294 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700295 goto out;
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700296 } else
297 if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700298 goto out;
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700299 }
300 xen_set_pte(ptep, pteval);
Jeremy Fitzhardinge2bd50032008-04-02 10:54:10 -0700301
302out:
303 if (mm == &init_mm)
304 preempt_enable();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700305}
306
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700307pteval_t xen_pte_val(pte_t pte)
308{
309 pteval_t ret = pte.pte;
310
311 if (ret & _PAGE_PRESENT)
312 ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT;
313
314 return ret;
315}
316
317pgdval_t xen_pgd_val(pgd_t pgd)
318{
319 pgdval_t ret = pgd.pgd;
320 if (ret & _PAGE_PRESENT)
321 ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT;
322 return ret;
323}
324
325pte_t xen_make_pte(pteval_t pte)
326{
327 if (pte & _PAGE_PRESENT) {
328 pte = phys_to_machine(XPADDR(pte)).maddr;
329 pte &= ~(_PAGE_PCD | _PAGE_PWT);
330 }
331
332 return (pte_t){ .pte = pte };
333}
334
335pgd_t xen_make_pgd(pgdval_t pgd)
336{
337 if (pgd & _PAGE_PRESENT)
338 pgd = phys_to_machine(XPADDR(pgd)).maddr;
339
340 return (pgd_t){ pgd };
341}
342
343pmdval_t xen_pmd_val(pmd_t pmd)
344{
345 pmdval_t ret = native_pmd_val(pmd);
346 if (ret & _PAGE_PRESENT)
347 ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT;
348 return ret;
349}
Jeremy Fitzhardinge3843fc22008-05-09 12:05:57 +0100350
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700351void xen_set_pud(pud_t *ptr, pud_t val)
352{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700353 struct multicall_space mcs;
354 struct mmu_update *u;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700355
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700356 preempt_disable();
357
358 mcs = xen_mc_entry(sizeof(*u));
359 u = mcs.args;
360 u->ptr = virt_to_machine(ptr).maddr;
361 u->val = pud_val_ma(val);
362 MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF);
363
364 xen_mc_issue(PARAVIRT_LAZY_MMU);
365
366 preempt_enable();
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700367}
368
369void xen_set_pte(pte_t *ptep, pte_t pte)
370{
371 ptep->pte_high = pte.pte_high;
372 smp_wmb();
373 ptep->pte_low = pte.pte_low;
374}
375
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700376void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
377{
378 set_64bit((u64 *)ptep, pte_val_ma(pte));
379}
380
381void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
382{
383 ptep->pte_low = 0;
384 smp_wmb(); /* make sure low gets written first */
385 ptep->pte_high = 0;
386}
387
388void xen_pmd_clear(pmd_t *pmdp)
389{
390 xen_set_pmd(pmdp, __pmd(0));
391}
392
Jeremy Fitzhardingeabf33032008-03-17 16:37:07 -0700393pmd_t xen_make_pmd(pmdval_t pmd)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700394{
Jeremy Fitzhardinge430442e2008-03-17 16:37:08 -0700395 if (pmd & _PAGE_PRESENT)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700396 pmd = phys_to_machine(XPADDR(pmd)).maddr;
397
Jeremy Fitzhardinge947a69c2008-03-17 16:37:09 -0700398 return native_make_pmd(pmd);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700399}
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700400
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700401/*
402 (Yet another) pagetable walker. This one is intended for pinning a
403 pagetable. This means that it walks a pagetable and calls the
404 callback function on each page it finds making up the page table,
405 at every level. It walks the entire pagetable, but it only bothers
406 pinning pte pages which are below pte_limit. In the normal case
407 this will be TASK_SIZE, but at boot we need to pin up to
408 FIXADDR_TOP. But the important bit is that we don't pin beyond
409 there, because then we start getting into Xen's ptes.
410*/
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700411static int pgd_walk(pgd_t *pgd_base, int (*func)(struct page *, enum pt_level),
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700412 unsigned long limit)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700413{
414 pgd_t *pgd = pgd_base;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700415 int flush = 0;
416 unsigned long addr = 0;
417 unsigned long pgd_next;
418
419 BUG_ON(limit > FIXADDR_TOP);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700420
421 if (xen_feature(XENFEAT_auto_translated_physmap))
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700422 return 0;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700423
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700424 for (; addr != FIXADDR_TOP; pgd++, addr = pgd_next) {
425 pud_t *pud;
426 unsigned long pud_limit, pud_next;
427
428 pgd_next = pud_limit = pgd_addr_end(addr, FIXADDR_TOP);
429
430 if (!pgd_val(*pgd))
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700431 continue;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700432
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700433 pud = pud_offset(pgd, 0);
434
435 if (PTRS_PER_PUD > 1) /* not folded */
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700436 flush |= (*func)(virt_to_page(pud), PT_PUD);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700437
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700438 for (; addr != pud_limit; pud++, addr = pud_next) {
439 pmd_t *pmd;
440 unsigned long pmd_limit;
441
442 pud_next = pud_addr_end(addr, pud_limit);
443
444 if (pud_next < limit)
445 pmd_limit = pud_next;
446 else
447 pmd_limit = limit;
448
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700449 if (pud_none(*pud))
450 continue;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700451
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700452 pmd = pmd_offset(pud, 0);
453
454 if (PTRS_PER_PMD > 1) /* not folded */
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700455 flush |= (*func)(virt_to_page(pmd), PT_PMD);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700456
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700457 for (; addr != pmd_limit; pmd++) {
458 addr += (PAGE_SIZE * PTRS_PER_PTE);
459 if ((pmd_limit-1) < (addr-1)) {
460 addr = pmd_limit;
461 break;
462 }
463
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700464 if (pmd_none(*pmd))
465 continue;
466
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700467 flush |= (*func)(pmd_page(*pmd), PT_PTE);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700468 }
469 }
470 }
471
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700472 flush |= (*func)(virt_to_page(pgd_base), PT_PGD);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700473
474 return flush;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700475}
476
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700477static spinlock_t *lock_pte(struct page *page)
478{
479 spinlock_t *ptl = NULL;
480
481#if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
482 ptl = __pte_lockptr(page);
483 spin_lock(ptl);
484#endif
485
486 return ptl;
487}
488
489static void do_unlock(void *v)
490{
491 spinlock_t *ptl = v;
492 spin_unlock(ptl);
493}
494
495static void xen_do_pin(unsigned level, unsigned long pfn)
496{
497 struct mmuext_op *op;
498 struct multicall_space mcs;
499
500 mcs = __xen_mc_entry(sizeof(*op));
501 op = mcs.args;
502 op->cmd = level;
503 op->arg1.mfn = pfn_to_mfn(pfn);
504 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
505}
506
507static int pin_page(struct page *page, enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700508{
Christoph Lameterd60cd462008-04-28 02:12:51 -0700509 unsigned pgfl = TestSetPagePinned(page);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700510 int flush;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700511
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700512 if (pgfl)
513 flush = 0; /* already pinned */
514 else if (PageHighMem(page))
515 /* kmaps need flushing if we found an unpinned
516 highpage */
517 flush = 1;
518 else {
519 void *pt = lowmem_page_address(page);
520 unsigned long pfn = page_to_pfn(page);
521 struct multicall_space mcs = __xen_mc_entry(0);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700522 spinlock_t *ptl;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700523
524 flush = 0;
525
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700526 ptl = NULL;
527 if (level == PT_PTE)
528 ptl = lock_pte(page);
529
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700530 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
531 pfn_pte(pfn, PAGE_KERNEL_RO),
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700532 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
533
534 if (level == PT_PTE)
535 xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
536
537 if (ptl) {
538 /* Queue a deferred unlock for when this batch
539 is completed. */
540 xen_mc_callback(do_unlock, ptl);
541 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700542 }
543
544 return flush;
545}
546
547/* This is called just after a mm has been created, but it has not
548 been used yet. We need to make sure that its pagetable is all
549 read-only, and can be pinned. */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700550void xen_pgd_pin(pgd_t *pgd)
551{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700552 xen_mc_batch();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700553
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700554 if (pgd_walk(pgd, pin_page, TASK_SIZE)) {
555 /* re-enable interrupts for kmap_flush_unused */
556 xen_mc_issue(0);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700557 kmap_flush_unused();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700558 xen_mc_batch();
559 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700560
Jeremy Fitzhardinge3843fc22008-05-09 12:05:57 +0100561 xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700562 xen_mc_issue(0);
563}
564
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100565/*
566 * On save, we need to pin all pagetables to make sure they get their
567 * mfns turned into pfns. Search the list for any unpinned pgds and pin
568 * them (unpinned pgds are not currently in use, probably because the
569 * process is under construction or destruction).
570 */
571void xen_mm_pin_all(void)
572{
573 unsigned long flags;
574 struct page *page;
575
576 spin_lock_irqsave(&pgd_lock, flags);
577
578 list_for_each_entry(page, &pgd_list, lru) {
579 if (!PagePinned(page)) {
580 xen_pgd_pin((pgd_t *)page_address(page));
581 SetPageSavePinned(page);
582 }
583 }
584
585 spin_unlock_irqrestore(&pgd_lock, flags);
586}
587
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700588/* The init_mm pagetable is really pinned as soon as its created, but
589 that's before we have page structures to store the bits. So do all
590 the book-keeping now. */
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700591static __init int mark_pinned(struct page *page, enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700592{
593 SetPagePinned(page);
594 return 0;
595}
596
597void __init xen_mark_init_mm_pinned(void)
598{
599 pgd_walk(init_mm.pgd, mark_pinned, FIXADDR_TOP);
600}
601
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700602static int unpin_page(struct page *page, enum pt_level level)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700603{
Christoph Lameterd60cd462008-04-28 02:12:51 -0700604 unsigned pgfl = TestClearPagePinned(page);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700605
606 if (pgfl && !PageHighMem(page)) {
607 void *pt = lowmem_page_address(page);
608 unsigned long pfn = page_to_pfn(page);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700609 spinlock_t *ptl = NULL;
610 struct multicall_space mcs;
611
612 if (level == PT_PTE) {
613 ptl = lock_pte(page);
614
615 xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
616 }
617
618 mcs = __xen_mc_entry(0);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700619
620 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
621 pfn_pte(pfn, PAGE_KERNEL),
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700622 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
623
624 if (ptl) {
625 /* unlock when batch completed */
626 xen_mc_callback(do_unlock, ptl);
627 }
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700628 }
629
630 return 0; /* never need to flush on unpin */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700631}
632
633/* Release a pagetables pages back as normal RW */
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700634static void xen_pgd_unpin(pgd_t *pgd)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700635{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700636 xen_mc_batch();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700637
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700638 xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700639
640 pgd_walk(pgd, unpin_page, TASK_SIZE);
641
642 xen_mc_issue(0);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700643}
644
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100645/*
646 * On resume, undo any pinning done at save, so that the rest of the
647 * kernel doesn't see any unexpected pinned pagetables.
648 */
649void xen_mm_unpin_all(void)
650{
651 unsigned long flags;
652 struct page *page;
653
654 spin_lock_irqsave(&pgd_lock, flags);
655
656 list_for_each_entry(page, &pgd_list, lru) {
657 if (PageSavePinned(page)) {
658 BUG_ON(!PagePinned(page));
659 printk("unpinning pinned %p\n", page_address(page));
660 xen_pgd_unpin((pgd_t *)page_address(page));
661 ClearPageSavePinned(page);
662 }
663 }
664
665 spin_unlock_irqrestore(&pgd_lock, flags);
666}
667
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700668void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
669{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700670 spin_lock(&next->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700671 xen_pgd_pin(next->pgd);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700672 spin_unlock(&next->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700673}
674
675void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
676{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700677 spin_lock(&mm->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700678 xen_pgd_pin(mm->pgd);
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700679 spin_unlock(&mm->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700680}
681
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700682
683#ifdef CONFIG_SMP
684/* Another cpu may still have their %cr3 pointing at the pagetable, so
685 we need to repoint it somewhere else before we can unpin it. */
686static void drop_other_mm_ref(void *info)
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700687{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700688 struct mm_struct *mm = info;
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700689
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700690 if (__get_cpu_var(cpu_tlbstate).active_mm == mm)
691 leave_mm(smp_processor_id());
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -0700692
693 /* If this cpu still has a stale cr3 reference, then make sure
694 it has been flushed. */
695 if (x86_read_percpu(xen_current_cr3) == __pa(mm->pgd)) {
696 load_cr3(swapper_pg_dir);
697 arch_flush_lazy_cpu_mode();
698 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700699}
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700700
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700701static void drop_mm_ref(struct mm_struct *mm)
702{
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -0700703 cpumask_t mask;
704 unsigned cpu;
705
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700706 if (current->active_mm == mm) {
707 if (current->mm == mm)
708 load_cr3(swapper_pg_dir);
709 else
710 leave_mm(smp_processor_id());
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -0700711 arch_flush_lazy_cpu_mode();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700712 }
713
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -0700714 /* Get the "official" set of cpus referring to our pagetable. */
715 mask = mm->cpu_vm_mask;
716
717 /* It's possible that a vcpu may have a stale reference to our
718 cr3, because its in lazy mode, and it hasn't yet flushed
719 its set of pending hypercalls yet. In this case, we can
720 look at its actual current cr3 value, and force it to flush
721 if needed. */
722 for_each_online_cpu(cpu) {
723 if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
724 cpu_set(cpu, mask);
725 }
726
727 if (!cpus_empty(mask))
728 xen_smp_call_function_mask(mask, drop_other_mm_ref, mm, 1);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700729}
730#else
731static void drop_mm_ref(struct mm_struct *mm)
732{
733 if (current->active_mm == mm)
734 load_cr3(swapper_pg_dir);
735}
736#endif
737
738/*
739 * While a process runs, Xen pins its pagetables, which means that the
740 * hypervisor forces it to be read-only, and it controls all updates
741 * to it. This means that all pagetable updates have to go via the
742 * hypervisor, which is moderately expensive.
743 *
744 * Since we're pulling the pagetable down, we switch to use init_mm,
745 * unpin old process pagetable and mark it all read-write, which
746 * allows further operations on it to be simple memory accesses.
747 *
748 * The only subtle point is that another CPU may be still using the
749 * pagetable because of lazy tlb flushing. This means we need need to
750 * switch all CPUs off this pagetable before we can unpin it.
751 */
752void xen_exit_mmap(struct mm_struct *mm)
753{
754 get_cpu(); /* make sure we don't move around */
755 drop_mm_ref(mm);
756 put_cpu();
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700757
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700758 spin_lock(&mm->page_table_lock);
Jeremy Fitzhardingedf912ea2007-09-25 11:50:00 -0700759
760 /* pgd may not be pinned in the error exit path of execve */
761 if (PagePinned(virt_to_page(mm->pgd)))
762 xen_pgd_unpin(mm->pgd);
Jeremy Fitzhardinge74260712007-10-16 11:51:30 -0700763
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700764 spin_unlock(&mm->page_table_lock);
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700765}