Home >Operation and Maintenance >Linux Operation and Maintenance >In what space does the linux driver run?
Linux driver runs in the "kernel" space. Generally, drivers call kmalloc() to allocate memory for data structures, call vmalloc() to allocate data structures for active swap areas, allocate buffers for some I/O drivers, or allocate space for modules; kmalloc and vmalloc allocate kernel memory.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux driver runs in the "kernel" space.
For generally written microcontroller programs, applications and drivers are often mixed. Microcontroller programmers with a certain level of ability can realize the layering of applications and drivers. In Linux systems, applications and drivers have been forced to be layered.
In the microcontroller program, the application can directly operate the underlying registers. However, such behavior is prohibited in Linux systems. For example: The writer of a Linux application deliberately calls the power management driver in the application and shuts down the system. Isn't it worth the gain?
The specific Linux application calls the driver as shown in the figure:
The application runs in the user space, and the driver runs in the kernel space. If an application in user space wants to operate the kernel, it must use a "system call" method to enter the kernel space from user space and operate the underlying layer.
The kernel is also a program and should also have its own virtual memory space. However, as a program that serves user programs, the kernel space has its own characteristics.
The relationship between kernel space and user space
In a 32-bit system, the maximum virtual space of a program can be 4GB, then the maximum The straightforward approach is to regard the kernel as a program so that it has the same 4GB space as other programs. However, this approach will cause the system to continuously switch the page table of the user program and the kernel page table, thus affecting the efficiency of the computer. The best way to solve this problem is to divide the 4GB space into two parts: one part is user space and the other part is kernel space. This can ensure that the kernel space is fixed and unchanged, and when the program switches, only the The program's page table. The only disadvantage of this approach is that both kernel space and user space become smaller.
For example: On a 32-bit hardware platform such as i386, Linux defines a constant PAGE_OFFSET in the file page.h:
#ifdef CONFIG_MMU #define __PAGE_OFFSET (0xC0000000) //0xC0000000为3GB #else #define __PAGE_OFFSET (0x00000000) #endif #define PAGE_OFFSET ((unsigned long)__PAGE_OFFSET)
Linux uses PAGE_OFFSET as the boundary The 4GB virtual memory space is divided into two parts: the low address space at addresses 0~3G-1 is user space, with a size of 3GB; the high address space at addresses 3GB~4GB-1 is kernel space, with a size of 1GB.
When multiple programs are running in the system, the relationship between multiple user spaces and kernel space can be expressed as follows:
As shown in the figure As shown, programs 1, 2...n share the kernel space. Of course, sharing here refers to time-sharing, because at any time, for a single-core processor system, only one program can be running.
The overall layout of the kernel space
In the development process of Linux, with the update of hardware equipment and the improvement of technical level, its kernel space The development of the layout is also a continuous patching process. The consequence of this is that the kernel space is divided into several different areas, and different areas have different mapping methods. Usually, people think that the Linux kernel space has three areas, namely DMA area (ZONE_DMA), normal area (ZONE_NORMAL) and high-end memory area (ZONE_HIGHMEM).
The actual physical memory configured in early computers is usually only a few MB, so In order to improve the speed of the kernel accessing the physical address memory through the virtual address, the virtual address and the physical memory address of the kernel space adopt a fixed mapping method that corresponds one to one from the low address to the high address. As shown in the figure below Display:
It can be seen that This fixed mapping method makes the relationship between virtual address and physical address very simple, that is, the kernel virtual address and the actual physical address There is only a fixed offset PAGE_OFFSET in value, so when the kernel uses the virtual address to access the physical page frame, it only needs to subtract PAGE_OFFSET from the virtual address to get the actual physical address, which is faster than using the page table. many!
Since this approach is almost to directly use the physical address, this kind of kernel space based on a fixed mapping method is also called "physical memory space", or physical memory for short. In addition, Since the fixed mapping method is a linear mapping, this area is also called the linear mapping area.
Of course, in this case (when the actual physical memory of the computer is small), the kernel fixed mapping space only occupies a part of the entire 1GB kernel space. For example: When configuring an x86 computer system with 32MB of actual physical memory, the fixed mapping area of the kernel is the 32MB space of PAGE_OFFSET~ (PAGE_OFFSET 0x02000000). So what to do with the remaining kernel virtual space in the kernel space?
Of course, physical memory is still used in the non-linear mapping of page tables according to the management method of ordinary virtual space. Specifically, the fixed mapping area is removed from the entire 1GB kernel space, and then an 8MB isolation area at the beginning is removed from the remaining part. The remainder is an ordinary virtual memory mapping area that is mapped in the same way as user space. In this area, there is not only no fixed mapping relationship between virtual addresses and physical addresses, but also dynamic memory is obtained by calling the kernel function vmalloc(), so this area is called the vmalloc allocation area, As shown in the figure below:
For an x86 computer system configured with 32MB of actual physical memory, the starting position of the vmalloc allocation area is PAGE_OFFSET 0x02000000 0x00800000.
Let me explain here: The fixed mapping between the kernel space and the physical page frame mentioned here is essentially a "predetermination" of the kernel page to the physical page frame. It does not mean that these pages "occupy" the physical page frame. ” these physical page frames. That is, the virtual page is bound to the physical page frame only when the virtual page actually needs to access the physical page frame. Normally, when a physical page frame is not used by its corresponding virtual page, the page frame can be used by user space and the kernel kmalloc allocation area introduced later.
In short, in a system with smaller actual physical memory, the size of the actual memory is the boundary between the physical memory area of the kernel space and the vmalloc allocation area.
For the entire 1GB kernel space, people also call the 16MB at the head of the space DMA Zone, that is, ZONE_DMA zone, because in the past hardware fixed the DMA space in the lower 16MB space of physical memory; the remaining zones are called normal zones, that is, ZONE_NORMAL.
High-end memory of the kernel space
With the development of computer technology, the actual physical memory of the computer is becoming more and more Large, so that the kernel fixed mapping area (linear area) becomes larger and larger. Obviously, if no restrictions are imposed, when the actual physical memory reaches 1GB, the vmalloc allocation area (non-linear area) will no longer exist. Therefore, the previously developed kernel code that called vmalloc() is no longer available. Obviously, in order to be compatible with earlier kernel codes, this is not allowed.
The following figure shows the situation faced by this kernel space:
Obviously, The reason for the above problems is that the actual situation was not anticipated Physical memory can exceed 1GB, so there is no limit set on the boundaries of the kernel's fixed mapping area, which is allowed to grow with the actual physical memory.
The way to solve the above problem is: Limit the upper limit of the fixed mapping area of the kernel space so that it cannot increase arbitrarily with the increase of physical memory. Linux stipulates that the upper boundary value of the kernel mapping area cannot be greater than a constant high_menory that is less than 1G. When the actual physical memory is large, 3G high_memory is used as the boundary to determine the physical memory area.
For example: For x86 systems, the value of high_memory is 896M, so the remaining 128MB of 1GB kernel space is a non-linear mapping area. This ensures that in any case, the kernel has enough non-linear mapping area to be compatible with early code and can access more than 1GB of actual physical memory in a normal virtual memory manner.
In other words, the most basic idea of high-end memory: borrow a section of address space to establish a temporary address mapping, and release it after use. When this address space is reached, it can be recycled and access all physical memory. When the computer has a large physical memory, the schematic diagram of the kernel space is as follows:
Traditionally, Linux calls this part of the kernel space 3G high_memory~4G-1 High-end memory zone (ZONE_HIGHMEM).
To summarize: In the kernel space of the x86 structure, the three types of areas (calculated from 3G) are as follows:
According to different application targets, high-end memory is divided into vmalloc area, persistent mapping area and temporary mapping area. The layout of high-end memory in the kernel space is as shown below:
The mapping method of vmalloc mapping area is exactly the same as that of user space. The kernel can obtain memory in this area by calling the function vmalloc(). The function of this function is equivalent to malloc() in user space. The provided memory space is continuous at the virtual address (note that the physical address is not guaranteed to be continuous).
In the persistent kernel mapping area, you can establish a long-term mapping between the physical page frame and the kernel virtual page by calling the function kmap(). This space is usually 4MB and can map up to 1024 page frames. The number is relatively rare. Therefore, in order to enhance the turnover of page frames, the function kunmap() should be called in time to release the physical page frames that are no longer used.
linux user space and kernel space-detailed explanation of high-end memory.
Kernel memory allocation modifier gfp
#In order to make the necessary description of the request in the kernel memory request function, Linux defines many A memory allocation modifier gfp. They are behavior modifiers, area modifiers, and type modifiers.
Description | |
The allocator can sleep | |
The allocator can access the emergency buffer pool | |
The allocator can start disk IO | |
The allocator can start file system IO | |
The allocator should use page frames that are about to be evicted from the cache | |
The allocator does not issue WARNING | |
Reallocation on allocation failure | |
Reallocation on allocation failure, Until successful | |
No reallocation of the |
#The area modifier indicates which area of the kernel space needs to allocate memory. By default, the memory allocator starts from ZONE_NORMAL in the kernel space and gradually allocates the memory area to the memory requester. If the user specifically needs to obtain memory from ZONE_DMA or ZONE_HOGNMEM, then the memory requester needs to use the following two in the memory request function. Area modifier description:
Description | ||
Allocate memory from ZONE_DMA area | ||
Allocate memory from ZONE_HIGHMEM area |
Kernel space (3G~4G) |
High-end memory (3G high_memory ~4G) ZONE_HIGHMEM Nonlinear mapping area |
Temporary mapping area |
Persistent mapping area | ||
vmalloc area | ||
Low-end memory (3G~3G high_memory-1) Linear mapping area (fixed mapping area) |
ZONE_NORMAL | |
ZONE_DMA | ||
Page Directory-- >Intermediate page directory-->Page table |
Linux Video Tutorial"
The above is the detailed content of In what space does the linux driver run?. For more information, please follow other related articles on the PHP Chinese website!