Skip to contentSkip to conversation

Crash due to invalid env->current_tb

Qemu-devel Adam Lackorzynski 6 participants 20 messages Apr 29, 2008 – May 3, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'.
The segfault happens because of an invalid env->current_tb which seems to be caused by generated code. The following code in cpu_exec

   tc_ptr = tb->tc_ptr;
   env->current_tb = tb;
   gen_func = (void *)tc_ptr;
   T0 = gen_func();
   env->current_tb = NULL;

is being compiled to the following

   mov    0x14(%ecx),%eax
   mov    %ecx,0x56c(%ebp)
   xor    %edi,%edi
   call   *%eax
   mov    %edi,0x56c(%ebp)

After the call edi isn't 0 anymore and gets the bogus value. As edi is callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed after the call and everything works fine. So might the problem be that generated code isn't preserving edi/registers?

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#1 lore raw
Quoted 21 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'.
The segfault happens because of an invalid env->current_tb which seems to be caused by generated code. The following code in cpu_exec

   tc_ptr = tb->tc_ptr;
   env->current_tb = tb;
   gen_func = (void *)tc_ptr;
   T0 = gen_func();
   env->current_tb = NULL;

is being compiled to the following

   mov    0x14(%ecx),%eax
   mov    %ecx,0x56c(%ebp)
   xor    %edi,%edi
   call   *%eax
   mov    %edi,0x56c(%ebp)
Quoted 5 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed after the call and everything works fine. So might the problem be that generated code isn't preserving edi/registers?

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

cpu-exec.c +5 -0
Index: qemu/cpu-exec.c
===================================================================
@@ -690,6 +690,11 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+                asm volatile ("call *%0\n\t"
+                              : "=r" (T0)
+                              : "r" (gen_func)
+                              : "ebx", "esi", "edi");
 #else
                 T0 = gen_func();
 #endif
#2 lore raw
Quoted 28 lines · Blue Swirl
Quoted 21 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'.
The segfault happens because of an invalid env->current_tb which seems to be caused by generated code. The following code in cpu_exec

   tc_ptr = tb->tc_ptr;
   env->current_tb = tb;
   gen_func = (void *)tc_ptr;
   T0 = gen_func();
   env->current_tb = NULL;

is being compiled to the following

   mov    0x14(%ecx),%eax
   mov    %ecx,0x56c(%ebp)
   xor    %edi,%edi
   call   *%eax
   mov    %edi,0x56c(%ebp)
Quoted 5 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed after the call and everything works fine. So might the problem be that generated code isn't preserving edi/registers?

Quoted 3 lines

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

I just added an 'asm volatile("")' to stop reordering of instructions which of course isn't enough. The following works for me:

===================================================================

cpu-exec.c +5 -0
@@ -690,6 +691,11 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("call *%1\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi");
 #else
                 T0 = gen_func();
 #endif

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#3 lore raw
Quoted 42 lines · Adam Lackorzynski
Quoted 28 lines · Blue Swirl
Quoted 21 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'.
The segfault happens because of an invalid env->current_tb which seems to be caused by generated code. The following code in cpu_exec

  tc_ptr = tb->tc_ptr;
  env->current_tb = tb;
  gen_func = (void *)tc_ptr;
  T0 = gen_func();
  env->current_tb = NULL;

is being compiled to the following

  mov    0x14(%ecx),%eax
  mov    %ecx,0x56c(%ebp)
  xor    %edi,%edi
  call   *%eax
  mov    %edi,0x56c(%ebp)
Quoted 5 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed after the call and everything works fine. So might the problem be that generated code isn't preserving edi/registers?

Quoted 3 lines

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

I just added an 'asm volatile("")' to stop reordering of instructions which of course isn't enough. The following works for me:

===================================================================

cpu-exec.c +0 -0
@@ -690,6 +691,11 @@
 		fp.ip = tc_ptr;

cpu-exec.c line 701

Quoted 10 lines
cpu-exec.c +5 -0
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("call *%1\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi");
 #else
                T0 = gen_func();
 #endif

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

Alex

