🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #186

Re: Performance of xchg reg,reg

#186
From: EricP
Date: Sun, 18 Oct 2026 16:31
102 lines
5717 bytes
On 2026-Oct-17 13:31, Dan Cross wrote:
> In article <1792257076-5857@newsgrouper.org>,
> MitchAlsup  <user5857@newsgrouper.org.invalid> wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) posted:
>>
>>> In article <11au1vu$3ks5m$2@dont-email.me>,
>>> Thomas Koenig <tkoenig@netcologne.de> wrote:
>>>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>>>>> Unfortunately, the information is spread across two sections,
>>>>> and they contradict.  The description in the `LOCK` prefix
>>>>> section in the latest version of the Intel SDM includes the text
>>>>> Anton quoted, while the description of `XCHG` itself makes clear
>>>>> that the destination operand must be a memory operand for the
>>>>> locking protocol to automatically apply.  Which is correct?
>>>>
>>>> What would be the meaning of LOCK with memory operands?  I can't
>>>> think of any.
>>>
>>> Using the `LOCK` prefix without a destination operand that is a
>>> memory operand is an error, and the SDM is quite explicit about
>>> that.  In fact, it says that an explicit `LOCK` prefix on an
>>> `XCHG` a register destination operand is _not_ permitted and
>>> "may" generate an `#UD` exception.
>>>
>>> But sadly, when talking about an `XCHG` _without_ an explicit
>>> operand, it is left ambiguous; all it says is that, "The `XCHG`
>>> instruction always asserts the `LOCK#` signal regardless of the
>>> presence or absence of the `LOCK` prefix."  Devoid of the rest
>>> of the surrounding context, one could interpret that to mean
>>> that an `XCHG` _always_ forces a bus lock, irrespective of the
>>> type of its desintation operand.
>>>
>>> Why?
>>
>> Older machines performed the read, returned the read data to
>> a core and then sent the data to be written back to memory;
>> where the time between the read and the write was unknowable.
>>
>> Some newer machines take the data to be written and package it
>> up in an interconnect transaction so the read and write happen
>> on back-to-back cycles with no possibility of interference.
>> These are ATOMIC even without any bus-locking. The data read
>> out is returned to the core.
> 
> No, I get that; I mean in the case where both operands to `XCHG`
> are _register_ operands.
> 
> What I'm saying is that, as written, the bits in the `LOCK`
> description above _could_ be interpreted to mean that _any_
> `XCHG` will always assert the `LOCK#` signal, including an
> `XCHG` that does not touch memory at all.  The "Why?" was
> rhetorical, as in, "Why would the system do that?"  For example,
> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`?
> Truly I don't know, and I was just speculating.
> 
> But as I said, I don't think that is what the sentence in
> question is _actually_ saying, and I really don't think that
> `xchg %ecx, %eax` _actually_ asserts `LOCK#` (or, these days,
> has anything to do with cache coherency and memory ordering).
> 
>>>        I have no idea; as I said, I'm in the camp of going with
>>> what the description of `XCHG` itself says, and not the what the
>>> description of the `LOCK` prefix says: that sentence is _not_
>>> devoid of context, and besides, there has not been a physical
>>> `LOCK#` signal coming out of an Intel Xeon package since
>>> Nehalem.
>>>
>>> I suppose one could imagine it forcing a poor-man's barrier to
>>> prevent later loads from being reorderd before it (loads can't
>>> migrate before locked instructions).  But we've got explicit
>>> fence instructions for that, so I don't buy it.
>>>
>>> I suspect that sentence is just poorly phrased.
> 
> 	- Dan C.
> 

Intel Volume 3 System Programmers guide 10.1.2 Bus Locking
10.1.2.1 Automatic Locking is explicit: the automatic locking for XCHG
applies to memory operands only. There are a bunch of other situations
where automatic locking is asserted, such as setting the Accessed and
Modified bit on PTE's. BUT it can also depend on PTE caching attributes
and cpu model number.

The other manuals probably didn't want to worry programmers pretty little heads
with all those grotty details.

e.g.
10.1.4 Effects of a LOCK Operation on Internal Processor Caches

For the Intel486 and Pentium processors, the LOCK# signal is always asserted
on the bus during a LOCK operation, even if the area of memory being locked
is cached in the processor.

For the P6 and more recent processor families, if the area of memory being
locked during a LOCK operation is cached in the processor that is performing
the LOCK operation as write-back memory and is completely contained in a
cache line, the processor may not assert the LOCK# signal on the bus.
... blah blah blah


Technical Details
Message-ID: <nv6BS.146451$4b2.140904@fx47.iad>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx47.iad.POSTED!not-for-mail
References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11au1vu$3ks5m$2@dont-email.me> <11aunpn$8c6$1@reader2.panix.com> <1792257076-5857@newsgrouper.org> <11b0bd3$b5b$1@reader2.panix.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx47.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Performance of xchg reg,reg
Newsgroups: comp.arch
References: <2026Oct13.104347@mips.complang.tuwien.ac.at>
 <11au1vu$3ks5m$2@dont-email.me> <11aunpn$8c6$1@reader2.panix.com>
 <1792257076-5857@newsgrouper.org> <11b0bd3$b5b$1@reader2.panix.com>
Content-Language: en-CA
From: EricP <ThatWouldBeTelling@thevillage.com>
In-Reply-To: <11b0bd3$b5b$1@reader2.panix.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 102
Message-ID: <nv6BS.146451$4b2.140904@fx47.iad>
X-Complaints-To: abuse@UsenetServer.com
NNTP-Posting-Date: Sun, 18 Oct 2026 16:31:15 UTC
Date: Sun, 18 Oct 2026 12:31:13 -0400
X-Received-Bytes: 5486