This Section will cover the internals of Interrupt Handling in Linux Kernel (all explaination is related to i386 platform). This section is under development and might be incomplete right now.
I will cover the following topics in this section, explaining thehardware as well as software part of it, from how the interrupts aregenerated, routed and then handled by the low level code of Linux Kernel.
- CPU Support for Handling Interrupts
- Details of Programmable Interrupt Controller
- Hardware checks performed by CPU
- Details of Interrupt Descriptor Table
- Task Gates
- Trap Gates
- Interrupt Gates
- Kernel Support for Handling Interrupts
- Low Level Interrupt Stubs - Details of do_IRQ() function, core of Interrupt Handling
This section will discuss, the hardware prospective of interrupt handling fromCPU, Linux Kernel's Interrupt Routing subsystem, Device Drivers's rolein Interrupt handling.
Term Interrupt is self defined,Interrupts are signals sent to CPU on an INTR bus (connected to CPU) whenever any device want to get attention of CPU. As soon as theinterrupt signal occurs, CPU defer the current activity and service the interrupt by executing the interrupt handler corresponding to that interrupt number (also know as IRQ number).
One of the clasifications of Interrupts can be done as follows: - Synchronous Interrupts (also know on as software interrupts) - Asynchronous Interrupts (also know as hardware interrupts)
Basic difference between these is that, synchronous interrupts are generated by CPU's control unit on facing some abnormal condition; these are also know as exception in Intel's termenology. These are interrupts whihc are generated by CPU itself either when CPU detects an abnormal condition or CPU executes some of the special instructions like 'int'or 'int3' etc. on other hand, asynchronous interupts are those, which actually are generated by outside world (devices connected to CPU). As these interrupts can occur at any point of time, these are known asynchronous interrupts.
Its important to note that both synchornous and asynchronous interrupts are handled by CPU on the completion of insturction during which the interrupt occur. Execution of a machine instruction is not done in one single CPU cycle, it take somecycles to complete. Any interrupt occurs in between the execution of instruction, will not be handled imediately, rather CPU will check o finterrupts on the completion of instruction.
2. CPU's support for handling interrupts
For handling interrupts there are few of the things which we expecttheCPU to do on occurence of every interrupt. Whenever an interruptoccurs,CPU performs some of the hardware checks, which are very muchneeded tomake the system secure. Before explaining the hardware checks,we willunderstand how the interrupts are routed to the CPU fromhardwaredevices.
2a) Details of Programmable Interrupt Controller
On Intel architecture, system devices (devicecontrollers)areconnectedto a special device known as PIC (Programmable Interrupt Controller). CPU have two lines for receiving interrupt signals(NMIandINTR). NMIline is to recieve non-maskable interrupts; theinterruptswhich cannot be masked, means which can not be blocked atanycost.Theseinterrupts are of hightest priority and are rarelyused. INTRlineisthe line on which all the interrupts from system devicesarereceived.These interrupts can be masked or blocked. As alltheinterruptsignalsneed to be multiplxed on single CPU line, weneedsomemechanisumthrough which interrupts from different devicecontrollerscan berouted to single line of CPU. This routing ormultiplexing isdone PIC(Programmable Interrupt Controller). PIC sitsbetween systemdevicesand CPU and have multiple input lines; each lineconnected todifferentdivice contollers in system. On other hand IPChave only oneoutputline which is connected to the CPU's INTR line onwhich it sendssignalto CPU. There are two PIC controllers joinedtogether and theoutput ofsecond PIC controller is connected tothesecond input of firstPCI.This setup allows maximum of 15 input lines onwhich differentsystemdevice controllers can be connected. PIC havesomeprogrammableregisters, through which CPU communicates with it(givecommand,mask/unmask interrup lines, read status). Both PICs havetheirownfollowing registers:
Mask register is used to mask/unmask a specific interrupt line.CPUcanask the PIC to mask (block) the specific interrupt bysettingthecorresponding bit in mask register. Unmasking can be donebyclearingthat bit. When a particular interrupt is being masked, PICdoreceivethe interrupts on its corresponding input line, but do notsendtheinterrupt singnal to CPU in which case CPU keeps on doing whatitwasdoing. When an interrupts are being masked, they are notlost,ratherPIC remembers those and do send the interrupt to CPU whenCPUunmasksthat interrupt line. Masking is different from blockingalltheinterrupts toCPU. CPU can ignore all the interrupts coming onINTRlineby clearing the IF (Interrupt Falg) flag in EFLAGS registerofCPU.When this bit is cleared, interrupts coming on INTR linearesimplyignored by CPU, we can consider it to be blocking ofinterrupts.So nowwe understand that masking is done at PIC levelandindividualinterrupt lines can be masked or unmasked,where asblockingis done atCPU level and is done for all the interrupts comingto tahtCPU exceptNMI (Non-Maskable Interrupt), which is received on NMIlineof CPU andcan not be blocked or ignored.
Now days,interrupt architecture is not as simple as shown above.Nowdaysmachines uses the APIC (Advanced Programmable Interrupt Controller),which can support upto 256 interrupt lines. AlongwithAPIC, every CPUalso have inbuilt IO-APIC. I won't go into detailsofthese right now.
2b) Hardware checks performed by CPU
Once the interrupt signal is received by CPU, CPU performs some hardware checks for which no software machine instructions are executed. Before looking into what these checks are, we need to understand some architecture spcific data structures maintained by kernel.