This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] gdb: clean up x86 cpuid implementations


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:
	two copies of cpuid asm w/specific args, one 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 has been left
ifdef-ed out until someone can test & verify the new stuff works (and if
it doesn't, figure out how to make the new code work).

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
v2
	- leave the new code in go32-nat ifdef-ed out for someone to test

 gdb/Makefile.in                                 |  2 +-
 gdb/{testsuite/gdb.arch => common}/i386-cpuid.h | 50 +++++++++++++++----------
 gdb/common/linux-btrace.c                       | 41 +++++---------------
 gdb/go32-nat.c                                  | 22 +++++++++++
 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, 71 insertions(+), 55 deletions(-)
 rename gdb/{testsuite/gdb.arch => 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_string.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-btrace.h \
-ctf.h
+ctf.h common/i386-cpuid.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
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 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+#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)
 
 /* 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.  */
 
 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 = __level & 0x80000000;
 
+  if (!__eax)
+    __eax = &__scratch;
+  if (!__ebx)
+    __ebx = &__scratch;
+  if (!__ecx)
+    __ecx = &__scratch;
+  if (!__edx)
+    __edx = &__scratch;
+
   if (__get_cpuid_max (__ext, 0) < __level)
-    return 0;
+    return 1;
 
   __cpuid (__level, *__eax, *__ebx, *__ecx, *__edx);
-  return 1;
+  return 0;
 }
 
-#ifndef NOINLINE
-#define NOINLINE __attribute__ ((noinline))
-#endif
-
-unsigned int i386_cpuid (void) NOINLINE;
+#else
 
-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;
+}
 
-  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
-    return 0;
+#endif /* i386 && x86_64 */
 
-  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"
 
 #if HAVE_LINUX_PERF_EVENT_H
 
@@ -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;
 
-  __asm__ __volatile__ ("movl   $1, %%eax;"
-			"cpuid;"
-			: "=a" (cpuid)
-			:: "%ebx", "%ecx", "%edx");
+  if (i386_cpuid (1, &cpuid, NULL, NULL, NULL))
+    return 0;
 
   family = (cpuid >> 8) & 0xf;
   model = (cpuid >> 4) & 0xf;
@@ -376,12 +374,6 @@ intel_supports_btrace (void)
     }
 
   return 1;
-
-#else /* !defined __i386__ && !defined __x86_64__ */
-
-  return 0;
-
-#endif /* !defined __i386__ && !defined __x86_64__ */
 }
 
 /* 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];
 
-  __asm__ __volatile__ ("xorl   %%ebx, %%ebx;"
-			"xorl   %%ecx, %%ecx;"
-			"xorl   %%edx, %%edx;"
-			"movl   $0,    %%eax;"
-			"cpuid;"
-			"movl   %%ebx,  %0;"
-			"movl   %%edx,  %1;"
-			"movl   %%ecx,  %2;"
-			: "=m" (vendor[0]),
-			  "=m" (vendor[4]),
-			  "=m" (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] = '\0';
 
   if (strcmp (vendor, "GenuineIntel") == 0)
@@ -412,12 +397,6 @@ cpu_supports_btrace (void)
 
   /* 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__ */
 }
 
 /* See linux-btrace.h.  */
diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c
index ea6d1ca..9cde0e7 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"
@@ -1141,6 +1142,21 @@ go32_sysinfo (char *arg, int from_tty)
   else if (u.machine[0] == 'i' && u.machine[1] > 4)
     {
       /* CPUID with EAX = 0 returns the Vendor ID.  */
+#if 0
+      /* Ideally we would use i386_cpuid(), but it needs someone to run
+         native tests first to make sure things actually work.  They should.
+         http://sourceware.org/ml/gdb-patches/2013-05/msg00164.html  */
+      unsigned int eax, ebx, ecx, edx;
+
+      if (!i386_cpuid (0, &eax, &ebx, &ecx, &edx))
+	{
+	  cpuid_max = eax;
+	  memcpy (&vendor[0], &ebx, 4);
+	  memcpy (&vendor[4], &ecx, 4);
+	  memcpy (&vendor[8], &edx, 4);
+	  cpuid_vendor[12] = '\0';
+	}
+#else
       __asm__ __volatile__ ("xorl   %%ebx, %%ebx;"
 			    "xorl   %%ecx, %%ecx;"
 			    "xorl   %%edx, %%edx;"
@@ -1157,6 +1173,7 @@ go32_sysinfo (char *arg, int from_tty)
 			    :
 			    : "%eax", "%ebx", "%ecx", "%edx");
       cpuid_vendor[12] = '\0';
+#endif
     }
 
   printf_filtered ("CPU Type.......................%s", u.machine);
@@ -1182,6 +1199,10 @@ go32_sysinfo (char *arg, int from_tty)
       int amd_p = strcmp (cpuid_vendor, "AuthenticAMD") == 0;
       unsigned cpu_family, cpu_model;
 
+#if 0
+      /* See comment above about cpuid usage.  */
+      i386_cpuid (1, &cpuid_eax, &cpuid_ebx, NULL, &cpuid_edx);
+#else
       __asm__ __volatile__ ("movl   $1, %%eax;"
 			    "cpuid;"
 			    : "=a" (cpuid_eax),
@@ -1189,6 +1210,7 @@ go32_sysinfo (char *arg, int from_tty)
 			      "=d" (cpuid_edx)
 			    :
 			    : "%ecx");
+#endif
       brand_idx = cpuid_ebx & 0xff;
       cpu_family = (cpuid_eax >> 8) & 0xf;
       cpu_model  = (cpuid_eax >> 4) & 0xf;
diff --git a/gdb/testsuite/gdb.arch/i386-avx.c b/gdb/testsuite/gdb.arch/i386-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;
 
-  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
+  if (!i386_cpuid (1, &eax, &ebx, &ecx, &edx))
     return 0;
 
   if ((ecx & (bit_AVX | bit_OSXSAVE)) == (bit_AVX | bit_OSXSAVE))
diff --git a/gdb/testsuite/gdb.arch/i386-avx.exp b/gdb/testsuite/gdb.arch/i386-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] {
 
 set additional_flags ""
 if [test_compiler_info gcc*] {
-    set additional_flags "additional_flags=-mavx"
+    set additional_flags "additional_flags=-mavx -I${srcdir}/../common"
 }
 
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug $additional_flags]] != "" } {
diff --git a/gdb/testsuite/gdb.arch/i386-sse.c b/gdb/testsuite/gdb.arch/i386-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[] =
 int
 have_sse (void)
 {
-  int edx = i386_cpuid ();
+  int edx;
+
+  if (i386_cpuid (1, NULL, NULL, NULL, &edx))
+    return 0;
 
   if (edx & bit_SSE)
     return 1;
diff --git a/gdb/testsuite/gdb.arch/i386-sse.exp b/gdb/testsuite/gdb.arch/i386-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] {
 
 set additional_flags ""
 if [test_compiler_info gcc*] {
-    set additional_flags "additional_flags=-msse"
+    set additional_flags "additional_flags=-msse -I${srcdir}/../common"
 }
 
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug $additional_flags]] != "" } {
-- 
1.8.2.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]