操作系统导论习题解答(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.

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

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

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

posted @   astralcon  阅读(146)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示