Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating...

66
Memory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: Wisconsin CS-537 materials created by Remzi Arpaci-Dusseau. Licensed for use under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Transcript of Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating...

Page 1: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Memory Management: Address space and

SegmentationCS 571: Operating Systems (Spring 2020)

Lecture 6a

Yue Cheng

Some material taken/derived from: • Wisconsin CS-537 materials created by Remzi Arpaci-Dusseau.Licensed for use under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Page 2: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Announcement

• HW1 posted on BB

• Due next Monday, 03/09, 11:59pm

Y. Cheng GMU CS571 Spring 2020 2

Page 3: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Today’s outline

1. Address space

2. Virtual memory accesses

3. Relocation

4. Segmentation

3Y. Cheng GMU CS571 Spring 2020

Page 4: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Early Systems

• OS was a set of libraries• OS sat in memory starting

at physical address 0• The rest was used by

running program

4Y. Cheng GMU CS571 Spring 2020

Page 5: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Multiprogramming & Time Sharing

• OS makes sure each process is confined to its own address space in memory• One naïve

implementation: • <base register & limit

register> pair

5

Base reg

Limit reg

Y. Cheng GMU CS571 Spring 2020

Page 6: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

The Abstraction

• A process has a set of addresses that map to a collection of bytes

• This set is called an address space

• Review: what stuff is in an address space?

6Y. Cheng GMU CS571 Spring 2020

Page 7: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Match that Segment!

7

int x;int main(int argc, char *argv[]) {

int y;int *z = malloc(sizeof(int));

}

x codemain data

y heapz stack

Y. Cheng GMU CS571 Spring 2020

Page 8: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Match that Segment!

8

x codemain data

y heapz stack

int x;int main(int argc, char *argv[]) {

int y;int *z = malloc(sizeof(int));

}

Y. Cheng GMU CS571 Spring 2020

Page 9: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Match that Segment!

9

x codemain

y heapz stack

In OSTEP

int x;int main(int argc, char *argv[]) {

int y;int *z = malloc(sizeof(int));

}

Y. Cheng GMU CS571 Spring 2020

Page 10: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

The Address Space• Address space

• An easy-to-use abstraction of physical memory

• The address space is the running program’s view of memory in the system• Virtual address or logical address • Physical address refers to those

seen by the memory unit hardware

• The user program generates logical addresses; it never sees the real physical addresses

10Y. Cheng GMU CS571 Spring 2020

Page 11: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

High-level Goals

• Transparency• User program behaves as if it has its own private

physical memory

• Efficiency• Space and time efficient memory virtualization

• Performance relies on hardware support (e.g., TLBs)

• Protection• Isolation property

• User process shouldn’t access or affect anything outside its own address space

11Y. Cheng GMU CS571 Spring 2020

Page 12: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

All Memory Addresses You See are Virtual• Any address that a programmer can see is a

virtual address

Result:

12Y. Cheng GMU CS571 Spring 2020

Page 13: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

13

_main:0000000100000fa0 pushq %rbp0000000100000fa1 movq %rsp, %rbp0000000100000fa4 xorl %eax, %eax0000000100000fa6 movl %edi, -0x4(%rbp)0000000100000fa9 movq %rsi, -0x10(%rbp)0000000100000fad movl 0x8(%rbp), %edi0000000100000fb0 addl $0x2, %edi0000000100000fb3 movl %edi, 0x8(%rbp)0000000100000fb6 popq %rbp0000000100000fb7 retq

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]) {int x;x = x + 2;

}

% otool -tv demo(or objdump in Linux)

Y. Cheng GMU CS571 Spring 2020

Page 14: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

_main:0000000100000fa0 pushq %rbp0000000100000fa1 movq %rsp, %rbp0000000100000fa4 xorl %eax, %eax0000000100000fa6 movl %edi, -0x4(%rbp)0000000100000fa9 movq %rsi, -0x10(%rbp)0000000100000fad movl 0x8(%rbp), %edi0000000100000fb0 addl $0x2, %edi0000000100000fb3 movl %edi, 0x8(%rbp)0000000100000fb6 popq %rbp0000000100000fb7 retq

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]) {int x;x = x + 2;

}

Virtual Memory Accesses

14

% otool -tv demo(or objdump in Linux)

Y. Cheng GMU CS571 Spring 2020

Page 15: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

15

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

%rip = 0x100000fad%rbp = 0x200

Memory accesses:

Y. Cheng GMU CS571 Spring 2020

Page 16: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Virtual Memory Accesses

16

Memory accesses:Fetch instr. at addr 0x100000fad

%rip = 0x100000fad%rbp = 0x200

Y. Cheng GMU CS571 Spring 2020

Page 17: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Virtual Memory Accesses

17

%rip = 0x100000fad%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Y. Cheng GMU CS571 Spring 2020

Page 18: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

18

%rip = 0x100000fb0%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 19: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

19

%rip = 0x100000fb0%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb00x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 20: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

20

%rip = 0x100000fb0%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb0Exec, no load0x100000fad movl 0x8(%rbp), %edi