#4 lore raw
Quoted 55 lines · Alexander Graf
Quoted 52 lines · Adam Lackorzynski
Quoted 33 lines · Blue Swirl
Quoted 23 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target
arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'. The segfault happens because of an invalid env->current_tb which seems
to be caused by generated code. The following code in cpu_exec

  tc_ptr = tb->tc_ptr;
  env->current_tb = tb;
  gen_func = (void *)tc_ptr;
  T0 = gen_func();
  env->current_tb = NULL;

is being compiled to the following

  mov    0x14(%ecx),%eax
  mov    %ecx,0x56c(%ebp)
  xor    %edi,%edi
  call   *%eax
  mov    %edi,0x56c(%ebp)
Quoted 8 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is
callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed
after the call and everything works fine. So might the problem be that
generated code isn't preserving edi/registers?

Quoted 3 lines

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

I just added an 'asm volatile("")' to stop reordering of instructions which of course isn't enough. The following works for me:

===================================================================

cpu-exec.c +3 -0
@@ -690,6 +691,11 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("call *%1\n"
+		              : "=a" (T0)
Quoted 8 lines

cpu-exec.c line 701

Quoted 5 lines
cpu-exec.c +2 -0
+			      : "r" (gen_func)
+			      : "esi", "edi");
 #else
                T0 = gen_func();
 #endif

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

cpu-exec.c +9 -0
Index: cpu-exec.c
===================================================================
@@ -690,6 +691,15 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "push %%ebx\n"
+		              "call *%1\n"
+			      "pop %%ebx\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");
 #else
                 T0 = gen_func();
 #endif

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#5 lore raw
Quoted 17 lines · Adam Lackorzynski
Quoted 2 lines · Alexander Graf

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c
===================================================================

cpu-exec.c +2 -0
@@ -690,6 +691,15 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"

cpu-exec.c line 705

Quoted 10 lines
cpu-exec.c +7 -0
+		              "push %%ebx\n"
+		              "call *%1\n"
+			      "pop %%ebx\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");
 #else
                 T0 = gen_func();
 #endif

I just realised that the push and pop of ebx is not needed as T0 is ebx which gets overwritten in the output anyway.

cpu-exec.c +7 -0
Index: cpu-exec.c
===================================================================
@@ -690,6 +691,13 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "call *%1\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");
 #else
                 T0 = gen_func();
 #endif

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#6 lore raw
Quoted 26 lines · Adam Lackorzynski
Quoted 24 lines · Adam Lackorzynski
Quoted 2 lines · Alexander Graf

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c
===================================================================

cpu-exec.c (working copy) renamed from cpu-exec.c (revision 4276) +8 -0
@@ -690,6 +691,15 @@
              fp.ip = tc_ptr;
              fp.gp = code_gen_buffer + 2 * (1 << 20);
              (*(void (*)(void)) &fp)();
+#elif defined(__i386)
+             asm volatile ("push %%ebp\n"
+                           "push %%ebx\n"
+                           "call *%1\n"
+                           "pop %%ebx\n"
+                           "pop %%ebp\n"
+                           : "=a" (T0)
+                           : "r" (gen_func)
Quoted 8 lines

cpu-exec.c (working copy) line 705

Quoted 4 lines
cpu-exec.c (working copy) renamed from cpu-exec.c (revision 4276) +1 -0
+                           : "esi", "edi", "ecx", "edx");
 #else
                 T0 = gen_func();
 #endif

I just realised that the push and pop of ebx is not needed as T0 is ebx
which gets overwritten in the output anyway.

Sparc32 compiles, but for sparc64-softmmu target, I get compiler errors:
/src/qemu/cpu-exec.c: In function `cpu_sparc_exec':
/src/qemu/cpu-exec.c:694: error: impossible register constraint in asm' /src/qemu/cpu-exec.c:694: error: can't find a register in class ALL_REGS' while reloading `asm'

#7 lore raw
In reply to Adam Lackorzynski
Quoted 24 lines · Adam Lackorzynski
Quoted 22 lines · Adam Lackorzynski
Quoted 2 lines · Alexander Graf

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c
===================================================================

