http://scriptmatrix.net/cs2106/wiki/index.php/Page_Frame_Management

Page Frame Management

From The Linux Memory Wiki

Jump to: navigation, search

Inside 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.


In this chapter, we will talk about how the kernel manages memory of its self. Do not confuse this with managing memory of the processes. The kernel memory management basically also covers management of memory of all processes. After we know how the kernel manages memory itself, then only we can move to the next chapter, which descibes how the kernel handles memory of processes.


Contents

[hide]

Memory Layout

Before 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.


The first megabyte of the physical memory is reserved for architecture dependent BIOS procedues. This is to be used for very low level functionalities of your motherboard. Hence, normally the kernel's image is loaded into physical address starting from 0x00100000, which is the 2nd megabyte.

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.


Why do the kernel reserves so much memory from all processes? We will discuss this now in the following section.


Kernel Memory Mapping

The 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.


Hence, when the kernel wants to manipulate the physical memory, say, load a page into a page frame, the kernel can perform the task directly as if it were physical address. The kernel stays in protected mode hence it may still refer to the linear address of a process and rely the Paging Unit to transform the linear address into physical address.

Memory Zones

There are 3 kinds of zones exist in the 1GB kernel linear address space:

High Memory

If 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 kernel cannot perform in real mode, because it may needs to access information from the linear address of the process (the lower 3GB addresses). Hence the only way left is to reserve the top 128MB memory to perform indirect addressing.

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:

  • Say the kernel wants to access the physical address 0x12001000 to 0x12002000, it must have a linear address that points to this physical address.
  • The kernel searchs for a suitable linear address from the last 128MB of its address space, and found a range, say 0xfa001000 to 0xfa002000.
  • Because this linear address is not used by the kernel to do direct mapping, the kernel can modify the page table of this page and point it to physical address 0x12001000.
  • The kernel then perform tasks on this linear address 0xfa001000 to manipulate the physical address 0x12001000.


This zone of 128MB memory is called the High Memory Zone. It is used to dynamically map the physical address of above 896MB.


This high memory zone is yet another consequence of 32-bit 4GB limit. In 64-bit systems, because there is so much more linear address available than the physical memory, the kernel can safely reserve a large portion of linear address of a process's address space to manipulate physical addresses.

Hence in 64-bit systems, the size of the high memory zone is 0 instead.


More on high memory zone will be discussed later in this chapter.

DMA Zone

In 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 Zone

This 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 Descriptors

Page Descriptor

Linux 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 Descriptor

The 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.



Reply via email to