Interrupts/Exceptions

Slides:



Advertisements
Similar presentations
CPU Structure and Function
Advertisements

1 (Review of Prerequisite Material). Processes are an abstraction of the operation of computers. So, to understand operating systems, one must have a.
Computer Organization and Architecture
Computer Organization and Architecture
Chapter 6 Limited Direct Execution
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Pipeline Exceptions & ControlCSCE430/830 Pipeline: Exceptions & Control CSCE430/830 Computer Architecture Lecturer: Prof. Hong Jiang Courtesy of Yifeng.
CH12 CPU Structure and Function
Constructive Computer Architecture Interrupts/Exceptions/Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
Interrupts / Exceptions / Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology April 30, 2012L21-1
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
ECE/CS 552: Microprogramming and Exceptions Instructor: Mikko H Lipasti Fall 2010 University of Wisconsin-Madison Lecture notes based on set created by.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Multi-Cycle Processor.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Microprogramming and Exceptions Spring 2005 Ilam University.
Constructive Computer Architecture Interrupts/Exceptions/Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
Constructive Computer Architecture Virtual Memory and Interrupts Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
CS152 Lec12.1 CS 152 Computer Architecture and Engineering Lecture 12 Multicycle Controller Design Exceptions.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 16 - Multi-Cycle.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Exceptions and Interrupts “Unexpected” events requiring change in flow of control – Different ISAs use the terms differently Exception – Arises within.
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Constructive Computer Architecture Interrupts/Exceptions/Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
Introduction to Operating Systems
Interrupts and exceptions
Digital Logic Design Alex Bronstein
Constructive Computer Architecture Tutorial 6 Cache & Exception
Exceptions Another form of control hazard Could be caused by…
Computer Organization CS224
Handling Exceptions In MIPS, exceptions managed by a System Control Coprocessor (CP0) Save PC of offending (or interrupted) instruction In MIPS: Exception.
Interrupts and signals
Basic Processor Structure/design
William Stallings Computer Organization and Architecture 8th Edition
Overview Introduction General Register Organization Stack Organization
Interrupts/Exceptions/Faults
Chapter 10 The Stack.
Pipelining: Advanced ILP
The processor: Exceptions and Interrupts
Virtual Memory and Interrupts
CSCE 212 Chapter 5 The Processor: Datapath and Control
RISC-V Instruction Set Architecture (ISA)
From Address Translation to Demand Paging
The processor: Pipelining and Branching
ECE/CS 552: Microprogramming and Exceptions
BIC 10503: COMPUTER ARCHITECTURE
Architectural Support for OS
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Introduction to Microprocessor Programming
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
Control Hazards Branches (conditional, unconditional, call-return)
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
ECE/CS 552: Pipelining and Exceptions
Chapter 11 Processor Structure and function
Interrupts and exceptions
CMSC 611: Advanced Computer Architecture
Presentation transcript:

Interrupts/Exceptions Constructive Computer Architecture Interrupts/Exceptions Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology November 1, 2017 http://csg.csail.mit.edu/6.175

Hardware Software Interactions application Operating system Hardware Application binary interface (ABI) Supervisor binary interface (SBI) Modern processors cannot function without some resident programs (“services”), which are shared by all users Usually such programs need some special registers which are not visible to the users Therefore all ISAs have extensions to deal with these special registers Furthermore, these special registers cannot be manipulated by user programs; therefore user/privileged mode is needed to use these instructions November 1, 2017 http://csg.csail.mit.edu/6.175

Interrupt (aka Trap) altering the normal flow of control Ii-1 Ii Ii+1 HI1 HI2 HIn interrupt handler program For precise interrupt/exception, the branching out and back should be shown as ‘in-between instructions’. Even if I_i is a page fault, the exception is between I_i-1 and I_i, not on I_i. An external or internal event that needs to be processed by another (system) program. The event is usually unexpected or rare from program’s point of view. November 1, 2017 http://csg.csail.mit.edu/6.175

Interrupts caused by an external event External asynchronous event input/output device service-request/response timer expiration power disruptions, hardware failure After 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 It disables interrupts and transfers control to a designated interrupt handler running in the privilege mode Privileged/user mode to prevent user programs from causing harm to other users or OS November 1, 2017 http://csg.csail.mit.edu/6.175

