Interrupts - Signals Raised by Hardware External To The MP That Needs A Response (Help) From The MP
Interrupts - Signals Raised by Hardware External To The MP That Needs A Response (Help) From The MP
• CPU
Checks if interrupt can be taken
Executes handler
Serial
Port
MP
Network
Interface Interrupt reqest pins
This signal tells the MP that network chip needs service
Interrupts
• Signals from the hardware
When I/O devices need attention from the MP they let the MP
know by signaling it
• Interrupt handler
Subprogram
Instructions that handle specific hardware signals
Does some cleanup also
Turn on interrupts if they were turned off
Reset interrupt-detection hardware
• Nonmaskable interrupt
An interrupt that cannot be turned off ever
Used for exceptional circumstances
Beyond the normal range of ordinary processing
Catastrophic event
Power failure
• 13 interrupts:
10 external
1 timer
2 software
• 16 interrupt levels
• CPU uses IDT Limit and IDT base registers for interrupt
vector lookup.
Figure taken from Intel 80386 Programmer’s Reference
Interrupt Nesting
• Software
k0 and k1 registers are interrupts only
Interrupt stack: exclusive for nested interrupts
Question: What is the maximum depth of interrupt stack?
Example: Exception Nesting in 80386
void main() {
while(TRUE) {
if (iTemperatures[0] != iTemperatures[1]) { Interrupt can occur
//set off alarm! between any
} two instructions!
}
}
Nuclear Reactor Monitoring System
static int iTemperatures[2];
void interrupt vReadTemperatures () {
iTemperatures[0] = //read value from hardware
iTemperatures[1] = //read value from hardware
}
void main() {
while(TRUE) {
if (iTemperatures[0] != iTemperatures[1]) { Interrupt can occur
//set off alarm! between any
} two instructions!
}
}
PUSH AR3
LDIU SP, AR3
L11 LDIU @2E2h, R0 Problem!
CMPI @2E3h, R0
BZ L11
// code in if part of the branch (executed when not taken)
BU L11
Shared Data Problem
• Difficult because
They don’t happen every time the code runs
In the previous example, the interrupt could happen at other points
in main’s execution and doesn’t cause a problem
Events (interrupts) happen in different orders at different
times
Non-deterministic (not necessarily repeatable)
• Possible solution
Disable interrupts whenever the task code uses shared data
Disabling IRQs to Solve the Shared Data Pbm
• Shared-data problem
Task code and interrupt routine share data
Task code uses the data in a way that is not atomic
Solution: Disable interrupts for task code that uses data
Atomic code: Code that cannot be interrupted
• Critical section: set of instructions that must be atomic
for correct execution
• Atomic code
Code that cannot be interrupted by anything that might
modify the data being used
Allows specific interrupts to be disabled as needed
And other left enabled
More Examples
static int iSeconds, iMinutes, iHours;
void interrupt vUpdateTime() { • Hardware timer
if (++iSeconds >= 60) { asserts an IRQ
iSeconds = 0; each second
if (++iMinutes >= 60) {
iMinutes = 0;
Invoking
if (++iHours >= 24) { vUpdateTime
iHours = 0; } Where is the
}
}
problem?
// update the HW as needed
}
long lSecondsSinceMidnight() {
return(
(((iHours*60)+iMinutes)*60)+iSeconds);
}
More Examples
static int iSeconds, iMinutes, iHours;
void interrupt vUpdateTime() { • Hardware timer
if (++iSeconds >= 60) {
iSeconds = 0;
asserts an IRQ
if (++iMinutes >= 60) { each second
iMinutes = 0; Invoking
if (++iHours >= 24) {
iHours = 0; }
vUpdateTime
} Is this a
} solution?
// update the HW as needed
}
long lSecondsSinceMidnight() {
disable();
return(
(((iHours*60)+iMinutes)*60)+iSeconds;
enable();
}
More Examples
static int iSeconds, iMinutes, iHours;
void interrupt vUpdateTime() { • Hardware timer
if (++iSeconds >= 60) { asserts an IRQ
iSeconds = 0; each second
if (++iMinutes >= 60) {
iMinutes = 0;
Invoking
if (++iHours >= 24) { vUpdateTime
iHours = 0; } Is this a
}
}
solution?
// update the HW as needed
}
long lSecondsSinceMidnight() {
disable();
long retn =
(((iHours*60)+iMinutes)*60)+iSeconds;
enable();
return retn;
}
More Examples
• Hardware timer asserts an
static int iSeconds, iMinutes, iHours; IRQ each second
void interrupt vUpdateTime() {
if (++iSeconds >= 60) {
Invoking vUpdateTime
iSeconds = 0; Is this a solution?
if (++iMinutes >= 60) { What happens if
iMinutes = 0; lSecondsSinceMidnight() is
if (++iHours >= 24) { called from within a critical
iHours = 0; } section?:
} disable();
} ...
// update the HW as needed lSecondsSinceMidnight();
} ...
enable();
long lSecondsSinceMidnight() {
disable(); Check before disabling
long retn = Disadvantages?
(((iHours*60)+iMinutes)*60)+iSeconds;
enable();
return retn;
}
Interrupt Latency
• Disabling interrupts
Doing it often, decreases your reponse time
• Some systems (real-time) makes guarantees about
response time
As you design the system you must ensure that such
guarantees (real-time or not) are met
• For some codes, you can avoid disabling interrupts
Via careful coding
To decrease interrupt latency
Makes code fragile
Difficult to ensure that you’ve got it right
Volatile Variables
• They can be
Precise - the system knows the exact cause and how to
respond
Imprecise - catastrophic event; abort
Asynchronous and synchronous (to MP clock)…
Interrupts, Traps, and Exceptions
• Interrupt - a raised CPU signal
External - raised by an external event
Internal - generated by on-chip peripherals
Can happen any time -- asynchronous interrupts
• Trap - software interrupt (synchronous interrupts)
Mechanism that changes control flow
Bridge to supervisor mode (system calls)
Requires additional data to be communicated
Parameters, type of request
Return values (status)
• Exception - unprogrammed control transfer
General term for synchronous interrupts
On some machines these include internal async interrupts
Interrupts, Traps, and Exceptions
• External Interrupts - a raised CPU signal
Asynchronous to program execution
May be handled between instructions
Simply suspend and resume user program
• Traps (and exceptions)
Exceptional conditions (overflow)
Invalid instruction
Faults (non-resident page in memory)
Internal hardware error
Synchronous to program execution
Condition must be remedied by the handler
Restarting After a Synchronous Interrupt