http://scriptmatrix.net/cs2106/wiki/index.php/Page_Frame_ManagementPage Frame ManagementFrom The Linux Memory WikiInside Linux, the kernel itself need to manage alot about the physical memory. The kernel has to monitor over all page frames and decide a suitable page frame to insert a page. Moreover, the kernel itself needs alot of dynamic memory allocation, especially for all kinds of descriptors it has.
Memory LayoutBefore we want to manage memory, we have to first figure out which memory is usable. First, obviously the kernel cannot manage the physical address that is beyond the total capacity of the RAM. Hence during boot up, the kernel determines the memory available and marks the page frames outside the bound to be not available.
For those who doesn't know, a typical kernel image is only 3MB, that means theoretically a system with 5MB of RAM is large enough to boot a Linux system! (Compare this with 2GB requirement of Windows Vista) There are some distribution that optimize the Linux kernel to further reduce the kernel image. The result is the memory requirement for Linux becomes as low as just 2MB.
When a process invokes a system call to the kernel, the kernel may accepts some parameters that include pointers to some linear address inside the process. Hence the kernel needs to have access to the process's linear address space. However at the same time, the kernel has to manage the physical memory and the pages and page frames. There has to be some way for the kernel to map all this using a 32-bit linear address. To achieve this, the kernel reserves a greedy 1GB memory from all process's linear address space. The last GB of linear address from 0xc0000000 to 0xffffffff is reserved for this purpose. This means that if you write some program in assembly language, you cannot put anything in this linear address region. You cannot use the instruction jmp 0xc0000001 nor lw %eax, 0xdfffffff. This leads to the fact that a user process can actually use only up to 3GB of memory, not all 4GB.
Kernel Memory MappingThe kernel reserves the top 1 GB of the linear address to map the linear address directly to the physical address. In this 1GB address, the first 896MB address that is from 0xc0000000 to 0xf7ffffff are directly mapped to physical address 0x00000000 to 0x37ffffff, that is, the first 896MB of the physical address space. This is done by pointing the page tables of these entries to the corresponding physical address and never change it thereafter.
Memory ZonesThere are 3 kinds of zones exist in the 1GB kernel linear address space: High MemoryIf the kernel wants to map all physical memory, why is it 896MB? This 896MB puzzle is quite confusing. It is because the kernel only reserves 1GB of linear address. if the physical memory is more than 1GB, say 2GB, the kernel has to find some way to map any physical address inside the 2GB RAM.
The other reason for this is so that the page tables in TLB don't need to be flushed when the process switched into kernel mode. The kernel can directly access the data structure it needs in the kernel memory area without affecting any page tables. The procedure is the following:
Hence in 64-bit systems, the size of the high memory zone is 0 instead.
DMA ZoneIn old ISA buses the devices that use DMA had a limitation, that is DMA can only be performed on the first 16MB of physical address. In order to maintain compability with these old devices, the first 16MB has to be reserved as a DMA zone for DMA operations to be performed.
Normal ZoneThis is the rest of 1GB linear address that the kernel do fix mapping. Effectively this is 1024MB - 128MB - 16MB = 880MB from linear address 0xc1000000 to 0xf7ffffff. Linear address in this zone is usually mapped directly into the physical address. Memory DescriptorsPage DescriptorLinux uses several descriptors to store information of the memory management. First of all, we know that there are fixed page frames that resides in the physical memory and pages that hold the content of a piece of memory in the linear address. We also have learned that a page can be inserted into any page frame regardless of the linear address. To manage this the kernel needs to know which page frame is free and which is occupied, and it uses the page descriptor to store this information. It is important to notice that page descriptor describes about the page frame, not the page. So don't get confused by the name here! The page descriptor store information about various flags, which zone it belongs to, how many page table entries point to this page frame, and several other property. Zone DescriptorThe zone descriptor holds information about different zones. Since there are usually 3 memory zones in Linux as we discuss above, there are usually 3 zone descriptors that hold information of page frames in the zone. The zone descriptor holds some important property as follow: free_pages - The number of free pages available in this zone free_area - descriptor struct that store information of blocks of free page frames. remember and notice this field as we will discuss in further detail about this field in the following Buddy Allocator.
|