Slide 5-1 Device 5 Management
Announcements Slide 5-2 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
Slide 5-3 Determining When I/O is Complete CPU CPU Interrupt Pending Device Device Device Device Device Device 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
Control Unit with Interrupt Slide 5-4 (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] }; could be a trap instr. memory[1] contains the address of the interrupt handler
Examples of Exceptions in Pentium Slide 5-5 Systems Class Cause Async/ Trap Fault Abort Interrupt Intentional exception Potentially recoverable error nonrecoverable error signal from I/O device Sync Sync Sync Sync Async Return behavior always returns to next instruction might return to current instruction never returns always returns to next instruction
The Trap Instruction Operation Slide 5-6 1 Mode S Branch Table a trap is a software interrupt trap 2 3 Trusted Code User Supervisor hardware interrupts behave similarly, an interrupt gives an offset into interrupt vector table
Examples of Exceptions in Pentium Slide 5-7 Exception Number Systems Exception Table, also called Branch Table or Jump Table or Interrupt Vector Description Exception Class 0 Divide error fault 13 General protection fault fault 14 Page fault fault nonmaskable OS assigns 18 machine check abort 32-127 OS-defined Interrupt or trap 128 System call Trap 129-255 OS-defined Interrupt or trap maskable interrupts
Maskable Interrupts Slide 5-8 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 lowpriority interrupt
Disabling Maskable Interrupts Slide 5-9 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 reenable interrupts after done
Classes of System Calls Invoked by Slide 5-10 trap Process control File Management Device Management system call interface Information Communications Management end, abort load, execute create, terminate get attributes, set wait for time wait event, signal event allocate memory, free create, delete open, close read, write, reposition get attributes, set request device, release read, write, reposition get attributes, set logically attach or detach devices Note Similarity get time/date, set get system data, set get process, file, or device attributes, set create connection, delete send messages, receive transfer status info attach remote devices, detach
The Device Driver Interface Slide 5-11 write( ); Device Interface Terminal Driver Driver Printer Printer Driver Driver Disk Disk Driver Driver Terminal Controller Printer Printer Controller Disk Disk Controller
Device Management Organization Slide 5-12 System Interface Application Process e.g. write() File Manager Hardware Interface Command Device-Independent Device-Dependent Status Device Controller Data Data Device Manager or I/O Subsystem drivers
Device System Call Interface Slide 5-13 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)
Example: BSD UNIX Driver Slide 5-14 open close ioctl read write strategy select stop Prepare dev for operation No longer using the device Character dev specific info Character dev input op Character dev output op Block dev input/output ops Character dev check for data Discontinue a stream output op
Device Independent Function Call Slide 5-15 Trap Table func i ( ) dev_func_i(devid, ) { // Processing common to all devices switch(devid) { case dev0: dev0_func_i( ); case dev1: case devm: break; dev1_func_i( ); break; devm_func_i( ); break; }; // Processing common to all devices }
Overlapping the Operation of a Slide 5-16 Device and the CPU... read(dev_i, %d, x); y = f(x)...... startread(dev_i, %d, x);... While(stillReading()) ; y = f(x)... Data on device Variable x Register Device dev_i Memory CPU
Overlapping CPU-Controller Slide 5-17 Operations in a Process could be non-blocking or asynchronous system call App I/O Ctlr t 1 t 2 t 3 t 4 t 5 t 6 t 7 t 8 t 9
Overlapping Processing and I/O Slide 5-18 App1 makes a blocking or synchronous system call App 1 App 2 I/O Ctlr t 1 t 2 t 3 t 4
Device Manager I/O Strategies Slide 5-19 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
Polling I/O Read Operation Slide 5-20 System Interface 1 read(device, ); Data 2 3 4 read driver write driver 5 Hardware Interface Command Status Status Data Data Device Controller
Interrupt-driven I/O Operation Slide 5-21 1 System Interface read(device, ); Data 9 8b Device Status Table 3 2 read driver write driver 4 7 Device Device Handler Handler Interrupt Handler Handler 6 8a Hardware Interface 5 Command Status Status Data Data Device Controller
Driver-Kernel Interface Slide 5-22 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)
DMA with Interrupts Example Slide 5-23 5. DMA sends each byte to memory 6. DMA interrupts CPU when done 1. device driver told to transfer bytes from disk to memory 2. dev driver informs disk controller DMA/bus/interrupt Controller CPU CPU/Memory Bus I/O Bus Disk Controller Memory 3. disk controller initiates DMA 4. disk controller sends each byte to DMA controller
Handling Interrupts Slide 5-24 Device driver J int read( ) { // Prepare for I/O save_state(j); out dev# // Done (no return) } Device status table J Device interrupt handler J void dev_handler( ) { get_state(j); //Cleanup after op signal(dev[j]); return_from_sys_call(); } Interrupt Handler Device Controller
Handling Interrupts(2) Slide 5-25 Device driver J int read( ) { out dev# // Return after interrupt wait(dev[j}); return_from_sys_call(); } Device interrupt handler J void dev_handler( ) { //Cleanup after op signal(dev[j]); } Interrupt Handler Device Controller