cpu-exec.c +7 -0
@@ -690,6 +691,15 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "push %%ebx\n"
+		              "call *%1\n"
+			      "pop %%ebx\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
Quoted 8 lines

cpu-exec.c line 703

Quoted 5 lines
cpu-exec.c +2 -0
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");
 #else
                T0 = gen_func();
 #endif

I just realised that the push and pop of ebx is not needed as T0 is ebx which gets overwritten in the output anyway.

Why is T0 =a then? Shouldn't =a mean "input and output on eax for T0"?

Quoted 28 lines

Index: cpu-exec.c
===================================================================

cpu-exec.c +7 -0
@@ -690,6 +691,13 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "call *%1\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");
 #else
                T0 = gen_func();
 #endif

Adam

Signature · 2 lines
Adam                 [email protected]
 Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#8 lore raw
Quoted 44 lines · Alexander Graf
Quoted 6 lines · Adam Lackorzynski
Quoted 3 lines · Adam Lackorzynski
Quoted 1 line · Alexander Graf

There was a comment from Fabrice on how to do prologues in TCG to save

/

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c

===================================================================

Quoted 14 lines
Quoted 14 lines
cpu-exec.c (working copy) renamed from cpu-exec.c (revision 4276) +8 -0
@@ -690,6 +691,15 @@
               fp.ip = tc_ptr;
               fp.gp = code_gen_buffer + 2 * (1 << 20);
               (*(void (*)(void)) &fp)();
+#elif defined(__i386)
+               asm volatile ("push %%ebp\n"
+                             "push %%ebx\n"
+                             "call *%1\n"
+                             "pop %%ebx\n"
+                             "pop %%ebp\n"
+                             : "=a" (T0)
+                             : "r" (gen_func)
Quoted 9 lines

cpu-exec.c (working copy) line 705

Quoted 5 lines
cpu-exec.c (working copy) renamed from cpu-exec.c (revision 4276) +1 -0
+                             : "esi", "edi", "ecx", "edx");
 #else
               T0 = gen_func();
 #endif

I just realised that the push and pop of ebx is not needed as T0 is ebx which gets overwritten in the output anyway.

Quoted 1 line

Why is T0 =a then? Shouldn't =a mean "input and output on eax for T0"?

GCC-Inline-Assembly-HOWTO:
"=" : Means that this operand is write-only for this instruction; the previous value is discarded and replaced by output data.

The attached version survives quick tests for both Sparc32 and Sparc64.

cpu-exec.c +17 -0
Index: qemu/cpu-exec.c
===================================================================
@@ -690,6 +690,23 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+#if (TARGET_LONG_BITS == 32)
+                asm volatile ("call *%%eax\n"
+                              : "=a" (T0)
+                              : "a" (gen_func)
+                              :  "ecx", "edx", "esi", "edi", "ebp");
+#else
+                {
+                    long temp;
+
+                    asm volatile ("call *%%eax\n"
+                                  : "=a" (temp)
+                                  : "a" (gen_func)
+                                  : "ebx", "ecx", "edx", "esi", "edi", "ebp");
+                    T0 = temp;
+                }
+#endif
 #else
                 T0 = gen_func();
 #endif
#9 lore raw
Quoted 1 line · Blue Swirl

The attached version survives quick tests for both Sparc32 and Sparc64.

Ok, I did not check for 64bit targets. So what about the following, works for me for arm, x86-32 and x86-64.

cpu-exec.c +15 -0
Index: cpu-exec.c
===================================================================
@@ -690,7 +691,22 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+#if (TARGET_LONG_BITS == 32)
+#define CLOBBER ,"edx"
 #else
+#define CLOBBER ,"ebx"
+#endif
+		asm volatile ("push %%ebp\n"
+		              "call *%1\n"
+			      "pop %%ebp\n"
+		              : "=A" (T0)
+			      : "a" (gen_func)
+			      : "ecx", "esi", "edi", "cc" CLOBBER
+			      );
+		T0 &= 0xffffffff;
+#undef CLOBBER
+#else
                 T0 = gen_func();
 #endif
                 env->current_tb = NULL;

For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#10 lore raw
Quoted 36 lines · Adam Lackorzynski
Quoted 1 line · Blue Swirl

