EE 457 Unit 8. Exceptions What Happens When Things Go Wrong

Similar documents
What are Exceptions? EE 457 Unit 8. Exception Processing. Exception Examples 1. Exceptions What Happens When Things Go Wrong

There are different characteristics for exceptions. They are as follows:

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

Initial Representation Finite State Diagram. Logic Representation Logic Equations

EE 109 Unit 17 - Exceptions

Outline. A pipelined datapath Pipelined control Data hazards and forwarding Data hazards and stalls Branch (control) hazards Exception

CPS104 Computer Organization and Programming Lecture 17: Interrupts and Exceptions. Interrupts Exceptions and Traps. Visualizing an Interrupt

Initial Representation Finite State Diagram Microprogram. Sequencing Control Explicit Next State Microprogram counter

CISC 662 Graduate Computer Architecture Lecture 7 - Multi-cycles

Microprogramming. Microprogramming

CISC 662 Graduate Computer Architecture Lecture 7 - Multi-cycles. Interrupts and Exceptions. Device Interrupt (Say, arrival of network message)

Lecture 9 Pipeline and Cache

Lecture 11: Interrupt and Exception. James C. Hoe Department of ECE Carnegie Mellon University

Instruction Level Parallelism. ILP, Loop level Parallelism Dependences, Hazards Speculation, Branch prediction

LECTURE 3: THE PROCESSOR

Appendix C: Pipelining: Basic and Intermediate Concepts

COMPUTER ORGANIZATION AND DESIGN

CMCS Mohamed Younis CMCS 611, Advanced Computer Architecture 1

Instruction Set Architecture of MIPS Processor

Computer Systems Architecture I. CSE 560M Lecture 5 Prof. Patrick Crowley

EE 457 Midterm Summer 14 Redekopp Name: Closed Book / 105 minutes No CALCULATORS Score: / 100

Lecture 3: The Processor (Chapter 4 of textbook) Chapter 4.1

Exception Handling. Precise Exception Handling. Exception Types. Exception Handling Terminology

Predict Not Taken. Revisiting Branch Hazard Solutions. Filling the delay slot (e.g., in the compiler) Delayed Branch

Department of Computer and IT Engineering University of Kurdistan. Computer Architecture Pipelining. By: Dr. Alireza Abdollahpouri

COMPUTER ORGANIZATION AND DESIGN

Exceptions and Interrupts

Instruction Pipelining Review

ECE 486/586. Computer Architecture. Lecture # 12

COMPUTER ORGANIZATION AND DESI

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

EE 457 Midterm Summer 14 Redekopp Name: Closed Book / 105 minutes No CALCULATORS Score: / 100

Lecture 7: Pipelining Contd. More pipelining complications: Interrupts and Exceptions

Chapter 4. The Processor. Computer Architecture and IC Design Lab

Announcement. ECE475/ECE4420 Computer Architecture L4: Advanced Issues in Pipelining. Edward Suh Computer Systems Laboratory

CENG 3420 Lecture 06: Pipeline

Full Datapath. Chapter 4 The Processor 2

ECE 250 / CPS 250 Computer Architecture. Processor Design Datapath and Control

Notes Interrupts and Exceptions. The book uses exception as a general term for all interrupts...

The Operating System (OS) MicroComputer Engineering OperatingSystem slide 1!

THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY Computer Organization (COMP 2611) Spring Semester, 2014 Final Examination

LECTURE 10. Pipelining: Advanced ILP

Lecture 4: Review of MIPS. Instruction formats, impl. of control and datapath, pipelined impl.

COMPUTER ORGANIZATION AND DESIGN. The Hardware/Software Interface. Chapter 4. The Processor: A Based on P&H

Instruction Level Parallelism. Appendix C and Chapter 3, HP5e

MIPS An ISA for Pipelining

Minimizing Data hazard Stalls by Forwarding Data Hazard Classification Data Hazards Present in Current MIPS Pipeline

CSEE 3827: Fundamentals of Computer Systems

Traps, Exceptions, System Calls, & Privileged Mode

Thomas Polzer Institut für Technische Informatik

ENE 334 Microprocessors

Processor (I) - datapath & control. Hwansoo Han

Page 1, 5/4/99 8:22 PM

ECE/CS 552: Pipeline Hazards

RISC Processor Design

EE557--FALL 1999 MAKE-UP MIDTERM 1. Closed books, closed notes

Pipelined Datapath. Reading. Sections Practice Problems: 1, 3, 8, 12

COSC4201 Pipelining. Prof. Mokhtar Aboelaze York University

Advanced issues in pipelining

