Download Device Driver

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Slide 5-1
Device
Management
Copyright © 2004 Pearson Education, Inc.
5
Operating Systems: A Modern Perspective, Chapter 5
Announcements
• Homework Set #1 due Thursday 11 am, see
moodle
• copyright issues with lectures
• Read chapters 4 and 5
– skip sections 4.7, 4.8
– skip Sections 5.4, 5.5 for now
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-2
Slide 5-3
Determining When I/O is Complete
CPU
Interrupt Pending
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-4
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]
};
could
be a
trap
instr.
memory[1] contains the address of the interrupt handler
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Examples of Exceptions in Pentium
Systems
Slide 5-5
Class
Cause
Async/
Sync
Return
behavior
Trap
Intentional
exception
Sync
Fault
Potentially
recoverable
error
nonrecoverable error
signal from I/O
device
Sync
always returns
to next
instruction
might return to
current
instruction
never returns
Abort
Interrupt
Copyright © 2004 Pearson Education, Inc.
Sync
Async
Operating Systems: A Modern Perspective, Chapter 5
always returns
to next
instruction
The Trap Instruction Operation
Mode
Slide 5-6
a trap is a
“software” interrupt
S
Branch Table
1
2
trap
3
Trusted
Code
User
Supervisorhardware interrupts behave similarly,
an interrupt gives an offset into
interrupt vector table
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Examples of Exceptions in Pentium
Systems
Slide 5-7
Exception Table, also called Branch Table or
Jump Table or Interrupt Vector
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
OS
assigns
128
System call
Trap
129-255
OS-defined
Interrupt or trap
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
nonmaskable
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
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;
have to reenable
PC = memory[1]
};
interrupts after done
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Classes of System Calls Invoked by
trap
system call interface
Information CommManagement unications
Process
File
Device
control Management Management
•
•
•
•
•
•
•
end, abort
load, execute
create, terminate
get attributes, set
wait for time
wait event, signal event
allocate memory, free
•
•
•
•
•
•
•
•
request device, release
read, write, reposition
get attributes, set
logically attach or
detach devices
Note
Similarity
create, delete
open, close
read, write, reposition
get attributes, set
Copyright © 2004 Pearson Education, Inc.
•
•
•
Slide 5-10
•
•
•
•
create connection,
delete
send messages, receive
transfer status info
attach remote devices,
detach
get time/date, set
get system data, set
get process, file, or
device attributes, set
Operating Systems: A Modern Perspective, Chapter 5
The Device Driver Interface
…
write(…);
…
Device Interface
Terminal
Driver
Printer
Driver
Disk
Driver
Terminal
Controller
Printer
Controller
Disk
Controller
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-11
Slide 5-12
Device Management Organization
Application
Process
System Interface
e.g. write()
File
Manager
Device-Independent
Device-Dependent
Device Manager
or I/O Subsystem
Hardware Interface
drivers
Command
Status
Data
Device Controller
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Example: BSD UNIX Driver
open
close
ioctl
read
write
strategy
select
stop
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-14
Device Independent Function Call
Trap 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
…
}
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-15
Overlapping the Operation of a
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
Memory
CPU
Device dev_I
Copyright © 2004 Pearson Education, Inc.
Slide 5-16
Operating Systems: A Modern Perspective, Chapter 5
Overlapping CPU-Controller
Operations in a Process
Slide 5-17
could be non-blocking or asynchronous
system call
App
I/O Ctlr
t1
Copyright © 2004 Pearson Education, Inc.
t2 t3 t4
t5
t6
t7
Operating Systems: A Modern Perspective, Chapter 5
t8
t9
Overlapping Processing and I/O
App1 makes a blocking or synchronous
system call
App 1
App 2
I/O Ctlr
t1
Copyright © 2004 Pearson Education, Inc.
t2
t3
Operating Systems: A Modern Perspective, Chapter 5
t4
Slide 5-18
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Polling I/O Read Operation
read(device, …);
1
System Interface
Data
read driver
5
write driver
2
3
4
Hardware Interface
Command
Status
Data
Device Controller
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-20
Interrupt-driven I/O Operation
read(device, …);
1
Slide 5-21
9
8b
Data
System Interface
Device Status Table
4
read driver
2
7
Device
Handler
write driver
6
3
Interrupt
Handler
5
Hardware Interface
Command
Status
Data
Device Controller
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
8a
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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
DMA with Interrupts Example
1. device driver told
to transfer bytes from
disk to memory
2. dev driver informs
disk controller
5. DMA
sends each DMA/bus/interrupt
Controller
byte to
memory
6. DMA
interrupts
CPU when
done
Copyright © 2004 Pearson Education, Inc.
Slide 5-23
CPU
CPU/Memory Bus
Memory
I/O Bus
3. disk controller
initiates DMA
Disk
Controller 4. disk controller sends
each byte to DMA
controller
Operating Systems: A Modern Perspective, Chapter 5
Handling Interrupts
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
Copyright © 2004 Pearson Education, Inc.
Slide 5-24
Operating Systems: A Modern Perspective, Chapter 5
Handling Interrupts(2)
Device driver J
Device interrupt handler J
int read(…) {
…
out dev#
// Return after interrupt
wait(dev[J});
return_from_sys_call();
}
void dev_handler(…) {
//Cleanup after op
signal(dev[j]);
}
Interrupt Handler
Device Controller
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-25
Slide 5-26
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Reconfigurable Device Drivers
Slide 5-27
System call interface
open(){…}
read(){…}
Entry Points for Device j
etc.
Driver for Device j
Copyright © 2004 Pearson Education, Inc.
Other
Kernel
services
Operating Systems: A Modern Perspective, Chapter 5
The Pure Cycle Water Company
Customer Office
Water Company
Returning the Empties
Water Producer
Water Consumers
Delivering Water
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-28
Slide 5-29
Hardware Buffering
Process
Controller
Process
Controller
Controller
Data
A
Device
Device
Unbuffered
Copyright © 2004 Pearson Education, Inc.
Process
B
Process reads bi-1
Controller reads bi
A
B
Device
Process reads bi
Controller reads bi+1
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-30
Driver
Double Buffering in the Driver
Process
Process
A
A
B
Hardware
Controller
A
B
Device
Copyright © 2004 Pearson Education, Inc.
B
Controller
A
B
Device
Operating Systems: A Modern Perspective, Chapter 5
Circular Buffering
Buffer j
Buffer i
To data consumer
From data producer
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-31
A Generic Communications Device
Slide 5-32
Bus
Generic
Controller
Communications
Controller
Local
Device
Cabling connecting the
controller to the device
Device
•Printer
•Modem
•Network
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Rotating Media
Track (Cylinder)
(a) Multi-surface Disk
Copyright © 2004 Pearson Education, Inc.
(b) Disk Surface
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-33
Cylinder
(set of tracks)
(b) Cylinders
Slide 5-34
Storage
Device
Device Driver API
Driver
• Get disk description
• Set SCSI parms
•read/write ops
• Interrupt hander
SCSI API
•commands
•bits per byte
•etc.
Controller
(SCSI)
Magnetic Disk
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Compute vs I/O Bound
Slide 5-35
Compute-bound
Time
I/O-bound
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Disk Optimizations
• Transfer Time: Time to copy bits from disk
surface to memory
• Disk latency time: Rotational delay waiting
for proper sector to rotate under R/W head
• Disk seek time: Delay while R/W head
moves to the destination track/cylinder
• Access Time = seek + latency + transfer
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-36
Optimizing Seek Time
• Multiprogramming on I/O-bound programs
=> set of processes waiting for disk
• Seek time dominates access time =>
minimize seek time across the set
• Tracks 0:99; Head at track 75, requests for
23, 87, 36, 93, 66
• FCFS: 52+ 64 + 51 + 57 + 27 = 251 steps
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-37
Optimizing Seek Time (cont)
• Requests = 23, 87, 36, 93, 66
• SSTF: (75), 66, 87, 93, 36, 23
– 11 + 21 + 6 + 57 + 13 = 107 steps
• Scan: (75), 87, 93, 99, 66, 36, 23
– 12 + 6 + 6 + 33 + 30 + 13 = 100 steps
• Look: (75), 87, 93, 66, 36, 23
– 12 + 6 + 27 + 30 + 13 = 87 steps
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-38
Optimizing Seek Time (cont)
Slide 5-39
• Requests = 23, 87, 36, 93, 66
• Circular Scan: (75), 87, 93, 99, 23, 36, 66
– 12 + 6 + 6 + home + 23 + 13 + 30 = 90 + home
• Circular Look: (75), 87, 93, 23, 36, 66
– 12 + 6 + home + 23 + 13 + 30 = 84 + home
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Serial Port
CPU
Memory
Serial
Device
• Printer
• Terminal
• Modem
• Mouse
• etc.
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-40
Serial Port
Slide 5-41
Device Driver API
Device Driver
Software on the CPU
• Set UART parms
•read/write ops
•Interrupt hander
UART API
•parity
•bits per byte
•etc.
Bus Interface
Serial Device
(UART)
RS-232 Interface
• 9-pin connector
• 4-wires
• bit transmit/receive
• ...
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Adding a Modem
CPU
Memory
Serial
Device
Modem
• Dialing & connecting
• Convert analog voice to/from digital
• Convert bytes to/from bit streams
• Transmit/receive protocol
Phone
Switched Telephone Network
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-42
Serial Communication
Slide 5-43
Device Driver
•Set UART parms
•read/write ops
•Interrupt hander
Driver-Modem Protocol
• Dialing & connecting
• Convert analog voice to/from digital
• Convert bytes to/from bit streams
• Transmit/receive protocol
Copyright © 2004 Pearson Education, Inc.
Serial Device
RS-232
Modem
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-44
Exploiting the Phone Network
Logical Communication
CPU
Memory
CPU
Comm
Device
Comm
Device
Modem
Modem
Phone
Phone
Switched Telephone Network
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Memory
Slide 5-45
Data Networks
• Technology focus includes protocols and software
(more on this later … Chapter 15 and beyond ...)
Logical Communication
CPU
Memory
Network
Device
Network
Device
Data Network
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
CPU
Memory
MS Disk Description
0x00
0x03
0x0b
0x0d
0x10
0x11
0x13
0x15
0x16
0x18
0x1a
0x1c
0x1e
Copyright © 2004 Pearson Education, Inc.
0x02
0x0a
0x0c
0x0f
0x10
0x12
0x14
0x15
0x17
0x19
0x1b
0x1d
…
<a jump instruction to 0x1e>
Computer manufacturer name
Sectors per cluster (MS-DOS reads/writes a cluster of sectors)
Reserved sectors for the boot record
Number of FATs
Number of root directory entries
Number of logical sectors
Medium descriptor byte (used only on old versions of MS-DOS)
Sectors per FAT
Sectors per track
Number of surfaces (heads)
Number of hidden sectors
Bootstrap program
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-46
NT Driver Organization
Data Flow
I/O Portion of Native API
Filter Driver
Intermediate Driver
Filter Driver
Device Driver
HAL
Device
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
NT Executive
I/O Manager
File System Driver
Slide 5-47
NT Device Drivers
• API model is the same as for a file
• Extend device management by adding
modules to the stream
• Device driver is invoked via an Interrupt
Request Packet (IRP)
– IRP can come from another stream module
– IRP can come from the OS
– Driver must respond to minimum set of IRPs
• See Part I of notes
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 5
Slide 5-48
Related documents