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

Similar documents
Very Simple MIPS Implementation

Computer Architecture EE 4720 Midterm Examination

These slides do not give detailed coverage of the material. See class notes and solved problems (last page) for more information.

Very Simple MIPS Implementation

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

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

09-1 Multicycle Pipeline Operations

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

This Set. Scheduling and Dynamic Execution Definitions From various parts of Chapter 4. Description of Three Dynamic Scheduling Methods

This Set. Scheduling and Dynamic Execution Definitions From various parts of Chapter 4. Description of Two Dynamic Scheduling Methods

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

Computer Architecture EE 4720 Midterm Examination

Computer Architecture EE 4720 Final Examination

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

EITF20: Computer Architecture Part2.2.1: Pipeline-1

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

GPU Microarchitecture Note Set 2 Cores

EITF20: Computer Architecture Part2.2.1: Pipeline-1

LECTURE 3: THE PROCESSOR

Overview of the MIPS Architecture: Part I. CS 161: Lecture 0 1/24/17

Computer Architecture EE 4720 Midterm Examination

EITF20: Computer Architecture Part2.2.1: Pipeline-1

CISC 662 Graduate Computer Architecture Lecture 7 - Multi-cycles

CS3350B Computer Architecture Winter 2015

Spring 2014 Midterm Exam Review

Instruction Level Parallelism. Appendix C and Chapter 3, HP5e

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

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

Computer Architecture EE 4720 Midterm Examination

Advanced issues in pipelining

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

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

Chapter 12. CPU Structure and Function. Yonsei University

Appendix C: Pipelining: Basic and Intermediate Concepts

CS61C : Machine Structures

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

CPU Structure and Function

LECTURE 10. Pipelining: Advanced ILP

Slides for Lecture 15

Full Datapath. Chapter 4 The Processor 2

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

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

Definition: An operating system is the software that manages resources

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

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Computer Architecture EE 4720 Final Examination

LSU EE 4720 Homework 5 Solution Due: 20 March 2017

13-2 EE 4770 Lecture Transparency. Formatted 8:18, 13 March 1998 from lsli

ECE331: Hardware Organization and Design

ECE 353 Lab 3. (A) To gain further practice in writing C programs, this time of a more advanced nature than seen before.

Computer Architecture EE 4720 Midterm Examination

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

COMPUTER ORGANIZATION AND DESIGN

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Lecture 10: Simple Data Path

ECE473 Computer Architecture and Organization. Pipeline: Control Hazard

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

are Softw Instruction Set Architecture Microarchitecture are rdw

UNIT- 5. Chapter 12 Processor Structure and Function

Anne Bracy CS 3410 Computer Science Cornell University

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

COMPUTER ORGANIZATION AND DESI

CSEE 3827: Fundamentals of Computer Systems

Instruction Pipelining

Instruction Pipelining

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

3. (2 pts) Clock rates have grown by a factor of 1000 while power consumed has only grown by a factor of 30. How was this accomplished?

3/12/2014. Single Cycle (Review) CSE 2021: Computer Organization. Single Cycle with Jump. Multi-Cycle Implementation. Why Multi-Cycle?

Computer System Architecture Midterm Examination Spring 2002

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

CSE 120 Principles of Operating Systems

Instruction Set Architecture of MIPS Processor

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

CS 152, Spring 2011 Section 2

These slides do not give detailed coverage of the material. See class notes and solved problems (last page) for more information.

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

Syscalls, exceptions, and interrupts, oh my!

CS 104 Computer Organization and Design

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

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 10 Exceptions and Interrupts. How are exceptions generated?

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

Advanced processor designs

Topics in computer architecture

ECE 486/586. Computer Architecture. Lecture # 7

Chapter 4. The Processor

2. (3 pts) There are two different ways to measure performance. What are they? 3. (3 pts) Why doesn t MIPS have a subtract immediate instruction?

ECE 313 Computer Organization FINAL EXAM December 13, 2000

Computer Architecture EE 4720 Final Examination

CS 333 Introduction to Operating Systems. Class 1 - Introduction to OS-related Hardware and Software

1 Hazards COMP2611 Fall 2015 Pipelined Processor

