From: "Jan Beulich" Subject: [PATCH 2/4] VMX/vPMU: fix DebugCtl MSR handling Date: Tue, 12 Aug 2014 10:15:10 +0100 Message-ID: <53E9F73E020000780002B726@mail.emea.novell.com> References: <53E9F5A0020000780002B70C@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part0735B00E.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 1XH8Ak-0008AL-G3 for xen-devel@lists.xenproject.org; Tue, 12 Aug 2014 09:15:14 +0000 In-Reply-To: <53E9F5A0020000780002B70C@mail.emea.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 , Eddie Dong , 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. --=__Part0735B00E.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - writes with one of the vPMU-supported flags and some unsupported one set got accepted, leading to a VM entry failure - writes with one of the vPMU-supported flags set but the Debug Store feature unavailable produced a #GP (other than other writes to this MSR with unsupported bits set) Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -1789,7 +1789,7 @@ static int svm_msr_write_intercept(unsig case MSR_AMD_FAM15H_EVNTSEL3: case MSR_AMD_FAM15H_EVNTSEL4: case MSR_AMD_FAM15H_EVNTSEL5: - vpmu_do_wrmsr(msr, msr_content); + vpmu_do_wrmsr(msr, msr_content, 0); break; =20 case MSR_IA32_MCx_MISC(4): /* Threshold register */ --- a/xen/arch/x86/hvm/svm/vpmu.c +++ b/xen/arch/x86/hvm/svm/vpmu.c @@ -278,11 +278,14 @@ static void context_update(unsigned int=20 } } =20 -static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) +static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, + uint64_t supported) { struct vcpu *v =3D current; struct vpmu_struct *vpmu =3D vcpu_vpmu(v); =20 + ASSERT(!supported); + /* For all counters, enable guest only mode for HVM guest */ if ( (get_pmu_reg_type(msr) =3D=3D MSR_TYPE_CTRL) && !(is_guest_mode(msr_content)) ) --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2249,7 +2249,7 @@ static int vmx_msr_write_intercept(unsig if ( msr_content & ~supported ) { /* Perhaps some other bits are supported in vpmu. */ - if ( !vpmu_do_wrmsr(msr, msr_content) ) + if ( !vpmu_do_wrmsr(msr, msr_content, supported) ) break; } if ( msr_content & IA32_DEBUGCTLMSR_LBR ) @@ -2278,7 +2278,7 @@ static int vmx_msr_write_intercept(unsig goto gp_fault; break; default: - if ( vpmu_do_wrmsr(msr, msr_content) ) + if ( vpmu_do_wrmsr(msr, msr_content, 0) ) return X86EMUL_OKAY; if ( passive_domain_do_wrmsr(msr, msr_content) ) return X86EMUL_OKAY; --- a/xen/arch/x86/hvm/vmx/vpmu_core2.c +++ b/xen/arch/x86/hvm/vmx/vpmu_core2.c @@ -454,7 +454,8 @@ static int core2_vpmu_msr_common_check(u return 1; } =20 -static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) +static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, + uint64_t supported) { u64 global_ctrl, non_global_ctrl; char pmu_enable =3D 0; @@ -469,24 +470,26 @@ static int core2_vpmu_do_wrmsr(unsigned=20 /* Special handling for BTS */ if ( msr =3D=3D MSR_IA32_DEBUGCTLMSR ) { - uint64_t supported =3D IA32_DEBUGCTLMSR_TR | IA32_DEBUGCTLMSR_= BTS | - IA32_DEBUGCTLMSR_BTINT; + supported |=3D IA32_DEBUGCTLMSR_TR | IA32_DEBUGCTLMSR_BTS | + IA32_DEBUGCTLMSR_BTINT; =20 if ( cpu_has(¤t_cpu_data, X86_FEATURE_DSCPL) ) supported |=3D IA32_DEBUGCTLMSR_BTS_OFF_OS | IA32_DEBUGCTLMSR_BTS_OFF_USR; - if ( msr_content & supported ) - { - if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) ) - return 1; - gdprintk(XENLOG_WARNING, "Debug Store is not supported on = this cpu\n"); - hvm_inject_hw_exception(TRAP_gp_fault, 0); - return 0; - } + if ( !(msr_content & ~supported) && + vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) ) + return 1; + if ( (msr_content & supported) && + !vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) ) + printk(XENLOG_G_WARNING + "%pv: Debug Store unsupported on this CPU\n", + current); } return 0; } =20 + ASSERT(!supported); + core2_vpmu_cxt =3D vpmu->context; switch ( msr ) { --- a/xen/arch/x86/hvm/vpmu.c +++ b/xen/arch/x86/hvm/vpmu.c @@ -64,12 +64,12 @@ static void __init parse_vpmu_param(char } } =20 -int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) +int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t = supported) { struct vpmu_struct *vpmu =3D vcpu_vpmu(current); =20 if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr ) - return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content); + return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content, supported);= return 0; } =20 --- a/xen/include/asm-x86/hvm/vpmu.h +++ b/xen/include/asm-x86/hvm/vpmu.h @@ -45,7 +45,8 @@ =20 /* Arch specific operations shared by all vpmus */ struct arch_vpmu_ops { - int (*do_wrmsr)(unsigned int msr, uint64_t msr_content); + int (*do_wrmsr)(unsigned int msr, uint64_t msr_content, + uint64_t supported); int (*do_rdmsr)(unsigned int msr, uint64_t *msr_content); int (*do_interrupt)(struct cpu_user_regs *regs); void (*do_cpuid)(unsigned int input, @@ -86,7 +87,7 @@ struct vpmu_struct { #define vpmu_is_set(_vpmu, _x) ((_vpmu)->flags & (_x)) #define vpmu_clear(_vpmu) ((_vpmu)->flags =3D 0) =20 -int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content); +int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t = supported); int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content); int vpmu_do_interrupt(struct cpu_user_regs *regs); void vpmu_do_cpuid(unsigned int input, unsigned int *eax, unsigned int = *ebx, --=__Part0735B00E.1__= Content-Type: text/plain; name="VMX-vPMU-debugctl.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="VMX-vPMU-debugctl.patch" VMX/vPMU: fix DebugCtl MSR handling=0A=0A- writes with one of the = vPMU-supported flags and some unsupported one=0A set got accepted, = leading to a VM entry failure=0A- writes with one of the vPMU-supported = flags set but the Debug Store=0A feature unavailable produced a #GP = (other than other writes to this=0A MSR with unsupported bits set)=0A=0ASi= gned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/hvm/sv= m/svm.c=0A+++ b/xen/arch/x86/hvm/svm/svm.c=0A@@ -1789,7 +1789,7 @@ static = int svm_msr_write_intercept(unsig=0A case MSR_AMD_FAM15H_EVNTSEL3:=0A = case MSR_AMD_FAM15H_EVNTSEL4:=0A case MSR_AMD_FAM15H_EVNTSEL5:=0A- = vpmu_do_wrmsr(msr, msr_content);=0A+ vpmu_do_wrmsr(msr, = msr_content, 0);=0A break;=0A =0A case MSR_IA32_MCx_MISC(4): = /* Threshold register */=0A--- a/xen/arch/x86/hvm/svm/vpmu.c=0A+++ = b/xen/arch/x86/hvm/svm/vpmu.c=0A@@ -278,11 +278,14 @@ static void = context_update(unsigned int =0A }=0A }=0A =0A-static int amd_vpmu_do_wr= msr(unsigned int msr, uint64_t msr_content)=0A+static int amd_vpmu_do_wrmsr= (unsigned int msr, uint64_t msr_content,=0A+ = uint64_t supported)=0A {=0A struct vcpu *v =3D current;=0A struct = vpmu_struct *vpmu =3D vcpu_vpmu(v);=0A =0A+ ASSERT(!supported);=0A+=0A = /* For all counters, enable guest only mode for HVM guest */=0A if = ( (get_pmu_reg_type(msr) =3D=3D MSR_TYPE_CTRL) &&=0A !(is_guest_mod= e(msr_content)) )=0A--- a/xen/arch/x86/hvm/vmx/vmx.c=0A+++ b/xen/arch/x86/h= vm/vmx/vmx.c=0A@@ -2249,7 +2249,7 @@ static int vmx_msr_write_intercept(uns= ig=0A if ( msr_content & ~supported )=0A {=0A = /* Perhaps some other bits are supported in vpmu. */=0A- if ( = !vpmu_do_wrmsr(msr, msr_content) )=0A+ if ( !vpmu_do_wrmsr(msr, = msr_content, supported) )=0A break;=0A }=0A = if ( msr_content & IA32_DEBUGCTLMSR_LBR )=0A@@ -2278,7 +2278,7 @@ static = int vmx_msr_write_intercept(unsig=0A goto gp_fault;=0A = break;=0A default:=0A- if ( vpmu_do_wrmsr(msr, msr_content) = )=0A+ if ( vpmu_do_wrmsr(msr, msr_content, 0) )=0A = return X86EMUL_OKAY;=0A if ( passive_domain_do_wrmsr(msr, = msr_content) )=0A return X86EMUL_OKAY;=0A--- a/xen/arch/x86/hvm= /vmx/vpmu_core2.c=0A+++ b/xen/arch/x86/hvm/vmx/vpmu_core2.c=0A@@ -454,7 = +454,8 @@ static int core2_vpmu_msr_common_check(u=0A return 1;=0A = }=0A =0A-static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t = msr_content)=0A+static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t = msr_content,=0A+ uint64_t supported)=0A {=0A = u64 global_ctrl, non_global_ctrl;=0A char pmu_enable =3D 0;=0A@@ = -469,24 +470,26 @@ static int core2_vpmu_do_wrmsr(unsigned =0A /* = Special handling for BTS */=0A if ( msr =3D=3D MSR_IA32_DEBUGCTLMSR= )=0A {=0A- uint64_t supported =3D IA32_DEBUGCTLMSR_TR = | IA32_DEBUGCTLMSR_BTS |=0A- IA32_DEBUGCTLM= SR_BTINT;=0A+ supported |=3D IA32_DEBUGCTLMSR_TR | IA32_DEBUGCTL= MSR_BTS |=0A+ IA32_DEBUGCTLMSR_BTINT;=0A =0A = if ( cpu_has(¤t_cpu_data, X86_FEATURE_DSCPL) )=0A = supported |=3D IA32_DEBUGCTLMSR_BTS_OFF_OS |=0A = IA32_DEBUGCTLMSR_BTS_OFF_USR;=0A- if ( msr_content & = supported )=0A- {=0A- if ( vpmu_is_set(vpmu, = VPMU_CPU_HAS_BTS) )=0A- return 1;=0A- = gdprintk(XENLOG_WARNING, "Debug Store is not supported on this cpu\n");=0A-= hvm_inject_hw_exception(TRAP_gp_fault, 0);=0A- = return 0;=0A- }=0A+ if ( !(msr_content & = ~supported) &&=0A+ vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) = )=0A+ return 1;=0A+ if ( (msr_content & = supported) &&=0A+ !vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) = )=0A+ printk(XENLOG_G_WARNING=0A+ = "%pv: Debug Store unsupported on this CPU\n",=0A+ = current);=0A }=0A return 0;=0A }=0A =0A+ ASSERT(!sup= ported);=0A+=0A core2_vpmu_cxt =3D vpmu->context;=0A switch ( msr = )=0A {=0A--- a/xen/arch/x86/hvm/vpmu.c=0A+++ b/xen/arch/x86/hvm/vpmu.c= =0A@@ -64,12 +64,12 @@ static void __init parse_vpmu_param(char=0A = }=0A }=0A =0A-int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)=0A+= int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t = supported)=0A {=0A struct vpmu_struct *vpmu =3D vcpu_vpmu(current);=0A = =0A if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )=0A- = return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);=0A+ = return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content, supported);=0A = return 0;=0A }=0A =0A--- a/xen/include/asm-x86/hvm/vpmu.h=0A+++ b/xen/inclu= de/asm-x86/hvm/vpmu.h=0A@@ -45,7 +45,8 @@=0A =0A /* Arch specific = operations shared by all vpmus */=0A struct arch_vpmu_ops {=0A- int = (*do_wrmsr)(unsigned int msr, uint64_t msr_content);=0A+ int (*do_wrmsr)= (unsigned int msr, uint64_t msr_content,=0A+ uint64_t = supported);=0A int (*do_rdmsr)(unsigned int msr, uint64_t *msr_content)= ;=0A int (*do_interrupt)(struct cpu_user_regs *regs);=0A void = (*do_cpuid)(unsigned int input,=0A@@ -86,7 +87,7 @@ struct vpmu_struct = {=0A #define vpmu_is_set(_vpmu, _x) ((_vpmu)->flags & (_x))=0A #define = vpmu_clear(_vpmu) ((_vpmu)->flags =3D 0)=0A =0A-int vpmu_do_wrmsr(unsi= gned int msr, uint64_t msr_content);=0A+int vpmu_do_wrmsr(unsigned int = msr, uint64_t msr_content, uint64_t supported);=0A int vpmu_do_rdmsr(unsign= ed int msr, uint64_t *msr_content);=0A int vpmu_do_interrupt(struct = cpu_user_regs *regs);=0A void vpmu_do_cpuid(unsigned int input, unsigned = int *eax, unsigned int *ebx,=0A --=__Part0735B00E.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 --=__Part0735B00E.1__=--