Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 566 Vote(s) - 3.62 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do we need mfence when using xchg

#1
I have a set and test `xchg` based assembly lock. my question is :

Do we need to use memory fencing (`mfence`, `sfence` or `lfence` ) when using `xchg` instruction ?

Edit :

64 Bit platform : with Intel nehalem
Reply

#2
No. `xchg` is guaranteed to compile into something, that will assure consistency on the hardware level.
Reply

#3
According to Chapter 8 *Bus Locking*, of the *Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A*

> The memory-ordering model prevents loads and stores from being reordered with locked instructions that execute earlier or later.

So the locked `XCHG` instruction acts as a memory barrier, and no additional barrier is needed.
Reply

#4
As said in the other answers the lock prefix is implicit, here, so there is no problem on the assembler level. The problem may lay on the C (or C++) level when you use that as inline assembler. Here you have to ensure that the compiler doesn't reorder instructions with respect to your `xchg`. If you are using gcc (or cousins) you would typically do something like:


__asm__ __volatile__("xchgl %1, %0"
: "=r"(ret)
: "m"(*point), "0"(ret)
: "memory");

that is declare the instruction as volatile *and* add the "memory" clobber.
Reply

#5
xchg instruction has an implicit lock prefix according to Intel manuals.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through