Device

Document Sample
Device Powered By Docstoc
					                                                                                     Input and Output
                                                                                         A computer’s job is to process data
                     COS 318: Operating Systems                                               Computation (CPU, cache, and memory)
                                                                                              Move data into and out of a system (between I/O devices
                                                                                               and memory)
                     I/O Device and Drivers                                              Challenges with I/O devices
                                                                                              Different categories: storage, networking, displays, etc.
                                                                                              Large number of device drivers to support
                                                                                              Device drivers run in kernel mode and can crash systems
                                                                                         Goals of the OS
                                                                                              Provide a generic, consistent, convenient and reliable way to
                                                                                               access I/O devices
                                                                                              As device-independent as possible
                                                                                              Don’t hurt the performance capability of the I/O system too
                                                                                               much
                                                                                                                                                                        2




Revisit Hardware                                                                     Definitions and General Method
    Compute hardware                                                                    Overhead
         CPU and caches                                                                      Time that the CPU is tied up initiating/
                                                    CPU
                                                     CPU                                       ending an operation                           Initiate   Data transfer
         Chipset                                     CPU
         Memory                                       CPU                               Latency
                                                                                              Time to transfer one bit (typ. byte)
                                                                                              Overhead + 1 bit reaches destination
    I/O Hardware                          Memory
                                                                                         Bandwidth
         I/O bus or interconnect                            I/O bus                          Rate of I/O transfer, once initiated
         I/O controller or adaptor                                                           Mbytes/sec
         I/O device                                                                     General method
                                                                                              Higher level abstractions of byte transfers
    Two types of I/O                                                  Network                Batch transfers into block I/O for
         Programmed I/O (PIO)                                                                 efficiency to amortize overhead and
           •  CPU does the work of moving data
                                                                                               latency over a large unit
         Direct Memory Access (DMA)
           •  CPU offloads the work of moving
              data to DMA controller

                                                                                 3                                                                                      4




                                                                                                                                                                            1
Programmed Input Device                                Programmed Output Device
    Device controller                                     Device
          Status register                                      Status registers (ready, busy, … )
           ready: tells if the host is done                     Data registers
           busy: tells if the controller is done
           int: interrupt
                                                           Example
           …                                                    A serial output device
          Data registers                                  Perform an output
    A simple mouse design                                      Wait until ready bit is clear
          Put (X, Y) in data registers on a                    Poll the busy bit
           move                                                 Writes the data to register(s)
          Interrupt                                            Set ready bit
    Input on an interrupt                                      Controller sets busy bit and
          Read values in X, Y registers                         transfers data
          Set ready bit                                        Controller clears the ready bit and
                                                                 busy bit
          Wake up a process/thread or
           execute a piece of code

                                                   5                                                           6




Direct Memory Access (DMA)                             I/O Software Stack
    DMA controller or adaptor
          Status register
           (ready, busy, interrupt, …)
          DMA command register                                                    User-Level I/O Software
          DMA register (address, size)
          DMA buffer                                                                Device-Independent
    Host CPU initiates DMA                                                             OS software
          Device driver call (kernel mode)
          Wait until DMA device is free                                                   Device Drivers
          Initiate a DMA transaction
           (command, memory address, size)
          Block                                                                          Interrupt handlers
    Controller performs DMA
          DMA data to device
           (size--; address++)                                                               Hardware
          Interrupt on completion (size == 0)
    Interrupt handler (on completion)
          Wakeup the blocked process



                                                   7                                                           8




                                                                                                                   2
Recall Interrupt Handling                                                    Device Drivers
  Save context (registers that hw hasn’t saved, PSW etc)
                                                                                                    Device                                   Device
  Mask interrupts if needed                                                      Device
                                                                                                   controller                                driver




                                                                                                                      Interrupt Handling
  Set up a context for interrupt service

  Set up a stack for interrupt service                                                             Device                                   Device
                                                                                  Device
                                                                                                   controller                                driver
                                                                                                                                                        Rest of the
  Acknowledge interrupt controller, perhaps enable it                                                                                                  operating
                                                                                                        .
                                                                                                        .                                      .
                                                                                                                                               .         system
  Save entire context to PCB                                                                           .                                      .
  Run the interrupt service                                                      Device
                                                                                                    Device                                   Device
  Unmask interrupts if needed                                                                     controller                                driver
                                                                                  Device
  Possibly change the priority of the process
                                                                                                                                           I/O System
  Run the scheduler
                                                                                   Manage the complexity and differences among specific types of
  Then OS will set up context for next process, load                               devices (disk vs. mouse, different types of disks …)
   registers and PSW, start running process …                                      Each handles one type of device or small class of them (eg SCSI)
                                                                        9                                                                                             10




