Exceptional Control Flow

The sequence of instruction addresses that the PC register holds is called the flow of control or control flow. Each transition from one address to another is called a control transfer.

Abrupt changes to the control flow is referred to as exceptional control flow (ECF).  

8.1 Exceptions

An exception is an abrupt change to the control flow in response to a CPU state change.  The state is encoded in various bits and signals in the processor. The change in state is known as an event.

Each type of exception is assigned a unique non-negative number.

Exception types

  • Divide by zero
  • Page fault
  • memory access violation
  • system calls
  • hardware requests

At boot time the OS allocates a jump table (exception table) that maps exception types to the addresses of exception handler routines that are called when the particular exception occurs.

At run time, when an exception occurs the CPU

  • pushes a return address and other state information (EFLAGS) onto the kernel’s stack.
  • changes to kernel mode
  • makes a call to the exception hander

When the exception handler finished one of the following occurs:

  • The handler returns control to the current instruction
  • The handler returns control to the instruction that would have been executed had the exception not occurred
  • The handler aborts the interrupted program

If the processor returns to the interrupted user program it pops the stack and returns to user mode.

8.1.2  Classes of Exceptions

Interrupt Signal from I/O device Async Always returns to the next instruction
Trap Intentional exception Sync Always returns to the next instruction
Fault Potentially recoverable error Sync Might return to the current instruction
Abort Nonrecoverable error Sync Never returns

Interrupts

When a hardware device requests CPU time (e.g. data is on the network)

  • hardware device interrupt signal
  • CPU completes current instruction
  • puts exception type on the bus
  • CPU changes to kernel mode
  • CPU read the bus to determine which interrupt handler to invoke
  • call interrupt handler
  • After hander is executed, CPU changes back to user mode
  • CPU continues at the next instruction of the interrupted program

Traps

When a user program needs resources from the kernel (e.g. read file, create process) the program uses a syscall n instruction.  This instruction causes a trap exception which

  • changes CPU to kernel mode
  • executes the trap exception hander which decides the argument to determine which system call to invoke
  • the system call is executed
  • after the system call is executed the state changes back to user mode
  • the next instruction of the calling program is executed

Faults

Faults occur when the CPU can’t execute the current instruction (e.g. page fault) for some reason. A fault handler attempts to handler the issue.  If the fault handler can correct the error condition, the instruction that caused the fault is executed again by the processor, otherwise transfers control to an abort routine in the kernel and the processes is terminated.

Aborts

Aborts result from unrecoverable fatal errors, usually hardware errors.  Abort handlers transfer control to the abort routine in the kernel.

8.2 Processes

The kernel manages multiple running programs by allocating resources for each program in what are called processes.  The kernel schedules the processes to run on the CPU for small amounts of time.  The kernel changes control from one process to another by performing a context switch. During the context switch the kernel unloads (and saves) the state of the  stopped process and loads the state of the starting process.

The state of a running program consists of

  • The code and data stored in memory
  • The stack
  • The contents of the general purpose registers
  • value in PC
  • environmental variables
  • set of open file descriptors

© 2019, Eric. All rights reserved.