The attached version survives quick tests for both Sparc32 and Sparc64.

Ok, I did not check for 64bit targets. So what about the following,
works for me for arm, x86-32 and x86-64.

Index: cpu-exec.c
===================================================================
--- cpu-exec.c (revision 4276)
+++ cpu-exec.c (working copy)

(quoted diff) +1 -0
@@ -690,7 +691,22 @@
 
                fp.ip = tc_ptr;
                fp.gp = code_gen_buffer + 2 * (1 << 20);
                (*(void (*)(void)) &fp)();
 +#elif defined(__i386)
 
+#if (TARGET_LONG_BITS == 32)
 +#define CLOBBER ,"edx"
  #else
 +#define CLOBBER ,"ebx"
 +#endif
 +               asm volatile ("push %%ebp\n"
 +                             "call *%1\n"
 +                             "pop %%ebp\n"
 +                             : "=A" (T0)
 +                             : "a" (gen_func)
 +                             : "ecx", "esi", "edi", "cc" CLOBBER
 +                             );
 +               T0 &= 0xffffffff;
 +#undef CLOBBER
 +#else

Review

Quoted 10 lines
(quoted diff) +0 -0
                 T0 = gen_func();
  #endif
                 env->current_tb = NULL;
 
 
 For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed.
 The strange thing is that I need to throw away the upper 32bits because
 otherwise it won't work. gen_func is defined to return just long but T0
 is unsigned long long, this seems inconsistent. The 'and' does not
 appear in 32bit targets so it does not harm there.

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

#11 lore raw
Quoted 8 lines
Quoted 7 lines
                 T0 = gen_func();

For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

Quoted 5 lines

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

Do we need to use T0 at all here? Can't we just use a normal local variable?

Paul

#12 lore raw
Quoted 9 lines · Paul Brook
Quoted 9 lines
Quoted 8 lines
                 T0 = gen_func();

For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

Quoted 8 lines
Quoted 5 lines

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

Do we need to use T0 at all here? Can't we just use a normal local variable?

I suspect T0 was used to gain extra performance, but in case of 64-bit target on 32-bit host there is this unnecessary work.

But does cpu-exec.c need to know about T0/T1/T2 at all? Can we replace exec.h include with cpu.h one?

#13 lore raw
Quoted 16 lines · Blue Swirl
Quoted 15 lines · Paul Brook
Quoted 14 lines
Quoted 8 lines
                 T0 = gen_func();

For 64bit target T0 is 64bits so "=a" does not work and "=A" is
needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

Quoted 4 lines
Quoted 2 lines

Do we need to use T0 at all here? Can't we just use a normal local variable?

I suspect T0 was used to gain extra performance,

Really? I doubt it. Especially on x86 reserving a register for a fixed purpose is almost always a bad idea.

It was used with dyngen because there was no way of directly passing information between cpu_loop and generated code. However now we have a proper code generator there is no need for this.

Quoted 3 lines

but in case of 64-bit target on 32-bit host there is this unnecessary work.

But does cpu-exec.c need to know about T0/T1/T2 at all?

I don't think so.

Quoted 1 line

Can we replace exec.h include with cpu.h one?

Currently we still need it to setup "env". However once we have proper TCG prologue/epilogue we should be able to pass env as an argument to gen_func, and have that do the setup for us.

Paul

#14 lore raw
In reply to Blue Swirl
Quoted 7 lines · Blue Swirl
Quoted 5 lines · Adam Lackorzynski

For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

Quoted 5 lines

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

So I played a bit more with this by trying out the 'and' and the tmp variable approaches. With the tmp variables the generated code looks ok whereas with the 'and' approach it looks especially scary with gcc-4.3 (gcc-3.4 looks ok). I have two versions now, one condensed and ugly and then one with separate parts for 32 and 64 targets. I think this one should be prefered.

cpu-exec.c +18 -0
Index: cpu-exec.c
===================================================================
@@ -690,7 +691,25 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+#if (TARGET_LONG_BITS == 32)
+		asm volatile ("push %%ebp\n"
+		              "call *%1\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "a" (gen_func)
+			      : "ecx", "esi", "edi", "edx", "cc");
 #else
