Beruflich Dokumente
Kultur Dokumente
CPS104 Lec35.1
GK Spring 2004
Admin.
CPS104 Lec35.2
GK Spring 2004
CPS104 Lec35.3
GK Spring 2004
Interrupts, Exceptions and Traps are asynchronous changes in the control flow. Interrupts and Exceptions can be viewed as asynchronous (unscheduled) procedure calls. Interrupts and exceptions are designed to provide:
u
u u
protection mechanisms: Error handling, TLB management. efficiency: Overlap I/O and execution, . . . Illusion of parallelism: Timesharing, multithreading Ability to handle Asynchronous external events: Network connections, keyboard input, DMA I/O, . . .
CPS104 Lec35.4
GK Spring 2004
Exception: a change in execution caused by a condition that occurs within the processor.
u
u u u u
segmentation fault (access outside program boundaries, illegal access, . . .) bus error divide by 0 overflow page fault (virtual memory) devices: disk, network, keyboard, etc. clock for timesharing (multitasking) These are useful events, must do something when they occur.
Trap: a user-requested exception u Operating system call (syscall) u Breakpoints (debugging mode)
CPS104 Lec35.5
GK Spring 2004
An Execution Context
The state of the CPU associated with a thread of control (process) u general purpose registers (integer and floating point) u status registers (e.g., condition codes) u program counter, stack pointer Need to be able to switch between contexts u timesharing: sharing the machine among many processes u better utilization of machine (overlap I/O of one process with computation of another) u different modes (Kernel v.s. user) Maintained by operating system
CPS104 Lec35.6
GK Spring 2004
Context Switches
Save current execution context u Save registers and program counter u information about the context (e.g., ready, blocked) Restore other context Need data structures in kernel to support this u process control block Why do we context switch? u Timesharing: HW clock tick u I/O begin and/or end How do we know these events occur? u Interrupts...
CPS104 Lec35.7
GK Spring 2004
Handling an Exception
User Program ld add st mul beq ld sub bne Interrupt Handler
RETT
Invoke specific kernel routine based on type of interrupt u interrupt/exception handler Must determine what caused interrupt u Cause register holds information on what caused the interrupt u PC <= interrupt_handler Vectored Interrupts u PC = interrupt_table[i] Clear the interrupt Return from interrupt (RETT) to different process (e.g, context switch)
CPS104 Lec35.8
GK Spring 2004
Execution Mode
What if interrupt occurs while in interrupt handler? u Problem: Could lose information for one interrupt
clear of interrupt #1, clears both #1 and #2
Solution: disable interrupts Disabling interrupts is a protected operation u Only the kernel can execute it u user v.s. kernel mode u mode bit in CPU status register Other protected operations u installing interrupt handlers u manipulating CPU state (saving/restoring status registers) u Updating TLB, Changing page table, changing page protection. Changing modes u interrupts u system calls (syscall instruction)
u
CPS104 Lec35.9
GK Spring 2004
The MIPS has a special coprocessor CP0, that is dedicated to Exception handling.
Status Register Index Register Random Register Context Register BdAddr. Register Cause Register EPC Register PRId Register
EntryHi
EntryLo
TLB
CPS104 Lec35.10
GK Spring 2004
MIPS-2000 CP0
EPC (Exception Program Counter): a 32-bit register used to hold the address of the affected instruction u register 14 of coprocessor 0 Cause: a register used to record the cause of the exception. u In the MIPS architecture this register is 32 bits, though some bits are currently unused. BadVAddr: register contains memory address at which memory reference occurred u when memory access exception occurs u register 8 of coprocessor 0 Status: interrupt mask and enable bits u register 12 of coprocessor 0 PRId: Processor Revision Identifier register
CPS104 Lec35.11
GK Spring 2004
Special coprocessor instruction: u mfc0 rt, rd # register rd in CP0 is loaded into register rt u mtc0 rt, rd # register rt is loaded into CP0 register rd u rfe # Return from exception, restore Interrupt mask and status register, (allow change in execution mode). TLB special instructions: u tlbp # The index register is loaded with the address of the TLB entry whose contents match the contents of EntryHi and EntryLo registers. u tlbr # Read Indexed TLB entry: The EntryHi and EntryLo registers are loaded with the content of the TLB pointed at by the TLB Index register u tlbwi # Write Indexed TLB entry: The EntryHi and EntryLo registers are stored in the TLB entry pointed at by the TLB Index register u tlbwr # Write random TLB entry: The EntryHi and EntryLo registers are stored in the TLB entry pointed at by the TLB random register
GK Spring 2004
CPS104 Lec35.12
Mask = 1 bit for each of 5 hardware and 3 software interrupt levels u 1 enables interrupts, 0 disables interrupts k = kernel/user u 0 => was in the kernel when interrupt occurred u 1 => was running user mode e = interrupt enable u 0 means disabled, 1 means enabled Interrupt handler runs in kernel mode with interrupts disabled u When interrupt occurs, 6 LSB shifted left 2 bits, setting 2 LSB to 0
GK Spring 2004
CPS104 Lec35.13
Pending interrupt 5 hardware levels: bit set if interrupt occurs but not yet serviced u handles cases when more than one interrupt occurs at same time, or records interrupt requests when interrupts disabled Exception Code encodes reasons for interrupt u 0 (INT) => external interrupt u 4 (ADDRL) => address error exception (load or instr fetch) u 5 (ADDRS) => address error exception (store) u 6 (IBUS) => bus error on instruction fetch u 7 (DBUS) => bus error on data fetch u 8 (Syscall) => Syscall exception u 9 (BKPT) => Breakpoint exception u 10 (RI) => Reserved Instruction exception u 12 (OVF) => Arithmetic overflow exception
GK Spring 2004
CPS104 Lec35.14
Machine must save the address of the offending instruction in the EPC (Exception Program Counter) Then transfer control to the Operating System (OS) at some specified address OS performs some action in response OS terminates program or returns (eventually) using EPC u could return to different program (context switch) Exceptions are an unscheduled procedure call to a fixed location!
CPS104 Lec35.15
GK Spring 2004
2 types of exceptions in our current implementation u undefined instruction u arithmetic overflow Which Event caused Exception? u Option 1 (used by MIPS): Cause register contains reason u Option 2 Vectored interrupts: cause is part of the address.
u
CPS104 Lec35.16
GK Spring 2004
Some problems could occur in the way the exceptions are handled. Example: u in the case of arithmetic overflow, the instruction causing the overflow completes writing its result, because the overflow branch is in the state when the write completes. u However, the architecture may define the instruction as having no effect if the instruction causes an exception; MIPS specifies this. Virtual memory exceptions must prevent handler from changing the machine state. This aspect of handling exceptions becomes complex and potentially limits performance => why it is hard
CPS104 Lec35.17
GK Spring 2004
Pipelining
CPS104 Lec35.18
GK Spring 2004
Laundry Example Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, and fold Washer takes 30 minutes Dryer takes 40 minutes Folder takes 20 minutes
CPS104 Lec35.19
GK Spring 2004
Sequential Laundry 6 PM 7 8 9
Time
10
11
Midnight
30 40 20 30 40 20 30 40 20 30 40 20
T a s k O r d e r
A B C D
CPS104 Lec35.20
10
11
Midnight
30 40
T a s k O r d e r
40
40
40 20
A B C D
CPS104 Lec35.21
Pipelining Lessons 6 PM
9
Time
30 40
T a s k O r d e r
40
40
40 20
A B C D
Pipelining doesnt help latency of single task, it helps throughput of entire workload Pipeline rate limited by slowest pipeline stage Multiple tasks operating simultaneously Potential speedup = Number pipe stages Unbalanced lengths of pipe stages reduces speedup Time to fill pipeline and time to drain it reduces speedup
CPS104 Lec35.22
GK Spring 2004