From: "Jan Beulich" Subject: [PATCH 1/3] x86: don't expose XSAVES capability to PV guests Date: Mon, 19 Jan 2015 15:27:00 +0000 Message-ID: <54BD3054020000780005686D@mail.emea.novell.com> References: <54BD2F2B0200007800056859@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF7C21354.1__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YDEEK-0004nY-Cq for xen-devel@lists.xenproject.org; Mon, 19 Jan 2015 15:27:07 +0000 In-Reply-To: <54BD2F2B0200007800056859@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: Andrew Cooper , Keir Fraser 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. --=__PartF7C21354.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline As done by the recent Linux commit b65d6e17fe ("kvm: x86: mask out XSAVES") for KVM, we should also mask out XSAVES from what PV guests get to see as long as we don't emulate accesses to MSR_IA32_XSS. Actually, go beyond that: Just like for leaf 7, switch from blacklisting to whitelisting, i.e. only allow XSAVEOPT and XSAVEC for the time being. And do these overrides consistently for both Dom0 and DomU-s. Signed-off-by: Jan Beulich --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -837,7 +837,7 @@ void pv_cpuid(struct cpu_user_regs *regs =20 switch ( cpuid_leaf ) { - case 0xd: + case XSTATE_CPUID: { unsigned int _eax, _ebx, _ecx, _edx; /* EBX value of main leaf 0 depends on enabled xsave features = */ @@ -855,7 +855,7 @@ void pv_cpuid(struct cpu_user_regs *regs b =3D _eax + _ebx; } } - break; + goto xstate; } } goto out; @@ -931,9 +931,19 @@ void pv_cpuid(struct cpu_user_regs *regs a =3D c =3D d =3D 0; break; =20 - case 0x0000000d: /* XSAVE */ + case XSTATE_CPUID: + xstate: if ( !cpu_has_xsave ) goto unsupported; + if ( regs->_ecx =3D=3D 1 ) + { + a &=3D XSTATE_FEATURE_XSAVEOPT | + XSTATE_FEATURE_XSAVEC | + (cpu_has_xgetbv1 ? XSTATE_FEATURE_XGETBV1 : 0) | + (cpu_has_xsaves ? XSTATE_FEATURE_XSAVES : 0); + if ( !cpu_has_xsaves ) + b =3D c =3D d =3D 0; + } break; =20 case 0x80000001: --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -14,7 +14,10 @@ #include #include =20 -bool_t __read_mostly cpu_has_xsaveopt; +static bool_t __read_mostly cpu_has_xsaveopt; +static bool_t __read_mostly cpu_has_xsavec; +bool_t __read_mostly cpu_has_xgetbv1; +bool_t __read_mostly cpu_has_xsaves; =20 /* * Maximum size (in byte) of the XSAVE/XRSTOR save area required by all @@ -320,12 +323,22 @@ void xstate_init(bool_t bsp) BUG_ON(xsave_cntxt_size !=3D _xstate_ctxt_size(feature_mask)); } =20 - /* Check XSAVEOPT feature. */ + /* Check extended XSAVE features. */ cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx); if ( bsp ) + { cpu_has_xsaveopt =3D !!(eax & XSTATE_FEATURE_XSAVEOPT); + cpu_has_xsavec =3D !!(eax & XSTATE_FEATURE_XSAVEC); + /* XXX cpu_has_xgetbv1 =3D !!(eax & XSTATE_FEATURE_XGETBV1); */ + /* XXX cpu_has_xsaves =3D !!(eax & XSTATE_FEATURE_XSAVES); */ + } else + { BUG_ON(!cpu_has_xsaveopt !=3D !(eax & XSTATE_FEATURE_XSAVEOPT)); + BUG_ON(!cpu_has_xsavec !=3D !(eax & XSTATE_FEATURE_XSAVEC)); + /* XXX BUG_ON(!cpu_has_xgetbv1 !=3D !(eax & XSTATE_FEATURE_XGETBV1= )); */ + /* XXX BUG_ON(!cpu_has_xsaves !=3D !(eax & XSTATE_FEATURE_XSAVES))= ; */ + } } =20 static bool_t valid_xcr0(u64 xcr0) --- a/xen/include/asm-x86/xstate.h +++ b/xen/include/asm-x86/xstate.h @@ -16,6 +16,9 @@ =20 #define XSTATE_CPUID 0x0000000d #define XSTATE_FEATURE_XSAVEOPT (1 << 0) /* sub-leaf 1, eax[bit 0] = */ +#define XSTATE_FEATURE_XSAVEC (1 << 1) /* sub-leaf 1, eax[bit 1] = */ +#define XSTATE_FEATURE_XGETBV1 (1 << 2) /* sub-leaf 1, eax[bit 2] = */ +#define XSTATE_FEATURE_XSAVES (1 << 3) /* sub-leaf 1, eax[bit 3] = */ =20 #define XCR_XFEATURE_ENABLED_MASK 0x00000000 /* index of XCR0 */ =20 @@ -40,6 +43,7 @@ #define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY) =20 extern u64 xfeature_mask; +extern bool_t cpu_has_xsaves, cpu_has_xgetbv1; =20 /* extended state save area */ struct __packed __attribute__((aligned (64))) xsave_struct --=__PartF7C21354.1__= Content-Type: text/plain; name="x86-pv-suppress-xsaves.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-pv-suppress-xsaves.patch" x86: don't expose XSAVES capability to PV guests=0A=0AAs done by the = recent Linux commit b65d6e17fe ("kvm: x86: mask out=0AXSAVES") for KVM, we = should also mask out XSAVES from what PV guests=0Aget to see as long as we = don't emulate accesses to MSR_IA32_XSS.=0A=0AActually, go beyond that: = Just like for leaf 7, switch from=0Ablacklisting to whitelisting, i.e. = only allow XSAVEOPT and XSAVEC for=0Athe time being. And do these = overrides consistently for both Dom0 and=0ADomU-s.=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/arch/x86/traps.c=0A+++ = b/xen/arch/x86/traps.c=0A@@ -837,7 +837,7 @@ void pv_cpuid(struct = cpu_user_regs *regs=0A =0A switch ( cpuid_leaf )=0A {=0A- = case 0xd:=0A+ case XSTATE_CPUID:=0A {=0A = unsigned int _eax, _ebx, _ecx, _edx;=0A /* EBX value of main = leaf 0 depends on enabled xsave features */=0A@@ -855,7 +855,7 @@ void = pv_cpuid(struct cpu_user_regs *regs=0A b =3D _eax = + _ebx;=0A }=0A }=0A- break;=0A+ = goto xstate;=0A }=0A }=0A goto out;=0A@@ = -931,9 +931,19 @@ void pv_cpuid(struct cpu_user_regs *regs=0A a = =3D c =3D d =3D 0;=0A break;=0A =0A- case 0x0000000d: /* XSAVE = */=0A+ case XSTATE_CPUID:=0A+ xstate:=0A if ( !cpu_has_xsave = )=0A goto unsupported;=0A+ if ( regs->_ecx =3D=3D 1 = )=0A+ {=0A+ a &=3D XSTATE_FEATURE_XSAVEOPT |=0A+ = XSTATE_FEATURE_XSAVEC |=0A+ (cpu_has_xgetbv1 ? = XSTATE_FEATURE_XGETBV1 : 0) |=0A+ (cpu_has_xsaves ? = XSTATE_FEATURE_XSAVES : 0);=0A+ if ( !cpu_has_xsaves )=0A+ = b =3D c =3D d =3D 0;=0A+ }=0A break;=0A =0A = case 0x80000001:=0A--- a/xen/arch/x86/xstate.c=0A+++ b/xen/arch/x86/xstate.= c=0A@@ -14,7 +14,10 @@=0A #include =0A #include =0A =0A-bool_t __read_mostly cpu_has_xsaveopt;=0A+static bool_t = __read_mostly cpu_has_xsaveopt;=0A+static bool_t __read_mostly cpu_has_xsav= ec;=0A+bool_t __read_mostly cpu_has_xgetbv1;=0A+bool_t __read_mostly = cpu_has_xsaves;=0A =0A /*=0A * Maximum size (in byte) of the XSAVE/XRSTOR = save area required by all=0A@@ -320,12 +323,22 @@ void xstate_init(bool_t = bsp)=0A BUG_ON(xsave_cntxt_size !=3D _xstate_ctxt_size(feature_mask= ));=0A }=0A =0A- /* Check XSAVEOPT feature. */=0A+ /* Check = extended XSAVE features. */=0A cpuid_count(XSTATE_CPUID, 1, &eax, = &ebx, &ecx, &edx);=0A if ( bsp )=0A+ {=0A cpu_has_xsaveopt = =3D !!(eax & XSTATE_FEATURE_XSAVEOPT);=0A+ cpu_has_xsavec =3D = !!(eax & XSTATE_FEATURE_XSAVEC);=0A+ /* XXX cpu_has_xgetbv1 =3D = !!(eax & XSTATE_FEATURE_XGETBV1); */=0A+ /* XXX cpu_has_xsaves =3D = !!(eax & XSTATE_FEATURE_XSAVES); */=0A+ }=0A else=0A+ {=0A = BUG_ON(!cpu_has_xsaveopt !=3D !(eax & XSTATE_FEATURE_XSAVEOPT));=0A+ = BUG_ON(!cpu_has_xsavec !=3D !(eax & XSTATE_FEATURE_XSAVEC));=0A+ = /* XXX BUG_ON(!cpu_has_xgetbv1 !=3D !(eax & XSTATE_FEATURE_XGETBV1)); = */=0A+ /* XXX BUG_ON(!cpu_has_xsaves !=3D !(eax & XSTATE_FEATURE_XSA= VES)); */=0A+ }=0A }=0A =0A static bool_t valid_xcr0(u64 xcr0)=0A--- = a/xen/include/asm-x86/xstate.h=0A+++ b/xen/include/asm-x86/xstate.h=0A@@ = -16,6 +16,9 @@=0A =0A #define XSTATE_CPUID 0x0000000d=0A = #define XSTATE_FEATURE_XSAVEOPT (1 << 0) /* sub-leaf 1, eax[bit 0] = */=0A+#define XSTATE_FEATURE_XSAVEC (1 << 1) /* sub-leaf 1, eax[bit = 1] */=0A+#define XSTATE_FEATURE_XGETBV1 (1 << 2) /* sub-leaf 1, = eax[bit 2] */=0A+#define XSTATE_FEATURE_XSAVES (1 << 3) /* sub-leaf = 1, eax[bit 3] */=0A =0A #define XCR_XFEATURE_ENABLED_MASK 0x00000000 /* = index of XCR0 */=0A =0A@@ -40,6 +43,7 @@=0A #define XSTATE_LAZY = (XSTATE_ALL & ~XSTATE_NONLAZY)=0A =0A extern u64 xfeature_mask;=0A+extern = bool_t cpu_has_xsaves, cpu_has_xgetbv1;=0A =0A /* extended state save area = */=0A struct __packed __attribute__((aligned (64))) xsave_struct=0A --=__PartF7C21354.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 --=__PartF7C21354.1__=--