Device Drivers
OS & The Kernel · Assembly & OS track
7 min read
In this module
Talking to Hardware
Your computer is wired to dozens of hardware devices — a keyboard, a display, a network card, storage drives, USB peripherals, audio chips, more. Each device has its own protocol: specific registers to write to, data formats to follow, timing requirements to respect. The OS can't bake in support for every device ever manufactured. So it uses device drivers.
A device driver is a piece of software that knows how to speak a specific device's dialect. It translates the OS's generic requests into the exact commands a particular piece of hardware will accept.
The Driver Model
Operating systems define a standard interface every driver must implement. The OS might say, "Every storage driver must provide read_block and write_block." The driver for a specific SSD fills those functions with the right low-level commands for that hardware.
The kernel never needs to know the first thing about your specific hardware. It calls the standard interface; the driver handles the rest.
Plug in a new peripheral and it does nothing until you install a driver? This is why. The OS needs a driver that implements its standard interface for that specific hardware. Without one, there's no shared language to talk over.
Interrupts
Devices don't sit there politely waiting to be queried. When a device has something to report — a key was pressed, a network packet arrived, a disk read finished — it fires an interrupt at the CPU.
An interrupt is a hardware signal that yanks the CPU off its current work, makes it save its state, and sends it jumping into a special piece of code called an interrupt handler (part of the driver). The handler deals with the event, then the CPU picks up where it left off.
This is far more efficient than having the CPU constantly poll every device to ask "anything happened yet?" (the predictably named technique: polling).
Interrupts aren't free either. Each one costs a trip into the kernel, and a network card receiving hundreds of thousands of packets a second could spend all its time interrupting. So Linux network drivers switch modes under load: the first packet raises an interrupt, the driver turns that interrupt off and polls the card for a batch of packets, then turns interrupts back on once the queue is empty. Linux calls this scheme NAPI. Interrupts suit quiet devices; polling wins when the work never stops.
The driver reaches the device's registers through memory-mapped I/O (the registers show up at physical addresses, so ordinary loads and stores reach them) or, on x86, through dedicated port I/O instructions. For bulk data such as disk blocks or network packets, the device uses DMA (direct memory access) to copy straight into RAM, and the interrupt only arrives once the transfer is done.
The same mov instruction can end up in two very different places. Step through a store to RAM, then a store to a network card's register, then what the card does with that command:
Two stores, two destinations
The address is ordinary RAM, mapped write-back cacheable. The store lands in the CPU's cache and the instruction is finished as far as the program can tell. DRAM gets the new value later, when the cache line is evicted, and no device ever sees this write.
Worked Example: What cat /dev/hello Triggers
On Linux the "standard interface" for a character device is a structure of function pointers called struct file_operations. A driver fills in the operations it supports and registers itself. Here's a minimal driver for a pretend device that always reads back hello\n:
#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/module.h>
static const char msg[] = "hello\n";
static ssize_t hello_read(struct file *f, char __user *buf, size_t len, loff_t *off){ /* Copy from kernel memory into the user's buffer, starting at *off, and advance *off by however many bytes were copied. */ return simple_read_from_buffer(buf, len, off, msg, sizeof msg - 1);}
static const struct file_operations hello_fops = { .owner = THIS_MODULE, .read = hello_read,};
static struct miscdevice hello_dev = { .minor = MISC_DYNAMIC_MINOR, .name = "hello", /* appears as /dev/hello */ .fops = &hello_fops,};
module_misc_device(hello_dev);MODULE_LICENSE("GPL");Now type cat /dev/hello. Step by step:
catcallsopen("/dev/hello"). The kernel sees a device file, finds the driver registered for it and returns file descriptor 3.catcallsread(3, buf, 131072)(its buffer size here is illustrative). The kernel's genericreadcode findshello_fopsattached to the open file and callshello_readwithlen = 131072and*off = 0.- Six bytes are available from offset 0, so
simple_read_from_buffercopieshello\nintocat's buffer, sets*offto 6 and returns 6. The copy goes through a helper becausebufis a user-space address, and the kernel must check it before writing there. catwrites those 6 bytes to the terminal and callsreadagain.
Predict: what does hello_read return on that second call, and why does cat stop?
It returns 0. *off is now 6, which is already the end of the 6-byte message, so there's nothing left to copy. A read that returns 0 means end of file, and cat treats that as its cue to close the descriptor and exit. A driver that forgot to advance *off would hand back hello\n on every call, and cat would print it forever.
The kernel's file-handling code never learned anything about this device. It called .read through a pointer, exactly as it would for a disk or a serial port.
Drivers Run in Kernel Space
Because drivers need direct access to hardware, they typically run in kernel space with full privileges. Powerful, dangerous: a bug in a driver can topple the entire system. Driver bugs are, in fact, one of the most common causes of OS crashes.
This is one reason microkernels push drivers out to user space — a crashing driver becomes a crashing process, not a crashing kernel. Most mainstream operating systems still run most drivers in kernel space anyway, for the speed, though they offer user-space routes for some devices: Windows has its User-Mode Driver Framework (UMDF), and Linux lets file systems run under FUSE and USB drivers run through libusb.
Kernel privileges also make drivers a favourite target. A signed but buggy driver that lets user programs read or write arbitrary kernel memory is a gift to an attacker, and "bring your own vulnerable driver" attacks load exactly such a driver to get into ring 0. The security course returns to this in Windows Kernel Security.
The Full Picture
Press a key on your keyboard. The keyboard controller fires an interrupt. The CPU jumps to the keyboard driver's interrupt handler. The driver reads the keypress data and hands it up to the OS, which delivers it to whichever application has focus. All of it, in microseconds — totally invisible to you.