Operating Systems, Virtual Memory Intro
Instructor: Morgan Rae Reschenberg
Agenda
CS61C Su18 - Lecture 22
2
7/30/2018
CS61C so far…
CS61C Su18 - Lecture 22
3
7/30/2018
CPU
Caches
Memory
RISC-V Assembly
C Programs
�#include <stdlib.h>
int fib(int n) {
return� fib(n-1) +� fib(n-2);
}
.foo
lw t0, 4(s0)
addi t1, t0, 3
beq t1, t2, foo�nop
Project 1
Project 2
Labs
Project 3
But wait…
CS61C Su18 - Lecture 22
4
7/30/2018
Yes, but that’s just software! The Operating System (OS)
Well, “just software”
CS61C Su18 - Lecture 22
5
7/30/2018
Codebases (in millions of lines of code). CC BY-NC 3.0 — David McCandless © 2015�http://www.informationisbeautiful.net/visualizations/million-lines-of-code/
84 million lines of code!
What is an operating system?
6
Computer Hardware
(CPU, SSD/HD, RAM, …)
Operating System
Applications
(“Software”)
Operating systems control how software applications access and use hardware on your computer. They provide a general interface for common actions (ex. reading/writing disk) and allow software to run without knowledge of the machine it lives on!
What does the OS do?
CS61C Su18 - Lecture 22
7
7/30/2018
What is an operating system?
8
Have we always had OS?
Operating “systems” used to just be “operators”; these were people, usually women!
9
As late as the 1960s many people perceived computer programming as a natural career choice for savvy young women. Even the trend-spotters at Cosmopolitan Magazine urged their fashionable female readership to consider careers in programming. In an article titled “The Computer Girls,” the magazine described the field as offering better job opportunities for women than many other professional careers. As computer scientist Dr. Grace Hopper told a reporter, programming was “just like planning a dinner. You have to plan ahead and schedule everything so that it’s ready when you need it…. Women are ‘naturals’ at computer programming.” James Adams, the director of education for the Association for Computing Machinery, agreed: “I don’t know of any other field, outside of teaching, where there’s as much opportunity for a woman.”
What’s “different” about various OS?
10
Unix based, or … not
11
Agenda
CS61C Su18 - Lecture 22
12
7/30/2018
Administrivia
CS61C Su18 - Lecture 22
13
7/30/2018
Agenda
CS61C Su18 - Lecture 22
14
7/30/2018
What happens at boot?
CS61C Su18 - Lecture 22
15
7/30/2018
1. BIOS: Find a storage�device and load first sector (block of data)
2. Bootloader (stored on, e.g., disk): Load the OS kernel from disk into a location in memory and jump into it.
3. OS Boot: Initialize services, drivers, etc.
4. Init: Launch an application that waits for input in loop (e.g., Terminal/Desktop/...
Launching Applications
CS61C Su18 - Lecture 22
16
7/30/2018
Supervisor Mode
CS61C Su18 - Lecture 22
17
7/30/2018
What is an operating system?
18
Syscalls
CS61C Su18 - Lecture 22
19
7/30/2018
Syscalls in Venus
CS61C Su18 - Lecture 22
20
7/30/2018
Example Syscall
Print integer is syscall #1
li a0, 1
add a1, s3, x0
ecall
CS61C Su18 - Lecture 22
21
7/30/2018
Venus’s Environmental Calls
CS61C Su18 - Lecture 22
22
7/30/2018
Agenda
CS61C Su18 - Lecture 22
23
7/30/2018
Multiprogramming
CS61C Su18 - Lecture 22
24
7/30/2018
User mode: multiple applications
25
CPU
RAM
Process A
Process B
User mode: multiple applications
26
CPU
RAM
Process A
Process B
User mode: multiple applications
27
CPU
RAM
Process A
Process B
Process A
User mode: multiple applications
28
CPU
RAM
Process A
Process B
Process A
SWITCH
User mode: multiple applications
29
CPU
RAM
Process A
Process B
Process A
Process B
Wait… that shouldn’t happen!
User mode: multiple applications
30
CPU
RAM
Process A
Process B
Process A
Process B
Please give me Process A’s data! I am evil!
User mode: multiple applications
31
CPU
RAM
Process A
Process B
Process A
Process B
Process C
Process C
User mode: multiple applications
32
CPU
RAM
Process A
Process A
Process C
Process C
Process D
Process D
Hmm… There’s enough space, but not all together!
User mode: multiple applications
33
CPU
RAM
Process A
Process A
Process C
Process C
Process D
Process D
There we go!
User mode: multiple applications
34
CPU
RAM
Process A
Process A
Process C
Process C
Process D
Process D
Wait… This isn’t my data?
Protection, Translation, Paging
CS61C Su18 - Lecture 22
35
7/30/2018
CS61C Su18 - Lecture 4
36
6/21/2018
BREAK
Protection, Translation, Paging
CS61C Su18 - Lecture 22
37
7/30/2018
Virtual Memory
38
Virtual: The Illusion!
39
CPU
RAM
Process A
I am the ONLY PROCESS accessing memory, and I don’t have to share it with anyone!
Physical: The Reality!
40
CPU
RAM
Process A
Process B
Process A
Process B
Process C
Process C
DISK
Adding Disks to Hierarchy
CS61C Su18 - Lecture 22
41
7/30/2018
Memory Hierarchy
CS61C Su18 - Lecture 22
42
7/30/2018
Regs
L2 Cache
Memory
Disk
Tape
Instr Operands
Blocks
Pages
Files
Upper Level
Lower Level
Faster
Larger
L1 Cache
Blocks
Now:
Virtual�Memory
Earlier:�Caches
Agenda
CS61C Su18 - Lecture 22
43
7/30/2018
Virtual Memory Goals
CS61C Su18 - Lecture 22
44
7/30/2018
Segmented Memory
45
Segmented Memory
46
46
CPU
Process A
Process B
Process A
Process B
baseA
boundA
baseB
boundB
Simple Base and Bound Translation
CS61C Su18 - Lecture 22
47
7/30/2018
Load X
Program
Address Space
Bound
Register
Bounds
Violation?
Physical Memory
current
segment
Base
Register
+
Physical
Address
Logical
Address
Base and bounds registers are visible/accessible only when processor is running in supervisor mode
Base Physical Address
Segment Length
≤
Where have we seen segments before?
Stack, Heap, Static, Code, etc. !
Base & bound model was used to keep segments independent of each other on earlier machines; still used in some memory models (x86) today!
48
“Bare” 5-Stage Pipeline
CS61C Su18 - Lecture 22
49
7/30/2018
PC
Inst. Cache
D
Decode
E
M
Data Cache
W
+
Main Memory (DRAM)
Memory Controller
Physical Address
Physical Address
Physical Address
Physical Address
Physical Address
Base and Bound Machine
CS61C Su18 - Lecture 22
50
7/30/2018
PC
Inst. Cache
D
Decode
E
M
Data Cache
W
+
Main Memory (DRAM)
Memory Controller
Physical Address
Physical Address
Physical Address
Physical Address
Data Bound Register
Data Base Register
≤
+
Logical Address
Bounds Violation?
Physical Address
Prog. Bound Register
Program Base Register
≤
+
Logical Address
Bounds Violation?
Base & Bound: Problems!
51
Talk with your neighbours!
How can we fix our problems with base & bound? is there a better scheme?
52
Paged Memory!
53
Virtual: The Illusion!
54
CPU
RAM
Process A
I am the ONLY PROCESS accessing memory, and I don’t have to share it with anyone!
|
|
|
|
|
|
|
|
|
|
|
0
1
2
3
4
5
6
7
8
9
10
Physical: Paged Memory
55
55
CPU
Process A
Process B
Process A
Process B
Process A
Process A
Process B
Process B
Process A
0
�1
2
3
4
5
6
Physical Memory (RAM)
Shared
Paged Memory!
56
Page protection: Page tables!
57
58
58
CPU
Process A
Process B
Process A
Process B
Process A
Process A
Process B
Process B
Process A
0
�1
2
3
4
5
6
Physical Memory (RAM)
Shared
VPN | PPN | Valid? |
0 | | |
1 | | |
2 | | |
3 | | |
4 | | |
5 | | |
6 | | |
7 | | |
8 | | |
59
59
CPU
Process A
Process B
Process A
Process B
Process A
Process A
Process B
Process B
Process A
0
�1
2
3
4
5
6
Physical Memory (RAM)
Shared
Virtual Memory
(Process B Only!)
0
�1
2
3
4
5
6
7
8
Process B
Process B
Process B
VPN | PPN | Valid? |
0 | 2 | 1 |
1 | | |
2 | | |
3 | 6 | 1 |
4 | | |
5 | | |
6 | | |
7 | 4 | 1 |
8 | | |
60
60
CPU
Process A
Process B
Process A
Process B
Process A
Process A
Process B
Process B
Process A
0
�1
2
3
4
5
6
Physical Memory (RAM)
Shared
Virtual Memory
(Process B Only!)
0
�1
2
3
4
5
6
7
8
Process B
Process B
Process B
VPN | PPN | Valid? |
0 | 2 | 1 |
1 | X | 0 |
2 | X | 0 |
3 | 6 | 1 |
4 | X | 0 |
5 | X | 0 |
6 | X | 0 |
7 | 4 | 1 |
8 | X | 0 |
iClicker!
61
Modern Virtual Memory Systems� Illusion of a large, private, uniform store
CS61C Su18 - Lecture 22
62
7/30/2018
Protection
several programs, each with their private address space and one or more shared address spaces
Demand Paging
Provides the ability to run programs larger than the primary memory
Hides differences in machine configurations
The price is address translation on
each memory reference
OS
progi
Primary
Memory
Swapping
Store
VA
PA
mapping
TLB
Virtual Memory Goals
CS61C Su18 - Lecture 22
63
7/30/2018
Summary
CS61C Su18 - Lecture 22
64
7/30/2018