direct-io.hg

view xen/arch/ia64/xen/mm.c @ 11330:3e54734e55f3

[IA64] Remove extraneous verbose output to clean up Fedora boot.

Signed-off-by: Aron Griffis <aron@hp.com>
author awilliam@xenbuild.aw
date Wed Aug 23 13:26:46 2006 -0600 (2006-08-23)
parents 79afceca9065
children 965df40965e5 af50fb41612c
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()
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 <linux/efi.h>
177 static void domain_page_flush(struct domain* d, unsigned long mpaddr,
178 unsigned long old_mfn, unsigned long new_mfn);
180 extern unsigned long ia64_iobase;
182 static struct domain *dom_xen, *dom_io;
184 // followings are stolen from arch_init_memory() @ xen/arch/x86/mm.c
185 void
186 alloc_dom_xen_and_dom_io(void)
187 {
188 /*
189 * Initialise our DOMID_XEN domain.
190 * Any Xen-heap pages that we will allow to be mapped will have
191 * their domain field set to dom_xen.
192 */
193 dom_xen = alloc_domain(DOMID_XEN);
194 BUG_ON(dom_xen == NULL);
196 /*
197 * Initialise our DOMID_IO domain.
198 * This domain owns I/O pages that are within the range of the page_info
199 * array. Mappings occur at the priv of the caller.
200 */
201 dom_io = alloc_domain(DOMID_IO);
202 BUG_ON(dom_io == NULL);
203 }
205 // heavily depends on the struct page_info layout.
206 // if (page_get_owner(page) == d &&
207 // test_and_clear_bit(_PGC_allocated, &page->count_info)) {
208 // put_page(page);
209 // }
210 static void
211 try_to_clear_PGC_allocate(struct domain* d, struct page_info* page)
212 {
213 u32 _d, _nd;
214 u64 x, nx, y;
216 _d = pickle_domptr(d);
217 y = *((u64*)&page->count_info);
218 do {
219 x = y;
220 _nd = x >> 32;
221 nx = x - 1;
222 __clear_bit(_PGC_allocated, &nx);
224 if (unlikely(!(x & PGC_allocated)) || unlikely(_nd != _d)) {
225 struct domain* nd = unpickle_domptr(_nd);
226 if (nd == NULL) {
227 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
228 "sd=%p 0x%x,"
229 " caf=%016lx, taf=%" PRtype_info "\n",
230 (void *) page_to_mfn(page),
231 d, d->domain_id, _d,
232 nd, _nd,
233 x,
234 page->u.inuse.type_info);
235 }
236 break;
237 }
239 BUG_ON((nx & PGC_count_mask) < 1);
240 y = cmpxchg((u64*)&page->count_info, x, nx);
241 } while (unlikely(y != x));
242 }
244 static void
245 relinquish_pte(struct domain* d, pte_t* pte)
246 {
247 unsigned long mfn = pte_pfn(*pte);
248 struct page_info* page;
250 // vmx domain use bit[58:56] to distinguish io region from memory.
251 // see vmx_build_physmap_table() in vmx_init.c
252 if (!pte_mem(*pte))
253 return;
255 // domain might map IO space or acpi table pages. check it.
256 if (!mfn_valid(mfn))
257 return;
258 page = mfn_to_page(mfn);
259 // struct page_info corresponding to mfn may exist or not depending
260 // on CONFIG_VIRTUAL_FRAME_TABLE.
261 // This check is too easy.
262 // The right way is to check whether this page is of io area or acpi pages
263 if (page_get_owner(page) == NULL) {
264 BUG_ON(page->count_info != 0);
265 return;
266 }
268 if (page_get_owner(page) == d) {
269 BUG_ON(get_gpfn_from_mfn(mfn) == INVALID_M2P_ENTRY);
270 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
271 }
273 try_to_clear_PGC_allocate(d, page);
274 put_page(page);
275 }
277 static void
278 relinquish_pmd(struct domain* d, pmd_t* pmd, unsigned long offset)
279 {
280 unsigned long i;
281 pte_t* pte = pte_offset_map(pmd, offset);
283 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
284 if (!pte_present(*pte))
285 continue;
287 relinquish_pte(d, pte);
288 }
289 pte_free_kernel(pte_offset_map(pmd, offset));
290 }
292 static void
293 relinquish_pud(struct domain* d, pud_t *pud, unsigned long offset)
294 {
295 unsigned long i;
296 pmd_t *pmd = pmd_offset(pud, offset);
298 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
299 if (!pmd_present(*pmd))
300 continue;
302 relinquish_pmd(d, pmd, offset + (i << PMD_SHIFT));
303 }
304 pmd_free(pmd_offset(pud, offset));
305 }
307 static void
308 relinquish_pgd(struct domain* d, pgd_t *pgd, unsigned long offset)
309 {
310 unsigned long i;
311 pud_t *pud = pud_offset(pgd, offset);
313 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
314 if (!pud_present(*pud))
315 continue;
317 relinquish_pud(d, pud, offset + (i << PUD_SHIFT));
318 }
319 pud_free(pud_offset(pgd, offset));
320 }
322 void
323 relinquish_mm(struct domain* d)
324 {
325 struct mm_struct* mm = &d->arch.mm;
326 unsigned long i;
327 pgd_t* pgd;
329 if (mm->pgd == NULL)
330 return;
332 pgd = pgd_offset(mm, 0);
333 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
334 if (!pgd_present(*pgd))
335 continue;
337 relinquish_pgd(d, pgd, i << PGDIR_SHIFT);
338 }
339 pgd_free(mm->pgd);
340 mm->pgd = NULL;
341 }
343 // stolen from share_xen_page_with_guest() in xen/arch/x86/mm.c
344 void
345 share_xen_page_with_guest(struct page_info *page,
346 struct domain *d, int readonly)
347 {
348 if ( page_get_owner(page) == d )
349 return;
351 #if 1
352 if (readonly) {
353 printk("%s:%d readonly is not supported yet\n", __func__, __LINE__);
354 }
355 #endif
357 // alloc_xenheap_pages() doesn't initialize page owner.
358 //BUG_ON(page_get_owner(page) != NULL);
360 spin_lock(&d->page_alloc_lock);
362 #ifndef __ia64__
363 /* The incremented type count pins as writable or read-only. */
364 page->u.inuse.type_info = (readonly ? PGT_none : PGT_writable_page);
365 page->u.inuse.type_info |= PGT_validated | 1;
366 #endif
368 page_set_owner(page, d);
369 wmb(); /* install valid domain ptr before updating refcnt. */
370 ASSERT(page->count_info == 0);
371 page->count_info |= PGC_allocated | 1;
373 if ( unlikely(d->xenheap_pages++ == 0) )
374 get_knownalive_domain(d);
375 list_add_tail(&page->list, &d->xenpage_list);
377 // grant_table_destroy() releases these pages.
378 // but it doesn't clear their m2p entry. So there might remain stale
379 // entries. such a stale entry is cleared here.
380 set_gpfn_from_mfn(page_to_mfn(page), INVALID_M2P_ENTRY);
382 spin_unlock(&d->page_alloc_lock);
383 }
385 void
386 share_xen_page_with_privileged_guests(struct page_info *page, int readonly)
387 {
388 share_xen_page_with_guest(page, dom_xen, readonly);
389 }
391 unsigned long
392 gmfn_to_mfn_foreign(struct domain *d, unsigned long gpfn)
393 {
394 unsigned long pte;
396 pte = lookup_domain_mpa(d,gpfn << PAGE_SHIFT, NULL);
397 if (!pte) {
398 panic("gmfn_to_mfn_foreign: bad gpfn. spinning...\n");
399 }
400 return ((pte & _PFN_MASK) >> PAGE_SHIFT);
401 }
403 // given a domain virtual address, pte and pagesize, extract the metaphysical
404 // address, convert the pte for a physical address for (possibly different)
405 // Xen PAGE_SIZE and return modified pte. (NOTE: TLB insert should use
406 // PAGE_SIZE!)
407 u64 translate_domain_pte(u64 pteval, u64 address, u64 itir__, u64* logps,
408 struct p2m_entry* entry)
409 {
410 struct domain *d = current->domain;
411 ia64_itir_t itir = {.itir = itir__};
412 u64 mask, mpaddr, pteval2;
413 u64 arflags;
414 u64 arflags2;
415 u64 maflags2;
417 pteval &= ((1UL << 53) - 1);// ignore [63:53] bits
419 // FIXME address had better be pre-validated on insert
420 mask = ~itir_mask(itir.itir);
421 mpaddr = ((pteval & _PAGE_PPN_MASK) & ~mask) | (address & mask);
423 if (itir.ps > PAGE_SHIFT)
424 itir.ps = PAGE_SHIFT;
426 *logps = itir.ps;
428 pteval2 = lookup_domain_mpa(d, mpaddr, entry);
430 /* Check access rights. */
431 arflags = pteval & _PAGE_AR_MASK;
432 arflags2 = pteval2 & _PAGE_AR_MASK;
433 if (arflags != _PAGE_AR_R && arflags2 == _PAGE_AR_R) {
434 #if 0
435 DPRINTK("%s:%d "
436 "pteval 0x%lx arflag 0x%lx address 0x%lx itir 0x%lx "
437 "pteval2 0x%lx arflags2 0x%lx mpaddr 0x%lx\n",
438 __func__, __LINE__,
439 pteval, arflags, address, itir__,
440 pteval2, arflags2, mpaddr);
441 #endif
442 pteval = (pteval & ~_PAGE_AR_MASK) | _PAGE_AR_R;
443 }
445 /* Check memory attribute. The switch is on the *requested* memory
446 attribute. */
447 maflags2 = pteval2 & _PAGE_MA_MASK;
448 switch (pteval & _PAGE_MA_MASK) {
449 case _PAGE_MA_NAT:
450 /* NaT pages are always accepted! */
451 break;
452 case _PAGE_MA_UC:
453 case _PAGE_MA_UCE:
454 case _PAGE_MA_WC:
455 if (maflags2 == _PAGE_MA_WB) {
456 /* Don't let domains WB-map uncached addresses.
457 This can happen when domU tries to touch i/o
458 port space. Also prevents possible address
459 aliasing issues. */
460 printf("Warning: UC to WB for mpaddr=%lx\n", mpaddr);
461 pteval = (pteval & ~_PAGE_MA_MASK) | _PAGE_MA_WB;
462 }
463 break;
464 case _PAGE_MA_WB:
465 if (maflags2 != _PAGE_MA_WB) {
466 /* Forbid non-coherent access to coherent memory. */
467 panic_domain(NULL, "try to use WB mem attr on "
468 "UC page, mpaddr=%lx\n", mpaddr);
469 }
470 break;
471 default:
472 panic_domain(NULL, "try to use unknown mem attribute\n");
473 }
475 /* If shadow mode is enabled, virtualize dirty bit. */
476 if (shadow_mode_enabled(d) && (pteval & _PAGE_D)) {
477 u64 mp_page = mpaddr >> PAGE_SHIFT;
478 pteval |= _PAGE_VIRT_D;
480 /* If the page is not already dirty, don't set the dirty bit! */
481 if (mp_page < d->arch.shadow_bitmap_size * 8
482 && !test_bit(mp_page, d->arch.shadow_bitmap))
483 pteval &= ~_PAGE_D;
484 }
486 /* Ignore non-addr bits of pteval2 and force PL0->2
487 (PL3 is unaffected) */
488 return (pteval & ~_PAGE_PPN_MASK) |
489 (pteval2 & _PAGE_PPN_MASK) | _PAGE_PL_2;
490 }
492 // given a current domain metaphysical address, return the physical address
493 unsigned long translate_domain_mpaddr(unsigned long mpaddr,
494 struct p2m_entry* entry)
495 {
496 unsigned long pteval;
498 pteval = lookup_domain_mpa(current->domain, mpaddr, entry);
499 return ((pteval & _PAGE_PPN_MASK) | (mpaddr & ~PAGE_MASK));
500 }
502 //XXX !xxx_present() should be used instread of !xxx_none()?
503 // __assign_new_domain_page(), assign_new_domain_page() and
504 // assign_new_domain0_page() are used only when domain creation.
505 // their accesses aren't racy so that returned pte_t doesn't need
506 // volatile qualifier
507 static pte_t*
508 __lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
509 {
510 struct mm_struct *mm = &d->arch.mm;
511 pgd_t *pgd;
512 pud_t *pud;
513 pmd_t *pmd;
515 BUG_ON(mm->pgd == NULL);
516 pgd = pgd_offset(mm, mpaddr);
517 if (pgd_none(*pgd)) {
518 pgd_populate(mm, pgd, pud_alloc_one(mm,mpaddr));
519 }
521 pud = pud_offset(pgd, mpaddr);
522 if (pud_none(*pud)) {
523 pud_populate(mm, pud, pmd_alloc_one(mm,mpaddr));
524 }
526 pmd = pmd_offset(pud, mpaddr);
527 if (pmd_none(*pmd)) {
528 pmd_populate_kernel(mm, pmd, pte_alloc_one_kernel(mm, mpaddr));
529 }
531 return pte_offset_map(pmd, mpaddr);
532 }
534 //XXX !xxx_present() should be used instread of !xxx_none()?
535 // pud, pmd, pte page is zero cleared when they are allocated.
536 // Their area must be visible before population so that
537 // cmpxchg must have release semantics.
538 static volatile pte_t*
539 lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
540 {
541 struct mm_struct *mm = &d->arch.mm;
542 pgd_t *pgd;
543 pud_t *pud;
544 pmd_t *pmd;
546 BUG_ON(mm->pgd == NULL);
548 pgd = pgd_offset(mm, mpaddr);
549 again_pgd:
550 if (unlikely(pgd_none(*pgd))) {
551 pud_t *old_pud = NULL;
552 pud = pud_alloc_one(mm, mpaddr);
553 if (unlikely(!pgd_cmpxchg_rel(mm, pgd, old_pud, pud))) {
554 pud_free(pud);
555 goto again_pgd;
556 }
557 }
559 pud = pud_offset(pgd, mpaddr);
560 again_pud:
561 if (unlikely(pud_none(*pud))) {
562 pmd_t* old_pmd = NULL;
563 pmd = pmd_alloc_one(mm, mpaddr);
564 if (unlikely(!pud_cmpxchg_rel(mm, pud, old_pmd, pmd))) {
565 pmd_free(pmd);
566 goto again_pud;
567 }
568 }
570 pmd = pmd_offset(pud, mpaddr);
571 again_pmd:
572 if (unlikely(pmd_none(*pmd))) {
573 pte_t* old_pte = NULL;
574 pte_t* pte = pte_alloc_one_kernel(mm, mpaddr);
575 if (unlikely(!pmd_cmpxchg_kernel_rel(mm, pmd, old_pte, pte))) {
576 pte_free_kernel(pte);
577 goto again_pmd;
578 }
579 }
581 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
582 }
584 //XXX xxx_none() should be used instread of !xxx_present()?
585 volatile pte_t*
586 lookup_noalloc_domain_pte(struct domain* d, unsigned long mpaddr)
587 {
588 struct mm_struct *mm = &d->arch.mm;
589 pgd_t *pgd;
590 pud_t *pud;
591 pmd_t *pmd;
593 BUG_ON(mm->pgd == NULL);
594 pgd = pgd_offset(mm, mpaddr);
595 if (unlikely(!pgd_present(*pgd)))
596 return NULL;
598 pud = pud_offset(pgd, mpaddr);
599 if (unlikely(!pud_present(*pud)))
600 return NULL;
602 pmd = pmd_offset(pud, mpaddr);
603 if (unlikely(!pmd_present(*pmd)))
604 return NULL;
606 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
607 }
609 static volatile pte_t*
610 lookup_noalloc_domain_pte_none(struct domain* d, unsigned long mpaddr)
611 {
612 struct mm_struct *mm = &d->arch.mm;
613 pgd_t *pgd;
614 pud_t *pud;
615 pmd_t *pmd;
617 BUG_ON(mm->pgd == NULL);
618 pgd = pgd_offset(mm, mpaddr);
619 if (unlikely(pgd_none(*pgd)))
620 return NULL;
622 pud = pud_offset(pgd, mpaddr);
623 if (unlikely(pud_none(*pud)))
624 return NULL;
626 pmd = pmd_offset(pud, mpaddr);
627 if (unlikely(pmd_none(*pmd)))
628 return NULL;
630 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
631 }
633 unsigned long
634 ____lookup_domain_mpa(struct domain *d, unsigned long mpaddr)
635 {
636 volatile pte_t *pte;
638 pte = lookup_noalloc_domain_pte(d, mpaddr);
639 if (pte == NULL)
640 return INVALID_MFN;
642 if (pte_present(*pte))
643 return (pte->pte & _PFN_MASK);
644 else if (VMX_DOMAIN(d->vcpu[0]))
645 return GPFN_INV_MASK;
646 return INVALID_MFN;
647 }
649 unsigned long lookup_domain_mpa(struct domain *d, unsigned long mpaddr,
650 struct p2m_entry* entry)
651 {
652 volatile pte_t *pte = lookup_noalloc_domain_pte(d, mpaddr);
654 if (pte != NULL) {
655 pte_t tmp_pte = *pte;// pte is volatile. copy the value.
656 if (pte_present(tmp_pte)) {
657 //printk("lookup_domain_page: found mapping for %lx, pte=%lx\n",mpaddr,pte_val(*pte));
658 if (entry != NULL)
659 p2m_entry_set(entry, pte, tmp_pte);
660 return pte_val(tmp_pte);
661 } else if (VMX_DOMAIN(d->vcpu[0]))
662 return GPFN_INV_MASK;
663 }
665 printk("%s: d 0x%p id %d current 0x%p id %d\n",
666 __func__, d, d->domain_id, current, current->vcpu_id);
667 if ((mpaddr >> PAGE_SHIFT) < d->max_pages)
668 printk("%s: non-allocated mpa 0x%lx (< 0x%lx)\n", __func__,
669 mpaddr, (unsigned long)d->max_pages << PAGE_SHIFT);
670 else
671 printk("%s: bad mpa 0x%lx (=> 0x%lx)\n", __func__,
672 mpaddr, (unsigned long)d->max_pages << PAGE_SHIFT);
674 if (entry != NULL)
675 p2m_entry_set(entry, NULL, __pte(0));
676 //XXX This is a work around until the emulation memory access to a region
677 // where memory or device are attached is implemented.
678 return pte_val(pfn_pte(0, __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX)));
679 }
681 // FIXME: ONLY USE FOR DOMAIN PAGE_SIZE == PAGE_SIZE
682 #if 1
683 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
684 {
685 unsigned long pte = lookup_domain_mpa(d, mpaddr, NULL);
686 unsigned long imva;
688 pte &= _PAGE_PPN_MASK;
689 imva = (unsigned long) __va(pte);
690 imva |= mpaddr & ~PAGE_MASK;
691 return (void*)imva;
692 }
693 #else
694 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
695 {
696 unsigned long imva = __gpa_to_mpa(d, mpaddr);
698 return (void *)__va(imva);
699 }
700 #endif
702 /* Allocate a new page for domain and map it to the specified metaphysical
703 address. */
704 static struct page_info *
705 __assign_new_domain_page(struct domain *d, unsigned long mpaddr, pte_t* pte)
706 {
707 struct page_info *p;
708 unsigned long maddr;
709 int ret;
711 BUG_ON(!pte_none(*pte));
713 p = alloc_domheap_page(d);
714 if (unlikely(!p)) {
715 printf("assign_new_domain_page: Can't alloc!!!! Aaaargh!\n");
716 return(p);
717 }
719 // zero out pages for security reasons
720 clear_page(page_to_virt(p));
721 maddr = page_to_maddr (p);
722 if (unlikely(maddr > __get_cpu_var(vhpt_paddr)
723 && maddr < __get_cpu_var(vhpt_pend))) {
724 /* FIXME: how can this happen ?
725 vhpt is allocated by alloc_domheap_page. */
726 printf("assign_new_domain_page: reassigned vhpt page %lx!!\n",
727 maddr);
728 }
730 ret = get_page(p, d);
731 BUG_ON(ret == 0);
732 set_gpfn_from_mfn(page_to_mfn(p), mpaddr >> PAGE_SHIFT);
733 // clear_page() and set_gpfn_from_mfn() become visible before set_pte_rel()
734 // because set_pte_rel() has release semantics
735 set_pte_rel(pte,
736 pfn_pte(maddr >> PAGE_SHIFT,
737 __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX)));
739 smp_mb();
740 return p;
741 }
743 struct page_info *
744 assign_new_domain_page(struct domain *d, unsigned long mpaddr)
745 {
746 pte_t *pte = __lookup_alloc_domain_pte(d, mpaddr);
748 if (!pte_none(*pte))
749 return NULL;
751 return __assign_new_domain_page(d, mpaddr, pte);
752 }
754 void
755 assign_new_domain0_page(struct domain *d, unsigned long mpaddr)
756 {
757 pte_t *pte;
759 BUG_ON(d != dom0);
760 pte = __lookup_alloc_domain_pte(d, mpaddr);
761 if (pte_none(*pte)) {
762 struct page_info *p = __assign_new_domain_page(d, mpaddr, pte);
763 if (p == NULL) {
764 panic("%s: can't allocate page for dom0", __func__);
765 }
766 }
767 }
769 static unsigned long
770 flags_to_prot (unsigned long flags)
771 {
772 unsigned long res = _PAGE_PL_2 | __DIRTY_BITS;
774 res |= flags & ASSIGN_readonly ? _PAGE_AR_R: _PAGE_AR_RWX;
775 res |= flags & ASSIGN_nocache ? _PAGE_MA_UC: _PAGE_MA_WB;
777 return res;
778 }
780 /* map a physical address to the specified metaphysical addr */
781 // flags: currently only ASSIGN_readonly, ASSIGN_nocache
782 // This is called by assign_domain_mmio_page().
783 // So accessing to pte is racy.
784 void
785 __assign_domain_page(struct domain *d,
786 unsigned long mpaddr, unsigned long physaddr,
787 unsigned long flags)
788 {
789 volatile pte_t *pte;
790 pte_t old_pte;
791 pte_t new_pte;
792 pte_t ret_pte;
793 unsigned long prot = flags_to_prot(flags);
795 pte = lookup_alloc_domain_pte(d, mpaddr);
797 old_pte = __pte(0);
798 new_pte = pfn_pte(physaddr >> PAGE_SHIFT, __pgprot(prot));
799 ret_pte = ptep_cmpxchg_rel(&d->arch.mm, mpaddr, pte, old_pte, new_pte);
800 if (pte_val(ret_pte) == pte_val(old_pte))
801 smp_mb();
802 }
804 /* get_page() and map a physical address to the specified metaphysical addr */
805 void
806 assign_domain_page(struct domain *d,
807 unsigned long mpaddr, unsigned long physaddr)
808 {
809 struct page_info* page = mfn_to_page(physaddr >> PAGE_SHIFT);
810 int ret;
812 BUG_ON((physaddr & GPFN_IO_MASK) != GPFN_MEM);
813 ret = get_page(page, d);
814 BUG_ON(ret == 0);
815 set_gpfn_from_mfn(physaddr >> PAGE_SHIFT, mpaddr >> PAGE_SHIFT);
816 // because __assign_domain_page() uses set_pte_rel() which has
817 // release semantics, smp_mb() isn't needed.
818 __assign_domain_page(d, mpaddr, physaddr, ASSIGN_writable);
819 }
821 int
822 ioports_permit_access(struct domain *d, unsigned long fp, unsigned long lp)
823 {
824 int ret;
825 unsigned long off;
826 unsigned long fp_offset;
827 unsigned long lp_offset;
829 ret = rangeset_add_range(d->arch.ioport_caps, fp, lp);
830 if (ret != 0)
831 return ret;
833 /* Domain 0 doesn't virtualize IO ports space. */
834 if (d == dom0)
835 return 0;
837 fp_offset = IO_SPACE_SPARSE_ENCODING(fp) & ~PAGE_MASK;
838 lp_offset = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lp));
840 for (off = fp_offset; off <= lp_offset; off += PAGE_SIZE)
841 __assign_domain_page(d, IO_PORTS_PADDR + off,
842 __pa(ia64_iobase) + off, ASSIGN_nocache);
844 return 0;
845 }
847 static int
848 ioports_has_allowed(struct domain *d, unsigned long fp, unsigned long lp)
849 {
850 unsigned long i;
851 for (i = fp; i < lp; i++)
852 if (rangeset_contains_singleton(d->arch.ioport_caps, i))
853 return 1;
854 return 0;
855 }
857 int
858 ioports_deny_access(struct domain *d, unsigned long fp, unsigned long lp)
859 {
860 int ret;
861 struct mm_struct *mm = &d->arch.mm;
862 unsigned long off;
863 unsigned long io_ports_base;
864 unsigned long fp_offset;
865 unsigned long lp_offset;
867 ret = rangeset_remove_range(d->arch.ioport_caps, fp, lp);
868 if (ret != 0)
869 return ret;
870 if (d == dom0)
871 io_ports_base = __pa(ia64_iobase);
872 else
873 io_ports_base = IO_PORTS_PADDR;
875 fp_offset = IO_SPACE_SPARSE_ENCODING(fp) & PAGE_MASK;
876 lp_offset = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lp));
878 for (off = fp_offset; off < lp_offset; off += PAGE_SIZE) {
879 unsigned long mpaddr = io_ports_base + off;
880 unsigned long port;
881 volatile pte_t *pte;
882 pte_t old_pte;
884 port = IO_SPACE_SPARSE_DECODING (off);
885 if (port < fp || port + IO_SPACE_SPARSE_PORTS_PER_PAGE - 1 > lp) {
886 /* Maybe this covers an allowed port. */
887 if (ioports_has_allowed(d, port,
888 port + IO_SPACE_SPARSE_PORTS_PER_PAGE - 1))
889 continue;
890 }
892 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
893 BUG_ON(pte == NULL);
894 BUG_ON(pte_none(*pte));
896 // clear pte
897 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
898 }
899 domain_flush_vtlb_all();
900 return 0;
901 }
903 static void
904 assign_domain_same_page(struct domain *d,
905 unsigned long mpaddr, unsigned long size,
906 unsigned long flags)
907 {
908 //XXX optimization
909 unsigned long end = PAGE_ALIGN(mpaddr + size);
910 for (mpaddr &= PAGE_MASK; mpaddr < end; mpaddr += PAGE_SIZE) {
911 __assign_domain_page(d, mpaddr, mpaddr, flags);
912 }
913 }
915 int
916 efi_mmio(unsigned long physaddr, unsigned long size)
917 {
918 void *efi_map_start, *efi_map_end;
919 u64 efi_desc_size;
920 void* p;
922 efi_map_start = __va(ia64_boot_param->efi_memmap);
923 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
924 efi_desc_size = ia64_boot_param->efi_memdesc_size;
926 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
927 efi_memory_desc_t* md = (efi_memory_desc_t *)p;
928 unsigned long start = md->phys_addr;
929 unsigned long end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
931 if (start <= physaddr && physaddr < end) {
932 if ((physaddr + size) > end) {
933 DPRINTK("%s:%d physaddr 0x%lx size = 0x%lx\n",
934 __func__, __LINE__, physaddr, size);
935 return 0;
936 }
938 // for io space
939 if (md->type == EFI_MEMORY_MAPPED_IO ||
940 md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
941 return 1;
942 }
944 // for runtime
945 // see efi_enter_virtual_mode(void)
946 // in linux/arch/ia64/kernel/efi.c
947 if ((md->attribute & EFI_MEMORY_RUNTIME) &&
948 !(md->attribute & EFI_MEMORY_WB)) {
949 return 1;
950 }
952 return 0;
953 }
955 if (physaddr < start) {
956 break;
957 }
958 }
960 return 1;
961 }
963 unsigned long
964 assign_domain_mmio_page(struct domain *d,
965 unsigned long mpaddr, unsigned long size)
966 {
967 if (size == 0) {
968 DPRINTK("%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
969 __func__, d, mpaddr, size);
970 }
971 if (!efi_mmio(mpaddr, size)) {
972 #ifndef NDEBUG
973 DPRINTK("%s:%d domain %p mpaddr 0x%lx size = 0x%lx\n",
974 __func__, __LINE__, d, mpaddr, size);
975 #endif
976 return -EINVAL;
977 }
978 assign_domain_same_page(d, mpaddr, size, ASSIGN_writable | ASSIGN_nocache);
979 return mpaddr;
980 }
982 unsigned long
983 assign_domain_mach_page(struct domain *d,
984 unsigned long mpaddr, unsigned long size,
985 unsigned long flags)
986 {
987 assign_domain_same_page(d, mpaddr, size, flags);
988 return mpaddr;
989 }
991 // caller must get_page(mfn_to_page(mfn)) before call.
992 // caller must call set_gpfn_from_mfn() before call if necessary.
993 // because set_gpfn_from_mfn() result must be visible before pte xchg
994 // caller must use memory barrier. NOTE: xchg has acquire semantics.
995 // flags: currently only ASSIGN_readonly
996 static void
997 assign_domain_page_replace(struct domain *d, unsigned long mpaddr,
998 unsigned long mfn, unsigned long flags)
999 {
1000 struct mm_struct *mm = &d->arch.mm;
1001 volatile pte_t* pte;
1002 pte_t old_pte;
1003 pte_t npte;
1004 unsigned long prot = flags_to_prot(flags);
1006 pte = lookup_alloc_domain_pte(d, mpaddr);
1008 // update pte
1009 npte = pfn_pte(mfn, __pgprot(prot));
1010 old_pte = ptep_xchg(mm, mpaddr, pte, npte);
1011 if (pte_mem(old_pte)) {
1012 unsigned long old_mfn = pte_pfn(old_pte);
1014 // mfn = old_mfn case can happen when domain maps a granted page
1015 // twice with the same pseudo physial address.
1016 // It's non sense, but allowed.
1017 // __gnttab_map_grant_ref()
1018 // => create_host_mapping()
1019 // => assign_domain_page_replace()
1020 if (mfn != old_mfn) {
1021 struct page_info* old_page = mfn_to_page(old_mfn);
1023 if (page_get_owner(old_page) == d ||
1024 page_get_owner(old_page) == NULL) {
1025 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
1026 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
1029 domain_page_flush(d, mpaddr, old_mfn, mfn);
1031 try_to_clear_PGC_allocate(d, old_page);
1032 put_page(old_page);
1037 // caller must get_page(new_page) before
1038 // Only steal_page() calls this function.
1039 static int
1040 assign_domain_page_cmpxchg_rel(struct domain* d, unsigned long mpaddr,
1041 struct page_info* old_page,
1042 struct page_info* new_page,
1043 unsigned long flags)
1045 struct mm_struct *mm = &d->arch.mm;
1046 volatile pte_t* pte;
1047 unsigned long old_mfn;
1048 unsigned long old_arflags;
1049 pte_t old_pte;
1050 unsigned long new_mfn;
1051 unsigned long new_prot;
1052 pte_t new_pte;
1053 pte_t ret_pte;
1055 pte = lookup_alloc_domain_pte(d, mpaddr);
1057 again:
1058 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1059 old_mfn = page_to_mfn(old_page);
1060 old_pte = pfn_pte(old_mfn, __pgprot(old_arflags));
1061 if (!pte_present(old_pte)) {
1062 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx old_mfn 0x%lx\n",
1063 __func__, pte_val(old_pte), old_arflags, old_mfn);
1064 return -EINVAL;
1067 new_prot = flags_to_prot(flags);
1068 new_mfn = page_to_mfn(new_page);
1069 new_pte = pfn_pte(new_mfn, __pgprot(new_prot));
1071 // update pte
1072 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1073 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1074 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1075 goto again;
1078 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx old_mfn 0x%lx "
1079 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1080 __func__,
1081 pte_val(old_pte), old_arflags, old_mfn,
1082 pte_val(ret_pte), pte_pfn(ret_pte));
1083 return -EINVAL;
1086 BUG_ON(!pte_mem(old_pte));
1087 BUG_ON(page_get_owner(old_page) != d);
1088 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
1089 BUG_ON(old_mfn == new_mfn);
1091 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
1093 domain_page_flush(d, mpaddr, old_mfn, new_mfn);
1094 put_page(old_page);
1095 return 0;
1098 static void
1099 zap_domain_page_one(struct domain *d, unsigned long mpaddr, unsigned long mfn)
1101 struct mm_struct *mm = &d->arch.mm;
1102 volatile pte_t *pte;
1103 pte_t old_pte;
1104 struct page_info *page;
1106 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
1107 if (pte == NULL)
1108 return;
1109 if (pte_none(*pte))
1110 return;
1112 if (mfn == INVALID_MFN) {
1113 // clear pte
1114 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
1115 mfn = pte_pfn(old_pte);
1116 } else {
1117 unsigned long old_arflags;
1118 pte_t new_pte;
1119 pte_t ret_pte;
1121 again:
1122 // memory_exchange() calls guest_physmap_remove_page() with
1123 // a stealed page. i.e. page owner = NULL.
1124 BUG_ON(page_get_owner(mfn_to_page(mfn)) != d &&
1125 page_get_owner(mfn_to_page(mfn)) != NULL);
1126 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1127 old_pte = pfn_pte(mfn, __pgprot(old_arflags));
1128 new_pte = __pte(0);
1130 // update pte
1131 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1132 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1133 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1134 goto again;
1137 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx mfn 0x%lx "
1138 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1139 __func__,
1140 pte_val(old_pte), old_arflags, mfn,
1141 pte_val(ret_pte), pte_pfn(ret_pte));
1142 return;
1144 BUG_ON(mfn != pte_pfn(ret_pte));
1147 page = mfn_to_page(mfn);
1148 BUG_ON((page->count_info & PGC_count_mask) == 0);
1150 if (page_get_owner(page) == d ||
1151 page_get_owner(page) == NULL) {
1152 // exchange_memory() calls
1153 // steal_page()
1154 // page owner is set to NULL
1155 // guest_physmap_remove_page()
1156 // zap_domain_page_one()
1157 BUG_ON(get_gpfn_from_mfn(mfn) != (mpaddr >> PAGE_SHIFT));
1158 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
1161 domain_page_flush(d, mpaddr, mfn, INVALID_MFN);
1163 if (page_get_owner(page) != NULL) {
1164 try_to_clear_PGC_allocate(d, page);
1166 put_page(page);
1169 unsigned long
1170 dom0vp_zap_physmap(struct domain *d, unsigned long gpfn,
1171 unsigned int extent_order)
1173 if (extent_order != 0) {
1174 //XXX
1175 return -ENOSYS;
1178 zap_domain_page_one(d, gpfn << PAGE_SHIFT, INVALID_MFN);
1179 return 0;
1182 unsigned long
1183 dom0vp_add_physmap(struct domain* d, unsigned long gpfn, unsigned long mfn,
1184 unsigned long flags, domid_t domid)
1186 int error = 0;
1187 struct domain* rd;
1189 /* Not allowed by a domain. */
1190 if (flags & ASSIGN_nocache)
1191 return -EINVAL;
1193 rd = find_domain_by_id(domid);
1194 if (unlikely(rd == NULL)) {
1195 switch (domid) {
1196 case DOMID_XEN:
1197 rd = dom_xen;
1198 break;
1199 case DOMID_IO:
1200 rd = dom_io;
1201 break;
1202 default:
1203 DPRINTK("d 0x%p domid %d "
1204 "pgfn 0x%lx mfn 0x%lx flags 0x%lx domid %d\n",
1205 d, d->domain_id, gpfn, mfn, flags, domid);
1206 return -ESRCH;
1208 BUG_ON(rd == NULL);
1209 get_knownalive_domain(rd);
1212 if (unlikely(rd == d || !mfn_valid(mfn))) {
1213 error = -EINVAL;
1214 goto out1;
1216 if (unlikely(get_page(mfn_to_page(mfn), rd) == 0)) {
1217 error = -EINVAL;
1218 goto out1;
1220 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1221 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1222 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, flags);
1223 //don't update p2m table because this page belongs to rd, not d.
1224 out1:
1225 put_domain(rd);
1226 return error;
1229 // grant table host mapping
1230 // mpaddr: host_addr: pseudo physical address
1231 // mfn: frame: machine page frame
1232 // flags: GNTMAP_readonly | GNTMAP_application_map | GNTMAP_contains_pte
1233 int
1234 create_grant_host_mapping(unsigned long gpaddr,
1235 unsigned long mfn, unsigned int flags)
1237 struct domain* d = current->domain;
1238 struct page_info* page;
1239 int ret;
1241 if (flags & (GNTMAP_device_map |
1242 GNTMAP_application_map | GNTMAP_contains_pte)) {
1243 DPRINTK("%s: flags 0x%x\n", __func__, flags);
1244 return GNTST_general_error;
1247 BUG_ON(!mfn_valid(mfn));
1248 page = mfn_to_page(mfn);
1249 ret = get_page(page, page_get_owner(page));
1250 BUG_ON(ret == 0);
1251 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1252 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1253 assign_domain_page_replace(d, gpaddr, mfn, (flags & GNTMAP_readonly)?
1254 ASSIGN_readonly: ASSIGN_writable);
1255 return GNTST_okay;
1258 // grant table host unmapping
1259 int
1260 destroy_grant_host_mapping(unsigned long gpaddr,
1261 unsigned long mfn, unsigned int flags)
1263 struct domain* d = current->domain;
1264 volatile pte_t* pte;
1265 unsigned long cur_arflags;
1266 pte_t cur_pte;
1267 pte_t new_pte;
1268 pte_t old_pte;
1269 struct page_info* page;
1271 if (flags & (GNTMAP_application_map | GNTMAP_contains_pte)) {
1272 DPRINTK("%s: flags 0x%x\n", __func__, flags);
1273 return GNTST_general_error;
1276 pte = lookup_noalloc_domain_pte(d, gpaddr);
1277 if (pte == NULL) {
1278 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx\n", __func__, gpaddr, mfn);
1279 return GNTST_general_error;
1282 again:
1283 cur_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1284 cur_pte = pfn_pte(mfn, __pgprot(cur_arflags));
1285 if (!pte_present(cur_pte)) {
1286 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx\n",
1287 __func__, gpaddr, mfn, pte_val(cur_pte));
1288 return GNTST_general_error;
1290 new_pte = __pte(0);
1292 old_pte = ptep_cmpxchg_rel(&d->arch.mm, gpaddr, pte, cur_pte, new_pte);
1293 if (unlikely(!pte_present(old_pte))) {
1294 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx old_pte 0x%lx\n",
1295 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1296 return GNTST_general_error;
1298 if (unlikely(pte_val(cur_pte) != pte_val(old_pte))) {
1299 if (pte_pfn(old_pte) == mfn) {
1300 goto again;
1302 DPRINTK("%s gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx old_pte 0x%lx\n",
1303 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1304 return GNTST_general_error;
1306 BUG_ON(pte_pfn(old_pte) != mfn);
1308 domain_page_flush(d, gpaddr, mfn, INVALID_MFN);
1310 page = mfn_to_page(mfn);
1311 BUG_ON(page_get_owner(page) == d);//try_to_clear_PGC_allocate(d, page) is not needed.
1312 put_page(page);
1314 return GNTST_okay;
1317 // heavily depends on the struct page layout.
1318 // gnttab_transfer() calls steal_page() with memflags = 0
1319 // For grant table transfer, we must fill the page.
1320 // memory_exchange() calls steal_page() with memflags = MEMF_no_refcount
1321 // For memory exchange, we don't have to fill the page because
1322 // memory_exchange() does it.
1323 int
1324 steal_page(struct domain *d, struct page_info *page, unsigned int memflags)
1326 #if 0 /* if big endian */
1327 # error "implement big endian version of steal_page()"
1328 #endif
1329 u32 _d, _nd;
1330 u64 x, nx, y;
1332 if (page_get_owner(page) != d) {
1333 DPRINTK("%s d 0x%p owner 0x%p\n", __func__, d, page_get_owner(page));
1334 return -1;
1337 if (!(memflags & MEMF_no_refcount)) {
1338 unsigned long gpfn;
1339 struct page_info *new;
1340 unsigned long new_mfn;
1341 int ret;
1343 new = alloc_domheap_page(d);
1344 if (new == NULL) {
1345 DPRINTK("alloc_domheap_page() failed\n");
1346 return -1;
1348 // zero out pages for security reasons
1349 clear_page(page_to_virt(new));
1350 // assign_domain_page_cmpxchg_rel() has release semantics
1351 // so smp_mb() isn't needed.
1353 ret = get_page(new, d);
1354 BUG_ON(ret == 0);
1356 gpfn = get_gpfn_from_mfn(page_to_mfn(page));
1357 if (gpfn == INVALID_M2P_ENTRY) {
1358 free_domheap_page(new);
1359 return -1;
1361 new_mfn = page_to_mfn(new);
1362 set_gpfn_from_mfn(new_mfn, gpfn);
1363 // smp_mb() isn't needed because assign_domain_pge_cmpxchg_rel()
1364 // has release semantics.
1366 ret = assign_domain_page_cmpxchg_rel(d, gpfn << PAGE_SHIFT, page, new,
1367 ASSIGN_writable);
1368 if (ret < 0) {
1369 DPRINTK("assign_domain_page_cmpxchg_rel failed %d\n", ret);
1370 set_gpfn_from_mfn(new_mfn, INVALID_M2P_ENTRY);
1371 free_domheap_page(new);
1372 return -1;
1376 spin_lock(&d->page_alloc_lock);
1378 /*
1379 * The tricky bit: atomically release ownership while there is just one
1380 * benign reference to the page (PGC_allocated). If that reference
1381 * disappears then the deallocation routine will safely spin.
1382 */
1383 _d = pickle_domptr(d);
1384 y = *((u64*)&page->count_info);
1385 do {
1386 x = y;
1387 nx = x & 0xffffffff;
1388 // page->count_info: untouched
1389 // page->u.inused._domain = 0;
1390 _nd = x >> 32;
1392 if (unlikely(!(memflags & MEMF_no_refcount) &&
1393 ((x & (PGC_count_mask | PGC_allocated)) !=
1394 (1 | PGC_allocated))) ||
1396 // when MEMF_no_refcount, page isn't de-assigned from
1397 // this domain yet. So count_info = 2
1398 unlikely((memflags & MEMF_no_refcount) &&
1399 ((x & (PGC_count_mask | PGC_allocated)) !=
1400 (2 | PGC_allocated))) ||
1402 unlikely(_nd != _d)) {
1403 struct domain* nd = unpickle_domptr(_nd);
1404 if (nd == NULL) {
1405 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
1406 "sd=%p 0x%x,"
1407 " caf=%016lx, taf=%" PRtype_info
1408 " memflags 0x%x\n",
1409 (void *) page_to_mfn(page),
1410 d, d->domain_id, _d,
1411 nd, _nd,
1412 x,
1413 page->u.inuse.type_info,
1414 memflags);
1415 } else {
1416 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
1417 "sd=%p(%u) 0x%x,"
1418 " caf=%016lx, taf=%" PRtype_info
1419 " memflags 0x%x\n",
1420 (void *) page_to_mfn(page),
1421 d, d->domain_id, _d,
1422 nd, nd->domain_id, _nd,
1423 x,
1424 page->u.inuse.type_info,
1425 memflags);
1427 spin_unlock(&d->page_alloc_lock);
1428 return -1;
1431 y = cmpxchg((u64*)&page->count_info, x, nx);
1432 } while (unlikely(y != x));
1434 /*
1435 * Unlink from 'd'. At least one reference remains (now anonymous), so
1436 * noone else is spinning to try to delete this page from 'd'.
1437 */
1438 if ( !(memflags & MEMF_no_refcount) )
1439 d->tot_pages--;
1440 list_del(&page->list);
1442 spin_unlock(&d->page_alloc_lock);
1443 return 0;
1446 void
1447 guest_physmap_add_page(struct domain *d, unsigned long gpfn,
1448 unsigned long mfn)
1450 int ret;
1452 BUG_ON(!mfn_valid(mfn));
1453 ret = get_page(mfn_to_page(mfn), d);
1454 BUG_ON(ret == 0);
1455 set_gpfn_from_mfn(mfn, gpfn);
1456 smp_mb();
1457 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, ASSIGN_writable);
1459 //BUG_ON(mfn != ((lookup_domain_mpa(d, gpfn << PAGE_SHIFT) & _PFN_MASK) >> PAGE_SHIFT));
1462 void
1463 guest_physmap_remove_page(struct domain *d, unsigned long gpfn,
1464 unsigned long mfn)
1466 BUG_ON(mfn == 0);//XXX
1467 zap_domain_page_one(d, gpfn << PAGE_SHIFT, mfn);
1470 //XXX sledgehammer.
1471 // flush finer range.
1472 static void
1473 domain_page_flush(struct domain* d, unsigned long mpaddr,
1474 unsigned long old_mfn, unsigned long new_mfn)
1476 if (shadow_mode_enabled(d))
1477 shadow_mark_page_dirty(d, mpaddr >> PAGE_SHIFT);
1479 domain_flush_vtlb_all();
1482 int
1483 domain_page_mapped(struct domain* d, unsigned long mpaddr)
1485 volatile pte_t * pte;
1487 pte = lookup_noalloc_domain_pte(d, mpaddr);
1488 if(pte != NULL && !pte_none(*pte))
1489 return 1;
1490 return 0;
1493 /* Flush cache of domain d. */
1494 void domain_cache_flush (struct domain *d, int sync_only)
1496 struct mm_struct *mm = &d->arch.mm;
1497 pgd_t *pgd = mm->pgd;
1498 unsigned long maddr;
1499 int i,j,k, l;
1500 int nbr_page = 0;
1501 void (*flush_func)(unsigned long start, unsigned long end);
1502 extern void flush_dcache_range (unsigned long, unsigned long);
1504 if (sync_only)
1505 flush_func = &flush_icache_range;
1506 else
1507 flush_func = &flush_dcache_range;
1509 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
1510 pud_t *pud;
1511 if (!pgd_present(*pgd))
1512 continue;
1513 pud = pud_offset(pgd, 0);
1514 for (j = 0; j < PTRS_PER_PUD; pud++, j++) {
1515 pmd_t *pmd;
1516 if (!pud_present(*pud))
1517 continue;
1518 pmd = pmd_offset(pud, 0);
1519 for (k = 0; k < PTRS_PER_PMD; pmd++, k++) {
1520 pte_t *pte;
1521 if (!pmd_present(*pmd))
1522 continue;
1523 pte = pte_offset_map(pmd, 0);
1524 for (l = 0; l < PTRS_PER_PTE; pte++, l++) {
1525 if (!pte_present(*pte))
1526 continue;
1527 /* Convert PTE to maddr. */
1528 maddr = __va_ul (pte_val(*pte)
1529 & _PAGE_PPN_MASK);
1530 (*flush_func)(maddr, maddr+ PAGE_SIZE);
1531 nbr_page++;
1536 //printf ("domain_cache_flush: %d %d pages\n", d->domain_id, nbr_page);
1539 #ifdef VERBOSE
1540 #define MEM_LOG(_f, _a...) \
1541 printk("DOM%u: (file=mm.c, line=%d) " _f "\n", \
1542 current->domain->domain_id , __LINE__ , ## _a )
1543 #else
1544 #define MEM_LOG(_f, _a...) ((void)0)
1545 #endif
1547 static void free_page_type(struct page_info *page, u32 type)
1551 static int alloc_page_type(struct page_info *page, u32 type)
1553 return 1;
1556 unsigned long __get_free_pages(unsigned int mask, unsigned int order)
1558 void *p = alloc_xenheap_pages(order);
1560 memset(p,0,PAGE_SIZE<<order);
1561 return (unsigned long)p;
1564 void __free_pages(struct page_info *page, unsigned int order)
1566 if (order) BUG();
1567 free_xenheap_page(page);
1570 void *pgtable_quicklist_alloc(void)
1572 void *p;
1573 p = alloc_xenheap_pages(0);
1574 if (p)
1575 clear_page(p);
1576 return p;
1579 void pgtable_quicklist_free(void *pgtable_entry)
1581 free_xenheap_page(pgtable_entry);
1584 void put_page_type(struct page_info *page)
1586 u32 nx, x, y = page->u.inuse.type_info;
1588 again:
1589 do {
1590 x = y;
1591 nx = x - 1;
1593 ASSERT((x & PGT_count_mask) != 0);
1595 /*
1596 * The page should always be validated while a reference is held. The
1597 * exception is during domain destruction, when we forcibly invalidate
1598 * page-table pages if we detect a referential loop.
1599 * See domain.c:relinquish_list().
1600 */
1601 ASSERT((x & PGT_validated) ||
1602 test_bit(_DOMF_dying, &page_get_owner(page)->domain_flags));
1604 if ( unlikely((nx & PGT_count_mask) == 0) )
1606 /* Record TLB information for flush later. Races are harmless. */
1607 page->tlbflush_timestamp = tlbflush_current_time();
1609 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
1610 likely(nx & PGT_validated) )
1612 /*
1613 * Page-table pages must be unvalidated when count is zero. The
1614 * 'free' is safe because the refcnt is non-zero and validated
1615 * bit is clear => other ops will spin or fail.
1616 */
1617 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
1618 x & ~PGT_validated)) != x) )
1619 goto again;
1620 /* We cleared the 'valid bit' so we do the clean up. */
1621 free_page_type(page, x);
1622 /* Carry on, but with the 'valid bit' now clear. */
1623 x &= ~PGT_validated;
1624 nx &= ~PGT_validated;
1627 else if ( unlikely(((nx & (PGT_pinned | PGT_count_mask)) ==
1628 (PGT_pinned | 1)) &&
1629 ((nx & PGT_type_mask) != PGT_writable_page)) )
1631 /* Page is now only pinned. Make the back pointer mutable again. */
1632 nx |= PGT_va_mutable;
1635 while ( unlikely((y = cmpxchg_rel(&page->u.inuse.type_info, x, nx)) != x) );
1639 int get_page_type(struct page_info *page, u32 type)
1641 u32 nx, x, y = page->u.inuse.type_info;
1643 again:
1644 do {
1645 x = y;
1646 nx = x + 1;
1647 if ( unlikely((nx & PGT_count_mask) == 0) )
1649 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
1650 return 0;
1652 else if ( unlikely((x & PGT_count_mask) == 0) )
1654 if ( (x & (PGT_type_mask|PGT_va_mask)) != type )
1656 if ( (x & PGT_type_mask) != (type & PGT_type_mask) )
1658 /*
1659 * On type change we check to flush stale TLB
1660 * entries. This may be unnecessary (e.g., page
1661 * was GDT/LDT) but those circumstances should be
1662 * very rare.
1663 */
1664 cpumask_t mask =
1665 page_get_owner(page)->domain_dirty_cpumask;
1666 tlbflush_filter(mask, page->tlbflush_timestamp);
1668 if ( unlikely(!cpus_empty(mask)) )
1670 perfc_incrc(need_flush_tlb_flush);
1671 flush_tlb_mask(mask);
1675 /* We lose existing type, back pointer, and validity. */
1676 nx &= ~(PGT_type_mask | PGT_va_mask | PGT_validated);
1677 nx |= type;
1679 /* No special validation needed for writable pages. */
1680 /* Page tables and GDT/LDT need to be scanned for validity. */
1681 if ( type == PGT_writable_page )
1682 nx |= PGT_validated;
1685 else
1687 if ( unlikely((x & (PGT_type_mask|PGT_va_mask)) != type) )
1689 if ( unlikely((x & PGT_type_mask) != (type & PGT_type_mask) ) )
1691 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
1692 ((type & PGT_type_mask) != PGT_l1_page_table) )
1693 MEM_LOG("Bad type (saw %08x != exp %08x) "
1694 "for mfn %016lx (pfn %016lx)",
1695 x, type, page_to_mfn(page),
1696 get_gpfn_from_mfn(page_to_mfn(page)));
1697 return 0;
1699 else if ( (x & PGT_va_mask) == PGT_va_mutable )
1701 /* The va backpointer is mutable, hence we update it. */
1702 nx &= ~PGT_va_mask;
1703 nx |= type; /* we know the actual type is correct */
1705 else if ( ((type & PGT_va_mask) != PGT_va_mutable) &&
1706 ((type & PGT_va_mask) != (x & PGT_va_mask)) )
1708 #ifdef CONFIG_X86_PAE
1709 /* We use backptr as extra typing. Cannot be unknown. */
1710 if ( (type & PGT_type_mask) == PGT_l2_page_table )
1711 return 0;
1712 #endif
1713 /* This table is possibly mapped at multiple locations. */
1714 nx &= ~PGT_va_mask;
1715 nx |= PGT_va_unknown;
1718 if ( unlikely(!(x & PGT_validated)) )
1720 /* Someone else is updating validation of this page. Wait... */
1721 while ( (y = page->u.inuse.type_info) == x )
1722 cpu_relax();
1723 goto again;
1727 while ( unlikely((y = cmpxchg_acq(&page->u.inuse.type_info, x, nx)) != x) );
1729 if ( unlikely(!(nx & PGT_validated)) )
1731 /* Try to validate page type; drop the new reference on failure. */
1732 if ( unlikely(!alloc_page_type(page, type)) )
1734 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %08x"
1735 ": caf=%08x taf=%" PRtype_info,
1736 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
1737 type, page->count_info, page->u.inuse.type_info);
1738 /* Noone else can get a reference. We hold the only ref. */
1739 page->u.inuse.type_info = 0;
1740 return 0;
1743 /* Noone else is updating simultaneously. */
1744 __set_bit(_PGT_validated, &page->u.inuse.type_info);
1747 return 1;
1750 int memory_is_conventional_ram(paddr_t p)
1752 return (efi_mem_type(p) == EFI_CONVENTIONAL_MEMORY);
1755 /*
1756 * Local variables:
1757 * mode: C
1758 * c-set-style: "BSD"
1759 * c-basic-offset: 4
1760 * tab-width: 4
1761 * indent-tabs-mode: nil
1762 * End:
1763 */