Typical Device Driver Design                                                 Simplified Device Driver Behavior
    Operating system and driver communication                                   Check input parameters for validity, and translate them to device-
         Commands and data between OS and device drivers                         specific language
                                                                                 Check if device is free (wait or block if not)
    Driver and hardware communication
                                                                                 Issue commands to control device
         Commands and data between driver and hardware
                                                                                       Write them into device controller’s registers
    Driver responsibilities                                                           Check after each if device is ready for next (wait or block if not)
         Initialize devices                                                     Block or wait for controller to finish work
         Interpreting commands from OS                                          Check for errors, and pass data to device-indept software
         Schedule multiple outstanding requests                                 Return status information
         Manage data transfers                                                  Process next queued request, or block waitng for next
         Accept and process interrupts                                          Challenges:
         Maintain the integrity of driver and kernel data structures                  Must be reentrant (can be called by an interrupt while running)
                                                                                       Handle hot-pluggable devices and device removal while running
                                                                                       Complex and many of them; bugs in them can crash system

                                                                        11                                                                                            12




                                                                                                                                                                           3
Types of I/O Devices                                                        Typical Device Speeds
    Block devices                                                              Keyboard                            10       B/s
                                                                                Mouse                               100      B/s
         Organize data in fixed-size blocks
                                                                                Compact Flash card                  40       MB/s
         Transfers are in units of blocks                                      USB 2.0                             60       MB/s
         Blocks have addresses and data are therefore addressable              52x CD-ROM                          7.8 MB/s
         E.g. hard disks, USB disks, CD-ROMs                                   Scanner                             400      KB/s
                                                                                56K modem                           7        KB/s
    Character devices
                                                                                802.11g wireless net                6.75     MB/s
         Delivers or accepts a stream of characters, no block structure        Gigabit Ethernet                    320      MB/s
         Not addressable, no seeks                                             FireWire-1                          50       MB/s
         Can read from stream or write to stream                               FireWire 800                        100      MB/s
         Printers, network interfaces, terminals                               SCSI Ultra-2 disk                   80       MB/s
                                                                                SATA disk                           300      MB/s
    Like everything, not a perfect classification                              PCI bus                             528      MB/s
         E.g. tape drives have blocks but not randomly accessed                Ultrium tape                        320      MB/s
         Clocks are I/O devices that just generate interrupts
                                                                       13                                                                                 14




Device Driver Interface                                                     Character and Block Device Interfaces
    Open( deviceNumber )                                                       Character device interface
         Initialization and allocate resources (buffers)                             read( deviceNumber, bufferAddr, size )
    Close( deviceNumber )                                                              •  Reads “size” bytes from a byte stream device to “bufferAddr”
         Cleanup, deallocate, and possibly turnoff                                   write( deviceNumber, bufferAddr, size )
                                                                                        •  Write “size” bytes from “bufferAddr” to a byte stream device
    Device driver types
         Block: fixed sized block data transfer
                                                                                Block device interface
         Character: variable sized data transfer                                     read( deviceNumber, deviceAddr, bufferAddr )
                                                                                        •  Transfer a block of data from “deviceAddr” to “bufferAddr”
         Terminal: character driver with terminal control
                                                                                      write( deviceNumber, deviceAddr, bufferAddr )
         Network: streams for networking
                                                                                        •  Transfer a block of data from “bufferAddr” to “deviceAddr”
    Interfaces for block and character/stream oriented                               seek( deviceNumber, deviceAddress )
     devices (at least) are different                                                   •  Move the head to the correct position
         Like to preserve same interface within each category                          •  Usually not necessary

                                                                       15                                                                                 16




                                                                                                                                                               4
