From: "Jan Beulich" Subject: [PATCH 1/6] x86/EPT: don't walk entire page tables when globally changing types Date: Thu, 17 Apr 2014 16:43:56 +0100 Message-ID: <535012DC020000780000A1B8@nat28.tlf.novell.com> References: <535011EB020000780000A19A@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2E1C3FAC.2__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WaoTn-0002Js-2V for xen-devel@lists.xenproject.org; Thu, 17 Apr 2014 15:43:59 +0000 In-Reply-To: <535011EB020000780000A19A@nat28.tlf.novell.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 , Keir Fraser , suravee.suthikulpanit@amd.com, Tim Deegan , Eddie Dong , Jun Nakajima , Boris Ostrovsky 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. --=__Part2E1C3FAC.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Instead leverage the EPT_MISCONFIG VM exit by marking just the top level entries as needing recalculation of their type, propagating the the recalculation state down as necessary such that the actual recalculation gets done upon access. For this to work, we have to - restrict the types between which conversions can be done (right now only the two types involved in log dirty tracking need to be taken care of) - remember the ranges that log dirty tracking was requested for as well as whether global log dirty tracking is in effect Signed-off-by: Jan Beulich --- a/xen/arch/x86/mm/hap/hap.c +++ b/xen/arch/x86/mm/hap/hap.c @@ -110,11 +110,18 @@ int hap_track_dirty_vram(struct domain * if ( begin_pfn !=3D dirty_vram->begin_pfn || begin_pfn + nr !=3D dirty_vram->end_pfn ) { + unsigned long ostart =3D dirty_vram->begin_pfn; + unsigned long oend =3D dirty_vram->end_pfn; + dirty_vram->begin_pfn =3D begin_pfn; dirty_vram->end_pfn =3D begin_pfn + nr; =20 paging_unlock(d); =20 + if ( oend > ostart ) + p2m_change_type_range(d, ostart, oend, + p2m_ram_logdirty, p2m_ram_rw); + /* set l1e entries of range within P2M table to be read-only. = */ p2m_change_type_range(d, begin_pfn, begin_pfn + nr, p2m_ram_rw, p2m_ram_logdirty); @@ -150,11 +157,16 @@ int hap_track_dirty_vram(struct domain * * If zero pages specified while tracking dirty vram * then stop tracking */ + begin_pfn =3D dirty_vram->begin_pfn; + nr =3D dirty_vram->end_pfn - dirty_vram->begin_pfn; xfree(dirty_vram); d->arch.hvm_domain.dirty_vram =3D NULL; } =20 paging_unlock(d); + if ( nr ) + p2m_change_type_range(d, begin_pfn, begin_pfn + nr, + p2m_ram_logdirty, p2m_ram_rw); } out: if ( dirty_bitmap ) --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -116,8 +116,14 @@ static int p2m_init_hostp2m(struct domai =20 if ( p2m ) { - d->arch.p2m =3D p2m; - return 0; + p2m->logdirty_ranges =3D rangeset_new(d, "log-dirty", + RANGESETF_prettyprint_hex); + if ( p2m->logdirty_ranges ) + { + d->arch.p2m =3D p2m; + return 0; + } + p2m_free_one(p2m); } return -ENOMEM; } @@ -129,6 +135,7 @@ static void p2m_teardown_hostp2m(struct=20 =20 if ( p2m ) { + rangeset_destroy(p2m->logdirty_ranges); p2m_free_one(p2m); d->arch.p2m =3D NULL; } @@ -191,12 +198,25 @@ int p2m_init(struct domain *d) return rc; } =20 +int p2m_is_logdirty_range(struct p2m_domain *p2m, unsigned long start, + unsigned long end) +{ + ASSERT(!p2m_is_nestedp2m(p2m)); + if ( p2m->global_logdirty || + rangeset_contains_range(p2m->logdirty_ranges, start, end) ) + return 1; + if ( rangeset_overlaps_range(p2m->logdirty_ranges, start, end) ) + return -1; + return 0; +} + void p2m_change_entry_type_global(struct domain *d, p2m_type_t ot, p2m_type_t nt) { struct p2m_domain *p2m =3D p2m_get_hostp2m(d); p2m_lock(p2m); p2m->change_entry_type_global(p2m, ot, nt); + p2m->global_logdirty =3D (nt =3D=3D p2m_ram_logdirty); p2m_unlock(p2m); } =20 @@ -713,6 +733,7 @@ void p2m_change_type_range(struct domain unsigned long gfn; mfn_t mfn; struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + int rc =3D 0; =20 BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt)); =20 @@ -726,11 +747,22 @@ void p2m_change_type_range(struct domain mfn =3D p2m->get_entry(p2m, gfn, &pt, &a, 0, &order); while ( order > PAGE_ORDER_4K ) { - if ( pt !=3D ot ) - break; - if ( !(gfn & ((1UL << order) - 1)) && - end > (gfn | ((1UL << order) - 1)) ) - break; + unsigned long mask =3D ~0UL << order; + + /* + * Log-dirty ranges starting/ending in the middle of a super = page + * (with a page split still pending) can't have a consistent = type + * reported for the full range and hence need the split to be + * enforced here. + */ + if ( !p2m_is_changeable(pt) || + p2m_is_logdirty_range(p2m, gfn & mask, gfn | ~mask) >=3D = 0 ) + { + if ( pt !=3D ot ) + break; + if ( !(gfn & ~mask) && end > (gfn | ~mask) ) + break; + } if ( order =3D=3D PAGE_ORDER_1G ) order =3D PAGE_ORDER_2M; else @@ -744,6 +776,26 @@ void p2m_change_type_range(struct domain break; } =20 + switch ( nt ) + { + case p2m_ram_rw: + if ( ot =3D=3D p2m_ram_logdirty ) + rc =3D rangeset_remove_range(p2m->logdirty_ranges, start, end = - 1); + break; + case p2m_ram_logdirty: + if ( ot =3D=3D p2m_ram_rw ) + rc =3D rangeset_add_range(p2m->logdirty_ranges, start, end - = 1); + break; + default: + break; + } + if ( rc ) + { + printk(XENLOG_G_ERR "Error %d manipulating Dom%d's log-dirty = ranges\n", + rc, d->domain_id); + domain_crash(d); + } + p2m->defer_nested_flush =3D 0; if ( nestedhvm_enabled(d) ) p2m_flush_nestedp2m(d); --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -187,7 +187,6 @@ static int ept_split_super_page(struct p epte->mfn +=3D i * trunk; epte->snp =3D (iommu_enabled && iommu_snoop); ASSERT(!epte->rsvd1); - ASSERT(!epte->avail1); ASSERT(!epte->avail3); =20 ept_p2m_type_to_flags(epte, epte->sa_p2mt, epte->access); @@ -270,7 +269,7 @@ static int ept_next_level(struct p2m_dom return GUEST_TABLE_NORMAL_PAGE; } =20 -static bool_t ept_invalidate_emt(mfn_t mfn) +static bool_t ept_invalidate_emt(mfn_t mfn, bool_t recalc) { ept_entry_t *epte =3D map_domain_page(mfn_x(mfn)); unsigned int i; @@ -281,10 +280,12 @@ static bool_t ept_invalidate_emt(mfn_t m ept_entry_t e =3D atomic_read_ept_entry(&epte[i]); =20 if ( !is_epte_valid(&e) || !is_epte_present(&e) || - e.emt =3D=3D MTRR_NUM_TYPES ) + (e.emt =3D=3D MTRR_NUM_TYPES && (e.recalc || !recalc)) ) continue; =20 e.emt =3D MTRR_NUM_TYPES; + if ( recalc ) + e.recalc =3D 1; atomic_write_ept_entry(&epte[i], e); changed =3D 1; } @@ -294,23 +295,17 @@ static bool_t ept_invalidate_emt(mfn_t m return changed; } =20 -bool_t ept_handle_misconfig(uint64_t gpa) +static int resolve_misconfig(struct p2m_domain *p2m, unsigned long gfn) { - struct vcpu *curr =3D current; - struct p2m_domain *p2m =3D p2m_get_hostp2m(curr->domain); struct ept_data *ept =3D &p2m->ept; unsigned int level =3D ept_get_wl(ept); - unsigned long gfn =3D PFN_DOWN(gpa); unsigned long mfn =3D ept_get_asr(ept); ept_entry_t *epte; - int okay; + int rc =3D 0; =20 if ( !mfn ) return 0; =20 - p2m_lock(p2m); - - okay =3D -curr->arch.hvm_vmx.ept_spurious_misconfig; for ( ; ; --level ) { ept_entry_t e; @@ -340,6 +335,13 @@ bool_t ept_handle_misconfig(uint64_t gpa _mfn(e.mfn), 0, &ipat, e.sa_p2mt =3D=3D p2m_mmio_d= irect); e.ipat =3D ipat; + if ( e.recalc && p2m_is_changeable(e.sa_p2mt) ) + { + e.sa_p2mt =3D p2m_is_logdirty_range(p2m, gfn + = i, gfn + i) + ? p2m_ram_logdirty : p2m_ram_rw; + ept_p2m_type_to_flags(&e, e.sa_p2mt, e.access); + } + e.recalc =3D 0; atomic_write_ept_entry(&epte[i], e); } } @@ -348,6 +350,25 @@ bool_t ept_handle_misconfig(uint64_t gpa int emt =3D epte_get_entry_emt(p2m->domain, gfn, = _mfn(e.mfn), level * EPT_TABLE_ORDER, = &ipat, e.sa_p2mt =3D=3D p2m_mmio_dir= ect); + + if ( e.recalc && p2m_is_changeable(e.sa_p2mt) ) + { + unsigned long mask =3D ~0UL << (level * EPT_TABLE_ORD= ER); + + switch ( p2m_is_logdirty_range(p2m, gfn & mask, + gfn | ~mask) ) + { + case 0: + e.sa_p2mt =3D p2m_ram_rw; + break; + case 1: + e.sa_p2mt =3D p2m_ram_logdirty; + break; + default: /* Force split. */ + emt =3D -1; + break; + } + } if ( unlikely(emt < 0) ) { if ( ept_split_super_page(p2m, &e, level, level - 1) = ) @@ -357,27 +378,31 @@ bool_t ept_handle_misconfig(uint64_t gpa continue; } ept_free_entry(p2m, &e, level); - okay =3D 0; + rc =3D -ENOMEM; break; } e.emt =3D emt; e.ipat =3D ipat; + if ( e.recalc && p2m_is_changeable(e.sa_p2mt) ) + ept_p2m_type_to_flags(&e, e.sa_p2mt, e.access); + e.recalc =3D 0; atomic_write_ept_entry(&epte[i], e); } =20 - okay =3D 1; + rc =3D 1; break; } =20 if ( e.emt =3D=3D MTRR_NUM_TYPES ) { ASSERT(is_epte_present(&e)); - ept_invalidate_emt(_mfn(e.mfn)); + ept_invalidate_emt(_mfn(e.mfn), e.recalc); smp_wmb(); e.emt =3D 0; + e.recalc =3D 0; atomic_write_ept_entry(&epte[i], e); unmap_domain_page(epte); - okay =3D 1; + rc =3D 1; } else if ( is_epte_present(&e) && !e.emt ) unmap_domain_page(epte); @@ -388,18 +413,34 @@ bool_t ept_handle_misconfig(uint64_t gpa } =20 unmap_domain_page(epte); - if ( okay > 0 ) + if ( rc ) { struct vcpu *v; =20 - for_each_vcpu ( curr->domain, v ) + for_each_vcpu ( p2m->domain, v ) v->arch.hvm_vmx.ept_spurious_misconfig =3D 1; } + + return rc; +} + +bool_t ept_handle_misconfig(uint64_t gpa) +{ + struct vcpu *curr =3D current; + struct p2m_domain *p2m =3D p2m_get_hostp2m(curr->domain); + bool_t spurious; + int rc; + + p2m_lock(p2m); + + spurious =3D curr->arch.hvm_vmx.ept_spurious_misconfig; + rc =3D resolve_misconfig(p2m, PFN_DOWN(gpa)); curr->arch.hvm_vmx.ept_spurious_misconfig =3D 0; ept_sync_domain(p2m); + p2m_unlock(p2m); =20 - return !!okay; + return rc >=3D !spurious; } =20 /* @@ -416,12 +457,11 @@ ept_set_entry(struct p2m_domain *p2m, un unsigned long gfn_remainder =3D gfn; int i, target =3D order / EPT_TABLE_ORDER; int rc =3D 0; - int ret =3D 0; bool_t direct_mmio =3D (p2mt =3D=3D p2m_mmio_direct); uint8_t ipat =3D 0; int need_modify_vtd_table =3D 1; int vtd_pte_present =3D 0; - int needs_sync =3D 1; + int ret, needs_sync =3D -1; ept_entry_t old_entry =3D { .epte =3D 0 }; ept_entry_t new_entry =3D { .epte =3D 0 }; struct ept_data *ept =3D &p2m->ept; @@ -439,12 +479,23 @@ ept_set_entry(struct p2m_domain *p2m, un (order % EPT_TABLE_ORDER) ) return -EINVAL; =20 + /* Carry out any eventually pending earlier changes first. */ + ret =3D resolve_misconfig(p2m, gfn); + if ( ret < 0 ) + { + ept_sync_domain(p2m); + return ret; + } + if ( ret > 0 ) + needs_sync =3D 1; + ASSERT((target =3D=3D 2 && hvm_hap_has_1gb()) || (target =3D=3D 1 && hvm_hap_has_2mb()) || (target =3D=3D 0)); =20 table =3D map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m))); =20 + ret =3D GUEST_TABLE_MAP_FAILED; for ( i =3D ept_get_wl(ept); i > target; i-- ) { ret =3D ept_next_level(p2m, 0, &table, &gfn_remainder, i); @@ -478,7 +529,7 @@ ept_set_entry(struct p2m_domain *p2m, un /* We reached the target level. */ =20 /* No need to flush if the old entry wasn't valid */ - if ( !is_epte_present(ept_entry) ) + if ( needs_sync < 0 && !is_epte_present(ept_entry) ) needs_sync =3D 0; =20 /* If we're replacing a non-leaf entry with a leaf entry (1GiB or = 2MiB), @@ -596,6 +647,7 @@ static mfn_t ept_get_entry(struct p2m_do u32 index; int i; int ret =3D 0; + bool_t recalc =3D 0; mfn_t mfn =3D _mfn(INVALID_MFN); struct ept_data *ept =3D &p2m->ept; =20 @@ -611,6 +663,8 @@ static mfn_t ept_get_entry(struct p2m_do for ( i =3D ept_get_wl(ept); i > 0; i-- ) { retry: + if ( table[gfn_remainder >> (i * EPT_TABLE_ORDER)].recalc ) + recalc =3D 1; ret =3D ept_next_level(p2m, 1, &table, &gfn_remainder, i); if ( !ret ) goto out; @@ -657,7 +711,12 @@ static mfn_t ept_get_entry(struct p2m_do =20 if ( is_epte_valid(ept_entry) ) { - *t =3D ept_entry->sa_p2mt; + if ( (recalc || ept_entry->recalc) && + p2m_is_changeable(ept_entry->sa_p2mt) ) + *t =3D p2m_is_logdirty_range(p2m, gfn, gfn) ? p2m_ram_logdirty= + : p2m_ram_rw; + else + *t =3D ept_entry->sa_p2mt; *a =3D ept_entry->access; =20 mfn =3D _mfn(ept_entry->mfn); @@ -733,53 +792,18 @@ out: return; } =20 -/* - * Walk the whole p2m table, changing any entries of the old type - * to the new type. This is used in hardware-assisted paging to - * quickly enable or diable log-dirty tracking - */ -static void ept_change_entry_type_page(mfn_t ept_page_mfn, int ept_page_le= vel, - p2m_type_t ot, p2m_type_t nt) -{ - ept_entry_t e, *epte =3D map_domain_page(mfn_x(ept_page_mfn)); - - for ( int i =3D 0; i < EPT_PAGETABLE_ENTRIES; i++ ) - { - if ( !is_epte_valid(epte + i) ) - continue; - - if ( (ept_page_level > 0) && !is_epte_superpage(epte + i) ) - ept_change_entry_type_page(_mfn(epte[i].mfn), - ept_page_level - 1, ot, nt); - else - { - e =3D atomic_read_ept_entry(&epte[i]); - if ( e.sa_p2mt !=3D ot ) - continue; - - e.sa_p2mt =3D nt; - ept_p2m_type_to_flags(&e, nt, e.access); - atomic_write_ept_entry(&epte[i], e); - } - } - - unmap_domain_page(epte); -} - static void ept_change_entry_type_global(struct p2m_domain *p2m, p2m_type_t ot, p2m_type_t nt) { - struct ept_data *ept =3D &p2m->ept; - if ( ept_get_asr(ept) =3D=3D 0 ) - return; + unsigned long mfn =3D ept_get_asr(&p2m->ept); =20 - BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt)); - BUG_ON(p2m_is_mmio(ot) || p2m_is_mmio(nt)); + if ( !mfn || ot =3D=3D nt ) + return; =20 - ept_change_entry_type_page(_mfn(ept_get_asr(ept)), - ept_get_wl(ept), ot, nt); + BUG_ON(!p2m_is_changeable(ot) || !p2m_is_changeable(nt)); =20 - ept_sync_domain(p2m); + if ( ept_invalidate_emt(_mfn(mfn), 1) ) + ept_sync_domain(p2m); } =20 static void ept_memory_type_changed(struct p2m_domain *p2m) @@ -789,7 +813,7 @@ static void ept_memory_type_changed(stru if ( !mfn ) return; =20 - if ( ept_invalidate_emt(_mfn(mfn)) ) + if ( ept_invalidate_emt(_mfn(mfn), 0) ) ept_sync_domain(p2m); } =20 --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -38,7 +38,7 @@ typedef union { ipat : 1, /* bit 6 - Ignore PAT memory type */ sp : 1, /* bit 7 - Is this a superpage? */ rsvd1 : 2, /* bits 9:8 - Reserved for future use */ - avail1 : 1, /* bit 10 - Software available 1 */ + recalc : 1, /* bit 10 - Software available 1 */ snp : 1, /* bit 11 - VT-d snoop control in shared EPT/VT-d usage */ mfn : 40, /* bits 51:12 - Machine physical frame number = */ --- a/xen/include/asm-x86/p2m.h +++ b/xen/include/asm-x86/p2m.h @@ -139,6 +139,10 @@ typedef unsigned int p2m_query_t; | p2m_to_mask(p2m_grant_map_ro) \ | p2m_to_mask(p2m_ram_shared) ) =20 +/* Types that can be subject to bulk transitions. */ +#define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) \ + | p2m_to_mask(p2m_ram_logdirty) ) + #define P2M_POD_TYPES (p2m_to_mask(p2m_populate_on_demand)) =20 /* Pageable types */ @@ -167,6 +171,7 @@ typedef unsigned int p2m_query_t; #define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES) #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES) #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES) +#define p2m_is_changeable(_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES) #define p2m_is_pod(_t) (p2m_to_mask(_t) & P2M_POD_TYPES) #define p2m_is_grant(_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES) /* Grant types are *not* considered valid, because they can be @@ -209,6 +214,11 @@ struct p2m_domain { * threaded on in LRU order. */ struct list_head np2m_list; =20 + /* Host p2m: Log-dirty ranges registered for the domain. */ + struct rangeset *logdirty_ranges; + + /* Host p2m: Global log-dirty mode enabled for the domain. */ + bool_t global_logdirty; =20 /* Host p2m: when this flag is set, don't flush all the nested-p2m=20 * tables on every host-p2m change. The setter of this flag=20 @@ -510,6 +520,9 @@ p2m_type_t p2m_change_type(struct domain /* Report a change affecting memory types. */ void p2m_memory_type_changed(struct domain *d); =20 +int p2m_is_logdirty_range(struct p2m_domain *, unsigned long start, + unsigned long end); + /* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn); int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn); --=__Part2E1C3FAC.2__= Content-Type: text/plain; name="EPT-replace-cetg.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EPT-replace-cetg.patch" x86/EPT: don't walk entire page tables when globally changing types=0A=0AIn= stead leverage the EPT_MISCONFIG VM exit by marking just the top=0Alevel = entries as needing recalculation of their type, propagating the=0Athe = recalculation state down as necessary such that the actual=0Arecalculation = gets done upon access.=0A=0AFor this to work, we have to=0A- restrict the = types between which conversions can be done (right now=0A only the two = types involved in log dirty tracking need to be taken=0A care of)=0A- = remember the ranges that log dirty tracking was requested for as well=0A = as whether global log dirty tracking is in effect=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/arch/x86/mm/hap/hap.c=0A+++ = b/xen/arch/x86/mm/hap/hap.c=0A@@ -110,11 +110,18 @@ int hap_track_dirty_vra= m(struct domain *=0A if ( begin_pfn !=3D dirty_vram->begin_pfn = ||=0A begin_pfn + nr !=3D dirty_vram->end_pfn )=0A = {=0A+ unsigned long ostart =3D dirty_vram->begin_pfn;=0A+ = unsigned long oend =3D dirty_vram->end_pfn;=0A+=0A = dirty_vram->begin_pfn =3D begin_pfn;=0A dirty_vram->end_pfn = =3D begin_pfn + nr;=0A =0A paging_unlock(d);=0A =0A+ = if ( oend > ostart )=0A+ p2m_change_type_range(d, ostart, = oend,=0A+ p2m_ram_logdirty, p2m_ram_rw= );=0A+=0A /* set l1e entries of range within P2M table to be = read-only. */=0A p2m_change_type_range(d, begin_pfn, begin_pfn = + nr,=0A p2m_ram_rw, p2m_ram_logdirty);= =0A@@ -150,11 +157,16 @@ int hap_track_dirty_vram(struct domain *=0A = * If zero pages specified while tracking dirty vram=0A = * then stop tracking=0A */=0A+ begin_pfn =3D = dirty_vram->begin_pfn;=0A+ nr =3D dirty_vram->end_pfn - = dirty_vram->begin_pfn;=0A xfree(dirty_vram);=0A = d->arch.hvm_domain.dirty_vram =3D NULL;=0A }=0A =0A = paging_unlock(d);=0A+ if ( nr )=0A+ p2m_change_type_range= (d, begin_pfn, begin_pfn + nr,=0A+ = p2m_ram_logdirty, p2m_ram_rw);=0A }=0A out:=0A if ( dirty_bitmap = )=0A--- a/xen/arch/x86/mm/p2m.c=0A+++ b/xen/arch/x86/mm/p2m.c=0A@@ -116,8 = +116,14 @@ static int p2m_init_hostp2m(struct domai=0A =0A if ( p2m = )=0A {=0A- d->arch.p2m =3D p2m;=0A- return 0;=0A+ = p2m->logdirty_ranges =3D rangeset_new(d, "log-dirty",=0A+ = RANGESETF_prettyprint_hex);=0A+ if ( = p2m->logdirty_ranges )=0A+ {=0A+ d->arch.p2m =3D = p2m;=0A+ return 0;=0A+ }=0A+ p2m_free_one(p2m);=0A= }=0A return -ENOMEM;=0A }=0A@@ -129,6 +135,7 @@ static void = p2m_teardown_hostp2m(struct =0A =0A if ( p2m )=0A {=0A+ = rangeset_destroy(p2m->logdirty_ranges);=0A p2m_free_one(p2m);=0A = d->arch.p2m =3D NULL;=0A }=0A@@ -191,12 +198,25 @@ int p2m_init(s= truct domain *d)=0A return rc;=0A }=0A =0A+int p2m_is_logdirty_range(st= ruct p2m_domain *p2m, unsigned long start,=0A+ = unsigned long end)=0A+{=0A+ ASSERT(!p2m_is_nestedp2m(p2m));=0A+ if ( = p2m->global_logdirty ||=0A+ rangeset_contains_range(p2m->logdirty_r= anges, start, end) )=0A+ return 1;=0A+ if ( rangeset_overlaps_ran= ge(p2m->logdirty_ranges, start, end) )=0A+ return -1;=0A+ return = 0;=0A+}=0A+=0A void p2m_change_entry_type_global(struct domain *d,=0A = p2m_type_t ot, p2m_type_t nt)=0A {=0A = struct p2m_domain *p2m =3D p2m_get_hostp2m(d);=0A p2m_lock(p2m);=0A = p2m->change_entry_type_global(p2m, ot, nt);=0A+ p2m->global_logdirty = =3D (nt =3D=3D p2m_ram_logdirty);=0A p2m_unlock(p2m);=0A }=0A =0A@@ = -713,6 +733,7 @@ void p2m_change_type_range(struct domain=0A unsigned = long gfn;=0A mfn_t mfn;=0A struct p2m_domain *p2m =3D p2m_get_hostp= 2m(d);=0A+ int rc =3D 0;=0A =0A BUG_ON(p2m_is_grant(ot) || = p2m_is_grant(nt));=0A =0A@@ -726,11 +747,22 @@ void p2m_change_type_range(s= truct domain=0A mfn =3D p2m->get_entry(p2m, gfn, &pt, &a, 0, = &order);=0A while ( order > PAGE_ORDER_4K )=0A {=0A- = if ( pt !=3D ot )=0A- break;=0A- if ( !(gfn = & ((1UL << order) - 1)) &&=0A- end > (gfn | ((1UL << = order) - 1)) )=0A- break;=0A+ unsigned long mask = =3D ~0UL << order;=0A+=0A+ /*=0A+ * Log-dirty = ranges starting/ending in the middle of a super page=0A+ * = (with a page split still pending) can't have a consistent type=0A+ = * reported for the full range and hence need the split to be=0A+ = * enforced here.=0A+ */=0A+ if ( !p2m_is_chang= eable(pt) ||=0A+ p2m_is_logdirty_range(p2m, gfn & mask, = gfn | ~mask) >=3D 0 )=0A+ {=0A+ if ( pt !=3D ot = )=0A+ break;=0A+ if ( !(gfn & ~mask) && = end > (gfn | ~mask) )=0A+ break;=0A+ }=0A = if ( order =3D=3D PAGE_ORDER_1G )=0A order =3D = PAGE_ORDER_2M;=0A else=0A@@ -744,6 +776,26 @@ void p2m_change_t= ype_range(struct domain=0A break;=0A }=0A =0A+ switch ( = nt )=0A+ {=0A+ case p2m_ram_rw:=0A+ if ( ot =3D=3D p2m_ram_log= dirty )=0A+ rc =3D rangeset_remove_range(p2m->logdirty_ranges, = start, end - 1);=0A+ break;=0A+ case p2m_ram_logdirty:=0A+ = if ( ot =3D=3D p2m_ram_rw )=0A+ rc =3D rangeset_add_range(p2m->= logdirty_ranges, start, end - 1);=0A+ break;=0A+ default:=0A+ = break;=0A+ }=0A+ if ( rc )=0A+ {=0A+ printk(XENLOG_G_ER= R "Error %d manipulating Dom%d's log-dirty ranges\n",=0A+ = rc, d->domain_id);=0A+ domain_crash(d);=0A+ }=0A+=0A = p2m->defer_nested_flush =3D 0;=0A if ( nestedhvm_enabled(d) )=0A = p2m_flush_nestedp2m(d);=0A--- a/xen/arch/x86/mm/p2m-ept.c=0A+++ = b/xen/arch/x86/mm/p2m-ept.c=0A@@ -187,7 +187,6 @@ static int ept_split_supe= r_page(struct p=0A epte->mfn +=3D i * trunk;=0A epte->snp = =3D (iommu_enabled && iommu_snoop);=0A ASSERT(!epte->rsvd1);=0A- = ASSERT(!epte->avail1);=0A ASSERT(!epte->avail3);=0A =0A = ept_p2m_type_to_flags(epte, epte->sa_p2mt, epte->access);=0A@@ -270,7 = +269,7 @@ static int ept_next_level(struct p2m_dom=0A return GUEST_TABL= E_NORMAL_PAGE;=0A }=0A =0A-static bool_t ept_invalidate_emt(mfn_t = mfn)=0A+static bool_t ept_invalidate_emt(mfn_t mfn, bool_t recalc)=0A {=0A = ept_entry_t *epte =3D map_domain_page(mfn_x(mfn));=0A unsigned int = i;=0A@@ -281,10 +280,12 @@ static bool_t ept_invalidate_emt(mfn_t m=0A = ept_entry_t e =3D atomic_read_ept_entry(&epte[i]);=0A =0A if ( = !is_epte_valid(&e) || !is_epte_present(&e) ||=0A- e.emt =3D=3D = MTRR_NUM_TYPES )=0A+ (e.emt =3D=3D MTRR_NUM_TYPES && (e.recalc = || !recalc)) )=0A continue;=0A =0A e.emt =3D MTRR_NUM_T= YPES;=0A+ if ( recalc )=0A+ e.recalc =3D 1;=0A = atomic_write_ept_entry(&epte[i], e);=0A changed =3D 1;=0A = }=0A@@ -294,23 +295,17 @@ static bool_t ept_invalidate_emt(mfn_t m=0A = return changed;=0A }=0A =0A-bool_t ept_handle_misconfig(uint64_t gpa)=0A+st= atic int resolve_misconfig(struct p2m_domain *p2m, unsigned long gfn)=0A = {=0A- struct vcpu *curr =3D current;=0A- struct p2m_domain *p2m =3D = p2m_get_hostp2m(curr->domain);=0A struct ept_data *ept =3D &p2m->ept;= =0A unsigned int level =3D ept_get_wl(ept);=0A- unsigned long gfn = =3D PFN_DOWN(gpa);=0A unsigned long mfn =3D ept_get_asr(ept);=0A = ept_entry_t *epte;=0A- int okay;=0A+ int rc =3D 0;=0A =0A if ( = !mfn )=0A return 0;=0A =0A- p2m_lock(p2m);=0A-=0A- okay =3D = -curr->arch.hvm_vmx.ept_spurious_misconfig;=0A for ( ; ; --level )=0A = {=0A ept_entry_t e;=0A@@ -340,6 +335,13 @@ bool_t ept_handle_mis= config(uint64_t gpa=0A = _mfn(e.mfn), 0, &ipat,=0A = e.sa_p2mt =3D=3D p2m_mmio_direct);=0A e.ipat =3D = ipat;=0A+ if ( e.recalc && p2m_is_changeable(e.sa_p2mt) = )=0A+ {=0A+ e.sa_p2mt =3D = p2m_is_logdirty_range(p2m, gfn + i, gfn + i)=0A+ = ? p2m_ram_logdirty : p2m_ram_rw;=0A+ = ept_p2m_type_to_flags(&e, e.sa_p2mt, e.access);=0A+ = }=0A+ e.recalc =3D 0;=0A atomic_writ= e_ept_entry(&epte[i], e);=0A }=0A }=0A@@ = -348,6 +350,25 @@ bool_t ept_handle_misconfig(uint64_t gpa=0A = int emt =3D epte_get_entry_emt(p2m->domain, gfn, _mfn(e.mfn),=0A = level * EPT_TABLE_ORDER, &ipat,=0A = e.sa_p2mt =3D=3D p2m_mmio_direct= );=0A+=0A+ if ( e.recalc && p2m_is_changeable(e.sa_p2mt) = )=0A+ {=0A+ unsigned long mask =3D ~0UL = << (level * EPT_TABLE_ORDER);=0A+=0A+ switch ( = p2m_is_logdirty_range(p2m, gfn & mask,=0A+ = gfn | ~mask) )=0A+ {=0A+ = case 0:=0A+ e.sa_p2mt =3D p2m_ram_rw;=0A+= break;=0A+ case 1:=0A+ = e.sa_p2mt =3D p2m_ram_logdirty;=0A+ = break;=0A+ default: /* Force split. */=0A+ = emt =3D -1;=0A+ break;=0A+ = }=0A+ }=0A if ( unlikely(emt = < 0) )=0A {=0A if ( ept_split_super_pag= e(p2m, &e, level, level - 1) )=0A@@ -357,27 +378,31 @@ bool_t ept_handle_mi= sconfig(uint64_t gpa=0A continue;=0A = }=0A ept_free_entry(p2m, &e, level);=0A- = okay =3D 0;=0A+ rc =3D -ENOMEM;=0A = break;=0A }=0A e.emt =3D emt;=0A = e.ipat =3D ipat;=0A+ if ( e.recalc && = p2m_is_changeable(e.sa_p2mt) )=0A+ ept_p2m_type_to_flags= (&e, e.sa_p2mt, e.access);=0A+ e.recalc =3D 0;=0A = atomic_write_ept_entry(&epte[i], e);=0A }=0A =0A- = okay =3D 1;=0A+ rc =3D 1;=0A break;=0A = }=0A =0A if ( e.emt =3D=3D MTRR_NUM_TYPES )=0A {=0A = ASSERT(is_epte_present(&e));=0A- ept_invalidate_emt(_mfn(e.= mfn));=0A+ ept_invalidate_emt(_mfn(e.mfn), e.recalc);=0A = smp_wmb();=0A e.emt =3D 0;=0A+ e.recalc =3D = 0;=0A atomic_write_ept_entry(&epte[i], e);=0A = unmap_domain_page(epte);=0A- okay =3D 1;=0A+ rc =3D = 1;=0A }=0A else if ( is_epte_present(&e) && !e.emt )=0A = unmap_domain_page(epte);=0A@@ -388,18 +413,34 @@ bool_t ept_handle= _misconfig(uint64_t gpa=0A }=0A =0A unmap_domain_page(epte);=0A- = if ( okay > 0 )=0A+ if ( rc )=0A {=0A struct vcpu *v;=0A = =0A- for_each_vcpu ( curr->domain, v )=0A+ for_each_vcpu ( = p2m->domain, v )=0A v->arch.hvm_vmx.ept_spurious_misconfig =3D = 1;=0A }=0A+=0A+ return rc;=0A+}=0A+=0A+bool_t ept_handle_misconfig(u= int64_t gpa)=0A+{=0A+ struct vcpu *curr =3D current;=0A+ struct = p2m_domain *p2m =3D p2m_get_hostp2m(curr->domain);=0A+ bool_t = spurious;=0A+ int rc;=0A+=0A+ p2m_lock(p2m);=0A+=0A+ spurious =3D = curr->arch.hvm_vmx.ept_spurious_misconfig;=0A+ rc =3D resolve_misconfig(= p2m, PFN_DOWN(gpa));=0A curr->arch.hvm_vmx.ept_spurious_misconfig =3D = 0;=0A ept_sync_domain(p2m);=0A+=0A p2m_unlock(p2m);=0A =0A- = return !!okay;=0A+ return rc >=3D !spurious;=0A }=0A =0A /*=0A@@ = -416,12 +457,11 @@ ept_set_entry(struct p2m_domain *p2m, un=0A = unsigned long gfn_remainder =3D gfn;=0A int i, target =3D order / = EPT_TABLE_ORDER;=0A int rc =3D 0;=0A- int ret =3D 0;=0A bool_t = direct_mmio =3D (p2mt =3D=3D p2m_mmio_direct);=0A uint8_t ipat =3D = 0;=0A int need_modify_vtd_table =3D 1;=0A int vtd_pte_present =3D = 0;=0A- int needs_sync =3D 1;=0A+ int ret, needs_sync =3D -1;=0A = ept_entry_t old_entry =3D { .epte =3D 0 };=0A ept_entry_t new_entry = =3D { .epte =3D 0 };=0A struct ept_data *ept =3D &p2m->ept;=0A@@ = -439,12 +479,23 @@ ept_set_entry(struct p2m_domain *p2m, un=0A = (order % EPT_TABLE_ORDER) )=0A return -EINVAL;=0A =0A+ /* Carry = out any eventually pending earlier changes first. */=0A+ ret =3D = resolve_misconfig(p2m, gfn);=0A+ if ( ret < 0 )=0A+ {=0A+ = ept_sync_domain(p2m);=0A+ return ret;=0A+ }=0A+ if ( ret > 0 = )=0A+ needs_sync =3D 1;=0A+=0A ASSERT((target =3D=3D 2 && = hvm_hap_has_1gb()) ||=0A (target =3D=3D 1 && hvm_hap_has_2mb()) = ||=0A (target =3D=3D 0));=0A =0A table =3D map_domain_page(p= agetable_get_pfn(p2m_get_pagetable(p2m)));=0A =0A+ ret =3D GUEST_TABLE_M= AP_FAILED;=0A for ( i =3D ept_get_wl(ept); i > target; i-- )=0A = {=0A ret =3D ept_next_level(p2m, 0, &table, &gfn_remainder, = i);=0A@@ -478,7 +529,7 @@ ept_set_entry(struct p2m_domain *p2m, un=0A = /* We reached the target level. */=0A =0A /* No need to flush = if the old entry wasn't valid */=0A- if ( !is_epte_present(ept_entry= ) )=0A+ if ( needs_sync < 0 && !is_epte_present(ept_entry) )=0A = needs_sync =3D 0;=0A =0A /* If we're replacing a non-leaf = entry with a leaf entry (1GiB or 2MiB),=0A@@ -596,6 +647,7 @@ static mfn_t = ept_get_entry(struct p2m_do=0A u32 index;=0A int i;=0A int ret = =3D 0;=0A+ bool_t recalc =3D 0;=0A mfn_t mfn =3D _mfn(INVALID_MFN);= =0A struct ept_data *ept =3D &p2m->ept;=0A =0A@@ -611,6 +663,8 @@ = static mfn_t ept_get_entry(struct p2m_do=0A for ( i =3D ept_get_wl(ept)= ; i > 0; i-- )=0A {=0A retry:=0A+ if ( table[gfn_remainder = >> (i * EPT_TABLE_ORDER)].recalc )=0A+ recalc =3D 1;=0A = ret =3D ept_next_level(p2m, 1, &table, &gfn_remainder, i);=0A if ( = !ret )=0A goto out;=0A@@ -657,7 +711,12 @@ static mfn_t = ept_get_entry(struct p2m_do=0A =0A if ( is_epte_valid(ept_entry) )=0A = {=0A- *t =3D ept_entry->sa_p2mt;=0A+ if ( (recalc || = ept_entry->recalc) &&=0A+ p2m_is_changeable(ept_entry->sa_p2mt)= )=0A+ *t =3D p2m_is_logdirty_range(p2m, gfn, gfn) ? p2m_ram_log= dirty=0A+ : p2m_ram_rw= ;=0A+ else=0A+ *t =3D ept_entry->sa_p2mt;=0A *a = =3D ept_entry->access;=0A =0A mfn =3D _mfn(ept_entry->mfn);=0A@@ = -733,53 +792,18 @@ out:=0A return;=0A }=0A =0A-/*=0A- * Walk the whole = p2m table, changing any entries of the old type=0A- * to the new type. = This is used in hardware-assisted paging to=0A- * quickly enable or diable = log-dirty tracking=0A- */=0A-static void ept_change_entry_type_page(mfn_t = ept_page_mfn, int ept_page_level,=0A- = p2m_type_t ot, p2m_type_t nt)=0A-{=0A- ept_entry_t e, *epte =3D = map_domain_page(mfn_x(ept_page_mfn));=0A-=0A- for ( int i =3D 0; i < = EPT_PAGETABLE_ENTRIES; i++ )=0A- {=0A- if ( !is_epte_valid(epte = + i) )=0A- continue;=0A-=0A- if ( (ept_page_level > 0) = && !is_epte_superpage(epte + i) )=0A- ept_change_entry_type_page= (_mfn(epte[i].mfn),=0A- ept_page_leve= l - 1, ot, nt);=0A- else=0A- {=0A- e =3D = atomic_read_ept_entry(&epte[i]);=0A- if ( e.sa_p2mt !=3D ot = )=0A- continue;=0A-=0A- e.sa_p2mt =3D nt;=0A- = ept_p2m_type_to_flags(&e, nt, e.access);=0A- atomic_writ= e_ept_entry(&epte[i], e);=0A- }=0A- }=0A-=0A- unmap_domain_pag= e(epte);=0A-}=0A-=0A static void ept_change_entry_type_global(struct = p2m_domain *p2m,=0A p2m_type_t = ot, p2m_type_t nt)=0A {=0A- struct ept_data *ept =3D &p2m->ept;=0A- = if ( ept_get_asr(ept) =3D=3D 0 )=0A- return;=0A+ unsigned long = mfn =3D ept_get_asr(&p2m->ept);=0A =0A- BUG_ON(p2m_is_grant(ot) || = p2m_is_grant(nt));=0A- BUG_ON(p2m_is_mmio(ot) || p2m_is_mmio(nt));=0A+ = if ( !mfn || ot =3D=3D nt )=0A+ return;=0A =0A- ept_change_entr= y_type_page(_mfn(ept_get_asr(ept)),=0A- = ept_get_wl(ept), ot, nt);=0A+ BUG_ON(!p2m_is_changeable(ot) || = !p2m_is_changeable(nt));=0A =0A- ept_sync_domain(p2m);=0A+ if ( = ept_invalidate_emt(_mfn(mfn), 1) )=0A+ ept_sync_domain(p2m);=0A = }=0A =0A static void ept_memory_type_changed(struct p2m_domain *p2m)=0A@@ = -789,7 +813,7 @@ static void ept_memory_type_changed(stru=0A if ( !mfn = )=0A return;=0A =0A- if ( ept_invalidate_emt(_mfn(mfn)) )=0A+ = if ( ept_invalidate_emt(_mfn(mfn), 0) )=0A ept_sync_domain(p2m);= =0A }=0A =0A--- a/xen/include/asm-x86/hvm/vmx/vmx.h=0A+++ b/xen/include/asm= -x86/hvm/vmx/vmx.h=0A@@ -38,7 +38,7 @@ typedef union {=0A ipat = : 1, /* bit 6 - Ignore PAT memory type */=0A sp : = 1, /* bit 7 - Is this a superpage? */=0A rsvd1 : 2, /* = bits 9:8 - Reserved for future use */=0A- avail1 : 1, /* = bit 10 - Software available 1 */=0A+ recalc : 1, /* bit 10 = - Software available 1 */=0A snp : 1, /* bit 11 - VT-d = snoop control in shared=0A EPT/VT-d usage = */=0A mfn : 40, /* bits 51:12 - Machine physical frame = number */=0A--- a/xen/include/asm-x86/p2m.h=0A+++ b/xen/include/asm-x86/p2m= .h=0A@@ -139,6 +139,10 @@ typedef unsigned int p2m_query_t;=0A = | p2m_to_mask(p2m_grant_map_ro) \=0A | = p2m_to_mask(p2m_ram_shared) )=0A =0A+/* Types that can be subject to bulk = transitions. */=0A+#define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) = \=0A+ | p2m_to_mask(p2m_ram_logdirty) = )=0A+=0A #define P2M_POD_TYPES (p2m_to_mask(p2m_populate_on_demand))=0A = =0A /* Pageable types */=0A@@ -167,6 +171,7 @@ typedef unsigned int = p2m_query_t;=0A #define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)= =0A #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)=0A #define = p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)=0A+#define p2m_is_chan= geable(_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES)=0A #define p2m_is_pod(_= t) (p2m_to_mask(_t) & P2M_POD_TYPES)=0A #define p2m_is_grant(_t) (p2m_to_ma= sk(_t) & P2M_GRANT_TYPES)=0A /* Grant types are *not* considered valid, = because they can be=0A@@ -209,6 +214,11 @@ struct p2m_domain {=0A * = threaded on in LRU order. */=0A struct list_head np2m_list;=0A =0A+ = /* Host p2m: Log-dirty ranges registered for the domain. */=0A+ = struct rangeset *logdirty_ranges;=0A+=0A+ /* Host p2m: Global = log-dirty mode enabled for the domain. */=0A+ bool_t = global_logdirty;=0A =0A /* Host p2m: when this flag is set, don't = flush all the nested-p2m =0A * tables on every host-p2m change. The = setter of this flag =0A@@ -510,6 +520,9 @@ p2m_type_t p2m_change_type(struc= t domain=0A /* Report a change affecting memory types. */=0A void = p2m_memory_type_changed(struct domain *d);=0A =0A+int p2m_is_logdirty_range= (struct p2m_domain *, unsigned long start,=0A+ = unsigned long end);=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 int clear_mmio_p2m_entry(struct domain *d, unsigned = long gfn);=0A --=__Part2E1C3FAC.2__= 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 --=__Part2E1C3FAC.2__=--