During the 8086 period, registers were 16-bit and 20-bit physical addresses could not be stored. To solve this problem, we proposed a segmentation mechanism. The segmentation mechanism is to segment the memory, with the size of each segment 64kb, put the base address of the segment in the segment register, and then the offset of the plus segment becomes the physical address or linear address. Why is it "or? In the paging base address system, the linear address plus the page offset is the real physical address. If the paging mechanism is not used, the calculated physical address is used. Windows NT introduces the memory paging Mechanism, which enables exclusive use of 4 from the application perspective. Memory space in GB, memory space is isolated between applications and does not affect each other. A process crash does not spread and affects other processes. In addition, the virtual memory mechanism becomes possible, swap unused memory pages to the pagefile file on the disk. The memory paging mechanism divides the physical memory into multiple equal parts based on a fixed size, which is generally 4 K. The following section also describes the size of 4 K. Each equal part is called a page, I call it a physical page, and the linear address space is also paged. I call it a linear page. The linear address space accessible to a process is 0 ~ 0x3fffffff, how do I map it to the physical memory? That is to say, how do linear pages map to physical pages? No one calls linear pages or physical pages. I will introduce one. Next we will discuss it. The original 4 m space starting from 0xc0000000 in the process address space ends at 0xc03fffff, and stores the physical address values of the physical pages corresponding to each linear page. Each 4 bytes is one item, every 1024 items constitute a page table PT, so a total of 1024 PT items are generated. The index of the items in the page table is called PTE (page table entry). For example, the first linear page is 0 ~ 0x00000fff linear address space, which is its physical page? Apparently, it is the DWORD Value pointed to by the linear address 0xc0000000. So far, when the linear address fails, it often leads to Cheng Yuan? The value of xc0000001. After the linear page is mapped to the physical page, the physical address can be obtained by adding the offset in the previous page. It turns out to be quite simple. Obviously, the offset in the page is the last 12 digits of the linear address, that is, the following formula: Offset = La & 0x00000fff La: Linear address, line address Offset: Intra-page offset PA: physical address, physic address After understanding the previous description, it is easy to know that the 20-bit high of the linear address is the PTE index. That is, there is a pa = * (0xc0000000 + La> 12*4), * Indicates getting the address content What are the physical pages of the 4 m linear address starting with 0xc0000000? Let's calculate 0xc0000000 + 0xc0000000> 12*4 = 0xc0300000 It can be seen that the page table starting with 0xc0300000 is special, called a page Directory table. Each item of this table points to the starting position of a page table, that is, the table on the page, and the index of the page Directory table, called the page directory entry ), Assume that you know a linear address, such as the PDE and PTE, And you can calculate the physical address according to the following formula: Pa = * (0xc0000000 + PVDF * 1024*4 + PTE * 4) = * (0xc0000000 + (PVDF <10 + PTE) <2) Compare the previous formula, * (0xc0000000 + La> 12*4) = * (0xc0000000 + (PVDF <10 + PTE) <2) La> 12 = (PVDF <10 + PTE) The maximum power of a Pte is 1024 = the 10th power of 2. Therefore, the PVDF is the 10-bit high of the linear address, and the Pte is the 10 middle of the linear address. The low 12 of the linear address is the intra-page offset. In some articles, how do we understand the pdls and PTE addresses? This is the address of the linear address corresponding to the page Directory table, Pde_add = La> 22 + 0xc0300000 The PTE address refers to the address of the linear address corresponding to the page table, Pte_add = La> 12 + 0xc0000000 The 12-bit low PTE address contains page attributes, which can be seen in windbg. Lkd> dt _ hardware_pte NT! _ Hardware_pte + 0x000 valid: POS 0, 1 bit + 0x000 write: POS 1, 1 bit + 0x000 owner: POS 2, 1 bit + 0x000 writethrough: POS 3, 1 bit + 0x000 cachedisable: POS 4, 1 bit + 0x000 accessed: POS 5, 1 bit + 0x000 dirty: POS 6, 1 bit + 0x000 largepage: POS 7, 1 bit + 0x000 global: POS 8, 1 bit + 0x000 copyonwrite: POS 9, 1 bit + 0x000 prototype: POS 10, 1 bit + 0x000 Reserved: POS 11, 1 bit + 0x000 pageframenumber: POS 12, 20 bits When the 0-bit value is 1, it indicates that the Pte is valid and the page is in the physical memory, the 7-bit largepage indicates that the current virtual address is greater than or equal to 0x80000000 and smaller than 0xa0000000 (the physical address is obtained after 0x80000000 is subtracted) How does the CPU calculate the physical address of a linear address? Some online materials are referenced below. 1. The physical page address of the page Directory of the current active process is saved in the 33rd register. The CPU uses the 3rd to find the physical page address of the current page Directory. 2. Calculate the physical address of the pdpa: A + linear address is a 10-bit high left shift of two places (because each page directory contains four bytes), and The pdns at the address are extracted, the first 20-bit + 12-bit-low 0 of the pdpa is the physical address B of the page table corresponding to the virtual address. 3. Calculate the physical address of the Pte. Remove the two left digits in the middle of the B + virtual address and obtain the PTE at the address, the first 20 bits of PTE are the virtual address + 12 bits of low 0 is the address of the corresponding physical Page C 4. Calculate the physical address corresponding to the virtual address: the minimum number of C + virtual addresses is 12. |