Re: jmp 1b, how does this exit?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Thu, 13 Apr 2006 13:13:11 -0700
"Om Narasimhan" <om.turyx@xxxxxxxxx> wrote:

> >From linux-2.6.16/include/asm-i386/semaphore.h (
> http://lxr.linux.no/ident?v=2.6.10;i=down )
> 105   static inline void down(struct semaphore * sem)
> 106  {
>    might_sleep();
>    __asm__ __volatile__(
>        "# atomic down operation\n\t"
>        LOCK "decl %0\n\t"     /* --sem->count */ // locked and
> decremented. "js 2f\n" // if sign is set jump to 2:
>        "1:\n"
>        LOCK_SECTION_START("") // starts a new section in elf, what
> does this mean?
>        "2:\tlea %0,%%eax\n\t" // loads &sem (==&sem->count) into eax.
>        "call __down_failed\n\t" // this eventually returns when sem
> is acquired 116        "jmp 1b\n" // goes back to 1: ... how would
> this function ever return?
> 117         LOCK_SECTION_END
>        :"=m" (sem->count)
>        :
>        :"memory","ax");
> }


Ok, this is quite subtle. The explanation is in the
LOCK_SECTION_START() and LOCK_SECTION_END() macros. These macros puts
the code in-between in another section, far away from the current. So,
in fact, after the 1:, what you have in memory, is not the "lea", but
the next instruction after the down().

So, if you write some C code like :

 a = 2;
 down ();
 c = 1;

It will end up like this :

 - some assembly code to set a to 2
 - LOCK decl %0
 - js 2f /* Jump only in the contended case */
 - some assembly code to set c to 1

and then, far away :

 - lea %0, %%eax
 - call __down_failed

The idea is to optimize the non-contended case (when there's no
contention on the semaphore). The idea is to not trash the i-cache with
instructions that are useless most of the time, and probably to
optimize the pipeline usage by making sure that prefetched instructions
are the one that are most likely to be executed.

As this is a frequently asked questions, I think I'll contribute an
entry to the FAQ, now that the website is a Wiki.

Sincerly,

Thomas
-- 
PETAZZONI Thomas - thomas.petazzoni@xxxxxxxx 
http://{thomas,sos,kos}.enix.org - Jabber: thomas.petazzoni@xxxxxxxxx
http://{agenda,livret}dulibre.org - http://www.toulibre.org
Fingerprint : 0BE1 4CF3 CEA4 AC9D CC6E  1624 F653 CB30 98D3 F7A7

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux