JIURL plays Win2K memory booking mechanism (1)

zhaozj2021-02-16  36

JIURL play Win2k memory paging articles (a) Author: JIURL Home: http://jiurl.yeah.net Date: 2003-7-30

Basic Concept Windows 2000 uses virtual memory based on paging mechanisms. Each process has 4GB virtual address space. Based on the paging mechanism, some parts of this 4GB address space are mapped by physical memory, some partially mapped exchange files on the hard disk, and some parts have no mapping. The programs are used in the 4GB address space in the virtual address. And accessed physical memory, you need to use physical addresses. Let's take a look at what is a physical address, what is a virtual address. Physical Address: Add the address of the addressing bus. Placed on the address bus, if it is read, the circuit puts the data in the physical memory of the corresponding address in the data bus in the data bus according to the value per bit of this address. If it is written, the circuit puts the physical memory of the corresponding address in the physical memory of the corresponding address according to the value of this address. Physical memory is addressing in bytes (8 bits). Virtual Address: 4G address in the virtual address space, all virtual addresses used. If the paging flag in the CPU register is set, the CPU will automatically convert the virtual address into a physical address according to the information in the page table and the information, and complete the instruction. For example, MOV EAX, 004227B8H, which is the assembly code assigned to the register at the address 004227b8h, 004227B8 This address is the virtual address. When the CPU is executed, the paging flag bit in the discovery register has been set, automatically completes the virtual address to the transformation of the physical address, and use the physical address to remove the value, complete the instruction. For Intel CPUs, the paging flag is the 31st bit of the register CR0, and 1 means using paging, 0 means not using the paging. We observe CR0 after the initialization, and found that the 31st is 1. Indicates that Win2k is used by paging. After using the paging mechanism, 4G address space is divided into fixed size pages, each page or being mapped to physical memory, or is mapped to the exchange file on the hard disk, or not map anything. For general procedures, 4G address space, only a small part maps physical memory, and large blocks of large pieces are not mapped anything. Physical memory is also paging to map address space. For 32bit Win2K, the size of the page is 4K bytes. The CPU is used to convert virtual addresses into physical addresses in the structure called page directory and page table. Physical memory page, a physical page size is 4K bytes, starting from physical address 0x00000000. Since the size of the page is 4KB, it is 0x1000 bytes, so I started from the physical address 0x00001000 on page 1. Page 2 starts from physical address 0x00002000. You can see that the size of the page is 4KB, so only the 32bit address is required to address the physical page. Page table, a page table size is 4K bytes, placed in a physical page. Composed of 1024 4-byte page tables. The size of the page entry is 4 bytes (32bit), so there is a 1024 page entry in a page table. The contents of each of the pages (4 bytes per item, 32bit) are 20bit to put a physical address of a physical page, and there is some logo with low 12bit. Page Directory, a page directory size is 4K bytes, placed in a physical page. Composed of 1024 4-byte page directory items. The page catalog entry is 4 bytes (32bit), so there are 1024 page directory items in a page directory. The contents of each item in the page directory (each 4 bytes) high 20bit are used to place a page table (page table is placed in a physical page), and there are some signs with low 12bit. For X86 systems, the physical address of the page directory is placed in the CPU's CR3 register.

The CPU converts the virtual address into a physical address: a virtual address, the size of 4 bytes (32bit), contains information on the physical address, divided into 3 parts: 22nd to 31st (highest 10) ) Is the index in the page directory, and the 12th to 21st bit is the index in the page table, and the 12th digits (low 12 digits) of the 0th to the 11th is the page offset. For a virtual address to be converted into a physical address, the CPU first finds the physical page where the page directory is located according to the value in the CR3. Then use the 10-bit (highest 10bit) value as an index according to the value of the virtual address, find the corresponding page directory entry (PDE, Page Directory Entry), and the page directory item is available. The physical address of the table. With the physical address of the page table, according to the 12th digits of the virtual address as an index, find the corresponding page table item (PTE, PAGE Entry), page entry in this page table. There is this virtual address corresponding to the physical address of the physical page. Finally, the minimum 12 digits of the virtual address is also the offset within the page, and the physical address of this physical page is added, the physical address corresponding to the virtual address is obtained. A page directory has 1024 items, and the 10 bit of the virtual address can just index 1024 items (2 of 10 times is equal to 1024). A page table also has 1024 items, 10Bit of the virtual address intermediate part, just 1024 items. The minimum of the virtual address (2 is equal to 4096), the offset as the page, just can index 4KB, which is one byte in a physical page. A virtual address is converted into a physical address, that is, the processor finds the physical page where the current page directory is located via CR3, takes the high 10bit of the virtual address, then remove the 10bit right 2bit (because each page directory item 4 bytes , Right shift 2bit is equivalent to 4) get the address in this page, remove the address of the PDE (4 bytes), find the physical page where the virtual address corresponds to the page table, taking the virtual address 12th to the first 21 this 10-bit, then remove this 10bit right 2bit (because each page entry is 4 bytes long, right shift 2bit is equivalent to multiplying the address in this page, remove the PTE at this address (4 In-byte, find the address of the virtual address corresponding to the physical page, and finally the offset of the 12bit page has been submitted to the physical address. A pointer of 32bit, can address the range of 0x00000000-0xFfffffff, 4GB size. That is to say a 32 bit of pointer can address each byte of the entire 4GB address space. A page entry is responsible for 4K's address space and physical memory mapping, a page table 1024, which is responsible for the 1824 * 4k = 4m address space mapping. A page directory entry, corresponding to a page table. A page directory has 1024 items, and there is a 1024 page table, each page table is responsible for the map of 4M address space. 1024 page tables are responsible for 1024 * 4m = 4G address space mapping. One process has a page directory. So the page-page unit, page directory, and page table ensure the mapping of each page and physical memory in 4G address space. Each process has its own 4G address space, from 0x00000000-0xffffffffff. Implemented by each process yourself. Since each process has its own page directory and page table, the physical memory mapped in each process is not the same. The values ​​of the same virtual address of the two processes (if they have physical memory mapping) are generally different because they often correspond to different physical pages.

转载请注明原文地址:https://www.9cbs.com/read-26137.html

New Post(0)