]> git.proxmox.com Git - grub2.git/commitdiff
Move cpuid code to cpuid.h and TSC code to tsc.c.
authorVladimir Serbinenko <phcoder@gmail.com>
Sat, 26 Oct 2013 21:39:10 +0000 (23:39 +0200)
committerVladimir Serbinenko <phcoder@gmail.com>
Sat, 26 Oct 2013 21:39:10 +0000 (23:39 +0200)
ChangeLog
grub-core/commands/i386/cpuid.c
grub-core/kern/i386/tsc.c
grub-core/loader/i386/xnu.c
grub-core/video/i386/pc/vbe.c
include/grub/i386/cpuid.h
include/grub/i386/tsc.h

index 86b5aab0b65b413cbaa1c362ff13941339c0b41c..70b9b747e146be942bfd989bcbfebe2a15b1c425 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-10-26  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       Move cpuid code to cpuid.h and TSC code to tsc.c.
+
 2013-10-26  GrĂ©goire Sutre  <gregoire.sutre@gmail.com>
 
        * util/grub.d/00_header.in: Don't use LANG if it's not set.
index 3b895cb937202027781d6a53236f1d69ed64363e..af753590d769062a288edf64c35ceecd5a1238d0 100644 (file)
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-#define cpuid(num,a,b,c,d) \
-  asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
-               : "=a" (a), "=r" (b), "=c" (c), "=d" (d)  \
-               : "0" (num))
-
 static const struct grub_arg_option options[] =
   {
     /* TRANSLATORS: "(default)" at the end means that this option is used if
@@ -78,18 +73,18 @@ GRUB_MOD_INIT(cpuid)
     goto done;
 
   /* Check the highest input value for eax.  */
-  cpuid (0, eax, ebx, ecx, edx);
+  grub_cpuid (0, eax, ebx, ecx, edx);
   /* We only look at the first four characters.  */
   max_level = eax;
   if (max_level == 0)
     goto done;
 
-  cpuid (0x80000000, eax, ebx, ecx, edx);
+  grub_cpuid (0x80000000, eax, ebx, ecx, edx);
   ext_level = eax;
   if (ext_level < 0x80000000)
     goto done;
 
-  cpuid (0x80000001, eax, ebx, ecx, edx);
+  grub_cpuid (0x80000001, eax, ebx, ecx, edx);
   grub_cpuid_has_longmode = !!(edx & bit_LM);
 done:
 #endif
index 9efd6338cd294b73fa35564485ed13636601e3c5..d5c5669d2afce1ff44204008172c28b835e4faee 100644 (file)
@@ -24,6 +24,7 @@
 #include <grub/time.h>
 #include <grub/misc.h>
 #include <grub/i386/tsc.h>
+#include <grub/i386/cpuid.h>
 #include <grub/i386/pit.h>
 #include <grub/cpu/io.h>
 
@@ -35,6 +36,72 @@ static grub_uint64_t tsc_boot_time;
    in 32-bit.  */
 grub_uint32_t grub_tsc_rate;
 
+/* Read the TSC value, which increments with each CPU clock cycle. */
+static __inline grub_uint64_t
+grub_get_tsc (void)
+{
+  grub_uint32_t lo, hi;
+
+  /* The CPUID instruction is a 'serializing' instruction, and
+     avoids out-of-order execution of the RDTSC instruction. */
+#ifdef __APPLE__
+  __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
+#ifdef __x86_64__
+                       "push %%rbx\n"
+#else
+                       "push %%ebx\n"
+#endif
+                       "cpuid\n"
+#ifdef __x86_64__
+                       "pop %%rbx\n"
+#else
+                       "pop %%ebx\n"
+#endif
+                       :::"%rax", "%rcx", "%rdx");
+#else
+  __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
+                       "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");
+#endif
+  /* Read TSC value.  We cannot use "=A", since this would use
+     %rax on x86_64. */
+  __asm__ __volatile__ ("rdtsc":"=a" (lo), "=d" (hi));
+
+  return (((grub_uint64_t) hi) << 32) | lo;
+}
+
+static __inline int
+grub_cpu_is_tsc_supported (void)
+{
+  if (! grub_cpu_is_cpuid_supported ())
+    return 0;
+
+  grub_uint32_t features;
+#ifdef __APPLE__
+  __asm__ ("movl $1, %%eax\n\t"
+#ifdef __x86_64__
+          "push %%rbx\n"
+#else
+          "push %%ebx\n"
+#endif
+          "cpuid\n"
+#ifdef __x86_64__
+          "pop %%rbx\n"
+#else
+          "pop %%ebx\n"
+#endif
+           : "=d" (features)
+           : /* No inputs.  */
+          : /* Clobbered:  */ "%rax", "%rcx");
+#else
+  __asm__ ("movl $1, %%eax\n\t"
+           "cpuid\n"
+           : "=d" (features)
+           : /* No inputs.  */
+           : /* Clobbered:  */ "%rax", "%rbx", "%rcx");
+#endif
+  return (features & (1 << 4)) != 0;
+}
+
 static void
 grub_pit_wait (grub_uint16_t tics)
 {
@@ -63,7 +130,7 @@ grub_pit_wait (grub_uint16_t tics)
              GRUB_PIT_SPEAKER_PORT);
 }
 
