CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 4 of 8 · 2 min read

Masking and priority

Think of a "do not disturb" sign on a hotel door. Visitors wait until the sign is removed, and an emergency bell still gets through. Masking works in this way. The analogy stops in one place: a guest removes the sign when ready, and the kernel sets and clears the mask in short critical sections.

In plain words: Sometimes the kernel must not be interrupted. It might be in the middle of updating a table that a handler also uses. Hardware therefore lets the kernel hold off interrupts at critical moments. Modern interrupt hardware gives three features:

  1. Deferral. Interrupt handling can be delayed during critical work.
  2. Efficient dispatch. The interrupt reaches the correct handler without a search.
  3. Priority levels. The system can tell urgent events from unimportant ones, and let an urgent one interrupt a low-priority handler but not the other way round.

Most processors have two request lines. The nonmaskable interrupt is for events that cannot be ignored, such as an unrecoverable memory error. The maskable interrupt is used by device controllers, and the processor can switch it off before a critical instruction sequence. On Intel processors, vector numbers 0 to 31 are nonmaskable and signal errors, and numbers 32 to 255 are maskable and are used for devices. Number 0 means "divide error".

A machine has more devices than it has table entries. A common solution is interrupt chaining: each table entry points to a list of handlers. When the interrupt arrives, the handlers on that list run one by one until one of them recognises the device and services it.

A worked example.

A worked example (made up to show the idea). The kernel disables interrupts for 20 microseconds while it updates a shared counter. A device raises an interrupt 5 microseconds into that period. The request waits in the hardware until interrupts are enabled again, 15 microseconds later. The device waited 15 microseconds, and nothing was lost. If the kernel disabled interrupts for 20 milliseconds instead, a device sending data every 5 milliseconds could lose data, because the second event would arrive before the first was handled.

Maskable interrupt

A maskable interrupt is an interrupt that the processor can be told to ignore temporarily by setting a mask. The kernel masks such interrupts during short critical sections, and a masked interrupt is delivered when the mask is cleared. A non-maskable interrupt, reserved for serious events such as hardware faults, cannot be ignored.

Example: the kernel switches off device interrupts while it updates a list shared with a handler, then switches them back on.

Check yourself: 1. Which interrupts are nonmaskable on Intel processors? 2. What does interrupt chaining solve? 3. Why must interrupts never be disabled for long?