i/o devices and drivers vivek pai / kai li princeton university

25
I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

Post on 19-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

I/O Devices and Drivers

Vivek Pai / Kai LiPrinceton University

Page 2: I/O Devices and Drivers Vivek Pai / Kai Li Princeton 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

Page 3: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 4: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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?

Page 5: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 6: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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?

Page 7: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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?

Page 8: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

MemoryL2

Cache

I/O Bus

InterfaceX Y

Page 9: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 10: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

MemoryL2

Cache

I/O Bus

DMAInterface

Free to movedata during

DMA

Page 11: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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?

Page 12: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

12

Device Drivers

Rest of theoperating

system

Devicedriver

Devicedriver

...

Devicedriver

I/O System

Devicecontroller

Devicecontroller

...

Devicecontroller

Device

Device

Device

Device

Page 13: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

13

Device Driver Design Issues

Operating system and driver communication

Commands 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

Page 14: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

14

Device Driver InterfaceOpen( deviceNumber )

Initialization and allocate resources (buffers)

Close( deviceNumber )Cleanup, deallocate, and possibly turnoff

Device driver typesBlock: fixed sized block data transfer Character: variable sized data transferTerminal: character driver with terminal controlNetwork: streams for networking

Page 15: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 16: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 17: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 18: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 19: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

19

Why BufferingSpeed mismatch between producer and consumer

Character device and block device, for example

Adapt different data transfer sizesPackets vs. streams

Support copy semanticsDeal with address translation

I/O devices see physical memory, but programs use virtual memory

SpoolingAvoid deadlock problems

CachingAvoid I/O operations

Page 20: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 21: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

21

Asynchronous I/O

Why do we want asynchronous I/O?Life is simple if all I/O is synchronous

How to implement asynchronous I/O?On a read

copy 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

Page 22: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 23: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

23

Dynamic Binding with An Indirect Table

Open( 1, … );

Dri

ver-

kern

el in

terf

ace

Driver for device 0

open(…) {}

read(…) {}Driver for device 1

open(…) {}

read(…) {}

Indirect table

OtherKernel

services

Interrupthandlers

Page 24: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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

Page 25: I/O Devices and Drivers Vivek Pai / Kai Li Princeton University

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