Linux kernel: Process switching

Source: Internet
Author: User

Processes are one of the core concepts of modern operating systems and are used to allocate the use of systems (CPU, memory) resources. Understanding the Linux process and process switching knowledge, first to understand the difference between the process and the program, the process is the execution flow, is a dynamic concept, the program is a collection of data and instruction sequence, is a static concept. It is important to understand that the process acts as a dynamic execution flow and can freely select a program (as long as there are permissions) with the EXECV system call. In the third chapter of the book, "process", there are two places that are more difficult to understand.

1 the last parameter of the SWITCH_TO macro

When discussing switch_to macro in the book (page 110th), it is mentioned that the macro has 3 parameters: Prev,next and last. The first two are the current process descriptor address and the address of the process descriptor to be switched, I believe that there is no doubt about these two parameters, Prev is obtained from the present, and next is the schedule () function in accordance with the scheduling algorithm from the process waiting queue selected. The key is the third last parameter, why do you need such a parameter? The description in the book is rather difficult to understand. It means that when a switches to B, prev=a,next=b, after a certain amount of time, a is re-dispatched to the CPU to execute, a needs to know which process to switch from and need to get from the last parameter. In fact, we just need to focus on the a->b process to understand the use of the last parameter. Below we use the picture to record each step:

(1) Before the process switch, a is the current process, the ESP register points to a kernel stack, prev,next these two local variables are stored in the stack, that is, in the kernel stack of a. So does the kernel stack of B have these two parameters? Of course, because B is in the waiting queue, it is likely that B has experienced the process of switching out of other processes, and in that process, the kernel stack of B also holds these two variables (if B is a newly created process, you can press two values into the kernel stack at creation time or in the schedule function). But these two values must be different from the prev=a,next=b in a, because in that process, B is switched, so at this point, the kernel stack of B should be prev=b.

In order to switch to the B process execution, the prev parameter is correct, it is necessary to use the third parameter last. In the schedule function (it picks B, and of course the address of the B process descriptor), it gets the address of the kernel stack of b from the process descriptor of B (the Thread_info parameter in the book, 3.2.54 version of the code is changed to the stack parameter, the principle is the same), The address of the prev parameter of B is obtained and passed to the SWITCH_TO macro as the third parameter. The SWITCH_TO macro also loads the process descriptor address of a into the EAX register, and the contents of the EAX register do not change during process switching.

(2) The execution process switch, mainly is the kernel stack switch, because the kernel implementation, the THREAD_INFO structure and the kernel stack together, the ESP changed, the current parameter obtains the present process descriptor address also changes. At this point, the current process becomes a B-process and works on the kernel stack of B. Note that the prev parameter of the B kernel stack is still incorrect, and it points to B.

(3) Copy the contents of the EAX register to the memory pointed to last, which is the address of the Prev parameter of the B kernel stack. Thus, the Prev parameter on the B core stack points to the correct address of the a process descriptor.

2 process stack during process switching

In the book's description of the process switch, the description of the process stack is fragmented and easy for the person to paste. Stacks are important data structures in the process and play a central role in function calls, and a detailed description of the stack can be found in the in-depth understanding of computer systems. The following describes the evolution of the process stack during process switching.

The Linux process has two kinds of stacks, user stacks and kernel stacks, which are used in different memory areas, user stacks in the user state, in the user address space allocation (0~3G), kernel stacks in the kernel state, and in the kernel address space allocation (3G~4G). The user stack is primarily used for function calls and for storing local variables, and the kernel stack also saves the process of switching additional information, such as universal registers. Whether it is the user stack or the kernel stack, the CPU is to use the ESP register to save the top address of the stack, so long before the process switch, the process into the kernel state, the user stack needs to be switched out, the entire switching process, is working on the kernel stack, so the user stack and process switching is irrelevant. On the other hand, in the implementation of the kernel, the THREAD_INFO structure and the kernel stack are put together, the kernel stack has changed, the current parameter will be changed by the present process descriptor address, so the process switching is done by the kernel stack switch. The entire process switch can be divided into three parts, and the following assumption switches from process A to process B:

(1) The kernel state of the user-state-->a of a

This process is implemented by interrupts, exceptions, or system calls, which are described later in the book and discussed later. There are only a few points to be discussed here, each time you switch from the user state to the kernel, the kernel stack is emptied, the ESP points directly to the bottom of the kernel stack, and the information from the user stack is saved to the kernel stack. The design of the empty kernel stack is estimated to take into account the user-state operation, the previous kernel stack call information is useless, there is no need to save, after all, the kernel stack only allocated 8 K or 4 K space. So, before switching to the kernel, how does the kernel know the kernel stack address of the process, although the process descriptor holds the address of the kernel stack (the stack variable), but the process descriptor is in the dynamic state, the efficiency of reading from memory is too low. Implementation, it is obtained from the TSS.

The "Task Status section" section of the Book (page 108th) gives a more detailed description of TSS, with one tss,cpu per CPU to quickly access it. One of the most important functions of TSS is for the CPU to read the kernel stack address when the user state is switched to the kernel state, which is the Init_tss[cpu]->sp0 field (the 3.2.54 version of the Code), which in fact stores the stack-bottom address, so that when loaded into the ESP, the kernel stack is emptied simultaneously.

(2) kernel State of the kernel state of a->b

In this phase, the kernel stack switching between processes is implemented, while the process switching is also implemented. The most closely related to this process is the thread variable of the task_struct, the type of the thread variable is thread_struct, which can be called the thread descriptor, which holds the hardware context of the process switch (page 109th of the book). The switch_to and __switch_to functions in the book describe each step of the process switching process in detail, related to the kernel stack:

    • Save A's kernel stack top address, which is the contents of the ESP register to A_TASK->THREAD->SP. (The 3rd step of switch_to, the variable name is based on the code in version 3.2.54)
    • Load the B_TASK->THREAD->SP content into the esp. (The 4th step of switch_to, this step completes the kernel stack switch)
    • Load the b_task->thread->sp0 into the Init_tss[cpu]->sp0 field (step 3rd of __switch_to), which corresponds to the description of (1), and later B during operation, when the user state switches to the kernel State, The ESP register always gets the address of the kernel stack from the Init_tss[cpu]->sp0 field, which clears the kernel stack contents at the same time. (thread_struct structure has SP0,SP1 variable, sp0 save kernel stack bottom address, SP save stack top address).

(3) B's kernel state->b user state

Perform the reverse process with (1), remove the user stack information from the kernel stack (1), load the corresponding register, switch to the user stack, the kernel stack information does not have to be saved, because (2) the bottom of the stack is saved, and the next time you enter the kernel stack, it will be loaded into the ESP register (the bottom of the stack The following chapters in this process are also described in detail.

Linux kernel: Process switching

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.