EE457Unit8_Exceptions
EE457Unit8_Exceptions
EE 457 Unit 8
Exceptions
“What Happens When Things Go Wrong”
2
BACKGROUND
3
Address
– MIPS processors can use “shadow” Space
register sets (alternate GPR’s when in
kernel mode).
7
EXCEPTIONS OVERVIEW
8
Exception Processing
• Exception handling is similar to a subroutine ('jal') call but performed
automatically by the hardware
– Must save PC of offending instruction, program state, and any information needed to
return afterwards
– Flush the pipeline using the hardware already present for branches/jumps
– Execute the software handler by loading the PC with its start address (must be preset or
looked up by the hardware without help from software)
– Execute the handler routine to deal with the exception
– Return and restore the state
User Program System Exception
--------- Handler
--------- ---------
---------
1. Exception occurs ---------
Exception occurs ---------
2. Save State --------- ---------
3. Call handler
--------- ---------
--------- ---------
6. Resume normal
execution --------- Return from
--------- 4. Restore state exception
5. Return
10
Exception Examples 1
Example Stage Action
I/O Device Interrupt WB Take ASAP
• A peripheral device requires action from the CPU
(Interrupt I/O Driven)
Operating System Calls (“Traps”) [e.g. File Open] ID Precise
• Trap instruction causes processor to enter kernel mode
Exception Examples 2
Example Stage Action
Page Faults IF or MEM Precise
• Virtual memory access fault (no Page Table entry resident in
memory)
Misaligned Memory Address EX Abort
• Address is not multiple of operand size Process
Memory Protection Violations IF or MEM Abort
• Address is out of bounds; RWX violation Process
Undefined Instructions ID Precise
• Decode unit does not recognize opcode or other fields (Why not
• Could be useful to extend the instruction set abort)
Hardware failure WB Take ASAP
• Unrecoverable hardware error is detected; execution is
compromised
Power Failure WB Take ASAP
• Power has fallen below a threshold; Trap to software to save as
much state as possible
13
Status Register
• Allows software to understand the state of the processor and
to control whether certain exceptions (interrupts) are ignored
• Register 12 in coprocessor 0
– IM[7:0] – Interrupt Mask bits (1 = ignore / 0 = allow)
– UM – User Mode (1 = User mode / 0 = Kernel Mode)
– ERL/EXL = Exception/Error Level
• 1 = Already handling exception or error / 0 = Normal exec.
• If either bit is ‘1’ processor is also said to be in kernel mode
– IE = Interrupt Enable
• 1 = Allow unmasked interrupts / 0 = Ignore all interrupts
31 15 8 4 2 1 0
Status
ERL
EXL
IM5
IM7
IM6
IM4
IM3
IM2
IM1
IM0
UM
000 00
IE
0000 0000 0000 0000
Register
16
EPC Register
• Exception PC holds the address of the offending
instruction
– Can be used along with ‘Cause’ register to find and correct
some error conditions
• ‘eret’ instruction used to return from exception
handler and back to execution point in original code
(unless handling the error means having the OS kill
the process)
– ‘eret’ Operation: PC = EPC
31 0
EPC =
Exception PC
Address of instruction that generated the exception
17
Cause Register
Code Cause
• Register 13 in coprocessor 0 0 Interrupt (HW)
INT 2 Hand. x3
INT n Hand. 0x80000???
"PRECISE" EXCEPTIONS
22
Precise Exceptions
• Precise Exceptions: A pipelined or advanced out-of-
order execution processor's exception handling
should behave equivalently to exceptions on a
single-cycle CPU.
– Any instructions BEFORE the offending instruction should
complete before the handler runs
– Any instructions AFTER the offending instruction should
not appear to have executed (written to memory or
register)
• Very difficult in architectures in which multiple
instruction execute concurrently (i.e. our 5-stage
pipeline)
24
Exception in EX stage
FLUSH • Save EPC=PC+4 of offending instruction
• Record Cause
EX.RegWrite • Add 3rd input of 0x8000_0180 to PCSrc Mux (start
address of exception handler)
Stall EX.RegDst
Ex Mem WB
PCWrite HDU
IRWrite
Mem WB
IF.Flush
WB
+
MemToReg
Control Branch
4
+
rs Sh.
Read Left 2
EPC
MemRead &
MemWrite
5 Reg. 1 # Read
0
data 1 Cause
Read
Reg. 2 # 2
5
Reg. #
ALU
1
data 2
8000_0180
2 0 Res.
Write 1
0
D-Cache
Data 2
3 1
1
Register File
ALUSelB
Sign ALUSrc
Extend RegDst
Reset
16 rs
32
Forwarding 0
rt Unit 1
rd
26
Handling in WB Stage
• Handling in WB stage helps deal with temporal vs. program
order issues
Simplified Processing
• Precise exceptions are now taken in WB along with other HW interrupts
• Faulting instructions “carry” their cause and EPC values through the
pipeline stage registers
• Only one set of EPC and CAUSE registers in the WB stage
• When an instruction flagged as faulting reaches the WB stage
– Flush IF, ID, EX, MEM
• Make sure that if a SW is in MEM stage that it is not allowed to write
– Load the handler address in the PC
– Make sure EPC & Cause are software-readable (movable to GPR’s)