Advanced Computer Architectures: Exception Handling
Advanced Computer Architectures: Exception Handling
Computer Architectures
Exception handling
11 1
Types of Exceptions
• We use the term ‘exception’ to cover not only
exceptions but also interrupts and faults. More in
general, we consider the following type of events:
– I/O device request;
– Invoking OS system call from a user program;
– Tracing instruction execution;
– Integer arithmetic overflow/underflow;
– Floating point arithmetic anomaly;
– Page fault;
– Misaligned memory access;
– Memory protection violation;
– Hardware / power failure.
2 22 2
Causes of Interrupts / Exceptions
Interrupt: an event that requests the attention of the
processor
• Asynchronous: an external event, such as:
– input/output device service‐request
– timer expiration
– power disruptions, hardware failure
• Synchronous: an internal event (a.k.a. exceptions)
– undefined opcode, privileged instruction
– Integer arithmetic overflow, FPU exception
– misaligned memory access
– virtual memory exceptions: page faults, TLB misses, protection violations
– traps: system calls, e.g., jumps into kernel
3 33 3
Classes of exceptions
• Synchronous vs asynchronous
– Asynchronous events are caused by devices external to the
CPU and memory and can be handled after the completion of
the current instruction (easier to handle)
• User requested vs coerced
– User requested (such as I/O events) are predictable: treated
as exceptions because they use the same mechanisms that are
used to save and restore the state; handled after the
instruction has completed (easier to handle)
– Coerced are caused by some HW event not under control of the
user program; hard to implement because they are
unpredictable;
• User maskable vs user nonmaskable
– The mask simply controls whether the HW responds to the
exception or not
4
44 4
Classes of exceptions (cont’d)
5
55 5
Interrupts: altering the normal flow of control
Ii-1 HI1
Program Interrupt
Ii HI2 Handler
(user-mode)
(kernel-mode)
Ii+1 HIn
6 66 6
Asynchronous Interrupts
Invoking the interrupt handler
• An I/O device requests attention by asserting one
of the prioritized interrupt request lines
• When the processor decides to process the
interrupt
– It stops the current program at instruction Ii, completing all the
instructions up to Ii‐1 (precise interrupt)
– It saves the PC of instruction Ii in a special register Exception
Program Counter: PC ‐> EPC
– It disables interrupts and transfers control to a designated
interrupt handler running in the kernel mode:
Int. Vector Address ‐> PC
7 77 7
Interrupt Handler
• To allow nested interrupts, we need to save PC before
enabling interrupts
– need an instruction to move PC into GPRs
– need a way to mask further interrupts at least until PC can be
saved
• Needs to read a status register that indicates the cause of
the interrupt
• Uses a special indirect jump instruction RFE (return‐from‐
exception) which restore the PC and:
– enables interrupts
– restores the processor to the user mode
– restores hardware status and control state
• The instruction Ii and the next instructions (Ii+1, …) are
restarted
8 88 8
Synchronous Interrupts
• A synchronous interrupt (exception) is caused by a
particular instruction
• In general, the instruction Ii cannot be completed
and needs to be restarted after the exception has
been handled
– In the pipeline this would require undoing the effect of one or more
partially executed instructions
9 99 9
Precise Interrupts/Exceptions
• An interrupt or exception is precise if there is a single instruction (or
interrupt point) for which all instructions before have committed
their state and no following instructions (including the interrupting
instruction Ii) have modified any state.
– This means, effectively, that we can restart execution at the interrupt
point and “get the right answer”
– Implicit in our previous example of a device interrupt:
• Interrupt point is at red lw instruction Ii)
External Interrupt
add r1,r2,r3
Int
subi r4,r1,#4
handler
slli r4,r4,#2
lw r2,0(r4)
lw r3,4(r4)
add r2,r2,r3
sw 8(r4),r2
10
10
10 10
Exception Handling: 5‐Stage Pipeline
Inst. Data
PC D Decode E + M W
Mem Mem
Asynchronous Interrupts
• How to handle multiple simultaneous exceptions in
different pipeline stages?
• How and where to handle external asynchronous
interrupts?
11 11
11 11
Precise Exceptions in simple 5-stage pipeline
• Exceptions may occur at different stages in pipeline
-> Exceptions may be raised out of order
• Let’s consider this first example:
– Data page fault occurs in memory stage of first instruction
– Arithmetic exception occurs in execution stage of second instruction
• Data page fault is handled first: OK!
page fault
Inst. Data
PC D Decode E + M W
Mem Mem
Overflow
12
12
12 12
Precise Exceptions in simple 5-stage pipeline
• Exceptions may occur at different stages in pipeline
-> Exceptions may be raised out of order
• Let’s consider this second example:
– Instruction page fault occurs in Instruction Memory stage of first instruction
– Data page fault occurs in memory stage of second instruction
• Instruction page fault is handled first!!!
page fault
page fault
13
13
13 13
Another look at the exception problem
Time
Program Flow
Inst TLB fault IFetch Dcd Exec Mem WB
14
14
14 14
Exception Handling: 5‐Stage Pipeline
Commit
Point
Inst. Data
PC D Decode E + M W
Mem Mem
EPC Cause
Exc Exc Exc
D E M
PC PC PC
Select D E M Asynchronous
Kill F Kill D Kill E
Handler Interrupts Kill
Stage Stage Stage
PC Writeback
time
t0 t1 t2 t3 t4 t5 t6 t7 . . . .
(I1) 096: ADD IF1 ID1 EX1 MA1 nop overflow!
(I2) 100: XOR IF2 ID2 EX2 nop nop
(I3) 104: SUB IF3 ID3 nop nop nop
(I4) 108: ADD IF4 nop nop nop nop
(I5) Exc. Handler Add IF5 ID5 EX5 MA5 WB5
• After the end of the Exception Handler Routine, the ADD instruction
will be re-executed and the instruction flow will continue
16 16
16 16
Exception Handling: 5‐Stage Pipeline
• Hold exception flags in pipeline until commit point
(M stage)
• Exceptions in earlier pipe stages override later
exceptions for a given instruction
• Inject external interrupts at commit point (override
others)
• Later on we will discuss again exception handling in
out‐of‐order execution processors.
17 17
17 17