W10 - I-O and Device Drivers
Class: CSCE-410-02159
Notes:
I/O Devices
- A computer system is composed of processors, main memory and peripherals or Input / Output devices
- One of the tasks of the OS is to manage the I/O Devices
- Interface between I/O devices and the rest of the system
/CSCE-410-02159/Visual%20Aids/image-36.png)
I/O Device Classification
-
Block Devices
- Stores information in fixed-size blocks (512 to 65536 bytes)
- Each block can be read/written independently
- E.g. Hard disks, blu-ray disks, USB flash drives
-
Character Devices
- Processes information in single characters (1 byte)
- E.g. Printers, network interfaces, mouse
-
Other
- Some devices do not fit the above classification
- E.g. Clocks, memory-mapped screens
-
Dedicated Devices
- Can only be used by one process at a time (e.g. printer)
-
Shared Devices
- Can be used by many processes in parallel (e.g. hard disk
Device Controllers
- A chip or PCB that interfaces the hardware with the rest of systems
- The controller knows how to talk with the hardware
- The OS communicates with the controller
- Controllers may handle multiple devices
- The interface between the controller and the hardware can be a standard (ANSI, IEEE, ISO) or a de facto protocol
CPU/Controller Communication Principles
- Each device controller has a number of registers
- The CPU talks with the device by reading/writing the registers
- Types of registers
- Status registers (read-only)
- Command registers (write-only)
- Configuration registers (read/write)
- Data registers or buffers (read/write)
Addressing I/O Registers
- Separate I/O and memory address space
- Each I/O register is assigned an I/O port number
- Communication is based on special CPU instructions (in, out)
- Memory-mapped I/O
- I/O registers are mapped in memory space
- Standard memory instructions work on I/O registers
- Hybrid
- Data buffers/registers are memory-mapped
- Control registers are on a separate I/O space
Memory-mapped I/O
- How does it work?
- Each memory unit or peripheral device services a range of addresses
- They compare the requested address and respond only if it falls in their range
- Separate vs Memory-mapped I/O?
- Special instructions require the use of assembly, memory-mapped I/O registers can be addressed as C variables
- Memory-mapped I/O gives flexibility in access permissions (OS can put or not these addresses in the user virtual address space)
- Every instruction that can reference memory is compatible
Disadvantages of Memory-mapped I/O
- Caching a status register would be a disaster
- Need for selectively disable caching
- Challenging in systems with multiple buses
- Send request to memory then try other buses if this fails
/CSCE-410-02159/Visual%20Aids/image-37.png)
Direct Memory Access (DMA)
- A DMA controller is a peripheral (chip) that does one thing:
- Transfer data
- In separate or memory-mapped I/O the CPU transfers the data
- Alternatively, the CPU can ask the DMA controller to do it and do something else
/CSCE-410-02159/Visual%20Aids/image-38.png)
More about DMA
- Simple DMA controllers handle one data transfer at a time
- More sophisticated have multiple channels and can handle multiple data transfers
- Cycle stealing mode
- DMA acquires the bus for each word
- Burst mode
- DMA acquires the bus once and transfers multiple words
- More efficient but may block the CPU for long time
- Fly-by mode
- DMA tells device controller to transfer data directly to memory
- Alternatively, data is transferred to the DMA controller first
- Less efficient, but supports memory-to-memory and device-to-device transfers
DMA: Any Disadvantages?
- DMA is slower than the CPU
- It makes little sense when main CPU is idle
- In embedded systems, getting rid of the DMA saves money
- However, DMA is valuable in battery-powered embedded systems, as it is more energy-efficient than a CPU
Interrupts
- Device controllers can be programmed to issue interrupts on events
- Interrupts are implemented by changing the state of a bus line
- This change of state is detected by the interrupt controller
- The interrupt controller handles interrupt for multiple devices
- An interrupted CPU acknowledges the interrupt to indicate that it is ready to accept a new interrupt
/CSCE-410-02159/Visual%20Aids/image-39.png)
Precise and Imprecise Interrupts
- Challenge: When an interrupt occurs many instructions can be at various stages of execution
- Fetch unit -> Decode unit -> Execute unit
- A precise interrupt leaves the system at a well-defined state
- All instructions before the PC have been completed
- No instruction after the PC have been completed
- The execution state of the instruction pointed by the PC is known
- An imprecise interrupt does not meet these requirements
- Require to save a lot of information in the stack
/CSCE-410-02159/Visual%20Aids/image-40.png)
I/O Software: Programmed I/O
- CPU does all the work in a blocking manner (polling / busy waiting)
- Example: Print a text file
- Copy first character from memory to printer’s data register
- Read printer’s status register in a while loop until printer is done
- Repeat until the end of the document
- Disadvantages?
- Occupies CPU
- Energy waste
copy_from_user(buffer, p, count); /* p is the kernel buffer */
for (i = 0; i < count; i++) { /* loop on every character */
while (*printer_status_reg != READY) ; /* loop until ready */
*printer_data_register = p[i]; /* output one character */
}
return_to_user();
/CSCE-410-02159/Visual%20Aids/2026-11-28_18-50-47.png)
I/O Software: Interrupt-Driven I/O
- CPU does all the work in a non-blocking manner
- Example: Print a text file
- Enable/configure interrupts
- Copy first character from memory to printer’s data register
- Down a semaphore and sleep
- When printer is done it issues an interrupt
- The interrupt handler wakes up the process and acks the interrupt
- Copy next character until the end of file
- (alternatively, we can copy the next character from the interrupt handler and up the semaphore once at the end)
- Disadvantages?
- CPU still does all the work (albeit without waiting meaninglessly)
I/O Software: I/O Using DMA
- DMA does all the work
- Example: Print a text file
- Enable/configure interrupts
- Copy memory address, count, channel to DMA’s control registers
- Down a semaphore and sleep
- When DMA is done with the transfer it issues an interrupt
- The interrupt handler wakes up the process and acks the interrupt
- DMA reduces the number of interrupts from one per character to one per buffer
- But, DMA is slower than the CPU
I/O Software Levels
- User-level I/O software
- Device-independent operating system software
- Device drivers
- Interrupt handlers
- Hardware
Device Drivers
- Code that controls the I/O device
- Communicates with the device controller by writing/reading the device registers
- Provides API for initialising and using the I/O device
- Typically provided by the hardware manufacturer
- One for each supported OS
- Device drivers can be layered like the TCP/IP stack (e.g. USB)
- (Typically) device drivers are part of the kernel
/CSCE-410-02159/Visual%20Aids/image-41.png)
Device-Independent I/O Software
- Some I/O software is device-independent
- Efficiency, provide uniformity, avoid code replication
- Uniform interface for device drivers (Hardware Abstraction Layer)
- OS defines an API for common operations
- Device manufactures must make drivers that adhere to the API
- Advantage: Applications use the same API on all devices
- Disadvantage: Applications cannot access particular niche features
- Allocating and releasing dedicated devices
- Ensure proper locking to avoid race conditions
- Error handling (programming errors, hardware errors) and reporting
- Buffering
User-Space I/O Software
- I/O software that lives in user-space
- Background services (daemons)
- Libraries that are linked with the user programmes
- Spooling
- Suppose we have a dedicated I/O device (e.g. printer)
- Is it OK to give to the user access to the acquire/release API?
- The user may acquire a device and never release it
- Only a daemon process has the permissions to acquire the device
- User applications print via the printer daemon
- Formatting (e.g. printf)
- System call APIs
Summary of I/O Software
/CSCE-410-02159/Visual%20Aids/image-42.png)
Clocks
- Clocks or Timers
- A peripheral vital for the operation of the Operating System
- Schedule timeouts, schedule periodic events, time of day, etc
- Preemptive scheduling is based on clock interrupts
- How do computers measure time?
Programmable Clock
- Crystal Oscillator
- An electronic component that generates a signal at a very precise frequency
- From KHz (low power oscillators) to GHz
- A counter that decrements at each pulse
- A register that holds the initial value
- A clock interrupt is generated when counter reaches zero
- Programmable by software
/CSCE-410-02159/Visual%20Aids/image-43.png)
Clock Software: Measuring Real Time
- Higher-level time applications build on clock interrupts
- Real time
- High granularity (absolute time in ticks) is expensive
- Alternatively, we could measure time in seconds (see Linux)
- Use ticks for high granularity within the second
- Alternatively, we could measure time in ticks from time of boot
- Computers use batteries and low-power circuitry to keep time when powered off
/CSCE-410-02159/Visual%20Aids/image-44.png)
Clocks: Modes of Operation
- One-off mode
- Clock starts with initial value and issues an interrupt when zero
- Good for one-off timeouts, alarms
- Square-wave mode (or periodic mode)
- Clock automatically restarts after interrupt is issued
- Good for periodic events
- Clock ticks are these periodic clock interrupts
Clock Software: Timeouts and Alarms
- Time-based events
- E.g. preemptive scheduling, networking
- Multiple processes need to share one physical clock
- Solution: Virtual clocks
/CSCE-410-02159/Visual%20Aids/image-45.png)
Clock Software: Watchdog Timer
- A safety mechanism against system crashes
- A watchdog timer resets the system when it expires
- The OS periodically resets the timer, so that it never expires
- If the OS hangs, the watchdog timer will reset the system
Clock Drift
-
Crystal oscillators are precise but not perfect
-
What are the main sources of clock drift?
-
Production spread
- Manufacturing imperfections cause a frequency variance
- This error is measure in ppm (parts per million) or ppb
- E.g. 32KHz ±20ppm is equivalent to an operating frequency in
[32,767.34464 Hz, 32,768.65536 Hz] - 1 ppm drift is desync by 1 ms every 1,000 sec (16.6 minutes!)
-
Temperature dependence
- Crystal oscillators are calibrated to yield the nominal frequency at room temperature
- The slow down at very low and very high temperature following a parabolic pattern
Empirical Clock Drift:
/CSCE-410-02159/Visual%20Aids/image-46.png)