Operating System
Kernal \(\approx\) OS
Software of a Unix System

Application
- user code
- use library calls
Libraries
- pre-compiled
- written by experts
Portable OS Layer
- all high level codes
- system calls
machine-dependent layer
- bootstrap
- IO device driver
- interrupts and exceptions
- mem mangement
Protection
CPU modes:
- Kernal mode - can run all instructions
- User mode - can only run non-priviledged instructions
- Mode is indicated by a mode bit in a protected CPU control register
Priviledgd Instructions
a subset of instructions that can only run in kernal mode
- the CPU checks mode bit when priviledged instructions execute
- if the mode bit is set to user mode, the CPU will raise an exception
Priviledged Instructions Can DO:
- Directly access IO devices (disk, network, etc.)
- manipulate memory management (page tables, etc.)
- manipuate CPU protected control registers (mode bit, etc.), preventing user code from changing the mode bit
HLT: halts the CPU
Memory Protection
prevents user code from accessing kernal memory, Seperation of user and kernal memory
should programs trust OS?
may/may not be trusted, but the OS should not trust the programs
OS Events
unnatural change in the flow of control
- stops current execution
- change mode, context or both
OS defines handlers for these events, event handlers are executed in kernal mode after system booted, all entry to kernal occurs as result of an event
Os
OS itself is a big event handler
OS only executes in response to an event
interrupts
cause by external eventm abd are generated by hardware
interrupts on modern CPUs are precise: CPU transfer control only on instruction boundaries
Handling interrupts
graph TD;
A[Interrupt] --> B[Disable interrupts at lower priority]
B --> C[Save state - PC, registers, mode, etc]
C --> D[transfer control to interrupt service routine - in kernal]
D --> E[when done. restore state, resume execution]
E --> F[resume user level program at next instruction]
F --> A
exceptions
caused by program execution instructions
Faults
Hardware detects and report exceptional conditions
eg: try execute priviledged instruction in user mode
on exception, hardware faults, it needs to:
- Save state (PC, registers, mode, etc.)
- restart the faulting process
- CPU finds the exception handler in the kernal matching the exception type
- CPU switch to kernal mode and jumps to the exception handler
- after fault is handled, CPU returns to user mode and resumes execution (reverse of above)
some faults are handled by fixing the exceptional condition, eg: page fault -> bring page into memory
some faults are handled by notifying the process, where application register a fault handler with OS, and OS returns to the user mode fault handler. eg: SIGFPE, SIGTERM
some faults are handled by terminating the process, when there is no registered handler. halt state, write process state to file, and destroy process
if faults occur in kernal, eg dereference null pointer, the faults are considered fatal and the OS will crash. Unix panic, state dumped to core file
System calls
request kernal service by calling OS API
CPU provides a system call instruction to:
- cause exception
- pass parameter determining system routine to call
- save caller state to be restored
- return from system call restore the state
- need hardware to implement: restore saed state, reset mode, resume execution
| System Call | Description |
|---|---|
INT |
executes a system call |
SYSCALL |
executes a system call (64 bits) |
events can be unxpected or deliberate
example of system calls
graph TD;
A[Application: user action] --> B[system call: Library API call]
B --> C[user/kernl level switch]
C --> D[OS: trap to kernal]
D --> E[OS: kernal trap handler]
E --> F[OS: restores state, resume to user application]
F --> A
process and OS communication
- the OS returns names of the data structure in the kernal to the user process. why?
- The OS and user processs are in the different address spaces, therefore, it returns integer object handles or descriptors like unix file descriptors.
Events Comparison
| Characterstics | interrupts | faults | System calls |
|---|---|---|---|
| Handled by trapping to? | OS | OS | OS |
| handler runs in ? | kernal | kernal | kernal |
| states are saved by? | hardware | hardware | hardware |
| cause by? | external events | program execution | program execution |