The main problem when implementing ISRs is that they execute in what is known as the interrupt context. Kernel code can sometimes run both in the regular context and in the interrupt context. To distinguish between these two variants and to design code accordingly, the kernel provides the in_interrupt function to indicate whether or not an interrupt is currently being serviced.

The interrupt context differs in three important points from the normal context in which the kernel otherwise executes:

1. Interrupts are executed asynchronously; in other words, they can occur at any time. As a result, the handler routine is not executed in a clearly defined environment with respect to the reservation of userspace. This prohibits access to userspace and prevents above all the copying of memory contents into and out of the userspace addresses.

For network drivers, for example, it is therefore not possible to forward data received directly to the waiting application. After all, it is not certain that the application waiting for the data is running at the time (this is, in fact, extremely unlikely).

2. The scheduler may not be invoked in the interrupt context. It is therefore impossible to surrender control voluntarily.

3. The handler routine may not go to sleep. Sleep states can only be broken when an external event causes a state change and wakes the process. However, because interrupts are not allowed in the interrupt context, the sleeping process would wait forever for the relieving news. As the scheduler may also not be invoked, no other process can be selected to replace the current sleeping process.

It is not, of course, enough simply to make sure that only the direct code of a handler routine is free of possible sleeping instructions. All invoked procedures and functions (and procedures and functions invoked by these, in turn) must be free of expressions that could go to sleep. Checking that this is the case is not always trivial and must be done very carefully, particularly if control paths have numerous branches.

Continue reading here: Implementing Handlers

Was this article helpful?

0 0