Isolation
Dual Mode Execution
- key role of OS: manages and abstracts hardware resources
- processes can only access hardware through OS services
- how to achieve this?
- protection rings
- lower 2 bits of the
cs
register
- ring 0 (kernel mode)
- access to privileged instructions
- e.g. halt, copy I/O port data, update virtual memory mappings, disable interrupts
- access to all mapped virtual memory
- ring 3 (user mode)
- can only access non-privileged instructions
- most instructions (add, mov, call, ret)
- can only access virtual memory mapped with user permission
- ring 1 & 2
- can only access non-privileged instructions
- has access to more mapped virtual memory than ring 3
Types of Mode Transfer
- system calls
- kernel's service APIs (e.g. open, close, read, write)
syscall
and sysret
instructions to enter and return
- requested by user process (synchronous), looks like a procedure call
- executes the next user instruction upon return
- exceptions
- unexpected problem occurring at the current instruction (synchronous)
- e.g. divide by zero, null pointer access, segfault, execute privileged instruction
- terminate (for all the examples above) or resume on the faulting instruction depending on the exception
- interrupts
- hardware notifications, timely events
- e.g. I/O completion (disk write completed, packet arrived), timer interrupt, keyboard input
- unrelated to the current instruction (asynchronous)
- resumes on the current instruction since it was interrupted (didn't get to execute)
- when a process transfers from user mode into kernel mode, the same process "runs in the kernel"
- user mode: can execute arbitrary logic within its privilege boundary
- kernel mode: put on the kernel hat, only executes known kernel code
Mode Transfer Mechanisms
- mode transfer control flow
- switch into kernel mode -> switch to kernel/interrupt stack, saves process's states
-> execute corresponding handler -> restore process's states -> switch back to user mode
- what's the kernel stack for? why switch to a different stack?
- what happens if we use the same stack the process used in user mode?
- who has read/write access to the user stack?
- is the kernel/interrupt stack shared by all processes or per process?
- what are the process's states? why save the process's states?
- where are the states saved?
- some are saved by the hardware and some by the kernel, why?
- how does the computer know which handler to run on mode switch?
- interrupt vector table/interrupt descriptor table (x86)
- array of handlers set up by the OS on boot, hw understands it
- holds handlers for exceptions, syscall (software interrupt), and (hardware) interrupts