MSDN Docs – Windows Kernel

Overview

Interrupts are the primary mechanism that the Windows kernel uses to respond to asynchronous events from hardware and software. An interrupt transfers control from the currently executing code to a predefined interrupt service routine (ISR) that handles the event.

Types of Interrupts

Interrupt Handling

The kernel processes interrupts in several stages:

  1. ISR (Interrupt Service Routine) – Executes at high IRQL, performs minimal work, acknowledges hardware.
  2. DPC (Deferred Procedure Call) – Runs at DISPATCH_LEVEL, handles lengthy processing.
  3. Synchronisation – Uses spinlocks and IRQL masking to protect shared resources.

API Reference

FunctionDescription
KeInitializeInterruptInitialises a KINTERRUPT object.
IoConnectInterruptRegisters an ISR with the kernel.
IoDisconnectInterruptUnregisters an ISR.
KeRaiseIrqlRaises the current IRQL.
KeLowerIrqlLowers the IRQL.

Example Code

#include <ntddk.h>

VOID MyIsr(
    PKINTERRUPT Interrupt,
    PVOID ServiceContext
)
{
    UNREFERENCED_PARAMETER(Interrupt);
    UNREFERENCED_PARAMETER(ServiceContext);
    // Acknowledge hardware interrupt here
    // Queue a DPC for further processing
    KeInsertQueueDpc(&MyDpc, NULL, NULL);
}

VOID MyDpc(
    KDPC *Dpc,
    PVOID DeferredContext,
    PVOID SystemArgument1,
    PVOID SystemArgument2
)
{
    UNREFERENCED_PARAMETER(Dpc);
    UNREFERENCED_PARAMETER(DeferredContext);
    UNREFERENCED_PARAMETER(SystemArgument1);
    UNREFERENCED_PARAMETER(SystemArgument2);
    // Perform lengthy work safely at DISPATCH_LEVEL
}

NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
    UNREFERENCED_PARAMETER(RegistryPath);
    PKINTERRUPT interruptObject = NULL;
    NTSTATUS status;

    // Register ISR
    status = IoConnectInterrupt(
        &interruptObject,
        MyIsr,
        NULL,
        NULL,
        0, // Vector (auto-assigned)
        DIRQL,
        DIRQL,
        LevelSensitive,
        TRUE,
        0,
        FALSE
    );

    if (!NT_SUCCESS(status))
        return status;

    DriverObject->DriverUnload = DriverUnload;
    return STATUS_SUCCESS;
}