Received: from eggs.gnu.org ([208.118.235.92]:59376) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TlxQW-0008EC-Pd for qemu-devel@nongnu.org; Fri, 21 Dec 2012 02:53:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TlxQT-0002wI-Gp for qemu-devel@nongnu.org; Fri, 21 Dec 2012 02:53:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53131) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TlxQT-0002wA-9G for qemu-devel@nongnu.org; Fri, 21 Dec 2012 02:53:49 -0500 From: Markus Armbruster References: <1356029028-7356-1-git-send-email-ehabkost@redhat.com> Date: Fri, 21 Dec 2012 08:53:44 +0100 In-Reply-To: <1356029028-7356-1-git-send-email-ehabkost@redhat.com> (Eduardo Habkost's message of "Thu, 20 Dec 2012 16:43:48 -0200") Message-ID: <87r4mj96yf.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH] target-i386: CPUID: return highest basic leaf if eax > cpuid_xlevel List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eduardo Habkost Cc: Marcelo Tosatti , qemu-devel@nongnu.org, "brillywu@viatech.com.cn" Eduardo Habkost writes: > This fixes a subtle bug. A bug that probably won't cause trouble for any > existing OS, but a bug anyway: > > Intel SDM Volume 2, CPUID Instruction states: > >> Two types of information are returned: basic and extended function >> information. If a value entered for CPUID.EAX is higher than the maximum >> input value for basic or extended function for that processor then the >> data for the highest basic information leaf is returned. For example, >> using the Intel Core i7 processor, the following is true: >> >> CPUID.EAX =3D 05H (* Returns MONITOR/MWAIT leaf. *) >> CPUID.EAX =3D 0AH (* Returns Architectural Performance Monitoring leaf= . *) >> CPUID.EAX =3D 0BH (* Returns Extended Topology Enumeration leaf. *) >> CPUID.EAX =3D 0CH (* INVALID: Returns the same information as >> CPUID.EAX =3D 0BH. *) >> CPUID.EAX =3D 80000008H (* Returns linear/physical address size data. = *) >> CPUID.EAX =3D 8000000AH (* INVALID: Returns same information as >> CPUID.EAX =3D 0BH. *) > > AMD's CPUID Specification, on the other hand, is less specific: > >> The CPUID instruction supports two sets or ranges of functions, >> standard and extended. >> >> =E2=80=A2 The smallest function number of the standard function range is >> Fn0000_0000. The largest function num- ber of the standard function >> range, for a particular implementation, is returned in CPUID >> Fn0000_0000_EAX. >> >> =E2=80=A2 The smallest function number of the extended function range is >> Fn8000_0000. The largest function num- ber of the extended function >> range, for a particular implementation, is returned in CPUID >> Fn8000_0000_EAX. >> >> Functions that are neither standard nor extended are undefined and >> should not be relied upon. > > QEMU's behavior matched Intel's specification before, but this was > changed by commit b3baa152aaef1905876670590275c2dd0bbb088c. This patch > restores the behavior documented by Intel when cpuid_xlevel2 is 0. > > The existing behavior when cpuid_xlevel2 is set (falling back to > level=3Dcpuid_xlevel) is being kept, as I couldn't find any public > documentation on the CPUID 0xC0000000 function range on Centaur CPUs. > > Signed-off-by: Eduardo Habkost > --- > Cc: Markus Armbruster > Cc: brillywu@viatech.com.cn > Cc: Marcelo Tosatti > --- > target-i386/cpu.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/target-i386/cpu.c b/target-i386/cpu.c > index 1837f5a..3cd1cee 100644 > --- a/target-i386/cpu.c > +++ b/target-i386/cpu.c > @@ -1648,7 +1648,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t inde= x, uint32_t count, > index =3D env->cpuid_xlevel; > } > } else { > - index =3D env->cpuid_xlevel; > + /* Intel documentation states that invalid EAX input will > + * return the same information as EAX=3Dcpuid_level > + * (Intel SDM Vol. 2A - Instruction Set Reference - CPUI= D) > + */ > + index =3D env->cpuid_level; > } > } > } else { Trivia: brought to my attention by when Coverity voiced suspicions on the old code we still have in RHEL-6: if (index & 0x80000000) { if (index > env->cpuid_xlevel) index =3D env->cpuid_level; } else { if (index > env->cpuid_level) index =3D env->cpuid_level; } Looks just like a pasto, doesn't it? But it's correct! Commit b3baa152 silently "fixed" the pasto. This patch reverts that mistake. Thanks Eduardo for adding the explanatory comment. Reviewed-by: Markus Armbruster