Interrupt Handling
Overview
An interrupt is a signal sent by hardware or software to the processor for processing. The process that runs when an interrupt is generated is the Interrupt Handler. The CPU saves the state of the ongoing process and shifts its attention to the interrupt generated by giving access to the interrupt handler. This entire process is called interrupt handling.
Interrupt Handler's Overview
The CPU saves the state of the ongoing process and shifts its attention to the interrupt generated. CPU gives access to the interrupt handler to process the interrupt. This entire process is called interrupt handling.
Interrupt Handler is a process that runs when an interrupt is generated by hardware or software. The interrupt handler is also known as the Interrupt Service Routine (ISR). ISR handles the request and sends it to the CPU. When the ISR is complete, the process gets resumed.
Before working on the interrupt, the state of the current program which was in execution is saved. After saving the state of the current process, the control is then given to a program to handle the interrupt. We have several types of interrupt handlers. There is usually an interrupt handler associated with an interrupt.
For example, the keyboard has its interrupt handler, and the printer has its interrupt handler, and so on.
Types Of Interrupt Handlers
We can broadly divide interrupt handlers into two categories according to the interrupt handling time:
1. First Level Interrupt Handler or FLIH:
FLIH quickly services an interrupt and schedules the second-level interrupt handlers if needed. FLIH mainly deals with maskable interrupts(interrupts that can be ignored by the instructions of the CPU). The first level interrupt handler is also known as a fast interrupt handler or a hard interrupt handler.
2. Second Level Interrupt Handler or SLIH:
SLIH usually deals with the non-maskable interrupts that have higher priority than the CPU's current process. Hence, they need more attention. The second level interrupt handler is also known as a slow interrupt handler or a soft interrupt handler.
How To Handle Interrupt?
Let's now discuss how an interrupt is handled and how an interrupt handler works in detail.
An interrupt first goes to hardware known as Interrupt Controller. This controller will generate an interrupt to the CPU. After the completion of one instruction cycle, the CPU checks whether an interrupt is pending or not. If any interrupt is not there, then the CPU will continue with the next instruction. But if there is some un-serviced interrupt, then the CPU will pay attention to that interrupt.
One thing to note here is that the state of the current program which was in execution is saved along with its corresponding register states. After saving the state of the current process, the control is then given to a program to handle the interrupt, and every program is known as Interrupt Handler.
The CPU will now detect the kind of interrupt and its respective interrupt number. The interrupt number and its corresponding instructions set address(or process’s base address) are stored in a vector table known as IVT or Interrupt Vector Table.
Using the interrupt number and IVT, the CPU will get to know the base address of the process that is needed to handle the respective interrupt. Now, the control will be given to that process to handle the interrupt and this process is known as Interrupt Service Routine (ISR).
The interrupt handler will now execute the necessary set of instructions (ISR) to handle the interrupt. After its execution, the control will be coming back to the very process that was suspended. The suspended process will now be resumed.
Handling I/O Interrupts
I/O interrupts are bi-directional. Input/Output interrupts are easy to handle. A peripheral or terminal device like a keyboard or mouse either sends some data to the memory or receives some data. The sending of the signal is known as Interrupt Acknowledgement.
In the case of I/O interrupts, there is no repeated CPU checking of the I/O module. The CPU sends a read interrupt or a message to the Interrupt Handler that the CPU is ready to handle I/O operations. After sending a read interrupt, the CPU does the ongoing work and meanwhile, if an interrupt is generated from the peripheral devices like keyboard or mouse, then the CPU shifts its focus. I/O module sends an interrupt to the interrupt controller only when it is ready with some data to be written on memory(also known as words). After getting the interrupt from the interrupt controller, the CPU calls the respective interrupt handler. It is the task of the respective interrupt handler to write words to the memory.
Hence, better performance is guaranteed with no CPU wastage time.
Device Interrupts
Whenever there is an interrupt caused by devices, computer buses prioritize these interrupts as high-level and normal or low-level interrupts. Using these bus interrupt levels, processor-interrupt levels are mapped. The interrupt that maps to the scheduler level or above is termed a high-level interrupt. On the other hand, the interrupt that maps to a lower level than the scheduler level is termed a normal level interrupt.
According to the level of interrupt or interrupt request number, the CPU determines which device, of all the registered devices was interrupted. Further, based on this information, the interrupt handler is called.
Registering Interrupts
To receive and service interrupts, the device driver first registers an interrupt handler to the system via an interrupt controller. It helps the interrupt handler to associate itself with the respective interrupt specifications.
This interrupt registration further helps the interrupt controller to determine whether a newly generated interrupt is to be handled to masked.
Interrupt Latency
Latency simply means delay. Interrupt latency is the delay in time between the interrupt generation and the starting of interrupt handler execution. Interrupt latency is also known as Interrupt Response Time.
Interrupt Latency = Interrupt Execution Time - Interrupt Generation Time.
Interrupt Resource Management
IRM or Interrupt Resource Management is a feature that allows device drivers to utilize more interrupt resources by dynamically managing the device driver's interrupt configuration.
IRM feature monitors the system and recalculates the number of interrupt vectors needed for each device in response to the current changes. It further notifies each involved driver about the new allocation of interrupt vectors.
Features of IRM:
- IRM calculates the total number of available interrupt vectors.
- IRM also calculates the total number of requested interrupt vectors.
- It allocates interrupt vectors to required drivers dynamically.
- IRM is also responsible for maintaining fairness among all the drivers by equal allocation of interrupt vectors.
Interrupt Handler Functionality
Interrupt Handler is the main routine or piece of code containing a sequence of operations, responsible for managing and handling different types of interrupts as per their priority levels. The main job of an interrupt handler is to service the interrupt and stop it from interrupting.
Some of the functionalities that an Interrupt Handler provides are:
- Interrupt Handlers are initiated or called by different interrupts like hardware interrupt, software interrupt, or software exceptions.
- Interrupt Handler determines the type of interrupt and its requirements.
- Interrupt Handler registers the interrupts and finds out the source of generation.
- Interrupt Handler sometimes performs additional processing that could prevent future interrupts.
- Interrupt Handler then services the interrupts by executing a certain set of instructions.
- Interrupt Handler also sets the priority of the interrupt.
- Interrupt Handler is also responsible for masking the low-level interrupts.
- Interrupt Handler informs devices to be serviced.
- At last, the Interrupt Handler returns the control to the suspended process.
Handling High-Level Interrupts
Scheduler or above-level interrupts are called high-level interrupts. Since they have higher-level priority, they do not allow the scheduler to run.
Interrupt Handler cannot block or mask high-level interrupts. Once a specific interrupt is marked as a high-level interrupt by the interrupt handler, it is handled by the Operating System. The operating system uses mutually exclusive locks for locking. High-Level Interrupts are handled without using system services.
High-level interrupts are also known as bus level interrupts.
Event-Specific Interrupt Processing
Event-specific interrupts are program-specific interrupts or hardware failure interrupts that are generated by hardware failure or programmatic error(s) like dividing something with 0. Event-specific Interrupts may be generated by conditions like :
- arithmetic overflow
- division by zero
- a page fault
- invalid instructions in the process
- a reference to the invalid memory space
- power failure etc.
The CPU throws an exception interrupt in these cases. These interrupts are trapped like a hardware interrupt and the control is given to the OS which handles the events. The OS may terminate the interrupt-causing program or free the allocated resources accordingly. After the interrupt is handled, the CPU gets back the control.
Example Interrupt Like Keyboard Interrupt:
As we have discussed above I/O interrupts are bi-directional and easy to handle. Keyboard interrupt is an example of an I/O interrupt. Whenever a key is pressed or released, the keyboard controller generates a signal. The keyboard also generates a scan key which is used to identify the physical position of the key. The scan key also tells whether the key was pressed or released. The keyboard device driver will send the signal as well as the scan key to the Interrupt Controller. Interrupt Controller will further interrupt the CPU. The CPU will complete the ongoing instruction and then refer to this interrupt. The CPU will further use the information provided and the IVT table to call a specific interrupt handler.
Conclusion
- Interrupt is a signal sent by hardware or software to the processor for processing.
- An interrupt first goes to hardware known as Interrupt Controller. This controller will generate an interrupt to the CPU.
- The process that runs when an interrupt is generated is Interrupt Handler.
- Interrupt Handler is of two types namely - First Level Interrupt Handler(FLIH) and Second Level Interrupt Handler(SLIH).
- Interrupt Handler also sets the priority of the interrupt.
- Interrupt Handler is also responsible for masking the low-level interrupts.
- To receive and service interrupts, the device driver first registers an interrupt handler to the system via an interrupt controller.
- Latency simply means delay. Interrupt latency is the delay in time between the interrupt generation and the starting of interrupt handler execution.
- Interrupt Resource Management allows drivers to utilize more interrupt resources by dynamically managing the driver's interrupt configuration.