From: "Jan Beulich" Subject: =?utf-8?q?=5BPATCH_6/8=5D_x86/EPT=3A_force_re?= =?utf-8?q?=E2=80=91evaluation_of_memory_type_as_necessary?= Date: Wed, 26 Mar 2014 15:36:24 +0000 Message-ID: <5333020802000078000027F7@nat28.tlf.novell.com> References: <5332FF1902000078000027A2@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part12207EE8.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WSpsS-0001lP-Ld for xen-devel@lists.xenproject.org; Wed, 26 Mar 2014 15:36:29 +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. --=__Part12207EE8.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The main goal here is to drop the bogus dependency of epte_get_entry_emt() on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT]. Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the EPT_MISCONFIG VM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected). This is being done in a hierarchical manner to keep execution time down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing). This restoring can result in spurious EPT_MISCONFIG VM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits. Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the EPT_MISCONFIG VM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility. Signed-off-by: Jan Beulich --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -84,6 +84,8 @@ long arch_do_domctl( ret =3D ioports_permit_access(d, fp, fp + np - 1); else ret =3D ioports_deny_access(d, fp, fp + np - 1); + if ( !ret ) + memory_type_changed(d); } break; =20 @@ -705,6 +707,8 @@ long arch_do_domctl( ret, add ? "removing" : "denying", d->domain_id, mfn, mfn + nr_mfns - 1); } + /* Do this unconditionally to cover errors on above failure = paths. */ + memory_type_changed(d); } break; =20 @@ -791,6 +795,8 @@ long arch_do_domctl( "ioport_map: error %ld denying dom%d access to = [%x,%x]\n", ret, d->domain_id, fmp, fmp + np - 1); } + if ( !ret ) + memory_type_changed(d); } break; =20 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6 =20 if ( !hvm_funcs.set_guest_pat(v, guest_pat) ) v->arch.hvm_vcpu.pat_cr =3D guest_pat; + + memory_type_changed(v->domain); + return 1; } =20 --- a/xen/arch/x86/hvm/mtrr.c +++ b/xen/arch/x86/hvm/mtrr.c @@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct doma return 0; } =20 - m->enabled =3D enabled; - m->def_type =3D def_type; + if ( m->enabled !=3D enabled || m->def_type !=3D def_type ) + { + m->enabled =3D enabled; + m->def_type =3D def_type; + memory_type_changed(d); + } =20 return 1; } @@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct dom return 0; =20 fixed_range_base[row] =3D msr_content; + memory_type_changed(d); } =20 return 1; @@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set( =20 m->overlapped =3D is_var_mtrr_overlapped(m); =20 + memory_type_changed(d); + return 1; } =20 @@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct doma HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr, 1, HVMSR_PER_VCPU); =20 +void memory_type_changed(struct domain *d) +{ + if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0] ) + p2m_memory_type_changed(d); +} + uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t = mfn, uint8_t *ipat, bool_t direct_mmio) { @@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domain return MTRR_TYPE_WRBACK; } =20 - gmtrr_mtype =3D is_hvm_domain(d) && v && - d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] ? + gmtrr_mtype =3D is_hvm_domain(d) && v ? get_mtrr_type(&v->arch.hvm_vcpu.mtrr, (gfn << PAGE_SHIFT= )) : MTRR_TYPE_WRBACK; =20 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -3011,6 +3011,16 @@ void vmx_vmexit_handler(struct cpu_user_ break; } =20 + case EXIT_REASON_EPT_MISCONFIG: + { + paddr_t gpa; + + __vmread(GUEST_PHYSICAL_ADDRESS, &gpa); + if ( !ept_handle_misconfig(gpa) ) + goto exit_and_crash; + break; + } + case EXIT_REASON_MONITOR_TRAP_FLAG: v->arch.hvm_vmx.exec_control &=3D ~CPU_BASED_MONITOR_TRAP_FLAG; vmx_update_cpu_exec_control(v); --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -200,6 +200,18 @@ void p2m_change_entry_type_global(struct p2m_unlock(p2m); } =20 +void p2m_memory_type_changed(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + if ( p2m->memory_type_changed ) + { + p2m_lock(p2m); + p2m->memory_type_changed(p2m); + p2m_unlock(p2m); + } +} + mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn, p2m_type_t *t, p2m_access_t *a, p2m_query_t q, unsigned int *page_order, bool_t locked) --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -660,6 +660,130 @@ static void ept_change_entry_type_global ept_sync_domain(p2m); } =20 +static bool_t ept_invalidate_emt(mfn_t mfn) +{ + ept_entry_t *epte =3D map_domain_page(mfn_x(mfn)); + unsigned int i; + bool_t changed =3D 0; + + for ( i =3D 0; i < EPT_PAGETABLE_ENTRIES; i++ ) + { + ept_entry_t e =3D atomic_read_ept_entry(&epte[i]); + + if ( !is_epte_valid(&e) || !is_epte_present(&e) || + e.emt =3D=3D MTRR_NUM_TYPES ) + continue; + + e.emt =3D MTRR_NUM_TYPES; + atomic_write_ept_entry(&epte[i], e); + changed =3D 1; + } + + unmap_domain_page(epte); + + return changed; +} + +static void ept_memory_type_changed(struct p2m_domain *p2m) +{ + unsigned long mfn =3D ept_get_asr(&p2m->ept); + + if ( !mfn ) + return; + + if ( ept_invalidate_emt(_mfn(mfn)) ) + ept_sync_domain(p2m); +} + +bool_t ept_handle_misconfig(uint64_t gpa) +{ + 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; + bool_t okay; + + if ( !mfn ) + return 0; + + p2m_lock(p2m); + + for ( okay =3D curr->arch.hvm_vmx.ept_spurious_misconfig; ; --level) = { + ept_entry_t e; + unsigned int i; + + epte =3D map_domain_page(mfn); + i =3D (gfn >> (level * EPT_TABLE_ORDER)) & (EPT_PAGETABLE_ENTRIES = - 1); + e =3D atomic_read_ept_entry(&epte[i]); + + if ( level =3D=3D 0 || is_epte_superpage(&e) ) + { + uint8_t ipat =3D 0; + struct vcpu *v; + + if ( e.emt !=3D MTRR_NUM_TYPES ) + break; + + if ( level =3D=3D 0 ) + { + for ( gfn -=3D i, i =3D 0; i < EPT_PAGETABLE_ENTRIES; ++i = ) + { + e =3D atomic_read_ept_entry(&epte[i]); + if ( e.emt =3D=3D MTRR_NUM_TYPES ) + e.emt =3D 0; + if ( !is_epte_valid(&e) || !is_epte_present(&e) ) + continue; + e.emt =3D epte_get_entry_emt(p2m->domain, gfn + i, + _mfn(e.mfn), &ipat, + e.sa_p2mt =3D=3D p2m_mmio_d= irect); + e.ipat =3D ipat; + atomic_write_ept_entry(&epte[i], e); + } + } + else + { + e.emt =3D epte_get_entry_emt(p2m->domain, gfn, _mfn(e.mfn)= , + &ipat, + e.sa_p2mt =3D=3D p2m_mmio_direc= t); + e.ipat =3D ipat; + atomic_write_ept_entry(&epte[i], e); + } + + for_each_vcpu ( curr->domain, v ) + v->arch.hvm_vmx.ept_spurious_misconfig =3D 1; + okay =3D 1; + break; + } + + if ( e.emt =3D=3D MTRR_NUM_TYPES ) + { + ASSERT(is_epte_present(&e)); + e.emt =3D 0; + atomic_write_ept_entry(&epte[i], e); + unmap_domain_page(epte); + + ept_invalidate_emt(_mfn(e.mfn)); + okay =3D 1; + } + else if ( is_epte_present(&e) && !e.emt ) + unmap_domain_page(epte); + else + break; + + mfn =3D e.mfn; + } + + unmap_domain_page(epte); + curr->arch.hvm_vmx.ept_spurious_misconfig =3D 0; + ept_sync_domain(p2m); + p2m_unlock(p2m); + + return okay; +} + static void __ept_sync_domain(void *info) { struct ept_data *ept =3D &((struct p2m_domain *)info)->ept; @@ -697,6 +821,7 @@ int ept_p2m_init(struct p2m_domain *p2m) p2m->set_entry =3D ept_set_entry; p2m->get_entry =3D ept_get_entry; p2m->change_entry_type_global =3D ept_change_entry_type_global; + p2m->memory_type_changed =3D ept_memory_type_changed; p2m->audit_p2m =3D NULL; =20 /* Set the memory type used when accessing EPT paging structures. */ @@ -737,6 +862,7 @@ static void ept_dump_p2m_table(unsigned=20 [MTRR_TYPE_WRTHROUGH] =3D "WT", [MTRR_TYPE_WRPROT] =3D "WP", [MTRR_TYPE_WRBACK] =3D "WB", + [MTRR_NUM_TYPES] =3D "??" }; =20 for_each_domain(d) @@ -750,11 +876,16 @@ static void ept_dump_p2m_table(unsigned=20 =20 for ( gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D 1UL << = order ) { + char c =3D 0; + gfn_remainder =3D gfn; table =3D map_domain_page(pagetable_get_pfn(p2m_get_pagetable(= p2m))); =20 for ( i =3D ept_get_wl(ept); i > 0; i-- ) { + ept_entry =3D table + (gfn_remainder >> (i * EPT_TABLE_ORD= ER)); + if ( ept_entry->emt =3D=3D MTRR_NUM_TYPES ) + c =3D '?'; ret =3D ept_next_level(p2m, 1, &table, &gfn_remainder, = i); if ( ret !=3D GUEST_TABLE_NORMAL_PAGE ) break; @@ -775,7 +906,7 @@ static void ept_dump_p2m_table(unsigned=20 memoryTypes[ept_entry->emt][0], memoryTypes[ept_entry->emt][1] ?: ept_entry->emt + '0', - ept_entry->ipat ? '!' : ' '); + c ?: ept_entry->ipat ? '!' : ' '); =20 if ( !(record_counter++ % 100) ) process_pending_softirqs(); --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe ret =3D iomem_permit_access(d, mfn, mfn + nr_mfns - 1); else ret =3D iomem_deny_access(d, mfn, mfn + nr_mfns - 1); +#ifdef CONFIG_X86 + if ( !ret ) + memory_type_changed(d); +#endif } break; =20 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -124,6 +124,9 @@ struct arch_vmx_struct { =20 unsigned long host_cr0; =20 + /* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */ + bool_t ept_spurious_misconfig; + /* Is the guest in real mode? */ uint8_t vmx_realmode; /* Are we emulating rather than VMENTERing? */ --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m) void ept_p2m_uninit(struct p2m_domain *p2m); =20 void ept_walk_table(struct domain *d, unsigned long gfn); +bool_t ept_handle_misconfig(uint64_t gpa); void setup_ept_dump(void); =20 void update_guest_eip(void); --- a/xen/include/asm-x86/mtrr.h +++ b/xen/include/asm-x86/mtrr.h @@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(str uint32_t row, uint64_t msr_content); extern bool_t mtrr_def_type_msr_set(struct domain *, struct mtrr_state *, uint64_t msr_content); +extern void memory_type_changed(struct domain *); extern bool_t pat_msr_set(uint64_t *pat, uint64_t msr); =20 bool_t is_var_mtrr_overlapped(struct mtrr_state *m); --- a/xen/include/asm-x86/p2m.h +++ b/xen/include/asm-x86/p2m.h @@ -233,6 +233,7 @@ struct p2m_domain { void (*change_entry_type_global)(struct p2m_domain = *p2m, p2m_type_t ot, p2m_type_t nt); + void (*memory_type_changed)(struct p2m_domain *p2m); =20 void (*write_p2m_entry)(struct p2m_domain *p2m, unsigned long gfn, l1_pgentry_t = *p, @@ -506,6 +507,9 @@ void p2m_change_type_range(struct domain p2m_type_t p2m_change_type(struct domain *d, unsigned long gfn, p2m_type_t ot, p2m_type_t nt); =20 +/* Report a change affecting memory types. */ +void p2m_memory_type_changed(struct domain *d); + /* 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); --=__Part12207EE8.1__= Content-Type: text/plain; name="EPT-sync-mem-types.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EPT-sync-mem-types.patch" x86/EPT: force re-evaluation of memory type as necessary=0A=0AThe main = goal here is to drop the bogus dependency of=0Aepte_get_entry_emt() on = d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].=0A=0AAny change to state = influencing epte_get_entry_emt()'s decision needs=0Ato result in re-calcula= tion. Do this by using the EPT_MISCONFIG VM=0Aexit, storing an invalid = memory type into EPT's emt field (leaving the=0AIOMMU, which doesn't care = about memory types, unaffected).=0A=0AThis is being done in a hierarchical = manner to keep execution time=0Adown: Initially only the top level = directory gets invalidated this way.=0AUpon access, the involved intermedia= te page table levels get cleared=0Aback to zero, and the leaf entry gets = its field properly set. For 4k=0Aleaves all other entries in the same = directory also get processed to=0Aamortize the cost of the extra VM exit = (which halved the number of=0Athese VM exits in my testing).=0A=0AThis = restoring can result in spurious EPT_MISCONFIG VM exits (since=0Atwo = vCPU-s may access addresses involving identical page table=0Astructures). = Rather than simply returning in such cases (and risking=0Athat such a VM = exit results from a real mis-configuration, which=0Awould then result in = an endless loop rather than killing the VM), a=0Aper-vCPU flag is being = introduced indicating when such a spurious VM=0Aexit might validly happen = - if another one occurs right after VM re-=0Aentry, the flag would = generally end up being clear, causing the VM=0Ato be killed as before on = such VM exits.=0A=0ANote that putting a reserved memory type value in the = EPT structures=0Aisn't formally sanctioned by the specification. Intel = isn't willing to=0Aadjust the specification to make this or a similar use = of the=0AEPT_MISCONFIG VM exit formally possible, but they have indicated = that=0Aus using this is low risk wrt forward compatibility.=0A=0ASigned-off= -by: Jan Beulich =0A=0A--- a/xen/arch/x86/domctl.c=0A+++= b/xen/arch/x86/domctl.c=0A@@ -84,6 +84,8 @@ long arch_do_domctl(=0A = ret =3D ioports_permit_access(d, fp, fp + np - 1);=0A = else=0A ret =3D ioports_deny_access(d, fp, fp + np - 1);=0A+ = if ( !ret )=0A+ memory_type_changed(d);=0A }=0A = break;=0A =0A@@ -705,6 +707,8 @@ long arch_do_domctl(=0A = ret, add ? "removing" : "denying", d->domain_id,=0A = mfn, mfn + nr_mfns - 1);=0A }=0A+ /* Do this unconditio= nally to cover errors on above failure paths. */=0A+ memory_type_cha= nged(d);=0A }=0A break;=0A =0A@@ -791,6 +795,8 @@ long arch_do_domc= tl(=0A "ioport_map: error %ld denying dom%d access = to [%x,%x]\n",=0A ret, d->domain_id, fmp, fmp + np = - 1);=0A }=0A+ if ( !ret )=0A+ memory_type_change= d(d);=0A }=0A break;=0A =0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ = b/xen/arch/x86/hvm/hvm.c=0A@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct= vcpu *v, u6=0A =0A if ( !hvm_funcs.set_guest_pat(v, guest_pat) )=0A = v->arch.hvm_vcpu.pat_cr =3D guest_pat;=0A+=0A+ memory_type_changed= (v->domain);=0A+=0A return 1;=0A }=0A =0A--- a/xen/arch/x86/hvm/mtrr.c= =0A+++ b/xen/arch/x86/hvm/mtrr.c=0A@@ -431,8 +431,12 @@ bool_t mtrr_def_typ= e_msr_set(struct doma=0A return 0;=0A }=0A =0A- m->enabled = =3D enabled;=0A- m->def_type =3D def_type;=0A+ if ( m->enabled !=3D = enabled || m->def_type !=3D def_type )=0A+ {=0A+ m->enabled =3D = enabled;=0A+ m->def_type =3D def_type;=0A+ memory_type_change= d(d);=0A+ }=0A =0A return 1;=0A }=0A@@ -452,6 +456,7 @@ bool_t = mtrr_fix_range_msr_set(struct dom=0A return 0;=0A =0A = fixed_range_base[row] =3D msr_content;=0A+ memory_type_changed(d)= ;=0A }=0A =0A return 1;=0A@@ -496,6 +501,8 @@ bool_t mtrr_var_range= _msr_set(=0A =0A m->overlapped =3D is_var_mtrr_overlapped(m);=0A =0A+ = memory_type_changed(d);=0A+=0A return 1;=0A }=0A =0A@@ -690,6 = +697,12 @@ static int hvm_load_mtrr_msr(struct doma=0A HVM_REGISTER_SAVE_RE= STORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,=0A = 1, HVMSR_PER_VCPU);=0A =0A+void memory_type_changed(struct domain = *d)=0A+{=0A+ if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0]= )=0A+ p2m_memory_type_changed(d);=0A+}=0A+=0A uint8_t epte_get_entr= y_emt(struct domain *d, unsigned long gfn, mfn_t mfn,=0A = uint8_t *ipat, bool_t direct_mmio)=0A {=0A@@ -733,8 +746,7 @@ = uint8_t epte_get_entry_emt(struct domain=0A return MTRR_TYPE_WRBACK= ;=0A }=0A =0A- gmtrr_mtype =3D is_hvm_domain(d) && v &&=0A- = d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] ?=0A+ gmtrr_mtype= =3D is_hvm_domain(d) && v ?=0A get_mtrr_type(&v->arch.hv= m_vcpu.mtrr, (gfn << PAGE_SHIFT)) :=0A MTRR_TYPE_WRBACK;= =0A =0A--- a/xen/arch/x86/hvm/vmx/vmx.c=0A+++ b/xen/arch/x86/hvm/vmx/vmx.c= =0A@@ -3011,6 +3011,16 @@ void vmx_vmexit_handler(struct cpu_user_=0A = break;=0A }=0A =0A+ case EXIT_REASON_EPT_MISCONFIG:=0A+ {=0A+ = paddr_t gpa;=0A+=0A+ __vmread(GUEST_PHYSICAL_ADDRESS, = &gpa);=0A+ if ( !ept_handle_misconfig(gpa) )=0A+ goto = exit_and_crash;=0A+ break;=0A+ }=0A+=0A case EXIT_REASON_MONI= TOR_TRAP_FLAG:=0A v->arch.hvm_vmx.exec_control &=3D ~CPU_BASED_MONI= TOR_TRAP_FLAG;=0A vmx_update_cpu_exec_control(v);=0A--- a/xen/arch/= x86/mm/p2m.c=0A+++ b/xen/arch/x86/mm/p2m.c=0A@@ -200,6 +200,18 @@ void = p2m_change_entry_type_global(struct=0A p2m_unlock(p2m);=0A }=0A = =0A+void p2m_memory_type_changed(struct domain *d)=0A+{=0A+ struct = p2m_domain *p2m =3D p2m_get_hostp2m(d);=0A+=0A+ if ( p2m->memory_type_ch= anged )=0A+ {=0A+ p2m_lock(p2m);=0A+ p2m->memory_type_chan= ged(p2m);=0A+ p2m_unlock(p2m);=0A+ }=0A+}=0A+=0A mfn_t __get_gfn_= type_access(struct p2m_domain *p2m, unsigned long gfn,=0A = p2m_type_t *t, p2m_access_t *a, p2m_query_t q,=0A = unsigned int *page_order, bool_t locked)=0A--- a/xen/arch/x86/mm/p2m-ept.c= =0A+++ b/xen/arch/x86/mm/p2m-ept.c=0A@@ -660,6 +660,130 @@ static void = ept_change_entry_type_global=0A ept_sync_domain(p2m);=0A }=0A = =0A+static bool_t ept_invalidate_emt(mfn_t mfn)=0A+{=0A+ ept_entry_t = *epte =3D map_domain_page(mfn_x(mfn));=0A+ unsigned int i;=0A+ = bool_t changed =3D 0;=0A+=0A+ for ( i =3D 0; i < EPT_PAGETABLE_ENTRIES; = i++ )=0A+ {=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+ continue;=0A+=0A+ = e.emt =3D MTRR_NUM_TYPES;=0A+ atomic_write_ept_entry(&epte[i], = e);=0A+ changed =3D 1;=0A+ }=0A+=0A+ unmap_domain_page(epte);= =0A+=0A+ return changed;=0A+}=0A+=0A+static void ept_memory_type_changed= (struct p2m_domain *p2m)=0A+{=0A+ unsigned long mfn =3D ept_get_asr(&p2m= ->ept);=0A+=0A+ if ( !mfn )=0A+ return;=0A+=0A+ if ( = ept_invalidate_emt(_mfn(mfn)) )=0A+ ept_sync_domain(p2m);=0A+}=0A+= =0A+bool_t ept_handle_misconfig(uint64_t gpa)=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+ = bool_t okay;=0A+=0A+ if ( !mfn )=0A+ return 0;=0A+=0A+ = p2m_lock(p2m);=0A+=0A+ for ( okay =3D curr->arch.hvm_vmx.ept_spurious_mi= sconfig; ; --level) {=0A+ ept_entry_t e;=0A+ unsigned int = i;=0A+=0A+ epte =3D map_domain_page(mfn);=0A+ i =3D (gfn >> = (level * EPT_TABLE_ORDER)) & (EPT_PAGETABLE_ENTRIES - 1);=0A+ e =3D = atomic_read_ept_entry(&epte[i]);=0A+=0A+ if ( level =3D=3D 0 || = is_epte_superpage(&e) )=0A+ {=0A+ uint8_t ipat =3D = 0;=0A+ struct vcpu *v;=0A+=0A+ if ( e.emt !=3D = MTRR_NUM_TYPES )=0A+ break;=0A+=0A+ if ( level = =3D=3D 0 )=0A+ {=0A+ for ( gfn -=3D i, i =3D 0; = i < EPT_PAGETABLE_ENTRIES; ++i )=0A+ {=0A+ = e =3D atomic_read_ept_entry(&epte[i]);=0A+ if ( e.emt = =3D=3D MTRR_NUM_TYPES )=0A+ e.emt =3D 0;=0A+ = if ( !is_epte_valid(&e) || !is_epte_present(&e) )=0A+ = continue;=0A+ e.emt =3D epte_get_entry_emt= (p2m->domain, gfn + i,=0A+ = _mfn(e.mfn), &ipat,=0A+ = e.sa_p2mt =3D=3D p2m_mmio_direct);=0A+ e.ipat =3D = ipat;=0A+ atomic_write_ept_entry(&epte[i], e);=0A+ = }=0A+ }=0A+ else=0A+ {=0A+ = e.emt =3D epte_get_entry_emt(p2m->domain, gfn, _mfn(e.mfn),=0A+ = &ipat,=0A+ = e.sa_p2mt =3D=3D p2m_mmio_direct);=0A+ = e.ipat =3D ipat;=0A+ atomic_write_ept_entry(&epte[i], = e);=0A+ }=0A+=0A+ for_each_vcpu ( curr->domain, v = )=0A+ v->arch.hvm_vmx.ept_spurious_misconfig =3D 1;=0A+ = okay =3D 1;=0A+ break;=0A+ }=0A+=0A+ if ( = e.emt =3D=3D MTRR_NUM_TYPES )=0A+ {=0A+ ASSERT(is_epte_pr= esent(&e));=0A+ e.emt =3D 0;=0A+ atomic_write_ept_ent= ry(&epte[i], e);=0A+ unmap_domain_page(epte);=0A+=0A+ = ept_invalidate_emt(_mfn(e.mfn));=0A+ okay =3D 1;=0A+ = }=0A+ else if ( is_epte_present(&e) && !e.emt )=0A+ = unmap_domain_page(epte);=0A+ else=0A+ break;=0A+=0A+ = mfn =3D e.mfn;=0A+ }=0A+=0A+ unmap_domain_page(epte);=0A+ = curr->arch.hvm_vmx.ept_spurious_misconfig =3D 0;=0A+ ept_sync_domain(p2m= );=0A+ p2m_unlock(p2m);=0A+=0A+ return okay;=0A+}=0A+=0A static void = __ept_sync_domain(void *info)=0A {=0A struct ept_data *ept =3D = &((struct p2m_domain *)info)->ept;=0A@@ -697,6 +821,7 @@ int ept_p2m_init(s= truct p2m_domain *p2m)=0A p2m->set_entry =3D ept_set_entry;=0A = p2m->get_entry =3D ept_get_entry;=0A p2m->change_entry_type_global =3D = ept_change_entry_type_global;=0A+ p2m->memory_type_changed =3D = ept_memory_type_changed;=0A p2m->audit_p2m =3D NULL;=0A =0A /* Set = the memory type used when accessing EPT paging structures. */=0A@@ -737,6 = +862,7 @@ static void ept_dump_p2m_table(unsigned =0A [MTRR_TYPE_WR= THROUGH] =3D "WT",=0A [MTRR_TYPE_WRPROT] =3D "WP",=0A = [MTRR_TYPE_WRBACK] =3D "WB",=0A+ [MTRR_NUM_TYPES] = =3D "??"=0A };=0A =0A for_each_domain(d)=0A@@ -750,11 = +876,16 @@ static void ept_dump_p2m_table(unsigned =0A =0A for ( = gfn =3D 0; gfn <=3D p2m->max_mapped_pfn; gfn +=3D 1UL << order )=0A = {=0A+ char c =3D 0;=0A+=0A gfn_remainder =3D = gfn;=0A table =3D map_domain_page(pagetable_get_pfn(p2m_get_pag= etable(p2m)));=0A =0A for ( i =3D ept_get_wl(ept); i > 0; i-- = )=0A {=0A+ ept_entry =3D table + (gfn_remainder = >> (i * EPT_TABLE_ORDER));=0A+ if ( ept_entry->emt =3D=3D = MTRR_NUM_TYPES )=0A+ c =3D '?';=0A ret = =3D ept_next_level(p2m, 1, &table, &gfn_remainder, i);=0A = if ( ret !=3D GUEST_TABLE_NORMAL_PAGE )=0A break;=0A@@ = -775,7 +906,7 @@ static void ept_dump_p2m_table(unsigned =0A = memoryTypes[ept_entry->emt][0],=0A = memoryTypes[ept_entry->emt][1]=0A ?: ept_entry->= emt + '0',=0A- ept_entry->ipat ? '!' : ' ');=0A+ = c ?: ept_entry->ipat ? '!' : ' ');=0A =0A = if ( !(record_counter++ % 100) )=0A process_pe= nding_softirqs();=0A--- a/xen/common/domctl.c=0A+++ b/xen/common/domctl.c= =0A@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe=0A = ret =3D iomem_permit_access(d, mfn, mfn + nr_mfns - 1);=0A = else=0A ret =3D iomem_deny_access(d, mfn, mfn + nr_mfns - = 1);=0A+#ifdef CONFIG_X86=0A+ if ( !ret )=0A+ memory_type_= changed(d);=0A+#endif=0A }=0A break;=0A =0A--- a/xen/include/asm-x8= 6/hvm/vmx/vmcs.h=0A+++ b/xen/include/asm-x86/hvm/vmx/vmcs.h=0A@@ -124,6 = +124,9 @@ struct arch_vmx_struct {=0A =0A unsigned long = host_cr0;=0A =0A+ /* Do we need to tolerate a spurious EPT_MISCONFIG VM = exit? */=0A+ bool_t ept_spurious_misconfig;=0A+=0A /* = Is the guest in real mode? */=0A uint8_t vmx_realmode;=0A = /* Are we emulating rather than VMENTERing? */=0A--- a/xen/include/asm-= x86/hvm/vmx/vmx.h=0A+++ b/xen/include/asm-x86/hvm/vmx/vmx.h=0A@@ -520,6 = +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)=0A void ept_p2m_uninit(s= truct p2m_domain *p2m);=0A =0A void ept_walk_table(struct domain *d, = unsigned long gfn);=0A+bool_t ept_handle_misconfig(uint64_t gpa);=0A void = setup_ept_dump(void);=0A =0A void update_guest_eip(void);=0A--- a/xen/inclu= de/asm-x86/mtrr.h=0A+++ b/xen/include/asm-x86/mtrr.h=0A@@ -88,6 +88,7 @@ = extern bool_t mtrr_fix_range_msr_set(str=0A = uint32_t row, uint64_t msr_content);=0A extern bool_t mtrr_def_type_m= sr_set(struct domain *, struct mtrr_state *,=0A = uint64_t msr_content);=0A+extern void memory_type_changed(struct = domain *);=0A extern bool_t pat_msr_set(uint64_t *pat, uint64_t msr);=0A = =0A bool_t is_var_mtrr_overlapped(struct mtrr_state *m);=0A--- a/xen/includ= e/asm-x86/p2m.h=0A+++ b/xen/include/asm-x86/p2m.h=0A@@ -233,6 +233,7 @@ = struct p2m_domain {=0A void (*change_entry_type_global)(s= truct p2m_domain *p2m,=0A = p2m_type_t ot,=0A = p2m_type_t nt);=0A+ void (*memory_type_changed)(struct = p2m_domain *p2m);=0A =0A void (*write_p2m_entry)(stru= ct p2m_domain *p2m,=0A unsigned = long gfn, l1_pgentry_t *p,=0A@@ -506,6 +507,9 @@ void p2m_change_type_range= (struct domain=0A p2m_type_t p2m_change_type(struct domain *d, unsigned = long gfn,=0A p2m_type_t ot, p2m_type_t nt);=0A = =0A+/* Report a change affecting memory types. */=0A+void p2m_memory_type_c= hanged(struct domain *d);=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 --=__Part12207EE8.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 --=__Part12207EE8.1__=--