INTERRUPT HANDLERS def: an *interrupt handler* is code that is run when an interrupt happens How? When an interrupt is noticed: jump to code to handle that the handler: - saves state of running process - jumps to appropriate piece of code 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? No! MULTIPLE INTERRUPTS What if: - multiple interrupts happen at the same time? - if an interrupt happens when handling another interrupt? Possible problems: - missing (losing) interrupts (info.) - put the computer into an unanticipated state - livelock Approaches: - prioritize different kinds of interrupts - disable some interrupts when handling others - split handling into "levels": 1st level: just record info. schedule 2nd level handling 2nd level: do what is needed with the info. LIMITED DIRECT EXECUTION Goals: a. Run programs efficiently b. Share resources c. Prevent bad behavior Approach: For efficiency: execute directly on the computer For sharing and enforcing good behavior: don't allow user processes to execute some instructions or to write to some areas of memory Execution modes: user mode trap if executes privileged instructions kernel (privileged) mode can execute all instructions Hardware support: mode flag in the CPU (user vs. kernel) memory "fence" to protect some area of memory - needs a comparator 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 INTERRUPT VECTOR OR TRAP TABLE def: an *interrupt vector* or *trap table* is an array of starting addresses for system call interrupt handlers Why use a table? to save time in handling interrupts NEED FOR MEMORY PROTECTION On interrupt: hardware runs interrupt handler from a set location What happens if a user program can write those locations? could crash the OS or take control of the computer Needs hardware support to be efficient HOW TO PROTECT MEMORY How to prevent users from writing over code for interrupt handler(s)? - compare the address in a store to the fence address, trap if below that Is hardware needed? Yes! For efficiency Are changes to instructions needed? Need to have hardware only write to protected area of memory if the process is in kernel mode PRIVILEGED MODE Okay for a user process to change the fence register? No! Privileged mode: allows all instructions to be executed allows all memory to be written (and read) Hardware needed: Some fence register Comparator PROGRAM STATUS WORD Program status word is a (privileged) register containing: interrupt flags process mode (user or kernel) PC condition codes