如何使用页表将虚拟地址转换为物理地址? [英] How can I use a page table to convert a virtual address into a physical one?

查看:456
本文介绍了如何使用页表将虚拟地址转换为物理地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个普通的页表:

Lets say I have a normal page table:

页面表(页面大小= 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这样的任意逻辑地址转换为物理内存地址?

How can I convert an arbitrary logical address like 51996 into a physical memory address?

如果我以2为底的对数(4096),我得到12.我认为这是我应该用于地址偏移量的位数.

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.

我不确定. 51996/4096 = 12.69.那么这是否意味着它以一定的偏移量放置在第12页上?

I'm just not sure. 51996 / 4096 = 12.69. So does this mean it lay on page#12 with a certain offset?

然后我如何将其转换为"51996"的物理地址?

How do I then turn that into the physical address of "51996"?

推荐答案

要确定给定内存地址的页面,请获取(N位中的)前P位.

To determine the page of a given memory address, take the first P bits (of the N bit) number.

P = lg2(numberOfPages)
在您的示例中,P = lg2(16)= 4

P = lg2(numberOfPages)
In your example, P=lg2(16)=4

因此,给定内存地址的前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.

您的示例地址51996是二进制的1100101100011100. IE. [1100:101100011100].

Your example address, 51996, is 1100101100011100 in binary. I.e. [1100:101100011100].

1100(十进制12)是页码
101100011100(十进制2844)是偏移量

1100 (12 in decimal) is the page number
101100011100 (2844 in decimal) is the offset

现在,我们需要找到第12页在内存中的位置.
查看您的框架表,似乎第12页驻留在第6框架中.在所有内存都是可分页的系统(即没有内存映射的IO)中,第6页帧将位于(entriesPerPage * frameNum)-1

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

在这种情况下,4000 * 6-1 = 23999(由于内存索引为0,因此需要"-1".)

在这种情况下, 4096 * 6-1 = 24575(由于内存索引为0,因此需要"-1".)

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 = 0x68DB

24575 + 2844 = 27419 = 0x6B1B

24575 + 2844 = 27419 = 0x6B1B

完成!

希望此(编辑)对XD很有帮助

Hope this (edit) was helpful XD

感谢Jel抓住了我的错误:) 感谢user8捕捉到我的另一个错误! (frameNum而不是pageNum).

Thanks to Jel for catching my mistake :) Thanks to user8 for catching my other mistake! (frameNum instead of pageNum).

这篇关于如何使用页表将虚拟地址转换为物理地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