COS 318: Operating Systems I/O Device Interactions and Drivers
Total Page:16
File Type:pdf, Size:1020Kb
Topics COS 318: Operating Systems u So far: l Management of CPU and concurrency l Management of main memory and virtual memory I/O Device Interactions and u Next: Management of the I/O system Drivers l Interacting with I/O devices l Device drivers Jaswinder Pal Singh l Storage Devices Computer Science Department Princeton University u Then, File Systems l File System Structure (http://www.cs.princeton.edu/courses/cos318/) l Naming and Directories l Efficiency/Performance l Reliability and Protection 2 Input and Output Revisit Hardware u A computer u Compute hardware CPU l CPU cores and caches CPU l Computation (CPU, memory hierarchy) CPUCPU CPU l Memory $ l Move data into and out of a system (locketween I/O devices Chip l I/O and memory hierarchy) l Controllers and logic Memory I/O bridge u Challenges with I/O devices I/O bus l Different categories with different characteristics: storage, u I/O Hardware networking, displays, keyboard, mouse ... l I/O bus or interconnect l Large number of device drivers to support l I/O device l I/O controller or adapter l Device drivers run in kernel mode and can crash systems • Often on parent board Network u Goals of the OS • Cable connects it to device • Often using standard interfaces: IDE, l Provide a generic, consistent, convenient and reliable way to SATA, SCSI, USB, FireWire… access I/O devices • Has registers for control, data signals • Processor gives commands and/or l Achieve potential I/O performance in a system data to controller to do I/O • Special I/O instructions (w. port addr.) 3 or memory mapped I/O 4 1 I/O Hierarchy A typical PC bus structure u As with memory, fast I/O with less “capacity” near CPU, slower I/O with greater “capacity” further away 5 Performance Characteristics Interacting with Devices u Overhead u A device has an interface, and an implementation l CPU time to initiate an operation l Interface exposed to external software, typically by device u Latency controller Initiate Data transfer l Time to transfer one bit l Implementation may be hardware, firmware, software l Overhead + time for 1 bit to reach destination Time u Mechanisms u Bandwidth l Programmed I/O (PIO) l Rate at which subsequent bits are Device Transfer rate l Interrupts transferred or reach destination Keyboard 10Bytes/sec l Bits/sec or Bytes/sec Mouse 100Bytes/sec l Direct Memory Access (DMA) u In general … … 10GE NIC 1.2GBytes/sec l Different transfer rates l Abstraction of byte transfers l Amortize overhead over block of bytes as transfer unit 7 8 2 Programmed I/O Polling in Programmed I/O u Example u Wait until device is not “busy” l RS-232 serial port CPU l A polling loop u Simple serial controller l May also poll to wait for device to complete its work l Status registers (ready, busy, … ) Memory u Advantages l Data register I/O Bus u Output l Simple CPU: Serial u Disadvantage l Wait until device is not “busy” Busy Ready … controller l Slow l Write data to “data” register Data l Waste CPU cycles l Tell device “ready” Device u Example l Wait until “ready” l If a device runs 100 operations / second, CPU may need to l Clear “ready” and set “busy” wait for 10 msec or 10,000,000 CPU cycles (1Ghz CPU) l Take data from “data” register l Clear “busy” 9 10 Interrupt-Driven Device Interrupt Handling Revisited/Refined u Allows CPU to avoid polling u Save more context u Example: Mouse CPU u Mask interrupts if needed u Simple mouse controller l Status registers (done, int, …) u Set up a context for interrupt service l Data registers (ΔX, ΔY, button) Memory I/O Bus u Set up a stack for interrupt service u Input Mouse: u Acknowledge the interrupt controller, enable it if needed Mouse l Wait until “done” u Save context to PCB l Store ΔX, ΔY, and button into Done Int … controller data registers ΔX u Run the interrupt service l Raise interrupt ΔY u Unmask interrupts if needed CPU (interrupt handler) Button l Clear “done” u Possibly change the priority of the process l Move ΔX, ΔY, and button into kernel buffer u Run the scheduler l Set “done” l Call scheduler 11 3 Another Problem Direct Memory Access (DMA) u CPU has to copy data from memory to device u Takes many CPU cycles, esp for larger I/Os u Can we get the CPU out of the copying loop, so it can do other things in parallel while data are being copied? 13 Direct Memory Access (DMA) Where Are these I/O “Registers?” u Example of disk u Explicit I/O “ports” for devices I/O device u A simple disk adaptor l Status register (ready, …) l Accessed by privileged I/O device l DMA command instructions (in, out) … l DMA memory address and size CPU l DMA data buffer u Memory mapped I/O Kernel ALU/FPU u DMA Write Kernel memory Memory l A portion of physical memory CPU: registers l Wait until DMA device is “ready” Data I/O Bus for each device l Clear “ready” User Caches l Set DMAWrite, address, size l Advantages Disk memory l Set “start” • Simple and uniform Memory Ready Start Int … adaptor l Block current thread/process • CPU instructions can access Mapped Disk adaptor: DMA Command these “registers” as memory I/O l DMA data to device address size (size--; address++) l Issues Memory l Interrupt when “size == 0” Data CPU (interrupt handler): Data • These memory locations should l Put the blocked thread/process into DMA buffer not be cached. Why? ready queue • Mark them not cacheable Disk: Move data to disk u Both approaches are used 15 16 4 Device I/O port locations on PCs (partial) I/O Software Stack User-Level I/O Software Device-Independent OS software Device Drivers Interrupt handlers Hardware I/O Interface and Device Drivers I/O Interface and Device Drivers u I/O system calls encapsulate device behaviors in Device Device generic classes Device driver controller u Device-driver layer hides differences among I/O controllers from kernel Device Device Device Rest of the driver controller u Devices vary in many dimensions operating l Character-stream or block . system . l Sequential or random-access Device InterruptHandling l Sharable or dedicated Device Device l Speed of operation driver controller Device l Read-write, read only, or write only Drivers Operating System Hardware 19 5 Example Kernel I/O Structure Characteristics of I/O Devices What Does A Device Driver Do? Device Driver Operations u Provide “the rest of the OS” with APIs u Init ( deviceNumber ) l Init, Open, Close, Read, Write, … l Initialize hardware u Interface with controllers u Open( deviceNumber ) l Commands and data transfers with hardware controllers l Initialize driver and allocate resources u Driver operations u Close( deviceNumber ) l Initialize devices l Cleanup, deallocate, and possibly turnoff l Interpret outstanding requests u Device driver types l Manage data transfers l Character: variable sized data transfer l Accept and process interrupts l Block: fixed sized block data transfer l Maintain the integrity of driver and kernel data structures l Terminal: character driver with terminal control l Network: streams for networking 23 24 6 Character and Block Interfaces Network Devices u Character device interface (keyboard, mouse, ports) u Different enough from the block & character devices to l read( deviceNumber, bufferAddr, size ) have own interface • Reads “size” bytes from a byte stream device to “bufferAddr” l write( deviceNumber, bufferAddr, size ) u Unix and Windows/NT include socket interface • Write “size” bytes from “bufferAddr” to a byte stream device l Separates network protocol from network operation u Block device interface (disk drives) u Approaches vary widely (pipes, FIFOs, streams, l read( deviceNumber, deviceAddr, bufferAddr ) queues, mailboxes) • Transfer a block of data from “deviceAddr” to “bufferAddr” l write( deviceNumber, deviceAddr, bufferAddr ) • Transfer a block of data from “bufferAddr” to “deviceAddr” l seek( deviceNumber, deviceAddress ) • Move the head to the correct position • Usually not necessary 25 Clocks and Timers Unix Device Driver Entry Points u Provide current time, elapsed time, timer u init() l Initialize hardware u start() l Boot time initialization (require system services) u if programmable interval time used for timings, periodic u open(dev, flag, id)and close(dev, flag, id) interrupts l Initialization resources for read or write and release resources u halt() l Call before the system is shutdown u ioctl (on UNIX) covers odd aspects of I/O such as u intr(vector) clocks and timers l Called by the kernel on a hardware interrupt u read(…) and write() calls l Data transfer u poll(pri) l Called by the kernel 25 to 100 times a second u ioctl(dev, cmd, arg, mode) l special request processing 28 7 Synchronous and Asynchronous I/O Synchronous Blocking Read u Synchronous I/O Application Kernel HW Device l Calling process waits for I/O call to return before doing anything l Blocking I/O Switch to syscall Kernel context • Read() or write() will block a user process until its completion • Easy to use and understand Driver Initiates • OS overlaps synchronous I/O with another process DMA read block l Nonblocking I/O DMA • Return as much data (and count of it) as avaialble right away read Interrupt u Asynchronous I/O Copy to l Process runs while I/O executes User buf l Let user process do other things before I/O completion Switch to Unblock l I/O completion will notify the user process user context returnl 29 30 Synchronous Blocking Read Asynchronous Read u A process issues a read call which executes a system call Application Kernel HW Device u System call code checks for correctness and buffer cache Switch to u If it needs to perform I/O, it will issue a device