WHY HAVE INTERRUPTS? How to share resources on a computer? A. Provide libraries to access resources trust users will not take advantage Is that practical? No, malware user errors B. State policies about sharing humans penalized if violate policy Is that practical? No, user errors are still a problem C. Enforce polices about sharing at runtime, before harm is done Is that practical? more so, but not perfect INTERRUPT MECHANISM Goal: enforce polices on sharing, privacy, etc. How could the OS enforce policies? a. OS and user have same permissions b. OS has more permissions <---- this one c. OS has fewer permissions What would help OS enforce policies: 1. users cannot read some data 2. users cannot write some data 3. users cannot execute some instructions INTERRUPT MECHANISM Hardware: 1. Instructions added to signal interrupt events 2. At end of each fetch-execute cycle, the CPU checks for interrupt events TRAPS def: a *trap* is an interrupt that occurs due to a program error or malicious event Examples: divide by zero arithmetic overflow What should be done for traps? stop the computer notifiy the operator Better: run some code to "handle" the interrupt INTERRUPT HANDLERS def: an *interrupt handler* is code that How? When an interrupt is noticed: jump to I/O INTERRUPTS When device completes an operation it sets a bit in the CPU and then the OS: - suspends the running process - calls an interrupt handler TIMER INTERRUPTS When a process uses up a time slice it sets a bit in the CPU and then the OS: - suspends the running process - calls an interrupt handler to schedule another waiting process Would it be better to wait for another running process? MULTIPLE INTERRUPTS What if: - multiple interrupts happen at the same time? - if an interrupt happens when handling another interrupt? LIMITED DIRECT EXECUTION Goals: a. Run programs efficiently b. Share resources c. Prevent bad behavior Approach: For efficiency: For sharing and enforcing good behavior: Execution modes: Hardware support: INTERRUPTS AND SYSTEM CALLS Interrupt (trap, int, or svc) instruction - not privileged but starts system (kernel) mode - runs some specified code based on a (trap) table Return-from-trap instruction: - like procedure return instruction that specifies location of the trap table - privileged System call: - is a normal library function call - saves state (registers, PC, etc.) - pushes arguments on stack - sets the system call number (in a register) - executes interrupt instruction (pushes PC, saves process state on a kernel stack -- one per process, starts kernel mode, indirect jump through trap table to specialized code) (return-from-trap restores process state from kernel stack, starts user mode, restores PC) - adjusts any results on runtime stack (or in a register) - returns to caller