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.
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.
| Method | How the CPU reaches the registers | Used by |
|---|---|---|
| Memory-mapped I/O | Device registers appear at physical addresses; ordinary loads and stores reach them | Nearly all modern devices (ARM, RISC-V, PCIe) |
| Port-mapped I/O | Special instructions (in, out) on a separate address space | Legacy 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
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.
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
- The device raises an interrupt line (or sends a message-signalled interrupt, MSI) to the interrupt controller (APIC/GIC).
- The controller picks a CPU and delivers the interrupt vector number.
- 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).
- 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).
- 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
| Kind | Cause | Synchronous? | Examples |
|---|---|---|---|
| Interrupt | External device or timer | No | Network packet, keypress, timer tick |
| Exception / fault | The current instruction failed | Yes | Page fault, divide by zero, illegal instruction |
| Trap | Deliberate instruction | Yes | syscall, 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.
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
| Interconnect | Connects | Bandwidth (approx.) |
|---|---|---|
| On-chip mesh / ring | Cores, caches, memory controllers | Hundreds of GB/s |
| DDR5 memory channel | CPU ↔ DRAM | ~50 GB/s per channel |
| PCIe 5.0 x16 | GPUs, NVMe SSDs, NICs | ~64 GB/s each way |
| USB 3.2 / 4 | Peripherals | 10–40 Gbit/s |
| NVLink / CXL | GPU ↔ GPU, coherent memory expansion | Hundreds 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.
- 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.