Process switching of the Linux kernel (top)

Source: Internet
Author: User

Hardware Context Concepts

Although each process can have its own address space, all processes must share the CPU registers. Therefore, before resuming a process execution, the kernel must ensure that each register is loaded with the value of the pending process. The data in these registers that must be mounted is called a hardware context.

Task Status Segment

80x86 Architecture:
Includes a special segment type called the task status segment (TSS), which is the function of storing the hardware context. Segment is the concept of x86, in protected mode, segment selectors participate in addressing, segment selectors in segment registers, and TSS segments in TR registers. When the process switches, it will

Intel's recommendations:
To prepare a separate TSS segment for each process, the process switches the TR register to point to the TSS segment for that process, and then uses that segment to retain all registers when the task is switched, such as interrupts that involve privileged-level switching.

Linux design:
The Linux kernel did not follow Itel's recommendation to set up a TSS for each process, but instead set a TSS,TR register for each CPU to save the segment.

Process Switching Concept Chapter

According to Intel's design approach, the hardware context of the process switch is kept in TSS, which is possible because each process has TSS, but each CPU in Linux has a TSS, and all processes share a TSS, so where does the hardware context for switching processes be stored? The answer is the process descriptor. In Linux, the process descriptor has a thread_struct type of field thread, as long as the process is switched out, the kernel saves the hardware context in this structure, which contains most of the CPU registers, but does not include EAX, EBX wait, what are the values of these general-purpose registers? Nature is in the kernel stack! Process switching occurs only in the kernel state, and all registers used by the user process are saved in the kernel stack before the switch is performed, which also includes the pair of registers for SS and esp.

Small summary:
When performing a process switchover, the hardware context is saved in two places:

    1. Task_struct's thread field: This saves most of the registers, such as ESP,EIP.
    2. Kernel stack: status register, General register, such as EAX,EBX ...

So now there's a problem, since Linux uses thread_struct to save the hardware context, why design a TSS for each CPU?

There are two reasons:

    1. When the 80x86 CPU switch from the user state to the kernel state need to find the address of the kernel stack, where to find the address? is in TSS. When the process switches, only the Esp0/ss0 field in the TSS segment is updated to point to the kernel stack of the new process.
    2. When the user-state process is accessing a port through an in or out, the CPU needs to access the I/O license stored in the TSS the power map has checked to see if the process has access to the port.

Because process switching does not replace the TSS itself, the content of TR is not replaced at all. This is because changing the cost of SS0 and ESP0 in TSS is much smaller than replacing a TSS by loading a tr. Therefore, in the Linux kernel, TSS is not a resource for a process, but a global public resource. In multiprocessor cases, although there is indeed more than one TSS in the kernel, each CPU still has only one TSS, and the Init_tass array is used to represent multiple TSS, and if there are n CPUs, the length of the array is n.

The above reasons illustrate the role of TSS in Linux. There are 2 questions that have not been articulated so far: How does Linux update the content of TSS when the process is switched? What does the THREAD_STRUCT structure look like? Here are some answers to these questions.

Performing process switching

Essentially, each process switch consists of two steps:

    1. Switch the page global catalog to install a new address space.
    2. Toggles the kernel-state stack and hardware context, because the hardware context provides all the information that the kernel needs to execute a new process, including the CPU registers.
      The first step is the contents of the process address space, which is no longer described here, interested can see in-depth understanding of the Nineth chapter of the Linux kernel, we focus on the second step, this step is performed by SWITCH_TO macro, the specific process of switching in the next introduction.

Reference article:
http://blog.csdn.net/dog250/article/details/6203529
Http://oss.org.cn/kernel-book/ch05/5.4.1.htm

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Process switching of the Linux kernel (top)

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.