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.
...........略
...略