630 likes | 765 Views
Exception Processing. ECE511: Digital System & Microprocessor. What we are going to learn in this session:. What are exceptions. How M68k handles exceptions. M68k execution states and how they relate to exception handling. Introduction. Exceptions.
E N D
Exception Processing ECE511: Digital System & Microprocessor
What we are going to learn in this session: • What are exceptions. • How M68k handles exceptions. • M68k execution states and how they relate to exception handling.
Exceptions • Out-of-ordinary events that happen during M68k run-time. • Caused by: • Interrupt requests. • Errors during processing cycle. • Arithmetic operations that cannot be completed. • Illegal instructions.
Exception Example (Divide by Zero) • MOVE.W #0,D0 • MOVE.L #500000, D1 • DIVU D0,D1 500,000 / 0 = ? * Divide by Zero Exception
Exception Example: Address Error • Memory range = $000000 to $004000 • MOVE.B #$0A,$004500 $0000 $0001 $0002 … * Address Error Exception … $3FFE $3FFF $4000 $4500? (Address out of range)
Exception Example: Address Error • MOVE.B #$A00A,$000001 Word transfer to odd address. $0000 $0001 $0002 … * Address Error Exception … $3FFE $3FFF $4000
External Peripheral M68k T S I2 I1 I0 X N Z V C Exception Example: Interrupt Request M68k is executing instructions normally. 1 2 External peripheral has important task for M68k. 3 External peripheral asks for attention by outputting interrupt on IPL0, IPL1, IPL2. M68k compares interrupt level to SR. 4 M68k services/holds interrupt based on interrupt level 5
S0 S2 S4 S6 S0 S2 S1 S3 S5 S7 S1 Exception Example: Spurious Interrupt CLK A1 – A23 AS LDS/UDS R/W DTACK DTACK never responds D0 – D15 FC0 – FC2
Exceptions • Exceptions should be handled properly: • Can cause data loss. • System failure. • No prioritizing of tasks. • M68k handles exceptions using Exception Handling.
M68k Execution States • M68k always functions in either of the three states: • Normal. • Halted. • Exception.
Normal State • M68k operating normally. • Executing in user mode (S=0). • Has a restricted instruction set: • Can’t execute privileged instructions. • Prevents from executing potentially destructive instructions. • Reserved for SV.
Restricted Commands in Normal State • STOP • RESET • RTE • Any commands that modify the SR. • MOVE USP
Exception State • Handles special events (exceptions), then returns to normal execution. • Can execute all instructions. • Always saves processor context before handling exception, restored when returned to normal execution. • Always executes in SV mode (S=1).
The Status Register T S I2 I1 I0 X N Z V C Supervisor Mode, S = 1 (Exception State, Halted State) User Mode, S = 0 (Normal State)
Halted State • M68k stops execution of all instructions. • Done by activating HALT line: • Stops execution after current instruction. • Waits until HALT is inactive. • Resumes normal execution.
Halted State • Reason: • Catastrophic system failures (double bus faults, hardware failure). • User: intentionally activating the HALT line. • Purpose: • Protect data inside memory.
How States are Changed S=1, Exceptions, Interrupt Double bus fault/User-initiated Normal Exception Halted S=0, RTE, Edit SR Reset Exception User-initiated
Exception Handling • Series of steps performed to handle exceptions. • Ensures exceptions processed properly & resume normal execution. • Allows M68k to: • Save all processor status before exception. • Handle the exception. • Restore processor status when exception has been handled.
What Happens During an Exception? • Contents of SR saved to memory. • S bit is set. • Exception vector is obtained. • Processor context (PC & SR) saved on software stack. • PC set to address of exception vector. • Execution resumes from new PC location. • Control and context restored & execution resumes from last address before exception.
Exception Handler Operation Save D0-D7, A0-A7 Save PC and SR to stack. Handler Code TRAPV Read handler address from Vector Table. Reload D0-D7, A0-A7 Read original PC and SR from stack. RTE Next Instruction
Exception Processing Steps • Adjust SR. • Get Vector Number. • Save processor information. • Fetch new PC. • Restore context, resume last instruction in Normal mode.
Step 1: Adjust SR • To enter SV state and prepare for exception processing: • Store SR internally. • Set S = 1 (SV Mode). • Set T = 0 (Disable Tracing). • If interrupt, update Interrupt Mask Bits.
SR Bits Modified by Step 1 T S I2 I1 I0 X N Z V C T = 0 S = 1 Updated if: IREQ > ICURRENT
Step 2: Get Vector Number • Determines what type of exception, and get location of its appropriate handler. • VN: 8-bit value indicating exception type. • Supplied by external device or M68k. • What kind of exception is happening. • Ranges from $00 (0) to $FF (255). • VN used to find Vector Address (VA).
Vector Address (VA) • Special memory location that stores addresses of exception handlers. • VA obtained by multiplying VN with 4. • VN x 4 = VA. • VA contains a long-word value containing address of exception handler. • Contents insideVA loaded into PC, execution continued at this location.
How Exception Handling Works VA = VN x 4 $3000 Exception handler code $3002 VN $3004 $0006 $0000 xxx $0018 RTE $0004 xxx $0008 $3000 VN $000C xxx $0010 xxx PC $3000 • VN given by external • circuit/M68k. Based on VN, • M68k knows what type • of exception has occurred. 3. M68k saves registers into stack, execution resumes at exception handler address. 2. VN x 4 = VA. Address of exception handler contained inside VA. M68k loads value into PC.
VN VA = VN x 4 Assignment 0 0 = $0000 Reset: Initial SSP and PC 2 8 = $0008 Bus Error 3 12 = $000C Address Error 5 20 = $0014 Divide by Zero 8 32 = $0020 Privilege Violation 9 36 = $0024 Trace 24 96 = $0060 Spurious Interrupt 25-31 $0064 - $007C Interrupt Auto-vectors 32-47 $0080 – 00BC TRAP Instruction Vectors Vector Table (Antonakos, pg. 110)
Step 3: Save Processor Information • Saves processor context into stack: • PC: next instruction after exception processing. • SR. • Bus cycle attempted, part of instruction (for bus error and address error). • SSP is used already in SV mode.
How Context Saved into Stack – Typical Exception New SSP location Status Register PC (High Word) PC (Low Word) Old SSP location
How Context Saved into Stack • PC = $001111 • SR = $0115 • SSP = $002FFF $0115 New SSP location $002FF8 SSP-6 $0000 SSP-4 $002FFB $1111 SSP-2 $002FFD … Old SSP location SSP $002FFF
Step 4: Fetch New PC • Loads PC from vector table in Step 2. • Processing resumes at exception handler. • Returned to normal processing using (Return to Exception) RTE.
Exception Handler • Contains instructions on how to process the exception. • Located in SV memory space. • RTE at the end, resumes normal execution.
Exception Handler Contents Save Registers Save Registers, PC into Stack Handler Code Code to handle exception Reload Registers Reload Registers from Stack RTE (Return to Exception) Reload PC from stack, execution resumes at next instruction after exception.
Exception Example (Divide by Zero) • MOVE.W #0,D0 • MOVE.L #500000, D1 • DIVU D0,D1 • PC = $1040 • SR = 500,000 / 0 = ? * Divide by Zero Exception T S I I I X N Z V C 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 1 6
Step 1: Adjust SR • Store SR internally. • Set S = 1 (SV Mode). • Set T = 0 (Disable Tracing). • If interrupt, update Interrupt Mask Bits.
T S I I I X N Z V C 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 0 Step 1: Adjust SR T = 0 S = 1 Not interrupt = unchanged
Step 2: Get Vector Number • Divide by zero exception VN is VN = 5 • VN x 4 used to find Vector Address (VA). VA = VN x 4 = 20 = $000014
Vector Address (VA) • Special memory location that stores addresses of exception handlers. • VA obtained by multiplying VN with 4. • VN x 4 = VA. • VA contains a long-word value containing address of exception handler. • Contents insideVA loaded into PC, execution continued at this location.
Vector Table Address Value $0000 $00400500 $0008 $00400600 $000C $00400700 $0014 $00400800 $0020 $00400900 $0024 $00400A00 $0060 $00400B00
Step 3: Save Processor Information • Saves processor context into stack. • SSP is used already in SV mode. $0016 (SR) New SSP $3FFA PC = $001040 SSP/A7 = $004000 SR = $0016 $0000 (PC) $1040 (PC) Old SSP $4000
Address Value $0000 $00400500 $0008 $00400600 $000C $00400700 $0014 $00400800 $0020 $00400900 $0024 $00400A00 $0060 $00400B00 Step 4: Fetch New PC • Loads PC from vector table in Step 2. • Processing resumes at exception handler. New PC = $00400800
Exception Handler Contents $00400800 Save D0-D7, A0–A7 Save Registers into Stack Handler Code Code to handle exception Reload D0-D7, A0-A7 Reload Registers from Stack RTE (Return to Exception) Reload PC, SR from stack, execution resumes at next instruction after DIVU.
Handling Multiple Exceptions • It is possible for another exception to happen when the current exception is being handled. • An bus error occurs when M68k is handling an interrupt. • A trace exception generated during a divide by zero exception.
How does M68k handle multiple exceptions? • In M68k, each exception has priority level: • Reset exception has higher priority than Trace exception. • An interrupt exception is more important than a Divide-by-Zero exception. • When multiple exceptions happen, the more important one executed first.
Group Exception Priority 0 Reset (Highest) (Lowest) Bus Error Address Error 1 Trace (Highest) (Lowest) Interrupt Illegal Privilege 2 TRAP All have same priority. TRAPV CHK Zero Divide Exception Grouping & Priority Importance