Operating Systems and Interrupts/Exceptions

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

Computer Architecture

MIPS An ISA for Pipelining

Interrupt: (verb) the interruption of a CPU s normal processing...using a mechanism provided for this purpose.

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

Instruction Pipelining Review

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

Transcription:

08 1 Interrupts and Exceptions 08 1 Notes The book uses exception as a general term for all interrupts...... in these notes interrupt is used as the general term...... and a narrower definition is used for exception. The definitions of trap, interrupt, and exception given here...... are not explicitly provided in the text...... but are widely used. 08 1 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 1

08 2 Interrupts 08 2 Interrupt: Event that requires OS attention. Operating system takes over computer...... attends to whatever caused the interrupt...... and (most of the time) resumes interrupted program. Interrupt Terminology Handler: The OS program that takes over in response to interrupt. Privileged Mode: A state in which the CPU controller and memory system...... do not restrict instructions that can be executed...... or memory that can be accessed. Processor switches into privileged mode in response to interrupt...... and out of privileged mode when resuming the program. 08 2 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 2

08 3 Three Types of Interrupts. 08 3 Three Types: Trap: Sort of a subroutine call to OS. Exception: Something went wrong, triggered by an executing instruction. Exception has both a general and this specific meaning. Hardware Interrupt: Something outside the CPU is trying to get the computer s attention. Interrupt has both a general and this specific meaning. 08 3 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 3

08 4 Traps 08 4 Trap: (1) An instruction intended for user programs that transfers control to the operating system (privileged code). (2) The execution of such an instruction. Sort of a subroutine call to OS. Trap causes branch to OS code and a switch to privileged mode. Privileged Mode: a.k.a. System Mode and Supervisor Mode A processor mode in which there are fewer restrictions on instruction execution. Some instructions can only be executed in privileged mode. When in privileged mode a trap handler is executed to service request. Trap Handler: A program, running in privileged mode that responds to a trap. Traps typically used for I/O, memory allocation, etc. 08 4 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 4

08 5 08 5 Example, SPARC V8 trap instruction: ta rs1, imm. ISA has a trap base register (TBR) that is used to construct the trap address. Trap handler starts in trap table, each entry holds first four instructions of trap handler. Trap Address Construction: OS initializes TBR with upper 20 bits of trap table base. When, say, ta r1,3 executed, bits 4-10 set to low seven bits of r1+3. Low four bits of TBR always zero. 08 5 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 5

08 6 Example: Using trap for read on Solaris (Sun OS) 08 6 System Calls read(2) NAME read, readv, pread - read from file SYNOPSIS #include <unistd.h> ssize_t read(int fildes, void *buf, size_t nbyte); DESCRIPTION The read() function attempts to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.! Read System Call.! Parameters placed in %o0, %o1, %o2.! %o0: File descriptor (fildes)! %o1: Buffer pointer (buf, address where read data copied to).! %o2: Number of bytes to read. (nbyte) mov SYS_read, %g1! Argument for trap. ta %g0, ST_SYSCALL! Call trap. 08 6 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 6

08 7 08 7 Some SPARC Trap Codes /* Copyright (c) 1989 by Sun Microsystems, Inc. */ /* Software traps (ticc instructions). */ /* In sys/trap.h */ #define ST_OSYSCALL 0x00 #define ST_BREAKPOINT 0x01 #define ST_SYSCALL 0x08 #define ST_GETCC 0x20 // Move condition code to reg. #define ST_SETCC 0x21 // Move condition code from reg. /* In sys/syscall.h */ #define SYS_syscall 0 #define SYS_exit 1 #define SYS_fork 2 #define SYS_read 3 #define SYS_write 4 #define SYS_open 5 #define SYS_close 6 mov SYS_read, %g1! Argument for trap. ta %g0, ST_SYSCALL! Call trap. 08 7 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 7

08 8 08 8 Exceptions Exception: An interruption in normal execution triggered by an instruction that could not complete execution. An exception occurs when an instruction cannot fully execute. Faulting Instruction: Instruction that caused an exception. 08 8 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 8

