Skip to contentSkip to conversation

testandset asm fix

Qemu-devel Paul Brook 4 participants 7 messages Feb 6, 2005 – Feb 7, 2005

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

The inline assembly used for the x86/x86-64 host testandset routine is bogus. The operand constraints are wrong (Fails to compile at -O0).
Also the return value is incorrect. It should return 0 if the lock was successfully acquired.

Patch below fixes it. The additional sete test is unnecessary, we can just use the comparison/writeback value.

Paul

Index: exec-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/exec-all.h,v
retrieving revision 1.26

exec-all.h 6 Feb 2005 14:35:43 -0000 renamed from exec-all.h 10 Jan 2005 23:23:48 -0000 1.26 +12 -16
diff -u -p -r1.26 exec-all.h
@@ -392,28 +392,24 @@ static inline int testandset (int *p)
 #ifdef __i386__
 static inline int testandset (int *p)
 {
-    char ret;
-    long int readval;
-    
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
 #ifdef __x86_64__
 static inline int testandset (int *p)
 {
-    char ret;
-    int readval;
-    
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
#1 lore raw

m68k has similar problem

http://lists.gnu.org/archive/html/qemu-devel/2004-08/msg00122.html

Regards,

Piotrek

Quoted 63 lines · Paul Brook

The inline assembly used for the x86/x86-64 host testandset routine is bogus. The operand constraints are wrong (Fails to compile at -O0).
Also the return value is incorrect. It should return 0 if the lock was successfully acquired.

Patch below fixes it. The additional sete test is unnecessary, we can just use the comparison/writeback value.

Paul

Index: exec-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/exec-all.h,v
retrieving revision 1.26
diff -u -p -r1.26 exec-all.h

exec-all.h 6 Feb 2005 14:35:43 -0000 renamed from exec-all.h 10 Jan 2005 23:23:48 -0000 1.26 +12 -16
@@ -392,28 +392,24 @@ static inline int testandset (int *p)
 #ifdef __i386__
 static inline int testandset (int *p)
 {
-    char ret;
-    long int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
 #ifdef __x86_64__
 static inline int testandset (int *p)
 {
-    char ret;
-    int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#2 lore raw
In reply to Paul Brook

OK. Anyway, the locking in QEMU is mostly boggus. If SMP is implemented someday with host threads, then it will be the right time to correct it !

Fabrice.

Quoted 64 lines · Paul Brook

The inline assembly used for the x86/x86-64 host testandset routine is bogus. The operand constraints are wrong (Fails to compile at -O0).
Also the return value is incorrect. It should return 0 if the lock was successfully acquired.

Patch below fixes it. The additional sete test is unnecessary, we can just use the comparison/writeback value.

Paul

Index: exec-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/exec-all.h,v
retrieving revision 1.26
diff -u -p -r1.26 exec-all.h

exec-all.h 6 Feb 2005 14:35:43 -0000 renamed from exec-all.h 10 Jan 2005 23:23:48 -0000 1.26 +12 -16
@@ -392,28 +392,24 @@ static inline int testandset (int *p)
 #ifdef __i386__
 static inline int testandset (int *p)
 {
-    char ret;
-    long int readval;
-    
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
 #ifdef __x86_64__
 static inline int testandset (int *p)
 {
-    char ret;
-    int readval;
-    
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#3 lore raw

What about IRQ latency?

Piotrek

Quoted 78 lines · Fabrice Bellard

OK. Anyway, the locking in QEMU is mostly boggus. If SMP is implemented someday with host threads, then it will be the right time to correct it !

Fabrice.

Quoted 60 lines · Paul Brook

The inline assembly used for the x86/x86-64 host testandset routine is bogus. The operand constraints are wrong (Fails to compile at -O0).
Also the return value is incorrect. It should return 0 if the lock was successfully acquired.

Patch below fixes it. The additional sete test is unnecessary, we can just use the comparison/writeback value.

Paul

Index: exec-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/exec-all.h,v
retrieving revision 1.26
diff -u -p -r1.26 exec-all.h

exec-all.h 6 Feb 2005 14:35:43 -0000 renamed from exec-all.h 10 Jan 2005 23:23:48 -0000 1.26 +12 -16
@@ -392,28 +392,24 @@ static inline int testandset (int *p)
 #ifdef __i386__
 static inline int testandset (int *p)
 {
-    char ret;
-    long int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
 #ifdef __x86_64__
 static inline int testandset (int *p)
 {
-    char ret;
-    int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
Quoted 6 lines

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#4 lore raw

I didn't noticed that the patch was committed.

Piotrek

Quoted 84 lines · Piotras

What about IRQ latency?

Piotrek

Quoted 78 lines · Fabrice Bellard

OK. Anyway, the locking in QEMU is mostly boggus. If SMP is implemented someday with host threads, then it will be the right time to correct it !

Fabrice.

Quoted 60 lines · Paul Brook

The inline assembly used for the x86/x86-64 host testandset routine is bogus. The operand constraints are wrong (Fails to compile at -O0).
Also the return value is incorrect. It should return 0 if the lock was successfully acquired.

Patch below fixes it. The additional sete test is unnecessary, we can just use the comparison/writeback value.

Paul

Index: exec-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/exec-all.h,v
retrieving revision 1.26
diff -u -p -r1.26 exec-all.h

exec-all.h 6 Feb 2005 14:35:43 -0000 renamed from exec-all.h 10 Jan 2005 23:23:48 -0000 1.26 +12 -16
@@ -392,28 +392,24 @@ static inline int testandset (int *p)
 #ifdef __i386__
 static inline int testandset (int *p)
 {
-    char ret;
-    long int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
 #ifdef __x86_64__
 static inline int testandset (int *p)
 {
-    char ret;
-    int readval;
-
-    __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
-                          : "=q" (ret), "=m" (*p), "=a" (readval)
-                          : "r" (1), "m" (*p), "a" (0)
-                          : "memory");
-    return ret;
+    long int readval = 0;
+
+    __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
+                          : "+m" (*p), "+a" (readval)
+                          : "r" (1));
+    return readval;
 }
 #endif
 
Quoted 6 lines

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#5 lore raw
In reply to Fabrice Bellard
Quoted 3 lines · Fabrice Bellard

OK. Anyway, the locking in QEMU is mostly boggus. If SMP is implemented someday with host threads, then it will be the right time to correct it !

Differences in strict/loose memory ordering between different processor types will be a bigger problem for SMP emulation, IMHO. What are your plans on that?

cheers,
Lennert

#6 lore raw
Quoted 11 lines · Lennert Buytenhek
Quoted 3 lines · Fabrice Bellard

OK. Anyway, the locking in QEMU is mostly boggus. If SMP is implemented someday with host threads, then it will be the right time to correct it !

Differences in strict/loose memory ordering between different processor types will be a bigger problem for SMP emulation, IMHO. What are your plans on that?

For x86 on x86 it won't be a problem if we use the same locking instructions. For the rest, using high level locks might suffice at the expense of efficiency. In the worst case we can simply not use threads and do explicit scheduling based on a cycle counter.

Fabrice.