From: "Richard B. Johnson" <root@chaos.analogic.com>
To: "Maciej W. Rozycki" <macro@ds2.pg.gda.pl>
Cc: Jamie Lokier <jamie@shareable.org>,
Bill Davidsen <davidsen@tmr.com>, Len Brown <len.brown@intel.com>,
Chris Friesen <cfriesen@nortelnetworks.com>,
Willy Tarreau <willy@w.ods.org>,
Alan Cox <alan@lxorguk.ukuu.org.uk>,
Arkadiusz Miskiewicz <arekm@pld-linux.org>,
Marcelo Tosatti <marcelo.tosatti@cyclades.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
ACPI Developers <acpi-devel@lists.sourceforge.net>
Subject: Re: [ACPI] Re: Linux 2.4.26-rc1 (cmpxchg vs 80386 build)
Date: Thu, 1 Apr 2004 08:17:06 -0500 (EST) [thread overview]
Message-ID: <Pine.LNX.4.53.0404010814420.15020@chaos> (raw)
In-Reply-To: <Pine.LNX.4.55.0404011423070.3675@jurand.ds.pg.gda.pl>
On Thu, 1 Apr 2004, Maciej W. Rozycki wrote:
> On Wed, 31 Mar 2004, Jamie Lokier wrote:
>
> > > Well, "cmpxchg", "xadd", etc. can be easily emulated with an aid of a
> > > spinlock. With SMP operation included.
> >
> > Nope. Len Brown wrote:
> >
> > > Linux uses this locking mechanism to coordinate shared access
> > > to hardware registers with embedded controllers,
> > > which is true also on uniprocessors too.
> >
> > You can't do that with a spinlock. The embedded controllers would
> > need to know about the spinlock.
>
> Hmm, does it mean we support x86 systems where an iomem resource has to
> be atomically accessible by a CPU and a peripheral controller?
>
> --
> + Maciej W. Rozycki, Technical University of Gdansk, Poland +
> +--------------------------------------------------------------+
> + e-mail: macro@ds2.pg.gda.pl, PGP key available +
Of course they all can be emulated. And some "embedded
controllers" don't care and won't have a clue.
The whole idea of atomic operations is to have the specific
operation complete entirely so there is no intermediate
state that can catch you with your pants down.
For instance, If I code something in 'C' as:
x++;
I would expect that the value of x after the sequence-
point is exactly one more than it was before. However,
if x happens to be a long long in ix86 machines, you
are screwed because the operation occurs in two stages.
addl $1,(x) # Low word
adcl $0,4(x) # CY into high word
If you got interrupted between the first and second operation,
AND if the result of the operation was used before it was
completed, the user gets the wrong value.
In Intel machines ALL memory operations are atomic. What
the means is that if I make code that does:
addl %eax,(memory)
... what's in memory will always be the sum of what it was
before and the value in the EAX register. However, if I
made code that did:
movl (memory), %ecx # Get value from memory
addl %eax, %ecx # Add from EAX
movl %ecx, (memory) # Put value back into memory
... such operations are not atomic even though they do the
same thing.
A long time ago, somebody invented the 'lock' instruction
for Intel machines. It turns out that the first ones locked
the whole bus during an operation. Eventually somebody looked
at that, and by the time the '486 came out, they no longer
locked the whole bus. Then somebody else said; "WTF...
Why do we even need this stuff". It was a throw-back to
early primitive machines where there were only load and
store operations in memory. All arithmetic had to be done
in registers. Now, there are only a couple instructions you
can use the lock prefix with, or you get an invalid opcode
trap, and they are really no-ops because the instruction
itself is atomic.
To make the:
movl (memory), %ecx # Get value from memory
addl %eax, %ecx # Add from EAX
movl %ecx, (memory) # Put value back into memory
... code atomic, you need only to prevent it from being
interrupted. On a non-SMP machine, it's easy.
pushf # Save flags
cli # Clear interrupt bit
movl (memory), %ecx # Get value from memory
addl %eax, %ecx # Add from EAX
movl %ecx, (memory) # Put value back into memory
popf # Restore flags (and interrupt bit)
It's a bit more complicated on SMP machines because CLI only
affects the CPU that fetched it. You need a spin-lock for
that.
Now, if my memory operand happens to be some bits that
control a machine, we are not talking about atomic operations
at all, but the order of operations. This could take a
whole chapter in a book.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.
next prev parent reply other threads:[~2004-04-01 13:19 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <A6974D8E5F98D511BB910002A50A6647615F6939@hdsmsx402.hd.intel.com>
2004-03-29 4:49 ` Len Brown
2004-03-29 0:09 ` [ACPI] " Andi Kleen
2004-03-29 5:22 ` Willy Tarreau
2004-03-29 7:01 ` Denis Vlasenko
2004-03-29 19:57 ` Willy Tarreau
2004-03-29 22:07 ` Len Brown
2004-03-30 12:56 ` [ACPI] " Alan Cox
2004-03-30 13:15 ` Richard B. Johnson
2004-03-30 14:22 ` Willy Tarreau
2004-03-30 14:48 ` Richard B. Johnson
2004-03-30 15:09 ` Willy Tarreau
2004-03-30 15:33 ` Richard B. Johnson
2004-03-30 16:14 ` Willy Tarreau
2004-03-30 16:42 ` Chris Friesen
2004-03-30 17:44 ` Len Brown
2004-03-30 18:30 ` Arkadiusz Miskiewicz
2004-03-30 20:05 ` Len Brown
2004-03-30 20:25 ` Arkadiusz Miskiewicz
2004-03-30 21:49 ` Len Brown
2004-03-30 20:08 ` Bill Davidsen
2004-03-31 11:13 ` Maciej W. Rozycki
2004-03-31 13:04 ` Bill Davidsen
2004-03-31 15:02 ` Jamie Lokier
2004-04-01 12:29 ` Maciej W. Rozycki
2004-04-01 13:17 ` Richard B. Johnson [this message]
2004-05-08 10:18 ` Denis Vlasenko
2004-04-01 20:46 ` Len Brown
2004-04-02 10:54 ` Maciej W. Rozycki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.53.0404010814420.15020@chaos \
--to=root@chaos.analogic.com \
--cc=acpi-devel@lists.sourceforge.net \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=arekm@pld-linux.org \
--cc=cfriesen@nortelnetworks.com \
--cc=davidsen@tmr.com \
--cc=jamie@shareable.org \
--cc=len.brown@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=macro@ds2.pg.gda.pl \
--cc=marcelo.tosatti@cyclades.com \
--cc=willy@w.ods.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®