I/O System Architecture

  1. Buses

    • Data paths that used to communicate information with the CPU(s), RAM and I/O Devices.
    • CPU is attached to the main memory of the system via a memory bus.
    • Some devices are connected to the system via a general I/O bus.
  2. I/O Bus

    • data path that connects CPU to I/O Devices.
    • I/O Device is connected to I/O bus by three hardware components:
      1. I/O ports
      2. interfaces
      3. device controllers

Canonical Device

Hardware Interface

  • Allows the system software to control its operation

  • Operating system can control the device behavior by reading and writing the following registers:

    1. status register

      • see the current status of the device
    2. command register

      • tell the device to perform a certain task
    3. data register

      • pass data to the device or get data from the device
  • Tipical interaction example:

    1
    2
    3
    4
    5
    6
    7
    while ( STATUS == BUSY) ; // wait until device is not busy

    // write data to data register
    // write command to command register
    // starts the device and executes the command

    while ( STATUS == BUSY ) ; // wait until device is done with your request

Internals

  • Implement the specific operations

General Mechanisms

Polling

  • Operating system waits until the device is ready by repeatedly reading the status register.

  • Advantages:

    • simple and it works.
  • Disadvantages:

    • it wastes CPU time for just waiting for the device,
      so switching to another process during the waiting time may be a better utilize of CPU.

Interrupt

  • Put the I/O request process to sleep and context switch to another process.

  • When the device is finished, wake the process (waiting for the device finished) by interrupt.

  • Advantages:

    • allow CPU and the disk are properly utilized.
  • Noted: interrupt is not always the best

    • Because context switch is expensive, if the device performs very fast, then interrupt will slow down the system.
      • If the device is fast, then polling is the best
      • Otherwise, interrupt is the best

Direct Memory Access

  1. Problems:

    • CPU wastes a lot of time to copy a large chunk of data from memory to the device.
  2. Solution: Direct Memory Access DMA

    • Copy data in memory by knowing the location of data in memory and the number of data block need to copy.
    • When the copying finished, DMA raises an interrupt on the disk.

Device Interacftions

  • The communications between operating system and device

    1. I/O Instructions

      • a way for the OS to send data to specific device registers
      • e.g., in and out instructions on x86.
    2. Memory-mapped I/O

      • a specific area inside the memory for the I/O device (those device registers are available as memory locations).
      • i.e., the OS load (read) and store (write) to the device, instead of the main memory.

File System Abstraction

  • The abstraction excapsulate the specifics of device interaction which lets the OS can interact with different specific interfaces.

  • Problems:

    • If there is a device having many special undefined functions, then these functions will go unused in the generic interface layer
    • Over 70% of OS code is found in device drives
      • it takes lots of times to drive the devices
      • those devices are the primary contributors to kernel crashes

Simple IDE Disk Driver

Registers

  1. Control Register

    • Address 0x3F6 = 0X80 (0000 1RE0):
      • R = reset
      • E = 0 means “enable interrupt”
  2. Command Block Registers

    • Address 0x1F0 = Data Port
    • Address 0x1F1 = Error
    • Address 0x1F2 = Sector Count
    • Address 0x1F3 = LBA low byte
    • Address 0x1F4 = LBA mid byte
    • Address 0x1F5 = LBA hi byte
    • Address 0x1F6 = 1B1D TOP4LBA : B = LBA, D = drive
    • Address 0x1F7 = Command / Status
  3. Status Register (Address 0x1F7):

    • 7 : BUSY
    • 6 : READY
    • 5 : FAULT
    • 4 : SEEK
    • 3 : DRQ
    • 2 : CORR
    • 1 : IDDEX
    • 0 : ERROR
  4. Error Register (Address 0x1F1)

    • 7 : BBK (Bad Block)
    • 6 : UNC (Uncorrectable data error)
    • 5 : MC (Media Changed)
    • 4 : IDNF (ID mark Not Found)
    • 3 : MCR (Media Change Requested)
    • 2 : ABRT (Command aborted)
    • 1 : T0NF (Track 0 Not Found)
    • 0 : AMNF (Address Mark Not Found)

Operations

  1. Wait for drive to be ready

    • Read Status Register until drive is not busy and READY.
  2. Write parameters to command registers

    • Wite the sector count, logical block address (LBA) of the sectors to be accessed, and drive number (master=0x00 or slave=0x10, as IDE permits just two drives) to command registers (0x1F2 - 0x1F6).
  3. Start the I/O

    • Issuing read/ write to command register.
    • Write READ—WRITE command to command register (0x1F7).
  4. Data transfer (for writes)

    • Wait until the drive status is READY and DRQ, write data to data port.
  5. Handle interrupts

    • In the simplest case, handle an interrupt for each sector transferred.
    • More complex approaches allow batching.
  6. Handle errors

    • After each operation, read the status register. If the ERROR bit is on, read the error register for details.