memory: segmentation

28
1 Memory: Segmentation Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity 求于至简,归于永恒

Upload: lenhan

Post on 14-Feb-2017

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Memory: Segmentation

11

Memory: SegmentationInstructor: Hengming Zou, Ph.D.

In Pursuit of Absolute Simplicity 求于至简,归于永恒

Page 2: Memory: Segmentation

22

Content

Logical segmentation Segmentation with paging Case study: Multics

Page 3: Memory: Segmentation

33

(Logical) Segmentation

Segmentation divides both physical and virtual memory into segments– i.e. regions of contiguous memory space

Each segment is dedicated to one or more sections of a process– i.e. logical unit of separation, such as data, code, stack, etc.

The pure segmentation use entire process

Page 4: Memory: Segmentation

44

Segmentation

Allows each table to grow or shrink, independently

Page 5: Memory: Segmentation

55

Segmentation

Let’s generalize this to allow multiple segments– described by a table of base & bound pairs

Segment # Base Bound Description0 4000 700 Code segment1 0 500 Data segment2 Unused3 2000 1000 Stack segment

Page 6: Memory: Segmentation

66

Segmentation

data

stack

code

code

data

stack

Physical memory

Virtual memorysegment 1

Virtual memorysegment 3

Virtual memorysegment 0

6ff0

4ff0

fff0

46ff4000

2fff2000

4ff0

Page 7: Memory: Segmentation

77

Segmentation

Note that not all virtual addresses are valid– e.g. no valid data in segment 2,

→i.e. the process has no such specific segment– no valid data in segment 1 above 4ff

Valid means the region is part of the process’s virtual address space– Could be multiple set of virtual address spaces

Invalid means this virtual address is illegal for the process to access– Accesses to invalid address will cause OS to take corrective measures– usually a core dump

Page 8: Memory: Segmentation

88

Segmentation

Protection: – different segments can have different protection– e.g. code can be read-only (allows inst. fetch, load)– e.g. data is read/write (allows fetch, load, store)

In contrast, pure segmentation gives same protection to entire space

Page 9: Memory: Segmentation

99

Segmentation

In segmentation, a virtual address takes the form:– (virtual segment #, offset)

Could specify virtual segment # via – The high bits of the address, – Or a special register, – Or implicit to the instruction opcode

Page 10: Memory: Segmentation

1010

Implementation of Segmentation

Page 11: Memory: Segmentation

1111

Segmentation

What must be changed on a context switch?– Segmentation table or descriptor segment

Page 12: Memory: Segmentation

1212

Pros and Cons of Segmentation

+ easy to share whole segments without sharing entire address space + avoid collision within virtual address space + works well for sparse address spaces

– with big gaps of invalid areas

- complex memory allocation - external fragmentation

Page 13: Memory: Segmentation

1313

Segmentation

How to make memory allocation easy and – But still keeps the advantages of segmentation?

SEGMENTATION WITH PAGING!– Divide program into logical segmentations– Use paging within each segment

Page 14: Memory: Segmentation

1414

Segmentation with Paging

Page 15: Memory: Segmentation

1515

Segmentation with Paging

Descriptor segment points to page tables Page tables points to physical frames MULTICS use this method

Page 16: Memory: Segmentation

1616

Compare Paging and Segmentation

Consideration Paging Segmentation

Need programmer aware that this technique is being used

No Yes

How many linear address spaces? 1 Many

Can total address space exceed the size of physical memory

Yes Yes

Can procedures and data be distinguished & separately protected

No Yes

Can tables size fluctuate easily? No Yes

Sharing of procedures between users? No Yes

Why was this technique invented To get a large linear address space without buying more memory

Allow programs & data to be broken up into logically independent spaces and to aid sharing & protection

Page 17: Memory: Segmentation

SP Case Study

Page 18: Memory: Segmentation

1818

SP Example: MULTICS

A 34-bit MULTICS virtual address

Segment number

Address withinthe segment

18

Pagenumber

Offset withinthe page

6 10

Page 19: Memory: Segmentation

1919

SP Example: MULTICS

Segment number

MULTICS virtual space

18

Pagenumber

Offset withinthe page

6 10

Descriptor Page frame Word

Segmentnumber

Pagetable

Page

Pagenumber

Descriptorsegment

offset

Page 20: Memory: Segmentation

2020

SP Example: MULTICS TLB

Page 21: Memory: Segmentation

2121

SP Example: MULTICS

Do not show this slide Simplified version of the MULTICS TLB Existence of 2 page sizes makes actual TLB more complicated

Page 22: Memory: Segmentation

2222

SP Example: Pentium

Pentium virtual memory contains two tables: Global Descriptor Table:

– Describes system segments, including OS

Local Descriptor Table:– Describes segments local to each program

Page 23: Memory: Segmentation

2323

SP Example: Pentium

Pentium selector contains a bit to indicate if the segment is local or global

LDT or GDT entry numbers

1 213Bits

0 = GDT/1 = LDT Privilege level (0-3)

A Pentium selector

Page 24: Memory: Segmentation

2424

SP Example: Pentium

Pentium code segment descriptor (Data segments differ slightly)

Page 25: Memory: Segmentation

2525

SP Example: Pentium

Conversion of a (selector, offset) pair to a linear address

Page 26: Memory: Segmentation

2626

Pentium Address Mapping

Page 27: Memory: Segmentation

2727

Protection on the Pentium

Level

Page 28: Memory: Segmentation

Computer Changes Life