ia64/xen-unstable

view xen/arch/x86/memory.c @ 3746:73484d0fa776

bitkeeper revision 1.1159.223.82 (42093f8cxZb9FQfWwmj7IIok9ndiPg)

minor stats collection addition
ian@xensource.com
author iap10@labyrinth.cl.cam.ac.uk
date Tue Feb 08 22:39:08 2005 +0000 (2005-02-08)
parents 6062bb54a227
children fb3675bdcce5 58be428f51a8
line source
1 /******************************************************************************
2 * arch/x86/memory.c
3 *
4 * Copyright (c) 2002-2004 K A Fraser
5 * Copyright (c) 2004 Christian Limpach
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
22 /*
23 * A description of the x86 page table API:
24 *
25 * Domains trap to do_mmu_update with a list of update requests.
26 * This is a list of (ptr, val) pairs, where the requested operation
27 * is *ptr = val.
28 *
29 * Reference counting of pages:
30 * ----------------------------
31 * Each page has two refcounts: tot_count and type_count.
32 *
33 * TOT_COUNT is the obvious reference count. It counts all uses of a
34 * physical page frame by a domain, including uses as a page directory,
35 * a page table, or simple mappings via a PTE. This count prevents a
36 * domain from releasing a frame back to the free pool when it still holds
37 * a reference to it.
38 *
39 * TYPE_COUNT is more subtle. A frame can be put to one of three
40 * mutually-exclusive uses: it might be used as a page directory, or a
41 * page table, or it may be mapped writable by the domain [of course, a
42 * frame may not be used in any of these three ways!].
43 * So, type_count is a count of the number of times a frame is being
44 * referred to in its current incarnation. Therefore, a page can only
45 * change its type when its type count is zero.
46 *
47 * Pinning the page type:
48 * ----------------------
49 * The type of a page can be pinned/unpinned with the commands
50 * MMUEXT_[UN]PIN_L?_TABLE. Each page can be pinned exactly once (that is,
51 * pinning is not reference counted, so it can't be nested).
52 * This is useful to prevent a page's type count falling to zero, at which
53 * point safety checks would need to be carried out next time the count
54 * is increased again.
55 *
56 * A further note on writable page mappings:
57 * -----------------------------------------
58 * For simplicity, the count of writable mappings for a page may not
59 * correspond to reality. The 'writable count' is incremented for every
60 * PTE which maps the page with the _PAGE_RW flag set. However, for
61 * write access to be possible the page directory entry must also have
62 * its _PAGE_RW bit set. We do not check this as it complicates the
63 * reference counting considerably [consider the case of multiple
64 * directory entries referencing a single page table, some with the RW
65 * bit set, others not -- it starts getting a bit messy].
66 * In normal use, this simplification shouldn't be a problem.
67 * However, the logic can be added if required.
68 *
69 * One more note on read-only page mappings:
70 * -----------------------------------------
71 * We want domains to be able to map pages for read-only access. The
72 * main reason is that page tables and directories should be readable
73 * by a domain, but it would not be safe for them to be writable.
74 * However, domains have free access to rings 1 & 2 of the Intel
75 * privilege model. In terms of page protection, these are considered
76 * to be part of 'supervisor mode'. The WP bit in CR0 controls whether
77 * read-only restrictions are respected in supervisor mode -- if the
78 * bit is clear then any mapped page is writable.
79 *
80 * We get round this by always setting the WP bit and disallowing
81 * updates to it. This is very unlikely to cause a problem for guest
82 * OS's, which will generally use the WP bit to simplify copy-on-write
83 * implementation (in that case, OS wants a fault when it writes to
84 * an application-supplied buffer).
85 */
87 #include <xen/config.h>
88 #include <xen/init.h>
89 #include <xen/kernel.h>
90 #include <xen/lib.h>
91 #include <xen/mm.h>
92 #include <xen/sched.h>
93 #include <xen/errno.h>
94 #include <xen/perfc.h>
95 #include <xen/irq.h>
96 #include <xen/softirq.h>
97 #include <asm/shadow.h>
98 #include <asm/page.h>
99 #include <asm/flushtlb.h>
100 #include <asm/io.h>
101 #include <asm/uaccess.h>
102 #include <asm/domain_page.h>
103 #include <asm/ldt.h>
105 #ifdef VERBOSE
106 #define MEM_LOG(_f, _a...) \
107 printk("DOM%u: (file=memory.c, line=%d) " _f "\n", \
108 current->id , __LINE__ , ## _a )
109 #else
110 #define MEM_LOG(_f, _a...) ((void)0)
111 #endif
113 static int alloc_l2_table(struct pfn_info *page);
114 static int alloc_l1_table(struct pfn_info *page);
115 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d);
116 static int get_page_and_type_from_pagenr(unsigned long page_nr,
117 u32 type,
118 struct domain *d);
120 static void free_l2_table(struct pfn_info *page);
121 static void free_l1_table(struct pfn_info *page);
123 static int mod_l2_entry(l2_pgentry_t *, l2_pgentry_t, unsigned long);
124 static int mod_l1_entry(l1_pgentry_t *, l1_pgentry_t);
126 /* Used to defer flushing of memory structures. */
127 static struct {
128 #define DOP_FLUSH_TLB (1<<0) /* Flush the TLB. */
129 #define DOP_RELOAD_LDT (1<<1) /* Reload the LDT shadow mapping. */
130 unsigned long deferred_ops;
131 /* If non-NULL, specifies a foreign subject domain for some operations. */
132 struct domain *foreign;
133 } __cacheline_aligned percpu_info[NR_CPUS];
135 /*
136 * Returns the current foreign domain; defaults to the currently-executing
137 * domain if a foreign override hasn't been specified.
138 */
139 #define FOREIGNDOM (percpu_info[smp_processor_id()].foreign ? : current)
141 /* Private domain structs for DOMID_XEN and DOMID_IO. */
142 static struct domain *dom_xen, *dom_io;
144 /* Frame table and its size in pages. */
145 struct pfn_info *frame_table;
146 unsigned long frame_table_size;
147 unsigned long max_page;
149 void __init init_frametable(void)
150 {
151 unsigned long i, p;
153 frame_table = (struct pfn_info *)FRAMETABLE_VIRT_START;
154 frame_table_size = max_page * sizeof(struct pfn_info);
155 frame_table_size = (frame_table_size + PAGE_SIZE - 1) & PAGE_MASK;
157 for ( i = 0; i < frame_table_size; i += (4UL << 20) )
158 {
159 p = alloc_boot_pages(min(frame_table_size - i, 4UL << 20), 4UL << 20);
160 if ( p == 0 )
161 panic("Not enough memory for frame table\n");
162 idle_pg_table[(FRAMETABLE_VIRT_START + i) >> L2_PAGETABLE_SHIFT] =
163 mk_l2_pgentry(p | __PAGE_HYPERVISOR | _PAGE_PSE);
164 }
166 memset(frame_table, 0, frame_table_size);
167 }
169 void arch_init_memory(void)
170 {
171 unsigned long i;
173 /*
174 * We are rather picky about the layout of 'struct pfn_info'. The
175 * count_info and domain fields must be adjacent, as we perform atomic
176 * 64-bit operations on them. Also, just for sanity, we assert the size
177 * of the structure here.
178 */
179 if ( (offsetof(struct pfn_info, u.inuse.domain) !=
180 (offsetof(struct pfn_info, count_info) + sizeof(u32))) ||
181 (sizeof(struct pfn_info) != 24) )
182 {
183 printk("Weird pfn_info layout (%ld,%ld,%d)\n",
184 offsetof(struct pfn_info, count_info),
185 offsetof(struct pfn_info, u.inuse.domain),
186 sizeof(struct pfn_info));
187 for ( ; ; ) ;
188 }
190 memset(percpu_info, 0, sizeof(percpu_info));
192 /* Initialise to a magic of 0x55555555 so easier to spot bugs later. */
193 memset(machine_to_phys_mapping, 0x55, 4<<20);
195 /*
196 * Initialise our DOMID_XEN domain.
197 * Any Xen-heap pages that we will allow to be mapped will have
198 * their domain field set to dom_xen.
199 */
200 dom_xen = alloc_domain_struct();
201 atomic_set(&dom_xen->refcnt, 1);
202 dom_xen->id = DOMID_XEN;
204 /*
205 * Initialise our DOMID_IO domain.
206 * This domain owns no pages but is considered a special case when
207 * mapping I/O pages, as the mappings occur at the priv of the caller.
208 */
209 dom_io = alloc_domain_struct();
210 atomic_set(&dom_io->refcnt, 1);
211 dom_io->id = DOMID_IO;
213 /* M2P table is mappable read-only by privileged domains. */
214 for ( i = 0; i < 1024; i++ )
215 {
216 frame_table[m2p_start_mfn+i].count_info = PGC_allocated | 1;
217 /* gdt to make sure it's only mapped read-only by non-privileged
218 domains. */
219 frame_table[m2p_start_mfn+i].u.inuse.type_info = PGT_gdt_page | 1;
220 frame_table[m2p_start_mfn+i].u.inuse.domain = dom_xen;
221 }
222 }
224 static void __invalidate_shadow_ldt(struct domain *d)
225 {
226 int i;
227 unsigned long pfn;
228 struct pfn_info *page;
230 d->mm.shadow_ldt_mapcnt = 0;
232 for ( i = 16; i < 32; i++ )
233 {
234 pfn = l1_pgentry_to_pagenr(d->mm.perdomain_pt[i]);
235 if ( pfn == 0 ) continue;
236 d->mm.perdomain_pt[i] = mk_l1_pgentry(0);
237 page = &frame_table[pfn];
238 ASSERT_PAGE_IS_TYPE(page, PGT_ldt_page);
239 ASSERT_PAGE_IS_DOMAIN(page, d);
240 put_page_and_type(page);
241 }
243 /* Dispose of the (now possibly invalid) mappings from the TLB. */
244 percpu_info[d->processor].deferred_ops |= DOP_FLUSH_TLB | DOP_RELOAD_LDT;
245 }
248 static inline void invalidate_shadow_ldt(struct domain *d)
249 {
250 if ( d->mm.shadow_ldt_mapcnt != 0 )
251 __invalidate_shadow_ldt(d);
252 }
255 static int alloc_segdesc_page(struct pfn_info *page)
256 {
257 unsigned long *descs = map_domain_mem((page-frame_table) << PAGE_SHIFT);
258 int i;
260 for ( i = 0; i < 512; i++ )
261 if ( unlikely(!check_descriptor(&descs[i*2])) )
262 goto fail;
264 unmap_domain_mem(descs);
265 return 1;
267 fail:
268 unmap_domain_mem(descs);
269 return 0;
270 }
273 /* Map shadow page at offset @off. */
274 int map_ldt_shadow_page(unsigned int off)
275 {
276 struct domain *d = current;
277 unsigned long l1e;
279 if ( unlikely(in_irq()) )
280 BUG();
282 __get_user(l1e, (unsigned long *)&linear_pg_table[(d->mm.ldt_base >>
283 PAGE_SHIFT) + off]);
285 if ( unlikely(!(l1e & _PAGE_PRESENT)) ||
286 unlikely(!get_page_and_type(&frame_table[l1e >> PAGE_SHIFT],
287 d, PGT_ldt_page)) )
288 return 0;
290 d->mm.perdomain_pt[off + 16] = mk_l1_pgentry(l1e | _PAGE_RW);
291 d->mm.shadow_ldt_mapcnt++;
293 return 1;
294 }
297 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d)
298 {
299 struct pfn_info *page = &frame_table[page_nr];
301 if ( unlikely(!pfn_is_ram(page_nr)) )
302 {
303 MEM_LOG("Pfn %08lx is not RAM", page_nr);
304 return 0;
305 }
307 if ( unlikely(!get_page(page, d)) )
308 {
309 MEM_LOG("Could not get page ref for pfn %08lx", page_nr);
310 return 0;
311 }
313 return 1;
314 }
317 static int get_page_and_type_from_pagenr(unsigned long page_nr,
318 u32 type,
319 struct domain *d)
320 {
321 struct pfn_info *page = &frame_table[page_nr];
323 if ( unlikely(!get_page_from_pagenr(page_nr, d)) )
324 return 0;
326 if ( unlikely(!get_page_type(page, type)) )
327 {
328 #ifdef VERBOSE
329 if ( (type & PGT_type_mask) != PGT_l1_page_table )
330 MEM_LOG("Bad page type for pfn %08lx (%08x)",
331 page_nr, page->u.inuse.type_info);
332 #endif
333 put_page(page);
334 return 0;
335 }
337 return 1;
338 }
341 /*
342 * We allow an L2 tables to map each other (a.k.a. linear page tables). It
343 * needs some special care with reference counst and access permissions:
344 * 1. The mapping entry must be read-only, or the guest may get write access
345 * to its own PTEs.
346 * 2. We must only bump the reference counts for an *already validated*
347 * L2 table, or we can end up in a deadlock in get_page_type() by waiting
348 * on a validation that is required to complete that validation.
349 * 3. We only need to increment the reference counts for the mapped page
350 * frame if it is mapped by a different L2 table. This is sufficient and
351 * also necessary to allow validation of an L2 table mapping itself.
352 */
353 static int
354 get_linear_pagetable(
355 l2_pgentry_t l2e, unsigned long pfn, struct domain *d)
356 {
357 u32 x, y;
358 struct pfn_info *page;
360 if ( (l2_pgentry_val(l2e) & _PAGE_RW) )
361 {
362 MEM_LOG("Attempt to create linear p.t. with write perms");
363 return 0;
364 }
366 if ( (l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn )
367 {
368 /* Make sure the mapped frame belongs to the correct domain. */
369 if ( unlikely(!get_page_from_pagenr(l2_pgentry_to_pagenr(l2e), d)) )
370 return 0;
372 /*
373 * Make sure that the mapped frame is an already-validated L2 table.
374 * If so, atomically increment the count (checking for overflow).
375 */
376 page = &frame_table[l2_pgentry_to_pagenr(l2e)];
377 y = page->u.inuse.type_info;
378 do {
379 x = y;
380 if ( unlikely((x & PGT_count_mask) == PGT_count_mask) ||
381 unlikely((x & (PGT_type_mask|PGT_validated)) !=
382 (PGT_l2_page_table|PGT_validated)) )
383 {
384 put_page(page);
385 return 0;
386 }
387 }
388 while ( (y = cmpxchg(&page->u.inuse.type_info, x, x + 1)) != x );
389 }
391 return 1;
392 }
395 static int
396 get_page_from_l1e(
397 l1_pgentry_t l1e, struct domain *d)
398 {
399 unsigned long l1v = l1_pgentry_val(l1e);
400 unsigned long pfn = l1_pgentry_to_pagenr(l1e);
401 struct pfn_info *page = &frame_table[pfn];
402 extern int domain_iomem_in_pfn(struct domain *d, unsigned long pfn);
404 if ( !(l1v & _PAGE_PRESENT) )
405 return 1;
407 if ( unlikely(l1v & (_PAGE_GLOBAL|_PAGE_PAT)) )
408 {
409 MEM_LOG("Bad L1 type settings %04lx", l1v & (_PAGE_GLOBAL|_PAGE_PAT));
410 return 0;
411 }
413 if ( unlikely(!pfn_is_ram(pfn)) )
414 {
415 /* Revert to caller privileges if FD == DOMID_IO. */
416 if ( d == dom_io )
417 d = current;
419 if ( IS_PRIV(d) )
420 return 1;
422 if ( IS_CAPABLE_PHYSDEV(d) )
423 return domain_iomem_in_pfn(d, pfn);
425 MEM_LOG("Non-privileged attempt to map I/O space %08lx", pfn);
426 return 0;
427 }
429 return ((l1v & _PAGE_RW) ?
430 get_page_and_type(page, d, PGT_writable_page) :
431 get_page(page, d));
432 }
435 /* NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'. */
436 static int
437 get_page_from_l2e(
438 l2_pgentry_t l2e, unsigned long pfn,
439 struct domain *d, unsigned long va_idx)
440 {
441 int rc;
443 if ( !(l2_pgentry_val(l2e) & _PAGE_PRESENT) )
444 return 1;
446 if ( unlikely((l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE))) )
447 {
448 MEM_LOG("Bad L2 page type settings %04lx",
449 l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE));
450 return 0;
451 }
453 rc = get_page_and_type_from_pagenr(
454 l2_pgentry_to_pagenr(l2e),
455 PGT_l1_page_table | (va_idx<<PGT_va_shift), d);
457 if ( unlikely(!rc) )
458 return get_linear_pagetable(l2e, pfn, d);
460 return 1;
461 }
464 static void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
465 {
466 unsigned long l1v = l1_pgentry_val(l1e);
467 unsigned long pfn = l1_pgentry_to_pagenr(l1e);
468 struct pfn_info *page = &frame_table[pfn];
469 struct domain *e;
471 if ( !(l1v & _PAGE_PRESENT) || !pfn_is_ram(pfn) )
472 return;
474 e = page->u.inuse.domain;
475 if ( unlikely(e != d) )
476 {
477 /*
478 * Unmap a foreign page that may have been mapped via a grant table.
479 * Note that this can fail for a privileged domain that can map foreign
480 * pages via MMUEXT_SET_FOREIGNDOM. Such domains can have some mappings
481 * counted via a grant entry and some counted directly in the page
482 * structure's reference count. Note that reference counts won't get
483 * dangerously confused as long as we always try to decrement the
484 * grant entry first. We may end up with a mismatch between which
485 * mappings and which unmappings are counted via the grant entry, but
486 * really it doesn't matter as privileged domains have carte blanche.
487 */
488 if ( likely(gnttab_check_unmap(e, d, pfn, !(l1v & _PAGE_RW))) )
489 return;
490 /* Assume this mapping was made via MMUEXT_SET_FOREIGNDOM... */
491 }
493 if ( l1v & _PAGE_RW )
494 {
495 put_page_and_type(page);
496 }
497 else
498 {
499 /* We expect this is rare so we blow the entire shadow LDT. */
500 if ( unlikely(((page->u.inuse.type_info & PGT_type_mask) ==
501 PGT_ldt_page)) &&
502 unlikely(((page->u.inuse.type_info & PGT_count_mask) != 0)) )
503 invalidate_shadow_ldt(e);
504 put_page(page);
505 }
506 }
509 /*
510 * NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'.
511 * Note also that this automatically deals correctly with linear p.t.'s.
512 */
513 static void put_page_from_l2e(l2_pgentry_t l2e, unsigned long pfn)
514 {
515 if ( (l2_pgentry_val(l2e) & _PAGE_PRESENT) &&
516 ((l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn) )
517 put_page_and_type(&frame_table[l2_pgentry_to_pagenr(l2e)]);
518 }
521 static int alloc_l2_table(struct pfn_info *page)
522 {
523 struct domain *d = page->u.inuse.domain;
524 unsigned long page_nr = page_to_pfn(page);
525 l2_pgentry_t *pl2e;
526 int i;
528 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
530 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
531 if ( unlikely(!get_page_from_l2e(pl2e[i], page_nr, d, i)) )
532 goto fail;
534 #if defined(__i386__)
535 /* Now we add our private high mappings. */
536 memcpy(&pl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
537 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
538 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
539 pl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
540 mk_l2_pgentry((page_nr << PAGE_SHIFT) | __PAGE_HYPERVISOR);
541 pl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
542 mk_l2_pgentry(__pa(page->u.inuse.domain->mm.perdomain_pt) |
543 __PAGE_HYPERVISOR);
544 #endif
546 unmap_domain_mem(pl2e);
547 return 1;
549 fail:
550 while ( i-- > 0 )
551 put_page_from_l2e(pl2e[i], page_nr);
553 unmap_domain_mem(pl2e);
554 return 0;
555 }
558 static int alloc_l1_table(struct pfn_info *page)
559 {
560 struct domain *d = page->u.inuse.domain;
561 unsigned long page_nr = page_to_pfn(page);
562 l1_pgentry_t *pl1e;
563 int i;
565 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
567 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
568 if ( unlikely(!get_page_from_l1e(pl1e[i], d)) )
569 goto fail;
571 unmap_domain_mem(pl1e);
572 return 1;
574 fail:
575 while ( i-- > 0 )
576 put_page_from_l1e(pl1e[i], d);
578 unmap_domain_mem(pl1e);
579 return 0;
580 }
583 static void free_l2_table(struct pfn_info *page)
584 {
585 unsigned long page_nr = page - frame_table;
586 l2_pgentry_t *pl2e;
587 int i;
589 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
591 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
592 put_page_from_l2e(pl2e[i], page_nr);
594 unmap_domain_mem(pl2e);
595 }
598 static void free_l1_table(struct pfn_info *page)
599 {
600 struct domain *d = page->u.inuse.domain;
601 unsigned long page_nr = page - frame_table;
602 l1_pgentry_t *pl1e;
603 int i;
605 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
607 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
608 put_page_from_l1e(pl1e[i], d);
610 unmap_domain_mem(pl1e);
611 }
614 static inline int update_l2e(l2_pgentry_t *pl2e,
615 l2_pgentry_t ol2e,
616 l2_pgentry_t nl2e)
617 {
618 unsigned long o = cmpxchg((unsigned long *)pl2e,
619 l2_pgentry_val(ol2e),
620 l2_pgentry_val(nl2e));
621 if ( o != l2_pgentry_val(ol2e) )
622 MEM_LOG("Failed to update %08lx -> %08lx: saw %08lx\n",
623 l2_pgentry_val(ol2e), l2_pgentry_val(nl2e), o);
624 return (o == l2_pgentry_val(ol2e));
625 }
628 /* Update the L2 entry at pl2e to new value nl2e. pl2e is within frame pfn. */
629 static int mod_l2_entry(l2_pgentry_t *pl2e,
630 l2_pgentry_t nl2e,
631 unsigned long pfn)
632 {
633 l2_pgentry_t ol2e;
634 unsigned long _ol2e;
636 if ( unlikely((((unsigned long)pl2e & (PAGE_SIZE-1)) >> 2) >=
637 DOMAIN_ENTRIES_PER_L2_PAGETABLE) )
638 {
639 MEM_LOG("Illegal L2 update attempt in Xen-private area %p", pl2e);
640 return 0;
641 }
643 if ( unlikely(__get_user(_ol2e, (unsigned long *)pl2e) != 0) )
644 return 0;
645 ol2e = mk_l2_pgentry(_ol2e);
647 if ( l2_pgentry_val(nl2e) & _PAGE_PRESENT )
648 {
649 /* Differ in mapping (bits 12-31) or presence (bit 0)? */
650 if ( ((l2_pgentry_val(ol2e) ^ l2_pgentry_val(nl2e)) & ~0xffe) == 0 )
651 return update_l2e(pl2e, ol2e, nl2e);
653 if ( unlikely(!get_page_from_l2e(nl2e, pfn, current,
654 ((unsigned long)pl2e &
655 ~PAGE_MASK) >> 2)) )
656 return 0;
658 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
659 {
660 put_page_from_l2e(nl2e, pfn);
661 return 0;
662 }
664 put_page_from_l2e(ol2e, pfn);
665 return 1;
666 }
668 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
669 return 0;
671 put_page_from_l2e(ol2e, pfn);
672 return 1;
673 }
676 static inline int update_l1e(l1_pgentry_t *pl1e,
677 l1_pgentry_t ol1e,
678 l1_pgentry_t nl1e)
679 {
680 unsigned long o = l1_pgentry_val(ol1e);
681 unsigned long n = l1_pgentry_val(nl1e);
683 if ( unlikely(cmpxchg_user(pl1e, o, n) != 0) ||
684 unlikely(o != l1_pgentry_val(ol1e)) )
685 {
686 MEM_LOG("Failed to update %08lx -> %08lx: saw %08lx\n",
687 l1_pgentry_val(ol1e), l1_pgentry_val(nl1e), o);
688 return 0;
689 }
691 return 1;
692 }
695 /* Update the L1 entry at pl1e to new value nl1e. */
696 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e)
697 {
698 l1_pgentry_t ol1e;
699 unsigned long _ol1e;
700 struct domain *d = current;
702 if ( unlikely(__get_user(_ol1e, (unsigned long *)pl1e) != 0) )
703 {
704 MEM_LOG("Bad get_user\n");
705 return 0;
706 }
708 ol1e = mk_l1_pgentry(_ol1e);
710 if ( l1_pgentry_val(nl1e) & _PAGE_PRESENT )
711 {
712 /* Differ in mapping (bits 12-31), r/w (bit 1), or presence (bit 0)? */
713 if ( ((l1_pgentry_val(ol1e) ^ l1_pgentry_val(nl1e)) & ~0xffc) == 0 )
714 return update_l1e(pl1e, ol1e, nl1e);
716 if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
717 return 0;
719 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
720 {
721 put_page_from_l1e(nl1e, d);
722 return 0;
723 }
725 put_page_from_l1e(ol1e, d);
726 return 1;
727 }
729 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
730 return 0;
732 put_page_from_l1e(ol1e, d);
733 return 1;
734 }
737 int alloc_page_type(struct pfn_info *page, unsigned int type)
738 {
739 switch ( type )
740 {
741 case PGT_l1_page_table:
742 return alloc_l1_table(page);
743 case PGT_l2_page_table:
744 return alloc_l2_table(page);
745 case PGT_gdt_page:
746 case PGT_ldt_page:
747 return alloc_segdesc_page(page);
748 default:
749 printk("Bad type in alloc_page_type %x t=%x c=%x\n",
750 type, page->u.inuse.type_info,
751 page->count_info);
752 BUG();
753 }
755 return 0;
756 }
759 void free_page_type(struct pfn_info *page, unsigned int type)
760 {
761 struct domain *d = page->u.inuse.domain;
763 switch ( type )
764 {
765 case PGT_l1_page_table:
766 free_l1_table(page);
767 break;
769 case PGT_l2_page_table:
770 free_l2_table(page);
771 break;
773 default:
774 BUG();
775 }
777 if ( unlikely(d->mm.shadow_mode) &&
778 (get_shadow_status(&d->mm, page_to_pfn(page)) & PSH_shadowed) )
779 {
780 unshadow_table(page_to_pfn(page), type);
781 put_shadow_status(&d->mm);
782 }
783 }
786 void put_page_type(struct pfn_info *page)
787 {
788 u32 nx, x, y = page->u.inuse.type_info;
790 again:
791 do {
792 x = y;
793 nx = x - 1;
795 ASSERT((x & PGT_count_mask) != 0);
797 /*
798 * The page should always be validated while a reference is held. The
799 * exception is during domain destruction, when we forcibly invalidate
800 * page-table pages if we detect a referential loop.
801 * See domain.c:relinquish_list().
802 */
803 ASSERT((x & PGT_validated) ||
804 test_bit(DF_DYING, &page->u.inuse.domain->flags));
806 if ( unlikely((nx & PGT_count_mask) == 0) )
807 {
808 /* Record TLB information for flush later. Races are harmless. */
809 page->tlbflush_timestamp = tlbflush_current_time();
811 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
812 likely(nx & PGT_validated) )
813 {
814 /*
815 * Page-table pages must be unvalidated when count is zero. The
816 * 'free' is safe because the refcnt is non-zero and validated
817 * bit is clear => other ops will spin or fail.
818 */
819 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
820 x & ~PGT_validated)) != x) )
821 goto again;
822 /* We cleared the 'valid bit' so we do the clear up. */
823 free_page_type(page, x & PGT_type_mask);
824 /* Carry on, but with the 'valid bit' now clear. */
825 x &= ~PGT_validated;
826 nx &= ~PGT_validated;
827 }
828 }
829 else if ( unlikely((nx & (PGT_pinned | PGT_count_mask)) ==
830 (PGT_pinned | 1)) )
831 {
832 /* Page is now only pinned. Make the back pointer mutable again. */
833 nx |= PGT_va_mutable;
834 }
835 }
836 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
837 }
840 int get_page_type(struct pfn_info *page, u32 type)
841 {
842 u32 nx, x, y = page->u.inuse.type_info;
844 again:
845 do {
846 x = y;
847 nx = x + 1;
848 if ( unlikely((nx & PGT_count_mask) == 0) )
849 {
850 MEM_LOG("Type count overflow on pfn %08lx\n", page_to_pfn(page));
851 return 0;
852 }
853 else if ( unlikely((x & PGT_count_mask) == 0) )
854 {
855 if ( (x & (PGT_type_mask|PGT_va_mask)) != type )
856 {
857 /*
858 * On type change we check to flush stale TLB entries. This
859 * may be unnecessary (e.g., page was GDT/LDT) but those
860 * circumstances should be very rare.
861 */
862 struct domain *d = page->u.inuse.domain;
863 if ( unlikely(NEED_FLUSH(tlbflush_time[d->processor],
864 page->tlbflush_timestamp)) )
865 {
866 perfc_incr(need_flush_tlb_flush);
867 flush_tlb_cpu(d->processor);
868 }
870 /* We lose existing type, back pointer, and validity. */
871 nx &= ~(PGT_type_mask | PGT_va_mask | PGT_validated);
872 nx |= type;
874 /* No special validation needed for writable pages. */
875 /* Page tables and GDT/LDT need to be scanned for validity. */
876 if ( type == PGT_writable_page )
877 nx |= PGT_validated;
878 }
879 }
880 else if ( unlikely((x & (PGT_type_mask|PGT_va_mask)) != type) )
881 {
882 if ( unlikely((x & PGT_type_mask) != (type & PGT_type_mask) ) )
883 {
884 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
885 ((type & PGT_type_mask) != PGT_l1_page_table) )
886 MEM_LOG("Bad type (saw %08x != exp %08x) for pfn %08lx\n",
887 x & PGT_type_mask, type, page_to_pfn(page));
888 return 0;
889 }
890 else if ( (x & PGT_va_mask) == PGT_va_mutable )
891 {
892 /* The va backpointer is mutable, hence we update it. */
893 nx &= ~PGT_va_mask;
894 nx |= type; /* we know the actual type is correct */
895 }
896 else if ( unlikely((x & PGT_va_mask) != (type & PGT_va_mask)) )
897 {
898 /* This table is potentially mapped at multiple locations. */
899 nx &= ~PGT_va_mask;
900 nx |= PGT_va_unknown;
901 }
902 }
903 else if ( unlikely(!(x & PGT_validated)) )
904 {
905 /* Someone else is updating validation of this page. Wait... */
906 while ( (y = page->u.inuse.type_info) == x )
907 {
908 rep_nop();
909 barrier();
910 }
911 goto again;
912 }
913 }
914 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
916 if ( unlikely(!(nx & PGT_validated)) )
917 {
918 /* Try to validate page type; drop the new reference on failure. */
919 if ( unlikely(!alloc_page_type(page, type & PGT_type_mask)) )
920 {
921 MEM_LOG("Error while validating pfn %08lx for type %08x."
922 " caf=%08x taf=%08x\n",
923 page_to_pfn(page), type,
924 page->count_info,
925 page->u.inuse.type_info);
926 /* Noone else can get a reference. We hold the only ref. */
927 page->u.inuse.type_info = 0;
928 return 0;
929 }
931 /* Noone else is updating simultaneously. */
932 __set_bit(_PGT_validated, &page->u.inuse.type_info);
933 }
935 return 1;
936 }
939 static int do_extended_command(unsigned long ptr, unsigned long val)
940 {
941 int okay = 1, cpu = smp_processor_id();
942 unsigned int cmd = val & MMUEXT_CMD_MASK;
943 unsigned long pfn = ptr >> PAGE_SHIFT;
944 unsigned long old_base_pfn;
945 struct pfn_info *page = &frame_table[pfn];
946 struct domain *d = current, *nd, *e;
947 u32 x, y;
948 domid_t domid;
949 grant_ref_t gntref;
951 switch ( cmd )
952 {
953 case MMUEXT_PIN_L1_TABLE:
954 case MMUEXT_PIN_L2_TABLE:
955 /*
956 * We insist that, if you pin an L1 page, it's the first thing that
957 * you do to it. This is because we require the backptr to still be
958 * mutable. This assumption seems safe.
959 */
960 okay = get_page_and_type_from_pagenr(
961 pfn,
962 ((cmd==MMUEXT_PIN_L2_TABLE) ?
963 PGT_l2_page_table : (PGT_l1_page_table|PGT_va_mutable)),
964 FOREIGNDOM);
966 if ( unlikely(!okay) )
967 {
968 MEM_LOG("Error while pinning pfn %08lx", pfn);
969 break;
970 }
972 if ( unlikely(test_and_set_bit(_PGT_pinned,
973 &page->u.inuse.type_info)) )
974 {
975 MEM_LOG("Pfn %08lx already pinned", pfn);
976 put_page_and_type(page);
977 okay = 0;
978 break;
979 }
981 break;
983 case MMUEXT_UNPIN_TABLE:
984 if ( unlikely(!(okay = get_page_from_pagenr(pfn, FOREIGNDOM))) )
985 {
986 MEM_LOG("Page %08lx bad domain (dom=%p)",
987 ptr, page->u.inuse.domain);
988 }
989 else if ( likely(test_and_clear_bit(_PGT_pinned,
990 &page->u.inuse.type_info)) )
991 {
992 put_page_and_type(page);
993 put_page(page);
994 }
995 else
996 {
997 okay = 0;
998 put_page(page);
999 MEM_LOG("Pfn %08lx not pinned", pfn);
1001 break;
1003 case MMUEXT_NEW_BASEPTR:
1004 okay = get_page_and_type_from_pagenr(pfn, PGT_l2_page_table, d);
1005 if ( likely(okay) )
1007 invalidate_shadow_ldt(d);
1009 percpu_info[cpu].deferred_ops &= ~DOP_FLUSH_TLB;
1010 old_base_pfn = pagetable_val(d->mm.pagetable) >> PAGE_SHIFT;
1011 d->mm.pagetable = mk_pagetable(pfn << PAGE_SHIFT);
1013 shadow_mk_pagetable(&d->mm);
1015 write_ptbase(&d->mm);
1017 put_page_and_type(&frame_table[old_base_pfn]);
1019 else
1021 MEM_LOG("Error while installing new baseptr %08lx", ptr);
1023 break;
1025 case MMUEXT_TLB_FLUSH:
1026 percpu_info[cpu].deferred_ops |= DOP_FLUSH_TLB;
1027 break;
1029 case MMUEXT_INVLPG:
1030 __flush_tlb_one(ptr);
1031 break;
1033 case MMUEXT_FLUSH_CACHE:
1034 if ( unlikely(!IS_CAPABLE_PHYSDEV(d)) )
1036 MEM_LOG("Non-physdev domain tried to FLUSH_CACHE.\n");
1037 okay = 0;
1039 else
1041 wbinvd();
1043 break;
1045 case MMUEXT_SET_LDT:
1047 unsigned long ents = val >> MMUEXT_CMD_SHIFT;
1048 if ( ((ptr & (PAGE_SIZE-1)) != 0) ||
1049 (ents > 8192) ||
1050 ((ptr+ents*LDT_ENTRY_SIZE) < ptr) ||
1051 ((ptr+ents*LDT_ENTRY_SIZE) > PAGE_OFFSET) )
1053 okay = 0;
1054 MEM_LOG("Bad args to SET_LDT: ptr=%08lx, ents=%08lx", ptr, ents);
1056 else if ( (d->mm.ldt_ents != ents) ||
1057 (d->mm.ldt_base != ptr) )
1059 invalidate_shadow_ldt(d);
1060 d->mm.ldt_base = ptr;
1061 d->mm.ldt_ents = ents;
1062 load_LDT(d);
1063 percpu_info[cpu].deferred_ops &= ~DOP_RELOAD_LDT;
1064 if ( ents != 0 )
1065 percpu_info[cpu].deferred_ops |= DOP_RELOAD_LDT;
1067 break;
1070 case MMUEXT_SET_FOREIGNDOM:
1071 domid = (domid_t)(val >> 16);
1073 if ( (e = percpu_info[cpu].foreign) != NULL )
1074 put_domain(e);
1075 percpu_info[cpu].foreign = NULL;
1077 if ( !IS_PRIV(d) )
1079 switch ( domid )
1081 case DOMID_IO:
1082 get_knownalive_domain(dom_io);
1083 percpu_info[cpu].foreign = dom_io;
1084 break;
1085 default:
1086 MEM_LOG("Dom %u cannot set foreign dom\n", d->id);
1087 okay = 0;
1088 break;
1091 else
1093 percpu_info[cpu].foreign = e = find_domain_by_id(domid);
1094 if ( e == NULL )
1096 switch ( domid )
1098 case DOMID_XEN:
1099 get_knownalive_domain(dom_xen);
1100 percpu_info[cpu].foreign = dom_xen;
1101 break;
1102 case DOMID_IO:
1103 get_knownalive_domain(dom_io);
1104 percpu_info[cpu].foreign = dom_io;
1105 break;
1106 default:
1107 MEM_LOG("Unknown domain '%u'", domid);
1108 okay = 0;
1109 break;
1113 break;
1115 case MMUEXT_TRANSFER_PAGE:
1116 domid = (domid_t)(val >> 16);
1117 gntref = (grant_ref_t)((val & 0xFF00) | ((ptr >> 2) & 0x00FF));
1119 if ( unlikely(IS_XEN_HEAP_FRAME(page)) ||
1120 unlikely(!pfn_is_ram(pfn)) ||
1121 unlikely((e = find_domain_by_id(domid)) == NULL) )
1123 MEM_LOG("Bad frame (%08lx) or bad domid (%d).\n", pfn, domid);
1124 okay = 0;
1125 break;
1128 spin_lock(&d->page_alloc_lock);
1130 /*
1131 * The tricky bit: atomically release ownership while there is just one
1132 * benign reference to the page (PGC_allocated). If that reference
1133 * disappears then the deallocation routine will safely spin.
1134 */
1135 nd = page->u.inuse.domain;
1136 y = page->count_info;
1137 do {
1138 x = y;
1139 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1140 (1|PGC_allocated)) ||
1141 unlikely(nd != d) )
1143 MEM_LOG("Bad page values %08lx: ed=%p(%u), sd=%p,"
1144 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1145 d, d->id, nd, x, page->u.inuse.type_info);
1146 spin_unlock(&d->page_alloc_lock);
1147 put_domain(e);
1148 return 0;
1150 __asm__ __volatile__(
1151 LOCK_PREFIX "cmpxchg8b %2"
1152 : "=d" (nd), "=a" (y),
1153 "=m" (*(volatile u64 *)(&page->count_info))
1154 : "0" (d), "1" (x), "c" (NULL), "b" (x) );
1156 while ( unlikely(nd != d) || unlikely(y != x) );
1158 /*
1159 * Unlink from 'd'. At least one reference remains (now anonymous), so
1160 * noone else is spinning to try to delete this page from 'd'.
1161 */
1162 d->tot_pages--;
1163 list_del(&page->list);
1165 spin_unlock(&d->page_alloc_lock);
1167 spin_lock(&e->page_alloc_lock);
1169 /*
1170 * Check that 'e' will accept the page and has reservation headroom.
1171 * Also, a domain mustn't have PGC_allocated pages when it is dying.
1172 */
1173 ASSERT(e->tot_pages <= e->max_pages);
1174 if ( unlikely(test_bit(DF_DYING, &e->flags)) ||
1175 unlikely(e->tot_pages == e->max_pages) ||
1176 unlikely(!gnttab_prepare_for_transfer(e, d, gntref)) )
1178 MEM_LOG("Transferee has no reservation headroom (%d,%d), or "
1179 "provided a bad grant ref, or is dying (%08lx).\n",
1180 e->tot_pages, e->max_pages, e->flags);
1181 spin_unlock(&e->page_alloc_lock);
1182 put_domain(e);
1183 okay = 0;
1184 break;
1187 /* Okay, add the page to 'e'. */
1188 if ( unlikely(e->tot_pages++ == 0) )
1189 get_knownalive_domain(e);
1190 list_add_tail(&page->list, &e->page_list);
1191 page->u.inuse.domain = e;
1193 spin_unlock(&e->page_alloc_lock);
1195 /* Transfer is all done: tell the guest about its new page frame. */
1196 gnttab_notify_transfer(e, gntref, pfn);
1198 put_domain(e);
1199 break;
1201 case MMUEXT_REASSIGN_PAGE:
1202 if ( unlikely(!IS_PRIV(d)) )
1204 MEM_LOG("Dom %u has no reassignment priv", d->id);
1205 okay = 0;
1206 break;
1209 e = percpu_info[cpu].foreign;
1210 if ( unlikely(e == NULL) )
1212 MEM_LOG("No FOREIGNDOM to reassign pfn %08lx to", pfn);
1213 okay = 0;
1214 break;
1217 /*
1218 * Grab both page_list locks, in order. This prevents the page from
1219 * disappearing elsewhere while we modify the owner, and we'll need
1220 * both locks if we're successful so that we can change lists.
1221 */
1222 if ( d < e )
1224 spin_lock(&d->page_alloc_lock);
1225 spin_lock(&e->page_alloc_lock);
1227 else
1229 spin_lock(&e->page_alloc_lock);
1230 spin_lock(&d->page_alloc_lock);
1233 /* A domain shouldn't have PGC_allocated pages when it is dying. */
1234 if ( unlikely(test_bit(DF_DYING, &e->flags)) ||
1235 unlikely(IS_XEN_HEAP_FRAME(page)) )
1237 MEM_LOG("Reassignment page is Xen heap, or dest dom is dying.");
1238 okay = 0;
1239 goto reassign_fail;
1242 /*
1243 * The tricky bit: atomically change owner while there is just one
1244 * benign reference to the page (PGC_allocated). If that reference
1245 * disappears then the deallocation routine will safely spin.
1246 */
1247 nd = page->u.inuse.domain;
1248 y = page->count_info;
1249 do {
1250 x = y;
1251 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1252 (1|PGC_allocated)) ||
1253 unlikely(nd != d) )
1255 MEM_LOG("Bad page values %08lx: ed=%p(%u), sd=%p,"
1256 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1257 d, d->id, nd, x, page->u.inuse.type_info);
1258 okay = 0;
1259 goto reassign_fail;
1261 __asm__ __volatile__(
1262 LOCK_PREFIX "cmpxchg8b %3"
1263 : "=d" (nd), "=a" (y), "=c" (e),
1264 "=m" (*(volatile u64 *)(&page->count_info))
1265 : "0" (d), "1" (x), "c" (e), "b" (x) );
1267 while ( unlikely(nd != d) || unlikely(y != x) );
1269 /*
1270 * Unlink from 'd'. We transferred at least one reference to 'e', so
1271 * noone else is spinning to try to delete this page from 'd'.
1272 */
1273 d->tot_pages--;
1274 list_del(&page->list);
1276 /*
1277 * Add the page to 'e'. Someone may already have removed the last
1278 * reference and want to remove the page from 'e'. However, we have
1279 * the lock so they'll spin waiting for us.
1280 */
1281 if ( unlikely(e->tot_pages++ == 0) )
1282 get_knownalive_domain(e);
1283 list_add_tail(&page->list, &e->page_list);
1285 reassign_fail:
1286 spin_unlock(&d->page_alloc_lock);
1287 spin_unlock(&e->page_alloc_lock);
1288 break;
1290 case MMUEXT_CLEAR_FOREIGNDOM:
1291 if ( (e = percpu_info[cpu].foreign) != NULL )
1292 put_domain(e);
1293 percpu_info[cpu].foreign = NULL;
1294 break;
1296 default:
1297 MEM_LOG("Invalid extended pt command 0x%08lx", val & MMUEXT_CMD_MASK);
1298 okay = 0;
1299 break;
1302 return okay;
1305 int do_mmu_update(
1306 mmu_update_t *ureqs, unsigned int count, unsigned int *pdone)
1308 /*
1309 * We steal the m.s.b. of the @count parameter to indicate whether this
1310 * invocation of do_mmu_update() is resuming a previously preempted call.
1311 * We steal the next 15 bits to remember the current FOREIGNDOM.
1312 */
1313 #define MMU_UPDATE_PREEMPTED (~(~0U>>1))
1314 #define MMU_UPDATE_PREEMPT_FDOM_SHIFT ((sizeof(int)*8)-16)
1315 #define MMU_UPDATE_PREEMPT_FDOM_MASK (0x7FFFU<<MMU_UPDATE_PREEMPT_FDOM_SHIFT)
1317 mmu_update_t req;
1318 unsigned long va = 0, deferred_ops, pfn, prev_pfn = 0;
1319 struct pfn_info *page;
1320 int rc = 0, okay = 1, i = 0, cpu = smp_processor_id();
1321 unsigned int cmd, done = 0;
1322 unsigned long prev_spfn = 0;
1323 l1_pgentry_t *prev_spl1e = 0;
1324 struct domain *d = current;
1325 u32 type_info;
1326 domid_t domid;
1328 cleanup_writable_pagetable(d);
1330 /*
1331 * If we are resuming after preemption, read how much work we have already
1332 * done. This allows us to set the @done output parameter correctly.
1333 * We also reset FOREIGNDOM here.
1334 */
1335 if ( unlikely(count&(MMU_UPDATE_PREEMPTED|MMU_UPDATE_PREEMPT_FDOM_MASK)) )
1337 if ( !(count & MMU_UPDATE_PREEMPTED) )
1339 /* Count overflow into private FOREIGNDOM field. */
1340 MEM_LOG("do_mmu_update count is too large");
1341 rc = -EINVAL;
1342 goto out;
1344 count &= ~MMU_UPDATE_PREEMPTED;
1345 domid = count >> MMU_UPDATE_PREEMPT_FDOM_SHIFT;
1346 count &= ~MMU_UPDATE_PREEMPT_FDOM_MASK;
1347 if ( unlikely(pdone != NULL) )
1348 (void)get_user(done, pdone);
1349 if ( (domid != current->id) &&
1350 !do_extended_command(0, MMUEXT_SET_FOREIGNDOM | (domid << 16)) )
1352 rc = -EINVAL;
1353 goto out;
1357 perfc_incrc(calls_to_mmu_update);
1358 perfc_addc(num_page_updates, count);
1360 if ( unlikely(!array_access_ok(VERIFY_READ, ureqs, count, sizeof(req))) )
1362 rc = -EFAULT;
1363 goto out;
1366 for ( i = 0; i < count; i++ )
1368 if ( hypercall_preempt_check() )
1370 rc = hypercall_create_continuation(
1371 __HYPERVISOR_mmu_update, 3, ureqs,
1372 (count - i) |
1373 (FOREIGNDOM->id << MMU_UPDATE_PREEMPT_FDOM_SHIFT) |
1374 MMU_UPDATE_PREEMPTED, pdone);
1375 break;
1378 if ( unlikely(__copy_from_user(&req, ureqs, sizeof(req)) != 0) )
1380 MEM_LOG("Bad __copy_from_user");
1381 rc = -EFAULT;
1382 break;
1385 cmd = req.ptr & (sizeof(l1_pgentry_t)-1);
1386 pfn = req.ptr >> PAGE_SHIFT;
1388 okay = 0;
1390 switch ( cmd )
1392 /*
1393 * MMU_NORMAL_PT_UPDATE: Normal update to any level of page table.
1394 */
1395 case MMU_NORMAL_PT_UPDATE:
1396 if ( unlikely(!get_page_from_pagenr(pfn, current)) )
1398 MEM_LOG("Could not get page for normal update");
1399 break;
1402 if ( likely(prev_pfn == pfn) )
1404 va = (va & PAGE_MASK) | (req.ptr & ~PAGE_MASK);
1406 else
1408 if ( prev_pfn != 0 )
1409 unmap_domain_mem((void *)va);
1410 va = (unsigned long)map_domain_mem(req.ptr);
1411 prev_pfn = pfn;
1414 page = &frame_table[pfn];
1415 switch ( (type_info = page->u.inuse.type_info) & PGT_type_mask )
1417 case PGT_l1_page_table:
1418 if ( likely(get_page_type(
1419 page, type_info & (PGT_type_mask|PGT_va_mask))) )
1421 okay = mod_l1_entry((l1_pgentry_t *)va,
1422 mk_l1_pgentry(req.val));
1424 if ( unlikely(d->mm.shadow_mode) && okay &&
1425 (get_shadow_status(&d->mm, page-frame_table) &
1426 PSH_shadowed) )
1428 shadow_l1_normal_pt_update(
1429 req.ptr, req.val, &prev_spfn, &prev_spl1e);
1430 put_shadow_status(&d->mm);
1433 put_page_type(page);
1435 break;
1436 case PGT_l2_page_table:
1437 if ( likely(get_page_type(page, PGT_l2_page_table)) )
1439 okay = mod_l2_entry((l2_pgentry_t *)va,
1440 mk_l2_pgentry(req.val),
1441 pfn);
1443 if ( unlikely(d->mm.shadow_mode) && okay &&
1444 (get_shadow_status(&d->mm, page-frame_table) &
1445 PSH_shadowed) )
1447 shadow_l2_normal_pt_update(req.ptr, req.val);
1448 put_shadow_status(&d->mm);
1451 put_page_type(page);
1453 break;
1454 default:
1455 if ( likely(get_page_type(page, PGT_writable_page)) )
1457 *(unsigned long *)va = req.val;
1458 okay = 1;
1459 put_page_type(page);
1461 break;
1464 put_page(page);
1465 break;
1467 case MMU_MACHPHYS_UPDATE:
1468 if ( unlikely(!get_page_from_pagenr(pfn, FOREIGNDOM)) )
1470 MEM_LOG("Could not get page for mach->phys update");
1471 break;
1474 machine_to_phys_mapping[pfn] = req.val;
1475 okay = 1;
1477 /*
1478 * If in log-dirty mode, mark the corresponding pseudo-physical
1479 * page as dirty.
1480 */
1481 if ( unlikely(d->mm.shadow_mode == SHM_logdirty) &&
1482 mark_dirty(&d->mm, pfn) )
1483 d->mm.shadow_dirty_block_count++;
1485 put_page(&frame_table[pfn]);
1486 break;
1488 /*
1489 * MMU_EXTENDED_COMMAND: Extended command is specified
1490 * in the least-siginificant bits of the 'value' field.
1491 */
1492 case MMU_EXTENDED_COMMAND:
1493 req.ptr &= ~(sizeof(l1_pgentry_t) - 1);
1494 okay = do_extended_command(req.ptr, req.val);
1495 break;
1497 default:
1498 MEM_LOG("Invalid page update command %08lx", req.ptr);
1499 break;
1502 if ( unlikely(!okay) )
1504 rc = -EINVAL;
1505 break;
1508 ureqs++;
1511 out:
1512 if ( prev_pfn != 0 )
1513 unmap_domain_mem((void *)va);
1515 if ( unlikely(prev_spl1e != 0) )
1516 unmap_domain_mem((void *)prev_spl1e);
1518 deferred_ops = percpu_info[cpu].deferred_ops;
1519 percpu_info[cpu].deferred_ops = 0;
1521 if ( deferred_ops & DOP_FLUSH_TLB )
1522 local_flush_tlb();
1524 if ( deferred_ops & DOP_RELOAD_LDT )
1525 (void)map_ldt_shadow_page(0);
1527 if ( unlikely(percpu_info[cpu].foreign != NULL) )
1529 put_domain(percpu_info[cpu].foreign);
1530 percpu_info[cpu].foreign = NULL;
1533 /* Add incremental work we have done to the @done output parameter. */
1534 if ( unlikely(pdone != NULL) )
1535 __put_user(done + i, pdone);
1537 return rc;
1541 int do_update_va_mapping(unsigned long page_nr,
1542 unsigned long val,
1543 unsigned long flags)
1545 struct domain *d = current;
1546 int err = 0;
1547 unsigned int cpu = d->processor;
1548 unsigned long deferred_ops;
1550 perfc_incrc(calls_to_update_va);
1552 if ( unlikely(page_nr >= (HYPERVISOR_VIRT_START >> PAGE_SHIFT)) )
1553 return -EINVAL;
1555 cleanup_writable_pagetable(d);
1557 /*
1558 * XXX When we make this support 4MB superpages we should also deal with
1559 * the case of updating L2 entries.
1560 */
1562 if ( unlikely(!mod_l1_entry(&linear_pg_table[page_nr],
1563 mk_l1_pgentry(val))) )
1564 err = -EINVAL;
1566 if ( unlikely(d->mm.shadow_mode) )
1568 unsigned long sval;
1570 l1pte_propagate_from_guest(&d->mm, &val, &sval);
1572 if ( unlikely(__put_user(sval, ((unsigned long *)(
1573 &shadow_linear_pg_table[page_nr])))) )
1575 /*
1576 * Since L2's are guranteed RW, failure indicates the page was not
1577 * shadowed, so ignore.
1578 */
1579 perfc_incrc(shadow_update_va_fail);
1582 /*
1583 * If we're in log-dirty mode then we need to note that we've updated
1584 * the PTE in the PT-holding page. We need the machine frame number
1585 * for this.
1586 */
1587 if ( d->mm.shadow_mode == SHM_logdirty )
1588 mark_dirty(&current->mm, va_to_l1mfn(page_nr << PAGE_SHIFT));
1590 check_pagetable(&d->mm, d->mm.pagetable, "va"); /* debug */
1593 deferred_ops = percpu_info[cpu].deferred_ops;
1594 percpu_info[cpu].deferred_ops = 0;
1596 if ( unlikely(deferred_ops & DOP_FLUSH_TLB) ||
1597 unlikely(flags & UVMF_FLUSH_TLB) )
1598 local_flush_tlb();
1599 else if ( unlikely(flags & UVMF_INVLPG) )
1600 __flush_tlb_one(page_nr << PAGE_SHIFT);
1602 if ( unlikely(deferred_ops & DOP_RELOAD_LDT) )
1603 (void)map_ldt_shadow_page(0);
1605 return err;
1608 int do_update_va_mapping_otherdomain(unsigned long page_nr,
1609 unsigned long val,
1610 unsigned long flags,
1611 domid_t domid)
1613 unsigned int cpu = smp_processor_id();
1614 struct domain *d;
1615 int rc;
1617 if ( unlikely(!IS_PRIV(current)) )
1618 return -EPERM;
1620 percpu_info[cpu].foreign = d = find_domain_by_id(domid);
1621 if ( unlikely(d == NULL) )
1623 MEM_LOG("Unknown domain '%u'", domid);
1624 return -ESRCH;
1627 rc = do_update_va_mapping(page_nr, val, flags);
1629 put_domain(d);
1630 percpu_info[cpu].foreign = NULL;
1632 return rc;
1637 /*************************
1638 * Writable Pagetables
1639 */
1641 ptwr_info_t ptwr_info[NR_CPUS];
1643 #ifdef VERBOSE
1644 int ptwr_debug = 0x0;
1645 #define PTWR_PRINTK(_f, _a...) \
1646 do { if ( unlikely(ptwr_debug) ) printk( _f , ## _a ); } while ( 0 )
1647 #define PTWR_PRINT_WHICH (which ? 'I' : 'A')
1648 #else
1649 #define PTWR_PRINTK(_f, _a...) ((void)0)
1650 #endif
1652 /* Flush the given writable p.t. page and write-protect it again. */
1653 void ptwr_flush(const int which)
1655 unsigned long sstat, spte, pte, *ptep, l1va;
1656 l1_pgentry_t *sl1e = NULL, *pl1e, ol1e, nl1e;
1657 l2_pgentry_t *pl2e;
1658 int i, cpu = smp_processor_id();
1659 struct domain *d = current;
1661 l1va = ptwr_info[cpu].ptinfo[which].l1va;
1662 ptep = (unsigned long *)&linear_pg_table[l1va>>PAGE_SHIFT];
1664 /*
1665 * STEP 1. Write-protect the p.t. page so no more updates can occur.
1666 */
1668 if ( unlikely(__get_user(pte, ptep)) )
1670 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
1671 /*
1672 * Really a bug. We could read this PTE during the initial fault,
1673 * and pagetables can't have changed meantime. XXX Multi-CPU guests?
1674 */
1675 BUG();
1677 PTWR_PRINTK("[%c] disconnected_l1va at %p is %08lx\n",
1678 PTWR_PRINT_WHICH, ptep, pte);
1679 pte &= ~_PAGE_RW;
1681 if ( unlikely(d->mm.shadow_mode) )
1683 /* Write-protect the p.t. page in the shadow page table. */
1684 l1pte_propagate_from_guest(&d->mm, &pte, &spte);
1685 __put_user(
1686 spte, (unsigned long *)&shadow_linear_pg_table[l1va>>PAGE_SHIFT]);
1688 /* Is the p.t. page itself shadowed? Map it into Xen space if so. */
1689 sstat = get_shadow_status(&d->mm, pte >> PAGE_SHIFT);
1690 if ( sstat & PSH_shadowed )
1691 sl1e = map_domain_mem((sstat & PSH_pfn_mask) << PAGE_SHIFT);
1694 /* Write-protect the p.t. page in the guest page table. */
1695 if ( unlikely(__put_user(pte, ptep)) )
1697 MEM_LOG("ptwr: Could not update pte at %p\n", ptep);
1698 /*
1699 * Really a bug. We could write this PTE during the initial fault,
1700 * and pagetables can't have changed meantime. XXX Multi-CPU guests?
1701 */
1702 BUG();
1705 /* Ensure that there are no stale writable mappings in any TLB. */
1706 /* NB. INVLPG is a serialising instruction: flushes pending updates. */
1707 __flush_tlb_one(l1va); /* XXX Multi-CPU guests? */
1708 PTWR_PRINTK("[%c] disconnected_l1va at %p now %08lx\n",
1709 PTWR_PRINT_WHICH, ptep, pte);
1711 /*
1712 * STEP 2. Validate any modified PTEs.
1713 */
1715 pl1e = ptwr_info[cpu].ptinfo[which].pl1e;
1716 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
1718 ol1e = ptwr_info[cpu].ptinfo[which].page[i];
1719 nl1e = pl1e[i];
1721 if ( likely(l1_pgentry_val(ol1e) == l1_pgentry_val(nl1e)) )
1722 continue;
1724 /*
1725 * Fast path for PTEs that have merely been write-protected
1726 * (e.g., during a Unix fork()). A strict reduction in privilege.
1727 */
1728 if ( likely(l1_pgentry_val(ol1e) == (l1_pgentry_val(nl1e)|_PAGE_RW)) )
1730 if ( likely(l1_pgentry_val(nl1e) & _PAGE_PRESENT) )
1732 if ( unlikely(sl1e != NULL) )
1733 l1pte_propagate_from_guest(
1734 &d->mm, &l1_pgentry_val(nl1e),
1735 &l1_pgentry_val(sl1e[i]));
1736 put_page_type(&frame_table[l1_pgentry_to_pagenr(nl1e)]);
1738 continue;
1741 if ( unlikely(!get_page_from_l1e(nl1e, d)) )
1743 MEM_LOG("ptwr: Could not re-validate l1 page\n");
1744 /*
1745 * Make the remaining p.t's consistent before crashing, so the
1746 * reference counts are correct.
1747 */
1748 memcpy(&pl1e[i], &ptwr_info[cpu].ptinfo[which].page[i],
1749 (ENTRIES_PER_L1_PAGETABLE - i) * sizeof(l1_pgentry_t));
1750 unmap_domain_mem(pl1e);
1751 ptwr_info[cpu].ptinfo[which].l1va = 0;
1752 domain_crash();
1755 if ( unlikely(sl1e != NULL) )
1756 l1pte_propagate_from_guest(
1757 &d->mm, &l1_pgentry_val(nl1e), &l1_pgentry_val(sl1e[i]));
1759 if ( unlikely(l1_pgentry_val(ol1e) & _PAGE_PRESENT) )
1760 put_page_from_l1e(ol1e, d);
1762 unmap_domain_mem(pl1e);
1764 /*
1765 * STEP 3. Reattach the L1 p.t. page into the current address space.
1766 */
1768 if ( (which == PTWR_PT_ACTIVE) && likely(!d->mm.shadow_mode) )
1770 pl2e = &linear_l2_table[ptwr_info[cpu].ptinfo[which].l2_idx];
1771 *pl2e = mk_l2_pgentry(l2_pgentry_val(*pl2e) | _PAGE_PRESENT);
1774 /*
1775 * STEP 4. Final tidy-up.
1776 */
1778 ptwr_info[cpu].ptinfo[which].l1va = 0;
1780 if ( unlikely(sl1e != NULL) )
1782 unmap_domain_mem(sl1e);
1783 put_shadow_status(&d->mm);
1787 /* Write page fault handler: check if guest is trying to modify a PTE. */
1788 int ptwr_do_page_fault(unsigned long addr)
1790 unsigned long pte, pfn, l2e;
1791 struct pfn_info *page;
1792 l2_pgentry_t *pl2e;
1793 int which, cpu = smp_processor_id();
1794 u32 l2_idx;
1796 /*
1797 * Attempt to read the PTE that maps the VA being accessed. By checking for
1798 * PDE validity in the L2 we avoid many expensive fixups in __get_user().
1799 */
1800 if ( !(l2_pgentry_val(linear_l2_table[addr>>L2_PAGETABLE_SHIFT]) &
1801 _PAGE_PRESENT) ||
1802 __get_user(pte, (unsigned long *)&linear_pg_table[addr>>PAGE_SHIFT]) )
1803 return 0;
1805 pfn = pte >> PAGE_SHIFT;
1806 page = &frame_table[pfn];
1808 /* We are looking only for read-only mappings of p.t. pages. */
1809 if ( ((pte & (_PAGE_RW | _PAGE_PRESENT)) != _PAGE_PRESENT) ||
1810 ((page->u.inuse.type_info & PGT_type_mask) != PGT_l1_page_table) )
1811 return 0;
1813 /* Get the L2 index at which this L1 p.t. is always mapped. */
1814 l2_idx = page->u.inuse.type_info & PGT_va_mask;
1815 if ( unlikely(l2_idx >= PGT_va_unknown) )
1816 domain_crash(); /* Urk! This L1 is mapped in multiple L2 slots! */
1817 l2_idx >>= PGT_va_shift;
1819 if ( l2_idx == (addr >> L2_PAGETABLE_SHIFT) )
1821 MEM_LOG("PTWR failure! Pagetable maps itself at %08lx\n", addr);
1822 domain_crash();
1825 /*
1826 * Is the L1 p.t. mapped into the current address space? If so we call it
1827 * an ACTIVE p.t., otherwise it is INACTIVE.
1828 */
1829 pl2e = &linear_l2_table[l2_idx];
1830 l2e = l2_pgentry_val(*pl2e);
1831 which = PTWR_PT_INACTIVE;
1832 if ( (l2e >> PAGE_SHIFT) == pfn )
1834 /* Check the PRESENT bit to set ACTIVE. */
1835 if ( likely(l2e & _PAGE_PRESENT) )
1836 which = PTWR_PT_ACTIVE;
1837 else {
1838 /*
1839 * If the PRESENT bit is clear, we may be conflicting with
1840 * the current ACTIVE p.t. (it may be the same p.t. mapped
1841 * at another virt addr).
1842 * The ptwr_flush call below will restore the PRESENT bit.
1843 */
1844 if ( ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va &&
1845 l2_idx == ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l2_idx )
1846 which = PTWR_PT_ACTIVE;
1850 PTWR_PRINTK("[%c] page_fault on l1 pt at va %08lx, pt for %08x, "
1851 "pfn %08lx\n", PTWR_PRINT_WHICH,
1852 addr, l2_idx << L2_PAGETABLE_SHIFT, pfn);
1854 /*
1855 * We only allow one ACTIVE and one INACTIVE p.t. to be updated at at
1856 * time. If there is already one, we must flush it out.
1857 */
1858 if ( ptwr_info[cpu].ptinfo[which].l1va )
1859 ptwr_flush(which);
1861 ptwr_info[cpu].ptinfo[which].l1va = addr | 1;
1862 ptwr_info[cpu].ptinfo[which].l2_idx = l2_idx;
1864 /* For safety, disconnect the L1 p.t. page from current space. */
1865 if ( (which == PTWR_PT_ACTIVE) && likely(!current->mm.shadow_mode) )
1867 *pl2e = mk_l2_pgentry(l2e & ~_PAGE_PRESENT);
1868 flush_tlb(); /* XXX Multi-CPU guests? */
1871 /* Temporarily map the L1 page, and make a copy of it. */
1872 ptwr_info[cpu].ptinfo[which].pl1e = map_domain_mem(pfn << PAGE_SHIFT);
1873 memcpy(ptwr_info[cpu].ptinfo[which].page,
1874 ptwr_info[cpu].ptinfo[which].pl1e,
1875 ENTRIES_PER_L1_PAGETABLE * sizeof(l1_pgentry_t));
1877 /* Finally, make the p.t. page writable by the guest OS. */
1878 pte |= _PAGE_RW;
1879 PTWR_PRINTK("[%c] update %p pte to %08lx\n", PTWR_PRINT_WHICH,
1880 &linear_pg_table[addr>>PAGE_SHIFT], pte);
1881 if ( unlikely(__put_user(pte, (unsigned long *)
1882 &linear_pg_table[addr>>PAGE_SHIFT])) )
1884 MEM_LOG("ptwr: Could not update pte at %p\n", (unsigned long *)
1885 &linear_pg_table[addr>>PAGE_SHIFT]);
1886 /* Toss the writable pagetable state and crash. */
1887 unmap_domain_mem(ptwr_info[cpu].ptinfo[which].pl1e);
1888 ptwr_info[cpu].ptinfo[which].l1va = 0;
1889 domain_crash();
1892 return EXCRET_fault_fixed;
1895 static __init int ptwr_init(void)
1897 int i;
1899 for ( i = 0; i < smp_num_cpus; i++ )
1901 ptwr_info[i].ptinfo[PTWR_PT_ACTIVE].page =
1902 (void *)alloc_xenheap_page();
1903 ptwr_info[i].ptinfo[PTWR_PT_INACTIVE].page =
1904 (void *)alloc_xenheap_page();
1907 return 0;
1909 __initcall(ptwr_init);
1914 /************************************************************************/
1915 /************************************************************************/
1916 /************************************************************************/
1918 #ifndef NDEBUG
1920 void ptwr_status(void)
1922 unsigned long pte, *ptep, pfn;
1923 struct pfn_info *page;
1924 int cpu = smp_processor_id();
1926 ptep = (unsigned long *)&linear_pg_table
1927 [ptwr_info[cpu].ptinfo[PTWR_PT_INACTIVE].l1va>>PAGE_SHIFT];
1929 if ( __get_user(pte, ptep) ) {
1930 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
1931 domain_crash();
1934 pfn = pte >> PAGE_SHIFT;
1935 page = &frame_table[pfn];
1936 printk("need to alloc l1 page %p\n", page);
1937 /* make pt page writable */
1938 printk("need to make read-only l1-page at %p is %08lx\n",
1939 ptep, pte);
1941 if ( ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va == 0 )
1942 return;
1944 if ( __get_user(pte, (unsigned long *)
1945 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va) ) {
1946 MEM_LOG("ptwr: Could not read pte at %p\n", (unsigned long *)
1947 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va);
1948 domain_crash();
1950 pfn = pte >> PAGE_SHIFT;
1951 page = &frame_table[pfn];
1954 void audit_domain(struct domain *d)
1956 int ttot=0, ctot=0, io_mappings=0, lowmem_mappings=0;
1958 void adjust (struct pfn_info *page, int dir, int adjtype)
1960 int count = page->count_info & PGC_count_mask;
1962 if ( adjtype )
1964 int tcount = page->u.inuse.type_info & PGT_count_mask;
1966 ttot++;
1968 tcount += dir;
1970 if ( tcount < 0 )
1972 /* This will only come out once. */
1973 printk("Audit %d: type count whent below zero pfn=%x "
1974 "taf=%x otaf=%x\n",
1975 d->id, page-frame_table,
1976 page->u.inuse.type_info,
1977 page->tlbflush_timestamp);
1980 page->u.inuse.type_info =
1981 (page->u.inuse.type_info & ~PGT_count_mask) |
1982 (tcount & PGT_count_mask);
1985 ctot++;
1986 count += dir;
1987 if ( count < 0 )
1989 /* This will only come out once. */
1990 printk("Audit %d: general count whent below zero pfn=%x "
1991 "taf=%x otaf=%x\n",
1992 d->id, page-frame_table,
1993 page->u.inuse.type_info,
1994 page->tlbflush_timestamp);
1997 page->count_info =
1998 (page->count_info & ~PGC_count_mask) |
1999 (count & PGC_count_mask);
2003 void scan_for_pfn(struct domain *d, unsigned long xpfn)
2005 unsigned long pfn, *pt;
2006 struct list_head *list_ent;
2007 struct pfn_info *page;
2008 int i;
2010 list_ent = d->page_list.next;
2011 for ( i = 0; (list_ent != &d->page_list); i++ )
2013 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2014 page = &frame_table[pfn];
2016 switch ( page->u.inuse.type_info & PGT_type_mask )
2018 case PGT_l1_page_table:
2019 case PGT_l2_page_table:
2020 pt = map_domain_mem(pfn<<PAGE_SHIFT);
2021 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2022 if ( (pt[i] & _PAGE_PRESENT) &&
2023 ((pt[i] >> PAGE_SHIFT) == xpfn) )
2024 printk(" found dom=%d i=%x pfn=%lx t=%x c=%x\n",
2025 d->id, i, pfn, page->u.inuse.type_info,
2026 page->count_info);
2027 unmap_domain_mem(pt);
2030 list_ent = frame_table[pfn].list.next;
2035 void scan_for_pfn_remote(unsigned long xpfn)
2037 struct domain *e;
2038 for_each_domain ( e )
2039 scan_for_pfn( e, xpfn );
2042 int i, l1, l2;
2043 unsigned long pfn;
2044 struct list_head *list_ent;
2045 struct pfn_info *page;
2047 if ( d != current )
2048 domain_pause(d);
2049 synchronise_pagetables(~0UL);
2051 printk("pt base=%lx sh_info=%x\n",
2052 pagetable_val(d->mm.pagetable)>>PAGE_SHIFT,
2053 virt_to_page(d->shared_info)-frame_table);
2055 spin_lock(&d->page_alloc_lock);
2057 /* PHASE 0 */
2059 list_ent = d->page_list.next;
2060 for ( i = 0; (list_ent != &d->page_list); i++ )
2062 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2063 page = &frame_table[pfn];
2065 if ( page->u.inuse.domain != d )
2066 BUG();
2068 if ( (page->u.inuse.type_info & PGT_count_mask) >
2069 (page->count_info & PGC_count_mask) )
2070 printk("taf > caf %x %x pfn=%lx\n",
2071 page->u.inuse.type_info, page->count_info, pfn );
2073 #if 0 /* SYSV shared memory pages plus writeable files. */
2074 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page &&
2075 (page->u.inuse.type_info & PGT_count_mask) > 1 )
2077 printk("writeable page with type count >1: pfn=%lx t=%x c=%x\n",
2078 pfn,
2079 page->u.inuse.type_info,
2080 page->count_info );
2081 scan_for_pfn_remote(pfn);
2083 #endif
2084 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_none &&
2085 (page->u.inuse.type_info & PGT_count_mask) > 1 )
2087 printk("normal page with type count >1: pfn=%lx t=%x c=%x\n",
2088 pfn,
2089 page->u.inuse.type_info,
2090 page->count_info );
2093 /* Use tlbflush_timestamp to store original type_info. */
2094 page->tlbflush_timestamp = page->u.inuse.type_info;
2096 list_ent = frame_table[pfn].list.next;
2100 /* PHASE 1 */
2101 if( pagetable_val(d->mm.pagetable) )
2102 adjust(&frame_table[pagetable_val(d->mm.pagetable)>>PAGE_SHIFT], -1, 1);
2104 list_ent = d->page_list.next;
2105 for ( i = 0; (list_ent != &d->page_list); i++ )
2107 unsigned long *pt;
2108 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2109 page = &frame_table[pfn];
2111 if ( page->u.inuse.domain != d )
2112 BUG();
2114 switch ( page->u.inuse.type_info & PGT_type_mask )
2116 case PGT_l2_page_table:
2118 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2119 printk("Audit %d: L2 not validated %x\n",
2120 d->id, page->u.inuse.type_info);
2122 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2123 printk("Audit %d: L2 not pinned %x\n",
2124 d->id, page->u.inuse.type_info);
2125 else
2126 adjust( page, -1, 1 );
2128 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2130 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2132 if ( pt[i] & _PAGE_PRESENT )
2134 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2135 struct pfn_info *l1page = &frame_table[l1pfn];
2137 if ( l1page->u.inuse.domain != d )
2139 printk("L2: Skip bizarre page belonging to other "
2140 "dom %p\n", l1page->u.inuse.domain);
2141 continue;
2144 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2145 PGT_l2_page_table )
2146 printk("Audit %d: [%x] Found %s Linear PT "
2147 "t=%x pfn=%lx\n", d->id, i,
2148 (l1pfn==pfn) ? "Self" : "Other",
2149 l1page->u.inuse.type_info,
2150 l1pfn);
2151 else if ( (l1page->u.inuse.type_info & PGT_type_mask) !=
2152 PGT_l1_page_table )
2153 printk("Audit %d: [%x] Expected L1 t=%x pfn=%lx\n",
2154 d->id, i,
2155 l1page->u.inuse.type_info,
2156 l1pfn);
2158 adjust(l1page, -1, 1);
2162 unmap_domain_mem(pt);
2164 break;
2167 case PGT_l1_page_table:
2169 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2170 adjust( page, -1, 1 );
2172 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2173 printk("Audit %d: L1 not validated %x\n",
2174 d->id, page->u.inuse.type_info);
2175 #if 0
2176 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2177 printk("Audit %d: L1 not pinned %x\n",
2178 d->id, page->u.inuse.type_info);
2179 #endif
2180 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2182 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2184 if ( pt[i] & _PAGE_PRESENT )
2186 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2187 struct pfn_info *l1page = &frame_table[l1pfn];
2189 if ( l1pfn < 0x100 )
2191 lowmem_mappings++;
2192 continue;
2195 if ( l1pfn > max_page )
2197 io_mappings++;
2198 continue;
2201 if ( pt[i] & _PAGE_RW )
2204 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2205 PGT_l1_page_table ||
2206 (l1page->u.inuse.type_info & PGT_type_mask) ==
2207 PGT_l2_page_table )
2208 printk("Audit %d: [%x] Ilegal RW t=%x pfn=%lx\n",
2209 d->id, i,
2210 l1page->u.inuse.type_info,
2211 l1pfn);
2215 if ( l1page->u.inuse.domain != d )
2217 printk("Audit %d: [%lx,%x] Skip foreign page dom=%lx "
2218 "pfn=%lx c=%08x t=%08x m2p=%lx\n",
2219 d->id, pfn, i,
2220 (unsigned long)l1page->u.inuse.domain,
2221 l1pfn,
2222 l1page->count_info,
2223 l1page->u.inuse.type_info,
2224 machine_to_phys_mapping[l1pfn]);
2225 continue;
2228 adjust(l1page, -1, 0);
2232 unmap_domain_mem(pt);
2234 break;
2237 list_ent = frame_table[pfn].list.next;
2240 if ( (io_mappings > 0) || (lowmem_mappings > 0) )
2241 printk("Audit %d: Found %d lowmem mappings and %d io mappings\n",
2242 d->id, lowmem_mappings, io_mappings);
2244 /* PHASE 2 */
2246 ctot = ttot = 0;
2247 list_ent = d->page_list.next;
2248 for ( i = 0; (list_ent != &d->page_list); i++ )
2250 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2251 page = &frame_table[pfn];
2253 switch ( page->u.inuse.type_info & PGT_type_mask)
2255 case PGT_l1_page_table:
2256 case PGT_l2_page_table:
2257 if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
2259 printk("Audit %d: type count!=0 t=%x ot=%x c=%x pfn=%lx\n",
2260 d->id, page->u.inuse.type_info,
2261 page->tlbflush_timestamp,
2262 page->count_info, pfn );
2263 scan_for_pfn_remote(pfn);
2265 default:
2266 if ( (page->count_info & PGC_count_mask) != 1 )
2268 printk("Audit %d: gen count!=1 (c=%x) t=%x ot=%x pfn=%lx\n",
2269 d->id,
2270 page->count_info,
2271 page->u.inuse.type_info,
2272 page->tlbflush_timestamp, pfn );
2273 scan_for_pfn_remote(pfn);
2275 break;
2278 list_ent = frame_table[pfn].list.next;
2281 /* PHASE 3 */
2282 list_ent = d->page_list.next;
2283 l1 = l2 = 0;
2284 for ( i = 0; (list_ent != &d->page_list); i++ )
2286 unsigned long *pt;
2287 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2288 page = &frame_table[pfn];
2290 switch ( page->u.inuse.type_info & PGT_type_mask )
2292 case PGT_l2_page_table:
2293 l2++;
2294 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2295 adjust( page, 1, 1 );
2297 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2299 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2301 if ( pt[i] & _PAGE_PRESENT )
2303 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2304 struct pfn_info *l1page;
2306 if (l1pfn>max_page)
2307 continue;
2309 l1page = &frame_table[l1pfn];
2311 if ( l1page->u.inuse.domain == d)
2312 adjust(l1page, 1, 1);
2316 unmap_domain_mem(pt);
2317 break;
2319 case PGT_l1_page_table:
2320 l1++;
2321 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2322 adjust( page, 1, 1 );
2324 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2326 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2328 if ( pt[i] & _PAGE_PRESENT )
2330 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2331 struct pfn_info *l1page;
2333 if (l1pfn>max_page)
2334 continue;
2336 l1page = &frame_table[l1pfn];
2338 if ( (l1page->u.inuse.domain != d) ||
2339 (l1pfn < 0x100) || (l1pfn > max_page) )
2340 continue;
2342 adjust(l1page, 1, 0);
2346 unmap_domain_mem(pt);
2347 break;
2351 page->tlbflush_timestamp = 0;
2353 list_ent = frame_table[pfn].list.next;
2356 spin_unlock(&d->page_alloc_lock);
2358 if( pagetable_val(d->mm.pagetable) )
2359 adjust(&frame_table[pagetable_val(d->mm.pagetable)>>PAGE_SHIFT], 1, 1);
2361 printk("Audit %d: Done. pages=%d l1=%d l2=%d ctot=%d ttot=%d\n", d->id, i, l1, l2, ctot, ttot );
2363 if ( d != current )
2364 domain_unpause(d);
2367 void audit_domains(void)
2369 struct domain *d;
2370 for_each_domain ( d )
2371 audit_domain(d);
2374 void audit_domains_key(unsigned char key)
2376 audit_domains();
2379 #endif