Linux Kernel process switching (on)

Source: Internet
Author: User

Linux Kernel process switching (on)
Hardware context Concept

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

Task status segment

80X86 architecture:
It includes a special segment type, called the task status segment (TSS), to store the hardware context. Segment is the concept of x86. In protection mode, segment delimiters participate in addressing. segment delimiters are in segment registers, while tss segments are in tr registers. When the process is switched

Intel suggestions:
Prepare an independent tss segment for each process. During process switchover, switch the tr register to point it to the corresponding tss segment of the process, and then perform task switchover (for example, interruption involving privileged-level switchover) use this segment to keep all registers.

Linux design:
The Linux kernel does not follow the Itel recommendation to set a TSS for each process. Instead, a TSS is set for each CPU, and the tr register saves the segment.

Process switching concepts

According to Intel's design philosophy, the hardware context of process switching should be stored in TSS. This is acceptable because every process has TSS. However, in Linux, each CPU has a TSS, if all processes share one TSS, what is the hardware context protection during process switching? The answer is in the Process descriptor. In linux, the process descriptor has a thread_struct field thread. As long as the process is switched out, the kernel keeps the hardware context in this structure, this structure contains most of the CPU registers, but does not include General registers such as eax and ebx. where are their values? Naturally, it's in the kernel stack! Process switching only occurs in the kernel state. Before the switch is executed, all registers used by the user process are stored in the kernel stack, which also includes the ss and esp registers.

Summary:
During process switching, the hardware context is saved in two places:

Thread field of task_struct: Most registers, such as esp and eip, are saved here. Kernel state Stack: Status Registers, General registers, such as eax, ebx...

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

There are two reasons:

When the 80x86 CPU is switched from the user mode to the kernel mode, you need to find the address of the kernel mode stack. Where can I find the address? Is in TSS. During process switching, only the esp0/ss0 field in the tss segment is updated to point it to the kernel stack of the new process. When a user-mode process apprentice accesses a port through an in or out, the CPU needs to access the I/O permission bitmap stored in the TSS to check whether the process has the right to access the port.

Because process switching does not replace the TSS itself, that is, the TR content is not replaced at all. This is because the overhead of changing SS0 and ESP0 in TSS is much smaller than that of replacing a TSS by loading TR. Therefore, in the Linux kernel, TSS is not a process resource, but a global public resource. In the case of multi-processor, although there are indeed multiple TSS in the kernel, each CPU still has only one TSS, And the init_tass array is used to represent multiple TSS. If there are N CPUs, the length of the array is N.

The above reason illustrates the role of TSS in Linux. So far, there are two questions that have not been elaborated clearly, that is, how does Linux update tss content during process switching? What does the thread_struct struct look like? Here are the answers to these questions.

Execute Process Switching

Essentially, each process switchover consists of two steps:

Switch the global directory of the page to install a new address space. Switch the kernel state stack and hardware context because the hardware context provides all the information required for the kernel to execute a new process, including the CPU register.
The first step is the content of the process address space. I will not describe it here. If you are interested, please refer to Chapter 9 of Linux kernel. We will focus on step 2, this step is executed by the switch_to macro. The specific process of switching is described in the next article.

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.