Operating Systems: Three Easy Pieces学习笔记 - Virtualization - Mechaism: Address Translation

Mechanism: Address Translation

Crux: How to efficiently and flexibly virtualize memory?

We use hardware-based address translation, or just address translation for short. With address translation, the hardware transforms each memory access, changing the virtual address provided by the instruction to a physical address where the desired information is actually located.
Hardware only cannot virtualize memory, and the OS must get involved at key points to set up the hardware so that the correct translations take place; it must thus manage memory, keeping track of which locations are free and which are in use, and judiciously intervening to maintain control over how memory is used.

Assumptions

  1. The 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

Imagine there is a process whose address space as indicated in Figure 15.1.


image.png

It is a short code sequence as follows:

void func()
    int x;
    ...
    x = x + 3; // this is the line of code we are interested in

The compiler turns this line of code into assembly, which might look something like this in x86:

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

It presumes that the address of x has been placed in the register ebx, and then loads the value at the address into the general-purpose register eax using the movl instruction (for "longword" move). The next instruction adds 3 to eax, and the final instruction stores the value in eax back into memory at the same location.
You can see how both the code and data are laid out in the process's address space in Figure 15.1.
An example of what physical memory might look like once this process's address has been placed in memory is found in Figure 15.2.

image.png

In Figure 15.2, the OS uses the first slot of physical memory for itself, and it has relocated the process from the example above into the slot starting at physical memory address 32KB. The other two slots are free (16 KB - 32 KB and 48 KB - 64 KB).
Here comes the question: how can we relocate this process in memory in a way that is transparent to the process?

Dynamic (Hardware-based) Relocation

This method is also referred to as base and bounds.
We'll need two hardware registers within each CPU: one base register, and the other the bounds (or limit) register. This base-and-bounds pair is going to allow us to place the address space anywhere we'd like in physical memory while ensuring that the process can only access its own address space.
Each program is written and compiled as if it is loaded at address 0. But when it starts running, the OS decides where in physical memory it should be loaded and sets the base register to that value. In the example in Figure 15.2, the value of the base register is 32KB.
When any memory reference is generated by the process, it is translated by the processor in the following manner:

image.png

Each memory reference generated by the process is a *virtual address; the hardware in turn adds the contents of the base register to this address and the result is a physical address that can be issued to the memory system.
In the example above, consider the following instruction:

128: movl 0x0(%ebx), %eax

The PC is set to 128. When the hardware needs to fetch this instruction, it first adds the value to the base register value of 32 KB (32768) to get a physical address of 32896; the hardware then fetches the instruction from that physical address. Next, the processor begins executing the instruction. At some point, the process then issues the load from virtual address 15 KB (the address stored in ebx), which the processor takes and again adds to the base register (32 KB), getting the final physical address of 47 KB and thus the desired contents.
This relocation of the address happens at runtime, and because we can move address spaces even after the process has started running, the technique is often referred to as dynamic relocation.
As for the bounds register, it helps with protection. Specifically, the processor will first check that the memory reference is within bounds to make sure it is legal; in the example above, the bounds register would always be set to 16KB. If a process generates a virtual address that is greater than the bounds, or one that is negative, the CPU will raise an exception, and the process will likely be terminated. The point of the bounds is thus to make sure that all addresses generated by the process are legal and within the “bounds” of the process.
The base and bounds registers are hardware structures kept on the chip (one pair per CPU), and they are also called the memory management unit (MMU).
The bounds register can either hold the size of the address space or the physical address of the end of the address space, but the first method is simpler.

OS Issues

1. The OS must take action when a process is created, finding space for its address space in memory.

Under our assumptions, the OS can simply view physical memory as an array of slots, and track whether each one is free or in use. 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 puts its memory back on the free list, and cleans up any associated data structures as need be.

3. The OS must take action when a context switch occurs.

The values of the base and bounds register differ for each running program, thus, the OS must save and restore these register when it switches between processes. Specifically, the values are saved in a per-process structure such as the process structure or process control block (PCB).

When a process is stopped, it is possible for the OS to move an address space from one location in memory to another, and the value of the base register that is stored in the PCB should also be updated.
Access to the base and bounds registers is privileged.

Summary

This simple structure has its inefficiencies. As you can see in Figure 15.2, the relocated process is using physical memory from 32 KB to 48 KB; however, because the process stack and heap are not too big, all of the space between the two is simply wasted, which is called internal fragmentation.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容