Lec2 Lec3 Cpuvirt Full
Lec2 Lec3 Cpuvirt Full
Lec2 Lec3 Cpuvirt Full
Administrative
Announcements
- Midterm – 03/07 (tentative)
- C prep quiz posted, please attempt to answer the quiz
- 1st Warmup project will be released this week
Easiest project in the course (also good time to find project partner)
Virtualization:
The CPU
Q uestions answered in this lecture:
What is a process? (Chapter 4-5)
Why is limited direct execution a good approach for virtualizing the
CPU? (Chapter 6)
What execution state must be saved for a process? (Chapter 6)
What 3 modes could a process in? (Chapter 6)
What is a Process?
Process: An execution stream in the context of a process state
code
static
data
Progra
m
Process Creation
CPU Memory
code
static data
heap
stack
Process
code
static data
Program
Processes vs. Threads
• Example:
• Two processes examining same memory address 0xffe84264
see different values (I.e., different contents)
• Two threads examining memory address 0xffe84264
see same value (I.e., same contents)
Process Memory Segments
• The OS allocates memory for each process
- ie. a running
program– for data and code
Solution:
Limited direct execution – OS and hardware maintain some contr
Problem 1:
Restricted O PS
How can we ensure user process can’t harm others?
Solution: privilege levels supported by hardware (bit of status)
• User processes run in user mode (restricted mode)
• OS runs in kernel mode (not restricted)
• Instructions for interacting with devices
• Could have many privilege levels (advanced topic)
sys_read
RAM
RAM
RAM
RAM
read():
movl $6, %eax; int $64
movl %eax, …
• CPU uses contents of EAX register as source operand
System Call
Process P
RAM
RAM
sys_read
syscall
RAM
sys_read
syscall
buf
RAM
Syscall() { OS
sysnum = %eax Syscall table
sys_handle= get_fn_table(sysnum)
sys_handle (); Num Function
6 sys_read
}
7 sys_write
What to limit?
User processes are not allowed to perform:
• General memory access
• Disk I/O
• Special x86 instructions like
lidt
while (1) {
P1
yield() call
Cooperative Approach
yield() call
OS
Cooperative Approach
yield()
return
OS
Cooperative Approach
P2
yield()
return
Cooperative Approach
P2
yield() call
Q1: How Does Dispatcher
Run?
• Problem with cooperative approach?
• Disadvantages: Processes can misbehave
• By avoiding all traps and performing no I/O, can take over
entire machine
• Only solution: Reboot!
Data Data
User-level Stack User-level Stack
Heap Heap
Reg(A) Reg(B) OS
Process Control Block CPU Process Control Block
Kernel stack
B
Proc A
Kernel stack
Context Context
Switch()
Q4: What Context must be
Saved?
// the registers will save and restore
// to stop and subsequently restart a process
struct context {
int eip; // Index pointer register
int esp; // Stack pointer register
int ebx; // Called the base register
int ecx; // Called the counter register
int edx; // Called the data register
int esi; // Source index register
int edi; // Destination index register
int ebp; // Stack base pointer register
};
Running Ready
While (1) {
Char *cmd = getcmd();
Int retval = fork();
If (retval == 0) {
// This is the child process
// Setup the child’s process environment here
// E.g., where is standard I/O, how to handle signals?
exec(cmd);
// exec does not return if it succeeds
printf(“ERROR: Could not execute %s\n”, cmd);
exit(1);
} else {
// This is the parent process; Wait for child to finish
int pid = retval;
wait(pid);
}
}
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Stack Detour
Q4: What Context must be
Saved?
// the registers will save and restore
// to stop and subsequently restart a process
struct context {
int eip; // Index pointer register
int esp; // Stack pointer register
int ebx; // Called the base register
int ecx; // Called the counter register
int edx; // Called the data register
int esi; // Source index register
int edi; // Destination index register
int ebp; // Stack base pointer register
};
Virtualization:
The CPU
Disclaimer: Materials derived, reused, and modified from OSTEP book and lectures of Prof. Andrea and Remzi Arpaci-Dusseau and Prof. Yojip Wo