From: "Jan Beulich" Subject: [PATCH 1/2] VMX: correct feature checks for MPX and XSAVES Date: Fri, 02 Sep 2016 04:21:26 -0600 Message-ID: <57C96EC6020000780010B305@prv-mh.provo.novell.com> References: <57C96DBF020000780010B2E8@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part122481B6.2__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bflbH-0000ZG-9D for xen-devel@lists.xenproject.org; Fri, 02 Sep 2016 10:21:31 +0000 In-Reply-To: <57C96DBF020000780010B2E8@prv-mh.provo.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Andrew Cooper , Kevin Tian , Jun Nakajima 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. --=__Part122481B6.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Their VMCS fields aren't tied to the respective base CPU feature flags but instead to VMX specific ones. Note that while the VMCS GUEST_BNDCFGS field exists if either of the two respective features is available, MPX continues to get exposed to guests only with both features present. Also add the so far missing handling of - GUEST_BNDCFGS in construct_vmcs() - MSR_IA32_BNDCFGS in vmx_msr_{read,write}_intercept() and mirror the extra correctness checks during MSR write to vmx_load_msr(). Reported-by: "Rockosov, Dmitry" Signed-off-by: Jan Beulich Tested-by: "Rockosov, Dmitry" --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -168,8 +168,7 @@ static void __init calculate_hvm_feature */ if ( cpu_has_vmx ) { - if ( !(vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) || - !(vmx_vmentry_control & VM_ENTRY_LOAD_BNDCFGS) ) + if ( !cpu_has_vmx_mpx ) __clear_bit(X86_FEATURE_MPX, hvm_featureset); =20 if ( !cpu_has_vmx_xsaves ) --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1261,6 +1261,8 @@ static int construct_vmcs(struct vcpu *v __vmwrite(HOST_PAT, host_pat); __vmwrite(GUEST_PAT, guest_pat); } + if ( cpu_has_vmx_mpx ) + __vmwrite(GUEST_BNDCFGS, 0); if ( cpu_has_vmx_xsaves ) __vmwrite(XSS_EXIT_BITMAP, 0); =20 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -788,14 +788,15 @@ static int vmx_load_vmcs_ctxt(struct vcp =20 static unsigned int __init vmx_init_msr(void) { - return !!cpu_has_mpx + !!cpu_has_xsaves; + return (cpu_has_mpx && cpu_has_vmx_mpx) + + (cpu_has_xsaves && cpu_has_vmx_xsaves); } =20 static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt) { vmx_vmcs_enter(v); =20 - if ( cpu_has_mpx ) + if ( cpu_has_mpx && cpu_has_vmx_mpx ) { __vmread(GUEST_BNDCFGS, &ctxt->msr[ctxt->count].val); if ( ctxt->msr[ctxt->count].val ) @@ -804,7 +805,7 @@ static void vmx_save_msr(struct vcpu *v, =20 vmx_vmcs_exit(v); =20 - if ( cpu_has_xsaves ) + if ( cpu_has_xsaves && cpu_has_vmx_xsaves ) { ctxt->msr[ctxt->count].val =3D v->arch.hvm_vcpu.msr_xss; if ( ctxt->msr[ctxt->count].val ) @@ -824,13 +825,15 @@ static int vmx_load_msr(struct vcpu *v, switch ( ctxt->msr[i].index ) { case MSR_IA32_BNDCFGS: - if ( cpu_has_mpx ) + if ( cpu_has_mpx && cpu_has_vmx_mpx && + is_canonical_address(ctxt->msr[i].val) && + !(ctxt->msr[i].val & IA32_BNDCFGS_RESERVED) ) __vmwrite(GUEST_BNDCFGS, ctxt->msr[i].val); else if ( ctxt->msr[i].val ) err =3D -ENXIO; break; case MSR_IA32_XSS: - if ( cpu_has_xsaves ) + if ( cpu_has_xsaves && cpu_has_vmx_xsaves ) v->arch.hvm_vcpu.msr_xss =3D ctxt->msr[i].val; else err =3D -ENXIO; @@ -2643,6 +2646,11 @@ static int vmx_msr_read_intercept(unsign case MSR_IA32_DEBUGCTLMSR: __vmread(GUEST_IA32_DEBUGCTL, msr_content); break; + case MSR_IA32_BNDCFGS: + if ( !cpu_has_mpx || !cpu_has_vmx_mpx ) + goto gp_fault; + __vmread(GUEST_BNDCFGS, msr_content); + break; case MSR_IA32_FEATURE_CONTROL: case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_VMFUNC: if ( !nvmx_msr_read_intercept(msr, msr_content) ) @@ -2869,6 +2877,13 @@ static int vmx_msr_write_intercept(unsig =20 break; } + case MSR_IA32_BNDCFGS: + if ( !cpu_has_mpx || !cpu_has_vmx_mpx || + !is_canonical_address(msr_content) || + (msr_content & IA32_BNDCFGS_RESERVED) ) + goto gp_fault; + __vmwrite(GUEST_BNDCFGS, msr_content); + break; case MSR_IA32_FEATURE_CONTROL: case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_TRUE_ENTRY_CTLS: if ( !nvmx_msr_write_intercept(msr, msr_content) ) --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -375,6 +375,9 @@ extern u64 vmx_ept_vpid_cap; (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS) #define cpu_has_vmx_pml \ (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_PML) +#define cpu_has_vmx_mpx \ + ((vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) && \ + (vmx_vmentry_control & VM_ENTRY_LOAD_BNDCFGS)) #define cpu_has_vmx_xsaves \ (vmx_secondary_exec_control & SECONDARY_EXEC_XSAVES) #define cpu_has_vmx_tsc_scaling \ --- a/xen/include/asm-x86/msr-index.h +++ b/xen/include/asm-x86/msr-index.h @@ -56,7 +56,10 @@ #define MSR_IA32_DS_AREA 0x00000600 #define MSR_IA32_PERF_CAPABILITIES 0x00000345 =20 -#define MSR_IA32_BNDCFGS 0x00000D90 +#define MSR_IA32_BNDCFGS 0x00000d90 +#define IA32_BNDCFGS_ENABLE 0x00000001 +#define IA32_BNDCFGS_PRESERVE 0x00000002 +#define IA32_BNDCFGS_RESERVED 0x00000ffc =20 #define MSR_IA32_XSS 0x00000da0 =20 --=__Part122481B6.2__= Content-Type: text/plain; name="VMX-feature-checks.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="VMX-feature-checks.patch" VMX: correct feature checks for MPX and XSAVES=0A=0ATheir VMCS fields = aren't tied to the respective base CPU feature flags=0Abut instead to VMX = specific ones.=0A=0ANote that while the VMCS GUEST_BNDCFGS field exists if = either of the=0Atwo respective features is available, MPX continues to get = exposed to=0Aguests only with both features present.=0A=0AAlso add the so = far missing handling of=0A- GUEST_BNDCFGS in construct_vmcs()=0A- = MSR_IA32_BNDCFGS in vmx_msr_{read,write}_intercept()=0Aand mirror the = extra correctness checks during MSR write to=0Avmx_load_msr().=0A=0AReporte= d-by: "Rockosov, Dmitry" =0ASigned-off-by: Jan = Beulich =0ATested-by: "Rockosov, Dmitry" =0A=0A--- a/xen/arch/x86/cpuid.c=0A+++ b/xen/arch/x86/cpuid.c= =0A@@ -168,8 +168,7 @@ static void __init calculate_hvm_feature=0A = */=0A if ( cpu_has_vmx )=0A {=0A- if ( !(vmx_vmexit_control = & VM_EXIT_CLEAR_BNDCFGS) ||=0A- !(vmx_vmentry_control & = VM_ENTRY_LOAD_BNDCFGS) )=0A+ if ( !cpu_has_vmx_mpx )=0A = __clear_bit(X86_FEATURE_MPX, hvm_featureset);=0A =0A if ( = !cpu_has_vmx_xsaves )=0A--- a/xen/arch/x86/hvm/vmx/vmcs.c=0A+++ b/xen/arch/= x86/hvm/vmx/vmcs.c=0A@@ -1261,6 +1261,8 @@ static int construct_vmcs(struct= vcpu *v=0A __vmwrite(HOST_PAT, host_pat);=0A __vmwrite(GUE= ST_PAT, guest_pat);=0A }=0A+ if ( cpu_has_vmx_mpx )=0A+ = __vmwrite(GUEST_BNDCFGS, 0);=0A if ( cpu_has_vmx_xsaves )=0A = __vmwrite(XSS_EXIT_BITMAP, 0);=0A =0A--- a/xen/arch/x86/hvm/vmx/vmx.c=0A+++= b/xen/arch/x86/hvm/vmx/vmx.c=0A@@ -788,14 +788,15 @@ static int vmx_load_v= mcs_ctxt(struct vcp=0A =0A static unsigned int __init vmx_init_msr(void)=0A= {=0A- return !!cpu_has_mpx + !!cpu_has_xsaves;=0A+ return (cpu_has_m= px && cpu_has_vmx_mpx) +=0A+ (cpu_has_xsaves && cpu_has_vmx_xsave= s);=0A }=0A =0A static void vmx_save_msr(struct vcpu *v, struct hvm_msr = *ctxt)=0A {=0A vmx_vmcs_enter(v);=0A =0A- if ( cpu_has_mpx )=0A+ = if ( cpu_has_mpx && cpu_has_vmx_mpx )=0A {=0A __vmread(GUEST_BN= DCFGS, &ctxt->msr[ctxt->count].val);=0A if ( ctxt->msr[ctxt->count]= .val )=0A@@ -804,7 +805,7 @@ static void vmx_save_msr(struct vcpu *v,=0A = =0A vmx_vmcs_exit(v);=0A =0A- if ( cpu_has_xsaves )=0A+ if ( = cpu_has_xsaves && cpu_has_vmx_xsaves )=0A {=0A ctxt->msr[ctxt->= count].val =3D v->arch.hvm_vcpu.msr_xss;=0A if ( ctxt->msr[ctxt->co= unt].val )=0A@@ -824,13 +825,15 @@ static int vmx_load_msr(struct vcpu = *v,=0A switch ( ctxt->msr[i].index )=0A {=0A case = MSR_IA32_BNDCFGS:=0A- if ( cpu_has_mpx )=0A+ if ( = cpu_has_mpx && cpu_has_vmx_mpx &&=0A+ is_canonical_address(= ctxt->msr[i].val) &&=0A+ !(ctxt->msr[i].val & IA32_BNDCFGS_= RESERVED) )=0A __vmwrite(GUEST_BNDCFGS, ctxt->msr[i].val);= =0A else if ( ctxt->msr[i].val )=0A err =3D = -ENXIO;=0A break;=0A case MSR_IA32_XSS:=0A- = if ( cpu_has_xsaves )=0A+ if ( cpu_has_xsaves && cpu_has_vmx_xsa= ves )=0A v->arch.hvm_vcpu.msr_xss =3D ctxt->msr[i].val;=0A = else=0A err =3D -ENXIO;=0A@@ -2643,6 +2646,11 = @@ static int vmx_msr_read_intercept(unsign=0A case MSR_IA32_DEBUGCTLMS= R:=0A __vmread(GUEST_IA32_DEBUGCTL, msr_content);=0A = break;=0A+ case MSR_IA32_BNDCFGS:=0A+ if ( !cpu_has_mpx || = !cpu_has_vmx_mpx )=0A+ goto gp_fault;=0A+ __vmread(GUEST_= BNDCFGS, msr_content);=0A+ break;=0A case MSR_IA32_FEATURE_CONTR= OL:=0A case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_VMFUNC:=0A if ( = !nvmx_msr_read_intercept(msr, msr_content) )=0A@@ -2869,6 +2877,13 @@ = static int vmx_msr_write_intercept(unsig=0A =0A break;=0A = }=0A+ case MSR_IA32_BNDCFGS:=0A+ if ( !cpu_has_mpx || !cpu_has_vm= x_mpx ||=0A+ !is_canonical_address(msr_content) ||=0A+ = (msr_content & IA32_BNDCFGS_RESERVED) )=0A+ goto = gp_fault;=0A+ __vmwrite(GUEST_BNDCFGS, msr_content);=0A+ = break;=0A case MSR_IA32_FEATURE_CONTROL:=0A case MSR_IA32_VMX_BASIC= ...MSR_IA32_VMX_TRUE_ENTRY_CTLS:=0A if ( !nvmx_msr_write_intercept(= msr, msr_content) )=0A--- a/xen/include/asm-x86/hvm/vmx/vmcs.h=0A+++ = b/xen/include/asm-x86/hvm/vmx/vmcs.h=0A@@ -375,6 +375,9 @@ extern u64 = vmx_ept_vpid_cap;=0A (vmx_secondary_exec_control & SECONDARY_EXEC_ENABL= E_VIRT_EXCEPTIONS)=0A #define cpu_has_vmx_pml \=0A (vmx_secondary_exec_= control & SECONDARY_EXEC_ENABLE_PML)=0A+#define cpu_has_vmx_mpx \=0A+ = ((vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) && \=0A+ (vmx_vmentry_con= trol & VM_ENTRY_LOAD_BNDCFGS))=0A #define cpu_has_vmx_xsaves \=0A = (vmx_secondary_exec_control & SECONDARY_EXEC_XSAVES)=0A #define cpu_has_vmx= _tsc_scaling \=0A--- a/xen/include/asm-x86/msr-index.h=0A+++ b/xen/include/= asm-x86/msr-index.h=0A@@ -56,7 +56,10 @@=0A #define MSR_IA32_DS_AREA = 0x00000600=0A #define MSR_IA32_PERF_CAPABILITIES 0x00000345=0A = =0A-#define MSR_IA32_BNDCFGS 0x00000D90=0A+#define MSR_IA32_BNDC= FGS 0x00000d90=0A+#define IA32_BNDCFGS_ENABLE = 0x00000001=0A+#define IA32_BNDCFGS_PRESERVE 0x00000002=0A+#defi= ne IA32_BNDCFGS_RESERVED 0x00000ffc=0A =0A #define = MSR_IA32_XSS 0x00000da0=0A =0A --=__Part122481B6.2__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__Part122481B6.2__=--