0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 21: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

21

%rip = 0x100000fb3%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb0Exec, no load0x100000fad movl 0x8(%rbp), %edi

0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 22: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

22

%rip = 0x100000fb3%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb0Exec, no load

Fetch instr. at addr 0x100000fb3

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 23: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

23

%rip = 0x100000fb3%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb0Exec, no load

Fetch instr. at addr 0x100000fb3Exec, store to addr 0x208

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 24: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Virtual Memory Accesses

24

%rip = 0x100000fb3%rbp = 0x200

Memory accesses:Fetch instr. at addr 0x100000fadExec, load from addr 0x208

Fetch instr. at addr 0x100000fb0Exec, no load

Fetch instr. at addr 0x100000fb3Exec, store to addr 0x208

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

How to relocate the memory access in a way that is transparent to the process?

Y. Cheng GMU CS571 Spring 2020

Page 25: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

How to Run Multiple Programs?

• Approaches:• Static relocation

• Dynamic relocation

• Segmentation

25Y. Cheng GMU CS571 Spring 2020

Page 26: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Static Relocation

• Idea: rewrite each program before loading it into memory as a process

• Each rewrite uses different addresses and pointers

• Change jumps, loads, etc.

• Q: Can any addresses be unchanged?

26Y. Cheng GMU CS571 Spring 2020

Page 27: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Rewrite for Each New Process

27

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

0x100010fad movl 0x8(%rbp), %edi0x100010fb0 addl $0x2, %edi0x100010fb3 movl %edi, 0x8(%rbp)

0x100020fad movl 0x8(%rbp), %edi0x100020fb0 addl $0x2, %edi0x100020fb3 movl %edi, 0x8(%rbp)

rewrite

rewrite

Y. Cheng GMU CS571 Spring 2020

Page 28: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Rewrite for Each New Process

28

0x100000fad movl 0x8(%rbp), %edi0x100000fb0 addl $0x2, %edi0x100000fb3 movl %edi, 0x8(%rbp)

rewrite

rewrite

0x100010fad movl 0x8(%rbp), %edi0x100010fb0 addl $0x2, %edi0x100010fb3 movl %edi, 0x8(%rbp)

0x100020fad movl 0x8(%rbp), %edi0x100020fb0 addl $0x2, %edi0x100020fb3 movl %edi, 0x8(%rbp)

Y. Cheng GMU CS571 Spring 2020

Page 29: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Rewrite for Each New Process

29

0x100010fad movl 0x8(%rbp), %edi0x100010fb0 addl $0x2, %edi0x100010fb3 movl %edi, 0x8(%rbp)

0x100020fad movl 0x8(%rbp), %edi0x100020fb0 addl $0x2, %edi0x100020fb3 movl %edi, 0x8(%rbp)

Program codeHeap

Stack

Free

Program codeHeap

Stack

Free

Process 1

Process 2

Free

6KB

10KB

13KB

17KB

Y. Cheng GMU CS571 Spring 2020

Page 30: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Rewrite for Each New Process

30

0x100010fad movl 0x8(%rbp), %edi0x100010fb0 addl $0x2, %edi0x100010fb3 movl %edi, 0x8(%rbp)

0x100020fad movl 0x8(%rbp), %edi0x100020fb0 addl $0x2, %edi0x100020fb3 movl %edi, 0x8(%rbp)

Program codeHeap

Stack

Free

Program codeHeap

Stack

Free

Process 1

Process 2

Free

6KB

10KB

13KB

17KBWhy didn’t we have to rewrite the stack addr?

Y. Cheng GMU CS571 Spring 2020

Page 31: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Rewrite for Each New Process

31

0x100010fad movl 0x8(%rbp), %edi0x100010fb0 addl $0x2, %edi0x100010fb3 movl %edi, 0x8(%rbp)

0x100020fad movl 0x8(%rbp), %edi0x100020fb0 addl $0x2, %edi0x100020fb3 movl %edi, 0x8(%rbp)

Program codeHeap

Stack

Free

Program codeHeap

Stack

Free

Process 1

Process 2

Free

6KB

10KB

13KB

17KBWhy didn’t we have to rewrite the stack addr?A: $rbp is dynamically calculated according to the base addr

Y. Cheng GMU CS571 Spring 2020

Page 32: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

How to Run Multiple Programs?

• Approaches:• Static relocation

• Dynamic relocation• Base

• Base-and-Bounds

• Segmentation

32Y. Cheng GMU CS571 Spring 2020

Page 33: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base

• Idea: translate virtual address to physical by adding an offset each time

• Store base addr in a base register

• Each process has a different value in the base register when running

33Y. Cheng GMU CS571 Spring 2020

Page 34: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

34

P1

P2

2KB

4KB

10KB

12KB

Same code

Y. Cheng GMU CS571 Spring 2020

Page 35: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

35

P1 is running …

Base registerP1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 36: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

36

P2 is running …

Base register

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 37: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

37

Virtual Physical