08 9 08 9 Some Exception Causes Access to unallocated memory, a segmentation fault. Access to memory that s paged out (on disk), a page fault. Division by zero. Unimplemented instruction. In response to an exception...... OS fixes problem and re-tries the faulting instruction...... or OS fixes problem and skips the faulting instruction...... or OS terminates program. 08 9 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 9

08 10 Exception Example 08 10 Exception in ME stage of lw, indicated with an asterisk. Cycle: 0 1 2 3 4 5... 99 100... add r1, r2, r3 IF ID EX ME WB lw r6, 0(r1) IF ID EX ME*x IF ID sub r5, r6, r7 IF ID EXx IF xor r10, r11, r12 IF IDx and r20, r21, r22 IFx... Handler: sw... IF...... eret (exception return) IF ID EX ME WB Cycle 4: lw raises a page-fault exception,...... lw and following instructions are squashed,...... and address of handler routine computed and sent to PC. Cycle 5: handler fetched. When handler returns at 99, execution resumes with lw (its second try). 08 10 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 10

08 11 Another Exception Example 08 11 Two faults, ID stage of ant and ME stage of lw, indicated asterisks. Cycle: 0 1 2 3 4 5... 99 100... add r1, r2, r3 IF ID EX ME WB lw r6, 0(r1) IF ID EX ME*x IF ID EX ME WB ant r5, r6, r7 IF ID*EXx IF ID*EX MEx xor r10, r11, r12 IF IDx IF ID EXx and r20, r21, r22 IFx IF IDx... Handler: sw... IF... IF...... eret (exception return) IF ID EX ME WB Cycle 3: ant raises an illegal instruction exception. Cycle 4: lw raises an illegal instruction exception. Hardware checks for exceptions in ME...... to preserve program order...... and so lw is handled first. When handler returns at 99, execution resumes with lw (its second try). 08 11 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 11

Possible MIPS Exception Hardware 08 12 08 12 msb lsb 29:26 25:0 29:0 IF + ID = EX ME WB 15:0 MIPS' ISA-de ned handler locations. decode exception 16'hbfc0 16'h 20 16'ha000 16'h100 16'h200 msb lsb Exception handler address. priv PSW priv +1 2'b0 30 2 msb Addr Mem Port PC lsb priv Data Out Hardware interrupt code (or zero) from external devices. NPC priv IR 25:21 20:16 15:0 decode exc Addr Addr Addr decode format immed msb Data Data D In NPC rsv rtv IMM priv exc mop dst exc msb ALU rtv exc mop dst Mem Port Addr DIn priv mop nop Squash Exception code, or zero for no exception. MSb of non-zero exception code is always 1. priv 5'b0 D Out exc msb msb ALU MD dst 08 12 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 12

08 13 08 13 Interrupt Caused by an external event...... which typically needs routine attention. For example, Disk drive has data that was requested 20 ms ago. User pressed a key on the keyboard. User sneezed, causing mouse to move. Timer (used by the OS as an alarm clock) expired. 08 13 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 13

08 14 Hardware Interrupt Example 08 14 Example: add r1, r2, r3 sub r4, r5, r6 xor r7, r8, r9 IF ID EX ME WB IF ID EX ME WB IF ID EX ME WB As execution reaches code above, achoooo (user sneezes)...... moving mouse, triggering an interrupt. Based on time of sneeze, hardware completes add and sub...... but squashes xor (for now). The handler starts...... the screen pointer (the little arrow) is moved...... the handler finishes...... and execution resumes with xor. 08 14 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 14

08 15 08 15 Interrupt Mechanisms: Goals and Difficulties Goals Exceptions: Handle in program order. All Interrupts: Ability to resume execution as though nothing happened. Precise Exception: Must be able to re-start or skip faulting instruction...... and so last instruction must immediately precede faulting instruction. Difficulties Precise exceptions are easy to implement for integer pipeline...... because exceptions can be detected before later instructions write. Much harder with floating point operations (covered in next set). 08 15 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 15

08 16 08 16 Actions Initiated by HW Interrupt & Exceptions Simple Case Hardware executes up to and including last instruction...... and squashes all following instructions. Type and timing of interrupt determine a last instruction. The last and all preceding instructions allowed to complete...... instructions following last are squashed (nullified). Address of handler is determined...... based on the type of interrupt and exception. Processor jumps to handler (OS code) and switches to privileged mode. Handler attends to interrupt. If appropriate, state is restored and program resumes...... with the instruction following last. 08 16 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 16

