From: "Jan Beulich" Subject: [PATCH v5] x86/p2m: use large pages for MMIO mappings Date: Mon, 25 Jan 2016 09:18:54 -0700 Message-ID: <56A658FE02000078000CAC3D@prv-mh.provo.novell.com> References: <56A25C0602000078000CA367@prv-mh.provo.novell.com> <1453724207.4320.137.camel@citrix.com> <56A6371802000078000CAA6B@prv-mh.provo.novell.com> <1453730752.4320.164.camel@citrix.com> <56A63C4002000078000CAAA7@prv-mh.provo.novell.com> <1453731704.4320.173.camel@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2C1BD1FE.3__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aNjrB-0005s8-0a for xen-devel@lists.xenproject.org; Mon, 25 Jan 2016 16:19:09 +0000 In-Reply-To: <1453731704.4320.173.camel@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Kevin Tian , Wei Liu , Ian Campbell , Stefano Stabellini , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Jun Nakajima , Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part2C1BD1FE.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline When mapping large BARs (e.g. the frame buffer of a graphics card) the overhead of establishing such mappings using only 4k pages has, particularly after the XSA-125 fix, become unacceptable. Alter the XEN_DOMCTL_memory_mapping semantics once again, so that there's no longer a fixed amount of guest frames that represents the upper limit of what a single invocation can map. Instead bound execution time by limiting the number of iterations (regardless of page size). Signed-off-by: Jan Beulich --- Open issues (perhaps for subsequent changes): - ARM side unimplemented (and hence libxc for now made cope with both models), the main issue (besides my inability to test any change there) being the many internal uses of map_mmio_regions()) - iommu_{,un}map_page() interfaces don't support "order" (hence mmio_order() for now returns zero when !iommu_hap_pt_share, which in particular means the AMD side isn't being taken care of just yet, but note that this also has the intended effect of suppressing non-zero order mappings in the shadow mode case) --- v5: Refine comment in domctl.h. v4: Move cleanup duty entirely to the caller of the hypercall. Move return value description to from commit message to domctl.h. v3: Re-base on top of "x86/hvm: fold opt_hap_{2mb,1gb} into hap_capabilities". Extend description to spell out new return value meaning. Add a couple of code comments. Use PAGE_ORDER_4K instead of literal 0. Take into consideration r/o MMIO pages. v2: Produce valid entries for large p2m_mmio_direct mappings in p2m_pt_set_entry(). Don't open code iommu_use_hap_pt() in mmio_order(). Update function comment of set_typed_p2m_entry() and clear_mmio_p2m_entry(). Use PRI_mfn. Add ASSERT()s to {,un}map_mmio_regions() to detect otherwise endless loops. --- a/tools/libxc/xc_domain.c +++ b/tools/libxc/xc_domain.c @@ -2174,7 +2174,7 @@ int xc_domain_memory_mapping( { DECLARE_DOMCTL; xc_dominfo_t info; - int ret =3D 0, err; + int ret =3D 0, rc; unsigned long done =3D 0, nr, max_batch_sz; =20 if ( xc_domain_getinfo(xch, domid, 1, &info) !=3D 1 || @@ -2199,19 +2199,24 @@ int xc_domain_memory_mapping( domctl.u.memory_mapping.nr_mfns =3D nr; domctl.u.memory_mapping.first_gfn =3D first_gfn + done; domctl.u.memory_mapping.first_mfn =3D first_mfn + done; - err =3D do_domctl(xch, &domctl); - if ( err && errno =3D=3D E2BIG ) + rc =3D do_domctl(xch, &domctl); + if ( rc < 0 && errno =3D=3D E2BIG ) { if ( max_batch_sz <=3D 1 ) break; max_batch_sz >>=3D 1; continue; } + if ( rc > 0 ) + { + done +=3D rc; + continue; + } /* Save the first error... */ if ( !ret ) - ret =3D err; + ret =3D rc; /* .. and ignore the rest of them when removing. */ - if ( err && add_mapping !=3D DPCI_REMOVE_MAPPING ) + if ( rc && add_mapping !=3D DPCI_REMOVE_MAPPING ) break; =20 done +=3D nr; --- a/xen/arch/x86/domain_build.c +++ b/xen/arch/x86/domain_build.c @@ -436,7 +436,8 @@ static __init void pvh_add_mem_mapping(s else a =3D p2m_access_rw; =20 - if ( (rc =3D set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i), a)) ) + if ( (rc =3D set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i), + PAGE_ORDER_4K, a)) ) panic("pvh_add_mem_mapping: gfn:%lx mfn:%lx i:%ld rc:%d\n", gfn, mfn, i, rc); if ( !(i & 0xfffff) ) --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2491,7 +2491,7 @@ static int vmx_alloc_vlapic_mapping(stru share_xen_page_with_guest(pg, d, XENSHARE_writable); d->arch.hvm_domain.vmx.apic_access_mfn =3D mfn; set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE), _mfn(mfn),= - p2m_get_hostp2m(d)->default_access); + PAGE_ORDER_4K, p2m_get_hostp2m(d)->default_access);= =20 return 0; } --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -899,48 +899,62 @@ void p2m_change_type_range(struct domain p2m_unlock(p2m); } =20 -/* Returns: 0 for success, -errno for failure */ +/* + * Returns: + * 0 for success + * -errno for failure + * order+1 for caller to retry with order (guaranteed smaller than + * the order value passed in) + */ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn, mfn_t = mfn, - p2m_type_t gfn_p2mt, p2m_access_t access) + unsigned int order, p2m_type_t gfn_p2mt, + p2m_access_t access) { int rc =3D 0; p2m_access_t a; p2m_type_t ot; mfn_t omfn; + unsigned int cur_order =3D 0; struct p2m_domain *p2m =3D p2m_get_hostp2m(d); =20 if ( !paging_mode_translate(d) ) return -EIO; =20 - gfn_lock(p2m, gfn, 0); - omfn =3D p2m->get_entry(p2m, gfn, &ot, &a, 0, NULL, NULL); + gfn_lock(p2m, gfn, order); + omfn =3D p2m->get_entry(p2m, gfn, &ot, &a, 0, &cur_order, NULL); + if ( cur_order < order ) + { + gfn_unlock(p2m, gfn, order); + return cur_order + 1; + } if ( p2m_is_grant(ot) || p2m_is_foreign(ot) ) { - gfn_unlock(p2m, gfn, 0); + gfn_unlock(p2m, gfn, order); domain_crash(d); return -ENOENT; } else if ( p2m_is_ram(ot) ) { + unsigned long i; + ASSERT(mfn_valid(omfn)); - set_gpfn_from_mfn(mfn_x(omfn), INVALID_M2P_ENTRY); + for ( i =3D 0; i < (1UL << order); ++i ) + set_gpfn_from_mfn(mfn_x(omfn) + i, INVALID_M2P_ENTRY); } =20 P2M_DEBUG("set %d %lx %lx\n", gfn_p2mt, gfn, mfn_x(mfn)); - rc =3D p2m_set_entry(p2m, gfn, mfn, PAGE_ORDER_4K, gfn_p2mt, - access); + rc =3D p2m_set_entry(p2m, gfn, mfn, order, gfn_p2mt, access); if ( rc ) - gdprintk(XENLOG_ERR, - "p2m_set_entry failed! mfn=3D%08lx rc:%d\n", - mfn_x(get_gfn_query_unlocked(p2m->domain, gfn, &ot)), = rc); + gdprintk(XENLOG_ERR, "p2m_set_entry: %#lx:%u -> %d (0x%"PRI_mfn")\= n", + gfn, order, rc, mfn_x(mfn)); else if ( p2m_is_pod(ot) ) { pod_lock(p2m); - p2m->pod.entry_count--; + p2m->pod.entry_count -=3D 1UL << order; BUG_ON(p2m->pod.entry_count < 0); pod_unlock(p2m); } - gfn_unlock(p2m, gfn, 0); + gfn_unlock(p2m, gfn, order); =20 return rc; } @@ -949,14 +963,21 @@ static int set_typed_p2m_entry(struct do static int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn) { - return set_typed_p2m_entry(d, gfn, mfn, p2m_map_foreign, + return set_typed_p2m_entry(d, gfn, mfn, PAGE_ORDER_4K, p2m_map_foreign= , p2m_get_hostp2m(d)->default_access); } =20 int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, - p2m_access_t access) + unsigned int order, p2m_access_t access) { - return set_typed_p2m_entry(d, gfn, mfn, p2m_mmio_direct, access); + if ( order && + rangeset_overlaps_range(mmio_ro_ranges, mfn_x(mfn), + mfn_x(mfn) + (1UL << order) - 1) && + !rangeset_contains_range(mmio_ro_ranges, mfn_x(mfn), + mfn_x(mfn) + (1UL << order) - 1) ) + return order; + + return set_typed_p2m_entry(d, gfn, mfn, order, p2m_mmio_direct, = access); } =20 int set_identity_p2m_entry(struct domain *d, unsigned long gfn, @@ -1009,20 +1030,33 @@ int set_identity_p2m_entry(struct domain return ret; } =20 -/* Returns: 0 for success, -errno for failure */ -int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn) +/* + * Returns: + * 0 for success + * -errno for failure + * order+1 for caller to retry with order (guaranteed smaller than + * the order value passed in) + */ +int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, + unsigned int order) { int rc =3D -EINVAL; mfn_t actual_mfn; p2m_access_t a; p2m_type_t t; + unsigned int cur_order =3D 0; struct p2m_domain *p2m =3D p2m_get_hostp2m(d); =20 if ( !paging_mode_translate(d) ) return -EIO; =20 - gfn_lock(p2m, gfn, 0); - actual_mfn =3D p2m->get_entry(p2m, gfn, &t, &a, 0, NULL, NULL); + gfn_lock(p2m, gfn, order); + actual_mfn =3D p2m->get_entry(p2m, gfn, &t, &a, 0, &cur_order, NULL); + if ( cur_order < order ) + { + rc =3D cur_order + 1; + goto out; + } =20 /* Do not use mfn_valid() here as it will usually fail for MMIO = pages. */ if ( (INVALID_MFN =3D=3D mfn_x(actual_mfn)) || (t !=3D p2m_mmio_direct= ) ) @@ -1035,11 +1069,11 @@ int clear_mmio_p2m_entry(struct domain * gdprintk(XENLOG_WARNING, "no mapping between mfn %08lx and gfn %08lx\n", mfn_x(mfn), gfn); - rc =3D p2m_set_entry(p2m, gfn, _mfn(INVALID_MFN), PAGE_ORDER_4K, = p2m_invalid, + rc =3D p2m_set_entry(p2m, gfn, _mfn(INVALID_MFN), order, p2m_invalid, p2m->default_access); =20 out: - gfn_unlock(p2m, gfn, 0); + gfn_unlock(p2m, gfn, order); =20 return rc; } @@ -2095,6 +2129,25 @@ void *map_domain_gfn(struct p2m_domain * return map_domain_page(*mfn); } =20 +static unsigned int mmio_order(const struct domain *d, + unsigned long start_fn, unsigned long nr) +{ + if ( !need_iommu(d) || !iommu_use_hap_pt(d) || + (start_fn & ((1UL << PAGE_ORDER_2M) - 1)) || !(nr >> PAGE_ORDER_2= M) ) + return 0; + + if ( !(start_fn & ((1UL << PAGE_ORDER_1G) - 1)) && (nr >> PAGE_ORDER_1= G) && + hap_has_1gb ) + return PAGE_ORDER_1G; + + if ( hap_has_2mb ) + return PAGE_ORDER_2M; + + return 0; +} + +#define MAP_MMIO_MAX_ITER 64 /* pretty arbitrary */ + int map_mmio_regions(struct domain *d, unsigned long start_gfn, unsigned long nr, @@ -2102,22 +2155,29 @@ int map_mmio_regions(struct domain *d, { int ret =3D 0; unsigned long i; + unsigned int iter, order; =20 if ( !paging_mode_translate(d) ) return 0; =20 - for ( i =3D 0; !ret && i < nr; i++ ) + for ( iter =3D i =3D 0; i < nr && iter < MAP_MMIO_MAX_ITER; + i +=3D 1UL << order, ++iter ) { - ret =3D set_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i), - p2m_get_hostp2m(d)->default_access); - if ( ret ) + /* OR'ing gfn and mfn values will return an order suitable to = both. */ + for ( order =3D mmio_order(d, (start_gfn + i) | (mfn + i), nr - = i); ; + order =3D ret - 1 ) { - unmap_mmio_regions(d, start_gfn, i, mfn); - break; + ret =3D set_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i), = order, + p2m_get_hostp2m(d)->default_access); + if ( ret <=3D 0 ) + break; + ASSERT(ret <=3D order); } + if ( ret < 0 ) + break; } =20 - return ret; + return i =3D=3D nr ? 0 : i ?: ret; } =20 int unmap_mmio_regions(struct domain *d, @@ -2125,20 +2185,30 @@ int unmap_mmio_regions(struct domain *d, unsigned long nr, unsigned long mfn) { - int err =3D 0; + int ret =3D 0; unsigned long i; + unsigned int iter, order; =20 if ( !paging_mode_translate(d) ) return 0; =20 - for ( i =3D 0; i < nr; i++ ) + for ( iter =3D i =3D 0; i < nr && iter < MAP_MMIO_MAX_ITER; + i +=3D 1UL << order, ++iter ) { - int ret =3D clear_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + = i)); - if ( ret ) - err =3D ret; + /* OR'ing gfn and mfn values will return an order suitable to = both. */ + for ( order =3D mmio_order(d, (start_gfn + i) | (mfn + i), nr - = i); ; + order =3D ret - 1 ) + { + ret =3D clear_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i), = order); + if ( ret <=3D 0 ) + break; + ASSERT(ret <=3D order); + } + if ( ret < 0 ) + break; } =20 - return err; + return i =3D=3D nr ? 0 : i ?: ret; } =20 unsigned int p2m_find_altp2m_by_eptp(struct domain *d, uint64_t eptp) --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -136,6 +136,7 @@ static void ept_p2m_type_to_flags(struct entry->r =3D entry->x =3D 1; entry->w =3D !rangeset_contains_singleton(mmio_ro_ranges, entry->mfn); + ASSERT(entry->w || !is_epte_superpage(entry)); entry->a =3D !!cpu_has_vmx_ept_ad; entry->d =3D entry->w && cpu_has_vmx_ept_ad; break; --- a/xen/arch/x86/mm/p2m-pt.c +++ b/xen/arch/x86/mm/p2m-pt.c @@ -72,7 +72,8 @@ static const unsigned long pgt[] =3D { PGT_l3_page_table }; =20 -static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn) +static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn, + unsigned int level) { unsigned long flags; /* @@ -107,6 +108,8 @@ static unsigned long p2m_type_to_flags(p case p2m_mmio_direct: if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) ) flags |=3D _PAGE_RW; + else + ASSERT(!level); return flags | P2M_BASE_FLAGS | _PAGE_PCD; } } @@ -436,7 +439,7 @@ static int do_recalc(struct p2m_domain * p2m_type_t p2mt =3D p2m_is_logdirty_range(p2m, gfn & mask, = gfn | ~mask) ? p2m_ram_logdirty : p2m_ram_rw; unsigned long mfn =3D l1e_get_pfn(e); - unsigned long flags =3D p2m_type_to_flags(p2mt, _mfn(mfn)); + unsigned long flags =3D p2m_type_to_flags(p2mt, _mfn(mfn), = level); =20 if ( level ) { @@ -573,7 +576,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, ASSERT(!mfn_valid(mfn) || p2mt !=3D p2m_mmio_direct); l3e_content =3D mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) ? l3e_from_pfn(mfn_x(mfn), - p2m_type_to_flags(p2mt, mfn) | _PAGE_PSE) + p2m_type_to_flags(p2mt, mfn, 2) | _PAGE_PSE) : l3e_empty(); entry_content.l1 =3D l3e_content.l3; =20 @@ -609,7 +612,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, =20 if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) ) entry_content =3D p2m_l1e_from_pfn(mfn_x(mfn), - p2m_type_to_flags(p2mt, = mfn)); + p2m_type_to_flags(p2mt, mfn, = 0)); else entry_content =3D l1e_empty(); =20 @@ -645,7 +648,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, ASSERT(!mfn_valid(mfn) || p2mt !=3D p2m_mmio_direct); if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) ) l2e_content =3D l2e_from_pfn(mfn_x(mfn), - p2m_type_to_flags(p2mt, mfn) | + p2m_type_to_flags(p2mt, mfn, 1) | _PAGE_PSE); else l2e_content =3D l2e_empty(); --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -1046,10 +1046,12 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe (gfn + nr_mfns - 1) < gfn ) /* wrap? */ break; =20 +#ifndef CONFIG_X86 /* XXX ARM!? */ ret =3D -E2BIG; /* Must break hypercall up as this could take a while. */ if ( nr_mfns > 64 ) break; +#endif =20 ret =3D -EPERM; if ( !iomem_access_permitted(current->domain, mfn, mfn_end) || @@ -1067,7 +1069,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe d->domain_id, gfn, mfn, nr_mfns); =20 ret =3D map_mmio_regions(d, gfn, nr_mfns, mfn); - if ( ret ) + if ( ret < 0 ) printk(XENLOG_G_WARNING "memory_map:fail: dom%d gfn=3D%lx mfn=3D%lx = nr=3D%lx ret:%ld\n", d->domain_id, gfn, mfn, nr_mfns, ret); @@ -1079,7 +1081,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe d->domain_id, gfn, mfn, nr_mfns); =20 ret =3D unmap_mmio_regions(d, gfn, nr_mfns, mfn); - if ( ret && is_hardware_domain(current->domain) ) + if ( ret < 0 && is_hardware_domain(current->domain) ) printk(XENLOG_ERR "memory_map: error %ld removing dom%d access to = [%lx,%lx]\n", ret, d->domain_id, mfn, mfn_end); --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -259,7 +259,7 @@ int guest_remove_page(struct domain *d, } if ( p2mt =3D=3D p2m_mmio_direct ) { - clear_mmio_p2m_entry(d, gmfn, _mfn(mfn)); + clear_mmio_p2m_entry(d, gmfn, _mfn(mfn), 0); put_gfn(d, gmfn); return 1; } --- a/xen/include/asm-x86/p2m.h +++ b/xen/include/asm-x86/p2m.h @@ -574,8 +574,9 @@ int p2m_is_logdirty_range(struct p2m_dom =20 /* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, - p2m_access_t access); -int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn); + unsigned int order, p2m_access_t access); +int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, + unsigned int order); =20 /* Set identity addresses in the p2m table (for pass-through) */ int set_identity_p2m_entry(struct domain *d, unsigned long gfn, --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -542,8 +542,15 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_bind_ =20 =20 /* Bind machine I/O address range -> HVM address range. */ -/* If this returns -E2BIG lower nr_mfns value. */ /* XEN_DOMCTL_memory_mapping */ +/* Returns + - zero success, everything done + - -E2BIG passed in nr_mfns value too large for the implementation + - positive partial success for the first page frames (with + less than nr_mfns), requiring re-invocation by the + caller after updating inputs + - negative error; other than -E2BIG +*/ #define DPCI_ADD_MAPPING 1 #define DPCI_REMOVE_MAPPING 0 struct xen_domctl_memory_mapping { --=__Part2C1BD1FE.3__= Content-Type: text/plain; name="x86-P2M-mmio-large.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-P2M-mmio-large.patch" x86/p2m: use large pages for MMIO mappings=0A=0AWhen mapping large BARs = (e.g. the frame buffer of a graphics card) the=0Aoverhead of establishing = such mappings using only 4k pages has,=0Aparticularly after the XSA-125 = fix, become unacceptable. Alter the=0AXEN_DOMCTL_memory_mapping semantics = once again, so that there's no=0Alonger a fixed amount of guest frames = that represents the upper limit=0Aof what a single invocation can map. = Instead bound execution time by=0Alimiting the number of iterations = (regardless of page size).=0A=0ASigned-off-by: Jan Beulich =0A---=0AOpen issues (perhaps for subsequent changes):=0A- ARM side = unimplemented (and hence libxc for now made cope with both=0A models), = the main issue (besides my inability to test any change=0A there) being = the many internal uses of map_mmio_regions())=0A- iommu_{,un}map_page() = interfaces don't support "order" (hence=0A mmio_order() for now returns = zero when !iommu_hap_pt_share, which in=0A particular means the AMD side = isn't being taken care of just yet, but=0A note that this also has the = intended effect of suppressing non-zero=0A order mappings in the shadow = mode case)=0A---=0Av5: Refine comment in domctl.h.=0Av4: Move cleanup duty = entirely to the caller of the hypercall. Move=0A return value descriptio= n to from commit message to domctl.h.=0Av3: Re-base on top of "x86/hvm: = fold opt_hap_{2mb,1gb} into=0A hap_capabilities". Extend description to = spell out new return value=0A meaning. Add a couple of code comments. = Use PAGE_ORDER_4K instead=0A of literal 0. Take into consideration r/o = MMIO pages.=0Av2: Produce valid entries for large p2m_mmio_direct mappings = in=0A p2m_pt_set_entry(). Don't open code iommu_use_hap_pt() in=0A = mmio_order(). Update function comment of set_typed_p2m_entry() and=0A = clear_mmio_p2m_entry(). Use PRI_mfn. Add ASSERT()s to=0A {,un}map_mmio_r= egions() to detect otherwise endless loops.=0A=0A--- a/tools/libxc/xc_domai= n.c=0A+++ b/tools/libxc/xc_domain.c=0A@@ -2174,7 +2174,7 @@ int xc_domain_m= emory_mapping(=0A {=0A DECLARE_DOMCTL;=0A xc_dominfo_t info;=0A- = int ret =3D 0, err;=0A+ int ret =3D 0, rc;=0A unsigned long done = =3D 0, nr, max_batch_sz;=0A =0A if ( xc_domain_getinfo(xch, domid, 1, = &info) !=3D 1 ||=0A@@ -2199,19 +2199,24 @@ int xc_domain_memory_mapping(=0A= domctl.u.memory_mapping.nr_mfns =3D nr;=0A domctl.u.memory= _mapping.first_gfn =3D first_gfn + done;=0A domctl.u.memory_mapping= .first_mfn =3D first_mfn + done;=0A- err =3D do_domctl(xch, = &domctl);=0A- if ( err && errno =3D=3D E2BIG )=0A+ rc =3D = do_domctl(xch, &domctl);=0A+ if ( rc < 0 && errno =3D=3D E2BIG )=0A = {=0A if ( max_batch_sz <=3D 1 )=0A = break;=0A max_batch_sz >>=3D 1;=0A continue;=0A = }=0A+ if ( rc > 0 )=0A+ {=0A+ done +=3D = rc;=0A+ continue;=0A+ }=0A /* Save the first = error... */=0A if ( !ret )=0A- ret =3D err;=0A+ = ret =3D rc;=0A /* .. and ignore the rest of them when removing. = */=0A- if ( err && add_mapping !=3D DPCI_REMOVE_MAPPING )=0A+ = if ( rc && add_mapping !=3D DPCI_REMOVE_MAPPING )=0A = break;=0A =0A done +=3D nr;=0A--- a/xen/arch/x86/domain_build.c=0A+= ++ b/xen/arch/x86/domain_build.c=0A@@ -436,7 +436,8 @@ static __init void = pvh_add_mem_mapping(s=0A else=0A a =3D p2m_access_rw;= =0A =0A- if ( (rc =3D set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i), = a)) )=0A+ if ( (rc =3D set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + = i),=0A+ PAGE_ORDER_4K, a)) )=0A = panic("pvh_add_mem_mapping: gfn:%lx mfn:%lx i:%ld rc:%d\n",=0A = gfn, mfn, i, rc);=0A if ( !(i & 0xfffff) )=0A--- = a/xen/arch/x86/hvm/vmx/vmx.c=0A+++ b/xen/arch/x86/hvm/vmx/vmx.c=0A@@ = -2491,7 +2491,7 @@ static int vmx_alloc_vlapic_mapping(stru=0A = share_xen_page_with_guest(pg, d, XENSHARE_writable);=0A d->arch.hvm_dom= ain.vmx.apic_access_mfn =3D mfn;=0A set_mmio_p2m_entry(d, paddr_to_pfn(= APIC_DEFAULT_PHYS_BASE), _mfn(mfn),=0A- p2m_get_hostp= 2m(d)->default_access);=0A+ PAGE_ORDER_4K, p2m_get_ho= stp2m(d)->default_access);=0A =0A return 0;=0A }=0A--- a/xen/arch/x86/m= m/p2m.c=0A+++ b/xen/arch/x86/mm/p2m.c=0A@@ -899,48 +899,62 @@ void = p2m_change_type_range(struct domain=0A p2m_unlock(p2m);=0A }=0A =0A-/* = Returns: 0 for success, -errno for failure */=0A+/*=0A+ * Returns:=0A+ * = 0 for success=0A+ * -errno for failure=0A+ * order+1 for = caller to retry with order (guaranteed smaller than=0A+ * the = order value passed in)=0A+ */=0A static int set_typed_p2m_entry(struct = domain *d, unsigned long gfn, mfn_t mfn,=0A- = p2m_type_t gfn_p2mt, p2m_access_t access)=0A+ = unsigned int order, p2m_type_t gfn_p2mt,=0A+ = p2m_access_t access)=0A {=0A int rc =3D 0;=0A p2m_access_t a;=0A = p2m_type_t ot;=0A mfn_t omfn;=0A+ unsigned int cur_order =3D = 0;=0A struct p2m_domain *p2m =3D p2m_get_hostp2m(d);=0A =0A if ( = !paging_mode_translate(d) )=0A return -EIO;=0A =0A- gfn_lock(p2m= , gfn, 0);=0A- omfn =3D p2m->get_entry(p2m, gfn, &ot, &a, 0, NULL, = NULL);=0A+ gfn_lock(p2m, gfn, order);=0A+ omfn =3D p2m->get_entry(p2m= , gfn, &ot, &a, 0, &cur_order, NULL);=0A+ if ( cur_order < order )=0A+ = {=0A+ gfn_unlock(p2m, gfn, order);=0A+ return cur_order + = 1;=0A+ }=0A if ( p2m_is_grant(ot) || p2m_is_foreign(ot) )=0A = {=0A- gfn_unlock(p2m, gfn, 0);=0A+ gfn_unlock(p2m, gfn, = order);=0A domain_crash(d);=0A return -ENOENT;=0A }=0A = else if ( p2m_is_ram(ot) )=0A {=0A+ unsigned long i;=0A+=0A = ASSERT(mfn_valid(omfn));=0A- set_gpfn_from_mfn(mfn_x(omfn), = INVALID_M2P_ENTRY);=0A+ for ( i =3D 0; i < (1UL << order); ++i = )=0A+ set_gpfn_from_mfn(mfn_x(omfn) + i, INVALID_M2P_ENTRY);=0A = }=0A =0A P2M_DEBUG("set %d %lx %lx\n", gfn_p2mt, gfn, mfn_x(mfn));= =0A- rc =3D p2m_set_entry(p2m, gfn, mfn, PAGE_ORDER_4K, gfn_p2mt,=0A- = access);=0A+ rc =3D p2m_set_entry(p2m, gfn, mfn, = order, gfn_p2mt, access);=0A if ( rc )=0A- gdprintk(XENLOG_ERR,= =0A- "p2m_set_entry failed! mfn=3D%08lx rc:%d\n",=0A- = mfn_x(get_gfn_query_unlocked(p2m->domain, gfn, &ot)), rc);=0A+ = gdprintk(XENLOG_ERR, "p2m_set_entry: %#lx:%u -> %d (0x%"PRI_mfn")\n",= =0A+ gfn, order, rc, mfn_x(mfn));=0A else if ( = p2m_is_pod(ot) )=0A {=0A pod_lock(p2m);=0A- p2m->pod.ent= ry_count--;=0A+ p2m->pod.entry_count -=3D 1UL << order;=0A = BUG_ON(p2m->pod.entry_count < 0);=0A pod_unlock(p2m);=0A }=0A- = gfn_unlock(p2m, gfn, 0);=0A+ gfn_unlock(p2m, gfn, order);=0A =0A = return rc;=0A }=0A@@ -949,14 +963,21 @@ static int set_typed_p2m_entry(stru= ct do=0A static int set_foreign_p2m_entry(struct domain *d, unsigned long = gfn,=0A mfn_t mfn)=0A {=0A- return = set_typed_p2m_entry(d, gfn, mfn, p2m_map_foreign,=0A+ return set_typed_p= 2m_entry(d, gfn, mfn, PAGE_ORDER_4K, p2m_map_foreign,=0A = p2m_get_hostp2m(d)->default_access);=0A }=0A =0A int = set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,=0A- = p2m_access_t access)=0A+ unsigned = int order, p2m_access_t access)=0A {=0A- return set_typed_p2m_entry(d, = gfn, mfn, p2m_mmio_direct, access);=0A+ if ( order &&=0A+ = rangeset_overlaps_range(mmio_ro_ranges, mfn_x(mfn),=0A+ = mfn_x(mfn) + (1UL << order) - 1) &&=0A+ !rangeset_cont= ains_range(mmio_ro_ranges, mfn_x(mfn),=0A+ = mfn_x(mfn) + (1UL << order) - 1) )=0A+ return order;=0A+=0A+ = return set_typed_p2m_entry(d, gfn, mfn, order, p2m_mmio_direct, access);=0A= }=0A =0A int set_identity_p2m_entry(struct domain *d, unsigned long = gfn,=0A@@ -1009,20 +1030,33 @@ int set_identity_p2m_entry(struct domain=0A = return ret;=0A }=0A =0A-/* Returns: 0 for success, -errno for failure = */=0A-int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t = mfn)=0A+/*=0A+ * Returns:=0A+ * 0 for success=0A+ * -errno = for failure=0A+ * order+1 for caller to retry with order (guaranteed = smaller than=0A+ * the order value passed in)=0A+ */=0A+int = clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,=0A+ = unsigned int order)=0A {=0A int rc =3D = -EINVAL;=0A mfn_t actual_mfn;=0A p2m_access_t a;=0A p2m_type_t = t;=0A+ unsigned int cur_order =3D 0;=0A struct p2m_domain *p2m =3D = p2m_get_hostp2m(d);=0A =0A if ( !paging_mode_translate(d) )=0A = return -EIO;=0A =0A- gfn_lock(p2m, gfn, 0);=0A- actual_mfn =3D = p2m->get_entry(p2m, gfn, &t, &a, 0, NULL, NULL);=0A+ gfn_lock(p2m, gfn, = order);=0A+ actual_mfn =3D p2m->get_entry(p2m, gfn, &t, &a, 0, = &cur_order, NULL);=0A+ if ( cur_order < order )=0A+ {=0A+ rc = =3D cur_order + 1;=0A+ goto out;=0A+ }=0A =0A /* Do not use = mfn_valid() here as it will usually fail for MMIO pages. */=0A if ( = (INVALID_MFN =3D=3D mfn_x(actual_mfn)) || (t !=3D p2m_mmio_direct) )=0A@@ = -1035,11 +1069,11 @@ int clear_mmio_p2m_entry(struct domain *=0A = gdprintk(XENLOG_WARNING,=0A "no mapping between mfn %08lx = and gfn %08lx\n",=0A mfn_x(mfn), gfn);=0A- rc =3D = p2m_set_entry(p2m, gfn, _mfn(INVALID_MFN), PAGE_ORDER_4K, p2m_invalid,=0A+ = rc =3D p2m_set_entry(p2m, gfn, _mfn(INVALID_MFN), order, p2m_invalid,=0A= p2m->default_access);=0A =0A out:=0A- = gfn_unlock(p2m, gfn, 0);=0A+ gfn_unlock(p2m, gfn, order);=0A =0A = return rc;=0A }=0A@@ -2095,6 +2129,25 @@ void *map_domain_gfn(struct = p2m_domain *=0A return map_domain_page(*mfn);=0A }=0A =0A+static = unsigned int mmio_order(const struct domain *d,=0A+ = unsigned long start_fn, unsigned long nr)=0A+{=0A+ if ( = !need_iommu(d) || !iommu_use_hap_pt(d) ||=0A+ (start_fn & ((1UL << = PAGE_ORDER_2M) - 1)) || !(nr >> PAGE_ORDER_2M) )=0A+ return = 0;=0A+=0A+ if ( !(start_fn & ((1UL << PAGE_ORDER_1G) - 1)) && (nr >> = PAGE_ORDER_1G) &&=0A+ hap_has_1gb )=0A+ return PAGE_ORDER_1G= ;=0A+=0A+ if ( hap_has_2mb )=0A+ return PAGE_ORDER_2M;=0A+=0A+ = return 0;=0A+}=0A+=0A+#define MAP_MMIO_MAX_ITER 64 /* pretty arbitrary = */=0A+=0A int map_mmio_regions(struct domain *d,=0A = unsigned long start_gfn,=0A unsigned long nr,=0A@@ = -2102,22 +2155,29 @@ int map_mmio_regions(struct domain *d,=0A {=0A = int ret =3D 0;=0A unsigned long i;=0A+ unsigned int iter, order;=0A = =0A if ( !paging_mode_translate(d) )=0A return 0;=0A =0A- = for ( i =3D 0; !ret && i < nr; i++ )=0A+ for ( iter =3D i =3D 0; i < nr = && iter < MAP_MMIO_MAX_ITER;=0A+ i +=3D 1UL << order, ++iter )=0A = {=0A- ret =3D set_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + = i),=0A- p2m_get_hostp2m(d)->default_access)= ;=0A- if ( ret )=0A+ /* OR'ing gfn and mfn values will = return an order suitable to both. */=0A+ for ( order =3D mmio_order(= d, (start_gfn + i) | (mfn + i), nr - i); ;=0A+ order =3D ret = - 1 )=0A {=0A- unmap_mmio_regions(d, start_gfn, i, = mfn);=0A- break;=0A+ ret =3D set_mmio_p2m_entry(d, = start_gfn + i, _mfn(mfn + i), order,=0A+ = p2m_get_hostp2m(d)->default_access);=0A+ if ( ret <=3D 0 = )=0A+ break;=0A+ ASSERT(ret <=3D order);=0A = }=0A+ if ( ret < 0 )=0A+ break;=0A }=0A =0A- = return ret;=0A+ return i =3D=3D nr ? 0 : i ?: ret;=0A }=0A =0A int = unmap_mmio_regions(struct domain *d,=0A@@ -2125,20 +2185,30 @@ int = unmap_mmio_regions(struct domain *d,=0A unsigned = long nr,=0A unsigned long mfn)=0A {=0A- int err = =3D 0;=0A+ int ret =3D 0;=0A unsigned long i;=0A+ unsigned int = iter, order;=0A =0A if ( !paging_mode_translate(d) )=0A return = 0;=0A =0A- for ( i =3D 0; i < nr; i++ )=0A+ for ( iter =3D i =3D 0; = i < nr && iter < MAP_MMIO_MAX_ITER;=0A+ i +=3D 1UL << order, = ++iter )=0A {=0A- int ret =3D clear_mmio_p2m_entry(d, start_gfn = + i, _mfn(mfn + i));=0A- if ( ret )=0A- err =3D ret;=0A+ = /* OR'ing gfn and mfn values will return an order suitable to both. = */=0A+ for ( order =3D mmio_order(d, (start_gfn + i) | (mfn + i), = nr - i); ;=0A+ order =3D ret - 1 )=0A+ {=0A+ = ret =3D clear_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i), order);=0A+ = if ( ret <=3D 0 )=0A+ break;=0A+ = ASSERT(ret <=3D order);=0A+ }=0A+ if ( ret < 0 )=0A+ = break;=0A }=0A =0A- return err;=0A+ return i =3D=3D nr ? 0 : = i ?: ret;=0A }=0A =0A unsigned int p2m_find_altp2m_by_eptp(struct domain = *d, uint64_t eptp)=0A--- a/xen/arch/x86/mm/p2m-ept.c=0A+++ b/xen/arch/x86/m= m/p2m-ept.c=0A@@ -136,6 +136,7 @@ static void ept_p2m_type_to_flags(struct= =0A entry->r =3D entry->x =3D 1;=0A entry->w =3D = !rangeset_contains_singleton(mmio_ro_ranges,=0A = entry->mfn);=0A+ ASSERT(entry->w || = !is_epte_superpage(entry));=0A entry->a =3D !!cpu_has_vmx_ept_a= d;=0A entry->d =3D entry->w && cpu_has_vmx_ept_ad;=0A = break;=0A--- a/xen/arch/x86/mm/p2m-pt.c=0A+++ b/xen/arch/x86/mm/p2m-pt.c= =0A@@ -72,7 +72,8 @@ static const unsigned long pgt[] =3D {=0A = PGT_l3_page_table=0A };=0A =0A-static unsigned long p2m_type_to_flags(p2m_t= ype_t t, mfn_t mfn)=0A+static unsigned long p2m_type_to_flags(p2m_type_t = t, mfn_t mfn,=0A+ unsigned int = level)=0A {=0A unsigned long flags;=0A /*=0A@@ -107,6 +108,8 @@ = static unsigned long p2m_type_to_flags(p=0A case p2m_mmio_direct:=0A = if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )=0A = flags |=3D _PAGE_RW;=0A+ else=0A+ ASSERT(!level= );=0A return flags | P2M_BASE_FLAGS | _PAGE_PCD;=0A }=0A = }=0A@@ -436,7 +439,7 @@ static int do_recalc(struct p2m_domain *=0A = p2m_type_t p2mt =3D p2m_is_logdirty_range(p2m, gfn & mask, gfn | = ~mask)=0A ? p2m_ram_logdirty : p2m_ram_rw;=0A= unsigned long mfn =3D l1e_get_pfn(e);=0A- unsigned = long flags =3D p2m_type_to_flags(p2mt, _mfn(mfn));=0A+ unsigned = long flags =3D p2m_type_to_flags(p2mt, _mfn(mfn), level);=0A =0A = if ( level )=0A {=0A@@ -573,7 +576,7 @@ p2m_pt_set_entry(stru= ct p2m_domain *p2m,=0A ASSERT(!mfn_valid(mfn) || p2mt !=3D = p2m_mmio_direct);=0A l3e_content =3D mfn_valid(mfn) || p2m_allows_i= nvalid_mfn(p2mt)=0A ? l3e_from_pfn(mfn_x(mfn),=0A- = p2m_type_to_flags(p2mt, mfn) | _PAGE_PSE)=0A+ = p2m_type_to_flags(p2mt, mfn, 2) | _PAGE_PSE)=0A : = l3e_empty();=0A entry_content.l1 =3D l3e_content.l3;=0A =0A@@ = -609,7 +612,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,=0A =0A = if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )=0A = entry_content =3D p2m_l1e_from_pfn(mfn_x(mfn),=0A- = p2m_type_to_flags(p2mt, mfn));=0A+ = p2m_type_to_flags(p2mt, mfn, 0));=0A = else=0A entry_content =3D l1e_empty();=0A =0A@@ -645,7 +648,7 = @@ p2m_pt_set_entry(struct p2m_domain *p2m,=0A ASSERT(!mfn_valid(mf= n) || p2mt !=3D p2m_mmio_direct);=0A if ( mfn_valid(mfn) || = p2m_allows_invalid_mfn(p2mt) )=0A l2e_content =3D l2e_from_pfn(= mfn_x(mfn),=0A- p2m_type_to_flags(p2m= t, mfn) |=0A+ p2m_type_to_flags(p2mt,= mfn, 1) |=0A _PAGE_PSE);=0A = else=0A l2e_content =3D l2e_empty();=0A--- a/xen/common/domctl= .c=0A+++ b/xen/common/domctl.c=0A@@ -1046,10 +1046,12 @@ long do_domctl(XEN= _GUEST_HANDLE_PARAM(xe=0A (gfn + nr_mfns - 1) < gfn ) /* = wrap? */=0A break;=0A =0A+#ifndef CONFIG_X86 /* XXX ARM!? = */=0A ret =3D -E2BIG;=0A /* Must break hypercall up as = this could take a while. */=0A if ( nr_mfns > 64 )=0A = break;=0A+#endif=0A =0A ret =3D -EPERM;=0A if ( !iomem_acce= ss_permitted(current->domain, mfn, mfn_end) ||=0A@@ -1067,7 +1069,7 @@ = long do_domctl(XEN_GUEST_HANDLE_PARAM(xe=0A d->domain_id= , gfn, mfn, nr_mfns);=0A =0A ret =3D map_mmio_regions(d, gfn, = nr_mfns, mfn);=0A- if ( ret )=0A+ if ( ret < 0 )=0A = printk(XENLOG_G_WARNING=0A "memory_ma= p:fail: dom%d gfn=3D%lx mfn=3D%lx nr=3D%lx ret:%ld\n",=0A = d->domain_id, gfn, mfn, nr_mfns, ret);=0A@@ -1079,7 +1081,7 @@ long = do_domctl(XEN_GUEST_HANDLE_PARAM(xe=0A d->domain_id, = gfn, mfn, nr_mfns);=0A =0A ret =3D unmap_mmio_regions(d, gfn, = nr_mfns, mfn);=0A- if ( ret && is_hardware_domain(current->domai= n) )=0A+ if ( ret < 0 && is_hardware_domain(current->domain) = )=0A printk(XENLOG_ERR=0A "memory_ma= p: error %ld removing dom%d access to [%lx,%lx]\n",=0A = ret, d->domain_id, mfn, mfn_end);=0A--- a/xen/common/memory.c=0A+++ = b/xen/common/memory.c=0A@@ -259,7 +259,7 @@ int guest_remove_page(struct = domain *d,=0A }=0A if ( p2mt =3D=3D p2m_mmio_direct )=0A {=0A- = clear_mmio_p2m_entry(d, gmfn, _mfn(mfn));=0A+ clear_mmio_p2m_= entry(d, gmfn, _mfn(mfn), 0);=0A put_gfn(d, gmfn);=0A = return 1;=0A }=0A--- a/xen/include/asm-x86/p2m.h=0A+++ b/xen/include/as= m-x86/p2m.h=0A@@ -574,8 +574,9 @@ int p2m_is_logdirty_range(struct = p2m_dom=0A =0A /* Set mmio addresses in the p2m table (for pass-through) = */=0A int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t = mfn,=0A- p2m_access_t access);=0A-int clear_mmio_p2m_= entry(struct domain *d, unsigned long gfn, mfn_t mfn);=0A+ = unsigned int order, p2m_access_t access);=0A+int clear_mmio_p2m_entry= (struct domain *d, unsigned long gfn, mfn_t mfn,=0A+ = unsigned int order);=0A =0A /* Set identity addresses in the p2m table = (for pass-through) */=0A int set_identity_p2m_entry(struct domain *d, = unsigned long gfn,=0A--- a/xen/include/public/domctl.h=0A+++ b/xen/include/= public/domctl.h=0A@@ -542,8 +542,15 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_b= ind_=0A =0A =0A /* Bind machine I/O address range -> HVM address range. = */=0A-/* If this returns -E2BIG lower nr_mfns value. */=0A /* XEN_DOMCTL_me= mory_mapping */=0A+/* Returns=0A+ - zero success, everything = done=0A+ - -E2BIG passed in nr_mfns value too large for the implementat= ion=0A+ - positive partial success for the first page frames = (with=0A+ less than nr_mfns), requiring re-invocation= by the=0A+ caller after updating inputs=0A+ - negative = error; other than -E2BIG=0A+*/=0A #define DPCI_ADD_MAPPING 1=0A = #define DPCI_REMOVE_MAPPING 0=0A struct xen_domctl_memory_mapping {=0A --=__Part2C1BD1FE.3__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part2C1BD1FE.3__=--