-grub_uint64_t
+static grub_uint64_t
 grub_tsc_get_time_ms (void)
 {
   grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
index 497529bc3868cc4a4a66edb1e3fd09ef4703f5f8..1c574e4cf327640b854f4d6f497cb1558d53e88f 100644 (file)
@@ -25,6 +25,7 @@
 #include <grub/loader.h>
 #include <grub/autoefi.h>
 #include <grub/i386/tsc.h>
+#include <grub/i386/cpuid.h>
 #include <grub/efi/api.h>
 #include <grub/i386/pit.h>
 #include <grub/misc.h>
index f6cd51c7f39ec15d4639be5ea494d2070e2a54c4..a29a15dc3d60b88b87cc44127d928179cd93d8ac 100644 (file)
@@ -29,7 +29,7 @@
 #include <grub/video.h>
 #include <grub/i386/pc/int.h>
 #include <grub/i18n.h>
-#include <grub/cpu/tsc.h>
+#include <grub/cpu/cpuid.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -58,11 +58,6 @@ real2pm (grub_vbe_farptr_t ptr)
                    + ((unsigned long) ptr & 0x0000FFFF));
 }
 
-#define cpuid(num,a,b,c,d) \
-  asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
-                : "=a" (a), "=r" (b), "=c" (c), "=d" (d)  \
-                : "0" (num))
-
 #define rdmsr(num,a,d) \
   asm volatile ("rdmsr" : "=a" (a), "=d" (d) : "c" (num))
 
@@ -136,7 +131,7 @@ grub_vbe_enable_mtrr (grub_uint8_t *base, grub_size_t size)
   if (! grub_cpu_is_cpuid_supported ())
     return;
 
-  cpuid (1, eax, ebx, ecx, edx);
+  grub_cpuid (1, eax, ebx, ecx, edx);
   features = edx;
   if (! (features & 0x00001000)) /* MTRR */
     return;
@@ -147,11 +142,11 @@ grub_vbe_enable_mtrr (grub_uint8_t *base, grub_size_t size)
     return;
   var_mtrrs = (mtrrcap & 0xFF);
 
-  cpuid (0x80000000, eax, ebx, ecx, edx);
+  grub_cpuid (0x80000000, eax, ebx, ecx, edx);
   max_extended_cpuid = eax;
   if (max_extended_cpuid >= 0x80000008)
     {
-      cpuid (0x80000008, eax, ebx, ecx, edx);
+      grub_cpuid (0x80000008, eax, ebx, ecx, edx);
       maxphyaddr = (eax & 0xFF);
     }
   else
index 09b313bb8e20141334cceb96ecdd961c9e993288..ce087b61963776077e7475b003805e5c7c2b8674 100644 (file)
 
 extern unsigned char grub_cpuid_has_longmode;
 
+#ifdef __x86_64__
+
+static __inline int
+grub_cpu_is_cpuid_supported (void)
+{
+  grub_uint64_t id_supported;
+
+  __asm__ ("pushfq\n\t"
+           "popq %%rax             /* Get EFLAGS into EAX */\n\t"
+           "movq %%rax, %%rcx      /* Save original flags in ECX */\n\t"
+           "xorq $0x200000, %%rax  /* Flip ID bit in EFLAGS */\n\t"
+           "pushq %%rax            /* Store modified EFLAGS on stack */\n\t"
+           "popfq                  /* Replace current EFLAGS */\n\t"
+           "pushfq                 /* Read back the EFLAGS */\n\t"
+           "popq %%rax             /* Get EFLAGS into EAX */\n\t"
+           "xorq %%rcx, %%rax      /* Check if flag could be modified */\n\t"
+           : "=a" (id_supported)
+           : /* No inputs.  */
+           : /* Clobbered:  */ "%rcx");
+
+  return id_supported != 0;
+}
+
+#else
+
+static __inline int
+grub_cpu_is_cpuid_supported (void)
+{
+  grub_uint32_t id_supported;
+
+  __asm__ ("pushfl\n\t"
+           "popl %%eax             /* Get EFLAGS into EAX */\n\t"
+           "movl %%eax, %%ecx      /* Save original flags in ECX */\n\t"
+           "xorl $0x200000, %%eax  /* Flip ID bit in EFLAGS */\n\t"
+           "pushl %%eax            /* Store modified EFLAGS on stack */\n\t"
+           "popfl                  /* Replace current EFLAGS */\n\t"
+           "pushfl                 /* Read back the EFLAGS */\n\t"
+           "popl %%eax             /* Get EFLAGS into EAX */\n\t"
+           "xorl %%ecx, %%eax      /* Check if flag could be modified */\n\t"
+           : "=a" (id_supported)
+           : /* No inputs.  */
+           : /* Clobbered:  */ "%rcx");
+
+  return id_supported != 0;
+}
+
+#endif
+
+#define grub_cpuid(num,a,b,c,d) \
+  asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
+                : "=a" (a), "=r" (b), "=c" (c), "=d" (d)  \
+                : "0" (num))
+
 #endif
