Physical and virtual addresses

Source: Internet
Author: User

Source: http://zhidao.baidu.com/question/4672979.html? Fr = qrl3

Windows 2000 uses a paging-based virtual memory. Each process has 4 GB of virtual address space. Based on the paging mechanism, some parts of the 4 GB address space are mapped to physical memory, some are mapped to swap files on the hard disk, and some are not mapped to anything. All the virtual addresses in the 4 GB address space are used in the program. The physical address is used to access the physical memory.

Next we will look at what is a physical address and what is a virtual address.

Physical address: the address placed on the addressing bus. Put it on the addressing bus. If it is read, the circuit will put the data in the physical memory of the corresponding address into the data bus according to the value of each address. If it is a write, the circuit will put the physical memory of the corresponding address into the content on the Data Bus according to each value of this address. Physical memory is structured in bytes (8 bits.

Virtual Address: the address in the 4G virtual address space. All addresses in the program are virtual addresses.

If the paging flag in the CPU register is set, the CPU automatically converts the virtual address to a physical address based on the information in the page Directory and page table when executing the memory operation machine command, complete the command. For example, mov eax, 004227b8h. This is the assembly code that assigns the value at the address 004227b8h to the register. The address 004227b8 is the virtual address. When the CPU executes this line of code and finds that the paging flag in the register has been set, the virtual address is automatically converted to the physical address, and the value is retrieved using the physical address to complete the instruction. For Intel CPU, the paging flag is the 31st-bit CRC register. If it is set to 1, pagination is used. If it is set to 0, pagination is not used. For the Win2k after initialization, we observe Cr0 and find that the 31st bits are 1. It indicates that Win2k uses paging.

After the paging mechanism is used, 4G address space is divided into fixed pages, each page can be mapped to physical memory, or mapped to swap files on the hard disk, or you have not mapped anything. For general programs, only a small part of the 4G address space is mapped to the physical memory, while the large part is not mapped to anything. The physical memory is also paged to map the address space. For 32-bit Win2k, the page size is 4 K bytes. The CPU is used to convert a virtual address into a physical address and store the information in a structure called a page Directory and a page table.

Physical memory paging. The size of a physical page is 4 kb, and the size of 0th physical pages starts from the physical address 0x00000000. Because the page size is 4 kb, that is, 0 x 1st bytes, The 00001000 page starts from the physical address 0 x. Page 1 starts from the physical address 0 x 2nd. We can see that because the page size is 4 kb, we only need 20 bits in the 32bit address to address the physical page.

Page table. A page table is 4 K bytes in size and is placed on a physical page. It consists of 1024 4-byte page table items. The size of a page table item is 4 bytes (32 bit). Therefore, a page table contains 1024 page table items. The content of each item in the page table (4 bytes for each item, 32bit) is 20 bits in height to put the physical address of a physical page, and 12 bits in height are marked.

Page Directory, which is 4 K bytes in size and placed on a physical page. Consists of 1024 4-byte page Directory items. The size of the page Directory items is 4 bytes (32 bit), so a page directory contains 1024 page Directory items. The content of each item in the page Directory (4 bytes per item) is 20 bits in height to put the physical address of a page table (the page table is placed in a physical page), and 12 bits in height are marked.

For x86 systems, the physical address of the page Directory is placed in the CPU's Cr 3 register.

The CPU converts a virtual address to a physical address:
A virtual address is 4 bytes (32 bits) in size. It contains information about finding the physical address. It is divided into three parts: 10 to 22nd bits (up to 10 bits) it is the index in the page Directory. The 10 digits from 12th to 21st are the indexes in the page table, and the 12 digits from 0th to 11th are the intra-page offset. For a virtual address to be converted to a physical address, the CPU first finds the physical page where the page Directory is located based on the value in S3. Then, based on the value of the 10-bit (highest 10-bit) value from the 22nd-bit to the 31st-bit virtual address, find the corresponding page Directory item (DTA, page directory entry ), the page directory contains the physical address of the page table corresponding to the virtual address. With the physical address of the page table, the corresponding page table item (PTE, page table entry) in the page table is found based on the value of the 10-bit to 12th-bit virtual address as an index ), the page table item contains the physical address of the physical page corresponding to the virtual address. Finally, the physical address corresponding to the virtual address is obtained by adding the physical address of the physical page with the minimum 12 bits of the virtual address, that is, the offset in the page.

There are 1024 items in a page directory, and the top 10 bits of the virtual address can index 1024 items (the 10th power of 2 is equal to 1024 ). A page table also has 1024 items, 10 bits in the middle of the virtual address, and exactly indexes 1024 items. The lowest 12-bit virtual address (2 to the power of 12 is equal to 4096), as the intra-page offset, it can index 4 kb, that is, each byte in a physical page.

The computing process of converting a virtual address to a physical address is that the processor finds the physical page of the directory on the current page through the-bit high of the virtual address, then, shift the 10bit value to 2bit (because the four bytes of each page Directory item are long and the 2bit value is shifted to 4) to get the address on this page, obtain the address, in 4-byte format, and find the physical page where the virtual address corresponds to the page table. The virtual address ranges from 12th to 21st, then, shift the 10bit value to 2bit right (because each page table item has four bytes long, And the 2bit value shifted to the right is equivalent to multiplication 4) to get the address on this page, take the PTE (4 bytes) at the address, find the address of the physical page corresponding to the virtual address, and add a 12-bit page offset to obtain the physical address.

A 32-bit pointer that can be addressable in the range of 0x000000000000-0xffffff, 4 GB. That is to say, a 32-bit pointer can address every byte of the entire 4 GB address space. A page table is responsible for ing 4 K address space to physical memory. A page table contains 1024 items, that is, ing of 1024*4 k = 4 M address space. A page Directory item corresponds to a page table. There are 1024 items in a page Directory, which corresponds to 1024 page tables. Each page table is responsible for 4 M address space ing. The 1024 page tables are responsible for the address space ing of 1024*4 m = 4G. A process has a page Directory. Therefore, in the unit of pages, the page Directory and page table can ensure the ing between each page and physical memory in 4 GB address space.

Each process has its own 4G address space, from 0x000000000000-0xffffff. It is implemented through a set of page directories and page tables of each process. Since each process has its own page Directory and page table, the physical memory mapped to the address space of each process is different. The values of the two processes at the same virtual address (if both have physical memory ing) are generally different, because they usually correspond to different physical pages.

4G address space: medium to low 2g, 0x000000000000-0x7fffff is the user address space, 4G address space: medium to high 2g,
0x80000000-0xffffffff is the system address space. The program must have the ring0 permission to access the system address space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.