Operating Systems: Three Easy Pieces学习笔记 - Virtualization - Segmentation

Segmentation

Crux: How to support a large address space?

Segmentation: Generalized Base/Bounds

Instead of having just one base and bounds pair in our MMU, we have a base and bounds pair per logical segment of the address space.
A segment is a contiguous portion of the address space of a particular length, and in our canonical address space, we have three logically-different segments: code, stack and heap.Segmentation allows the OS to place each one of those segments in different parts of physical memory, and thus avoid filling physical memory with unused virtual address space.

image.png

Consider the address space of a program given in Figure 16.1. With a base and bounds pair per segment, we can place each segment independently in physical memory like in Figure 16.2.
image.png

Only used memory is allocated space in physical memory, and thus large address spaces with large amounts of unused address can be accommodated.
For more details about address translation, take a look at P143 of the book.

Which Segment Are We Referring To?

1. Explicit Approach

Chop up the address space into segments based on the top few bits of the virtual address. In the example above, we have three segments, thus we need two bits to accomplish this task. We use the top two bits of the 14-bit virtual address to select the segment. The virtual address looks like this:


image.png
  • If the top two bits are 00, the hardware knows the virtual address is in the code segment, and thus uses the code base and bounds pair to relocate the address to the correct physical location.
  • If the top two bits are 01, the hardware knows the address is in the heap, and thus uses the heap base and bounds.
    Consider there is an access to virtual address 4200, which in binary form looks like:
    image.png

    The top two bits (01) tell the hardware which segment we are referring to. The bottom 12 bits are the offset into the segment: 0000 0110 1000, or 104 in decimal. Thus, the hardware simply takes the first two bits to determine which segment register
    to use, and then takes the next 12 bits as the offset into the segment. By adding the base register to the offset, the hardware arrives at the final physical address.
    If base and bounds were arrays, the hardware would be doing something like this to obtain the desired physical address:
    image.png

    In the example above, SEG_MASK would be 0x3000, SEG_SHIFT would be 12 and OFFSET_MASK would be 0xFFF.

2. Implicit Approach

The hardware determines the segment by noticing how the address was formed. If, for example, the address was generated from the program counter (i.e., it was an instruction fetch), then the address is within the code segment; if the address is based off of the stack or base pointer, it must be in the stack segment; any other address must be in the heap.

What About The Stack?

In the example above, the stack has been relocated to physical address 28KB, but stack grows backwards in physical memory, it starts at 28KB and grows back to 26KB, corresponding to virtual addresses 16KB to 14KB. Therefore, translation has to proceed differently.
What we need is an extra hardware (one bit, for example) to know which way the segment grows:

image.png

In this example, assume we wish to access virtual address 15KB, which should map to physical address 27KB. Our virtual address, in binary form, thus looks like this: 11 1100 0000 0000 (hex0x3C00). The hardware uses the top two bits (11) to designate the segment, but then we are left with an offset of 3KB. To obtain the correct negative offset, we must subtract the maximum segment size from 3KB: in this example, a segment can be 4KB, and thus the correct negative offset is 3KB - 4KB which equals -1KB. We simply add the negative offset (-1KB) to the base (28KB) to arrive at the correct physical address: 27KB. The bounds check can be calculated by ensuring the absolute value of the negative offset is less than the segment’s size.

Support for Sharing

To save memory, sometimes it is useful to share certain memory segments between address spaces. To support sharing, we need a little extra support from the hardware, in the form of protection bits. Basic support adds a few bits per segment,
indicating whether or not a program can read or write a segment, or perhaps execute code that lies within the segment.

image.png

With protection bits, the hardware must also check whether a particular access is permissible.

Fine-grained vs. Coarse-grained Segmentation

Most of our examples thus far have focused on systems with just a
few segments (i.e., code, stack, heap); we can think of this segmentation as coarse-grained, as it chops up the address space into relatively large, coarse chunks.
Supporting many segments requires even further hardware support, with a segment table of some kind stored in memory.

OS Support

Segmentation raises a number of new issues:

  1. What should the OS do on a context switch?
  2. How to manage free space in physical memory?

The physical memory quickly becomes full of little holes of free space, making it difficult to allocate new segments, or to grow existing ones. This is called external fragmentation, as in Figure 16.3 (left).

image.png

One solution to this problem is to compact physical memory by rearranging the existing segments. The OS could stop whichever processes are running, copy their data to one contiguous region of memory, change their segment register values to point to the new physical locations, and thus have a large free extent of memory with which to work. Figure 16.3 (right) shows a diagram of compacted physical memory.
However, compaction is expensive, as copying segments is memory-intensive and thus would use a fair amount of processor time.
A simpler approach is to use a free-list management algorithm that tries to keep large extents of memory available for allocation, such as best-fit, worst-fit, first-fit and buddy algorithm. But no matter how smart the algorithm, external fragmentation still exists.
Actually, the only one solution is to avoid the problem altogether, which is to never allocate memory in variable-sized chunks.

Summary

Benefits of Segmentation

  1. Dynamic relocation
  2. Better support for sparse address spaces
  3. Fast and well-suited to hardware
  4. Code sharing

Problems

  1. External fragmentation
  2. If the model of how the address space is being used doesn't exactly match how the underlying segmentation has been designed to support it, segmentation doesn't work very well
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念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

推荐阅读更多精彩内容