direct-io.hg

view xen/arch/ia64/xen/mm.c @ 13460:4ef56c7cce12

[IA64] Fix dom0vp_expose_p2m

dom0vp_expose_p2m() assumes that memory is populated non-sparsely.
However with dom0 builder modification this assumption is not always true.
Make dom0vp_expose_p2m() allow sparsely populated memory.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author awilliam@xenbuild2.aw
date Thu Jan 04 16:22:31 2007 -0700 (2007-01-04)
parents d9b2dd57fdc4
children 9cbb1d6fb34a
line source
1 /*
2 * Copyright (C) 2005 Intel Co
3 * Kun Tian (Kevin Tian) <kevin.tian@intel.com>
4 *
5 * 05/04/29 Kun Tian (Kevin Tian) <kevin.tian@intel.com> Add VTI domain support
6 *
7 * Copyright (c) 2006 Isaku Yamahata <yamahata at valinux co jp>
8 * VA Linux Systems Japan K.K.
9 * dom0 vp model support
10 */
12 /*
13 * NOTES on SMP
14 *
15 * * shared structures
16 * There are some structures which are accessed by CPUs concurrently.
17 * Here is the list of shared structures and operations on them which
18 * read/write the structures.
19 *
20 * - struct page_info
21 * This is a xen global resource. This structure is accessed by
22 * any CPUs.
23 *
24 * operations on this structure:
25 * - get_page() and its variant
26 * - put_page() and its variant
27 *
28 * - vTLB
29 * vcpu->arch.{d, i}tlb: Software tlb cache. These are per VCPU data.
30 * DEFINE_PER_CPU (unsigned long, vhpt_paddr): VHPT table per physical CPU.
31 *
32 * domain_flush_vtlb_range() and domain_flush_vtlb_all()
33 * write vcpu->arch.{d, i}tlb and VHPT table of vcpu which isn't current.
34 * So there are potential races to read/write VHPT and vcpu->arch.{d, i}tlb.
35 * Please note that reading VHPT is done by hardware page table walker.
36 *
37 * operations on this structure:
38 * - global tlb purge
39 * vcpu_ptc_g(), vcpu_ptc_ga() and domain_page_flush_and_put()
40 * I.e. callers of domain_flush_vtlb_range() and domain_flush_vtlb_all()
41 * These functions invalidate VHPT entry and vcpu->arch.{i, d}tlb
42 *
43 * - tlb insert and fc
44 * vcpu_itc_i()
45 * vcpu_itc_d()
46 * ia64_do_page_fault()
47 * vcpu_fc()
48 * These functions set VHPT entry and vcpu->arch.{i, d}tlb.
49 * Actually vcpu_itc_no_srlz() does.
50 *
51 * - the P2M table
52 * domain->mm and pgd, pud, pmd, pte table page.
53 * This structure is used to convert domain pseudo physical address
54 * to machine address. This is per domain resource.
55 *
56 * operations on this structure:
57 * - populate the P2M table tree
58 * lookup_alloc_domain_pte() and its variants.
59 * - set p2m entry
60 * assign_new_domain_page() and its variants.
61 * assign_domain_page() and its variants.
62 * - xchg p2m entry
63 * assign_domain_page_replace()
64 * - cmpxchg p2m entry
65 * assign_domain_page_cmpxchg_rel()
66 * destroy_grant_host_mapping()
67 * steal_page()
68 * zap_domain_page_one()
69 * - read p2m entry
70 * lookup_alloc_domain_pte() and its variants.
71 *
72 * - the M2P table
73 * mpt_table (or machine_to_phys_mapping)
74 * This is a table which converts from machine address to pseudo physical
75 * address. This is a global structure.
76 *
77 * operations on this structure:
78 * - set m2p entry
79 * set_gpfn_from_mfn()
80 * - zap m2p entry
81 * set_gpfn_from_mfn(INVALID_P2M_ENTRY)
82 * - get m2p entry
83 * get_gpfn_from_mfn()
84 *
85 *
86 * * avoiding races
87 * The resources which are shared by CPUs must be accessed carefully
88 * to avoid race.
89 * IA64 has weak memory ordering so that attention must be paid
90 * to access shared structures. [SDM vol2 PartII chap. 2]
91 *
92 * - struct page_info memory ordering
93 * get_page() has acquire semantics.
94 * put_page() has release semantics.
95 *
96 * - populating the p2m table
97 * pgd, pud, pmd are append only.
98 *
99 * - races when updating the P2M tables and the M2P table
100 * The P2M entry are shared by more than one vcpu.
101 * So they are accessed atomic operations.
102 * I.e. xchg or cmpxchg must be used to update the p2m entry.
103 * NOTE: When creating/destructing a domain, we don't need to take care of
104 * this race.
105 *
106 * The M2P table is inverse of the P2M table.
107 * I.e. P2M(M2P(p)) = p and M2P(P2M(m)) = m
108 * The M2P table and P2M table must be updated consistently.
109 * Here is the update sequence
110 *
111 * xchg or cmpxchg case
112 * - set_gpfn_from_mfn(new_mfn, gpfn)
113 * - memory barrier
114 * - atomic update of the p2m entry (xchg or cmpxchg the p2m entry)
115 * get old_mfn entry as a result.
116 * - memory barrier
117 * - set_gpfn_from_mfn(old_mfn, INVALID_P2M_ENTRY)
118 *
119 * Here memory barrier can be achieved by release semantics.
120 *
121 * - races between global tlb purge and tlb insert
122 * This is a race between reading/writing vcpu->arch.{d, i}tlb or VHPT entry.
123 * When a vcpu is about to insert tlb, another vcpu may purge tlb
124 * cache globally. Inserting tlb (vcpu_itc_no_srlz()) or global tlb purge
125 * (domain_flush_vtlb_range() and domain_flush_vtlb_all()) can't update
126 * cpu->arch.{d, i}tlb, VHPT and mTLB. So there is a race here.
127 *
128 * Here check vcpu->arch.{d, i}tlb.p bit
129 * After inserting tlb entry, check the p bit and retry to insert.
130 * This means that when global tlb purge and tlb insert are issued
131 * simultaneously, always global tlb purge happens after tlb insert.
132 *
133 * - races between p2m entry update and tlb insert
134 * This is a race between reading/writing the p2m entry.
135 * reader: vcpu_itc_i(), vcpu_itc_d(), ia64_do_page_fault(), vcpu_fc()
136 * writer: assign_domain_page_cmpxchg_rel(), destroy_grant_host_mapping(),
137 * steal_page(), zap_domain_page_one()
138 *
139 * For example, vcpu_itc_i() is about to insert tlb by calling
140 * vcpu_itc_no_srlz() after reading the p2m entry.
141 * At the same time, the p2m entry is replaced by xchg or cmpxchg and
142 * tlb cache of the page is flushed.
143 * There is a possibility that the p2m entry doesn't already point to the
144 * old page, but tlb cache still points to the old page.
145 * This can be detected similar to sequence lock using the p2m entry itself.
146 * reader remember the read value of the p2m entry, and insert tlb.
147 * Then read the p2m entry again. If the new p2m entry value is different
148 * from the used p2m entry value, the retry.
149 *
150 * - races between referencing page and p2m entry update
151 * This is a race between reading/writing the p2m entry.
152 * reader: vcpu_get_domain_bundle(), vmx_get_domain_bundle(),
153 * efi_emulate_get_time()
154 * writer: assign_domain_page_cmpxchg_rel(), destroy_grant_host_mapping(),
155 * steal_page(), zap_domain_page_one()
156 *
157 * A page which assigned to a domain can be de-assigned by another vcpu.
158 * So before read/write to a domain page, the page's reference count
159 * must be incremented.
160 * vcpu_get_domain_bundle(), vmx_get_domain_bundle() and
161 * efi_emulate_get_time()
162 *
163 */
165 #include <xen/config.h>
166 #include <xen/sched.h>
167 #include <xen/domain.h>
168 #include <asm/xentypes.h>
169 #include <xen/mm.h>
170 #include <xen/errno.h>
171 #include <asm/pgalloc.h>
172 #include <asm/vhpt.h>
173 #include <asm/vcpu.h>
174 #include <asm/shadow.h>
175 #include <asm/p2m_entry.h>
176 #include <asm/tlb_track.h>
177 #include <linux/efi.h>
178 #include <xen/guest_access.h>
179 #include <asm/page.h>
180 #include <public/memory.h>
182 static void domain_page_flush_and_put(struct domain* d, unsigned long mpaddr,
183 volatile pte_t* ptep, pte_t old_pte,
184 struct page_info* page);
186 extern unsigned long ia64_iobase;
188 static struct domain *dom_xen, *dom_io;
190 // followings are stolen from arch_init_memory() @ xen/arch/x86/mm.c
191 void
192 alloc_dom_xen_and_dom_io(void)
193 {
194 /*
195 * Initialise our DOMID_XEN domain.
196 * Any Xen-heap pages that we will allow to be mapped will have
197 * their domain field set to dom_xen.
198 */
199 dom_xen = alloc_domain(DOMID_XEN);
200 BUG_ON(dom_xen == NULL);
202 /*
203 * Initialise our DOMID_IO domain.
204 * This domain owns I/O pages that are within the range of the page_info
205 * array. Mappings occur at the priv of the caller.
206 */
207 dom_io = alloc_domain(DOMID_IO);
208 BUG_ON(dom_io == NULL);
209 }
211 // heavily depends on the struct page_info layout.
212 // if (page_get_owner(page) == d &&
213 // test_and_clear_bit(_PGC_allocated, &page->count_info)) {
214 // put_page(page);
215 // }
216 static void
217 try_to_clear_PGC_allocate(struct domain* d, struct page_info* page)
218 {
219 u32 _d, _nd;
220 u64 x, nx, y;
222 _d = pickle_domptr(d);
223 y = *((u64*)&page->count_info);
224 do {
225 x = y;
226 _nd = x >> 32;
227 nx = x - 1;
228 __clear_bit(_PGC_allocated, &nx);
230 if (unlikely(!(x & PGC_allocated)) || unlikely(_nd != _d)) {
231 struct domain* nd = unpickle_domptr(_nd);
232 if (nd == NULL) {
233 gdprintk(XENLOG_INFO, "gnttab_transfer: "
234 "Bad page %p: ed=%p(%u) 0x%x, "
235 "sd=%p 0x%x,"
236 " caf=%016lx, taf=%" PRtype_info "\n",
237 (void *) page_to_mfn(page),
238 d, d->domain_id, _d,
239 nd, _nd,
240 x,
241 page->u.inuse.type_info);
242 }
243 break;
244 }
246 BUG_ON((nx & PGC_count_mask) < 1);
247 y = cmpxchg((u64*)&page->count_info, x, nx);
248 } while (unlikely(y != x));
249 }
251 static void
252 mm_teardown_pte(struct domain* d, volatile pte_t* pte, unsigned long offset)
253 {
254 pte_t old_pte;
255 unsigned long mfn;
256 struct page_info* page;
258 old_pte = ptep_get_and_clear(&d->arch.mm, offset, pte);// acquire semantics
260 // vmx domain use bit[58:56] to distinguish io region from memory.
261 // see vmx_build_physmap_table() in vmx_init.c
262 if (!pte_mem(old_pte))
263 return;
265 // domain might map IO space or acpi table pages. check it.
266 mfn = pte_pfn(old_pte);
267 if (!mfn_valid(mfn))
268 return;
269 page = mfn_to_page(mfn);
270 // struct page_info corresponding to mfn may exist or not depending
271 // on CONFIG_VIRTUAL_FRAME_TABLE.
272 // This check is too easy.
273 // The right way is to check whether this page is of io area or acpi pages
274 if (page_get_owner(page) == NULL) {
275 BUG_ON(page->count_info != 0);
276 return;
277 }
279 if (pte_pgc_allocated(old_pte)) {
280 BUG_ON(page_get_owner(page) != d);
281 BUG_ON(get_gpfn_from_mfn(mfn) == INVALID_M2P_ENTRY);
282 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
283 try_to_clear_PGC_allocate(d, page);
284 }
285 put_page(page);
286 }
288 static void
289 mm_teardown_pmd(struct domain* d, volatile pmd_t* pmd, unsigned long offset)
290 {
291 unsigned long i;
292 volatile pte_t* pte = pte_offset_map(pmd, offset);
294 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
295 if (!pte_present(*pte)) // acquire semantics
296 continue;
297 mm_teardown_pte(d, pte, offset + (i << PAGE_SHIFT));
298 }
299 }
301 static void
302 mm_teardown_pud(struct domain* d, volatile pud_t *pud, unsigned long offset)
303 {
304 unsigned long i;
305 volatile pmd_t *pmd = pmd_offset(pud, offset);
307 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
308 if (!pmd_present(*pmd)) // acquire semantics
309 continue;
310 mm_teardown_pmd(d, pmd, offset + (i << PMD_SHIFT));
311 }
312 }
314 static void
315 mm_teardown_pgd(struct domain* d, volatile pgd_t *pgd, unsigned long offset)
316 {
317 unsigned long i;
318 volatile pud_t *pud = pud_offset(pgd, offset);
320 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
321 if (!pud_present(*pud)) // acquire semantics
322 continue;
323 mm_teardown_pud(d, pud, offset + (i << PUD_SHIFT));
324 }
325 }
327 void
328 mm_teardown(struct domain* d)
329 {
330 struct mm_struct* mm = &d->arch.mm;
331 unsigned long i;
332 volatile pgd_t* pgd;
334 if (mm->pgd == NULL)
335 return;
337 pgd = pgd_offset(mm, 0);
338 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
339 if (!pgd_present(*pgd)) // acquire semantics
340 continue;
341 mm_teardown_pgd(d, pgd, i << PGDIR_SHIFT);
342 }
343 }
345 static void
346 mm_p2m_teardown_pmd(struct domain* d, volatile pmd_t* pmd,
347 unsigned long offset)
348 {
349 pte_free_kernel(pte_offset_map(pmd, offset));
350 }
352 static void
353 mm_p2m_teardown_pud(struct domain* d, volatile pud_t *pud,
354 unsigned long offset)
355 {
356 unsigned long i;
357 volatile pmd_t *pmd = pmd_offset(pud, offset);
359 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
360 if (!pmd_present(*pmd))
361 continue;
362 mm_p2m_teardown_pmd(d, pmd, offset + (i << PMD_SHIFT));
363 }
364 pmd_free(pmd_offset(pud, offset));
365 }
367 static void
368 mm_p2m_teardown_pgd(struct domain* d, volatile pgd_t *pgd,
369 unsigned long offset)
370 {
371 unsigned long i;
372 volatile pud_t *pud = pud_offset(pgd, offset);
374 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
375 if (!pud_present(*pud))
376 continue;
377 mm_p2m_teardown_pud(d, pud, offset + (i << PUD_SHIFT));
378 }
379 pud_free(pud_offset(pgd, offset));
380 }
382 static void
383 mm_p2m_teardown(struct domain* d)
384 {
385 struct mm_struct* mm = &d->arch.mm;
386 unsigned long i;
387 volatile pgd_t* pgd;
389 BUG_ON(mm->pgd == NULL);
390 pgd = pgd_offset(mm, 0);
391 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
392 if (!pgd_present(*pgd))
393 continue;
394 mm_p2m_teardown_pgd(d, pgd, i << PGDIR_SHIFT);
395 }
396 pgd_free(mm->pgd);
397 mm->pgd = NULL;
398 }
400 void
401 mm_final_teardown(struct domain* d)
402 {
403 if (d->arch.shadow_bitmap != NULL) {
404 xfree(d->arch.shadow_bitmap);
405 d->arch.shadow_bitmap = NULL;
406 }
407 mm_p2m_teardown(d);
408 }
410 // stolen from share_xen_page_with_guest() in xen/arch/x86/mm.c
411 void
412 share_xen_page_with_guest(struct page_info *page,
413 struct domain *d, int readonly)
414 {
415 if ( page_get_owner(page) == d )
416 return;
418 #if 1
419 if (readonly) {
420 printk("%s:%d readonly is not supported yet\n", __func__, __LINE__);
421 }
422 #endif
424 // alloc_xenheap_pages() doesn't initialize page owner.
425 //BUG_ON(page_get_owner(page) != NULL);
427 spin_lock(&d->page_alloc_lock);
429 #ifndef __ia64__
430 /* The incremented type count pins as writable or read-only. */
431 page->u.inuse.type_info = (readonly ? PGT_none : PGT_writable_page);
432 page->u.inuse.type_info |= PGT_validated | 1;
433 #endif
435 page_set_owner(page, d);
436 wmb(); /* install valid domain ptr before updating refcnt. */
437 ASSERT(page->count_info == 0);
438 page->count_info |= PGC_allocated | 1;
440 if ( unlikely(d->xenheap_pages++ == 0) )
441 get_knownalive_domain(d);
442 list_add_tail(&page->list, &d->xenpage_list);
444 // grant_table_destroy() releases these pages.
445 // but it doesn't clear their m2p entry. So there might remain stale
446 // entries. such a stale entry is cleared here.
447 set_gpfn_from_mfn(page_to_mfn(page), INVALID_M2P_ENTRY);
449 spin_unlock(&d->page_alloc_lock);
450 }
452 void
453 share_xen_page_with_privileged_guests(struct page_info *page, int readonly)
454 {
455 share_xen_page_with_guest(page, dom_xen, readonly);
456 }
458 unsigned long
459 gmfn_to_mfn_foreign(struct domain *d, unsigned long gpfn)
460 {
461 unsigned long pte;
463 pte = lookup_domain_mpa(d,gpfn << PAGE_SHIFT, NULL);
464 if (!pte) {
465 panic("gmfn_to_mfn_foreign: bad gpfn. spinning...\n");
466 }
467 return ((pte & _PFN_MASK) >> PAGE_SHIFT);
468 }
470 // given a domain virtual address, pte and pagesize, extract the metaphysical
471 // address, convert the pte for a physical address for (possibly different)
472 // Xen PAGE_SIZE and return modified pte. (NOTE: TLB insert should use
473 // PAGE_SIZE!)
474 u64 translate_domain_pte(u64 pteval, u64 address, u64 itir__, u64* logps,
475 struct p2m_entry* entry)
476 {
477 struct domain *d = current->domain;
478 ia64_itir_t itir = {.itir = itir__};
479 u64 mask, mpaddr, pteval2;
480 u64 arflags;
481 u64 arflags2;
482 u64 maflags2;
484 pteval &= ((1UL << 53) - 1);// ignore [63:53] bits
486 // FIXME address had better be pre-validated on insert
487 mask = ~itir_mask(itir.itir);
488 mpaddr = ((pteval & _PAGE_PPN_MASK) & ~mask) | (address & mask);
490 if (itir.ps > PAGE_SHIFT)
491 itir.ps = PAGE_SHIFT;
493 *logps = itir.ps;
495 pteval2 = lookup_domain_mpa(d, mpaddr, entry);
497 /* Check access rights. */
498 arflags = pteval & _PAGE_AR_MASK;
499 arflags2 = pteval2 & _PAGE_AR_MASK;
500 if (arflags != _PAGE_AR_R && arflags2 == _PAGE_AR_R) {
501 #if 0
502 dprintk(XENLOG_WARNING,
503 "%s:%d "
504 "pteval 0x%lx arflag 0x%lx address 0x%lx itir 0x%lx "
505 "pteval2 0x%lx arflags2 0x%lx mpaddr 0x%lx\n",
506 __func__, __LINE__,
507 pteval, arflags, address, itir__,
508 pteval2, arflags2, mpaddr);
509 #endif
510 pteval = (pteval & ~_PAGE_AR_MASK) | _PAGE_AR_R;
511 }
513 /* Check memory attribute. The switch is on the *requested* memory
514 attribute. */
515 maflags2 = pteval2 & _PAGE_MA_MASK;
516 switch (pteval & _PAGE_MA_MASK) {
517 case _PAGE_MA_NAT:
518 /* NaT pages are always accepted! */
519 break;
520 case _PAGE_MA_UC:
521 case _PAGE_MA_UCE:
522 case _PAGE_MA_WC:
523 if (maflags2 == _PAGE_MA_WB) {
524 /* Don't let domains WB-map uncached addresses.
525 This can happen when domU tries to touch i/o
526 port space. Also prevents possible address
527 aliasing issues. */
528 printk("Warning: UC to WB for mpaddr=%lx\n", mpaddr);
529 pteval = (pteval & ~_PAGE_MA_MASK) | _PAGE_MA_WB;
530 }
531 break;
532 case _PAGE_MA_WB:
533 if (maflags2 != _PAGE_MA_WB) {
534 /* Forbid non-coherent access to coherent memory. */
535 panic_domain(NULL, "try to use WB mem attr on "
536 "UC page, mpaddr=%lx\n", mpaddr);
537 }
538 break;
539 default:
540 panic_domain(NULL, "try to use unknown mem attribute\n");
541 }
543 /* If shadow mode is enabled, virtualize dirty bit. */
544 if (shadow_mode_enabled(d) && (pteval & _PAGE_D)) {
545 u64 mp_page = mpaddr >> PAGE_SHIFT;
546 pteval |= _PAGE_VIRT_D;
548 /* If the page is not already dirty, don't set the dirty bit! */
549 if (mp_page < d->arch.shadow_bitmap_size * 8
550 && !test_bit(mp_page, d->arch.shadow_bitmap))
551 pteval &= ~_PAGE_D;
552 }
554 /* Ignore non-addr bits of pteval2 and force PL0->2
555 (PL3 is unaffected) */
556 return (pteval & ~_PAGE_PPN_MASK) |
557 (pteval2 & _PAGE_PPN_MASK) | _PAGE_PL_2;
558 }
560 // given a current domain metaphysical address, return the physical address
561 unsigned long translate_domain_mpaddr(unsigned long mpaddr,
562 struct p2m_entry* entry)
563 {
564 unsigned long pteval;
566 pteval = lookup_domain_mpa(current->domain, mpaddr, entry);
567 return ((pteval & _PAGE_PPN_MASK) | (mpaddr & ~PAGE_MASK));
568 }
570 //XXX !xxx_present() should be used instread of !xxx_none()?
571 // pud, pmd, pte page is zero cleared when they are allocated.
572 // Their area must be visible before population so that
573 // cmpxchg must have release semantics.
574 static volatile pte_t*
575 lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
576 {
577 struct mm_struct *mm = &d->arch.mm;
578 volatile pgd_t *pgd;
579 volatile pud_t *pud;
580 volatile pmd_t *pmd;
582 BUG_ON(mm->pgd == NULL);
584 pgd = pgd_offset(mm, mpaddr);
585 again_pgd:
586 if (unlikely(pgd_none(*pgd))) { // acquire semantics
587 pud_t *old_pud = NULL;
588 pud = pud_alloc_one(mm, mpaddr);
589 if (unlikely(!pgd_cmpxchg_rel(mm, pgd, old_pud, pud))) {
590 pud_free(pud);
591 goto again_pgd;
592 }
593 }
595 pud = pud_offset(pgd, mpaddr);
596 again_pud:
597 if (unlikely(pud_none(*pud))) { // acquire semantics
598 pmd_t* old_pmd = NULL;
599 pmd = pmd_alloc_one(mm, mpaddr);
600 if (unlikely(!pud_cmpxchg_rel(mm, pud, old_pmd, pmd))) {
601 pmd_free(pmd);
602 goto again_pud;
603 }
604 }
606 pmd = pmd_offset(pud, mpaddr);
607 again_pmd:
608 if (unlikely(pmd_none(*pmd))) { // acquire semantics
609 pte_t* old_pte = NULL;
610 pte_t* pte = pte_alloc_one_kernel(mm, mpaddr);
611 if (unlikely(!pmd_cmpxchg_kernel_rel(mm, pmd, old_pte, pte))) {
612 pte_free_kernel(pte);
613 goto again_pmd;
614 }
615 }
617 return pte_offset_map(pmd, mpaddr);
618 }
620 //XXX xxx_none() should be used instread of !xxx_present()?
621 volatile pte_t*
622 lookup_noalloc_domain_pte(struct domain* d, unsigned long mpaddr)
623 {
624 struct mm_struct *mm = &d->arch.mm;
625 volatile pgd_t *pgd;
626 volatile pud_t *pud;
627 volatile pmd_t *pmd;
629 BUG_ON(mm->pgd == NULL);
630 pgd = pgd_offset(mm, mpaddr);
631 if (unlikely(!pgd_present(*pgd))) // acquire semantics
632 return NULL;
634 pud = pud_offset(pgd, mpaddr);
635 if (unlikely(!pud_present(*pud))) // acquire semantics
636 return NULL;
638 pmd = pmd_offset(pud, mpaddr);
639 if (unlikely(!pmd_present(*pmd))) // acquire semantics
640 return NULL;
642 return pte_offset_map(pmd, mpaddr);
643 }
645 static volatile pte_t*
646 lookup_noalloc_domain_pte_none(struct domain* d, unsigned long mpaddr)
647 {
648 struct mm_struct *mm = &d->arch.mm;
649 volatile pgd_t *pgd;
650 volatile pud_t *pud;
651 volatile pmd_t *pmd;
653 BUG_ON(mm->pgd == NULL);
654 pgd = pgd_offset(mm, mpaddr);
655 if (unlikely(pgd_none(*pgd))) // acquire semantics
656 return NULL;
658 pud = pud_offset(pgd, mpaddr);
659 if (unlikely(pud_none(*pud))) // acquire semantics
660 return NULL;
662 pmd = pmd_offset(pud, mpaddr);
663 if (unlikely(pmd_none(*pmd))) // acquire semantics
664 return NULL;
666 return pte_offset_map(pmd, mpaddr);
667 }
669 unsigned long
670 ____lookup_domain_mpa(struct domain *d, unsigned long mpaddr)
671 {
672 volatile pte_t *pte;
674 pte = lookup_noalloc_domain_pte(d, mpaddr);
675 if (pte == NULL)
676 return INVALID_MFN;
678 if (pte_present(*pte))
679 return (pte->pte & _PFN_MASK);
680 else if (VMX_DOMAIN(d->vcpu[0]))
681 return GPFN_INV_MASK;
682 return INVALID_MFN;
683 }
685 unsigned long lookup_domain_mpa(struct domain *d, unsigned long mpaddr,
686 struct p2m_entry* entry)
687 {
688 volatile pte_t *pte = lookup_noalloc_domain_pte(d, mpaddr);
690 if (pte != NULL) {
691 pte_t tmp_pte = *pte;// pte is volatile. copy the value.
692 if (pte_present(tmp_pte)) {
693 //printk("lookup_domain_page: found mapping for %lx, pte=%lx\n",mpaddr,pte_val(*pte));
694 if (entry != NULL)
695 p2m_entry_set(entry, pte, tmp_pte);
696 return pte_val(tmp_pte);
697 } else if (VMX_DOMAIN(d->vcpu[0]))
698 return GPFN_INV_MASK;
699 }
701 printk("%s: d 0x%p id %d current 0x%p id %d\n",
702 __func__, d, d->domain_id, current, current->vcpu_id);
703 if (mpaddr < d->arch.convmem_end)
704 printk("%s: non-allocated mpa 0x%lx (< 0x%lx)\n", __func__,
705 mpaddr, d->arch.convmem_end);
706 else
707 printk("%s: bad mpa 0x%lx (=> 0x%lx)\n", __func__,
708 mpaddr, d->arch.convmem_end);
710 if (entry != NULL)
711 p2m_entry_set(entry, NULL, __pte(0));
712 //XXX This is a work around until the emulation memory access to a region
713 // where memory or device are attached is implemented.
714 return pte_val(pfn_pte(0, __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX)));
715 }
717 // FIXME: ONLY USE FOR DOMAIN PAGE_SIZE == PAGE_SIZE
718 #if 1
719 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
720 {
721 unsigned long pte = lookup_domain_mpa(d, mpaddr, NULL);
722 unsigned long imva;
724 pte &= _PAGE_PPN_MASK;
725 imva = (unsigned long) __va(pte);
726 imva |= mpaddr & ~PAGE_MASK;
727 return (void*)imva;
728 }
729 #else
730 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
731 {
732 unsigned long imva = __gpa_to_mpa(d, mpaddr);
734 return (void *)__va(imva);
735 }
736 #endif
738 unsigned long
739 xencomm_paddr_to_maddr(unsigned long paddr)
740 {
741 struct vcpu *v = current;
742 struct domain *d = v->domain;
743 u64 pa;
745 pa = ____lookup_domain_mpa(d, paddr);
746 if (pa == INVALID_MFN) {
747 printk("%s: called with bad memory address: 0x%lx - iip=%lx\n",
748 __func__, paddr, vcpu_regs(v)->cr_iip);
749 return 0;
750 }
751 return __va_ul((pa & _PFN_MASK) | (paddr & ~PAGE_MASK));
752 }
754 /* Allocate a new page for domain and map it to the specified metaphysical
755 address. */
756 static struct page_info *
757 __assign_new_domain_page(struct domain *d, unsigned long mpaddr,
758 volatile pte_t* pte)
759 {
760 struct page_info *p;
761 unsigned long maddr;
762 int ret;
764 BUG_ON(!pte_none(*pte));
766 p = alloc_domheap_page(d);
767 if (unlikely(!p)) {
768 printk("assign_new_domain_page: Can't alloc!!!! Aaaargh!\n");
769 return(p);
770 }
772 // zero out pages for security reasons
773 clear_page(page_to_virt(p));
774 maddr = page_to_maddr (p);
775 if (unlikely(maddr > __get_cpu_var(vhpt_paddr)
776 && maddr < __get_cpu_var(vhpt_pend))) {
777 /* FIXME: how can this happen ?
778 vhpt is allocated by alloc_domheap_page. */
779 printk("assign_new_domain_page: reassigned vhpt page %lx!!\n",
780 maddr);
781 }
783 ret = get_page(p, d);
784 BUG_ON(ret == 0);
785 set_gpfn_from_mfn(page_to_mfn(p), mpaddr >> PAGE_SHIFT);
786 // clear_page() and set_gpfn_from_mfn() become visible before set_pte_rel()
787 // because set_pte_rel() has release semantics
788 set_pte_rel(pte,
789 pfn_pte(maddr >> PAGE_SHIFT,
790 __pgprot(_PAGE_PGC_ALLOCATED | __DIRTY_BITS |
791 _PAGE_PL_2 | _PAGE_AR_RWX)));
793 smp_mb();
794 return p;
795 }
797 struct page_info *
798 assign_new_domain_page(struct domain *d, unsigned long mpaddr)
799 {
800 volatile pte_t *pte = lookup_alloc_domain_pte(d, mpaddr);
802 if (!pte_none(*pte))
803 return NULL;
805 return __assign_new_domain_page(d, mpaddr, pte);
806 }
808 void
809 assign_new_domain0_page(struct domain *d, unsigned long mpaddr)
810 {
811 volatile pte_t *pte;
813 BUG_ON(d != dom0);
814 pte = lookup_alloc_domain_pte(d, mpaddr);
815 if (pte_none(*pte)) {
816 struct page_info *p = __assign_new_domain_page(d, mpaddr, pte);
817 if (p == NULL) {
818 panic("%s: can't allocate page for dom0", __func__);
819 }
820 }
821 }
823 static unsigned long
824 flags_to_prot (unsigned long flags)
825 {
826 unsigned long res = _PAGE_PL_2 | __DIRTY_BITS;
828 res |= flags & ASSIGN_readonly ? _PAGE_AR_R: _PAGE_AR_RWX;
829 res |= flags & ASSIGN_nocache ? _PAGE_MA_UC: _PAGE_MA_WB;
830 #ifdef CONFIG_XEN_IA64_TLB_TRACK
831 res |= flags & ASSIGN_tlb_track ? _PAGE_TLB_TRACKING: 0;
832 #endif
833 res |= flags & ASSIGN_pgc_allocated ? _PAGE_PGC_ALLOCATED: 0;
835 return res;
836 }
838 /* map a physical address to the specified metaphysical addr */
839 // flags: currently only ASSIGN_readonly, ASSIGN_nocache, ASSIGN_tlb_tack
840 // This is called by assign_domain_mmio_page().
841 // So accessing to pte is racy.
842 int
843 __assign_domain_page(struct domain *d,
844 unsigned long mpaddr, unsigned long physaddr,
845 unsigned long flags)
846 {
847 volatile pte_t *pte;
848 pte_t old_pte;
849 pte_t new_pte;
850 pte_t ret_pte;
851 unsigned long prot = flags_to_prot(flags);
853 pte = lookup_alloc_domain_pte(d, mpaddr);
855 old_pte = __pte(0);
856 new_pte = pfn_pte(physaddr >> PAGE_SHIFT, __pgprot(prot));
857 ret_pte = ptep_cmpxchg_rel(&d->arch.mm, mpaddr, pte, old_pte, new_pte);
858 if (pte_val(ret_pte) == pte_val(old_pte)) {
859 smp_mb();
860 return 0;
861 }
863 // dom0 tries to map real machine's I/O region, but failed.
864 // It is very likely that dom0 doesn't boot correctly because
865 // it can't access I/O. So complain here.
866 if ((flags & ASSIGN_nocache) &&
867 (pte_pfn(ret_pte) != (physaddr >> PAGE_SHIFT) ||
868 !(pte_val(ret_pte) & _PAGE_MA_UC)))
869 printk("%s:%d WARNING can't assign page domain 0x%p id %d\n"
870 "\talready assigned pte_val 0x%016lx\n"
871 "\tmpaddr 0x%016lx physaddr 0x%016lx flags 0x%lx\n",
872 __func__, __LINE__,
873 d, d->domain_id, pte_val(ret_pte),
874 mpaddr, physaddr, flags);
876 return -EAGAIN;
877 }
879 /* get_page() and map a physical address to the specified metaphysical addr */
880 void
881 assign_domain_page(struct domain *d,
882 unsigned long mpaddr, unsigned long physaddr)
883 {
884 struct page_info* page = mfn_to_page(physaddr >> PAGE_SHIFT);
885 int ret;
887 BUG_ON((physaddr & GPFN_IO_MASK) != GPFN_MEM);
888 ret = get_page(page, d);
889 BUG_ON(ret == 0);
890 set_gpfn_from_mfn(physaddr >> PAGE_SHIFT, mpaddr >> PAGE_SHIFT);
891 // because __assign_domain_page() uses set_pte_rel() which has
892 // release semantics, smp_mb() isn't needed.
893 (void)__assign_domain_page(d, mpaddr, physaddr,
894 ASSIGN_writable | ASSIGN_pgc_allocated);
895 }
897 int
898 ioports_permit_access(struct domain *d, unsigned long fp, unsigned long lp)
899 {
900 int ret;
901 unsigned long off;
902 unsigned long fp_offset;
903 unsigned long lp_offset;
905 ret = rangeset_add_range(d->arch.ioport_caps, fp, lp);
906 if (ret != 0)
907 return ret;
909 /* Domain 0 doesn't virtualize IO ports space. */
910 if (d == dom0)
911 return 0;
913 fp_offset = IO_SPACE_SPARSE_ENCODING(fp) & ~PAGE_MASK;
914 lp_offset = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lp));
916 for (off = fp_offset; off <= lp_offset; off += PAGE_SIZE)
917 (void)__assign_domain_page(d, IO_PORTS_PADDR + off,
918 __pa(ia64_iobase) + off, ASSIGN_nocache);
920 return 0;
921 }
923 static int
924 ioports_has_allowed(struct domain *d, unsigned long fp, unsigned long lp)
925 {
926 unsigned long i;
927 for (i = fp; i < lp; i++)
928 if (rangeset_contains_singleton(d->arch.ioport_caps, i))
929 return 1;
930 return 0;
931 }
933 int
934 ioports_deny_access(struct domain *d, unsigned long fp, unsigned long lp)
935 {
936 int ret;
937 struct mm_struct *mm = &d->arch.mm;
938 unsigned long off;
939 unsigned long io_ports_base;
940 unsigned long fp_offset;
941 unsigned long lp_offset;
943 ret = rangeset_remove_range(d->arch.ioport_caps, fp, lp);
944 if (ret != 0)
945 return ret;
946 if (d == dom0)
947 io_ports_base = __pa(ia64_iobase);
948 else
949 io_ports_base = IO_PORTS_PADDR;
951 fp_offset = IO_SPACE_SPARSE_ENCODING(fp) & PAGE_MASK;
952 lp_offset = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lp));
954 for (off = fp_offset; off < lp_offset; off += PAGE_SIZE) {
955 unsigned long mpaddr = io_ports_base + off;
956 unsigned long port;
957 volatile pte_t *pte;
958 pte_t old_pte;
960 port = IO_SPACE_SPARSE_DECODING (off);
961 if (port < fp || port + IO_SPACE_SPARSE_PORTS_PER_PAGE - 1 > lp) {
962 /* Maybe this covers an allowed port. */
963 if (ioports_has_allowed(d, port,
964 port + IO_SPACE_SPARSE_PORTS_PER_PAGE - 1))
965 continue;
966 }
968 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
969 BUG_ON(pte == NULL);
970 BUG_ON(pte_none(*pte));
972 // clear pte
973 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
974 }
975 domain_flush_vtlb_all();
976 return 0;
977 }
979 static void
980 assign_domain_same_page(struct domain *d,
981 unsigned long mpaddr, unsigned long size,
982 unsigned long flags)
983 {
984 //XXX optimization
985 unsigned long end = PAGE_ALIGN(mpaddr + size);
986 for (mpaddr &= PAGE_MASK; mpaddr < end; mpaddr += PAGE_SIZE) {
987 (void)__assign_domain_page(d, mpaddr, mpaddr, flags);
988 }
989 }
991 int
992 efi_mmio(unsigned long physaddr, unsigned long size)
993 {
994 void *efi_map_start, *efi_map_end;
995 u64 efi_desc_size;
996 void* p;
998 efi_map_start = __va(ia64_boot_param->efi_memmap);
999 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1000 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1002 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1003 efi_memory_desc_t* md = (efi_memory_desc_t *)p;
1004 unsigned long start = md->phys_addr;
1005 unsigned long end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
1007 if (start <= physaddr && physaddr < end) {
1008 if ((physaddr + size) > end) {
1009 gdprintk(XENLOG_INFO, "%s: physaddr 0x%lx size = 0x%lx\n",
1010 __func__, physaddr, size);
1011 return 0;
1014 // for io space
1015 if (md->type == EFI_MEMORY_MAPPED_IO ||
1016 md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
1017 return 1;
1020 // for runtime
1021 // see efi_enter_virtual_mode(void)
1022 // in linux/arch/ia64/kernel/efi.c
1023 if ((md->attribute & EFI_MEMORY_RUNTIME) &&
1024 !(md->attribute & EFI_MEMORY_WB)) {
1025 return 1;
1028 return 0;
1031 if (physaddr < start) {
1032 break;
1036 return 1;
1039 unsigned long
1040 assign_domain_mmio_page(struct domain *d,
1041 unsigned long mpaddr, unsigned long size)
1043 if (size == 0) {
1044 gdprintk(XENLOG_INFO, "%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
1045 __func__, d, mpaddr, size);
1047 if (!efi_mmio(mpaddr, size)) {
1048 #ifndef NDEBUG
1049 gdprintk(XENLOG_INFO, "%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
1050 __func__, d, mpaddr, size);
1051 #endif
1052 return -EINVAL;
1054 assign_domain_same_page(d, mpaddr, size, ASSIGN_writable | ASSIGN_nocache);
1055 return mpaddr;
1058 unsigned long
1059 assign_domain_mach_page(struct domain *d,
1060 unsigned long mpaddr, unsigned long size,
1061 unsigned long flags)
1063 BUG_ON(flags & ASSIGN_pgc_allocated);
1064 assign_domain_same_page(d, mpaddr, size, flags);
1065 return mpaddr;
1068 static void
1069 domain_put_page(struct domain* d, unsigned long mpaddr,
1070 volatile pte_t* ptep, pte_t old_pte, int clear_PGC_allocate)
1072 unsigned long mfn = pte_pfn(old_pte);
1073 struct page_info* page = mfn_to_page(mfn);
1075 if (pte_pgc_allocated(old_pte)) {
1076 if (page_get_owner(page) == d || page_get_owner(page) == NULL) {
1077 BUG_ON(get_gpfn_from_mfn(mfn) != (mpaddr >> PAGE_SHIFT));
1078 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
1079 } else {
1080 BUG();
1083 if (clear_PGC_allocate)
1084 try_to_clear_PGC_allocate(d, page);
1086 domain_page_flush_and_put(d, mpaddr, ptep, old_pte, page);
1089 // caller must get_page(mfn_to_page(mfn)) before call.
1090 // caller must call set_gpfn_from_mfn() before call if necessary.
1091 // because set_gpfn_from_mfn() result must be visible before pte xchg
1092 // caller must use memory barrier. NOTE: xchg has acquire semantics.
1093 // flags: ASSIGN_xxx
1094 static void
1095 assign_domain_page_replace(struct domain *d, unsigned long mpaddr,
1096 unsigned long mfn, unsigned long flags)
1098 struct mm_struct *mm = &d->arch.mm;
1099 volatile pte_t* pte;
1100 pte_t old_pte;
1101 pte_t npte;
1102 unsigned long prot = flags_to_prot(flags);
1104 pte = lookup_alloc_domain_pte(d, mpaddr);
1106 // update pte
1107 npte = pfn_pte(mfn, __pgprot(prot));
1108 old_pte = ptep_xchg(mm, mpaddr, pte, npte);
1109 if (pte_mem(old_pte)) {
1110 unsigned long old_mfn = pte_pfn(old_pte);
1112 // mfn = old_mfn case can happen when domain maps a granted page
1113 // twice with the same pseudo physial address.
1114 // It's non sense, but allowed.
1115 // __gnttab_map_grant_ref()
1116 // => create_host_mapping()
1117 // => assign_domain_page_replace()
1118 if (mfn != old_mfn) {
1119 domain_put_page(d, mpaddr, pte, old_pte, 1);
1122 perfc_incrc(assign_domain_page_replace);
1125 // caller must get_page(new_page) before
1126 // Only steal_page() calls this function.
1127 static int
1128 assign_domain_page_cmpxchg_rel(struct domain* d, unsigned long mpaddr,
1129 struct page_info* old_page,
1130 struct page_info* new_page,
1131 unsigned long flags)
1133 struct mm_struct *mm = &d->arch.mm;
1134 volatile pte_t* pte;
1135 unsigned long old_mfn;
1136 unsigned long old_prot;
1137 pte_t old_pte;
1138 unsigned long new_mfn;
1139 unsigned long new_prot;
1140 pte_t new_pte;
1141 pte_t ret_pte;
1143 pte = lookup_alloc_domain_pte(d, mpaddr);
1145 again:
1146 old_prot = pte_val(*pte) & ~_PAGE_PPN_MASK;
1147 old_mfn = page_to_mfn(old_page);
1148 old_pte = pfn_pte(old_mfn, __pgprot(old_prot));
1149 if (!pte_present(old_pte)) {
1150 gdprintk(XENLOG_INFO,
1151 "%s: old_pte 0x%lx old_prot 0x%lx old_mfn 0x%lx\n",
1152 __func__, pte_val(old_pte), old_prot, old_mfn);
1153 return -EINVAL;
1156 new_prot = flags_to_prot(flags);
1157 new_mfn = page_to_mfn(new_page);
1158 new_pte = pfn_pte(new_mfn, __pgprot(new_prot));
1160 // update pte
1161 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1162 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1163 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1164 goto again;
1167 gdprintk(XENLOG_INFO,
1168 "%s: old_pte 0x%lx old_prot 0x%lx old_mfn 0x%lx "
1169 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1170 __func__,
1171 pte_val(old_pte), old_prot, old_mfn,
1172 pte_val(ret_pte), pte_pfn(ret_pte));
1173 return -EINVAL;
1176 BUG_ON(!pte_mem(old_pte));
1177 BUG_ON(!pte_pgc_allocated(old_pte));
1178 BUG_ON(page_get_owner(old_page) != d);
1179 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
1180 BUG_ON(old_mfn == new_mfn);
1182 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
1184 domain_page_flush_and_put(d, mpaddr, pte, old_pte, old_page);
1185 perfc_incrc(assign_domain_pge_cmpxchg_rel);
1186 return 0;
1189 static void
1190 zap_domain_page_one(struct domain *d, unsigned long mpaddr, unsigned long mfn)
1192 struct mm_struct *mm = &d->arch.mm;
1193 volatile pte_t *pte;
1194 pte_t old_pte;
1195 struct page_info *page;
1197 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
1198 if (pte == NULL)
1199 return;
1200 if (pte_none(*pte))
1201 return;
1203 if (mfn == INVALID_MFN) {
1204 // clear pte
1205 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
1206 mfn = pte_pfn(old_pte);
1207 } else {
1208 unsigned long old_arflags;
1209 pte_t new_pte;
1210 pte_t ret_pte;
1212 again:
1213 // memory_exchange() calls guest_physmap_remove_page() with
1214 // a stealed page. i.e. page owner = NULL.
1215 BUG_ON(page_get_owner(mfn_to_page(mfn)) != d &&
1216 page_get_owner(mfn_to_page(mfn)) != NULL);
1217 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1218 old_pte = pfn_pte(mfn, __pgprot(old_arflags));
1219 new_pte = __pte(0);
1221 // update pte
1222 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1223 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1224 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1225 goto again;
1228 gdprintk(XENLOG_INFO, "%s: old_pte 0x%lx old_arflags 0x%lx mfn 0x%lx "
1229 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1230 __func__,
1231 pte_val(old_pte), old_arflags, mfn,
1232 pte_val(ret_pte), pte_pfn(ret_pte));
1233 return;
1235 BUG_ON(mfn != pte_pfn(ret_pte));
1238 page = mfn_to_page(mfn);
1239 BUG_ON((page->count_info & PGC_count_mask) == 0);
1241 // exchange_memory() calls
1242 // steal_page()
1243 // page owner is set to NULL
1244 // guest_physmap_remove_page()
1245 // zap_domain_page_one()
1246 domain_put_page(d, mpaddr, pte, old_pte, (page_get_owner(page) != NULL));
1247 perfc_incrc(zap_dcomain_page_one);
1250 unsigned long
1251 dom0vp_zap_physmap(struct domain *d, unsigned long gpfn,
1252 unsigned int extent_order)
1254 if (extent_order != 0) {
1255 //XXX
1256 return -ENOSYS;
1259 zap_domain_page_one(d, gpfn << PAGE_SHIFT, INVALID_MFN);
1260 perfc_incrc(dom0vp_zap_physmap);
1261 return 0;
1264 static unsigned long
1265 __dom0vp_add_physmap(struct domain* d, unsigned long gpfn,
1266 unsigned long mfn_or_gmfn,
1267 unsigned long flags, domid_t domid, int is_gmfn)
1269 int error = -EINVAL;
1270 struct domain* rd;
1271 unsigned long mfn;
1273 /* Not allowed by a domain. */
1274 if (flags & (ASSIGN_nocache | ASSIGN_pgc_allocated))
1275 return -EINVAL;
1277 rd = find_domain_by_id(domid);
1278 if (unlikely(rd == NULL)) {
1279 switch (domid) {
1280 case DOMID_XEN:
1281 rd = dom_xen;
1282 break;
1283 case DOMID_IO:
1284 rd = dom_io;
1285 break;
1286 default:
1287 gdprintk(XENLOG_INFO, "d 0x%p domid %d "
1288 "pgfn 0x%lx mfn_or_gmfn 0x%lx flags 0x%lx domid %d\n",
1289 d, d->domain_id, gpfn, mfn_or_gmfn, flags, domid);
1290 return -ESRCH;
1292 BUG_ON(rd == NULL);
1293 get_knownalive_domain(rd);
1296 if (unlikely(rd == d))
1297 goto out1;
1298 /*
1299 * DOMID_XEN and DOMID_IO don't have their own p2m table.
1300 * It can be considered that their p2m conversion is p==m.
1301 */
1302 if (likely(is_gmfn && domid != DOMID_XEN && domid != DOMID_IO))
1303 mfn = gmfn_to_mfn(rd, mfn_or_gmfn);
1304 else
1305 mfn = mfn_or_gmfn;
1306 if (unlikely(!mfn_valid(mfn) || get_page(mfn_to_page(mfn), rd) == 0))
1307 goto out1;
1309 error = 0;
1310 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1311 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1312 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, flags);
1313 //don't update p2m table because this page belongs to rd, not d.
1314 perfc_incrc(dom0vp_add_physmap);
1315 out1:
1316 put_domain(rd);
1317 return error;
1320 unsigned long
1321 dom0vp_add_physmap(struct domain* d, unsigned long gpfn, unsigned long mfn,
1322 unsigned long flags, domid_t domid)
1324 return __dom0vp_add_physmap(d, gpfn, mfn, flags, domid, 0);
1327 unsigned long
1328 dom0vp_add_physmap_with_gmfn(struct domain* d, unsigned long gpfn,
1329 unsigned long gmfn, unsigned long flags,
1330 domid_t domid)
1332 return __dom0vp_add_physmap(d, gpfn, gmfn, flags, domid, 1);
1335 #ifdef CONFIG_XEN_IA64_EXPOSE_P2M
1336 static struct page_info* p2m_pte_zero_page = NULL;
1338 void
1339 expose_p2m_init(void)
1341 pte_t* pte;
1343 pte = pte_alloc_one_kernel(NULL, 0);
1344 BUG_ON(pte == NULL);
1345 smp_mb();// make contents of the page visible.
1346 p2m_pte_zero_page = virt_to_page(pte);
1349 static int
1350 expose_p2m_page(struct domain* d, unsigned long mpaddr, struct page_info* page)
1352 // we can't get_page(page) here.
1353 // pte page is allocated form xen heap.(see pte_alloc_one_kernel().)
1354 // so that the page has NULL page owner and it's reference count
1355 // is useless.
1356 // see also mm_teardown_pte()'s page_get_owner() == NULL check.
1357 BUG_ON(page_get_owner(page) != NULL);
1359 return __assign_domain_page(d, mpaddr, page_to_maddr(page),
1360 ASSIGN_readonly);
1363 // It is possible to optimize loop, But this isn't performance critical.
1364 unsigned long
1365 dom0vp_expose_p2m(struct domain* d,
1366 unsigned long conv_start_gpfn,
1367 unsigned long assign_start_gpfn,
1368 unsigned long expose_size, unsigned long granule_pfn)
1370 unsigned long expose_num_pfn = expose_size >> PAGE_SHIFT;
1371 unsigned long i;
1372 volatile pte_t* conv_pte;
1373 volatile pte_t* assign_pte;
1375 if ((expose_size % PAGE_SIZE) != 0 ||
1376 (granule_pfn % PTRS_PER_PTE) != 0 ||
1377 (expose_num_pfn % PTRS_PER_PTE) != 0 ||
1378 (conv_start_gpfn % granule_pfn) != 0 ||
1379 (assign_start_gpfn % granule_pfn) != 0 ||
1380 (expose_num_pfn % granule_pfn) != 0) {
1381 gdprintk(XENLOG_INFO,
1382 "%s conv_start_gpfn 0x%016lx assign_start_gpfn 0x%016lx "
1383 "expose_size 0x%016lx granulte_pfn 0x%016lx\n", __func__,
1384 conv_start_gpfn, assign_start_gpfn, expose_size, granule_pfn);
1385 return -EINVAL;
1388 if (granule_pfn != PTRS_PER_PTE) {
1389 gdprintk(XENLOG_INFO,
1390 "%s granule_pfn 0x%016lx PTRS_PER_PTE 0x%016lx\n",
1391 __func__, granule_pfn, PTRS_PER_PTE);
1392 return -ENOSYS;
1395 // allocate pgd, pmd.
1396 i = conv_start_gpfn;
1397 while (i < expose_num_pfn) {
1398 conv_pte = lookup_noalloc_domain_pte(d, (conv_start_gpfn + i) <<
1399 PAGE_SHIFT);
1400 if (conv_pte == NULL) {
1401 i++;
1402 continue;
1405 assign_pte = lookup_alloc_domain_pte(d, (assign_start_gpfn <<
1406 PAGE_SHIFT) + i * sizeof(pte_t));
1407 if (assign_pte == NULL) {
1408 gdprintk(XENLOG_INFO, "%s failed to allocate pte page\n", __func__);
1409 return -ENOMEM;
1412 // skip to next pte page
1413 i += PTRS_PER_PTE;
1414 i &= ~(PTRS_PER_PTE - 1);
1417 // expose pte page
1418 i = 0;
1419 while (i < expose_num_pfn) {
1420 conv_pte = lookup_noalloc_domain_pte(d, (conv_start_gpfn + i) <<
1421 PAGE_SHIFT);
1422 if (conv_pte == NULL) {
1423 i++;
1424 continue;
1427 if (expose_p2m_page(d, (assign_start_gpfn << PAGE_SHIFT) +
1428 i * sizeof(pte_t), virt_to_page(conv_pte)) < 0) {
1429 gdprintk(XENLOG_INFO, "%s failed to assign page\n", __func__);
1430 return -EAGAIN;
1433 // skip to next pte page
1434 i += PTRS_PER_PTE;
1435 i &= ~(PTRS_PER_PTE - 1);
1438 // expose p2m_pte_zero_page
1439 for (i = 0; i < expose_num_pfn / PTRS_PER_PTE + 1; i++) {
1440 assign_pte = lookup_noalloc_domain_pte(d, (assign_start_gpfn + i) <<
1441 PAGE_SHIFT);
1442 if (assign_pte == NULL || pte_present(*assign_pte))
1443 continue;
1445 if (expose_p2m_page(d, (assign_start_gpfn + i) << PAGE_SHIFT,
1446 p2m_pte_zero_page) < 0) {
1447 gdprintk(XENLOG_INFO, "%s failed to assign zero-pte page\n", __func__);
1448 return -EAGAIN;
1452 return 0;
1454 #endif
1456 // grant table host mapping
1457 // mpaddr: host_addr: pseudo physical address
1458 // mfn: frame: machine page frame
1459 // flags: GNTMAP_readonly | GNTMAP_application_map | GNTMAP_contains_pte
1460 int
1461 create_grant_host_mapping(unsigned long gpaddr,
1462 unsigned long mfn, unsigned int flags)
1464 struct domain* d = current->domain;
1465 struct page_info* page;
1466 int ret;
1468 if (flags & (GNTMAP_device_map |
1469 GNTMAP_application_map | GNTMAP_contains_pte)) {
1470 gdprintk(XENLOG_INFO, "%s: flags 0x%x\n", __func__, flags);
1471 return GNTST_general_error;
1474 BUG_ON(!mfn_valid(mfn));
1475 page = mfn_to_page(mfn);
1476 ret = get_page(page, page_get_owner(page));
1477 BUG_ON(ret == 0);
1478 assign_domain_page_replace(d, gpaddr, mfn,
1479 #ifdef CONFIG_XEN_IA64_TLB_TRACK
1480 ASSIGN_tlb_track |
1481 #endif
1482 ((flags & GNTMAP_readonly) ?
1483 ASSIGN_readonly : ASSIGN_writable));
1484 perfc_incrc(create_grant_host_mapping);
1485 return GNTST_okay;
1488 // grant table host unmapping
1489 int
1490 destroy_grant_host_mapping(unsigned long gpaddr,
1491 unsigned long mfn, unsigned int flags)
1493 struct domain* d = current->domain;
1494 unsigned long gpfn = gpaddr >> PAGE_SHIFT;
1495 volatile pte_t* pte;
1496 unsigned long cur_arflags;
1497 pte_t cur_pte;
1498 pte_t new_pte;
1499 pte_t old_pte;
1500 struct page_info* page = mfn_to_page(mfn);
1502 if (flags & (GNTMAP_application_map | GNTMAP_contains_pte)) {
1503 gdprintk(XENLOG_INFO, "%s: flags 0x%x\n", __func__, flags);
1504 return GNTST_general_error;
1507 pte = lookup_noalloc_domain_pte(d, gpaddr);
1508 if (pte == NULL) {
1509 gdprintk(XENLOG_INFO, "%s: gpaddr 0x%lx mfn 0x%lx\n",
1510 __func__, gpaddr, mfn);
1511 return GNTST_general_error;
1514 again:
1515 cur_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1516 cur_pte = pfn_pte(mfn, __pgprot(cur_arflags));
1517 if (!pte_present(cur_pte) ||
1518 (page_get_owner(page) == d && get_gpfn_from_mfn(mfn) == gpfn)) {
1519 gdprintk(XENLOG_INFO, "%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx\n",
1520 __func__, gpaddr, mfn, pte_val(cur_pte));
1521 return GNTST_general_error;
1523 new_pte = __pte(0);
1525 old_pte = ptep_cmpxchg_rel(&d->arch.mm, gpaddr, pte, cur_pte, new_pte);
1526 if (unlikely(!pte_present(old_pte))) {
1527 gdprintk(XENLOG_INFO, "%s: gpaddr 0x%lx mfn 0x%lx"
1528 " cur_pte 0x%lx old_pte 0x%lx\n",
1529 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1530 return GNTST_general_error;
1532 if (unlikely(pte_val(cur_pte) != pte_val(old_pte))) {
1533 if (pte_pfn(old_pte) == mfn) {
1534 goto again;
1536 gdprintk(XENLOG_INFO, "%s gpaddr 0x%lx mfn 0x%lx cur_pte "
1537 "0x%lx old_pte 0x%lx\n",
1538 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1539 return GNTST_general_error;
1541 BUG_ON(pte_pfn(old_pte) != mfn);
1543 /* try_to_clear_PGC_allocate(d, page) is not needed. */
1544 BUG_ON(page_get_owner(page) == d &&
1545 get_gpfn_from_mfn(mfn) == gpfn);
1546 domain_page_flush_and_put(d, gpaddr, pte, old_pte, page);
1548 perfc_incrc(destroy_grant_host_mapping);
1549 return GNTST_okay;
1552 // heavily depends on the struct page layout.
1553 // gnttab_transfer() calls steal_page() with memflags = 0
1554 // For grant table transfer, we must fill the page.
1555 // memory_exchange() calls steal_page() with memflags = MEMF_no_refcount
1556 // For memory exchange, we don't have to fill the page because
1557 // memory_exchange() does it.
1558 int
1559 steal_page(struct domain *d, struct page_info *page, unsigned int memflags)
1561 #if 0 /* if big endian */
1562 # error "implement big endian version of steal_page()"
1563 #endif
1564 u32 _d, _nd;
1565 u64 x, nx, y;
1567 if (page_get_owner(page) != d) {
1568 gdprintk(XENLOG_INFO, "%s d 0x%p owner 0x%p\n",
1569 __func__, d, page_get_owner(page));
1570 return -1;
1573 if (!(memflags & MEMF_no_refcount)) {
1574 unsigned long gpfn;
1575 struct page_info *new;
1576 unsigned long new_mfn;
1577 int ret;
1579 new = alloc_domheap_page(d);
1580 if (new == NULL) {
1581 gdprintk(XENLOG_INFO, "alloc_domheap_page() failed\n");
1582 return -1;
1584 // zero out pages for security reasons
1585 clear_page(page_to_virt(new));
1586 // assign_domain_page_cmpxchg_rel() has release semantics
1587 // so smp_mb() isn't needed.
1589 ret = get_page(new, d);
1590 BUG_ON(ret == 0);
1592 gpfn = get_gpfn_from_mfn(page_to_mfn(page));
1593 if (gpfn == INVALID_M2P_ENTRY) {
1594 free_domheap_page(new);
1595 return -1;
1597 new_mfn = page_to_mfn(new);
1598 set_gpfn_from_mfn(new_mfn, gpfn);
1599 // smp_mb() isn't needed because assign_domain_pge_cmpxchg_rel()
1600 // has release semantics.
1602 ret = assign_domain_page_cmpxchg_rel(d, gpfn << PAGE_SHIFT, page, new,
1603 ASSIGN_writable |
1604 ASSIGN_pgc_allocated);
1605 if (ret < 0) {
1606 gdprintk(XENLOG_INFO, "assign_domain_page_cmpxchg_rel failed %d\n",
1607 ret);
1608 set_gpfn_from_mfn(new_mfn, INVALID_M2P_ENTRY);
1609 free_domheap_page(new);
1610 return -1;
1612 perfc_incrc(steal_page_refcount);
1615 spin_lock(&d->page_alloc_lock);
1617 /*
1618 * The tricky bit: atomically release ownership while there is just one
1619 * benign reference to the page (PGC_allocated). If that reference
1620 * disappears then the deallocation routine will safely spin.
1621 */
1622 _d = pickle_domptr(d);
1623 y = *((u64*)&page->count_info);
1624 do {
1625 x = y;
1626 nx = x & 0xffffffff;
1627 // page->count_info: untouched
1628 // page->u.inused._domain = 0;
1629 _nd = x >> 32;
1631 if (
1632 // when !MEMF_no_refcount, page might be put_page()'d or
1633 // it will be put_page()'d later depending on queued.
1634 unlikely(!(memflags & MEMF_no_refcount) &&
1635 ((x & (PGC_count_mask | PGC_allocated)) !=
1636 (1 | PGC_allocated))) ||
1637 // when MEMF_no_refcount, page isn't de-assigned from
1638 // this domain yet. So count_info = 2
1639 unlikely((memflags & MEMF_no_refcount) &&
1640 ((x & (PGC_count_mask | PGC_allocated)) !=
1641 (2 | PGC_allocated))) ||
1643 unlikely(_nd != _d)) {
1644 struct domain* nd = unpickle_domptr(_nd);
1645 if (nd == NULL) {
1646 gdprintk(XENLOG_INFO, "gnttab_transfer: "
1647 "Bad page %p: ed=%p(%u) 0x%x, "
1648 "sd=%p 0x%x,"
1649 " caf=%016lx, taf=%" PRtype_info
1650 " memflags 0x%x\n",
1651 (void *) page_to_mfn(page),
1652 d, d->domain_id, _d,
1653 nd, _nd,
1654 x,
1655 page->u.inuse.type_info,
1656 memflags);
1657 } else {
1658 gdprintk(XENLOG_WARNING, "gnttab_transfer: "
1659 "Bad page %p: ed=%p(%u) 0x%x, "
1660 "sd=%p(%u) 0x%x,"
1661 " caf=%016lx, taf=%" PRtype_info
1662 " memflags 0x%x\n",
1663 (void *) page_to_mfn(page),
1664 d, d->domain_id, _d,
1665 nd, nd->domain_id, _nd,
1666 x,
1667 page->u.inuse.type_info,
1668 memflags);
1670 spin_unlock(&d->page_alloc_lock);
1671 return -1;
1674 y = cmpxchg((u64*)&page->count_info, x, nx);
1675 } while (unlikely(y != x));
1677 /*
1678 * Unlink from 'd'. At least one reference remains (now anonymous), so
1679 * noone else is spinning to try to delete this page from 'd'.
1680 */
1681 if ( !(memflags & MEMF_no_refcount) )
1682 d->tot_pages--;
1683 list_del(&page->list);
1685 spin_unlock(&d->page_alloc_lock);
1686 perfc_incrc(steal_page);
1687 return 0;
1690 void
1691 guest_physmap_add_page(struct domain *d, unsigned long gpfn,
1692 unsigned long mfn)
1694 int ret;
1696 BUG_ON(!mfn_valid(mfn));
1697 ret = get_page(mfn_to_page(mfn), d);
1698 BUG_ON(ret == 0);
1699 set_gpfn_from_mfn(mfn, gpfn);
1700 smp_mb();
1701 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn,
1702 ASSIGN_writable | ASSIGN_pgc_allocated);
1704 //BUG_ON(mfn != ((lookup_domain_mpa(d, gpfn << PAGE_SHIFT) & _PFN_MASK) >> PAGE_SHIFT));
1706 perfc_incrc(guest_physmap_add_page);
1709 void
1710 guest_physmap_remove_page(struct domain *d, unsigned long gpfn,
1711 unsigned long mfn)
1713 BUG_ON(mfn == 0);//XXX
1714 zap_domain_page_one(d, gpfn << PAGE_SHIFT, mfn);
1715 perfc_incrc(guest_physmap_remove_page);
1718 static void
1719 domain_page_flush_and_put(struct domain* d, unsigned long mpaddr,
1720 volatile pte_t* ptep, pte_t old_pte,
1721 struct page_info* page)
1723 #ifdef CONFIG_XEN_IA64_TLB_TRACK
1724 struct tlb_track_entry* entry;
1725 #endif
1727 if (shadow_mode_enabled(d))
1728 shadow_mark_page_dirty(d, mpaddr >> PAGE_SHIFT);
1730 #ifndef CONFIG_XEN_IA64_TLB_TRACK
1731 //XXX sledgehammer.
1732 // flush finer range.
1733 domain_flush_vtlb_all();
1734 put_page(page);
1735 #else
1736 switch (tlb_track_search_and_remove(d->arch.tlb_track,
1737 ptep, old_pte, &entry)) {
1738 case TLB_TRACK_NOT_TRACKED:
1739 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_NOT_TRACKED\n", __func__);
1740 /* This page is zapped from this domain
1741 * by memory decrease or exchange or dom0vp_zap_physmap.
1742 * I.e. the page is zapped for returning this page to xen
1743 * (balloon driver or DMA page allocation) or
1744 * foreign domain mapped page is unmapped from the domain.
1745 * In the former case the page is to be freed so that
1746 * we can defer freeing page to batch.
1747 * In the latter case the page is unmapped so that
1748 * we need to flush it. But to optimize it, we
1749 * queue the page and flush vTLB only once.
1750 * I.e. The caller must call dfree_flush() explicitly.
1751 */
1752 domain_flush_vtlb_all();
1753 put_page(page);
1754 break;
1755 case TLB_TRACK_NOT_FOUND:
1756 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_NOT_FOUND\n", __func__);
1757 /* This page is zapped from this domain
1758 * by grant table page unmap.
1759 * Luckily the domain that mapped this page didn't
1760 * access this page so that we don't have to flush vTLB.
1761 * Probably the domain did only DMA.
1762 */
1763 /* do nothing */
1764 put_page(page);
1765 break;
1766 case TLB_TRACK_FOUND:
1767 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_FOUND\n", __func__);
1768 /* This page is zapped from this domain
1769 * by grant table page unmap.
1770 * Fortunately this page is accessced via only one virtual
1771 * memory address. So it is easy to flush it.
1772 */
1773 domain_flush_vtlb_track_entry(d, entry);
1774 tlb_track_free_entry(d->arch.tlb_track, entry);
1775 put_page(page);
1776 break;
1777 case TLB_TRACK_MANY:
1778 gdprintk(XENLOG_INFO, "%s TLB_TRACK_MANY\n", __func__);
1779 /* This page is zapped from this domain
1780 * by grant table page unmap.
1781 * Unfortunately this page is accessced via many virtual
1782 * memory address (or too many times with single virtual address).
1783 * So we abondaned to track virtual addresses.
1784 * full vTLB flush is necessary.
1785 */
1786 domain_flush_vtlb_all();
1787 put_page(page);
1788 break;
1789 case TLB_TRACK_AGAIN:
1790 gdprintk(XENLOG_ERR, "%s TLB_TRACK_AGAIN\n", __func__);
1791 BUG();
1792 break;
1794 #endif
1795 perfc_incrc(domain_page_flush_and_put);
1798 int
1799 domain_page_mapped(struct domain* d, unsigned long mpaddr)
1801 volatile pte_t * pte;
1803 pte = lookup_noalloc_domain_pte(d, mpaddr);
1804 if(pte != NULL && !pte_none(*pte))
1805 return 1;
1806 return 0;
1809 /* Flush cache of domain d. */
1810 void domain_cache_flush (struct domain *d, int sync_only)
1812 struct mm_struct *mm = &d->arch.mm;
1813 volatile pgd_t *pgd = mm->pgd;
1814 unsigned long maddr;
1815 int i,j,k, l;
1816 int nbr_page = 0;
1817 void (*flush_func)(unsigned long start, unsigned long end);
1818 extern void flush_dcache_range (unsigned long, unsigned long);
1820 if (sync_only)
1821 flush_func = &flush_icache_range;
1822 else
1823 flush_func = &flush_dcache_range;
1825 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
1826 volatile pud_t *pud;
1827 if (!pgd_present(*pgd)) // acquire semantics
1828 continue;
1829 pud = pud_offset(pgd, 0);
1830 for (j = 0; j < PTRS_PER_PUD; pud++, j++) {
1831 volatile pmd_t *pmd;
1832 if (!pud_present(*pud)) // acquire semantics
1833 continue;
1834 pmd = pmd_offset(pud, 0);
1835 for (k = 0; k < PTRS_PER_PMD; pmd++, k++) {
1836 volatile pte_t *pte;
1837 if (!pmd_present(*pmd)) // acquire semantics
1838 continue;
1839 pte = pte_offset_map(pmd, 0);
1840 for (l = 0; l < PTRS_PER_PTE; pte++, l++) {
1841 if (!pte_present(*pte)) // acquire semantics
1842 continue;
1843 /* Convert PTE to maddr. */
1844 maddr = __va_ul (pte_val(*pte)
1845 & _PAGE_PPN_MASK);
1846 (*flush_func)(maddr, maddr+ PAGE_SIZE);
1847 nbr_page++;
1852 //printk ("domain_cache_flush: %d %d pages\n", d->domain_id, nbr_page);
1855 #ifdef VERBOSE
1856 #define MEM_LOG(_f, _a...) \
1857 printk("DOM%u: (file=mm.c, line=%d) " _f "\n", \
1858 current->domain->domain_id , __LINE__ , ## _a )
1859 #else
1860 #define MEM_LOG(_f, _a...) ((void)0)
1861 #endif
1863 static void free_page_type(struct page_info *page, u32 type)
1867 static int alloc_page_type(struct page_info *page, u32 type)
1869 return 1;
1872 unsigned long __get_free_pages(unsigned int mask, unsigned int order)
1874 void *p = alloc_xenheap_pages(order);
1876 memset(p,0,PAGE_SIZE<<order);
1877 return (unsigned long)p;
1880 void __free_pages(struct page_info *page, unsigned int order)
1882 if (order) BUG();
1883 free_xenheap_page(page);
1886 void *pgtable_quicklist_alloc(void)
1888 void *p;
1889 p = alloc_xenheap_pages(0);
1890 if (p)
1891 clear_page(p);
1892 return p;
1895 void pgtable_quicklist_free(void *pgtable_entry)
1897 free_xenheap_page(pgtable_entry);
1900 void put_page_type(struct page_info *page)
1902 u32 nx, x, y = page->u.inuse.type_info;
1904 again:
1905 do {
1906 x = y;
1907 nx = x - 1;
1909 ASSERT((x & PGT_count_mask) != 0);
1911 /*
1912 * The page should always be validated while a reference is held. The
1913 * exception is during domain destruction, when we forcibly invalidate
1914 * page-table pages if we detect a referential loop.
1915 * See domain.c:relinquish_list().
1916 */
1917 ASSERT((x & PGT_validated) ||
1918 test_bit(_DOMF_dying, &page_get_owner(page)->domain_flags));
1920 if ( unlikely((nx & PGT_count_mask) == 0) )
1922 /* Record TLB information for flush later. Races are harmless. */
1923 page->tlbflush_timestamp = tlbflush_current_time();
1925 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
1926 likely(nx & PGT_validated) )
1928 /*
1929 * Page-table pages must be unvalidated when count is zero. The
1930 * 'free' is safe because the refcnt is non-zero and validated
1931 * bit is clear => other ops will spin or fail.
1932 */
1933 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
1934 x & ~PGT_validated)) != x) )
1935 goto again;
1936 /* We cleared the 'valid bit' so we do the clean up. */
1937 free_page_type(page, x);
1938 /* Carry on, but with the 'valid bit' now clear. */
1939 x &= ~PGT_validated;
1940 nx &= ~PGT_validated;
1944 while ( unlikely((y = cmpxchg_rel(&page->u.inuse.type_info, x, nx)) != x) );
1948 int get_page_type(struct page_info *page, u32 type)
1950 u32 nx, x, y = page->u.inuse.type_info;
1952 ASSERT(!(type & ~PGT_type_mask));
1954 again:
1955 do {
1956 x = y;
1957 nx = x + 1;
1958 if ( unlikely((nx & PGT_count_mask) == 0) )
1960 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
1961 return 0;
1963 else if ( unlikely((x & PGT_count_mask) == 0) )
1965 if ( (x & PGT_type_mask) != type )
1967 /*
1968 * On type change we check to flush stale TLB entries. This
1969 * may be unnecessary (e.g., page was GDT/LDT) but those
1970 * circumstances should be very rare.
1971 */
1972 cpumask_t mask =
1973 page_get_owner(page)->domain_dirty_cpumask;
1974 tlbflush_filter(mask, page->tlbflush_timestamp);
1976 if ( unlikely(!cpus_empty(mask)) )
1978 perfc_incrc(need_flush_tlb_flush);
1979 flush_tlb_mask(mask);
1982 /* We lose existing type, back pointer, and validity. */
1983 nx &= ~(PGT_type_mask | PGT_validated);
1984 nx |= type;
1986 /* No special validation needed for writable pages. */
1987 /* Page tables and GDT/LDT need to be scanned for validity. */
1988 if ( type == PGT_writable_page )
1989 nx |= PGT_validated;
1992 else if ( unlikely((x & PGT_type_mask) != type) )
1994 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
1995 (type != PGT_l1_page_table) )
1996 MEM_LOG("Bad type (saw %08x != exp %08x) "
1997 "for mfn %016lx (pfn %016lx)",
1998 x, type, page_to_mfn(page),
1999 get_gpfn_from_mfn(page_to_mfn(page)));
2000 return 0;
2002 else if ( unlikely(!(x & PGT_validated)) )
2004 /* Someone else is updating validation of this page. Wait... */
2005 while ( (y = page->u.inuse.type_info) == x )
2006 cpu_relax();
2007 goto again;
2010 while ( unlikely((y = cmpxchg_acq(&page->u.inuse.type_info, x, nx)) != x) );
2012 if ( unlikely(!(nx & PGT_validated)) )
2014 /* Try to validate page type; drop the new reference on failure. */
2015 if ( unlikely(!alloc_page_type(page, type)) )
2017 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %08x"
2018 ": caf=%08x taf=%" PRtype_info,
2019 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
2020 type, page->count_info, page->u.inuse.type_info);
2021 /* Noone else can get a reference. We hold the only ref. */
2022 page->u.inuse.type_info = 0;
2023 return 0;
2026 /* Noone else is updating simultaneously. */
2027 __set_bit(_PGT_validated, &page->u.inuse.type_info);
2030 return 1;
2033 int memory_is_conventional_ram(paddr_t p)
2035 return (efi_mem_type(p) == EFI_CONVENTIONAL_MEMORY);
2039 long
2040 arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
2042 switch (op) {
2043 case XENMEM_add_to_physmap:
2045 struct xen_add_to_physmap xatp;
2046 unsigned long prev_mfn, mfn = 0, gpfn;
2047 struct domain *d;
2049 if (copy_from_guest(&xatp, arg, 1))
2050 return -EFAULT;
2052 if (xatp.domid == DOMID_SELF) {
2053 d = current->domain;
2054 get_knownalive_domain(d);
2056 else if (!IS_PRIV(current->domain))
2057 return -EPERM;
2058 else if ((d = find_domain_by_id(xatp.domid)) == NULL)
2059 return -ESRCH;
2061 /* This hypercall is used for VT-i domain only */
2062 if (!VMX_DOMAIN(d->vcpu[0])) {
2063 put_domain(d);
2064 return -ENOSYS;
2067 switch (xatp.space) {
2068 case XENMAPSPACE_shared_info:
2069 if (xatp.idx == 0)
2070 mfn = virt_to_mfn(d->shared_info);
2071 break;
2072 case XENMAPSPACE_grant_table:
2073 if (xatp.idx < NR_GRANT_FRAMES)
2074 mfn = virt_to_mfn(d->grant_table->shared) + xatp.idx;
2075 break;
2076 default:
2077 break;
2080 LOCK_BIGLOCK(d);
2082 /* Remove previously mapped page if it was present. */
2083 prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
2084 if (prev_mfn && mfn_valid(prev_mfn)) {
2085 if (IS_XEN_HEAP_FRAME(mfn_to_page(prev_mfn)))
2086 /* Xen heap frames are simply unhooked from this phys slot. */
2087 guest_physmap_remove_page(d, xatp.gpfn, prev_mfn);
2088 else
2089 /* Normal domain memory is freed, to avoid leaking memory. */
2090 guest_remove_page(d, xatp.gpfn);
2093 /* Unmap from old location, if any. */
2094 gpfn = get_gpfn_from_mfn(mfn);
2095 if (gpfn != INVALID_M2P_ENTRY)
2096 guest_physmap_remove_page(d, gpfn, mfn);
2098 /* Map at new location. */
2099 guest_physmap_add_page(d, xatp.gpfn, mfn);
2101 UNLOCK_BIGLOCK(d);
2103 put_domain(d);
2105 break;
2108 default:
2109 return -ENOSYS;
2112 return 0;
2115 /*
2116 * Local variables:
2117 * mode: C
2118 * c-set-style: "BSD"
2119 * c-basic-offset: 4
2120 * tab-width: 4
2121 * indent-tabs-mode: nil
2122 * End:
2123 */