I/O Devices and Drivers
Vivek Pai / Kai LiPrinceton University
2
MechanicsI’ve figured out the next project
Regular precepts next weekIt should be fun, I hope
Still feeling run downOffice hours by appointment – e-mail
My DSL connection is badMakes working from home harder
3
Gaining FlexibilityQuestion: how do you make a file descriptor refer to non-files?Answer: treat it as an object
System 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 syscalls
4
OverviewData must enter/leave the system
How do we communicate with devices?How does data get transferred?How do we structure the OS?How do we increase flexibility?
5
Definitions & General Method
OverheadCPU time to initiate operation (cannot be overlapped)
LatencyTime to perform 1-byte I/O operation
BandwidthRate of I/O transfer, once initiated
General methodAbstraction of byte transfersBatch transfers into block I/O for efficiency to prorate overhead and latency over a large unit
6
OverviewData must enter/leave the system
How do we communicate with devices?
Special instructions (in/out port/device)Memory-mapped – logic on adaptor
How does data get transferred?How do we structure the OS?How do we increase flexibility?
7
OverviewData must enter/leave the system
How do we communicate with devices?How does data get transferred?
Each byte moved manually – handshakingSeparate engine arranges movement
How do we structure the OS?How do we increase flexibility?
8
Programmed I/O “Slow” Input Device
DeviceData registersStatus register(ready, busy, interrupt, … )
A simple mouse designPut (X, Y) in data registers on a moveInterrupt
Perform an inputOn an interrupt
reads values in X, Y registerssets ready bitwakes up a process/thread or execute a piece of code
CPU
Memory L2Cache
I/O Bus
InterfaceX Y
9
Programmed I/O Output Device
DeviceData 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 bit
10
Direct 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)Block
DMA interfaceDMA data to device(size--; address++)Interrupt on completion(size == 0)
Interrupt handler (on completion)
Wakeup the blocked process
CPU
Memory L2Cache
I/O BusDMA
Interface
Free to movedata during
DMA
11
OverviewData must enter/leave the system
How 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 OS
How do we increase flexibility?
12
Device Drivers
Rest of theoperating
system
Devicedriver
Devicedriver
...
Devicedriver
I/O System
Devicecontroller
Devicecontroller
...Device
controller
Device
Device
Device
Device
13
Device Driver Design Issues
Operating system and driver communicationCommands and data between OS and device drivers
Driver and hardware communicationCommands and data between driver and hardware
Driver operationsInitialize devicesInterpreting commands from OSSchedule multiple outstanding requestsManage data transfersAccept and process interruptsMaintain the integrity of driver and kernel data structures
14
Device Driver InterfaceOpen( deviceNumber )
Initialization and allocate resources (buffers)Close( deviceNumber )
Cleanup, deallocate, and possibly turnoffDevice driver types
Block: fixed sized block data transfer Character: variable sized data transferTerminal: character driver with terminal controlNetwork: streams for networking
15
Block 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 necessary
16
Character 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 device
17
Unix Device Driver Interface Entry Points
init(): 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 processing
18
OverviewData must enter/leave the system
How 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/binding
19
Why BufferingSpeed mismatch between producer and consumer
Character device and block device, for exampleAdapt different data transfer sizes
Packets vs. streamsSupport copy semanticsDeal with address translation
I/O devices see physical memory, but programs use virtual memory
SpoolingAvoid deadlock problems
CachingAvoid I/O operations
20
Detailed Steps of Blocked Read
A 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 continues
21
Asynchronous 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 about completion?
On a writecopy to a system buffer, initiate the write and return
22
Other Design IssuesBuild device drivers
staticallydynamically
How to down load device driver dynamically?
load drivers into kernel memoryinstall entry points and maintain related data structuresinitialize the device drivers
23
Dynamic Binding with An Indirect Table
Open( 1, … );
Driv
er-k
erne
l int
erfa
ce Driver for device 0
…
open(…) {}
read(…) {}Driver for device 1
…
open(…) {}
read(…) {}
Indirect table
OtherKernelservices
Interrupthandlers
24
Dynamic BindingDownload drivers by users (may require a reboot)
Allocate a piece of kernel memoryPut device driver into the memoryBind device driver with the device
Pros: flexible and support ISVs and IHVsCons: security holes
25
Think About PerformanceA terminal connects to computer via a serial line
Type character and get characters back to displayRS-232 is bit serial: start bit, character code, stop bit (9600 baud)
Do we have any cycles left?10 users or 10 modems900 interrupts/sec per userOverhead of handing an interrupt = 100 sec
Top Related