Professional Documents
Culture Documents
Mechanism Limited Direct Execution
Mechanism Limited Direct Execution
1
Questions?
When and how other processes run? How OS services are invoked?
2
Separation of policy and mechanism
Re-occuring theme in OS
Policy: high level decision-making to optimize some workload perf-
ormance metric
Answer to “which” question
Ex: Which process should OS run right now?
Process Scheduler: Future lecture
Mechanism: Low-level code/implementation of the decision
Answer to a “How” question?
Ex: how does an OS run a process or switches from one to another?
Today’s lecture
3
The Crux: How to efficiently virtualize the CPU with control?
Issue
Performance: How can we implement virtualization without adding
excessive overhead to the system?
Control: How can we run processes efficiently while retaining control over
the CPU?
4
Direct Execution
OS Program
1. Create entry for process list
2. Allocate memory for program
3. Load program into memory
4. Set up stack with argc / argv
5. Clear registers
6. Execute call main()
7. Run main()
8. Execute return from main()
9. Free memory of process
10. Remove from process list
5
Problem 1: Restricted Operation
Kernel mode: The OS has access to the full resources of the machine
6
System Call
7
How is system call different from function call ?
If it looks just like a procedure call, how does the system know it’s a
system call?
A Process can only see its own memory because of user mode (other
areas, including kernel, are hidden) – then how can it call any Kernel
code ?
8
How is System Call different (Cont.)?
Every user process has 2 stacks - a user stack (resides in user address
space) and a dedicated kernel stack (resides in kernel address space)
IDT has addresses of kernel functions to run for system calls and other
events
9
Mechanism of system call
Return-from-trap instruction
Restore context of CPU registers from kernel stack
10
More on the trap instruction
Interrupt (external device needs attention of OS, e.g., a network packet has
arrived on network card)
Across all cases, the mechanism is: save context on kernel stack and
switch to OS address in IDT
11
Limited Direction Execution Protocol
OS @ boot Hardware
(kernel mode)
12
Limited Direction Execution Protocol (Cont.)
(Cont.)
13
Problem 2: Switching Between Processes
How can the OS regain control of the CPU so that it can switch
between processes?
A cooperative Approach: Wait for system calls
14
A cooperative Approach: Wait for system calls
Ex) Early versions of the Macintosh OS, The old Xerox Alto system
15
A Non-Cooperative Approach: OS Takes Control
A timer interrupt
During the boot sequence, the OS start the timer.
16
Saving and Restoring Context
17
Context Switch
PC
Restore a few for the soon-to-be-executing process from its kernel stack
18
Limited Direction Execution Protocol (Timer interrupt)
OS @ boot Hardware
(kernel mode)
OS @ run Program
Hardware
(kernel mode) (user mode)
Process A
…
timer interrupt
save regs(A) to k-stack(A)
move to kernel mode
jump to trap handler
19
Limited Direction Execution Protocol (Timer interrupt)
OS @ run Program
Hardware
(kernel mode) (user mode)
(Cont.)
Handle the trap
Call switch() routine
save regs(A) to proc-struct(A)
restore regs(B) from proc-struct(B)
switch to k-stack(B)
return-from-trap (into B)
restore regs(B) from k-stack(B)
move to user mode
jump to B’s PC
Process B
…
20
Worried About Concurrency?
21
Disclaimer: This lecture slide set was initially developed for Operating System course in
Computer Science Dept. at Hanyang University. This lecture slide set is for OSTEP book
written by Remzi and Andrea at University of Wisconsin.
22