For reasons to long and boring to mention here, I need to be able to call the 
128-bit atomic compare-and-exchange operator on an amd64 platform.

Having no previous experience with in-line assembler etc, I cribbed something 
together based on a few examples on the net and on the os sourcecode 
repository. Initially I just wanted to create my own version of the 
atomic_cas_64 call (on the basis that this would be a simpler case to debug, 
and then I could easily 'upgrade' to the 128-bit version).

What I have at the moment is this assembler:

[b]testasm.il:[/b]

.inline matt_cas_64,0
        movq    %rsi, %rax
        lock
        cmpxchgq %rdx, (%rdi)
        ret
.end

and this c code:

[b]test.c:[/b]

#include <stdio.h>
#include <sys/types.h>

uint64_t matt_cas_64(uint64_t volatile*, uint64_t, uint64_t);

int main(int argc, char* argv[])
{
    volatile uint64_t foo = 12;
    uint64_t bar = 21;
    
    if (12 == matt_cas_64(&foo, 12, bar))
    {
        printf("Success: %ld\n", foo);
    }
    else
    {
        printf("Failure\n");
    }
    
    return 0;
}

Compile and link (using Studio11) thusly:

[b]cc -xarch=amd64 testasm.il test.c[/b]

to get a binary which dies unpleasantly when run:

> dbx a.out
Reading a.out
Reading ld.so.1
Reading libc.so.1
(dbx) run
Running: a.out 
(process id 16963)
signal SEGV (no mapping at the fault address) in (unknown) at 0xc
0x000000000000000c:     <bad address 0xc>
(dbx) where
  [1] 0xc(0xfffffd7fffdff870, 0xc, 0x15, 0x0, 0xc, 0x0), at 0xc 

So, something is clearly very wrong with my assembler or how I'm trying to use 
it. Can anyone point me in the right direction?

Thanks,

Matt.
 
 
This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
[EMAIL PROTECTED]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to