540 likes | 546 Views
Operating Systems. I/O System. Alok Kumar Jagadev. I/O Hardware. Incredible variety of I/O devices Common concepts Port ( a connection point through computer accesses a device) Bus (daisy chain or shared direct access) Medium over which signals are sent/received
E N D
Operating Systems I/O System Alok Kumar Jagadev
I/O Hardware • Incredible variety of I/O devices • Common concepts • Port ( a connection point through computer accesses a device) • Bus (daisy chain or shared direct access) • Medium over which signals are sent/received • Controller (host adapter) • Chip that can be accessed by CPU and that controls the device/port/bus • PCI controller, Serial Port controller, keyboard controller, … • I/O instructions control devices • Devices have addresses, used by • Direct I/O instructions • Memory-mapped I/O
I/O hardware concepts CPU Memory I/O instructions (bits/bytes/data) Serial Portcontroller SCSI (host) controller Graphics controller Serial port I/O hardware Screen SCSI bus Disk Controller cable Disk
I/O hardware concepts:direct I/O max mov… or load, store… Main Memoryaddress Space 0 max I/O portaddress Space in, out 0
I/O hardware concepts:direct I/O in [address], Rx CPU Memory out [address], Rx registers/buffers(addresses) registers/buffers(addresses) registers/buffers(addresses) Serial Portcontroller SCSI (host) controller Graphics controller Serial port Screen SCSI bus Disk Controller cable Disk
I/O hardware concepts:memory mapped I/O max mov… or load, store… Main Memoryaddress Space 0 I/O port address range
I/O hardware concepts:memory mapped I/O mov [address], Rx CPU Memory mov Rx, [address] registers/buffers(addresses) registers/buffers(addresses) registers/buffers(addresses) Serial Portcontroller SCSI (host) controller Graphics controller Serial port Screen SCSI bus Disk Controller cable Disk
I/O port concept I/O instructions use those addressesto access the controller I/O port to access the device address range: I/O port addresses Device controller control and dataregisters
I/O port addresses 000 Device X Device Y Device Z I/O port address space …. …
Interacting with the Device controller • Host (CPU+Memory) and Device Controller interaction (data transfers and control) can be in one of 3 ways: • Polling • Interrupt driven I/O • Interrupt driven with help of DMA
Interrupts vs. Polling • An interrupt is an external or internal event that interrupts the microcontroller • To inform it that a device needs its service • A single microcontroller can serve several devices by two ways • Interrupts • Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal • Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device
Interrupts vs. Polling (cont.) • The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler • Polling • The microcontroller continuously monitors the status of a given device • ex. JNB TF, target • When the conditions met, it performs the service • After that, it moves on to monitor the next device until every one is serviced • Polling can monitor the status of several devices and serve each of them as certain conditions are met • The polling method is not efficient, since it wastes much of the microcontroller’s time by polling devices that do not need service
Interrupts vs. Polling (cont.) • The advantage of interrupts is: • The microcontroller can serve many devices (not all at the same time) • Each device can get the attention of the microcontroller based on the assigned priority • For the polling method, it is not possible to assign priority since it checks all devices in a round-robin fashion • The microcontroller can also ignore (mask) a device request for service • This is not possible for the polling method
Interrupt Service Routine • For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler • When an interrupt is invoked, the microcontroller runs the interrupt service routine • There is a fixed location in memory that holds the address of its ISR • The group of memory locations set aside to hold the addresses of ISRs is called interrupt vector table
Steps in Executing an Interrupt • Upon activation of an interrupt, the microcontroller goes through: • It finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack • It also saves the current status of all the registers internally (not on the stack) • It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR
Steps in Executing an Interrupt (cont.) • It gets the address of the ISR from the interrupt vector table and jumps to ISR • It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt) • Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted • It gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC • It starts to execute from that address
Six Interrupts in 8051 • Six interrupts are allocated as follows • Reset – power-up reset • Two interrupts are set aside for the timers: • One for timer 0 and one for timer 1 • Two interrupts are set aside for hardware external interrupts • P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2) • Serial communication has a single interrupt that belongs to both receive and transfer
Example: polling based writing CPU Memory system bus check the busy bit write byte set command ready bit set the write bit (write command) Device Controller data-in register command/control register set the busy bit data-out register status register clear the busy bit, clear the command ready bit Device/Port/Cable… Perform write operation
Example: polling based writing CPU Controller 1. read and check the busy bit 2. if busy go to 1. 3. set the write bit in command register 4. Write byte (word) into data out register 5. Set the command ready bit 6. Go to 1(maybe after doing something else) • notices command ready bit set • set the busy bit • controller reads the command (itis write command), gets bytefrom data out register andwrite the byte out(this may take time) • clears the busy bit • clears the command ready bit • clears the error bit
Transferring Data between host and device: Polling • Determines state of device • command-ready • busy • Error • Busy-wait cycle to wait for I/O from device
Transferring Data between host and device: Interrupts • CPU Interrupt-request line triggered by I/O device • Interrupt handler receives interrupts • Maskable to ignore or delay some interrupts • Interrupt vector to dispatch interrupt to the correct handler • Based on priority • Some nonmaskable • Interrupt mechanism also used for exceptions
Interrupts • CPU Interrupt-request line triggered by I/O device • Interrupt handler receives interrupts • Maskable to ignore or delay some interrupts • Interrupt vector to dispatch interrupt to correct handler • Based on priority • Some nonmaskable • Interrupt mechanism also used for exceptions
Direct Memory Access • Used to avoid programmed I/O for large data movement • Requires DMA controller • Bypasses CPU to transfer data directly between I/O device and memory
Application I/O Interface • I/O system calls encapsulate device behaviors in generic classes • Device-driver layer hides differences among I/O controllers from kernel • Devices vary in many dimensions • Character-stream or block • Sequential or random-access • Sharable or dedicated • Speed of operation • read-write, read only, or write only
Block and Character Devices • Block devices include disk drives • Commands include read, write, seek • Raw I/O or file-system access • Memory-mapped file access possible • Character devices include keyboards, mice, serial ports • Commands include get, put • Libraries layered on top allow line editing
Network Devices • Varying enough from block and character to have own interface • Unix and Windows NT/9x/2000 include socket interface • Separates network protocol from network operation • Includes select functionality • Approaches vary widely (pipes, FIFOs, streams, queues, mailboxes)
Clocks and Timers • Provide current time, elapsed time, timer • Programmable interval timer used for timings, periodic interrupts • ioctl (on UNIX) covers odd aspects of I/O such as clocks and timers
Blocking and Nonblocking I/O • Blocking - process suspended until I/O completed • Easy to use and understand • Insufficient for some needs • Nonblocking - I/O call returns as much as available • User interface, data copy (buffered I/O) • Implemented via multi-threading • Returns quickly with count of bytes read or written • Asynchronous - process runs while I/O executes • Difficult to use • I/O subsystem signals process when I/O completed
Two I/O Methods Asynchronous Synchronous
Kernel I/O Subsystem • Scheduling • Some I/O request ordering via per-device queue • Some OSs try fairness • Buffering - store data in memory while transferring between devices • To cope with device speed mismatch • To cope with device transfer size mismatch • To maintain “copy semantics”
Kernel I/O Subsystem • Caching - fast memory holding copy of data • Always just a copy • Key to performance • Spooling - hold output for a device • If 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
Error Handling • OS can recover from disk read, device unavailable, transient write failures • Must return an error number or code when I/O request fails • System error logs hold problem reports
I/O Protection • User process may accidentally or purposefully attempt to disrupt normal operation via illegal I/O instructions • All I/O instructions defined to be privileged • I/O must be performed via system calls • Memory-mapped and I/O port memory locations must be protected too
Kernel Data Structures • Kernel keeps state info for I/O components, including open file tables, network connections, character device state • Many, many complex data structures to track buffers, memory allocation, “dirty” blocks • Some use object-oriented methods and message passing to implement I/O
I/O Requests to Hardware Operations • Consider reading a file from disk for a process: • Determine device holding file • Translate name to device representation • Physically read data from disk into buffer • Make data available to requesting process • Return control to process
Device Drivers • Device Driver: Device-specific code in the kernel that interacts directly with the device hardware • Supports a standard, internal interface • Same kernel I/O system can interact easily with different device drivers • Special device-specific configuration supported with the ioctl() system call • Device Drivers typically divided into two pieces: • Top half: accessed in call path from system calls • implements a set of standard, cross-device calls like open(), close(), read(), write(), ioctl(), strategy() • This is the kernel’s interface to the device driver • Top half will start I/O to device, may put thread to sleep until finished • Bottom half: run as interrupt routine • Gets input or transfers next block of output • May wake sleeping threads if I/O now complete
Life Cycle of An I/O Request User Program Kernel I/O Subsystem Device Driver Top Half Device Driver Bottom Half Device Hardware
STREAMS • STREAM – a full-duplex communication channel between a user-level process and a device in Unix System V and beyond • A STREAM consists of: - STREAM head interfaces with the user process - driver end interfaces with the device- zero or more STREAM modules between them. • Each module contains a read queue and a write queue • Message passing is used to communicate between queues