Device 5 Management - CU-Boulder Computer Science
Transcript of Device 5 Management - CU-Boulder Computer Science
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-1
Copyright © 2004 Pearson Education, Inc.
5DeviceManagement
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-2
Copyright © 2004 Pearson Education, Inc.
Announcements
• Homework Set #1 due Thursday 11 am, see moodle
• copyright issues with lectures• Program Assignment #1 coming soon• Read chapters 4 and 5
– skip sections 4.7, 4.8– skip Sections 5.4, 5.5 for now
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-3
Copyright © 2004 Pearson Education, Inc.
Determining When I/O is Complete
CPUCPU
DeviceDevice DeviceDevice DeviceDevice
Interrupt Pending
• CPU incorporates an “interrupt pending” flag• When device.busy → FALSE, interrupt pending flag is set• Hardware “tells” OS that the interrupt occurred• Interrupt handler part of the OS makes process ready to run
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-4
Copyright © 2004 Pearson Education, Inc.
Control Unit with Interrupt (Hardware)
PC = <machine start address>;IR = memory[PC];haltFlag = CLEAR;while(haltFlag not SET) {
execute(IR);PC = PC + sizeof(INSTRUCT);IR = memory[PC];if(InterruptRequest) {
memory[0] = PC;PC = memory[1]
};
couldbe atrapinstr.
memory[1] contains the address of the interrupt handler
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-5
Copyright © 2004 Pearson Education, Inc.
Examples of Exceptions in Pentium Systems
Class Cause Async/Sync
Return behavior
Trap Intentional exception
Sync always returns to next
instructionFault Potentially
recoverable error
Sync might return to current
instructionAbort nonrecover-
able errorSync never returns
Interrupt signal from I/O device
Async always returns to next
instruction
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-6
Copyright © 2004 Pearson Education, Inc.
The Trap Instruction Operation
SMode
TrustedCode
trap
User Supervisor
Branch Table
2
3
1
a trap is a “software” interrupt
hardware interrupts behave similarly, an interrupt gives an offset into interrupt vector table
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-7
Copyright © 2004 Pearson Education, Inc.
Examples of Exceptions in Pentium Systems
Exception Number
Description Exception Class
0 Divide error fault
13 General protection fault
fault
14 Page fault fault
18 machine check abort
32-127 OS-defined Interrupt or trap
128 System call Trap
129-255 OS-defined Interrupt or trap
Exception Table, also called Branch Table orJump Table or Interrupt Vector
non-maskable
OSassigns
maskableinterrupts
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-8
Copyright © 2004 Pearson Education, Inc.
Maskable Interrupts
• Maskable interrupts can be turned off by CPU before execution of critical instruction sequences– are used by device controllers to talk with CPU
• Nonmaskable interrupts/exceptions is reserved for events such as unrecoverable memory errors and cannot be turned off by the CPU
• Can have multiple interrupt priority levels– high-priority interrupts can preempt execution of a low-
priority interrupt
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-9
Copyright © 2004 Pearson Education, Inc.
Disabling Maskable Interrupts
saveProcessorState() {for(i=0; i<NumberOfRegisters; i++)
memory[K+i] = R[i];for(i=0; i<NumberOfStatusRegisters; i++)
memory[K+NumberOfRegisters+i] = StatusRegister[i];}
PC = <machine start address>;IR = memory[PC];haltFlag = CLEAR;while(haltFlag not SET) {
execute(IR);PC = PC + sizeof(INSTRUCT);IR = memory[PC];if(InterruptRequest && InterruptEnabled) {
disableInterupts();memory[0] = PC;PC = memory[1]
};
have to reenableinterrupts after done
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-10
Copyright © 2004 Pearson Education, Inc.
Classes of System Calls Invoked bytrap
• end, abort• load, execute• create, terminate• get attributes, set• wait for time• wait event, signal event• allocate memory, free
Processcontrol
Comm-unications
FileManagement
DeviceManagement
InformationManagement
system call interface
• request device, release• read, write, reposition• get attributes, set• logically attach or
detach devices
• create connection, delete
• send messages, receive• transfer status info• attach remote devices,
detach
• create, delete• open, close• read, write, reposition• get attributes, set
• get time/date, set• get system data, set• get process, file, or
device attributes, set
NoteSimilarity
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-11
Copyright © 2004 Pearson Education, Inc.
The Device Driver Interface…write(…);…
Device InterfaceDevice Interface
TerminalDriver
TerminalDriver
PrinterDriver
PrinterDriver
DiskDriverDisk
Driver
TerminalControllerTerminalController
PrinterControllerPrinter
ControllerDisk
ControllerDisk
Controller
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-12
Copyright © 2004 Pearson Education, Inc.
Device Management OrganizationApplication
ProcessApplication
Process
FileManager
FileManager
Device ControllerCommandCommand StatusStatus DataData
Hardware Interface
System Interface
Device-IndependentDevice-Independent
Device-DependentDevice-Dependent
Device Manageror I/O Subsystem
drivers
e.g. write()
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-13
Copyright © 2004 Pearson Education, Inc.
Device System Call Interface
• Functions available to application programs• Abstract all devices (and files) to a few interfaces• Make interfaces as similar as possible
– Block vs character– Sequential vs direct/random access– Blocking versus Non-Blocking I/O
• blocking system call: process put on wait queue until I/O completes• non-blocking system call: returns immediately with partial number of
bytes transferred, e.g. keyboard, mouse, network sockets– Synchronous versus asynchronous
• asynchronous returns immediately, but at some later time, the full number of bytes requested is transferred
• Device driver implements functions (one entry point per API function)
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-14
Copyright © 2004 Pearson Education, Inc.
Example: BSD UNIX Driver
open Prepare dev for operationclose No longer using the deviceioctl Character dev specific inforead Character dev input opwrite Character dev output opstrategy Block dev input/output opsselect Character dev check for datastop Discontinue a stream output op
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-15
Copyright © 2004 Pearson Education, Inc.
Device Independent Function CallTrap Table
funci(…)dev_func_i(devID, …) {// Processing common to all devices…switch(devID) {case dev0: dev0_func_i(…);
break;case dev1: dev1_func_i(…);
break;…case devM: devM_func_i(…);
break;};
// Processing common to all devices…
}
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-16
Copyright © 2004 Pearson Education, Inc.
Overlapping the Operation of a Device and the CPU
. . .startRead(dev_I, “%d”, x);. . .While(stillReading()) ;y = f(x). . .
. . .read(dev_I, “%d”, x);y = f(x). . .
Variable x Register
Data on device
Device dev_IMemory CPU
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-17
Copyright © 2004 Pearson Education, Inc.
Overlapping CPU-Controller Operations in a Process
t1 t2 t3 t4 t5 t6 t7 t8 t9
could be non-blocking or asynchronoussystem call
App
I/O Ctlr
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-18
Copyright © 2004 Pearson Education, Inc.
Overlapping Processing and I/O
t1 t2 t3 t4
App1 makes a blocking or synchronoussystem call
App 1App 2I/O Ctlr
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-19
Copyright © 2004 Pearson Education, Inc.
Device Manager I/O Strategies
• Underneath the blocking/non-blocking synchronous/asynchronous system call API, OS can implement several strategies for I/O with devices– direct I/O with polling– direct I/O with interrupts– DMA with interrupts
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-20
Copyright © 2004 Pearson Education, Inc.
Polling I/O Read Operation
read(device, …);
Data
Device Controller
CommandCommand StatusStatus DataData
read driver
write driver
1
2 3 4
5
Hardware Interface
System Interface
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-21
Copyright © 2004 Pearson Education, Inc.
Interrupt-driven I/O Operation
read(device, …);
Data
Device Controller
CommandCommand StatusStatus DataData
read driver
write driver
1
2
4
5Hardware Interface
System InterfaceDevice Status Table
DeviceHandlerDeviceHandler
InterruptHandler
InterruptHandler
6
7
8b
9
8a3
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-22
Copyright © 2004 Pearson Education, Inc.
Driver-Kernel Interface
• Drivers are distinct from main part of kernel• Kernel makes calls on specific functions,
drivers implement them• Drivers use kernel functions for:
– Device allocation– Resource (e.g., memory) allocation– Scheduling– etc. (varies from OS to OS)
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-23
Copyright © 2004 Pearson Education, Inc.
DMA with Interrupts Example
CPU
MemoryDMA/bus/interruptController
DiskController
CPU/Memory Bus
1. device driver toldto transfer bytes fromdisk to memory2. dev driver informsdisk controller
5. DMAsends eachbyte tomemory6. DMAinterruptsCPU whendone
I/O Bus
3. disk controllerinitiates DMA4. disk controller sendseach byte to DMAcontroller
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-24
Copyright © 2004 Pearson Education, Inc.
Handling Interrupts
int read(…) {// Prepare for I/Osave_state(J);out dev#
// Done (no return)}
Device driver J
Device ControllerDevice Controller
Interrupt HandlerInterrupt Handler
void dev_handler(…) {get_state(J);
//Cleanup after opsignal(dev[j]);return_from_sys_call();
}
Device interrupt handler J
J
Device status table
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-25
Copyright © 2004 Pearson Education, Inc.
Handling Interrupts(2)
int read(…) {…out dev#
// Return after interruptwait(dev[J});return_from_sys_call();
}
Device driver J
Device ControllerDevice Controller
Interrupt HandlerInterrupt Handler
void dev_handler(…) {//Cleanup after opsignal(dev[j]);
}
Device interrupt handler J