Skip to contentSkip to conversation

Don't clobber ebx/rbx in inline assembly when compiling with Apple's CC

PATCH In review Vladimir 'phcoder' Serbinenko 2 participants 3 messages May 30, 2009 – Jun 16, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 7/18 Don't clobber ebx/rbx in inline assembly when compiling with Apple's CC

Unfortunately Apple's CC doesn't accept ebx to be clobbered. This patch makes inline assembly restore ebx

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
efiemu/runtime/efiemu.c +2 -2
index 5887c48..f73f843 100644
@@ -162,8 +162,8 @@ static inline void
 write_cmos (grub_uint8_t addr, grub_uint8_t val)
 {
   __asm__ __volatile__ ("outb %%al,$0x70\n"
-			"mov %%bl, %%al\n"
-			"outb %%al,$0x71": :"a" (addr), "b" (val));
+			"mov %%cl, %%al\n"
+			"outb %%al,$0x71": :"a" (addr), "c" (val));
 }
 
 static inline grub_uint8_t
include/grub/i386/tsc.h +36 -2
index 9633701..aa40145 100644
@@ -29,8 +29,24 @@ grub_get_tsc (void)
 
   /* The CPUID instruction is a 'serializing' instruction, and
      avoids out-of-order execution of the RDTSC instruction. */
+#ifdef APPLE_CC
   __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
-                        "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");
+#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));
@@ -93,11 +109,29 @@ grub_cpu_is_tsc_supported (void)
     return 0;
 
   grub_uint32_t features;
+#ifdef APPLE_CC
   __asm__ ("movl $1, %%eax\n\t"
-           "cpuid"
+#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;
 }
 
loader/i386/xnu.c +41 -1

62 lines not on this page · raw message

#1 lore raw

include/grub/i386/tsc.h grub_get_tsc (void) · line 33

Quoted 3 lines · Vladimir 'phcoder' Serbinenko
include/grub/i386/tsc.h +1 -1
+#ifdef APPLE_CC
   __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
-                        "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");

Why do we need preprocessor conditionals here? What are we winning? I think it's more important to have reusable, well tested and well maintained code that to save two assembly instructions.

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 9 lines · Pavel Roskin

include/grub/i386/tsc.h grub_get_tsc (void) · line 33

Quoted 3 lines · Vladimir 'phcoder' Serbinenko
include/grub/i386/tsc.h +1 -1
+#ifdef APPLE_CC
   __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
-                        "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");

Why do we need preprocessor conditionals here? What are we winning? I think it's more important to have reusable, well tested and well maintained code that to save two assembly instructions.

You're right. Just when doing this thing I was concerned to affect generic code as less as possible

Quoted 11 lines

--
Regards,
Pavel Roskin

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko