28
ECE291 ECE291 Lecture 9 Lecture 9 Interrupts I Interrupts I

ECE291 Lecture 9 Interrupts I. ECE 291 Lecture 9Slide 2 of 28 Lecture Outline Printed lab manual?Printed lab manual? Interrupt I/OInterrupt I/O Interrupt

Embed Size (px)

Citation preview

ECE291ECE291

Lecture 9Lecture 9

Interrupts IInterrupts I

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 22 of 28 of 28

Lecture OutlineLecture Outline

• Printed lab manual?Printed lab manual?• Interrupt I/OInterrupt I/O• Interrupt vectorsInterrupt vectors• Software interruptsSoftware interrupts• Hardware interruptsHardware interrupts• 8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller• Interrupt PriorityInterrupt Priority• Writing your own ISRsWriting your own ISRs

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 33 of 28 of 28

Interrupt Driven I/O Interrupt Driven I/O • Consider an I/O operation, where the CPU constantly tests a port to Consider an I/O operation, where the CPU constantly tests a port to

see if data is available see if data is available CPU polls the port if it has data available or can accept dataCPU polls the port if it has data available or can accept data

• Polled I/O is inherently inefficientPolled I/O is inherently inefficient Program may check port more often than necessary Program may check port more often than necessary wastes CPU wastes CPU

cyclescycles Program may also not check port enough Program may also not check port enough loses data loses data Analogy:Analogy: Checking your watch every 5 minutes until class is over Checking your watch every 5 minutes until class is over

(NEVER in this class though I bet), or continuously opening up the door (NEVER in this class though I bet), or continuously opening up the door to see if your friends are comingto see if your friends are coming

• Solution is to provide interrupt driven I/OSolution is to provide interrupt driven I/O Perform regular work until an event occursPerform regular work until an event occurs Process event when it happens, then resume normal activitiesProcess event when it happens, then resume normal activities Analogy:Analogy: Alarm clock, doorbell, telephone ring Alarm clock, doorbell, telephone ring

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 44 of 28 of 28

InterruptsInterrupts

• Triggers that cause the CPU to perform various tasks on Triggers that cause the CPU to perform various tasks on demanddemand

• Three kinds:Three kinds: SoftwareSoftware interrupts – initiated by the INT instruction interrupts – initiated by the INT instruction HardwareHardware interrupts – originate in peripheral hardware interrupts – originate in peripheral hardware ExceptionsExceptions – occur in response to error states in the processor – occur in response to error states in the processor

• Regardless of source, they are handled the sameRegardless of source, they are handled the same Each interrupt has a unique interrupt number from 0 to 255. Each interrupt has a unique interrupt number from 0 to 255.

These are called These are called interrupt vectorsinterrupt vectors.. For each interrupt vector, there is an entry in the interrupt vector For each interrupt vector, there is an entry in the interrupt vector

table.table. The interrupt vector table is simply a jump table containing The interrupt vector table is simply a jump table containing

segment:offset addresses of procedures to handle each interruptsegment:offset addresses of procedures to handle each interrupt These procedures are called These procedures are called interrupt handlersinterrupt handlers or or interrupt interrupt

service routinesservice routines (ISR’s) (ISR’s)

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 55 of 28 of 28

Interrupt VectorsInterrupt Vectors

• The first 1024 bytes of memory (addresses The first 1024 bytes of memory (addresses 00000 – 003FF) always contain the interrupt 00000 – 003FF) always contain the interrupt vector table. Always.vector table. Always.

• Each of the 256 vectors requires four bytes—two Each of the 256 vectors requires four bytes—two for segment, two for offsetfor segment, two for offset

Memory address (hex) Interrupt function pointer

003FC4 * x000080000400000

INT 255INT xINT 2INT 1INT 0

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 66 of 28 of 28

Software InterruptsSoftware Interrupts

• The operating system has handlers for The operating system has handlers for many interrupts. These routines provide many interrupts. These routines provide various built-in functions:various built-in functions: Displaying text to the screen Displaying text to the screen File I/OFile I/O Rebooting the systemRebooting the system Changing video modesChanging video modes

• Accessed with the INT instructionAccessed with the INT instruction

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 77 of 28 of 28

Software InterruptsSoftware Interrupts

• Essentially just function calls using a Essentially just function calls using a different instruction to do the callingdifferent instruction to do the calling

• Software interrupts give you access to Software interrupts give you access to “built-in” code from BIOS, operating “built-in” code from BIOS, operating system, or peripheral devicessystem, or peripheral devices

• Use the INT instruction to “call” these Use the INT instruction to “call” these functionsfunctions

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 88 of 28 of 28

System BIOS FunctionsSystem BIOS Functions