08 17 08 17 The Last Instruction The last and all preceding instructions must execute completely...... while instructions following the last must have no effect at all...... even though they may have already started when the interrupt occurred. These squashed instructions will be executed after the handler completes. 08 17 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 17

08 18 08 18 Choice of Last Instruction Choice depends on type of interrupt. For traps, the trap instruction itself is last. No problem. For hardware interrupts, a convenient last instruction can be chosen. No problem again. Precise exception, the instruction preceding faulting instruction. Problem: exception can occur in any of several stages. Problem: more than one instruction can raise exception. Problem: an instruction can raise exception before its predecessor. Problem: despite problems, some exceptions must be precise. 08 18 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 18

08 19 08 19 Squashing Instructions When an interrupt occurs instructions may be squashed. When the handler finishes and the program resumes... it must be as though the squashed instructions never even started. So...... they cannot write registers...... they cannot write memory...... or set any kind of condition codes...... unless...... the state change can be un-done. 08 19 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 19

08 20 08 20 Squashing Instructions in MIPS In the MIPS implementation it s easy to squash integer instructions...... because they only change state in the last two stages (ME & WB). To squash an instruction in the IF, ID, or EX stages...... the opcode is replaced with a NOP...... or any control bits that initiate a memory or register write...... are set to perform no action. An instruction in WB cannot easily be squashed...... because the following instruction, in ME,...... would already be changing state. Fortunately, there s never a need to squash an instruction in WB...... although an already-squashed instruction can enter WB. An instruction in ME cannot be squashed...... unless the memory operation fails...... which, luckily, is the only reason to squash the instruction. 08 20 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 20

08 21 08 21 Implementing Exceptions in MIPS Each stage has its own hardware to detect exceptions. IF: Page fault etc on instruction fetch, detected by mem port. ID: Illegal opcode, detected by decode logic. EX: Arithmetic exception, detected by ALU. ME: Page fault on load/store, detected by memory port. Pipeline latches have exception registers. Normally set to null. If exception occurs, written with exception info. 08 21 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 21

08 22 08 22 When exception occurs: Exception pipeline latch (at faulting instruction) written. NOPs written to IRs for following (to the left) instructions. Following instructions proceed normally. In writeback stage: Exception latch checked (every cycle). If exception latch non-null...... exception latch value written to cause register...... PC written to EPC (exception PC) register...... and processor jumps to an entry in the exception table...... which contains beginning of handler. Note Exceptions handled in program order because exception register tested at one place in pipeline, in class ME. 08 22 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 22

08 23 08 23 Precise Exceptions Precise Exception: An exception for which when the handler is called all of the instructions before the faulting instruction finish and none of the instructions after finish. Deferred Exception: An exception for which when the handler is called all of the instructions before the faulting instruction finish and a few consecutive instructions after the faulting instruction finish. 08 23 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 23

08 24 08 24 Resuming of Interrupted Program After handler finishes it may resume or terminate program. With precise exceptions handler can resume at or after faulting instruction. With deferred exceptions handler can only resume long after faulting instruction. Resuming of Program for Precise Exceptions If an exception is precise, handler can return to faulting instruction (thereby giving it a second chance). Handler also has option to return to instruction after faulting instruction...... this is done to emulate instructions that are not implemented in hardware. Resuming of Program for Deferred Exceptions Cannot return to faulting instruction if it raises a deferred exception...... because instructions after faulting instruction would be executed twice! 08 24 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 24

08 25 08 25 Need for Precise Exceptions Precise exceptions are necessary for some instructions...... and expensive for others. They are necessary for instructions...... such as memory loads and stores. For other instructions they are a convenience...... for example FP instructions...... that can write error values instead of numbers...... if they don t complete. In many systems precise exceptions are optional for floating point...... but always provided for other instructions. 08 25 EE 4720 Lecture Transparency. Formatted 13:02, 3 April 2017 from lsli08. 08 25