ECE 313 Computer Organization FINAL EXAM December 11, Multicycle Processor Design 30 Points

EE 457 Unit 6a. Basic Pipelining Techniques

Processor Design Pipelined Processor (II) Hung-Wei Tseng

Anti-Inspiration. It s true hard work never killed anybody, but I figure, why take the chance? Ronald Reagan, US President

Chapter 4 The Processor 1. Chapter 4A. The Processor

CS 351 Exam 2 Mon. 11/2/2015

ECE 313 Computer Organization FINAL EXAM December 14, This exam is open book and open notes. You have 2 hours.

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

CO Computer Architecture and Programming Languages CAPL. Lecture 18 & 19

CSEN 601: Computer System Architecture Summer 2014

The Processor. Z. Jerry Shi Department of Computer Science and Engineering University of Connecticut. CSE3666: Introduction to Computer Architecture

CPE 335. Basic MIPS Architecture Part II

1 Hazards COMP2611 Fall 2015 Pipelined Processor

Chapter 4. The Processor

EE 109 Unit 15 Subroutines and Stacks

Computer and Information Sciences College / Computer Science Department Enhancing Performance with Pipelining

Very short answer questions. "True" and "False" are considered short answers.

CSE 141 Computer Architecture Spring Lectures 11 Exceptions and Introduction to Pipelining. Announcements

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

EITF20: Computer Architecture Part2.2.1: Pipeline-1

Anne Bracy CS 3410 Computer Science Cornell University

RISC Design: Multi-Cycle Implementation

CS 252 Graduate Computer Architecture. Lecture 4: Instruction-Level Parallelism

CSE502: Computer Architecture CSE 502: Computer Architecture

CSE 378 Midterm 2/12/10 Sample Solution

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

ECS 154B Computer Architecture II Spring 2009

Chapter 12. CPU Structure and Function. Yonsei University

EITF20: Computer Architecture Part2.2.1: Pipeline-1

Lecture 10: Simple Data Path

Chapter 4 The Processor 1. Chapter 4B. The Processor

Lecture 7 Pipelining. Peng Liu.

The Processor: Improving the performance - Control Hazards

Pipelining. Principles of pipelining. Simple pipelining. Structural Hazards. Data Hazards. Control Hazards. Interrupts. Multicycle operations

Pipelined Datapath. Reading. Sections Practice Problems: 1, 3, 8, 12 (2) Lecture notes from MKP, H. H. Lee and S.

Mark Redekopp and Gandhi Puvvada, All rights reserved. EE 357 Unit 15. Single-Cycle CPU Datapath and Control

COMP2611: Computer Organization. The Pipelined Processor

EECS 151/251A Fall 2017 Digital Design and Integrated Circuits. Instructor: John Wawrzynek and Nicholas Weaver. Lecture 13 EE141

Suggested Readings! Recap: Pipelining improves throughput! Processor comparison! Lecture 17" Short Pipelining Review! ! Readings!

EE 457 Unit 9a. Exploiting ILP Out-of-Order Execution

Instr. execution impl. view

Transcription:

1 EE 457 Unit 8 Exceptions What Happens When Things Go Wrong

2 What are Exceptions? Exceptions are rare events triggered by the hardware and forcing the processor to execute a software handler HW Interrupts Error conditions Traps or system calls Similar to conditional branches/subroutine calls Utilize the hardware already in place for branches Flush pipeline Fetch from entry point of software exceptions handler

3 Exception Processing Save necessary state to be able to restart the process Save PC of offending instruction Call an appropriate handler routine to deal with the error / interrupt / syscall Handler identifies cause of exception and handles it May need to save more state Restore state and return to offending application (or kill it if recovery is impossible)

4 MIPS Coprocessor 0 Registers Status Register Enables and disables the handling of exceptions/interrupts Controls user/kernel processor modes Kernel mode allows access to certain regions of the address space and execution of certain instructions Register: Indicates which exception/interrupt occurred Exception PC (EPC) Register Indicates the address of the instruction causing the exception This is also the instruction we should return to after handling the exception Coprocessor registers can be accessed via the mtc0 and mfc0 instructions mfc0 $gpr,$c0_reg # R[gpr] = C0[c0_reg] mtc0 $gpr,$c0_reg # C0[c0_reg] = R[gpr]

IM7 IM6 IM5 IM4 IM3 IM2 IM1 IM0 UM ERL EXL IE 5 Status Register Register 12 in coprocessor 0 Bit definitions IM[7:0] Interrupt Mask 1 = Ignore interrupt / 0 = Allow interrupt 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 Status Register 31 15 8 4 2 1 0 0000 0000 0000 0000 0 0 0 0 0

