Contents (8)
[Operating System] I/O Systems
Hardware alternates between computation and I/O work. For example, even when you're editing a file, the I/O work of input coming in from the keyboard happens at the same time as the computation that writes the information into the file. Today, I'm going to explain how the OS handles this I/O system.
1 I/O Hardware
OS designers have to make it possible to manage a huge variety of I/O devices that can be connected to a computer. So the thing that lets all these very different devices interact with the OS through a consistent interface is the device driver. The device driver provides a consistent interface to the OS's I/O Subsystem, and it's the same concept as providing a standard interface between apps and the operating system through System Calls.
I/O hardware includes storage devices (disk), transmission devices (network, bluetooth), human-interface devices (monitor, keyboard, mouse, audio), and so on. As for how these devices communicate with each other, let me first go over the terms we'll need for the explanation.
bus: a bundle of wires shared by multiple devices, plus a communication protocol
daisy chain: a way of connecting devices one after another in sequence (connecting A->B, B->C in a chain)
PCIe (PCI Express): a high-speed serial bus used to connect peripherals (memory, graphics cards, etc.)
SAS (Serial-Attached SCSI): a serial bus standard for connecting high-speed storage devices
Lane: a channel for sending and receiving data in PCIe; a single lane is made up of 2 pairs of wires (one for receiving, one for sending)
1.1 Physical Connection of I/O Hardware
Let's look at how I/O hardware is physically connected so that the pieces can communicate with each other. Below is the bus structure of a typical PC.

From the picture, you can see that the Controllers are connected to the PCIe bus and can communicate through it. PCIe is a high-speed serial bus; in the old days buses were parallel, but they were hard to control and actually ended up slower, so the trend is to go serial. A Controller is an electronic circuit used to control ports, buses, and devices, and it can even do its own computation and caching. And expansion devices that would be inefficient to hook up to a relatively fast bus (slow devices, ex. keyboard, mouse) are connected through a separate expansion bus.
1.2 Memory-Mapped I/O
Now let's look at how I/O transfers work, that is, how data and commands get exchanged with the controller. To put it simply first, the processor performs I/O work by passing commands and data through reading and writing bit patterns in one or more registers in the controller.
And there are 2 ways to pass commands and data to this I/O.
Sending data to a port address using special I/O instructions
Using memory-mapped I/O to include device registers in the memory address space and accessing them with ordinary memory instructions
The table below is a partial example showing which device is mapped to which memory address.
I/O address range (hexadecimal) | Device |
000-00F | DMA controller |
020-021 | interrupt controller |
040-043 | timer |
200-20F | game controller |
2F8-2FF | serial port (secondary) |
320-32f | hard-disk controller |
378-37F | parallel port |
3D0-3DF | graphics controller |
3F0-3F7 | diskette-drive controller |
3F8-3FF | serial port (primary) |
You can think of it as devices being mapped to memory addresses like the ones above, and data being sent like this.
OUT 0x60, AL ; Uses the CPU's special OUT instruction to send the value in the AL register to the keyboard controller (address 0x60)*(volatile unsigned int *)0xFFFF0000 = 0x1; // Writes 1 to some device allocated in memory to make it operateHowever, communicating through I/O instructions isn't used much anymore. That's because when you display a screen on a monitor with a graphics card, writing millions of bytes directly into graphics memory is faster than having the graphics controller write data into lots of memory-mapped regions to turn it into values to display and then having the graphics controller output those values to the monitor, so these days most I/O work uses the memory-mapped I/O approach.
And below is the register layout for device control in the memory-mapped I/O approach.
data-in register
Read by the host to receive input data
data-out register
Written by the host to send output data to the device
status register
Read by the host to check the device's state
e.g., whether a command has completed, whether there's data to read, whether an error occurred, etc.
control register
Written by the host to start a command or change the device's mode
e.g., settings such as half/full duplex, whether to do parity checking, word length, communication speed, etc.
Data registers are usually 1 to 4 bytes in size, and some controllers have a FIFO that can temporarily store several bytes, holding them for a while until the device or host is able to process the data.
1.3 How the CPU Gives Commands to a Device - The Polling Approach
To send data to the controller, the CPU needs to know whether the device is ready. There are two ways of doing that: the Polling approach and the Interrupt approach.
Polling is when the CPU (host) keeps checking the state of the device (controller), repeatedly reading the Status register until the device is ready. And the Handshaking process through which the host sends output data to the controller via this Polling process goes like this.
The host keeps checking the busy bit
→ When the controller is busy, the busy bit is 1.
→ When the controller is ready, it becomes 0.
→ The host waits until the busy bit becomes 0.The host prepares to send data
→ Writes one byte of data to the data-out register.
→ Sets the write bit in the command register.The host sets the command-ready bit
→ Signals that the controller can now process it.The controller detects command-ready and sets the busy bit
→ Sets the busy bit to 1 to indicate it's working.The controller executes the command
→ Reads the command register and checks whether it's a write command.
→ Reads the byte from data-out and outputs it to the device.The controller resets its state after finishing the work
→ Resets the command-ready bit to 0.
→ Sets the error bit to 0 to signal that there was no error.
→ Also sets the busy bit to 0 to signal completion.
The process above is repeated for every byte. In other words, the handshaking process above is repeated to send each byte one at a time, in order. And since it has to keep checking whether the device is ready, it has to wait. (The device ends up in a busy-waiting, polling state)
1.4 CPU to Device - The Interrupt Approach
The Interrupt approach is when the device sends a notification (Interrupt) directly to the CPU once it's ready. This approach is considered more efficient because the CPU can do other work and only handle the device when it's ready. In the Interrupt approach, the CPU has an interrupt-request line, and every time it executes an instruction, it senses the interrupt-request line and runs the interrupt-handler routine. So when a device sends a signal on the interrupt-request line to raise an interrupt, the CPU can detect it. (device raises an interrupt => CPU senses the interrupt-request line => interrupt is handed off to the interrupt-handler => interrupt handler processes it => the original work resumes)