Exceptions caused by the execution of an instruction The instruction cannot be completed undefined opcode, privileged instructions arithmetic overflow, FPU exception misaligned memory access virtual memory exceptions: page faults, TLB misses, protection violations System call: Deliberately used by the programmer to invoke a kernel service Exception instruction might need to restart (page fault) or be skipped (emulated mult). The handler adjusts the EPC depending on the situation. I implemented Syscall as an exception itself (so not completed) and is skipped by the handler. Either the faulting condition is fixed; or the instruction is emulated by the exception handler; or the program is aborted The pipeline must undo any partial execution and record the cause of the exception November 1, 2017 http://csg.csail.mit.edu/6.175

Interrupt* handling When an interrupt is caused Hardware saves the information about the interrupt in CSRs: mepc – exception PC mcause – cause of the exception mstatus.mpp – privilege mode of exception ... Processor jumps to the address of the interrupt handler (stored in the mtvec CSR) and increases the privilege level A interrupt handler, a software program, takes over and performs the necessary action *From now on, interrupts and exceptions are used interchangeably November 1, 2017 http://csg.csail.mit.edu/6.175

Software for interrupt handling Hardware transfers control to the common software interrupt handler (CH) which: Saves all GPRs into known memory locations Passes mcause, mepc, stack pointer to the IH (a C function) to handle the specific interrupt On the return from the IH, writes the return value to mepc Loads all GPRs from the memory Execute mret, which does: set pc to mepc pop mstatus (mode, enable) stack GPR IH CH 1 2 3 4 5 IH a new instruction IH November 1, 2017 http://csg.csail.mit.edu/6.175

Saving GPRs Operating system manages the space where GPRs are saved and provides a pointer to the space for the current program For simplicity we’ll assume this location is fixed at GPR_BASE RISC-V’s memory instructions require the base address to be stored in a GPR Therefore before GPRs can be saved, we need to free up a GPR (say x1) by storing its value in a special scratch register (mscratch) After saving the other GPRs, they are freed up Finally, x1 needs to be restored from mscratch and saved another CSR November 1, 2017 http://csg.csail.mit.edu/6.175

Common Interrupt Handler- SW (RISC-V Assembly) common_handler: # entry point for exception handler # save x1 to mscratch to free up a register csrw mscratch, x1 # write x1 to mscratch # get the pointer for storing GPRs li x1, GPR_BASE # save x2 - x31 sw x2, 8(x1) sw x3, 12(x1) ... sw x31, 124(x1) # now registers x2 – x31 are free # save original x1 (now in mscratch) csrr x2, mscratch # x2 used as temporary register sw x2, 4(x1) # x1 still points to GPR storage November 1, 2017 http://csg.csail.mit.edu/6.175

Common handler- SW cont. Setting up and calling IH_Dispacher ... # we have saved all GPRs to stack # call C function to handle interrupt csrr a0, mcause # arg 0: cause csrr a1, mepc # arg 1: epc li a2, GPR_BASE # arg 2: pointer to all saved GPRs ... # set up stack pointer for C-code jal ih_dispatcher # calls ih_dispatcher which may # have been written in C # return value is the PC to resume csrw mepc, a0 # restore mscratch and all GPRs li x1, GPR_BASE; lw x2, 8(x1); lw x3, 12(x1); ...; lw x31, 124(x1) lw x1, 4(x1) # restore x1 last mret # finish handling interrupt November 1, 2017 http://csg.csail.mit.edu/6.175

