ia64/xen-unstable

view xen/arch/x86/mm.c @ 12599:93e657836d07

[XEN] Remove VALID_MFN(); replace uses with mfn_valid().
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Mon Nov 27 17:48:24 2006 +0000 (2006-11-27)
parents 2ae4e4e89d6d
children 88935ae47fa9
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(!mfn_valid(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 if ( mfn != (PADDR_MASK >> PAGE_SHIFT) ) /* INVALID_MFN? */
576 MEM_LOG("Non-privileged (%u) attempt to map I/O space %08lx",
577 d->domain_id, mfn);
578 return 0;
579 }
581 /* No reference counting for out-of-range I/O pages. */
582 if ( !mfn_valid(mfn) )
583 return 1;
585 d = dom_io;
586 }
588 /* Foreign mappings into guests in shadow external mode don't
589 * contribute to writeable mapping refcounts. (This allows the
590 * qemu-dm helper process in dom0 to map the domain's memory without
591 * messing up the count of "real" writable mappings.) */
592 okay = (((l1e_get_flags(l1e) & _PAGE_RW) &&
593 !(unlikely(shadow_mode_external(d) && (d != current->domain))))
594 ? get_page_and_type(page, d, PGT_writable_page)
595 : get_page(page, d));
596 if ( !okay )
597 {
598 MEM_LOG("Error getting mfn %lx (pfn %lx) from L1 entry %" PRIpte
599 " for dom%d",
600 mfn, get_gpfn_from_mfn(mfn),
601 l1e_get_intpte(l1e), d->domain_id);
602 }
604 return okay;
605 }
608 /* NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'. */
609 static int
610 get_page_from_l2e(
611 l2_pgentry_t l2e, unsigned long pfn, struct domain *d)
612 {
613 int rc;
615 if ( !(l2e_get_flags(l2e) & _PAGE_PRESENT) )
616 return 1;
618 if ( unlikely((l2e_get_flags(l2e) & L2_DISALLOW_MASK)) )
619 {
620 MEM_LOG("Bad L2 flags %x", l2e_get_flags(l2e) & L2_DISALLOW_MASK);
621 return 0;
622 }
624 rc = get_page_and_type_from_pagenr(l2e_get_pfn(l2e), PGT_l1_page_table, d);
625 #if CONFIG_PAGING_LEVELS == 2
626 if ( unlikely(!rc) )
627 rc = get_linear_pagetable(l2e, pfn, d);
628 #endif
629 return rc;
630 }
633 #if CONFIG_PAGING_LEVELS >= 3
634 static int
635 get_page_from_l3e(
636 l3_pgentry_t l3e, unsigned long pfn, struct domain *d)
637 {
638 int rc;
640 if ( !(l3e_get_flags(l3e) & _PAGE_PRESENT) )
641 return 1;
643 if ( unlikely((l3e_get_flags(l3e) & L3_DISALLOW_MASK)) )
644 {
645 MEM_LOG("Bad L3 flags %x", l3e_get_flags(l3e) & L3_DISALLOW_MASK);
646 return 0;
647 }
649 rc = get_page_and_type_from_pagenr(l3e_get_pfn(l3e), PGT_l2_page_table, d);
650 return rc;
651 }
652 #endif /* 3 level */
654 #if CONFIG_PAGING_LEVELS >= 4
655 static int
656 get_page_from_l4e(
657 l4_pgentry_t l4e, unsigned long pfn, struct domain *d)
658 {
659 int rc;
661 if ( !(l4e_get_flags(l4e) & _PAGE_PRESENT) )
662 return 1;
664 if ( unlikely((l4e_get_flags(l4e) & L4_DISALLOW_MASK)) )
665 {
666 MEM_LOG("Bad L4 flags %x", l4e_get_flags(l4e) & L4_DISALLOW_MASK);
667 return 0;
668 }
670 rc = get_page_and_type_from_pagenr(l4e_get_pfn(l4e), PGT_l3_page_table, d);
672 if ( unlikely(!rc) )
673 rc = get_linear_pagetable(l4e, pfn, d);
675 return rc;
676 }
677 #endif /* 4 level */
679 #ifdef __x86_64__
681 #ifdef USER_MAPPINGS_ARE_GLOBAL
682 #define adjust_guest_l1e(pl1e) \
683 do { \
684 if ( likely(l1e_get_flags((pl1e)) & _PAGE_PRESENT) ) \
685 { \
686 /* _PAGE_GUEST_KERNEL page cannot have the Global bit set. */ \
687 if ( (l1e_get_flags((pl1e)) & (_PAGE_GUEST_KERNEL|_PAGE_GLOBAL)) \
688 == (_PAGE_GUEST_KERNEL|_PAGE_GLOBAL) ) \
689 MEM_LOG("Global bit is set to kernel page %lx", \
690 l1e_get_pfn((pl1e))); \
691 if ( !(l1e_get_flags((pl1e)) & _PAGE_USER) ) \
692 l1e_add_flags((pl1e), (_PAGE_GUEST_KERNEL|_PAGE_USER)); \
693 if ( !(l1e_get_flags((pl1e)) & _PAGE_GUEST_KERNEL) ) \
694 l1e_add_flags((pl1e), (_PAGE_GLOBAL|_PAGE_USER)); \
695 } \
696 } while ( 0 )
697 #else
698 #define adjust_guest_l1e(pl1e) \
699 do { \
700 if ( likely(l1e_get_flags((pl1e)) & _PAGE_PRESENT) ) \
701 l1e_add_flags((pl1e), _PAGE_USER); \
702 } while ( 0 )
703 #endif
705 #define adjust_guest_l2e(pl2e) \
706 do { \
707 if ( likely(l2e_get_flags((pl2e)) & _PAGE_PRESENT) ) \
708 l2e_add_flags((pl2e), _PAGE_USER); \
709 } while ( 0 )
711 #define adjust_guest_l3e(pl3e) \
712 do { \
713 if ( likely(l3e_get_flags((pl3e)) & _PAGE_PRESENT) ) \
714 l3e_add_flags((pl3e), _PAGE_USER); \
715 } while ( 0 )
717 #define adjust_guest_l4e(pl4e) \
718 do { \
719 if ( likely(l4e_get_flags((pl4e)) & _PAGE_PRESENT) ) \
720 l4e_add_flags((pl4e), _PAGE_USER); \
721 } while ( 0 )
723 #else /* !defined(__x86_64__) */
725 #define adjust_guest_l1e(_p) ((void)0)
726 #define adjust_guest_l2e(_p) ((void)0)
727 #define adjust_guest_l3e(_p) ((void)0)
729 #endif
731 void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
732 {
733 unsigned long pfn = l1e_get_pfn(l1e);
734 struct page_info *page = mfn_to_page(pfn);
735 struct domain *e;
736 struct vcpu *v;
738 if ( !(l1e_get_flags(l1e) & _PAGE_PRESENT) || !mfn_valid(pfn) )
739 return;
741 e = page_get_owner(page);
743 /*
744 * Check if this is a mapping that was established via a grant reference.
745 * If it was then we should not be here: we require that such mappings are
746 * explicitly destroyed via the grant-table interface.
747 *
748 * The upshot of this is that the guest can end up with active grants that
749 * it cannot destroy (because it no longer has a PTE to present to the
750 * grant-table interface). This can lead to subtle hard-to-catch bugs,
751 * hence a special grant PTE flag can be enabled to catch the bug early.
752 *
753 * (Note that the undestroyable active grants are not a security hole in
754 * Xen. All active grants can safely be cleaned up when the domain dies.)
755 */
756 if ( (l1e_get_flags(l1e) & _PAGE_GNTTAB) &&
757 !(d->domain_flags & (DOMF_shutdown|DOMF_dying)) )
758 {
759 MEM_LOG("Attempt to implicitly unmap a granted PTE %" PRIpte,
760 l1e_get_intpte(l1e));
761 domain_crash(d);
762 }
764 /* Remember we didn't take a type-count of foreign writable mappings
765 * to shadow external domains */
766 if ( (l1e_get_flags(l1e) & _PAGE_RW) &&
767 !(unlikely((e != d) && shadow_mode_external(e))) )
768 {
769 put_page_and_type(page);
770 }
771 else
772 {
773 /* We expect this is rare so we blow the entire shadow LDT. */
774 if ( unlikely(((page->u.inuse.type_info & PGT_type_mask) ==
775 PGT_ldt_page)) &&
776 unlikely(((page->u.inuse.type_info & PGT_count_mask) != 0)) &&
777 (d == e) )
778 {
779 for_each_vcpu ( d, v )
780 invalidate_shadow_ldt(v);
781 }
782 put_page(page);
783 }
784 }
787 /*
788 * NB. Virtual address 'l2e' maps to a machine address within frame 'pfn'.
789 * Note also that this automatically deals correctly with linear p.t.'s.
790 */
791 static void put_page_from_l2e(l2_pgentry_t l2e, unsigned long pfn)
792 {
793 if ( (l2e_get_flags(l2e) & _PAGE_PRESENT) &&
794 (l2e_get_pfn(l2e) != pfn) )
795 put_page_and_type(mfn_to_page(l2e_get_pfn(l2e)));
796 }
799 #if CONFIG_PAGING_LEVELS >= 3
800 static void put_page_from_l3e(l3_pgentry_t l3e, unsigned long pfn)
801 {
802 if ( (l3e_get_flags(l3e) & _PAGE_PRESENT) &&
803 (l3e_get_pfn(l3e) != pfn) )
804 put_page_and_type(mfn_to_page(l3e_get_pfn(l3e)));
805 }
806 #endif
808 #if CONFIG_PAGING_LEVELS >= 4
809 static void put_page_from_l4e(l4_pgentry_t l4e, unsigned long pfn)
810 {
811 if ( (l4e_get_flags(l4e) & _PAGE_PRESENT) &&
812 (l4e_get_pfn(l4e) != pfn) )
813 put_page_and_type(mfn_to_page(l4e_get_pfn(l4e)));
814 }
815 #endif
817 static int alloc_l1_table(struct page_info *page)
818 {
819 struct domain *d = page_get_owner(page);
820 unsigned long pfn = page_to_mfn(page);
821 l1_pgentry_t *pl1e;
822 int i;
824 ASSERT(!shadow_mode_refcounts(d));
826 pl1e = map_domain_page(pfn);
828 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
829 {
830 if ( is_guest_l1_slot(i) &&
831 unlikely(!get_page_from_l1e(pl1e[i], d)) )
832 goto fail;
834 adjust_guest_l1e(pl1e[i]);
835 }
837 unmap_domain_page(pl1e);
838 return 1;
840 fail:
841 MEM_LOG("Failure in alloc_l1_table: entry %d", i);
842 while ( i-- > 0 )
843 if ( is_guest_l1_slot(i) )
844 put_page_from_l1e(pl1e[i], d);
846 unmap_domain_page(pl1e);
847 return 0;
848 }
850 #ifdef CONFIG_X86_PAE
851 static int create_pae_xen_mappings(l3_pgentry_t *pl3e)
852 {
853 struct page_info *page;
854 l2_pgentry_t *pl2e;
855 l3_pgentry_t l3e3;
856 int i;
858 pl3e = (l3_pgentry_t *)((unsigned long)pl3e & PAGE_MASK);
860 /* 3rd L3 slot contains L2 with Xen-private mappings. It *must* exist. */
861 l3e3 = pl3e[3];
862 if ( !(l3e_get_flags(l3e3) & _PAGE_PRESENT) )
863 {
864 MEM_LOG("PAE L3 3rd slot is empty");
865 return 0;
866 }
868 /*
869 * The Xen-private mappings include linear mappings. The L2 thus cannot
870 * be shared by multiple L3 tables. The test here is adequate because:
871 * 1. Cannot appear in slots != 3 because get_page_type() checks the
872 * PGT_pae_xen_l2 flag, which is asserted iff the L2 appears in slot 3
873 * 2. Cannot appear in another page table's L3:
874 * a. alloc_l3_table() calls this function and this check will fail
875 * b. mod_l3_entry() disallows updates to slot 3 in an existing table
876 *
877 * XXX -- this needs revisiting for shadow_mode_refcount()==true...
878 */
879 page = l3e_get_page(l3e3);
880 BUG_ON(page->u.inuse.type_info & PGT_pinned);
881 BUG_ON((page->u.inuse.type_info & PGT_count_mask) == 0);
882 BUG_ON(!(page->u.inuse.type_info & PGT_pae_xen_l2));
883 if ( (page->u.inuse.type_info & PGT_count_mask) != 1 )
884 {
885 MEM_LOG("PAE L3 3rd slot is shared");
886 return 0;
887 }
889 /* Xen private mappings. */
890 pl2e = map_domain_page(l3e_get_pfn(l3e3));
891 memcpy(&pl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
892 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
893 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
894 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
895 pl2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] =
896 l2e_from_page(
897 virt_to_page(page_get_owner(page)->arch.mm_perdomain_pt) + i,
898 __PAGE_HYPERVISOR);
899 for ( i = 0; i < (LINEARPT_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
900 pl2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
901 (l3e_get_flags(pl3e[i]) & _PAGE_PRESENT) ?
902 l2e_from_pfn(l3e_get_pfn(pl3e[i]), __PAGE_HYPERVISOR) :
903 l2e_empty();
904 unmap_domain_page(pl2e);
906 return 1;
907 }
909 /* Flush a pgdir update into low-memory caches. */
910 static void pae_flush_pgd(
911 unsigned long mfn, unsigned int idx, l3_pgentry_t nl3e)
912 {
913 struct domain *d = page_get_owner(mfn_to_page(mfn));
914 struct vcpu *v;
915 intpte_t _ol3e, _nl3e, _pl3e;
916 l3_pgentry_t *l3tab_ptr;
917 struct pae_l3_cache *cache;
919 /* If below 4GB then the pgdir is not shadowed in low memory. */
920 if ( !l3tab_needs_shadow(mfn) )
921 return;
923 for_each_vcpu ( d, v )
924 {
925 cache = &v->arch.pae_l3_cache;
927 spin_lock(&cache->lock);
929 if ( cache->high_mfn == mfn )
930 {
931 l3tab_ptr = &cache->table[cache->inuse_idx][idx];
932 _ol3e = l3e_get_intpte(*l3tab_ptr);
933 _nl3e = l3e_get_intpte(nl3e);
934 _pl3e = cmpxchg((intpte_t *)l3tab_ptr, _ol3e, _nl3e);
935 BUG_ON(_pl3e != _ol3e);
936 }
938 spin_unlock(&cache->lock);
939 }
941 flush_tlb_mask(d->domain_dirty_cpumask);
942 }
944 #elif CONFIG_X86_64
945 # define create_pae_xen_mappings(pl3e) (1)
946 # define pae_flush_pgd(mfn, idx, nl3e) ((void)0)
947 #else
948 # define create_pae_xen_mappings(pl3e) (1)
949 #endif
951 static int alloc_l2_table(struct page_info *page, unsigned long type)
952 {
953 struct domain *d = page_get_owner(page);
954 unsigned long pfn = page_to_mfn(page);
955 l2_pgentry_t *pl2e;
956 int i;
958 ASSERT(!shadow_mode_refcounts(d));
960 pl2e = map_domain_page(pfn);
962 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
963 {
964 if ( is_guest_l2_slot(type, i) &&
965 unlikely(!get_page_from_l2e(pl2e[i], pfn, d)) )
966 goto fail;
968 adjust_guest_l2e(pl2e[i]);
969 }
971 #if CONFIG_PAGING_LEVELS == 2
972 /* Xen private mappings. */
973 memcpy(&pl2e[L2_PAGETABLE_FIRST_XEN_SLOT],
974 &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT],
975 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
976 pl2e[l2_table_offset(LINEAR_PT_VIRT_START)] =
977 l2e_from_pfn(pfn, __PAGE_HYPERVISOR);
978 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
979 pl2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] =
980 l2e_from_page(
981 virt_to_page(page_get_owner(page)->arch.mm_perdomain_pt) + i,
982 __PAGE_HYPERVISOR);
983 #endif
985 unmap_domain_page(pl2e);
986 return 1;
988 fail:
989 MEM_LOG("Failure in alloc_l2_table: entry %d", i);
990 while ( i-- > 0 )
991 if ( is_guest_l2_slot(type, i) )
992 put_page_from_l2e(pl2e[i], pfn);
994 unmap_domain_page(pl2e);
995 return 0;
996 }
999 #if CONFIG_PAGING_LEVELS >= 3
1000 static int alloc_l3_table(struct page_info *page)
1002 struct domain *d = page_get_owner(page);
1003 unsigned long pfn = page_to_mfn(page);
1004 l3_pgentry_t *pl3e;
1005 int i;
1007 ASSERT(!shadow_mode_refcounts(d));
1009 #ifdef CONFIG_X86_PAE
1010 /*
1011 * PAE pgdirs above 4GB are unacceptable if the guest does not understand
1012 * the weird 'extended cr3' format for dealing with high-order address
1013 * bits. We cut some slack for control tools (before vcpu0 is initialised).
1014 */
1015 if ( (pfn >= 0x100000) &&
1016 unlikely(!VM_ASSIST(d, VMASST_TYPE_pae_extended_cr3)) &&
1017 d->vcpu[0] && test_bit(_VCPUF_initialised, &d->vcpu[0]->vcpu_flags) )
1019 MEM_LOG("PAE pgd must be below 4GB (0x%lx >= 0x100000)", pfn);
1020 return 0;
1022 #endif
1024 pl3e = map_domain_page(pfn);
1025 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1027 #ifdef CONFIG_X86_PAE
1028 if ( i == 3 )
1030 if ( !(l3e_get_flags(pl3e[i]) & _PAGE_PRESENT) ||
1031 (l3e_get_flags(pl3e[i]) & L3_DISALLOW_MASK) ||
1032 !get_page_and_type_from_pagenr(l3e_get_pfn(pl3e[i]),
1033 PGT_l2_page_table |
1034 PGT_pae_xen_l2,
1035 d) )
1036 goto fail;
1038 else
1039 #endif
1040 if ( is_guest_l3_slot(i) &&
1041 unlikely(!get_page_from_l3e(pl3e[i], pfn, d)) )
1042 goto fail;
1044 adjust_guest_l3e(pl3e[i]);
1047 if ( !create_pae_xen_mappings(pl3e) )
1048 goto fail;
1050 unmap_domain_page(pl3e);
1051 return 1;
1053 fail:
1054 MEM_LOG("Failure in alloc_l3_table: entry %d", i);
1055 while ( i-- > 0 )
1056 if ( is_guest_l3_slot(i) )
1057 put_page_from_l3e(pl3e[i], pfn);
1059 unmap_domain_page(pl3e);
1060 return 0;
1062 #else
1063 #define alloc_l3_table(page) (0)
1064 #endif
1066 #if CONFIG_PAGING_LEVELS >= 4
1067 static int alloc_l4_table(struct page_info *page)
1069 struct domain *d = page_get_owner(page);
1070 unsigned long pfn = page_to_mfn(page);
1071 l4_pgentry_t *pl4e = page_to_virt(page);
1072 int i;
1074 ASSERT(!shadow_mode_refcounts(d));
1076 for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
1078 if ( is_guest_l4_slot(i) &&
1079 unlikely(!get_page_from_l4e(pl4e[i], pfn, d)) )
1080 goto fail;
1082 adjust_guest_l4e(pl4e[i]);
1085 /* Xen private mappings. */
1086 memcpy(&pl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1087 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1088 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1089 pl4e[l4_table_offset(LINEAR_PT_VIRT_START)] =
1090 l4e_from_pfn(pfn, __PAGE_HYPERVISOR);
1091 pl4e[l4_table_offset(PERDOMAIN_VIRT_START)] =
1092 l4e_from_page(
1093 virt_to_page(page_get_owner(page)->arch.mm_perdomain_l3),
1094 __PAGE_HYPERVISOR);
1096 return 1;
1098 fail:
1099 MEM_LOG("Failure in alloc_l4_table: entry %d", i);
1100 while ( i-- > 0 )
1101 if ( is_guest_l4_slot(i) )
1102 put_page_from_l4e(pl4e[i], pfn);
1104 return 0;
1106 #else
1107 #define alloc_l4_table(page) (0)
1108 #endif
1111 static void free_l1_table(struct page_info *page)
1113 struct domain *d = page_get_owner(page);
1114 unsigned long pfn = page_to_mfn(page);
1115 l1_pgentry_t *pl1e;
1116 int i;
1118 pl1e = map_domain_page(pfn);
1120 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
1121 if ( is_guest_l1_slot(i) )
1122 put_page_from_l1e(pl1e[i], d);
1124 unmap_domain_page(pl1e);
1128 static void free_l2_table(struct page_info *page)
1130 unsigned long pfn = page_to_mfn(page);
1131 l2_pgentry_t *pl2e;
1132 int i;
1134 pl2e = map_domain_page(pfn);
1136 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
1137 if ( is_guest_l2_slot(page->u.inuse.type_info, i) )
1138 put_page_from_l2e(pl2e[i], pfn);
1140 unmap_domain_page(pl2e);
1142 page->u.inuse.type_info &= ~PGT_pae_xen_l2;
1146 #if CONFIG_PAGING_LEVELS >= 3
1148 static void free_l3_table(struct page_info *page)
1150 unsigned long pfn = page_to_mfn(page);
1151 l3_pgentry_t *pl3e;
1152 int i;
1154 pl3e = map_domain_page(pfn);
1156 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1157 if ( is_guest_l3_slot(i) )
1158 put_page_from_l3e(pl3e[i], pfn);
1160 unmap_domain_page(pl3e);
1163 #endif
1165 #if CONFIG_PAGING_LEVELS >= 4
1167 static void free_l4_table(struct page_info *page)
1169 unsigned long pfn = page_to_mfn(page);
1170 l4_pgentry_t *pl4e = page_to_virt(page);
1171 int i;
1173 for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
1174 if ( is_guest_l4_slot(i) )
1175 put_page_from_l4e(pl4e[i], pfn);
1178 #endif
1180 static inline int update_l1e(l1_pgentry_t *pl1e,
1181 l1_pgentry_t ol1e,
1182 l1_pgentry_t nl1e,
1183 unsigned long gl1mfn,
1184 struct vcpu *v)
1186 int rv = 1;
1187 if ( unlikely(shadow_mode_enabled(v->domain)) )
1188 shadow_lock(v->domain);
1189 #ifndef PTE_UPDATE_WITH_CMPXCHG
1190 rv = (!__copy_to_user(pl1e, &nl1e, sizeof(nl1e)));
1191 #else
1193 intpte_t o = l1e_get_intpte(ol1e);
1194 intpte_t n = l1e_get_intpte(nl1e);
1196 for ( ; ; )
1198 if ( unlikely(cmpxchg_user(pl1e, o, n) != 0) )
1200 MEM_LOG("Failed to update %" PRIpte " -> %" PRIpte
1201 ": saw %" PRIpte,
1202 l1e_get_intpte(ol1e),
1203 l1e_get_intpte(nl1e),
1204 o);
1205 rv = 0;
1206 break;
1209 if ( o == l1e_get_intpte(ol1e) )
1210 break;
1212 /* Allowed to change in Accessed/Dirty flags only. */
1213 BUG_ON((o ^ l1e_get_intpte(ol1e)) &
1214 ~(int)(_PAGE_ACCESSED|_PAGE_DIRTY));
1215 ol1e = l1e_from_intpte(o);
1218 #endif
1219 if ( unlikely(shadow_mode_enabled(v->domain)) && rv )
1221 shadow_validate_guest_entry(v, _mfn(gl1mfn), pl1e);
1222 shadow_unlock(v->domain);
1224 return rv;
1228 /* Update the L1 entry at pl1e to new value nl1e. */
1229 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
1230 unsigned long gl1mfn)
1232 l1_pgentry_t ol1e;
1233 struct domain *d = current->domain;
1235 if ( unlikely(__copy_from_user(&ol1e, pl1e, sizeof(ol1e)) != 0) )
1236 return 0;
1238 if ( unlikely(shadow_mode_refcounts(d)) )
1239 return update_l1e(pl1e, ol1e, nl1e, gl1mfn, current);
1241 if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
1243 /* Translate foreign guest addresses. */
1244 nl1e = l1e_from_pfn(gmfn_to_mfn(FOREIGNDOM, l1e_get_pfn(nl1e)),
1245 l1e_get_flags(nl1e));
1247 if ( unlikely(l1e_get_flags(nl1e) & L1_DISALLOW_MASK) )
1249 MEM_LOG("Bad L1 flags %x",
1250 l1e_get_flags(nl1e) & L1_DISALLOW_MASK);
1251 return 0;
1254 adjust_guest_l1e(nl1e);
1256 /* Fast path for identical mapping, r/w and presence. */
1257 if ( !l1e_has_changed(ol1e, nl1e, _PAGE_RW | _PAGE_PRESENT) )
1258 return update_l1e(pl1e, ol1e, nl1e, gl1mfn, current);
1260 if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
1261 return 0;
1263 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e, gl1mfn, current)) )
1265 put_page_from_l1e(nl1e, d);
1266 return 0;
1269 else
1271 if ( unlikely(!update_l1e(pl1e, ol1e, nl1e, gl1mfn, current)) )
1272 return 0;
1275 put_page_from_l1e(ol1e, d);
1276 return 1;
1279 #ifndef PTE_UPDATE_WITH_CMPXCHG
1280 #define _UPDATE_ENTRY(_t,_p,_o,_n) ({ (*(_p) = (_n)); 1; })
1281 #else
1282 #define _UPDATE_ENTRY(_t,_p,_o,_n) ({ \
1283 for ( ; ; ) \
1284 { \
1285 intpte_t __o = cmpxchg((intpte_t *)(_p), \
1286 _t ## e_get_intpte(_o), \
1287 _t ## e_get_intpte(_n)); \
1288 if ( __o == _t ## e_get_intpte(_o) ) \
1289 break; \
1290 /* Allowed to change in Accessed/Dirty flags only. */ \
1291 BUG_ON((__o ^ _t ## e_get_intpte(_o)) & \
1292 ~(int)(_PAGE_ACCESSED|_PAGE_DIRTY)); \
1293 _o = _t ## e_from_intpte(__o); \
1294 } \
1295 1; })
1296 #endif
1297 #define UPDATE_ENTRY(_t,_p,_o,_n,_m) ({ \
1298 int rv; \
1299 if ( unlikely(shadow_mode_enabled(current->domain)) ) \
1300 shadow_lock(current->domain); \
1301 rv = _UPDATE_ENTRY(_t, _p, _o, _n); \
1302 if ( unlikely(shadow_mode_enabled(current->domain)) ) \
1303 { \
1304 shadow_validate_guest_entry(current, _mfn(_m), (_p)); \
1305 shadow_unlock(current->domain); \
1306 } \
1307 rv; \
1308 })
1310 /* Update the L2 entry at pl2e to new value nl2e. pl2e is within frame pfn. */
1311 static int mod_l2_entry(l2_pgentry_t *pl2e,
1312 l2_pgentry_t nl2e,
1313 unsigned long pfn,
1314 unsigned long type)
1316 l2_pgentry_t ol2e;
1318 if ( unlikely(!is_guest_l2_slot(type,pgentry_ptr_to_slot(pl2e))) )
1320 MEM_LOG("Illegal L2 update attempt in Xen-private area %p", pl2e);
1321 return 0;
1324 if ( unlikely(__copy_from_user(&ol2e, pl2e, sizeof(ol2e)) != 0) )
1325 return 0;
1327 if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
1329 if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
1331 MEM_LOG("Bad L2 flags %x",
1332 l2e_get_flags(nl2e) & L2_DISALLOW_MASK);
1333 return 0;
1336 adjust_guest_l2e(nl2e);
1338 /* Fast path for identical mapping and presence. */
1339 if ( !l2e_has_changed(ol2e, nl2e, _PAGE_PRESENT))
1340 return UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn);
1342 if ( unlikely(!get_page_from_l2e(nl2e, pfn, current->domain)) )
1343 return 0;
1345 if ( unlikely(!UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn)) )
1347 put_page_from_l2e(nl2e, pfn);
1348 return 0;
1351 else if ( unlikely(!UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, pfn)) )
1353 return 0;
1356 put_page_from_l2e(ol2e, pfn);
1357 return 1;
1360 #if CONFIG_PAGING_LEVELS >= 3
1362 /* Update the L3 entry at pl3e to new value nl3e. pl3e is within frame pfn. */
1363 static int mod_l3_entry(l3_pgentry_t *pl3e,
1364 l3_pgentry_t nl3e,
1365 unsigned long pfn)
1367 l3_pgentry_t ol3e;
1368 int okay;
1370 if ( unlikely(!is_guest_l3_slot(pgentry_ptr_to_slot(pl3e))) )
1372 MEM_LOG("Illegal L3 update attempt in Xen-private area %p", pl3e);
1373 return 0;
1376 #ifdef CONFIG_X86_PAE
1377 /*
1378 * Disallow updates to final L3 slot. It contains Xen mappings, and it
1379 * would be a pain to ensure they remain continuously valid throughout.
1380 */
1381 if ( pgentry_ptr_to_slot(pl3e) >= 3 )
1382 return 0;
1383 #endif
1385 if ( unlikely(__copy_from_user(&ol3e, pl3e, sizeof(ol3e)) != 0) )
1386 return 0;
1388 if ( l3e_get_flags(nl3e) & _PAGE_PRESENT )
1390 if ( unlikely(l3e_get_flags(nl3e) & L3_DISALLOW_MASK) )
1392 MEM_LOG("Bad L3 flags %x",
1393 l3e_get_flags(nl3e) & L3_DISALLOW_MASK);
1394 return 0;
1397 adjust_guest_l3e(nl3e);
1399 /* Fast path for identical mapping and presence. */
1400 if (!l3e_has_changed(ol3e, nl3e, _PAGE_PRESENT))
1401 return UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn);
1403 if ( unlikely(!get_page_from_l3e(nl3e, pfn, current->domain)) )
1404 return 0;
1406 if ( unlikely(!UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn)) )
1408 put_page_from_l3e(nl3e, pfn);
1409 return 0;
1412 else if ( unlikely(!UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, pfn)) )
1414 return 0;
1417 okay = create_pae_xen_mappings(pl3e);
1418 BUG_ON(!okay);
1420 pae_flush_pgd(pfn, pgentry_ptr_to_slot(pl3e), nl3e);
1422 put_page_from_l3e(ol3e, pfn);
1423 return 1;
1426 #endif
1428 #if CONFIG_PAGING_LEVELS >= 4
1430 /* Update the L4 entry at pl4e to new value nl4e. pl4e is within frame pfn. */
1431 static int mod_l4_entry(l4_pgentry_t *pl4e,
1432 l4_pgentry_t nl4e,
1433 unsigned long pfn)
1435 l4_pgentry_t ol4e;
1437 if ( unlikely(!is_guest_l4_slot(pgentry_ptr_to_slot(pl4e))) )
1439 MEM_LOG("Illegal L4 update attempt in Xen-private area %p", pl4e);
1440 return 0;
1443 if ( unlikely(__copy_from_user(&ol4e, pl4e, sizeof(ol4e)) != 0) )
1444 return 0;
1446 if ( l4e_get_flags(nl4e) & _PAGE_PRESENT )
1448 if ( unlikely(l4e_get_flags(nl4e) & L4_DISALLOW_MASK) )
1450 MEM_LOG("Bad L4 flags %x",
1451 l4e_get_flags(nl4e) & L4_DISALLOW_MASK);
1452 return 0;
1455 adjust_guest_l4e(nl4e);
1457 /* Fast path for identical mapping and presence. */
1458 if (!l4e_has_changed(ol4e, nl4e, _PAGE_PRESENT))
1459 return UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn);
1461 if ( unlikely(!get_page_from_l4e(nl4e, pfn, current->domain)) )
1462 return 0;
1464 if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn)) )
1466 put_page_from_l4e(nl4e, pfn);
1467 return 0;
1470 else if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn)) )
1472 return 0;
1475 put_page_from_l4e(ol4e, pfn);
1476 return 1;
1479 #endif
1481 int alloc_page_type(struct page_info *page, unsigned long type)
1483 struct domain *owner = page_get_owner(page);
1485 /* A page table is dirtied when its type count becomes non-zero. */
1486 if ( likely(owner != NULL) )
1487 mark_dirty(owner, page_to_mfn(page));
1489 switch ( type & PGT_type_mask )
1491 case PGT_l1_page_table:
1492 return alloc_l1_table(page);
1493 case PGT_l2_page_table:
1494 return alloc_l2_table(page, type);
1495 case PGT_l3_page_table:
1496 return alloc_l3_table(page);
1497 case PGT_l4_page_table:
1498 return alloc_l4_table(page);
1499 case PGT_gdt_page:
1500 case PGT_ldt_page:
1501 return alloc_segdesc_page(page);
1502 default:
1503 printk("Bad type in alloc_page_type %lx t=%" PRtype_info " c=%x\n",
1504 type, page->u.inuse.type_info,
1505 page->count_info);
1506 BUG();
1509 return 0;
1513 void free_page_type(struct page_info *page, unsigned long type)
1515 struct domain *owner = page_get_owner(page);
1516 unsigned long gmfn;
1518 if ( likely(owner != NULL) )
1520 /*
1521 * We have to flush before the next use of the linear mapping
1522 * (e.g., update_va_mapping()) or we could end up modifying a page
1523 * that is no longer a page table (and hence screw up ref counts).
1524 */
1525 this_cpu(percpu_mm_info).deferred_ops |= DOP_FLUSH_ALL_TLBS;
1527 if ( unlikely(shadow_mode_enabled(owner)) )
1529 /* A page table is dirtied when its type count becomes zero. */
1530 mark_dirty(owner, page_to_mfn(page));
1532 if ( shadow_mode_refcounts(owner) )
1533 return;
1535 gmfn = mfn_to_gmfn(owner, page_to_mfn(page));
1536 ASSERT(VALID_M2P(gmfn));
1537 shadow_remove_all_shadows(owner->vcpu[0], _mfn(gmfn));
1541 switch ( type & PGT_type_mask )
1543 case PGT_l1_page_table:
1544 free_l1_table(page);
1545 break;
1547 case PGT_l2_page_table:
1548 free_l2_table(page);
1549 break;
1551 #if CONFIG_PAGING_LEVELS >= 3
1552 case PGT_l3_page_table:
1553 free_l3_table(page);
1554 break;
1555 #endif
1557 #if CONFIG_PAGING_LEVELS >= 4
1558 case PGT_l4_page_table:
1559 free_l4_table(page);
1560 break;
1561 #endif
1563 default:
1564 printk("%s: type %lx pfn %lx\n",__FUNCTION__,
1565 type, page_to_mfn(page));
1566 BUG();
1571 void put_page_type(struct page_info *page)
1573 unsigned long nx, x, y = page->u.inuse.type_info;
1575 again:
1576 do {
1577 x = y;
1578 nx = x - 1;
1580 ASSERT((x & PGT_count_mask) != 0);
1582 if ( unlikely((nx & PGT_count_mask) == 0) )
1584 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
1585 likely(nx & PGT_validated) )
1587 /*
1588 * Page-table pages must be unvalidated when count is zero. The
1589 * 'free' is safe because the refcnt is non-zero and validated
1590 * bit is clear => other ops will spin or fail.
1591 */
1592 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
1593 x & ~PGT_validated)) != x) )
1594 goto again;
1595 /* We cleared the 'valid bit' so we do the clean up. */
1596 free_page_type(page, x);
1597 /* Carry on, but with the 'valid bit' now clear. */
1598 x &= ~PGT_validated;
1599 nx &= ~PGT_validated;
1602 /*
1603 * Record TLB information for flush later. We do not stamp page
1604 * tables when running in shadow mode:
1605 * 1. Pointless, since it's the shadow pt's which must be tracked.
1606 * 2. Shadow mode reuses this field for shadowed page tables to
1607 * store flags info -- we don't want to conflict with that.
1608 */
1609 if ( !(shadow_mode_enabled(page_get_owner(page)) &&
1610 (page->count_info & PGC_page_table)) )
1611 page->tlbflush_timestamp = tlbflush_current_time();
1614 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
1618 int get_page_type(struct page_info *page, unsigned long type)
1620 unsigned long nx, x, y = page->u.inuse.type_info;
1622 ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
1624 again:
1625 do {
1626 x = y;
1627 nx = x + 1;
1628 if ( unlikely((nx & PGT_count_mask) == 0) )
1630 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
1631 return 0;
1633 else if ( unlikely((x & PGT_count_mask) == 0) )
1635 struct domain *d = page_get_owner(page);
1637 /* Never allow a shadowed frame to go from type count 0 to 1 */
1638 if ( d && shadow_mode_enabled(d) )
1639 shadow_remove_all_shadows(d->vcpu[0], _mfn(page_to_mfn(page)));
1641 ASSERT(!(x & PGT_pae_xen_l2));
1642 if ( (x & PGT_type_mask) != type )
1644 /*
1645 * On type change we check to flush stale TLB entries. This
1646 * may be unnecessary (e.g., page was GDT/LDT) but those
1647 * circumstances should be very rare.
1648 */
1649 cpumask_t mask = d->domain_dirty_cpumask;
1651 /* Don't flush if the timestamp is old enough */
1652 tlbflush_filter(mask, page->tlbflush_timestamp);
1654 if ( unlikely(!cpus_empty(mask)) &&
1655 /* Shadow mode: track only writable pages. */
1656 (!shadow_mode_enabled(page_get_owner(page)) ||
1657 ((nx & PGT_type_mask) == PGT_writable_page)) )
1659 perfc_incrc(need_flush_tlb_flush);
1660 flush_tlb_mask(mask);
1663 /* We lose existing type, back pointer, and validity. */
1664 nx &= ~(PGT_type_mask | PGT_validated);
1665 nx |= type;
1667 /* No special validation needed for writable pages. */
1668 /* Page tables and GDT/LDT need to be scanned for validity. */
1669 if ( type == PGT_writable_page )
1670 nx |= PGT_validated;
1673 else if ( unlikely((x & (PGT_type_mask|PGT_pae_xen_l2)) != type) )
1675 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
1676 (type != PGT_l1_page_table) )
1677 MEM_LOG("Bad type (saw %" PRtype_info
1678 " != exp %" PRtype_info ") "
1679 "for mfn %lx (pfn %lx)",
1680 x, type, page_to_mfn(page),
1681 get_gpfn_from_mfn(page_to_mfn(page)));
1682 return 0;
1684 else if ( unlikely(!(x & PGT_validated)) )
1686 /* Someone else is updating validation of this page. Wait... */
1687 while ( (y = page->u.inuse.type_info) == x )
1688 cpu_relax();
1689 goto again;
1692 while ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x) );
1694 if ( unlikely(!(nx & PGT_validated)) )
1696 /* Try to validate page type; drop the new reference on failure. */
1697 if ( unlikely(!alloc_page_type(page, type)) )
1699 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %"
1700 PRtype_info ": caf=%08x taf=%" PRtype_info,
1701 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
1702 type, page->count_info, page->u.inuse.type_info);
1703 /* Noone else can get a reference. We hold the only ref. */
1704 page->u.inuse.type_info = 0;
1705 return 0;
1708 /* Noone else is updating simultaneously. */
1709 __set_bit(_PGT_validated, &page->u.inuse.type_info);
1712 return 1;
1716 int new_guest_cr3(unsigned long mfn)
1718 struct vcpu *v = current;
1719 struct domain *d = v->domain;
1720 int okay;
1721 unsigned long old_base_mfn;
1723 if ( is_hvm_domain(d) && !hvm_paging_enabled(v) )
1724 return 0;
1726 if ( shadow_mode_refcounts(d) )
1728 okay = get_page_from_pagenr(mfn, d);
1729 if ( unlikely(!okay) )
1731 MEM_LOG("Error while installing new baseptr %lx", mfn);
1732 return 0;
1735 else
1737 okay = get_page_and_type_from_pagenr(mfn, PGT_root_page_table, d);
1738 if ( unlikely(!okay) )
1740 /* Switch to idle pagetable: this VCPU has no active p.t. now. */
1741 MEM_LOG("New baseptr %lx: slow path via idle pagetables", mfn);
1742 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1743 v->arch.guest_table = pagetable_null();
1744 update_cr3(v);
1745 write_cr3(__pa(idle_pg_table));
1746 if ( old_base_mfn != 0 )
1747 put_page_and_type(mfn_to_page(old_base_mfn));
1749 /* Retry the validation with no active p.t. for this VCPU. */
1750 okay = get_page_and_type_from_pagenr(mfn, PGT_root_page_table, d);
1751 if ( !okay )
1753 /* Failure here is unrecoverable: the VCPU has no pagetable! */
1754 MEM_LOG("Fatal error while installing new baseptr %lx", mfn);
1755 domain_crash(d);
1756 ASSERT(v->processor == smp_processor_id());
1757 this_cpu(percpu_mm_info).deferred_ops = 0;
1758 return 0;
1763 invalidate_shadow_ldt(v);
1765 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1767 v->arch.guest_table = pagetable_from_pfn(mfn);
1768 update_cr3(v); /* update shadow_table and cr3 fields of vcpu struct */
1770 write_ptbase(v);
1772 if ( likely(old_base_mfn != 0) )
1774 if ( shadow_mode_refcounts(d) )
1775 put_page(mfn_to_page(old_base_mfn));
1776 else
1777 put_page_and_type(mfn_to_page(old_base_mfn));
1780 return 1;
1783 static void process_deferred_ops(void)
1785 unsigned int deferred_ops;
1786 struct domain *d = current->domain;
1787 struct percpu_mm_info *info = &this_cpu(percpu_mm_info);
1789 deferred_ops = info->deferred_ops;
1790 info->deferred_ops = 0;
1792 if ( deferred_ops & (DOP_FLUSH_ALL_TLBS|DOP_FLUSH_TLB) )
1794 if ( deferred_ops & DOP_FLUSH_ALL_TLBS )
1795 flush_tlb_mask(d->domain_dirty_cpumask);
1796 else
1797 local_flush_tlb();
1800 if ( deferred_ops & DOP_RELOAD_LDT )
1801 (void)map_ldt_shadow_page(0);
1803 if ( unlikely(info->foreign != NULL) )
1805 put_domain(info->foreign);
1806 info->foreign = NULL;
1810 static int set_foreigndom(domid_t domid)
1812 struct domain *e, *d = current->domain;
1813 struct percpu_mm_info *info = &this_cpu(percpu_mm_info);
1814 int okay = 1;
1816 ASSERT(info->foreign == NULL);
1818 if ( likely(domid == DOMID_SELF) )
1819 goto out;
1821 if ( unlikely(domid == d->domain_id) )
1823 MEM_LOG("Dom %u tried to specify itself as foreign domain",
1824 d->domain_id);
1825 okay = 0;
1827 else if ( unlikely(shadow_mode_translate(d)) )
1829 MEM_LOG("Cannot mix foreign mappings with translated domains");
1830 okay = 0;
1832 else if ( !IS_PRIV(d) )
1834 switch ( domid )
1836 case DOMID_IO:
1837 get_knownalive_domain(dom_io);
1838 info->foreign = dom_io;
1839 break;
1840 default:
1841 MEM_LOG("Dom %u cannot set foreign dom", d->domain_id);
1842 okay = 0;
1843 break;
1846 else
1848 info->foreign = e = find_domain_by_id(domid);
1849 if ( e == NULL )
1851 switch ( domid )
1853 case DOMID_XEN:
1854 get_knownalive_domain(dom_xen);
1855 info->foreign = dom_xen;
1856 break;
1857 case DOMID_IO:
1858 get_knownalive_domain(dom_io);
1859 info->foreign = dom_io;
1860 break;
1861 default:
1862 MEM_LOG("Unknown domain '%u'", domid);
1863 okay = 0;
1864 break;
1869 out:
1870 return okay;
1873 static inline cpumask_t vcpumask_to_pcpumask(
1874 struct domain *d, unsigned long vmask)
1876 unsigned int vcpu_id;
1877 cpumask_t pmask = CPU_MASK_NONE;
1878 struct vcpu *v;
1880 while ( vmask != 0 )
1882 vcpu_id = find_first_set_bit(vmask);
1883 vmask &= ~(1UL << vcpu_id);
1884 if ( (vcpu_id < MAX_VIRT_CPUS) &&
1885 ((v = d->vcpu[vcpu_id]) != NULL) )
1886 cpus_or(pmask, pmask, v->vcpu_dirty_cpumask);
1889 return pmask;
1892 int do_mmuext_op(
1893 XEN_GUEST_HANDLE(mmuext_op_t) uops,
1894 unsigned int count,
1895 XEN_GUEST_HANDLE(uint) pdone,
1896 unsigned int foreigndom)
1898 struct mmuext_op op;
1899 int rc = 0, i = 0, okay;
1900 unsigned long mfn = 0, gmfn = 0, type;
1901 unsigned int done = 0;
1902 struct page_info *page;
1903 struct vcpu *v = current;
1904 struct domain *d = v->domain;
1906 LOCK_BIGLOCK(d);
1908 if ( unlikely(count & MMU_UPDATE_PREEMPTED) )
1910 count &= ~MMU_UPDATE_PREEMPTED;
1911 if ( unlikely(!guest_handle_is_null(pdone)) )
1912 (void)copy_from_guest(&done, pdone, 1);
1915 if ( !set_foreigndom(foreigndom) )
1917 rc = -ESRCH;
1918 goto out;
1921 if ( unlikely(!guest_handle_okay(uops, count)) )
1923 rc = -EFAULT;
1924 goto out;
1927 for ( i = 0; i < count; i++ )
1929 if ( hypercall_preempt_check() )
1931 rc = hypercall_create_continuation(
1932 __HYPERVISOR_mmuext_op, "hihi",
1933 uops, (count - i) | MMU_UPDATE_PREEMPTED, pdone, foreigndom);
1934 break;
1937 if ( unlikely(__copy_from_guest(&op, uops, 1) != 0) )
1939 MEM_LOG("Bad __copy_from_guest");
1940 rc = -EFAULT;
1941 break;
1944 okay = 1;
1945 gmfn = op.arg1.mfn;
1946 mfn = gmfn_to_mfn(FOREIGNDOM, gmfn);
1947 page = mfn_to_page(mfn);
1949 switch ( op.cmd )
1951 case MMUEXT_PIN_L1_TABLE:
1952 type = PGT_l1_page_table;
1953 goto pin_page;
1955 case MMUEXT_PIN_L2_TABLE:
1956 type = PGT_l2_page_table;
1957 goto pin_page;
1959 case MMUEXT_PIN_L3_TABLE:
1960 type = PGT_l3_page_table;
1961 goto pin_page;
1963 case MMUEXT_PIN_L4_TABLE:
1964 type = PGT_l4_page_table;
1966 pin_page:
1967 /* Ignore pinning of invalid paging levels. */
1968 if ( (op.cmd - MMUEXT_PIN_L1_TABLE) > (CONFIG_PAGING_LEVELS - 1) )
1969 break;
1971 if ( shadow_mode_refcounts(FOREIGNDOM) )
1972 break;
1974 okay = get_page_and_type_from_pagenr(mfn, type, FOREIGNDOM);
1975 if ( unlikely(!okay) )
1977 MEM_LOG("Error while pinning mfn %lx", mfn);
1978 break;
1981 if ( unlikely(test_and_set_bit(_PGT_pinned,
1982 &page->u.inuse.type_info)) )
1984 MEM_LOG("Mfn %lx already pinned", mfn);
1985 put_page_and_type(page);
1986 okay = 0;
1987 break;
1990 /* A page is dirtied when its pin status is set. */
1991 mark_dirty(d, mfn);
1993 break;
1995 case MMUEXT_UNPIN_TABLE:
1996 if ( shadow_mode_refcounts(d) )
1997 break;
1999 if ( unlikely(!(okay = get_page_from_pagenr(mfn, d))) )
2001 MEM_LOG("Mfn %lx bad domain (dom=%p)",
2002 mfn, page_get_owner(page));
2004 else if ( likely(test_and_clear_bit(_PGT_pinned,
2005 &page->u.inuse.type_info)) )
2007 put_page_and_type(page);
2008 put_page(page);
2009 /* A page is dirtied when its pin status is cleared. */
2010 mark_dirty(d, mfn);
2012 else
2014 okay = 0;
2015 put_page(page);
2016 MEM_LOG("Mfn %lx not pinned", mfn);
2018 break;
2020 case MMUEXT_NEW_BASEPTR:
2021 okay = new_guest_cr3(mfn);
2022 this_cpu(percpu_mm_info).deferred_ops &= ~DOP_FLUSH_TLB;
2023 break;
2025 #ifdef __x86_64__
2026 case MMUEXT_NEW_USER_BASEPTR:
2027 okay = 1;
2028 if (likely(mfn != 0))
2030 if ( shadow_mode_refcounts(d) )
2031 okay = get_page_from_pagenr(mfn, d);
2032 else
2033 okay = get_page_and_type_from_pagenr(
2034 mfn, PGT_root_page_table, d);
2036 if ( unlikely(!okay) )
2038 MEM_LOG("Error while installing new mfn %lx", mfn);
2040 else
2042 unsigned long old_mfn =
2043 pagetable_get_pfn(v->arch.guest_table_user);
2044 v->arch.guest_table_user = pagetable_from_pfn(mfn);
2045 if ( old_mfn != 0 )
2047 if ( shadow_mode_refcounts(d) )
2048 put_page(mfn_to_page(old_mfn));
2049 else
2050 put_page_and_type(mfn_to_page(old_mfn));
2053 break;
2054 #endif
2056 case MMUEXT_TLB_FLUSH_LOCAL:
2057 this_cpu(percpu_mm_info).deferred_ops |= DOP_FLUSH_TLB;
2058 break;
2060 case MMUEXT_INVLPG_LOCAL:
2061 if ( !shadow_mode_enabled(d)
2062 || shadow_invlpg(v, op.arg1.linear_addr) != 0 )
2063 local_flush_tlb_one(op.arg1.linear_addr);
2064 break;
2066 case MMUEXT_TLB_FLUSH_MULTI:
2067 case MMUEXT_INVLPG_MULTI:
2069 unsigned long vmask;
2070 cpumask_t pmask;
2071 if ( unlikely(copy_from_guest(&vmask, op.arg2.vcpumask, 1)) )
2073 okay = 0;
2074 break;
2076 pmask = vcpumask_to_pcpumask(d, vmask);
2077 if ( op.cmd == MMUEXT_TLB_FLUSH_MULTI )
2078 flush_tlb_mask(pmask);
2079 else
2080 flush_tlb_one_mask(pmask, op.arg1.linear_addr);
2081 break;
2084 case MMUEXT_TLB_FLUSH_ALL:
2085 flush_tlb_mask(d->domain_dirty_cpumask);
2086 break;
2088 case MMUEXT_INVLPG_ALL:
2089 flush_tlb_one_mask(d->domain_dirty_cpumask, op.arg1.linear_addr);
2090 break;
2092 case MMUEXT_FLUSH_CACHE:
2093 if ( unlikely(!cache_flush_permitted(d)) )
2095 MEM_LOG("Non-physdev domain tried to FLUSH_CACHE.");
2096 okay = 0;
2098 else
2100 wbinvd();
2102 break;
2104 case MMUEXT_SET_LDT:
2106 unsigned long ptr = op.arg1.linear_addr;
2107 unsigned long ents = op.arg2.nr_ents;
2109 if ( shadow_mode_external(d) )
2111 MEM_LOG("ignoring SET_LDT hypercall from external "
2112 "domain %u", d->domain_id);
2113 okay = 0;
2115 else if ( ((ptr & (PAGE_SIZE-1)) != 0) ||
2116 (ents > 8192) ||
2117 !array_access_ok(ptr, ents, LDT_ENTRY_SIZE) )
2119 okay = 0;
2120 MEM_LOG("Bad args to SET_LDT: ptr=%lx, ents=%lx", ptr, ents);
2122 else if ( (v->arch.guest_context.ldt_ents != ents) ||
2123 (v->arch.guest_context.ldt_base != ptr) )
2125 invalidate_shadow_ldt(v);
2126 v->arch.guest_context.ldt_base = ptr;
2127 v->arch.guest_context.ldt_ents = ents;
2128 load_LDT(v);
2129 this_cpu(percpu_mm_info).deferred_ops &= ~DOP_RELOAD_LDT;
2130 if ( ents != 0 )
2131 this_cpu(percpu_mm_info).deferred_ops |= DOP_RELOAD_LDT;
2133 break;
2136 default:
2137 MEM_LOG("Invalid extended pt command 0x%x", op.cmd);
2138 rc = -ENOSYS;
2139 okay = 0;
2140 break;
2143 if ( unlikely(!okay) )
2145 rc = rc ? rc : -EINVAL;
2146 break;
2149 guest_handle_add_offset(uops, 1);
2152 out:
2153 process_deferred_ops();
2155 /* Add incremental work we have done to the @done output parameter. */
2156 if ( unlikely(!guest_handle_is_null(pdone)) )
2158 done += i;
2159 copy_to_guest(pdone, &done, 1);
2162 UNLOCK_BIGLOCK(d);
2163 return rc;
2166 int do_mmu_update(
2167 XEN_GUEST_HANDLE(mmu_update_t) ureqs,
2168 unsigned int count,
2169 XEN_GUEST_HANDLE(uint) pdone,
2170 unsigned int foreigndom)
2172 struct mmu_update req;
2173 void *va;
2174 unsigned long gpfn, gmfn, mfn;
2175 struct page_info *page;
2176 int rc = 0, okay = 1, i = 0;
2177 unsigned int cmd, done = 0;
2178 struct vcpu *v = current;
2179 struct domain *d = v->domain;
2180 unsigned long type_info;
2181 struct domain_mmap_cache mapcache, sh_mapcache;
2183 LOCK_BIGLOCK(d);
2185 if ( unlikely(count & MMU_UPDATE_PREEMPTED) )
2187 count &= ~MMU_UPDATE_PREEMPTED;
2188 if ( unlikely(!guest_handle_is_null(pdone)) )
2189 (void)copy_from_guest(&done, pdone, 1);
2192 domain_mmap_cache_init(&mapcache);
2193 domain_mmap_cache_init(&sh_mapcache);
2195 if ( !set_foreigndom(foreigndom) )
2197 rc = -ESRCH;
2198 goto out;
2201 perfc_incrc(calls_to_mmu_update);
2202 perfc_addc(num_page_updates, count);
2204 if ( unlikely(!guest_handle_okay(ureqs, count)) )
2206 rc = -EFAULT;
2207 goto out;
2210 for ( i = 0; i < count; i++ )
2212 if ( hypercall_preempt_check() )
2214 rc = hypercall_create_continuation(
2215 __HYPERVISOR_mmu_update, "hihi",
2216 ureqs, (count - i) | MMU_UPDATE_PREEMPTED, pdone, foreigndom);
2217 break;
2220 if ( unlikely(__copy_from_guest(&req, ureqs, 1) != 0) )
2222 MEM_LOG("Bad __copy_from_guest");
2223 rc = -EFAULT;
2224 break;
2227 cmd = req.ptr & (sizeof(l1_pgentry_t)-1);
2228 okay = 0;
2230 switch ( cmd )
2232 /*
2233 * MMU_NORMAL_PT_UPDATE: Normal update to any level of page table.
2234 */
2235 case MMU_NORMAL_PT_UPDATE:
2237 gmfn = req.ptr >> PAGE_SHIFT;
2238 mfn = gmfn_to_mfn(d, gmfn);
2240 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2242 MEM_LOG("Could not get page for normal update");
2243 break;
2246 va = map_domain_page_with_cache(mfn, &mapcache);
2247 va = (void *)((unsigned long)va +
2248 (unsigned long)(req.ptr & ~PAGE_MASK));
2249 page = mfn_to_page(mfn);
2251 switch ( (type_info = page->u.inuse.type_info) & PGT_type_mask )
2253 case PGT_l1_page_table:
2254 case PGT_l2_page_table:
2255 case PGT_l3_page_table:
2256 case PGT_l4_page_table:
2258 if ( shadow_mode_refcounts(d) )
2260 MEM_LOG("mmu update on shadow-refcounted domain!");
2261 break;
2264 if ( unlikely(!get_page_type(
2265 page, type_info & (PGT_type_mask|PGT_pae_xen_l2))) )
2266 goto not_a_pt;
2268 switch ( type_info & PGT_type_mask )
2270 case PGT_l1_page_table:
2272 l1_pgentry_t l1e = l1e_from_intpte(req.val);
2273 okay = mod_l1_entry(va, l1e, mfn);
2275 break;
2276 case PGT_l2_page_table:
2278 l2_pgentry_t l2e = l2e_from_intpte(req.val);
2279 okay = mod_l2_entry(
2280 (l2_pgentry_t *)va, l2e, mfn, type_info);
2282 break;
2283 #if CONFIG_PAGING_LEVELS >= 3
2284 case PGT_l3_page_table:
2286 l3_pgentry_t l3e = l3e_from_intpte(req.val);
2287 okay = mod_l3_entry(va, l3e, mfn);
2289 break;
2290 #endif
2291 #if CONFIG_PAGING_LEVELS >= 4
2292 case PGT_l4_page_table:
2294 l4_pgentry_t l4e = l4e_from_intpte(req.val);
2295 okay = mod_l4_entry(va, l4e, mfn);
2297 break;
2298 #endif
2301 put_page_type(page);
2303 break;
2305 default:
2306 not_a_pt:
2308 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
2309 break;
2311 if ( unlikely(shadow_mode_enabled(d)) )
2312 shadow_lock(d);
2314 *(intpte_t *)va = req.val;
2315 okay = 1;
2317 if ( unlikely(shadow_mode_enabled(d)) )
2319 shadow_validate_guest_entry(v, _mfn(mfn), va);
2320 shadow_unlock(d);
2323 put_page_type(page);
2325 break;
2328 unmap_domain_page_with_cache(va, &mapcache);
2330 put_page(page);
2331 break;
2333 case MMU_MACHPHYS_UPDATE:
2335 mfn = req.ptr >> PAGE_SHIFT;
2336 gpfn = req.val;
2338 if ( unlikely(!get_page_from_pagenr(mfn, FOREIGNDOM)) )
2340 MEM_LOG("Could not get page for mach->phys update");
2341 break;
2344 if ( unlikely(shadow_mode_translate(FOREIGNDOM)) )
2346 MEM_LOG("Mach-phys update on shadow-translate guest");
2347 break;
2350 set_gpfn_from_mfn(mfn, gpfn);
2351 okay = 1;
2353 mark_dirty(FOREIGNDOM, mfn);
2355 put_page(mfn_to_page(mfn));
2356 break;
2358 default:
2359 MEM_LOG("Invalid page update command %x", cmd);
2360 rc = -ENOSYS;
2361 okay = 0;
2362 break;
2365 if ( unlikely(!okay) )
2367 rc = rc ? rc : -EINVAL;
2368 break;
2371 guest_handle_add_offset(ureqs, 1);
2374 out:
2375 domain_mmap_cache_destroy(&mapcache);
2376 domain_mmap_cache_destroy(&sh_mapcache);
2378 process_deferred_ops();
2380 /* Add incremental work we have done to the @done output parameter. */
2381 if ( unlikely(!guest_handle_is_null(pdone)) )
2383 done += i;
2384 copy_to_guest(pdone, &done, 1);
2387 UNLOCK_BIGLOCK(d);
2388 return rc;
2392 static int create_grant_pte_mapping(
2393 uint64_t pte_addr, l1_pgentry_t nl1e, struct vcpu *v)
2395 int rc = GNTST_okay;
2396 void *va;
2397 unsigned long gmfn, mfn;
2398 struct page_info *page;
2399 u32 type;
2400 l1_pgentry_t ol1e;
2401 struct domain *d = v->domain;
2403 ASSERT(spin_is_locked(&d->big_lock));
2405 adjust_guest_l1e(nl1e);
2407 gmfn = pte_addr >> PAGE_SHIFT;
2408 mfn = gmfn_to_mfn(d, gmfn);
2410 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2412 MEM_LOG("Could not get page for normal update");
2413 return GNTST_general_error;
2416 va = map_domain_page(mfn);
2417 va = (void *)((unsigned long)va + ((unsigned long)pte_addr & ~PAGE_MASK));
2418 page = mfn_to_page(mfn);
2420 type = page->u.inuse.type_info & PGT_type_mask;
2421 if ( (type != PGT_l1_page_table) || !get_page_type(page, type) )
2423 MEM_LOG("Grant map attempted to update a non-L1 page");
2424 rc = GNTST_general_error;
2425 goto failed;
2428 ol1e = *(l1_pgentry_t *)va;
2429 if ( !update_l1e(va, ol1e, nl1e, mfn, v) )
2431 put_page_type(page);
2432 rc = GNTST_general_error;
2433 goto failed;
2436 if ( !shadow_mode_refcounts(d) )
2437 put_page_from_l1e(ol1e, d);
2439 put_page_type(page);
2441 failed:
2442 unmap_domain_page(va);
2443 put_page(page);
2445 return rc;
2448 static int destroy_grant_pte_mapping(
2449 uint64_t addr, unsigned long frame, struct domain *d)
2451 int rc = GNTST_okay;
2452 void *va;
2453 unsigned long gmfn, mfn;
2454 struct page_info *page;
2455 u32 type;
2456 l1_pgentry_t ol1e;
2458 gmfn = addr >> PAGE_SHIFT;
2459 mfn = gmfn_to_mfn(d, gmfn);
2461 if ( unlikely(!get_page_from_pagenr(mfn, current->domain)) )
2463 MEM_LOG("Could not get page for normal update");
2464 return GNTST_general_error;
2467 va = map_domain_page(mfn);
2468 va = (void *)((unsigned long)va + ((unsigned long)addr & ~PAGE_MASK));
2469 page = mfn_to_page(mfn);
2471 type = page->u.inuse.type_info & PGT_type_mask;
2472 if ( (type != PGT_l1_page_table) || !get_page_type(page, type) )
2474 MEM_LOG("Grant map attempted to update a non-L1 page");
2475 rc = GNTST_general_error;
2476 goto failed;
2479 if ( __copy_from_user(&ol1e, (l1_pgentry_t *)va, sizeof(ol1e)) )
2481 put_page_type(page);
2482 rc = GNTST_general_error;
2483 goto failed;
2486 /* Check that the virtual address supplied is actually mapped to frame. */
2487 if ( unlikely((l1e_get_intpte(ol1e) >> PAGE_SHIFT) != frame) )
2489 MEM_LOG("PTE entry %lx for address %"PRIx64" doesn't match frame %lx",
2490 (unsigned long)l1e_get_intpte(ol1e), addr, frame);
2491 put_page_type(page);
2492 rc = GNTST_general_error;
2493 goto failed;
2496 /* Delete pagetable entry. */
2497 if ( unlikely(!update_l1e(
2498 (l1_pgentry_t *)va, ol1e, l1e_empty(), mfn,
2499 d->vcpu[0] /* Change if we go to per-vcpu shadows. */)) )
2501 MEM_LOG("Cannot delete PTE entry at %p", va);
2502 put_page_type(page);
2503 rc = GNTST_general_error;
2504 goto failed;
2507 put_page_type(page);
2509 failed:
2510 unmap_domain_page(va);
2511 put_page(page);
2512 return rc;
2516 static int create_grant_va_mapping(
2517 unsigned long va, l1_pgentry_t nl1e, struct vcpu *v)
2519 l1_pgentry_t *pl1e, ol1e;
2520 struct domain *d = v->domain;
2521 unsigned long gl1mfn;
2522 int okay;
2524 ASSERT(spin_is_locked(&d->big_lock));
2526 adjust_guest_l1e(nl1e);
2528 pl1e = guest_map_l1e(v, va, &gl1mfn);
2529 if ( !pl1e )
2531 MEM_LOG("Could not find L1 PTE for address %lx", va);
2532 return GNTST_general_error;
2534 ol1e = *pl1e;
2535 okay = update_l1e(pl1e, ol1e, nl1e, gl1mfn, v);
2536 guest_unmap_l1e(v, pl1e);
2537 pl1e = NULL;
2539 if ( !okay )
2540 return GNTST_general_error;
2542 if ( !shadow_mode_refcounts(d) )
2543 put_page_from_l1e(ol1e, d);
2545 return GNTST_okay;
2548 static int destroy_grant_va_mapping(
2549 unsigned long addr, unsigned long frame, struct vcpu *v)
2551 l1_pgentry_t *pl1e, ol1e;
2552 unsigned long gl1mfn;
2553 int rc = 0;
2555 pl1e = guest_map_l1e(v, addr, &gl1mfn);
2556 if ( !pl1e )
2558 MEM_LOG("Could not find L1 PTE for address %lx", addr);
2559 return GNTST_general_error;
2561 ol1e = *pl1e;
2563 /* Check that the virtual address supplied is actually mapped to frame. */
2564 if ( unlikely(l1e_get_pfn(ol1e) != frame) )
2566 MEM_LOG("PTE entry %lx for address %lx doesn't match frame %lx",
2567 l1e_get_pfn(ol1e), addr, frame);
2568 rc = GNTST_general_error;
2569 goto out;
2572 /* Delete pagetable entry. */
2573 if ( unlikely(!update_l1e(pl1e, ol1e, l1e_empty(), gl1mfn, v)) )
2575 MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
2576 rc = GNTST_general_error;
2577 goto out;
2580 out:
2581 guest_unmap_l1e(v, pl1e);
2582 return rc;
2585 int create_grant_host_mapping(
2586 uint64_t addr, unsigned long frame, unsigned int flags)
2588 l1_pgentry_t pte = l1e_from_pfn(frame, GRANT_PTE_FLAGS);
2590 if ( (flags & GNTMAP_application_map) )
2591 l1e_add_flags(pte,_PAGE_USER);
2592 if ( !(flags & GNTMAP_readonly) )
2593 l1e_add_flags(pte,_PAGE_RW);
2595 if ( flags & GNTMAP_contains_pte )
2596 return create_grant_pte_mapping(addr, pte, current);
2597 return create_grant_va_mapping(addr, pte, current);
2600 int destroy_grant_host_mapping(
2601 uint64_t addr, unsigned long frame, unsigned int flags)
2603 if ( flags & GNTMAP_contains_pte )
2604 return destroy_grant_pte_mapping(addr, frame, current->domain);
2605 return destroy_grant_va_mapping(addr, frame, current);
2608 int steal_page(
2609 struct domain *d, struct page_info *page, unsigned int memflags)
2611 u32 _d, _nd, x, y;
2613 spin_lock(&d->page_alloc_lock);
2615 /*
2616 * The tricky bit: atomically release ownership while there is just one
2617 * benign reference to the page (PGC_allocated). If that reference
2618 * disappears then the deallocation routine will safely spin.
2619 */
2620 _d = pickle_domptr(d);
2621 _nd = page->u.inuse._domain;
2622 y = page->count_info;
2623 do {
2624 x = y;
2625 if (unlikely((x & (PGC_count_mask|PGC_allocated)) !=
2626 (1 | PGC_allocated)) || unlikely(_nd != _d)) {
2627 MEM_LOG("gnttab_transfer: Bad page %p: ed=%p(%u), sd=%p,"
2628 " caf=%08x, taf=%" PRtype_info "\n",
2629 (void *) page_to_mfn(page),
2630 d, d->domain_id, unpickle_domptr(_nd), x,
2631 page->u.inuse.type_info);
2632 spin_unlock(&d->page_alloc_lock);
2633 return -1;
2635 __asm__ __volatile__(
2636 LOCK_PREFIX "cmpxchg8b %2"
2637 : "=d" (_nd), "=a" (y),
2638 "=m" (*(volatile u64 *)(&page->count_info))
2639 : "0" (_d), "1" (x), "c" (NULL), "b" (x) );
2640 } while (unlikely(_nd != _d) || unlikely(y != x));
2642 /*
2643 * Unlink from 'd'. At least one reference remains (now anonymous), so
2644 * noone else is spinning to try to delete this page from 'd'.
2645 */
2646 if ( !(memflags & MEMF_no_refcount) )
2647 d->tot_pages--;
2648 list_del(&page->list);
2650 spin_unlock(&d->page_alloc_lock);
2652 return 0;
2655 int do_update_va_mapping(unsigned long va, u64 val64,
2656 unsigned long flags)
2658 l1_pgentry_t val = l1e_from_intpte(val64);
2659 struct vcpu *v = current;
2660 struct domain *d = v->domain;
2661 l1_pgentry_t *pl1e;
2662 unsigned long vmask, bmap_ptr, gl1mfn;
2663 cpumask_t pmask;
2664 int rc = 0;
2666 perfc_incrc(calls_to_update_va);
2668 if ( unlikely(!__addr_ok(va) && !shadow_mode_external(d)) )
2669 return -EINVAL;
2671 LOCK_BIGLOCK(d);
2673 pl1e = guest_map_l1e(v, va, &gl1mfn);
2675 if ( unlikely(!pl1e || !mod_l1_entry(pl1e, val, gl1mfn)) )
2676 rc = -EINVAL;
2678 if ( pl1e )
2679 guest_unmap_l1e(v, pl1e);
2680 pl1e = NULL;
2682 switch ( flags & UVMF_FLUSHTYPE_MASK )
2684 case UVMF_TLB_FLUSH:
2685 switch ( (bmap_ptr = flags & ~UVMF_FLUSHTYPE_MASK) )
2687 case UVMF_LOCAL:
2688 local_flush_tlb();
2689 break;
2690 case UVMF_ALL:
2691 flush_tlb_mask(d->domain_dirty_cpumask);
2692 break;
2693 default:
2694 if ( unlikely(get_user(vmask, (unsigned long *)bmap_ptr)) )
2695 rc = -EFAULT;
2696 pmask = vcpumask_to_pcpumask(d, vmask);
2697 flush_tlb_mask(pmask);
2698 break;
2700 break;
2702 case UVMF_INVLPG:
2703 switch ( (bmap_ptr = flags & ~UVMF_FLUSHTYPE_MASK) )
2705 case UVMF_LOCAL:
2706 if ( !shadow_mode_enabled(d)
2707 || (shadow_invlpg(current, va) != 0) )
2708 local_flush_tlb_one(va);
2709 break;
2710 case UVMF_ALL:
2711 flush_tlb_one_mask(d->domain_dirty_cpumask, va);
2712 break;
2713 default:
2714 if ( unlikely(get_user(vmask, (unsigned long *)bmap_ptr)) )
2715 rc = -EFAULT;
2716 pmask = vcpumask_to_pcpumask(d, vmask);
2717 flush_tlb_one_mask(pmask, va);
2718 break;
2720 break;
2723 process_deferred_ops();
2725 UNLOCK_BIGLOCK(d);
2727 return rc;
2730 int do_update_va_mapping_otherdomain(unsigned long va, u64 val64,
2731 unsigned long flags,
2732 domid_t domid)
2734 int rc;
2736 if ( unlikely(!IS_PRIV(current->domain)) )
2737 return -EPERM;
2739 if ( !set_foreigndom(domid) )
2740 return -ESRCH;
2742 rc = do_update_va_mapping(va, val64, flags);
2744 return rc;
2749 /*************************
2750 * Descriptor Tables
2751 */
2753 void destroy_gdt(struct vcpu *v)
2755 int i;
2756 unsigned long pfn;
2758 v->arch.guest_context.gdt_ents = 0;
2759 for ( i = 0; i < FIRST_RESERVED_GDT_PAGE; i++ )
2761 if ( (pfn = l1e_get_pfn(v->arch.perdomain_ptes[i])) != 0 )
2762 put_page_and_type(mfn_to_page(pfn));
2763 v->arch.perdomain_ptes[i] = l1e_empty();
2764 v->arch.guest_context.gdt_frames[i] = 0;
2769 long set_gdt(struct vcpu *v,
2770 unsigned long *frames,
2771 unsigned int entries)
2773 struct domain *d = v->domain;
2774 /* NB. There are 512 8-byte entries per GDT page. */
2775 int i, nr_pages = (entries + 511) / 512;
2776 unsigned long mfn;
2778 if ( entries > FIRST_RESERVED_GDT_ENTRY )
2779 return -EINVAL;
2781 /* Check the pages in the new GDT. */
2782 for ( i = 0; i < nr_pages; i++ ) {
2783 mfn = frames[i] = gmfn_to_mfn(d, frames[i]);
2784 if ( !mfn_valid(mfn) ||
2785 !get_page_and_type(mfn_to_page(mfn), d, PGT_gdt_page) )
2786 goto fail;
2789 /* Tear down the old GDT. */
2790 destroy_gdt(v);
2792 /* Install the new GDT. */
2793 v->arch.guest_context.gdt_ents = entries;
2794 for ( i = 0; i < nr_pages; i++ )
2796 v->arch.guest_context.gdt_frames[i] = frames[i];
2797 v->arch.perdomain_ptes[i] =
2798 l1e_from_pfn(frames[i], __PAGE_HYPERVISOR);
2801 return 0;
2803 fail:
2804 while ( i-- > 0 )
2805 put_page_and_type(mfn_to_page(frames[i]));
2806 return -EINVAL;
2810 long do_set_gdt(XEN_GUEST_HANDLE(ulong) frame_list, unsigned int entries)
2812 int nr_pages = (entries + 511) / 512;
2813 unsigned long frames[16];
2814 long ret;
2816 /* Rechecked in set_gdt, but ensures a sane limit for copy_from_user(). */
2817 if ( entries > FIRST_RESERVED_GDT_ENTRY )
2818 return -EINVAL;
2820 if ( copy_from_guest((unsigned long *)frames, frame_list, nr_pages) )
2821 return -EFAULT;
2823 LOCK_BIGLOCK(current->domain);
2825 if ( (ret = set_gdt(current, frames, entries)) == 0 )
2826 local_flush_tlb();
2828 UNLOCK_BIGLOCK(current->domain);
2830 return ret;
2834 long do_update_descriptor(u64 pa, u64 desc)
2836 struct domain *dom = current->domain;
2837 unsigned long gmfn = pa >> PAGE_SHIFT;
2838 unsigned long mfn;
2839 unsigned int offset;
2840 struct desc_struct *gdt_pent, d;
2841 struct page_info *page;
2842 long ret = -EINVAL;
2844 offset = ((unsigned int)pa & ~PAGE_MASK) / sizeof(struct desc_struct);
2846 *(u64 *)&d = desc;
2848 LOCK_BIGLOCK(dom);
2850 mfn = gmfn_to_mfn(dom, gmfn);
2851 if ( (((unsigned int)pa % sizeof(struct desc_struct)) != 0) ||
2852 !mfn_valid(mfn) ||
2853 !check_descriptor(&d) )
2855 UNLOCK_BIGLOCK(dom);
2856 return -EINVAL;
2859 page = mfn_to_page(mfn);
2860 if ( unlikely(!get_page(page, dom)) )
2862 UNLOCK_BIGLOCK(dom);
2863 return -EINVAL;
2866 /* Check if the given frame is in use in an unsafe context. */
2867 switch ( page->u.inuse.type_info & PGT_type_mask )
2869 case PGT_gdt_page:
2870 if ( unlikely(!get_page_type(page, PGT_gdt_page)) )
2871 goto out;
2872 break;
2873 case PGT_ldt_page:
2874 if ( unlikely(!get_page_type(page, PGT_ldt_page)) )
2875 goto out;
2876 break;
2877 default:
2878 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
2879 goto out;
2880 break;
2883 mark_dirty(dom, mfn);
2885 /* All is good so make the update. */
2886 gdt_pent = map_domain_page(mfn);
2887 memcpy(&gdt_pent[offset], &d, 8);
2888 unmap_domain_page(gdt_pent);
2890 put_page_type(page);
2892 ret = 0; /* success */
2894 out:
2895 put_page(page);
2897 UNLOCK_BIGLOCK(dom);
2899 return ret;
2902 typedef struct e820entry e820entry_t;
2903 DEFINE_XEN_GUEST_HANDLE(e820entry_t);
2905 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
2907 switch ( op )
2909 case XENMEM_add_to_physmap:
2911 struct xen_add_to_physmap xatp;
2912 unsigned long prev_mfn, mfn = 0, gpfn;
2913 struct domain *d;
2915 if ( copy_from_guest(&xatp, arg, 1) )
2916 return -EFAULT;
2918 if ( xatp.domid == DOMID_SELF )
2920 d = current->domain;
2921 get_knownalive_domain(d);
2923 else if ( !IS_PRIV(current->domain) )
2924 return -EPERM;
2925 else if ( (d = find_domain_by_id(xatp.domid)) == NULL )
2926 return -ESRCH;
2928 switch ( xatp.space )
2930 case XENMAPSPACE_shared_info:
2931 if ( xatp.idx == 0 )
2932 mfn = virt_to_mfn(d->shared_info);
2933 break;
2934 case XENMAPSPACE_grant_table:
2935 if ( xatp.idx < NR_GRANT_FRAMES )
2936 mfn = virt_to_mfn(d->grant_table->shared) + xatp.idx;
2937 break;
2938 default:
2939 break;
2942 if ( !shadow_mode_translate(d) || (mfn == 0) )
2944 put_domain(d);
2945 return -EINVAL;
2948 LOCK_BIGLOCK(d);
2950 /* Remove previously mapped page if it was present. */
2951 prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
2952 if ( mfn_valid(prev_mfn) )
2954 if ( IS_XEN_HEAP_FRAME(mfn_to_page(prev_mfn)) )
2955 /* Xen heap frames are simply unhooked from this phys slot. */
2956 guest_physmap_remove_page(d, xatp.gpfn, prev_mfn);
2957 else
2958 /* Normal domain memory is freed, to avoid leaking memory. */
2959 guest_remove_page(d, xatp.gpfn);
2962 /* Unmap from old location, if any. */
2963 gpfn = get_gpfn_from_mfn(mfn);
2964 if ( gpfn != INVALID_M2P_ENTRY )
2965 guest_physmap_remove_page(d, gpfn, mfn);
2967 /* Map at new location. */
2968 guest_physmap_add_page(d, xatp.gpfn, mfn);
2970 UNLOCK_BIGLOCK(d);
2972 put_domain(d);
2974 break;
2977 case XENMEM_memory_map:
2979 return -ENOSYS;
2982 case XENMEM_machine_memory_map:
2984 struct xen_memory_map memmap;
2985 XEN_GUEST_HANDLE(e820entry_t) buffer;
2986 int count;
2988 if ( !IS_PRIV(current->domain) )
2989 return -EINVAL;
2991 if ( copy_from_guest(&memmap, arg, 1) )
2992 return -EFAULT;
2993 if ( memmap.nr_entries < e820.nr_map + 1 )
2994 return -EINVAL;
2996 buffer = guest_handle_cast(memmap.buffer, e820entry_t);
2998 count = min((unsigned int)e820.nr_map, memmap.nr_entries);
2999 if ( copy_to_guest(buffer, &e820.map[0], count) < 0 )
3000 return -EFAULT;
3002 memmap.nr_entries = count;
3004 if ( copy_to_guest(arg, &memmap, 1) )
3005 return -EFAULT;
3007 return 0;
3010 case XENMEM_machphys_mapping:
3012 struct xen_machphys_mapping mapping = {
3013 .v_start = MACH2PHYS_VIRT_START,
3014 .v_end = MACH2PHYS_VIRT_END,
3015 .max_mfn = MACH2PHYS_NR_ENTRIES - 1
3016 };
3018 if ( copy_to_guest(arg, &mapping, 1) )
3019 return -EFAULT;
3021 return 0;
3024 default:
3025 return subarch_memory_op(op, arg);
3028 return 0;
3032 /*************************
3033 * Writable Pagetables
3034 */
3036 static int ptwr_emulated_update(
3037 unsigned long addr,
3038 paddr_t old,
3039 paddr_t val,
3040 unsigned int bytes,
3041 unsigned int do_cmpxchg)
3043 unsigned long gmfn, mfn;
3044 struct page_info *page;
3045 l1_pgentry_t pte, ol1e, nl1e, *pl1e;
3046 struct vcpu *v = current;
3047 struct domain *d = v->domain;
3049 /* Aligned access only, thank you. */
3050 if ( !access_ok(addr, bytes) || ((addr & (bytes-1)) != 0) )
3052 MEM_LOG("ptwr_emulate: Unaligned or bad size ptwr access (%d, %lx)",
3053 bytes, addr);
3054 return X86EMUL_UNHANDLEABLE;
3057 /* Turn a sub-word access into a full-word access. */
3058 if ( bytes != sizeof(paddr_t) )
3060 paddr_t full;
3061 unsigned int rc, offset = addr & (sizeof(paddr_t)-1);
3063 /* Align address; read full word. */
3064 addr &= ~(sizeof(paddr_t)-1);
3065 if ( (rc = copy_from_user(&full, (void *)addr, sizeof(paddr_t))) != 0 )
3067 propagate_page_fault(addr+sizeof(paddr_t)-rc, 0); /* read fault */
3068 return X86EMUL_PROPAGATE_FAULT;
3070 /* Mask out bits provided by caller. */
3071 full &= ~((((paddr_t)1 << (bytes*8)) - 1) << (offset*8));
3072 /* Shift the caller value and OR in the missing bits. */
3073 val &= (((paddr_t)1 << (bytes*8)) - 1);
3074 val <<= (offset)*8;
3075 val |= full;
3076 /* Also fill in missing parts of the cmpxchg old value. */
3077 old &= (((paddr_t)1 << (bytes*8)) - 1);
3078 old <<= (offset)*8;
3079 old |= full;
3082 /* Read the PTE that maps the page being updated. */
3083 guest_get_eff_l1e(v, addr, &pte);
3084 if ( unlikely(!(l1e_get_flags(pte) & _PAGE_PRESENT)) )
3086 MEM_LOG("%s: Cannot get L1 PTE for guest address %lx",
3087 __func__, addr);
3088 return X86EMUL_UNHANDLEABLE;
3091 gmfn = l1e_get_pfn(pte);
3092 mfn = gmfn_to_mfn(d, gmfn);
3093 page = mfn_to_page(mfn);
3095 /* We are looking only for read-only mappings of p.t. pages. */
3096 ASSERT((l1e_get_flags(pte) & (_PAGE_RW|_PAGE_PRESENT)) == _PAGE_PRESENT);
3097 ASSERT((page->u.inuse.type_info & PGT_type_mask) == PGT_l1_page_table);
3098 ASSERT((page->u.inuse.type_info & PGT_count_mask) != 0);
3099 ASSERT(page_get_owner(page) == d);
3101 /* Check the new PTE. */
3102 nl1e = l1e_from_intpte(val);
3103 if ( unlikely(!get_page_from_l1e(gl1e_to_ml1e(d, nl1e), d)) )
3105 if ( (CONFIG_PAGING_LEVELS == 3) &&
3106 (bytes == 4) &&
3107 !do_cmpxchg &&
3108 (l1e_get_flags(nl1e) & _PAGE_PRESENT) )
3110 /*
3111 * If this is a half-write to a PAE PTE then we assume that the
3112 * guest has simply got the two writes the wrong way round. We
3113 * zap the PRESENT bit on the assumption the bottom half will be
3114 * written immediately after we return to the guest.
3115 */
3116 MEM_LOG("ptwr_emulate: fixing up invalid PAE PTE %"PRIpte,
3117 l1e_get_intpte(nl1e));
3118 l1e_remove_flags(nl1e, _PAGE_PRESENT);
3120 else
3122 MEM_LOG("ptwr_emulate: could not get_page_from_l1e()");
3123 return X86EMUL_UNHANDLEABLE;
3127 adjust_guest_l1e(nl1e);
3129 /* Checked successfully: do the update (write or cmpxchg). */
3130 pl1e = map_domain_page(page_to_mfn(page));
3131 pl1e = (l1_pgentry_t *)((unsigned long)pl1e + (addr & ~PAGE_MASK));
3132 if ( do_cmpxchg )
3134 if ( shadow_mode_enabled(d) )
3135 shadow_lock(d);
3136 ol1e = l1e_from_intpte(old);
3137 if ( cmpxchg((intpte_t *)pl1e, old, val) != old )
3139 if ( shadow_mode_enabled(d) )
3140 shadow_unlock(d);
3141 unmap_domain_page(pl1e);
3142 put_page_from_l1e(gl1e_to_ml1e(d, nl1e), d);
3143 return X86EMUL_CMPXCHG_FAILED;
3145 if ( unlikely(shadow_mode_enabled(d)) )
3147 shadow_validate_guest_entry(v, _mfn(page_to_mfn(page)), pl1e);
3148 shadow_unlock(d);
3151 else
3153 ol1e = *pl1e;
3154 if ( !update_l1e(pl1e, ol1e, nl1e, page_to_mfn(page), v) )
3155 BUG();
3158 unmap_domain_page(pl1e);
3160 /* Finally, drop the old PTE. */
3161 put_page_from_l1e(gl1e_to_ml1e(d, ol1e), d);
3163 return X86EMUL_CONTINUE;
3166 static int ptwr_emulated_write(
3167 unsigned long addr,
3168 unsigned long val,
3169 unsigned int bytes,
3170 struct x86_emulate_ctxt *ctxt)
3172 return ptwr_emulated_update(addr, 0, val, bytes, 0);
3175 static int ptwr_emulated_cmpxchg(
3176 unsigned long addr,
3177 unsigned long old,
3178 unsigned long new,
3179 unsigned int bytes,
3180 struct x86_emulate_ctxt *ctxt)
3182 return ptwr_emulated_update(addr, old, new, bytes, 1);
3185 static int ptwr_emulated_cmpxchg8b(
3186 unsigned long addr,
3187 unsigned long old,
3188 unsigned long old_hi,
3189 unsigned long new,
3190 unsigned long new_hi,
3191 struct x86_emulate_ctxt *ctxt)
3193 if ( CONFIG_PAGING_LEVELS == 2 )
3194 return X86EMUL_UNHANDLEABLE;
3195 else
3196 return ptwr_emulated_update(
3197 addr, ((u64)old_hi << 32) | old, ((u64)new_hi << 32) | new, 8, 1);
3200 static struct x86_emulate_ops ptwr_emulate_ops = {
3201 .read_std = x86_emulate_read_std,
3202 .write_std = x86_emulate_write_std,
3203 .read_emulated = x86_emulate_read_std,
3204 .write_emulated = ptwr_emulated_write,
3205 .cmpxchg_emulated = ptwr_emulated_cmpxchg,
3206 .cmpxchg8b_emulated = ptwr_emulated_cmpxchg8b
3207 };
3209 /* Write page fault handler: check if guest is trying to modify a PTE. */
3210 int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
3211 struct cpu_user_regs *regs)
3213 struct domain *d = v->domain;
3214 unsigned long pfn;
3215 struct page_info *page;
3216 l1_pgentry_t pte;
3217 struct x86_emulate_ctxt emul_ctxt;
3219 LOCK_BIGLOCK(d);
3221 /*
3222 * Attempt to read the PTE that maps the VA being accessed. By checking for
3223 * PDE validity in the L2 we avoid many expensive fixups in __get_user().
3224 */
3225 guest_get_eff_l1e(v, addr, &pte);
3226 if ( !(l1e_get_flags(pte) & _PAGE_PRESENT) )
3227 goto bail;
3228 pfn = l1e_get_pfn(pte);
3229 page = mfn_to_page(pfn);
3231 /* We are looking only for read-only mappings of p.t. pages. */
3232 if ( ((l1e_get_flags(pte) & (_PAGE_PRESENT|_PAGE_RW)) != _PAGE_PRESENT) ||
3233 ((page->u.inuse.type_info & PGT_type_mask) != PGT_l1_page_table) ||
3234 ((page->u.inuse.type_info & PGT_count_mask) == 0) ||
3235 (page_get_owner(page) != d) )
3236 goto bail;
3238 emul_ctxt.regs = guest_cpu_user_regs();
3239 emul_ctxt.cr2 = addr;
3240 emul_ctxt.mode = X86EMUL_MODE_HOST;
3241 if ( x86_emulate_memop(&emul_ctxt, &ptwr_emulate_ops) )
3242 goto bail;
3244 UNLOCK_BIGLOCK(d);
3245 perfc_incrc(ptwr_emulations);
3246 return EXCRET_fault_fixed;
3248 bail:
3249 UNLOCK_BIGLOCK(d);
3250 return 0;
3253 int map_pages_to_xen(
3254 unsigned long virt,
3255 unsigned long mfn,
3256 unsigned long nr_mfns,
3257 unsigned long flags)
3259 l2_pgentry_t *pl2e, ol2e;
3260 l1_pgentry_t *pl1e, ol1e;
3261 unsigned int i;
3263 unsigned int map_small_pages = !!(flags & MAP_SMALL_PAGES);
3264 flags &= ~MAP_SMALL_PAGES;
3266 while ( nr_mfns != 0 )
3268 pl2e = virt_to_xen_l2e(virt);
3270 if ( ((((virt>>PAGE_SHIFT) | mfn) & ((1<<PAGETABLE_ORDER)-1)) == 0) &&
3271 (nr_mfns >= (1<<PAGETABLE_ORDER)) &&
3272 !map_small_pages )
3274 /* Super-page mapping. */
3275 ol2e = *pl2e;
3276 *pl2e = l2e_from_pfn(mfn, flags|_PAGE_PSE);
3278 if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) )
3280 local_flush_tlb_pge();
3281 if ( !(l2e_get_flags(ol2e) & _PAGE_PSE) )
3282 free_xen_pagetable(l2e_get_page(ol2e));
3285 virt += 1UL << L2_PAGETABLE_SHIFT;
3286 mfn += 1UL << PAGETABLE_ORDER;
3287 nr_mfns -= 1UL << PAGETABLE_ORDER;
3289 else
3291 /* Normal page mapping. */
3292 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
3294 pl1e = page_to_virt(alloc_xen_pagetable());
3295 clear_page(pl1e);
3296 *pl2e = l2e_from_page(virt_to_page(pl1e), __PAGE_HYPERVISOR);
3298 else if ( l2e_get_flags(*pl2e) & _PAGE_PSE )
3300 pl1e = page_to_virt(alloc_xen_pagetable());
3301 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
3302 pl1e[i] = l1e_from_pfn(
3303 l2e_get_pfn(*pl2e) + i,
3304 l2e_get_flags(*pl2e) & ~_PAGE_PSE);
3305 *pl2e = l2e_from_page(virt_to_page(pl1e), __PAGE_HYPERVISOR);
3306 local_flush_tlb_pge();
3309 pl1e = l2e_to_l1e(*pl2e) + l1_table_offset(virt);
3310 ol1e = *pl1e;
3311 *pl1e = l1e_from_pfn(mfn, flags);
3312 if ( (l1e_get_flags(ol1e) & _PAGE_PRESENT) )
3313 local_flush_tlb_one(virt);
3315 virt += 1UL << L1_PAGETABLE_SHIFT;
3316 mfn += 1UL;
3317 nr_mfns -= 1UL;
3321 return 0;
3324 void __set_fixmap(
3325 enum fixed_addresses idx, unsigned long mfn, unsigned long flags)
3327 BUG_ON(idx >= __end_of_fixed_addresses);
3328 map_pages_to_xen(fix_to_virt(idx), mfn, 1, flags);
3331 #ifdef MEMORY_GUARD
3333 void memguard_init(void)
3335 map_pages_to_xen(
3336 PAGE_OFFSET, 0, xenheap_phys_end >> PAGE_SHIFT,
3337 __PAGE_HYPERVISOR|MAP_SMALL_PAGES);
3340 static void __memguard_change_range(void *p, unsigned long l, int guard)
3342 unsigned long _p = (unsigned long)p;
3343 unsigned long _l = (unsigned long)l;
3344 unsigned long flags = __PAGE_HYPERVISOR | MAP_SMALL_PAGES;
3346 /* Ensure we are dealing with a page-aligned whole number of pages. */
3347 ASSERT((_p&PAGE_MASK) != 0);
3348 ASSERT((_l&PAGE_MASK) != 0);
3349 ASSERT((_p&~PAGE_MASK) == 0);
3350 ASSERT((_l&~PAGE_MASK) == 0);
3352 if ( guard )
3353 flags &= ~_PAGE_PRESENT;
3355 map_pages_to_xen(
3356 _p, virt_to_maddr(p) >> PAGE_SHIFT, _l >> PAGE_SHIFT, flags);
3359 void memguard_guard_range(void *p, unsigned long l)
3361 __memguard_change_range(p, l, 1);
3364 void memguard_unguard_range(void *p, unsigned long l)
3366 __memguard_change_range(p, l, 0);
3369 #endif
3371 void memguard_guard_stack(void *p)
3373 BUILD_BUG_ON((DEBUG_STACK_SIZE + PAGE_SIZE) > STACK_SIZE);
3374 p = (void *)((unsigned long)p + STACK_SIZE - DEBUG_STACK_SIZE - PAGE_SIZE);
3375 memguard_guard_range(p, PAGE_SIZE);
3378 /*
3379 * Local variables:
3380 * mode: C
3381 * c-set-style: "BSD"
3382 * c-basic-offset: 4
3383 * tab-width: 4
3384 * indent-tabs-mode: nil
3385 * End:
3386 */