I/O & Interrupts

How the CPU talks to devices — memory-mapped I/O, polling vs interrupts, DMA, buses like PCIe, and how interrupts, exceptions and traps transfer control to the OS.

Intermediate 4 min readLesson 13 of 15

Computation is only useful if data gets in and out. Devices — disks, network cards, keyboards, GPUs — are slow and unpredictable compared with the CPU, so the hardware provides mechanisms to talk to them efficiently: device registers, interrupts and DMA.

Talking to devices

Each device has a controller exposing registers: status (busy? done? error?), control (start, reset) and data.

MethodHow the CPU reaches the registersUsed by
Memory-mapped I/ODevice registers appear at physical addresses; ordinary loads and stores reach themNearly all modern devices (ARM, RISC-V, PCIe)
Port-mapped I/OSpecial instructions (in, out) on a separate address spaceLegacy x86 devices

Those addresses must bypass the caches (marked uncacheable) and must not be reordered, otherwise a write to "start" could reach the device before the data it depends on.

Polling vs interrupts

Polling (busy-waiting)

The CPU repeatedly reads the status register until the device is ready. Simple and lowest latency when the event comes quickly, but it wastes CPU time while waiting.

Interrupts

The device signals the CPU when it needs attention. The CPU does other work in the meantime, then stops, runs an interrupt handler, and resumes. Efficient for rare or slow events.

How an interrupt is handled

  1. The device raises an interrupt line (or sends a message-signalled interrupt, MSI) to the interrupt controller (APIC/GIC).
  2. The controller picks a CPU and delivers the interrupt vector number.
  3. At the next instruction boundary the CPU saves the PC and flags, switches to kernel mode, and jumps to the handler listed in the interrupt vector table (IDT on x86).
  4. The handler does the minimal urgent work (acknowledge the device, grab the data) and schedules the rest for later (Linux "top half / bottom half", softirqs).
  5. It returns from the interrupt; the CPU restores state and continues the interrupted code.

Interrupts have priorities and can be masked (temporarily disabled) during critical sections. Non-maskable interrupts (NMIs) are reserved for things like hardware failures and watchdogs.

Interrupts, exceptions and traps

KindCauseSynchronous?Examples
InterruptExternal device or timerNoNetwork packet, keypress, timer tick
Exception / faultThe current instruction failedYesPage fault, divide by zero, illegal instruction
TrapDeliberate instructionYessyscall, ecall, breakpoint (int3)

All three use the same mechanism: save state, switch to kernel mode, jump to a handler. The timer interrupt is what lets the OS preempt a running program and switch to another: the foundation of multitasking (CPU scheduling).

DMA: direct memory access

Copying a 1 MB disk block through the CPU one word at a time would waste millions of cycles. With DMA, the CPU tells the device controller "transfer N bytes to address X" and gets on with other work. The controller moves the data to or from RAM itself and raises one interrupt when it's done.

CPUDMA / deviceRAMsetup: address, length, directionruns other worktransfer data (burst)interrupt: done

Modern devices use descriptor rings: queues in RAM describing many buffers, so a network card can receive thousands of packets without CPU involvement. An IOMMU translates and restricts device addresses so a buggy or malicious device can't write anywhere in memory.

Buses and interconnects

InterconnectConnectsBandwidth (approx.)
On-chip mesh / ringCores, caches, memory controllersHundreds of GB/s
DDR5 memory channelCPU ↔ DRAM~50 GB/s per channel
PCIe 5.0 x16GPUs, NVMe SSDs, NICs~64 GB/s each way
USB 3.2 / 4Peripherals10–40 Gbit/s
NVLink / CXLGPU ↔ GPU, coherent memory expansionHundreds of GB/s

PCIe is a set of point-to-point serial lanes, not a shared bus; devices are enumerated at boot and their registers mapped into the physical address space.

I/O software layers

Hardware is only half the story. The OS wraps devices in drivers, a generic block or network layer, buffering and scheduling, and finally the system calls that programs use (read, write). See I/O systems & disks.

Key takeaways
  • Devices expose registers, usually memory-mapped and uncacheable.
  • Polling wastes CPU while waiting; interrupts let the CPU work until the device signals.
  • Interrupts (asynchronous), exceptions and traps (synchronous) share one entry mechanism into the kernel.
  • DMA moves bulk data without the CPU; the IOMMU keeps it safe.
  • PCIe lanes connect high-speed devices; timer interrupts enable preemptive multitasking.