lec15-io - nc state computer science€¦ · qmanage data transfer qaccept and process interrupts...
TRANSCRIPT
I/O Devices
CSC501 Operating Systems Principles
1
Previous Lectures
q AbstractionQ ProcessQ Memory
q NowQ I/O Devices
Basic Computer Architecture
CPU
memory
I/O Devices
CPUSystem Bus &MMU/AGP/PCI
Controller
I/O Bus
IDE DiskController
USBController Another
I/O BusSerial & Parallel Ports
Keyboard & Mouse
Intel SR440BX MotherboardMemory
q The largest, most complex subsystem in OSq Most lines of code with the highest rate of code
changesQ Where OS engineers most likely to workQ Difficult to test thoroughlyQ Big impact on performance and perceptionQ Bigger impact on acceptability in market
OS Designer’s Perspective
Principles of I/O Software
q Efficiency - Do not allow I/O operations to become system bottleneck, especially for slow devices
q Generality - Provide a common API to access I/O devicesQ Uniform naming/interfacev open, close read, write…
Q Device independencev isolate OS and application programs from device
specific details and peculiaritiesv isolate the impact of device errors, retry where
possiblen Errors are abundant in I/O
6
I/O Software “Stack”
User Level Software
Device IndependentSoftware
Device Drivers
Interrupt Handlers
Hardware
I/O API & libraries
Device Dependent
Device Dependent – as
short as possible
(Rest of the OS)
Communication with I/O Devices
q Programmed I/O (a.k.a., polling)q Interrupt-Driven I/Oq DMA-based I/O
8
Programmed I/O (Polling)
q Used when device and controller are relatively quick to process an I/O operationQ Device driverv Gains access to devicev Initiates I/O operationv Loops testing for completion of I/O operationv If there are more I/O operations, repeat
Q Used in following kinds of cases v Service interrupt time > Device response timev Device has no interrupt capabilityv Embedded systems where CPU has nothing else to do
Any Example?
Interrupt-Driven I/Oq Interrupts occur on I/O events Q Operation completion Q Error or change of status
q Interrupt handlingQ Step 1: Save the current machine stateQ Step 2: Load the machine state for interrupt
handlingQ Step 3: Invoke the corresponding ISRQ Step 4: Resume the program execution
Any Example?
Question: Where do we save the current machine states?
DMA-Based I/O
q Direct Memory AccessQ Device read/write directly from/to memoryQ Transfer from memory to device typically
initiated from CPUQ Transfer from device to memory can be
initiated by the device or the CPU
11
Any Example?
DMA-Based I/O
12
q Comparison: Polling, Interrupt, and DMAQ Polling and Interrupt = word at a timeQ DMA = block at a time
Communication with I/O Devices
CPU Memory
Disk
Interconnect
CPU Memory
Disk
Interconnect
CPU Memory
Disk
Interconnect
Polling Interrupt DMA
14
Question:
How should the device driver be designed?
Device Driver
q Hides the device specifics from the above layersQ Supporting a common API
q Translates logical I/O into device I/OQ E.g., logical disk blocks into {head, track, sector}Q Performs data buffering and scheduling of I/O
operationsq StructureQ Several synchronous entry points: device
initialization, queue I/O requests, state control, read/write
Q An asynchronous entry point to handle interrupts
Device Driver – A Typical Design
q OS and driver communication: OS-dependentQ Commands and data between OS and driversv Meet the interface specs of the device independent
layerv Utilize the facilities supplied by the OS – buffers,
error codes, etc.q Driver and hardware comm.: Device-dependentQ Commands and data between driver and hardwarev Actions during interrupt handling
16
Device Driver – A Typical Design
q Driver operationsQ Initialize devicesQ Interpret commands from OSQ Schedule multiple outstanding requestsQ Manage data transferQ Accept and process interruptsQ …
q Typically use open-close-read-write paradigmQ Support open, close, read, write, seek functionsQ Use ioctl function as escape mechanism
17
Uniform API (e.g., in Linux)
18
Case Study w/ Xinu
q Code divided into two partsQ Upper Halfv Defines abstract operationsv Executed by application to request I/O
Q Lower Halfv Device-specific interrupt routinev Executed by arbitrary (current) processv Invoked when operation completes
19
User
system call Device
specific upper-half
routine
Device specific
lower-half routine
Device
Device switch table devtab[ ]
Case Study w/ Xinu
q Device switch tableQ One row per deviceQ One column per operationQ Entry points to a procedure
20
21
int dvnum;
long int (*dvinit)();
long int (*dvshutdown)();
long int (*dvopen)();
long int (*dvclose)();
long int (*dvread)();
long int (*dvwrite)();
long int (*dvseek)();
long int (*dvgetc)();
long int (*dvputc)();
long int (*dvcntl)();
long int (*dviint)();
long int (*dvoint)();
long int dvcsr;
long int dvivec;
long int dvovec;
char *dvioblk;
int dvminor;
device switch table entry ( struct devsw)
Ptrs to device specific high-level
routines
The structure of the control block varies from device to device. The control block contains information associated with the device.
Ptr to device control block
Device Switch Table
Case Study w/ Xinu
q Device switch tableQ Device-independent operations ‘‘generic’’Q Given operation may not apply to given devicev Example: seek on keyboard or network
Q All entries in device switch table must be valid
22
Any solution?
Case Study w/ Xinu
q Device switch tableQ Device-independent operations ‘‘generic’’Q Given operation may not apply to given devicev Example: seek on keyboard ornetwork
Q All entries in device switch table must be validq Special EntriesQ ionullv Used for innocuous operation, always returns OK
Q ioerrv Used for incorrect operation, always returns
SYSERR
23
Example Device Driver tty in Xinu
q Used for I /O to consoleq Separate input and output buffersq Use semaphores to synchronize
q Upper-half routinesQ ttyinit, ttyopen, ttyclose, ttyread, ttywrite,
ttyputc, ttygetc, ttypcntlq Lower-harf routinesQ ttyiin (input interrupt handler) Q ttyoin (output interrupt handler)
24
Example Device Driver tty in Xinu
q Actions during a character outputQ Upper-halfv Waits for buffer spacev Deposits characterv Triggers device
Q Lower-halfv Extracts character and pass to devicev Signals space semaphore
25
Next Lecture
q Hard Disk Q Scheduling and RAID
26