Download presentation
Presentation is loading. Please wait.
Published byDonald Walters Modified over 9 years ago
2
1 Input/Output Chapter 5 5.1 Principles of I/O hardware 5.2 Principles of I/O software 5.3 I/O software layers 5.4 Disks
3
2 OS functions about I/O devices Issue commands to the device Catch interrupts Handle errors Provide easy-to-use interface between the devices and the rest of the OS –Ideally device independent
4
3 Principles of I/O hardware Different people look at I/O hardware in different ways. –Electrical engineers look at chips, wires, power supplies, motors, and other physical components. –Programmers look at the interface presented to the software: commands, functions, reported errors Our interest is restricted to how the hardware is programmed, not how it works inside.
5
4 I/O Devices Two categories of I/O devices: –Block devices: (disk) It stores information in fixed-size blocks, each one with its own address. It is possible to read or write each block independently. –Character devices: (printer, modem) It delivers or accepts a stream of characters. It is not addressable and does not have any seek operation. Some devices do not fit in the classification scheme. For example: clocks, memory-mapped screen. The model of block and character devices is used as a basis for making I/O device software of OS device independent. For example, the file system deals just with abstract block devices and leaves the device-dependent part to the lower- level software.
6
5 Some typical device, network, and data base rates
7
6 Device Controllers I/O devices consist of mechanical and electronic component The electronic component is the device controller –On PC, it takes the form of a printed circuit card that can be inserted into an expansion slot –May be able to handle multiple devices –The interface between the controller and the device is often a very low-level interface For example, a disk might be formatted with 256 sectors of 512 bytes per track. What actually comes off the drive is a serial bit stream, starting with a preamble, then the 4096 bits (512 bytes) in a sector, and finally an error-correcting code (ECC).
8
7 Device Controllers Controllers jobs –Disk Convert serial bit stream to blocks of bytes Perform error correction if necessary –The block of bytes is typically first assembled in a buffer inside the controller. After its checksum has been verified, it can then be copied to main memory. –Monitor Read bytes containing the character to be displayed from memory and generate the signals used to modulate the CRT beam to cause it to write on the screen. The OS initializes the controller with a few parameters, such as the number of characters or pixels per line and number of lines per screen, and lets the controller take care of actually driving the beam.
9
8 Memory-Mapped I/O Each controller has a few registers for communicating with the CPU. –By writing into these registers, the OS can command the device to deliver data, accept data, switch itself on/off, etc. –By reading from these registers, the OS can learn the device’s state, whether it is ready to accept a new command, etc. Many devices have a data buffer that the OS can read and write. For example, video RAM.
10
9 Memory-Mapped I/O How the CPU communicates with the control registers and the device data buffers? 1.Each control register is assigned an I/O port number. Using special I/O instructions like –IN REG, PORT –Compare IN R0, 4 with MOV R0, 4 for memory address –OUT PORT, REG 2.Map all the control registers into the memory space –Each control register is assigned a unique memory address to which no memory is assigned. (memory-mapped I/O) 3.Hybrid: Memory-mapped data buffers and separate I/O ports for the control registers. (Pentium)
11
10 Memory-Mapped I/O (a) Separate I/O and memory space (b) Memory-mapped I/O (c) Hybrid I/O ports
12
11 Memory-Mapped I/O When the CPU wants to read a word, either from memory or an I/O port, –it puts the address on the bus’ address lines and then asserts a READ signal on a bus’ control line. –A second signal line is used to tell whether I/O space or memory space is needed. The corresponding unit then responds to the request. For one address space (Fig 5-2(b)), every memory module and every I/O device compares the address lines to its servicing range. If ( 訂正 ) the address falls in its range, it responds to the request.
13
12 Memory-Mapped I/O Comparison of memory-mapped I/O with special I/O ports: –For special I/O instruction, assembly codes are needed to access control registers. For memory-mapped I/O, control registers can be addressed in C the same as other variables. –With memory-mapped I/O, no special protection mechanism is needed to keep user processes from performing I/O. If each device has its control registers on a different page, the OS can give user control over specific devices by including the desired pages in its page table. Allow different device drivers to be placed in different address space. Not only reducing kernel size, but also keep the driver from interfering with others. –With memory-mapped I/O, every instruction that can reference memory can also reference control registers.
14
13 Memory-Mapped I/O Disadvantage of memory-mapped I/O –Most computers have caching of memory words. Caching a device control register would be disastrous. The hardware has to be equipped with the ability to selectively disable caching on a per page basis. –If there is only one address space, then all memory modules and all I/O devices must examine all memory references to see which ones to respond to. However, the trend is having a dedicated high-speed memory bus as shown in Fig 5-3(b). I/O devices have no way of seeing memory addresses as they go by the memory bus, so they have no way of responding.
15
14 Memory-Mapped I/O A single-bus architecture A dual-bus memory architecture
16
15 Memory-Mapped I/O First possible design: first send all memory references to the memory. If the memory fails to respond, then CPU tries the other buses. Second possible design: put a snooping ( 窺探 ) device on the memory bus to pass all addresses to potentially interested I/O devices. Third possible design: like the one used in Pentium (Figure 1-11), filter addresses in the PCI bridge chip. This chip contains range registers that are preloaded at boot time. Addresses that fall within one of the ranges marked as nonmemory are forwarded onto the PCI bus instead of memory. The disadvantage is the need for figuring out at boot time which memory addresses are not really memory addresses.
17
16 Direct Memory Access Operation of a DMA transfer, the DMA controller has access to the system bus independent of the CPU
18
17 Direct Memory Access Step 1: The CPU programs the DMA controller by setting its registers so it knows what to transfer where. It also issues a command to the disk controller telling to read data from the disk into its internal buffer and verify its checksum. When valid data are in the disk controller’s buffer, DMA can begin. Step 2: The DMA controller initiates the transfer by issuing a read request over the bus to the disk controller. Step 3: Data are written to memory. Step 4: When the write is complete, the disk controller sends an acknowledge signal to the DMA (p 277 訂正 ) controller over the bus. The DMA controller then increments the memory address and decrements the byte count. Repeat steps 2 to 4 until the byte count reaches 0. Then, the DMA controller interrupts the CPU to let it know the transfer is complete.
19
18 Direct Memory Access More complex DMA controllers can be programmed to handle multiple transfers at once. Some DMA controllers can operate in word-at-a-time and block mode. Word-at-a-time mode: the DMA controller requests for the transfer of one word and gets it. If the CPU also wants the bus, it has to wait. This mechanism is called cycle stealing because the device controller sneaks in and steals an occasional bus cycle from the CPU. Block mode: the DMA controller tells the device to acquire the bus, issue a series of transfers, then release the bus. This is called burst mode. It can block the CPU and other devices for a substantial period if a long burst is being transferred.
20
19 Direct Memory Access The above discussed model is called fly-by mode, the DMA controller tells the device controller to transfer the data directly to main memory. An alternative mode is to have the device controller send the word to the DMA controller, which then issues a second bus request to write the word to wherever it is supposed to go. It is more flexible in that it can also perform device-to-device copies and even memory-to- memory copies.
21
20 Direct Memory Access Why does not the controller just store the bytes in main memory as soon as it gets them from disk? Why does it need an internal buffer? –The disk controller can verify the checksum before starting a transfer. If the checksum is incorrect, an error is signaled and no transfer is done. –Once a disk transfer has started, the bits keep arriving from the disk at a constant rate, whether the controller is ready for them or not. If the controller tried to write directly to memory, it would have to go over the system for every word transferred. If the bus were very busy, the controller might end up storing quite a few words and having a lot of administration to do as well. When the block is buffered internally, the bus is not needed until the DMA begins, so the design of the controller is much simpler. Not all computers use DMA because the CPU is often far faster than the DMA controller and can do the job much faster. Also, getting rid of DMA controller saves money.
22
21 Interrupts Revisited (5.1.5, p279—280 上半 ) How an interrupts happens. Connections between devices and interrupt controller actually use interrupt lines on the bus rather than dedicated wires Bus
23
22 Interrupts Revisited When an I/O device has finished the work given to it, it causes an interrupt. It does this by asserting a signal on a bus line that it has been assigned. This signal is then detected by the interrupt controller chip on the parentboard, which then decides what to do. If no other interrupts are pending, the interrupt controller processes the interrupt immediately. Otherwise, the device is just ignored for the moment. In this case, it continues to assert an interrupt signal on the bus until it is serviced by the CPU. To handle the interrupt, the controller puts a number on the address lines specifying which device wants attention and asserts a signal that interrupts the CPU.
24
23 Interrupts Revisited The interrupt signal causes the CPU to stop what it is doing and start doing something else. The number on the address line is used as an index into the interrupt vector to fetch a new program counter, which points to the start of the corresponding interrupt service procedure. Shortly after it starts running, the interrupt service procedure acknowledges the interrupt by writing a certain value to one of the interrupt controller’s I/O ports, so that the controller now is free to issue another interrupt. By having the CPU delay this acknowledgement until it is ready to handle the next interrupt, race conditions involving multiple simultaneous interrupts can be avoided.
25
24 Interrupts Revisited Further issues: –Before starting the service procedure, which information is saved and where it is saved? –In pipelined/superscalar systems, when the interrupt occurs, the program counter may not reflect the correct boundary between executed and non-executed instructions. Skip p 280 下 半至 282
26
25 5.2 Goals of the I/O Software Concepts in the design of I/O software –Device independence programs can access any I/O device without specifying device in advance. Ex., read a file from floppy, hard drive, or CD-ROM –Uniform naming name of a file or device should be a string or an integer, and not depend on the device. Ex., all files and devices are addressed by a path name –Error handling Errors handled as close to the hardware as possible The upper layers should be told about an error only if the lower layers are not able to deal with the problem
27
26 Goals of the I/O Software –Synchronous vs. asynchronous transfers blocked transfers (easy to program) vs. interrupt-driven (most physical I/O is asynchronous) –Buffering Often data coming off a device cannot be stored in its final destination. Ex: packets off the network. Some devices have severe real-time constraints, so the data must be put into the output buffer in advance to avoid buffer under- runs ( 缺貨 ). Involves considerable copying and has a major impact on I/O performance –Sharable vs. dedicated devices Some devices, like disks, are sharable Some devices, like tape drives, should be dedicated Dedicated devices introduces a variety of problems, such as deadlocks
28
27 Programmed I/O Three ways to perform I/O: –Programmed I/O, –interrupt-driven I/O, –I/O using DMA Programmed I/O: –Have the CPU do all the work
29
28 Programmed I/O Steps in printing a string
30
29 Programmed I/O 1.First assembles the string in a buffer in the user space 2.The user process then acquires the printer by making a system call to open it. 3.The OS then copies the buffer with the string to an array, say p, in the kernel space. As soon as the printer is available, the OS copies the first character to the printer’s data register, in this case using memory-mapped I/O. 4.After that, the OS checks to see if the printer is ready to accept another one. The printer indicates its availability by setting some bit in its status register or putting some value in it. 5.The OS now waits for the printer to become ready again. When that happened, it prints the next character. This loop continues until the entire string has been printed. Then control returns to the user process.
31
30 Programmed I/O Writing a string to the printer using programmed I/O. After outputting a character, the CPU continuously polls the device to see if it is ready to accept another. This is called polling or busy waiting. However, each character takes about 10 ms to print. It is simple, but tied up the CPU till I/O is done
32
31 Interrupt-Driven I/O Writing a string to the printer using interrupt-driven I/O (a) Code executed when print system call is made (b)Interrupt service procedure
33
32 Interrupt-Driven I/O When the printer has printed the character and is prepared to accept the next one, it generates an interrupt. The interrupt stops the current process and saves its state. Then the printer interrupt service procedure is run. Disadvantage: an interrupt occurs on every character
34
33 I/O Using DMA(Direct Memory Access) (a) code executed when the print system call is made Idea: let the DMA controller feed the character to the printer one at a time, without the CPU being bothered. Reduce the number of interrupts from one per character to one per buffer If DMA is not capable of driving the device at full speed or the CPU has nothing to do while waiting for the DMA interrupt, then interrupt-driven I/O or programmed I/O may be better. (b)interrupt service procedure
35
34 I/O Software Layers Layers of the I/O Software System
36
35 Interrupt Handlers Interrupt handlers are best hidden –have driver starting an I/O operation block until I/O has completed and the interrupt occurs. Like doing a down on a semaphore, a wait on a condition variable, or a receive on a message. –Then the interrupt procedure does its task to handle the interrupt –Then the interrupt procedure unblocks the driver that started it (like an up on a semaphore) –This models works best if drivers are structured as kernel processes, with their own states, stacks, and program counters.
37
36 Interrupt Handlers Steps must be performed in software after the hardware interrupt has completed 1.Save any registers (like PSW) not already saved by the interrupt hardware 2.Set up a context for the interrupt service procedure (TLB, MMU, page tables) 3.Set up a stack for the interrupt service procedure 4.Acknowledge the interrupt controller. If there is no centralized interrupt controller, reenable interrupts 5.Copy the registers from where they were saved to the process table
38
37 Interrupt Handlers 6.Run the interrupt service procedure. It will extract info from the interrupting device controller’s registers 7.Choose which process to run next. If the interrupt has caused some high-priority process that was blocked to become ready, it may be chosen to run now. 8.Set up the MMU context for the process to run next. Some TLB set up may be needed. 9.Load new process' registers, including PSW 10.Start running the new process
39
38 Device Drivers The number of device registers and the nature of commands vary radically. Ex: mouse driver has to accept information about how far the mouse has moved and which button are currently pressed. What information does a disk driver need? Each I/O device needs some device-specific code for controlling it. This code, called device driver, is generally written by the device’s manufacturer and delivered along with the device for each OS.
40
39 Device Drivers Each device driver normally handles one device type, at most, one class of closed related devices. Ex: SCSI disk driver. In order to access the device’s hardware, meaning the controller’s registers, the device driver normally has to be part of the OS kernel. The designer of the architecture of OS needs to have a well-defined model of what a driver does and how it interacts with the rest of the OS.
41
40 Device Drivers Logical position of device drivers is shown here. In reality all communication between drivers and device controllers goes over the bus.
42
41 Device Drivers Device driver categories –Block device: Read a block for block device –Character device: Write a character string for character device In Unix, OS is a single binary program that contains all of the drivers that it will need compiled into it. In PC, starting with MS-DOS, drivers were dynamically loaded into the system during execution
43
42 Device Drivers Device driver functions: –Accept abstract read and write requests and see that they are carried out. –Initialize the device, if needed –Manage power requirement –log events
44
43 Device Drivers General structure of device driver program 1.Checking whether input parameters are valid –If yes, a translation from abstract to concrete terms may be needed –If not, an error is returned 2.Check if the device is currently in use –If yes, the request is queued for later processing –If no, check hardware status to see if the request could be handled now 3.It may be necessary to switch the device on or start a motor before transfers can be begun. Once the device is on and ready to go, the actual control can begin
45
44 Device Driver After the driver knows which command it is going to issue, the driver 1.Write commands into the controller’s device registers. Check to see if the controller accepted the command and is prepared to accept the next one 2.After the commands have been issued, two cases: Wait until the controller does some work for it, so it blocks itself until the interrupt comes in to unblock it The operation finishes with no delay, like scrolling the screen in the character mode 3.After the operation has been completed, check for errors and If OK, the driver may have data to pass to the device- independent software. 4.Finally, it returns status info to its caller. –If other requests are queued, one of them will be selected and started. Otherwise, the driver blocks waiting for the next request.
46
45 Device Drivers Other issues –An I/O device may complete while a driver is running, interrupting the driver. Ex: while the network driver is processing an incoming packet, another packet may arrive. Drivers have to be reentrant, meaning a running driver has to expect that it will be called a second time before the first call has completed. –Handle hot pluggable system: When the user suddenly removed a device from the system, not only must the current I/O be aborted, but pending requests for that device must be gracefully removed from the system. Unexpected adding of new devices may cause the kernel to juggle resources ( 如: interrupt request lines) –Drivers may need to call certain kernel procedures Ex: to manage MMU, timer, DMA controller, interrupt controller, etc.
47
46 Device-Independent I/O Software Functions of the device-independent I/O software Uniform interfacing for device drivers Buffering Error reporting (skipped) Allocating and releasing dedicate devices (skipped) Providing a device-independent block size (skipped) Make all I/O devices and drivers look the same
48
47 Uniform Interface for Device Drivers (a) Without a standard driver interface (b) With a standard driver interface
49
48 Uniform Interface for Device Drivers Issues –The interface between the device drivers and the rest of the OS –How I/O devices are named: mapping symbolic device names onto the proper driver. In I-node of UNIX, major device number to locate the driver, minor device number to specify the unit to the read or written –Protection: how does the system prevent users from accessing devices that they are not entitled to use. Usual protection rules for files also apply to I/O devices
50
49 Buffering
51
50 Buffering (a) Unbuffered input: each arriving character causes an interrupt (b) Buffering in user space: problem: what happens if the buffer is paged out when a character arrives? (c) Buffering in the kernel followed by copying to user space: problem: what happens to characters that arrive while the page with the user buffer is being brought in from the disk? (d) Double buffering in the kernel: while one is being copied to user space, the other is accumulating input. Output buffer? Use (c) 但方向要相反
52
51 Buffering Networking may involve many copies
53
52 Buffering 1.The kernel copies the packet to a kernel buffer to allow the user to proceed immediately. 2.When the driver is called, it copies the packet to the controller for output. 3.The packet is copied out onto the network. 4.The packet is copied to the receiver’s kernel buffer. 5.The packet is copied to the receiving process’ buffer. Usually an acknowledgement is sent back. When the sender gets the acknowledge, it is free to send the next packet. All the copying will slow down the transmission rate since all the steps must happen sequentially. Skip 5.3.3 from p. 297 to 298
54
53 User-Space I/O Software Library procedures –Put their parameters in the proper place for the system call. Ex: count=write(fd,buffer,nbytes) –Do real work. Ex: formatting I/O by printf, scanf Spooling System: a way of dealing with dedicated I/O devices in a multiprogramming system. Create a daemon and a spooling directory. –To print a file, a process first generates the entire file to be printed and puts it in the spooling directory –It is up to the daemon to print the files in the directory. –News server is another example.
55
54 User-Space I/O Software Layers of the I/O system and the main functions of each layer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.