0% found this document useful (0 votes)
36 views

Interrupts, Part 1 Dr. Dimitrios S. Nikolopoulos Csl/Uiuc

The document discusses interrupts in three sentences: 1) Interrupts allow hardware devices and software to signal the CPU to pause normal instruction execution and handle an asynchronous event using interrupt handlers. 2) They are implemented through interrupt vectors, which provide jump addresses to interrupt service routines, and the 8259 Programmable Interrupt Controller arbitrates interrupts from multiple devices. 3) Interrupts are important for concurrency, preemptability, scheduling, measuring time, and allowing peripherals to communicate with the CPU.

Uploaded by

Dinesh Parmar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Interrupts, Part 1 Dr. Dimitrios S. Nikolopoulos Csl/Uiuc

The document discusses interrupts in three sentences: 1) Interrupts allow hardware devices and software to signal the CPU to pause normal instruction execution and handle an asynchronous event using interrupt handlers. 2) They are implemented through interrupt vectors, which provide jump addresses to interrupt service routines, and the 8259 Programmable Interrupt Controller arbitrates interrupts from multiple devices. 3) Interrupts are important for concurrency, preemptability, scheduling, measuring time, and allowing peripherals to communicate with the CPU.

Uploaded by

Dinesh Parmar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lecture 9

Interrupts, Part 1
Dr. Dimitrios S. Nikolopoulos
CSL/UIUC
Outline

• The importance of interrupts


• Interrupt vectors
• Software interrupts
• Hardware interrupts
• 8259 Programmable Interrupt Controller

ECE291 Lecture 9 2
Why are interrupts important

• Interrupts let you use the operating system (run your


programs, manage your files, access your
peripherals etc.)
• Interrupts help peripherals “talk” to your
microprocessor
• Interrupts help you measure time and control the
timing of certain tasks in your microprocessors

ECE291 Lecture 9 3
Interrupts from a pedagogical perspective

• By learning interrupts you learn important concepts


such as:
– Concurrency: how your processor manages to service
interrupts while your program doesn’t know anything about
them and how multiple interrupts are serviced at the same
time
– Preemptability and priorities, how can a low priority-task be
preempted by a high-priority task
– Scheduling: how can we assure that both low and high-
priority tasks get the service they deserve from the
processor

ECE291 Lecture 9 4
Interrupts and our everyday lives

• We will spend at least two lectures to explain how to measure


and track time in your microprocessor and you will be
wondering why don’t we just look at our watches…
• But we will also learn that looking at your watch all the time is
not a good thing to do, especially if you’re a microprocessor…
• We all have priorities
– E.g. you do your ECE291 homework and your girlfriend/boyfriend
calls, there’s a high-priority interrupt
– While you talk to your girlfriend/boyfriend you get another incoming
call from your mom, there’s an interrupt that you decide how to
handle
• High priority: put the girlfriend/boyfriend on hold
• Low priority: put your mom on hold or don’t even bother to switch to the
other line

ECE291 Lecture 9 5
Interrupts…seriously defined

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


• Three types:
– Software interrupts – initiated by the INT instruction in your program
– Hardware interrupts – initiated by peripheral hardware
– Exceptions – occur in response to error states in the processor or during
debugging (trace, breakpoints etc.)
• Regardless of source, they are handled the same
– Each interrupt has a unique interrupt number from 0 to 255. These are
called interrupt vectors.
– For each interrupt vector, there is an entry in the interrupt vector table.
– The interrupt vector table is simply a jump table containing segment:offset
addresses of procedures to handle each interrupt
– These procedures are called interrupt handlers or interrupt service routines
(ISR’s)

ECE291 Lecture 9 6
Interrupt vectors

• The first 1024 bytes of memory (addresses 00000 –


003FF) always contain the interrupt vector table.
Always. Never anything else.
• Each of the 256 vectors requires four bytes—two for
segment, two for offset
Memory address (hex)

003FC INT 255


4*x INT x
00008 INT 2
00004 INT 1
00000 INT 0
ECE291 Lecture 9 7
Software interrupts

• Essentially function calls using a different instruction


to do the calling and different conventions
• Software interrupts give you access to “built-in” code
in the BIOS, the operating system, or peripheral
devices
• Software interrupts are triggered with the INT
instruction

ECE291 Lecture 9 8
The INT and IRET instructions

• Syntax: INT imm8


• imm8 is an interrupt vector from 0 to 255
• INT does the following:
– Pushes flag register (pushf)
– Pushes return CS and IP
– Far jumps to [0000:(4*imm8)]
– Usually clears the interrupt flag disabling the interrupt
system
• IRET is to INT what RET is to CALL
– Pops flag register
– Performs a far return

ECE291 Lecture 9 9
Things to notice

• The interrupt vector table is just a big permanently


located jump table
• The values of the jump table are pointers to code
provided by bios, hardware, the operating system or
YOU!
• Interrupt service routines preserve the flags – the
state of the microprocessor before the INT should be
completely unaltered by the ISR and your program
must return to normal operation.

ECE291 Lecture 9 10
Hardware interrupts

• Alert the processor of some hardware situation that


