Professional Documents
Culture Documents
Lect2 424 002
Lect2 424 002
Review: What is an Operating System?
• Refere
– Manage sharing of resources, Protection, Isolatio
» Resource allocation, isolation, communicatio
• Illusionis
– Provide clean, easy to use abstractions of physical
resource
» Infinite memory, dedicated machin
» Higher level objects: files, users, message
» Masking limitations, virtualizatio
• Glu
– Common service
» Storage, Window system, Networkin
» Sharing, Authorizatio
» Look and feel
s
Recall: Increasing Software Complexity
Linux 2.2.0
Firefox
New Versions usually (much) larger older versions!
Android
Windows 7
Windows Vista
Cars getting really complex!
Facebook
Mac OS X "Tiger"
Modern Car
0 30 60 90 120
• OS Goals
– Remove software/hardware quirks (fight complexity)
– Optimize for convenience, utilization, reliability, … (help the programmer)
• For any OS area (e.g. file systems, virtual memory, networking,
scheduling)
– What hardware interface to handle? (physical reality
– What’s software interface to provide? (nicer abstraction)
Virtual Machines
• Software emulation of an abstract machin
– Give programs illusion they own the machin
– Make it look like hardware has features you wan
• Two types of “Virtual Machine”
– Process VM: supports the execution of a single program; this
functionality typically provided by O
– System VM: supports the execution of an entire OS and its applications
(e.g., VMWare Fusion, Virtual box, Parallels Desktop, Xen)
Process VMs
• Programming simplicit
– Each process thinks it has all memory/CPU tim
– Each process thinks it owns all device
– Different devices appear to have same high level interfac
– Device interfaces more powerful than raw hardwar
» Bitmapped display ⇒ windowing syste
» Ethernet card ⇒ reliable, ordered, networking (TCP/IP
• Fault Isolatio
– Processes unable to directly impact other processe
– Bugs cannot crash whole machin
• Protection and Portabilit
– Java interface safe and stable across many platforms
Virtual Machines
• Virtualize every detail of a hardware configuration so perfectly that
you can run an operating system (and many applications) on top of
it
– VMWare Fusion, Virtual box, Parallels Desktop, Xen, Vagran
• Provides isolatio
• Complete insulation from chang
• The norm in the Cloud (server consolidation
• Long history (60’s in IBM OS development
• All our work will take place INSIDE a V
– Vagrant (new image just for you)
UNIX OS Structure
“In conclusion…”
• Operating systems provide a virtual machine abstraction to handle
diverse hardwar
– Operating systems simplify application development by providing
standard service
• Operating systems coordinate resources and protect users from
each othe
– Operating systems can provide an array of fault containment, fault
tolerance, and fault recover
)
compiler
int main()
{… data stack
Execute
editor
Load &
}
Memory
instructions heap
a.out data
foo.c
Decode decode
Registers
Execute
ALU
data
R3 Data
F
Fetc
Exec Data
F3 Inst23
PC Inst23
Inst
• Execution sequence
Inst
– Fetch Instruction at PC
Inst PC
– Decod Inst2 PC
– Execute (possibly using registers Inst PC
– Write results to registers/me Inst0 PC
– PC = Next Instruction(PC
Addr 0
– Repeat
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 23
…
e
x86 Registers
stack
Proc 1 Proc 2 Proc n
… heap
Static Data
OS code
stack
heap
Static Data
code
stack
heap
Static Data
code
• Consider
– At T1: vCPU1 on real core, vCPU2 in memor
– At T2: vCPU2 on real core, vCPU1 in memor
• What happened
– OS Ran [how?
– Saved PC, SP, … in vCPU1's thread control block (memory
– Loaded PC, SP, … from vCPU2's TCB, jumped to P
• What triggered this switch
– Timer, voluntary yield, I/O, other things we will discuss
heap
Processo
registers Static Data
instruction
Code Segment
0x000…
y
stack
stack
0100…
Base
0010… 1000… code 1000…
>=
Static Data
Progra 1010…
address heap
Bound <
stack
1100… 1100…
FFFF…
stack
stack
0100…
Base
0010… 1000… code 1000…
>=
Static Data
Progra 1010…
address heap
Bound <
Addresses translated stack
when program loaded 1100… 1100…
Review: Relocation
heap
Addresses translated Static Data
on-the-fly heap
stack
0100… stack
Base Address
1000…
0010… 1000… code
Bound <
stack
1100…
0100…
• Hardware relocatio
• Can the program touch OS
FFFF…
• Can it touch other programs?
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 38
m
n
code
static data
heap
Processor Registers
CS EIP
stack
SS ESP
CS:
EAX
EIP: code
DS EBX
static data
ES ECX
EDX
ESI
heap
EDI
s”
s”
res
res
0x000…
add
add
l
sica
ual
y
t
Processor Memory
“ph
“vir
translator
Registers
0xFFF…
• Another review…
Processor
<Frame Addr>
instruction
<Page Offset>
Page (eg, 4 kb)
<Virtual Address> =
<Page #> <Page Offset>
PT Addr
User Mode
interrupt
exception
syscall
rtn rfi
exit
exec Kernel Mode
Applications
User Mode
Standard Libs
Kernel Mode
Hardware
OS heap
stack
sysmode 1 1000…
code
Base xxxx … 0000… Static Data
uPC xxxx…
stack 1100…
PC
code 3000…
regs
Static Data
… heap
stack 3080…
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 50
Simple B&B: OS gets ready to execute process
0000…
Proc 1 Proc 2 Proc n code RTU
…
Static Data
OS heap
stack
sysmode 1 1000…
code
Base 1000 … 0000… Static Data
uPC 0001…
stack 1100…
• Privileged Inst: set PC
special register code 3000…
regs
• RTU (Return To 00FF…
Static Data
Usermode) … heap
stack 3080…
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 51
s
OS heap
stack
sysmode 0 1000…
code
Base 1000 … 0000… Static Data
stack 3080…
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 52
?
3 types of Mode Transfer
• Syscal
– Process requests a system service, e.g., exi
– Like a function call, but “outside” the proces
– Does not have the address of the system function to cal
– Like a Remote Procedure Call (RPC) – for late
– Marshall the syscall id and args in registers and exec syscal
• Interrup
– External asynchronous event triggers context switc
– e. g., Timer, I/O devic
– Independent of user proces
• Trap or Exceptio
– Internal synchronous event in process triggers context switc
– e.g., Protection violation (segmentation fault), Divide by zero,
• All 3 are an UNPROGRAMMED CONTROL TRANSFE
– Where does it go?
intrpHandler_i () {
….
}
OS heap
stack
sysmode 0 1000…
code
Base 1000 … 0000… Static Data
uPC xxxx…
stack 1100…
PC 0000 1234
code 3000…
regs
Static Data
00FF…
… heap
• How to return
to system? stack 3080…
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 56
Simple B&B: Interrupt
0000…
Proc 1 Proc 2 Proc n code
…
Static Data
OS heap
stack
sysmode 1 1000…
code
Base 1000 … 0000… Static Data
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 57
Simple B&B: Switch User Process
0000…
Proc 1 Proc 2 Proc n code RTU
…
Static Data
OS heap
stack
sysmode 1 1000…
1000 … code
Base 3000 … 0000… Static Data
1100 …
Bound 0080 … FFFF… heap
0000 1234
uPC 0000 0248
regs
stack 1100…
PC 0001 0124
00FF… code 3000…
regs
Static Data
00D0…
• How to save … heap
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 58
Simple B&B: “resume”
0000…
Proc 1 Proc 2 Proc n code RTU
…
Static Data
OS heap
stack
sysmode 0 1000…
1000 … code
Base 3000 … 0000… Static Data
1100 …
Bound 0080 … FFFF… heap
0000 1234
uPC xxxx xxxx
regs
stack 1100…
PC 000 0248
00FF… code 3000…
regs
Static Data
00D0…
• How to save … heap
FFFF…
1/23/2020 Kubiatowicz CS162 ©UCB Spring 2020 59
Running Many Programs ???
• We have the basic mechanism to
– switch between user processes and the kernel,
– the kernel can switch among user processes
– Protect OS from user processes and processes from each othe
• Questions ??
• How do we decide which user process to run
• How do we represent user processes in the OS
• How do we pack up the process and set it aside
• How do we get a stack and heap for the kernel
• Aren’t we wasting a lot of memory
• …
Scheduler
if ( readyProcesses(PCBs) ) {
nextPCB = selectProcess(PCBs);
run( nextPCB );
} else {
run_idle_process();
}
)