+		unsigned long tmp;
+		asm volatile ("push %%ebp\n"
+		              "call *%1\n"
+			      "pop %%ebp\n"
+		              : "=a" (tmp)
+			      : "a" (gen_func)
+			      : "ebx", "ecx", "esi", "edi", "edx", "cc");
+		T0 = tmp;
+#endif
+#else
                 T0 = gen_func();
 #endif
                 env->current_tb = NULL;
cpu-exec.c +24 -0
Index: cpu-exec.c
===================================================================
@@ -690,7 +691,31 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+#if (TARGET_LONG_BITS == 32)
+#define CLOBBER
+#define OUTPUT T0
+#define OP
+#define OUTPUT2
 #else
+#define CLOBBER  ,"ebx"
+#define OUTPUT   *((unsigned long *)&T0)
+#define OUTPUT2  , [upperT0] "=m" (*((unsigned long *)&T0 + 1))
+#define OP       "movl $0, %[upperT0]\n"
+#endif
+		asm volatile ("push %%ebp\n"
+		              "call *%[func]\n"
+			      "pop %%ebp\n"
+			      OP
+		              : "=a" (OUTPUT) OUTPUT2
+			      : [func] "a" (gen_func)
+			      : "ecx", "esi", "edi", "edx", "cc" CLOBBER
+			      );
+#undef CLOBBER
+#undef OUTPUT
+#undef OUTPUT2
+#undef OP
+#else
                 T0 = gen_func();
 #endif
                 env->current_tb = NULL;

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#15 lore raw
Quoted 17 lines · Adam Lackorzynski
Quoted 8 lines · Blue Swirl
Quoted 5 lines · Adam Lackorzynski

For 64bit target T0 is 64bits so "=a" does not work and "=A" is needed. The strange thing is that I need to throw away the upper 32bits because otherwise it won't work. gen_func is defined to return just long but T0 is unsigned long long, this seems inconsistent. The 'and' does not appear in 32bit targets so it does not harm there.

Quoted 5 lines

This is because in this special case, T0 is not used as target CPU temporary, but instead to return next TB address. On i386 this is 32 bits, so only EAX is needed. TCG does not touch EDX, so it contains garbage. This also means that moving EDX to high word of T0 and then throwing the high word away may be slightly wasteful.

Quoted 6 lines

So I played a bit more with this by trying out the 'and' and the tmp
variable approaches. With the tmp variables the generated code looks ok whereas with the 'and' approach it looks especially scary with gcc-4.3 (gcc-3.4 looks ok). I have two versions now, one condensed and ugly and then one with separate parts for 32 and 64 targets. I think this one should be prefered.

I made a new version that does not use T0 at all. Tested on i386 and AMD64, both Sparc32 and Sparc64 work. AMD64 asm version does not seem to be necessary.

cpu-exec.c +39 -24
Index: qemu/cpu-exec.c
===================================================================
@@ -36,6 +36,7 @@
 #endif
 
 int tb_invalidated_flag;
+unsigned long next_tb;
 
 //#define DEBUG_EXEC
 //#define DEBUG_SIGNAL
@@ -273,14 +274,12 @@
             /* as some TB could have been invalidated because
                of memory exceptions while generating the code, we
                must recompute the hash index here */
-            T0 = 0;
+            next_tb = 0;
         }
     }
     return tb;
 }
 
-#define BREAK_CHAIN T0 = 0
-
 /* main execution loop */
 
 int cpu_exec(CPUState *env1)
@@ -293,7 +292,7 @@
 #endif
 #endif
     int ret, interrupt_request;
-    long (*gen_func)(void);
+    unsigned long (*gen_func)(void);
     TranslationBlock *tb;
     uint8_t *tc_ptr;
 
@@ -414,7 +413,7 @@
             }
 #endif
 
