8.1.Exceptions
\(8.1.\)Exceptions
An exception is an abrupt change in the control flow in response to some change in the processor's state:
1.Exception Handling
- Each type of possible exception in a system is assigned a unique nonnegative integer exception number.
- At system boot time(when the computer is reset or powered on), the operating system allocates and initializes a jump table called an exception table, so that entry \(k\) contains the address of the handler for exception \(k\).
- At run time(when the system is executing some program), the processor detects that an exception has occurred and determines the corresponding exception number \(k\). The processor then triggers the exception by making an indirect procedure call, through entry \(k\) of the exception table, to the corresponding handler.
- The starting address of the exception table is contained in a special CPU register called the exception table base register.
2.Differences between Exceptions and Procedure Call
As with a procedure call, the processor pushes a return address on the stack before branching to the handler. However, depending on the class of exception, the return address will have multiple choices.
The processor also pushes some additional processor state onto the stack that will be necessary to restart the interrupted program when the handler returns.
Once the hardware triggers the exception, the rest of the work is done in software by the exception handler. After the handler has processed the event, it optionally returns to the interrupted program by executing a special "return from interrupt" instruction, which pops the appropriate state back into the processor's control and data registers, restores the state to user mode if the exception interrupted a user program, and then returns control to the interrupted program.
3.Classes of Exceptions
\(a.\)Interrupts
Interrupts occur asynchronously as a result of signals from I/O devices that are external to the processor.
I/O devices interrupts by signaling a pin on the processor chip and placing onto the system bus the exception number that identifies the device that caused the interrupt.
After the current instruction finishes executing, the processor notices that the interrupt pin has gone high, reads the exception number from the system bus, and then calls the appropriate interrupt handler.
When the handler returns, it returns control to the next instruction. The program continues executing as though the interrupt had never happened.
The remaining classes of exceptions(traps, faults, and aborts) occur synchronously as a result of executing the current instruction. We refer to this instruction as the faulting instruction.
\(b.\)Traps and system calls
The most important use of traps is to provide a procedure-like interface between user programs and the kernel, known as a system call.
User program often needs to request services from the kernel.
processors provide a special syscall n
instruction that
user programs can execute when they want to request service
n.
- Executing the syscall instruction causes a trap to an exception handler that decodes the argument and calls the appropriate kernel routine.
\(c.\)Faults
\(d.\)Abort
4.Linux/x86-64 System Calls
- Each system call has a unique integer number that corresponds to an offset in a jump table in the kernel.(This jump table is not the same as the exception table.)
The C standard library provides a set of convenient wrapper functions for most system calls. The wrapper functions package up the arguments, trap to the kernel with the appropriate system call instruction, and then pass the return status of the system call back to the calling program.
- All arguments to Linux system calls are passed through general-purpose registers rather than the stack.