如何使用页表将虚拟地址转换为物理地址?
假设我有一个普通的页表:
页表(页面大小 = 4k)
Page #: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Page Frame #: 3 x 1 x 0 x 2 x 5 x 7 4 6 x x x
如何将任意逻辑地址(如 51996)转换为物理内存地址?
如果我采用以 2 为底的对数 (4096),则得到 12。我认为这就是我应该用于地址偏移量的位数。
我只是不确定。 51996 / 4096 = 12.69。 那么这是否意味着它位于第 12 页并具有一定的偏移量?
那么如何将其转换为“51996”的物理地址?
Lets say I have a normal page table:
Page Table (Page size = 4k)
Page #: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Page Frame #: 3 x 1 x 0 x 2 x 5 x 7 4 6 x x x
How can I convert an arbitrary logical address like 51996 into a physical memory address?
If I take log base 2 (4096), I get 12. I think this is how many bits I'm suppose to use for the offset of my address.
I'm just not sure. 51996 / 4096 = 12.69. So does this mean it lay on page#12 with a certain offset?
How do I then turn that into the physical address of "51996"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要确定给定内存地址的页,请取(N 位中的)前 P 位数字。
P = lg2(页数)
在您的示例中,P=lg2(16)=4
因此给定内存地址的前 4 位将告诉我们页面。 这意味着其余部分应该是距该页面开头的偏移量。
您的示例地址 51996 的二进制值为 1100101100011100。 即[1100:101100011100]。
1100(十进制 12)是页码
101100011100(十进制为2844)是偏移量
现在我们需要找到第12页在内存中的位置。
查看您的帧表,第 12 页似乎驻留在第 6 帧中。 在所有内存均可分页的系统中(即没有内存映射 IO),第 6 个页框将位于 (entriesPerPage*frameNum)-1
在这种情况下,4000*6-1 = 23999 (需要“-1”,因为内存是 0 索引的。)在这种情况下,4096*6-1 = 24575(需要“-1”,因为内存是 0 -索引。)
现在我们要做的就是添加偏移量,我们就得到了物理内存地址:
23999 + 2844=26843 = 0x68DB24575 + 2844 = 27419 = 0x6B1B
完毕!
希望这(编辑)有帮助 XD
编辑:
感谢杰尔发现了我的错误:)
感谢 user8 发现我的另一个错误! (frameNum 而不是 pageNum)。
To determine the page of a given memory address, take the first P bits (of the N bit) number.
P = lg2(numberOfPages)
In your example, P=lg2(16)=4
So the first 4 bits of a given memory address will tell us the page. That means the rest should be the offset from the start of that page.
Your example address, 51996, is 1100101100011100 in binary. I.e. [1100:101100011100].
1100 (12 in decimal) is the page number
101100011100 (2844 in decimal) is the offset
Now we need to find where page 12 is in memory.
Looking at your frame table, it appears that page 12 is resident in the 6th frame. In a system where all memory is pageable (i.e. no memory mapped IO) the 6th page frame will be at (entriesPerPage*frameNum)-1
In this case, 4000*6-1 = 23999 (The "-1" is needed since memory is 0-indexed.)In this case, 4096*6-1 = 24575 (The "-1" is needed since memory is 0-indexed.)
Now all we have to do is add the offset and we have the physical memory address:
23999 + 2844=26843 = 0x68DB24575 + 2844 = 27419 = 0x6B1B
Done!
Hope this (edit) was helpful XD
Edit:
Thanks to Jel for catching my mistake :)
Thanks to user8 for catching my other mistake! (frameNum instead of pageNum).
如果我正确理解你的问题(我可能没有),你想知道如何使用页表结构从虚拟地址找到物理地址。 在这种情况下,假装您是处理者。 使用地址的10个最高有效位来查找页目录中的页表(顶级页表)。 接下来的 10 位是页表(较低级别页表)的索引。 使用该页表条目中的地址来查找物理页地址。 最后十位是页中的字节地址。
顺便说一句,您可能会在面向操作系统的网站(例如 OSDev)上找到更多能够理解此类问题的人。 我无法在这个答案中详细说明,因为我已经很多年没有做过此类事情了。
If I understand your question correctly (I probably don't), you want to know how to find the physical address from the virtual address using the page table structures. In that case, pretend you are the processor. Use the 10 most significant bits of the address to find the page table in the page directory (the top level page table). The next 10 bits are the index into the page table (the lower level page table). Use the address in that page table entry to find the physical page address. The last ten bits are the byte address into the page.
By the way, you would probably find a lot more people who would understand this type of question at an OS oriented site such as OSDev. I couldn't really go into much detail in this answer because I haven't done this type of stuff in years.
这可能会有所帮助:
This is might help:
第一步:51996 / 4000 = 12 -> p ,仍=3996→ d(偏移)。
现在看表 p(12) = 6
第二步 : (6*4000) + 3996 : 27996
物理地址是 27996。
first step : 51996 / 4000 = 12 -> p , remain= 3996 -> d (offset).
now look at the table p(12) = 6
second step : (6*4000) + 3996 : 27996
the physical address is 27996.
以下页表适用于具有 16 位虚拟地址和物理地址以及 4,096 字节页的系统。 当页面被引用时,引用位被设置为1。 线程定期将引用位的所有值清零。 页框的破折号表示该页不在内存中。 页面替换算法是本地化的 LRU,并且所有数字均以十进制形式提供。
页页框参考位
0 9 0
1 1 0
2 14 0
3 10 0
4 - 0
5 13 0
6 8 0
7 15 0
8 0 0
9 - 0
10 5 0
11 4 0
12 - 0
13 3 0
14 - 0
15 2 0
A。 将以下虚拟地址(十六进制)转换为等效的物理地址(以十六进制和十进制提供答案)。 还要为页表中的相应条目设置引用位。 (3)
我。 0xBC2C
二. 0x00ED
三. 0xEA14
四. 0x6901
v.0x23A1
六. 0xA999
The following page table is for a system with 16-bit virtual and physical addresses and with 4,096-byte pages. The reference bit is set to 1 when the page has been referenced. Periodically, a thread zeroes out all values of the reference bit. A dash for a page frame indicates the page is not in memory. The page-replacement algorithm is localized LRU, and all numbers are provided in decimal.
Page Page Frame Reference Bit
0 9 0
1 1 0
2 14 0
3 10 0
4 - 0
5 13 0
6 8 0
7 15 0
8 0 0
9 - 0
10 5 0
11 4 0
12 - 0
13 3 0
14 - 0
15 2 0
a. Convert the following virtual addresses (in hexadecimal) to the equivalent physical addresses (provide answers in hexadecimal AND decimal). Also set the reference bit for the appropriate entry in the page table. (3)
i. 0xBC2C
ii. 0x00ED
iii. 0xEA14
iv. 0x6901
v. 0x23A1
vi. 0xA999