BD PI7 PI6 PI5 PI4 PI3 PI2 PI1 PI0 6 Register Register 13 in coprocessor 0 Bit definitions BD Branch Delay The offending instruction was in the branch delay slot EPC points at the branch but it was EPC+4 that caused the exception PI[7:0] Pending Interrupt 1 = Interrupt Requested / 0 = No interrupt requested Exception Code Indicates cause of exception (see table) Code 0 Interrupt (HW) 4, 5 Load (4), Store (5) Address Error 6, 7 Instruc. (6), Data (7) Bus Error 8 Syscall 9 Breakpoint 10 Reserved Instruc. 11 CoProc. Unusable 12 Arith. Overflow 13 Trap 15 Floating Point Register 31 15 8 7 6 2 1 0 000 0000 0000 0000 0 Exception Code 0 0

7 EPC Register Exception PC holds the address of the offending instruction Can be used along with 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 EPC = Exception PC 31 0 Address of instruction that generated the exception

8 Exception Examples 1 Example Stage Action I/O Device Interrupt A peripheral device requires action from the CPU (Interrupt I/O Driven) Operating System Calls ( Traps ) [e.g. File Open] Trap instruction causes processor to enter kernel mode WB ID Take ASAP Precise Instruction Tracing and Breakpoints When TRAP Bit is set all instructions cause exceptions Particular instructions are flagged for exceptions (debugging) ID Precise Arithmetic Exceptions Overflow or Divide-by-0 EX Precise

9 Exception Examples 2 Example Stage Action Page Faults Virtual memory access fault (no Page Table entry resident in memory) Misaligned Memory Address Address is not multiple of operand size Memory Protection Violations Address is out of bounds; RWX violation Undefined Instructions Decode unit does not recognize opcode or other fields Could be useful to extend the instruction set Hardware failure Unrecoverable hardware error is detected; execution is compromised Power Failure Power has fallen below a threshold; Trap to software to save as much state as possible IF or MEM EX MEM ID WB WB Precise Abort Process Abort Process Precise (Why not abort) Take ASAP Take ASAP

10 System Calls/Traps A controlled-method for user application calling OS services Switches processor to kernel mode where certain privileges are enabled that we would not want normal user apps to access x86 System Call (old DOS OS call) IN AH, 01H INT 20H // getchar() PSW Processor Status Word Instruction Tracing and Breakpoint Single-stepping & Breakpoint in x86 TF Trap Flag

11 Exception Processing Exception = Asynchronous (non-programmed) control transfer Synchronous system call/trap Must save PC of offending instruction, program state, and any information needed to return afterwards Restore upon return User Program System Exception Handler Return from exception

12 Problem of Calling a Handler We can t use explicit jal instructions to call exception handlers since we don t when they will occur.text MAIN: ---- ---- ---- ---- ---- jr $ra Many instructions could cause an error condition. Or a hardware event like a keyboard press could occur at any point in the code.

13 Solution for Calling a Handler Since we don t know when an exception will occur there must be a preset location where an exception handler should be defined or some way of telling the processor in advance where our exception handlers will be located Method 1: Single hardwired address for master handler Early MIPS architecture defines that the exception handler should be located at 0x8000_0180. Code there should then examine CAUSE register and then call appropriate handler routine Method 2: Vectored locations (usually for interrupts) Each interrupt handler at a different address based on interrupt number (a.k.a. vector) (INT1 @ 0x80000200, INT2 @ 0x80000300) Method 3: Vector tables Table in memory holding start address of exception handlers (i.e. overflow exception handler pointer at 0x0004, FP exception handler pointer at 0x0008, etc.)

14 Handler Calling Methods Kernel Space 0xffffffff Kernel Space 0xffffffff Kernel Space 0xffffffff INT n Hand. 0x80000??? INT 2 Hand. x3 Exception Handler 0x80000180 0x80000000 INT 2 Hand. INT 1 Hand. Exception Handler 0x80000300 0x80000200 0x80000180 0x80000000 Handler 1 INT 1 Hand. addr x3 addr x2 addr x1 x1 x2 0x80000000 User Space User Space User Space 0x00000000 0x00000000 0x00000000 Method 1 Method 2 Method 3

15 Precise Exceptions Two conditions: Synchronized with an instruction A particular instruction caused the exception Not an interrupt or some kind of failure Must resume execution after handler Restart the instruction causing the exception after exception handler returns Not an exception that will cause the process to abort Not difficult in a processor executing one instruction at a time Very difficult in architectures in which multiple instruction execute concurrently (i.e. our 5-stage pipeline)

