ia64/xen-unstable

view xen/arch/ia64/xen/mm.c @ 18633:f27787b9f8d7

[IA64] Change ioports_permit_access interface().

use VTD to assing device, guest port may not be equal to host port.
Change ioports_permit_access interface to get guest pseudo physical
address.

Signed-off-by: Anthony Xu <anthony.xu@intel.com>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Fri Oct 17 17:40:15 2008 +0900 (2008-10-17)
parents 89ef37e0f4b8
children 07558e1876e4
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 * replace_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(), replace_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(), replace_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 <linux/sort.h>
179 #include <xen/guest_access.h>
180 #include <asm/page.h>
181 #include <asm/dom_fw_common.h>
182 #include <public/memory.h>
183 #include <asm/event.h>
184 #include <asm/debugger.h>
186 static void domain_page_flush_and_put(struct domain* d, unsigned long mpaddr,
187 volatile pte_t* ptep, pte_t old_pte,
188 struct page_info* page);
190 static void __xencomm_mark_dirty(struct domain *d,
191 unsigned long addr, unsigned int len);
193 extern unsigned long ia64_iobase;
195 struct domain *dom_xen, *dom_io;
197 /*
198 * This number is bigger than DOMID_SELF, DOMID_XEN and DOMID_IO.
199 * If more reserved domain ids are introduced, this might be increased.
200 */
201 #define DOMID_P2M (0x7FF8U)
202 static struct domain *dom_p2m;
204 // followings are stolen from arch_init_memory() @ xen/arch/x86/mm.c
205 void
206 alloc_dom_xen_and_dom_io(void)
207 {
208 /*
209 * Initialise our DOMID_XEN domain.
210 * Any Xen-heap pages that we will allow to be mapped will have
211 * their domain field set to dom_xen.
212 */
213 dom_xen = domain_create(DOMID_XEN, DOMCRF_dummy, 0);
214 BUG_ON(dom_xen == NULL);
216 /*
217 * Initialise our DOMID_IO domain.
218 * This domain owns I/O pages that are within the range of the page_info
219 * array. Mappings occur at the priv of the caller.
220 */
221 dom_io = domain_create(DOMID_IO, DOMCRF_dummy, 0);
222 BUG_ON(dom_io == NULL);
223 }
225 static int
226 mm_teardown_can_skip(struct domain* d, unsigned long offset)
227 {
228 return d->arch.mm_teardown_offset > offset;
229 }
231 static void
232 mm_teardown_update_offset(struct domain* d, unsigned long offset)
233 {
234 d->arch.mm_teardown_offset = offset;
235 }
237 static void
238 mm_teardown_pte(struct domain* d, volatile pte_t* pte, unsigned long offset)
239 {
240 pte_t old_pte;
241 unsigned long mfn;
242 struct page_info* page;
244 old_pte = ptep_get_and_clear(&d->arch.mm, offset, pte);// acquire semantics
246 // vmx domain use bit[58:56] to distinguish io region from memory.
247 // see vmx_build_physmap_table() in vmx_init.c
248 if (!pte_mem(old_pte))
249 return;
251 // domain might map IO space or acpi table pages. check it.
252 mfn = pte_pfn(old_pte);
253 if (!mfn_valid(mfn))
254 return;
255 page = mfn_to_page(mfn);
256 BUG_ON(page_get_owner(page) == NULL);
258 // struct page_info corresponding to mfn may exist or not depending
259 // on CONFIG_VIRTUAL_FRAME_TABLE.
260 // The above check is too easy.
261 // The right way is to check whether this page is of io area or acpi pages
263 if (pte_pgc_allocated(old_pte)) {
264 BUG_ON(page_get_owner(page) != d);
265 BUG_ON(get_gpfn_from_mfn(mfn) == INVALID_M2P_ENTRY);
266 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
267 if (test_and_clear_bit(_PGC_allocated, &page->count_info))
268 put_page(page);
269 } else {
270 put_page(page);
271 }
272 }
274 static int
275 mm_teardown_pmd(struct domain* d, volatile pmd_t* pmd, unsigned long offset)
276 {
277 unsigned long i;
278 volatile pte_t* pte = pte_offset_map(pmd, offset);
280 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
281 unsigned long cur_offset = offset + (i << PAGE_SHIFT);
282 if (mm_teardown_can_skip(d, cur_offset + PAGE_SIZE))
283 continue;
284 if (!pte_present(*pte)) { // acquire semantics
285 mm_teardown_update_offset(d, cur_offset);
286 continue;
287 }
288 mm_teardown_update_offset(d, cur_offset);
289 mm_teardown_pte(d, pte, cur_offset);
290 if (hypercall_preempt_check())
291 return -EAGAIN;
292 }
293 return 0;
294 }
296 static int
297 mm_teardown_pud(struct domain* d, volatile pud_t *pud, unsigned long offset)
298 {
299 unsigned long i;
300 volatile pmd_t *pmd = pmd_offset(pud, offset);
302 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
303 unsigned long cur_offset = offset + (i << PMD_SHIFT);
304 if (mm_teardown_can_skip(d, cur_offset + PMD_SIZE))
305 continue;
306 if (!pmd_present(*pmd)) { // acquire semantics
307 mm_teardown_update_offset(d, cur_offset);
308 continue;
309 }
310 if (mm_teardown_pmd(d, pmd, cur_offset))
311 return -EAGAIN;
312 }
313 return 0;
314 }
316 static int
317 mm_teardown_pgd(struct domain* d, volatile pgd_t *pgd, unsigned long offset)
318 {
319 unsigned long i;
320 volatile pud_t *pud = pud_offset(pgd, offset);
322 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
323 unsigned long cur_offset = offset + (i << PUD_SHIFT);
324 #ifndef __PAGETABLE_PUD_FOLDED
325 if (mm_teardown_can_skip(d, cur_offset + PUD_SIZE))
326 continue;
327 #endif
328 if (!pud_present(*pud)) { // acquire semantics
329 #ifndef __PAGETABLE_PUD_FOLDED
330 mm_teardown_update_offset(d, cur_offset);
331 #endif
332 continue;
333 }
334 if (mm_teardown_pud(d, pud, cur_offset))
335 return -EAGAIN;
336 }
337 return 0;
338 }
340 int
341 mm_teardown(struct domain* d)
342 {
343 struct mm_struct* mm = &d->arch.mm;
344 unsigned long i;
345 volatile pgd_t* pgd;
347 if (mm->pgd == NULL)
348 return 0;
350 pgd = pgd_offset(mm, 0);
351 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
352 unsigned long cur_offset = i << PGDIR_SHIFT;
354 if (mm_teardown_can_skip(d, cur_offset + PGDIR_SIZE))
355 continue;
356 if (!pgd_present(*pgd)) { // acquire semantics
357 mm_teardown_update_offset(d, cur_offset);
358 continue;
359 }
360 if (mm_teardown_pgd(d, pgd, cur_offset))
361 return -EAGAIN;
362 }
364 foreign_p2m_destroy(d);
365 return 0;
366 }
368 static void
369 mm_p2m_teardown_pmd(struct domain* d, volatile pmd_t* pmd,
370 unsigned long offset)
371 {
372 pte_free_kernel(pte_offset_map(pmd, offset));
373 }
375 static void
376 mm_p2m_teardown_pud(struct domain* d, volatile pud_t *pud,
377 unsigned long offset)
378 {
379 unsigned long i;
380 volatile pmd_t *pmd = pmd_offset(pud, offset);
382 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
383 if (!pmd_present(*pmd))
384 continue;
385 mm_p2m_teardown_pmd(d, pmd, offset + (i << PMD_SHIFT));
386 }
387 pmd_free(pmd_offset(pud, offset));
388 }
390 static void
391 mm_p2m_teardown_pgd(struct domain* d, volatile pgd_t *pgd,
392 unsigned long offset)
393 {
394 unsigned long i;
395 volatile pud_t *pud = pud_offset(pgd, offset);
397 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
398 if (!pud_present(*pud))
399 continue;
400 mm_p2m_teardown_pud(d, pud, offset + (i << PUD_SHIFT));
401 }
402 pud_free(pud_offset(pgd, offset));
403 }
405 static void
406 mm_p2m_teardown(struct domain* d)
407 {
408 struct mm_struct* mm = &d->arch.mm;
409 unsigned long i;
410 volatile pgd_t* pgd;
412 BUG_ON(mm->pgd == NULL);
413 pgd = pgd_offset(mm, 0);
414 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
415 if (!pgd_present(*pgd))
416 continue;
417 mm_p2m_teardown_pgd(d, pgd, i << PGDIR_SHIFT);
418 }
419 pgd_free(mm->pgd);
420 mm->pgd = NULL;
421 }
423 void
424 mm_final_teardown(struct domain* d)
425 {
426 if (d->arch.shadow_bitmap != NULL) {
427 xfree(d->arch.shadow_bitmap);
428 d->arch.shadow_bitmap = NULL;
429 }
430 mm_p2m_teardown(d);
431 }
433 unsigned long
434 domain_get_maximum_gpfn(struct domain *d)
435 {
436 return (d->arch.convmem_end - 1) >> PAGE_SHIFT;
437 }
439 // stolen from share_xen_page_with_guest() in xen/arch/x86/mm.c
440 void
441 share_xen_page_with_guest(struct page_info *page,
442 struct domain *d, int readonly)
443 {
444 if ( page_get_owner(page) == d )
445 return;
447 #if 1
448 if (readonly) {
449 printk("%s:%d readonly is not supported yet\n", __func__, __LINE__);
450 }
451 #endif
453 // alloc_xenheap_pages() doesn't initialize page owner.
454 //BUG_ON(page_get_owner(page) != NULL);
456 spin_lock(&d->page_alloc_lock);
458 #ifndef __ia64__
459 /* The incremented type count pins as writable or read-only. */
460 page->u.inuse.type_info = (readonly ? PGT_none : PGT_writable_page);
461 page->u.inuse.type_info |= PGT_validated | 1;
462 #endif
464 page_set_owner(page, d);
465 wmb(); /* install valid domain ptr before updating refcnt. */
466 ASSERT(page->count_info == 0);
468 /* Only add to the allocation list if the domain isn't dying. */
469 if ( !d->is_dying )
470 {
471 page->count_info |= PGC_allocated | 1;
472 if ( unlikely(d->xenheap_pages++ == 0) )
473 get_knownalive_domain(d);
474 list_add_tail(&page->list, &d->xenpage_list);
475 }
477 // grant_table_destroy() releases these pages.
478 // but it doesn't clear their m2p entry. So there might remain stale
479 // entries. such a stale entry is cleared here.
480 set_gpfn_from_mfn(page_to_mfn(page), INVALID_M2P_ENTRY);
482 spin_unlock(&d->page_alloc_lock);
483 }
485 void
486 share_xen_page_with_privileged_guests(struct page_info *page, int readonly)
487 {
488 share_xen_page_with_guest(page, dom_xen, readonly);
489 }
491 unsigned long
492 gmfn_to_mfn_foreign(struct domain *d, unsigned long gpfn)
493 {
494 unsigned long pte;
496 pte = lookup_domain_mpa(d,gpfn << PAGE_SHIFT, NULL);
497 if (!pte) {
498 panic("gmfn_to_mfn_foreign: bad gpfn. spinning...\n");
499 }
501 if ((pte & _PAGE_IO) && is_hvm_domain(d))
502 return INVALID_MFN;
504 return ((pte & _PFN_MASK) >> PAGE_SHIFT);
505 }
507 // given a domain virtual address, pte and pagesize, extract the metaphysical
508 // address, convert the pte for a physical address for (possibly different)
509 // Xen PAGE_SIZE and return modified pte. (NOTE: TLB insert should use
510 // current->arch.vhpt_pg_shift!)
511 u64 translate_domain_pte(u64 pteval, u64 address, u64 itir__, u64* itir,
512 struct p2m_entry* entry)
513 {
514 struct domain *d = current->domain;
515 ia64_itir_t _itir = {.itir = itir__};
516 u64 mask, mpaddr, pteval2;
517 u64 arflags;
518 u64 arflags2;
519 u64 maflags2;
521 pteval &= ((1UL << 53) - 1);// ignore [63:53] bits
523 // FIXME address had better be pre-validated on insert
524 mask = ~itir_mask(_itir.itir);
525 mpaddr = ((pteval & _PAGE_PPN_MASK) & ~mask) | (address & mask);
527 if (_itir.ps > PAGE_SHIFT)
528 _itir.ps = PAGE_SHIFT;
530 ((ia64_itir_t*)itir)->itir = _itir.itir;/* Copy the whole register. */
531 ((ia64_itir_t*)itir)->ps = _itir.ps; /* Overwrite ps part! */
533 pteval2 = lookup_domain_mpa(d, mpaddr, entry);
534 if (_itir.ps < PAGE_SHIFT)
535 pteval2 |= mpaddr & ~PAGE_MASK & ~((1L << _itir.ps) - 1);
537 /* Check access rights. */
538 arflags = pteval & _PAGE_AR_MASK;
539 arflags2 = pteval2 & _PAGE_AR_MASK;
540 if (arflags != _PAGE_AR_R && arflags2 == _PAGE_AR_R) {
541 #if 0
542 dprintk(XENLOG_WARNING,
543 "%s:%d "
544 "pteval 0x%lx arflag 0x%lx address 0x%lx itir 0x%lx "
545 "pteval2 0x%lx arflags2 0x%lx mpaddr 0x%lx\n",
546 __func__, __LINE__,
547 pteval, arflags, address, itir__,
548 pteval2, arflags2, mpaddr);
549 #endif
550 pteval = (pteval & ~_PAGE_AR_MASK) | _PAGE_AR_R;
551 }
553 /* Check memory attribute. The switch is on the *requested* memory
554 attribute. */
555 maflags2 = pteval2 & _PAGE_MA_MASK;
556 switch (pteval & _PAGE_MA_MASK) {
557 case _PAGE_MA_NAT:
558 /* NaT pages are always accepted! */
559 break;
560 case _PAGE_MA_UC:
561 case _PAGE_MA_UCE:
562 case _PAGE_MA_WC:
563 if (maflags2 == _PAGE_MA_WB) {
564 /* Don't let domains WB-map uncached addresses.
565 This can happen when domU tries to touch i/o
566 port space. Also prevents possible address
567 aliasing issues. */
568 if (!(mpaddr - IO_PORTS_PADDR < IO_PORTS_SIZE)) {
569 u64 ucwb;
571 /*
572 * If dom0 page has both UC & WB attributes
573 * don't warn about attempted UC access.
574 */
575 ucwb = efi_mem_attribute(mpaddr, PAGE_SIZE);
576 ucwb &= EFI_MEMORY_UC | EFI_MEMORY_WB;
577 ucwb ^= EFI_MEMORY_UC | EFI_MEMORY_WB;
579 if (d != dom0 || ucwb != 0)
580 gdprintk(XENLOG_WARNING, "Warning: UC"
581 " to WB for mpaddr=%lx\n",
582 mpaddr);
583 }
584 pteval = (pteval & ~_PAGE_MA_MASK) | _PAGE_MA_WB;
585 }
586 break;
587 case _PAGE_MA_WB:
588 if (maflags2 != _PAGE_MA_WB) {
589 /* Forbid non-coherent access to coherent memory. */
590 panic_domain(NULL, "try to use WB mem attr on "
591 "UC page, mpaddr=%lx\n", mpaddr);
592 }
593 break;
594 default:
595 panic_domain(NULL, "try to use unknown mem attribute\n");
596 }
598 /* If shadow mode is enabled, virtualize dirty bit. */
599 if (shadow_mode_enabled(d) && (pteval & _PAGE_D)) {
600 u64 mp_page = mpaddr >> PAGE_SHIFT;
601 pteval |= _PAGE_VIRT_D;
603 /* If the page is not already dirty, don't set the dirty bit! */
604 if (mp_page < d->arch.shadow_bitmap_size * 8
605 && !test_bit(mp_page, d->arch.shadow_bitmap))
606 pteval &= ~_PAGE_D;
607 }
609 /* Ignore non-addr bits of pteval2 and force PL0->1
610 (PL3 is unaffected) */
611 return (pteval & ~(_PAGE_PPN_MASK | _PAGE_PL_MASK)) |
612 (pteval2 & _PAGE_PPN_MASK) |
613 (vcpu_pl_adjust(pteval, 7) & _PAGE_PL_MASK);
614 }
616 // given a current domain metaphysical address, return the physical address
617 unsigned long translate_domain_mpaddr(unsigned long mpaddr,
618 struct p2m_entry* entry)
619 {
620 unsigned long pteval;
622 pteval = lookup_domain_mpa(current->domain, mpaddr, entry);
623 return ((pteval & _PAGE_PPN_MASK) | (mpaddr & ~PAGE_MASK));
624 }
626 //XXX !xxx_present() should be used instread of !xxx_none()?
627 // pud, pmd, pte page is zero cleared when they are allocated.
628 // Their area must be visible before population so that
629 // cmpxchg must have release semantics.
630 static volatile pte_t*
631 lookup_alloc_domain_pte(struct domain* d, unsigned long mpaddr)
632 {
633 struct mm_struct *mm = &d->arch.mm;
634 volatile pgd_t *pgd;
635 volatile pud_t *pud;
636 volatile pmd_t *pmd;
638 BUG_ON(mm->pgd == NULL);
640 pgd = pgd_offset(mm, mpaddr);
641 again_pgd:
642 if (unlikely(pgd_none(*pgd))) { // acquire semantics
643 pud_t *old_pud = NULL;
644 pud = pud_alloc_one(mm, mpaddr);
645 if (unlikely(!pgd_cmpxchg_rel(mm, pgd, old_pud, pud))) {
646 pud_free(pud);
647 goto again_pgd;
648 }
649 }
651 pud = pud_offset(pgd, mpaddr);
652 again_pud:
653 if (unlikely(pud_none(*pud))) { // acquire semantics
654 pmd_t* old_pmd = NULL;
655 pmd = pmd_alloc_one(mm, mpaddr);
656 if (unlikely(!pud_cmpxchg_rel(mm, pud, old_pmd, pmd))) {
657 pmd_free(pmd);
658 goto again_pud;
659 }
660 }
662 pmd = pmd_offset(pud, mpaddr);
663 again_pmd:
664 if (unlikely(pmd_none(*pmd))) { // acquire semantics
665 pte_t* old_pte = NULL;
666 pte_t* pte = pte_alloc_one_kernel(mm, mpaddr);
667 if (unlikely(!pmd_cmpxchg_kernel_rel(mm, pmd, old_pte, pte))) {
668 pte_free_kernel(pte);
669 goto again_pmd;
670 }
671 }
673 return pte_offset_map(pmd, mpaddr);
674 }
676 //XXX xxx_none() should be used instread of !xxx_present()?
677 volatile pte_t*
678 lookup_noalloc_domain_pte(struct domain* d, unsigned long mpaddr)
679 {
680 struct mm_struct *mm = &d->arch.mm;
681 volatile pgd_t *pgd;
682 volatile pud_t *pud;
683 volatile pmd_t *pmd;
685 BUG_ON(mm->pgd == NULL);
686 pgd = pgd_offset(mm, mpaddr);
687 if (unlikely(!pgd_present(*pgd))) // acquire semantics
688 return NULL;
690 pud = pud_offset(pgd, mpaddr);
691 if (unlikely(!pud_present(*pud))) // acquire semantics
692 return NULL;
694 pmd = pmd_offset(pud, mpaddr);
695 if (unlikely(!pmd_present(*pmd))) // acquire semantics
696 return NULL;
698 return pte_offset_map(pmd, mpaddr);
699 }
701 static volatile pte_t*
702 lookup_noalloc_domain_pte_none(struct domain* d, unsigned long mpaddr)
703 {
704 struct mm_struct *mm = &d->arch.mm;
705 volatile pgd_t *pgd;
706 volatile pud_t *pud;
707 volatile pmd_t *pmd;
709 BUG_ON(mm->pgd == NULL);
710 pgd = pgd_offset(mm, mpaddr);
711 if (unlikely(pgd_none(*pgd))) // acquire semantics
712 return NULL;
714 pud = pud_offset(pgd, mpaddr);
715 if (unlikely(pud_none(*pud))) // acquire semantics
716 return NULL;
718 pmd = pmd_offset(pud, mpaddr);
719 if (unlikely(pmd_none(*pmd))) // acquire semantics
720 return NULL;
722 return pte_offset_map(pmd, mpaddr);
723 }
725 unsigned long
726 ____lookup_domain_mpa(struct domain *d, unsigned long mpaddr)
727 {
728 volatile pte_t *pte;
730 pte = lookup_noalloc_domain_pte(d, mpaddr);
731 if (pte == NULL)
732 return INVALID_MFN;
734 if (pte_present(*pte))
735 return (pte->pte & _PFN_MASK);
736 return INVALID_MFN;
737 }
739 unsigned long lookup_domain_mpa(struct domain *d, unsigned long mpaddr,
740 struct p2m_entry* entry)
741 {
742 volatile pte_t *pte = lookup_noalloc_domain_pte(d, mpaddr);
744 if (pte != NULL) {
745 pte_t tmp_pte = *pte;// pte is volatile. copy the value.
746 if (pte_present(tmp_pte)) {
747 if (entry != NULL)
748 p2m_entry_set(entry, pte, tmp_pte);
749 return pte_val(tmp_pte);
750 } else if (is_hvm_domain(d))
751 return INVALID_MFN;
752 }
754 if (mpaddr < d->arch.convmem_end && !d->is_dying) {
755 gdprintk(XENLOG_WARNING, "vcpu %d iip 0x%016lx: non-allocated mpa "
756 "d %"PRId16" 0x%lx (< 0x%lx)\n",
757 current->vcpu_id, PSCB(current, iip),
758 d->domain_id, mpaddr, d->arch.convmem_end);
759 } else if (mpaddr - IO_PORTS_PADDR < IO_PORTS_SIZE) {
760 /* Log I/O port probing, but complain less loudly about it */
761 gdprintk(XENLOG_INFO, "vcpu %d iip 0x%016lx: bad I/O port access "
762 "d %"PRId16" 0x%lx\n",
763 current->vcpu_id, PSCB(current, iip), d->domain_id,
764 IO_SPACE_SPARSE_DECODING(mpaddr - IO_PORTS_PADDR));
765 } else {
766 gdprintk(XENLOG_WARNING, "vcpu %d iip 0x%016lx: bad mpa "
767 "d %"PRId16" 0x%lx (=> 0x%lx)\n",
768 current->vcpu_id, PSCB(current, iip),
769 d->domain_id, mpaddr, d->arch.convmem_end);
770 }
772 debugger_event (XEN_IA64_DEBUG_ON_BAD_MPA);
774 if (entry != NULL)
775 p2m_entry_set(entry, NULL, __pte(0));
776 //XXX This is a work around until the emulation memory access to a region
777 // where memory or device are attached is implemented.
778 return pte_val(pfn_pte(0, __pgprot(__DIRTY_BITS | _PAGE_PL_PRIV |
779 _PAGE_AR_RWX)));
780 }
782 // FIXME: ONLY USE FOR DOMAIN PAGE_SIZE == PAGE_SIZE
783 #if 1
784 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
785 {
786 unsigned long pte = lookup_domain_mpa(d, mpaddr, NULL);
787 unsigned long imva;
789 pte &= _PAGE_PPN_MASK;
790 imva = (unsigned long) __va(pte);
791 imva |= mpaddr & ~PAGE_MASK;
792 return (void*)imva;
793 }
794 #else
795 void *domain_mpa_to_imva(struct domain *d, unsigned long mpaddr)
796 {
797 unsigned long imva = __gpa_to_mpa(d, mpaddr);
799 return (void *)__va(imva);
800 }
801 #endif
803 unsigned long
804 paddr_to_maddr(unsigned long paddr)
805 {
806 struct vcpu *v = current;
807 struct domain *d = v->domain;
808 u64 pa;
810 pa = ____lookup_domain_mpa(d, paddr);
811 if (pa == INVALID_MFN) {
812 printk("%s: called with bad memory address: 0x%lx - iip=%lx\n",
813 __func__, paddr, vcpu_regs(v)->cr_iip);
814 return 0;
815 }
816 return (pa & _PFN_MASK) | (paddr & ~PAGE_MASK);
817 }
819 /* Allocate a new page for domain and map it to the specified metaphysical
820 address. */
821 static struct page_info *
822 __assign_new_domain_page(struct domain *d, unsigned long mpaddr,
823 volatile pte_t* pte)
824 {
825 struct page_info *p;
826 unsigned long maddr;
828 BUG_ON(!pte_none(*pte));
830 p = alloc_domheap_page(d, 0);
831 if (unlikely(!p)) {
832 printk("assign_new_domain_page: Can't alloc!!!! Aaaargh!\n");
833 return(p);
834 }
836 // zero out pages for security reasons
837 clear_page(page_to_virt(p));
838 maddr = page_to_maddr (p);
839 if (unlikely(maddr > __get_cpu_var(vhpt_paddr)
840 && maddr < __get_cpu_var(vhpt_pend))) {
841 /* FIXME: how can this happen ?
842 vhpt is allocated by alloc_domheap_page. */
843 printk("assign_new_domain_page: reassigned vhpt page %lx!!\n",
844 maddr);
845 }
847 set_gpfn_from_mfn(page_to_mfn(p), mpaddr >> PAGE_SHIFT);
848 // clear_page() and set_gpfn_from_mfn() become visible before set_pte_rel()
849 // because set_pte_rel() has release semantics
850 set_pte_rel(pte,
851 pfn_pte(maddr >> PAGE_SHIFT,
852 __pgprot(_PAGE_PGC_ALLOCATED | __DIRTY_BITS |
853 _PAGE_PL_PRIV | _PAGE_AR_RWX)));
855 smp_mb();
856 return p;
857 }
859 struct page_info *
860 assign_new_domain_page(struct domain *d, unsigned long mpaddr)
861 {
862 volatile pte_t *pte = lookup_alloc_domain_pte(d, mpaddr);
864 if (!pte_none(*pte))
865 return NULL;
867 return __assign_new_domain_page(d, mpaddr, pte);
868 }
870 void __init
871 assign_new_domain0_page(struct domain *d, unsigned long mpaddr)
872 {
873 volatile pte_t *pte;
875 BUG_ON(d != dom0);
876 pte = lookup_alloc_domain_pte(d, mpaddr);
877 if (pte_none(*pte)) {
878 struct page_info *p = __assign_new_domain_page(d, mpaddr, pte);
879 if (p == NULL) {
880 panic("%s: can't allocate page for dom0\n", __func__);
881 }
882 }
883 }
885 static unsigned long
886 flags_to_prot (unsigned long flags)
887 {
888 unsigned long res = _PAGE_PL_PRIV | __DIRTY_BITS;
890 res |= flags & ASSIGN_readonly ? _PAGE_AR_R: _PAGE_AR_RWX;
891 res |= flags & ASSIGN_nocache ? _PAGE_MA_UC: _PAGE_MA_WB;
892 #ifdef CONFIG_XEN_IA64_TLB_TRACK
893 res |= flags & ASSIGN_tlb_track ? _PAGE_TLB_TRACKING: 0;
894 #endif
895 res |= flags & ASSIGN_pgc_allocated ? _PAGE_PGC_ALLOCATED: 0;
896 res |= flags & ASSIGN_io ? _PAGE_IO: 0;
898 return res;
899 }
901 /* map a physical address to the specified metaphysical addr */
902 // flags: currently only ASSIGN_readonly, ASSIGN_nocache, ASSIGN_tlb_tack
903 // This is called by assign_domain_mmio_page().
904 // So accessing to pte is racy.
905 int
906 __assign_domain_page(struct domain *d,
907 unsigned long mpaddr, unsigned long physaddr,
908 unsigned long flags)
909 {
910 volatile pte_t *pte;
911 pte_t old_pte;
912 pte_t new_pte;
913 pte_t ret_pte;
914 unsigned long prot = flags_to_prot(flags);
916 pte = lookup_alloc_domain_pte(d, mpaddr);
918 old_pte = __pte(0);
919 new_pte = pfn_pte(physaddr >> PAGE_SHIFT, __pgprot(prot));
920 ret_pte = ptep_cmpxchg_rel(&d->arch.mm, mpaddr, pte, old_pte, new_pte);
921 if (pte_val(ret_pte) == pte_val(old_pte)) {
922 smp_mb();
923 return 0;
924 }
926 // dom0 tries to map real machine's I/O region, but failed.
927 // It is very likely that dom0 doesn't boot correctly because
928 // it can't access I/O. So complain here.
929 if (flags & ASSIGN_nocache) {
930 int warn = 0;
932 if (pte_pfn(ret_pte) != (physaddr >> PAGE_SHIFT))
933 warn = 1;
934 else if (!(pte_val(ret_pte) & _PAGE_MA_UC)) {
935 u32 type;
936 u64 attr;
938 warn = 1;
940 /*
941 * See
942 * complete_dom0_memmap()
943 * case EFI_RUNTIME_SERVICES_CODE:
944 * case EFI_RUNTIME_SERVICES_DATA:
945 * case EFI_ACPI_RECLAIM_MEMORY:
946 * case EFI_ACPI_MEMORY_NVS:
947 * case EFI_RESERVED_TYPE:
948 *
949 * Currently only EFI_RUNTIME_SERVICES_CODE is found
950 * so that we suppress only EFI_RUNTIME_SERVICES_CODE case.
951 */
952 type = efi_mem_type(physaddr);
953 attr = efi_mem_attributes(physaddr);
954 if (type == EFI_RUNTIME_SERVICES_CODE &&
955 (attr & EFI_MEMORY_UC) && (attr & EFI_MEMORY_WB))
956 warn = 0;
957 }
958 if (warn)
959 printk("%s:%d WARNING can't assign page domain 0x%p id %d\n"
960 "\talready assigned pte_val 0x%016lx\n"
961 "\tmpaddr 0x%016lx physaddr 0x%016lx flags 0x%lx\n",
962 __func__, __LINE__,
963 d, d->domain_id, pte_val(ret_pte),
964 mpaddr, physaddr, flags);
965 }
967 return -EAGAIN;
968 }
970 /* get_page() and map a physical address to the specified metaphysical addr */
971 void
972 assign_domain_page(struct domain *d,
973 unsigned long mpaddr, unsigned long physaddr)
974 {
975 struct page_info* page = mfn_to_page(physaddr >> PAGE_SHIFT);
977 BUG_ON((physaddr & _PAGE_PPN_MASK) != physaddr);
978 BUG_ON(page->count_info != (PGC_allocated | 1));
979 set_gpfn_from_mfn(physaddr >> PAGE_SHIFT, mpaddr >> PAGE_SHIFT);
980 // because __assign_domain_page() uses set_pte_rel() which has
981 // release semantics, smp_mb() isn't needed.
982 (void)__assign_domain_page(d, mpaddr, physaddr,
983 ASSIGN_writable | ASSIGN_pgc_allocated);
984 }
986 /*
987 * Inpurt
988 * fgp: first guest port
989 * fmp: first machine port
990 * lmp: last machine port
991 */
992 int
993 ioports_permit_access(struct domain *d, unsigned int fgp,
994 unsigned int fmp, unsigned int lmp)
995 {
996 struct io_space *space;
997 unsigned long mmio_start, mach_start, mach_end;
998 int ret;
1000 if (IO_SPACE_NR(fmp) >= num_io_spaces) {
1001 dprintk(XENLOG_WARNING, "Unknown I/O Port range 0x%x - 0x%x\n", fmp, lmp);
1002 return -EFAULT;
1005 /*
1006 * The ioport_cap rangeset tracks the I/O port address including
1007 * the port space ID. This means port space IDs need to match
1008 * between Xen and dom0. This is also a requirement because
1009 * the hypercall to pass these port ranges only uses a u32.
1011 * NB - non-dom0 driver domains may only have a subset of the
1012 * I/O port spaces and thus will number port spaces differently.
1013 * This is ok, they don't make use of this interface.
1014 */
1015 ret = rangeset_add_range(d->arch.ioport_caps, fmp, lmp);
1016 if (ret != 0)
1017 return ret;
1019 space = &io_space[IO_SPACE_NR(fmp)];
1021 /* Legacy I/O on dom0 is already setup */
1022 if (d == dom0 && space == &io_space[0])
1023 return 0;
1025 fmp = IO_SPACE_PORT(fmp);
1026 lmp = IO_SPACE_PORT(lmp);
1028 if (space->sparse) {
1029 mach_start = IO_SPACE_SPARSE_ENCODING(fmp) & PAGE_MASK;
1030 mach_end = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lmp));
1031 } else {
1032 mach_start = fmp & PAGE_MASK;
1033 mach_end = PAGE_ALIGN(lmp);
1036 /*
1037 * The "machine first port" is not necessarily identity mapped
1038 * to the guest first port. At least for the legacy range.
1039 */
1040 mach_start = mach_start | __pa(space->mmio_base);
1041 mach_end = mach_end | __pa(space->mmio_base);
1043 mmio_start = IO_SPACE_SPARSE_ENCODING(fgp) & PAGE_MASK;
1045 if (VMX_DOMAIN(d->vcpu[0]))
1046 mmio_start |= LEGACY_IO_START;
1047 else if (space == &io_space[0])
1048 mmio_start |= IO_PORTS_PADDR;
1049 else
1050 mmio_start |= __pa(space->mmio_base);
1052 while (mach_start < mach_end) {
1053 (void)__assign_domain_page(d, mmio_start, mach_start, ASSIGN_nocache);
1054 mmio_start += PAGE_SIZE;
1055 mach_start += PAGE_SIZE;
1058 return 0;
1061 static int
1062 ioports_has_allowed(struct domain *d, unsigned int fp, unsigned int lp)
1064 for (; fp < lp; fp++)
1065 if (rangeset_contains_singleton(d->arch.ioport_caps, fp))
1066 return 1;
1068 return 0;
1071 int
1072 ioports_deny_access(struct domain *d, unsigned int fp, unsigned int lp)
1074 int ret;
1075 struct mm_struct *mm = &d->arch.mm;
1076 unsigned long mmio_start, mmio_end, mmio_base;
1077 unsigned int fp_base, lp_base;
1078 struct io_space *space;
1080 if (IO_SPACE_NR(fp) >= num_io_spaces) {
1081 dprintk(XENLOG_WARNING, "Unknown I/O Port range 0x%x - 0x%x\n", fp, lp);
1082 return -EFAULT;
1085 ret = rangeset_remove_range(d->arch.ioport_caps, fp, lp);
1086 if (ret != 0)
1087 return ret;
1089 space = &io_space[IO_SPACE_NR(fp)];
1090 fp_base = IO_SPACE_PORT(fp);
1091 lp_base = IO_SPACE_PORT(lp);
1093 if (space->sparse) {
1094 mmio_start = IO_SPACE_SPARSE_ENCODING(fp_base) & PAGE_MASK;
1095 mmio_end = PAGE_ALIGN(IO_SPACE_SPARSE_ENCODING(lp_base));
1096 } else {
1097 mmio_start = fp_base & PAGE_MASK;
1098 mmio_end = PAGE_ALIGN(lp_base);
1101 if (VMX_DOMAIN(d->vcpu[0]))
1102 mmio_base = LEGACY_IO_START;
1103 else if (space == &io_space[0] && d != dom0)
1104 mmio_base = IO_PORTS_PADDR;
1105 else
1106 mmio_base = __pa(space->mmio_base);
1108 for (; mmio_start < mmio_end; mmio_start += PAGE_SIZE) {
1109 unsigned int port, range;
1110 unsigned long mpaddr;
1111 volatile pte_t *pte;
1112 pte_t old_pte;
1114 if (space->sparse) {
1115 port = IO_SPACE_SPARSE_DECODING(mmio_start);
1116 range = IO_SPACE_SPARSE_PORTS_PER_PAGE - 1;
1117 } else {
1118 port = mmio_start;
1119 range = PAGE_SIZE - 1;
1122 port |= IO_SPACE_BASE(IO_SPACE_NR(fp));
1124 if (port < fp || port + range > lp) {
1125 /* Maybe this covers an allowed port. */
1126 if (ioports_has_allowed(d, port, port + range))
1127 continue;
1130 mpaddr = mmio_start | mmio_base;
1131 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
1132 BUG_ON(pte == NULL);
1133 BUG_ON(pte_none(*pte));
1135 /* clear pte */
1136 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
1138 domain_flush_vtlb_all(d);
1139 return 0;
1142 static void
1143 assign_domain_same_page(struct domain *d,
1144 unsigned long mpaddr, unsigned long size,
1145 unsigned long flags)
1147 //XXX optimization
1148 unsigned long end = PAGE_ALIGN(mpaddr + size);
1149 for (mpaddr &= PAGE_MASK; mpaddr < end; mpaddr += PAGE_SIZE) {
1150 (void)__assign_domain_page(d, mpaddr, mpaddr, flags);
1154 int
1155 efi_mmio(unsigned long physaddr, unsigned long size)
1157 void *efi_map_start, *efi_map_end;
1158 u64 efi_desc_size;
1159 void* p;
1161 efi_map_start = __va(ia64_boot_param->efi_memmap);
1162 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1163 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1165 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1166 efi_memory_desc_t* md = (efi_memory_desc_t *)p;
1167 unsigned long start = md->phys_addr;
1168 unsigned long end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
1170 if (start <= physaddr && physaddr < end) {
1171 if ((physaddr + size) > end) {
1172 gdprintk(XENLOG_INFO, "%s: physaddr 0x%lx size = 0x%lx\n",
1173 __func__, physaddr, size);
1174 return 0;
1177 // for io space
1178 if (md->type == EFI_MEMORY_MAPPED_IO ||
1179 md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
1180 return 1;
1183 // for runtime
1184 // see efi_enter_virtual_mode(void)
1185 // in linux/arch/ia64/kernel/efi.c
1186 if ((md->attribute & EFI_MEMORY_RUNTIME) &&
1187 !(md->attribute & EFI_MEMORY_WB)) {
1188 return 1;
1191 return 0;
1194 if (physaddr < start) {
1195 break;
1199 return 1;
1202 unsigned long
1203 assign_domain_mmio_page(struct domain *d, unsigned long mpaddr,
1204 unsigned long phys_addr, unsigned long size,
1205 unsigned long flags)
1207 unsigned long addr = mpaddr & PAGE_MASK;
1208 unsigned long end = PAGE_ALIGN(mpaddr + size);
1210 if (size == 0) {
1211 gdprintk(XENLOG_INFO, "%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
1212 __func__, d, mpaddr, size);
1214 if (!efi_mmio(phys_addr, size)) {
1215 #ifndef NDEBUG
1216 gdprintk(XENLOG_INFO, "%s: domain %p mpaddr 0x%lx size = 0x%lx\n",
1217 __func__, d, mpaddr, size);
1218 #endif
1219 return -EINVAL;
1222 for (phys_addr &= PAGE_MASK; addr < end;
1223 addr += PAGE_SIZE, phys_addr += PAGE_SIZE) {
1224 __assign_domain_page(d, addr, phys_addr, flags);
1227 return mpaddr;
1230 unsigned long
1231 assign_domain_mach_page(struct domain *d,
1232 unsigned long mpaddr, unsigned long size,
1233 unsigned long flags)
1235 BUG_ON(flags & ASSIGN_pgc_allocated);
1236 assign_domain_same_page(d, mpaddr, size, flags);
1237 return mpaddr;
1240 static void
1241 adjust_page_count_info(struct page_info* page)
1243 struct domain* d = page_get_owner(page);
1244 BUG_ON((page->count_info & PGC_count_mask) < 1);
1245 if (d != NULL) {
1246 int ret = get_page(page, d);
1247 BUG_ON(ret == 0);
1248 } else {
1249 u64 x, nx, y;
1251 y = *((u64*)&page->count_info);
1252 do {
1253 x = y;
1254 nx = x + 1;
1256 BUG_ON((x >> 32) != 0);
1257 BUG_ON((nx & PGC_count_mask) != 2);
1258 y = cmpxchg((u64*)&page->count_info, x, nx);
1259 } while (unlikely(y != x));
1263 static void
1264 domain_put_page(struct domain* d, unsigned long mpaddr,
1265 volatile pte_t* ptep, pte_t old_pte, int clear_PGC_allocate)
1267 unsigned long mfn = pte_pfn(old_pte);
1268 struct page_info* page = mfn_to_page(mfn);
1270 if (pte_pgc_allocated(old_pte)) {
1271 if (page_get_owner(page) == d || page_get_owner(page) == NULL) {
1272 BUG_ON(get_gpfn_from_mfn(mfn) != (mpaddr >> PAGE_SHIFT));
1273 set_gpfn_from_mfn(mfn, INVALID_M2P_ENTRY);
1274 } else {
1275 BUG();
1278 if (likely(clear_PGC_allocate)) {
1279 if (!test_and_clear_bit(_PGC_allocated, &page->count_info))
1280 BUG();
1281 /* put_page() is done by domain_page_flush_and_put() */
1282 } else {
1283 // In this case, page reference count mustn't touched.
1284 // domain_page_flush_and_put() decrements it, we increment
1285 // it in advence. This patch is slow path.
1286 //
1287 // guest_remove_page(): owner = d, count_info = 1
1288 // memory_exchange(): owner = NULL, count_info = 1
1289 // XENMEM_add_to_physmap: ower = d, count_info >= 1
1290 adjust_page_count_info(page);
1293 domain_page_flush_and_put(d, mpaddr, ptep, old_pte, page);
1296 // caller must get_page(mfn_to_page(mfn)) before call.
1297 // caller must call set_gpfn_from_mfn() before call if necessary.
1298 // because set_gpfn_from_mfn() result must be visible before pte xchg
1299 // caller must use memory barrier. NOTE: xchg has acquire semantics.
1300 // flags: ASSIGN_xxx
1301 static void
1302 assign_domain_page_replace(struct domain *d, unsigned long mpaddr,
1303 unsigned long mfn, unsigned long flags)
1305 struct mm_struct *mm = &d->arch.mm;
1306 volatile pte_t* pte;
1307 pte_t old_pte;
1308 pte_t npte;
1309 unsigned long prot = flags_to_prot(flags);
1311 pte = lookup_alloc_domain_pte(d, mpaddr);
1313 // update pte
1314 npte = pfn_pte(mfn, __pgprot(prot));
1315 old_pte = ptep_xchg(mm, mpaddr, pte, npte);
1316 if (pte_mem(old_pte)) {
1317 unsigned long old_mfn = pte_pfn(old_pte);
1319 // mfn = old_mfn case can happen when domain maps a granted page
1320 // twice with the same pseudo physial address.
1321 // It's non sense, but allowed.
1322 // __gnttab_map_grant_ref()
1323 // => create_host_mapping()
1324 // => assign_domain_page_replace()
1325 if (mfn != old_mfn) {
1326 domain_put_page(d, mpaddr, pte, old_pte, 1);
1329 perfc_incr(assign_domain_page_replace);
1332 // caller must get_page(new_page) before
1333 // Only steal_page() calls this function.
1334 static int
1335 assign_domain_page_cmpxchg_rel(struct domain* d, unsigned long mpaddr,
1336 struct page_info* old_page,
1337 struct page_info* new_page,
1338 unsigned long flags, int clear_PGC_allocate)
1340 struct mm_struct *mm = &d->arch.mm;
1341 volatile pte_t* pte;
1342 unsigned long old_mfn;
1343 unsigned long old_prot;
1344 pte_t old_pte;
1345 unsigned long new_mfn;
1346 unsigned long new_prot;
1347 pte_t new_pte;
1348 pte_t ret_pte;
1350 BUG_ON((flags & ASSIGN_pgc_allocated) == 0);
1351 pte = lookup_alloc_domain_pte(d, mpaddr);
1353 again:
1354 old_prot = pte_val(*pte) & ~_PAGE_PPN_MASK;
1355 old_mfn = page_to_mfn(old_page);
1356 old_pte = pfn_pte(old_mfn, __pgprot(old_prot));
1357 if (!pte_present(old_pte)) {
1358 gdprintk(XENLOG_INFO,
1359 "%s: old_pte 0x%lx old_prot 0x%lx old_mfn 0x%lx\n",
1360 __func__, pte_val(old_pte), old_prot, old_mfn);
1361 return -EINVAL;
1364 new_prot = flags_to_prot(flags);
1365 new_mfn = page_to_mfn(new_page);
1366 new_pte = pfn_pte(new_mfn, __pgprot(new_prot));
1368 // update pte
1369 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1370 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1371 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1372 goto again;
1375 gdprintk(XENLOG_INFO,
1376 "%s: old_pte 0x%lx old_prot 0x%lx old_mfn 0x%lx "
1377 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1378 __func__,
1379 pte_val(old_pte), old_prot, old_mfn,
1380 pte_val(ret_pte), pte_pfn(ret_pte));
1381 return -EINVAL;
1384 BUG_ON(!pte_mem(old_pte));
1385 BUG_ON(!pte_pgc_allocated(old_pte));
1386 BUG_ON(page_get_owner(old_page) != d);
1387 BUG_ON(get_gpfn_from_mfn(old_mfn) != (mpaddr >> PAGE_SHIFT));
1388 BUG_ON(old_mfn == new_mfn);
1390 set_gpfn_from_mfn(old_mfn, INVALID_M2P_ENTRY);
1391 if (likely(clear_PGC_allocate)) {
1392 if (!test_and_clear_bit(_PGC_allocated, &old_page->count_info))
1393 BUG();
1394 } else {
1395 int ret;
1396 // adjust for count_info for domain_page_flush_and_put()
1397 // This is slow path.
1398 BUG_ON(!test_bit(_PGC_allocated, &old_page->count_info));
1399 BUG_ON(d == NULL);
1400 ret = get_page(old_page, d);
1401 BUG_ON(ret == 0);
1404 domain_page_flush_and_put(d, mpaddr, pte, old_pte, old_page);
1405 perfc_incr(assign_domain_pge_cmpxchg_rel);
1406 return 0;
1409 static void
1410 zap_domain_page_one(struct domain *d, unsigned long mpaddr,
1411 int clear_PGC_allocate, unsigned long mfn)
1413 struct mm_struct *mm = &d->arch.mm;
1414 volatile pte_t *pte;
1415 pte_t old_pte;
1416 struct page_info *page;
1418 pte = lookup_noalloc_domain_pte_none(d, mpaddr);
1419 if (pte == NULL)
1420 return;
1421 if (pte_none(*pte))
1422 return;
1424 if (mfn == INVALID_MFN) {
1425 // clear pte
1426 old_pte = ptep_get_and_clear(mm, mpaddr, pte);
1427 mfn = pte_pfn(old_pte);
1428 } else {
1429 unsigned long old_arflags;
1430 pte_t new_pte;
1431 pte_t ret_pte;
1433 again:
1434 // memory_exchange() calls guest_physmap_remove_page() with
1435 // a stealed page. i.e. page owner = NULL.
1436 BUG_ON(page_get_owner(mfn_to_page(mfn)) != d &&
1437 page_get_owner(mfn_to_page(mfn)) != NULL);
1438 old_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
1439 old_pte = pfn_pte(mfn, __pgprot(old_arflags));
1440 new_pte = __pte(0);
1442 // update pte
1443 ret_pte = ptep_cmpxchg_rel(mm, mpaddr, pte, old_pte, new_pte);
1444 if (unlikely(pte_val(old_pte) != pte_val(ret_pte))) {
1445 if (pte_pfn(old_pte) == pte_pfn(ret_pte)) {
1446 goto again;
1449 gdprintk(XENLOG_INFO, "%s: old_pte 0x%lx old_arflags 0x%lx mfn 0x%lx "
1450 "ret_pte 0x%lx ret_mfn 0x%lx\n",
1451 __func__,
1452 pte_val(old_pte), old_arflags, mfn,
1453 pte_val(ret_pte), pte_pfn(ret_pte));
1454 return;
1456 BUG_ON(mfn != pte_pfn(ret_pte));
1459 page = mfn_to_page(mfn);
1460 BUG_ON((page->count_info & PGC_count_mask) == 0);
1462 BUG_ON(clear_PGC_allocate && (page_get_owner(page) == NULL));
1463 domain_put_page(d, mpaddr, pte, old_pte, clear_PGC_allocate);
1464 perfc_incr(zap_domain_page_one);
1467 unsigned long
1468 dom0vp_zap_physmap(struct domain *d, unsigned long gpfn,
1469 unsigned int extent_order)
1471 if (extent_order != 0) {
1472 //XXX
1473 return -ENOSYS;
1476 zap_domain_page_one(d, gpfn << PAGE_SHIFT, 1, INVALID_MFN);
1477 perfc_incr(dom0vp_zap_physmap);
1478 return 0;
1481 static unsigned long
1482 __dom0vp_add_physmap(struct domain* d, unsigned long gpfn,
1483 unsigned long mfn_or_gmfn,
1484 unsigned long flags, domid_t domid, int is_gmfn)
1486 int error = -EINVAL;
1487 struct domain* rd;
1488 unsigned long mfn;
1490 /* Not allowed by a domain. */
1491 if (flags & (ASSIGN_nocache | ASSIGN_pgc_allocated))
1492 return -EINVAL;
1494 rd = rcu_lock_domain_by_id(domid);
1495 if (unlikely(rd == NULL)) {
1496 switch (domid) {
1497 case DOMID_XEN:
1498 rd = dom_xen;
1499 break;
1500 case DOMID_IO:
1501 rd = dom_io;
1502 break;
1503 default:
1504 gdprintk(XENLOG_INFO, "d 0x%p domid %d "
1505 "gpfn 0x%lx mfn_or_gmfn 0x%lx flags 0x%lx domid %d\n",
1506 d, d->domain_id, gpfn, mfn_or_gmfn, flags, domid);
1507 return -ESRCH;
1509 BUG_ON(rd == NULL);
1510 rcu_lock_domain(rd);
1513 if (unlikely(rd == d))
1514 goto out1;
1515 /*
1516 * DOMID_XEN and DOMID_IO don't have their own p2m table.
1517 * It can be considered that their p2m conversion is p==m.
1518 */
1519 if (likely(is_gmfn && domid != DOMID_XEN && domid != DOMID_IO))
1520 mfn = gmfn_to_mfn(rd, mfn_or_gmfn);
1521 else
1522 mfn = mfn_or_gmfn;
1523 if (unlikely(!mfn_valid(mfn) || get_page(mfn_to_page(mfn), rd) == 0))
1524 goto out1;
1526 error = 0;
1527 BUG_ON(page_get_owner(mfn_to_page(mfn)) == d &&
1528 get_gpfn_from_mfn(mfn) != INVALID_M2P_ENTRY);
1529 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn, flags);
1530 //don't update p2m table because this page belongs to rd, not d.
1531 perfc_incr(dom0vp_add_physmap);
1532 out1:
1533 rcu_unlock_domain(rd);
1534 return error;
1537 unsigned long
1538 dom0vp_add_physmap(struct domain* d, unsigned long gpfn, unsigned long mfn,
1539 unsigned long flags, domid_t domid)
1541 return __dom0vp_add_physmap(d, gpfn, mfn, flags, domid, 0);
1544 unsigned long
1545 dom0vp_add_physmap_with_gmfn(struct domain* d, unsigned long gpfn,
1546 unsigned long gmfn, unsigned long flags,
1547 domid_t domid)
1549 return __dom0vp_add_physmap(d, gpfn, gmfn, flags, domid, 1);
1552 #ifdef CONFIG_XEN_IA64_EXPOSE_P2M
1553 #define P2M_PFN_ROUNDUP(x) (((x) + PTRS_PER_PTE - 1) & \
1554 ~(PTRS_PER_PTE - 1))
1555 #define P2M_PFN_ROUNDDOWN(x) ((x) & ~(PTRS_PER_PTE - 1))
1556 #define P2M_NUM_PFN(x) (((x) + PTRS_PER_PTE - 1) / PTRS_PER_PTE)
1557 #define MD_END(md) ((md)->phys_addr + \
1558 ((md)->num_pages << EFI_PAGE_SHIFT))
1559 static struct page_info* p2m_pte_zero_page = NULL;
1561 /* This must called before dom0 p2m table allocation */
1562 void __init
1563 expose_p2m_init(void)
1565 pte_t* pte;
1567 /*
1568 * Initialise our DOMID_P2M domain.
1569 * This domain owns m2p table pages.
1570 */
1571 dom_p2m = domain_create(DOMID_P2M, DOMCRF_dummy, 0);
1572 BUG_ON(dom_p2m == NULL);
1573 dom_p2m->max_pages = ~0U;
1575 pte = pte_alloc_one_kernel(NULL, 0);
1576 BUG_ON(pte == NULL);
1577 smp_mb();// make contents of the page visible.
1578 p2m_pte_zero_page = virt_to_page(pte);
1581 // allocate pgd, pmd of dest_dom if necessary
1582 static int
1583 allocate_pgd_pmd(struct domain* dest_dom, unsigned long dest_gpfn,
1584 struct domain* src_dom,
1585 unsigned long src_gpfn, unsigned long num_src_gpfn)
1587 unsigned long i = 0;
1589 BUG_ON((src_gpfn % PTRS_PER_PTE) != 0);
1590 BUG_ON((num_src_gpfn % PTRS_PER_PTE) != 0);
1592 while (i < num_src_gpfn) {
1593 volatile pte_t* src_pte;
1594 volatile pte_t* dest_pte;
1596 src_pte = lookup_noalloc_domain_pte(src_dom,
1597 (src_gpfn + i) << PAGE_SHIFT);
1598 if (src_pte == NULL) {
1599 i++;
1600 continue;
1603 dest_pte = lookup_alloc_domain_pte(dest_dom,
1604 (dest_gpfn << PAGE_SHIFT) +
1605 i * sizeof(pte_t));
1606 if (dest_pte == NULL) {
1607 gdprintk(XENLOG_INFO, "%s failed to allocate pte page\n",
1608 __func__);
1609 return -ENOMEM;
1612 // skip to next pte page
1613 i = P2M_PFN_ROUNDDOWN(i + PTRS_PER_PTE);
1615 return 0;
1618 static int
1619 expose_p2m_page(struct domain* d, unsigned long mpaddr, struct page_info* page)
1621 int ret = get_page(page, dom_p2m);
1622 BUG_ON(ret != 1);
1623 return __assign_domain_page(d, mpaddr, page_to_maddr(page),
1624 ASSIGN_readonly);
1627 // expose pte page
1628 static int
1629 expose_p2m_range(struct domain* dest_dom, unsigned long dest_gpfn,
1630 struct domain* src_dom,
1631 unsigned long src_gpfn, unsigned long num_src_gpfn)
1633 unsigned long i = 0;
1635 BUG_ON((src_gpfn % PTRS_PER_PTE) != 0);
1636 BUG_ON((num_src_gpfn % PTRS_PER_PTE) != 0);
1638 while (i < num_src_gpfn) {
1639 volatile pte_t* pte;
1641 pte = lookup_noalloc_domain_pte(src_dom, (src_gpfn + i) << PAGE_SHIFT);
1642 if (pte == NULL) {
1643 i++;
1644 continue;
1647 if (expose_p2m_page(dest_dom,
1648 (dest_gpfn << PAGE_SHIFT) + i * sizeof(pte_t),
1649 virt_to_page(pte)) < 0) {
1650 gdprintk(XENLOG_INFO, "%s failed to assign page\n", __func__);
1651 return -EAGAIN;
1654 // skip to next pte page
1655 i = P2M_PFN_ROUNDDOWN(i + PTRS_PER_PTE);
1657 return 0;
1660 // expose p2m_pte_zero_page
1661 static int
1662 expose_zero_page(struct domain* dest_dom, unsigned long dest_gpfn,
1663 unsigned long num_src_gpfn)
1665 unsigned long i;
1667 for (i = 0; i < P2M_NUM_PFN(num_src_gpfn); i++) {
1668 volatile pte_t* pte;
1669 pte = lookup_noalloc_domain_pte(dest_dom,
1670 (dest_gpfn + i) << PAGE_SHIFT);
1671 if (pte == NULL || pte_present(*pte))
1672 continue;
1674 if (expose_p2m_page(dest_dom, (dest_gpfn + i) << PAGE_SHIFT,
1675 p2m_pte_zero_page) < 0) {
1676 gdprintk(XENLOG_INFO, "%s failed to assign zero-pte page\n",
1677 __func__);
1678 return -EAGAIN;
1681 return 0;
1684 static int
1685 expose_p2m(struct domain* dest_dom, unsigned long dest_gpfn,
1686 struct domain* src_dom,
1687 unsigned long src_gpfn, unsigned long num_src_gpfn)
1689 if (allocate_pgd_pmd(dest_dom, dest_gpfn,
1690 src_dom, src_gpfn, num_src_gpfn))
1691 return -ENOMEM;
1693 if (expose_p2m_range(dest_dom, dest_gpfn,
1694 src_dom, src_gpfn, num_src_gpfn))
1695 return -EAGAIN;
1697 if (expose_zero_page(dest_dom, dest_gpfn, num_src_gpfn))
1698 return -EAGAIN;
1700 return 0;
1703 static void
1704 unexpose_p2m(struct domain* dest_dom,
1705 unsigned long dest_gpfn, unsigned long num_dest_gpfn)
1707 unsigned long i;
1709 for (i = 0; i < num_dest_gpfn; i++) {
1710 zap_domain_page_one(dest_dom, (dest_gpfn + i) << PAGE_SHIFT,
1711 0, INVALID_MFN);
1715 // It is possible to optimize loop, But this isn't performance critical.
1716 unsigned long
1717 dom0vp_expose_p2m(struct domain* d,
1718 unsigned long conv_start_gpfn,
1719 unsigned long assign_start_gpfn,
1720 unsigned long expose_size, unsigned long granule_pfn)
1722 unsigned long ret;
1723 unsigned long expose_num_pfn = expose_size >> PAGE_SHIFT;
1725 if ((expose_size % PAGE_SIZE) != 0 ||
1726 (granule_pfn % PTRS_PER_PTE) != 0 ||
1727 (expose_num_pfn % PTRS_PER_PTE) != 0 ||
1728 (conv_start_gpfn % granule_pfn) != 0 ||
1729 (assign_start_gpfn % granule_pfn) != 0 ||
1730 (expose_num_pfn % granule_pfn) != 0) {
1731 gdprintk(XENLOG_INFO,
1732 "%s conv_start_gpfn 0x%016lx assign_start_gpfn 0x%016lx "
1733 "expose_size 0x%016lx granulte_pfn 0x%016lx\n", __func__,
1734 conv_start_gpfn, assign_start_gpfn, expose_size, granule_pfn);
1735 return -EINVAL;
1738 if (granule_pfn != PTRS_PER_PTE) {
1739 gdprintk(XENLOG_INFO,
1740 "%s granule_pfn 0x%016lx PTRS_PER_PTE 0x%016lx\n",
1741 __func__, granule_pfn, PTRS_PER_PTE);
1742 return -ENOSYS;
1744 ret = expose_p2m(d, assign_start_gpfn,
1745 d, conv_start_gpfn, expose_num_pfn);
1746 return ret;
1749 static int
1750 memmap_info_copy_from_guest(struct xen_ia64_memmap_info* memmap_info,
1751 char** memmap_p,
1752 XEN_GUEST_HANDLE(char) buffer)
1754 char *memmap;
1755 char *p;
1756 char *memmap_end;
1757 efi_memory_desc_t *md;
1758 unsigned long start;
1759 unsigned long end;
1760 efi_memory_desc_t *prev_md;
1762 if (copy_from_guest((char*)memmap_info, buffer, sizeof(*memmap_info)))
1763 return -EFAULT;
1764 if (memmap_info->efi_memdesc_size < sizeof(efi_memory_desc_t) ||
1765 memmap_info->efi_memmap_size < memmap_info->efi_memdesc_size ||
1766 (memmap_info->efi_memmap_size % memmap_info->efi_memdesc_size) != 0)
1767 return -EINVAL;
1769 memmap = _xmalloc(memmap_info->efi_memmap_size,
1770 __alignof__(efi_memory_desc_t));
1771 if (memmap == NULL)
1772 return -ENOMEM;
1773 if (copy_from_guest_offset(memmap, buffer, sizeof(*memmap_info),
1774 memmap_info->efi_memmap_size)) {
1775 xfree(memmap);
1776 return -EFAULT;
1779 /* intergirty check & simplify */
1780 sort(memmap, memmap_info->efi_memmap_size / memmap_info->efi_memdesc_size,
1781 memmap_info->efi_memdesc_size, efi_mdt_cmp, NULL);
1783 /* alignement & overlap check */
1784 prev_md = NULL;
1785 p = memmap;
1786 memmap_end = memmap + memmap_info->efi_memmap_size;
1787 for (p = memmap; p < memmap_end; p += memmap_info->efi_memmap_size) {
1788 md = (efi_memory_desc_t*)p;
1789 start = md->phys_addr;
1791 if (start & ((1UL << EFI_PAGE_SHIFT) - 1) || md->num_pages == 0) {
1792 xfree(memmap);
1793 return -EINVAL;
1796 if (prev_md != NULL) {
1797 unsigned long prev_end = MD_END(prev_md);
1798 if (prev_end > start) {
1799 xfree(memmap);
1800 return -EINVAL;
1804 prev_md = (efi_memory_desc_t *)p;
1807 /* coalease */
1808 prev_md = NULL;
1809 p = memmap;
1810 while (p < memmap_end) {
1811 md = (efi_memory_desc_t*)p;
1812 start = md->phys_addr;
1813 end = MD_END(md);
1815 start = P2M_PFN_ROUNDDOWN(start >> PAGE_SHIFT) << PAGE_SHIFT;
1816 end = P2M_PFN_ROUNDUP(end >> PAGE_SHIFT) << PAGE_SHIFT;
1817 md->phys_addr = start;
1818 md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
1820 if (prev_md != NULL) {
1821 unsigned long prev_end = MD_END(prev_md);
1822 if (prev_end >= start) {
1823 size_t left;
1824 end = max(prev_end, end);
1825 prev_md->num_pages = (end - prev_md->phys_addr) >> EFI_PAGE_SHIFT;
1827 left = memmap_end - p;
1828 if (left > memmap_info->efi_memdesc_size) {
1829 left -= memmap_info->efi_memdesc_size;
1830 memmove(p, p + memmap_info->efi_memdesc_size, left);
1833 memmap_info->efi_memmap_size -= memmap_info->efi_memdesc_size;
1834 memmap_end -= memmap_info->efi_memdesc_size;
1835 continue;
1839 prev_md = md;
1840 p += memmap_info->efi_memdesc_size;
1843 if (copy_to_guest(buffer, (char*)memmap_info, sizeof(*memmap_info)) ||
1844 copy_to_guest_offset(buffer, sizeof(*memmap_info),
1845 (char*)memmap, memmap_info->efi_memmap_size)) {
1846 xfree(memmap);
1847 return -EFAULT;
1850 *memmap_p = memmap;
1851 return 0;
1854 static int
1855 foreign_p2m_allocate_pte(struct domain* d,
1856 const struct xen_ia64_memmap_info* memmap_info,
1857 const void* memmap)
1859 const void* memmap_end = memmap + memmap_info->efi_memmap_size;
1860 const void* p;
1862 for (p = memmap; p < memmap_end; p += memmap_info->efi_memdesc_size) {
1863 const efi_memory_desc_t* md = p;
1864 unsigned long start = md->phys_addr;
1865 unsigned long end = MD_END(md);
1866 unsigned long gpaddr;
1868 for (gpaddr = start; gpaddr < end; gpaddr += PAGE_SIZE) {
1869 if (lookup_alloc_domain_pte(d, gpaddr) == NULL) {
1870 return -ENOMEM;
1875 return 0;
1878 struct foreign_p2m_region {
1879 unsigned long gpfn;
1880 unsigned long num_gpfn;
1881 };
1883 struct foreign_p2m_entry {
1884 struct list_head list;
1885 int busy;
1887 /* src domain */
1888 struct domain* src_dom;
1890 /* region into which foreign p2m table is mapped */
1891 unsigned long gpfn;
1892 unsigned long num_gpfn;
1893 unsigned int num_region;
1894 struct foreign_p2m_region region[0];
1895 };
1897 /* caller must increment the reference count of src_dom */
1898 static int
1899 foreign_p2m_alloc(struct foreign_p2m* foreign_p2m,
1900 unsigned long dest_gpfn, struct domain* src_dom,
1901 struct xen_ia64_memmap_info* memmap_info, void* memmap,
1902 struct foreign_p2m_entry** entryp)
1904 void* memmap_end = memmap + memmap_info->efi_memmap_size;
1905 efi_memory_desc_t* md;
1906 unsigned long dest_gpfn_end;
1907 unsigned long src_gpfn;
1908 unsigned long src_gpfn_end;
1910 unsigned int num_region;
1911 struct foreign_p2m_entry* entry;
1912 struct foreign_p2m_entry* prev;
1913 struct foreign_p2m_entry* pos;
1915 num_region = (memmap_end - memmap) / memmap_info->efi_memdesc_size;
1917 md = memmap;
1918 src_gpfn = P2M_PFN_ROUNDDOWN(md->phys_addr >> PAGE_SHIFT);
1920 md = memmap + (num_region - 1) * memmap_info->efi_memdesc_size;
1921 src_gpfn_end = MD_END(md) >> PAGE_SHIFT;
1922 if (src_gpfn_end >
1923 P2M_PFN_ROUNDUP(src_dom->arch.convmem_end >> PAGE_SHIFT))
1924 return -EINVAL;
1926 src_gpfn_end = P2M_PFN_ROUNDUP(src_gpfn_end);
1927 dest_gpfn_end = dest_gpfn + P2M_NUM_PFN(src_gpfn_end - src_gpfn);
1928 entry = _xmalloc(sizeof(*entry) + num_region * sizeof(entry->region[0]),
1929 __alignof__(*entry));
1930 if (entry == NULL)
1931 return -ENOMEM;
1933 entry->busy = 1;
1934 entry->gpfn = dest_gpfn;
1935 entry->num_gpfn = dest_gpfn_end - dest_gpfn;
1936 entry->src_dom = src_dom;
1937 entry->num_region = 0;
1938 memset(entry->region, 0, sizeof(entry->region[0]) * num_region);
1939 prev = NULL;
1941 spin_lock(&foreign_p2m->lock);
1942 if (list_empty(&foreign_p2m->head))
1943 prev = (struct foreign_p2m_entry*)&foreign_p2m->head;
1945 list_for_each_entry(pos, &foreign_p2m->head, list) {
1946 if (pos->gpfn + pos->num_gpfn < dest_gpfn) {
1947 prev = pos;
1948 continue;
1951 if (dest_gpfn_end < pos->gpfn) {
1952 if (prev != NULL && prev->gpfn + prev->num_gpfn > dest_gpfn)
1953 prev = NULL;/* overlap */
1954 break;
1957 /* overlap */
1958 prev = NULL;
1959 break;
1961 if (prev != NULL) {
1962 list_add(&entry->list, &prev->list);
1963 spin_unlock(&foreign_p2m->lock);
1964 *entryp = entry;
1965 return 0;
1967 spin_unlock(&foreign_p2m->lock);
1968 xfree(entry);
1969 return -EBUSY;
1972 static void
1973 foreign_p2m_unexpose(struct domain* dest_dom, struct foreign_p2m_entry* entry)
1975 unsigned int i;
1977 BUG_ON(!entry->busy);
1978 for (i = 0; i < entry->num_region; i++)
1979 unexpose_p2m(dest_dom,
1980 entry->region[i].gpfn, entry->region[i].num_gpfn);
1983 static void
1984 foreign_p2m_unbusy(struct foreign_p2m* foreign_p2m,
1985 struct foreign_p2m_entry* entry)
1987 spin_lock(&foreign_p2m->lock);
1988 BUG_ON(!entry->busy);
1989 entry->busy = 0;
1990 spin_unlock(&foreign_p2m->lock);
1993 static void
1994 foreign_p2m_free(struct foreign_p2m* foreign_p2m,
1995 struct foreign_p2m_entry* entry)
1997 spin_lock(&foreign_p2m->lock);
1998 BUG_ON(!entry->busy);
1999 list_del(&entry->list);
2000 spin_unlock(&foreign_p2m->lock);
2002 put_domain(entry->src_dom);
2003 xfree(entry);
2006 void
2007 foreign_p2m_init(struct domain* d)
2009 struct foreign_p2m* foreign_p2m = &d->arch.foreign_p2m;
2010 INIT_LIST_HEAD(&foreign_p2m->head);
2011 spin_lock_init(&foreign_p2m->lock);
2014 void
2015 foreign_p2m_destroy(struct domain* d)
2017 struct foreign_p2m* foreign_p2m = &d->arch.foreign_p2m;
2018 struct foreign_p2m_entry* entry;
2019 struct foreign_p2m_entry* n;
2021 spin_lock(&foreign_p2m->lock);
2022 list_for_each_entry_safe(entry, n, &foreign_p2m->head, list) {
2023 /* mm_teardown() cleared p2m table already */
2024 /* foreign_p2m_unexpose(d, entry);*/
2025 list_del(&entry->list);
2026 put_domain(entry->src_dom);
2027 xfree(entry);
2029 spin_unlock(&foreign_p2m->lock);
2032 unsigned long
2033 dom0vp_expose_foreign_p2m(struct domain* dest_dom,
2034 unsigned long dest_gpfn,
2035 domid_t domid,
2036 XEN_GUEST_HANDLE(char) buffer,
2037 unsigned long flags)
2039 unsigned long ret = 0;
2040 struct domain* src_dom;
2041 struct xen_ia64_memmap_info memmap_info;
2042 char* memmap;
2043 void* memmap_end;
2044 void* p;
2046 struct foreign_p2m_entry* entry;
2048 ret = memmap_info_copy_from_guest(&memmap_info, &memmap, buffer);
2049 if (ret != 0)
2050 return ret;
2052 dest_dom = rcu_lock_domain(dest_dom);
2053 if (dest_dom == NULL) {
2054 ret = -EINVAL;
2055 goto out;
2057 #if 1
2058 // Self foreign domain p2m exposure isn't allowed.
2059 // Otherwise the domain can't be destroyed because
2060 // no one decrements the domain reference count.
2061 if (domid == dest_dom->domain_id) {
2062 ret = -EINVAL;
2063 goto out;
2065 #endif
2067 src_dom = get_domain_by_id(domid);
2068 if (src_dom == NULL) {
2069 ret = -EINVAL;
2070 goto out_unlock;
2073 if (flags & IA64_DOM0VP_EFP_ALLOC_PTE) {
2074 ret = foreign_p2m_allocate_pte(src_dom, &memmap_info, memmap);
2075 if (ret != 0)
2076 goto out_unlock;
2079 ret = foreign_p2m_alloc(&dest_dom->arch.foreign_p2m, dest_gpfn,
2080 src_dom, &memmap_info, memmap, &entry);
2081 if (ret != 0)
2082 goto out_unlock;
2084 memmap_end = memmap + memmap_info.efi_memmap_size;
2085 for (p = memmap; p < memmap_end; p += memmap_info.efi_memdesc_size) {
2086 efi_memory_desc_t* md = p;
2087 unsigned long src_gpfn =
2088 P2M_PFN_ROUNDDOWN(md->phys_addr >> PAGE_SHIFT);
2089 unsigned long src_gpfn_end =
2090 P2M_PFN_ROUNDUP(MD_END(md) >> PAGE_SHIFT);
2091 unsigned long num_src_gpfn = src_gpfn_end - src_gpfn;
2093 ret = expose_p2m(dest_dom, dest_gpfn + src_gpfn / PTRS_PER_PTE,
2094 src_dom, src_gpfn, num_src_gpfn);
2095 if (ret != 0)
2096 break;
2098 entry->region[entry->num_region].gpfn =
2099 dest_gpfn + src_gpfn / PTRS_PER_PTE;
2100 entry->region[entry->num_region].num_gpfn = P2M_NUM_PFN(num_src_gpfn);
2101 entry->num_region++;
2104 if (ret == 0) {
2105 foreign_p2m_unbusy(&dest_dom->arch.foreign_p2m, entry);
2106 } else {
2107 foreign_p2m_unexpose(dest_dom, entry);
2108 foreign_p2m_free(&dest_dom->arch.foreign_p2m, entry);
2111 out_unlock:
2112 rcu_unlock_domain(dest_dom);
2113 out:
2114 xfree(memmap);
2115 return ret;
2118 unsigned long
2119 dom0vp_unexpose_foreign_p2m(struct domain* dest_dom,
2120 unsigned long dest_gpfn,
2121 domid_t domid)
2123 int ret = -ENOENT;
2124 struct foreign_p2m* foreign_p2m = &dest_dom->arch.foreign_p2m;
2125 struct foreign_p2m_entry* entry;
2127 dest_dom = rcu_lock_domain(dest_dom);
2128 if (dest_dom == NULL)
2129 return ret;
2130 spin_lock(&foreign_p2m->lock);
2131 list_for_each_entry(entry, &foreign_p2m->head, list) {
2132 if (entry->gpfn < dest_gpfn)
2133 continue;
2134 if (dest_gpfn < entry->gpfn)
2135 break;
2137 if (domid == entry->src_dom->domain_id)
2138 ret = 0;
2139 else
2140 ret = -EINVAL;
2141 break;
2143 if (ret == 0) {
2144 if (entry->busy == 0)
2145 entry->busy = 1;
2146 else
2147 ret = -EBUSY;
2149 spin_unlock(&foreign_p2m->lock);
2151 if (ret == 0) {
2152 foreign_p2m_unexpose(dest_dom, entry);
2153 foreign_p2m_free(&dest_dom->arch.foreign_p2m, entry);
2155 rcu_unlock_domain(dest_dom);
2156 return ret;
2159 /* this lock can be only for memmap_info. domain_lock() is abused here */
2160 static void
2161 memmap_lock(struct domain *d)
2163 domain_lock(d);
2166 static void
2167 memmap_unlock(struct domain *d)
2169 domain_unlock(d);
2172 /* copy memory range to domain pseudo physical address space */
2173 static int
2174 __memmap_copy_to(struct domain *d, unsigned long dest_gpfn,
2175 void *src, unsigned long num_pages)
2177 BUG_ON(((unsigned long)src & ~PAGE_MASK) != 0);
2179 while (num_pages > 0) {
2180 unsigned long mfn;
2181 struct page_info *page;
2182 void *virt;
2184 mfn = gmfn_to_mfn_foreign(d, dest_gpfn);
2185 if (mfn == 0 || mfn == INVALID_MFN)
2186 return -EFAULT;
2187 page = mfn_to_page(mfn);
2188 if (get_page(page, d) == 0)
2189 return -EFAULT;
2190 virt = mfn_to_virt(mfn);
2191 copy_page(virt, src);
2192 __xencomm_mark_dirty(d, (unsigned long)virt, PAGE_SIZE);
2193 put_page(page);
2195 src += PAGE_SIZE;
2196 dest_gpfn++;
2197 num_pages--;
2200 return 0;
2203 /* copy memory range from domain pseudo physical address space */
2204 static int
2205 __memmap_copy_from(void *dest, struct domain *d, unsigned long src_gpfn,
2206 unsigned long num_pages)
2208 BUG_ON(((unsigned long)dest & ~PAGE_MASK) != 0);
2210 while (num_pages > 0) {
2211 unsigned long mfn;
2212 struct page_info *page;
2214 mfn = gmfn_to_mfn_foreign(d, src_gpfn);
2215 if (mfn == 0 || mfn == INVALID_MFN)
2216 return -EFAULT;
2217 page = mfn_to_page(mfn);
2218 if (get_page(page, d) == 0)
2219 return -EFAULT;
2220 copy_page(dest, mfn_to_virt(mfn));
2221 put_page(page);
2223 dest += PAGE_SIZE;
2224 src_gpfn++;
2225 num_pages--;
2228 return 0;
2231 /* This function unlock/lock memmeap_lock.
2232 * caller must free (*page, *order) even if error case by ckecking
2233 * *page = NULL.
2234 */
2235 static int
2236 memmap_copy_from(struct domain *d,
2237 struct page_info **page, unsigned long *order)
2239 unsigned long num_pages;
2240 struct xen_ia64_memmap_info *memmap_info;
2241 unsigned long memmap_info_pfn;
2243 num_pages = d->shared_info->arch.memmap_info_num_pages;
2244 memmap_unlock(d);
2246 again:
2247 *order = get_order(num_pages << PAGE_SHIFT);
2248 *page = alloc_domheap_pages(NULL, *order, 0);
2249 if (*page == NULL)
2250 return -ENOMEM;
2251 memmap_info = page_to_virt(*page);
2253 memmap_lock(d);
2254 if (d->shared_info->arch.memmap_info_num_pages != num_pages) {
2255 num_pages = d->shared_info->arch.memmap_info_num_pages;
2256 memmap_unlock(d);
2257 free_domheap_pages(*page, *order);
2258 goto again;
2260 memmap_info_pfn = d->shared_info->arch.memmap_info_pfn;
2262 /* copy into local to make them virtually contiguous */
2263 return __memmap_copy_from(memmap_info, d, memmap_info_pfn, num_pages);
2266 static int
2267 memdesc_can_expand(const struct xen_ia64_memmap_info *memmap_info,
2268 unsigned long num_pages)
2270 /* Is there room for one more md? */
2271 if ((num_pages << PAGE_SHIFT) <
2272 (sizeof(*memmap_info) + memmap_info->efi_memmap_size +
2273 memmap_info->efi_memdesc_size))
2274 return 0;
2276 return 1;
2279 static int
2280 memdesc_can_collapse(const efi_memory_desc_t *lhs,
2281 const efi_memory_desc_t *rhs)
2283 return (lhs->type == rhs->type && lhs->attribute == rhs->attribute);
2286 static int
2287 __dom0vp_add_memdesc_one(struct xen_ia64_memmap_info *memmap_info,
2288 unsigned long num_pages,
2289 const efi_memory_desc_t *md)
2291 void* const memmap_end = (void*)memmap_info->memdesc +
2292 memmap_info->efi_memmap_size;
2293 void *p;
2294 efi_memory_desc_t *tmp_md;
2295 efi_memory_desc_t *s_md;
2296 efi_memory_desc_t *e_md;
2297 u64 phys_addr;
2298 u64 phys_addr_end;
2300 /* fast path. appending to the last entry */
2301 tmp_md = (efi_memory_desc_t*)(memmap_end - memmap_info->efi_memdesc_size);
2302 if (MD_END(tmp_md) < md->phys_addr) {
2303 /* append one */
2304 if (!memdesc_can_expand(memmap_info, num_pages))
2305 return -ENOMEM;
2307 memcpy(memmap_end, md, memmap_info->efi_memdesc_size);
2308 memmap_info->efi_memmap_size += memmap_info->efi_memdesc_size;
2309 return 0;
2311 /* fast path. expand the last entry */
2312 if (tmp_md->phys_addr <= md->phys_addr) {
2313 if (!memdesc_can_collapse(tmp_md, md))
2314 return -EINVAL;
2316 phys_addr_end = max(MD_END(tmp_md), MD_END(md));
2317 tmp_md->num_pages =
2318 (phys_addr_end - tmp_md->phys_addr) >> EFI_PAGE_SHIFT;
2319 return 0;
2322 /* slow path */
2323 s_md = NULL;
2324 e_md = NULL;
2325 for (p = memmap_info->memdesc;
2326 p < memmap_end;
2327 p += memmap_info->efi_memdesc_size) {
2328 tmp_md = p;
2330 if (MD_END(tmp_md) < md->phys_addr)
2331 continue;
2333 if (MD_END(md) < tmp_md->phys_addr) {
2334 if (s_md == NULL) {
2335 void *next_md = p + memmap_info->efi_memdesc_size;
2336 size_t left_size = memmap_end - (void*)tmp_md;
2338 /* found hole. just insert md here*/
2339 if (!memdesc_can_expand(memmap_info, num_pages))
2340 return -ENOMEM;
2342 memmove(next_md, tmp_md, left_size);
2343 memcpy(tmp_md, md, memmap_info->efi_memdesc_size);
2344 memmap_info->efi_memmap_size += memmap_info->efi_memdesc_size;
2345 return 0;
2347 break;
2350 if (s_md == NULL)
2351 s_md = tmp_md;
2352 e_md = tmp_md;
2354 if (!memdesc_can_collapse(tmp_md, md))
2355 return -EINVAL;
2357 BUG_ON(s_md == NULL || e_md == NULL);
2359 /* collapse into one */
2360 phys_addr = min(md->phys_addr, s_md->phys_addr);
2361 phys_addr_end = max(MD_END(md), MD_END(e_md));
2362 s_md->phys_addr = phys_addr;
2363 s_md->num_pages = (phys_addr_end - phys_addr) >> EFI_PAGE_SHIFT;
2364 if (s_md != e_md) {
2365 void *next_s_md = (void*)s_md + memmap_info->efi_memdesc_size;
2366 void *next_e_md = (void*)e_md + memmap_info->efi_memdesc_size;
2367 size_t left_size = memmap_end - (void*)next_e_md;
2369 memmap_info->efi_memmap_size -= (void*)e_md - (void*)s_md;
2370 if (left_size > 0)
2371 memmove(next_s_md, next_e_md, left_size);
2374 return 0;
2377 /*
2378 * d->arch.convmem_end is mostly read only and sometimes increased.
2379 * It is protected by memmap_lock
2381 * d->arch.convmem_end is also referned by guest(self p2m exposure)
2382 * d->shared_info.arch.memmap_info_xxx and memmap_info are
2383 * referenced by tools stack(save/dump-core/foreign p2m exposure).
2385 * reader side:
2386 * - get d->arch.convmem_end (via XENMEM_maximum_gpfn)
2387 * - issue get_memmap hypercall to get memmap
2388 * In VMM
2389 * - lock memmap_lock
2390 * - copy memmap from target guest
2391 * - unlock memmap_lock
2392 * - copy memmap into tools stack address space.
2393 * - check d->shared_info.memmap_info_num_pages. try again if necessary
2394 * - get d->arch.convmem_end. try again if changed.
2396 * writer side:
2397 * - lock memmap_lock
2398 * - increase d->arch.convmem_end at first if necessary
2399 * - unlock memmap_lock
2400 * - allocate memory
2401 * In fact page allocation isn't blocking, so unlock/lock isn't necessary.
2402 * - lock memmap_lock
2403 * - update memmap_info
2404 * - unlock memmap_lock
2405 */
2406 static int
2407 __dom0vp_add_memdesc(struct domain *targ_d,
2408 const struct xen_ia64_memmap_info *u_memmap_info,
2409 const char *u_memmap)
2411 int ret = 0;
2412 const void* const u_memmap_end = u_memmap + u_memmap_info->efi_memmap_size;
2413 const efi_memory_desc_t *md;
2415 unsigned long md_end_max;
2416 unsigned long num_pages;
2417 unsigned long order;
2418 unsigned long memmap_info_pfn;
2420 struct page_info *page = NULL;
2421 struct xen_ia64_memmap_info *memmap_info;
2422 size_t unused_size;
2424 const void *p;
2426 /* update d->arch.convmem_end */
2427 md_end_max = 0;
2428 for (p = u_memmap; p < u_memmap_end;
2429 p += u_memmap_info->efi_memdesc_size) {
2430 md = p;
2431 if (MD_END(md) > md_end_max)
2432 md_end_max = MD_END(md);
2434 memmap_lock(targ_d);
2435 /* convmem_end is also protected memdesc lock */
2436 if (md_end_max > targ_d->arch.convmem_end)
2437 targ_d->arch.convmem_end = md_end_max;
2439 /* memmap_copy_from_guest() unlock/lock memmap_lock() */
2440 ret = memmap_copy_from(targ_d, &page, &order);
2441 if (ret != 0)
2442 goto out;
2443 memmap_info = page_to_virt(page);
2444 num_pages = targ_d->shared_info->arch.memmap_info_num_pages;
2445 memmap_info_pfn = targ_d->shared_info->arch.memmap_info_pfn;
2447 if (memmap_info->efi_memdesc_size != u_memmap_info->efi_memdesc_size ||
2448 memmap_info->efi_memdesc_version !=
2449 u_memmap_info->efi_memdesc_version) {
2450 ret = -EINVAL;
2451 goto out;
2454 /* update memdesc */
2455 for (p = u_memmap;
2456 p < u_memmap_end;
2457 p += u_memmap_info->efi_memdesc_size) {
2458 md = p;
2459 ret = __dom0vp_add_memdesc_one(memmap_info, num_pages, md);
2460 if (ret != 0)
2461 goto out;
2464 /* zero out the unused region to avoid hypervisor bit leak */
2465 unused_size = (num_pages << PAGE_SHIFT) -
2466 (sizeof(*memmap_info) + memmap_info->efi_memmap_size);
2467 if (unused_size > 0)
2468 memset((void*)memmap_info->memdesc + memmap_info->efi_memmap_size,
2469 0, unused_size);
2471 /* copy back into domain. */
2472 ret = __memmap_copy_to(targ_d, memmap_info_pfn, memmap_info, num_pages);
2474 out:
2475 memmap_unlock(targ_d);
2477 if (page != NULL)
2478 free_domheap_pages(page, order);
2479 return ret;
2482 unsigned long
2483 dom0vp_get_memmap(domid_t domid, XEN_GUEST_HANDLE(char) buffer)
2485 unsigned long ret = 0;
2486 struct domain *targ_d;
2488 struct page_info *page = NULL;
2489 unsigned long order;
2491 struct xen_ia64_memmap_info *memmap_info;
2492 unsigned long num_pages;
2494 ret = rcu_lock_target_domain_by_id(domid, &targ_d);
2495 if (ret != 0)
2496 return ret;
2498 memmap_lock(targ_d);
2500 ret = memmap_copy_from(targ_d, &page, &order);
2501 if (ret != 0)
2502 goto unlock_out;
2504 memmap_info = page_to_virt(page);
2505 num_pages = targ_d->shared_info->arch.memmap_info_num_pages;
2506 if ((num_pages << PAGE_SHIFT) - sizeof(*memmap_info) <
2507 memmap_info->efi_memmap_size) {
2508 ret = -EFAULT;
2509 goto unlock_out;
2511 memmap_unlock(targ_d);
2512 rcu_unlock_domain(targ_d);
2514 if (copy_to_guest(buffer, (char*)memmap_info, sizeof(*memmap_info)) ||
2515 copy_to_guest_offset(buffer, sizeof(*memmap_info),
2516 (char*)memmap_info->memdesc,
2517 memmap_info->efi_memmap_size))
2518 ret = -EFAULT;
2520 out:
2521 if (page != NULL)
2522 free_domheap_pages(page, order);
2523 return ret;
2525 unlock_out:
2526 memmap_unlock(targ_d);
2527 rcu_unlock_domain(targ_d);
2528 goto out;
2530 #endif
2532 // grant table host mapping
2533 // mpaddr: host_addr: pseudo physical address
2534 // mfn: frame: machine page frame
2535 // flags: GNTMAP_readonly | GNTMAP_application_map | GNTMAP_contains_pte
2536 int
2537 create_grant_host_mapping(unsigned long gpaddr, unsigned long mfn,
2538 unsigned int flags, unsigned int cache_flags)
2540 struct domain* d = current->domain;
2541 struct page_info* page;
2542 int ret;
2544 if ((flags & (GNTMAP_device_map |
2545 GNTMAP_application_map | GNTMAP_contains_pte)) ||
2546 (cache_flags)) {
2547 gdprintk(XENLOG_INFO, "%s: flags 0x%x cache_flags 0x%x\n",
2548 __func__, flags, cache_flags);
2549 return GNTST_general_error;
2552 BUG_ON(!mfn_valid(mfn));
2553 page = mfn_to_page(mfn);
2554 ret = get_page(page, page_get_owner(page));
2555 BUG_ON(ret == 0);
2556 assign_domain_page_replace(d, gpaddr, mfn,
2557 #ifdef CONFIG_XEN_IA64_TLB_TRACK
2558 ASSIGN_tlb_track |
2559 #endif
2560 ((flags & GNTMAP_readonly) ?
2561 ASSIGN_readonly : ASSIGN_writable));
2562 perfc_incr(create_grant_host_mapping);
2563 return GNTST_okay;
2566 // grant table host unmapping
2567 int
2568 replace_grant_host_mapping(unsigned long gpaddr,
2569 unsigned long mfn, unsigned long new_gpaddr, unsigned int flags)
2571 struct domain* d = current->domain;
2572 unsigned long gpfn = gpaddr >> PAGE_SHIFT;
2573 volatile pte_t* pte;
2574 unsigned long cur_arflags;
2575 pte_t cur_pte;
2576 pte_t new_pte = __pte(0);
2577 pte_t old_pte;
2578 struct page_info* page = mfn_to_page(mfn);
2579 struct page_info* new_page = NULL;
2580 volatile pte_t* new_page_pte = NULL;
2581 unsigned long new_page_mfn = INVALID_MFN;
2583 if (new_gpaddr) {
2584 new_page_pte = lookup_noalloc_domain_pte_none(d, new_gpaddr);
2585 if (likely(new_page_pte != NULL)) {
2586 new_pte = ptep_get_and_clear(&d->arch.mm,
2587 new_gpaddr, new_page_pte);
2588 if (likely(pte_present(new_pte))) {
2589 struct domain* page_owner;
2591 new_page_mfn = pte_pfn(new_pte);
2592 new_page = mfn_to_page(new_page_mfn);
2593 page_owner = page_get_owner(new_page);
2594 if (unlikely(page_owner == NULL)) {
2595 gdprintk(XENLOG_INFO,
2596 "%s: page_owner == NULL "
2597 "gpaddr 0x%lx mfn 0x%lx "
2598 "new_gpaddr 0x%lx mfn 0x%lx\n",
2599 __func__, gpaddr, mfn, new_gpaddr, new_page_mfn);
2600 new_page = NULL; /* prevent domain_put_page() */
2601 return GNTST_general_error;
2604 /*
2605 * domain_put_page(clear_PGC_allcoated = 0)
2606 * doesn't decrement refcount of page with
2607 * pte_ptc_allocated() = 1. Be carefull.
2608 */
2609 if (unlikely(!pte_pgc_allocated(new_pte))) {
2610 /* domain_put_page() decrements page refcount. adjust it. */
2611 if (get_page(new_page, page_owner)) {
2612 gdprintk(XENLOG_INFO,
2613 "%s: get_page() failed. "
2614 "gpaddr 0x%lx mfn 0x%lx "
2615 "new_gpaddr 0x%lx mfn 0x%lx\n",
2616 __func__, gpaddr, mfn,
2617 new_gpaddr, new_page_mfn);
2618 return GNTST_general_error;
2621 domain_put_page(d, new_gpaddr, new_page_pte, new_pte, 0);
2622 } else
2623 new_pte = __pte(0);
2627 if (flags & (GNTMAP_application_map | GNTMAP_contains_pte)) {
2628 gdprintk(XENLOG_INFO, "%s: flags 0x%x\n", __func__, flags);
2629 return GNTST_general_error;
2632 pte = lookup_noalloc_domain_pte(d, gpaddr);
2633 if (pte == NULL) {
2634 gdprintk(XENLOG_INFO, "%s: gpaddr 0x%lx mfn 0x%lx\n",
2635 __func__, gpaddr, mfn);
2636 return GNTST_general_error;
2639 again:
2640 cur_arflags = pte_val(*pte) & ~_PAGE_PPN_MASK;
2641 cur_pte = pfn_pte(mfn, __pgprot(cur_arflags));
2642 if (!pte_present(cur_pte) ||
2643 (page_get_owner(page) == d && get_gpfn_from_mfn(mfn) == gpfn)) {
2644 gdprintk(XENLOG_INFO, "%s: gpaddr 0x%lx mfn 0x%lx cur_pte 0x%lx\n",
2645 __func__, gpaddr, mfn, pte_val(cur_pte));
2646 return GNTST_general_error;
2649 if (new_page) {
2650 BUG_ON(new_page_mfn == INVALID_MFN);
2651 set_gpfn_from_mfn(new_page_mfn, gpfn);
2652 /* smp_mb() isn't needed because assign_domain_pge_cmpxchg_rel()
2653 has release semantics. */
2655 old_pte = ptep_cmpxchg_rel(&d->arch.mm, gpaddr, pte, cur_pte, new_pte);
2656 if (unlikely(pte_val(cur_pte) != pte_val(old_pte))) {
2657 if (pte_pfn(old_pte) == mfn) {
2658 goto again;
2660 if (new_page) {
2661 BUG_ON(new_page_mfn == INVALID_MFN);
2662 set_gpfn_from_mfn(new_page_mfn, INVALID_M2P_ENTRY);
2663 domain_put_page(d, new_gpaddr, new_page_pte, new_pte, 1);
2665 goto out;
2667 if (unlikely(!pte_present(old_pte)))
2668 goto out;
2669 BUG_ON(pte_pfn(old_pte) != mfn);
2671 /* try_to_clear_PGC_allocate(d, page) is not needed. */
2672 BUG_ON(page_get_owner(page) == d &&
2673 get_gpfn_from_mfn(mfn) == gpfn);
2674 BUG_ON(pte_pgc_allocated(old_pte));
2675 domain_page_flush_and_put(d, gpaddr, pte, old_pte, page);
2677 perfc_incr(replace_grant_host_mapping);
2678 return GNTST_okay;
2680 out:
2681 gdprintk(XENLOG_INFO, "%s gpaddr 0x%lx mfn 0x%lx cur_pte "
2682 "0x%lx old_pte 0x%lx\n",
2683 __func__, gpaddr, mfn, pte_val(cur_pte), pte_val(old_pte));
2684 return GNTST_general_error;
2687 // heavily depends on the struct page layout.
2688 // gnttab_transfer() calls steal_page() with memflags = 0
2689 // For grant table transfer, we must fill the page.
2690 // memory_exchange() calls steal_page() with memflags = MEMF_no_refcount
2691 // For memory exchange, we don't have to fill the page because
2692 // memory_exchange() does it.
2693 int
2694 steal_page(struct domain *d, struct page_info *page, unsigned int memflags)
2696 #if 0 /* if big endian */
2697 # error "implement big endian version of steal_page()"
2698 #endif
2699 u32 _d, _nd;
2700 u64 x, nx, y;
2702 if (page_get_owner(page) != d) {
2703 gdprintk(XENLOG_INFO, "%s d 0x%p owner 0x%p\n",
2704 __func__, d, page_get_owner(page));
2705 return -1;
2708 if (!(memflags & MEMF_no_refcount)) {
2709 unsigned long gpfn;
2710 struct page_info *new;
2711 unsigned long new_mfn;
2712 int ret;
2714 new = alloc_domheap_page(d, 0);
2715 if (new == NULL) {
2716 gdprintk(XENLOG_INFO, "alloc_domheap_page() failed\n");
2717 return -1;
2719 // zero out pages for security reasons
2720 clear_page(page_to_virt(new));
2721 // assign_domain_page_cmpxchg_rel() has release semantics
2722 // so smp_mb() isn't needed.
2724 gpfn = get_gpfn_from_mfn(page_to_mfn(page));
2725 if (gpfn == INVALID_M2P_ENTRY) {
2726 free_domheap_page(new);
2727 return -1;
2729 new_mfn = page_to_mfn(new);
2730 set_gpfn_from_mfn(new_mfn, gpfn);
2731 // smp_mb() isn't needed because assign_domain_pge_cmpxchg_rel()
2732 // has release semantics.
2734 ret = assign_domain_page_cmpxchg_rel(d, gpfn << PAGE_SHIFT, page, new,
2735 ASSIGN_writable |
2736 ASSIGN_pgc_allocated, 0);
2737 if (ret < 0) {
2738 gdprintk(XENLOG_INFO, "assign_domain_page_cmpxchg_rel failed %d\n",
2739 ret);
2740 set_gpfn_from_mfn(new_mfn, INVALID_M2P_ENTRY);
2741 free_domheap_page(new);
2742 return -1;
2744 perfc_incr(steal_page_refcount);
2747 spin_lock(&d->page_alloc_lock);
2749 /*
2750 * The tricky bit: atomically release ownership while there is just one
2751 * benign reference to the page (PGC_allocated). If that reference
2752 * disappears then the deallocation routine will safely spin.
2753 */
2754 _d = pickle_domptr(d);
2755 y = *((u64*)&page->count_info);
2756 do {
2757 x = y;
2758 nx = x & 0xffffffff;
2759 // page->count_info: untouched
2760 // page->u.inused._domain = 0;
2761 _nd = x >> 32;
2763 if (unlikely(((x & (PGC_count_mask | PGC_allocated)) !=
2764 (1 | PGC_allocated))) ||
2765 unlikely(_nd != _d)) {
2766 struct domain* nd = unpickle_domptr(_nd);
2767 if (nd == NULL) {
2768 gdprintk(XENLOG_INFO, "gnttab_transfer: "
2769 "Bad page %p: ed=%p(%u) 0x%x, "
2770 "sd=%p 0x%x,"
2771 " caf=%016lx, taf=%" PRtype_info
2772 " memflags 0x%x\n",
2773 (void *) page_to_mfn(page),
2774 d, d->domain_id, _d,
2775 nd, _nd,
2776 x,
2777 page->u.inuse.type_info,
2778 memflags);
2779 } else {
2780 gdprintk(XENLOG_WARNING, "gnttab_transfer: "
2781 "Bad page %p: ed=%p(%u) 0x%x, "
2782 "sd=%p(%u) 0x%x,"
2783 " caf=%016lx, taf=%" PRtype_info
2784 " memflags 0x%x\n",
2785 (void *) page_to_mfn(page),
2786 d, d->domain_id, _d,
2787 nd, nd->domain_id, _nd,
2788 x,
2789 page->u.inuse.type_info,
2790 memflags);
2792 spin_unlock(&d->page_alloc_lock);
2793 return -1;
2796 y = cmpxchg((u64*)&page->count_info, x, nx);
2797 } while (unlikely(y != x));
2799 /*
2800 * Unlink from 'd'. At least one reference remains (now anonymous), so
2801 * noone else is spinning to try to delete this page from 'd'.
2802 */
2803 if ( !(memflags & MEMF_no_refcount) )
2804 d->tot_pages--;
2805 list_del(&page->list);
2807 spin_unlock(&d->page_alloc_lock);
2808 perfc_incr(steal_page);
2809 return 0;
2812 static void
2813 __guest_physmap_add_page(struct domain *d, unsigned long gpfn,
2814 unsigned long mfn)
2816 set_gpfn_from_mfn(mfn, gpfn);
2817 smp_mb();
2818 assign_domain_page_replace(d, gpfn << PAGE_SHIFT, mfn,
2819 ASSIGN_writable | ASSIGN_pgc_allocated);
2822 int
2823 guest_physmap_add_page(struct domain *d, unsigned long gpfn,
2824 unsigned long mfn, unsigned int page_order)
2826 unsigned long i;
2828 for (i = 0; i < (1UL << page_order); i++) {
2829 BUG_ON(!mfn_valid(mfn));
2830 BUG_ON(mfn_to_page(mfn)->count_info != (PGC_allocated | 1));
2831 __guest_physmap_add_page(d, gpfn, mfn);
2832 mfn++;
2833 gpfn++;
2836 perfc_incr(guest_physmap_add_page);
2837 return 0;
2840 void
2841 guest_physmap_remove_page(struct domain *d, unsigned long gpfn,
2842 unsigned long mfn, unsigned int page_order)
2844 unsigned long i;
2846 BUG_ON(mfn == 0);//XXX
2848 for (i = 0; i < (1UL << page_order); i++)
2849 zap_domain_page_one(d, (gpfn+i) << PAGE_SHIFT, 0, mfn+i);
2851 perfc_incr(guest_physmap_remove_page);
2854 static void
2855 domain_page_flush_and_put(struct domain* d, unsigned long mpaddr,
2856 volatile pte_t* ptep, pte_t old_pte,
2857 struct page_info* page)
2859 #ifdef CONFIG_XEN_IA64_TLB_TRACK
2860 struct tlb_track_entry* entry;
2861 #endif
2863 if (shadow_mode_enabled(d))
2864 shadow_mark_page_dirty(d, mpaddr >> PAGE_SHIFT);
2866 #ifndef CONFIG_XEN_IA64_TLB_TRACK
2867 //XXX sledgehammer.
2868 // flush finer range.
2869 domain_flush_vtlb_all(d);
2870 put_page(page);
2871 #else
2872 switch (tlb_track_search_and_remove(d->arch.tlb_track,
2873 ptep, old_pte, &entry)) {
2874 case TLB_TRACK_NOT_TRACKED:
2875 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_NOT_TRACKED\n", __func__);
2876 /* This page is zapped from this domain
2877 * by memory decrease or exchange or dom0vp_zap_physmap.
2878 * I.e. the page is zapped for returning this page to xen
2879 * (balloon driver or DMA page allocation) or
2880 * foreign domain mapped page is unmapped from the domain.
2881 * In the former case the page is to be freed so that
2882 * we can defer freeing page to batch.
2883 * In the latter case the page is unmapped so that
2884 * we need to flush it. But to optimize it, we
2885 * queue the page and flush vTLB only once.
2886 * I.e. The caller must call dfree_flush() explicitly.
2887 */
2888 domain_flush_vtlb_all(d);
2889 put_page(page);
2890 break;
2891 case TLB_TRACK_NOT_FOUND:
2892 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_NOT_FOUND\n", __func__);
2893 /* This page is zapped from this domain
2894 * by grant table page unmap.
2895 * Luckily the domain that mapped this page didn't
2896 * access this page so that we don't have to flush vTLB.
2897 * Probably the domain did only DMA.
2898 */
2899 /* do nothing */
2900 put_page(page);
2901 break;
2902 case TLB_TRACK_FOUND:
2903 // dprintk(XENLOG_WARNING, "%s TLB_TRACK_FOUND\n", __func__);
2904 /* This page is zapped from this domain
2905 * by grant table page unmap.
2906 * Fortunately this page is accessced via only one virtual
2907 * memory address. So it is easy to flush it.
2908 */
2909 domain_flush_vtlb_track_entry(d, entry);
2910 tlb_track_free_entry(d->arch.tlb_track, entry);
2911 put_page(page);
2912 break;
2913 case TLB_TRACK_MANY:
2914 gdprintk(XENLOG_INFO, "%s TLB_TRACK_MANY\n", __func__);
2915 /* This page is zapped from this domain
2916 * by grant table page unmap.
2917 * Unfortunately this page is accessced via many virtual
2918 * memory address (or too many times with single virtual address).
2919 * So we abondaned to track virtual addresses.
2920 * full vTLB flush is necessary.
2921 */
2922 domain_flush_vtlb_all(d);
2923 put_page(page);
2924 break;
2925 case TLB_TRACK_AGAIN:
2926 gdprintk(XENLOG_ERR, "%s TLB_TRACK_AGAIN\n", __func__);
2927 BUG();
2928 break;
2930 #endif
2931 perfc_incr(domain_page_flush_and_put);
2934 int
2935 domain_page_mapped(struct domain* d, unsigned long mpaddr)
2937 volatile pte_t * pte;
2939 pte = lookup_noalloc_domain_pte(d, mpaddr);
2940 if(pte != NULL && !pte_none(*pte))
2941 return 1;
2942 return 0;
2945 /* Flush cache of domain d. */
2946 void domain_cache_flush (struct domain *d, int sync_only)
2948 struct mm_struct *mm = &d->arch.mm;
2949 volatile pgd_t *pgd = mm->pgd;
2950 unsigned long maddr;
2951 int i,j,k, l;
2952 int nbr_page = 0;
2953 void (*flush_func)(unsigned long start, unsigned long end);
2954 extern void flush_dcache_range (unsigned long, unsigned long);
2956 if (sync_only)
2957 flush_func = &flush_icache_range;
2958 else
2959 flush_func = &flush_dcache_range;
2961 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
2962 volatile pud_t *pud;
2963 if (!pgd_present(*pgd)) // acquire semantics
2964 continue;
2965 pud = pud_offset(pgd, 0);
2966 for (j = 0; j < PTRS_PER_PUD; pud++, j++) {
2967 volatile pmd_t *pmd;
2968 if (!pud_present(*pud)) // acquire semantics
2969 continue;
2970 pmd = pmd_offset(pud, 0);
2971 for (k = 0; k < PTRS_PER_PMD; pmd++, k++) {
2972 volatile pte_t *pte;
2973 if (!pmd_present(*pmd)) // acquire semantics
2974 continue;
2975 pte = pte_offset_map(pmd, 0);
2976 for (l = 0; l < PTRS_PER_PTE; pte++, l++) {
2977 if (!pte_present(*pte)) // acquire semantics
2978 continue;
2979 /* Convert PTE to maddr. */
2980 maddr = __va_ul (pte_val(*pte)
2981 & _PAGE_PPN_MASK);
2982 (*flush_func)(maddr, maddr+ PAGE_SIZE);
2983 nbr_page++;
2988 //printk ("domain_cache_flush: %d %d pages\n", d->domain_id, nbr_page);
2991 #ifdef VERBOSE
2992 #define MEM_LOG(_f, _a...) \
2993 printk("DOM%u: (file=mm.c, line=%d) " _f "\n", \
2994 current->domain->domain_id , __LINE__ , ## _a )
2995 #else
2996 #define MEM_LOG(_f, _a...) ((void)0)
2997 #endif
2999 static void free_page_type(struct page_info *page, u32 type)
3003 static int alloc_page_type(struct page_info *page, u32 type)
3005 return 1;
3008 static int opt_p2m_xenheap;
3009 boolean_param("p2m_xenheap", opt_p2m_xenheap);
3011 void *pgtable_quicklist_alloc(void)
3013 void *p;
3015 BUG_ON(dom_p2m == NULL);
3016 if (!opt_p2m_xenheap) {
3017 struct page_info *page = alloc_domheap_page(dom_p2m, 0);
3018 if (page == NULL)
3019 return NULL;
3020 p = page_to_virt(page);
3021 clear_page(p);
3022 return p;
3024 p = alloc_xenheap_pages(0);
3025 if (p) {
3026 clear_page(p);
3027 /*
3028 * This page should be read only. At this moment, the third
3029 * argument doesn't make sense. It should be 1 when supported.
3030 */
3031 share_xen_page_with_guest(virt_to_page(p), dom_p2m, 0);
3033 return p;
3036 void pgtable_quicklist_free(void *pgtable_entry)
3038 struct page_info* page = virt_to_page(pgtable_entry);
3040 BUG_ON(page_get_owner(page) != dom_p2m);
3041 BUG_ON(page->count_info != (1 | PGC_allocated));
3043 put_page(page);
3044 if (opt_p2m_xenheap)
3045 free_xenheap_page(pgtable_entry);
3048 void put_page_type(struct page_info *page)
3050 u64 nx, x, y = page->u.inuse.type_info;
3052 again:
3053 do {
3054 x = y;
3055 nx = x - 1;
3057 ASSERT((x & PGT_count_mask) != 0);
3059 /*
3060 * The page should always be validated while a reference is held. The
3061 * exception is during domain destruction, when we forcibly invalidate
3062 * page-table pages if we detect a referential loop.
3063 * See domain.c:relinquish_list().
3064 */
3065 ASSERT((x & PGT_validated) || page_get_owner(page)->is_dying);
3067 if ( unlikely((nx & PGT_count_mask) == 0) )
3069 /* Record TLB information for flush later. Races are harmless. */
3070 page->tlbflush_timestamp = tlbflush_current_time();
3072 if ( unlikely((nx & PGT_type_mask) <= PGT_l4_page_table) &&
3073 likely(nx & PGT_validated) )
3075 /*
3076 * Page-table pages must be unvalidated when count is zero. The
3077 * 'free' is safe because the refcnt is non-zero and validated
3078 * bit is clear => other ops will spin or fail.
3079 */
3080 if ( unlikely((y = cmpxchg(&page->u.inuse.type_info, x,
3081 x & ~PGT_validated)) != x) )
3082 goto again;
3083 /* We cleared the 'valid bit' so we do the clean up. */
3084 free_page_type(page, x);
3085 /* Carry on, but with the 'valid bit' now clear. */
3086 x &= ~PGT_validated;
3087 nx &= ~PGT_validated;
3091 while ( unlikely((y = cmpxchg_rel(&page->u.inuse.type_info, x, nx)) != x) );
3095 static int get_page_from_pagenr(unsigned long page_nr, struct domain *d)
3097 struct page_info *page = mfn_to_page(page_nr);
3099 if ( unlikely(!mfn_valid(page_nr)) || unlikely(!get_page(page, d)) )
3101 MEM_LOG("Could not get page ref for pfn %lx", page_nr);
3102 return 0;
3105 return 1;
3109 int get_page_type(struct page_info *page, u32 type)
3111 u64 nx, x, y = page->u.inuse.type_info;
3113 ASSERT(!(type & ~PGT_type_mask));
3115 again:
3116 do {
3117 x = y;
3118 nx = x + 1;
3119 if ( unlikely((nx & PGT_count_mask) == 0) )
3121 MEM_LOG("Type count overflow on pfn %lx", page_to_mfn(page));
3122 return 0;
3124 else if ( unlikely((x & PGT_count_mask) == 0) )
3126 if ( (x & PGT_type_mask) != type )
3128 /*
3129 * On type change we check to flush stale TLB entries. This
3130 * may be unnecessary (e.g., page was GDT/LDT) but those
3131 * circumstances should be very rare.
3132 */
3133 cpumask_t mask =
3134 page_get_owner(page)->domain_dirty_cpumask;
3135 tlbflush_filter(mask, page->tlbflush_timestamp);
3137 if ( unlikely(!cpus_empty(mask)) )
3139 perfc_incr(need_flush_tlb_flush);
3140 flush_tlb_mask(mask);
3143 /* We lose existing type, back pointer, and validity. */
3144 nx &= ~(PGT_type_mask | PGT_validated);
3145 nx |= type;
3147 /* No special validation needed for writable pages. */
3148 /* Page tables and GDT/LDT need to be scanned for validity. */
3149 if ( type == PGT_writable_page )
3150 nx |= PGT_validated;
3153 else if ( unlikely((x & PGT_type_mask) != type) )
3155 if ( ((x & PGT_type_mask) != PGT_l2_page_table) ||
3156 (type != PGT_l1_page_table) )
3157 MEM_LOG("Bad type (saw %08lx != exp %08x) "
3158 "for mfn %016lx (pfn %016lx)",
3159 x, type, page_to_mfn(page),
3160 get_gpfn_from_mfn(page_to_mfn(page)));
3161 return 0;
3163 else if ( unlikely(!(x & PGT_validated)) )
3165 /* Someone else is updating validation of this page. Wait... */
3166 while ( (y = page->u.inuse.type_info) == x )
3167 cpu_relax();
3168 goto again;
3171 while ( unlikely((y = cmpxchg_acq(&page->u.inuse.type_info, x, nx)) != x) );
3173 if ( unlikely(!(nx & PGT_validated)) )
3175 /* Try to validate page type; drop the new reference on failure. */
3176 if ( unlikely(!alloc_page_type(page, type)) )
3178 MEM_LOG("Error while validating mfn %lx (pfn %lx) for type %08x"
3179 ": caf=%08x taf=%" PRtype_info,
3180 page_to_mfn(page), get_gpfn_from_mfn(page_to_mfn(page)),
3181 type, page->count_info, page->u.inuse.type_info);
3182 /* Noone else can get a reference. We hold the only ref. */
3183 page->u.inuse.type_info = 0;
3184 return 0;
3187 /* Noone else is updating simultaneously. */
3188 __set_bit(_PGT_validated, &page->u.inuse.type_info);
3191 return 1;
3194 int memory_is_conventional_ram(paddr_t p)
3196 return (efi_mem_type(p) == EFI_CONVENTIONAL_MEMORY);
3200 long
3201 arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
3203 struct page_info *page = NULL;
3204 long rc;
3206 switch (op) {
3207 case XENMEM_add_to_physmap:
3209 struct xen_add_to_physmap xatp;
3210 unsigned long prev_mfn, mfn = 0, gpfn;
3211 struct domain *d;
3213 if (copy_from_guest(&xatp, arg, 1))
3214 return -EFAULT;
3216 rc = rcu_lock_target_domain_by_id(xatp.domid, &d);
3217 if (rc)
3218 return rc;
3220 /* This hypercall is used for VT-i domain only */
3221 if (!is_hvm_domain(d)) {
3222 rcu_unlock_domain(d);
3223 return -ENOSYS;
3226 switch (xatp.space) {
3227 case XENMAPSPACE_shared_info:
3228 if (xatp.idx == 0)
3229 mfn = virt_to_mfn(d->shared_info);
3230 break;
3231 case XENMAPSPACE_grant_table:
3232 spin_lock(&d->grant_table->lock);
3234 if ((xatp.idx >= nr_grant_frames(d->grant_table)) &&
3235 (xatp.idx < max_nr_grant_frames))
3236 gnttab_grow_table(d, xatp.idx + 1);
3238 if (xatp.idx < nr_grant_frames(d->grant_table))
3239 mfn = virt_to_mfn(d->grant_table->shared[xatp.idx]);
3241 spin_unlock(&d->grant_table->lock);
3242 break;
3243 case XENMAPSPACE_mfn:
3245 if ( get_page_from_pagenr(xatp.idx, d) ) {
3246 struct xen_ia64_memmap_info memmap_info;
3247 efi_memory_desc_t md;
3248 int ret;
3250 mfn = xatp.idx;
3251 page = mfn_to_page(mfn);
3253 memmap_info.efi_memmap_size = sizeof(md);
3254 memmap_info.efi_memdesc_size = sizeof(md);
3255 memmap_info.efi_memdesc_version =
3256 EFI_MEMORY_DESCRIPTOR_VERSION;
3258 md.type = EFI_CONVENTIONAL_MEMORY;
3259 md.pad = 0;
3260 md.phys_addr = xatp.gpfn << PAGE_SHIFT;
3261 md.virt_addr = 0;
3262 md.num_pages = 1UL << (PAGE_SHIFT - EFI_PAGE_SHIFT);
3263 md.attribute = EFI_MEMORY_WB;
3265 ret = __dom0vp_add_memdesc(d, &memmap_info, (char*)&md);
3266 if (ret != 0) {
3267 put_page(page);
3268 rcu_unlock_domain(d);
3269 gdprintk(XENLOG_DEBUG,
3270 "%s:%d td %d gpfn 0x%lx mfn 0x%lx ret %d\n",
3271 __func__, __LINE__,
3272 d->domain_id, xatp.gpfn, xatp.idx, ret);
3273 return ret;
3276 break;
3278 default:
3279 break;
3282 if (mfn == 0) {
3283 if ( page )
3284 put_page(page);
3285 rcu_unlock_domain(d);
3286 return -EINVAL;
3289 domain_lock(d);
3291 /* Check remapping necessity */
3292 prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
3293 if (mfn == prev_mfn)
3294 goto out;
3296 /* Remove previously mapped page if it was present. */
3297 if (prev_mfn && mfn_valid(prev_mfn)) {
3298 if (is_xen_heap_mfn(prev_mfn))
3299 /* Xen heap frames are simply unhooked from this phys slot. */
3300 guest_physmap_remove_page(d, xatp.gpfn, prev_mfn, 0);
3301 else
3302 /* Normal domain memory is freed, to avoid leaking memory. */
3303 guest_remove_page(d, xatp.gpfn);
3306 /* Unmap from old location, if any. */
3307 gpfn = get_gpfn_from_mfn(mfn);
3308 if (gpfn != INVALID_M2P_ENTRY)
3309 guest_physmap_remove_page(d, gpfn, mfn, 0);
3311 /* Map at new location. */
3312 /* Here page->count_info = PGC_allocated | N where N >= 1*/
3313 __guest_physmap_add_page(d, xatp.gpfn, mfn);
3314 page = NULL; /* prevent put_page() */
3316 out:
3317 domain_unlock(d);
3319 if ( page )
3320 put_page(page);
3322 rcu_unlock_domain(d);
3324 break;
3327 case XENMEM_remove_from_physmap:
3329 struct xen_remove_from_physmap xrfp;
3330 unsigned long mfn;
3331 struct domain *d;
3333 if ( copy_from_guest(&xrfp, arg, 1) )
3334 return -EFAULT;
3336 rc = rcu_lock_target_domain_by_id(xrfp.domid, &d);
3337 if ( rc != 0 )
3338 return rc;
3340 domain_lock(d);
3342 mfn = gmfn_to_mfn(d, xrfp.gpfn);
3344 if ( mfn_valid(mfn) )
3345 guest_physmap_remove_page(d, xrfp.gpfn, mfn, 0);
3347 domain_unlock(d);
3349 rcu_unlock_domain(d);
3351 break;
3355 case XENMEM_machine_memory_map:
3357 struct xen_memory_map memmap;
3358 struct xen_ia64_memmap_info memmap_info;
3359 XEN_GUEST_HANDLE(char) buffer;
3361 if (!IS_PRIV(current->domain))
3362 return -EINVAL;
3363 if (copy_from_guest(&memmap, arg, 1))
3364 return -EFAULT;
3365 if (memmap.nr_entries <
3366 sizeof(memmap_info) + ia64_boot_param->efi_memmap_size)
3367 return -EINVAL;
3369 memmap.nr_entries =
3370 sizeof(memmap_info) + ia64_boot_param->efi_memmap_size;
3371 memset(&memmap_info, 0, sizeof(memmap_info));
3372 memmap_info.efi_memmap_size = ia64_boot_param->efi_memmap_size;
3373 memmap_info.efi_memdesc_size = ia64_boot_param->efi_memdesc_size;
3374 memmap_info.efi_memdesc_version = ia64_boot_param->efi_memdesc_version;
3376 buffer = guest_handle_cast(memmap.buffer, char);
3377 if (copy_to_guest(buffer, (char*)&memmap_info, sizeof(memmap_info)) ||
3378 copy_to_guest_offset(buffer, sizeof(memmap_info),
3379 (char*)__va(ia64_boot_param->efi_memmap),
3380 ia64_boot_param->efi_memmap_size) ||
3381 copy_to_guest(arg, &memmap, 1))
3382 return -EFAULT;
3383 return 0;
3386 default:
3387 return -ENOSYS;
3390 return 0;
3393 int is_iomem_page(unsigned long mfn)
3395 return (!mfn_valid(mfn) || (page_get_owner(mfn_to_page(mfn)) == dom_io));
3398 static void __xencomm_mark_dirty(struct domain *d,
3399 unsigned long addr, unsigned int len)
3401 unsigned long gpfn;
3402 unsigned long end_addr = addr + len;
3404 if (shadow_mode_enabled(d)) {
3405 for (addr &= PAGE_MASK; addr < end_addr; addr += PAGE_SIZE) {
3406 gpfn = get_gpfn_from_mfn(virt_to_mfn(addr));
3407 shadow_mark_page_dirty(d, gpfn);
3412 void xencomm_mark_dirty(unsigned long addr, unsigned int len)
3414 __xencomm_mark_dirty(current->domain, addr, len);
3417 int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn)
3419 /* STUB to compile */
3420 return -ENOSYS;
3423 int iommu_unmap_page(struct domain *d, unsigned long gfn)
3425 /* STUB to compile */
3426 return -ENOSYS;
3429 /*
3430 * Local variables:
3431 * mode: C
3432 * c-set-style: "BSD"
3433 * c-basic-offset: 4
3434 * tab-width: 4
3435 * indent-tabs-mode: nil
3436 * End:
3437 */