index d25d0e3e9d9eaf7f3f1ed3c5ecb4a92e4bdebfb0..71b32e6b844dee5dbad8a5b5a91998a6844585ea 100644 (file)
 
 #include <grub/types.h>
 
-/* Read the TSC value, which increments with each CPU clock cycle. */
-static __inline grub_uint64_t
-grub_get_tsc (void)
-{
-  grub_uint32_t lo, hi;
-
-  /* The CPUID instruction is a 'serializing' instruction, and
-     avoids out-of-order execution of the RDTSC instruction. */
-#ifdef __APPLE__
-  __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
-#ifdef __x86_64__
-                       "push %%rbx\n"
-#else
-                       "push %%ebx\n"
-#endif
-                       "cpuid\n"
-#ifdef __x86_64__
-                       "pop %%rbx\n"
-#else
-                       "pop %%ebx\n"
-#endif
-                       :::"%rax", "%rcx", "%rdx");
-#else
-  __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
-                       "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");
-#endif
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  __asm__ __volatile__ ("rdtsc":"=a" (lo), "=d" (hi));
-
-  return (((grub_uint64_t) hi) << 32) | lo;
-}
-
-#ifdef __x86_64__
-
-static __inline int
-grub_cpu_is_cpuid_supported (void)
-{
-  grub_uint64_t id_supported;
-
-  __asm__ ("pushfq\n\t"
-           "popq %%rax             /* Get EFLAGS into EAX */\n\t"
-           "movq %%rax, %%rcx      /* Save original flags in ECX */\n\t"
-           "xorq $0x200000, %%rax  /* Flip ID bit in EFLAGS */\n\t"
-           "pushq %%rax            /* Store modified EFLAGS on stack */\n\t"
-           "popfq                  /* Replace current EFLAGS */\n\t"
-           "pushfq                 /* Read back the EFLAGS */\n\t"
-           "popq %%rax             /* Get EFLAGS into EAX */\n\t"
-           "xorq %%rcx, %%rax      /* Check if flag could be modified */\n\t"
-           : "=a" (id_supported)
-           : /* No inputs.  */
-           : /* Clobbered:  */ "%rcx");
-
-  return id_supported != 0;
-}
-
-#else
-
-static __inline int
-grub_cpu_is_cpuid_supported (void)
-{
-  grub_uint32_t id_supported;
-
-  __asm__ ("pushfl\n\t"
-           "popl %%eax             /* Get EFLAGS into EAX */\n\t"
-           "movl %%eax, %%ecx      /* Save original flags in ECX */\n\t"
-           "xorl $0x200000, %%eax  /* Flip ID bit in EFLAGS */\n\t"
-           "pushl %%eax            /* Store modified EFLAGS on stack */\n\t"
-           "popfl                  /* Replace current EFLAGS */\n\t"
-           "pushfl                 /* Read back the EFLAGS */\n\t"
-           "popl %%eax             /* Get EFLAGS into EAX */\n\t"
-           "xorl %%ecx, %%eax      /* Check if flag could be modified */\n\t"
-           : "=a" (id_supported)
-           : /* No inputs.  */
-           : /* Clobbered:  */ "%rcx");
-
-  return id_supported != 0;
-}
-
-#endif
-
-static __inline int
-grub_cpu_is_tsc_supported (void)
-{
-  if (! grub_cpu_is_cpuid_supported ())
-    return 0;
-
-  grub_uint32_t features;
-#ifdef __APPLE__
-  __asm__ ("movl $1, %%eax\n\t"
-#ifdef __x86_64__
-          "push %%rbx\n"
-#else
-          "push %%ebx\n"
-#endif
-          "cpuid\n"
-#ifdef __x86_64__
-          "pop %%rbx\n"
-#else
-          "pop %%ebx\n"
-#endif
-           : "=d" (features)
-           : /* No inputs.  */
-          : /* Clobbered:  */ "%rax", "%rcx");
-#else
-  __asm__ ("movl $1, %%eax\n\t"
-           "cpuid\n"
-           : "=d" (features)
-           : /* No inputs.  */
-           : /* Clobbered:  */ "%rax", "%rbx", "%rcx");
-#endif
-  return (features & (1 << 4)) != 0;
-}
-
 void grub_tsc_init (void);
-grub_uint64_t grub_tsc_get_time_ms (void);
 /* In ms per 2^32 ticks.  */
 extern grub_uint32_t EXPORT_VAR(grub_tsc_rate);