To sum up, interrupt-driven I/O is a mechanism that lets the CPU respond to asynchronous events.
1.5 Direct Memory Access(DMA)
For devices that handle large amounts of data, like disk drives, it's way too inefficient for the CPU to receive the data in 4kb chunks (fixed units) hundreds of thousands of times and load it into memory. (Or, just imagine sending a 4mb file and having the CPU receive and process it 4kb at a time, more than 1000 times. That's really inefficient.) So a new approach called DMA was introduced to load large amounts of data into memory. In this approach, the drive controller, instead of the CPU, transfers the data directly into memory with a DMA transfer.
When the CPU passes the drive controller a command block asking it to transfer x bytes to address y, the drive controller loads the x bytes directly into memory at address y through a DMA transfer. And while the device controller is doing the DMA transfer, the CPU can do other work. Then, when the DMA controller finishes its job, it notifies the CPU of completion using an Interrupt.

Wrapping Up
Now let me sum up Memory-Mapped I/O, Polling, Interrupt, and DMA in one table!!
Memory-Mapped I/O, DMA, Direct CPU Copy
Flow
Memory-Mapped I/O (for control)
CPU -> controller register
Direct CPU copy
Controller register-> CPU -> system memory
DMA (for data)
Controller register -> system memory
Polling vs Interrupt
Description
Polling
The CPU keeps checking the device's state
Interrupt
The hardware notifies the CPU on completion/error
That makes sense right away, doesn't it? But aren't you curious how the CPU writes data to controller registers with Memory-Mapped I/O?? Apparently, in the past, a Memory Controller was used whenever the CPU exchanged data with Memory, and this Memory Controller could look at the address value and send it on its own to the right device over PCIe, while these days the address just gets interpreted automatically and the command is passed to the I/O Controller.
