ia64/xen-unstable

view xen/arch/x86/mm.c @ 12039:b3d94f4ddffe

[XEN] Add implicit "\n" back into MEM_LOG(). Removed by mistake.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Sat Oct 28 12:30:38 2006 +0100 (2006-10-28)
parents 36679b74e24a
children 7b5115221dfc
line source
1 /******************************************************************************
2 * arch/x86/mm.c
3 *
4 * Copyright (c) 2002-2005 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/domain.h>
93 #include <xen/sched.h>
94 #include <xen/errno.h>
95 #include <xen/perfc.h>
96 #include <xen/irq.h>
97 #include <xen/softirq.h>
98 #include <xen/domain_page.h>
99 #include <xen/event.h>
100 #include <xen/iocap.h>
101 #include <xen/guest_access.h>
102 #include <asm/shadow.h>
103 #include <asm/page.h>
104 #include <asm/flushtlb.h>
105 #include <asm/io.h>
106 #include <asm/ldt.h>
107 #include <asm/x86_emulate.h>
108 #include <asm/e820.h>
109 #include <public/memory.h>
111 #define MEM_LOG(_f, _a...) gdprintk(XENLOG_WARNING , _f "\n" , ## _a)
113 /*
114 * PTE updates can be done with ordinary writes except:
115 * 1. Debug builds get extra checking by using CMPXCHG[8B].
116 * 2. PAE builds perform an atomic 8-byte store with CMPXCHG8B.
117 */
118 #if !defined(NDEBUG) || defined(CONFIG_X86_PAE)
119 #define PTE_UPDATE_WITH_CMPXCHG
120 #endif
122 /*
123 * Both do_mmuext_op() and do_mmu_update():
124 * We steal the m.s.b. of the @count parameter to indicate whether this
125 * invocation of do_mmu_update() is resuming a previously preempted call.
126 */
127 #define MMU_UPDATE_PREEMPTED (~(~0U>>1))
129 static void free_l2_table(struct page_info *page);
130 static void free_l1_table(struct page_info *page);
132 static int mod_l2_entry(l2_pgentry_t *, l2_pgentry_t, unsigned long,
133 unsigned long type);
134 static int mod_l1_entry(l1_pgentry_t *, l1_pgentry_t, unsigned long gl1mfn);
136 /* Used to defer flushing of memory structures. */
137 struct percpu_mm_info {
138 #define DOP_FLUSH_TLB (1<<0) /* Flush the local TLB. */
139 #define DOP_FLUSH_ALL_TLBS (1<<1) /* Flush TLBs of all VCPUs of current dom. */
140 #define DOP_RELOAD_LDT (1<<2) /* Reload the LDT shadow mapping. */
141 unsigned int deferred_ops;
142 /* If non-NULL, specifies a foreign subject domain for some operations. */
143 struct domain *foreign;
144 };
145 static DEFINE_PER_CPU(struct percpu_mm_info, percpu_mm_info);
147 /*
148 * Returns the current foreign domain; defaults to the currently-executing
149 * domain if a foreign override hasn't been specified.
150 */
151 #define FOREIGNDOM (this_cpu(percpu_mm_info).foreign ?: current->domain)
153 /* Private domain structs for DOMID_XEN and DOMID_IO. */
154 static struct domain *dom_xen, *dom_io;
156 /* Frame table and its size in pages. */
157 struct page_info *frame_table;
158 unsigned long max_page;
159 unsigned long total_pages;
161 void __init init_frametable(void)
162 {
163 unsigned long nr_pages, page_step, i, mfn;
165 frame_table = (struct page_info *)FRAMETABLE_VIRT_START;
167 nr_pages = PFN_UP(max_page * sizeof(*frame_table));
168 page_step = (1 << L2_PAGETABLE_SHIFT) >> PAGE_SHIFT;
170 for ( i = 0; i < nr_pages; i += page_step )
171 {
172 mfn = alloc_boot_pages(min(nr_pages - i, page_step), page_step);
173 if ( mfn == 0 )
174 panic("Not enough memory for frame table\n");
175 map_pages_to_xen(
176 FRAMETABLE_VIRT_START + (i << PAGE_SHIFT),
177 mfn, page_step, PAGE_HYPERVISOR);
178 }
180 memset(frame_table, 0, nr_pages << PAGE_SHIFT);
181 }
183 void arch_init_memory(void)
184 {
185 extern void subarch_init_memory(void);
187 unsigned long i, pfn, rstart_pfn, rend_pfn;
189 /*
190 * Initialise our DOMID_XEN domain.
191 * Any Xen-heap pages that we will allow to be mapped will have
192 * their domain field set to dom_xen.
193 */
194 dom_xen = alloc_domain(DOMID_XEN);
195 BUG_ON(dom_xen == NULL);
197 /*
198 * Initialise our DOMID_IO domain.
199 * This domain owns I/O pages that are within the range of the page_info
200 * array. Mappings occur at the priv of the caller.
201 */
202 dom_io = alloc_domain(DOMID_IO);
203 BUG_ON(dom_io == NULL);
205 /* First 1MB of RAM is historically marked as I/O. */
206 for ( i = 0; i < 0x100; i++ )
207 share_xen_page_with_guest(mfn_to_page(i), dom_io, XENSHARE_writable);
209 /* Any areas not specified as RAM by the e820 map are considered I/O. */
210 for ( i = 0, pfn = 0; i < e820.nr_map; i++ )
211 {
212 if ( e820.map[i].type != E820_RAM )
213 continue;
214 /* Every page from cursor to start of next RAM region is I/O. */
215 rstart_pfn = PFN_UP(e820.map[i].addr);
216 rend_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
217 for ( ; pfn < rstart_pfn; pfn++ )
218 {
219 BUG_ON(!mfn_valid(pfn));
220 share_xen_page_with_guest(
221 mfn_to_page(pfn), dom_io, XENSHARE_writable);
222 }
223 /* Skip the RAM region. */
224 pfn = rend_pfn;
225 }
226 BUG_ON(pfn != max_page);
228 subarch_init_memory();
229 }
231 int memory_is_conventional_ram(paddr_t p)
232 {
233 int i;
235 for ( i = 0; i < e820.nr_map; i++ )
236 {
237 if ( (e820.map[i].type == E820_RAM) &&
238 (e820.map[i].addr <= p) &&
239 (e820.map[i].size > p) )
240 return 1;
241 }
243 return 0;
244 }
246 void share_xen_page_with_guest(
247 struct page_info *page, struct domain *d, int readonly)
248 {
249 if ( page_get_owner(page) == d )
250 return;
252 set_gpfn_from_mfn(page_to_mfn(page), INVALID_M2P_ENTRY);
254 spin_lock(&d->page_alloc_lock);
256 /* The incremented type count pins as writable or read-only. */
257 page->u.inuse.type_info = (readonly ? PGT_none : PGT_writable_page);
258 page->u.inuse.type_info |= PGT_validated | 1;
260 page_set_owner(page, d);
261 wmb(); /* install valid domain ptr before updating refcnt. */
262 ASSERT(page->count_info == 0);
263 page->count_info |= PGC_allocated | 1;
265 if ( unlikely(d->xenheap_pages++ == 0) )
266 get_knownalive_domain(d);
267 list_add_tail(&page->list, &d->xenpage_list);
269 spin_unlock(&d->page_alloc_lock);
270 }
272 void share_xen_page_with_privileged_guests(
273 struct page_info *page, int readonly)
274 {
275 share_xen_page_with_guest(page, dom_xen, readonly);
276 }
278 #if defined(CONFIG_X86_PAE)
280 #ifdef NDEBUG
281 /* Only PDPTs above 4GB boundary need to be shadowed in low memory. */
282 #define l3tab_needs_shadow(mfn) ((mfn) >= 0x100000)
283 #else
284 /*
285 * In debug builds we shadow a selection of <4GB PDPTs to exercise code paths.
286 * We cannot safely shadow the idle page table, nor shadow (v1) page tables
287 * (detected by lack of an owning domain). As required for correctness, we
288 * always shadow PDPTs above 4GB.
289 */
290 #define l3tab_needs_shadow(mfn) \
291 (((((mfn) << PAGE_SHIFT) != __pa(idle_pg_table)) && \
292 (page_get_owner(mfn_to_page(mfn)) != NULL) && \
293 ((mfn) & 1)) || /* odd MFNs are shadowed */ \
294 ((mfn) >= 0x100000))
295 #endif
297 static l1_pgentry_t *fix_pae_highmem_pl1e;
299 /* Cache the address of PAE high-memory fixmap page tables. */
300 static int __init cache_pae_fixmap_address(void)
301 {
302 unsigned long fixmap_base = fix_to_virt(FIX_PAE_HIGHMEM_0);
303 l2_pgentry_t *pl2e = virt_to_xen_l2e(fixmap_base);
304 fix_pae_highmem_pl1e = l2e_to_l1e(*pl2e) + l1_table_offset(fixmap_base);
305 return 0;
306 }
307 __initcall(cache_pae_fixmap_address);
309 static DEFINE_PER_CPU(u32, make_cr3_timestamp);
311 void make_cr3(struct vcpu *v, unsigned long mfn)
312 /* Takes the MFN of a PAE l3 table, copies the contents to below 4GB if
313 * necessary, and sets v->arch.cr3 to the value to load in CR3. */
314 {
315 l3_pgentry_t *highmem_l3tab, *lowmem_l3tab;
316 struct pae_l3_cache *cache = &v->arch.pae_l3_cache;
317 unsigned int cpu = smp_processor_id();
319 /* Fast path: does this mfn need a shadow at all? */
320 if ( !l3tab_needs_shadow(mfn) )
321 {
322 v->arch.cr3 = mfn << PAGE_SHIFT;
323 /* Cache is no longer in use or valid */
324 cache->high_mfn = 0;
325 return;
326 }
328 /* Caching logic is not interrupt safe. */
329 ASSERT(!in_irq());
331 /* Protects against pae_flush_pgd(). */
332 spin_lock(&cache->lock);
334 cache->inuse_idx ^= 1;
335 cache->high_mfn = mfn;
337 /* Map the guest L3 table and copy to the chosen low-memory cache. */
338 *(fix_pae_highmem_pl1e - cpu) = l1e_from_pfn(mfn, __PAGE_HYPERVISOR);
339 /* First check the previous high mapping can't be in the TLB.
340 * (i.e. have we loaded CR3 since we last did this?) */
341 if ( unlikely(this_cpu(make_cr3_timestamp) == this_cpu(tlbflush_time)) )
342 local_flush_tlb_one(fix_to_virt(FIX_PAE_HIGHMEM_0 + cpu));
343 highmem_l3tab = (l3_pgentry_t *)fix_to_virt(FIX_PAE_HIGHMEM_0 + cpu);
344 lowmem_l3tab = cache->table[cache->inuse_idx];
345 memcpy(lowmem_l3tab, highmem_l3tab, sizeof(cache->table[0]));
346 *(fix_pae_highmem_pl1e - cpu) = l1e_empty();
347 this_cpu(make_cr3_timestamp) = this_cpu(tlbflush_time);
349 v->arch.cr3 = __pa(lowmem_l3tab);
351 spin_unlock(&cache->lock);
352 }
354 #else /* !CONFIG_X86_PAE */
356 void make_cr3(struct vcpu *v, unsigned long mfn)
357 {
358 v->arch.cr3 = mfn << PAGE_SHIFT;
359 }
361 #endif /* !CONFIG_X86_PAE */
363 void write_ptbase(struct vcpu *v)
364 {
365 write_cr3(v->arch.cr3);
366 }
368 void invalidate_shadow_ldt(struct vcpu *v)
369 {
370 int i;
371 unsigned long pfn;
372 struct page_info *page;
374 if ( v->arch.shadow_ldt_mapcnt == 0 )
375 return;
377 v->arch.shadow_ldt_mapcnt = 0;
379 for ( i = 16; i < 32; i++ )
380 {
381 pfn = l1e_get_pfn(v->arch.perdomain_ptes[i]);
382 if ( pfn == 0 ) continue;
383 v->arch.perdomain_ptes[i] = l1e_empty();
384 page = mfn_to_page(pfn);
385 ASSERT_PAGE_IS_TYPE(page, PGT_ldt_page);
386 ASSERT_PAGE_IS_DOMAIN(page, v->domain);
387 put_page_and_type(page);
388 }
390 /* Dispose of the (now possibly invalid) mappings from the TLB. */
391 ASSERT(v->processor == smp_processor_id());
392 this_cpu(percpu_mm_info).deferred_ops |= DOP_FLUSH_TLB | DOP_RELOAD_LDT;
393 }
396 static int alloc_segdesc_page(struct page_info *page)
397 {
398 struct desc_struct *descs;
399 int i;
401 descs = map_domain_page(page_to_mfn(page));
403 for ( i = 0; i < 512; i++ )
404 if ( unlikely(!check_descriptor(&descs[i])) )
405 goto fail;
407 unmap_domain_page(descs);
408 return 1;
410 fail:
411 unmap_domain_page(descs);
412 return 0;
413 }
416 /* Map shadow page at offset @off. */
417 int map_ldt_shadow_page(unsigned int off)
418 {
419 struct vcpu *v = current;
420 struct domain *d = v->domain;
421 unsigned long gmfn, mfn;
422 l1_pgentry_t l1e, nl1e;
423 unsigned long gva = v->arch.guest_context.ldt_base + (off << PAGE_SHIFT);
424 int okay;
426 BUG_ON(unlikely(in_irq()));
428 guest_get_eff_kern_l1e(v, gva, &l1e);
429 if ( unlikely(!(l1e_get_flags(l1e) & _PAGE_PRESENT)) )
430 return 0;
432 gmfn = l1e_get_pfn(l1e);
433 mfn = gmfn_to_mfn(d, gmfn);
434 if ( unlikely(!VALID_MFN(mfn)) )
435 return 0;
437 okay = get_page_and_type(mfn_to_page(mfn), d, PGT_ldt_page);
439 if ( !okay && unlikely(shadow_mode_refcounts(d)) )
440 {
441 shadow_lock(d);
442 shadow_remove_write_access(d->vcpu[0], _mfn(mfn), 0, 0);
443 okay = get_page_and_type(mfn_to_page(mfn), d, PGT_ldt_page);
444 shadow_unlock(d);
445 }
447 if ( unlikely(!okay) )
448 return 0;
450 nl1e = l1e_from_pfn(mfn, l1e_get_flags(l1e) | _PAGE_RW);
452 v->arch.perdomain_ptes[off + 16] = nl1e;
453 v->arch.shadow_ldt_mapcnt++;
455 return 1;
456 }
459 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d)
460 {
461 struct page_info *page = mfn_to_page(page_nr);
463 if ( unlikely(!mfn_valid(page_nr)) || unlikely(!get_page(page, d)) )
464 {
465 MEM_LOG("Could not get page ref for pfn %lx", page_nr);
466 return 0;
467 }
469 return 1;
470 }
473 static int get_page_and_type_from_pagenr(unsigned long page_nr,
474 unsigned long type,
475 struct domain *d)
476 {
477 struct page_info *page = mfn_to_page(page_nr);
479 if ( unlikely(!get_page_from_pagenr(page_nr, d)) )
480 return 0;
482 if ( unlikely(!get_page_type(page, type)) )
483 {
484 put_page(page);
485 return 0;
486 }
488 return 1;
489 }
491 #ifndef CONFIG_X86_PAE /* We do not support guest linear mappings on PAE. */
492 /*
493 * We allow root tables to map each other (a.k.a. linear page tables). It
494 * needs some special care with reference counts and access permissions:
495 * 1. The mapping entry must be read-only, or the guest may get write access
496 * to its own PTEs.
497 * 2. We must only bump the reference counts for an *already validated*
498 * L2 table, or we can end up in a deadlock in get_page_type() by waiting
499 * on a validation that is required to complete that validation.
500 * 3. We only need to increment the reference counts for the mapped page
501 * frame if it is mapped by a different root table. This is sufficient and
502 * also necessary to allow validation of a root table mapping itself.
503 */
504 static int
505 get_linear_pagetable(
506 root_pgentry_t re, unsigned long re_pfn, struct domain *d)
507 {
508 unsigned long x, y;
509 struct page_info *page;
510 unsigned long pfn;
512 ASSERT( !shadow_mode_refcounts(d) );
514 if ( (root_get_flags(re) & _PAGE_RW) )
515 {
516 MEM_LOG("Attempt to create linear p.t. with write perms");
517 return 0;
518 }
520 if ( (pfn = root_get_pfn(re)) != re_pfn )
521 {
522 /* Make sure the mapped frame belongs to the correct domain. */
523 if ( unlikely(!get_page_from_pagenr(pfn, d)) )
524 return 0;
526 /*
527 * Make sure that the mapped frame is an already-validated L2 table.
528 * If so, atomically increment the count (checking for overflow).
529 */
530 page = mfn_to_page(pfn);
531 y = page->u.inuse.type_info;
532 do {
533 x = y;
534 if ( unlikely((x & PGT_count_mask) == PGT_count_mask) ||
535 unlikely((x & (PGT_type_mask|PGT_validated)) !=
536 (PGT_root_page_table|PGT_validated)) )
537 {
538 put_page(page);
539 return 0;
540 }
541 }
542 while ( (y = cmpxchg(&page->u.inuse.type_info, x, x + 1)) != x );
543 }
545 return 1;
546 }
547 #endif /* !CONFIG_X86_PAE */
549 int
550 get_page_from_l1e(
551 l1_pgentry_t l1e, struct domain *d)
552 {
553 unsigned long mfn = l1e_get_pfn(l1e);
554 struct page_info *page = mfn_to_page(mfn);
555 int okay;
557 if ( !(l1e_get_flags(l1e) & _PAGE_PRESENT) )
558 return 1;
560 if ( unlikely(l1e_get_flags(l1e) & L1_DISALLOW_MASK) )
561 {
562 MEM_LOG("Bad L1 flags %x", l1e_get_flags(l1e) & L1_DISALLOW_MASK);
563 return 0;
564 }
566 if ( unlikely(!mfn_valid(mfn)) ||
567 unlikely(page_get_owner(page) == dom_io) )
568 {
569 /* DOMID_IO reverts to caller for privilege checks. */
570 if ( d == dom_io )
571 d = current->domain;
573 if ( !iomem_access_permitted(d, mfn, mfn) )
574 {
575 MEM_LOG("Non-privileged (%u) attempt to map I/O space %08lx",
576 d->domain_id, mfn);
577 return 0;
578 }
580 /* No reference counting for out-of-range I/O pages. */
581 if ( !mfn_valid(mfn) )
582 return 1;
584 d = dom_io;
585 }
587 /* Foreign mappings into guests in shadow external mode don't
588 * contribute to writeable mapping refcounts. (This allows the
589 * qemu-dm helper process in dom0 to map the domain's memory without
590 * messing up the count of "real" writable mappings.) */
591 okay = (((l1e_get_flags(l1e) & _PAGE_RW) &&
592 !(unlikely(shadow_mode_external(d) && (d != current->domain))))
593 ? get_page_and_type(page, d, PGT_writable_page)
594 : get_page(page, d));
595 if ( !okay )
596 {
597 MEM_LOG("Error getting mfn %lx (pfn %lx) from L1 entry %" PRIpte
598 " for dom%d",
599 mfn, get_gpfn_from_mfn(mfn),
600 l1e_get_intpte(l1e), d->domain_id);
601 }
603 return okay;
604 }
607 /* NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'. */
608 static int
609 get_page_from_l2e(
610 l2_pgentry_t l2e, unsigned long pfn, struct domain *d)
611 {
612 int rc;
614 if ( !(l2e_get_flags(l2e) & _PAGE_PRESENT) )
615 return 1;
617 if ( unlikely((l2e_get_flags(l2e) & L2_DISALLOW_MASK)) )
618 {
619 MEM_LOG("Bad L2 flags %x", l2e_get_flags(l2e) & L2_DISALLOW_MASK);
620 return 0;
621 }
623 rc = get_page_and_type_from_pagenr(l2e_get_pfn(l2e), PGT_l1_page_table, d);
624 #if CONFIG_PAGING_LEVELS == 2
625 if ( unlikely(!rc) )
626 rc = get_linear_pagetable(l2e, pfn, d);
627 #endif
628 return rc;
629 }
632 #if CONFIG_PAGING_LEVELS >= 3
633 static int
634 get_page_from_l3e(
635 l3_pgentry_t l3e, unsigned long pfn, struct domain *d)
636 {
637 int rc;
639 if ( !(l3e_get_flags(l3e) & _PAGE_PRESENT) )
640 return 1;
642 if ( unlikely((l3e_get_flags(l3e) & L3_DISALLOW_MASK)) )
643 {
644 MEM_LOG("Bad L3 flags %x", l3e_get_flags(l3e) & L3_DISALLOW_MASK);
645 return 0;
646 }
648 rc = get_page_and_type_from_pagenr(l3e_get_pfn(l3e), PGT_l2_page_table, d);
649 return rc;
650 }
651 #endif /* 3 level */
653 #if CONFIG_PAGING_LEVELS >= 4
654 static int
655 get_page_from_l4e(
656 l4_pgentry_t l4e, unsigned long pfn, struct domain *d)
657 {
658 int rc;
660 if ( !(l4e_get_flags(l4e) & _PAGE_PRESENT) )
661 return 1;
663 if ( unlikely((l4e_get_flags(l4e) & L4_DISALLOW_MASK)) )
664 {
665 MEM_LOG("Bad L4 flags %x", l4e_get_flags(l4e) & L4_DISALLOW_MASK);
666 return 0;
667 }
669 rc = get_page_and_type_from_pagenr(l4e_get_pfn(l4e), PGT_l3_page_table, d);
671 if ( unlikely(!rc) )
672 rc = get_linear_pagetable(l4e, pfn, d);
674 return rc;
675 }
676 #endif /* 4 level */
678 #ifdef __x86_64__
680 #ifdef USER_MAPPINGS_ARE_GLOBAL
681 #define adjust_guest_l1e(pl1e) \
682 do { \
683 if ( likely(l1e_get_flags((pl1e)) & _PAGE_PRESENT) ) \
684 { \
685 /* _PAGE_GUEST_KERNEL page cannot have the Global bit set. */ \
686 if ( (l1e_get_flags((pl1e)) & (_PAGE_GUEST_KERNEL|_PAGE_GLOBAL)) \
687 == (_PAGE_GUEST_KERNEL|_PAGE_GLOBAL) ) \
688 MEM_LOG("Global bit is set to kernel page %lx", \
689 l1e_get_pfn((pl1e))); \
690 if ( !(l1e_get_flags((pl1e)) & _PAGE_USER) ) \
691 l1e_add_flags((pl1e), (_PAGE_GUEST_KERNEL|_PAGE_USER)); \
692 if ( !(l1e_get_flags((pl1e)) & _PAGE_GUEST_KERNEL) ) \
693 l1e_add_flags((pl1e), (_PAGE_GLOBAL|_PAGE_USER)); \
694 } \
695 } while ( 0 )
696 #else
697 #define adjust_guest_l1e(pl1e) \
698 do { \
699 if ( likely(l1e_get_flags((pl1e)) & _PAGE_PRESENT) ) \
700 l1e_add_flags((pl1e), _PAGE_USER); \
701 } while ( 0 )
702 #endif
704 #define adjust_guest_l2e(pl2e) \
705 do { \
706 if ( likely(l2e_get_flags((pl2e)) & _PAGE_PRESENT) ) \
707 l2e_add_flags((pl2e), _PAGE_USER); \
708 } while ( 0 )
710 #define adjust_guest_l3e(pl3e) \
711 do { \
712 if ( likely(l3e_get_flags((pl3e)) & _PAGE_PRESENT) ) \
713 l3e_add_flags((pl3e), _PAGE_USER); \
714 } while ( 0 )
716 #define adjust_guest_l4e(pl4e) \
717 do { \
718 if ( likely(l4e_get_flags((pl4e)) & _PAGE_PRESENT) ) \
719 l4e_add_flags((pl4e), _PAGE_USER); \
720 } while ( 0 )
722 #else /* !defined(__x86_64__) */
724 #define adjust_guest_l1e(_p) ((void)0)
725 #define adjust_guest_l2e(_p) ((void)0)
726 #define adjust_guest_l3e(_p) ((void)0)
728 #endif
730 void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
731 {
732 unsigned long pfn = l1e_get_pfn(l1e);
733 struct page_info *page = mfn_to_page(pfn);
734 struct domain *e;
735 struct vcpu *v;
737 if ( !(l1e_get_flags(l1e) & _PAGE_PRESENT) || !mfn_valid(pfn) )
738 return;
740 e = page_get_owner(page);
742 /*
743 * Check if this is a mapping that was established via a grant reference.
744 * If it was then we should not be here: we require that such mappings are
745 * explicitly destroyed via the grant-table interface.
746 *
747 * The upshot of this is that the guest can end up with active grants that
748 * it cannot destroy (because it no longer has a PTE to present to the
749 * grant-table interface). This can lead to subtle hard-to-catch bugs,
750 * hence a special grant PTE flag can be enabled to catch the bug early.
751 *
752 * (Note that the undestroyable active grants are not a security hole in
753 * Xen. All active grants can safely be cleaned up when the domain dies.)
754 */
755 if ( (l1e_get_flags(l1e) & _PAGE_GNTTAB) &&
756 !(d->domain_flags & (DOMF_shutdown|DOMF_dying)) )
757 {
758 MEM_LOG("Attempt to implicitly unmap a granted PTE %" PRIpte,
759 l1e_get_intpte(l1e));
760 domain_crash(d);
761 }
763 /* Remember we didn't take a type-count of foreign writable mappings
764 * to shadow external domains */
765 if ( (l1e_get_flags(l1e) & _PAGE_RW) &&
766 !(unlikely((e != d) && shadow_mode_external(e))) )
767 {
768 put_page_and_type(page);
769 }
770 else
771 {
772 /* We expect this is rare so we blow the entire shadow LDT. */
773 if ( unlikely(((page->u.inuse.type_info & PGT_type_mask) ==
774 PGT_ldt_page)) &&
775 unlikely(((page->u.inuse.type_info & PGT_count_mask) != 0)) &&
776 (d == e) )
777 {
778 for_each_vcpu ( d, v )
779 invalidate_shadow_ldt(v);
780 }
781 put_page(page);
782 }
783 }
786 /*
787 * NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'.
788 * Note also that this automatically deals correctly with linear p.t.'s.
789 */
790 static void put_page_from_l2e(l2_pgentry_t l2e, unsigned long pfn)
791 {
792 if ( (l2e_get_flags(l2e) & _PAGE_PRESENT) &&
793 (l2e_get_pfn(l2e) != pfn) )
794 put_page_and_type(mfn_to_page(l2e_get_pfn(l2e)));
795 }
798 #if CONFIG_PAGING_LEVELS >= 3
799 static void put_page_from_l3e(l3_pgentry_t l3e, unsigned long pfn)
800 {
801 if ( (l3e_get_flags(l3e) & _PAGE_PRESENT) &&
802 (l3e_get_pfn(l3e) != pfn) )
803 put_page_and_type(mfn_to_page(l3e_get_pfn(l3e)));
804 }
805 #endif
807 #if CONFIG_PAGING_LEVELS >= 4
808 static void put_page_from_l4e(l4_pgentry_t l4e, unsigned long pfn)
809 {
810 if ( (l4e_get_flags(l4e) & _PAGE_PRESENT) &&
811 (l4e_get_pfn(l4e) != pfn) )
812 put_page_and_type(mfn_to_page(l4e_get_pfn(l4e)));
813 }
814 #endif
816 static int alloc_l1_table(struct page_info *page)
817 {
818 struct domain *d = page_get_owner(page);
819 unsigned long pfn = page_to_mfn(page);
820 l1_pgentry_t *pl1e;
821 int i;
823 ASSERT(!shadow_mode_refcounts(d));
825 pl1e = map_domain_page(pfn);
827 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
828 {
829 if ( is_guest_l1_slot(i) &&
830 unlikely(!get_page_from_l1e(pl1e[i], d)) )
831 goto fail;
833 adjust_guest_l1e(pl1e[i]);
834 }
836 unmap_domain_page(pl1e);
837 return 1;
839 fail:
840 MEM_LOG("Failure in alloc_l1_table: entry %d", i);
841 while ( i-- > 0 )
842 if ( is_guest_l1_slot(i) )
843 put_page_from_l1e(pl1e[i], d);
845 unmap_domain_page(pl1e);
846 return 0;
847 }
849 #ifdef CONFIG_X86_PAE
850 static int create_pae_xen_mappings(l3_pgentry_t *pl3e)
851 {
852 struct page_info *page;
853 l2_pgentry_t *pl2e;
854 l3_pgentry_t l3e3;
855 int i;
857 pl3e = (l3_pgentry_t *)((unsigned long)pl3e & PAGE_MASK);
859 /* 3rd L3 slot contains L2 with Xen-private mappings. It *must* exist. */
860 l3e3 = pl3e[3];
861 if ( !(l3e_get_flags(l3e3) & _PAGE_PRESENT) )
862 {
863 MEM_LOG("PAE L3 3rd slot is empty");
864 return 0;
865 }
867 /*
868 * The Xen-private mappings include linear mappings. The L2 thus cannot
869 * be shared by multiple L3 tables. The test here is adequate because:
870 * 1. Cannot appear in slots != 3 because get_page_type() checks the
871 * PGT_pae_xen_l2 flag, which is asserted iff the L2 appears in slot 3
872 * 2. Cannot appear in another page table's L3:
873 * a. alloc_l3_table() calls this function and this check will fail
874 * b. mod_l3_entry() disallows updates to slot 3 in an existing table
875 *
876 * XXX -- this needs revisiting for shadow_mode_refcount()==true...
877 */
878 page = l3e_get_page(l3e3);
879 BUG_ON(page->u.inuse.type_info & PGT_pinned);
880 BUG_ON((page->u.inuse.type_info & PGT_count_mask) == 0);
881 BUG_ON(!(page->u.inuse.type_info & PGT_pae_xen_l2));
882 if ( (page->u.inuse.type_info & PGT_count_mask) != 1 )
883 {
884 MEM_LOG("PAE L3 3rd slot is shared");
885 return 0;
886 }
888 /* Xen private mappings. */
889 pl2e = map_domain_page(l3e_get_pfn(l3e3));
890 memcpy(&pl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
891 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
892 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
893 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
894 pl2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] =
895 l2e_from_page(
896 virt_to_page(page_get_owner(page)->arch.mm_perdomain_pt) + i,
897 __PAGE_HYPERVISOR);
898 for ( i = 0; i < (LINEARPT_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
899 pl2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
900 (l3e_get_flags(pl3e[i]) & _PAGE_PRESENT) ?
901 l2e_from_pfn(l3e_get_pfn(pl3e[i]), __PAGE_HYPERVISOR) :
902 l2e_empty();
903 unmap_domain_page(pl2e);
905 return 1;
906 }
908 /* Flush a pgdir update into low-memory caches. */
909 static void pae_flush_pgd(
910 unsigned long mfn, unsigned int idx, l3_pgentry_t nl3e)
911 {
912 struct domain *d = page_get_owner(mfn_to_page(mfn));
913 struct vcpu *v;
914 intpte_t _ol3e, _nl3e, _pl3e;
915 l3_pgentry_t *l3tab_ptr;
916 struct pae_l3_cache *cache;
918 /* If below 4GB then the pgdir is not shadowed in low memory. */
919 if ( !l3tab_needs_shadow(mfn) )
920 return;
922 for_each_vcpu ( d, v )
923 {
924 cache = &v->arch.pae_l3_cache;
926 spin_lock(&cache->lock);
928 if ( cache->high_mfn == mfn )
929 {
930 l3tab_ptr = &cache->table[cache->inuse_idx][idx];
931 _ol3e = l3e_get_intpte(*l3tab_ptr);
932 _nl3e = l3e_get_intpte(nl3e);
933 _pl3e = cmpxchg((intpte_t *)l3tab_ptr, _ol3e, _nl3e);
934 BUG_ON(_pl3e != _ol3e);
935 }
937 spin_unlock(&cache->lock);
938 }
940 flush_tlb_mask(d->domain_dirty_cpumask);
941 }
943 #elif CONFIG_X86_64
944 # define create_pae_xen_mappings(pl3e) (1)
945 # define pae_flush_pgd(mfn, idx, nl3e) ((void)0)
946 #else
947 # define create_pae_xen_mappings(pl3e) (1)
948 #endif
950 static int alloc_l2_table(struct page_info *page, unsigned long type)
951 {
952 struct domain *d = page_get_owner(page);
953 unsigned long pfn = page_to_mfn(page);
954 l2_pgentry_t *pl2e;
955 int i;
957 ASSERT(!shadow_mode_refcounts(d));
959 pl2e = map_domain_page(pfn);
961 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
962 {
963 if ( is_guest_l2_slot(type, i) &&
964 unlikely(!get_page_from_l2e(pl2e[i], pfn, d)) )
965 goto fail;
967 adjust_guest_l2e(pl2e[i]);
968 }
970 #if CONFIG_PAGING_LEVELS == 2
971 /* Xen private mappings. */
972 memcpy(&pl2e[L2_PAGETABLE_FIRST_XEN_SLOT],
973 &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT],
974 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
975 pl2e[l2_table_offset(LINEAR_PT_VIRT_START)] =
976 l2e_from_pfn(pfn, __PAGE_HYPERVISOR);
977 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
978 pl2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] =
979 l2e_from_page(
980 virt_to_page(page_get_owner(page)->arch.mm_perdomain_pt) + i,
981 __PAGE_HYPERVISOR);
982 #endif
984 unmap_domain_page(pl2e);
985 return 1;
987 fail:
988 MEM_LOG("Failure in alloc_l2_table: entry %d", i);
989 while ( i-- > 0 )
990 if ( is_guest_l2_slot(type, i) )
991 put_page_from_l2e(pl2e[i], pfn);
993 unmap_domain_page(pl2e);
994 return 0;
995 }
998 #if CONFIG_PAGING_LEVELS >= 3
999 static int alloc_l3_table(struct page_info *page)
1001 struct domain *d = page_get_owner(page);
1002 unsigned long pfn = page_to_mfn(page);
1003 l3_pgentry_t *pl3e;
1004 int i;
1006 ASSERT(!shadow_mode_refcounts(d));
1008 #ifdef CONFIG_X86_PAE
1009 /*
1010 * PAE pgdirs above 4GB are unacceptable if the guest does not understand
1011 * the weird 'extended cr3' format for dealing with high-order address
1012 * bits. We cut some slack for control tools (before vcpu0 is initialised).
1013 */
1014 if ( (pfn >= 0x100000) &&
1015 unlikely(!VM_ASSIST(d, VMASST_TYPE_pae_extended_cr3)) &&
1016 d->vcpu[0] && test_bit(_VCPUF_initialised, &d->vcpu[0]->vcpu_flags) )
1018 MEM_LOG("PAE pgd must be below 4GB (0x%lx >= 0x100000)", pfn);
1019 return 0;
1021 #endif
1023 pl3e = map_domain_page(pfn);
1024 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1026 #ifdef CONFIG_X86_PAE
1027 if ( i == 3 )
1029 if ( !(l3e_get_flags(pl3e[i]) & _PAGE_PRESENT) ||
1030 (l3e_get_flags(pl3e[i]) & L3_DISALLOW_MASK) ||
1031 !get_page_and_type_from_pagenr(l3e_get_pfn(pl3e[i]),
1032 PGT_l2_page_table |
1033 PGT_pae_xen_l2,
1034 d) )
1035 goto fail;
1037 else
1038 #endif
1039 if ( is_guest_l3_slot(i) &&
1040 unlikely(!get_page_from_l3e(pl3e[i], pfn, d)) )
1041 goto fail;
1043 adjust_guest_l3e(pl3e[i]);
1046 if ( !create_pae_xen_mappings(pl3e) )
1047 goto fail;
1049 unmap_domain_page(pl3e);
1050 return 1;
1052 fail:
1053 MEM_LOG("Failure in alloc_l3_table: entry %d", i);
1054 while ( i-- > 0 )
1055 if ( is_guest_l3_slot(i) )
1056 put_page_from_l3e(pl3e[i], pfn);
1058 unmap_domain_page(pl3e);
1059 return 0;
1061 #else
1062 #define alloc_l3_table(page) (0)
1063 #endif
1065 #if CONFIG_PAGING_LEVELS >= 4
1066 static int alloc_l4_table(struct page_info *page)
1068 struct domain *d = page_get_owner(page);
1069 unsigned long pfn = page_to_mfn(page);
1070 l4_pgentry_t *pl4e = page_to_virt(page);
1071 int i;
1073 ASSERT(!shadow_mode_refcounts(d));
1075 for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
1077 if ( is_guest_l4_slot(i) &&
1078 unlikely(!get_page_from_l4e(pl4e[i], pfn, d)) )
1079 goto fail;
1081 adjust_guest_l4e(pl4e[i]);
1084 /* Xen private mappings. */
1085 memcpy(&pl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1086 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1087 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1088 pl4e[l4_table_offset(LINEAR_PT_VIRT_START)] =
1089 l4e_from_pfn(pfn, __PAGE_HYPERVISOR);
1090 pl4e[l4_table_offset(PERDOMAIN_VIRT_START)] =
1091 l4e_from_page(
1092 virt_to_page(page_get_owner(page)->arch.mm_perdomain_l3),
1093 __PAGE_HYPERVISOR);
1095 return 1;
1097 fail:
1098 MEM_LOG("Failure in alloc_l4_table: entry %d", i);
1099 while ( i-- > 0 )
1100 if ( is_guest_l4_slot(i) )
1101 put_page_from_l4e(pl4e[i], pfn);
1103 return 0;
1105 #else
1106 #define alloc_l4_table(page) (0)
1107 #endif
1110 static void free_l1_table(struct page_info *page)
1112 struct domain *d = page_get_owner(page);
1113 unsigned long pfn = page_to_mfn(page);
1114 l1_pgentry_t *pl1e;
1115 int i;
1117 pl1e = map_domain_page(pfn);
1119 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
1120 if ( is_guest_l1_slot(i) )
1121 put_page_from_l1e(pl1e[i], d);
1123 unmap_domain_page(pl1e);
1127 static void free_l2_table(struct page_info *page)
1129 unsigned long pfn = page_to_mfn(page);
1130 l2_pgentry_t *pl2e;
1131 int i;
1133 pl2e = map_domain_page(pfn);
1135 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
1136 if ( is_guest_l2_slot(page->u.inuse.type_info, i) )
1137 put_page_from_l2e(pl2e[i], pfn);
1139 unmap_domain_page(pl2e);
1141 page->u.inuse.type_info &= ~PGT_pae_xen_l2;
1145 #if CONFIG_PAGING_LEVELS >= 3
1147 static void free_l3_table(struct page_info *page)
1149 unsigned long pfn = page_to_mfn(page);
1150 l3_pgentry_t *pl3e;
1151 int i;
1153 pl3e = map_domain_page(pfn);
1155 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1156 if ( is_guest_l3_slot(i) )
1157 put_page_from_l3e(pl3e[i], pfn);
1159 unmap_domain_page(pl3e);
1162 #endif
1164 #if CONFIG_PAGING_LEVELS >= 4
1166 static void free_l4_table(struct page_info *page)
1168 unsigned long pfn = page_to_mfn(page);
1169 l4_pgentry_t *pl4e = page_to_virt(page);
1170 int i;
1172 for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
1173 if ( is_guest_l4_slot(i) )
1174 put_page_from_l4e(pl4e[i], pfn);
1177 #endif
1179 static inline int update_l1e(l1_pgentry_t *pl1e,
1180 l1_pgentry_t ol1e,
1181 l1_pgentry_t nl1e,
1182 unsigned long gl1mfn,
1183 struct vcpu *v)
1185 int rv = 1;
1186 if ( unlikely(shadow_mode_enabled(v->domain)) )
1187 shadow_lock(v->domain);
1188 #ifndef PTE_UPDATE_WITH_CMPXCHG
1189 rv = (!__copy_to_user(pl1e, &nl1e, sizeof(nl1e)));
1190 #else
1192 intpte_t o = l1e_get_intpte(ol1e);
1193 intpte_t n = l1e_get_intpte(nl1e);
1195 for ( ; ; )
1197 if ( unlikely(cmpxchg_user(pl1e, o, n) != 0) )
1199 MEM_LOG("Failed to update %" PRIpte " -> %" PRIpte
1200 ": saw %" PRIpte,
1201 l1e_get_intpte(ol1e),
1202 l1e_get_intpte(nl1e),
1203 o);
1204 rv = 0;
1205 break;
1208 if ( o == l1e_get_intpte(ol1e) )
1209 break;
1211 /* Allowed to change in Accessed/Dirty flags only. */
1212 BUG_ON((o ^ l1e_get_intpte(ol1e)) &
1213 ~(int)(_PAGE_ACCESSED|_PAGE_DIRTY));
1214 ol1e = l1e_from_intpte(o);
1217 #endif
1218 if ( unlikely(shadow_mode_enabled(v->domain)) && rv )
1220 shadow_validate_guest_entry(v, _mfn(gl1mfn), pl1e);
1221 shadow_unlock(v->domain);
1223 return rv;
1227 /* Update the L1 entry at pl1e to new value nl1e. */
1228 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
1229 unsigned long gl1mfn)
1231 l1_pgentry_t ol1e;
1232 struct domain *d = current->domain;
1234 if ( unlikely(__copy_from_user(&ol1e, pl1e, sizeof(ol1e)) != 0) )
1235 return 0;
1237 if ( unlikely(shadow_mode_refcounts(d)) )
1238 return update_l1e(pl1e, ol1e, nl1e, gl1mfn, current);
1240 if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
1242 if ( unlikely(l1e_get_flags(nl1e) & L1_DISALLOW_MASK) )
1244 MEM_LOG("Bad L1 flags %x",
1245 l1e_get_flags(nl1e) & L1_DISALLOW_MASK);
1246 return 0;
1249 adjust_guest_l1e(nl1e);
1251 /* Fast path for identical mapping, r/w and presence. */
1252 if ( !l1e_has_changed(ol1e, nl1e, _PAGE_RW | _PAGE_PRESENT) )
1253 return update_l1e(pl1e, ol1e, nl1e, gl1mfn, current);
1255 if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
1256 return 0;
1258 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e, gl1mfn, current)) )
1260 put_page_from_l1e(nl1e, d);
1261 return 0;
1264 else
1266 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e, gl1mfn, current)) )
1267 return 0;
1270 put_page_from_l1e(ol1e, d);
1271 return 1;
1274 #ifndef PTE_UPDATE_WITH_CMPXCHG
1275 #define _UPDATE_ENTRY(_t,_p,_o,_n) ({ (*(_p) = (_n)); 1; })
1276 #else
1277 #define _UPDATE_ENTRY(_t,_p,_o,_n) ({ \
1278 for ( ; ; ) \
1279 { \
1280 intpte_t __o = cmpxchg((intpte_t *)(_p), \
1281 _t ## e_get_intpte(_o), \
1282 _t ## e_get_intpte(_n)); \
1283 if ( __o == _t ## e_get_intpte(_o) ) \
1284 break; \
1285 /* Allowed to change in Accessed/Dirty flags only. */ \
1286 BUG_ON((__o ^ _t ## e_get_intpte(_o)) & \
1287 ~(int)(_PAGE_ACCESSED|_PAGE_DIRTY)); \
1288 _o = _t ## e_from_intpte(__o); \
1289 } \
1290 1; })
1291 #endif
1292 #define UPDATE_ENTRY(_t,_p,_o,_n,_m) ({ \
1293 int rv; \
1294 if ( unlikely(shadow_mode_enabled(current->domain)) ) \
1295 shadow_lock(current->domain); \
1296 rv = _UPDATE_ENTRY(_t, _p, _o, _n); \
1297 if ( unlikely(shadow_mode_enabled(current->domain)) ) \
1298 { \
1299 shadow_validate_guest_entry(current, _mfn(_m), (_p)); \
1300 shadow_unlock(current->domain); \
1301 } \
1302 rv; \
1303 })
1305 /* Update the L2 entry at pl2e to new value nl2e. pl2e is within frame pfn. */
1306 static int mod_l2_entry(l2_pgentry_t *pl2e,
1307 l2_pgentry_t nl2e,
1308 unsigned long pfn,
1309 unsigned long type)
1311 l2_pgentry_t ol2e;
1313 if ( unlikely(!is_guest_l2_slot(type,pgentry_ptr_to_slot(pl2e))) )
1315 MEM_LOG("Illegal L2 update attempt in Xen-private area %p", pl2e);
1316 return 0;
1319 if ( unlikely(__copy_from_user(&ol2e, pl2e, sizeof(ol2e)) != 0) )
1320 return 0;
1322 if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
1324 if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
1326 MEM_LOG("Bad L2 flags %x",
1327 l2e_get_flags(nl2e) & L2_DISALLOW_MASK);
1328 return 0;
1331 adjust_guest_l2e(nl2e);
1333 /* Fast path for identical mapping and presence. */
1334 if ( !l2e_has_changed(ol2e, nl2e, _PAGE_PRESENT))
1335 return UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn);
1337 if ( unlikely(!get_page_from_l2e(nl2e, pfn, current->domain)) )
1338 return 0;
1340 if ( unlikely(!UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn)) )
1342 put_page_from_l2e(nl2e, pfn);
1343 return 0;
1346 else if ( unlikely(!UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn)) )
1348 return 0;
1351 put_page_from_l2e(ol2e, pfn);
1352 return 1;
1355 #if CONFIG_PAGING_LEVELS >= 3
1357 /* Update the L3 entry at pl3e to new value nl3e. pl3e is within frame pfn. */
1358 static int mod_l3_entry(l3_pgentry_t *pl3e,
1359 l3_pgentry_t nl3e,
1360 unsigned long pfn)
1362 l3_pgentry_t ol3e;
1363 int okay;
1365 if ( unlikely(!is_guest_l3_slot(pgentry_ptr_to_slot(pl3e))) )
1367 MEM_LOG("Illegal L3 update attempt in Xen-private area %p", pl3e);
1368 return 0;
1371 #ifdef CONFIG_X86_PAE
1372 /*
1373 * Disallow updates to final L3 slot. It contains Xen mappings, and it
1374 * would be a pain to ensure they remain continuously valid throughout.
1375 */
1376 if ( pgentry_ptr_to_slot(pl3e) >= 3 )
1377 return 0;
1378 #endif
1380 if ( unlikely(__copy_from_user(&ol3e, pl3e, sizeof(ol3e)) != 0) )
1381 return 0;
1383 if ( l3e_get_flags(nl3e) & _PAGE_PRESENT )
1385 if ( unlikely(l3e_get_flags(nl3e) & L3_DISALLOW_MASK) )
1387 MEM_LOG("Bad L3 flags %x",
1388 l3e_get_flags(nl3e) & L3_DISALLOW_MASK);
1389 return 0;
1392 adjust_guest_l3e(nl3e);
1394 /* Fast path for identical mapping and presence. */
1395 if (!l3e_has_changed(ol3e, nl3e, _PAGE_PRESENT))
1396 return UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn);
1398 if ( unlikely(!get_page_from_l3e(nl3e, pfn, current->domain)) )
1399 return 0;
1401 if ( unlikely(!UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn)) )
1403 put_page_from_l3e(nl3e, pfn);
1404 return 0;
1407 else if ( unlikely(!UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn)) )
1409 return 0;
1412 okay = create_pae_xen_mappings(pl3e);
1413 BUG_ON(!okay);
1415 pae_flush_pgd(pfn, pgentry_ptr_to_slot(pl3e), nl3e);
1417 put_page_from_l3e(ol3e, pfn);
1418 return 1;
1421 #endif
1423 #if CONFIG_PAGING_LEVELS >= 4
1425 /* Update the L4 entry at pl4e to new value nl4e. pl4e is within frame pfn. */
1426 static int mod_l4_entry(l4_pgentry_t *pl4e,
1427 l4_pgentry_t nl4e,
1428 unsigned long pfn)
1430 l4_pgentry_t ol4e;
1432 if ( unlikely(!is_guest_l4_slot(pgentry_ptr_to_slot(pl4e))) )
1434 MEM_LOG("Illegal L4 update attempt in Xen-private area %p", pl4e);
1435 return 0;
1438 if ( unlikely(__copy_from_user(&ol4e, pl4e, sizeof(ol4e)) != 0) )
1439 return 0;
1441 if ( l4e_get_flags(nl4e) & _PAGE_PRESENT )
1443 if ( unlikely(l4e_get_flags(nl4e) & L4_DISALLOW_MASK) )
1445 MEM_LOG("Bad L4 flags %x",
1446 l4e_get_flags(nl4e) & L4_DISALLOW_MASK);
1447 return 0;
1450 adjust_guest_l4e(nl4e);
1452 /* Fast path for identical mapping and presence. */
1453 if (!l4e_has_changed(ol4e, nl4e, _PAGE_PRESENT))
1454 return UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn);
1456 if ( unlikely(!get_page_from_l4e(nl4e, pfn, current->domain)) )
1457 return 0;
1459 if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn)) )
1461 put_page_from_l4e(nl4e, pfn);
1462 return 0;
1465 else if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn)) )
1467 return 0;
1470 put_page_from_l4e(ol4e, pfn);
1471 return 1;
1474 #endif
1476 int alloc_page_type(struct page_info *page, unsigned long type)
1478 struct domain *owner = page_get_owner(page);
1480 /* A page table is dirtied when its type count becomes non-zero. */
1481 if ( likely(owner != NULL) )
1482 mark_dirty(owner, page_to_mfn(page));
1484 switch ( type & PGT_type_mask )
1486 case PGT_l1_page_table:
1487 return alloc_l1_table(page);
1488 case PGT_l2_page_table:
1489 return alloc_l2_table(page, type);
1490 case PGT_l3_page_table:
1491 return alloc_l3_table(page);
1492 case PGT_l4_page_table:
1493 return alloc_l4_table(page);
1494 case PGT_gdt_page:
1495 case PGT_ldt_page:
1496 return alloc_segdesc_page(page);
1497 default:
1498 printk("Bad type in alloc_page_type %lx t=%" PRtype_info " c=%x\n",
1499 type, page->u.inuse.type_info,
1500 page->count_info);
1501 BUG();
1504 return 0;
1508 void free_page_type(struct page_info *page, unsigned long type)
1510 struct domain *owner = page_get_owner(page);
1511 unsigned long gmfn;
1513 if ( likely(owner != NULL) )
1515 /*
1516 * We have to flush before the next use of the linear mapping
1517 * (e.g., update_va_mapping()) or we could end up modifying a page
1518 * that is no longer a page table (and hence screw up ref counts).
1519 */
1520 this_cpu(percpu_mm_info).deferred_ops |= DOP_FLUSH_ALL_TLBS;
1522 if ( unlikely(shadow_mode_enabled(owner)) )
1524 /* A page table is dirtied when its type count becomes zero. */
1525 mark_dirty(owner, page_to_mfn(page));
1527 if ( shadow_mode_refcounts(owner) )
1528 return;
1530 gmfn = mfn_to_gmfn(owner, page_to_mfn(page));
1531 ASSERT(VALID_M2P(gmfn));
1532 shadow_remove_all_shadows(owner->vcpu[0], _mfn(gmfn));
1536 switch ( type & PGT_type_mask )
1538 case PGT_l1_page_table:
1539 free_l1_table(page);
1540 break;
1542 case PGT_l2_page_table:
1543 free_l2_table(page);
1544 break;
1546 #if CONFIG_PAGING_LEVELS >= 3
1547 case PGT_l3_page_table:
1548 free_l3_table(page);
1549 break;
1550 #endif
1552 #if CONFIG_PAGING_LEVELS >= 4
1553 case PGT_l4_page_table:
1554 free_l4_table(page);
1555 break;
1556 #endif
1558 default:
1559 printk("%s: type %lx pfn %lx\n",__FUNCTION__,
1560 type, page_to_mfn(page));
1561 BUG();
1566 void put_page_type(struct page_info *page)
1568 unsigned long nx, x, y = page->u.inuse.type_info;
1570 again:
1571 do {
1572 x = y;
1573 nx = x - 1;
1575 ASSERT((x & PGT_count_mask) != 0);
1577 if ( unlikely((nx & PGT_count_mask) == 0) )
1579 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
1580 likely(nx & PGT_validated) )
1582 /*
1583 * Page-table pages must be unvalidated when count is zero. The
1584 * 'free' is safe because the refcnt is non-zero and validated
1585 * bit is clear => other ops will spin or fail.
1586 */
1587 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
1588 x & ~PGT_validated)) != x) )
1589 goto again;
1590 /* We cleared the 'valid bit' so we do the clean up. */
1591 free_page_type(page, x);
1592 /* Carry on, but with the 'valid bit' now clear. */
1593 x &= ~PGT_validated;
1594 nx &= ~PGT_validated;
1597 /*
1598 * Record TLB information for flush later. We do not stamp page
1599 * tables when running in shadow mode:
1600 * 1. Pointless, since it's the shadow pt's which must be tracked.
1601 * 2. Shadow mode reuses this field for shadowed page tables to
1602 * store flags info -- we don't want to conflict with that.
1603 */
1604 if ( !(shadow_mode_enabled(page_get_owner(page)) &&
1605 (page->count_info & PGC_page_table)) )
1606 page->tlbflush_timestamp = tlbflush_current_time();
1609 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
1613 int get_page_type(struct page_info *page, unsigned long type)
1615 unsigned long nx, x, y = page->u.inuse.type_info;
1617 ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
1619 again:
1620 do {
1621 x = y;
1622 nx = x + 1;
1623 if ( unlikely((nx & PGT_count_mask) == 0) )
1625 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
1626 return 0;
1628 else if ( unlikely((x & PGT_count_mask) == 0) )
1630 struct domain *d = page_get_owner(page);
1632 /* Never allow a shadowed frame to go from type count 0 to 1 */
1633 if ( d && shadow_mode_enabled(d) )
1634 shadow_remove_all_shadows(d->vcpu[0], _mfn(page_to_mfn(page)));
1636 ASSERT(!(x & PGT_pae_xen_l2));
1637 if ( (x & PGT_type_mask) != type )
1639 /*
1640 * On type change we check to flush stale TLB entries. This
1641 * may be unnecessary (e.g., page was GDT/LDT) but those
1642 * circumstances should be very rare.
1643 */
1644 cpumask_t mask = d->domain_dirty_cpumask;
1646 /* Don't flush if the timestamp is old enough */
1647 tlbflush_filter(mask, page->tlbflush_timestamp);
1649 if ( unlikely(!cpus_empty(mask)) &&
1650 /* Shadow mode: track only writable pages. */
1651 (!shadow_mode_enabled(page_get_owner(page)) ||
1652 ((nx & PGT_type_mask) == PGT_writable_page)) )
1654 perfc_incrc(need_flush_tlb_flush);
1655 flush_tlb_mask(mask);
1658 /* We lose existing type, back pointer, and validity. */
1659 nx &= ~(PGT_type_mask | PGT_validated);
1660 nx |= type;
1662 /* No special validation needed for writable pages. */
1663 /* Page tables and GDT/LDT need to be scanned for validity. */
1664 if ( type == PGT_writable_page )
1665 nx |= PGT_validated;
1668 else if ( unlikely((x & (PGT_type_mask|PGT_pae_xen_l2)) != type) )
1670 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
1671 (type != PGT_l1_page_table) )
1672 MEM_LOG("Bad type (saw %" PRtype_info
1673 " != exp %" PRtype_info ") "
1674 "for mfn %lx (pfn %lx)",
1675 x, type, page_to_mfn(page),
1676 get_gpfn_from_mfn(page_to_mfn(page)));
1677 return 0;
1679 else if ( unlikely(!(x & PGT_validated)) )
1681 /* Someone else is updating validation of this page. Wait... */
1682 while ( (y = page->u.inuse.type_info) == x )
1683 cpu_relax();
1684 goto again;
1687 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
1689 if ( unlikely(!(nx & PGT_validated)) )
1691 /* Try to validate page type; drop the new reference on failure. */
1692 if ( unlikely(!alloc_page_type(page, type)) )
1694 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %"
1695 PRtype_info ": caf=%08x taf=%" PRtype_info,
1696 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
1697 type, page->count_info, page->u.inuse.type_info);
1698 /* Noone else can get a reference. We hold the only ref. */
1699 page->u.inuse.type_info = 0;
1700 return 0;
1703 /* Noone else is updating simultaneously. */
1704 __set_bit(_PGT_validated, &page->u.inuse.type_info);
1707 return 1;
1711 int new_guest_cr3(unsigned long mfn)
1713 struct vcpu *v = current;
1714 struct domain *d = v->domain;
1715 int okay;
1716 unsigned long old_base_mfn;
1718 if ( hvm_guest(v) && !hvm_paging_enabled(v) )
1719 domain_crash_synchronous();
1721 if ( shadow_mode_refcounts(d) )
1723 okay = get_page_from_pagenr(mfn, d);
1724 if ( unlikely(!okay) )
1726 MEM_LOG("Error while installing new baseptr %lx", mfn);
1727 return 0;
1730 else
1732 okay = get_page_and_type_from_pagenr(mfn, PGT_root_page_table, d);
1733 if ( unlikely(!okay) )
1735 /* Switch to idle pagetable: this VCPU has no active p.t. now. */
1736 MEM_LOG("New baseptr %lx: slow path via idle pagetables", mfn);
1737 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1738 v->arch.guest_table = pagetable_null();
1739 update_cr3(v);
1740 write_cr3(__pa(idle_pg_table));
1741 if ( old_base_mfn != 0 )
1742 put_page_and_type(mfn_to_page(old_base_mfn));
1744 /* Retry the validation with no active p.t. for this VCPU. */
1745 okay = get_page_and_type_from_pagenr(mfn, PGT_root_page_table, d);
1746 if ( !okay )
1748 /* Failure here is unrecoverable: the VCPU has no pagetable! */
1749 MEM_LOG("Fatal error while installing new baseptr %lx", mfn);
1750 domain_crash(d);
1751 ASSERT(v->processor == smp_processor_id());
1752 this_cpu(percpu_mm_info).deferred_ops = 0;
1753 return 0;
1758 invalidate_shadow_ldt(v);
1760 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1762 v->arch.guest_table = pagetable_from_pfn(mfn);
1763 update_cr3(v); /* update shadow_table and cr3 fields of vcpu struct */
1765 write_ptbase(v);
1767 if ( likely(old_base_mfn != 0) )
1769 if ( shadow_mode_refcounts(d) )
1770 put_page(mfn_to_page(old_base_mfn));
1771 else
1772 put_page_and_type(mfn_to_page(old_base_mfn));
1775 return 1;
1778 static void process_deferred_ops(void)
1780 unsigned int deferred_ops;
1781 struct domain *d = current->domain;
1782 struct percpu_mm_info *info = &this_cpu(percpu_mm_info);
1784 deferred_ops = info->deferred_ops;
1785 info->deferred_ops = 0;
1787 if ( deferred_ops & (DOP_FLUSH_ALL_TLBS|DOP_FLUSH_TLB) )
1789 if ( deferred_ops & DOP_FLUSH_ALL_TLBS )
1790 flush_tlb_mask(d->domain_dirty_cpumask);
1791 else
1792 local_flush_tlb();
1795 if ( deferred_ops & DOP_RELOAD_LDT )
1796 (void)map_ldt_shadow_page(0);
1798 if ( unlikely(info->foreign != NULL) )
1800 put_domain(info->foreign);
1801 info->foreign = NULL;
1805 static int set_foreigndom(domid_t domid)
1807 struct domain *e, *d = current->domain;
1808 struct percpu_mm_info *info = &this_cpu(percpu_mm_info);
1809 int okay = 1;
1811 ASSERT(info->foreign == NULL);
1813 if ( likely(domid == DOMID_SELF) )
1814 goto out;
1816 if ( domid == d->domain_id )
1818 MEM_LOG("Dom %u tried to specify itself as foreign domain",
1819 d->domain_id);
1820 okay = 0;
1822 else if ( !IS_PRIV(d) )
1824 switch ( domid )
1826 case DOMID_IO:
1827 get_knownalive_domain(dom_io);
1828 info->foreign = dom_io;
1829 break;
1830 default:
1831 MEM_LOG("Dom %u cannot set foreign dom", d->domain_id);
1832 okay = 0;
1833 break;
1836 else
1838 info->foreign = e = find_domain_by_id(domid);
1839 if ( e == NULL )
1841 switch ( domid )
1843 case DOMID_XEN:
1844 get_knownalive_domain(dom_xen);
1845 info->foreign = dom_xen;
1846 break;
1847 case DOMID_IO:
1848 get_knownalive_domain(dom_io);
1849 info->foreign = dom_io;
1850 break;
1851 default:
1852 MEM_LOG("Unknown domain '%u'", domid);
1853 okay = 0;
1854 break;
1859 if ( unlikely(shadow_mode_translate(d)) )
1861 MEM_LOG("%s: can not mix foreign mappings with translated domains",
1862 __func__);
1863 info->foreign = NULL;
1864 okay = 0;
1867 out:
1868 return okay;
1871 static inline cpumask_t vcpumask_to_pcpumask(
1872 struct domain *d, unsigned long vmask)
1874 unsigned int vcpu_id;
1875 cpumask_t pmask = CPU_MASK_NONE;
1876 struct vcpu *v;
1878 while ( vmask != 0 )
1880 vcpu_id = find_first_set_bit(vmask);
1881 vmask &= ~(1UL << vcpu_id);
1882 if ( (vcpu_id < MAX_VIRT_CPUS) &&
1883 ((v = d->vcpu[vcpu_id]) != NULL) )
1884 cpus_or(pmask, pmask, v->vcpu_dirty_cpumask);
1887 return pmask;
1890 int do_mmuext_op(
1891 XEN_GUEST_HANDLE(mmuext_op_t) uops,
1892 unsigned int count,
1893 XEN_GUEST_HANDLE(uint) pdone,
1894 unsigned int foreigndom)
1896 struct mmuext_op op;
1897 int rc = 0, i = 0, okay;
1898 unsigned long mfn = 0, gmfn = 0, type;
1899 unsigned int done = 0;
1900 struct page_info *page;
1901 struct vcpu *v = current;
1902 struct domain *d = v->domain;
1904 LOCK_BIGLOCK(d);
1906 if ( unlikely(count & MMU_UPDATE_PREEMPTED) )
1908 count &= ~MMU_UPDATE_PREEMPTED;
1909 if ( unlikely(!guest_handle_is_null(pdone)) )
1910 (void)copy_from_guest(&done, pdone, 1);
1913 if ( !set_foreigndom(foreigndom) )
1915 rc = -ESRCH;
1916 goto out;
1919 if ( unlikely(!guest_handle_okay(uops, count)) )
1921 rc = -EFAULT;
1922 goto out;
1925 for ( i = 0; i < count; i++ )
1927 if ( hypercall_preempt_check() )
1929 rc = hypercall_create_continuation(
1930 __HYPERVISOR_mmuext_op, "hihi",
1931 uops, (count - i) | MMU_UPDATE_PREEMPTED, pdone, foreigndom);
1932 break;
1935 if ( unlikely(__copy_from_guest(&op, uops, 1) != 0) )
1937 MEM_LOG("Bad __copy_from_guest");
1938 rc = -EFAULT;
1939 break;
1942 okay = 1;
1943 gmfn = op.arg1.mfn;
1944 mfn = gmfn_to_mfn(FOREIGNDOM, gmfn);
1945 page = mfn_to_page(mfn);
1947 switch ( op.cmd )
1949 case MMUEXT_PIN_L1_TABLE:
1950 type = PGT_l1_page_table;
1951 goto pin_page;
1953 case MMUEXT_PIN_L2_TABLE:
1954 type = PGT_l2_page_table;
1955 goto pin_page;
1957 case MMUEXT_PIN_L3_TABLE:
1958 type = PGT_l3_page_table;
1959 goto pin_page;
1961 case MMUEXT_PIN_L4_TABLE:
1962 type = PGT_l4_page_table;
1964 pin_page:
1965 /* Ignore pinning of invalid paging levels. */
1966 if ( (op.cmd - MMUEXT_PIN_L1_TABLE) > (CONFIG_PAGING_LEVELS - 1) )
1967 break;
1969 if ( shadow_mode_refcounts(FOREIGNDOM) )
1970 break;
1972 okay = get_page_and_type_from_pagenr(mfn, type, FOREIGNDOM);
1973 if ( unlikely(!okay) )
1975 MEM_LOG("Error while pinning mfn %lx", mfn);
1976 break;
1979 if ( unlikely(test_and_set_bit(_PGT_pinned,
1980 &page->u.inuse.type_info)) )
1982 MEM_LOG("Mfn %lx already pinned", mfn);
1983 put_page_and_type(page);
1984 okay = 0;
1985 break;
1988 /* A page is dirtied when its pin status is set. */
1989 mark_dirty(d, mfn);
1991 break;
1993 case MMUEXT_UNPIN_TABLE:
1994 if ( shadow_mode_refcounts(d) )
1995 break;
1997 if ( unlikely(!(okay = get_page_from_pagenr(mfn, d))) )
1999 MEM_LOG("Mfn %lx bad domain (dom=%p)",
2000 mfn, page_get_owner(page));
2002 else if ( likely(test_and_clear_bit(_PGT_pinned,
2003 &page->u.inuse.type_info)) )
2005 put_page_and_type(page);
2006 put_page(page);
2007 /* A page is dirtied when its pin status is cleared. */
2008 mark_dirty(d, mfn);
2010 else
2012 okay = 0;
2013 put_page(page);
2014 MEM_LOG("Mfn %lx not pinned", mfn);
2016 break;
2018 case MMUEXT_NEW_BASEPTR:
2019 okay = new_guest_cr3(mfn);
2020 this_cpu(percpu_mm_info).deferred_ops &= ~DOP_FLUSH_TLB;
2021 break;
2023 #ifdef __x86_64__
2024 case MMUEXT_NEW_USER_BASEPTR:
2025 okay = 1;
2026 if (likely(mfn != 0))
2028 if ( shadow_mode_refcounts(d) )
2029 okay = get_page_from_pagenr(mfn, d);
2030 else
2031 okay = get_page_and_type_from_pagenr(
2032 mfn, PGT_root_page_table, d);
2034 if ( unlikely(!okay) )
2036 MEM_LOG("Error while installing new mfn %lx", mfn);
2038 else
2040 unsigned long old_mfn =
2041 pagetable_get_pfn(v->arch.guest_table_user);
2042 v->arch.guest_table_user = pagetable_from_pfn(mfn);
2043 if ( old_mfn != 0 )
2045 if ( shadow_mode_refcounts(d) )
2046 put_page(mfn_to_page(old_mfn));
2047 else
2048 put_page_and_type(mfn_to_page(old_mfn));
2051 break;
2052 #endif
2054 case MMUEXT_TLB_FLUSH_LOCAL:
2055 this_cpu(percpu_mm_info).deferred_ops |= DOP_FLUSH_TLB;
2056 break;
2058 case MMUEXT_INVLPG_LOCAL:
2059 if ( !shadow_mode_enabled(d)
2060 || shadow_invlpg(v, op.arg1.linear_addr) != 0 )
2061 local_flush_tlb_one(op.arg1.linear_addr);
2062 break;
2064 case MMUEXT_TLB_FLUSH_MULTI:
2065 case MMUEXT_INVLPG_MULTI:
2067 unsigned long vmask;
2068 cpumask_t pmask;
2069 if ( unlikely(get_user(vmask, (unsigned long *)op.arg2.vcpumask)) )
2071 okay = 0;
2072 break;
2074 pmask = vcpumask_to_pcpumask(d, vmask);
2075 if ( op.cmd == MMUEXT_TLB_FLUSH_MULTI )
2076 flush_tlb_mask(pmask);
2077 else
2078 flush_tlb_one_mask(pmask, op.arg1.linear_addr);
2079 break;
2082 case MMUEXT_TLB_FLUSH_ALL:
2083 flush_tlb_mask(d->domain_dirty_cpumask);
2084 break;
2086 case MMUEXT_INVLPG_ALL:
2087 flush_tlb_one_mask(d->domain_dirty_cpumask, op.arg1.linear_addr);
2088 break;
2090 case MMUEXT_FLUSH_CACHE:
2091 if ( unlikely(!cache_flush_permitted(d)) )
2093 MEM_LOG("Non-physdev domain tried to FLUSH_CACHE.");
2094 okay = 0;
2096 else
2098 wbinvd();
2100 break;
2102 case MMUEXT_SET_LDT:
2104 unsigned long ptr = op.arg1.linear_addr;
2105 unsigned long ents = op.arg2.nr_ents;
2107 if ( shadow_mode_external(d) )
2109 MEM_LOG("ignoring SET_LDT hypercall from external "
2110 "domain %u", d->domain_id);
2111 okay = 0;
2113 else if ( ((ptr & (PAGE_SIZE-1)) != 0) ||
2114 (ents > 8192) ||
2115 !array_access_ok(ptr, ents, LDT_ENTRY_SIZE) )
2117 okay = 0;
2118 MEM_LOG("Bad args to SET_LDT: ptr=%lx, ents=%lx", ptr, ents);
2120 else if ( (v->arch.guest_context.ldt_ents != ents) ||
2121 (v->arch.guest_context.ldt_base != ptr) )
2123 invalidate_shadow_ldt(v);
2124 v->arch.guest_context.ldt_base = ptr;
2125 v->arch.guest_context.ldt_ents = ents;
2126 load_LDT(v);
2127 this_cpu(percpu_mm_info).deferred_ops &= ~DOP_RELOAD_LDT;
2128 if ( ents != 0 )
2129 this_cpu(percpu_mm_info).deferred_ops |= DOP_RELOAD_LDT;
2131 break;
2134 default:
2135 MEM_LOG("Invalid extended pt command 0x%x", op.cmd);
2136 okay = 0;
2137 break;
2140 if ( unlikely(!okay) )
2142 rc = -EINVAL;
2143 break;
2146 guest_handle_add_offset(uops, 1);
2149 out:
2150 process_deferred_ops();
2152 /* Add incremental work we have done to the @done output parameter. */
2153 done += i;
2154 if ( unlikely(!guest_handle_is_null(pdone)) )
2155 copy_to_guest(pdone, &done, 1);
2157 UNLOCK_BIGLOCK(d);
2158 return rc;
2161 int do_mmu_update(
2162 XEN_GUEST_HANDLE(mmu_update_t) ureqs,
2163 unsigned int count,
2164 XEN_GUEST_HANDLE(uint) pdone,
2165 unsigned int foreigndom)
2167 struct mmu_update req;
2168 void *va;
2169 unsigned long gpfn, gmfn, mfn;
2170 struct page_info *page;
2171 int rc = 0, okay = 1, i = 0;
2172 unsigned int cmd, done = 0;
2173 struct vcpu *v = current;
2174 struct domain *d = v->domain;
2175 unsigned long type_info;
2176 struct domain_mmap_cache mapcache, sh_mapcache;
2178 LOCK_BIGLOCK(d);
2180 if ( unlikely(count & MMU_UPDATE_PREEMPTED) )
2182 count &= ~MMU_UPDATE_PREEMPTED;
2183 if ( unlikely(!guest_handle_is_null(pdone)) )
2184 (void)copy_from_guest(&done, pdone, 1);
2187 domain_mmap_cache_init(&mapcache);
2188 domain_mmap_cache_init(&sh_mapcache);
2190 if ( !set_foreigndom(foreigndom) )
2192 rc = -ESRCH;
2193 goto out;
2196 perfc_incrc(calls_to_mmu_update);
2197 perfc_addc(num_page_updates, count);
2199 if ( unlikely(!guest_handle_okay(ureqs, count)) )
2201 rc = -EFAULT;
2202 goto out;
2205 for ( i = 0; i < count; i++ )
2207 if ( hypercall_preempt_check() )
2209 rc = hypercall_create_continuation(
2210 __HYPERVISOR_mmu_update, "hihi",
2211 ureqs, (count - i) | MMU_UPDATE_PREEMPTED, pdone, foreigndom);
2212 break;
2215 if ( unlikely(__copy_from_guest(&req, ureqs, 1) != 0) )
2217 MEM_LOG("Bad __copy_from_guest");
2218 rc = -EFAULT;
2219 break;
2222 cmd = req.ptr & (sizeof(l1_pgentry_t)-1);
2223 okay = 0;
2225 switch ( cmd )
2227 /*
2228 * MMU_NORMAL_PT_UPDATE: Normal update to any level of page table.
2229 */
2230 case MMU_NORMAL_PT_UPDATE:
2232 gmfn = req.ptr >> PAGE_SHIFT;
2233 mfn = gmfn_to_mfn(d, gmfn);
2235 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2237 MEM_LOG("Could not get page for normal update");
2238 break;
2241 va = map_domain_page_with_cache(mfn, &mapcache);
2242 va = (void *)((unsigned long)va +
2243 (unsigned long)(req.ptr & ~PAGE_MASK));
2244 page = mfn_to_page(mfn);
2246 switch ( (type_info = page->u.inuse.type_info) & PGT_type_mask )
2248 case PGT_l1_page_table:
2249 case PGT_l2_page_table:
2250 case PGT_l3_page_table:
2251 case PGT_l4_page_table:
2253 if ( shadow_mode_refcounts(d) )
2255 MEM_LOG("mmu update on shadow-refcounted domain!");
2256 break;
2259 if ( unlikely(!get_page_type(
2260 page, type_info & (PGT_type_mask|PGT_pae_xen_l2))) )
2261 goto not_a_pt;
2263 switch ( type_info & PGT_type_mask )
2265 case PGT_l1_page_table:
2267 l1_pgentry_t l1e = l1e_from_intpte(req.val);
2268 okay = mod_l1_entry(va, l1e, mfn);
2270 break;
2271 case PGT_l2_page_table:
2273 l2_pgentry_t l2e = l2e_from_intpte(req.val);
2274 okay = mod_l2_entry(
2275 (l2_pgentry_t *)va, l2e, mfn, type_info);
2277 break;
2278 #if CONFIG_PAGING_LEVELS >= 3
2279 case PGT_l3_page_table:
2281 l3_pgentry_t l3e = l3e_from_intpte(req.val);
2282 okay = mod_l3_entry(va, l3e, mfn);
2284 break;
2285 #endif
2286 #if CONFIG_PAGING_LEVELS >= 4
2287 case PGT_l4_page_table:
2289 l4_pgentry_t l4e = l4e_from_intpte(req.val);
2290 okay = mod_l4_entry(va, l4e, mfn);
2292 break;
2293 #endif
2296 put_page_type(page);
2298 break;
2300 default:
2301 not_a_pt:
2303 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
2304 break;
2306 if ( unlikely(shadow_mode_enabled(d)) )
2307 shadow_lock(d);
2309 *(intpte_t *)va = req.val;
2310 okay = 1;
2312 if ( unlikely(shadow_mode_enabled(d)) )
2314 shadow_validate_guest_entry(v, _mfn(mfn), va);
2315 shadow_unlock(d);
2318 put_page_type(page);
2320 break;
2323 unmap_domain_page_with_cache(va, &mapcache);
2325 put_page(page);
2326 break;
2328 case MMU_MACHPHYS_UPDATE:
2330 mfn = req.ptr >> PAGE_SHIFT;
2331 gpfn = req.val;
2333 if ( unlikely(!get_page_from_pagenr(mfn, FOREIGNDOM)) )
2335 MEM_LOG("Could not get page for mach->phys update");
2336 break;
2339 if ( shadow_mode_translate(FOREIGNDOM) )
2340 shadow_guest_physmap_add_page(FOREIGNDOM, gpfn, mfn);
2341 else
2342 set_gpfn_from_mfn(mfn, gpfn);
2343 okay = 1;
2345 // Mark the new gfn dirty...
2346 mark_dirty(FOREIGNDOM, mfn);
2348 put_page(mfn_to_page(mfn));
2349 break;
2351 default:
2352 MEM_LOG("Invalid page update command %x", cmd);
2353 break;
2356 if ( unlikely(!okay) )
2358 rc = -EINVAL;
2359 break;
2362 guest_handle_add_offset(ureqs, 1);
2365 out:
2366 domain_mmap_cache_destroy(&mapcache);
2367 domain_mmap_cache_destroy(&sh_mapcache);
2369 process_deferred_ops();
2371 /* Add incremental work we have done to the @done output parameter. */
2372 done += i;
2373 if ( unlikely(!guest_handle_is_null(pdone)) )
2374 copy_to_guest(pdone, &done, 1);
2376 UNLOCK_BIGLOCK(d);
2377 return rc;
2381 static int create_grant_pte_mapping(
2382 unsigned long pte_addr, l1_pgentry_t nl1e, struct vcpu *v)
2384 int rc = GNTST_okay;
2385 void *va;
2386 unsigned long gmfn, mfn;
2387 struct page_info *page;
2388 u32 type;
2389 l1_pgentry_t ol1e;
2390 struct domain *d = v->domain;
2392 ASSERT(spin_is_locked(&d->big_lock));
2394 adjust_guest_l1e(nl1e);
2396 gmfn = pte_addr >> PAGE_SHIFT;
2397 mfn = gmfn_to_mfn(d, gmfn);
2399 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2401 MEM_LOG("Could not get page for normal update");
2402 return GNTST_general_error;
2405 va = map_domain_page(mfn);
2406 va = (void *)((unsigned long)va + (pte_addr & ~PAGE_MASK));
2407 page = mfn_to_page(mfn);
2409 type = page->u.inuse.type_info & PGT_type_mask;
2410 if ( (type != PGT_l1_page_table) || !get_page_type(page, type) )
2412 MEM_LOG("Grant map attempted to update a non-L1 page");
2413 rc = GNTST_general_error;
2414 goto failed;
2417 ol1e = *(l1_pgentry_t *)va;
2418 if ( !update_l1e(va, ol1e, nl1e, mfn, v) )
2420 put_page_type(page);
2421 rc = GNTST_general_error;
2422 goto failed;
2425 if ( !shadow_mode_refcounts(d) )
2426 put_page_from_l1e(ol1e, d);
2428 put_page_type(page);
2430 failed:
2431 unmap_domain_page(va);
2432 put_page(page);
2434 return rc;
2437 static int destroy_grant_pte_mapping(
2438 unsigned long addr, unsigned long frame, struct domain *d)
2440 int rc = GNTST_okay;
2441 void *va;
2442 unsigned long gmfn, mfn;
2443 struct page_info *page;
2444 u32 type;
2445 l1_pgentry_t ol1e;
2447 gmfn = addr >> PAGE_SHIFT;
2448 mfn = gmfn_to_mfn(d, gmfn);
2450 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2452 MEM_LOG("Could not get page for normal update");
2453 return GNTST_general_error;
2456 va = map_domain_page(mfn);
2457 va = (void *)((unsigned long)va + (addr & ~PAGE_MASK));
2458 page = mfn_to_page(mfn);
2460 type = page->u.inuse.type_info & PGT_type_mask;
2461 if ( (type != PGT_l1_page_table) || !get_page_type(page, type) )
2463 MEM_LOG("Grant map attempted to update a non-L1 page");
2464 rc = GNTST_general_error;
2465 goto failed;
2468 if ( __copy_from_user(&ol1e, (l1_pgentry_t *)va, sizeof(ol1e)) )
2470 put_page_type(page);
2471 rc = GNTST_general_error;
2472 goto failed;
2475 /* Check that the virtual address supplied is actually mapped to frame. */
2476 if ( unlikely((l1e_get_intpte(ol1e) >> PAGE_SHIFT) != frame) )
2478 MEM_LOG("PTE entry %lx for address %lx doesn't match frame %lx",
2479 (unsigned long)l1e_get_intpte(ol1e), addr, frame);
2480 put_page_type(page);
2481 rc = GNTST_general_error;
2482 goto failed;
2485 /* Delete pagetable entry. */
2486 if ( unlikely(!update_l1e(
2487 (l1_pgentry_t *)va, ol1e, l1e_empty(), mfn,
2488 d->vcpu[0] /* Change if we go to per-vcpu shadows. */)) )
2490 MEM_LOG("Cannot delete PTE entry at %p", va);
2491 put_page_type(page);
2492 rc = GNTST_general_error;
2493 goto failed;
2496 put_page_type(page);
2498 failed:
2499 unmap_domain_page(va);
2500 put_page(page);
2501 return rc;
2505 static int create_grant_va_mapping(
2506 unsigned long va, l1_pgentry_t nl1e, struct vcpu *v)
2508 l1_pgentry_t *pl1e, ol1e;
2509 struct domain *d = v->domain;
2510 unsigned long gl1mfn;
2511 int okay;
2513 ASSERT(spin_is_locked(&d->big_lock));
2515 adjust_guest_l1e(nl1e);
2517 pl1e = guest_map_l1e(v, va, &gl1mfn);
2518 if ( !pl1e )
2520 MEM_LOG("Could not find L1 PTE for address %lx", va);
2521 return GNTST_general_error;
2523 ol1e = *pl1e;
2524 okay = update_l1e(pl1e, ol1e, nl1e, gl1mfn, v);
2525 guest_unmap_l1e(v, pl1e);
2526 pl1e = NULL;
2528 if ( !okay )
2529 return GNTST_general_error;
2531 if ( !shadow_mode_refcounts(d) )
2532 put_page_from_l1e(ol1e, d);
2534 return GNTST_okay;
2537 static int destroy_grant_va_mapping(
2538 unsigned long addr, unsigned long frame, struct vcpu *v)
2540 l1_pgentry_t *pl1e, ol1e;
2541 unsigned long gl1mfn;
2542 int rc = 0;
2544 pl1e = guest_map_l1e(v, addr, &gl1mfn);
2545 if ( !pl1e )
2547 MEM_LOG("Could not find L1 PTE for address %lx", addr);
2548 return GNTST_general_error;
2550 ol1e = *pl1e;
2552 /*
2553 * Check that the virtual address supplied is actually mapped to
2554 * frame.
2555 */
2556 if ( unlikely(l1e_get_pfn(ol1e) != frame) )
2558 MEM_LOG("PTE entry %lx for address %lx doesn't match frame %lx",
2559 l1e_get_pfn(ol1e), addr, frame);
2560 rc = GNTST_general_error;
2561 goto out;
2564 /* Delete pagetable entry. */
2565 if ( unlikely(!update_l1e(pl1e, ol1e, l1e_empty(), gl1mfn, v)) )
2567 MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
2568 rc = GNTST_general_error;
2569 goto out; // this is redundant & unnecessary, but informative
2572 out:
2573 guest_unmap_l1e(v, pl1e);
2574 return rc;
2577 int create_grant_host_mapping(
2578 unsigned long addr, unsigned long frame, unsigned int flags)
2580 l1_pgentry_t pte = l1e_from_pfn(frame, GRANT_PTE_FLAGS);
2582 if ( (flags & GNTMAP_application_map) )
2583 l1e_add_flags(pte,_PAGE_USER);
2584 if ( !(flags & GNTMAP_readonly) )
2585 l1e_add_flags(pte,_PAGE_RW);
2587 if ( flags & GNTMAP_contains_pte )
2588 return create_grant_pte_mapping(addr, pte, current);
2589 return create_grant_va_mapping(addr, pte, current);
2592 int destroy_grant_host_mapping(
2593 unsigned long addr, unsigned long frame, unsigned int flags)
2595 if ( flags & GNTMAP_contains_pte )
2596 return destroy_grant_pte_mapping(addr, frame, current->domain);
2597 return destroy_grant_va_mapping(addr, frame, current);
2600 int steal_page(
2601 struct domain *d, struct page_info *page, unsigned int memflags)
2603 u32 _d, _nd, x, y;
2605 spin_lock(&d->page_alloc_lock);
2607 /*
2608 * The tricky bit: atomically release ownership while there is just one
2609 * benign reference to the page (PGC_allocated). If that reference
2610 * disappears then the deallocation routine will safely spin.
2611 */
2612 _d = pickle_domptr(d);
2613 _nd = page->u.inuse._domain;
2614 y = page->count_info;
2615 do {
2616 x = y;
2617 if (unlikely((x & (PGC_count_mask|PGC_allocated)) !=
2618 (1 | PGC_allocated)) || unlikely(_nd != _d)) {
2619 MEM_LOG("gnttab_transfer: Bad page %p: ed=%p(%u), sd=%p,"
2620 " caf=%08x, taf=%" PRtype_info "\n",
2621 (void *) page_to_mfn(page),
2622 d, d->domain_id, unpickle_domptr(_nd), x,
2623 page->u.inuse.type_info);
2624 spin_unlock(&d->page_alloc_lock);
2625 return -1;
2627 __asm__ __volatile__(
2628 LOCK_PREFIX "cmpxchg8b %2"
2629 : "=d" (_nd), "=a" (y),
2630 "=m" (*(volatile u64 *)(&page->count_info))
2631 : "0" (_d), "1" (x), "c" (NULL), "b" (x) );
2632 } while (unlikely(_nd != _d) || unlikely(y != x));
2634 /*
2635 * Unlink from 'd'. At least one reference remains (now anonymous), so
2636 * noone else is spinning to try to delete this page from 'd'.
2637 */
2638 if ( !(memflags & MEMF_no_refcount) )
2639 d->tot_pages--;
2640 list_del(&page->list);
2642 spin_unlock(&d->page_alloc_lock);
2644 return 0;
2647 int do_update_va_mapping(unsigned long va, u64 val64,
2648 unsigned long flags)
2650 l1_pgentry_t val = l1e_from_intpte(val64);
2651 struct vcpu *v = current;
2652 struct domain *d = v->domain;
2653 l1_pgentry_t *pl1e;
2654 unsigned long vmask, bmap_ptr, gl1mfn;
2655 cpumask_t pmask;
2656 int rc = 0;
2658 perfc_incrc(calls_to_update_va);
2660 if ( unlikely(!__addr_ok(va) && !shadow_mode_external(d)) )
2661 return -EINVAL;
2663 LOCK_BIGLOCK(d);
2665 pl1e = guest_map_l1e(v, va, &gl1mfn);
2667 if ( unlikely(!pl1e || !mod_l1_entry(pl1e, val, gl1mfn)) )
2668 rc = -EINVAL;
2670 if ( pl1e )
2671 guest_unmap_l1e(v, pl1e);
2672 pl1e = NULL;
2674 switch ( flags & UVMF_FLUSHTYPE_MASK )
2676 case UVMF_TLB_FLUSH:
2677 switch ( (bmap_ptr = flags & ~UVMF_FLUSHTYPE_MASK) )
2679 case UVMF_LOCAL:
2680 local_flush_tlb();
2681 break;
2682 case UVMF_ALL:
2683 flush_tlb_mask(d->domain_dirty_cpumask);
2684 break;
2685 default:
2686 if ( unlikely(get_user(vmask, (unsigned long *)bmap_ptr)) )
2687 rc = -EFAULT;
2688 pmask = vcpumask_to_pcpumask(d, vmask);
2689 flush_tlb_mask(pmask);
2690 break;
2692 break;
2694 case UVMF_INVLPG:
2695 switch ( (bmap_ptr = flags & ~UVMF_FLUSHTYPE_MASK) )
2697 case UVMF_LOCAL:
2698 if ( !shadow_mode_enabled(d)
2699 || (shadow_invlpg(current, va) != 0) )
2700 local_flush_tlb_one(va);
2701 break;
2702 case UVMF_ALL:
2703 flush_tlb_one_mask(d->domain_dirty_cpumask, va);
2704 break;
2705 default:
2706 if ( unlikely(get_user(vmask, (unsigned long *)bmap_ptr)) )
2707 rc = -EFAULT;
2708 pmask = vcpumask_to_pcpumask(d, vmask);
2709 flush_tlb_one_mask(pmask, va);
2710 break;
2712 break;
2715 process_deferred_ops();
2717 UNLOCK_BIGLOCK(d);
2719 return rc;
2722 int do_update_va_mapping_otherdomain(unsigned long va, u64 val64,
2723 unsigned long flags,
2724 domid_t domid)
2726 int rc;
2728 if ( unlikely(!IS_PRIV(current->domain)) )
2729 return -EPERM;
2731 if ( !set_foreigndom(domid) )
2732 return -ESRCH;
2734 rc = do_update_va_mapping(va, val64, flags);
2736 return rc;
2741 /*************************
2742 * Descriptor Tables
2743 */
2745 void destroy_gdt(struct vcpu *v)
2747 int i;
2748 unsigned long pfn;
2750 v->arch.guest_context.gdt_ents = 0;
2751 for ( i = 0; i < FIRST_RESERVED_GDT_PAGE; i++ )
2753 if ( (pfn = l1e_get_pfn(v->arch.perdomain_ptes[i])) != 0 )
2754 put_page_and_type(mfn_to_page(pfn));
2755 v->arch.perdomain_ptes[i] = l1e_empty();
2756 v->arch.guest_context.gdt_frames[i] = 0;
2761 long set_gdt(struct vcpu *v,
2762 unsigned long *frames,
2763 unsigned int entries)
2765 struct domain *d = v->domain;
2766 /* NB. There are 512 8-byte entries per GDT page. */
2767 int i, nr_pages = (entries + 511) / 512;
2768 unsigned long mfn;
2770 if ( entries > FIRST_RESERVED_GDT_ENTRY )
2771 return -EINVAL;
2773 /* Check the pages in the new GDT. */
2774 for ( i = 0; i < nr_pages; i++ ) {
2775 mfn = frames[i] = gmfn_to_mfn(d, frames[i]);
2776 if ( !mfn_valid(mfn) ||
2777 !get_page_and_type(mfn_to_page(mfn), d, PGT_gdt_page) )
2778 goto fail;
2781 /* Tear down the old GDT. */
2782 destroy_gdt(v);
2784 /* Install the new GDT. */
2785 v->arch.guest_context.gdt_ents = entries;
2786 for ( i = 0; i < nr_pages; i++ )
2788 v->arch.guest_context.gdt_frames[i] = frames[i];
2789 v->arch.perdomain_ptes[i] =
2790 l1e_from_pfn(frames[i], __PAGE_HYPERVISOR);
2793 return 0;
2795 fail:
2796 while ( i-- > 0 )
2797 put_page_and_type(mfn_to_page(frames[i]));
2798 return -EINVAL;
2802 long do_set_gdt(XEN_GUEST_HANDLE(ulong) frame_list, unsigned int entries)
2804 int nr_pages = (entries + 511) / 512;
2805 unsigned long frames[16];
2806 long ret;
2808 /* Rechecked in set_gdt, but ensures a sane limit for copy_from_user(). */
2809 if ( entries > FIRST_RESERVED_GDT_ENTRY )
2810 return -EINVAL;
2812 if ( copy_from_guest((unsigned long *)frames, frame_list, nr_pages) )
2813 return -EFAULT;
2815 LOCK_BIGLOCK(current->domain);
2817 if ( (ret = set_gdt(current, frames, entries)) == 0 )
2818 local_flush_tlb();
2820 UNLOCK_BIGLOCK(current->domain);
2822 return ret;
2826 long do_update_descriptor(u64 pa, u64 desc)
2828 struct domain *dom = current->domain;
2829 unsigned long gmfn = pa >> PAGE_SHIFT;
2830 unsigned long mfn;
2831 unsigned int offset;
2832 struct desc_struct *gdt_pent, d;
2833 struct page_info *page;
2834 long ret = -EINVAL;
2836 offset = ((unsigned int)pa & ~PAGE_MASK) / sizeof(struct desc_struct);
2838 *(u64 *)&d = desc;
2840 LOCK_BIGLOCK(dom);
2842 if ( !VALID_MFN(mfn = gmfn_to_mfn(dom, gmfn)) ||
2843 (((unsigned int)pa % sizeof(struct desc_struct)) != 0) ||
2844 !mfn_valid(mfn) ||
2845 !check_descriptor(&d) )
2847 UNLOCK_BIGLOCK(dom);
2848 return -EINVAL;
2851 page = mfn_to_page(mfn);
2852 if ( unlikely(!get_page(page, dom)) )
2854 UNLOCK_BIGLOCK(dom);
2855 return -EINVAL;
2858 /* Check if the given frame is in use in an unsafe context. */
2859 switch ( page->u.inuse.type_info & PGT_type_mask )
2861 case PGT_gdt_page:
2862 if ( unlikely(!get_page_type(page, PGT_gdt_page)) )
2863 goto out;
2864 break;
2865 case PGT_ldt_page:
2866 if ( unlikely(!get_page_type(page, PGT_ldt_page)) )
2867 goto out;
2868 break;
2869 default:
2870 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
2871 goto out;
2872 break;
2875 mark_dirty(dom, mfn);
2877 /* All is good so make the update. */
2878 gdt_pent = map_domain_page(mfn);
2879 memcpy(&gdt_pent[offset], &d, 8);
2880 unmap_domain_page(gdt_pent);
2882 put_page_type(page);
2884 ret = 0; /* success */
2886 out:
2887 put_page(page);
2889 UNLOCK_BIGLOCK(dom);
2891 return ret;
2894 typedef struct e820entry e820entry_t;
2895 DEFINE_XEN_GUEST_HANDLE(e820entry_t);
2897 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
2899 switch ( op )
2901 case XENMEM_add_to_physmap:
2903 struct xen_add_to_physmap xatp;
2904 unsigned long prev_mfn, mfn = 0, gpfn;
2905 struct domain *d;
2907 if ( copy_from_guest(&xatp, arg, 1) )
2908 return -EFAULT;
2910 if ( xatp.domid == DOMID_SELF )
2912 d = current->domain;
2913 get_knownalive_domain(d);
2915 else if ( !IS_PRIV(current->domain) )
2916 return -EPERM;
2917 else if ( (d = find_domain_by_id(xatp.domid)) == NULL )
2918 return -ESRCH;
2920 switch ( xatp.space )
2922 case XENMAPSPACE_shared_info:
2923 if ( xatp.idx == 0 )
2924 mfn = virt_to_mfn(d->shared_info);
2925 break;
2926 case XENMAPSPACE_grant_table:
2927 if ( xatp.idx < NR_GRANT_FRAMES )
2928 mfn = virt_to_mfn(d->grant_table->shared) + xatp.idx;
2929 break;
2930 default:
2931 break;
2934 if ( !shadow_mode_translate(d) || (mfn == 0) )
2936 put_domain(d);
2937 return -EINVAL;
2940 LOCK_BIGLOCK(d);
2942 /* Remove previously mapped page if it was present. */
2943 prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
2944 if ( mfn_valid(prev_mfn) )
2946 if ( IS_XEN_HEAP_FRAME(mfn_to_page(prev_mfn)) )
2947 /* Xen heap frames are simply unhooked from this phys slot. */
2948 guest_physmap_remove_page(d, xatp.gpfn, prev_mfn);
2949 else
2950 /* Normal domain memory is freed, to avoid leaking memory. */
2951 guest_remove_page(d, xatp.gpfn);
2954 /* Unmap from old location, if any. */
2955 gpfn = get_gpfn_from_mfn(mfn);
2956 if ( gpfn != INVALID_M2P_ENTRY )
2957 guest_physmap_remove_page(d, gpfn, mfn);
2959 /* Map at new location. */
2960 guest_physmap_add_page(d, xatp.gpfn, mfn);
2962 UNLOCK_BIGLOCK(d);
2964 put_domain(d);
2966 break;
2969 case XENMEM_memory_map:
2971 return -ENOSYS;
2974 case XENMEM_machine_memory_map:
2976 struct xen_memory_map memmap;
2977 XEN_GUEST_HANDLE(e820entry_t) buffer;
2978 int count;
2980 if ( !IS_PRIV(current->domain) )
2981 return -EINVAL;
2983 if ( copy_from_guest(&memmap, arg, 1) )
2984 return -EFAULT;
2985 if ( memmap.nr_entries < e820.nr_map + 1 )
2986 return -EINVAL;
2988 buffer = guest_handle_cast(memmap.buffer, e820entry_t);
2990 count = min((unsigned int)e820.nr_map, memmap.nr_entries);
2991 if ( copy_to_guest(buffer, &e820.map[0], count) < 0 )
2992 return -EFAULT;
2994 memmap.nr_entries = count;
2996 if ( copy_to_guest(arg, &memmap, 1) )
2997 return -EFAULT;
2999 return 0;
3002 case XENMEM_machphys_mapping:
3004 struct xen_machphys_mapping mapping = {
3005 .v_start = MACH2PHYS_VIRT_START,
3006 .v_end = MACH2PHYS_VIRT_END,
3007 .max_mfn = MACH2PHYS_NR_ENTRIES - 1
3008 };
3010 if ( copy_to_guest(arg, &mapping, 1) )
3011 return -EFAULT;
3013 return 0;
3016 default:
3017 return subarch_memory_op(op, arg);
3020 return 0;
3024 /*************************
3025 * Writable Pagetables
3026 */
3028 static int ptwr_emulated_update(
3029 unsigned long addr,
3030 paddr_t old,
3031 paddr_t val,
3032 unsigned int bytes,
3033 unsigned int do_cmpxchg)
3035 unsigned long gmfn, mfn;
3036 struct page_info *page;
3037 l1_pgentry_t pte, ol1e, nl1e, *pl1e;
3038 struct vcpu *v = current;
3039 struct domain *d = v->domain;
3041 /* Aligned access only, thank you. */
3042 if ( !access_ok(addr, bytes) || ((addr & (bytes-1)) != 0) )
3044 MEM_LOG("ptwr_emulate: Unaligned or bad size ptwr access (%d, %lx)",
3045 bytes, addr);
3046 return X86EMUL_UNHANDLEABLE;
3049 /* Turn a sub-word access into a full-word access. */
3050 if ( bytes != sizeof(paddr_t) )
3052 paddr_t full;
3053 unsigned int rc, offset = addr & (sizeof(paddr_t)-1);
3055 /* Align address; read full word. */
3056 addr &= ~(sizeof(paddr_t)-1);
3057 if ( (rc = copy_from_user(&full, (void *)addr, sizeof(paddr_t))) != 0 )
3059 propagate_page_fault(addr+sizeof(paddr_t)-rc, 0); /* read fault */
3060 return X86EMUL_PROPAGATE_FAULT;
3062 /* Mask out bits provided by caller. */
3063 full &= ~((((paddr_t)1 << (bytes*8)) - 1) << (offset*8));
3064 /* Shift the caller value and OR in the missing bits. */
3065 val &= (((paddr_t)1 << (bytes*8)) - 1);
3066 val <<= (offset)*8;
3067 val |= full;
3068 /* Also fill in missing parts of the cmpxchg old value. */
3069 old &= (((paddr_t)1 << (bytes*8)) - 1);
3070 old <<= (offset)*8;
3071 old |= full;
3074 /* Read the PTE that maps the page being updated. */
3075 guest_get_eff_l1e(v, addr, &pte);
3076 if ( unlikely(!(l1e_get_flags(pte) & _PAGE_PRESENT)) )
3078 MEM_LOG("%s: Cannot get L1 PTE for guest address %lx",
3079 __func__, addr);
3080 return X86EMUL_UNHANDLEABLE;
3083 gmfn = l1e_get_pfn(pte);
3084 mfn = gmfn_to_mfn(d, gmfn);
3085 page = mfn_to_page(mfn);
3087 /* We are looking only for read-only mappings of p.t. pages. */
3088 ASSERT((l1e_get_flags(pte) & (_PAGE_RW|_PAGE_PRESENT)) == _PAGE_PRESENT);
3089 ASSERT((page->u.inuse.type_info & PGT_type_mask) == PGT_l1_page_table);
3090 ASSERT((page->u.inuse.type_info & PGT_count_mask) != 0);
3091 ASSERT(page_get_owner(page) == d);
3093 /* Check the new PTE. */
3094 nl1e = l1e_from_intpte(val);
3095 if ( unlikely(!get_page_from_l1e(gl1e_to_ml1e(d, nl1e), d)) )
3097 if ( (CONFIG_PAGING_LEVELS == 3) &&
3098 (bytes == 4) &&
3099 !do_cmpxchg &&
3100 (l1e_get_flags(nl1e) & _PAGE_PRESENT) )
3102 /*
3103 * If this is a half-write to a PAE PTE then we assume that the
3104 * guest has simply got the two writes the wrong way round. We
3105 * zap the PRESENT bit on the assumption the bottom half will be
3106 * written immediately after we return to the guest.
3107 */
3108 MEM_LOG("ptwr_emulate: fixing up invalid PAE PTE %"PRIpte"\n",
3109 l1e_get_intpte(nl1e));
3110 l1e_remove_flags(nl1e, _PAGE_PRESENT);
3112 else
3114 MEM_LOG("ptwr_emulate: could not get_page_from_l1e()");
3115 return X86EMUL_UNHANDLEABLE;
3119 adjust_guest_l1e(nl1e);
3121 /* Checked successfully: do the update (write or cmpxchg). */
3122 pl1e = map_domain_page(page_to_mfn(page));
3123 pl1e = (l1_pgentry_t *)((unsigned long)pl1e + (addr & ~PAGE_MASK));
3124 if ( do_cmpxchg )
3126 if ( shadow_mode_enabled(d) )
3127 shadow_lock(d);
3128 ol1e = l1e_from_intpte(old);
3129 if ( cmpxchg((intpte_t *)pl1e, old, val) != old )
3131 if ( shadow_mode_enabled(d) )
3132 shadow_unlock(d);
3133 unmap_domain_page(pl1e);
3134 put_page_from_l1e(gl1e_to_ml1e(d, nl1e), d);
3135 return X86EMUL_CMPXCHG_FAILED;
3137 if ( unlikely(shadow_mode_enabled(d)) )
3139 shadow_validate_guest_entry(v, _mfn(page_to_mfn(page)), pl1e);
3140 shadow_unlock(d);
3143 else
3145 ol1e = *pl1e;
3146 if ( !update_l1e(pl1e, ol1e, nl1e, page_to_mfn(page), v) )
3147 BUG();
3150 unmap_domain_page(pl1e);
3152 /* Finally, drop the old PTE. */
3153 put_page_from_l1e(gl1e_to_ml1e(d, ol1e), d);
3155 return X86EMUL_CONTINUE;
3158 static int ptwr_emulated_write(
3159 unsigned long addr,
3160 unsigned long val,
3161 unsigned int bytes,
3162 struct x86_emulate_ctxt *ctxt)
3164 return ptwr_emulated_update(addr, 0, val, bytes, 0);
3167 static int ptwr_emulated_cmpxchg(
3168 unsigned long addr,
3169 unsigned long old,
3170 unsigned long new,
3171 unsigned int bytes,
3172 struct x86_emulate_ctxt *ctxt)
3174 return ptwr_emulated_update(addr, old, new, bytes, 1);
3177 static int ptwr_emulated_cmpxchg8b(
3178 unsigned long addr,
3179 unsigned long old,
3180 unsigned long old_hi,
3181 unsigned long new,
3182 unsigned long new_hi,
3183 struct x86_emulate_ctxt *ctxt)
3185 if ( CONFIG_PAGING_LEVELS == 2 )
3186 return X86EMUL_UNHANDLEABLE;
3187 else
3188 return ptwr_emulated_update(
3189 addr, ((u64)old_hi << 32) | old, ((u64)new_hi << 32) | new, 8, 1);
3192 static struct x86_emulate_ops ptwr_emulate_ops = {
3193 .read_std = x86_emulate_read_std,
3194 .write_std = x86_emulate_write_std,
3195 .read_emulated = x86_emulate_read_std,
3196 .write_emulated = ptwr_emulated_write,
3197 .cmpxchg_emulated = ptwr_emulated_cmpxchg,
3198 .cmpxchg8b_emulated = ptwr_emulated_cmpxchg8b
3199 };
3201 /* Write page fault handler: check if guest is trying to modify a PTE. */
3202 int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
3203 struct cpu_user_regs *regs)
3205 struct domain *d = v->domain;
3206 unsigned long pfn;
3207 struct page_info *page;
3208 l1_pgentry_t pte;
3209 struct x86_emulate_ctxt emul_ctxt;
3211 LOCK_BIGLOCK(d);
3213 /*
3214 * Attempt to read the PTE that maps the VA being accessed. By checking for
3215 * PDE validity in the L2 we avoid many expensive fixups in __get_user().
3216 */
3217 guest_get_eff_l1e(v, addr, &pte);
3218 if ( !(l1e_get_flags(pte) & _PAGE_PRESENT) )
3219 goto bail;
3220 pfn = l1e_get_pfn(pte);
3221 page = mfn_to_page(pfn);
3223 /* We are looking only for read-only mappings of p.t. pages. */
3224 if ( ((l1e_get_flags(pte) & (_PAGE_PRESENT|_PAGE_RW)) != _PAGE_PRESENT) ||
3225 ((page->u.inuse.type_info & PGT_type_mask) != PGT_l1_page_table) ||
3226 ((page->u.inuse.type_info & PGT_count_mask) == 0) ||
3227 (page_get_owner(page) != d) )
3228 goto bail;
3230 emul_ctxt.regs = guest_cpu_user_regs();
3231 emul_ctxt.cr2 = addr;
3232 emul_ctxt.mode = X86EMUL_MODE_HOST;
3233 if ( x86_emulate_memop(&emul_ctxt, &ptwr_emulate_ops) )
3234 goto bail;
3236 UNLOCK_BIGLOCK(d);
3237 perfc_incrc(ptwr_emulations);
3238 return EXCRET_fault_fixed;
3240 bail:
3241 UNLOCK_BIGLOCK(d);
3242 return 0;
3245 int map_pages_to_xen(
3246 unsigned long virt,
3247 unsigned long mfn,
3248 unsigned long nr_mfns,
3249 unsigned long flags)
3251 l2_pgentry_t *pl2e, ol2e;
3252 l1_pgentry_t *pl1e, ol1e;
3253 unsigned int i;
3255 unsigned int map_small_pages = !!(flags & MAP_SMALL_PAGES);
3256 flags &= ~MAP_SMALL_PAGES;
3258 while ( nr_mfns != 0 )
3260 pl2e = virt_to_xen_l2e(virt);
3262 if ( ((((virt>>PAGE_SHIFT) | mfn) & ((1<<PAGETABLE_ORDER)-1)) == 0) &&
3263 (nr_mfns >= (1<<PAGETABLE_ORDER)) &&
3264 !map_small_pages )
3266 /* Super-page mapping. */
3267 ol2e = *pl2e;
3268 *pl2e = l2e_from_pfn(mfn, flags|_PAGE_PSE);
3270 if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) )
3272 local_flush_tlb_pge();
3273 if ( !(l2e_get_flags(ol2e) & _PAGE_PSE) )
3274 free_xen_pagetable(l2e_get_page(ol2e));
3277 virt += 1UL << L2_PAGETABLE_SHIFT;
3278 mfn += 1UL << PAGETABLE_ORDER;
3279 nr_mfns -= 1UL << PAGETABLE_ORDER;
3281 else
3283 /* Normal page mapping. */
3284 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
3286 pl1e = page_to_virt(alloc_xen_pagetable());
3287 clear_page(pl1e);
3288 *pl2e = l2e_from_page(virt_to_page(pl1e), __PAGE_HYPERVISOR);
3290 else if ( l2e_get_flags(*pl2e) & _PAGE_PSE )
3292 pl1e = page_to_virt(alloc_xen_pagetable());
3293 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
3294 pl1e[i] = l1e_from_pfn(
3295 l2e_get_pfn(*pl2e) + i,
3296 l2e_get_flags(*pl2e) & ~_PAGE_PSE);
3297 *pl2e = l2e_from_page(virt_to_page(pl1e), __PAGE_HYPERVISOR);
3298 local_flush_tlb_pge();
3301 pl1e = l2e_to_l1e(*pl2e) + l1_table_offset(virt);
3302 ol1e = *pl1e;
3303 *pl1e = l1e_from_pfn(mfn, flags);
3304 if ( (l1e_get_flags(ol1e) & _PAGE_PRESENT) )
3305 local_flush_tlb_one(virt);
3307 virt += 1UL << L1_PAGETABLE_SHIFT;
3308 mfn += 1UL;
3309 nr_mfns -= 1UL;
3313 return 0;
3316 void __set_fixmap(
3317 enum fixed_addresses idx, unsigned long mfn, unsigned long flags)
3319 BUG_ON(idx >= __end_of_fixed_addresses);
3320 map_pages_to_xen(fix_to_virt(idx), mfn, 1, flags);
3323 #ifdef MEMORY_GUARD
3325 void memguard_init(void)
3327 map_pages_to_xen(
3328 PAGE_OFFSET, 0, xenheap_phys_end >> PAGE_SHIFT,
3329 __PAGE_HYPERVISOR|MAP_SMALL_PAGES);
3332 static void __memguard_change_range(void *p, unsigned long l, int guard)
3334 unsigned long _p = (unsigned long)p;
3335 unsigned long _l = (unsigned long)l;
3336 unsigned long flags = __PAGE_HYPERVISOR | MAP_SMALL_PAGES;
3338 /* Ensure we are dealing with a page-aligned whole number of pages. */
3339 ASSERT((_p&PAGE_MASK) != 0);
3340 ASSERT((_l&PAGE_MASK) != 0);
3341 ASSERT((_p&~PAGE_MASK) == 0);
3342 ASSERT((_l&~PAGE_MASK) == 0);
3344 if ( guard )
3345 flags &= ~_PAGE_PRESENT;
3347 map_pages_to_xen(
3348 _p, virt_to_maddr(p) >> PAGE_SHIFT, _l >> PAGE_SHIFT, flags);
3351 void memguard_guard_range(void *p, unsigned long l)
3353 __memguard_change_range(p, l, 1);
3356 void memguard_unguard_range(void *p, unsigned long l)
3358 __memguard_change_range(p, l, 0);
3361 #endif
3363 void memguard_guard_stack(void *p)
3365 BUILD_BUG_ON((DEBUG_STACK_SIZE + PAGE_SIZE) > STACK_SIZE);
3366 p = (void *)((unsigned long)p + STACK_SIZE - DEBUG_STACK_SIZE - PAGE_SIZE);
3367 memguard_guard_range(p, PAGE_SIZE);
3370 /*
3371 * Local variables:
3372 * mode: C
3373 * c-set-style: "BSD"
3374 * c-basic-offset: 4
3375 * tab-width: 4
3376 * indent-tabs-mode: nil
3377 * End:
3378 */