操作系统导论习题解答(15. Address Translation)

Mechanism: Address Translation

In developing the virtualization of the CPU, we focused on a general mechanism known as limited direct execution (or LDE ).

1. An Example

void func () {
	int x = 3000;
	x = x + 3;
}

对于这个程序,它的存储地址如下:在这里插入图片描述
对应的汇编代码如下:

128: movl 0x0(%ebx), %eax
132: addl $0x03, %eax
135: movl %eax, 0x0(%ebx)

在这里插入图片描述
上面这个程序从地址0开始并且最大到16KB,但是对于虚拟内存而言,操作系统想要放置这个程序在任何地址,不一定从地址0开始。下面这个图片就是把程序放在以32KB地址开头的位置。在这里插入图片描述

2. Dynamic (Hardware-based) Relocation

dynamic relocation : base and bounds.
对于每个CPU,都有2个硬件寄存器,一个为base register,一个为 bounds register.
每个程序的实际物理地址就等于虚拟地址加上基准地址。如下:在这里插入图片描述
以上图(Figure 15.2)所示,程序的首地址为32KB,那么就设置base register为这个值。再看上上图(Figure 15.1),程序自己觉得自己的地址是从0开始,所以virtual address就为0。

bounds register的作用就是保护,bounds register的值有两种定义方式。一种是定义为程序总的长度,另一种是定义为程序最后结束的实际物理地址值。
我们定义它采用第一种方式,看上面那个程序,程序总的地址长度为16KB,那么bounds register的值就为16KB,如果发生越界,操作系统就抛出异常。

3. Homework (Simulation)

The program relocation.py allows you to see how address translations are performed in a system with base and bounds registers. See the README for details.在这里插入图片描述

3.1 Question & Answer

1. Run with seeds 1, 2, and 3, and compute whether each virtual address generated by the process is in or out of bounds. If in bounds, compute the translation.

在这里插入图片描述
只有 VA 1 没有超出界限。
VA 1 --> VALID: 0x00003741(decimal: 14145)
只要VA N (N = 0, 1, 2, 3, 4,...)的 decimal < Limit,就没有超出界限。
对于没有超出界限的有: VALID = Base + Virtual Address在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

2. Run with these flags: -s 0 -n 10. What value do you have set -l (the bounds register) to in order to ensure that all the generated virtual addresses are within bounds?

在这里插入图片描述
从上图我们可以看出,VA中最大的为dicimal 929,我们就可以把Limit设置成930(为什么不是929?这是要特别注意的地方)。

在这里插入图片描述

在这里插入图片描述

3. Run with these flags: -s 1 -n 10 -l 100. What is the maximum value that base can be set to, such that the address space still fits into physical memory in its entirety?

在这里插入图片描述
我们看一下上图,Limit的值为100,而物理地址的值= Base + VA,能够表示的物理地址最大值 = Base + Limit,故只要Limit >= VA,那么无论Base设置什么值,都超出了范围。
不存在可以设置的Base的最大值,使得所有地址空间适合物理内存。

4. Run some of the same problems above, but with larger address spaces (-a) and physical memories (-p).

在这里插入图片描述在这里插入图片描述
从上面这两副图,我们可以看到当把 address space size 扩大1倍后(从1K(默认值) --> 2K),相应的VA 的大小也扩大了1倍。

在这里插入图片描述

在这里插入图片描述
从上面两幅图,我们可以看到当把 phys mem size 扩大1倍后(从13884(默认值) --> 27768),相应的 Base的大小也扩大了1倍。

5. What fraction of randomly-generated virtual addresses are valid, as a function of the value of the bounds register? Make a graph from running with different random seeds, with limit values ranging from 0 up to the maximum size of the address space.

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述
...........略

在这里插入图片描述
...略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值