I/O Devices and DriversMechanicsGaining FlexibilityOverviewDefinitions & General MethodSlide 6Slide 7Programmed I/O “Slow” Input DeviceProgrammed I/O Output DeviceDirect Memory Access (DMA)Slide 11Device DriversDevice Driver Design IssuesDevice Driver InterfaceBlock Device InterfaceCharacter Device InterfaceUnix Device Driver Interface Entry PointsSlide 18Why BufferingDetailed Steps of Blocked ReadAsynchronous I/OOther Design IssuesDynamic Binding with An Indirect TableDynamic BindingThink About PerformanceI/O Devices and DriversVivek Pai / Kai LiPrinceton University2MechanicsI’ve figured out the next projectRegular precepts next weekIt should be fun, I hopeStill feeling run downOffice hours by appointment – e-mailMy DSL connection is badMakes working from home harder3Gaining FlexibilityQuestion: how do you make a file descriptor refer to non-files?Answer: treat it as an objectSystem calls have a shared part of codeActual work done by calls to function ptrsEach type of object exports a structure of func ptrs that handle all file-related syscalls4OverviewData must enter/leave the systemHow do we communicate with devices?How does data get transferred?How do we structure the OS?How do we increase flexibility?5Definitions & General MethodOverheadCPU time to initiate operation (cannot be overlapped)LatencyTime to perform 1-byte I/O operationBandwidthRate of I/O transfer, once initiatedGeneral methodAbstraction of byte transfersBatch transfers into block I/O for efficiency to prorate overhead and latency over a large unit6OverviewData must enter/leave the systemHow do we communicate with devices?Special instructions (in/out port/device)Memory-mapped – logic on adaptorHow does data get transferred?How do we structure the OS?How do we increase flexibility?7OverviewData must enter/leave the systemHow do we communicate with devices?How does data get transferred?Each byte moved manually – handshakingSeparate engine arranges movementHow do we structure the OS?How do we increase flexibility?8Programmed I/O “Slow” Input DeviceDeviceData registersStatus register(ready, busy, interrupt, … )A simple mouse designPut (X, Y) in data registers on a moveInterruptPerform an inputOn an interrupt reads values in X, Y registerssets ready bitwakes up a process/thread or execute a piece of codeCPUMemoryL2CacheI/O BusInterfaceX Y9Programmed I/O Output DeviceDeviceData registersStatus registers (ready, busy, … )Perform an outputPolls the busy bitWrites the data to data register(s)Sets ready bitController sets busy bit and transfers dataController clears the ready bit and busy bit10Direct Memory Access (DMA)Perform DMA from host CPUDevice driver call (kernel mode)Wait until DMA device is freeInitiate a DMA transaction(command, memory address, size)BlockDMA interfaceDMA data to device(size--; address++)Interrupt on completion(size == 0)Interrupt handler (on completion)Wakeup the blocked processCPUMemoryL2CacheI/O BusDMAInterfaceFree to movedata during DMA11OverviewData must enter/leave the systemHow do we communicate with devices?How does data get transferred?How do we structure the OS?Standard interface between OS/deviceMoves “intimate knowledge” out of OSHow do we increase flexibility?12Device DriversRest of theoperatingsystemDevicedriverDevicedriver...DevicedriverI/O SystemDevicecontrollerDevicecontroller...DevicecontrollerDeviceDeviceDeviceDevice13Device Driver Design IssuesOperating system and driver communicationCommands and data between OS and device driversDriver and hardware communicationCommands and data between driver and hardwareDriver operationsInitialize devicesInterpreting commands from OSSchedule multiple outstanding requestsManage data transfersAccept and process interruptsMaintain the integrity of driver and kernel data structures14Device Driver InterfaceOpen( deviceNumber )Initialization and allocate resources (buffers)Close( deviceNumber )Cleanup, deallocate, and possibly turnoffDevice driver typesBlock: fixed sized block data transfer Character: variable sized data transferTerminal: character driver with terminal controlNetwork: streams for networking15Block Device Interfaceread( deviceNumber, deviceAddr, bufferAddr )transfer a block of data from “deviceAddr” to “bufferAddr”write( deviceNumber, deviceAddr, bufferAddr )transfer a block of data from “bufferAddr” to “deviceAddr”seek( deviceNumber, deviceAddress )move the head to the correct positionusually not necessary16Character Device Interfaceread( deviceNumber, bufferAddr, size )reads “size” bytes from a byte stream device to “bufferAddr”write( deviceNumber, bufferAddr, size )write “size” bytes from “bufferSize” to a byte stream device17Unix Device Driver Interface Entry Pointsinit(): Initialize hardwarestart(): Boot time initialization (require system services)open(dev, flag, id): initialization for read or writeclose(dev, flag, id): release resources after read and writehalt(): call before the system is shutdownintr(vector): called by the kernel on a hardware interruptread/write calls: data transferpoll(pri): called by the kernel 25 to 100 times a secondioctl(dev, cmd, arg, mode): special request processing18OverviewData must enter/leave the systemHow do we communicate with devices?How does data get transferred?How do we structure the OS?How do we increase flexibility?Sync/Async I/O, buffering in kernelDynamic loading/binding19Why BufferingSpeed mismatch between producer and consumerCharacter device and block device, for exampleAdapt different data transfer sizesPackets vs. streamsSupport copy semanticsDeal with address translationI/O devices see physical memory, but programs use virtual memorySpoolingAvoid deadlock problemsCachingAvoid I/O operations20Detailed Steps of Blocked ReadA process issues a read call which executes a system callSystem call code checks for correctness and cacheIf it needs to perform I/O, it will issues a device driver callDevice driver allocates a buffer for read and schedules I/OController performs DMA data transfer, blocks the processDevice generates an interrupt on completionInterrupt handler stores any data and notifies completionMove data from kernel buffer to user buffer and wakeup blocked processUser process continues21Asynchronous I/OWhy do we want asynchronous I/O?Life is simple if all I/O is synchronousHow to implement asynchronous I/O?On a readcopy data from a system buffer if the data is thereOtherwise, initiate I/OHow does process find out
View Full Document