View
1
Download
0
Category
Preview:
Citation preview
I/O and Device Drivers
Minsoo Ryu
Real-Time Computing and Communications Lab.
Hanyang University
msryu@hanyang.ac.kr
2
Topics Covered
I/O Components
I/O Interface
I/O Operations
Device Drivers
I/O Components
4
CPU Execution and I/O
The two main jobs of a computer
▪ CPU execution
▪ Input/Output
Usually, the main job is I/O
▪ The CPU execution is incidental
5
Typical I/O Data Rates
6
Variety of I/O Devices
A computer is equipped with
▪ CPU and memory
▪ Many I/O devices
• VGA card, network card, disk controller, …
Main board
CPU
Memory
Network card
Hard disk
7
Types of I/O Devices
Character devices (mouse, terminal, etc)
▪ Commands include get and put
Block devices (disk drive, flash drive, etc)
▪ Commands include read, write, and seek
▪ Raw I/O or file-system access
▪ Memory-mapped file I/O access possible
• mmap( ) function: convenient programming interface
• mmap is faster than read, because of single copy operation
Network devices
▪ NIC (network interface card)
8
Character Device: Mouse (1)
9
Character Device: Mouse (2)
Brief history
▪ First mechanical mouse with a roller ball
• Bill English at Xerox PARC in the early 1970s
▪ Introduced by Apple Macintosh in 1984
• They have helped to completely redefine the way we use computers
since then
▪ Became the PC-human interface of choice quickly when
Windows 3.1 made Graphical User Interface (GUI) a standard
▪ Optical Mouse
• Gary Gordon at Agilent Laboratories in 1999
10
Character Device: Mouse (3)
Optical mouse
▪ Tiny camera takes 1500-7080 images per second
• Camera = laser + a CMOS sensor
▪ Images sent for analysis to a DSP operating typically at 18
MIPS
▪ DSP detects patterns in images and thus estimates motion
▪ Data ports are used for two-way communication
▪ Upon mouse movement, a 3/5-byte packet is sent to the port
• Typical description of the data
✓ (xs, ys), (xd, yd), mouse-up/down
▪ This data packet is decoded by the mouse driver and its
internal co-ordinates are updated
11
Character Device: Mouse (4)
PC mouse system (data transfer chain)
▪ Sensors (CMOS)
▪ Mouse Controller (DSP)
▪ Communication link (Cable/Wireless)
▪ Data interface (Serial, PS/2, USB)
▪ Device driver
▪ Application
12
Character Device: Mouse (5)
How OS collaborate with mouse driver
▪ Applications wait for mouse movement or click
▪ When a mouse movement occurs, the mouse driver informs
the event manager of OS about the event
• Event manager determines whether to queue the event or not
• Mouse driver automatically tracks the mouse and displays the cursor
as the user moves the mouse
▪ When a mouse-up or mouse-down event occurs, the event
manager records the action in the event queue and informs
the active application about it
▪ The active program decides what action is to be taken
• Ex: Show the mouse cursor, hide the cursor and draw something onto
screen, etc.
13
Character Device: Terminal (1)
Example
▪ DEC VT100, Heathkit Z19
14
Character Device: Terminal (2)
Terminal = keyboard + display
▪ Keyboard and display are handled independent in most
systems (no automatic echo, full duplex serial link)
I/O registers are connected to host via serial line
▪ Keyboard data/status registers
▪ Display data/status registers
One interrupt per character
▪ One character (8-bit data or control function) is sent at a time
ASCII encoding is used: ‘A’ is 0x65
Slow speed
▪ 10-1800 characters per second
▪ Measure: Baud rate (bits per second)
15
Character Device: Terminal (3)
Keyboard input
▪ When a character is typed:
• Its ASCII code is placed in bits [7:0] of keyboard data register
• The “ready bit” of keyboard status register is set to zero
• Keyboard is disabled
✓ Any typed characters will be ignored
▪ When KBDR is read:
• The “ready bit” of KBSR is set to one
• Keyboard is enabled
KBSR
KBDR15 8 7 0
1514 0
keyboard data
ready bit
16
Character Device: Terminal (4)
Display output
▪ When monitor is ready to display another character
• The “ready bit” of display status register is set to one
▪ When data is written to DDR:
• The “ready bit” of DSR is set to zero
• Character in DDR is displayed
• Any other character data written to DDR is ignored
DSR
DDR15 8 7 0
1514 0
display data
ready bit
17
Character Device: Terminal (5)
Keyboard echo
▪ Usually, input character is also printed to screen
▪ User gets feedback on character typed and knows it’s ok to
type the next character
18
Block Device: Disk Drive (1)
19
Block Device: Disk Drive (2)
Disk characteristics (technology of 2012)
▪ 2-6 heads (platters x 2)
▪ Platter diameter between 0.8" and 8"
▪ 16,383 tracks (cylinders) per surface
▪ 63 sectors per track
▪ Sector size of 512 to 4096 bytes
• 4KB physical emulated at 512-byte sectors
▪ Capacity ranges up to 4 TB
20
Block Device: Disk Drive (3)
Disk operation
▪ Select desired read/write head
▪ Move heads to the correct track (“seek”)
• Seek time
▪ Wait for disk to rotate desired sector into position
• Rotational delay
▪ Read and write sector while it spins by
• Transfer latency
Disk performance
▪ Seek time: 0-50 ms (average 10-20 ms)
▪ Rotational delay: 0-16 ms
▪ Typical drive spins at 3600-5400 RPM
21
Network Interfaces
Network interfaces
▪ UART and RS-232, USB, ethernet, etc
22
Layering (Protocol Stack)
Data
DataTCP
DataTCPIP
DataTCPIPEthernet
Network
Adapter (NIC)
Message
Segment/Datagram
Datagram
Frame
Bit stream
Port no.
IP address
MAC address
23
TCP Socket Programming Flow
ServerClient socket()
listen()
accept()
read()
bind()
Block until
connection from client
Process requests
write()
read()
close()
socket()
write()
connect()
read()
close()
well-known
port
Connection establishmentTCP three-way handshake
Data (request)
Data (reply)
End-of-file notification
Implicit bind
24
Clocks and Timers
Provide three basic functions▪ Give the current time
▪ Give the elapsed time
▪ Set a timer to trigger operation X at time Y
Programmable interval timer▪ The hardware for last two functions
▪ Wait and generate an interrupt
▪ CPU scheduler uses this
Unfortunately, the system calls for timer functions are not standardized across operating systems▪ alarm() in UNIX
▪ timer_create() and timer_settime() in POSIX
25
Unix Time Values
Two different time values
▪ Calendar time
• Counts the number of seconds since the Epoch (Jan 1, 1970)
• UTC (coordinated Universal Time, Greenwich Mean Time)
▪ Process time
• Measures the CPU time used by a process
• Process time is measured in clock ticks
✓ Usually, 50, 60, or 100 ticks per second
• CPU time = User CPU time + System CPU time
Lack of timer support on Unix
▪ We can get around by using alarm( ) system call
• The unit is second
▪ ioctl on UNIX covers odd aspects of I/O such as clocks and timers
• Catchall for I/O operations
26
Linux
The timer interrupt is set to a default frequency
▪ “linux/param.h”
▪ 100 Hz for most hardware platforms
Jiffies
▪ The number of clock ticks since the operating system was
booted
▪ When the timer interrupt occurs, the jiffies value is
incremented
I/O Interface and Operations
28
I/O Bus and Connection
Devices are attached to I/O bus
▪ ISA, PCI, EISA, SCSI, …
I/O BUS
CPU VGA Ethernet Sound
29
I/O Buses and Connections
30
Generic Model of I/O Interface
I/O Interface
31
I/O Registers
An I/O port typically consists of four registers
▪ Status register: states read by the host
• Whether the current command has completed
• Whether a byte is available to be read from data-in register
• Whether there is a device error
▪ Control register: command written by the host
• Start a command and change the mode of a device
▪ Data-in register: input read by the host
▪ Data-out register: output written by the host
32
I/O Register Access
Memory-mapped IO▪ Data Bus, Address Bus, Control Bus are shared
▪ e.g., Motorolla 68000
▪ Memory and I/O access use the same instructions
Port-mapped IO▪ Data Bus and Address Bus are shared, but Control Bus not
shared
▪ e.g., Intel x86 and Z80
▪ Memory and I/O access use different instructions
Data Channel▪ Separate Data Bus, Address Bus, and Control Bus
▪ Dedicated I/O processors are used
33
Memory-Mapped I/O
Memory mapped I/O uses special memory locations in the normal address space
Possible to design address decoder to “divert” read or write to a device instead of memory
Use move instead of in and out
PCs use both port-mapped mapped I/O and memory-mapped I/O
▪ Graphics controller has I/O ports for basic control operations
▪ But it also has a large memory-mapped region to hold screen contents
▪ The controller generates the screen image based on the contents of this memory
▪ Memory-mapped I/O is vulnerable to accidental modification by using pointers, but protected memory reduces this risk
34
Port-mapped I/O and Address Space
Port-mapped IO structure provides two address
spaces
▪ Memory address space and I/O (or port) address space
▪ The I/O address space uses the same address pins on the
processor as the memory address space
❖ RD and WR lines determine
whether data being read or written
❖ When M/IO line is high,
memory is selected
❖ When M/IO line is low,
I/O is selected
35
Instructions for Port-Mapped I/O
Port-mapped input/output uses special instructions
Register I/O instructions
▪ e.g., in and out (80386)
▪ Move data between I/O ports and general registers
▪ Data may be a byte, word, or doubleword
Block I/O instructions
▪ e.g., ins and outs (80386)
▪ Move blocks of data between I/O ports and memory space
36
Device I/O Port Locations on PCs (partial)
37
Basic I/O Operations
1. CPU checks I/O module status
2. I/O module return status
3. If ready, CPU requests data transfer
4. I/O module gets data from device
5. I/O module transfers data to CPU
❖ Variations for output such as DMA
38
Two Types of I/O
Polling (Programmed I/O)
▪ The system repeatedly checks the status of the I/O module if
data can be read or written
▪ Usually used for character devices
Interrupts (Interrupt-Driven I/O)
▪ The system sleeps the process until data can be read or
written and wakes it up upon receiving the awaited interrupt
▪ Usually used for character and block devices
39
DMA (Direct Memory Access)
Interrupt driven and programmed I/O require active
CPU intervention
▪ Transfer rate is limited
▪ CPU is tied up
DMA (Direct Memory Access)
▪ DMA controller is required
▪ Bypasses CPU to transfer data directly between I/O device
and memory
40
DMA Controllers
To do DMA, I/O device attached to DMA controller
▪ Multiple devices can be connected to one controller
▪ Controller itself seen as a memory mapped I/O device
• Processor initializes start memory address, transfer size, etc.
▪ DMA controller takes care of bus arbitration and transfer
details
• That’s why buses support arbitration and multiple masters
41
Cycle Stealing
When the DMA controller seizes the memory bus,
▪ The CPU is momentarily prevented from accessing main
memory
▪ But, it can still access data items in its primary and
secondary cache
The cycle stealing can slow down the CPU
computation
However, offloading the data-transfer work to a DMA
controller generally improves the total system
performance
Device Drivers
43
Types of Linux Devices
Character Devices
▪ Transfer unit: character (byte)
▪ Can be accessed like a file (open, close, read, write, …)
▪ (Ex) console, keyboard, mouse, … (/dev/tty1, /dev/lp1, …)
Block Devices
▪ Transfer unit: block (usually some kilobytes)
▪ Can be accessed like a file (open, close, read, write, …)
▪ (Ex) hard disk drive, CD-ROM drive, … (/dev/hda1, …)
Network Interfaces
▪ Not mapped to neither character or block devices
▪ (Ex) eth0
44
Device Files
Represent an I/O device
▪ /dev/tty0 = first serial port
Attributes
▪ Type: block or character
▪ Major number: specifies device driver
▪ Minor number: argument to device driver, kernel don’t care
Name Type Major Minor Desc
/dev/fd0 Block 2 0 Floppy Disk
/dev/had Block 3 0 First IDE disk
/dev/hda2 Block 3 2 Second partition of first IDE disk
/dev/hdb Block 3 64 Second IDE disk
/dev/console Char 5 1 Console
/dev/null char 1 3 Null device
45
Structure of Character Device Drivers
Name
File operations
…
Character Device Table
0
1
2
Character Device Driver
Open
Read
Write
Close
Ioctl
…
Read code
write code
Open code
…
Device Name
Registered when
device being installed
Character DeviceIndex =
major number
46
Character Device Drivers
To applications
▪ Device file = Regular file
To file system
▪ Regular file: read from or write to disk drive
▪ Device file: invoke device driver operations
• open: initialize device
• read: device data to user buffer
• write: user buffer to device
• close: called when removed from device table
• ioctl: device specific control
✓ (Ex) baud rate change, access permission set
47
Structure of Block Device Drivers
Block Device Table Block Device Driver
Open
Read
Write
Close
Ioctl
…
Open code
…Block Device
Handling request code
(strategy routine)
Buffer Cache Manager
Block Read
Block Write
BufferBuffer
BufferBlock Buffer
(Buffer Cache)
Request queue
char
block
48
Interrupt Handling in Device Drivers
Register “interrupt handling routine”
▪ Kernel invokes registered handler when interrupt occurs
Linux interrupt handling (two-level processing)
▪ Interrupt handler (top half)
• High priority function (such as acknowledging to PIC)
• Run with interrupt disabled
• Invoked every time interrupt occurred
• Marks a bottom half as active
▪ Bottom half
• Low priority function (such as transferring data from device)
• Run with interrupt enabled
• Execution may be deferred
49
Bottom Half
Proc
Int_handler_1
Ack Intr_1, activate bottom_half_1
Bottom_half_1
Intr_2
Int_handler_2
Ack Intr_2, activate bottom_half_1
Proc
Interrupt
enabled
Interrupt
disabled
In this example,
the bottom half is activated twice but executed once
Intr_1
Interrupt
disabled
50
General Device Driver Routines (1)
Open
▪ Reads minor number
▪ Initializes the appropriate device
▪ Initializes the device driver internal data structures
▪ If needed, changes the file operation table
(according to the minor number)
▪ Increases the usage counter
▪ If needed, registers the interrupt handler and enable IRQ
Close
▪ Frees dynamically allocated data structures
▪ Decreases the usage counter
▪ Un-registers the interrupt handler
51
General Device Driver Routines (2)
Read/Write
▪ Polling vs. interrupt driven
• Polling
✓Checks the device if data can be read or written
✓Usually used for character devices
• Interrupt Driven
✓ Sleeps the process until data can be read or written
✓Usually used for character and block devices
▪ Blocking vs. non-blocking
• Blocking
✓ If data cannot be read or written, waits until ready
• Non-blocking
✓ If data cannot be read or written, immediately returns
52
General Device Driver Routines (3)
Character Device: Polling and Blocking I/O
Check Device
Device ready?
No
Read Data
(device user)
Return to User
Yes
Read
Character Device: Non-Blocking I/O
Check Device
Device ready?No
Read Data
(device user)
Return to User
Yes
Read
53
General Device Driver Routines (4)
Character Device: Interrupt Driven and Blocking I/O
Check Device
Device Ready?
Sleep process
No
Read
Yes
Read ready interrupt
Wakeup process
Read Data
(device user)
Return to User
54
General Device Driver Routines (5)
Block Device: Interrupt Driven and Blocking I/O
Check write buffer
Buffer full ?
Issue Write Command
Sleep process
No
Write
Yes
Sleep process
Wakeup by another
process
Write complete interrupt
Wakeup process
Return to User
55
Recommended