ia64/xen-unstable

view xen/arch/ia64/xen/mm.c @ 12794:9787cb7262e8

[IA64] changed foreign domain page mapping semantic.

x86 foreign HVM domain page mapping semantic was changed to use gmfn
instead mfn. It applies to domains with auto_translated_mode enabled,
and all ia64 domains enable auto_translated_mode. This patch changes
ia64 foreign domain page mapping to use gmfn and fixes ia64 domU buidler.
However this patch breaks domain save/restore/dump-core.
They should also be fixed-up

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