From: "Jan Beulich" Subject: [PATCH 2/8] x86/EPT: simplification and cleanup Date: Wed, 26 Mar 2014 15:33:19 +0000 Message-ID: <5333014F02000078000027E7@nat28.tlf.novell.com> References: <5332FF1902000078000027A2@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartD5E7B92F.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WSppU-0000tD-I2 for xen-devel@lists.xenproject.org; Wed, 26 Mar 2014 15:33:25 +0000 In-Reply-To: <5332FF1902000078000027A2@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 , Tim Deegan , Eddie Dong , Jun Nakajima , Yang Z Zhang 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. --=__PartD5E7B92F.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - drop rsvd*_ prefixes from fields not really reserved anymore - replace odd uses of ? 1 : 0 - drop pointless variables from ept_set_entry() - streamline IOMMU mirroring code in ept_set_entry() - don't open code is_epte_valid() (and properly use it when dumping) - streamline entry cloning in ept_split_super_page() - compact dumping code and output Signed-off-by: Jan Beulich --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -182,14 +182,13 @@ static int ept_split_super_page(struct p { ept_entry_t *epte =3D table + i; =20 - epte->epte =3D 0; - epte->emt =3D ept_entry->emt; - epte->ipat =3D ept_entry->ipat; - epte->sp =3D (level > 1) ? 1 : 0; - epte->access =3D ept_entry->access; - epte->sa_p2mt =3D ept_entry->sa_p2mt; - epte->mfn =3D ept_entry->mfn + i * trunk; - epte->rsvd2_snp =3D ( iommu_enabled && iommu_snoop ) ? 1 : 0; + *epte =3D *ept_entry; + epte->sp =3D (level > 1); + 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); =20 @@ -281,8 +280,6 @@ ept_set_entry(struct p2m_domain *p2m, un { ept_entry_t *table, *ept_entry =3D NULL; unsigned long gfn_remainder =3D gfn; - unsigned long offset =3D 0; - u32 index; int i, target =3D order / EPT_TABLE_ORDER; int rv =3D 0; int ret =3D 0; @@ -324,13 +321,10 @@ ept_set_entry(struct p2m_domain *p2m, un =20 ASSERT(ret !=3D GUEST_TABLE_POD_PAGE || i !=3D target); =20 - index =3D gfn_remainder >> (i * EPT_TABLE_ORDER); - offset =3D gfn_remainder & ((1UL << (i * EPT_TABLE_ORDER)) - 1); - - ept_entry =3D table + index; + ept_entry =3D table + (gfn_remainder >> (i * EPT_TABLE_ORDER)); =20 /* In case VT-d uses same page table, this flag is needed by VT-d = */=20 - vtd_pte_present =3D is_epte_present(ept_entry) ? 1 : 0; + vtd_pte_present =3D is_epte_present(ept_entry); =20 /* * If we're here with i > target, we must be at a leaf node, and @@ -364,7 +358,7 @@ ept_set_entry(struct p2m_domain *p2m, un direct_mmio); =20 new_entry.ipat =3D ipat; - new_entry.sp =3D order ? 1 : 0; + new_entry.sp =3D !!order; new_entry.sa_p2mt =3D p2mt; new_entry.access =3D p2ma; new_entry.rsvd2_snp =3D (iommu_enabled && iommu_snoop); @@ -406,17 +400,14 @@ ept_set_entry(struct p2m_domain *p2m, un /* We just installed the pages we need. */ ASSERT(i =3D=3D target); =20 - index =3D gfn_remainder >> (i * EPT_TABLE_ORDER); - offset =3D gfn_remainder & ((1UL << (i * EPT_TABLE_ORDER)) - 1); - - ept_entry =3D table + index; + ept_entry =3D table + (gfn_remainder >> (i * EPT_TABLE_ORDER)); =20 new_entry.emt =3D epte_get_entry_emt(d, gfn, mfn, &ipat, = direct_mmio); new_entry.ipat =3D ipat; - new_entry.sp =3D i ? 1 : 0; + new_entry.sp =3D !!i; new_entry.sa_p2mt =3D p2mt; new_entry.access =3D p2ma; - new_entry.rsvd2_snp =3D (iommu_enabled && iommu_snoop); + new_entry.snp =3D (iommu_enabled && iommu_snoop); =20 /* the caller should take care of the previous page */ new_entry.mfn =3D mfn_x(mfn); @@ -445,36 +436,20 @@ out: ept_sync_domain(p2m); =20 /* For non-nested p2m, may need to change VT-d page table.*/ - if ( rv && !p2m_is_nestedp2m(p2m) && iommu_enabled && - need_iommu(p2m->domain) && need_modify_vtd_table ) + if ( rv && !p2m_is_nestedp2m(p2m) && need_iommu(d) && + need_modify_vtd_table ) { if ( iommu_hap_pt_share ) - iommu_pte_flush(d, gfn, (u64*)ept_entry, order, vtd_pte_presen= t); + iommu_pte_flush(d, gfn, &ept_entry->epte, order, vtd_pte_prese= nt); else { if ( p2mt =3D=3D p2m_ram_rw ) - { - if ( order > 0 ) - { - for ( i =3D 0; i < (1 << order); i++ ) - iommu_map_page( - p2m->domain, gfn - offset + i, mfn_x(mfn) - = offset + i, - IOMMUF_readable | IOMMUF_writable); - } - else if ( !order ) - iommu_map_page( - p2m->domain, gfn, mfn_x(mfn), IOMMUF_readable | = IOMMUF_writable); - } + for ( i =3D 0; i < (1 << order); i++ ) + iommu_map_page(d, gfn + i, mfn_x(mfn) + i, + IOMMUF_readable | IOMMUF_writable); else - { - if ( order > 0 ) - { - for ( i =3D 0; i < (1 << order); i++ ) - iommu_unmap_page(p2m->domain, gfn - offset + i); - } - else if ( !order ) - iommu_unmap_page(p2m->domain, gfn); - } + for ( i =3D 0; i < (1 << order); i++ ) + iommu_unmap_page(d, gfn + i); } } =20 @@ -558,9 +533,7 @@ static mfn_t ept_get_entry(struct p2m_do goto out; } =20 - /* Need to check for all-zeroes because typecode 0 is p2m_ram and an - * entirely empty entry shouldn't have RAM type. */ - if ( ept_entry->epte !=3D 0 && ept_entry->sa_p2mt !=3D p2m_invalid ) + if ( is_epte_valid(ept_entry) ) { *t =3D ept_entry->sa_p2mt; *a =3D ept_entry->access; @@ -750,12 +723,9 @@ static void ept_dump_p2m_table(unsigned=20 { struct domain *d; ept_entry_t *table, *ept_entry; - mfn_t mfn; int order; int i; - int is_pod; int ret =3D 0; - unsigned long index; unsigned long gfn, gfn_remainder; unsigned long record_counter =3D 0; struct p2m_domain *p2m; @@ -768,12 +738,11 @@ static void ept_dump_p2m_table(unsigned=20 =20 p2m =3D p2m_get_hostp2m(d); ept =3D &p2m->ept; - printk("\ndomain%d EPT p2m table: \n", d->domain_id); + printk("\ndomain%d EPT p2m table:\n", d->domain_id); =20 - for ( gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D (1 << = order) ) + for ( gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D 1UL << = order ) { gfn_remainder =3D gfn; - mfn =3D _mfn(INVALID_MFN); table =3D map_domain_page(pagetable_get_pfn(p2m_get_pagetable(= p2m))); =20 for ( i =3D ept_get_wl(ept); i > 0; i-- ) @@ -784,25 +753,18 @@ static void ept_dump_p2m_table(unsigned=20 } =20 order =3D i * EPT_TABLE_ORDER; - - if ( ret =3D=3D GUEST_TABLE_MAP_FAILED ) - goto out; - - index =3D gfn_remainder >> order; - ept_entry =3D table + index; - if ( ept_entry->sa_p2mt !=3D p2m_invalid ) + ept_entry =3D table + (gfn_remainder >> order); + if ( ret !=3D GUEST_TABLE_MAP_FAILED && is_epte_valid(ept_entr= y) ) { - ( ept_entry->sa_p2mt =3D=3D p2m_populate_on_demand ) ?=20 - ( mfn =3D _mfn(INVALID_MFN), is_pod =3D 1 ) : - ( mfn =3D _mfn(ept_entry->mfn), is_pod =3D 0 ); - - printk("gfn: %-16lx mfn: %-16lx order: %2d is_pod: = %d\n", - gfn, mfn_x(mfn), order, is_pod); + if ( ept_entry->sa_p2mt =3D=3D p2m_populate_on_demand ) + printk("gfn: %13lx order: %2d PoD\n", gfn, order); + else + printk("gfn: %13lx order: %2d mfn: %13lx\n", + gfn, order, ept_entry->mfn + 0UL); =20 if ( !(record_counter++ % 100) ) process_pending_softirqs(); } -out: unmap_domain_page(table); } } --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -39,13 +39,13 @@ typedef union { sp : 1, /* bit 7 - Is this a superpage? */ rsvd1 : 2, /* bits 9:8 - Reserved for future use */ avail1 : 1, /* bit 10 - Software available 1 */ - rsvd2_snp : 1, /* bit 11 - Used for VT-d snoop control - in shared EPT/VT-d usage */ + snp : 1, /* bit 11 - VT-d snoop control in shared + EPT/VT-d usage */ mfn : 40, /* bits 51:12 - Machine physical frame number = */ sa_p2mt : 6, /* bits 57:52 - Software available 2 */ access : 4, /* bits 61:58 - p2m_access_t */ - rsvd3_tm : 1, /* bit 62 - Used for VT-d transient-mapping - hint in shared EPT/VT-d usage */ + tm : 1, /* bit 62 - VT-d transient-mapping hint in + shared EPT/VT-d usage */ avail3 : 1; /* bit 63 - Software available 3 */ }; u64 epte; --=__PartD5E7B92F.1__= Content-Type: text/plain; name="EPT-simplify.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EPT-simplify.patch" x86/EPT: simplification and cleanup=0A=0A- drop rsvd*_ prefixes from = fields not really reserved anymore=0A- replace odd uses of ? 1 : = 0=0A- drop pointless variables from ept_set_entry()=0A- streamline IOMMU = mirroring code in ept_set_entry()=0A- don't open code is_epte_valid() (and = properly use it when dumping)=0A- streamline entry cloning in ept_split_sup= er_page()=0A- compact dumping code and output=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/arch/x86/mm/p2m-ept.c=0A+++ = b/xen/arch/x86/mm/p2m-ept.c=0A@@ -182,14 +182,13 @@ static int ept_split_su= per_page(struct p=0A {=0A ept_entry_t *epte =3D table + i;=0A = =0A- epte->epte =3D 0;=0A- epte->emt =3D ept_entry->emt;=0A- = epte->ipat =3D ept_entry->ipat;=0A- epte->sp =3D (level > 1) = ? 1 : 0;=0A- epte->access =3D ept_entry->access;=0A- = epte->sa_p2mt =3D ept_entry->sa_p2mt;=0A- epte->mfn =3D ept_entry->m= fn + i * trunk;=0A- epte->rsvd2_snp =3D ( iommu_enabled && = iommu_snoop ) ? 1 : 0;=0A+ *epte =3D *ept_entry;=0A+ = epte->sp =3D (level > 1);=0A+ epte->mfn +=3D i * trunk;=0A+ = epte->snp =3D (iommu_enabled && iommu_snoop);=0A+ ASSERT(!epte->rsvd= 1);=0A+ ASSERT(!epte->avail1);=0A+ ASSERT(!epte->avail3);=0A = =0A ept_p2m_type_to_flags(epte, epte->sa_p2mt, epte->access);=0A = =0A@@ -281,8 +280,6 @@ ept_set_entry(struct p2m_domain *p2m, un=0A {=0A = ept_entry_t *table, *ept_entry =3D NULL;=0A unsigned long gfn_remainde= r =3D gfn;=0A- unsigned long offset =3D 0;=0A- u32 index;=0A int = i, target =3D order / EPT_TABLE_ORDER;=0A int rv =3D 0;=0A int ret = =3D 0;=0A@@ -324,13 +321,10 @@ ept_set_entry(struct p2m_domain *p2m, un=0A = =0A ASSERT(ret !=3D GUEST_TABLE_POD_PAGE || i !=3D target);=0A =0A- = index =3D gfn_remainder >> (i * EPT_TABLE_ORDER);=0A- offset =3D = gfn_remainder & ((1UL << (i * EPT_TABLE_ORDER)) - 1);=0A-=0A- ept_entry = =3D table + index;=0A+ ept_entry =3D table + (gfn_remainder >> (i * = EPT_TABLE_ORDER));=0A =0A /* In case VT-d uses same page table, this = flag is needed by VT-d */ =0A- vtd_pte_present =3D is_epte_present(ept_e= ntry) ? 1 : 0;=0A+ vtd_pte_present =3D is_epte_present(ept_entry);=0A = =0A /*=0A * If we're here with i > target, we must be at a leaf = node, and=0A@@ -364,7 +358,7 @@ ept_set_entry(struct p2m_domain *p2m, = un=0A direct_mmio);=0A =0A = new_entry.ipat =3D ipat;=0A- new_entry.sp =3D order = ? 1 : 0;=0A+ new_entry.sp =3D !!order;=0A new_entry.= sa_p2mt =3D p2mt;=0A new_entry.access =3D p2ma;=0A = new_entry.rsvd2_snp =3D (iommu_enabled && iommu_snoop);=0A@@ -406,17 = +400,14 @@ ept_set_entry(struct p2m_domain *p2m, un=0A /* We just = installed the pages we need. */=0A ASSERT(i =3D=3D target);=0A = =0A- index =3D gfn_remainder >> (i * EPT_TABLE_ORDER);=0A- = offset =3D gfn_remainder & ((1UL << (i * EPT_TABLE_ORDER)) - 1);=0A-=0A- = ept_entry =3D table + index;=0A+ ept_entry =3D table + = (gfn_remainder >> (i * EPT_TABLE_ORDER));=0A =0A new_entry.emt =3D = epte_get_entry_emt(d, gfn, mfn, &ipat, direct_mmio);=0A new_entry.i= pat =3D ipat;=0A- new_entry.sp =3D i ? 1 : 0;=0A+ new_entry.s= p =3D !!i;=0A new_entry.sa_p2mt =3D p2mt;=0A new_entry.acce= ss =3D p2ma;=0A- new_entry.rsvd2_snp =3D (iommu_enabled && = iommu_snoop);=0A+ new_entry.snp =3D (iommu_enabled && iommu_snoop);= =0A =0A /* the caller should take care of the previous page */=0A = new_entry.mfn =3D mfn_x(mfn);=0A@@ -445,36 +436,20 @@ out:=0A = ept_sync_domain(p2m);=0A =0A /* For non-nested p2m, may need to = change VT-d page table.*/=0A- if ( rv && !p2m_is_nestedp2m(p2m) && = iommu_enabled &&=0A- need_iommu(p2m->domain) && need_modify_vtd_tab= le )=0A+ if ( rv && !p2m_is_nestedp2m(p2m) && need_iommu(d) &&=0A+ = need_modify_vtd_table )=0A {=0A if ( iommu_hap_pt_share = )=0A- iommu_pte_flush(d, gfn, (u64*)ept_entry, order, vtd_pte_pr= esent);=0A+ iommu_pte_flush(d, gfn, &ept_entry->epte, order, = vtd_pte_present);=0A else=0A {=0A if ( p2mt = =3D=3D p2m_ram_rw )=0A- {=0A- if ( order > 0 = )=0A- {=0A- for ( i =3D 0; i < (1 << = order); i++ )=0A- iommu_map_page(=0A- = p2m->domain, gfn - offset + i, mfn_x(mfn) - offset + i,=0A- = IOMMUF_readable | IOMMUF_writable);=0A- = }=0A- else if ( !order )=0A- = iommu_map_page(=0A- p2m->domain, gfn, mfn_x(mfn), = IOMMUF_readable | IOMMUF_writable);=0A- }=0A+ = for ( i =3D 0; i < (1 << order); i++ )=0A+ iommu_map_pag= e(d, gfn + i, mfn_x(mfn) + i,=0A+ = IOMMUF_readable | IOMMUF_writable);=0A else=0A- = {=0A- if ( order > 0 )=0A- {=0A- = for ( i =3D 0; i < (1 << order); i++ )=0A- = iommu_unmap_page(p2m->domain, gfn - offset + i);=0A- }=0A- = else if ( !order )=0A- iommu_unmap_page(p2= m->domain, gfn);=0A- }=0A+ for ( i =3D 0; i < (1 = << order); i++ )=0A+ iommu_unmap_page(d, gfn + i);=0A = }=0A }=0A =0A@@ -558,9 +533,7 @@ static mfn_t ept_get_entry(struc= t p2m_do=0A goto out;=0A }=0A =0A- /* Need to check for = all-zeroes because typecode 0 is p2m_ram and an=0A- * entirely empty = entry shouldn't have RAM type. */=0A- if ( ept_entry->epte !=3D 0 && = ept_entry->sa_p2mt !=3D p2m_invalid )=0A+ if ( is_epte_valid(ept_entry) = )=0A {=0A *t =3D ept_entry->sa_p2mt;=0A *a =3D = ept_entry->access;=0A@@ -750,12 +723,9 @@ static void ept_dump_p2m_table(un= signed =0A {=0A struct domain *d;=0A ept_entry_t *table, *ept_entry= ;=0A- mfn_t mfn;=0A int order;=0A int i;=0A- int is_pod;=0A = int ret =3D 0;=0A- unsigned long index;=0A unsigned long gfn, = gfn_remainder;=0A unsigned long record_counter =3D 0;=0A struct = p2m_domain *p2m;=0A@@ -768,12 +738,11 @@ static void ept_dump_p2m_table(uns= igned =0A =0A p2m =3D p2m_get_hostp2m(d);=0A ept =3D = &p2m->ept;=0A- printk("\ndomain%d EPT p2m table: \n", d->domain_id);= =0A+ printk("\ndomain%d EPT p2m table:\n", d->domain_id);=0A =0A- = for ( gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D (1 << order) = )=0A+ for ( gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D 1UL = << order )=0A {=0A gfn_remainder =3D gfn;=0A- = mfn =3D _mfn(INVALID_MFN);=0A table =3D map_domain_page(paget= able_get_pfn(p2m_get_pagetable(p2m)));=0A =0A for ( i =3D = ept_get_wl(ept); i > 0; i-- )=0A@@ -784,25 +753,18 @@ static void = ept_dump_p2m_table(unsigned =0A }=0A =0A order =3D = i * EPT_TABLE_ORDER;=0A-=0A- if ( ret =3D=3D GUEST_TABLE_MAP_FAI= LED )=0A- goto out;=0A-=0A- index =3D gfn_remaind= er >> order;=0A- ept_entry =3D table + index;=0A- if = ( ept_entry->sa_p2mt !=3D p2m_invalid )=0A+ ept_entry =3D table = + (gfn_remainder >> order);=0A+ if ( ret !=3D GUEST_TABLE_MAP_FA= ILED && is_epte_valid(ept_entry) )=0A {=0A- ( = ept_entry->sa_p2mt =3D=3D p2m_populate_on_demand ) ? =0A- ( = mfn =3D _mfn(INVALID_MFN), is_pod =3D 1 ) :=0A- ( mfn =3D = _mfn(ept_entry->mfn), is_pod =3D 0 );=0A-=0A- printk("gfn: = %-16lx mfn: %-16lx order: %2d is_pod: %d\n",=0A- = gfn, mfn_x(mfn), order, is_pod);=0A+ if ( ept_entry->sa_p2mt= =3D=3D p2m_populate_on_demand )=0A+ printk("gfn: %13lx = order: %2d PoD\n", gfn, order);=0A+ else=0A+ = printk("gfn: %13lx order: %2d mfn: %13lx\n",=0A+ = gfn, order, ept_entry->mfn + 0UL);=0A =0A if ( = !(record_counter++ % 100) )=0A process_pending_softirqs= ();=0A }=0A-out:=0A unmap_domain_page(table);=0A = }=0A }=0A--- a/xen/include/asm-x86/hvm/vmx/vmx.h=0A+++ b/xen/incl= ude/asm-x86/hvm/vmx/vmx.h=0A@@ -39,13 +39,13 @@ typedef union {=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- rsvd2_snp : 1, = /* bit 11 - Used for VT-d snoop control=0A- = in shared EPT/VT-d usage */=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 sa_p2mt : 6, /* bits 57:52 - Software = available 2 */=0A access : 4, /* bits 61:58 - p2m_access_t = */=0A- rsvd3_tm : 1, /* bit 62 - Used for VT-d transient-mappi= ng=0A- hint in shared EPT/VT-d usage */=0A+ = tm : 1, /* bit 62 - VT-d transient-mapping hint in=0A+ = shared EPT/VT-d usage */=0A avail3 = : 1; /* bit 63 - Software available 3 */=0A };=0A u64 epte;=0A --=__PartD5E7B92F.1__= 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 --=__PartD5E7B92F.1__=--