16 Why are Exceptions So Important? Exceptions are part of the ISA (Instruction Set Architecture) specification Any implementation of an ISA must comply with its Exception model Precise exception handling constrains what the architecture can do Exceptions are rare yet we must functionally support them If we did not have to comply to the exception model architects would have a lot more freedom in their design When designing micro-architectures for the common case, exceptions must always be in the back of your mind!

17 Exceptions in the 5-Stage Pipeline To support precise exceptions in the 5-stage pipeline we must Identify the pipeline stage and instruction causing the exceptions Any stage can trigger an exception (except for the WB stage) Identify the cause of the exception Save the process state at the faulting instruction Including registers, PC, and cause Usually done by software exception handler Complete the execution of instructions preceding the faulting instruction Flush instruction following the faulting instruction plus the faulting instruction Transfer control to exception handler Use many of the same mechanisms as conditional branches.

ALUSelB Instruction Register Pipeline Stage Register Pipeline Stage Register D-Cache 8000_0180 PC I-Cache Pipeline Stage Register IRWrite Mem WB WB 18 Exception in EX stage 0 1 2 PCWrite 4 +. rs rt 5 5 HDU IF.Flush Control Read Reg. 1 # Read Reg. 2 # Write Reg. # Write Data Stall Read data 1 Read data 2 Register File FLUSH EX.RegWrite EX.RegDst Branch Sh. Left 2 0 1 2 3 0 1 2 3 ALUSelA = + Ex Mem WB 0 1 EPC Save EPC=PC+4 of offending instruction Record Add 3 rd input of 0x8000_0180 to PCSrc Mux (start address of exception handler) ALU Res. MemRead & MemWrite 0 1 MemToReg Reset Sign 16 Extend rs 32 rt Forwarding Unit ALUSrc 0 1 RegDst rd

19 Exception Handling Complexities When the arithmetic exception is triggered in EX, we must flush IF, ID and EX and start fetching from 0x8000_0180 Note that the handler s software must have access to CAUSE and EPC registers to figure out what to do Realize though exceptions may occur in all but the WB stage 4 possible values of CAUSE and EPC Software needs to know which value is the actual cause and EPC Depending on the stage where the exception occurs, we have to flush different stages Muxes EPC EPC EPC EPC EPC IM Reg ALU DM Reg But it gets worse!!!

20 More Complex Complexities? What happens if multiple exceptions occur in the same cycle from different instructions in different stages Should take the oldest exception in program/process order Program/process order = Order if only 1 instruction were executed at a time (= Fetch order) Thus oldest instruction is the one deepest (furthest) into the pipeline There is no point in dealing with all exceptions, just the oldest one Let software deal with the oldest and then restart if later instruction were going to generate an exception, then they will again upon restart and we can handle it then But it gets worse!!! Cycle n We can start handling illegal instruction exception. What could happen on cycle n+1? IM Reg ALU DM Reg I-Fetch TLB miss / Page Fault Illegal Instruction SW address computation LW TLB miss Page hit (no exception)

21 More Complex Complexities? Remember we must complete instruction preceding the faulting instruction Remember we are supposed to handle instruction in program order (not temporal order) Cycle n+1 Which exception should we have handled? IM Reg ALU DM Reg I-Fetch TLB miss / Page Fault Illegal Instruction SW page fault LW

22 Simplify the Process It is not practical to take an exception in the cycle when it happens Multiple exceptions in the same cycle It is complex to take exception in various pipeline stages since we have to take them in program order and not temporal order Instead, we will just tag an instruction in the pipeline if it causes and exception (recording the cause and EPC) Turn the offending instruction into a NOOP (bubble) Let the instructions continue to flow down the pipeline and handle the offending instruction s execution in the WB stage The cause and status info is carried down the pipe via stage registers Exception remains silent until it reaches the WB stage Exceptions are then processed into the WB stage Muxes Actual EPC & Register EPC EPC EPC EPC EPC IM Reg ALU DM Reg

23 Handling in WB Stage Handling in WB stage helps deal with temporal vs. program order issues CC1 CC2 CC3 CC4 CC5 CC6 CC7 CC8 40: LW $1,0($8) IM Reg ALU DM Reg 44: AND $12,$2,$5 IM Reg ALU DM Reg 48: OR $13,$6,$2 IM Reg ALU DM Reg 52: ADD $14,$2,$2 IM Reg ALU DM Reg

24 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 & are software-readable (movable to GPR s) This is a general approach to dealing with exceptions in the processor: Wait until the faulting instruction exits the machine to trigger the handling procedure