1 COS 318: Operating Systems I/O Device and Drivers 2 Input and Output A computer’s job is to process data Computation (CPU, cache, and memory) Move data into and out of a system (between I/O devices and memory) Challenges with I/O devices Different categories: storage, networking, displays, etc. Large number of device drivers to support Device drivers run in kernel mode and can crash systems Goals of the OS Provide a generic, consistent, convenient and reliable way to access I/O devices As device-independent as possible Don’t hurt the performance capability of the I/O system too much 3 Revisit Hardware Compute hardware CPU and caches Chipset Memory I/O Hardware I/O bus or interconnect I/O controller or adaptor I/O device Two types of I/O Programmed I/O (PIO) • CPU does the work of moving data Direct Memory Access (DMA) • CPU offloads the work of moving data to DMA controller CPU Memory I/O bus CPU CPU CPU Network 4 Definitions and General Method Overhead Time that the CPU is tied up initiating/ending an operation Latency Time to transfer one bit (typ. byte) Overhead + 1 bit reaches destination Bandwidth Rate of I/O transfer, once initiated Mbytes/sec General method Higher level abstractions of byte transfers Batch transfers into block I/O for efficiency to amortize overhead and latency over a large unit Initiate Data transfer2 5 Programmed Input Device Device controller Status register ready: tells if the host is done busy: tells if the controller is done int: interrupt … Data registers A simple mouse design Put (X, Y) in data registers on a move Interrupt Input on an interrupt Read values in X, Y registers Set ready bit Wake up a process/thread or execute a piece of code 6 Programmed Output Device Device Status registers (ready, busy, … ) Data registers Example A serial output device Perform an output Wait until ready bit is clear Poll the busy bit Writes the data to register(s) Set ready bit Controller sets busy bit and transfers data Controller clears the ready bit and busy bit 7 Direct Memory Access (DMA) DMA controller or adaptor Status register (ready, busy, interrupt, …) DMA command register DMA register (address, size) DMA buffer Host CPU initiates DMA Device driver call (kernel mode) Wait until DMA device is free Initiate a DMA transaction (command, memory address, size) Block Controller performs DMA DMA data to device (size--; address++) Interrupt on completion (size == 0) Interrupt handler (on completion) Wakeup the blocked process 8 I/O Software Stack User-Level I/O Software Device-Independent OS software Device Drivers Interrupt handlers Hardware3 9 Recall Interrupt Handling Save context (registers that hw hasn’t saved, PSW etc) Mask interrupts if needed Set up a context for interrupt service Set up a stack for interrupt service Acknowledge interrupt controller, perhaps enable it Save entire context to PCB Run the interrupt service Unmask interrupts if needed Possibly change the priority of the process Run the scheduler Then OS will set up context for next process, load registers and PSW, start running process … 10 I/O System Device Drivers Rest of the operating system Device driver Device driver . . . Device driver Device controller Device controller . . . Device controller Device Device Device Device Interrupt Handling Manage the complexity and differences among specific types of devices (disk vs. mouse, different types of disks …) Each handles one type of device or small class of them (eg SCSI) 11 Typical Device Driver Design Operating system and driver communication Commands and data between OS and device drivers Driver and hardware communication Commands and data between driver and hardware Driver responsibilities Initialize devices Interpreting commands from OS Schedule multiple outstanding requests Manage data transfers Accept and process interrupts Maintain the integrity of driver and kernel data structures Simplified Device Driver Behavior Check input parameters for validity, and translate them to device-specific language Check if device is free (wait or block if not) Issue commands to control device Write them into device controller’s registers Check after each if device is ready for next (wait or block if not) Block or wait for controller to finish work Check for errors, and pass data to device-indept software Return status information Process next queued request, or block waitng for next Challenges: Must be reentrant (can be called by an interrupt while running) Handle hot-pluggable devices and device removal while running Complex and many of them; bugs in them can crash system 124 Types of I/O Devices Block devices Organize data in fixed-size blocks Transfers are in units of blocks Blocks have addresses and data are therefore addressable E.g. hard disks, USB disks, CD-ROMs Character devices Delivers or accepts a stream of characters, no block structure Not addressable, no seeks Can read from stream or write to stream Printers, network interfaces, terminals Like everything, not a perfect classification E.g. tape drives have blocks but not randomly accessed Clocks are I/O devices that just generate interrupts 13 Typical Device Speeds Keyboard Mouse Compact Flash card USB 2.0 52x CD-ROM Scanner 56K modem 802.11g wireless net Gigabit Ethernet FireWire-1 FireWire 800 SCSI Ultra-2 disk SATA disk PCI bus Ultrium tape 14 10 B/s 100 B/s 40 MB/s 60 MB/s 7.8 MB/s 400 KB/s 7 KB/s 6.75 MB/s 320 MB/s 50 MB/s 100 MB/s 80 MB/s 300 MB/s 528 MB/s 320 MB/s 15 Device Driver Interface
View Full Document