P1: load 100, R1P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 38: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

38

Virtual Physical

P1: load 100, R1 load 2148, R1P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 39: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

39

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 40: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

40

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 41: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

41

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 42: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

42

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 43: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

43

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 44: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

44

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1 load 4048, R1

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 45: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation Hardware

45Y. Cheng GMU CS571 Spring 2020

Page 46: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

46

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1 load 4048, R1

Can P1 hurt P2?Can P2 hurt P1?

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 47: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base Relocation

47

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1 load 4048, R1

P1: store 9241, R1 store 11289, R1

Can P1 hurt P2?Can P2 hurt P1?

Overflow!

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 48: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

How to Run Multiple Programs?

• Approaches:• Static relocation

• Dynamic relocation• Base

• Base-and-Bounds

• Segmentation

48Y. Cheng GMU CS571 Spring 2020

Page 49: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds• Idea: add bound register to avoid “overflow”• Two CPU registers

• Base register• Bounds register (or limit register)

physical addr = virtual addr + base

• The base-and-bounds hardware referred to as Memory Management Unit (MMU)• Protection: The hardware provides special instructions

to modify the base and bounds register• Allowing OS to change them when different processes run• Privileged (only in kernel mode)

49Y. Cheng GMU CS571 Spring 2020

Page 50: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds

50

P1 is running …

Base registerBase + BoundsP1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 51: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds

51

P2 is running …

Base registerBase + Bounds

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 52: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds

52

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1 load 4048, R1

P1: store 9241, R1

Can P1 hurt P2?

P1

P2

2KB

4KB

10KB

12KB

Y. Cheng GMU CS571 Spring 2020

Page 53: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

P2

2KB

4KB

10KB

12KB

Base-and-Bounds

53

Virtual Physical

P1: load 100, R1 load 2148, R1

P2: load 100, R1 load 10340, R1

P2: load 2000, R1 load 12240, R1

P1: load 2000, R1 load 4048, R1

P1: store 9241, R1 Interrupt!

Can P1 hurt P2?

Y. Cheng GMU CS571 Spring 2020

Page 54: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds Pros/Cons

• Pros?• Fast + simple

• Little bookkeeping overhead (2 registers)

• Cons?• Not flexible

• Wastes memory for large memory addresses

54Y. Cheng GMU CS571 Spring 2020

Page 55: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Base-and-Bounds Pros/Cons

• Pros?• Fast + simple

• Little bookkeeping overhead (2 registers)

• Cons?• Not flexible

• Wastes memory for large memory addresses

55Y. Cheng GMU CS571 Spring 2020

Page 56: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Problems with Base-and-Bounds?

• Simple base-and-bounds approach wastes a chunk of “free” space between stack and heap

• Impossible to run a program when its entire address space is greater than the memory capacity

56

Wastedspace

Y. Cheng GMU CS571 Spring 2020

Page 57: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

How to Run Multiple Programs?

• Approaches:• Static relocation

• Dynamic relocation• Base

• Base-and-Bounds

• Segmentation

57Y. Cheng GMU CS571 Spring 2020

Page 58: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Segmentation

• Idea: generalize base-and-bounds

• Each base+bounds pair is a segment

• Use different segments for heap and memory• Requires more registers

• Resize segments as needed

58Y. Cheng GMU CS571 Spring 2020

Page 59: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Segmentation (cont.)

• A segment is a contiguous portion of the address space

• A program is a collection of segments

• A segment can be a logical unit: • E.g., main program, procedure, function, object, local variables, global variables, common block, stack, heap, symbol table, or arrays, etc.

59Y. Cheng GMU CS571 Spring 2020

Page 60: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Logical View of Segmentation

60

1

3

2

4

1

4

2

3

Virtual address space of a process physical memory

Y. Cheng GMU CS571 Spring 2020

Page 61: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Our Old Example

61Y. Cheng GMU CS571 Spring 2020

Page 62: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Segfault!

62

Segmentation fault

Access to the address 7KB …

Y. Cheng GMU CS571 Spring 2020

Page 63: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Segmentation Architecture• Logical address consists of a pair:

<segment-number, offset>• Segment table – maps two-dimensional physical

addresses. Each table entry has:• base – contains the starting physical address where the

segments reside in memory• limit – specifies the length of the segment (or bound)

• Segment-table base register (STBR) points to the segment table’s location in memory

• Segment-table length register (STLR) indicates number of segments used by a process• segment number s is legal if s < STLR

63Y. Cheng GMU CS571 Spring 2020

Page 64: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Segmentation Hardware

64

s: Segment #d: offset

Y. Cheng GMU CS571 Spring 2020

Page 65: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

Example of Segmentation

65Y. Cheng GMU CS571 Spring 2020

Page 66: Memory Managementnotes.pdfMemory Management: Address space and Segmentation CS 571: Operating Systems (Spring 2020) Lecture 6a Yue Cheng Some material taken/derived from: •Wisconsin

66

Worksheet

Y. Cheng GMU CS571 Spring 2020