Analysis of Cmpxchg function of Linux Kernel

Source: Internet
Author: User
Tags documentation volatile

Recently saw the Linux Kernel Cmpxchg code, the implementation very does not understand. I read the inline assembly and Intel development documentation on the Internet, and then slowly understood it and recorded it for developers who were as confused as I am. In fact, the principle of atomic operation implemented by CMPXCHG has long been known:

Cmpxchg (void* ptr, int old, int new), if the value of PTR and old is the same, writes new to PTR memory, otherwise returns the value of PTR, the entire operation is atomic. Under the Intel platform, the lock Cmpxchg is used, and the lock is a personal understanding of locking the memory bus so that if another thread wants to access the memory of PTR, it will block.

OK, let's look at the Linux kernel in the CMPXCHG (on the Internet, I did not find the corresponding header file on the machine, it is said in Include/asm-i386/cmpxchg.h) to achieve:

/* todo:you should use modern GCC atomic instruction Builtins instead of this. */#include <stdint.h> #define CMPXCHG (PTR, _old, _new) {   volatile uint32_t *__ptr = (volatile uint32_t *) (PTR);     uint32_t __ret;                                       ASM volatile ("lock; Cmpxchgl%2,%1 "               :" =a "(__ret)," +m "(*__ptr)                    :" R "(_new)," 0 "(_old)                         :" Memory ");   );                                               __ret;                                         }
Mainly to understand the inline assembly, C's inline assembly format is

ASM (Assembler template    : Output operands                   (optional)    : input operands                    (optional)    : clobbered Registers list          (optional)    );
Output operands and Inpupt operands specify parameters, which are arranged from left to right, with ', ' split, numbering starting with 0. Take CMPXCHG compilation as an example, (__ret) corresponds to 0, (*__PTR) corresponds to 1, (_new) corresponds to 2, (_old) corresponds to 3, if "%2" is used in the assembly, then refers to _new, "%1" refers to (*__ptr).

"=a" is said to write the results into the __ret, and to use the EAX register, so the last time to write the result is the operation is mov eax, ret (Eax==>__ret). "R" (_new) is to be used to read the value of _new into a common register.

In Cmpxchg, note "0" (_old), this is the place to confuse me, it's like telling You (_old) and the No. 0 operand to use the same register or memory, i.e. (_old) is stored in the same place as the number No. 0 operand. In Cmpxchg, that is to say _old and __ret use the same registers, and __ret use the Register is EAX, so _old also use EAX.

Understand this, and then look at Cmpxchgl, in the Intel development documentation, says:

0F b1/r CMPXCHG r/m32, R32 MR Valid valid* Compare EAX with R/m32. If equal, ZF is set
And R32 is loaded into r/m32. Else, Clear ZF
and load R/m32 into EAX.

Translation:

Compare the value of the EAX and the destination operand (the first operand), if the same, the ZF flag is set, and the value of the source operand (the second operation) is written to the destination operand, otherwise, the ZF flag, and the value of the purpose operand is written back to EAX.

Well, the above sentence on the CMPXCHG is:

Compare the values of _old and (*__PTR), if the same, the ZF flag is set, while the value of _new is written to (*__PTR), otherwise, the ZF flag, and the value of (*__PTR) is written back to _old.

It is clear that we are in accord with our understanding of CMPXCHG.

Another: The Intel Development manual says lock is to allow the CPU to use memory exclusively.

Linux Kernel cmpxchg function Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.