Exception Basics

Exception is an abnormal condition which affects the normal execution flow of a program. In this situation Operating system will take over the execution flow and checks the error type either to Abort the system or correct the error for resuming the normal flow of program execution.

Exception classifications

Faults: A fault is an exception which can be corrected and once it is corrected it can allow restarting a program with no loss of anything. Instead of executing next line of instruction, it will execute same instruction which caused exception earlier. In a rare case when fault occurs, this will result some loss of processor state. For instance, while executing POPAD instruction. If this results in crossing stack limit this will generate a fault, where it is not possible to correct the error and executing the same instructions. Example: If a program tries to read/write a byte where there is no physical page in memory, this will results in fault and later Operating system will identify physical page not found in memory and then allocates the physical page, then resumes the program execution of the same instruction which last time generated exception.

Traps: A trap is an exception that is reported after executing a trap instruction. Trap will continue program without loss of any processor state. This will return the next instruction to be executed after trapping instruction. Example: INT 3(or CC or CD 21) is a trap instruction. If a program has this instruction and if the debugger is running, it will return the next instruction address to this debugger. Where debugger can continue it debugging.

Aborts: An abort is an exception and it may not report exact location to tell which instruction is caused exception. Due to this inconsistency this kind of exception we cannot correct and resume program execution. Aborting is used for severe errors like hardware errors or illegal values in system tables. Example: Bus errors.

Hardware supported exception

  1. Divide Error(Type: Fault), This indicates that the divisor operand for a DIV or IDIV instruction is 0 or the result cannot be represented in the number of bits specified for the destination operand.
  2. Debug Exception (Type: Trap/Fault), this indicates that one or more debug exception has been detected. Debugger uses this interrupt normally for doing single step in a program.
  3. Breakpoint Exception (Type: Trap), this indicates that program executed INT 3 instruction and caused trap. Normally debugger will replace one byte of instruction with INT 3(CC opcode) after trap generation; trap handler will replace INT 3 back to original instruction which it was earlier.
  4. Overflow Exception (Type: Trap), this trap generation happens after executing INTO instructions. INTO instruction will check OF (Overflow Flag) in EFLAGS register, if this is enabled then it generates this trap.
  5. Bound Range Exceed Exception (Type: Fault), this fault occurs when it executes BOUND instruction. This instruction check the signed array index is within upper and lower range. If array index is not within range, then this fault occurs.
  6. Invalid Opcode Exception (Type: Fault), Attempting to execute an invalid instruction. We cannot execute any instruction any time. To execute an instruction it needs to have in some mode or in some situation. If we execute blindly instruction in invalid conditions, this fault will result.
  7. Device not available Exception(Type: Fault), If we execute an instruction when device is not available to do requested job, like if we execute an floating-point instruction when x87 FPU floating-point unit is not available.
  8. Double Fault Exception (Type: Abort), during processing one exception, if there is again an exception and if processor cannot handle, it generates a double fault exception.
  9. Coprocessor Segment Overrun (Type: Abort), Intel 486+ onwards doesn’t use this exception.
  10. Invalid TSS Exception (Type: Fault), Indicates that task switch was attempted and found invalid information in target task. Invalid conditions are like: Code segment is not executable, Data segment is not readable, etc…
  11. Segment not Present (Type: Fault), Indicates that the present flag of segment or gate descriptor is clear. Operating system uses segment not present exception to implement virtual memory at the segment level. If exception handler loads segment and returns successfully, interrupted program resumes execution. Operating system might use not-present in gate descriptor flag for its own use.
  12. Stack Fault Exception(Type: Fault), This exception will be generated when ENTER instruction is executed and if there is not enough stack space or while attempting to load segment register and if it detects no-present stack segment. It is possible to recover from this error provided we extend the stack, when limit is less or loading the missing segment into memory.
  13. General Protection Exception(Type: Fault), Indicates that the processor detected a violations of protection like Writing to a code segment or read-only data segment and many other reasons exists for this exception.
  14. Page Fault Exception (Type: Fault), this fault occurs mainly during page-translation mechanism to translate from linear address to physical address.
  15. x87 FPU Floating-Point Error (Type: Fault), to generate this fault, NE Flag in CR0 register must be set for an Interrupt 16(Floating point error exception) to be generated.
  16. Alignment Check Exception (Type: Fault), Indicates that the processor detected an unaligned memory operand when alignment checking is enabled. This checking is carried only in Data or Stack segments not in Code or system segment. Example of alignment check violation is storing a double word stored address and that address is not divisible by 4.
  17. Machine Check Exception (Type: Abort), this indicates that processor detected an internal machine error or bus error or that the external agent detected a bus error. This implementation various between Pentium processors.
  18. SIMD Floating-Point Exception (Type: Fault), this indicates that processor has detected a SSE or SSE2 SIMD Floating-point exception. Appropriate flag in MXCSR should be set.

All these hardware generated exceptions can be masked by operating system and give it own exception error code. In this way, code written for WinNT OS might work on other hardware where WinNT supports. Since, type of exceptions is platform dependent.

Software generated exception

In WinNT, to raise it own custom exception program can use function RaiseException(). During a function call, if a function was unable to allocate some memory, it has option to throw the caller exception saying that failed to allocate memory. Here, Function implementer should be documented the meaning of his exception error code.

Doesn’t matter who generates exception either software or hardware. Only software can process the exception and take appropriate action.

During execution of program, if there is any kind of exception happens CPU will transfer the control to a Interrupt Handler based on the type of exception. If there is a Breakpoint trap, it will transfer control to INT 3 code, if there is a Single Step trap/fault, it will transfer control to INT 1 code. Like this, there are 256 Interrupt handler exists. INT 32 and above are user defined interrupts. After executing the interrupt handler, based on the trap or faults and if that is recoverable, CPU will continue to execute the original task which resume earlier due to exception.

In WinNT, after trapping exception in kernel mode (all 32/64 bit OS traps only in kernel). Operating system will check who created this exception if this is done by some drivers itself, it will try to check handlers in kernel mode for handling exception, if it doesn’t find any exception handlers, then default OS exception-handler of kernel mode will KeBugCheck() the system (Results in Blue Screen of Death).

If this exception is generated from some user mode program, then kernel will transfer control to user mode program to check any handler present to handle this exception if nobody claimed, the default OS exception-handler in user mode will display exception dialog box (mostly Dr. Watson dialog, if there is no other debugger installed) and terminates that application itself.

C/C++ Languages will derive from Operating System provided exception handler and also adds there own field in Exception information.

When there is any exception, hardware will provide following basic information:

  1. Exception Class: Indicated class of exception like Fault, Trap or Abort.
  2. Description: Gives a general description of the exception and also described how does processor handles it.
  3. Exception Error Code: This indicates what error code is saved in exception. If this code is saving, it also describes it.
  4. Saved Instruction Pointer: Describes which instruction saved or returned instructions pointer points to. This also indicates whether the pointer can be used to restart a faulting instruction.
  5. Program State Change: This explains the effect of the exception on the state of currently state of running program and the possibility of restarting the program without loss of processor state.