public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* What's the meaning of keyword "memory" in clobber list?
@ 2011-06-05  2:18 Parmenides
  2011-06-05  7:33 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Parmenides @ 2011-06-05  2:18 UTC (permalink / raw)
  To: gcc-help


Hi,

    For the following function from the Linux kernel, I have two
    questions:

static inline int test_and_set_bit(int nr, volatile unsigned long *
addr)
{
    int oldbit;

    __asm__ __volatile__( LOCK_PREFIX
                          "btsl %2,%1\n\tsbbl %0,%0"
                          :"=r" (oldbit),"+m" (ADDR)
                          :"Ir" (nr) : "memory");
    return oldbit;
}

1. There are two instructions in the inline assemably, namely btsl and
sbbl. But, can the only one LOCK_PREFIX ensure that the operation is
atomic?

2. The clobber list of the inline assembly contains a string of
"memory". What is the meaning of this declaration and why does the
operation need it ? After all, some other operations such as
clear_bit() (see the following code) dose not need "memory" indeed.
The declaration of "memory" appears in source code of kernel
here and there, and bothers me for a long time. So any details about
it will be appreciated.

static inline void clear_bit(int nr, volatile unsigned long * addr)
{
    __asm__ __volatile__( LOCK_PREFIX
                          "btrl %1,%0"
                          :"+m" (ADDR)
                          :"Ir" (nr));
}

Thanks!

-- 
Cogito ergo sum.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What's the meaning of keyword "memory" in clobber list?
  2011-06-05  2:18 What's the meaning of keyword "memory" in clobber list? Parmenides
@ 2011-06-05  7:33 ` Ian Lance Taylor
  2011-06-05  9:23   ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2011-06-05  7:33 UTC (permalink / raw)
  To: Parmenides; +Cc: gcc-help

Parmenides <mobile.parmenides@gmail.com> writes:

> static inline int test_and_set_bit(int nr, volatile unsigned long *
> addr)
> {
>     int oldbit;
>
>     __asm__ __volatile__( LOCK_PREFIX
>                           "btsl %2,%1\n\tsbbl %0,%0"
>                           :"=r" (oldbit),"+m" (ADDR)
>                           :"Ir" (nr) : "memory");
>     return oldbit;
> }
>
> 1. There are two instructions in the inline assemably, namely btsl and
> sbbl. But, can the only one LOCK_PREFIX ensure that the operation is
> atomic?

The sbbl instruction here is a register-to-register operation anyhow.
There would be no point to using a lock prefix on it.  It's just there
to move the result of the atomic btsl operation from the carry flag to a
register.


> 2. The clobber list of the inline assembly contains a string of
> "memory". What is the meaning of this declaration and why does the
> operation need it ? After all, some other operations such as
> clear_bit() (see the following code) dose not need "memory" indeed.
> The declaration of "memory" appears in source code of kernel
> here and there, and bothers me for a long time. So any details about
> it will be appreciated.
>
> static inline void clear_bit(int nr, volatile unsigned long * addr)
> {
>     __asm__ __volatile__( LOCK_PREFIX
>                           "btrl %1,%0"
>                           :"+m" (ADDR)
>                           :"Ir" (nr));
> }

This is documented here

http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Extended-Asm.html

A "memory" clobber means that the instruction modifies memory in a way
which is difficult to express.  As far as I can see, it is not needed in
the first example above, as the '+' constraint on the memory location
ADDR tells gcc about the modification to memory.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What's the meaning of keyword "memory" in clobber list?
  2011-06-05  7:33 ` Ian Lance Taylor
@ 2011-06-05  9:23   ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2011-06-05  9:23 UTC (permalink / raw)
  To: Parmenides; +Cc: gcc-help

Parmenides <mobile.parmenides@gmail.com> writes:

> static inline int test_and_set_bit(int nr, volatile unsigned long *
> addr)
> {
>     int oldbit;
>
>     __asm__ __volatile__( LOCK_PREFIX
>                           "btsl %2,%1\n\tsbbl %0,%0"
>                           :"=r" (oldbit),"+m" (ADDR)
>                           :"Ir" (nr) : "memory");
>     return oldbit;
> }
>
> 1. There are two instructions in the inline assemably, namely btsl and
> sbbl. But, can the only one LOCK_PREFIX ensure that the operation is
> atomic?

The sbbl instruction here is a register-to-register operation anyhow.
There would be no point to using a lock prefix on it.  It's just there
to move the result of the atomic btsl operation from the carry flag to a
register.


> 2. The clobber list of the inline assembly contains a string of
> "memory". What is the meaning of this declaration and why does the
> operation need it ? After all, some other operations such as
> clear_bit() (see the following code) dose not need "memory" indeed.
> The declaration of "memory" appears in source code of kernel
> here and there, and bothers me for a long time. So any details about
> it will be appreciated.
>
> static inline void clear_bit(int nr, volatile unsigned long * addr)
> {
>     __asm__ __volatile__( LOCK_PREFIX
>                           "btrl %1,%0"
>                           :"+m" (ADDR)
>                           :"Ir" (nr));
> }

This is documented here

http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Extended-Asm.html

A "memory" clobber means that the instruction modifies memory in a way
which is difficult to express.  As far as I can see, it is not needed in
the first example above, as the '+' constraint on the memory location
ADDR tells gcc about the modification to memory.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-06-05  7:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-05  2:18 What's the meaning of keyword "memory" in clobber list? Parmenides
2011-06-05  7:33 ` Ian Lance Taylor
2011-06-05  9:23   ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).