debuggers.hg

view xen/arch/x86/mm.c @ 3770:d21fbb46b9d8

bitkeeper revision 1.1159.253.1 (4208f8a54Zaz-XgC11YTHeLxPHPoZg)

Merge scramble.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into scramble.cl.cam.ac.uk:/local/scratch/kaf24/xen-unstable.bk
author kaf24@scramble.cl.cam.ac.uk
date Tue Feb 08 17:36:37 2005 +0000 (2005-02-08)
parents f5f2757b3aa2 cb87fd290eb0
children 12104922e743
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*- */
2 /******************************************************************************
3 * arch/x86/mm.c
4 *
5 * Copyright (c) 2002-2005 K A Fraser
6 * Copyright (c) 2004 Christian Limpach
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /*
24 * A description of the x86 page table API:
25 *
26 * Domains trap to do_mmu_update with a list of update requests.
27 * This is a list of (ptr, val) pairs, where the requested operation
28 * is *ptr = val.
29 *
30 * Reference counting of pages:
31 * ----------------------------
32 * Each page has two refcounts: tot_count and type_count.
33 *
34 * TOT_COUNT is the obvious reference count. It counts all uses of a
35 * physical page frame by a domain, including uses as a page directory,
36 * a page table, or simple mappings via a PTE. This count prevents a
37 * domain from releasing a frame back to the free pool when it still holds
38 * a reference to it.
39 *
40 * TYPE_COUNT is more subtle. A frame can be put to one of three
41 * mutually-exclusive uses: it might be used as a page directory, or a
42 * page table, or it may be mapped writable by the domain [of course, a
43 * frame may not be used in any of these three ways!].
44 * So, type_count is a count of the number of times a frame is being
45 * referred to in its current incarnation. Therefore, a page can only
46 * change its type when its type count is zero.
47 *
48 * Pinning the page type:
49 * ----------------------
50 * The type of a page can be pinned/unpinned with the commands
51 * MMUEXT_[UN]PIN_L?_TABLE. Each page can be pinned exactly once (that is,
52 * pinning is not reference counted, so it can't be nested).
53 * This is useful to prevent a page's type count falling to zero, at which
54 * point safety checks would need to be carried out next time the count
55 * is increased again.
56 *
57 * A further note on writable page mappings:
58 * -----------------------------------------
59 * For simplicity, the count of writable mappings for a page may not
60 * correspond to reality. The 'writable count' is incremented for every
61 * PTE which maps the page with the _PAGE_RW flag set. However, for
62 * write access to be possible the page directory entry must also have
63 * its _PAGE_RW bit set. We do not check this as it complicates the
64 * reference counting considerably [consider the case of multiple
65 * directory entries referencing a single page table, some with the RW
66 * bit set, others not -- it starts getting a bit messy].
67 * In normal use, this simplification shouldn't be a problem.
68 * However, the logic can be added if required.
69 *
70 * One more note on read-only page mappings:
71 * -----------------------------------------
72 * We want domains to be able to map pages for read-only access. The
73 * main reason is that page tables and directories should be readable
74 * by a domain, but it would not be safe for them to be writable.
75 * However, domains have free access to rings 1 & 2 of the Intel
76 * privilege model. In terms of page protection, these are considered
77 * to be part of 'supervisor mode'. The WP bit in CR0 controls whether
78 * read-only restrictions are respected in supervisor mode -- if the
79 * bit is clear then any mapped page is writable.
80 *
81 * We get round this by always setting the WP bit and disallowing
82 * updates to it. This is very unlikely to cause a problem for guest
83 * OS's, which will generally use the WP bit to simplify copy-on-write
84 * implementation (in that case, OS wants a fault when it writes to
85 * an application-supplied buffer).
86 */
88 #include <xen/config.h>
89 #include <xen/init.h>
90 #include <xen/kernel.h>
91 #include <xen/lib.h>
92 #include <xen/mm.h>
93 #include <xen/sched.h>
94 #include <xen/errno.h>
95 #include <xen/perfc.h>
96 #include <xen/irq.h>
97 #include <xen/softirq.h>
98 #include <asm/shadow.h>
99 #include <asm/page.h>
100 #include <asm/flushtlb.h>
101 #include <asm/io.h>
102 #include <asm/uaccess.h>
103 #include <asm/domain_page.h>
104 #include <asm/ldt.h>
106 #ifdef VERBOSE
107 #define MEM_LOG(_f, _a...) \
108 printk("DOM%u: (file=memory.c, line=%d) " _f "\n", \
109 current->domain->id , __LINE__ , ## _a )
110 #else
111 #define MEM_LOG(_f, _a...) ((void)0)
112 #endif
114 static int alloc_l2_table(struct pfn_info *page);
115 static int alloc_l1_table(struct pfn_info *page);
116 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d);
117 static int get_page_and_type_from_pagenr(unsigned long page_nr,
118 u32 type,
119 struct domain *d);
121 static void free_l2_table(struct pfn_info *page);
122 static void free_l1_table(struct pfn_info *page);
124 static int mod_l2_entry(l2_pgentry_t *, l2_pgentry_t, unsigned long);
125 static int mod_l1_entry(l1_pgentry_t *, l1_pgentry_t);
127 /* Used to defer flushing of memory structures. */
128 static struct {
129 #define DOP_FLUSH_TLB (1<<0) /* Flush the TLB. */
130 #define DOP_RELOAD_LDT (1<<1) /* Reload the LDT shadow mapping. */
131 unsigned long deferred_ops;
132 /* If non-NULL, specifies a foreign subject domain for some operations. */
133 struct domain *foreign;
134 } __cacheline_aligned percpu_info[NR_CPUS];
136 /*
137 * Returns the current foreign domain; defaults to the currently-executing
138 * domain if a foreign override hasn't been specified.
139 */
140 #define FOREIGNDOM (percpu_info[smp_processor_id()].foreign ? : current->domain)
142 /* Private domain structs for DOMID_XEN and DOMID_IO. */
143 static struct domain *dom_xen, *dom_io;
145 /* Frame table and its size in pages. */
146 struct pfn_info *frame_table;
147 unsigned long frame_table_size;
148 unsigned long max_page;
150 void __init init_frametable(void)
151 {
152 unsigned long i, p;
154 frame_table = (struct pfn_info *)FRAMETABLE_VIRT_START;
155 frame_table_size = max_page * sizeof(struct pfn_info);
156 frame_table_size = (frame_table_size + PAGE_SIZE - 1) & PAGE_MASK;
158 for ( i = 0; i < frame_table_size; i += (4UL << 20) )
159 {
160 p = alloc_boot_pages(min(frame_table_size - i, 4UL << 20), 4UL << 20);
161 if ( p == 0 )
162 panic("Not enough memory for frame table\n");
163 map_pages(idle_pg_table, FRAMETABLE_VIRT_START + i, p,
164 4UL << 20, PAGE_HYPERVISOR);
165 }
167 memset(frame_table, 0, frame_table_size);
168 }
170 void arch_init_memory(void)
171 {
172 extern void subarch_init_memory(struct domain *);
174 memset(percpu_info, 0, sizeof(percpu_info));
176 /*
177 * Initialise our DOMID_XEN domain.
178 * Any Xen-heap pages that we will allow to be mapped will have
179 * their domain field set to dom_xen.
180 */
181 dom_xen = alloc_domain_struct();
182 atomic_set(&dom_xen->refcnt, 1);
183 dom_xen->id = DOMID_XEN;
185 /*
186 * Initialise our DOMID_IO domain.
187 * This domain owns no pages but is considered a special case when
188 * mapping I/O pages, as the mappings occur at the priv of the caller.
189 */
190 dom_io = alloc_domain_struct();
191 atomic_set(&dom_io->refcnt, 1);
192 dom_io->id = DOMID_IO;
194 subarch_init_memory(dom_xen);
195 }
197 void write_ptbase(struct exec_domain *ed)
198 {
199 struct domain *d = ed->domain;
200 unsigned long pa;
202 #ifdef CONFIG_VMX
203 if ( unlikely(shadow_mode(d)) )
204 pa = ((shadow_mode(d) == SHM_full_32) ?
205 pagetable_val(ed->arch.monitor_table) :
206 pagetable_val(ed->arch.shadow_table));
207 else
208 pa = pagetable_val(ed->arch.pagetable);
209 #else
210 if ( unlikely(shadow_mode(d)) )
211 pa = pagetable_val(ed->arch.shadow_table);
212 else
213 pa = pagetable_val(ed->arch.pagetable);
214 #endif
216 write_cr3(pa);
217 }
219 static void __invalidate_shadow_ldt(struct exec_domain *d)
220 {
221 int i;
222 unsigned long pfn;
223 struct pfn_info *page;
225 d->arch.shadow_ldt_mapcnt = 0;
227 for ( i = 16; i < 32; i++ )
228 {
229 pfn = l1_pgentry_to_pfn(d->arch.perdomain_ptes[i]);
230 if ( pfn == 0 ) continue;
231 d->arch.perdomain_ptes[i] = mk_l1_pgentry(0);
232 page = &frame_table[pfn];
233 ASSERT_PAGE_IS_TYPE(page, PGT_ldt_page);
234 ASSERT_PAGE_IS_DOMAIN(page, d->domain);
235 put_page_and_type(page);
236 }
238 /* Dispose of the (now possibly invalid) mappings from the TLB. */
239 percpu_info[d->processor].deferred_ops |= DOP_FLUSH_TLB | DOP_RELOAD_LDT;
240 }
243 static inline void invalidate_shadow_ldt(struct exec_domain *d)
244 {
245 if ( d->arch.shadow_ldt_mapcnt != 0 )
246 __invalidate_shadow_ldt(d);
247 }
250 static int alloc_segdesc_page(struct pfn_info *page)
251 {
252 struct desc_struct *descs;
253 int i;
255 descs = map_domain_mem((page-frame_table) << PAGE_SHIFT);
257 for ( i = 0; i < 512; i++ )
258 if ( unlikely(!check_descriptor(&descs[i])) )
259 goto fail;
261 unmap_domain_mem(descs);
262 return 1;
264 fail:
265 unmap_domain_mem(descs);
266 return 0;
267 }
270 /* Map shadow page at offset @off. */
271 int map_ldt_shadow_page(unsigned int off)
272 {
273 struct exec_domain *ed = current;
274 struct domain *d = ed->domain;
275 unsigned long l1e;
277 if ( unlikely(in_irq()) )
278 BUG();
280 __get_user(l1e, (unsigned long *)
281 &linear_pg_table[l1_linear_offset(ed->arch.ldt_base) + off]);
283 if ( unlikely(!(l1e & _PAGE_PRESENT)) ||
284 unlikely(!get_page_and_type(&frame_table[l1e >> PAGE_SHIFT],
285 d, PGT_ldt_page)) )
286 return 0;
288 ed->arch.perdomain_ptes[off + 16] = mk_l1_pgentry(l1e | _PAGE_RW);
289 ed->arch.shadow_ldt_mapcnt++;
291 return 1;
292 }
295 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d)
296 {
297 struct pfn_info *page = &frame_table[page_nr];
299 if ( unlikely(!pfn_is_ram(page_nr)) )
300 {
301 MEM_LOG("Pfn %p is not RAM", page_nr);
302 return 0;
303 }
305 if ( unlikely(!get_page(page, d)) )
306 {
307 MEM_LOG("Could not get page ref for pfn %p", page_nr);
308 return 0;
309 }
311 return 1;
312 }
315 static int get_page_and_type_from_pagenr(unsigned long page_nr,
316 u32 type,
317 struct domain *d)
318 {
319 struct pfn_info *page = &frame_table[page_nr];
321 if ( unlikely(!get_page_from_pagenr(page_nr, d)) )
322 return 0;
324 if ( unlikely(!get_page_type(page, type)) )
325 {
326 if ( (type & PGT_type_mask) != PGT_l1_page_table )
327 MEM_LOG("Bad page type for pfn %p (%08x)",
328 page_nr, page->u.inuse.type_info);
329 put_page(page);
330 return 0;
331 }
333 return 1;
334 }
337 /*
338 * We allow an L2 tables to map each other (a.k.a. linear page tables). It
339 * needs some special care with reference counst and access permissions:
340 * 1. The mapping entry must be read-only, or the guest may get write access
341 * to its own PTEs.
342 * 2. We must only bump the reference counts for an *already validated*
343 * L2 table, or we can end up in a deadlock in get_page_type() by waiting
344 * on a validation that is required to complete that validation.
345 * 3. We only need to increment the reference counts for the mapped page
346 * frame if it is mapped by a different L2 table. This is sufficient and
347 * also necessary to allow validation of an L2 table mapping itself.
348 */
349 static int
350 get_linear_pagetable(
351 l2_pgentry_t l2e, unsigned long pfn, struct domain *d)
352 {
353 u32 x, y;
354 struct pfn_info *page;
356 if ( (l2_pgentry_val(l2e) & _PAGE_RW) )
357 {
358 MEM_LOG("Attempt to create linear p.t. with write perms");
359 return 0;
360 }
362 if ( (l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn )
363 {
364 /* Make sure the mapped frame belongs to the correct domain. */
365 if ( unlikely(!get_page_from_pagenr(l2_pgentry_to_pfn(l2e), d)) )
366 return 0;
368 /*
369 * Make sure that the mapped frame is an already-validated L2 table.
370 * If so, atomically increment the count (checking for overflow).
371 */
372 page = &frame_table[l2_pgentry_to_pfn(l2e)];
373 y = page->u.inuse.type_info;
374 do {
375 x = y;
376 if ( unlikely((x & PGT_count_mask) == PGT_count_mask) ||
377 unlikely((x & (PGT_type_mask|PGT_validated)) !=
378 (PGT_l2_page_table|PGT_validated)) )
379 {
380 put_page(page);
381 return 0;
382 }
383 }
384 while ( (y = cmpxchg(&page->u.inuse.type_info, x, x + 1)) != x );
385 }
387 return 1;
388 }
391 static int
392 get_page_from_l1e(
393 l1_pgentry_t l1e, struct domain *d)
394 {
395 unsigned long l1v = l1_pgentry_val(l1e);
396 unsigned long pfn = l1_pgentry_to_pfn(l1e);
397 struct pfn_info *page = &frame_table[pfn];
398 extern int domain_iomem_in_pfn(struct domain *d, unsigned long pfn);
400 if ( !(l1v & _PAGE_PRESENT) )
401 return 1;
403 if ( unlikely(l1v & (_PAGE_GLOBAL|_PAGE_PAT)) )
404 {
405 MEM_LOG("Bad L1 type settings %04lx", l1v & (_PAGE_GLOBAL|_PAGE_PAT));
406 return 0;
407 }
409 if ( unlikely(!pfn_is_ram(pfn)) )
410 {
411 /* Revert to caller privileges if FD == DOMID_IO. */
412 if ( d == dom_io )
413 d = current->domain;
415 if ( IS_PRIV(d) )
416 return 1;
418 if ( IS_CAPABLE_PHYSDEV(d) )
419 return domain_iomem_in_pfn(d, pfn);
421 MEM_LOG("Non-privileged attempt to map I/O space %p", pfn);
422 return 0;
423 }
425 return ((l1v & _PAGE_RW) ?
426 get_page_and_type(page, d, PGT_writable_page) :
427 get_page(page, d));
428 }
431 /* NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'. */
432 static int
433 get_page_from_l2e(
434 l2_pgentry_t l2e, unsigned long pfn,
435 struct domain *d, unsigned long va_idx)
436 {
437 int rc;
439 if ( !(l2_pgentry_val(l2e) & _PAGE_PRESENT) )
440 return 1;
442 if ( unlikely((l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE))) )
443 {
444 MEM_LOG("Bad L2 page type settings %04lx",
445 l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE));
446 return 0;
447 }
449 rc = get_page_and_type_from_pagenr(
450 l2_pgentry_to_pfn(l2e),
451 PGT_l1_page_table | (va_idx<<PGT_va_shift), d);
453 if ( unlikely(!rc) )
454 return get_linear_pagetable(l2e, pfn, d);
456 return 1;
457 }
460 static void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
461 {
462 unsigned long l1v = l1_pgentry_val(l1e);
463 unsigned long pfn = l1_pgentry_to_pfn(l1e);
464 struct pfn_info *page = &frame_table[pfn];
465 struct domain *e;
467 if ( !(l1v & _PAGE_PRESENT) || !pfn_is_ram(pfn) )
468 return;
470 e = page_get_owner(page);
471 if ( unlikely(e != d) )
472 {
473 /*
474 * Unmap a foreign page that may have been mapped via a grant table.
475 * Note that this can fail for a privileged domain that can map foreign
476 * pages via MMUEXT_SET_FOREIGNDOM. Such domains can have some mappings
477 * counted via a grant entry and some counted directly in the page
478 * structure's reference count. Note that reference counts won't get
479 * dangerously confused as long as we always try to decrement the
480 * grant entry first. We may end up with a mismatch between which
481 * mappings and which unmappings are counted via the grant entry, but
482 * really it doesn't matter as privileged domains have carte blanche.
483 */
484 if ( likely(gnttab_check_unmap(e, d, pfn, !(l1v & _PAGE_RW))) )
485 return;
486 /* Assume this mapping was made via MMUEXT_SET_FOREIGNDOM... */
487 }
489 if ( l1v & _PAGE_RW )
490 {
491 put_page_and_type(page);
492 }
493 else
494 {
495 /* We expect this is rare so we blow the entire shadow LDT. */
496 if ( unlikely(((page->u.inuse.type_info & PGT_type_mask) ==
497 PGT_ldt_page)) &&
498 unlikely(((page->u.inuse.type_info & PGT_count_mask) != 0)) )
499 invalidate_shadow_ldt(e->exec_domain[0]);
500 put_page(page);
501 }
502 }
505 /*
506 * NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'.
507 * Note also that this automatically deals correctly with linear p.t.'s.
508 */
509 static void put_page_from_l2e(l2_pgentry_t l2e, unsigned long pfn)
510 {
511 if ( (l2_pgentry_val(l2e) & _PAGE_PRESENT) &&
512 ((l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn) )
513 put_page_and_type(&frame_table[l2_pgentry_to_pfn(l2e)]);
514 }
517 static int alloc_l2_table(struct pfn_info *page)
518 {
519 struct domain *d = page_get_owner(page);
520 unsigned long page_nr = page_to_pfn(page);
521 l2_pgentry_t *pl2e;
522 int i;
524 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
526 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
527 if ( unlikely(!get_page_from_l2e(pl2e[i], page_nr, d, i)) )
528 goto fail;
530 #if defined(__i386__)
531 /* Now we add our private high mappings. */
532 memcpy(&pl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
533 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
534 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
535 pl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
536 mk_l2_pgentry((page_nr << PAGE_SHIFT) | __PAGE_HYPERVISOR);
537 pl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
538 mk_l2_pgentry(__pa(page_get_owner(page)->arch.mm_perdomain_pt) |
539 __PAGE_HYPERVISOR);
540 #endif
542 unmap_domain_mem(pl2e);
543 return 1;
545 fail:
546 while ( i-- > 0 )
547 put_page_from_l2e(pl2e[i], page_nr);
549 unmap_domain_mem(pl2e);
550 return 0;
551 }
554 static int alloc_l1_table(struct pfn_info *page)
555 {
556 struct domain *d = page_get_owner(page);
557 unsigned long page_nr = page_to_pfn(page);
558 l1_pgentry_t *pl1e;
559 int i;
561 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
563 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
564 if ( unlikely(!get_page_from_l1e(pl1e[i], d)) )
565 goto fail;
567 unmap_domain_mem(pl1e);
568 return 1;
570 fail:
571 while ( i-- > 0 )
572 put_page_from_l1e(pl1e[i], d);
574 unmap_domain_mem(pl1e);
575 return 0;
576 }
579 static void free_l2_table(struct pfn_info *page)
580 {
581 unsigned long page_nr = page - frame_table;
582 l2_pgentry_t *pl2e;
583 int i;
585 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
587 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
588 put_page_from_l2e(pl2e[i], page_nr);
590 unmap_domain_mem(pl2e);
591 }
594 static void free_l1_table(struct pfn_info *page)
595 {
596 struct domain *d = page_get_owner(page);
597 unsigned long page_nr = page - frame_table;
598 l1_pgentry_t *pl1e;
599 int i;
601 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
603 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
604 put_page_from_l1e(pl1e[i], d);
606 unmap_domain_mem(pl1e);
607 }
610 static inline int update_l2e(l2_pgentry_t *pl2e,
611 l2_pgentry_t ol2e,
612 l2_pgentry_t nl2e)
613 {
614 unsigned long o = cmpxchg((unsigned long *)pl2e,
615 l2_pgentry_val(ol2e),
616 l2_pgentry_val(nl2e));
617 if ( o != l2_pgentry_val(ol2e) )
618 MEM_LOG("Failed to update %p -> %p: saw %p\n",
619 l2_pgentry_val(ol2e), l2_pgentry_val(nl2e), o);
620 return (o == l2_pgentry_val(ol2e));
621 }
624 /* Update the L2 entry at pl2e to new value nl2e. pl2e is within frame pfn. */
625 static int mod_l2_entry(l2_pgentry_t *pl2e,
626 l2_pgentry_t nl2e,
627 unsigned long pfn)
628 {
629 l2_pgentry_t ol2e;
630 unsigned long _ol2e;
632 if ( unlikely((((unsigned long)pl2e & (PAGE_SIZE-1)) >> 2) >=
633 DOMAIN_ENTRIES_PER_L2_PAGETABLE) )
634 {
635 MEM_LOG("Illegal L2 update attempt in Xen-private area %p", pl2e);
636 return 0;
637 }
639 if ( unlikely(__get_user(_ol2e, (unsigned long *)pl2e) != 0) )
640 return 0;
641 ol2e = mk_l2_pgentry(_ol2e);
643 if ( l2_pgentry_val(nl2e) & _PAGE_PRESENT )
644 {
645 /* Differ in mapping (bits 12-31) or presence (bit 0)? */
646 if ( ((l2_pgentry_val(ol2e) ^ l2_pgentry_val(nl2e)) & ~0xffe) == 0 )
647 return update_l2e(pl2e, ol2e, nl2e);
649 if ( unlikely(!get_page_from_l2e(nl2e, pfn, current->domain,
650 ((unsigned long)pl2e &
651 ~PAGE_MASK) >> 2)) )
652 return 0;
654 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
655 {
656 put_page_from_l2e(nl2e, pfn);
657 return 0;
658 }
660 put_page_from_l2e(ol2e, pfn);
661 return 1;
662 }
664 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
665 return 0;
667 put_page_from_l2e(ol2e, pfn);
668 return 1;
669 }
672 static inline int update_l1e(l1_pgentry_t *pl1e,
673 l1_pgentry_t ol1e,
674 l1_pgentry_t nl1e)
675 {
676 unsigned long o = l1_pgentry_val(ol1e);
677 unsigned long n = l1_pgentry_val(nl1e);
679 if ( unlikely(cmpxchg_user(pl1e, o, n) != 0) ||
680 unlikely(o != l1_pgentry_val(ol1e)) )
681 {
682 MEM_LOG("Failed to update %p -> %p: saw %p\n",
683 l1_pgentry_val(ol1e), l1_pgentry_val(nl1e), o);
684 return 0;
685 }
687 return 1;
688 }
691 /* Update the L1 entry at pl1e to new value nl1e. */
692 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e)
693 {
694 l1_pgentry_t ol1e;
695 unsigned long _ol1e;
696 struct domain *d = current->domain;
698 if ( unlikely(__get_user(_ol1e, (unsigned long *)pl1e) != 0) )
699 {
700 MEM_LOG("Bad get_user\n");
701 return 0;
702 }
704 ol1e = mk_l1_pgentry(_ol1e);
706 if ( l1_pgentry_val(nl1e) & _PAGE_PRESENT )
707 {
708 /* Same mapping (bits 12-31), r/w (bit 1), and presence (bit 0)? */
709 if ( ((l1_pgentry_val(ol1e) ^ l1_pgentry_val(nl1e)) & ~0xffc) == 0 )
710 return update_l1e(pl1e, ol1e, nl1e);
712 if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
713 return 0;
715 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
716 {
717 put_page_from_l1e(nl1e, d);
718 return 0;
719 }
721 put_page_from_l1e(ol1e, d);
722 return 1;
723 }
725 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
726 return 0;
728 put_page_from_l1e(ol1e, d);
729 return 1;
730 }
733 int alloc_page_type(struct pfn_info *page, unsigned int type)
734 {
735 switch ( type )
736 {
737 case PGT_l1_page_table:
738 return alloc_l1_table(page);
739 case PGT_l2_page_table:
740 return alloc_l2_table(page);
741 case PGT_gdt_page:
742 case PGT_ldt_page:
743 return alloc_segdesc_page(page);
744 default:
745 printk("Bad type in alloc_page_type %x t=%x c=%x\n",
746 type, page->u.inuse.type_info,
747 page->count_info);
748 BUG();
749 }
751 return 0;
752 }
755 void free_page_type(struct pfn_info *page, unsigned int type)
756 {
757 struct domain *d = page_get_owner(page);
759 switch ( type )
760 {
761 case PGT_l1_page_table:
762 free_l1_table(page);
763 break;
765 case PGT_l2_page_table:
766 free_l2_table(page);
767 break;
769 default:
770 BUG();
771 }
773 if ( unlikely(shadow_mode(d)) &&
774 (get_shadow_status(d, page_to_pfn(page)) & PSH_shadowed) )
775 {
776 unshadow_table(page_to_pfn(page), type);
777 put_shadow_status(d);
778 }
779 }
782 void put_page_type(struct pfn_info *page)
783 {
784 u32 nx, x, y = page->u.inuse.type_info;
786 again:
787 do {
788 x = y;
789 nx = x - 1;
791 ASSERT((x & PGT_count_mask) != 0);
793 /*
794 * The page should always be validated while a reference is held. The
795 * exception is during domain destruction, when we forcibly invalidate
796 * page-table pages if we detect a referential loop.
797 * See domain.c:relinquish_list().
798 */
799 ASSERT((x & PGT_validated) ||
800 test_bit(DF_DYING, &page_get_owner(page)->d_flags));
802 if ( unlikely((nx & PGT_count_mask) == 0) )
803 {
804 /* Record TLB information for flush later. Races are harmless. */
805 page->tlbflush_timestamp = tlbflush_current_time();
807 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
808 likely(nx & PGT_validated) )
809 {
810 /*
811 * Page-table pages must be unvalidated when count is zero. The
812 * 'free' is safe because the refcnt is non-zero and validated
813 * bit is clear => other ops will spin or fail.
814 */
815 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
816 x & ~PGT_validated)) != x) )
817 goto again;
818 /* We cleared the 'valid bit' so we do the clear up. */
819 free_page_type(page, x & PGT_type_mask);
820 /* Carry on, but with the 'valid bit' now clear. */
821 x &= ~PGT_validated;
822 nx &= ~PGT_validated;
823 }
824 }
825 else if ( unlikely((nx & (PGT_pinned | PGT_count_mask)) ==
826 (PGT_pinned | 1)) )
827 {
828 /* Page is now only pinned. Make the back pointer mutable again. */
829 nx |= PGT_va_mutable;
830 }
831 }
832 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
833 }
836 int get_page_type(struct pfn_info *page, u32 type)
837 {
838 u32 nx, x, y = page->u.inuse.type_info;
840 again:
841 do {
842 x = y;
843 nx = x + 1;
844 if ( unlikely((nx & PGT_count_mask) == 0) )
845 {
846 MEM_LOG("Type count overflow on pfn %p\n", page_to_pfn(page));
847 return 0;
848 }
849 else if ( unlikely((x & PGT_count_mask) == 0) )
850 {
851 if ( (x & (PGT_type_mask|PGT_va_mask)) != type )
852 {
853 /*
854 * On type change we check to flush stale TLB entries. This
855 * may be unnecessary (e.g., page was GDT/LDT) but those
856 * circumstances should be very rare.
857 */
858 struct domain *d = page_get_owner(page);
859 if ( unlikely(NEED_FLUSH(tlbflush_time[d->exec_domain[0]->processor],
860 page->tlbflush_timestamp)) )
861 {
862 perfc_incr(need_flush_tlb_flush);
863 flush_tlb_cpu(d->exec_domain[0]->processor);
864 }
866 /* We lose existing type, back pointer, and validity. */
867 nx &= ~(PGT_type_mask | PGT_va_mask | PGT_validated);
868 nx |= type;
870 /* No special validation needed for writable pages. */
871 /* Page tables and GDT/LDT need to be scanned for validity. */
872 if ( type == PGT_writable_page )
873 nx |= PGT_validated;
874 }
875 }
876 else if ( unlikely((x & (PGT_type_mask|PGT_va_mask)) != type) )
877 {
878 if ( unlikely((x & PGT_type_mask) != (type & PGT_type_mask) ) )
879 {
880 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
881 ((type & PGT_type_mask) != PGT_l1_page_table) )
882 MEM_LOG("Bad type (saw %08x != exp %08x) for pfn %p\n",
883 x & PGT_type_mask, type, page_to_pfn(page));
884 return 0;
885 }
886 else if ( (x & PGT_va_mask) == PGT_va_mutable )
887 {
888 /* The va backpointer is mutable, hence we update it. */
889 nx &= ~PGT_va_mask;
890 nx |= type; /* we know the actual type is correct */
891 }
892 else if ( unlikely((x & PGT_va_mask) != (type & PGT_va_mask)) )
893 {
894 /* This table is potentially mapped at multiple locations. */
895 nx &= ~PGT_va_mask;
896 nx |= PGT_va_unknown;
897 }
898 }
899 else if ( unlikely(!(x & PGT_validated)) )
900 {
901 /* Someone else is updating validation of this page. Wait... */
902 while ( (y = page->u.inuse.type_info) == x )
903 {
904 rep_nop();
905 barrier();
906 }
907 goto again;
908 }
909 }
910 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
912 if ( unlikely(!(nx & PGT_validated)) )
913 {
914 /* Try to validate page type; drop the new reference on failure. */
915 if ( unlikely(!alloc_page_type(page, type & PGT_type_mask)) )
916 {
917 MEM_LOG("Error while validating pfn %p for type %08x."
918 " caf=%08x taf=%08x\n",
919 page_to_pfn(page), type,
920 page->count_info,
921 page->u.inuse.type_info);
922 /* Noone else can get a reference. We hold the only ref. */
923 page->u.inuse.type_info = 0;
924 return 0;
925 }
927 /* Noone else is updating simultaneously. */
928 __set_bit(_PGT_validated, &page->u.inuse.type_info);
929 }
931 return 1;
932 }
935 int new_guest_cr3(unsigned long pfn)
936 {
937 struct exec_domain *ed = current;
938 struct domain *d = ed->domain;
939 int okay, cpu = smp_processor_id();
940 unsigned long old_base_pfn;
942 okay = get_page_and_type_from_pagenr(pfn, PGT_l2_page_table, d);
943 if ( likely(okay) )
944 {
945 invalidate_shadow_ldt(ed);
947 percpu_info[cpu].deferred_ops &= ~DOP_FLUSH_TLB;
948 old_base_pfn = pagetable_val(ed->arch.pagetable) >> PAGE_SHIFT;
949 ed->arch.pagetable = mk_pagetable(pfn << PAGE_SHIFT);
951 shadow_mk_pagetable(ed);
953 write_ptbase(ed);
955 put_page_and_type(&frame_table[old_base_pfn]);
956 }
957 else
958 {
959 MEM_LOG("Error while installing new baseptr %p", pfn);
960 }
962 return okay;
963 }
965 static int do_extended_command(unsigned long ptr, unsigned long val)
966 {
967 int okay = 1, cpu = smp_processor_id();
968 unsigned int cmd = val & MMUEXT_CMD_MASK;
969 unsigned long pfn = ptr >> PAGE_SHIFT;
970 struct pfn_info *page = &frame_table[pfn];
971 struct exec_domain *ed = current;
972 struct domain *d = ed->domain, *nd, *e;
973 u32 x, y;
974 domid_t domid;
975 grant_ref_t gntref;
977 switch ( cmd )
978 {
979 case MMUEXT_PIN_L1_TABLE:
980 case MMUEXT_PIN_L2_TABLE:
981 /*
982 * We insist that, if you pin an L1 page, it's the first thing that
983 * you do to it. This is because we require the backptr to still be
984 * mutable. This assumption seems safe.
985 */
986 okay = get_page_and_type_from_pagenr(
987 pfn,
988 ((cmd==MMUEXT_PIN_L2_TABLE) ?
989 PGT_l2_page_table : (PGT_l1_page_table|PGT_va_mutable)),
990 FOREIGNDOM);
992 if ( unlikely(!okay) )
993 {
994 MEM_LOG("Error while pinning pfn %p", pfn);
995 break;
996 }
998 if ( unlikely(test_and_set_bit(_PGT_pinned,
999 &page->u.inuse.type_info)) )
1001 MEM_LOG("Pfn %p already pinned", pfn);
1002 put_page_and_type(page);
1003 okay = 0;
1004 break;
1007 break;
1009 case MMUEXT_UNPIN_TABLE:
1010 if ( unlikely(!(okay = get_page_from_pagenr(pfn, FOREIGNDOM))) )
1012 MEM_LOG("Page %p bad domain (dom=%p)",
1013 ptr, page_get_owner(page));
1015 else if ( likely(test_and_clear_bit(_PGT_pinned,
1016 &page->u.inuse.type_info)) )
1018 put_page_and_type(page);
1019 put_page(page);
1021 else
1023 okay = 0;
1024 put_page(page);
1025 MEM_LOG("Pfn %p not pinned", pfn);
1027 break;
1029 case MMUEXT_NEW_BASEPTR:
1030 okay = new_guest_cr3(pfn);
1031 break;
1033 case MMUEXT_TLB_FLUSH:
1034 percpu_info[cpu].deferred_ops |= DOP_FLUSH_TLB;
1035 break;
1037 case MMUEXT_INVLPG:
1038 __flush_tlb_one(ptr);
1039 break;
1041 case MMUEXT_FLUSH_CACHE:
1042 if ( unlikely(!IS_CAPABLE_PHYSDEV(d)) )
1044 MEM_LOG("Non-physdev domain tried to FLUSH_CACHE.\n");
1045 okay = 0;
1047 else
1049 wbinvd();
1051 break;
1053 case MMUEXT_SET_LDT:
1055 unsigned long ents = val >> MMUEXT_CMD_SHIFT;
1056 if ( ((ptr & (PAGE_SIZE-1)) != 0) ||
1057 (ents > 8192) ||
1058 ((ptr+ents*LDT_ENTRY_SIZE) < ptr) ||
1059 ((ptr+ents*LDT_ENTRY_SIZE) > PAGE_OFFSET) )
1061 okay = 0;
1062 MEM_LOG("Bad args to SET_LDT: ptr=%p, ents=%p", ptr, ents);
1064 else if ( (ed->arch.ldt_ents != ents) ||
1065 (ed->arch.ldt_base != ptr) )
1067 invalidate_shadow_ldt(ed);
1068 ed->arch.ldt_base = ptr;
1069 ed->arch.ldt_ents = ents;
1070 load_LDT(ed);
1071 percpu_info[cpu].deferred_ops &= ~DOP_RELOAD_LDT;
1072 if ( ents != 0 )
1073 percpu_info[cpu].deferred_ops |= DOP_RELOAD_LDT;
1075 break;
1078 case MMUEXT_SET_FOREIGNDOM:
1079 domid = (domid_t)(val >> 16);
1081 if ( (e = percpu_info[cpu].foreign) != NULL )
1082 put_domain(e);
1083 percpu_info[cpu].foreign = NULL;
1085 if ( !IS_PRIV(d) )
1087 switch ( domid )
1089 case DOMID_IO:
1090 get_knownalive_domain(dom_io);
1091 percpu_info[cpu].foreign = dom_io;
1092 break;
1093 default:
1094 MEM_LOG("Dom %u cannot set foreign dom\n", d->id);
1095 okay = 0;
1096 break;
1099 else
1101 percpu_info[cpu].foreign = e = find_domain_by_id(domid);
1102 if ( e == NULL )
1104 switch ( domid )
1106 case DOMID_XEN:
1107 get_knownalive_domain(dom_xen);
1108 percpu_info[cpu].foreign = dom_xen;
1109 break;
1110 case DOMID_IO:
1111 get_knownalive_domain(dom_io);
1112 percpu_info[cpu].foreign = dom_io;
1113 break;
1114 default:
1115 MEM_LOG("Unknown domain '%u'", domid);
1116 okay = 0;
1117 break;
1121 break;
1123 case MMUEXT_TRANSFER_PAGE:
1124 domid = (domid_t)(val >> 16);
1125 gntref = (grant_ref_t)((val & 0xFF00) | ((ptr >> 2) & 0x00FF));
1127 if ( unlikely(IS_XEN_HEAP_FRAME(page)) ||
1128 unlikely(!pfn_is_ram(pfn)) ||
1129 unlikely((e = find_domain_by_id(domid)) == NULL) )
1131 MEM_LOG("Bad frame (%p) or bad domid (%d).\n", pfn, domid);
1132 okay = 0;
1133 break;
1136 spin_lock(&d->page_alloc_lock);
1138 /*
1139 * The tricky bit: atomically release ownership while there is just one
1140 * benign reference to the page (PGC_allocated). If that reference
1141 * disappears then the deallocation routine will safely spin.
1142 */
1143 nd = page_get_owner(page);
1144 y = page->count_info;
1145 do {
1146 x = y;
1147 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1148 (1|PGC_allocated)) ||
1149 unlikely(nd != d) )
1151 MEM_LOG("Bad page values %p: ed=%p(%u), sd=%p,"
1152 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1153 d, d->id, nd, x, page->u.inuse.type_info);
1154 spin_unlock(&d->page_alloc_lock);
1155 put_domain(e);
1156 return 0;
1158 __asm__ __volatile__(
1159 LOCK_PREFIX "cmpxchg8b %2"
1160 : "=d" (nd), "=a" (y),
1161 "=m" (*(volatile u64 *)(&page->count_info))
1162 : "0" (d), "1" (x), "c" (NULL), "b" (x) );
1164 while ( unlikely(nd != d) || unlikely(y != x) );
1166 /*
1167 * Unlink from 'd'. At least one reference remains (now anonymous), so
1168 * noone else is spinning to try to delete this page from 'd'.
1169 */
1170 d->tot_pages--;
1171 list_del(&page->list);
1173 spin_unlock(&d->page_alloc_lock);
1175 spin_lock(&e->page_alloc_lock);
1177 /*
1178 * Check that 'e' will accept the page and has reservation headroom.
1179 * Also, a domain mustn't have PGC_allocated pages when it is dying.
1180 */
1181 ASSERT(e->tot_pages <= e->max_pages);
1182 if ( unlikely(test_bit(DF_DYING, &e->d_flags)) ||
1183 unlikely(e->tot_pages == e->max_pages) ||
1184 unlikely(!gnttab_prepare_for_transfer(e, d, gntref)) )
1186 MEM_LOG("Transferee has no reservation headroom (%d,%d), or "
1187 "provided a bad grant ref, or is dying (%p).\n",
1188 e->tot_pages, e->max_pages, e->d_flags);
1189 spin_unlock(&e->page_alloc_lock);
1190 put_domain(e);
1191 okay = 0;
1192 break;
1195 /* Okay, add the page to 'e'. */
1196 if ( unlikely(e->tot_pages++ == 0) )
1197 get_knownalive_domain(e);
1198 list_add_tail(&page->list, &e->page_list);
1199 page_set_owner(page, e);
1201 spin_unlock(&e->page_alloc_lock);
1203 /* Transfer is all done: tell the guest about its new page frame. */
1204 gnttab_notify_transfer(e, gntref, pfn);
1206 put_domain(e);
1207 break;
1209 case MMUEXT_REASSIGN_PAGE:
1210 if ( unlikely(!IS_PRIV(d)) )
1212 MEM_LOG("Dom %u has no reassignment priv", d->id);
1213 okay = 0;
1214 break;
1217 e = percpu_info[cpu].foreign;
1218 if ( unlikely(e == NULL) )
1220 MEM_LOG("No FOREIGNDOM to reassign pfn %p to", pfn);
1221 okay = 0;
1222 break;
1225 /*
1226 * Grab both page_list locks, in order. This prevents the page from
1227 * disappearing elsewhere while we modify the owner, and we'll need
1228 * both locks if we're successful so that we can change lists.
1229 */
1230 if ( d < e )
1232 spin_lock(&d->page_alloc_lock);
1233 spin_lock(&e->page_alloc_lock);
1235 else
1237 spin_lock(&e->page_alloc_lock);
1238 spin_lock(&d->page_alloc_lock);
1241 /* A domain shouldn't have PGC_allocated pages when it is dying. */
1242 if ( unlikely(test_bit(DF_DYING, &e->d_flags)) ||
1243 unlikely(IS_XEN_HEAP_FRAME(page)) )
1245 MEM_LOG("Reassignment page is Xen heap, or dest dom is dying.");
1246 okay = 0;
1247 goto reassign_fail;
1250 /*
1251 * The tricky bit: atomically change owner while there is just one
1252 * benign reference to the page (PGC_allocated). If that reference
1253 * disappears then the deallocation routine will safely spin.
1254 */
1255 nd = page_get_owner(page);
1256 y = page->count_info;
1257 do {
1258 x = y;
1259 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1260 (1|PGC_allocated)) ||
1261 unlikely(nd != d) )
1263 MEM_LOG("Bad page values %p: ed=%p(%u), sd=%p,"
1264 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1265 d, d->id, nd, x, page->u.inuse.type_info);
1266 okay = 0;
1267 goto reassign_fail;
1269 __asm__ __volatile__(
1270 LOCK_PREFIX "cmpxchg8b %3"
1271 : "=d" (nd), "=a" (y), "=c" (e),
1272 "=m" (*(volatile u64 *)(&page->count_info))
1273 : "0" (d), "1" (x), "c" (e), "b" (x) );
1275 while ( unlikely(nd != d) || unlikely(y != x) );
1277 /*
1278 * Unlink from 'd'. We transferred at least one reference to 'e', so
1279 * noone else is spinning to try to delete this page from 'd'.
1280 */
1281 d->tot_pages--;
1282 list_del(&page->list);
1284 /*
1285 * Add the page to 'e'. Someone may already have removed the last
1286 * reference and want to remove the page from 'e'. However, we have
1287 * the lock so they'll spin waiting for us.
1288 */
1289 if ( unlikely(e->tot_pages++ == 0) )
1290 get_knownalive_domain(e);
1291 list_add_tail(&page->list, &e->page_list);
1293 reassign_fail:
1294 spin_unlock(&d->page_alloc_lock);
1295 spin_unlock(&e->page_alloc_lock);
1296 break;
1298 case MMUEXT_CLEAR_FOREIGNDOM:
1299 if ( (e = percpu_info[cpu].foreign) != NULL )
1300 put_domain(e);
1301 percpu_info[cpu].foreign = NULL;
1302 break;
1304 default:
1305 MEM_LOG("Invalid extended pt command 0x%p", val & MMUEXT_CMD_MASK);
1306 okay = 0;
1307 break;
1310 return okay;
1313 int do_mmu_update(
1314 mmu_update_t *ureqs, unsigned int count, unsigned int *pdone)
1316 /*
1317 * We steal the m.s.b. of the @count parameter to indicate whether this
1318 * invocation of do_mmu_update() is resuming a previously preempted call.
1319 * We steal the next 15 bits to remember the current FOREIGNDOM.
1320 */
1321 #define MMU_UPDATE_PREEMPTED (~(~0U>>1))
1322 #define MMU_UPDATE_PREEMPT_FDOM_SHIFT ((sizeof(int)*8)-16)
1323 #define MMU_UPDATE_PREEMPT_FDOM_MASK (0x7FFFU<<MMU_UPDATE_PREEMPT_FDOM_SHIFT)
1325 mmu_update_t req;
1326 unsigned long va = 0, deferred_ops, pfn, prev_pfn = 0;
1327 struct pfn_info *page;
1328 int rc = 0, okay = 1, i = 0, cpu = smp_processor_id();
1329 unsigned int cmd, done = 0;
1330 unsigned long prev_smfn = 0;
1331 l1_pgentry_t *prev_spl1e = 0;
1332 struct exec_domain *ed = current;
1333 struct domain *d = ed->domain;
1334 u32 type_info;
1335 domid_t domid;
1337 LOCK_BIGLOCK(d);
1339 cleanup_writable_pagetable(d);
1341 if ( unlikely(shadow_mode(d)) )
1342 check_pagetable(d, ed->arch.pagetable, "pre-mmu"); /* debug */
1344 /*
1345 * If we are resuming after preemption, read how much work we have already
1346 * done. This allows us to set the @done output parameter correctly.
1347 * We also reset FOREIGNDOM here.
1348 */
1349 if ( unlikely(count&(MMU_UPDATE_PREEMPTED|MMU_UPDATE_PREEMPT_FDOM_MASK)) )
1351 if ( !(count & MMU_UPDATE_PREEMPTED) )
1353 /* Count overflow into private FOREIGNDOM field. */
1354 MEM_LOG("do_mmu_update count is too large");
1355 rc = -EINVAL;
1356 goto out;
1358 count &= ~MMU_UPDATE_PREEMPTED;
1359 domid = count >> MMU_UPDATE_PREEMPT_FDOM_SHIFT;
1360 count &= ~MMU_UPDATE_PREEMPT_FDOM_MASK;
1361 if ( unlikely(pdone != NULL) )
1362 (void)get_user(done, pdone);
1363 if ( (domid != current->domain->id) &&
1364 !do_extended_command(0, MMUEXT_SET_FOREIGNDOM | (domid << 16)) )
1366 rc = -EINVAL;
1367 goto out;
1371 perfc_incrc(calls_to_mmu_update);
1372 perfc_addc(num_page_updates, count);
1374 if ( unlikely(!array_access_ok(VERIFY_READ, ureqs, count, sizeof(req))) )
1376 rc = -EFAULT;
1377 goto out;
1380 for ( i = 0; i < count; i++ )
1382 if ( hypercall_preempt_check() )
1384 rc = hypercall3_create_continuation(
1385 __HYPERVISOR_mmu_update, ureqs,
1386 (count - i) |
1387 (FOREIGNDOM->id << MMU_UPDATE_PREEMPT_FDOM_SHIFT) |
1388 MMU_UPDATE_PREEMPTED, pdone);
1389 break;
1392 if ( unlikely(__copy_from_user(&req, ureqs, sizeof(req)) != 0) )
1394 MEM_LOG("Bad __copy_from_user");
1395 rc = -EFAULT;
1396 break;
1399 cmd = req.ptr & (sizeof(l1_pgentry_t)-1);
1400 pfn = req.ptr >> PAGE_SHIFT;
1402 okay = 0;
1404 switch ( cmd )
1406 /*
1407 * MMU_NORMAL_PT_UPDATE: Normal update to any level of page table.
1408 */
1409 case MMU_NORMAL_PT_UPDATE:
1410 if ( unlikely(!get_page_from_pagenr(pfn, current->domain)) )
1412 MEM_LOG("Could not get page for normal update");
1413 break;
1416 if ( likely(prev_pfn == pfn) )
1418 va = (va & PAGE_MASK) | (req.ptr & ~PAGE_MASK);
1420 else
1422 if ( prev_pfn != 0 )
1423 unmap_domain_mem((void *)va);
1424 va = (unsigned long)map_domain_mem(req.ptr);
1425 prev_pfn = pfn;
1428 page = &frame_table[pfn];
1429 switch ( (type_info = page->u.inuse.type_info) & PGT_type_mask )
1431 case PGT_l1_page_table:
1432 if ( likely(get_page_type(
1433 page, type_info & (PGT_type_mask|PGT_va_mask))) )
1435 okay = mod_l1_entry((l1_pgentry_t *)va,
1436 mk_l1_pgentry(req.val));
1438 if ( unlikely(shadow_mode(d)) && okay &&
1439 (get_shadow_status(d, page-frame_table) &
1440 PSH_shadowed) )
1442 shadow_l1_normal_pt_update(
1443 req.ptr, req.val, &prev_smfn, &prev_spl1e);
1444 put_shadow_status(d);
1447 put_page_type(page);
1449 break;
1450 case PGT_l2_page_table:
1451 if ( likely(get_page_type(page, PGT_l2_page_table)) )
1453 okay = mod_l2_entry((l2_pgentry_t *)va,
1454 mk_l2_pgentry(req.val),
1455 pfn);
1457 if ( unlikely(shadow_mode(d)) && okay &&
1458 (get_shadow_status(d, page-frame_table) &
1459 PSH_shadowed) )
1461 shadow_l2_normal_pt_update(req.ptr, req.val);
1462 put_shadow_status(d);
1465 put_page_type(page);
1467 break;
1468 default:
1469 if ( likely(get_page_type(page, PGT_writable_page)) )
1471 *(unsigned long *)va = req.val;
1472 okay = 1;
1473 put_page_type(page);
1475 break;
1478 put_page(page);
1479 break;
1481 case MMU_MACHPHYS_UPDATE:
1482 if ( unlikely(!get_page_from_pagenr(pfn, FOREIGNDOM)) )
1484 MEM_LOG("Could not get page for mach->phys update");
1485 break;
1488 machine_to_phys_mapping[pfn] = req.val;
1489 okay = 1;
1491 /*
1492 * If in log-dirty mode, mark the corresponding pseudo-physical
1493 * page as dirty.
1494 */
1495 if ( unlikely(shadow_mode(d) == SHM_logdirty) &&
1496 mark_dirty(d, pfn) )
1497 d->arch.shadow_dirty_block_count++;
1499 put_page(&frame_table[pfn]);
1500 break;
1502 /*
1503 * MMU_EXTENDED_COMMAND: Extended command is specified
1504 * in the least-siginificant bits of the 'value' field.
1505 */
1506 case MMU_EXTENDED_COMMAND:
1507 req.ptr &= ~(sizeof(l1_pgentry_t) - 1);
1508 okay = do_extended_command(req.ptr, req.val);
1509 break;
1511 default:
1512 MEM_LOG("Invalid page update command %p", req.ptr);
1513 break;
1516 if ( unlikely(!okay) )
1518 rc = -EINVAL;
1519 break;
1522 ureqs++;
1525 out:
1526 if ( prev_pfn != 0 )
1527 unmap_domain_mem((void *)va);
1529 if ( unlikely(prev_spl1e != 0) )
1530 unmap_domain_mem((void *)prev_spl1e);
1532 deferred_ops = percpu_info[cpu].deferred_ops;
1533 percpu_info[cpu].deferred_ops = 0;
1535 if ( deferred_ops & DOP_FLUSH_TLB )
1536 local_flush_tlb();
1538 if ( deferred_ops & DOP_RELOAD_LDT )
1539 (void)map_ldt_shadow_page(0);
1541 if ( unlikely(percpu_info[cpu].foreign != NULL) )
1543 put_domain(percpu_info[cpu].foreign);
1544 percpu_info[cpu].foreign = NULL;
1547 /* Add incremental work we have done to the @done output parameter. */
1548 if ( unlikely(pdone != NULL) )
1549 __put_user(done + i, pdone);
1551 if ( unlikely(shadow_mode(d)) )
1552 check_pagetable(d, ed->arch.pagetable, "post-mmu"); /* debug */
1554 UNLOCK_BIGLOCK(d);
1555 return rc;
1559 int do_update_va_mapping(unsigned long va,
1560 unsigned long val,
1561 unsigned long flags)
1563 struct exec_domain *ed = current;
1564 struct domain *d = ed->domain;
1565 int err = 0;
1566 unsigned int cpu = ed->processor;
1567 unsigned long deferred_ops;
1569 perfc_incrc(calls_to_update_va);
1571 if ( unlikely(!__addr_ok(va)) )
1572 return -EINVAL;
1574 LOCK_BIGLOCK(d);
1576 cleanup_writable_pagetable(d);
1578 /*
1579 * XXX When we make this support 4MB superpages we should also deal with
1580 * the case of updating L2 entries.
1581 */
1583 if ( unlikely(!mod_l1_entry(&linear_pg_table[l1_linear_offset(va)],
1584 mk_l1_pgentry(val))) )
1585 err = -EINVAL;
1587 if ( unlikely(shadow_mode(d)) )
1589 unsigned long sval = 0;
1591 l1pte_propagate_from_guest(d, &val, &sval);
1593 if ( unlikely(__put_user(sval, ((unsigned long *)(
1594 &shadow_linear_pg_table[l1_linear_offset(va)])))) )
1596 /*
1597 * Since L2's are guranteed RW, failure indicates the page was not
1598 * shadowed, so ignore.
1599 */
1600 perfc_incrc(shadow_update_va_fail);
1603 /*
1604 * If we're in log-dirty mode then we need to note that we've updated
1605 * the PTE in the PT-holding page. We need the machine frame number
1606 * for this.
1607 */
1608 if ( shadow_mode(d) == SHM_logdirty )
1609 mark_dirty(d, va_to_l1mfn(va));
1611 check_pagetable(d, ed->arch.pagetable, "va"); /* debug */
1614 deferred_ops = percpu_info[cpu].deferred_ops;
1615 percpu_info[cpu].deferred_ops = 0;
1617 if ( unlikely(deferred_ops & DOP_FLUSH_TLB) ||
1618 unlikely(flags & UVMF_FLUSH_TLB) )
1619 local_flush_tlb();
1620 else if ( unlikely(flags & UVMF_INVLPG) )
1621 __flush_tlb_one(va);
1623 if ( unlikely(deferred_ops & DOP_RELOAD_LDT) )
1624 (void)map_ldt_shadow_page(0);
1626 UNLOCK_BIGLOCK(d);
1628 return err;
1631 int do_update_va_mapping_otherdomain(unsigned long va,
1632 unsigned long val,
1633 unsigned long flags,
1634 domid_t domid)
1636 unsigned int cpu = smp_processor_id();
1637 struct domain *d;
1638 int rc;
1640 if ( unlikely(!IS_PRIV(current->domain)) )
1641 return -EPERM;
1643 percpu_info[cpu].foreign = d = find_domain_by_id(domid);
1644 if ( unlikely(d == NULL) )
1646 MEM_LOG("Unknown domain '%u'", domid);
1647 return -ESRCH;
1650 rc = do_update_va_mapping(va, val, flags);
1652 put_domain(d);
1653 percpu_info[cpu].foreign = NULL;
1655 return rc;
1660 /*************************
1661 * Descriptor Tables
1662 */
1664 void destroy_gdt(struct exec_domain *ed)
1666 int i;
1667 unsigned long pfn;
1669 for ( i = 0; i < 16; i++ )
1671 if ( (pfn = l1_pgentry_to_pfn(ed->arch.perdomain_ptes[i])) != 0 )
1672 put_page_and_type(&frame_table[pfn]);
1673 ed->arch.perdomain_ptes[i] = mk_l1_pgentry(0);
1678 long set_gdt(struct exec_domain *ed,
1679 unsigned long *frames,
1680 unsigned int entries)
1682 struct domain *d = ed->domain;
1683 /* NB. There are 512 8-byte entries per GDT page. */
1684 int i = 0, nr_pages = (entries + 511) / 512;
1685 struct desc_struct *vgdt;
1686 unsigned long pfn;
1688 /* Check the first page in the new GDT. */
1689 if ( (pfn = frames[0]) >= max_page )
1690 goto fail;
1692 /* The first page is special because Xen owns a range of entries in it. */
1693 if ( !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
1695 /* GDT checks failed: try zapping the Xen reserved entries. */
1696 if ( !get_page_and_type(&frame_table[pfn], d, PGT_writable_page) )
1697 goto fail;
1698 vgdt = map_domain_mem(pfn << PAGE_SHIFT);
1699 memset(vgdt + FIRST_RESERVED_GDT_ENTRY, 0,
1700 NR_RESERVED_GDT_ENTRIES*8);
1701 unmap_domain_mem(vgdt);
1702 put_page_and_type(&frame_table[pfn]);
1704 /* Okay, we zapped the entries. Now try the GDT checks again. */
1705 if ( !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
1706 goto fail;
1709 /* Check the remaining pages in the new GDT. */
1710 for ( i = 1; i < nr_pages; i++ )
1711 if ( ((pfn = frames[i]) >= max_page) ||
1712 !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
1713 goto fail;
1715 /* Copy reserved GDT entries to the new GDT. */
1716 vgdt = map_domain_mem(frames[0] << PAGE_SHIFT);
1717 memcpy(vgdt + FIRST_RESERVED_GDT_ENTRY,
1718 gdt_table + FIRST_RESERVED_GDT_ENTRY,
1719 NR_RESERVED_GDT_ENTRIES*8);
1720 unmap_domain_mem(vgdt);
1722 /* Tear down the old GDT. */
1723 destroy_gdt(ed);
1725 /* Install the new GDT. */
1726 for ( i = 0; i < nr_pages; i++ )
1727 ed->arch.perdomain_ptes[i] =
1728 mk_l1_pgentry((frames[i] << PAGE_SHIFT) | __PAGE_HYPERVISOR);
1730 SET_GDT_ADDRESS(ed, GDT_VIRT_START(ed));
1731 SET_GDT_ENTRIES(ed, entries);
1733 return 0;
1735 fail:
1736 while ( i-- > 0 )
1737 put_page_and_type(&frame_table[frames[i]]);
1738 return -EINVAL;
1742 long do_set_gdt(unsigned long *frame_list, unsigned int entries)
1744 int nr_pages = (entries + 511) / 512;
1745 unsigned long frames[16];
1746 long ret;
1748 if ( (entries <= LAST_RESERVED_GDT_ENTRY) || (entries > 8192) )
1749 return -EINVAL;
1751 if ( copy_from_user(frames, frame_list, nr_pages * sizeof(unsigned long)) )
1752 return -EFAULT;
1754 LOCK_BIGLOCK(current->domain);
1756 if ( (ret = set_gdt(current, frames, entries)) == 0 )
1758 local_flush_tlb();
1759 __asm__ __volatile__ ("lgdt %0" : "=m" (*current->arch.gdt));
1762 UNLOCK_BIGLOCK(current->domain);
1764 return ret;
1768 long do_update_descriptor(
1769 unsigned long pa, unsigned long word1, unsigned long word2)
1771 unsigned long pfn = pa >> PAGE_SHIFT;
1772 struct desc_struct *gdt_pent, d;
1773 struct pfn_info *page;
1774 struct exec_domain *ed;
1775 long ret = -EINVAL;
1777 d.a = (u32)word1;
1778 d.b = (u32)word2;
1780 LOCK_BIGLOCK(current->domain);
1782 if ( (pa & 7) || (pfn >= max_page) || !check_descriptor(&d) ) {
1783 UNLOCK_BIGLOCK(current->domain);
1784 return -EINVAL;
1787 page = &frame_table[pfn];
1788 if ( unlikely(!get_page(page, current->domain)) ) {
1789 UNLOCK_BIGLOCK(current->domain);
1790 return -EINVAL;
1793 /* Check if the given frame is in use in an unsafe context. */
1794 switch ( page->u.inuse.type_info & PGT_type_mask )
1796 case PGT_gdt_page:
1797 /* Disallow updates of Xen-reserved descriptors in the current GDT. */
1798 for_each_exec_domain(current->domain, ed) {
1799 if ( (l1_pgentry_to_pfn(ed->arch.perdomain_ptes[0]) == pfn) &&
1800 (((pa&(PAGE_SIZE-1))>>3) >= FIRST_RESERVED_GDT_ENTRY) &&
1801 (((pa&(PAGE_SIZE-1))>>3) <= LAST_RESERVED_GDT_ENTRY) )
1802 goto out;
1804 if ( unlikely(!get_page_type(page, PGT_gdt_page)) )
1805 goto out;
1806 break;
1807 case PGT_ldt_page:
1808 if ( unlikely(!get_page_type(page, PGT_ldt_page)) )
1809 goto out;
1810 break;
1811 default:
1812 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
1813 goto out;
1814 break;
1817 /* All is good so make the update. */
1818 gdt_pent = map_domain_mem(pa);
1819 memcpy(gdt_pent, &d, 8);
1820 unmap_domain_mem(gdt_pent);
1822 put_page_type(page);
1824 ret = 0; /* success */
1826 out:
1827 put_page(page);
1829 UNLOCK_BIGLOCK(current->domain);
1831 return ret;
1836 /*************************
1837 * Writable Pagetables
1838 */
1840 ptwr_info_t ptwr_info[NR_CPUS];
1842 #ifdef VERBOSE
1843 int ptwr_debug = 0x0;
1844 #define PTWR_PRINTK(_f, _a...) \
1845 do { if ( unlikely(ptwr_debug) ) printk( _f , ## _a ); } while ( 0 )
1846 #define PTWR_PRINT_WHICH (which ? 'I' : 'A')
1847 #else
1848 #define PTWR_PRINTK(_f, _a...) ((void)0)
1849 #endif
1851 /* Flush the given writable p.t. page and write-protect it again. */
1852 void ptwr_flush(const int which)
1854 unsigned long sstat, spte, pte, *ptep, l1va;
1855 l1_pgentry_t *sl1e = NULL, *pl1e, ol1e, nl1e;
1856 l2_pgentry_t *pl2e;
1857 int i, cpu = smp_processor_id();
1858 struct exec_domain *ed = current;
1859 struct domain *d = ed->domain;
1861 l1va = ptwr_info[cpu].ptinfo[which].l1va;
1862 ptep = (unsigned long *)&linear_pg_table[l1va>>PAGE_SHIFT];
1864 /*
1865 * STEP 1. Write-protect the p.t. page so no more updates can occur.
1866 */
1868 if ( unlikely(__get_user(pte, ptep)) )
1870 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
1871 /*
1872 * Really a bug. We could read this PTE during the initial fault,
1873 * and pagetables can't have changed meantime. XXX Multi-CPU guests?
1874 */
1875 BUG();
1877 PTWR_PRINTK("[%c] disconnected_l1va at %p is %p\n",
1878 PTWR_PRINT_WHICH, ptep, pte);
1879 pte &= ~_PAGE_RW;
1881 if ( unlikely(shadow_mode(d)) )
1883 /* Write-protect the p.t. page in the shadow page table. */
1884 l1pte_propagate_from_guest(d, &pte, &spte);
1885 __put_user(
1886 spte, (unsigned long *)&shadow_linear_pg_table[l1va>>PAGE_SHIFT]);
1888 /* Is the p.t. page itself shadowed? Map it into Xen space if so. */
1889 sstat = get_shadow_status(d, pte >> PAGE_SHIFT);
1890 if ( sstat & PSH_shadowed )
1891 sl1e = map_domain_mem((sstat & PSH_pfn_mask) << PAGE_SHIFT);
1894 /* Write-protect the p.t. page in the guest page table. */
1895 if ( unlikely(__put_user(pte, ptep)) )
1897 MEM_LOG("ptwr: Could not update pte at %p\n", ptep);
1898 /*
1899 * Really a bug. We could write this PTE during the initial fault,
1900 * and pagetables can't have changed meantime. XXX Multi-CPU guests?
1901 */
1902 BUG();
1905 /* Ensure that there are no stale writable mappings in any TLB. */
1906 /* NB. INVLPG is a serialising instruction: flushes pending updates. */
1907 #if 1
1908 __flush_tlb_one(l1va); /* XXX Multi-CPU guests? */
1909 #else
1910 flush_tlb_all();
1911 #endif
1912 PTWR_PRINTK("[%c] disconnected_l1va at %p now %p\n",
1913 PTWR_PRINT_WHICH, ptep, pte);
1915 /*
1916 * STEP 2. Validate any modified PTEs.
1917 */
1919 pl1e = ptwr_info[cpu].ptinfo[which].pl1e;
1920 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
1922 ol1e = ptwr_info[cpu].ptinfo[which].page[i];
1923 nl1e = pl1e[i];
1925 if ( likely(l1_pgentry_val(ol1e) == l1_pgentry_val(nl1e)) )
1926 continue;
1928 /*
1929 * Fast path for PTEs that have merely been write-protected
1930 * (e.g., during a Unix fork()). A strict reduction in privilege.
1931 */
1932 if ( likely(l1_pgentry_val(ol1e) == (l1_pgentry_val(nl1e)|_PAGE_RW)) )
1934 if ( likely(l1_pgentry_val(nl1e) & _PAGE_PRESENT) )
1936 if ( unlikely(sl1e != NULL) )
1937 l1pte_propagate_from_guest(
1938 d, &l1_pgentry_val(nl1e),
1939 &l1_pgentry_val(sl1e[i]));
1940 put_page_type(&frame_table[l1_pgentry_to_pfn(nl1e)]);
1942 continue;
1945 if ( unlikely(!get_page_from_l1e(nl1e, d)) )
1947 MEM_LOG("ptwr: Could not re-validate l1 page\n");
1948 /*
1949 * Make the remaining p.t's consistent before crashing, so the
1950 * reference counts are correct.
1951 */
1952 memcpy(&pl1e[i], &ptwr_info[cpu].ptinfo[which].page[i],
1953 (ENTRIES_PER_L1_PAGETABLE - i) * sizeof(l1_pgentry_t));
1954 unmap_domain_mem(pl1e);
1955 ptwr_info[cpu].ptinfo[which].l1va = 0;
1956 UNLOCK_BIGLOCK(d);
1957 domain_crash();
1960 if ( unlikely(sl1e != NULL) )
1961 l1pte_propagate_from_guest(
1962 d, &l1_pgentry_val(nl1e), &l1_pgentry_val(sl1e[i]));
1964 if ( unlikely(l1_pgentry_val(ol1e) & _PAGE_PRESENT) )
1965 put_page_from_l1e(ol1e, d);
1967 unmap_domain_mem(pl1e);
1969 /*
1970 * STEP 3. Reattach the L1 p.t. page into the current address space.
1971 */
1973 if ( (which == PTWR_PT_ACTIVE) && likely(!shadow_mode(d)) )
1975 pl2e = &linear_l2_table[ptwr_info[cpu].ptinfo[which].l2_idx];
1976 *pl2e = mk_l2_pgentry(l2_pgentry_val(*pl2e) | _PAGE_PRESENT);
1979 /*
1980 * STEP 4. Final tidy-up.
1981 */
1983 ptwr_info[cpu].ptinfo[which].l1va = 0;
1985 if ( unlikely(sl1e != NULL) )
1987 unmap_domain_mem(sl1e);
1988 put_shadow_status(d);
1992 /* Write page fault handler: check if guest is trying to modify a PTE. */
1993 int ptwr_do_page_fault(unsigned long addr)
1995 unsigned long pte, pfn, l2e;
1996 struct pfn_info *page;
1997 l2_pgentry_t *pl2e;
1998 int which, cpu = smp_processor_id();
1999 u32 l2_idx;
2001 #ifdef __x86_64__
2002 return 0; /* Writable pagetables need fixing for x86_64. */
2003 #endif
2005 /*
2006 * Attempt to read the PTE that maps the VA being accessed. By checking for
2007 * PDE validity in the L2 we avoid many expensive fixups in __get_user().
2008 */
2009 if ( !(l2_pgentry_val(linear_l2_table[addr>>L2_PAGETABLE_SHIFT]) &
2010 _PAGE_PRESENT) ||
2011 __get_user(pte, (unsigned long *)&linear_pg_table[addr>>PAGE_SHIFT]) )
2013 return 0;
2016 pfn = pte >> PAGE_SHIFT;
2017 page = &frame_table[pfn];
2019 /* We are looking only for read-only mappings of p.t. pages. */
2020 if ( ((pte & (_PAGE_RW | _PAGE_PRESENT)) != _PAGE_PRESENT) ||
2021 ((page->u.inuse.type_info & PGT_type_mask) != PGT_l1_page_table) )
2023 return 0;
2026 /* Get the L2 index at which this L1 p.t. is always mapped. */
2027 l2_idx = page->u.inuse.type_info & PGT_va_mask;
2028 if ( unlikely(l2_idx >= PGT_va_unknown) )
2030 domain_crash(); /* Urk! This L1 is mapped in multiple L2 slots! */
2032 l2_idx >>= PGT_va_shift;
2034 if ( l2_idx == (addr >> L2_PAGETABLE_SHIFT) )
2036 MEM_LOG("PTWR failure! Pagetable maps itself at %p\n", addr);
2037 domain_crash();
2040 /*
2041 * Is the L1 p.t. mapped into the current address space? If so we call it
2042 * an ACTIVE p.t., otherwise it is INACTIVE.
2043 */
2044 pl2e = &linear_l2_table[l2_idx];
2045 l2e = l2_pgentry_val(*pl2e);
2046 which = PTWR_PT_INACTIVE;
2047 if ( (l2e >> PAGE_SHIFT) == pfn )
2049 /* Check the PRESENT bit to set ACTIVE. */
2050 if ( likely(l2e & _PAGE_PRESENT) )
2051 which = PTWR_PT_ACTIVE;
2052 else {
2053 /*
2054 * If the PRESENT bit is clear, we may be conflicting with
2055 * the current ACTIVE p.t. (it may be the same p.t. mapped
2056 * at another virt addr).
2057 * The ptwr_flush call below will restore the PRESENT bit.
2058 */
2059 if ( ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va &&
2060 l2_idx == ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l2_idx )
2061 which = PTWR_PT_ACTIVE;
2065 PTWR_PRINTK("[%c] page_fault on l1 pt at va %p, pt for %08x, "
2066 "pfn %p\n", PTWR_PRINT_WHICH,
2067 addr, l2_idx << L2_PAGETABLE_SHIFT, pfn);
2069 /*
2070 * We only allow one ACTIVE and one INACTIVE p.t. to be updated at at
2071 * time. If there is already one, we must flush it out.
2072 */
2073 if ( ptwr_info[cpu].ptinfo[which].l1va )
2074 ptwr_flush(which);
2076 ptwr_info[cpu].ptinfo[which].l1va = addr | 1;
2077 ptwr_info[cpu].ptinfo[which].l2_idx = l2_idx;
2079 /* For safety, disconnect the L1 p.t. page from current space. */
2080 if ( (which == PTWR_PT_ACTIVE) &&
2081 likely(!shadow_mode(current->domain)) )
2083 *pl2e = mk_l2_pgentry(l2e & ~_PAGE_PRESENT);
2084 #if 1
2085 flush_tlb(); /* XXX Multi-CPU guests? */
2086 #else
2087 flush_tlb_all();
2088 #endif
2091 /* Temporarily map the L1 page, and make a copy of it. */
2092 ptwr_info[cpu].ptinfo[which].pl1e = map_domain_mem(pfn << PAGE_SHIFT);
2093 memcpy(ptwr_info[cpu].ptinfo[which].page,
2094 ptwr_info[cpu].ptinfo[which].pl1e,
2095 ENTRIES_PER_L1_PAGETABLE * sizeof(l1_pgentry_t));
2097 /* Finally, make the p.t. page writable by the guest OS. */
2098 pte |= _PAGE_RW;
2099 PTWR_PRINTK("[%c] update %p pte to %p\n", PTWR_PRINT_WHICH,
2100 &linear_pg_table[addr>>PAGE_SHIFT], pte);
2101 if ( unlikely(__put_user(pte, (unsigned long *)
2102 &linear_pg_table[addr>>PAGE_SHIFT])) )
2104 MEM_LOG("ptwr: Could not update pte at %p\n", (unsigned long *)
2105 &linear_pg_table[addr>>PAGE_SHIFT]);
2106 /* Toss the writable pagetable state and crash. */
2107 unmap_domain_mem(ptwr_info[cpu].ptinfo[which].pl1e);
2108 ptwr_info[cpu].ptinfo[which].l1va = 0;
2109 domain_crash();
2112 return EXCRET_fault_fixed;
2115 static __init int ptwr_init(void)
2117 int i;
2119 for ( i = 0; i < smp_num_cpus; i++ )
2121 ptwr_info[i].ptinfo[PTWR_PT_ACTIVE].page =
2122 (void *)alloc_xenheap_page();
2123 ptwr_info[i].ptinfo[PTWR_PT_INACTIVE].page =
2124 (void *)alloc_xenheap_page();
2127 return 0;
2129 __initcall(ptwr_init);
2134 /************************************************************************/
2135 /************************************************************************/
2136 /************************************************************************/
2138 #ifndef NDEBUG
2140 void ptwr_status(void)
2142 unsigned long pte, *ptep, pfn;
2143 struct pfn_info *page;
2144 int cpu = smp_processor_id();
2146 ptep = (unsigned long *)&linear_pg_table
2147 [ptwr_info[cpu].ptinfo[PTWR_PT_INACTIVE].l1va>>PAGE_SHIFT];
2149 if ( __get_user(pte, ptep) ) {
2150 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
2151 domain_crash();
2154 pfn = pte >> PAGE_SHIFT;
2155 page = &frame_table[pfn];
2156 printk("need to alloc l1 page %p\n", page);
2157 /* make pt page writable */
2158 printk("need to make read-only l1-page at %p is %p\n",
2159 ptep, pte);
2161 if ( ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va == 0 )
2162 return;
2164 if ( __get_user(pte, (unsigned long *)
2165 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va) ) {
2166 MEM_LOG("ptwr: Could not read pte at %p\n", (unsigned long *)
2167 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va);
2168 domain_crash();
2170 pfn = pte >> PAGE_SHIFT;
2171 page = &frame_table[pfn];
2174 void audit_domain(struct domain *d)
2176 int ttot=0, ctot=0, io_mappings=0, lowmem_mappings=0;
2178 void adjust (struct pfn_info *page, int dir, int adjtype)
2180 int count = page->count_info & PGC_count_mask;
2182 if ( adjtype )
2184 int tcount = page->u.inuse.type_info & PGT_count_mask;
2186 ttot++;
2188 tcount += dir;
2190 if ( tcount < 0 )
2192 /* This will only come out once. */
2193 printk("Audit %d: type count whent below zero pfn=%x "
2194 "taf=%x otaf=%x\n",
2195 d->id, page-frame_table,
2196 page->u.inuse.type_info,
2197 page->tlbflush_timestamp);
2200 page->u.inuse.type_info =
2201 (page->u.inuse.type_info & ~PGT_count_mask) |
2202 (tcount & PGT_count_mask);
2205 ctot++;
2206 count += dir;
2207 if ( count < 0 )
2209 /* This will only come out once. */
2210 printk("Audit %d: general count whent below zero pfn=%x "
2211 "taf=%x otaf=%x\n",
2212 d->id, page-frame_table,
2213 page->u.inuse.type_info,
2214 page->tlbflush_timestamp);
2217 page->count_info =
2218 (page->count_info & ~PGC_count_mask) |
2219 (count & PGC_count_mask);
2223 void scan_for_pfn(struct domain *d, unsigned long xpfn)
2225 unsigned long pfn, *pt;
2226 struct list_head *list_ent;
2227 struct pfn_info *page;
2228 int i;
2230 list_ent = d->page_list.next;
2231 for ( i = 0; (list_ent != &d->page_list); i++ )
2233 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2234 page = &frame_table[pfn];
2236 switch ( page->u.inuse.type_info & PGT_type_mask )
2238 case PGT_l1_page_table:
2239 case PGT_l2_page_table:
2240 pt = map_domain_mem(pfn<<PAGE_SHIFT);
2241 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2242 if ( (pt[i] & _PAGE_PRESENT) &&
2243 ((pt[i] >> PAGE_SHIFT) == xpfn) )
2244 printk(" found dom=%d i=%x pfn=%lx t=%x c=%x\n",
2245 d->id, i, pfn, page->u.inuse.type_info,
2246 page->count_info);
2247 unmap_domain_mem(pt);
2250 list_ent = frame_table[pfn].list.next;
2255 void scan_for_pfn_remote(unsigned long xpfn)
2257 struct domain *e;
2258 for_each_domain ( e )
2259 scan_for_pfn( e, xpfn );
2262 int i;
2263 unsigned long pfn;
2264 struct list_head *list_ent;
2265 struct pfn_info *page;
2267 if ( d != current->domain )
2268 domain_pause(d);
2269 synchronise_pagetables(~0UL);
2271 printk("pt base=%lx sh_info=%x\n",
2272 pagetable_val(d->exec_domain[0]->arch.pagetable)>>PAGE_SHIFT,
2273 virt_to_page(d->shared_info)-frame_table);
2275 spin_lock(&d->page_alloc_lock);
2277 /* PHASE 0 */
2279 list_ent = d->page_list.next;
2280 for ( i = 0; (list_ent != &d->page_list); i++ )
2282 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2283 page = &frame_table[pfn];
2285 if ( page_get_owner(page) != d )
2286 BUG();
2288 if ( (page->u.inuse.type_info & PGT_count_mask) >
2289 (page->count_info & PGC_count_mask) )
2290 printk("taf > caf %x %x pfn=%lx\n",
2291 page->u.inuse.type_info, page->count_info, pfn );
2293 #if 0 /* SYSV shared memory pages plus writeable files. */
2294 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page &&
2295 (page->u.inuse.type_info & PGT_count_mask) > 1 )
2297 printk("writeable page with type count >1: pfn=%lx t=%x c=%x\n",
2298 pfn,
2299 page->u.inuse.type_info,
2300 page->count_info );
2301 scan_for_pfn_remote(pfn);
2303 #endif
2304 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_none &&
2305 (page->u.inuse.type_info & PGT_count_mask) > 1 )
2307 printk("normal page with type count >1: pfn=%lx t=%x c=%x\n",
2308 pfn,
2309 page->u.inuse.type_info,
2310 page->count_info );
2313 /* Use tlbflush_timestamp to store original type_info. */
2314 page->tlbflush_timestamp = page->u.inuse.type_info;
2316 list_ent = frame_table[pfn].list.next;
2320 /* PHASE 1 */
2322 adjust(&frame_table[pagetable_val(d->exec_domain[0]->arch.pagetable)>>PAGE_SHIFT], -1, 1);
2324 list_ent = d->page_list.next;
2325 for ( i = 0; (list_ent != &d->page_list); i++ )
2327 unsigned long *pt;
2328 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2329 page = &frame_table[pfn];
2331 if ( page_get_owner(page) != d )
2332 BUG();
2334 switch ( page->u.inuse.type_info & PGT_type_mask )
2336 case PGT_l2_page_table:
2338 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2339 printk("Audit %d: L2 not validated %x\n",
2340 d->id, page->u.inuse.type_info);
2342 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2343 printk("Audit %d: L2 not pinned %x\n",
2344 d->id, page->u.inuse.type_info);
2345 else
2346 adjust( page, -1, 1 );
2348 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2350 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2352 if ( pt[i] & _PAGE_PRESENT )
2354 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2355 struct pfn_info *l1page = &frame_table[l1pfn];
2357 if ( page_get_owner(l1page) != d )
2359 printk("L2: Skip bizarre page belonging to other "
2360 "dom %p\n", page_get_owner(l1page));
2361 continue;
2364 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2365 PGT_l2_page_table )
2366 printk("Audit %d: [%x] Found %s Linear PT "
2367 "t=%x pfn=%lx\n", d->id, i,
2368 (l1pfn==pfn) ? "Self" : "Other",
2369 l1page->u.inuse.type_info,
2370 l1pfn);
2371 else if ( (l1page->u.inuse.type_info & PGT_type_mask) !=
2372 PGT_l1_page_table )
2373 printk("Audit %d: [%x] Expected L1 t=%x pfn=%lx\n",
2374 d->id, i,
2375 l1page->u.inuse.type_info,
2376 l1pfn);
2378 adjust(l1page, -1, 1);
2382 unmap_domain_mem(pt);
2384 break;
2387 case PGT_l1_page_table:
2389 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2390 adjust( page, -1, 1 );
2392 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2393 printk("Audit %d: L1 not validated %x\n",
2394 d->id, page->u.inuse.type_info);
2395 #if 0
2396 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2397 printk("Audit %d: L1 not pinned %x\n",
2398 d->id, page->u.inuse.type_info);
2399 #endif
2400 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2402 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2404 if ( pt[i] & _PAGE_PRESENT )
2406 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2407 struct pfn_info *l1page = &frame_table[l1pfn];
2409 if ( l1pfn < 0x100 )
2411 lowmem_mappings++;
2412 continue;
2415 if ( l1pfn > max_page )
2417 io_mappings++;
2418 continue;
2421 if ( pt[i] & _PAGE_RW )
2424 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2425 PGT_l1_page_table ||
2426 (l1page->u.inuse.type_info & PGT_type_mask) ==
2427 PGT_l2_page_table )
2428 printk("Audit %d: [%x] Ilegal RW t=%x pfn=%lx\n",
2429 d->id, i,
2430 l1page->u.inuse.type_info,
2431 l1pfn);
2435 if ( page_get_owner(l1page) != d )
2437 printk("Audit %d: [%lx,%x] Skip foreign page dom=%p "
2438 "pfn=%lx c=%08x t=%08x m2p=%lx\n",
2439 d->id, pfn, i,
2440 page_get_owner(l1page),
2441 l1pfn,
2442 l1page->count_info,
2443 l1page->u.inuse.type_info,
2444 machine_to_phys_mapping[l1pfn]);
2445 continue;
2448 adjust(l1page, -1, 0);
2452 unmap_domain_mem(pt);
2454 break;
2457 list_ent = frame_table[pfn].list.next;
2460 if ( (io_mappings > 0) || (lowmem_mappings > 0) )
2461 printk("Audit %d: Found %d lowmem mappings and %d io mappings\n",
2462 d->id, lowmem_mappings, io_mappings);
2464 /* PHASE 2 */
2466 ctot = ttot = 0;
2467 list_ent = d->page_list.next;
2468 for ( i = 0; (list_ent != &d->page_list); i++ )
2470 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2471 page = &frame_table[pfn];
2473 switch ( page->u.inuse.type_info & PGT_type_mask)
2475 case PGT_l1_page_table:
2476 case PGT_l2_page_table:
2477 if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
2479 printk("Audit %d: type count!=0 t=%x ot=%x c=%x pfn=%lx\n",
2480 d->id, page->u.inuse.type_info,
2481 page->tlbflush_timestamp,
2482 page->count_info, pfn );
2483 scan_for_pfn_remote(pfn);
2485 default:
2486 if ( (page->count_info & PGC_count_mask) != 1 )
2488 printk("Audit %d: gen count!=1 (c=%x) t=%x ot=%x pfn=%lx\n",
2489 d->id,
2490 page->count_info,
2491 page->u.inuse.type_info,
2492 page->tlbflush_timestamp, pfn );
2493 scan_for_pfn_remote(pfn);
2495 break;
2498 list_ent = frame_table[pfn].list.next;
2501 /* PHASE 3 */
2502 list_ent = d->page_list.next;
2503 for ( i = 0; (list_ent != &d->page_list); i++ )
2505 unsigned long *pt;
2506 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2507 page = &frame_table[pfn];
2509 switch ( page->u.inuse.type_info & PGT_type_mask )
2511 case PGT_l2_page_table:
2512 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2513 adjust( page, 1, 1 );
2515 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2517 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2519 if ( pt[i] & _PAGE_PRESENT )
2521 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2522 struct pfn_info *l1page;
2524 if (l1pfn>max_page)
2525 continue;
2527 l1page = &frame_table[l1pfn];
2529 if ( page_get_owner(l1page) == d )
2530 adjust(l1page, 1, 1);
2534 unmap_domain_mem(pt);
2535 break;
2537 case PGT_l1_page_table:
2538 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2539 adjust( page, 1, 1 );
2541 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2543 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2545 if ( pt[i] & _PAGE_PRESENT )
2547 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2548 struct pfn_info *l1page;
2550 if (l1pfn>max_page)
2551 continue;
2553 l1page = &frame_table[l1pfn];
2555 if ( (page_get_owner(l1page) != d) ||
2556 (l1pfn < 0x100) || (l1pfn > max_page) )
2557 continue;
2559 adjust(l1page, 1, 0);
2563 unmap_domain_mem(pt);
2564 break;
2568 page->tlbflush_timestamp = 0;
2570 list_ent = frame_table[pfn].list.next;
2573 spin_unlock(&d->page_alloc_lock);
2575 adjust(&frame_table[pagetable_val(
2576 d->exec_domain[0]->arch.pagetable)>>PAGE_SHIFT], 1, 1);
2578 printk("Audit %d: Done. ctot=%d ttot=%d\n", d->id, ctot, ttot );
2580 if ( d != current->domain )
2581 domain_unpause(d);
2584 void audit_domains(void)
2586 struct domain *d;
2587 for_each_domain ( d )
2588 audit_domain(d);
2591 void audit_domains_key(unsigned char key)
2593 audit_domains();
2596 #endif