ia64/xen-unstable

view xen/arch/ia64/xen/mm.c @ 10786:86e5d8458c08

[IA64] live migration

Shadow mode and live migration.

Virtualize Dirty bit.

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author awilliam@xenbuild.aw
date Wed Jul 26 09:36:36 2006 -0600 (2006-07-26)
parents dc26ac2f7718
children 7be1cfe8345b
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 <asm/mm.h>
170 #include <asm/pgalloc.h>
171 #include <asm/vhpt.h>
172 #include <asm/vcpu.h>
173 #include <asm/shadow.h>
174 #include <linux/efi.h>
176 #ifndef CONFIG_XEN_IA64_DOM0_VP
177 #define CONFIG_DOMAIN0_CONTIGUOUS
178 #else
179 static void domain_page_flush(struct domain* d, unsigned long mpaddr,
180 unsigned long old_mfn, unsigned long new_mfn);
181 #endif
183 static struct domain *dom_xen, *dom_io;
185 // followings are stolen from arch_init_memory() @ xen/arch/x86/mm.c
186 void
187 alloc_dom_xen_and_dom_io(void)
188 {
189 /*
190 * Initialise our DOMID_XEN domain.
191 * Any Xen-heap pages that we will allow to be mapped will have
192 * their domain field set to dom_xen.
193 */
194 dom_xen = alloc_domain(DOMID_XEN);
195 BUG_ON(dom_xen == NULL);
197 /*
198 * Initialise our DOMID_IO domain.
199 * This domain owns I/O pages that are within the range of the page_info
200 * array. Mappings occur at the priv of the caller.
201 */
202 dom_io = alloc_domain(DOMID_IO);
203 BUG_ON(dom_io == NULL);
204 }
206 // heavily depends on the struct page_info layout.
207 // if (page_get_owner(page) == d &&
208 // test_and_clear_bit(_PGC_allocated, &page->count_info)) {
209 // put_page(page);
210 // }
211 static void
212 try_to_clear_PGC_allocate(struct domain* d, struct page_info* page)
213 {
214 u32 _d, _nd;
215 u64 x, nx, y;
217 _d = pickle_domptr(d);
218 y = *((u64*)&page->count_info);
219 do {
220 x = y;
221 _nd = x >> 32;
222 nx = x - 1;
223 __clear_bit(_PGC_allocated, &nx);
225 if (unlikely(!(x & PGC_allocated)) || unlikely(_nd != _d)) {
226 struct domain* nd = unpickle_domptr(_nd);
227 if (nd == NULL) {
228 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
229 "sd=%p 0x%x,"
230 " caf=%016lx, taf=%" PRtype_info "\n",
231 (void *) page_to_mfn(page),
232 d, d->domain_id, _d,
233 nd, _nd,
234 x,
235 page->u.inuse.type_info);
236 }
237 break;
238 }
240 BUG_ON((nx & PGC_count_mask) < 1);
241 y = cmpxchg((u64*)&page->count_info, x, nx);
242 } while (unlikely(y != x));
243 }
245 static void
246 relinquish_pte(struct domain* d, pte_t* pte)
247 {
248 unsigned long mfn = pte_pfn(*pte);
249 struct page_info* page;
251 // vmx domain use bit[58:56] to distinguish io region from memory.
252 // see vmx_build_physmap_table() in vmx_init.c
253 if (!pte_mem(*pte))
254 return;
256 // domain might map IO space or acpi table pages. check it.
257 if (!mfn_valid(mfn))
258 return;
259 page = mfn_to_page(mfn);
260 // struct page_info corresponding to mfn may exist or not depending
261 // on CONFIG_VIRTUAL_FRAME_TABLE.
262 // This check is too easy.
263 // The right way is to check whether this page is of io area or acpi pages
264 if (page_get_owner(page) == NULL) {
265 BUG_ON(page->count_info != 0);
266 return;
267 }
269 #ifdef CONFIG_XEN_IA64_DOM0_VP
270 if (page_get_owner(page) == d) {
271 BUG_ON(get_gpfn_from_mfn(mfn) == INVALID_M2P_ENTRY);
272 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
273 }
274 #endif
275 try_to_clear_PGC_allocate(d, page);
276 put_page(page);
277 }
279 static void
280 relinquish_pmd(struct domain* d, pmd_t* pmd, unsigned long offset)
281 {
282 unsigned long i;
283 pte_t* pte = pte_offset_map(pmd, offset);
285 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
286 if (!pte_present(*pte))
287 continue;
289 relinquish_pte(d, pte);
290 }
291 pte_free_kernel(pte_offset_map(pmd, offset));
292 }
294 static void
295 relinquish_pud(struct domain* d, pud_t *pud, unsigned long offset)
296 {
297 unsigned long i;
298 pmd_t *pmd = pmd_offset(pud, offset);
300 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
301 if (!pmd_present(*pmd))
302 continue;
304 relinquish_pmd(d, pmd, offset + (i << PMD_SHIFT));
305 }
306 pmd_free(pmd_offset(pud, offset));
307 }
309 static void
310 relinquish_pgd(struct domain* d, pgd_t *pgd, unsigned long offset)
311 {
312 unsigned long i;
313 pud_t *pud = pud_offset(pgd, offset);
315 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
316 if (!pud_present(*pud))
317 continue;
319 relinquish_pud(d, pud, offset + (i << PUD_SHIFT));
320 }
321 pud_free(pud_offset(pgd, offset));
322 }
324 void
325 relinquish_mm(struct domain* d)
326 {
327 struct mm_struct* mm = &d->arch.mm;
328 unsigned long i;
329 pgd_t* pgd;
331 if (mm->pgd == NULL)
332 return;
334 pgd = pgd_offset(mm, 0);
335 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
336 if (!pgd_present(*pgd))
337 continue;
339 relinquish_pgd(d, pgd, i << PGDIR_SHIFT);
340 }
341 pgd_free(mm->pgd);
342 mm->pgd = NULL;
343 }
345 // stolen from share_xen_page_with_guest() in xen/arch/x86/mm.c
346 void
347 share_xen_page_with_guest(struct page_info *page,
348 struct domain *d, int readonly)
349 {
350 if ( page_get_owner(page) == d )
351 return;
353 #if 1
354 if (readonly) {
355 printk("%s:%d readonly is not supported yet\n", __func__, __LINE__);
356 }
357 #endif
359 // alloc_xenheap_pages() doesn't initialize page owner.
360 //BUG_ON(page_get_owner(page) != NULL);
362 spin_lock(&d->page_alloc_lock);
364 #ifndef __ia64__
365 /* The incremented type count pins as writable or read-only. */
366 page->u.inuse.type_info = (readonly ? PGT_none : PGT_writable_page);
367 page->u.inuse.type_info |= PGT_validated | 1;
368 #endif
370 page_set_owner(page, d);
371 wmb(); /* install valid domain ptr before updating refcnt. */
372 ASSERT(page->count_info == 0);
373 page->count_info |= PGC_allocated | 1;
375 if ( unlikely(d->xenheap_pages++ == 0) )
376 get_knownalive_domain(d);
377 list_add_tail(&page->list, &d->xenpage_list);
379 // grant_table_destroy() releases these pages.
380 // but it doesn't clear their m2p entry. So there might remain stale
381 // entries. such a stale entry is cleared here.
382 set_gpfn_from_mfn(page_to_mfn(page), INVALID_M2P_ENTRY);
384 spin_unlock(&d->page_alloc_lock);
385 }
387 void
388 share_xen_page_with_privileged_guests(struct page_info *page, int readonly)
389 {
390 share_xen_page_with_guest(page, dom_xen, readonly);
391 }
393 unsigned long
394 gmfn_to_mfn_foreign(struct domain *d, unsigned long gpfn)
395 {
396 unsigned long pte;
398 #ifndef CONFIG_XEN_IA64_DOM0_VP
399 if (d == dom0)
400 return(gpfn);
401 #endif
402 pte = lookup_domain_mpa(d,gpfn << PAGE_SHIFT, NULL);
403 if (!pte) {
404 panic("gmfn_to_mfn_foreign: bad gpfn. spinning...\n");
405 }
406 return ((pte & _PFN_MASK) >> PAGE_SHIFT);
407 }
409 // given a domain virtual address, pte and pagesize, extract the metaphysical
410 // address, convert the pte for a physical address for (possibly different)
411 // Xen PAGE_SIZE and return modified pte. (NOTE: TLB insert should use
412 // PAGE_SIZE!)
413 u64 translate_domain_pte(u64 pteval, u64 address, u64 itir__, u64* logps,
414 struct p2m_entry* entry)
415 {
416 struct domain *d = current->domain;
417 ia64_itir_t itir = {.itir = itir__};
418 u64 mask, mpaddr, pteval2;
419 u64 arflags;
420 u64 arflags2;
422 pteval &= ((1UL << 53) - 1);// ignore [63:53] bits
424 // FIXME address had better be pre-validated on insert
425 mask = ~itir_mask(itir.itir);
426 mpaddr = (((pteval & ~_PAGE_ED) & _PAGE_PPN_MASK) & ~mask) |
427 (address & mask);
428 #ifdef CONFIG_XEN_IA64_DOM0_VP
429 if (itir.ps > PAGE_SHIFT) {
430 itir.ps = PAGE_SHIFT;
431 }
432 #endif
433 *logps = itir.ps;
434 #ifndef CONFIG_XEN_IA64_DOM0_VP
435 if (d == dom0) {
436 if (mpaddr < dom0_start || mpaddr >= dom0_start + dom0_size) {
437 /*
438 printk("translate_domain_pte: out-of-bounds dom0 mpaddr 0x%lx! itc=%lx...\n",
439 mpaddr, ia64_get_itc());
440 */
441 }
442 }
443 else if ((mpaddr >> PAGE_SHIFT) > d->max_pages) {
444 /* Address beyond the limit. However the grant table is
445 also beyond the limit. Display a message if not in the
446 grant table. */
447 if (mpaddr >= IA64_GRANT_TABLE_PADDR
448 && mpaddr < (IA64_GRANT_TABLE_PADDR
449 + (ORDER_GRANT_FRAMES << PAGE_SHIFT)))
450 printf("translate_domain_pte: bad mpa=0x%lx (> 0x%lx),"
451 "vadr=0x%lx,pteval=0x%lx,itir=0x%lx\n",
452 mpaddr, (unsigned long)d->max_pages<<PAGE_SHIFT,
453 address, pteval, itir.itir);
454 }
455 #endif
456 pteval2 = lookup_domain_mpa(d, mpaddr, entry);
457 arflags = pteval & _PAGE_AR_MASK;
458 arflags2 = pteval2 & _PAGE_AR_MASK;
459 if (arflags != _PAGE_AR_R && arflags2 == _PAGE_AR_R) {
460 #if 0
461 DPRINTK("%s:%d "
462 "pteval 0x%lx arflag 0x%lx address 0x%lx itir 0x%lx "
463 "pteval2 0x%lx arflags2 0x%lx mpaddr 0x%lx\n",
464 __func__, __LINE__,
465 pteval, arflags, address, itir__,
466 pteval2, arflags2, mpaddr);
467 #endif
468 pteval = (pteval & ~_PAGE_AR_MASK) | _PAGE_AR_R;
469 }
471 pteval2 &= _PAGE_PPN_MASK; // ignore non-addr bits
472 pteval2 |= (pteval & _PAGE_ED);
473 pteval2 |= _PAGE_PL_2; // force PL0->2 (PL3 is unaffected)
474 pteval2 |= (pteval & ~_PAGE_PPN_MASK);
475 /*
476 * Don't let non-dom0 domains map uncached addresses. This can
477 * happen when domU tries to touch i/o port space. Also prevents
478 * possible address aliasing issues.
479 * WB => WB
480 * UC, UCE, WC => WB
481 * NaTPage => NaTPage
482 */
483 if (d != dom0 && (pteval2 & _PAGE_MA_MASK) != _PAGE_MA_NAT)
484 pteval2 &= ~_PAGE_MA_MASK;
486 /* If shadow mode is enabled, virtualize dirty bit. */
487 if (shadow_mode_enabled(d) && (pteval2 & _PAGE_D)) {
488 u64 mp_page = mpaddr >> PAGE_SHIFT;
489 pteval2 |= _PAGE_VIRT_D;
491 /* If the page is not already dirty, don't set the dirty bit.
492 This is a small optimization! */
493 if (mp_page < d->arch.shadow_bitmap_size * 8
494 && !test_bit(mp_page, d->arch.shadow_bitmap))
495 pteval2 = (pteval2 & ~_PAGE_D);
496 }
498 return pteval2;
499 }
501 // given a current domain metaphysical address, return the physical address
502 unsigned long translate_domain_mpaddr(unsigned long mpaddr,
503 struct p2m_entry* entry)
504 {
505 unsigned long pteval;
507 #ifndef CONFIG_XEN_IA64_DOM0_VP
508 if (current->domain == dom0) {
509 if (mpaddr < dom0_start || mpaddr >= dom0_start + dom0_size) {
510 printk("translate_domain_mpaddr: out-of-bounds dom0 mpaddr 0x%lx! continuing...\n",
511 mpaddr);
512 }
513 }
514 #endif
515 pteval = lookup_domain_mpa(current->domain, mpaddr, entry);
516 return ((pteval & _PAGE_PPN_MASK) | (mpaddr & ~PAGE_MASK));
517 }
519 //XXX !xxx_present() should be used instread of !xxx_none()?
520 // __assign_new_domain_page(), assign_new_domain_page() and
521 // assign_new_domain0_page() are used only when domain creation.
522 // their accesses aren't racy so that returned pte_t doesn't need
523 // volatile qualifier
524 static pte_t*
525 __lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
526 {
527 struct mm_struct *mm = &d->arch.mm;
528 pgd_t *pgd;
529 pud_t *pud;
530 pmd_t *pmd;
532 BUG_ON(mm->pgd == NULL);
533 pgd = pgd_offset(mm, mpaddr);
534 if (pgd_none(*pgd)) {
535 pgd_populate(mm, pgd, pud_alloc_one(mm,mpaddr));
536 }
538 pud = pud_offset(pgd, mpaddr);
539 if (pud_none(*pud)) {
540 pud_populate(mm, pud, pmd_alloc_one(mm,mpaddr));
541 }
543 pmd = pmd_offset(pud, mpaddr);
544 if (pmd_none(*pmd)) {
545 pmd_populate_kernel(mm, pmd, pte_alloc_one_kernel(mm, mpaddr));
546 }
548 return pte_offset_map(pmd, mpaddr);
549 }
551 //XXX !xxx_present() should be used instread of !xxx_none()?
552 // pud, pmd, pte page is zero cleared when they are allocated.
553 // Their area must be visible before population so that
554 // cmpxchg must have release semantics.
555 static volatile pte_t*
556 lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
557 {
558 struct mm_struct *mm = &d->arch.mm;
559 pgd_t *pgd;
560 pud_t *pud;
561 pmd_t *pmd;
563 BUG_ON(mm->pgd == NULL);
565 pgd = pgd_offset(mm, mpaddr);
566 again_pgd:
567 if (unlikely(pgd_none(*pgd))) {
568 pud_t *old_pud = NULL;
569 pud = pud_alloc_one(mm, mpaddr);
570 if (unlikely(!pgd_cmpxchg_rel(mm, pgd, old_pud, pud))) {
571 pud_free(pud);
572 goto again_pgd;
573 }
574 }
576 pud = pud_offset(pgd, mpaddr);
577 again_pud:
578 if (unlikely(pud_none(*pud))) {
579 pmd_t* old_pmd = NULL;
580 pmd = pmd_alloc_one(mm, mpaddr);
581 if (unlikely(!pud_cmpxchg_rel(mm, pud, old_pmd, pmd))) {
582 pmd_free(pmd);
583 goto again_pud;
584 }
585 }
587 pmd = pmd_offset(pud, mpaddr);
588 again_pmd:
589 if (unlikely(pmd_none(*pmd))) {
590 pte_t* old_pte = NULL;
591 pte_t* pte = pte_alloc_one_kernel(mm, mpaddr);
592 if (unlikely(!pmd_cmpxchg_kernel_rel(mm, pmd, old_pte, pte))) {
593 pte_free_kernel(pte);
594 goto again_pmd;
595 }
596 }
598 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
599 }
601 //XXX xxx_none() should be used instread of !xxx_present()?
602 volatile pte_t*
603 lookup_noalloc_domain_pte(struct domain* d, unsigned long mpaddr)
604 {
605 struct mm_struct *mm = &d->arch.mm;
606 pgd_t *pgd;
607 pud_t *pud;
608 pmd_t *pmd;
610 BUG_ON(mm->pgd == NULL);
611 pgd = pgd_offset(mm, mpaddr);
612 if (unlikely(!pgd_present(*pgd)))
613 return NULL;
615 pud = pud_offset(pgd, mpaddr);
616 if (unlikely(!pud_present(*pud)))
617 return NULL;
619 pmd = pmd_offset(pud, mpaddr);
620 if (unlikely(!pmd_present(*pmd)))
621 return NULL;
623 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
624 }
626 #ifdef CONFIG_XEN_IA64_DOM0_VP
627 static volatile pte_t*
628 lookup_noalloc_domain_pte_none(struct domain* d, unsigned long mpaddr)
629 {
630 struct mm_struct *mm = &d->arch.mm;
631 pgd_t *pgd;
632 pud_t *pud;
633 pmd_t *pmd;
635 BUG_ON(mm->pgd == NULL);
636 pgd = pgd_offset(mm, mpaddr);
637 if (unlikely(pgd_none(*pgd)))
638 return NULL;
640 pud = pud_offset(pgd, mpaddr);
641 if (unlikely(pud_none(*pud)))
642 return NULL;
644 pmd = pmd_offset(pud, mpaddr);
645 if (unlikely(pmd_none(*pmd)))
646 return NULL;
648 return (volatile pte_t*)pte_offset_map(pmd, mpaddr);
649 }
651 unsigned long
652 ____lookup_domain_mpa(struct domain *d, unsigned long mpaddr)
653 {
654 volatile pte_t *pte;
656 pte = lookup_noalloc_domain_pte(d, mpaddr);
657 if (pte == NULL)
658 return INVALID_MFN;
660 if (pte_present(*pte))
661 return (pte->pte & _PFN_MASK);
662 else if (VMX_DOMAIN(d->vcpu[0]))
663 return GPFN_INV_MASK;
664 return INVALID_MFN;
665 }
666 #endif
668 unsigned long lookup_domain_mpa(struct domain *d, unsigned long mpaddr,
669 struct p2m_entry* entry)
670 {
671 volatile pte_t *pte;
673 #ifdef CONFIG_DOMAIN0_CONTIGUOUS
674 if (d == dom0) {
675 pte_t pteval;
676 if (mpaddr < dom0_start || mpaddr >= dom0_start + dom0_size) {
677 //printk("lookup_domain_mpa: bad dom0 mpaddr 0x%lx!\n",mpaddr);
678 //printk("lookup_domain_mpa: start=0x%lx,end=0x%lx!\n",dom0_start,dom0_start+dom0_size);
679 }
680 pteval = pfn_pte(mpaddr >> PAGE_SHIFT,
681 __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX));
682 return pte_val(pteval);
683 }
684 #endif
685 pte = lookup_noalloc_domain_pte(d, mpaddr);
686 if (pte != NULL) {
687 pte_t tmp_pte = *pte;// pte is volatile. copy the value.
688 if (pte_present(tmp_pte)) {
689 //printk("lookup_domain_page: found mapping for %lx, pte=%lx\n",mpaddr,pte_val(*pte));
690 if (entry != NULL)
691 p2m_entry_set(entry, pte, tmp_pte);
692 return pte_val(tmp_pte);
693 } else if (VMX_DOMAIN(d->vcpu[0]))
694 return GPFN_INV_MASK;
695 }
697 printk("%s: d 0x%p id %d current 0x%p id %d\n",
698 __func__, d, d->domain_id, current, current->vcpu_id);
699 if ((mpaddr >> PAGE_SHIFT) < d->max_pages)
700 printk("%s: non-allocated mpa 0x%lx (< 0x%lx)\n", __func__,
701 mpaddr, (unsigned long)d->max_pages << PAGE_SHIFT);
702 else
703 printk("%s: bad mpa 0x%lx (=> 0x%lx)\n", __func__,
704 mpaddr, (unsigned long)d->max_pages << PAGE_SHIFT);
706 if (entry != NULL)
707 p2m_entry_set(entry, NULL, __pte(0));
708 //XXX This is a work around until the emulation memory access to a region
709 // where memory or device are attached is implemented.
710 return pte_val(pfn_pte(0, __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX)));
711 }
713 // FIXME: ONLY USE FOR DOMAIN PAGE_SIZE == PAGE_SIZE
714 #if 1
715 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
716 {
717 unsigned long pte = lookup_domain_mpa(d, mpaddr, NULL);
718 unsigned long imva;
720 pte &= _PAGE_PPN_MASK;
721 imva = (unsigned long) __va(pte);
722 imva |= mpaddr & ~PAGE_MASK;
723 return (void*)imva;
724 }
725 #else
726 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
727 {
728 unsigned long imva = __gpa_to_mpa(d, mpaddr);
730 return (void *)__va(imva);
731 }
732 #endif
734 /* Allocate a new page for domain and map it to the specified metaphysical
735 address. */
736 static struct page_info *
737 __assign_new_domain_page(struct domain *d, unsigned long mpaddr, pte_t* pte)
738 {
739 struct page_info *p = NULL;
740 unsigned long maddr;
741 int ret;
743 BUG_ON(!pte_none(*pte));
745 #ifdef CONFIG_DOMAIN0_CONTIGUOUS
746 if (d == dom0) {
747 #if 0
748 if (mpaddr < dom0_start || mpaddr >= dom0_start + dom0_size) {
749 /* FIXME: is it true ?
750 dom0 memory is not contiguous! */
751 panic("assign_new_domain_page: bad domain0 "
752 "mpaddr=%lx, start=%lx, end=%lx!\n",
753 mpaddr, dom0_start, dom0_start+dom0_size);
754 }
755 #endif
756 p = mfn_to_page((mpaddr >> PAGE_SHIFT));
757 return p;
758 }
759 #endif
761 p = alloc_domheap_page(d);
762 if (unlikely(!p)) {
763 printf("assign_new_domain_page: Can't alloc!!!! Aaaargh!\n");
764 return(p);
765 }
767 // zero out pages for security reasons
768 clear_page(page_to_virt(p));
769 maddr = page_to_maddr (p);
770 if (unlikely(maddr > __get_cpu_var(vhpt_paddr)
771 && maddr < __get_cpu_var(vhpt_pend))) {
772 /* FIXME: how can this happen ?
773 vhpt is allocated by alloc_domheap_page. */
774 printf("assign_new_domain_page: reassigned vhpt page %lx!!\n",
775 maddr);
776 }
778 ret = get_page(p, d);
779 BUG_ON(ret == 0);
780 set_gpfn_from_mfn(page_to_mfn(p), mpaddr >> PAGE_SHIFT);
781 // clear_page() and set_gpfn_from_mfn() become visible before set_pte_rel()
782 // because set_pte_rel() has release semantics
783 set_pte_rel(pte,
784 pfn_pte(maddr >> PAGE_SHIFT,
785 __pgprot(__DIRTY_BITS | _PAGE_PL_2 | _PAGE_AR_RWX)));
787 smp_mb();
788 return p;
789 }
791 struct page_info *
792 assign_new_domain_page(struct domain *d, unsigned long mpaddr)
793 {
794 #ifdef CONFIG_DOMAIN0_CONTIGUOUS
795 pte_t dummy_pte = __pte(0);
796 return __assign_new_domain_page(d, mpaddr, &dummy_pte);
797 #else
798 struct page_info *p = NULL;
799 pte_t *pte;
801 pte = __lookup_alloc_domain_pte(d, mpaddr);
802 if (pte_none(*pte))
803 p = __assign_new_domain_page(d, mpaddr, pte);
805 return p;
806 #endif
807 }
809 void
810 assign_new_domain0_page(struct domain *d, unsigned long mpaddr)
811 {
812 #ifndef CONFIG_DOMAIN0_CONTIGUOUS
813 pte_t *pte;
815 BUG_ON(d != dom0);
816 pte = __lookup_alloc_domain_pte(d, mpaddr);
817 if (pte_none(*pte)) {
818 struct page_info *p = __assign_new_domain_page(d, mpaddr, pte);
819 if (p == NULL) {
820 panic("%s: can't allocate page for dom0", __func__);
821 }
822 }
823 #endif
824 }
826 /* map a physical address to the specified metaphysical addr */
827 // flags: currently only ASSIGN_readonly
828 // This is called by assign_domain_mmio_page().
829 // So accessing to pte is racy.
830 void
831 __assign_domain_page(struct domain *d,
832 unsigned long mpaddr, unsigned long physaddr,
833 unsigned long flags)
834 {
835 volatile pte_t *pte;
836 pte_t old_pte;
837 pte_t new_pte;
838 pte_t ret_pte;
839 unsigned long arflags = (flags & ASSIGN_readonly)? _PAGE_AR_R: _PAGE_AR_RWX;
841 pte = lookup_alloc_domain_pte(d, mpaddr);
843 old_pte = __pte(0);
844 new_pte = pfn_pte(physaddr >> PAGE_SHIFT,
845 __pgprot(__DIRTY_BITS | _PAGE_PL_2 | arflags));
846 ret_pte = ptep_cmpxchg_rel(&d->arch.mm, mpaddr, pte, old_pte, new_pte);
847 if (pte_val(ret_pte) == pte_val(old_pte))
848 smp_mb();
849 }
851 /* get_page() and map a physical address to the specified metaphysical addr */
852 void
853 assign_domain_page(struct domain *d,
854 unsigned long mpaddr, unsigned long physaddr)
855 {
856 struct page_info* page = mfn_to_page(physaddr >> PAGE_SHIFT);
857 int ret;
859 BUG_ON((physaddr & GPFN_IO_MASK) != GPFN_MEM);
860 ret = get_page(page, d);
861 BUG_ON(ret == 0);
862 set_gpfn_from_mfn(physaddr >> PAGE_SHIFT, mpaddr >> PAGE_SHIFT);
863 // because __assign_domain_page() uses set_pte_rel() which has
864 // release semantics, smp_mb() isn't needed.
865 __assign_domain_page(d, mpaddr, physaddr, ASSIGN_writable);
866 }
868 #ifdef CONFIG_XEN_IA64_DOM0_VP
869 static void
870 assign_domain_same_page(struct domain *d,
871 unsigned long mpaddr, unsigned long size,
872 unsigned long flags)
873 {
874 //XXX optimization
875 unsigned long end = PAGE_ALIGN(mpaddr + size);
876 for (mpaddr &= PAGE_MASK; mpaddr < end; mpaddr += PAGE_SIZE) {
877 __assign_domain_page(d, mpaddr, mpaddr, flags);
878 }
879 }
881 int
882 efi_mmio(unsigned long physaddr, unsigned long size)
883 {
884 void *efi_map_start, *efi_map_end;
885 u64 efi_desc_size;
886 void* p;
888 efi_map_start = __va(ia64_boot_param->efi_memmap);
889 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
890 efi_desc_size = ia64_boot_param->efi_memdesc_size;
892 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
893 efi_memory_desc_t* md = (efi_memory_desc_t *)p;
894 unsigned long start = md->phys_addr;
895 unsigned long end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
897 if (start <= physaddr && physaddr < end) {
898 if ((physaddr + size) > end) {
899 DPRINTK("%s:%d physaddr 0x%lx size = 0x%lx\n",
900 __func__, __LINE__, physaddr, size);
901 return 0;
902 }
904 // for io space
905 if (md->type == EFI_MEMORY_MAPPED_IO ||
906 md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
907 return 1;
908 }
910 // for runtime
911 // see efi_enter_virtual_mode(void)
912 // in linux/arch/ia64/kernel/efi.c
913 if ((md->attribute & EFI_MEMORY_RUNTIME) &&
914 !(md->attribute & EFI_MEMORY_WB)) {
915 return 1;
916 }
918 DPRINTK("%s:%d physaddr 0x%lx size = 0x%lx\n",
919 __func__, __LINE__, physaddr, size);
920 return 0;
921 }
923 if (physaddr < start) {
924 break;
925 }
926 }
928 return 1;
929 }
931 unsigned long
932 assign_domain_mmio_page(struct domain *d,
933 unsigned long mpaddr, unsigned long size)
934 {
935 if (size == 0) {
936 DPRINTK("%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
937 __func__, d, mpaddr, size);
938 }
939 if (!efi_mmio(mpaddr, size)) {
940 DPRINTK("%s:%d domain %p mpaddr 0x%lx size = 0x%lx\n",
941 __func__, __LINE__, d, mpaddr, size);
942 return -EINVAL;
943 }
944 assign_domain_same_page(d, mpaddr, size, ASSIGN_writable);
945 return mpaddr;
946 }
948 unsigned long
949 assign_domain_mach_page(struct domain *d,
950 unsigned long mpaddr, unsigned long size,
951 unsigned long flags)
952 {
953 assign_domain_same_page(d, mpaddr, size, flags);
954 return mpaddr;
955 }
957 // caller must get_page(mfn_to_page(mfn)) before call.
958 // caller must call set_gpfn_from_mfn() before call if necessary.
959 // because set_gpfn_from_mfn() result must be visible before pte xchg
960 // caller must use memory barrier. NOTE: xchg has acquire semantics.
961 // flags: currently only ASSIGN_readonly
962 static void
963 assign_domain_page_replace(struct domain *d, unsigned long mpaddr,
964 unsigned long mfn, unsigned long flags)
965 {
966 struct mm_struct *mm = &d->arch.mm;
967 volatile pte_t* pte;
968 pte_t old_pte;
969 pte_t npte;
970 unsigned long arflags = (flags & ASSIGN_readonly)? _PAGE_AR_R: _PAGE_AR_RWX;
971 pte = lookup_alloc_domain_pte(d, mpaddr);
973 // update pte
974 npte = pfn_pte(mfn, __pgprot(__DIRTY_BITS | _PAGE_PL_2 | arflags));
975 old_pte = ptep_xchg(mm, mpaddr, pte, npte);
976 if (pte_mem(old_pte)) {
977 unsigned long old_mfn = pte_pfn(old_pte);
979 // mfn = old_mfn case can happen when domain maps a granted page
980 // twice with the same pseudo physial address.
981 // It's non sense, but allowed.
982 // __gnttab_map_grant_ref()
983 // => create_host_mapping()
984 // => assign_domain_page_replace()
985 if (mfn != old_mfn) {
986 struct page_info* old_page = mfn_to_page(old_mfn);
988 if (page_get_owner(old_page) == d ||
989 page_get_owner(old_page) == NULL) {
990 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
991 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
992 }
994 domain_page_flush(d, mpaddr, old_mfn, mfn);
996 try_to_clear_PGC_allocate(d, old_page);
997 put_page(old_page);
998 }
999 }
1002 // caller must get_page(new_page) before
1003 // Only steal_page() calls this function.
1004 static int
1005 assign_domain_page_cmpxchg_rel(struct domain* d, unsigned long mpaddr,
1006 struct page_info* old_page,
1007 struct page_info* new_page,
1008 unsigned long flags)
1010 struct mm_struct *mm = &d->arch.mm;
1011 volatile pte_t* pte;
1012 unsigned long old_mfn;
1013 unsigned long old_arflags;
1014 pte_t old_pte;
1015 unsigned long new_mfn;
1016 unsigned long new_arflags;
1017 pte_t new_pte;
1018 pte_t ret_pte;
1020 pte = lookup_alloc_domain_pte(d, mpaddr);
1022 again:
1023 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1024 old_mfn = page_to_mfn(old_page);
1025 old_pte = pfn_pte(old_mfn, __pgprot(old_arflags));
1026 if (!pte_present(old_pte)) {
1027 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx old_mfn 0x%lx\n",
1028 __func__, pte_val(old_pte), old_arflags, old_mfn);
1029 return -EINVAL;
1032 new_arflags = (flags & ASSIGN_readonly)? _PAGE_AR_R: _PAGE_AR_RWX;
1033 new_mfn = page_to_mfn(new_page);
1034 new_pte = pfn_pte(new_mfn,
1035 __pgprot(__DIRTY_BITS | _PAGE_PL_2 | new_arflags));
1037 // update pte
1038 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1039 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1040 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1041 goto again;
1044 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx old_mfn 0x%lx "
1045 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1046 __func__,
1047 pte_val(old_pte), old_arflags, old_mfn,
1048 pte_val(ret_pte), pte_pfn(ret_pte));
1049 return -EINVAL;
1052 BUG_ON(!pte_mem(old_pte));
1053 BUG_ON(page_get_owner(old_page) != d);
1054 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
1055 BUG_ON(old_mfn == new_mfn);
1057 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
1059 domain_page_flush(d, mpaddr, old_mfn, new_mfn);
1060 put_page(old_page);
1061 return 0;
1064 static void
1065 zap_domain_page_one(struct domain *d, unsigned long mpaddr, unsigned long mfn)
1067 struct mm_struct *mm = &d->arch.mm;
1068 volatile pte_t *pte;
1069 pte_t old_pte;
1070 struct page_info *page;
1072 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
1073 if (pte == NULL)
1074 return;
1075 if (pte_none(*pte))
1076 return;
1078 if (mfn == INVALID_MFN) {
1079 // clear pte
1080 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
1081 mfn = pte_pfn(old_pte);
1082 } else {
1083 unsigned long old_arflags;
1084 pte_t new_pte;
1085 pte_t ret_pte;
1087 again:
1088 // memory_exchange() calls guest_physmap_remove_page() with
1089 // a stealed page. i.e. page owner = NULL.
1090 BUG_ON(page_get_owner(mfn_to_page(mfn)) != d &&
1091 page_get_owner(mfn_to_page(mfn)) != NULL);
1092 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1093 old_pte = pfn_pte(mfn, __pgprot(old_arflags));
1094 new_pte = __pte(0);
1096 // update pte
1097 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1098 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1099 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1100 goto again;
1103 DPRINTK("%s: old_pte 0x%lx old_arflags 0x%lx mfn 0x%lx "
1104 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1105 __func__,
1106 pte_val(old_pte), old_arflags, mfn,
1107 pte_val(ret_pte), pte_pfn(ret_pte));
1108 return;
1110 BUG_ON(mfn != pte_pfn(ret_pte));
1113 page = mfn_to_page(mfn);
1114 BUG_ON((page->count_info & PGC_count_mask) == 0);
1116 if (page_get_owner(page) == d ||
1117 page_get_owner(page) == NULL) {
1118 // exchange_memory() calls
1119 // steal_page()
1120 // page owner is set to NULL
1121 // guest_physmap_remove_page()
1122 // zap_domain_page_one()
1123 BUG_ON(get_gpfn_from_mfn(mfn) != (mpaddr >> PAGE_SHIFT));
1124 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
1127 domain_page_flush(d, mpaddr, mfn, INVALID_MFN);
1129 if (page_get_owner(page) != NULL) {
1130 try_to_clear_PGC_allocate(d, page);
1132 put_page(page);
1135 unsigned long
1136 dom0vp_zap_physmap(struct domain *d, unsigned long gpfn,
1137 unsigned int extent_order)
1139 if (extent_order != 0) {
1140 //XXX
1141 return -ENOSYS;
1144 zap_domain_page_one(d, gpfn << PAGE_SHIFT, INVALID_MFN);
1145 return 0;
1148 unsigned long
1149 dom0vp_add_physmap(struct domain* d, unsigned long gpfn, unsigned long mfn,
1150 unsigned long flags, domid_t domid)
1152 int error = 0;
1153 struct domain* rd;
1155 rd = find_domain_by_id(domid);
1156 if (unlikely(rd == NULL)) {
1157 switch (domid) {
1158 case DOMID_XEN:
1159 rd = dom_xen;
1160 break;
1161 case DOMID_IO:
1162 rd = dom_io;
1163 break;
1164 default:
1165 DPRINTK("d 0x%p domid %d "
1166 "pgfn 0x%lx mfn 0x%lx flags 0x%lx domid %d\n",
1167 d, d->domain_id, gpfn, mfn, flags, domid);
1168 return -ESRCH;
1170 BUG_ON(rd == NULL);
1171 get_knownalive_domain(rd);
1174 if (unlikely(rd == d || !mfn_valid(mfn))) {
1175 error = -EINVAL;
1176 goto out1;
1178 if (unlikely(get_page(mfn_to_page(mfn), rd) == 0)) {
1179 error = -EINVAL;
1180 goto out1;
1182 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1183 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1184 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, flags);
1185 //don't update p2m table because this page belongs to rd, not d.
1186 out1:
1187 put_domain(rd);
1188 return error;
1191 // grant table host mapping
1192 // mpaddr: host_addr: pseudo physical address
1193 // mfn: frame: machine page frame
1194 // flags: GNTMAP_readonly | GNTMAP_application_map | GNTMAP_contains_pte
1195 int
1196 create_grant_host_mapping(unsigned long gpaddr,
1197 unsigned long mfn, unsigned int flags)
1199 struct domain* d = current->domain;
1200 struct page_info* page;
1201 int ret;
1203 if (flags & (GNTMAP_device_map |
1204 GNTMAP_application_map | GNTMAP_contains_pte)) {
1205 DPRINTK("%s: flags 0x%x\n", __func__, flags);
1206 return GNTST_general_error;
1209 BUG_ON(!mfn_valid(mfn));
1210 page = mfn_to_page(mfn);
1211 ret = get_page(page, page_get_owner(page));
1212 BUG_ON(ret == 0);
1213 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1214 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1215 assign_domain_page_replace(d, gpaddr, mfn, (flags & GNTMAP_readonly)?
1216 ASSIGN_readonly: ASSIGN_writable);
1217 return GNTST_okay;
1220 // grant table host unmapping
1221 int
1222 destroy_grant_host_mapping(unsigned long gpaddr,
1223 unsigned long mfn, unsigned int flags)
1225 struct domain* d = current->domain;
1226 volatile pte_t* pte;
1227 unsigned long cur_arflags;
1228 pte_t cur_pte;
1229 pte_t new_pte;
1230 pte_t old_pte;
1231 struct page_info* page;
1233 if (flags & (GNTMAP_application_map | GNTMAP_contains_pte)) {
1234 DPRINTK("%s: flags 0x%x\n", __func__, flags);
1235 return GNTST_general_error;
1238 pte = lookup_noalloc_domain_pte(d, gpaddr);
1239 if (pte == NULL) {
1240 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx\n", __func__, gpaddr, mfn);
1241 return GNTST_general_error;
1244 again:
1245 cur_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1246 cur_pte = pfn_pte(mfn, __pgprot(cur_arflags));
1247 if (!pte_present(cur_pte)) {
1248 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx\n",
1249 __func__, gpaddr, mfn, pte_val(cur_pte));
1250 return GNTST_general_error;
1252 new_pte = __pte(0);
1254 old_pte = ptep_cmpxchg_rel(&d->arch.mm, gpaddr, pte, cur_pte, new_pte);
1255 if (unlikely(!pte_present(old_pte))) {
1256 DPRINTK("%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx old_pte 0x%lx\n",
1257 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1258 return GNTST_general_error;
1260 if (unlikely(pte_val(cur_pte) != pte_val(old_pte))) {
1261 if (pte_pfn(old_pte) == mfn) {
1262 goto again;
1264 DPRINTK("%s gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx old_pte 0x%lx\n",
1265 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
1266 return GNTST_general_error;
1268 BUG_ON(pte_pfn(old_pte) != mfn);
1270 domain_page_flush(d, gpaddr, mfn, INVALID_MFN);
1272 page = mfn_to_page(mfn);
1273 BUG_ON(page_get_owner(page) == d);//try_to_clear_PGC_allocate(d, page) is not needed.
1274 put_page(page);
1276 return GNTST_okay;
1279 // heavily depends on the struct page layout.
1280 // gnttab_transfer() calls steal_page() with memflags = 0
1281 // For grant table transfer, we must fill the page.
1282 // memory_exchange() calls steal_page() with memflags = MEMF_no_refcount
1283 // For memory exchange, we don't have to fill the page because
1284 // memory_exchange() does it.
1285 int
1286 steal_page(struct domain *d, struct page_info *page, unsigned int memflags)
1288 #if 0 /* if big endian */
1289 # error "implement big endian version of steal_page()"
1290 #endif
1291 u32 _d, _nd;
1292 u64 x, nx, y;
1294 if (page_get_owner(page) != d) {
1295 DPRINTK("%s d 0x%p owner 0x%p\n", __func__, d, page_get_owner(page));
1296 return -1;
1299 if (!(memflags & MEMF_no_refcount)) {
1300 unsigned long gpfn;
1301 struct page_info *new;
1302 unsigned long new_mfn;
1303 int ret;
1305 new = alloc_domheap_page(d);
1306 if (new == NULL) {
1307 DPRINTK("alloc_domheap_page() failed\n");
1308 return -1;
1310 // zero out pages for security reasons
1311 clear_page(page_to_virt(new));
1312 // assign_domain_page_cmpxchg_rel() has release semantics
1313 // so smp_mb() isn't needed.
1315 ret = get_page(new, d);
1316 BUG_ON(ret == 0);
1318 gpfn = get_gpfn_from_mfn(page_to_mfn(page));
1319 if (gpfn == INVALID_M2P_ENTRY) {
1320 free_domheap_page(new);
1321 return -1;
1323 new_mfn = page_to_mfn(new);
1324 set_gpfn_from_mfn(new_mfn, gpfn);
1325 // smp_mb() isn't needed because assign_domain_pge_cmpxchg_rel()
1326 // has release semantics.
1328 ret = assign_domain_page_cmpxchg_rel(d, gpfn << PAGE_SHIFT, page, new,
1329 ASSIGN_writable);
1330 if (ret < 0) {
1331 DPRINTK("assign_domain_page_cmpxchg_rel failed %d\n", ret);
1332 set_gpfn_from_mfn(new_mfn, INVALID_M2P_ENTRY);
1333 free_domheap_page(new);
1334 return -1;
1338 spin_lock(&d->page_alloc_lock);
1340 /*
1341 * The tricky bit: atomically release ownership while there is just one
1342 * benign reference to the page (PGC_allocated). If that reference
1343 * disappears then the deallocation routine will safely spin.
1344 */
1345 _d = pickle_domptr(d);
1346 y = *((u64*)&page->count_info);
1347 do {
1348 x = y;
1349 nx = x & 0xffffffff;
1350 // page->count_info: untouched
1351 // page->u.inused._domain = 0;
1352 _nd = x >> 32;
1354 if (unlikely(!(memflags & MEMF_no_refcount) &&
1355 ((x & (PGC_count_mask | PGC_allocated)) !=
1356 (1 | PGC_allocated))) ||
1358 // when MEMF_no_refcount, page isn't de-assigned from
1359 // this domain yet. So count_info = 2
1360 unlikely((memflags & MEMF_no_refcount) &&
1361 ((x & (PGC_count_mask | PGC_allocated)) !=
1362 (2 | PGC_allocated))) ||
1364 unlikely(_nd != _d)) {
1365 struct domain* nd = unpickle_domptr(_nd);
1366 if (nd == NULL) {
1367 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
1368 "sd=%p 0x%x,"
1369 " caf=%016lx, taf=%" PRtype_info
1370 " memflags 0x%x\n",
1371 (void *) page_to_mfn(page),
1372 d, d->domain_id, _d,
1373 nd, _nd,
1374 x,
1375 page->u.inuse.type_info,
1376 memflags);
1377 } else {
1378 DPRINTK("gnttab_transfer: Bad page %p: ed=%p(%u) 0x%x, "
1379 "sd=%p(%u) 0x%x,"
1380 " caf=%016lx, taf=%" PRtype_info
1381 " memflags 0x%x\n",
1382 (void *) page_to_mfn(page),
1383 d, d->domain_id, _d,
1384 nd, nd->domain_id, _nd,
1385 x,
1386 page->u.inuse.type_info,
1387 memflags);
1389 spin_unlock(&d->page_alloc_lock);
1390 return -1;
1393 y = cmpxchg((u64*)&page->count_info, x, nx);
1394 } while (unlikely(y != x));
1396 /*
1397 * Unlink from 'd'. At least one reference remains (now anonymous), so
1398 * noone else is spinning to try to delete this page from 'd'.
1399 */
1400 if ( !(memflags & MEMF_no_refcount) )
1401 d->tot_pages--;
1402 list_del(&page->list);
1404 spin_unlock(&d->page_alloc_lock);
1405 return 0;
1408 void
1409 guest_physmap_add_page(struct domain *d, unsigned long gpfn,
1410 unsigned long mfn)
1412 int ret;
1414 BUG_ON(!mfn_valid(mfn));
1415 ret = get_page(mfn_to_page(mfn), d);
1416 BUG_ON(ret == 0);
1417 set_gpfn_from_mfn(mfn, gpfn);
1418 smp_mb();
1419 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, ASSIGN_writable);
1421 //BUG_ON(mfn != ((lookup_domain_mpa(d, gpfn << PAGE_SHIFT) & _PFN_MASK) >> PAGE_SHIFT));
1424 void
1425 guest_physmap_remove_page(struct domain *d, unsigned long gpfn,
1426 unsigned long mfn)
1428 BUG_ON(mfn == 0);//XXX
1429 zap_domain_page_one(d, gpfn << PAGE_SHIFT, mfn);
1432 //XXX sledgehammer.
1433 // flush finer range.
1434 static void
1435 domain_page_flush(struct domain* d, unsigned long mpaddr,
1436 unsigned long old_mfn, unsigned long new_mfn)
1438 if (shadow_mode_enabled(d))
1439 shadow_mark_page_dirty(d, mpaddr >> PAGE_SHIFT);
1441 domain_flush_vtlb_all();
1444 int
1445 domain_page_mapped(struct domain* d, unsigned long mpaddr)
1447 volatile pte_t * pte;
1449 pte = lookup_noalloc_domain_pte(d, mpaddr);
1450 if(pte != NULL && !pte_none(*pte))
1451 return 1;
1452 return 0;
1454 #endif
1456 /* Flush cache of domain d. */
1457 void domain_cache_flush (struct domain *d, int sync_only)
1459 struct mm_struct *mm = &d->arch.mm;
1460 pgd_t *pgd = mm->pgd;
1461 unsigned long maddr;
1462 int i,j,k, l;
1463 int nbr_page = 0;
1464 void (*flush_func)(unsigned long start, unsigned long end);
1465 extern void flush_dcache_range (unsigned long, unsigned long);
1467 if (sync_only)
1468 flush_func = &flush_icache_range;
1469 else
1470 flush_func = &flush_dcache_range;
1472 #ifdef CONFIG_DOMAIN0_CONTIGUOUS
1473 if (d == dom0) {
1474 /* This is not fully correct (because of hole), but it should
1475 be enough for now. */
1476 (*flush_func)(__va_ul (dom0_start),
1477 __va_ul (dom0_start + dom0_size));
1478 return;
1480 #endif
1481 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
1482 pud_t *pud;
1483 if (!pgd_present(*pgd))
1484 continue;
1485 pud = pud_offset(pgd, 0);
1486 for (j = 0; j < PTRS_PER_PUD; pud++, j++) {
1487 pmd_t *pmd;
1488 if (!pud_present(*pud))
1489 continue;
1490 pmd = pmd_offset(pud, 0);
1491 for (k = 0; k < PTRS_PER_PMD; pmd++, k++) {
1492 pte_t *pte;
1493 if (!pmd_present(*pmd))
1494 continue;
1495 pte = pte_offset_map(pmd, 0);
1496 for (l = 0; l < PTRS_PER_PTE; pte++, l++) {
1497 if (!pte_present(*pte))
1498 continue;
1499 /* Convert PTE to maddr. */
1500 maddr = __va_ul (pte_val(*pte)
1501 & _PAGE_PPN_MASK);
1502 (*flush_func)(maddr, maddr+ PAGE_SIZE);
1503 nbr_page++;
1508 //printf ("domain_cache_flush: %d %d pages\n", d->domain_id, nbr_page);
1511 #ifdef VERBOSE
1512 #define MEM_LOG(_f, _a...) \
1513 printk("DOM%u: (file=mm.c, line=%d) " _f "\n", \
1514 current->domain->domain_id , __LINE__ , ## _a )
1515 #else
1516 #define MEM_LOG(_f, _a...) ((void)0)
1517 #endif
1519 static void free_page_type(struct page_info *page, u32 type)
1523 static int alloc_page_type(struct page_info *page, u32 type)
1525 return 1;
1528 unsigned long __get_free_pages(unsigned int mask, unsigned int order)
1530 void *p = alloc_xenheap_pages(order);
1532 memset(p,0,PAGE_SIZE<<order);
1533 return (unsigned long)p;
1536 void __free_pages(struct page_info *page, unsigned int order)
1538 if (order) BUG();
1539 free_xenheap_page(page);
1542 void *pgtable_quicklist_alloc(void)
1544 void *p;
1545 p = alloc_xenheap_pages(0);
1546 if (p)
1547 clear_page(p);
1548 return p;
1551 void pgtable_quicklist_free(void *pgtable_entry)
1553 free_xenheap_page(pgtable_entry);
1556 void cleanup_writable_pagetable(struct domain *d)
1558 return;
1561 void put_page_type(struct page_info *page)
1563 u32 nx, x, y = page->u.inuse.type_info;
1565 again:
1566 do {
1567 x = y;
1568 nx = x - 1;
1570 ASSERT((x & PGT_count_mask) != 0);
1572 /*
1573 * The page should always be validated while a reference is held. The
1574 * exception is during domain destruction, when we forcibly invalidate
1575 * page-table pages if we detect a referential loop.
1576 * See domain.c:relinquish_list().
1577 */
1578 ASSERT((x & PGT_validated) ||
1579 test_bit(_DOMF_dying, &page_get_owner(page)->domain_flags));
1581 if ( unlikely((nx & PGT_count_mask) == 0) )
1583 /* Record TLB information for flush later. Races are harmless. */
1584 page->tlbflush_timestamp = tlbflush_current_time();
1586 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
1587 likely(nx & PGT_validated) )
1589 /*
1590 * Page-table pages must be unvalidated when count is zero. The
1591 * 'free' is safe because the refcnt is non-zero and validated
1592 * bit is clear => other ops will spin or fail.
1593 */
1594 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
1595 x & ~PGT_validated)) != x) )
1596 goto again;
1597 /* We cleared the 'valid bit' so we do the clean up. */
1598 free_page_type(page, x);
1599 /* Carry on, but with the 'valid bit' now clear. */
1600 x &= ~PGT_validated;
1601 nx &= ~PGT_validated;
1604 else if ( unlikely(((nx & (PGT_pinned | PGT_count_mask)) ==
1605 (PGT_pinned | 1)) &&
1606 ((nx & PGT_type_mask) != PGT_writable_page)) )
1608 /* Page is now only pinned. Make the back pointer mutable again. */
1609 nx |= PGT_va_mutable;
1612 while ( unlikely((y = cmpxchg_rel(&page->u.inuse.type_info, x, nx)) != x) );
1616 int get_page_type(struct page_info *page, u32 type)
1618 u32 nx, x, y = page->u.inuse.type_info;
1620 again:
1621 do {
1622 x = y;
1623 nx = x + 1;
1624 if ( unlikely((nx & PGT_count_mask) == 0) )
1626 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
1627 return 0;
1629 else if ( unlikely((x & PGT_count_mask) == 0) )
1631 if ( (x & (PGT_type_mask|PGT_va_mask)) != type )
1633 if ( (x & PGT_type_mask) != (type & PGT_type_mask) )
1635 /*
1636 * On type change we check to flush stale TLB
1637 * entries. This may be unnecessary (e.g., page
1638 * was GDT/LDT) but those circumstances should be
1639 * very rare.
1640 */
1641 cpumask_t mask =
1642 page_get_owner(page)->domain_dirty_cpumask;
1643 tlbflush_filter(mask, page->tlbflush_timestamp);
1645 if ( unlikely(!cpus_empty(mask)) )
1647 perfc_incrc(need_flush_tlb_flush);
1648 flush_tlb_mask(mask);
1652 /* We lose existing type, back pointer, and validity. */
1653 nx &= ~(PGT_type_mask | PGT_va_mask | PGT_validated);
1654 nx |= type;
1656 /* No special validation needed for writable pages. */
1657 /* Page tables and GDT/LDT need to be scanned for validity. */
1658 if ( type == PGT_writable_page )
1659 nx |= PGT_validated;
1662 else
1664 if ( unlikely((x & (PGT_type_mask|PGT_va_mask)) != type) )
1666 if ( unlikely((x & PGT_type_mask) != (type & PGT_type_mask) ) )
1668 if ( current->domain == page_get_owner(page) )
1670 /*
1671 * This ensures functions like set_gdt() see up-to-date
1672 * type info without needing to clean up writable p.t.
1673 * state on the fast path.
1674 */
1675 LOCK_BIGLOCK(current->domain);
1676 cleanup_writable_pagetable(current->domain);
1677 y = page->u.inuse.type_info;
1678 UNLOCK_BIGLOCK(current->domain);
1679 /* Can we make progress now? */
1680 if ( ((y & PGT_type_mask) == (type & PGT_type_mask)) ||
1681 ((y & PGT_count_mask) == 0) )
1682 goto again;
1684 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
1685 ((type & PGT_type_mask) != PGT_l1_page_table) )
1686 MEM_LOG("Bad type (saw %08x != exp %08x) "
1687 "for mfn %016lx (pfn %016lx)",
1688 x, type, page_to_mfn(page),
1689 get_gpfn_from_mfn(page_to_mfn(page)));
1690 return 0;
1692 else if ( (x & PGT_va_mask) == PGT_va_mutable )
1694 /* The va backpointer is mutable, hence we update it. */
1695 nx &= ~PGT_va_mask;
1696 nx |= type; /* we know the actual type is correct */
1698 else if ( ((type & PGT_va_mask) != PGT_va_mutable) &&
1699 ((type & PGT_va_mask) != (x & PGT_va_mask)) )
1701 #ifdef CONFIG_X86_PAE
1702 /* We use backptr as extra typing. Cannot be unknown. */
1703 if ( (type & PGT_type_mask) == PGT_l2_page_table )
1704 return 0;
1705 #endif
1706 /* This table is possibly mapped at multiple locations. */
1707 nx &= ~PGT_va_mask;
1708 nx |= PGT_va_unknown;
1711 if ( unlikely(!(x & PGT_validated)) )
1713 /* Someone else is updating validation of this page. Wait... */
1714 while ( (y = page->u.inuse.type_info) == x )
1715 cpu_relax();
1716 goto again;
1720 while ( unlikely((y = cmpxchg_acq(&page->u.inuse.type_info, x, nx)) != x) );
1722 if ( unlikely(!(nx & PGT_validated)) )
1724 /* Try to validate page type; drop the new reference on failure. */
1725 if ( unlikely(!alloc_page_type(page, type)) )
1727 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %08x"
1728 ": caf=%08x taf=%" PRtype_info,
1729 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
1730 type, page->count_info, page->u.inuse.type_info);
1731 /* Noone else can get a reference. We hold the only ref. */
1732 page->u.inuse.type_info = 0;
1733 return 0;
1736 /* Noone else is updating simultaneously. */
1737 __set_bit(_PGT_validated, &page->u.inuse.type_info);
1740 return 1;
1743 /*
1744 * Local variables:
1745 * mode: C
1746 * c-set-style: "BSD"
1747 * c-basic-offset: 4
1748 * tab-width: 4
1749 * indent-tabs-mode: nil
1750 * End:
1751 */