为何需要虚拟地址?
- Programmer no longer have to be responsible for storage allocation.
- Virtual memory allows for sharing data and code between programs by having two virtual addresses point to the same physical memory location.
- It allows us to run more applications on the system than we have enough physical memory to support.
虚拟地址和物理地址如何进行转化?
如下这张图清晰的表示了从virtual address maps to physical address的详细过程。
p stands for 虚拟地址中的 pages no.(页号)。 f stands for 物理地址中的frame no.(页帧号)。对于虚拟地址和物理地址来说,页面的offset是完全相同的。因此,上图中的o stands for 虚拟地址和物理地址中的offset。所谓的虚拟地址到物理地址的映射,就是指的如何根据虚拟地址找到物理地址的过程。由上图可以清晰的看出Page Table(页表)可以帮助该过程的实现。
- 虚拟地址的offset不经过任何处理直接映射到物理地址的offset。
- 虚拟地址的page no.通过Page Table完成到物理地址的frame no.的映射。
- 虚拟地址中有多少个page no,对应的Page Table中就有多少行。
- Page Table的每一行的大小是由物理地址的frame no的个数决定的。比如物理地址有20个frame,那么Page Table的每一行就有20bit,因为只有如此,才能刚好指定从0到(2^20-1)之间的任意frame no.
- 对Page Table来说,行数目 X 行的大小 = Page Table的大小。如此,可以计算出Page Table的大小。
比如:
虚拟地址为30bits,物理地址为20bits,page的大小为1KB。
page table的大小为:
page table的行数:2^(30 - 10) = 2^20
每行的大小:2^20/2^10 = 2^10, 即每一行有10bit才能表示2^10个frame no
所以总的page table的大小为2^20 X 10 bits
另附一mapping图