Home  >  Article  >  System Tutorial  >  Detailed explanation of linux kernel space and user space

Detailed explanation of linux kernel space and user space

王林
王林forward
2024-02-13 21:36:151041browse

Linux drivers typically run in kernel space, but can also run in user space. Below we will analyze the concepts of kernel space and user space in detail and how to judge them.

Linux simplifies the segmentation mechanism so that the virtual address and the linear address are always consistent. Therefore, the virtual address space of Linux is also 0~4G. The Linux kernel divides this 4G byte space into two parts. The highest 1G bytes (from virtual address 0xC0000000 to 0xFFFFFFFF) are used by the kernel and are called "kernel space". The lower 3G bytes (from virtual address 0x00000000 to 0xBFFFFFFF) are used by each process and are called "user space". Because each process can enter the kernel through system calls, the Linux kernel is composed of all processes in the system. Sharing. Therefore, from the perspective of a specific process, each process can have 4G bytes of virtual space.

Linux uses a two-level protection mechanism: level 0 for the kernel and level 3 for user programs. As can be seen from the figure (the figure cannot be represented here), each process has its own private user space (0~3G). This space is invisible to other processes in the system. The top 1GB of virtual kernel space is shared by all processes and the kernel.

Detailed explanation of linux kernel space and user space

The kernel space stores kernel code and data, while the user space of the process stores the code and data of the user program. Whether it is kernel space or user space, they are all in virtual space. Although the kernel space occupies the top 1GB of each virtual space, mapping to physical memory always starts from the lowest address (0x00000000). For the kernel space, its address mapping is a very simple linear mapping. 0xC0000000 is the displacement between the physical address and the linear address, which is called PAGE_OFFSET in the Linux code.

So how to communicate between kernel space and user space? Communication is generally carried out through system calls.

How to determine whether a driver is a user-mode driver or a kernel-mode driver? What are the criteria for judgment?

User space mode drivers generally complete access to hardware through system calls, such as mapping the driver's IO space to user space, etc. Therefore, the main basis for judgment is system calls. In addition, the linked list in user mode is different from the linked list in kernel mode. User mode uses printf and kernel mode uses printk. Each application space in user mode is virtual and relatively independent, but it is not independent in kernel mode, so programming needs to be very careful. Of course, communication between user-mode and kernel-mode programs can be accomplished through ioctl, sysfs, proc, etc.

Kernel mode and user mode

When a task (process) executes a system call and is trapped in the kernel code for execution, we say that the process is in the kernel running state (or simply called the kernel state). At this time, the processor is executing in the kernel code with the highest privilege level (level 0). When a process is in kernel mode, the executed kernel code uses the kernel stack of the current process. Each process has its own kernel stack. When a process is executing the user's own code, it is said to be in the user running state (user state). That is, the processor is running in user code with the lowest privilege level (level 3). When a user program is being executed and is suddenly interrupted by an interrupt program, the user program can also be symbolically said to be in the kernel state of the process. Because the interrupt handler will use the current process's kernel stack. This is somewhat similar to the state of a process in kernel mode.

Process context and interrupt context

The processor is always in one of the following states:

1. Kernel state, running in the process context, the kernel represents the process running in the kernel space;

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

3. User mode, running in user space.

User space applications enter the kernel space through system calls. At this time, the user space process needs to pass many variables and parameter values ​​to the kernel. When the kernel mode is running, it also needs to save some register values ​​and variables of the user process. The so-called "process context" can be seen as the parameters passed by the user process to the kernel, as well as the complete set of variables and register values ​​​​to be saved by the kernel and the environment at that time.

The hardware triggers the signal, causing the kernel to call the interrupt handler and enter the kernel space. During this process, some variables and parameters of the hardware must also be passed to the kernel, and the kernel uses these parameters to perform interrupt processing. The so-called "interrupt context" can actually be regarded as these parameters passed by the hardware and some other environments that the kernel needs to save (mainly the process environment that is currently interrupted).

The above is the detailed content of Detailed explanation of linux kernel space and user space. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete