From: "Jan Beulich" Subject: [PATCH v2] x86: correct CPUID output for out of bounds input Date: Mon, 05 Sep 2016 09:26:46 -0600 Message-ID: <57CDAAD6020000780010BD77@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartDAEC4DA6.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bgvnQ-0006Po-Uo for xen-devel@lists.xenproject.org; Mon, 05 Sep 2016 15:26:53 +0000 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 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. --=__PartDAEC4DA6.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Another place where we should try to behave sufficiently close to how real hardware does; see the code comments. Signed-off-by: Jan Beulich --- v2: Uniformly return zero for out of range leaves. Only consider basic and extended groups as valid. Avoid recursion in hvm_cpuid(). --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3364,6 +3364,27 @@ void hvm_cpuid(unsigned int input, unsig if ( cpuid_hypervisor_leaves(input, count, eax, ebx, ecx, edx) ) return; =20 + if ( input & 0x7fffffff ) + { + /* + * Requests outside the supported leaf ranges return zero on AMD + * and the highest basic leaf output on Intel. Uniformly follow + * the AMD model as the more sane one. + */ + unsigned int limit; + + domain_cpuid(d, (input >> 16) !=3D 0x8000 ? 0 : 0x80000000, 0, + &limit, &dummy, &dummy, &dummy); + if ( input > limit ) + { + *eax =3D 0; + *ebx =3D 0; + *ecx =3D 0; + *edx =3D 0; + return; + } + } + domain_cpuid(d, input, count, eax, ebx, ecx, edx); =20 switch ( input ) --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -952,6 +952,29 @@ void pv_cpuid(struct cpu_user_regs *regs if ( cpuid_hypervisor_leaves(leaf, subleaf, &a, &b, &c, &d) ) goto out; =20 + if ( leaf & 0x7fffffff ) + { + /* + * Requests outside the supported leaf ranges return zero on AMD + * and the highest basic leaf output on Intel. Uniformly follow + * the AMD model as the more sane one. + */ + unsigned int limit =3D (leaf >> 16) !=3D 0x8000 ? 0 : 0x80000000, = dummy; + + if ( !is_control_domain(currd) && !is_hardware_domain(currd) ) + domain_cpuid(currd, limit, 0, &limit, &dummy, &dummy, = &dummy); + else + limit =3D cpuid_eax(limit); + if ( leaf > limit ) + { + regs->eax =3D 0; + regs->ebx =3D 0; + regs->ecx =3D 0; + regs->edx =3D 0; + return; + } + } + if ( !is_control_domain(currd) && !is_hardware_domain(currd) ) domain_cpuid(currd, leaf, subleaf, &a, &b, &c, &d); else --=__PartDAEC4DA6.1__= Content-Type: text/plain; name="x86-CPUID-level-check.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-CPUID-level-check.patch" x86: correct CPUID output for out of bounds input=0A=0AAnother place where = we should try to behave sufficiently close to how=0Areal hardware does; = see the code comments.=0A=0ASigned-off-by: Jan Beulich = =0A---=0Av2: Uniformly return zero for out of range leaves. Only consider = basic=0A and extended groups as valid. Avoid recursion in hvm_cpuid().= =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ = -3364,6 +3364,27 @@ void hvm_cpuid(unsigned int input, unsig=0A if ( = cpuid_hypervisor_leaves(input, count, eax, ebx, ecx, edx) )=0A = return;=0A =0A+ if ( input & 0x7fffffff )=0A+ {=0A+ /*=0A+ = * Requests outside the supported leaf ranges return zero on AMD=0A+ = * and the highest basic leaf output on Intel. Uniformly follow=0A+ = * the AMD model as the more sane one.=0A+ */=0A+ = unsigned int limit;=0A+=0A+ domain_cpuid(d, (input >> 16) !=3D = 0x8000 ? 0 : 0x80000000, 0,=0A+ &limit, &dummy, = &dummy, &dummy);=0A+ if ( input > limit )=0A+ {=0A+ = *eax =3D 0;=0A+ *ebx =3D 0;=0A+ *ecx =3D 0;=0A+ = *edx =3D 0;=0A+ return;=0A+ }=0A+ }=0A+=0A = domain_cpuid(d, input, count, eax, ebx, ecx, edx);=0A =0A switch ( = input )=0A--- a/xen/arch/x86/traps.c=0A+++ b/xen/arch/x86/traps.c=0A@@ = -952,6 +952,29 @@ void pv_cpuid(struct cpu_user_regs *regs=0A if ( = cpuid_hypervisor_leaves(leaf, subleaf, &a, &b, &c, &d) )=0A goto = out;=0A =0A+ if ( leaf & 0x7fffffff )=0A+ {=0A+ /*=0A+ = * Requests outside the supported leaf ranges return zero on AMD=0A+ = * and the highest basic leaf output on Intel. Uniformly follow=0A+ = * the AMD model as the more sane one.=0A+ */=0A+ unsigned = int limit =3D (leaf >> 16) !=3D 0x8000 ? 0 : 0x80000000, dummy;=0A+=0A+ = if ( !is_control_domain(currd) && !is_hardware_domain(currd) )=0A+ = domain_cpuid(currd, limit, 0, &limit, &dummy, &dummy, &dummy);=0A+ = else=0A+ limit =3D cpuid_eax(limit);=0A+ if ( leaf = > limit )=0A+ {=0A+ regs->eax =3D 0;=0A+ = regs->ebx =3D 0;=0A+ regs->ecx =3D 0;=0A+ regs->edx = =3D 0;=0A+ return;=0A+ }=0A+ }=0A+=0A if ( = !is_control_domain(currd) && !is_hardware_domain(currd) )=0A = domain_cpuid(currd, leaf, subleaf, &a, &b, &c, &d);=0A else=0A --=__PartDAEC4DA6.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartDAEC4DA6.1__=--