What Is a Virtual Address?
The virtual address is a Windows program running in 386 protected mode. In this way, the logical address used by the program to access the memory is called a virtual address. Similar to the segment address in real address mode, the virtual address can also be written as "segment: offset ", Where a segment is a segment selector.
Virtual address
- Virtual address is a Windows program when running in 386 protected mode so that the program accesses
- Windows 2000 based
- Let's see what a physical address is and what a virtual address is.
- Physical address : An address placed on the addressing bus. It is placed on the address bus. If it is read, the circuit will transfer the data in the physical memory of the corresponding address to the data bus according to the value of each bit of this address. If it is a write, the circuit places the contents of the data bus in the physical memory of the corresponding address according to the value of each bit of this address. Physical memory is addressed in bytes (8 bits).
- Virtual address : After the CPU starts the protected mode, the program runs in the virtual address space. Note that not all "programs" run in virtual addresses. When the CPU starts up, it runs in real mode. The bootloader and kernel do not use virtual addresses before initializing the page table, but directly use physical addresses.
- If the paging flag bit in the CPU register is set, the CPU (to be precise, the MMU, or Memory Management Unit) will automatically use the information in the page directory and page table when executing machine instructions for memory operations. Convert the virtual address to a physical address to complete the instruction.
- For example, mov eax, [004227b8h], this is the assembly code that assigns the value at address 004227b8h to the register. The address 004227b8 is the virtual address. When the CPU executes this line of code, it finds that the paging flag bit in the register has been set, it automatically completes the conversion from the virtual address to the physical address, uses the physical address to fetch the value, and completes the instruction. For the Intel CPU, the paging flag is the 31st bit of register CR0. A 1 indicates that paging is used, and a 0 indicates that no paging is used. For Win2k after initialization, we observe CR0 and find that the 31st bit is 1. Shows that Win2k uses paging.
- After using the paging mechanism, the 4G address space is divided into fixed-size pages, and each page is either mapped to physical memory or mapped to a swap file on the hard disk, or nothing is mapped. For general programs, only a small part of the 4G address space is mapped to physical memory, and large parts are not mapped to anything. Physical memory is also paged to map the address space. For 32bit Win2k, the page size is 4K bytes . The information that the CPU uses to translate virtual addresses into physical addresses is stored in structures called page directories and page tables.
- Physical memory paging. The size of a physical page is 4K bytes. The 0th physical page starts at physical address 0x00000000. Because the page size is 4KB, which is 0x1000 bytes, the first page starts at physical address 0x00001000. Page 2 starts at physical address 0x00002000. It can be seen that because the page size is 4KB, only the upper 20 bits of the 32-bit address are needed to address the physical page.
- Page table, a page table is 4K bytes in size , placed in a physical page. Consists of 1024 4-byte page table entries. The size of a page table entry is 4 bytes (32 bits), so there are 1024 page table entries in a page table. The content of each entry in the page table (each entry is 4 bytes, 32bit). The upper 20bit is used to put the physical address of a physical page, and the lower 12bit is used to put some marks.
- A page directory has 1024 entries, and the highest 10 bits of the virtual address can just index 1024 entries (2 to the power of 10 equals 1024). A page table also has 1024 entries, 10 bits in the middle part of the virtual address, just indexing 1024 entries. The lowest 12 bits of the virtual address (2 to the power of 12 equals 4096). As an intra-page offset, it can just index 4KB, which is each byte in a physical page.
- For x86 systems, the physical address of the page directory is placed in the CR3 register of the CPU.
- The CPU translates the virtual address into a physical address:
- A virtual address, 4 bytes (32bit) in size, contains information to find the physical address, and is divided into 3 parts:
- The 10th (highest 10) bits from 22nd to 31st are the indexes in the page directory,
- The 10th to 10th bits are the indexes in the page table.
- The 12th (lower 12th) bits from the 0th to the 11th are offsets within the page.
- 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 CR3. Then, according to the 10th (highest 10bit) value of the 22nd to 31st bits of the virtual address as an index, find the corresponding page directory entry (PDE, page directory entry). The page directory entry contains the page corresponding to this virtual address The physical address of the table. With the physical address of the page table, the corresponding page table entry (PTE, page table entry) in the page table is found based on the 10-bit value of the 12th to 21st bits of the virtual address. There is the physical address of the physical page corresponding to this virtual address. Finally, the lowest 12 bits of the virtual address, that is, the offset within the page, and the physical address of the physical page are added to obtain the physical address corresponding to the virtual address.
- A page directory has 1024 entries, and the highest 10 bits of the virtual address can just index 1024 entries (2 to the power of 10 equals 1024). A page table also has 1024 entries, 10 bits in the middle part of the virtual address, just indexing 1024 entries. The lowest 12 bits of the virtual address (2 to the power of 12 equals 4096). As an intra-page offset, it can just index 4KB, which is each byte in a physical page.
- The calculation process of converting a virtual address into a physical address is that the processor finds the physical page where the current page directory is located through CR3, takes the high 10bit of the virtual address, and shifts the 10bit to the left by 2bit (because each page directory entry is 4 bytes long , Shifting 2 bits to the left is equivalent to multiplying 4) to get the address in the page, take out the PDE (4 bytes) at the address, and find the physical page corresponding to the page table of the virtual address. Take the 12th to the virtual address of the virtual address. 21 bits and 10 bits, and then shift the 10 bits to the left by 2 bits (because each page table entry is 4 bytes long, 2 bits to the left is equivalent to multiplying 4) to get the address on the page, and take out the PTE (4 Bytes), we find the address of the physical page corresponding to the virtual address, and finally add the 12-bit page offset to get the physical address.
- A 32-bit pointer that can address the range 0x00000000-0xFFFFFFFF, 4GB. In other words, a 32-bit pointer can address each byte of the entire 4GB address space. One page table entry is responsible for the mapping of the 4K address space and physical memory, and one page table entry is 1024, which is responsible for the mapping of the address space of 1024 * 4k = 4M. A page directory entry corresponds to a page table. A page directory has 1024 entries, which corresponds to 1024 page tables. Each page table is responsible for the mapping of the 4M address space. The 1024 page tables are responsible for 1024 * 4M = 4G address space mapping. A process has a page directory. So in pages, the page directory and page table can ensure the mapping of each page and physical memory in the 4G address space.
- Each process has its own 4G address space, from 0x00000000-0xFFFFFFFF. This is achieved through each process's own set of page directories and page tables. Because each process has its own page directory and page table, the physical memory mapped in the address space of each process is different. The values of the two processes at the same virtual address (if both have physical memory mappings) are generally different because they often correspond to different physical pages.
- Under Windows, the 4G address space is 2G lower, 0x00000000-0x7FFFFFFF is the user address space, and the 4G address space is 2G higher,
- 0x80000000-0xFFFFFFFF is the system address space. Access to the system address space requires the program to have ring0 permissions. The Linux division of 4G space is different from windows. Linux uses the highest 1G byte (from the virtual address 0xC0000000 to 0xFFFFFFFF) for the kernel to use, called "kernel space". The lower 3G bytes (from virtual address 0x00000000 to 0xBFFFFFFF) are used by various processes and are called "user space".
- Under the LINUX system, 0xC00000000-0XFFFFFFFF is the system space, which is shared by all system processes, and 0X00000000-0XBFFFFFFF is the user space.