Полезная информация

next up previous contents index
Next: Keyboards on the Intel Up: Linux Kernel Module Programming Previous: Scheduling Tasks

    
Interrupt Handlers

Except for the last chapter, everything we did in the kernel so far we've done as a response to a process asking for it, either by dealing with a special file, sending an ioctl, or issuing a system call. But the job of the kernel isn't just to respond to process requests. Another job, which is every bit as important, is to speak to the hardware connected to the machine.

There are two types of interaction between the CPU and the rest of the computer's hardware. The first type is when the CPU gives orders to the hardware, the other is when the hardware needs to tell the CPU something. The second, called interrupts, is much harder to implement because it has to be dealt with when convenient for the hardware, not the CPU. Hardware devices typically have a very small amount of ram, and if you don't read their information when available, it is lost.

Under Linux, hardware interrupts are called IRQs (short for Interrupt Requests)11.1. There are two types of IRQs, short and long. A short IRQ is one which is expected to take a very short period of time, during which the rest of the machine will be blocked and no other interrupts will be handled. A long IRQ is one which can take longer, and during which other interrupts may occur (but not interrupts from the same device). If at all possible, it's better to declare an interrupt handler to be long.

When the CPU receives an interrupt, it stops whatever it's doing (unless it's processing a more important interrupt, in which case it will deal with this one only when the more important one is done), saves certain parameters on the stack and calls the interrupt handler. This means that certain things are not allowed in the interrupt handler itself, because the system is in an unknown state. The solution to this problem is for the interrupt handler to do what needs to be done immediately, usually read something from the hardware or send something to the hardware, and then schedule the handling of the new information at a later time (this is called the `bottom half') and return. The kernel is then guaranteed to call the bottom half as soon as possible -- and when it does, everything allowed in kernel modules will be allowed.  

The way to implement this is to call request_irq to get your interrupt handler called when the relevant IRQ is received (there are 16 of them on Intel platforms). This function receives the IRQ number, the name of the function, flags, a name for /proc/interrupts and a parameter to pass to the interrupt handler. The flags can include SA_SHIRQ to indicate you're willing to share the IRQ with other interrupt handlers (usually because a number of hardware devices sit on the same IRQ) and SA_INTERRUPT to indicate this is a fast interrupt. This function will only succeed if there isn't already a handler on this IRQ, or if you're both willing to share.        

Then, from within the interrupt handler, we communicate with the hardware and then use queue_task_irq with tq_immediate and mark_bh(BH_IMMEDIATE) to schedule the bottom half. The reason we can't use the standard queue_task in version 2.0 is that the interrupt might happen right in the middle of somebody else's queue_task11.2. We need mark_bh because earlier versions of Linux only had an array of 32 bottom halves, and now one of them (BH_IMMEDIATE) is used for the linked list of bottom halves for drivers which didn't get a bottom half entry assigned to them.          



 
next up previous contents index
Next: Keyboards on the Intel Up: Linux Kernel Module Programming Previous: Scheduling Tasks

1999-05-19