Sub topic 1
I/O Devices and Controllers
Prerequisites
This chapter assumes you know the following:
- What a process, a system call, and kernel mode versus user mode are.
- How a CPU, main memory, and interrupts fit together at a basic level.
- Binary numbers, powers of two, and conversions between KB, MB and GB.
If any of these feel shaky, review them first. Later chapters on polling, interrupts, DMA and disk scheduling build directly on the vocabulary introduced here.
Why the OS needs a model of I/O hardware
A computer is useful only if it can exchange data with the outside world: disks, keyboards, network cards, displays. These devices differ enormously in speed and behavior. A keyboard produces a few bytes per second. An NVMe drive moves gigabytes per second. The operating system must hide this variety behind a uniform interface while still using each device efficiently.
GATE CS questions in this area test three things: who talks to whom (CPU, controller, device), how the CPU addresses a device (port-mapped versus memory-mapped I/O), and how the register protocol between driver and controller works. This chapter covers all three.
Devices, controllers and device drivers
An I/O system has layers. Each layer hides detail from the one above it.
- A device is the physical hardware: a disk platter and arm, a keyboard matrix, a network cable.
- A device controller is the electronics that operates the device and connects it to the system. It may be a chip on the device, or a card in a slot, such as a network adapter. Controllers contain a small processor, a buffer, and registers.
- A bus is the shared set of wires that carries addresses, data and control signals between the CPU, memory and controllers.
- A device driver is kernel software that knows one controller's register layout and command set. It presents a standard interface to the rest of the OS.
The CPU never moves a disk arm directly. It writes commands into the controller's registers, and the controller performs the physical work.
Device registers
A controller exposes a handful of registers. The exact set varies, but four roles recur.
| Register | Direction | Purpose |
|---|---|---|
| Status | Read by CPU | Bits such as busy, data-ready, error |
| Control (command) | Written by CPU | Start a command, set the mode, enable interrupts |
| Data-in | Read by CPU | Bytes the device has produced |
| Data-out | Written by CPU | Bytes the CPU wants the device to consume |
A typical driver for a simple device follows a handshake:
- Read the status register until the busy bit is clear.
- Write the data to the data-out register.
- Set the command-ready bit in the control register.
- The controller sets the busy bit and does the work.
- When finished, the controller clears the command-ready bit and the busy bit.
Step 1 is a loop. How the CPU waits there is the subject of the next chapter, on polling, interrupts and DMA.
How the CPU addresses a device
The CPU needs a way to name a register. There are two classic schemes.
Port-mapped I/O
The processor has a separate I/O address space and special instructions to reach it. On x86, IN and OUT read and write a port number. A control line tells the bus whether an address refers to memory or to I/O.
- Memory addresses are not consumed by devices.
- The I/O instructions are privileged, which helps protection.
- Ordinary load and store instructions cannot touch device registers, so you cannot use C pointers directly.
Memory-mapped I/O
Device registers (and sometimes device buffers) are assigned addresses in the same address space as RAM. The CPU uses ordinary LOAD and STORE instructions.
- No special instructions are needed. Drivers can be written in C using pointers.
- Protection is done through the memory-management unit. The OS simply does not map the device addresses into user processes.
- Part of the address space is used up by devices. This matters on 32-bit systems with large graphics buffers.
- Registers must not be cached, or the CPU would read a stale status value.
| Property | Port-mapped | Memory-mapped |
|---|---|---|
| Address space | Separate | Shared with RAM |
| Instructions | Special (`IN`, `OUT`) | Ordinary loads and stores |
| Protection | Privileged instructions | Page tables |
| Caching concern | None for I/O space | Must mark region uncacheable |
Many real systems use both. A PC uses port I/O for legacy devices and memory-mapped I/O for graphics cards and modern controllers.
Why the caching rule matters
Suppose a status register is memory-mapped and the CPU caches it. A driver loops reading the busy bit. The first read goes to the device and fills a cache line. Every later read hits the cache and returns the same stale value, so the loop never sees the device finish. For this reason the OS marks device regions uncacheable. In C, a driver also declares such pointers volatile, so the compiler does not remove repeated reads as redundant.
#include <stdint.h>
#define STATUS_BUSY 0x01u
#define CMD_START 0x02u
/* Pointers to memory-mapped registers (addresses are illustrative). */
volatile uint32_t *const STATUS = (volatile uint32_t *)0x40000000u;
volatile uint32_t *const CMD = (volatile uint32_t *)0x40000004u;
volatile uint32_t *const DATA = (volatile uint32_t *)0x40000008u;
void write_byte(uint8_t b) {
while (*STATUS & STATUS_BUSY) {
/* wait: each iteration performs a real read of the register */
}
*DATA = b;
*CMD = CMD_START;
}The sketch shows the handshake from the previous section. It is not meant to run on a desktop, because those addresses are not mapped in a normal process.
Classifying devices
The OS groups devices by how data moves, since each class needs a different interface.
- Block devices transfer fixed-size blocks and allow random access by block number. Disks and SSDs are the main examples. Operations are
read,writeandseek. A file system sits on top. - Character devices transfer a stream of bytes with no random access. Keyboards, mice and serial ports are examples. Operations are
getandput. - Network devices are usually handled with a socket interface, because their behavior differs from both block and character devices.
Devices also differ in other ways that affect the driver:
- Sharing: a disk can serve many processes, while a printer needs exclusive use during a job.
- Speed: from bytes per second up to gigabytes per second.
- Direction: read-only (a CD-ROM), write-only (a display), or both.
- Transfer unit: one byte at a time or a block at a time.
Common errors
- Confusing the controller with the driver. The controller is hardware. The driver is kernel software.
- Saying memory-mapped I/O needs special instructions. It does not. That is the port-mapped scheme.
- Forgetting that memory-mapped registers must be uncached. A GATE question may ask what goes wrong otherwise. The answer is stale status values.
- Treating "busy" as a data bit. Status and control bits are protocol signals, not data.
- Assuming all devices are block devices. Keyboards and serial lines are character devices and have no seek.
Exercise
A system uses memory-mapped I/O. A driver polls a status register with a loop, but the loop never ends even though the device has finished. The register address is mapped through a cacheable page. Explain the cause and give two fixes.
Show answer
The first read of the status register loads its value into the CPU cache. Later reads hit the cache and return the stale "busy" value, so the CPU never sees the device's update. Fix 1: mark the page holding the device registers as uncacheable in the page table. Fix 2: in C, declare the register pointer volatile so the compiler issues a real read on every iteration. The uncacheable mapping is needed regardless, since volatile only controls the compiler and does not bypass the hardware cache.
Exercise
A 32-bit system has a 4 GB address space. Memory-mapped devices occupy 768 MB. What is the largest amount of RAM whose addresses fit, and would port-mapped I/O change the answer?
Show answer
4 GB is 4096 MB. Subtracting 768 MB leaves 3328 MB, which is 3.25 GB. With port-mapped I/O the devices use a separate I/O address space, so the full 4 GB of memory addresses would be available for RAM.