[Learning & amp; understanding] process Switching Based on TSS and kernel stack,

Source: Internet
Author: User

[Learning & understanding] process Switching Based on TSS and kernel stack,

In Linux0.11, process switching is based on the TSS mechanism provided by intel. to switch from a process to another process, switch the TSS structure. However, this switching method is too inefficient, so later Linux and Windows both adopt Kernel stack-based switching. Because the TSS mechanism is relatively simple, most of the energy is used to explain the kernel-based stack mechanism.

The following kernel-based stack method is implemented by modifying the TSS method of Linux0.11.

 

 

 

TSS Mechanism

 

The following is a flowchart of TSS switching found on the Internet:

 

 

 

As shown in this figure, TR (task register) is the TSS descriptor used to index the current task in GDT. It is a Selection Sub-operator. If you switch the task, you need to modify TR as well.

 

First, we need to save the environment of the current task before switching the task (that is, to save the register value in the CPU to the corresponding location of "Current TSS", eax to eax, ebx to ebx ).

 

Then find the "target TSS" and restore it (that is, set the value of the Register in the CPU to saved in the "target TSS ).

 

After the environment is restored, you need to set TR to the "target TSS" option.

 

The details of the GDT table are as follows:

 

 

 

The LDT selection child of each process is saved in the corresponding TSS. Because this mechanism takes a lot of time to save and assign values, the two operations are relatively inefficient, so this mechanism is no longer used in Linux and Windows.

 

 

 

Kernel stack Mechanism

To switch, first talk about the structure of the next process.

A process is created by its parent process calling the system call.

Each time you use fork () to create a process, a page of space (4 kb) is applied. The low-address space base is used to store the PCB of the process, the base + PAGE_SIZE is used as the bottom of the kernel stack of the process.

This stack is used to store various register values of the parent process. After all, the sub-processes generated by fork are exactly the same as the parent process (unless exec functions are called ). The registers of sub-processes are basically assigned values using the corresponding registers of the parent process (except for eax, which is the return value of fork and the value of sub-processes is 0 ).

When a parent process calls a system call to create a sub-process, it pushes its ss, esp, EFLAGS, cs, and eip to the stack, this call to the copy_process function is equivalent to passing parameters (in fact, all parameters in the function are obtained from the stack), and other registers of the parent process will be pushed into the interrupt processing function. In this case, copy_process (int nr, long ebp, long edi, long esi, long gs, long none, long ebx, long ecx, long edx, long fs, long es, long ds, the long eip, long cs, long eflags, long esp, long ss) function obtains all the required parameters (the register of the parent process ).

Press cs: The eip points to the address of the next instruction of the fork function. If a sub-process is called, The first instruction is the following one after the fork function is called. For example, if (fork () = 0), the so-called "first instruction" here is to compare the return value of the fork function with 0.

Then, the switch_to function is used to switch the process. This function passes in two parameters: the PCB address of the sub-process and the index of its ldt descriptor. This function is composed of assembly code. First, it saves a series of registers of the parent process to the kernel stack of the parent process, and also saves esp in the corresponding PCB fields of the parent process, write the kernel stack pointer of the sub-process to the esp0 field in the global TSS, and then write the kernel stack pointer of the sub-process (in the PCB) to the current esp register again, so that the ss can be used: esp is used to press the stack and exit the stack. The next step is to switch the LDT and use the lldt command. Then, set the fs register to save the Local Descriptor pointing to the user's data space (0x17 = 0001 0111b ). The last step is the continuous out-of-stack command, store all the previously created sub-processes stored in the sub-process Kernel stack to the corresponding register (at this time, the kernel stack has been switched to the sub-process ).

Here, the switchover is complete.

 

You are welcome to point out that you must correct the error in time and would like to extend your sincere thanks!

 

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.