Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On a multicore x86, is a LOCK necessary as a prefix to XCHG?

Tags:

If mem is a shared memory location, do I need:

XCHG EAX,mem 

or:

LOCK XCHG EAX,mem 

to do the exchange atomically?

Googling this yields both yes and no answers. Does anyone know this definitively?

like image 1000
Walter Bright Avatar asked Jun 29 '10 20:06

Walter Bright


People also ask

What is the use of LOCK prefix?

The LOCK prefix is typically used with the BTS instruction to perform a read-modify-write operation on a memory location in shared memory environment. The integrity of the LOCK prefix is not affected by the alignment of the memory field. Memory locking is observed for arbitrarily misaligned fields.

What does Xchg do in assembly?

Microprocessor 8085 In 8085 Instruction set, there is one mnemonic XCHG, which stands for eXCHanGe. This is an instruction to exchange contents of HL register pair with DE register pair.


1 Answers

Intel's documentation seems pretty clear that it is redundant.

IA-32 Intel® Architecture Software Developer’s Manual Volume 3A: System Programming Guide, Part 1

7.1.2.1 says:

The operations on which the processor automatically follows the LOCK semantics are as follows:

  • When executing an XCHG instruction that references memory.

Similarly,

Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: Instruction Set Reference, N-Z

XCHG:

If a memory operand is referenced, the processor’s locking protocol is automatically implemented for the duration of the exchange operation, regardless of the presence or absence of the LOCK prefix or of the value of the IOPL.

Note that this doesn't actually meant that the LOCK# signal is asserted whether or not the LOCK prefix is used, 7.1.4 describes how on later processors locking semantics are preserved without a LOCK# if the memory location is cached. Clever, and definitely over my head.

like image 59
CB Bailey Avatar answered Sep 27 '22 20:09

CB Bailey