• All PCs come with a BIOS ROM (or All PCs come with a BIOS ROM (or EPROM). EPROM).

• The BIOS contains procedures that The BIOS contains procedures that provide basic functions such as provide basic functions such as bootstrapping and primitive I/O. bootstrapping and primitive I/O. INT 19h: Reboot systemINT 19h: Reboot system INT 11h: Get equipment configurationINT 11h: Get equipment configuration INT 16h: Keyboard I/OINT 16h: Keyboard I/O

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 99 of 28 of 28

Video BIOS FunctionsVideo BIOS Functions

• Video cards come with procedures stored in a ROMVideo cards come with procedures stored in a ROM• Collectively known as the video BIOSCollectively known as the video BIOS• Located at C0000-C7FFF and holds routines for handling Located at C0000-C7FFF and holds routines for handling

basic video adapter functionsbasic video adapter functions• To execute a function in video BIOS ROM, do an INT To execute a function in video BIOS ROM, do an INT

10h with video sub-function number stored in AX10h with video sub-function number stored in AX• INT 10h, Sub-function examplesINT 10h, Sub-function examples

AH=0, AL=2h: 80 column x 25 row text display modeAH=0, AL=2h: 80 column x 25 row text display mode AH=0, AL=13h: 320x200 pixel, 256-color graphics display modeAH=0, AL=13h: 320x200 pixel, 256-color graphics display mode

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1010 of 28 of 28

DOS Function DispatcherDOS Function Dispatcher• INT 21h is the DOS function dispatcher. It gives you access to INT 21h is the DOS function dispatcher. It gives you access to

dozens of functions built into the operating system.dozens of functions built into the operating system.• To execute one of the many DOS functions, you can specify a sub-To execute one of the many DOS functions, you can specify a sub-

function by loading a value into AH just before calling INT 21function by loading a value into AH just before calling INT 21• INT 21h sub-functionsINT 21h sub-functions

AH=3Dh: Open File AH=3Dh: Open File AH=3Fh: Read File AH=3Fh: Read File AH=3Eh: Close File AH=3Eh: Close File AH=13h: Delete FileAH=13h: Delete File AH=2Ah: Get system date AH=2Ah: Get system date AH=2Ch: Get system time AH=2Ch: Get system time AH=2Ch: Read DOS Version AH=2Ch: Read DOS Version AH=47h: Get Current Directory AH=47h: Get Current Directory AH=48h: Allocate Memory block (specified in paragraphs==16 bytes) AH=48h: Allocate Memory block (specified in paragraphs==16 bytes) AH=49h: Free Memory block AH=49h: Free Memory block AH=4Ch: Terminate program (and free resources)AH=4Ch: Terminate program (and free resources)

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1111 of 28 of 28

The INT and IRET InstructionsThe INT and IRET Instructions

• Syntax: INT imm8Syntax: INT imm8• Imm8 is an interrupt vector from 0 to 255Imm8 is an interrupt vector from 0 to 255• INT does the following:INT does the following:

Pushes flag register (pushf)Pushes flag register (pushf) Pushes return CS and IPPushes return CS and IP Far jumps to [0000:(4*imm8)]Far jumps to [0000:(4*imm8)] Clears the interrupt flag disabling the interrupt systemClears the interrupt flag disabling the interrupt system

• IRET is to INT what RET is to CALLIRET is to INT what RET is to CALL Pops flag registerPops flag register Performs a far returnPerforms a far return

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1212 of 28 of 28

Things to NoticeThings to Notice

• The interrupt vector table is just a big The interrupt vector table is just a big permanently located jump tablepermanently located jump table

• The values of the jump table are pointers The values of the jump table are pointers to code provided by the BIOS, hardware, to code provided by the BIOS, hardware, the OS, or eventually youthe OS, or eventually you

• Interrupt service routines preserve the Interrupt service routines preserve the flags as well as the registers they modify flags as well as the registers they modify because the entire state of the computer because the entire state of the computer must be completely unaltered by an ISRmust be completely unaltered by an ISR

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1313 of 28 of 28

Hardware InterruptsHardware Interrupts

• Alert the processor of some hardware situation Alert the processor of some hardware situation that needs tending to:that needs tending to: A key has been pressedA key has been pressed A timer has expiredA timer has expired A network packet has arrivedA network packet has arrived

• Same software calling protocolSame software calling protocol• Additional level of complexity with the interrupt Additional level of complexity with the interrupt

“call” not coming from your program code“call” not coming from your program code• Can happen at any time during the execution of Can happen at any time during the execution of

your programyour program

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1414 of 28 of 28

The 80x86 Interrupt InterfaceThe 80x86 Interrupt Interface

