How can engineers not be embarrassed after "falling down"

 Anker  360 cloud computing 


Heroine declaration


In the past two days, she was screened by the fall of Xi Mengyao of the Shanghai Victoria's Secret Show. In fact, the scary thing is not that she fell under the eyes of the veteran in the modeling industry, but that you lack the professional knowledge system in the technical world and face it. The embarrassment when the question is not answered. Today, the young master will teach you the basic knowledge about the Linux system kernel to consolidate the knowledge system.

PS: Rich first-line technology and diversified forms of expression are all in the " HULK first-line technology talk ", please pay attention!


 

Introduction

In our daily work with Linux, we often see user space and kernel space, as well as process context and interrupt context . Looks very familiar, but can't tell what is going on for a long time, and what is the difference. When reading a book, I often feel deceived, and I feel very uncomfortable. Today I will deepen your understanding of it and get rid of the "embarrassment" of it .


 

User space and kernel space

The operating system uses virtual memory, so for a 32-bit operating system, its addressing space (virtual storage space) is 4G (2 to the 32th power). The core of the operating system is the kernel, which is independent of ordinary applications and can access the protected memory space as well as all the permissions to access the underlying hardware devices. In order to ensure that user processes cannot directly manipulate the kernel, thereby protecting the security of the kernel, the operating system divides the virtual space into two parts, one part is the kernel space, and the other is the user space.


Kernel space:For the Linux operating system, the highest 1G byte (from the virtual address 0xC0000000 to 0xFFFFFFFF) is used by the kernel, which is called the kernel space.


User space: The lower 3G bytes (from virtual address 0x00000000 to 0xBFFFFFFF) are used by each process, which is called user space.


Each process can enter the kernel through system calls. Therefore, the Linux kernel is shared with all processes in the system. Therefore, from the perspective of a specific process, each process can have 4G bytes of virtual space. The space allocation is shown in the figure below:


image


With user space and kernel space, the entire linux internal structure can be divided into three parts, from the bottom to the top in order: hardware --> kernel space --> user space . As shown below:


image.png


Details that need to be paid attention to:

  1. The kernel code and data are stored in the kernel space, and the code and data of the user program are stored in the user space of the process. Regardless of whether it is kernel space or user space, they are all in virtual space.

  2. Linux uses a two-level protection mechanism: level 0 is for the kernel and level 3 is for user programs.


Kernel mode and user mode:

  1. When a task (process) executes a system call and gets executed in the kernel code, it is said that the process is in the kernel running state (kernel state). The processor is now executing in the kernel code with the highest privilege level (level 0). When the process is in the kernel mode, the executed kernel code will use the kernel stack of the current process. Each process has its own kernel stack.


  2. When the process is executing the user's own code, it is said to be in the user running state (user state). At this time, the processor is running in user code with the lowest privilege level (level 3). When the user program is being executed and is suddenly interrupted by the interrupt program, the user program can also be symbolically referred to as being in the kernel state of the process. Because the interrupt handler will use the kernel stack of the current process.


 

Process context and interrupt context

What is process context?

The chapter on process management in the book "Linux Kernel Design and Implementation" introduces the process context. The book says that when a program executes a system call or triggers an exception (soft interrupt), it will fall into the kernel space at this time. Time represents the execution of the process and is in the context of the process. In order to better understand, summarized as follows:

In the process of program execution, there are usually two states: user mode and kernel mode. The CPU pair in the kernel mode is further subdivided according to the context, so there are the following three states:

  1. Kernel mode runs in the context of the process, and the kernel represents the process running in the kernel space.

  2. Kernel mode, running in the interrupt context, the kernel represents the hardware running in the kernel space.

  3. User mode, running in user space.


Context: A context is simply an environment.

Application programs in user space enter the kernel space through system calls. At this time, the user space process has to pass many variables and parameter values ​​to the kernel, and some register values ​​and variables of the user process should be saved when the kernel mode is running. The so-called "process context" can be regarded as the parameters passed by the user process to the kernel, the set of variables and register values ​​to be saved by the kernel, and the environment at that time.


Relative to the process, it is the environment in which the process is executed. Specifically, each variable and data, including all register variables, files opened by the process, memory information, etc. The context of a process can be divided into three parts: user-level context, register context, and system-level context .

  1. User-level context: text, data, user stack, and shared memory area;

  2. Register context: general registers, program registers (IP), processor status registers (EFLAGS), stack pointer (ESP);

  3. System-level context: process control block task_struct, memory management information (mm_struct, vm_area_struct, pgd, pte), kernel stack.


When process scheduling occurs, process switching is context switch . The operating system must switch all the information mentioned above before the newly scheduled process can run. The system calls the mode switch (mode switch). Compared with process switching, mode switching is much easier and saves time, because the main task of mode switching is to switch the context of the process register.


What is an interrupt context?

Through the trigger signal, the hardware causes the kernel to call the interrupt handler and enter the kernel space. In this process, some variables and parameters of the hardware are also passed to the kernel, and the kernel performs interrupt processing through these parameters. The so-called "interrupt context" can actually be regarded as the parameters passed by the hardware and some other environments that the kernel needs to save (mainly the process environment that is currently interrupted execution) . When interrupted, the kernel does not run on behalf of any process. It generally only accesses the system space, but not the process space. The kernel generally does not block when executing in the interrupt context.


 

to sum up

When a process is executing, the values ​​in all registers of the CPU, the state of the process, and the contents of the stack are called the context of the process.

When the kernel needs to switch to another process, it needs to save all the state of the current process, that is, save the context of the current process, so that when the process is executed again, the state at the time of switching can be executed.

In LINUX, the current process context is stored in the task data structure of the process. When an interrupt occurs, the kernel executes interrupt service routines in the context of the interrupted process. But at the same time, all necessary resources are reserved so that the execution of the interrupted process can be resumed when the relay service ends.


Taste this article patiently and read it several times. I believe your professional knowledge system will be improved to a certain extent!



Guess you like

Origin blog.51cto.com/15127564/2667909