-            T0 = 0; /* force lookup of first TB */
+            next_tb = 0; /* force lookup of first TB */
             for(;;) {
                 SAVE_GLOBALS();
                 interrupt_request = env->interrupt_request;
@@ -443,13 +442,13 @@
                         svm_check_intercept(SVM_EXIT_SMI);
                         env->interrupt_request &= ~CPU_INTERRUPT_SMI;
                         do_smm_enter();
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     } else if ((interrupt_request & CPU_INTERRUPT_NMI) &&
                         !(env->hflags & HF_NMI_MASK)) {
                         env->interrupt_request &= ~CPU_INTERRUPT_NMI;
                         env->hflags |= HF_NMI_MASK;
                         do_interrupt(EXCP02_NMI, 0, 0, 0, 1);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
                         (env->eflags & IF_MASK || env->hflags & HF_HIF_MASK) &&
                         !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
@@ -463,7 +462,7 @@
                         do_interrupt(intno, 0, 0, 0, 1);
                         /* ensure that no TB jump will be modified as
                            the program flow was changed */
-                        BREAK_CHAIN;
+                        next_tb = 0;
 #if !defined(CONFIG_USER_ONLY)
                     } else if ((interrupt_request & CPU_INTERRUPT_VIRQ) &&
                         (env->eflags & IF_MASK) && !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
@@ -477,7 +476,7 @@
 	                 do_interrupt(intno, 0, 0, -1, 1);
                          stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl),
                                   ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl)) & ~V_IRQ_MASK);
-                        BREAK_CHAIN;
+                        next_tb = 0;
 #endif
                     }
 #elif defined(TARGET_PPC)
@@ -490,7 +489,7 @@
                         ppc_hw_interrupt(env);
                         if (env->pending_interrupts == 0)
                             env->interrupt_request &= ~CPU_INTERRUPT_HARD;
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_MIPS)
                     if ((interrupt_request & CPU_INTERRUPT_HARD) &&
@@ -503,7 +502,7 @@
                         env->exception_index = EXCP_EXT_INTERRUPT;
                         env->error_code = 0;
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_SPARC)
                     if ((interrupt_request & CPU_INTERRUPT_HARD) &&
@@ -520,7 +519,7 @@
 #if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
                             cpu_check_irqs(env);
 #endif
-                        BREAK_CHAIN;
+                        next_tb = 0;
 			}
 		    } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
 			//do_interrupt(0, 0, 0, 0, 0);
