From c1bee64676c6dde2affa42cbeecef9e6aba1a9f9 Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Sat, 26 Oct 2013 23:39:10 +0200
Subject: [PATCH] 	Move cpuid code to cpuid.h and TSC code to tsc.c.

---
 ChangeLog                       |   4 ++
 grub-core/commands/i386/cpuid.c |  11 ++--
 grub-core/kern/i386/tsc.c       |  69 +++++++++++++++++++++++-
 grub-core/loader/i386/xnu.c     |   1 +
 grub-core/video/i386/pc/vbe.c   |  13 ++---
 include/grub/i386/cpuid.h       |  53 ++++++++++++++++++
 include/grub/i386/tsc.h         | 115 ----------------------------------------
 7 files changed, 133 insertions(+), 133 deletions(-)

Index: grub-2.00/grub-core/commands/i386/cpuid.c
===================================================================
--- grub-2.00.orig/grub-core/commands/i386/cpuid.c
+++ grub-2.00/grub-core/commands/i386/cpuid.c
@@ -29,11 +29,6 @@
 
 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: grub-2.00/grub-core/kern/i386/tsc.c
===================================================================
--- grub-2.00.orig/grub-core/kern/i386/tsc.c
+++ grub-2.00/grub-core/kern/i386/tsc.c
@@ -24,6 +24,7 @@
 #include <grub/time.h>
 #include <grub/misc.h>
 #include <grub/i386/tsc.h>
+#include <grub/i386/cpuid.h>
 #ifdef GRUB_MACHINE_XEN
 #include <grub/xen.h>
 #else
@@ -39,6 +40,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;
+}
+
 #ifndef GRUB_MACHINE_XEN
 
 static void
@@ -70,7 +137,7 @@ grub_pit_wait (grub_uint16_t tics)
 }
 #endif
 
-grub_uint64_t
+static grub_uint64_t
 grub_tsc_get_time_ms (void)
 {
   grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
Index: grub-2.00/grub-core/loader/i386/xnu.c
===================================================================
--- grub-2.00.orig/grub-core/loader/i386/xnu.c
+++ grub-2.00/grub-core/loader/i386/xnu.c
@@ -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: grub-2.00/grub-core/video/i386/pc/vbe.c
===================================================================
--- grub-2.00.orig/grub-core/video/i386/pc/vbe.c
+++ grub-2.00/grub-core/video/i386/pc/vbe.c
@@ -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
   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
     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: grub-2.00/include/grub/i386/cpuid.h
===================================================================
--- grub-2.00.orig/include/grub/i386/cpuid.h
+++ grub-2.00/include/grub/i386/cpuid.h
@@ -21,4 +21,57 @@
 
 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: grub-2.00/include/grub/i386/tsc.h
===================================================================
--- grub-2.00.orig/include/grub/i386/tsc.h
+++ grub-2.00/include/grub/i386/tsc.h
@@ -21,122 +21,7 @@
 
 #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);
 
