Operating Systems
Chapter 4 I/O and Driver
Tien Pham Van, Dr. rer. nat.
Compiled with reference to other presentations
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 1
Email:
[email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Concept
• Communication between an information processing
system (computer) and peripherals, or another
processing system
• It may refer to input/output operations for data
exchange
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 2
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Overview
• Two fundamental operations performed by a computing system
– Processing of data (implement an algorithm)
– Perform I/O (move data into and out of the system)
• Large diversity in I/O devices, capabilities and performance
– Common Categories: storage, transmission and user-interface devices.
– Examples: display, keyboard, network, hard disks, tape drives, CDROM ...
• OS Goal is to
1. provide simple and consistent interface to user
2. optimize I/O use for maximum concurrency
• Mechanisms used by OS
– device drivers provide standard interface to I/O devices
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 3
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Human readable
– Used to communicate with the user
– Printers
– Video display terminals
• Display
• Keyboard
• Mouse
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 4
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Machine readable
– Used to communicate with electronic equipment
– Disk and tape drives
– Sensors
– Controllers
– Actuators
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 5
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Communication
– Used to communicate with remote devices
– Digital line drivers
– Modems
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 6
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Data rate
– May be differences of several orders of magnitude
between the data transfer rates
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 7
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Application
– Disk used to store files requires file management
software
– Disk used to store virtual memory pages needs special
hardware and software to support it
– Terminal used by system administrator may have a higher
priority
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 8
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Complexity of control
• Unit of transfer
– Data may be transferred as a stream of bytes for a
terminal or in larger blocks for a disk
• Data representation
– Encoding schemes
• Error conditions
– Devices respond to errors differently
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 9
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Performing I/O
• Programmed I/O
– Process is busy-waiting for the operation to complete
• Interrupt-driven I/O
– I/O command is issued
– Processor continues executing instructions
– I/O module sends an interrupt when done
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 10
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Performing I/O
• Direct Memory Access (DMA)
– DMA module controls exchange of data between main
memory and the I/O device
– Processor interrupted only after entire block has been
transferred
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 11
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Relationship Among Techniques
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 12
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O and Devices
Processor Device table
dispatcher Device Driver
X
(interrupt handler)
Bus
command status data 0
data 1
Device Controller
(firmware and logic) ...
Device X data N-1
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 13
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Programmed I/O
• Busy-wait cycle to wait for I/O from Insert Read
device command to CPU I/O
I/O Module
• Poll at select times: periodic, Read Status
entering/leaving kernel etc. of I/O
Module
I/O CPU
– Determines state of device: command- Not
Ready
ready, busy, error Check Error
Condition
Status
• Processor transfer data to/from Ready
device. Read word
from I/O I/O CPU
• Read/write directly to status and
Module
command registers Write word
CPU Memory
into memory
• Processor polls device for status
• Consumes a lot of processor time No
Done?
because every word read or written Yes
passes through the processor Next Instruction
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 14
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Interrupt-Driven I/O
Insert Read
command to CPU I/O
• Similar to direct I/O but processor I/O Module Do something
else
not required to poll device. Read Status
Interrupt
of I/O
• Interrupt asserted to notify Module I/O CPU
processor of a change in status Error
Check
• Interrupt handler receives interrupts Status Condition
Ready
• Maskable to ignore or delay some Read word
interrupts from I/O I/O CPU
Module
• Interrupt vector to dispatch interrupt Write word
to correct handler into memory
CPU Memory
– Based on priority
No
– Some unmaskable Done?
Yes
Next Instruction
Interrupt mechanism also used for exceptions
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 15
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Direct Memory Access (DMA)
• I/O exchanges to memory
– Processor grants I/O module
authority to read from or write to
memory
Issue Read
– Relieves the processor from the task
CPU DMA
block command
to I/O module Do something
– Processor is free to do other things else
• An interrupt is sent when the task
Read status
is complete of DMA Interrupt
module
• The processor is only involved at DMA CPU
the beginning and end of the
transfer Next Instruction
• Used to avoid programmed I/O for
large data movement
• Requires DMA controller
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 16
Email:
[email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Six steps to perform DMA transfer
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 17
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Programmed I/O
Writing a string to the printer using programmed I/O
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 18
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Interrupt-Driven I/O
• Writing a string to the printer using interrupt-driven I/O
– Code executed when print system call is made
– Interrupt service procedure
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 19
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Using DMA
• Printing a string using DMA
– code executed when the print system call is made
– interrupt service procedure
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 20
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Subsystem and the Layered Design
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
IO Subsystem
IO subsystem defines a standard set of
functions for IO operations
◼ To hide device peculiarities from applications
All IO device drivers conform to and support
this function set
◼ To provide uniform IO to applications across a
wide spectrum of IO devices of varying types
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
IO Functions
Function Description
Create Creates a virtual instance of an I/O device
Destroy Deletes a virtual instance of an I/O device
Open Prepares an I/O device for use.
Close Communicates to the device that its services are no
longer required, which typically initiates device-specific
cleanup operations.
Read Reads data from an I/O device
Write Writes data into an I/O device
Ioctl Issues control commands to the I/O device (I/O control)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
IO Function Mapping
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device tree
Devicetree is a data structure describing the
hardware components of a particular
computer so that the kernel can use and
manage those components, including the
CPUs, memory, buses and peripherals
It is a tree of named nodes and properties.
Nodes contain properties and child nodes,
while properties are name–value pairs
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Kernel I/O functions
• Scheduling
– Efficiency, fairness, prioritized access
• Buffering - store data in memory while transferring between
devices
– To cope with device speed mismatch, transfer size mismatch and to maintain
“copy semantics”
• Caching - fast memory holding copy of data
– Always just a copy, Key to performance
• Spooling - hold output for a device
– Used when device can serve only one request at a time, i.e., Printing
• Device reservation - provides exclusive access to a device
– System calls for allocation and deallocation
– Watch out for deadlock
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 26
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Error Handling
• OS can recover from disk read, device unavailable,
transient write failures
• Most return an error number or code when I/O
request fails
• System error logs hold problem reports
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 27
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Improving Performance
• I/O a major factor in system performance
– Demands CPU to execute device driver, kernel I/O code
– Context switches due to interrupts
– Data copying
– Network traffic especially stressful
• Improving Performance
– Reduce number of context switches
– Reduce data copying
– Reduce interrupts by using large transfers, smart controllers,
polling
– Use DMA
– Balance CPU, memory, bus, and I/O performance for highest
throughput
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 28
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O and registers
• The I/O ports of each device are actually structured into a
set of specialized registers
• CPU write commands to control registers
• CPU reads a value that represents the internal state of the
device from status register
• CPU fetch data from a device by reading input register
• CPU push data to device by writing bytes into output
registers Control register
Status register Device’s
CPU I/O Interface
input register
Output register
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 29
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
What is an I/O interface?
• An I/O interface is a hardware circuit inserted
between a group of I/O ports and the
corresponding device controller.
• It acts as an interpreter that translates the values in
the I/O ports into commands and data for the
device.
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 30
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Types of I/O interfaces
• Custom I/O interfaces
– devoted to one specific
hardware device
– usually, the device
controller is on the same
card of I/O interfaces.
• General-purpose I/O
interface (GPIO)
– used to connect several
different hardware
devices
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 31
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Custom I/O interfaces
• Keyboard interface
• Graphic interface
• Disk interface
• Network interface
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 32
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device driver development
• https://www.coursera.org/lecture/iot-
architecture/device-drivers-AL7YG
• Windows: Device Driver Development
Tutorial – YouTube
https://www.youtube.com/watch?v=jC0B2kSyKAI
• Linux: Learning Linux Device Drivers
Development : Find and Create Network
Drivers | packtpub.com - YouTube
https://www.youtube.com/watch?v=4Njw_VJUhZ4
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 33
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Practice: device driver development
• https://github.com/PacktPublishing/Linux-
Device-Drivers-Development
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 34
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596