sharing main memory, segmentation, simple paging aravind …ee469/lectures/469_lec13... · 2021. 2....

25
1 Sharing Main Memory, Segmentation, Simple Paging ECE469, Mar 02 Aravind Machiry

Upload: others

Post on 11-Aug-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

1

Sharing Main Memory,Segmentation,Simple Paging

ECE469, Mar 02

Aravind Machiry

Page 2: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

2

Reading assignment

● Dinosaur Chapter 8

● Comet Chapter 13, 15, 16

Page 3: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

3

Connecting the dots

main.c math.c

main.o math.o a.out

Load a.out to memManage mem for proc

Instructionexecution

compiler linker loader

memorymanagement

arch

Page 4: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

4

OS

Segment 1

address 0

1. Simple uniprogramming:Single segment (code, data, stack heap) per process

Physical memory

Page 5: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

5

OS

Segment 1

Segment 2

2. Simple multiprogramming:Single segment per process, static relocation

Page 6: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

Static Relocation

foo() {..

main() {..

call N+0x30

0x30

0x20

0x0

N+ 0x30

N

Compiler View Loaded at N

OS Will relocate ONCE during the load time!

Page 7: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

7

● Four drawbacks1. No protection2. Low utilization -- Cannot relocate dynamically

● Binary is fixed (after loading)● Cannot do anything about holes

3. No sharing -- Single segment per process● Cannot share part of process address space (e.g. text)

4. Entire address space needs to fit in mem● Need to swap whole, very expensive!

Simple multiprogramming:Single segment per process, static relocation

Page 8: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

What else can we do?

● Already tried● Compile time / linking time● Loading time

● Let us try execution time!

8

Page 9: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

9

3. Dynamic memory relocation

● Instead of changing the address of a program before it’s loaded, change the address dynamically during every reference● Under dynamic relocation, each

program-generated address (called a logical address or virtual address) is translated in hardware to a physical or real address

Page 10: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

10

Translation overview

● Actual translation process is usually performed by hardware

● Translation table is set up by software

● CPU view● what program sees, virtual

addresses

● Memory view● physical memory addresses

Translation

CPU

virtual address

Physicalmemory

physical address

I/Odevice

Page 11: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

Compilation and Linking is simplified

● Generate executable assuming load address is always 0x0

11

Page 12: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

12

Base and bound

● OS can load at the executable starting at arbitrary physical address: base.

● A program can only access physical memory in [base, base+bound]

● On a context switch: save/restore base, bound registers

virtual address

base

bound

error

+

>

physical address

Page 13: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

13

Base and bound

● The essence: ● A level of indirection

● Phy. Addr = Vir. Addr + basevirtual address

base

bound

error

+

>

physical address

Page 14: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

14

Base and bound

● Pros: ● simple, fast translation,

cheap● Can relocate segment: just

change the base!!● Protects process from

corrupting each other:● How? (Hint: error)

virtual address

base

bound

error

+

>

physical address

Page 15: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

15

Base and bound

● Cons:● Only one segment per

process

● How can two processes share code while keeping private data areas (shared editors)?● Can it be done safely with

a single-segment scheme?

virtual address

base

bound

error

+

>

physical address

Page 16: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

16

● Four drawbacks1. No protection2. Low utilization -- Cannot relocate dynamically

● Binary is fixed (after loading)● Cannot do anything about holes

3. No sharing -- Single segment per process● Cannot share part of process address space (e.g. text)

4. Entire address space needs to fit in mem● Need to swap whole, very expensive!

What have we solved?

Page 17: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

17

Multiple Segments

● Split the process address space into multiple segments each of same size.

● Example:14-bit address space, i.e., 0x0000 - 0x3FFF (16 KB): 4 Segments of 4K bytes each.● 0x0000 - 0x0FFF● 0x1000 - 0x1FFF● 0x2000 - 0x2FFF● 0x3000 - 0x3FFF

14-bit address: Top 2-bits indicate segment number. Rest 12-bits indicate offset with in the segment.

Page 18: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

18

Multiple Segments

● Have a table of (seg, size)

● Further protection: each entry has(nil, read, write, exec)

● On a context switch: save/restore the table (or a pointer to the table) in kernel memory physical address

+

segment offset

Virtual address

Seg base size

...

>

errorYes

No

Page 19: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

19

Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:

2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 ← Code: Read Only

1 0 6A0 11 ← Date: Read Write

2 3000 FFF 11 ← Heap: Read Write

3 -- -- 00 ← Unused

Page 20: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

20

Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:

2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 -> 4000 + 1F4 = 41F4 1 0 6A0 11 2 3000 FFF 11 3 -- -- 00

🡺 Where is 01F4 in physical memory?

Page 21: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

21

Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:

2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 1 0000 6A0 11 -> 0000 + 5DC = 05DC 2 3000 FFF 11 3 -- -- 00

🡺 Where is 15DC in physical memory?

Page 22: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

How does this allow two processes to share code segment?

22

Seg Base Bounds RW

0 4000 4B0 10

1 0000 6A0 11

2 3000 FFF 11

3 -- -- 00

Seg Base Bounds RW

0 5000 1C0 11

1 -- -- 00

2 4000 4B0 10

3 -- -- 00

Process A Process B

● Process A and B have their different segments map to the same physical segment i.e., One copy of physical code segment (0x4000-0x44B0) shared between 2 processes.

Page 23: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

23

Pros/cons of segmentation

● Pros: ● Process can be split among several segments

● Allows sharing● Segments can be assigned, moved, or swapped

independently

● Cons:● External fragmentation: many holes in physical

memory● Also happens in base and bound scheme

Page 24: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

24

OS

Segment 2

Simple multiprogramming:Single segment per process, static relocation

Segment 3

Segment 4?

External fragmentation

Page 25: Sharing Main Memory, Segmentation, Simple Paging Aravind …ee469/lectures/469_lec13... · 2021. 2. 27. · linker loader memory management arch. 4 OS Segment 1 address 0 1. Simple

25

What fundamentally causes external fragmentation?

1. Segments of many different sizes

2. Each has to be allocated contiguously