needs the processor’s attention
– A key has been pressed
– A timer has expired
– A network packet has arrived
• Same software calling protocol
• Additional level of complexity with the interrupt “call”
not coming from your program code
• Can happen at any time during the execution of your
program, invocations of ISRs for hardware interrupts
are asynchronous
ECE291 Lecture 9 11
The 80x86 interrupt interface
INT Request (INTR)

80x86 INT Acknowledge (INTA)


Some device
processor
Data bus

• Device generates request signal


• Device supplies interrupt vector number on data bus
• Processor completes the execution of current instruction and
executes ISR corresponding to the interrupt vector number on
the data bus
• ISR upon completion acknowledges the interrupt by asserting
the INTA signal
ECE291 Lecture 9 12
It is not that simple…

• What if we want to connect more than one devices to


the processor ?
• What happens if multiple devices generate multiple
interrupts at the same time ?
• We need a way to share the two interrupt lines
among multiple devices
• 8259 Programmable Interrupt Controller
• The 8259 PIC operates as an arbiter for interrupts
triggered by multiple devices
• One 8259 serves up to 8 devices, but multiple 8259
chips can be “cascaded” to serve up to 64 devices
ECE291 Lecture 9 13
The 8259 PIC
0
1
2
Interrupt outputs
3
from peripheral
4
devices
8259 5
PIC 6
7

INTR
INTA 80x86
INT#

ECE291 Lecture 9 14
Master-Slave Configuration
0 IRQ8
Base vector is 08h 1 IRQ9
Slave 2 IRQ10
0 IRQ0 8259 3 IRQ11
1 IRQ1 4 IRQ12
Master 2 INTR 5 IRQ13
8259 3 IRQ3 6 IRQ14
80x86 4 IRQ4 7 IRQ15

INTR 5 IRQ5
6 IRQ6 Base vector is 78h
7 IRQ7

ECE291 Lecture 9 15
The 8259 PIC

• PIC is very complex to program, fortunately the BIOS


does most of the work needed
• Programmed with the I/O address 20h-21h (master)
and 0A0h-0A1h (slave)
• I/O instructions yet to be discussed…
– in reads from an I/O address
– out writes to an I/O address
• Consider them as two registers the status register
and the interrupt mask register

ECE291 Lecture 9 16
The 8259 PIC

• The mask register is addressed from 21h


• It lets you enable/disable specific hardware interrupts
• Counterintuitive: a 0 ENABLES an interrupt and a 1
DISABLES the interrupt
• Never load a value immediately to the mask register
• Always read the previous value and use and/or
instructions to set the new mask
in al, 21h ; this one reads the value of the mask register
and al, 0efh ; this zeroes out bit 4 i.e. IRQ4
out 21h, al ; this actually disables the interrupt in IRQ4
ECE291 Lecture 9 17
The 8259 PIC

• When an interrupt occurs and the processor starts executing the


ISR all further interrupts from the same device are blocked until
the ISR issues an end of interrupt instruction
mov al, 20h
out 20h, al
• You must end exactly one interrupt!
– Not sending one will block all interrupts from the save device
– Sending two or more means that you might accidentally acknowledge
the end of a pending interrupt!
• Two more registers track pending interrupts received at the PIC
and interrupt priorities
• You must be careful when you’re patching existing ISR’s (because
the end instruction sequence may already be included in the ISR)

ECE291 Lecture 9 18
The 8259 PIC

• IRQ mapping
– Interrupt vectors 8 through 0Fh map to IRQ0-IRQ7
– Interrupt vectores 70h-77h map to IRQ8-IRQ15

ECE291 Lecture 9 19
Interrupt vectors and the 8259 PIC
0 IRQ8
Base vector is 08h 1 IRQ9
Slave 2 IRQ10
0 IRQ0 8259 3 IRQ11
1 IRQ1 4 IRQ12
Master 2 INTR 5 IRQ13
8259 3 IRQ3 6 IRQ14
80x86 4 IRQ4 7 IRQ15

INTR 5 IRQ5
6 IRQ6 Base vector is 78h
7 IRQ7

ECE291 Lecture 9 20
Typical 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
ECE291 Lecture 9 21
Interrupt priority

• Lower interrupt vectors have higher priority


• Lower priority can’t interrupt higher priority
• Higher priority can interrupt lower priority
– ISR for INT 21h is running
• Computer gets request from device attached to IRQ8 (INT 78h)
• INT 21h procedure must finish before IRQ8 device can be
serviced
– ISR for INT 21h is running
• Computer gets request from Timer 0 IRQ0 (INT 8h)
• Code for INT 21h gets interrupted, ISR for timer runs
immediately, INT21h finishes afterwards

ECE291 Lecture 9 22
Priority in the 8259

• 8259 supports several priority schemes


• On PC’s the 8259 uses the simplest form of fixed priorities
• Each IRQ has a fixed priority
• Lower IRQs has higher priority
• The timer interrupt (IRQ0) has lower priority than any other IRQ
• If you really need higher priority than the timer (e.g. connecting
a nuclear reactor to your microprocessor) it is possible to use a
NMI (non-maskable interrupt)
• NMI has the highest priority among all hardware interrupts and
cannot be disabled by the program

