memory managementnotes.pdfmemory management: address space and segmentation cs 571: operating...
TRANSCRIPT
![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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/14.jpg)
_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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/62.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/63.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/64.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052518/5f07dcf57e708231d41f2104/html5/thumbnails/66.jpg)
66
Worksheet
Y. Cheng GMU CS571 Spring 2020