Unix Device Driver Interface Entry Points                                            Synchronous vs. Asynchronous I/O
    init()                                                                              Synchronous I/O
          Initialize hardware
    start()                                                                                  read() or write() will block a user process until its completion
          Boot time initialization (require system services)                                 OS overlaps synchronous I/O with another process
    open(dev, flag, id) and close(dev, flag, id)
          Initialization resources for read or write, and release afterwards
                                                                                         Asynchronous I/O
    halt()                                                                                   read() or write() will not block a user process
          Call before the system is shutdown                                                 user process can do other things before I/O completion
    intr(vector)                                                                             I/O completion will notify the user process
          Called by the kernel on a hardware interrupt
    read(…) and write() calls
          Data transfer
    poll(pri)
          Called by the kernel 25 to 100 times a second
    ioctl(dev, cmd, arg, mode)
          special request processing


                                                                                17                                                                                18




Detailed Steps of Blocked Read                                                       Asynchronous I/O
    A process issues a read call which executes a system call                           API
    System call code checks for correctness                                                  Non-blocking read() and write()
    If it needs to perform I/O, it will issues a device driver call                          Status checking call
    Device driver allocates a buffer for read and schedules I/O                              Notification call
    Controller performs DMA data transfer                                                    Different form the synchronous I/O API
    Block the current process and schedule a ready process                              Implementation
    Device generates an interrupt on completion                                              On a write
    Interrupt handler stores any data and notifies completion                                  •  Copy to a system buffer, initiate the write and return
    Move data from kernel buffer to user buffer                                                •  Interrupt on completion or check status
    Wakeup blocked process (make it ready)                                                   On a read
    User process continues when it is scheduled to run                                         •  Copy data from a system buffer if the data are there
                                                                                                •  Otherwise, return with a special status



                                                                                19                                                                                20




                                                                                                                                                                       5
Why Buffering?                                                             Think About Performance
    Speed mismatch between the producer and consumer                          A terminal connects to computer via a serial line
         Character device and block device, for example                            Type character and get characters back to display
         Adapt different data transfer sizes (packets vs. streams)                 RS-232 is bit serial: start bit, character code, stop bit (9600
    Deal with address translation                                                   baud)
         I/O devices see physical memory                                      Do we have any cycles left?
         User programs use virtual memory                                          What should the overhead of an interrupt be
    Caching                                                                   Technique to minimize interrupt overhead
         Avoid I/O operations                                                      Interrupt coalescing
    User-level and kernel-level buffering
    Spooling
         Avoid user processes holding up resources in multi-user
          environment

                                                                      21                                                                                                          22




Other Design Issues                                                        Dynamic Binding: Indirection
  Build      device drivers
                                                                                       Open( 1, … );
         Statically
           •  A new device driver requires reboot OS
                                                                                           Indirect table




                                                                                                            Driver-kernel interface
         Dynamically                                                                                                                                       Driver for device 0
           •  Download a device driver without rebooting OS                                                                                                  open(…) {
           •  Almost every modern OS has this capability                                                                                                     }
                                                                                                                                                            …
  How       to down load device driver dynamically?                                                                                                         read(…) {
                                                                                                                                      Driver for device 1
       Load drivers into kernel memory                                                   Interrupt                                                          }

       Install entry points and maintain related data structures                         handlers                                     open(…) {
                                                                                                                                       }
       Initialize the device drivers                                                                                                 …
                                                                                           Other                                       read(…) {
                                                                                                                                       }
                                                                                           Kernel
                                                                                          services
                                                                      23                                                                                                          24




                                                                                                                                                                                       6
Issues with Device Drivers                                                 Summary
    Flexible for users, ISVs and IHVs                                         Device controllers
         Users can download and install device drivers                             Programmed I/O is simple but inefficient
         Vendors can work with open hardware platforms                             DMA is efficient (asynchronous) and complex
    Dangerous methods                                                         Device drivers
         Device drivers run in kernel mode                                         Dominate the code size of OS
         Bad device drivers can cause kernel crashes and introduce                 Dynamic binding is desirable for desktops or laptops
          security holes                                                            Device drivers can introduce security holes
                                                                                    Progress on secure code for device drivers but completely
    Progress on making device driver more secure                                    removing device driver security is still an open problem
         Checking device driver codes
         Build state machines for device drivers



                                                                      25                                                                         26




                                                                                                                                                      7

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:11/28/2011
language:English
pages:7