• Device generates request signalDevice generates request signal• Device supplies interrupt vector number on data busDevice supplies interrupt vector number on data bus• Processor completes the execution of current instruction and Processor completes the execution of current instruction and

executes ISR corresponding to the interrupt vector number on the executes ISR corresponding to the interrupt vector number on the data busdata bus

• ISR upon completion acknowledges the interrupt by asserting the ISR upon completion acknowledges the interrupt by asserting the INTA signal INTA signal

80x86 processor

Some device

INT Request (INTR)

INT Acknowledge (INTA)

Data bus

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1515 of 28 of 28

The 80x86 Interrupt InterfaceThe 80x86 Interrupt Interface

• The previous setup could get complicated The previous setup could get complicated with multiple devices generating multiple with multiple devices generating multiple interrupts connected to the same few pins interrupts connected to the same few pins on the processoron the processor

• Solution: the 8259 Programmable Interrupt Solution: the 8259 Programmable Interrupt ControllerController

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1616 of 28 of 28

The 8259 PICThe 8259 PIC

8259PIC

01234567

INTRINTAINT#

80x86

Interrupt outputs from peripheral

devices

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1717 of 28 of 28

The 8259 PICThe 8259 PIC

• The PIC is programmed with a base The PIC is programmed with a base interrupt vector numberinterrupt vector number ““0” corresponds to this base0” corresponds to this base ““1” corresponds to this base + 11” corresponds to this base + 1 ““n”n” corresponds to this base + corresponds to this base + nn

• For example, if the PIC is programmed For example, if the PIC is programmed with a base interrupt vector of 8, then “0” with a base interrupt vector of 8, then “0” corresponds to interrupt vector 8corresponds to interrupt vector 8

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1818 of 28 of 28

Typical System ConfigurationTypical System Configuration

Master8259

INTR

01234567

Slave8259

INTR

01234567

IRQ0IRQ1

IRQ3IRQ4IRQ5IRQ6IRQ7

IRQ8IRQ9IRQ10IRQ11IRQ12IRQ13IRQ14IRQ1580x86

Base vector is 68h

Base vector is 08h

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 1919 of 28 of 28

Typical IRQ AssignmentsTypical IRQ Assignments

• IRQ 0: Timer (triggered 18.2/second) • IRQ 1: Keyboard (keypress) • IRQ 2: Slave PIC • IRQ 3: Serial Ports (Modem, network) • IRQ 5: Sound card • IRQ 6: Floppy (read/write completed) • IRQ 8: Real-time Clock • IRQ 12: Mouse• IRQ 13: Math Co-Processor • IRQ 14: IDE Hard-Drive Controller

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2020 of 28 of 28

Interrupt PriorityInterrupt Priority

• Lower interrupt vectors have higher priorityLower interrupt vectors have higher priority• Lower priority interrupts normally cannot Lower priority interrupts normally cannot

interrupt higher priority interruptsinterrupt higher priority interrupts ISR for INT 21h is runningISR for INT 21h is running

• Computer gets request from device attached to IRQ8 (INT Computer gets request from device attached to IRQ8 (INT 78h)78h)

• INT 21h procedure must finish before IRQ8 device can be INT 21h procedure must finish before IRQ8 device can be servicedserviced

ISR for INT 21h is runningISR for INT 21h is running• Computer gets request from Timer 0 IRQ0 (INT 8h)Computer gets request from Timer 0 IRQ0 (INT 8h)• Code for INT 21h gets interrupted, ISR for timer runs Code for INT 21h gets interrupted, ISR for timer runs

immediately, INT21h finishes afterwardsimmediately, INT21h finishes afterwards

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2121 of 28 of 28

Preemptive/Non-PreemptivePreemptive/Non-Preemptive

• A A preemptivepreemptive interrupt is one that interrupt is one that cancan be be interrupted by another interrupt (i.e. one of interrupted by another interrupt (i.e. one of higher priority)higher priority)

• A A non-preemptivenon-preemptive interrupt is one that interrupt is one that cannotcannot be be interrupted by another interruptinterrupted by another interrupt

• How can an interrupt “become” non-preemptive?How can an interrupt “become” non-preemptive? CLI – CLear Interrupt flag – disables interrupts CLI – CLear Interrupt flag – disables interrupts STI – SeT Interrupt flag – enables interruptsSTI – SeT Interrupt flag – enables interrupts

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2222 of 28 of 28

Preemptive/Non-PreemptivePreemptive/Non-Preemptive

• Making interrupts behave as Making interrupts behave as non-preemptive:non-preemptive: Software interruptsSoftware interruptsCLICLIINT xxINT xxSTISTI

Hardware InterruptsHardware InterruptsMy_ISRMy_ISR CLI CLI <ISR code> <ISR code> STI STI IRET IRET

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2323 of 28 of 28

