I/O Devices & Storage
I/O System Architecture

Buses
- Data paths that used to communicate information with the CPU(s), RAM and I/O Devices.
- CPU is attached to the main memory of the system via a memory bus.
- Some devices are connected to the system via a general I/O bus.
I/O Bus
- data path that connects CPU to I/O Devices.
- I/O Device is connected to I/O bus by three hardware components:
- I/O ports
- interfaces
- device controllers
Canonical Device

Hardware Interface
Allows the system software to control its operation
Operating system can control the device behavior by reading and writing the following registers:
status register
- see the current status of the device
command register
- tell the device to perform a certain task
data register
- pass data to the device or get data from the device
Tipical interaction example:
1
2
3
4
5
6
7while ( STATUS == BUSY) ; // wait until device is not busy
// write data to data register
// write command to command register
// starts the device and executes the command
while ( STATUS == BUSY ) ; // wait until device is done with your request
Internals
- Implement the specific operations
General Mechanisms
Polling
Operating system waits until the device is ready by repeatedly reading the status register.
Advantages:
- simple and it works.
Disadvantages:
- it wastes CPU time for just waiting for the device,
so switching to another process during the waiting time may be a better utilize of CPU.
- it wastes CPU time for just waiting for the device,

Interrupt
Put the I/O request process to sleep and context switch to another process.
When the device is finished, wake the process (waiting for the device finished) by interrupt.
Advantages:
- allow CPU and the disk are properly utilized.
Noted: interrupt is not always the best
- Because context switch is expensive, if the device performs very fast, then interrupt will slow down the system.
- If the device is fast, then polling is the best
- Otherwise, interrupt is the best
- Because context switch is expensive, if the device performs very fast, then interrupt will slow down the system.

Direct Memory Access
Problems:
- CPU wastes a lot of time to copy a large chunk of data from memory to the device.

- CPU wastes a lot of time to copy a large chunk of data from memory to the device.
Solution: Direct Memory Access DMA
- Copy data in memory by knowing the location of data in memory and the number of data block need to copy.
- When the copying finished, DMA raises an interrupt on the disk.

Device Interacftions
The communications between operating system and device
I/O Instructions
- a way for the OS to send data to specific device registers
- e.g.,
inandoutinstructions on x86.
Memory-mapped I/O
- a specific area inside the memory for the I/O device (those device registers are available as memory locations).
- i.e., the OS
load(read) andstore(write) to the device, instead of the main memory.
File System Abstraction
The abstraction excapsulate the specifics of device interaction which lets the OS can interact with different specific interfaces.

Problems:
- If there is a device having many special undefined functions, then these functions will go unused in the generic interface layer
- Over 70% of OS code is found in device drives
- it takes lots of times to drive the devices
- those devices are the primary contributors to kernel crashes
Simple IDE Disk Driver
Registers
Control Register
- Address
0x3F6 = 0X80 (0000 1RE0):R= resetE = 0means “enable interrupt”
- Address
Command Block Registers
- Address
0x1F0= Data Port - Address
0x1F1= Error - Address
0x1F2= Sector Count - Address
0x1F3= LBA low byte - Address
0x1F4= LBA mid byte - Address
0x1F5= LBA hi byte - Address
0x1F6 = 1B1D TOP4LBA: B = LBA, D = drive - Address
0x1F7= Command / Status
- Address
Status Register (Address
0x1F7):7:BUSY6:READY5:FAULT4:SEEK3:DRQ2:CORR1:IDDEX0:ERROR
Error Register (Address
0x1F1)7:BBK(Bad Block)6:UNC(Uncorrectable data error)5:MC(Media Changed)4:IDNF(ID mark Not Found)3:MCR(Media Change Requested)2:ABRT(Command aborted)1:T0NF(Track 0 Not Found)0:AMNF(Address Mark Not Found)
Operations
Wait for drive to be ready
- Read Status Register until drive is not busy and
READY.
- Read Status Register until drive is not busy and
Write parameters to command registers
- Wite the sector count, logical block address (LBA) of the sectors to be accessed, and drive number (
master=0x00orslave=0x10, as IDE permits just two drives) to command registers (0x1F2-0x1F6).
- Wite the sector count, logical block address (LBA) of the sectors to be accessed, and drive number (
Start the I/O
- Issuing read/ write to command register.
- Write
READ—WRITEcommand to command register (0x1F7).
Data transfer (for writes)
- Wait until the drive status is
READYandDRQ, write data to data port.
- Wait until the drive status is
Handle interrupts
- In the simplest case, handle an interrupt for each sector transferred.
- More complex approaches allow batching.
Handle errors
- After each operation, read the status register. If the
ERRORbit is on, read the error register for details.
- After each operation, read the status register. If the


