User Program
SYSCALL(Print)
…
Want to Print
Interrupt Priority ?
Network Card
Network
Buffer (Queue)
Packet Arrived
Non maskable interrupts.
Most CPUs have two interrupt request lines.
-
One is the nonmaskable interrupt (NMI) which is reserved for events such as unrecoverable memory errors, or power related signals.
-
The second interrupt line is maskable. It can be turned off (disable interrupts) by the CPU before the execution of critical instruction sequences that must not be interrupted.
The maskable interrupt is used by device controllers to request service.
Reentrancy_problems._Priority._Sequential_Data.___The_interrupt_mechanism_also_implements_a_system_of_interrupt_priority'>Defer Interrupts. Reentrancy problems. Priority. Sequential Data.
The interrupt mechanism also implements a system of interrupt priority levels. This mechanism enables the CPU to defer the handling of low-priority interrupts without masking off all interrupts and makes it possible for a high-priority interrupt to preempt the execution of a low-priority interrupt.
Very often there is mandatory to serve the interrupts from the same device or from the same group of devices sequentially. That requirement is related with the Reentrancy problems or sequential nature of the data transferred during the interrupts. The reentrancy problem example is shown at the end of this handout.
In both cases the interrupts should be served sequentially and the next interrupts are deferred until the previous one is not served fully.
Interrupt Controller
The interrupt is a Physical signal coming from some device. It’s a wire going into the system and when it is asserted it says that I need the service, When it is not asserted it says nothing.
The signals could be different for different systems (edge triggered, level triggered, positive / negative logic).
The main task of Interrupt Controller is to regulate the hardware interrupts arrival to the CPU. It is programmed by OS that is why it’s called Programmable Interrupt Controller (PIC). It accepts the interrupts and talks to CPU to allow the necessary ones.
The interrupt controller is a device which could be implemented on a separate chip or be included into a larger chipset together with the other devices.
The Programmable Interrupt Controller (PIC) Intel's original controller was done on a separate chip 8259A.
The modern Advanced Programmable Interrupt Controller (APIC) is a more complex one and is incorporated with more complex chipset (South Bridge).
Interrupt mask – is a bit mask that allows the appropriate interrupt to go to processor or no. It’s set up by software (OS). If the line is blocked then whatever happens on the interrupt line it’s not passed through.
Typically CPU blocks the interrupt mask for particular device while it serves the previous interrupt of the same device – to not have nested or recursive interrupts from one device.
When we allow all 3 masks and have simultaneous interrupts from different devices Printer, CD, Network Card then we have to choose which one to pass at the moment. So we have to have a prioritizing mechanism.
The Priority Encoder helps us to block the interrupts from lower interrupt levels and allow the interrupts of higher level only.
The priority encoder takes the allowed interrupt and sends it to CPU with the interrupt ID. The interrupt line tells CPU that it should serve this interrupt and that the number of interrupt is equal to ID.
CPU stops what it’s doing now and runs the interrupt routine corresponding to the interrupt ID. There could be different interrupt routines for CD, Printer or Network Card.
Interrupt Disable bit – If this bit is setup then no interrupts allowed at all. The interrupt line is blocked fully.
NMI is not affected by Interrupt Disable bit. All Non Maskable Interrupts are allowed always.
System Calls setup some bits in Software Interrupt registers which then cause interrupt in usual way as the other interrupt sources.
-
Interrupts invoked with interrupt lines from devices
-
Interrupt controller chooses interrupt request to honor
-
Mask enables/disables interrupts
-
Priority encoder picks highest enabled interrupt
-
Software Interrupt Set/Cleared by Software
-
Interrupt identity specified with ID line
-
CPU can disable all interrupts with internal flag
-
Non-maskable interrupt line (NMI) can’t be disabled
An example of network card interrupt
Suppose the network card got packet and asserted interrupt signal to the Interrupt Controller.
Below steps are done by hardware:
-
The controller passes the interrupt request to CPU and the program flow shown on the below picture is interrupted.
-
The first what happened the pipeline should be drained – pipeline flush – to fully stop any activity initiated by the previous program.
-
PC is saved to use it later to return to the interrupted program.
-
All interrupts are disabled by setting Disable Interrupt bit.
In hardware we disable all interrupts but in software we change the priority of interrupts
-
We Raise the priority to prevent recursive infinite interrupts of the same device or of the same level.
-
But we cannot leave the system without interrupt service during whole interrupt service time that is why immediately after raising priority we reenable interrupts to be able to handle the higher level interrupts. This is done purely by software and the software can choose the hierarchy of levels of interrupts. Typically it will just turn off the mask of this device and reenable all the other devices masks.
-
Save registers – of the interrupted program
-
Dispatch to network packet receiver handler
-
Transfer packet to the Kernel buffer.
-
Restore registers.
-
Talk to device and clear the interrupt. Setup appropriate bits in device controller to inform that the interrupt is serviced and the device can send new interrupt.
-
Now we have to enable the interrupts again from this device but we do not want to do it in this routine. Because this routine could be interrupted immediately after enabling the interrupts.
-
That was why we again disable all interrupts
-
Then restore the priority preparing to accept new interrupts from the same devices but not yet allowing them.
-
Finally enable the interrupts by hardware in RTI (iret in x86, eret in MIPS) instruction.
-
Disable/Enable All Ints Internal CPU disable bit
-
RTI reenables interrupts, returns to user program
-
Raise/lower priority: change interrupt mask
-
Software interrupts can be provided entirely in software at priority switching boundaries
Reentrancy Problems
A minor problem develops with developing ISRs, what happens if you enable interrupts while in an ISR and a second interrupt from the same device comes along? This would interrupt the ISR and then reenter the ISR from the beginning. Many applications do not behave properly under these conditions.
-
An application that can properly handle this situation is said to be reentrant.
-
Code segments that do not operate properly when reentered are nonreentrant.
Consider the TIMER.ASM program in the previous section. This is an example of a nonreentrant program. Suppose that while executing the ISR, it is interrupted at the following point:
TimerISR proc near
push ds
push ax
mov ax, dseg
mov ds, ax
mov ax, MSEC
add ax, 55 ;Interrupt every 55 msec.
cmp ax, 1000
jb SetMSEC
; <<<<< Suppose the interrupt occurs at this point >>>>>
inc Timer ;A second just passed.
sub ax, 1000 ;Adjust MSEC value.
SetMSEC: mov MSEC, ax
pop ax
pop ds
jmp cseg:OldInt1C ;Transfer to original ISR.
TimerISR endp
Suppose that, on the first invocation of the interrupt, MSEC contains 950 and Timer contains three. If a second interrupt occurs and the specified point above, ax will contain 1005. So the interrupt suspends the ISR and reenters it from the beginning. Note that Timer ISR is nice enough to preserve the ax register containing the value 1005. When the second invocation of Timer ISR executes, it finds that MSEC still contains 950 because the first invocation has yet to update MSEC. Therefore, it adds 55 to this value, determines that it exceeds 1000, increments Timer (it becomes four) and then stores five into MSEC.
Then it returns (by jumping to the next ISR in the int 1ch chain). Eventually, control returns the first invocation of the TimerISR routine. At this time (less than 55 msec after updating Timer by the second invocation) the TimerISR code increments the Timer variable again and updates MSEC to five. The problem with this sequence is that it has incremented the Timer variable twice in less than 55 msec.
Now you might argue that hardware interrupts always clear the interrupt disable flag so it would not be possible for this interrupt to be reentered. Furthermore, you might argue that this routine is so short, it would never take more than 55 msec to get to the noted point in the code above. However, you are forgetting something: some other timer ISR could be in the system that calls your code after it is done. That code could take 55 msec and just happen to turn the interrupts back on, making it perfectly possible that your code could be reentered.
The code between the
mov ax, MSEC
and
mov MSEC, ax
instructions above is called a critical region or critical section. A program must not be reentered while it is executing in a critical region. Note that having critical regions does not mean that a program is not reentrant. Most programs, even those that are reentrant, have various critical regions. The key is to prevent an interrupt that could cause a critical region to be reentered while in that critical region. The easiest way to prevent such an occurrence is to turn off the interrupts while executing code in a critical section. We can easily modify the TimerISR to do this with the following code:
TimerISR proc near
push ds
push ax
mov ax, dseg
mov ds, ax
; Beginning of critical section, turn off interrupts.
pushf ;Preserve current I flag state.
cli ;Make sure interrupts are off.
mov ax, MSEC
add ax, 55 ;Interrupt every 55 msec.
cmp ax, 1000
jb SetMSEC
inc Timer ;A second just passed.
sub ax, 1000 ;Adjust MSEC value.
SetMSEC: mov MSEC, ax
; End of critical region, restore the I flag to its former glory.
popf
pop ax
pop ds
jmp cseg:OldInt1C;Transfer to original ISR.
TimerISR endp
Share with your friends: |