linux paging part2
时间:2005-11-03 来源:redog
前两次读了个大概,这一遍读的比较详细,细节琢磨的比较细,收获不小。另外,每次的读书笔记发上来也是对自己的督促.
2.5 Paging in linux
linux defines three types of paging tables:
Page Global Directory
Page Middle Directory
Page Table
The Page Global Directory includes the addresses of serval Page Middle Dirctories,which in turn include the address
of several Page Tables.Each Page Table entry points to a page frame.The linear address is thus split into four parts
Linux handing of process relies heavily on paging ,In fact the automatic translation of linear address into
physical ones makes the following design objectives feasible:
1.Assign a different physical address space to eache process ,thus ensuring an efficient protection against address errors
2.Distinguish pages,that is group of data ,from page frames,that is physical address in main memory.this allows the same page
to be stored in a page frame,then saved to disk,and later reloaded in a different page frame.This is the basic
ingredient of the virtual memory mechanism.
Each process has its own Page Global Directory and its own set of Page Tables.When a process switching occurs
linux saves in a TSS segment the contents of the cr3 control register and loads from another TSS segment a new
value into cr3.thus when the new process resumes its execution on the CPU,the paging unit refers to the correct
set of page tables.
The Linear Address Fields
PAGE_SHIFT
Specifies the length in bits of the Offset field; when applied to Pentium processors it yields the value 12.
Since all the address in a page must fit in the offset field, the size of a page on Intel 80x86 system is 2**12
or familiar 4096 bytes;the PAGE_SHIFT of 12 can thus be considered the logarithm base 2 of the total page size
this macro is used ty PAGE_SIZE to return the size of the page.Finally the PAGE_MASK macro is defined as the value
0xfffff000;it is used to mask all the bits of the Offset field.
PMD_SHIFT
Determines the number of bits in address that are mapped by the second-level page table.it yields the values
22,the PMD_SIZE macro computes the size fo the area mapped by a single entry of the Page Middle Directory,
that is of a Page Table. Thus PMD_SIZE yields 2**22 or 4mb the PMD_MASK macro yields the value 0xffc00000;
it is used to mask all the bit of the offset and Table fields.
PGDIR_SHIFT
Determines the logarithm of the size of the area a first-leve page table can map.since the middle directory field
has length 0,this macro yields the same value yields by PMD_SHIFT, which is 22.the PAGDIR_SIZE macro computers the
size of the area mapped by a single entry of the page global directory,that is of a page directory .PGDIR_SIZE
therefore yields 4mb the PGDIR_MASK macro yields the value oxffc00000,the same as PMD_MASK.
PTRS_PER_PTE,PTRS_PER_PMD,and PTRS_PER_PGD
Compute the number of entries in the Page Table,Page Middle Directory.and Page Global Directory; they yield the
values 1024,1 and 1024 respectively.
Page Table Handing
pte_t,pmd_t,pgd_t are 32-bit date types that describe,respectively,a Page Tabel,a Page Middle Directory,and a Page Global
Directory entry. pgprot_t is another 32-bit date type that represents the protection flags associated with a single entry
Four type-conversion macros(pte_val,pmd_val,pgd_val),and pgprot_val)cast a 32-bit unsigned integer into the required tyep
Four other type-conversion macros (_pte(),_pmd(),_pgd() and _pgprot())perform the reverse casting from one of the four
previously mentioned specialized types into a 32-bit unsigned integer.
The kernel alse provides several macros and function to read or modify pagge table entries:
1.the pte_none(),pmd_none(),and pgd_none()macros yield the value 1 if the corresponding entry has the value 0; otherwise,they yield the
value 0
2.the pte_prsent(),pmd_present(),and pgd_present() macros yield the value 1 if the present flag of the corresponding entry is equal to 1
that is if the corresponding page or page table is loaded in main memory.
3.the pte_clear(),pmd_clear(),and pgd_clear()macros clear an entry of the corresponding page table.
other pass
Now come the macros that combine a page address and a group of protection flags into a 32-bit page entry or perform
the reverse operation of extracting the page address from a page table entry:
mk_ptd()
Combine a linear address and a group of access rights to create a 32-bit page Table entry.
mk_pte_phys
Creates a Page Table entry by combing the physical address and the access rights of the page
pte_page() and pmd_page
return the linear address of a page from its Page Table entry,and of a page table from its page middle directory enty
pgd_offset(p,a)
receives as parameters a memory descriptor p and a linear address a. the macro yields the address of the entry in a
page global directory that corresponds to the address a; the page global directory is found through a pointer within
the memory descriptor p.
接下来还是一些操作这些数据结构的宏和函数
Reserved Page Frames
The kernel's code and date structures are stored in a group of reserved page frames,A page contained in one of these
page frames can never be dynamically assigned or swapped to disk.
As a gerneral rule the linux kernel is installed in RAM starting from physical address
0x00100000 that is from the second megabyte.the total number of page frames required depends on how the kernel har beek
configured : a typical configuration yields a kernel that can be loaded in less than 2 MBS of RAM.
Why is not the kernel loaded starting with the first available megabyte of RAM? The pc architecture has servral peculiarities
that must be taken into accout:
Page frame is useed by BIOS to store the system hardware configuration detected during the power-on self-test
other not import pass
in order to aviod loading the kernel into groups of noncontiguous page frames linux prefers to skip the first megabyte
of RAM page frames not reserved by the PC architecture will be used by linux to store dynamically assigned pages.
Process Page Tables
the linear address space of a process is divided into two parts:
linear address from 0x0000000 to PAGE_OFFSET-1 can be addressed when the process is in either user or kernul Mode
linear address form PAGE_OFFSET to oxfffffff can be addressed only when the process is in kernel mode.
Usually the PAGE_OFFSET macro yields the value 0xc0000000
Kernel Page Tables
in the first phase, the kernel create a limited 4MBaddress space ,which is enough for it to intall itself in RAM
in the second phase,the kernel takes adcantage of all of the existing RAM and sets up the paging tables properly.
the next section examines how this plan is executed.
The Page Global Directory is contained in the swapper_pg_dir variable,while the Page Table that spans the first 4MB
of RAM is contained in the pg0 variable.
the objective of this first phase of paging is to allow these 4 MB to be easily addressed in both real mode and protected
mode therefore the kernel must create a mapping from both the linear address 0x0000000through0x003fffff and the linear
address PAGE_OFFSET through PAGE_OFFSET+0x3fffff into the physical address 0x00000000 through 0x003fffff.in other
words, the kernel during its first phase of initialization can address the first 4MB either using linear address identical
to the physical ones of using 4MB worth of linear address starting from PAGE_OFFSET.
Assuming the PAGE_OFFSET yields the value 0xc0000000,the kernel creates the desired mapping by filling all the
swapper_pg_dir entries with zeros,except for entries and 0x300;the latter entry spans all linear address between0xc0000000
and 0xc03fffff,the and 0x300 entried are initialized as follows:
the addrss field is set to the address of pg0
the present,read/write and user/supervisor flags are set
the accessed,dirty,pcd,pwd,and page size flags are cleared
the single pg0 page table is also statically initialized so that the ith entry address the ith page frame.
the paging uint is enabled by the startup_32() assembly-language function. this is achieved by loading in the cr3
control register the address of swapper_pg_dir and by setting the PG flag of the cr0 control register.
Final Kernel Page Table
the final mapping provided by the kernel page tables must transfrom linear address staring from PAGE_OFFSET into physicl
address starting from 0
the _pa macro is used to convert a linear address starting form PAGE_OFFSET to the corresponding physical address,
while the _va macro does the reverse.
the final kernel Page Global Directory is still stored in swapper_pg_dir it is initialized by the paging_init()function
this function acts on two input parameters:
start_men
the linear address of the first byte of RAM rigth after the kernel code and date areas.
end_mem
the linear address of the end of memory