@@ -531,7 +530,7 @@
                         && !(env->uncached_cpsr & CPSR_F)) {
                         env->exception_index = EXCP_FIQ;
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
                     /* ARMv7-M interrupt return works by loading a magic value
                        into the PC.  On real hardware the load causes the
@@ -547,22 +546,22 @@
                             || !(env->uncached_cpsr & CPSR_I))) {
                         env->exception_index = EXCP_IRQ;
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_SH4)
                     if (interrupt_request & CPU_INTERRUPT_HARD) {
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_ALPHA)
                     if (interrupt_request & CPU_INTERRUPT_HARD) {
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_CRIS)
                     if (interrupt_request & CPU_INTERRUPT_HARD) {
                         do_interrupt(env);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #elif defined(TARGET_M68K)
                     if (interrupt_request & CPU_INTERRUPT_HARD
@@ -575,7 +574,7 @@
                            first signalled.  */
                         env->exception_index = env->pending_vector;
                         do_interrupt(1);
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
 #endif
                    /* Don't use the cached interupt_request value,
@@ -584,7 +583,7 @@
                         env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
                         /* ensure that no TB jump will be modified as
                            the program flow was changed */
-                        BREAK_CHAIN;
+                        next_tb = 0;
                     }
                     if (interrupt_request & CPU_INTERRUPT_EXIT) {
                         env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
@@ -640,13 +639,13 @@
                    spans two pages, we cannot safely do a direct
                    jump. */
                 {
-                    if (T0 != 0 &&
+                    if (next_tb != 0 &&
 #if USE_KQEMU
                         (env->kqemu_enabled != 2) &&
 #endif
                         tb->page_addr[1] == -1) {
                     spin_lock(&tb_lock);
-                    tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
+                    tb_add_jump((TranslationBlock *)(next_tb & ~3), next_tb & 3, tb);
                     spin_unlock(&tb_lock);
                 }
                 }
@@ -667,7 +666,7 @@
                 asm volatile ("ble  0(%%sr4,%1)\n"
                               "copy %%r31,%%r18\n"
                               "copy %%r28,%0\n"
-                              : "=r" (T0)
+                              : "=r" (next_tb)
                               : "r" (gen_func)
                               : "r1", "r2", "r3", "r4", "r5", "r6", "r7",
                                 "r8", "r9", "r10", "r11", "r12", "r13",
@@ -690,8 +689,24 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+                asm volatile ("push %%ebp\n\t"
+                              "call *%1\n\t"
+                              "pop %%ebp\n\t"
+                              : "=a" (next_tb)
+                              : "a" (gen_func)
+                              :  "ebx", "ecx", "edx", "esi", "edi", "cc");
+#elif defined(__x86_64__)
+                asm volatile ("push %%rbp\n\t"
+                              "call *%1\n\t"
+                              "pop %%rbp\n\t"
+                              : "=a" (next_tb)
+                              : "a" (gen_func)
+                              :  "rbx", "rcx", "rdx", "rsi", "rdi", "r8", "r9",
+                                 "r10", "r11", "r12", "r13", "r14", "r15", "cc",
+                                 "memory");
 #else
-                T0 = gen_func();
+                next_tb = gen_func();
 #endif
                 env->current_tb = NULL;
                 /* reset soft MMU for next block (it can currently
@@ -700,7 +715,7 @@
                 if (env->hflags & HF_SOFTMMU_MASK) {
                     env->hflags &= ~HF_SOFTMMU_MASK;
                     /* do not allow linking to another block */
-                    T0 = 0;
+                    next_tb = 0;
                 }
 #endif
 #if defined(USE_KQEMU)
#16 lore raw
Quoted 3 lines

I made a new version that does not use T0 at all. Tested on i386 and AMD64, both Sparc32 and Sparc64 work. AMD64 asm version does not seem to be necessary.

I guess amd64 is currently working by luck rather than by design.

You're pushing a single word to the stack, which could cause issues with stack alignment. I suggest:

sub $12,%%esp
push %%ebp
call *%1
pop %%ebp
add $12,%%esp

Likewise for amd64 you want sub $8, %%rsp, etc.

Quoted 1 line

+ : "ebx", "ecx", "edx", "esi", "edi", "cc");

You also want to add "memory" here.

cpu-exec.c line 709

Quoted 3 lines

#else

cpu-exec.c +1 -1
-                T0 = gen_func();
+                next_tb = gen_func();

I'd just make this a #error. Other host are likely to need special consideration anyway.

Other than that, looks ok to me.

Paul

#17 lore raw
In reply to Adam Lackorzynski
Quoted 75 lines · Adam Lackorzynski
Quoted 55 lines · Alexander Graf
Quoted 52 lines · Adam Lackorzynski
Quoted 33 lines · Blue Swirl
Quoted 23 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target
arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'. The segfault happens because of an invalid env->current_tb which seems
to be caused by generated code. The following code in cpu_exec

  tc_ptr = tb->tc_ptr;
  env->current_tb = tb;
  gen_func = (void *)tc_ptr;
  T0 = gen_func();
  env->current_tb = NULL;

is being compiled to the following

  mov    0x14(%ecx),%eax
  mov    %ecx,0x56c(%ebp)
  xor    %edi,%edi
  call   *%eax
  mov    %edi,0x56c(%ebp)
Quoted 8 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is
callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed
after the call and everything works fine. So might the problem be that
generated code isn't preserving edi/registers?

Quoted 3 lines

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

I just added an 'asm volatile("")' to stop reordering of instructions which of course isn't enough. The following works for me:

===================================================================

cpu-exec.c +3 -0
@@ -690,6 +691,11 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("call *%1\n"
+		              : "=a" (T0)
Quoted 8 lines

cpu-exec.c line 705

Quoted 5 lines
cpu-exec.c +2 -0
+			      : "r" (gen_func)
+			      : "esi", "edi");
 #else
                T0 = gen_func();
 #endif

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c
===================================================================

cpu-exec.c +0 -0
@@ -690,6 +691,15 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);

cpu-exec.c line 702

