The introduction
Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
Modern operating systems generally adopt the Virtual Memory Management mechanism, which requires the support of Memory Management Unit (MMU). This article briefly introduces the Virtual Memory Management mechanism of Linux operating system.
Memory space
For 32-bit cpus. The addressable space can reach 4 GIGABytes. Linux divides these 4 GIGABytes into two parts: one part is provided to users for access, and the other part is provided to the kernel for access by switching to kernel mode, as shown in the following figure:
A virtual address
But it’s impossible for the CPU to connect that many address lines to pins, so what if the user wants to access the physical memory space just by having the kernel associate with the actual physical memory space? Then you can’t access it directly, so with a transformation, the actual address that the user accesses is intercepted by the MMU, and it doesn’t go directly to the pins of the CPU, it goes through a transformation and accesses another memory address, also called a memory map.
The memory mapping
Memory mapping refers to mapping virtual memory addresses to physical memory addresses. Instead of giving each process a separate physical memory address, processes can use the same virtual address as long as they map to different physical addresses, not casually, but through the page table.
A page table
The page table records the mapping between virtual addresses and physical addresses. It is similar to a router that acts as a route. The page table is inside the MMU.
MMU
MMU maps virtual addresses to physical addresses in pages. For 32-bit cpus, a Page is usually 4KB. Each mapping requires 4KB or an integer multiple of memory space, but this creates a problem: Filling a page table requires too many items, and Linux provides multilevel page tables and hugePages to solve the problem of excessive page table entries.