Why do I need to use virtual memory
As you all know, the code and data that the process needs to use are in memory, much faster than in external memory. The problem is that the memory space is too small to meet the needs of the process, and it is now a multi-process situation that is even worse. Therefore, virtual memory is proposed so that each process is used for 3G of independent user memory space and shared 1G kernel memory space. (Each process has its own page table, which makes the 3G user space independent) so the process runs fast. The virtual memory mechanism also solves the problem of memory fragmentation and memory discontinuity. Why is it possible to achieve this effect on limited physical memory?
Implementation mechanism of two virtual memory
First of all, to mention a concept, swap space, this should be no strangers, in the reload system, will let you choose the disk partition, such as a hard disk in several parts to manage. It will be divided into a portion of disk space as a swap, called swap space. In fact, is a temporary storage space, memory is not enough to use it, although it is also in the disk, but save a lot of time to find AH. When a process switch occurs, the memory and swap space is about to occur when the data exchange meets the requirements. So, the switching consumption of the process is very large, which also explains why the spin lock is more efficient than the semaphore.
So when we apply for memory in our program, the Linux kernel actually allocates only one virtual memory (linear address) and does not allocate actual physical memory. Physical memory is allocated only when the program is actually using this memory. This is called the deferred allocation and paging mechanism. When the memory is freed, the physical memory corresponding to the linear area is released, and then the linear zone is released, and the "paging mechanism" delays the allocation of physical memory, which is the use of the program's locality, saving memory space and improving the system throughput; That means a function may only be in physical memory for a while, and then it is erased. Although the virtual address space is still there. (However, the virtual address space is not a de facto storage, so it can only be said that this function occupies a virtual address space, when you access this address, it will produce a missing pages processing, from the swap area to move the corresponding code to physical memory)
Three physical and virtual memory layouts
The left is the physical address assignment, which is related to the actual CPU. 4KB of these are the possession of some controllers, such as the LCDC SD card, their register address is so dead. However, when we want to access these registers, it is still not directly used, to use the rules of memory management, using virtual address to access it, so in the driver and other kernel programs need to use virtual address access register. If someone accesses the register directly using the physical address, the only explanation is that the MMU is not open. However, your process will not have 4G of memory to use.
Physical Address Distribution:
This is a stolen figure, the physical address has 896M directly mapped to the virtual address of the memory space, which is the mapping of one by one, only the starting address is not the same, the offset is the same. This size is mostly fixed, even if your memory is more than a G, too small to say otherwise. Note: The code for the user area is also placed in the physical address, which means that the physical address can be mapped two times. But anyway, this physical address is managed by the kernel. What happens to the remaining memory when you have a large memory, over 896M? This is called high-end memory, if you use the Vmalloc application space, will be allocated in high-end memory, if you use Kmalloc request space, will be less than 896 in memory allocation. So it is very fastidious ah!! If your program needs to use high-end memory, call the kernel API to allocate, so high-end memory is not intended to use the OH. However, it is a good practice to keep some applications in high-end memory through the system. But the premise is that your memory is very gray.
Why did you do it? Let's see what's in there.
Virtual Address distribution:
About the distribution of 0-3g user space memory:
When it comes to segment distribution, the logical address, the relationship between the linear address and the physical address:
Linux converts a logical address into a virtual address (that is, a linear address) through a segment mechanism, and then translates the virtual address into a physical address through the page mechanism. The so-called segmentation is the base address, the same as the offset, for example, 32, the general program will not use so many bits, the first 12 bits can be used as a base address, the last 20 bits as an offset, so that in a specific segment can only use offset addressing. Addressing is convenient, but the Linux page base is doing better.
Finally, let's say a few more points:
1 linear address space: refers to the virtual address space in a Linux system.
2 CPU addressing is a physical address. So you need to convert the address before using CPU addressing.
3 high-end memory in physical memory is the one that comes out of DDR minus 896M. The high-end memory inside the virtual address refers to the virtual address space used to map high-end memory. But high-end memory is mapped to user space, that's another matter.
4 kernel space is accessible to user space, high level is good ah. However, it is not accessed directly through a virtual address.
Ext.: http://www.cnblogs.com/autum/archive/2012/10/12/linuxmalloc.html
"Go" Linux memory management