ECE291 Lecture 9 23
Interrupt enabling/disabling

• You can enable/disable all maskable hardware


interrupts
• The CLI instruction disables all maskable hardware
interrupts
• The STI instruction enables all maskable hardware
interrupts
• Be very careful if you ever need to use them
– Many deadlock scenarios!

ECE291 Lecture 9 24
The ugly details

• ISRs for hardware interrupts clear the interrupt flag at the beginning to
disable interrupts. They may include a STI instruction if they want to
enable interrupts before they finish
– It’s all about performance! Keeping interrupts blocked for long is a BAD
IDEA
• ISRs for software interrupts do not disallow hardware interrupts
automatically at the beginning. If an ISR for a software interrupt needs
to do that it must issue a CLI instruction
– This is what most ISRs do
– Again for the sake of performance a STI instruction must be issued as soon
as possible
– Note that when interrupts are enabled the priority rule applies
• The CLI works only for maskable hardware interrupts
• Code enclosed between CLI/SCI is often called a critical section, an
uninterruptible piece of code

ECE291 Lecture 9 25
Is there a way out of this mess ?

• In many critical section situations (e.g. patching the interrupt


vector tables) DOS helps us ensure the required atomicity
• Convenient calls for
– Safely getting the value of the interrupt vector from the interrupt
vector table
– Safely storing a new value to the interrupt vector table (patching the
interrupt vector table)
• In all difficult situations always examine what if scenarios
– What if a hardware interrupt occurs at different points of our ISR ?
– Identify the points that need to be protected and protect them with
CLI/STI

ECE291 Lecture 9 26
Servicing a hardware interrupt
• Complete current instruction • Execute ISR
– usually the handler immediately
• Preserve current context re-enables the interrupt system
– PUSHF Store flags to stack (to allow higher priority
– Clear Trap Flag (TF) & Interrupt interrupts to occur) (STI
Flag (IF) instruction)
– Store return address to stack – process the interrupt
PUSH CS, PUSH IP • Indicate End-Of-Interrupt (EOI)
• Identify Source to 8259 PIC
– Read 8259 PIC status register mov al, 20h
– Determine which device (N) out 20h, al
triggered the interrupt
• Return (IRET)
• Activate ISR – POP IP (Far Return)
– Use N to index vector table – POP CS
– Read CS/IP from table – POPF (Restore Flags)
– Jump to instruction

ECE291 Lecture 9 27
Interrupt service routines

• Reasons for writing your own ISR’s


– to override the default ISR for internal hardware interrupts
(e.g., division by zero need not terminate the program)
– to chain your own ISR onto the default system ISR for a
hardware device, so that both the system’s actions and your
own will occur on an interrupt (e.g., clock-tick interrupt,
measure elapsed time)
– to service interrupts not supported by the default device
drivers (a new hardware device for which you may be writing
a driver)
– to provide communication between a program that
terminates and stays resident (TSR) and other application
software (maintain your ISRs)

ECE291 Lecture 9 28
Impact of interrupts on performance

• The frequency of occurrence and the latency of the ISR


determine the impact of servicing interrupts to your program
• The latency of the ISR is non-negligible!
• You may not notice it but you may be interrupted several times
while executing your program. The good thing is that you don’t
notice it!
• Always remember:
– When the processor starts executing an ISR there might be other
ISRs executing already
– Your ISR may be interrupted by a higher-priority interrupt
– Many devices expect low latency from your ISR (imagine what
happens if you hit a key in the keyboard and wait for a minute!)
– Even those devices with high latencies (e.g. the disk) are not
allowed to block other activity in the processor for long
ECE291 Lecture 9 29
Bottom line

YOUR INTERRUPT SERVICE ROUTINES


MUST BE SHORT AND ACHIEVE THEIR
PURPOSE WITH THE MAXIMUM
EFFICIENCY! NEVER BLOCK THE SYSTEM
WITH YOUR ISRs
Interrupt Service Routines

• ISRs are meant to be short


– keep the time that interrupts are disabled and the total length of the
service routine to an absolute minimum
– remember after interrupts are re-enabled (STI instruction),
interrupts of the same or lower priority remain blocked if the
interrupt was received through the 8259A PIC
• ISRs can be interrupted
• ISRs must be in memory
– Option 1: Redefine interrupt only while your program is running
• the default ISR will be restored when the executing program terminates
– Option 2: Use DOS Terminate-and-Stay-Resident (TSR) command
to load and leave program code permanently in memory

ECE291 Lecture 9 31
Interrupt Driven I/O

• Consider an I/O operation, where the CPU constantly tests a port (e.g.,
keyboard) to see if data is available
– CPU polls the port if it has data available or can accept data
• Polled I/O is inherently inefficient
• Wastes CPU cycles until event occurs
• Analogy: Checking your watch every 30 seconds until your popcorn is
done, or standing at the door until someone comes by

• Solution is to provide interrupt driven I/O


• Perform regular work until an event occurs
• Process event when it happens, then resume normal activities
• Analogy: Alarm clock, doorbell, telephone ring

ECE291 Lecture 9 32

You might also like