View previous topic :: View next topic |
Author |
Message |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Wed Aug 24, 2016 6:21 am Post subject: [Assembly] LOCK Prefix with CMPXCHG8B doesn't work properly |
|
|
Been some time
anyway.. I started messing around with atomic operations and tried to use the instruction CMPXCHG8B to modify a 8 byte memory buffer
important to say that I'm working on linux here
so I was trying to write my own InterlockedCompareExchange, and here's the code I'v come up with
Code: |
static
int64_t
interlocked_compare_exchange(
volatile int64_t m64, /* where to write atomic */
int64_t xchg_value
)
{
__asm__("mov esi,dword ptr %0;"::"m"(xchg_value));
__asm__("mov eax,dword ptr [esi];");
__asm__("mov edx,dword ptr [esi+4];");
__asm__("mov esi,dword ptr %0;"::"m"(xchg_value));
__asm__("mov ebx,dword ptr [esi];");
__asm__("mov ecx,dword ptr [esi+4];");
__asm__("mov esi,dword ptr %0;"::"m"(m64));
__asm__("lock;");
__asm__("cmpxchg8b qword ptr [esi];");
return 0;
}
|
as you can see I have no compare value since I want to be sure that the new value will always be written to the memory.
but as the CMPXCHG8B instruction is being executed nothing's happening and the memory remains the same.
any suggestion? ideas?
I'm quite new to atomic operations
Thanks
_________________
Stylo |
|
Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4702
|
Posted: Wed Aug 24, 2016 1:33 pm Post subject: |
|
|
Intel's Instruction Set Reference wrote: | CMPXCHG8B m64 - Compare EDX:EAX with m64. If equal, set ZF and load ECX:EBX into m64. Else, clear ZF and load m64 into EDX:EAX |
EAX = lower 4 bytes of the m64 [xchg_Value]
EDX = upper 4 bytes of the m64 [xchg_Value]
EBX = EAX
ECX = EDX
If EDX:EAX == [m64], then nothing will change because ECX:EBX == EDX:EAX.
If EDX:EAX != [m64], then EDX:EAX = [m64]. I'm guessing you're not using EDX:EAX after the fact.
What are you trying to do with that function?
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Thu Aug 25, 2016 7:42 am Post subject: |
|
|
I'm familiar with the syntax of that instruction
all I'm trying to do is to write atomically 8 bytes into memory using that instruction
So what I'm actually doing is using the same value for exchanging and comparing so no matter what the result will be it will change the value located in m64
I believe I should flush the instruction cache but I have absolutely no idea how to do it.
_________________
Stylo |
|
Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4702
|
Posted: Thu Aug 25, 2016 8:38 am Post subject: |
|
|
But letting ECX:EBX equal EDX:EAX means that m64 is never going to change regardless of what values you put in. I don't believe this has anything to do with the lock prefix.
If you want to always write ECX:EBX into that m64, then let EDX:EAX equal the m64.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
|
Back to top |
|
 |
|