From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8050 invoked by alias); 6 May 2013 18:51:24 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 8037 invoked by uid 89); 6 May 2013 18:51:24 -0000 X-Spam-SWARE-Status: No, score=-8.8 required=5.0 tests=AWL,BAYES_00,KHOP_PGP_SIGNED,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL,RP_MATCHES_RCVD,SPF_PASS,TW_AV,TW_CP,TW_EB autolearn=ham version=3.3.1 Received: from smtp.gentoo.org (HELO smtp.gentoo.org) (140.211.166.183) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Mon, 06 May 2013 18:51:23 +0000 Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id C82BC33DFC5 for ; Mon, 6 May 2013 18:51:21 +0000 (UTC) From: Mike Frysinger To: gdb-patches@sourceware.org Subject: [patch/rfc] gdb: clean up x86 cpuid implementations Date: Mon, 06 May 2013 18:51:00 -0000 User-Agent: KMail/1.13.7 (Linux/3.8.3; KDE/4.6.5; x86_64; ; ) MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart1500446.ZiJOoPJTxv"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201305061451.24861.vapier@gentoo.org> X-Virus-Found: No X-SW-Source: 2013-05/txt/msg00135.txt.bz2 --nextPart1500446.ZiJOoPJTxv Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Content-length: 10501 We've currently got 3 files doing open coded implementations of cpuid. Each has its own set of workarounds and varying levels of how well they're written and are generally hardcoded to specific cpuid functions. If you try to build the latest gdb as a PIE on an i386 system, the build will fail because one of them lacks PIC workarounds (wrt ebx). Specifically, we have: common/linux-btrace.c: two copies of cpuid asm w/specific args, one has no workarounds while the other implicitly does to avoid memcpy go32-nat.c: one copy of cpuid asm w/specific args, kind of has workarounds to avoid memcpy gdb/testsuite/gdb.arch/i386-cpuid.h: one general cpuid asm w/many workarounds Fortunately, that last header there is pretty damn good -- it handles lots of edge cases, the code is nice & tight (uses gcc asm operands rather than manual movs), and is already almost a general library type header. So what I've done is pull that test header out and into gdb/common/ (not sure if there's a better place), generalized the API slightly, and then cut over all the existing call points to this new header. Since the func has support for "is cpuid supported on this proc", it makes it trivial to push the i386/x86_64 ifdefs down into this one header too. Now the api is clear (returns 0 if supported, 1 otherwise) and can be safely used for all targets. I've verified the gdb.arch testsuite still passes, and this code compiles for an armv7a host as well as x86_64. The go32-nat code *looks* correct, but apparently that's for a DOS target so there's no hope of me testing it :). Signed-off-by: Mike Frysinger --- gdb/Makefile.in | 2 +- gdb/{testsuite/gdb.arch =3D> common}/i386-cpuid.h | 50 +++++++++++++++----= ------ gdb/common/linux-btrace.c | 41 +++++--------------- gdb/go32-nat.c | 27 ++++++------- gdb/testsuite/gdb.arch/i386-avx.c | 2 +- gdb/testsuite/gdb.arch/i386-avx.exp | 2 +- gdb/testsuite/gdb.arch/i386-sse.c | 5 ++- gdb/testsuite/gdb.arch/i386-sse.exp | 2 +- 8 files changed, 60 insertions(+), 71 deletions(-) rename gdb/{testsuite/gdb.arch =3D> common}/i386-cpuid.h (87%) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 625ca4a..bfdeb77 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -846,7 +846,7 @@ common/common-utils.h common/xml-utils.h common/buffer.= h common/ptid.h \ common/format.h common/host-defs.h utils.h common/queue.h common/gdb_strin= g.h \ common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h \ gdb_bfd.h sparc-ravenscar-thread.h ppc-ravenscar-thread.h common/linux-btr= ace.h \ -ctf.h +ctf.h common/i386-cpuid.h =20 # Header files that already have srcdir in them, or which are in objdir. =20 diff --git a/gdb/testsuite/gdb.arch/i386-cpuid.h b/gdb/common/i386-cpuid.h similarity index 87% rename from gdb/testsuite/gdb.arch/i386-cpuid.h rename to gdb/common/i386-cpuid.h index 084a083..c1725bf 100644 --- a/gdb/testsuite/gdb.arch/i386-cpuid.h +++ b/gdb/common/i386-cpuid.h @@ -23,6 +23,11 @@ * . */ =20 +#ifndef I386_CPUID_COMMON_H +#define I386_CPUID_COMMON_H + +#if defined(__i386__) || defined(__x86_64__) + /* %ecx */ #define bit_SSE3 (1 << 0) #define bit_PCLMUL (1 << 1) @@ -165,36 +170,43 @@ __get_cpuid_max (unsigned int __ext, unsigned int *__= sig) =20 /* Return cpuid data for requested cpuid level, as found in returned eax, ebx, ecx and edx registers. The function checks if cpuid is - supported and returns 1 for valid cpuid information or 0 for - unsupported cpuid level. All pointers are required to be non-null. */ + supported and returns 0 for valid cpuid information or 1 for + unsupported cpuid level. Pointers may be non-null. */ =20 static __inline int -__get_cpuid (unsigned int __level, - unsigned int *__eax, unsigned int *__ebx, - unsigned int *__ecx, unsigned int *__edx) +i386_cpuid (unsigned int __level, + unsigned int *__eax, unsigned int *__ebx, + unsigned int *__ecx, unsigned int *__edx) { + unsigned int __scratch; unsigned int __ext =3D __level & 0x80000000; =20 + if (!__eax) + __eax =3D &__scratch; + if (!__ebx) + __ebx =3D &__scratch; + if (!__ecx) + __ecx =3D &__scratch; + if (!__edx) + __edx =3D &__scratch; + if (__get_cpuid_max (__ext, 0) < __level) - return 0; + return 1; =20 __cpuid (__level, *__eax, *__ebx, *__ecx, *__edx); - return 1; + return 0; } =20 -#ifndef NOINLINE -#define NOINLINE __attribute__ ((noinline)) -#endif - -unsigned int i386_cpuid (void) NOINLINE; +#else =20 -unsigned int NOINLINE -i386_cpuid (void) +static __inline int +i386_cpuid (unsigned int __level, + unsigned int *__eax, unsigned int *__ebx, + unsigned int *__ecx, unsigned int *__edx) { - unsigned int eax, ebx, ecx, edx; + return 1; +} =20 - if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) - return 0; +#endif /* i386 && x86_64 */ =20 - return edx; -} +#endif /* I386_CPUID_COMMON_H */ diff --git a/gdb/common/linux-btrace.c b/gdb/common/linux-btrace.c index d3c8705..386fb12 100644 --- a/gdb/common/linux-btrace.c +++ b/gdb/common/linux-btrace.c @@ -30,6 +30,7 @@ #include "gdb_assert.h" #include "regcache.h" #include "gdbthread.h" +#include "i386-cpuid.h" =20 #if HAVE_LINUX_PERF_EVENT_H =20 @@ -339,13 +340,10 @@ kernel_supports_btrace (void) static int intel_supports_btrace (void) { -#if defined __i386__ || defined __x86_64__ unsigned int cpuid, model, family; =20 - __asm__ __volatile__ ("movl $1, %%eax;" - "cpuid;" - : "=3Da" (cpuid) - :: "%ebx", "%ecx", "%edx"); + if (i386_cpuid (1, &cpuid, NULL, NULL, NULL)) + return 0; =20 family =3D (cpuid >> 8) & 0xf; model =3D (cpuid >> 4) & 0xf; @@ -376,12 +374,6 @@ intel_supports_btrace (void) } =20 return 1; - -#else /* !defined __i386__ && !defined __x86_64__ */ - - return 0; - -#endif /* !defined __i386__ && !defined __x86_64__ */ } =20 /* Check whether the cpu supports branch tracing. */ @@ -389,22 +381,15 @@ intel_supports_btrace (void) static int cpu_supports_btrace (void) { -#if defined __i386__ || defined __x86_64__ + unsigned int ebx, ecx, edx; char vendor[13]; =20 - __asm__ __volatile__ ("xorl %%ebx, %%ebx;" - "xorl %%ecx, %%ecx;" - "xorl %%edx, %%edx;" - "movl $0, %%eax;" - "cpuid;" - "movl %%ebx, %0;" - "movl %%edx, %1;" - "movl %%ecx, %2;" - : "=3Dm" (vendor[0]), - "=3Dm" (vendor[4]), - "=3Dm" (vendor[8]) - : - : "%eax", "%ebx", "%ecx", "%edx"); + if (i386_cpuid (0, NULL, &ebx, &ecx, &edx)) + return 0; + + memcpy (&vendor[0], &ebx, 4); + memcpy (&vendor[4], &ecx, 4); + memcpy (&vendor[8], &edx, 4); vendor[12] =3D '\0'; =20 if (strcmp (vendor, "GenuineIntel") =3D=3D 0) @@ -412,12 +397,6 @@ cpu_supports_btrace (void) =20 /* Don't know about others. Let's assume they do. */ return 1; - -#else /* !defined __i386__ && !defined __x86_64__ */ - - return 0; - -#endif /* !defined __i386__ && !defined __x86_64__ */ } =20 /* See linux-btrace.h. */ diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c index ea6d1ca..72adc88 100644 --- a/gdb/go32-nat.c +++ b/gdb/go32-nat.c @@ -96,6 +96,7 @@ #include "buildsym.h" #include "i387-tdep.h" #include "i386-tdep.h" +#include "i386-cpuid.h" #include "value.h" #include "regcache.h" #include "gdb_string.h" @@ -1140,23 +1141,17 @@ go32_sysinfo (char *arg, int from_tty) strcpy (u.machine, "Unknown x86"); else if (u.machine[0] =3D=3D 'i' && u.machine[1] > 4) { + unsigned int eax, ebx, ecx, edx; + /* CPUID with EAX =3D 0 returns the Vendor ID. */ - __asm__ __volatile__ ("xorl %%ebx, %%ebx;" - "xorl %%ecx, %%ecx;" - "xorl %%edx, %%edx;" - "movl $0, %%eax;" - "cpuid;" - "movl %%ebx, %0;" - "movl %%edx, %1;" - "movl %%ecx, %2;" - "movl %%eax, %3;" - : "=3Dm" (cpuid_vendor[0]), - "=3Dm" (cpuid_vendor[4]), - "=3Dm" (cpuid_vendor[8]), - "=3Dm" (cpuid_max) - : - : "%eax", "%ebx", "%ecx", "%edx"); - cpuid_vendor[12] =3D '\0'; + if (!i386_cpuid (0, &eax, &ebx, &ecx, &edx)) + { + cpuid_max =3D eax; + memcpy (&vendor[0], &ebx, 4); + memcpy (&vendor[4], &ecx, 4); + memcpy (&vendor[8], &edx, 4); + cpuid_vendor[12] =3D '\0'; + } } =20 printf_filtered ("CPU Type.......................%s", u.machine); diff --git a/gdb/testsuite/gdb.arch/i386-avx.c b/gdb/testsuite/gdb.arch/i38= 6-avx.c index bcfa18f..7fd1217 100644 --- a/gdb/testsuite/gdb.arch/i386-avx.c +++ b/gdb/testsuite/gdb.arch/i386-avx.c @@ -53,7 +53,7 @@ have_avx (void) { unsigned int eax, ebx, ecx, edx; =20 - if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) + if (!i386_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; =20 if ((ecx & (bit_AVX | bit_OSXSAVE)) =3D=3D (bit_AVX | bit_OSXSAVE)) diff --git a/gdb/testsuite/gdb.arch/i386-avx.exp b/gdb/testsuite/gdb.arch/i= 386-avx.exp index 964806c..bbbc6f4 100644 --- a/gdb/testsuite/gdb.arch/i386-avx.exp +++ b/gdb/testsuite/gdb.arch/i386-avx.exp @@ -34,7 +34,7 @@ if [get_compiler_info] { =20 set additional_flags "" if [test_compiler_info gcc*] { - set additional_flags "additional_flags=3D-mavx" + set additional_flags "additional_flags=3D-mavx -I${srcdir}/../common" } =20 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable= [list debug $additional_flags]] !=3D "" } { diff --git a/gdb/testsuite/gdb.arch/i386-sse.c b/gdb/testsuite/gdb.arch/i38= 6-sse.c index d431527..5d2d277 100644 --- a/gdb/testsuite/gdb.arch/i386-sse.c +++ b/gdb/testsuite/gdb.arch/i386-sse.c @@ -51,7 +51,10 @@ v4sf_t data[] =3D int have_sse (void) { - int edx =3D i386_cpuid (); + int edx; + + if (i386_cpuid (1, NULL, NULL, NULL, &edx)) + return 0; =20 if (edx & bit_SSE) return 1; diff --git a/gdb/testsuite/gdb.arch/i386-sse.exp b/gdb/testsuite/gdb.arch/i= 386-sse.exp index 5923eca..c62a3a0 100644 --- a/gdb/testsuite/gdb.arch/i386-sse.exp +++ b/gdb/testsuite/gdb.arch/i386-sse.exp @@ -34,7 +34,7 @@ if [get_compiler_info] { =20 set additional_flags "" if [test_compiler_info gcc*] { - set additional_flags "additional_flags=3D-msse" + set additional_flags "additional_flags=3D-msse -I${srcdir}/../common" } =20 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable= [list debug $additional_flags]] !=3D "" } { --nextPart1500446.ZiJOoPJTxv Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. Content-length: 836 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQIcBAABAgAGBQJRh/usAAoJEEFjO5/oN/WBoNYP/A+EqkIHcNxm8OfO11Cw5YRI nz1/qOaG0XDEsgyyfSqm9YrOT+YvyMxepsVa7PKDSUOxxD1J0g8/ji4Jip74q5eK eluGQFEXegP+TVS7NeLo62/3p/QLqgpuvghYSZgqIHcBr46J8PWG4r2qDIzzVTT7 wOPqJ05jHMHJ6gNs2Uy6jV40lZmen3PZGbDEGKaR1qTcqR4jmKaxHPQshsRqgC8g oZk3I+XxYSoUuhA3pnQkXLgL++gk27DEHzYKN+jacZJJq3nz1ADop4nEYH3LTAAE q4YL1nQHuskLIm93bQUfkwrjXTIeZve+y5W8VY0CCKEFuQwFkWXT+WJ83Dsi+vOo hXuAkmpAbApyi1bC+2x5X97hkwVgEhqyLMvQIQ4li6hQtct6QP50LdeG8UUGZjL6 PRQsq8hdPxvQVJAKfw6Z8kqePge0bUd83Jkc/yFF/WhzpAribQe0NvVTkpPqnfFt yi0c3tdzPGpZumrcWRbpfq1Ck/HP2aT2Xonm+uNWpEL6ay+ag3CLEBdiiGAHOZKh Ucf/l1L6g9VLNvB3hTsvkHg198ssR4MZjfgOk76aSm9LlAkWsKQWV2vsEwnNFNeA K4Z3O1cMWwsnSlxu9pst8Ch4kN2xfi3rGiVdgCgenGs14/wst0QR9EmnitwOOOjs vPNMtvgPGIhtAurtPBa7 =WugK -----END PGP SIGNATURE----- --nextPart1500446.ZiJOoPJTxv--