qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

cmpxchg.c (911B)


      1 #include <assert.h>
      2 
      3 static int mem;
      4 
      5 static unsigned long test_cmpxchgb(unsigned long orig)
      6 {
      7   unsigned long ret;
      8   mem = orig;
      9   asm("cmpxchgb %b[cmp],%[mem]"
     10       : [ mem ] "+m"(mem), [ rax ] "=a"(ret)
     11       : [ cmp ] "r"(0x77), "a"(orig));
     12   return ret;
     13 }
     14 
     15 static unsigned long test_cmpxchgw(unsigned long orig)
     16 {
     17   unsigned long ret;
     18   mem = orig;
     19   asm("cmpxchgw %w[cmp],%[mem]"
     20       : [ mem ] "+m"(mem), [ rax ] "=a"(ret)
     21       : [ cmp ] "r"(0x7777), "a"(orig));
     22   return ret;
     23 }
     24 
     25 static unsigned long test_cmpxchgl(unsigned long orig)
     26 {
     27   unsigned long ret;
     28   mem = orig;
     29   asm("cmpxchgl %[cmp],%[mem]"
     30       : [ mem ] "+m"(mem), [ rax ] "=a"(ret)
     31       : [ cmp ] "r"(0x77777777u), "a"(orig));
     32   return ret;
     33 }
     34 
     35 int main()
     36 {
     37   unsigned long test = 0xdeadbeef12345678ull;
     38   assert(test == test_cmpxchgb(test));
     39   assert(test == test_cmpxchgw(test));
     40   assert(test == test_cmpxchgl(test));
     41   return 0;
     42 }