虚拟空间地址与页面号的转换
2014-01-16 14:52
381 查看
当程序中出现内存写入访问错误时,ExcetionRecord.ExceptionInfomation数组第一个元素标识为写入;若是读取错误时,则 标识为读取状态。第二个元素,则是当前读取或写入的虚拟空间地址。在某些状况下,我们需要知道出错的虚拟地址所对应的虚拟内存页面,然后针对此页面做一些处理。那么如何由当前虚拟地址计算出对应的虚拟内存页面?
delphi代码:var pAddr, pPage: Pointer; begin //此处省略若干行 pAddr := Pointer(ExcetionInfo^.ExcetionRecord^.ExceptionInformation[1]);
pPage := Pointer(Integer(pAddr) and (not (Page_Size - 1)))//Page_Size = 4096
end;
下面的内容,则是有关计算的原理和依据
Virtual Addresses
When a program accesses memory, it does not know or care where the physical memory backing the address is stored. It knows it is up to the operating system and hardware to work together tomap locate the right physical address and thus provide access to the data it wants. Thus we term the address a program is using to access memory a virtual address. A virtual address consists of two parts; the page and
an offset into that page.
Page
Since the entire possible address space is divided up into regular sized pages, every possible address resides within a page. The page component of the virtual address acts as an index into the page table. Since the page is the smallest unit of memory allocationwithin the system there is a trade-off between making pages very small, and thus having very many pages for the operating-system to manage, and making pages larger but potentially wasting memory
Offset
The last bits of the virtual address are called the offset which is the location difference between the byte address you want and the start of the page. You require enough bits in the offset to be able to get to anybyte in the page. For a 4K page you require (4K == (4 * 1024) == 4096 == 212 ==) 12 bits of offset. Remember that the smallest amount of memory that the operating system or hardware deals with is a page, so each of these 4096 bytes reside within
a single page and are dealt with as "one".
Virtual Address Translation
Virtual address translation refers to the process of finding out which physical page maps to which virtual page.When translating a virtual-address to a physical-address we only deal with the page number . The essence of the procedure is to take the page number of the given address and look it up in the page-table to
find a pointer to a physical address, to which the offset from the virtual address is added, giving the actual location in system memory.
Since the page-tables are under the control of the operating system, if the virtual-address doesn't exist in the page-table then the operating-system knows the process is trying to access memory that has not been allocated to it and the access will not be
allowed.
Figure 6.3. Virtual Address Translation
We can follow this through for our previous example of a simple linear page-table. We calculated that a 32-bit address-space would require a table of 1048576 entries when using 4KiB pages. Thus to map a theoretical
address of 0x80001234, the first step would be to remove the offset bits. In this case, with 4KiB pages, we know we have 12-bits (212 == 4096) of offset. So we would right-shift out 12-bits of the virtual address, leaving us with 0x80001. Thus (in
decimal) the value in row 524289 of the linear page table would be the physical frame corresponding to this page.
You might see a problem with a linear page-table : since every page must be accounted for, whether in use or not, a physically linear page-table is completely impractical with a 64-bit address space. Consider a 64-bit address space divided into (generously
large) 64 KiB pages creates 264/216 = 252 pages to be managed; assuming each page requires an 8-byte pointer to a physical location a total of 252/23 = 249 or 512 GiB of contiguous memory is
required just for the page table!
相关文章推荐
- 在用户态进行虚拟空间地址向物理空间地址的转换
- 在用户态进行虚拟空间地址向物理空间地址的转换
- 外设IO地址空间中的地址怎么转换到内核态的虚拟地址空间(一个ioremap函数真的解释清楚了么)
- 用户空间的虚拟地址如何转换得到实际的物理地址
- Linux 内核空间虚拟地址和物理地址相互转换
- 在用户态进行虚拟空间地址向物理空间地址的转换
- 在用户态进行虚拟空间地址向物理空间地址的转换
- linux虚拟地址空间你真的理解了吗?
- 虚拟地址、线性地址和物理地址的转换
- 虚拟地址空间的内存布局
- 启用PAE机制下虚拟地址到物理地址的转换
- GDTR和LDTR如何把虚拟地址转换成线性地址
- 虚拟地址空间
- 实践使用WinDBG从虚拟地址转换到物理地址
- 进程的虚拟地址空间,堆栈、堆、数据段、代码段
- 虚拟地址空间以及编译模式
- 关于Windows编程中进程虚拟地址空间/虚拟内存及内存映射方面的资料
- Linux虚拟地址空间布局
- Linux系统--进程地址空间之虚拟地址空间
- 內存和虚拟地址空间的关系