Operating Systems CMPSC 473
CPU virtualization
January 26, 2021 – Lecture 3 Instructor: Bhuvan Urgaonkar
From the last lecture
• Frame pointer vs. stack pointer
• More address space minutiae • ASLR
• disable this in project 1
• Even parts of code+data may be dynamically added/removed
• DLLs
• There is something still missing – the OS kernel!
• Reminders:
• Linux accounts • Piazza
• Be on the lookout for an email re. sharing your github handles with Tas
• Project 1 coming out on 1/28
Administrative
#include
char c;
int *a = (int *) malloc (sizeof (int)); printf (“%p\n”, a);
sleep (100);
}
How Does the OS Help Multiple Processes Share the Same CPU?
E.g., Two processes on a CPU
• Let’s consider (only) two processes A and B that are running on the same CPU (along with the OS)
• Let us look closely at some illuminating events in such a system
T1 T2 Time A OS B OS A
We identify four basic questions worth considering
T1 T2 Time
A OS B OS A
Q1: what if the process does something undesirable here?
T1 T2 Time
A OS B OS A
Q2: how does the OS get to start running here?
T1 T2 Time
A OS B OS A
Q3: how do we ensure that A resumes execution at T2 as if it had not been
taken off the CPU at T1?
T1 T2 T3 Time
A OS B OS A
Q4: How does the OS decide which process to run next?
T1 T2 Time
A OS B OS A
Q1: what if the process does something undesirable here?
• What “undesirable” things might a process do?
Undesirable #1: Executing Certain Instructions
• Should a process be allowed to execute all instructions in the ISA?
• Answer:No
• E.g., what could go wrong if a program were allowed to execute the “halt” instruction?
• E.g., what could go wrong if a program were allowed to execute IO instructions? (will become more clear later in the course)
Recall System ISA
Undesirable #2: Certain Error Conditions
• Considerthefollowingerrorsourprogramsoftenruninto: – Segmentation fault
– Division by zero – Think of more
Solution: Traps
• TheCPUisdesigneds.t.upontheoccurrenceofthe following, it enters a special state and control jumps to the OS:
– A process executes a privileged instruction
– A process or the OS encounters one of a set of error conditions defined for the CPU
• Such events are called traps
Traps for system calls
• Programs are offered a special instruction via which they can raise a trap
–E.g., “int” on x86
–Is this a privileged instruction?
–Examples of unprivileged instruction: – Load, store, add, sub, jmp
–Examples of privileged instructions
add
– Halt, in/out, …
Summary
• Three ways in which traps may be raised • Involuntary:
• Execution of a privileged instruction – may harm other processes • CeertaintypesoferrorsthattheCPUcir.Iscapableofdetecting
• Voluntary • System calls
• Ondetectingtrap,CPUmust:
– Save process state
– Transfer control to trap handler (in OS) • CPU indexes trap vector by trap number
• Jumps to address
– Restore process state and resume
Traps