Linux Kernel process switching code analysis

Source: Internet
Author: User

# Define switch_to (prev, next, last) do {

ASM volatile {"pushl % ESI/n/t"

"Pushl % EDI/n/t"

 

"Movl % ESP, % 0/n/t"

"Movl % 3, % ESP/n/t"

"Movl $ 1f, % 1/N/t"

"Pushl % 4/n/t"

"1/T"

"Popl % EBP/n/t"

"Popl % EDI/n/t"

"Popl % ESI/n/t"

: "M =" (prev-> thread. ESP), "M" (prev-> thread. EIP ),/

"B" (last)

: "M" (next-> thread. ESP), "M" (next-> thread. EIP ),

"A" (prev), "D" (next ),

"B" (prev ));

} While (0)

Both % 0 and % 1 are in the memory, which are Prev-> thread. ESP and Prev-> thread. EIP, while % 2 is combined with the register EBX and corresponds to the last value in the parameter. In the memory, % 3 and % 4 are in the next-> thread. ESP and next-> thread. EIP, % 5, % 6, and % 7 are combined with registers eax, EDX, and EBX, respectively, corresponding to Prev, next, and Prev.

, A is the current running process (prev), and B is the process to be switched (next ). The switchover process consists of four steps:

Step 1: movl % esp. % 0 stores the value in register ESP in thread. esp of process.

Step 2: grant the value of thread. esp of process B to the Register ESP. (In fact, this value is

This is the result of the first step of the previous switch from B. To return data, you must consider it carefully later .)

Step 3: movl $ 1f. % 1 1f indicates the place marked as 1 after the program. Save the address of the Code marked as 1.

To thread. EIP of.

Step 4: Press pushl % 4 to stack the thread. EIP value of process B. At this time, esp points to the stack of process B. (In fact, the thread. EIP at this time is the result of step 3 when the last switching from B, that is, the position where the number is obtained. Therefore, when any process resumes running, it must first execute the Code marked 1 .)

It should be noted that the Code behind pushl % 4 is to convert JMP _ switch_to and _ switch_to is a function. After execution, A ret operation will be performed, the first address in the stack is used as the address returned by the function, so the code will be executed in the area marked with 1.

Because the _ switch_to code is in Schedule (), and The shedule () function is in other system call functions, such as sys_exit, therefore, the system first returns to schedule () when the last switch of process B is called, and then to the system call function that calls schedule (). Finally, the system call is called in the user space, all the code returned to the place called by the system is executed. In this way, the B process is completely returned. Note that the return path is returned Based on the return address saved in B stack, so it will certainly be returned to the B process.

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.