IH Dispatcher (in C) Dispatches to a specific handler based on “cause” long ih_dispatcher(long cause, long epc, long *regs) { // regs[i] refers to GPR xi stored in stack if(cause == 0x02) // illegal instruction return illegal_ih(cause, epc, regs); else if(cause == 0x08) // system call return syscall_ih(cause, epc, regs); else ... // other causes } Reg 26 and 27 are temporary registers available to the kernel (specified by Application Binary Interface (ABI)) Next we give examples of two interrupt Handlers: Software implementation of multiply instruction System call (e.g., printf, open, ...) November 1, 2017 http://csg.csail.mit.edu/6.175

SW emulation of MULT instruction mul rd, rs1, rs2 With proper exception handlers we can implement unsupported instructions in SW MUL returns the low 32-bit result of rs1*rs2 into rd MUL is decoded as an unsupported instruction and will throw an Illegal Instruction exception SW handles the exception in illegal_inst_ih() function illegal_inst_ih() checks the opcode and function code of MUL to call the emulated multiply function Control is resumed to epc + 4 after emulation is done (MRET) November 1, 2017 http://csg.csail.mit.edu/6.175

Illegal Instruction IH (in C) long illegal_inst_ih(long cause, long epc, long *regs) { uint32_t inst = *((uint32_t*)epc); // fetch inst // check opcode & function codes if((inst & MASK_MUL) == MATCH_MUL) { // is MUL, extract rd, rs1, rs2 from inst int rd = (inst >> 7) & 0x01F; int rs1 = ...; int rs2 = ...; // emulate regs[rd] = regs[rs1] * regs[rs2] emulate_multiply(rd, rs1, rs2, regs); return epc + 4; // done, resume at epc+4 } else abort(); } November 1, 2017 http://csg.csail.mit.edu/6.175

System call as an instruction The ecall instruction raises an exception; its meaning is derived based on conventions of the operating system Register a7 contains the desired function, register a0,a1,a2 contain the arguments, result is returned in register a0 a0 – a7 are register name aliases (i.e. x10 – x17) Processor can’t function without the cooperation of the software Single-cycle implementation: next few slides November 1, 2017 http://csg.csail.mit.edu/6.175

Syscall Interrupt Handler (in C) long syscall_ih(long cause, long epc, long *regs) { // figure out the type of SCALL (stored in a7/x17) // args are in a0/x10, a1/x11, a2/x12 long type = regs[17]; long arg0 = regs[10]; long arg1 = regs[11]; long arg2 = regs[12]; if(type == SysPrintChar) { ... } else if(type == SysPrintInt) { ... } else if(type == SysExit) { ... } else ... // ECALL finshes, we need to resume to epc + 4 return epc + 4; } Reg 26 and 27 are temporary registers available to the kernel (specified by Application Binary Interface (ABI)) November 1, 2017 http://csg.csail.mit.edu/6.175

Decode function DecodedInst decode(Data inst, Status status); DecodedInst dInst = ?; ... opSystem: begin case (funct3) ... fnPRIV: begin // privilege inst dInst.iType = (case(inst[31:20]) fn12ECALL: Syscall; // sys call // MRET can only be executed under privilege mode fn12MRET: isPrivMode(status) ? MRet : NoPermit; ... default: Unsupported; endcase); dInst.dst = Invalid; dInst.src1 = Invalid; dInst.src2 = Invalid; dInst.imm = Invalid; dInst.aluFunc = ?; dInst.brFunc = NT; end ... endcase end return dInst; endfunction In my version, Mult (or any unimplemented instruction) gets decoded to fcMULT, fcMULTU: begin dInst.iType = Unsupported; dInst.dst = Invalid; dInst.src1 = Invalid; dInst.src2 = Invalid; dInst.imm = Invalid; dInst.brFunc = NT; end typedef enum {Alu,..., NoPermit,Syscall,MRet} IType November 1, 2017 http://csg.csail.mit.edu/6.175 16

Add Execute code for setting CSRs ... if (eInst.iType==Syscall) begin csrf.setStatus(statusPush(csrf.getStatus)); csrf.setCause(32’h08); // cause for System call csrf.setEpc(pc); end else if (eInst.iType==MRet) begin csrf.setStatus(statusPop(csrf.getStatus)); end November 1, 2017 http://csg.csail.mit.edu/6.175

Redirecting PC single cycle implementation if (eInst.iType==Syscall) pc <= csrf.getTvec; else if (eInst.iType==MRet) pc <= csrf.getEpc; else pc <= eInst.brTaken ? eInst.addr : pc + 4; Interrupt redirection is done from the last stage; speed is not a paramount concern in hardware handling of interrupts November 1, 2017 http://csg.csail.mit.edu/6.175

Exception Handling in pipelined machines Commit Point PC D E M W Inst. Mem Decode Data Mem + Kill D Stage Kill F Stage Kill E Stage Select Handler PC Kill Writeback Illegal Opcode Overflow Data address Exceptions PC address Exception Ex D Ex E Ex M Cause PC D PC E PC M EPC External Interrupts 1. An instruction may cause multiple exceptions; which one should we process? from the earliest stage 2. When multiple instructions are causing exceptions; which one should we process first? from the oldest instruction November 1, 2017 http://csg.csail.mit.edu/6.175