ostep15-Mechanism:Address Translation

ostep15-Mechanism:Address Translation

Assumptions

1、user’s address space must be placed contiguously in physical memory

2、the size of the address space is not too big; specifically, that it is less than the size of physical memory.

3、each address space is exactly the same size.

An Example

virtual address

physical address

instructions

128: movl 0x0(%ebx), %eax ;load 0+ebx into eax
132: addl $0x03, %eax ;add 3 to eax register
135: movl %eax, 0x0(%ebx) ;store eax back to mem

procedure

• Fetch instruction at address 128
• Execute this instruction (load from address 15 KB)
• Fetch instruction at address 132
• Execute this instruction (no memory reference)
• Fetch the instruction at address 135
• Execute this instruction (store to address 15 KB)

address translation

Transforming a virtual address into a physical address is exactly the technique.

static relocation

.c 文件在编译 后成为不同的目标模块(.obj),这些装入模块都是逻辑编址,每个模块从0开始;经过链接器链接之后形成的装入模块(PE/ELF),当然装入模块也是从0开始编址,也是一个逻辑地址。

PE/ELF文件装入内存之后,CPU需要知道真实的内存地址和程序的逻辑地址之间的对应关系,而static relocation就是表示在程序运行之前,就把上述映射关系设定好,CPU按照逻辑地址就可以知道物理内存上的地址,也就可以读取、执行指定的指令了。

-

优点:无需硬件支持
缺点:
    1)程序重定位之后就不能在内存中搬动了;
    2)要求程序的存储空间是连续的,不能把程序放在若干个不连续的区域中。

dynamic (hardware-based) relocation

the hardware takes a virtual address the process thinks it is referencing and transforms it into a physical address which is where the data actually resides.

dynamic relocation就是在程序运行的过程中进行地址映射;即CPU每次访问内存单元的时候进行地址变换。实现这种机制需要 硬件支持,即需要寄存器动态的指定PE/ELF中的基地址。

-

优点:
    1)目标模块装入内存时无需任何修改,因而装入之后再搬迁也不会影响其正确执行,这对于存储器紧缩、解决碎片问题是极其有利的;
    2)一个程序由若干个相对独立的目标模块组成时,每个目标模块各装入一个存储区域,这些存储区域可以不是顺序相邻的,只要各个模块有自己对应的定位寄存器就行。
缺点:需要硬件支持。

mechanism

need two hardware registers within each CPU: one is called the base register , and the other the bounds (sometimes called a limit register).

With dynamic relocation, we can see how a little hardware goes a long way. Namely, a base register is used to transform virtual addresses (generated by the program) into physical addresses. A bounds (or limit) register ensures that such addresses are within the confines of the address space. Together, they combine to provide a simple and efficient  virtualization of memory.

Sometimes people call the part of the processor that helps with address translation the memory management unit (MMU)

the virtual address is “too big” or negative will the result be a fault.(protection)

free list

The OS must track which parts of free memory are not in use, so as to be able to allocate memory to processes.

free list is a list of the ranges of the physical memory which are not currently in use.

OS Issues

1、The OS must take action when a process is created, finding space for its address space in memory. When a new process is created, the OS will have to search a data structure (often called a free list) to find room for the new address space and then mark it used.

2、The OS must take action when a process is terminated, reclaiming all of its memory for use in other processes or the OS. Upon termination of a process, the OS thus puts its memory back on the free list, and cleans up any associated data structures as need be.

3、The OS must also take action when a context switch occurs.Cause there is only one base and bounds register on each CPU.The OS must save and restore the base-and-bounds pair when it switches between processes.

Other Issues

We should note that when a process is stopped (i.e., not running), it is possible for the OS to move an address space from one location in memory to another rather easily. To move a process’s address space, the OS first deschedules the process; then, the OS copies the address space from     the current location to the new location; finally, the OS updates the saved base register (in the process structure) to point to the new location. When the process is resumed, its (new) base register is restored, and it begins running again, oblivious that its instructions and data are now in a completely new spot in memory!

We should also note that access to the base and bounds registers is obviously privileged. Special hardware instructions are required to access base-and-bounds registers; if a process, running in user mode, attempts to do so, the CPU will raise an exception and the OS will likely terminate the process. Only in kernel (or privileged) mode can such registers be modified. Imagine the havoc a user process could wreak1 if it could arbitrarily change the base register while running. Imagine it! And then quickly flush such dark thoughts from your mind, as they are the ghastly stuff of which nightmares are made.

summary

efficient

protection

internal fragmentation
因为动态重定向会导致内存碎片

segmentation


参考:
http://www.xuebuyuan.com/1252408.html

http://blog.csdn.net/kennyrose/article/details/7531718

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值