Quoted 10 lines
cpu-exec.c +9 -0
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "push %%ebx\n"
+		              "call *%1\n"
+			      "pop %%ebx\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");

Why don't you add ebp and ebx in the clobbered registers list (like "esi", "edi", "ecx", "edx") ?

Quoted 8 lines
cpu-exec.c +0 -0
 #else
                 T0 = gen_func();
 #endif

Adam

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#18 lore raw
Quoted 82 lines · Laurent Vivier
Quoted 80 lines · Adam Lackorzynski
Quoted 55 lines · Alexander Graf
Quoted 52 lines · Adam Lackorzynski
Quoted 33 lines · Blue Swirl
Quoted 23 lines · Adam Lackorzynski

Hi,

I've been experiencing crashes of latest svn Qemu, host ia32 and target
arm, host gcc is 'gcc version 3.4.6 (Debian 3.4.6-7)'. The segfault happens because of an invalid env->current_tb which seems
to be caused by generated code. The following code in cpu_exec

  tc_ptr = tb->tc_ptr;
  env->current_tb = tb;
  gen_func = (void *)tc_ptr;
  T0 = gen_func();
  env->current_tb = NULL;

is being compiled to the following

  mov    0x14(%ecx),%eax
  mov    %ecx,0x56c(%ebp)
  xor    %edi,%edi
  call   *%eax
  mov    %edi,0x56c(%ebp)
Quoted 8 lines

After the call edi isn't 0 anymore and gets the bogus value. As edi is
callee saved the code itself seems ok.
When I add a barrier before "env->current_tb = NULL" the xor is placed
after the call and everything works fine. So might the problem be that
generated code isn't preserving edi/registers?

Quoted 3 lines

Right. How did you make the barrier? My version (attached) just crashes, I'm not fluent on i386 assembly. Maybe your version could serve as a temporary fix.

I just added an 'asm volatile("")' to stop reordering of instructions which of course isn't enough. The following works for me:

===================================================================

cpu-exec.c +3 -0
@@ -690,6 +691,11 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("call *%1\n"
+		              : "=a" (T0)
Quoted 8 lines

cpu-exec.c line 705

Quoted 5 lines
cpu-exec.c +2 -0
+			      : "r" (gen_func)
+			      : "esi", "edi");
 #else
                T0 = gen_func();
 #endif

There was a comment from Fabrice on how to do prologues in TCG to save / restore the clobbered values. Btw, ebx gets clobbered as well.

tcg/README says that some registers are clobbered. So something like this should be safe:

Index: cpu-exec.c
===================================================================

cpu-exec.c +4 -0
@@ -690,6 +691,15 @@
 		fp.ip = tc_ptr;
 		fp.gp = code_gen_buffer + 2 * (1 << 20);
 		(*(void (*)(void)) &fp)();
+#elif defined(__i386)
+		asm volatile ("push %%ebp\n"
+		              "push %%ebx\n"
+		              "call *%1\n"
Quoted 8 lines

cpu-exec.c line 702

Quoted 5 lines
cpu-exec.c +5 -0
+			      "pop %%ebx\n"
+			      "pop %%ebp\n"
+		              : "=a" (T0)
+			      : "r" (gen_func)
+			      : "esi", "edi", "ecx", "edx");

Why don't you add ebp and ebx in the clobbered registers list (like "esi", "edi", "ecx", "edx") ?

For ebp it's more safe to use push as it depends whether the binary is compiled with frame-pointer or without. When without you can put it into the clobber list, when with you should not, we had some bad experience with this (also see gcc bugzilla #11807).
T0 is register defined to ebx, so it's not needed (the push/pop is also not needed), and also it does not work, gcc complains.

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#19 lore raw
Quoted 7 lines · Adam Lackorzynski
Quoted 2 lines

Why don't you add ebp and ebx in the clobbered registers list (like "esi", "edi", "ecx", "edx") ?

For ebp it's more safe to use push as it depends whether the binary is compiled with frame-pointer or without. When without you can put it into the clobber list, when with you should not, we had some bad experience with this (also see gcc bugzilla #11807).

Well the gcc bug is, that clobbering "ebp" is silently ignored if compiled without frame-pointer.

Bernhard Kauer