Interrupt Priority ExampleInterrupt Priority Example

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2424 of 28 of 28

Interrupt Service RoutinesInterrupt Service Routines

• Reasons for writing your own ISR’sReasons for writing your own ISR’s to supersede the default ISR for internal hardware interrupts to supersede the default ISR for internal hardware interrupts

(e.g., division by zero)(e.g., division by zero) to chain your own ISR onto the default system ISR for a to chain your own ISR onto the default system ISR for a

hardware device, so that both the system’s actions and your own hardware device, so that both the system’s actions and your own will occur on an interrupt (e.g., clock-tick interrupt)will occur on an interrupt (e.g., clock-tick interrupt)

to service interrupts not supported by the default device drivers to service interrupts not supported by the default device drivers (a new hardware device for which you may be writing a driver)(a new hardware device for which you may be writing a driver)

to provide communication between a program that terminates to provide communication between a program that terminates and stays resident (TSR) and other application software and stays resident (TSR) and other application software

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2525 of 28 of 28

Interrupt Service RoutinesInterrupt Service Routines

• ISRs are meant to be shortISRs are meant to be short keep the time that interrupts are disable and the total length of keep the time that interrupts are disable and the total length of

the service routine to an the service routine to an absolute minimum absolute minimum – you do not want to – you do not want to block interrupts of lower priority!block interrupts of lower priority!

• ISRs can be interruptedISRs can be interrupted• ISRs must be in memory ISRs must be in memory

Option 1: Redefine interrupt only while your program is running Option 1: Redefine interrupt only while your program is running • the default ISR will be restored when the executing program the default ISR will be restored when the executing program

terminatesterminates Option 2: Use DOS Terminate-and-Stay-Resident (TSR) Option 2: Use DOS Terminate-and-Stay-Resident (TSR)

command to load and leave program code permanently in command to load and leave program code permanently in memorymemory

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2626 of 28 of 28

Servicing an InterruptServicing an Interrupt• Complete current instructionComplete current instruction • Preserve current contextPreserve current context

PUSHF Store flags to stack PUSHF Store flags to stack Clear Trap Flag (TF) & Interrupt Clear Trap Flag (TF) & Interrupt

Flag (IF)Flag (IF) Store return address to stack Store return address to stack

PUSH CS, PUSH IP PUSH CS, PUSH IP

• Identify SourceIdentify Source Read 8259 PIC status register Read 8259 PIC status register Determine which device (N) Determine which device (N)

triggeredtriggeredinterrupt interrupt

• Activate Interrupt Service RoutineActivate Interrupt Service Routine Use N to index vector table Use N to index vector table Read CS/IP from table Read CS/IP from table Jump to instruction Jump to instruction

• Execute Interrupt Service RoutineExecute Interrupt Service Routine usually the handler immediately re-usually the handler immediately re-

enables the interrupt system (to enables the interrupt system (to allow higher priority interrupts to allow higher priority interrupts to occur) (STI instruction)occur) (STI instruction)

process the interruptprocess the interrupt

• Indicate End-Of-Interrupt (EOI) to Indicate End-Of-Interrupt (EOI) to 8259 PIC8259 PIC

mov al, 20h mov al, 20h out 20h, al out 20h, al ;transfers the contents of AL to I/O port 20h;transfers the contents of AL to I/O port 20h

• Return (IRET)Return (IRET) POP IP (Far Return) POP IP (Far Return) POP CS POP CS POPF (Restore Flags)POPF (Restore Flags)

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2727 of 28 of 28

Timer Interrupt ExampleTimer Interrupt Example

• The ISR will count the number The ISR will count the number of timer interrupts receivedof timer interrupts received

• The main program will use this The main program will use this count to display elapsed time in count to display elapsed time in minutes and secondsminutes and seconds

• http://courses.ece.uiuc.edu/ece291/lecture/timer.exehttp://courses.ece.uiuc.edu/ece291/lecture/timer.exe

ECE 291 Lecture 9ECE 291 Lecture 9

Slide Slide 2828 of 28 of 28

Timer interrupt – ISR codeTimer interrupt – ISR code;====== ISR Code =========myint

push ds ;Save all registerspush axmov ax, cs ;Load default segmentmov ds, axpushf ;Call Orig Function w/flagscall far [oldv] ;Far Call to existing routine

inc word [count];Increment Interrupt

countcmp word [count],18jne .myintdone

inc word [scount] ;Next second mov word [count], 0cmp word [scount], 60jne .myintdoneinc word [mcount] ; Next minutemov word [scount], 0

.myintdonemov al, 20h ;Reset the PIC out 20h, al ;End-of-Interrupt signalpop ax ;Restore all Registerspop dsiret ;Return from Interrupt