ia64/xen-unstable

view xen/arch/x86/memory.c @ 2718:db83b3aaf89f

bitkeeper revision 1.1159.130.5 (4179350dKbV8Dl-DTyoBQ9c__K-pFw)

Fix incorrect error path in Xen's p.t. update code.
author kaf24@freefall.cl.cam.ac.uk
date Fri Oct 22 16:27:57 2004 +0000 (2004-10-22)
parents ed0f5b1a41ba
children bd93b9d866a5
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/lib.h>
90 #include <xen/mm.h>
91 #include <xen/sched.h>
92 #include <xen/errno.h>
93 #include <xen/perfc.h>
94 #include <xen/irq.h>
95 #include <xen/softirq.h>
96 #include <asm/shadow.h>
97 #include <asm/page.h>
98 #include <asm/flushtlb.h>
99 #include <asm/io.h>
100 #include <asm/uaccess.h>
101 #include <asm/domain_page.h>
102 #include <asm/ldt.h>
104 #ifdef VERBOSE
105 #define MEM_LOG(_f, _a...) \
106 printk("DOM%u: (file=memory.c, line=%d) " _f "\n", \
107 current->id , __LINE__ , ## _a )
108 #else
109 #define MEM_LOG(_f, _a...) ((void)0)
110 #endif
112 static int alloc_l2_table(struct pfn_info *page);
113 static int alloc_l1_table(struct pfn_info *page);
114 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d);
115 static int get_page_and_type_from_pagenr(unsigned long page_nr,
116 u32 type,
117 struct domain *d);
119 static void free_l2_table(struct pfn_info *page);
120 static void free_l1_table(struct pfn_info *page);
122 static int mod_l2_entry(l2_pgentry_t *, l2_pgentry_t, unsigned long);
123 static int mod_l1_entry(l1_pgentry_t *, l1_pgentry_t);
125 /* Used to defer flushing of memory structures. */
126 static struct {
127 #define DOP_FLUSH_TLB (1<<0) /* Flush the TLB. */
128 #define DOP_RELOAD_LDT (1<<1) /* Reload the LDT shadow mapping. */
129 unsigned long deferred_ops;
130 unsigned long cr0;
131 /* If non-NULL, specifies a foreign subject domain for some operations. */
132 struct domain *foreign;
133 } percpu_info[NR_CPUS] __cacheline_aligned;
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 void arch_init_memory(void)
145 {
146 unsigned long mfn;
148 /*
149 * We are rather picky about the layout of 'struct pfn_info'. The
150 * count_info and domain fields must be adjacent, as we perform atomic
151 * 64-bit operations on them. Also, just for sanity, we assert the size
152 * of the structure here.
153 */
154 if ( (offsetof(struct pfn_info, u.inuse.domain) !=
155 (offsetof(struct pfn_info, count_info) + sizeof(u32))) ||
156 (sizeof(struct pfn_info) != 24) )
157 {
158 printk("Weird pfn_info layout (%ld,%ld,%d)\n",
159 offsetof(struct pfn_info, count_info),
160 offsetof(struct pfn_info, u.inuse.domain),
161 sizeof(struct pfn_info));
162 for ( ; ; ) ;
163 }
165 memset(percpu_info, 0, sizeof(percpu_info));
167 /* Initialise to a magic of 0x55555555 so easier to spot bugs later. */
168 memset(machine_to_phys_mapping, 0x55, 4<<20);
170 /*
171 * Initialise our DOMID_XEN domain.
172 * Any Xen-heap pages that we will allow to be mapped will have
173 * their domain field set to dom_xen.
174 */
175 dom_xen = alloc_domain_struct();
176 atomic_set(&dom_xen->refcnt, 1);
177 dom_xen->id = DOMID_XEN;
179 /*
180 * Initialise our DOMID_IO domain.
181 * This domain owns no pages but is considered a special case when
182 * mapping I/O pages, as the mappings occur at the priv of the caller.
183 */
184 dom_io = alloc_domain_struct();
185 atomic_set(&dom_io->refcnt, 1);
186 dom_io->id = DOMID_IO;
188 /* M2P table is mappable read-only by privileged domains. */
189 for ( mfn = virt_to_phys(&machine_to_phys_mapping[0<<20])>>PAGE_SHIFT;
190 mfn < virt_to_phys(&machine_to_phys_mapping[1<<20])>>PAGE_SHIFT;
191 mfn++ )
192 {
193 frame_table[mfn].count_info = PGC_allocated | 1;
194 frame_table[mfn].u.inuse.type_info = PGT_gdt_page | 1; /* non-RW */
195 frame_table[mfn].u.inuse.domain = dom_xen;
196 }
197 }
199 static void __invalidate_shadow_ldt(struct domain *d)
200 {
201 int i;
202 unsigned long pfn;
203 struct pfn_info *page;
205 d->mm.shadow_ldt_mapcnt = 0;
207 for ( i = 16; i < 32; i++ )
208 {
209 pfn = l1_pgentry_to_pagenr(d->mm.perdomain_pt[i]);
210 if ( pfn == 0 ) continue;
211 d->mm.perdomain_pt[i] = mk_l1_pgentry(0);
212 page = &frame_table[pfn];
213 ASSERT_PAGE_IS_TYPE(page, PGT_ldt_page);
214 ASSERT_PAGE_IS_DOMAIN(page, d);
215 put_page_and_type(page);
216 }
218 /* Dispose of the (now possibly invalid) mappings from the TLB. */
219 percpu_info[d->processor].deferred_ops |= DOP_FLUSH_TLB | DOP_RELOAD_LDT;
220 }
223 static inline void invalidate_shadow_ldt(struct domain *d)
224 {
225 if ( d->mm.shadow_ldt_mapcnt != 0 )
226 __invalidate_shadow_ldt(d);
227 }
230 static int alloc_segdesc_page(struct pfn_info *page)
231 {
232 unsigned long *descs = map_domain_mem((page-frame_table) << PAGE_SHIFT);
233 int i;
235 for ( i = 0; i < 512; i++ )
236 if ( unlikely(!check_descriptor(&descs[i*2])) )
237 goto fail;
239 unmap_domain_mem(descs);
240 return 1;
242 fail:
243 unmap_domain_mem(descs);
244 return 0;
245 }
248 /* Map shadow page at offset @off. */
249 int map_ldt_shadow_page(unsigned int off)
250 {
251 struct domain *d = current;
252 unsigned long l1e;
254 if ( unlikely(in_irq()) )
255 BUG();
257 __get_user(l1e, (unsigned long *)&linear_pg_table[(d->mm.ldt_base >>
258 PAGE_SHIFT) + off]);
260 if ( unlikely(!(l1e & _PAGE_PRESENT)) ||
261 unlikely(!get_page_and_type(&frame_table[l1e >> PAGE_SHIFT],
262 d, PGT_ldt_page)) )
263 return 0;
265 d->mm.perdomain_pt[off + 16] = mk_l1_pgentry(l1e | _PAGE_RW);
266 d->mm.shadow_ldt_mapcnt++;
268 return 1;
269 }
272 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d)
273 {
274 struct pfn_info *page = &frame_table[page_nr];
276 if ( unlikely(!pfn_is_ram(page_nr)) )
277 {
278 MEM_LOG("Pfn %08lx is not RAM", page_nr);
279 return 0;
280 }
282 if ( unlikely(!get_page(page, d)) )
283 {
284 MEM_LOG("Could not get page ref for pfn %08lx", page_nr);
285 return 0;
286 }
288 return 1;
289 }
292 static int get_page_and_type_from_pagenr(unsigned long page_nr,
293 u32 type,
294 struct domain *d)
295 {
296 struct pfn_info *page = &frame_table[page_nr];
298 if ( unlikely(!get_page_from_pagenr(page_nr, d)) )
299 return 0;
301 if ( unlikely(!get_page_type(page, type)) )
302 {
303 #ifdef VERBOSE
304 if ( (type & PGT_type_mask) != PGT_l1_page_table )
305 MEM_LOG("Bad page type for pfn %08lx (%08x)",
306 page_nr, page->u.inuse.type_info);
307 #endif
308 put_page(page);
309 return 0;
310 }
312 return 1;
313 }
316 /*
317 * We allow an L2 tables to map each other (a.k.a. linear page tables). It
318 * needs some special care with reference counst and access permissions:
319 * 1. The mapping entry must be read-only, or the guest may get write access
320 * to its own PTEs.
321 * 2. We must only bump the reference counts for an *already validated*
322 * L2 table, or we can end up in a deadlock in get_page_type() by waiting
323 * on a validation that is required to complete that validation.
324 * 3. We only need to increment the reference counts for the mapped page
325 * frame if it is mapped by a different L2 table. This is sufficient and
326 * also necessary to allow validation of an L2 table mapping itself.
327 */
328 static int
329 get_linear_pagetable(
330 l2_pgentry_t l2e, unsigned long pfn, struct domain *d)
331 {
332 u32 x, y;
333 struct pfn_info *page;
335 if ( (l2_pgentry_val(l2e) & _PAGE_RW) )
336 {
337 MEM_LOG("Attempt to create linear p.t. with write perms");
338 return 0;
339 }
341 if ( (l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn )
342 {
343 /* Make sure the mapped frame belongs to the correct domain. */
344 if ( unlikely(!get_page_from_pagenr(l2_pgentry_to_pagenr(l2e), d)) )
345 return 0;
347 /*
348 * Make sure that the mapped frame is an already-validated L2 table.
349 * If so, atomically increment the count (checking for overflow).
350 */
351 page = &frame_table[l2_pgentry_to_pagenr(l2e)];
352 y = page->u.inuse.type_info;
353 do {
354 x = y;
355 if ( unlikely((x & PGT_count_mask) == PGT_count_mask) ||
356 unlikely((x & (PGT_type_mask|PGT_validated)) !=
357 (PGT_l2_page_table|PGT_validated)) )
358 {
359 put_page(page);
360 return 0;
361 }
362 }
363 while ( (y = cmpxchg(&page->u.inuse.type_info, x, x + 1)) != x );
364 }
366 return 1;
367 }
370 static int
371 get_page_from_l1e(
372 l1_pgentry_t l1e, struct domain *d)
373 {
374 unsigned long l1v = l1_pgentry_val(l1e);
375 unsigned long pfn = l1_pgentry_to_pagenr(l1e);
376 struct pfn_info *page = &frame_table[pfn];
377 extern int domain_iomem_in_pfn(struct domain *d, unsigned long pfn);
379 if ( !(l1v & _PAGE_PRESENT) )
380 return 1;
382 if ( unlikely(l1v & (_PAGE_GLOBAL|_PAGE_PAT)) )
383 {
384 MEM_LOG("Bad L1 type settings %04lx", l1v & (_PAGE_GLOBAL|_PAGE_PAT));
385 return 0;
386 }
388 if ( unlikely(!pfn_is_ram(pfn)) )
389 {
390 /* Revert to caller privileges if FD == DOMID_IO. */
391 if ( d == dom_io )
392 d = current;
394 if ( IS_PRIV(d) )
395 return 1;
397 if ( IS_CAPABLE_PHYSDEV(d) )
398 return domain_iomem_in_pfn(d, pfn);
400 MEM_LOG("Non-privileged attempt to map I/O space %08lx", pfn);
401 return 0;
402 }
404 return ((l1v & _PAGE_RW) ?
405 get_page_and_type(page, d, PGT_writable_page) :
406 get_page(d));
407 }
410 /* NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'. */
411 static int
412 get_page_from_l2e(
413 l2_pgentry_t l2e, unsigned long pfn,
414 struct domain *d, unsigned long va_idx)
415 {
416 int rc;
418 if ( !(l2_pgentry_val(l2e) & _PAGE_PRESENT) )
419 return 1;
421 if ( unlikely((l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE))) )
422 {
423 MEM_LOG("Bad L2 page type settings %04lx",
424 l2_pgentry_val(l2e) & (_PAGE_GLOBAL|_PAGE_PSE));
425 return 0;
426 }
428 rc = get_page_and_type_from_pagenr(
429 l2_pgentry_to_pagenr(l2e),
430 PGT_l1_page_table | (va_idx<<PGT_va_shift), d);
432 if ( unlikely(!rc) )
433 return get_linear_pagetable(l2e, pfn, d);
435 return 1;
436 }
439 static void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
440 {
441 unsigned long l1v = l1_pgentry_val(l1e);
442 unsigned long pfn = l1_pgentry_to_pagenr(l1e);
443 struct pfn_info *page = &frame_table[pfn];
444 struct domain *e = page->u.inuse.domain;
446 if ( !(l1v & _PAGE_PRESENT) || !pfn_is_ram(pfn) )
447 return;
449 if ( unlikely(e != d) )
450 {
451 /*
452 * Unmap a foreign page that may have been mapped via a grant table.
453 * Note that this can fail for a privileged domain that can map foreign
454 * pages via MMUEXT_SET_FOREIGNDOM. Such domains can have some mappings
455 * counted via a grant entry and some counted directly in the page
456 * structure's reference count. Note that reference counts won't get
457 * dangerously confused as long as we always try to decrement the
458 * grant entry first. We may end up with a mismatch between which
459 * mappings and which unmappings are counted via the grant entry, but
460 * really it doesn't matter as privileged domains have carte blanche.
461 */
462 if ( likely(gnttab_check_unmap(e, d, pfn, !(l1v & _PAGE_RW))) )
463 return;
464 /* Assume this mapping was made via MMUEXT_SET_FOREIGNDOM... */
465 }
467 if ( l1v & _PAGE_RW )
468 {
469 put_page_and_type(page);
470 }
471 else
472 {
473 /* We expect this is rare so we blow the entire shadow LDT. */
474 if ( unlikely(((page->u.inuse.type_info & PGT_type_mask) ==
475 PGT_ldt_page)) &&
476 unlikely(((page->u.inuse.type_info & PGT_count_mask) != 0)) )
477 invalidate_shadow_ldt(e);
478 put_page(page);
479 }
480 }
483 /*
484 * NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'.
485 * Note also that this automatically deals correctly with linear p.t.'s.
486 */
487 static void put_page_from_l2e(l2_pgentry_t l2e, unsigned long pfn)
488 {
489 if ( (l2_pgentry_val(l2e) & _PAGE_PRESENT) &&
490 ((l2_pgentry_val(l2e) >> PAGE_SHIFT) != pfn) )
491 put_page_and_type(&frame_table[l2_pgentry_to_pagenr(l2e)]);
492 }
495 static int alloc_l2_table(struct pfn_info *page)
496 {
497 struct domain *d = page->u.inuse.domain;
498 unsigned long page_nr = page_to_pfn(page);
499 l2_pgentry_t *pl2e;
500 int i;
502 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
504 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ ) {
505 if ( unlikely(!get_page_from_l2e(pl2e[i], page_nr, d, i)) )
506 goto fail;
507 }
509 #if defined(__i386__)
510 /* Now we add our private high mappings. */
511 memcpy(&pl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
512 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
513 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
514 pl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
515 mk_l2_pgentry((page_nr << PAGE_SHIFT) | __PAGE_HYPERVISOR);
516 pl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
517 mk_l2_pgentry(__pa(page->u.inuse.domain->mm.perdomain_pt) |
518 __PAGE_HYPERVISOR);
519 #endif
521 unmap_domain_mem(pl2e);
522 return 1;
524 fail:
525 while ( i-- > 0 )
526 put_page_from_l2e(pl2e[i], page_nr);
528 unmap_domain_mem(pl2e);
529 return 0;
530 }
533 static int alloc_l1_table(struct pfn_info *page)
534 {
535 struct domain *d = page->u.inuse.domain;
536 unsigned long page_nr = page_to_pfn(page);
537 l1_pgentry_t *pl1e;
538 int i;
540 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
542 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
543 if ( unlikely(!get_page_from_l1e(pl1e[i], d)) )
544 goto fail;
546 unmap_domain_mem(pl1e);
547 return 1;
549 fail:
550 while ( i-- > 0 )
551 put_page_from_l1e(pl1e[i], d);
553 unmap_domain_mem(pl1e);
554 return 0;
555 }
558 static void free_l2_table(struct pfn_info *page)
559 {
560 unsigned long page_nr = page - frame_table;
561 l2_pgentry_t *pl2e;
562 int i;
564 pl2e = map_domain_mem(page_nr << PAGE_SHIFT);
566 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
567 put_page_from_l2e(pl2e[i], page_nr);
569 unmap_domain_mem(pl2e);
570 }
573 static void free_l1_table(struct pfn_info *page)
574 {
575 struct domain *d = page->u.inuse.domain;
576 unsigned long page_nr = page - frame_table;
577 l1_pgentry_t *pl1e;
578 int i;
580 pl1e = map_domain_mem(page_nr << PAGE_SHIFT);
582 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
583 put_page_from_l1e(pl1e[i], d);
585 unmap_domain_mem(pl1e);
586 }
589 static inline int update_l2e(l2_pgentry_t *pl2e,
590 l2_pgentry_t ol2e,
591 l2_pgentry_t nl2e)
592 {
593 unsigned long o = cmpxchg((unsigned long *)pl2e,
594 l2_pgentry_val(ol2e),
595 l2_pgentry_val(nl2e));
596 if ( o != l2_pgentry_val(ol2e) )
597 MEM_LOG("Failed to update %08lx -> %08lx: saw %08lx\n",
598 l2_pgentry_val(ol2e), l2_pgentry_val(nl2e), o);
599 return (o == l2_pgentry_val(ol2e));
600 }
603 /* Update the L2 entry at pl2e to new value nl2e. pl2e is within frame pfn. */
604 static int mod_l2_entry(l2_pgentry_t *pl2e,
605 l2_pgentry_t nl2e,
606 unsigned long pfn)
607 {
608 l2_pgentry_t ol2e;
609 unsigned long _ol2e;
611 if ( unlikely((((unsigned long)pl2e & (PAGE_SIZE-1)) >> 2) >=
612 DOMAIN_ENTRIES_PER_L2_PAGETABLE) )
613 {
614 MEM_LOG("Illegal L2 update attempt in Xen-private area %p", pl2e);
615 return 0;
616 }
618 if ( unlikely(__get_user(_ol2e, (unsigned long *)pl2e) != 0) )
619 return 0;
620 ol2e = mk_l2_pgentry(_ol2e);
622 if ( l2_pgentry_val(nl2e) & _PAGE_PRESENT )
623 {
624 /* Differ in mapping (bits 12-31) or presence (bit 0)? */
625 if ( ((l2_pgentry_val(ol2e) ^ l2_pgentry_val(nl2e)) & ~0xffe) == 0 )
626 return update_l2e(pl2e, ol2e, nl2e);
628 if ( unlikely(!get_page_from_l2e(nl2e, pfn, current,
629 ((unsigned long)pl2e &
630 ~PAGE_MASK) >> 2)) )
631 return 0;
633 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
634 {
635 put_page_from_l2e(nl2e, pfn);
636 return 0;
637 }
639 put_page_from_l2e(ol2e, pfn);
640 return 1;
641 }
643 if ( unlikely(!update_l2e(pl2e, ol2e, nl2e)) )
644 return 0;
646 put_page_from_l2e(ol2e, pfn);
647 return 1;
648 }
651 static inline int update_l1e(l1_pgentry_t *pl1e,
652 l1_pgentry_t ol1e,
653 l1_pgentry_t nl1e)
654 {
655 unsigned long o = l1_pgentry_val(ol1e);
656 unsigned long n = l1_pgentry_val(nl1e);
658 if ( unlikely(cmpxchg_user(pl1e, o, n) != 0) ||
659 unlikely(o != l1_pgentry_val(ol1e)) )
660 {
661 MEM_LOG("Failed to update %08lx -> %08lx: saw %08lx\n",
662 l1_pgentry_val(ol1e), l1_pgentry_val(nl1e), o);
663 return 0;
664 }
666 return 1;
667 }
670 /* Update the L1 entry at pl1e to new value nl1e. */
671 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e)
672 {
673 l1_pgentry_t ol1e;
674 unsigned long _ol1e;
675 struct domain *d = current;
677 if ( unlikely(__get_user(_ol1e, (unsigned long *)pl1e) != 0) )
678 {
679 MEM_LOG("Bad get_user\n");
680 return 0;
681 }
683 ol1e = mk_l1_pgentry(_ol1e);
685 if ( l1_pgentry_val(nl1e) & _PAGE_PRESENT )
686 {
687 /* Differ in mapping (bits 12-31), r/w (bit 1), or presence (bit 0)? */
688 if ( ((l1_pgentry_val(ol1e) ^ l1_pgentry_val(nl1e)) & ~0xffc) == 0 )
689 return update_l1e(pl1e, ol1e, nl1e);
691 if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
692 return 0;
694 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
695 {
696 put_page_from_l1e(nl1e, d);
697 return 0;
698 }
700 put_page_from_l1e(ol1e, d);
701 return 1;
702 }
704 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e)) )
705 return 0;
707 put_page_from_l1e(ol1e, d);
708 return 1;
709 }
712 int alloc_page_type(struct pfn_info *page, unsigned int type)
713 {
714 switch ( type )
715 {
716 case PGT_l1_page_table:
717 return alloc_l1_table(page);
718 case PGT_l2_page_table:
719 return alloc_l2_table(page);
720 case PGT_gdt_page:
721 case PGT_ldt_page:
722 return alloc_segdesc_page(page);
723 default:
724 printk("Bad type in alloc_page_type %x t=%x c=%x\n",
725 type, page->u.inuse.type_info,
726 page->count_info);
727 BUG();
728 }
730 return 0;
731 }
734 void free_page_type(struct pfn_info *page, unsigned int type)
735 {
736 struct domain *d = page->u.inuse.domain;
738 switch ( type )
739 {
740 case PGT_l1_page_table:
741 free_l1_table(page);
742 break;
744 case PGT_l2_page_table:
745 free_l2_table(page);
746 break;
748 default:
749 BUG();
750 }
752 if ( unlikely(d->mm.shadow_mode) &&
753 (get_shadow_status(&d->mm, page_to_pfn(page)) & PSH_shadowed) )
754 {
755 unshadow_table(page_to_pfn(page), type);
756 put_shadow_status(&d->mm);
757 }
758 }
761 void put_page_type(struct pfn_info *page)
762 {
763 u32 nx, x, y = page->u.inuse.type_info;
765 again:
766 do {
767 x = y;
768 nx = x - 1;
770 ASSERT((x & PGT_count_mask) != 0);
772 /*
773 * The page should always be validated while a reference is held. The
774 * exception is during domain destruction, when we forcibly invalidate
775 * page-table pages if we detect a referential loop.
776 * See domain.c:relinquish_list().
777 */
778 ASSERT((x & PGT_validated) ||
779 test_bit(DF_DYING, &page->u.inuse.domain->flags));
781 if ( unlikely((nx & PGT_count_mask) == 0) )
782 {
783 /* Record TLB information for flush later. Races are harmless. */
784 page->tlbflush_timestamp = tlbflush_clock;
786 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
787 likely(nx & PGT_validated) )
788 {
789 /*
790 * Page-table pages must be unvalidated when count is zero. The
791 * 'free' is safe because the refcnt is non-zero and validated
792 * bit is clear => other ops will spin or fail.
793 */
794 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
795 x & ~PGT_validated)) != x) )
796 goto again;
797 /* We cleared the 'valid bit' so we do the clear up. */
798 free_page_type(page, x & PGT_type_mask);
799 /* Carry on, but with the 'valid bit' now clear. */
800 x &= ~PGT_validated;
801 nx &= ~PGT_validated;
802 }
803 }
804 else if ( unlikely((nx & (PGT_pinned | PGT_count_mask)) ==
805 (PGT_pinned | 1)) )
806 {
807 /* Page is now only pinned. Make the back pointer mutable again. */
808 nx |= PGT_va_mutable;
809 }
810 }
811 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
812 }
815 int get_page_type(struct pfn_info *page, u32 type)
816 {
817 u32 nx, x, y = page->u.inuse.type_info;
819 again:
820 do {
821 x = y;
822 nx = x + 1;
823 if ( unlikely((nx & PGT_count_mask) == 0) )
824 {
825 MEM_LOG("Type count overflow on pfn %08lx\n", page_to_pfn(page));
826 return 0;
827 }
828 else if ( unlikely((x & PGT_count_mask) == 0) )
829 {
830 if ( (x & (PGT_type_mask|PGT_va_mask)) != type )
831 {
832 /*
833 * On type change we check to flush stale TLB entries. This
834 * may be unnecessary (e.g., page was GDT/LDT) but those
835 * circumstances should be very rare.
836 */
837 struct domain *d = page->u.inuse.domain;
838 if ( unlikely(NEED_FLUSH(tlbflush_time[d->processor],
839 page->tlbflush_timestamp)) )
840 {
841 perfc_incr(need_flush_tlb_flush);
842 flush_tlb_cpu(d->processor);
843 }
845 /* We lose existing type, back pointer, and validity. */
846 nx &= ~(PGT_type_mask | PGT_va_mask | PGT_validated);
847 nx |= type;
849 /* No special validation needed for writable pages. */
850 /* Page tables and GDT/LDT need to be scanned for validity. */
851 if ( type == PGT_writable_page )
852 nx |= PGT_validated;
853 }
854 }
855 else if ( unlikely((x & (PGT_type_mask|PGT_va_mask)) != type) )
856 {
857 if ( unlikely((x & PGT_type_mask) != (type & PGT_type_mask) ) )
858 {
859 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
860 ((type & PGT_type_mask) != PGT_l1_page_table) )
861 MEM_LOG("Bad type (saw %08x != exp %08x) for pfn %08lx\n",
862 x & PGT_type_mask, type, page_to_pfn(page));
863 return 0;
864 }
865 else if ( (x & PGT_va_mask) == PGT_va_mutable )
866 {
867 /* The va backpointer is mutable, hence we update it. */
868 nx &= ~PGT_va_mask;
869 nx |= type; /* we know the actual type is correct */
870 }
871 else if ( unlikely((x & PGT_va_mask) != (type & PGT_va_mask)) )
872 {
873 /* This table is potentially mapped at multiple locations. */
874 nx &= ~PGT_va_mask;
875 nx |= PGT_va_unknown;
876 }
877 }
878 else if ( unlikely(!(x & PGT_validated)) )
879 {
880 /* Someone else is updating validation of this page. Wait... */
881 while ( (y = page->u.inuse.type_info) == x )
882 {
883 rep_nop();
884 barrier();
885 }
886 goto again;
887 }
888 }
889 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
891 if ( unlikely(!(nx & PGT_validated)) )
892 {
893 /* Try to validate page type; drop the new reference on failure. */
894 if ( unlikely(!alloc_page_type(page, type & PGT_type_mask)) )
895 {
896 MEM_LOG("Error while validating pfn %08lx for type %08x."
897 " caf=%08x taf=%08x\n",
898 page_to_pfn(page), type,
899 page->count_info,
900 page->u.inuse.type_info);
901 /* Noone else can get a reference. We hold the only ref. */
902 page->u.inuse.type_info = 0;
903 return 0;
904 }
906 /* Noone else is updating simultaneously. */
907 __set_bit(_PGT_validated, &page->u.inuse.type_info);
908 }
910 return 1;
911 }
914 static int do_extended_command(unsigned long ptr, unsigned long val)
915 {
916 int okay = 1, cpu = smp_processor_id();
917 unsigned int cmd = val & MMUEXT_CMD_MASK;
918 unsigned long pfn = ptr >> PAGE_SHIFT;
919 unsigned long old_base_pfn;
920 struct pfn_info *page = &frame_table[pfn];
921 struct domain *d = current, *nd, *e;
922 u32 x, y;
923 domid_t domid;
924 grant_ref_t gntref;
926 switch ( cmd )
927 {
928 case MMUEXT_PIN_L1_TABLE:
929 case MMUEXT_PIN_L2_TABLE:
930 /*
931 * We insist that, if you pin an L1 page, it's the first thing that
932 * you do to it. This is because we require the backptr to still be
933 * mutable. This assumption seems safe.
934 */
935 okay = get_page_and_type_from_pagenr(
936 pfn,
937 ((cmd==MMUEXT_PIN_L2_TABLE) ?
938 PGT_l2_page_table : (PGT_l1_page_table|PGT_va_mutable)),
939 FOREIGNDOM);
941 if ( unlikely(!okay) )
942 {
943 MEM_LOG("Error while pinning pfn %08lx", pfn);
944 break;
945 }
947 if ( unlikely(test_and_set_bit(_PGT_pinned,
948 &page->u.inuse.type_info)) )
949 {
950 MEM_LOG("Pfn %08lx already pinned", pfn);
951 put_page_and_type(page);
952 okay = 0;
953 break;
954 }
956 break;
958 case MMUEXT_UNPIN_TABLE:
959 if ( unlikely(!(okay = get_page_from_pagenr(pfn, FOREIGNDOM))) )
960 {
961 MEM_LOG("Page %08lx bad domain (dom=%p)",
962 ptr, page->u.inuse.domain);
963 }
964 else if ( likely(test_and_clear_bit(_PGT_pinned,
965 &page->u.inuse.type_info)) )
966 {
967 put_page_and_type(page);
968 put_page(page);
969 }
970 else
971 {
972 okay = 0;
973 put_page(page);
974 MEM_LOG("Pfn %08lx not pinned", pfn);
975 }
976 break;
978 case MMUEXT_NEW_BASEPTR:
979 okay = get_page_and_type_from_pagenr(pfn, PGT_l2_page_table, d);
980 if ( likely(okay) )
981 {
982 invalidate_shadow_ldt(d);
984 percpu_info[cpu].deferred_ops &= ~DOP_FLUSH_TLB;
985 old_base_pfn = pagetable_val(d->mm.pagetable) >> PAGE_SHIFT;
986 d->mm.pagetable = mk_pagetable(pfn << PAGE_SHIFT);
988 shadow_mk_pagetable(&d->mm);
990 write_ptbase(&d->mm);
992 put_page_and_type(&frame_table[old_base_pfn]);
994 /*
995 * Note that we tick the clock /after/ dropping the old base's
996 * reference count. If the page tables got freed then this will
997 * avoid unnecessary TLB flushes when the pages are reused. */
998 tlb_clocktick();
999 }
1000 else
1002 MEM_LOG("Error while installing new baseptr %08lx", ptr);
1004 break;
1006 case MMUEXT_TLB_FLUSH:
1007 percpu_info[cpu].deferred_ops |= DOP_FLUSH_TLB;
1008 break;
1010 case MMUEXT_INVLPG:
1011 __flush_tlb_one(ptr);
1012 break;
1014 case MMUEXT_FLUSH_CACHE:
1015 if ( unlikely(!IS_CAPABLE_PHYSDEV(d)) )
1017 MEM_LOG("Non-physdev domain tried to FLUSH_CACHE.\n");
1018 okay = 0;
1020 else
1022 wbinvd();
1024 break;
1026 case MMUEXT_SET_LDT:
1028 unsigned long ents = val >> MMUEXT_CMD_SHIFT;
1029 if ( ((ptr & (PAGE_SIZE-1)) != 0) ||
1030 (ents > 8192) ||
1031 ((ptr+ents*LDT_ENTRY_SIZE) < ptr) ||
1032 ((ptr+ents*LDT_ENTRY_SIZE) > PAGE_OFFSET) )
1034 okay = 0;
1035 MEM_LOG("Bad args to SET_LDT: ptr=%08lx, ents=%08lx", ptr, ents);
1037 else if ( (d->mm.ldt_ents != ents) ||
1038 (d->mm.ldt_base != ptr) )
1040 invalidate_shadow_ldt(d);
1041 d->mm.ldt_base = ptr;
1042 d->mm.ldt_ents = ents;
1043 load_LDT(d);
1044 percpu_info[cpu].deferred_ops &= ~DOP_RELOAD_LDT;
1045 if ( ents != 0 )
1046 percpu_info[cpu].deferred_ops |= DOP_RELOAD_LDT;
1048 break;
1051 case MMUEXT_SET_FOREIGNDOM:
1052 domid = (domid_t)(val >> 16);
1054 if ( (e = percpu_info[cpu].foreign) != NULL )
1055 put_domain(e);
1056 percpu_info[cpu].foreign = NULL;
1058 if ( !IS_PRIV(d) )
1060 switch ( domid )
1062 case DOMID_IO:
1063 get_knownalive_domain(dom_io);
1064 percpu_info[cpu].foreign = dom_io;
1065 break;
1066 default:
1067 MEM_LOG("Dom %u cannot set foreign dom\n", d->id);
1068 okay = 0;
1069 break;
1072 else
1074 percpu_info[cpu].foreign = e = find_domain_by_id(domid);
1075 if ( e == NULL )
1077 switch ( domid )
1079 case DOMID_XEN:
1080 get_knownalive_domain(dom_xen);
1081 percpu_info[cpu].foreign = dom_xen;
1082 break;
1083 case DOMID_IO:
1084 get_knownalive_domain(dom_io);
1085 percpu_info[cpu].foreign = dom_io;
1086 break;
1087 default:
1088 MEM_LOG("Unknown domain '%u'", domid);
1089 okay = 0;
1090 break;
1094 break;
1096 case MMUEXT_TRANSFER_PAGE:
1097 domid = (domid_t)(val >> 16);
1098 gntref = (grant_ref_t)((val & 0xFF00) | ((ptr >> 2) & 0x00FF));
1100 if ( unlikely(IS_XEN_HEAP_FRAME(page)) ||
1101 unlikely(!pfn_is_ram(pfn)) ||
1102 unlikely((e = find_domain_by_id(domid)) == NULL) )
1104 MEM_LOG("Bad frame (%08lx) or bad domid (%d).\n", pfn, domid);
1105 okay = 0;
1106 break;
1109 spin_lock(&d->page_alloc_lock);
1111 /*
1112 * The tricky bit: atomically release ownership while there is just one
1113 * benign reference to the page (PGC_allocated). If that reference
1114 * disappears then the deallocation routine will safely spin.
1115 */
1116 nd = page->u.inuse.domain;
1117 y = page->count_info;
1118 do {
1119 x = y;
1120 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1121 (1|PGC_allocated)) ||
1122 unlikely(nd != d) )
1124 MEM_LOG("Bad page values %08lx: ed=%p(%u), sd=%p,"
1125 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1126 d, d->id, nd, x, page->u.inuse.type_info);
1127 spin_unlock(&d->page_alloc_lock);
1128 put_domain(e);
1129 return 0;
1131 __asm__ __volatile__(
1132 LOCK_PREFIX "cmpxchg8b %2"
1133 : "=d" (nd), "=a" (y),
1134 "=m" (*(volatile u64 *)(&page->count_info))
1135 : "0" (d), "1" (x), "c" (NULL), "b" (x) );
1137 while ( unlikely(nd != d) || unlikely(y != x) );
1139 /*
1140 * Unlink from 'd'. At least one reference remains (now anonymous), so
1141 * noone else is spinning to try to delete this page from 'd'.
1142 */
1143 d->tot_pages--;
1144 list_del(&page->list);
1146 spin_unlock(&d->page_alloc_lock);
1148 spin_lock(&e->page_alloc_lock);
1150 /*
1151 * Check that 'e' will accept the page and has reservation headroom.
1152 * Also, a domain mustn't have PGC_allocated pages when it is dying.
1153 */
1154 ASSERT(e->tot_pages <= e->max_pages);
1155 if ( unlikely(test_bit(DF_DYING, &e->flags)) ||
1156 unlikely(e->tot_pages == e->max_pages) ||
1157 unlikely(!gnttab_prepare_for_transfer(e, d, gntref)) )
1159 MEM_LOG("Transferee has no reservation headroom (%d,%d), or "
1160 "provided a bad grant ref, or is dying (%08lx).\n",
1161 e->tot_pages, e->max_pages, e->flags);
1162 spin_unlock(&e->page_alloc_lock);
1163 put_domain(e);
1164 okay = 0;
1165 break;
1168 /* Okay, add the page to 'e'. */
1169 if ( unlikely(e->tot_pages++ == 0) )
1170 get_knownalive_domain(e);
1171 list_add_tail(&page->list, &e->page_list);
1172 page->u.inuse.domain = e;
1174 spin_unlock(&e->page_alloc_lock);
1176 /* Transfer is all done: tell the guest about its new page frame. */
1177 gnttab_notify_transfer(e, gntref, pfn);
1179 put_domain(e);
1180 break;
1182 case MMUEXT_REASSIGN_PAGE:
1183 if ( unlikely(!IS_PRIV(d)) )
1185 MEM_LOG("Dom %u has no reassignment priv", d->id);
1186 okay = 0;
1187 break;
1190 e = percpu_info[cpu].foreign;
1191 if ( unlikely(e == NULL) )
1193 MEM_LOG("No FOREIGNDOM to reassign pfn %08lx to", pfn);
1194 okay = 0;
1195 break;
1198 /*
1199 * Grab both page_list locks, in order. This prevents the page from
1200 * disappearing elsewhere while we modify the owner, and we'll need
1201 * both locks if we're successful so that we can change lists.
1202 */
1203 if ( d < e )
1205 spin_lock(&d->page_alloc_lock);
1206 spin_lock(&e->page_alloc_lock);
1208 else
1210 spin_lock(&e->page_alloc_lock);
1211 spin_lock(&d->page_alloc_lock);
1214 /* A domain shouldn't have PGC_allocated pages when it is dying. */
1215 if ( unlikely(test_bit(DF_DYING, &e->flags)) ||
1216 unlikely(IS_XEN_HEAP_FRAME(page)) )
1218 MEM_LOG("Reassignment page is Xen heap, or dest dom is dying.");
1219 okay = 0;
1220 goto reassign_fail;
1223 /*
1224 * The tricky bit: atomically change owner while there is just one
1225 * benign reference to the page (PGC_allocated). If that reference
1226 * disappears then the deallocation routine will safely spin.
1227 */
1228 nd = page->u.inuse.domain;
1229 y = page->count_info;
1230 do {
1231 x = y;
1232 if ( unlikely((x & (PGC_count_mask|PGC_allocated)) !=
1233 (1|PGC_allocated)) ||
1234 unlikely(nd != d) )
1236 MEM_LOG("Bad page values %08lx: ed=%p(%u), sd=%p,"
1237 " caf=%08x, taf=%08x\n", page_to_pfn(page),
1238 d, d->id, nd, x, page->u.inuse.type_info);
1239 okay = 0;
1240 goto reassign_fail;
1242 __asm__ __volatile__(
1243 LOCK_PREFIX "cmpxchg8b %3"
1244 : "=d" (nd), "=a" (y), "=c" (e),
1245 "=m" (*(volatile u64 *)(&page->count_info))
1246 : "0" (d), "1" (x), "c" (e), "b" (x) );
1248 while ( unlikely(nd != d) || unlikely(y != x) );
1250 /*
1251 * Unlink from 'd'. We transferred at least one reference to 'e', so
1252 * noone else is spinning to try to delete this page from 'd'.
1253 */
1254 d->tot_pages--;
1255 list_del(&page->list);
1257 /*
1258 * Add the page to 'e'. Someone may already have removed the last
1259 * reference and want to remove the page from 'e'. However, we have
1260 * the lock so they'll spin waiting for us.
1261 */
1262 if ( unlikely(e->tot_pages++ == 0) )
1263 get_knownalive_domain(e);
1264 list_add_tail(&page->list, &e->page_list);
1266 reassign_fail:
1267 spin_unlock(&d->page_alloc_lock);
1268 spin_unlock(&e->page_alloc_lock);
1269 break;
1271 case MMUEXT_CLEAR_FOREIGNDOM:
1272 if ( (e = percpu_info[cpu].foreign) != NULL )
1273 put_domain(e);
1274 percpu_info[cpu].foreign = NULL;
1275 break;
1277 default:
1278 MEM_LOG("Invalid extended pt command 0x%08lx", val & MMUEXT_CMD_MASK);
1279 okay = 0;
1280 break;
1283 return okay;
1287 int do_mmu_update(mmu_update_t *ureqs, int count, int *success_count)
1289 mmu_update_t req;
1290 unsigned long va = 0, deferred_ops, pfn, prev_pfn = 0;
1291 struct pfn_info *page;
1292 int rc = 0, okay = 1, i, cpu = smp_processor_id();
1293 unsigned int cmd;
1294 unsigned long prev_spfn = 0;
1295 l1_pgentry_t *prev_spl1e = 0;
1296 struct domain *d = current;
1297 u32 type_info;
1299 perfc_incrc(calls_to_mmu_update);
1300 perfc_addc(num_page_updates, count);
1302 cleanup_writable_pagetable(d, PTWR_CLEANUP_ACTIVE | PTWR_CLEANUP_INACTIVE);
1304 if ( unlikely(!access_ok(VERIFY_READ, ureqs, count * sizeof(req))) )
1305 return -EFAULT;
1307 for ( i = 0; i < count; i++ )
1309 if ( unlikely(__copy_from_user(&req, ureqs, sizeof(req)) != 0) )
1311 MEM_LOG("Bad __copy_from_user");
1312 rc = -EFAULT;
1313 break;
1316 cmd = req.ptr & (sizeof(l1_pgentry_t)-1);
1317 pfn = req.ptr >> PAGE_SHIFT;
1319 okay = 0;
1321 switch ( cmd )
1323 /*
1324 * MMU_NORMAL_PT_UPDATE: Normal update to any level of page table.
1325 */
1326 case MMU_NORMAL_PT_UPDATE:
1327 if ( unlikely(!get_page_from_pagenr(pfn, current)) )
1329 MEM_LOG("Could not get page for normal update");
1330 break;
1333 if ( likely(prev_pfn == pfn) )
1335 va = (va & PAGE_MASK) | (req.ptr & ~PAGE_MASK);
1337 else
1339 if ( prev_pfn != 0 )
1340 unmap_domain_mem((void *)va);
1341 va = (unsigned long)map_domain_mem(req.ptr);
1342 prev_pfn = pfn;
1345 page = &frame_table[pfn];
1346 switch ( (type_info = page->u.inuse.type_info) & PGT_type_mask )
1348 case PGT_l1_page_table:
1349 if ( likely(get_page_type(
1350 page, type_info & (PGT_type_mask|PGT_va_mask))) )
1352 okay = mod_l1_entry((l1_pgentry_t *)va,
1353 mk_l1_pgentry(req.val));
1355 if ( unlikely(d->mm.shadow_mode) && okay &&
1356 (get_shadow_status(&d->mm, page-frame_table) &
1357 PSH_shadowed) )
1359 shadow_l1_normal_pt_update(
1360 req.ptr, req.val, &prev_spfn, &prev_spl1e);
1361 put_shadow_status(&d->mm);
1364 put_page_type(page);
1366 break;
1367 case PGT_l2_page_table:
1368 if ( likely(get_page_type(page, PGT_l2_page_table)) )
1370 okay = mod_l2_entry((l2_pgentry_t *)va,
1371 mk_l2_pgentry(req.val),
1372 pfn);
1374 if ( unlikely(d->mm.shadow_mode) && okay &&
1375 (get_shadow_status(&d->mm, page-frame_table) &
1376 PSH_shadowed) )
1378 shadow_l2_normal_pt_update(req.ptr, req.val);
1379 put_shadow_status(&d->mm);
1382 put_page_type(page);
1384 break;
1385 default:
1386 if ( likely(get_page_type(page, PGT_writable_page)) )
1388 *(unsigned long *)va = req.val;
1389 okay = 1;
1390 put_page_type(page);
1392 break;
1395 put_page(page);
1396 break;
1398 case MMU_MACHPHYS_UPDATE:
1399 if ( unlikely(!get_page_from_pagenr(pfn, FOREIGNDOM)) )
1401 MEM_LOG("Could not get page for mach->phys update");
1402 break;
1405 machine_to_phys_mapping[pfn] = req.val;
1406 okay = 1;
1408 /*
1409 * If in log-dirty mode, mark the corresponding pseudo-physical
1410 * page as dirty.
1411 */
1412 if ( unlikely(d->mm.shadow_mode == SHM_logdirty) &&
1413 mark_dirty(&d->mm, pfn) )
1414 d->mm.shadow_dirty_block_count++;
1416 put_page(&frame_table[pfn]);
1417 break;
1419 /*
1420 * MMU_EXTENDED_COMMAND: Extended command is specified
1421 * in the least-siginificant bits of the 'value' field.
1422 */
1423 case MMU_EXTENDED_COMMAND:
1424 req.ptr &= ~(sizeof(l1_pgentry_t) - 1);
1425 okay = do_extended_command(req.ptr, req.val);
1426 break;
1428 default:
1429 MEM_LOG("Invalid page update command %08lx", req.ptr);
1430 break;
1433 if ( unlikely(!okay) )
1435 rc = -EINVAL;
1436 break;
1439 ureqs++;
1442 if ( prev_pfn != 0 )
1443 unmap_domain_mem((void *)va);
1445 if ( unlikely(prev_spl1e != 0) )
1446 unmap_domain_mem((void *)prev_spl1e);
1448 deferred_ops = percpu_info[cpu].deferred_ops;
1449 percpu_info[cpu].deferred_ops = 0;
1451 if ( deferred_ops & DOP_FLUSH_TLB )
1452 local_flush_tlb();
1454 if ( deferred_ops & DOP_RELOAD_LDT )
1455 (void)map_ldt_shadow_page(0);
1457 if ( unlikely(percpu_info[cpu].foreign != NULL) )
1459 put_domain(percpu_info[cpu].foreign);
1460 percpu_info[cpu].foreign = NULL;
1463 if ( unlikely(success_count != NULL) )
1464 put_user(count, success_count);
1466 return rc;
1470 int do_update_va_mapping(unsigned long page_nr,
1471 unsigned long val,
1472 unsigned long flags)
1474 struct domain *d = current;
1475 int err = 0;
1476 unsigned int cpu = d->processor;
1477 unsigned long deferred_ops;
1479 perfc_incrc(calls_to_update_va);
1481 if ( unlikely(page_nr >= (HYPERVISOR_VIRT_START >> PAGE_SHIFT)) )
1482 return -EINVAL;
1484 cleanup_writable_pagetable(d, PTWR_CLEANUP_ACTIVE | PTWR_CLEANUP_INACTIVE);
1486 /*
1487 * XXX When we make this support 4MB superpages we should also deal with
1488 * the case of updating L2 entries.
1489 */
1491 if ( unlikely(!mod_l1_entry(&linear_pg_table[page_nr],
1492 mk_l1_pgentry(val))) )
1493 err = -EINVAL;
1495 if ( unlikely(d->mm.shadow_mode) )
1497 unsigned long sval;
1499 l1pte_propagate_from_guest(&d->mm, &val, &sval);
1501 if ( unlikely(__put_user(sval, ((unsigned long *)(
1502 &shadow_linear_pg_table[page_nr])))) )
1504 /*
1505 * Since L2's are guranteed RW, failure indicates the page was not
1506 * shadowed, so ignore.
1507 */
1508 perfc_incrc(shadow_update_va_fail);
1511 /*
1512 * If we're in log-dirty mode then we need to note that we've updated
1513 * the PTE in the PT-holding page. We need the machine frame number
1514 * for this.
1515 */
1516 if ( d->mm.shadow_mode == SHM_logdirty )
1517 mark_dirty(&current->mm, va_to_l1mfn(page_nr << PAGE_SHIFT));
1519 check_pagetable(&d->mm, d->mm.pagetable, "va"); /* debug */
1522 deferred_ops = percpu_info[cpu].deferred_ops;
1523 percpu_info[cpu].deferred_ops = 0;
1525 if ( unlikely(deferred_ops & DOP_FLUSH_TLB) ||
1526 unlikely(flags & UVMF_FLUSH_TLB) )
1527 local_flush_tlb();
1528 else if ( unlikely(flags & UVMF_INVLPG) )
1529 __flush_tlb_one(page_nr << PAGE_SHIFT);
1531 if ( unlikely(deferred_ops & DOP_RELOAD_LDT) )
1532 (void)map_ldt_shadow_page(0);
1534 return err;
1537 int do_update_va_mapping_otherdomain(unsigned long page_nr,
1538 unsigned long val,
1539 unsigned long flags,
1540 domid_t domid)
1542 unsigned int cpu = smp_processor_id();
1543 struct domain *d;
1544 int rc;
1546 if ( unlikely(!IS_PRIV(current)) )
1547 return -EPERM;
1549 percpu_info[cpu].foreign = d = find_domain_by_id(domid);
1550 if ( unlikely(d == NULL) )
1552 MEM_LOG("Unknown domain '%u'", domid);
1553 return -ESRCH;
1556 rc = do_update_va_mapping(page_nr, val, flags);
1558 put_domain(d);
1559 percpu_info[cpu].foreign = NULL;
1561 return rc;
1566 /*************************
1567 * Writable Pagetables
1568 */
1570 ptwr_info_t ptwr_info[NR_CPUS];
1572 #ifdef VERBOSE
1573 int ptwr_debug = 0x0;
1574 #define PTWR_PRINTK(_f, _a...) \
1575 do { if ( unlikely(ptwr_debug) ) printk( _f , ## _a ); } while ( 0 )
1576 #define PTWR_PRINT_WHICH (which ? 'I' : 'A')
1577 #else
1578 #define PTWR_PRINTK(_f, _a...) ((void)0)
1579 #endif
1581 /* Flush the given writable p.t. page and write-protect it again. */
1582 void ptwr_flush(const int which)
1584 unsigned long sstat, spte, pte, *ptep, l1va;
1585 l1_pgentry_t *sl1e = NULL, *pl1e, ol1e, nl1e;
1586 l2_pgentry_t *pl2e, nl2e;
1587 int i, cpu = smp_processor_id();
1588 struct domain *d = current;
1590 l1va = ptwr_info[cpu].ptinfo[which].l1va;
1591 ptep = (unsigned long *)&linear_pg_table[l1va>>PAGE_SHIFT];
1593 /*
1594 * STEP 1. Write-protect the p.t. page so no more updates can occur.
1595 */
1597 if ( unlikely(__get_user(pte, ptep)) )
1599 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
1600 /*
1601 * Really a bug. We could read this PTE during the initial fault,
1602 * and pagetables can't have changed meantime. XXX Multi-proc guests?
1603 */
1604 BUG();
1606 PTWR_PRINTK("[%c] disconnected_l1va at %p is %08lx\n",
1607 PTWR_PRINT_WHICH, ptep, pte);
1608 pte &= ~_PAGE_RW;
1610 if ( unlikely(d->mm.shadow_mode) )
1612 /* Write-protect the p.t. page in the shadow page table. */
1613 l1pte_propagate_from_guest(&d->mm, &pte, &spte);
1614 __put_user(
1615 spte, (unsigned long *)&shadow_linear_pg_table[l1va>>PAGE_SHIFT]);
1617 /* Is the p.t. page itself shadowed? Map it into Xen space if so. */
1618 sstat = get_shadow_status(&d->mm, pte >> PAGE_SHIFT);
1619 if ( sstat & PSH_shadowed )
1620 sl1e = map_domain_mem((sstat & PSH_pfn_mask) << PAGE_SHIFT);
1623 /* Write-protect the p.t. page in the guest page table. */
1624 if ( unlikely(__put_user(pte, ptep)) )
1626 MEM_LOG("ptwr: Could not update pte at %p\n", ptep);
1627 /*
1628 * Really a bug. We could write this PTE during the initial fault,
1629 * and pagetables can't have changed meantime. XXX Multi-proc guests?
1630 */
1631 BUG();
1634 /* Ensure that there are no stale writable mappings in any TLB. */
1635 __flush_tlb_one(l1va);
1636 PTWR_PRINTK("[%c] disconnected_l1va at %p now %08lx\n",
1637 PTWR_PRINT_WHICH, ptep, pte);
1639 /*
1640 * STEP 2. Validate any modified PTEs.
1641 */
1643 pl1e = ptwr_info[cpu].ptinfo[which].pl1e;
1644 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
1646 ol1e = ptwr_info[cpu].ptinfo[which].page[i];
1647 nl1e = pl1e[i];
1649 if ( likely(l1_pgentry_val(ol1e) == l1_pgentry_val(nl1e)) )
1650 continue;
1652 /*
1653 * Fast path for PTEs that have merely been write-protected
1654 * (e.g., during a Unix fork()). A strict reduction in privilege.
1655 */
1656 if ( likely(l1_pgentry_val(ol1e) == (l1_pgentry_val(nl1e)|_PAGE_RW)) )
1658 if ( likely(l1_pgentry_val(nl1e) & _PAGE_PRESENT) )
1660 if ( unlikely(sl1e != NULL) )
1661 l1pte_propagate_from_guest(
1662 &d->mm, &l1_pgentry_val(nl1e),
1663 &l1_pgentry_val(sl1e[i]));
1664 put_page_type(&frame_table[l1_pgentry_to_pagenr(nl1e)]);
1666 continue;
1669 if ( unlikely(!get_page_from_l1e(nl1e, d)) )
1671 MEM_LOG("ptwr: Could not re-validate l1 page\n");
1672 /*
1673 * Make the remaining p.t's consistent before crashing, so the
1674 * reference counts are correct.
1675 */
1676 memcpy(&pl1e[i], &ptwr_info[cpu].ptinfo[which].page[i],
1677 (ENTRIES_PER_L1_PAGETABLE - i) * sizeof(l1_pgentry_t));
1678 unmap_domain_mem(pl1e);
1679 ptwr_info[cpu].ptinfo[which].l1va = 0;
1680 domain_crash();
1683 if ( unlikely(sl1e != NULL) )
1684 l1pte_propagate_from_guest(
1685 &d->mm, &l1_pgentry_val(nl1e), &l1_pgentry_val(sl1e[i]));
1687 if ( unlikely(l1_pgentry_val(ol1e) & _PAGE_PRESENT) )
1688 put_page_from_l1e(ol1e, d);
1690 unmap_domain_mem(pl1e);
1692 /*
1693 * STEP 3. Reattach the L1 p.t. page into the current address space.
1694 */
1696 if ( (which == PTWR_PT_ACTIVE) && likely(!d->mm.shadow_mode) )
1698 pl2e = &linear_l2_table[ptwr_info[cpu].ptinfo[which].l2_idx];
1699 nl2e = mk_l2_pgentry(l2_pgentry_val(*pl2e) | _PAGE_PRESENT);
1700 update_l2e(pl2e, *pl2e, nl2e);
1703 /*
1704 * STEP 4. Final tidy-up.
1705 */
1707 ptwr_info[cpu].ptinfo[which].l1va = 0;
1709 if ( unlikely(sl1e != NULL) )
1711 unmap_domain_mem(sl1e);
1712 put_shadow_status(&d->mm);
1716 /* Write page fault handler: check if guest is trying to modify a PTE. */
1717 int ptwr_do_page_fault(unsigned long addr)
1719 unsigned long pte, pfn;
1720 struct pfn_info *page;
1721 l2_pgentry_t *pl2e, nl2e;
1722 int which, cpu = smp_processor_id();
1723 u32 l2_idx;
1725 /*
1726 * Attempt to read the PTE that maps the VA being accessed. By checking for
1727 * PDE validity in the L2 we avoid many expensive fixups in __get_user().
1728 */
1729 if ( !(l2_pgentry_val(linear_l2_table[addr>>L2_PAGETABLE_SHIFT]) &
1730 _PAGE_PRESENT) ||
1731 __get_user(pte, (unsigned long *)&linear_pg_table[addr>>PAGE_SHIFT]) )
1732 return 0;
1734 pfn = pte >> PAGE_SHIFT;
1735 page = &frame_table[pfn];
1737 /* We are looking only for read-only mappings of p.t. pages. */
1738 if ( ((pte & (_PAGE_RW | _PAGE_PRESENT)) != _PAGE_PRESENT) ||
1739 ((page->u.inuse.type_info & PGT_type_mask) != PGT_l1_page_table) )
1740 return 0;
1742 /* Get the L2 index at which this L1 p.t. is always mapped. */
1743 l2_idx = page->u.inuse.type_info & PGT_va_mask;
1744 if ( unlikely(l2_idx >= PGT_va_unknown) )
1745 domain_crash(); /* Urk! This L1 is mapped in multiple L2 slots! */
1746 l2_idx >>= PGT_va_shift;
1748 /*
1749 * Is the L1 p.t. mapped into the current address space? If so we call it
1750 * an ACTIVE p.t., otherwise it is INACTIVE.
1751 */
1752 pl2e = &linear_l2_table[l2_idx];
1753 which = (l2_pgentry_val(*pl2e) >> PAGE_SHIFT != pfn) ?
1754 PTWR_PT_INACTIVE : PTWR_PT_ACTIVE;
1756 PTWR_PRINTK("[%c] page_fault on l1 pt at va %08lx, pt for %08x, "
1757 "pfn %08lx\n", PTWR_PRINT_WHICH,
1758 addr, l2_idx << L2_PAGETABLE_SHIFT, pfn);
1760 /*
1761 * We only allow one ACTIVE and one INACTIVE p.t. to be updated at at
1762 * time. If there is already one, we must flush it out.
1763 */
1764 if ( ptwr_info[cpu].ptinfo[which].l1va )
1765 ptwr_flush(which);
1767 ptwr_info[cpu].ptinfo[which].l1va = addr | 1;
1768 ptwr_info[cpu].ptinfo[which].l2_idx = l2_idx;
1770 /* For safety, disconnect the L1 p.t. page from current space. */
1771 if ( (which == PTWR_PT_ACTIVE) && likely(!current->mm.shadow_mode) )
1773 nl2e = mk_l2_pgentry(l2_pgentry_val(*pl2e) & ~_PAGE_PRESENT);
1774 update_l2e(pl2e, *pl2e, nl2e);
1775 flush_tlb();
1778 /* Temporarily map the L1 page, and make a copy of it. */
1779 ptwr_info[cpu].ptinfo[which].pl1e = map_domain_mem(pfn << PAGE_SHIFT);
1780 memcpy(ptwr_info[cpu].ptinfo[which].page,
1781 ptwr_info[cpu].ptinfo[which].pl1e,
1782 ENTRIES_PER_L1_PAGETABLE * sizeof(l1_pgentry_t));
1784 /* Finally, make the p.t. page writable by the guest OS. */
1785 pte |= _PAGE_RW;
1786 PTWR_PRINTK("[%c] update %p pte to %08lx\n", PTWR_PRINT_WHICH,
1787 &linear_pg_table[addr>>PAGE_SHIFT], pte);
1788 if ( unlikely(__put_user(pte, (unsigned long *)
1789 &linear_pg_table[addr>>PAGE_SHIFT])) )
1791 MEM_LOG("ptwr: Could not update pte at %p\n", (unsigned long *)
1792 &linear_pg_table[addr>>PAGE_SHIFT]);
1793 /* Toss the writable pagetable state and crash. */
1794 unmap_domain_mem(ptwr_info[cpu].ptinfo[which].pl1e);
1795 ptwr_info[cpu].ptinfo[which].l1va = 0;
1796 domain_crash();
1799 /* Maybe fall through to shadow mode to propagate writable L1. */
1800 return !current->mm.shadow_mode;
1803 static __init int ptwr_init(void)
1805 int i;
1807 for ( i = 0; i < smp_num_cpus; i++ )
1809 ptwr_info[i].ptinfo[PTWR_PT_ACTIVE].page =
1810 (void *)alloc_xenheap_page();
1811 ptwr_info[i].ptinfo[PTWR_PT_INACTIVE].page =
1812 (void *)alloc_xenheap_page();
1815 return 0;
1817 __initcall(ptwr_init);
1822 /************************************************************************/
1823 /************************************************************************/
1824 /************************************************************************/
1826 #ifndef NDEBUG
1828 void ptwr_status(void)
1830 unsigned long pte, *ptep, pfn;
1831 struct pfn_info *page;
1832 int cpu = smp_processor_id();
1834 ptep = (unsigned long *)&linear_pg_table
1835 [ptwr_info[cpu].ptinfo[PTWR_PT_INACTIVE].l1va>>PAGE_SHIFT];
1837 if ( __get_user(pte, ptep) ) {
1838 MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
1839 domain_crash();
1842 pfn = pte >> PAGE_SHIFT;
1843 page = &frame_table[pfn];
1844 printk("need to alloc l1 page %p\n", page);
1845 /* make pt page writable */
1846 printk("need to make read-only l1-page at %p is %08lx\n",
1847 ptep, pte);
1849 if ( ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va == 0 )
1850 return;
1852 if ( __get_user(pte, (unsigned long *)
1853 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va) ) {
1854 MEM_LOG("ptwr: Could not read pte at %p\n", (unsigned long *)
1855 ptwr_info[cpu].ptinfo[PTWR_PT_ACTIVE].l1va);
1856 domain_crash();
1858 pfn = pte >> PAGE_SHIFT;
1859 page = &frame_table[pfn];
1862 void audit_domain(struct domain *d)
1864 int ttot=0, ctot=0, io_mappings=0, lowmem_mappings=0;
1866 void adjust (struct pfn_info *page, int dir, int adjtype)
1868 int count = page->count_info & PGC_count_mask;
1870 if ( adjtype )
1872 int tcount = page->u.inuse.type_info & PGT_count_mask;
1874 ttot++;
1876 tcount += dir;
1878 if ( tcount < 0 )
1880 /* This will only come out once. */
1881 printk("Audit %d: type count whent below zero pfn=%x "
1882 "taf=%x otaf=%x\n",
1883 d->id, page-frame_table,
1884 page->u.inuse.type_info,
1885 page->tlbflush_timestamp);
1888 page->u.inuse.type_info =
1889 (page->u.inuse.type_info & ~PGT_count_mask) |
1890 (tcount & PGT_count_mask);
1893 ctot++;
1894 count += dir;
1895 if ( count < 0 )
1897 /* This will only come out once. */
1898 printk("Audit %d: general count whent below zero pfn=%x "
1899 "taf=%x otaf=%x\n",
1900 d->id, page-frame_table,
1901 page->u.inuse.type_info,
1902 page->tlbflush_timestamp);
1905 page->count_info =
1906 (page->count_info & ~PGC_count_mask) |
1907 (count & PGC_count_mask);
1911 void scan_for_pfn(struct domain *d, unsigned long xpfn)
1913 unsigned long pfn, *pt;
1914 struct list_head *list_ent;
1915 struct pfn_info *page;
1916 int i;
1918 list_ent = d->page_list.next;
1919 for ( i = 0; (list_ent != &d->page_list); i++ )
1921 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
1922 page = &frame_table[pfn];
1924 switch ( page->u.inuse.type_info & PGT_type_mask )
1926 case PGT_l1_page_table:
1927 case PGT_l2_page_table:
1928 pt = map_domain_mem(pfn<<PAGE_SHIFT);
1929 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
1930 if ( (pt[i] & _PAGE_PRESENT) &&
1931 ((pt[i] >> PAGE_SHIFT) == xpfn) )
1932 printk(" found dom=%d i=%x pfn=%lx t=%x c=%x\n",
1933 d->id, i, pfn, page->u.inuse.type_info,
1934 page->count_info);
1935 unmap_domain_mem(pt);
1938 list_ent = frame_table[pfn].list.next;
1943 void scan_for_pfn_remote(unsigned long xpfn)
1945 struct domain *e;
1946 for_each_domain ( e )
1947 scan_for_pfn( e, xpfn );
1950 int i;
1951 unsigned long pfn;
1952 struct list_head *list_ent;
1953 struct pfn_info *page;
1955 if ( d != current )
1956 domain_pause(d);
1957 synchronise_pagetables(~0UL);
1959 printk("pt base=%lx sh_info=%x\n",
1960 pagetable_val(d->mm.pagetable)>>PAGE_SHIFT,
1961 virt_to_page(d->shared_info)-frame_table);
1963 spin_lock(&d->page_alloc_lock);
1965 /* PHASE 0 */
1967 list_ent = d->page_list.next;
1968 for ( i = 0; (list_ent != &d->page_list); i++ )
1970 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
1971 page = &frame_table[pfn];
1973 if ( page->u.inuse.domain != d )
1974 BUG();
1976 if ( (page->u.inuse.type_info & PGT_count_mask) >
1977 (page->count_info & PGC_count_mask) )
1978 printk("taf > caf %x %x pfn=%lx\n",
1979 page->u.inuse.type_info, page->count_info, pfn );
1981 #if 0 /* SYSV shared memory pages plus writeable files. */
1982 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page &&
1983 (page->u.inuse.type_info & PGT_count_mask) > 1 )
1985 printk("writeable page with type count >1: pfn=%lx t=%x c=%x\n",
1986 pfn,
1987 page->u.inuse.type_info,
1988 page->count_info );
1989 scan_for_pfn_remote(pfn);
1991 #endif
1992 if ( (page->u.inuse.type_info & PGT_type_mask) == PGT_none &&
1993 (page->u.inuse.type_info & PGT_count_mask) > 1 )
1995 printk("normal page with type count >1: pfn=%lx t=%x c=%x\n",
1996 pfn,
1997 page->u.inuse.type_info,
1998 page->count_info );
2001 /* Use tlbflush_timestamp to store original type_info. */
2002 page->tlbflush_timestamp = page->u.inuse.type_info;
2004 list_ent = frame_table[pfn].list.next;
2008 /* PHASE 1 */
2010 adjust(&frame_table[pagetable_val(d->mm.pagetable)>>PAGE_SHIFT], -1, 1);
2012 list_ent = d->page_list.next;
2013 for ( i = 0; (list_ent != &d->page_list); i++ )
2015 unsigned long *pt;
2016 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2017 page = &frame_table[pfn];
2019 if ( page->u.inuse.domain != d )
2020 BUG();
2022 switch ( page->u.inuse.type_info & PGT_type_mask )
2024 case PGT_l2_page_table:
2026 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2027 printk("Audit %d: L2 not validated %x\n",
2028 d->id, page->u.inuse.type_info);
2030 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2031 printk("Audit %d: L2 not pinned %x\n",
2032 d->id, page->u.inuse.type_info);
2033 else
2034 adjust( page, -1, 1 );
2036 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2038 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2040 if ( pt[i] & _PAGE_PRESENT )
2042 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2043 struct pfn_info *l1page = &frame_table[l1pfn];
2045 if ( l1page->u.inuse.domain != d )
2047 printk("L2: Skip bizarre page belonging to other "
2048 "dom %p\n", l1page->u.inuse.domain);
2049 continue;
2052 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2053 PGT_l2_page_table )
2054 printk("Audit %d: [%x] Found %s Linear PT "
2055 "t=%x pfn=%lx\n", d->id, i,
2056 (l1pfn==pfn) ? "Self" : "Other",
2057 l1page->u.inuse.type_info,
2058 l1pfn);
2059 else if ( (l1page->u.inuse.type_info & PGT_type_mask) !=
2060 PGT_l1_page_table )
2061 printk("Audit %d: [%x] Expected L1 t=%x pfn=%lx\n",
2062 d->id, i,
2063 l1page->u.inuse.type_info,
2064 l1pfn);
2066 adjust(l1page, -1, 1);
2070 unmap_domain_mem(pt);
2072 break;
2075 case PGT_l1_page_table:
2077 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2078 adjust( page, -1, 1 );
2080 if ( (page->u.inuse.type_info & PGT_validated) != PGT_validated )
2081 printk("Audit %d: L1 not validated %x\n",
2082 d->id, page->u.inuse.type_info);
2083 #if 0
2084 if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
2085 printk("Audit %d: L1 not pinned %x\n",
2086 d->id, page->u.inuse.type_info);
2087 #endif
2088 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2090 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2092 if ( pt[i] & _PAGE_PRESENT )
2094 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2095 struct pfn_info *l1page = &frame_table[l1pfn];
2097 if ( l1pfn < 0x100 )
2099 lowmem_mappings++;
2100 continue;
2103 if ( l1pfn > max_page )
2105 io_mappings++;
2106 continue;
2109 if ( pt[i] & _PAGE_RW )
2112 if ( (l1page->u.inuse.type_info & PGT_type_mask) ==
2113 PGT_l1_page_table ||
2114 (l1page->u.inuse.type_info & PGT_type_mask) ==
2115 PGT_l2_page_table )
2116 printk("Audit %d: [%x] Ilegal RW t=%x pfn=%lx\n",
2117 d->id, i,
2118 l1page->u.inuse.type_info,
2119 l1pfn);
2123 if ( l1page->u.inuse.domain != d )
2125 printk("Audit %d: [%lx,%x] Skip foreign page dom=%lx "
2126 "pfn=%lx c=%08x t=%08x m2p=%lx\n",
2127 d->id, pfn, i,
2128 (unsigned long)l1page->u.inuse.domain,
2129 l1pfn,
2130 l1page->count_info,
2131 l1page->u.inuse.type_info,
2132 machine_to_phys_mapping[l1pfn]);
2133 continue;
2136 adjust(l1page, -1, 0);
2140 unmap_domain_mem(pt);
2142 break;
2145 list_ent = frame_table[pfn].list.next;
2148 if ( (io_mappings > 0) || (lowmem_mappings > 0) )
2149 printk("Audit %d: Found %d lowmem mappings and %d io mappings\n",
2150 d->id, lowmem_mappings, io_mappings);
2152 /* PHASE 2 */
2154 ctot = ttot = 0;
2155 list_ent = d->page_list.next;
2156 for ( i = 0; (list_ent != &d->page_list); i++ )
2158 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2159 page = &frame_table[pfn];
2161 switch ( page->u.inuse.type_info & PGT_type_mask)
2163 case PGT_l1_page_table:
2164 case PGT_l2_page_table:
2165 if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
2167 printk("Audit %d: type count!=0 t=%x ot=%x c=%x pfn=%lx\n",
2168 d->id, page->u.inuse.type_info,
2169 page->tlbflush_timestamp,
2170 page->count_info, pfn );
2171 scan_for_pfn_remote(pfn);
2173 default:
2174 if ( (page->count_info & PGC_count_mask) != 1 )
2176 printk("Audit %d: gen count!=1 (c=%x) t=%x ot=%x pfn=%lx\n",
2177 d->id,
2178 page->count_info,
2179 page->u.inuse.type_info,
2180 page->tlbflush_timestamp, pfn );
2181 scan_for_pfn_remote(pfn);
2183 break;
2186 list_ent = frame_table[pfn].list.next;
2189 /* PHASE 3 */
2191 list_ent = d->page_list.next;
2192 for ( i = 0; (list_ent != &d->page_list); i++ )
2194 unsigned long *pt;
2195 pfn = list_entry(list_ent, struct pfn_info, list) - frame_table;
2196 page = &frame_table[pfn];
2198 switch ( page->u.inuse.type_info & PGT_type_mask )
2200 case PGT_l2_page_table:
2201 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2202 adjust( page, 1, 1 );
2204 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2206 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
2208 if ( pt[i] & _PAGE_PRESENT )
2210 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2211 struct pfn_info *l1page = &frame_table[l1pfn];
2213 if ( l1page->u.inuse.domain == d)
2214 adjust(l1page, 1, 1);
2218 unmap_domain_mem(pt);
2219 break;
2221 case PGT_l1_page_table:
2222 if ( (page->u.inuse.type_info & PGT_pinned) == PGT_pinned )
2223 adjust( page, 1, 1 );
2225 pt = map_domain_mem( pfn<<PAGE_SHIFT );
2227 for ( i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++ )
2229 if ( pt[i] & _PAGE_PRESENT )
2231 unsigned long l1pfn = pt[i]>>PAGE_SHIFT;
2232 struct pfn_info *l1page = &frame_table[l1pfn];
2234 if ( (l1page->u.inuse.domain != d) ||
2235 (l1pfn < 0x100) || (l1pfn > max_page) )
2236 continue;
2238 adjust(l1page, 1, 0);
2242 unmap_domain_mem(pt);
2243 break;
2247 page->tlbflush_timestamp = 0;
2249 list_ent = frame_table[pfn].list.next;
2252 spin_unlock(&d->page_alloc_lock);
2254 adjust(&frame_table[pagetable_val(d->mm.pagetable)>>PAGE_SHIFT], 1, 1);
2256 printk("Audit %d: Done. ctot=%d ttot=%d\n", d->id, ctot, ttot );
2258 if ( d != current )
2259 domain_unpause(d);
2262 void audit_domains(void)
2264 struct domain *d;
2265 for_each_domain ( d )
2266 audit_domain(d);
2269 void audit_domains_key(unsigned char key, void *dev_id,
2270 struct pt_regs *regs)
2272 open_softirq(MEMAUDIT_SOFTIRQ, audit_domains);
2273 raise_softirq(MEMAUDIT_SOFTIRQ);
2276 #endif