# Overview

I/O 裝置在功能與速度方面變化極大 (滑鼠、硬碟、光碟機),因此需要許多不同的功能來控制

  • I/O 裝置目前有兩個相互衝突的發展方向
    • 軟體與硬體介面逐漸趨於標準化,有助於我們將裝置整合至電腦
  • I/O 裝置的種類與功能不斷增加,有些新裝置與舊裝置差異極大,將裝置整合至電腦變的很有挑戰性
  • Device drivers
    裝置驅動程式 (Device drivers) 讓 I/O subsystem 可取得設備控制的介面,類似 apps & OS 之間的系統呼叫
  • Device 分類 – 不同類型去設計不同的 interface
    • Storage devices:disks, tapes
    • Transmission devices:network cards, modems
    • Human-interface devices:keyboard, screen, mouse
    • Specialized devices:joystick, touchpad

# I/O Hardware

  • Port
    • Host 與 I/O devices 之間的連接點
    • E.g.:USB ports
  • Bus
    • 一組 wires 和資料如何傳輸的 protocol,如 PCI bus
    • E.g.:PCI bus
  • Controller
    • A collection of electronics 可以操控 port,bus,device
    • controller 可以有他自己的 processor 和 memory, etc. (E.g.: SCSI controller)
  • Typical PC Bus Structure
    • 北橋晶片控制比較快的 controller,bw、freq 高
    • 南橋晶片接傳統 ide,比較慢的

# I/O Method

# Basic I/O Method

  • 每個 I/O port (device) 被指定一個獨特的 port address,一開始就燒死與軟體無關
  • 每個 I/O port 包含四個 registers (1 ~ 4 Bytes)
    • 資料輸入暫存器 (data-in register):由主機讀取輸入
    • 資料輸出暫存器 (data-out register):由主機寫入要輸出資料
    • 狀態暫存器 (status register):包含可被主機讀取的位元資料,這些位元指出是否可讀、裝置錯誤的狀態
    • 控制暫存器 (control register):用以起始指令或改變裝置模式
  • Program 與 I/O port 的互動透過 special I/O instructions,i/o instruction 裡一定有 port address

# I/O Method Categorization

  • Depending on how to address a device
    • Port-mapped I/O
      • 與 memory 用不一樣的 address space
      • 透過 special I/O instruction (e.g. IN, OUT) 做存取
    • Memory-mapped I/O
      • 保留特別的 memory space 給 device
      • 透過標準的 data-transfer instruction (e.g. MOV) 做存取
      • 寫到 memory 等於寫到某個 device 的 buffer,要事先 register 好
      • 優點:對大量的 memory I/O 更有效率 (e.g. graphic card)
      • 缺點:Vulnerable to accidental modification, error
  • Depending on how to interact with a device
    • Poll (busy-waiting):processor 週期性的檢查 device 的 status register
    • Interrupt:device 通知 processor I/O 的完成,若是資料量大,選擇用 interrupt
  • Depending on who to control the transfer
    • Programmed I/O:由 CPU 控制
      • CPU 會一直接收到 interrupt,速度變慢
    • Direct memory access (DMA) I/O:由 DMA controller 控制 (一個有特別目的的 controller)
      • 由 I/O controller 負責搬資料,不屬於任何一個 I/O device
      • 設計給大量資料傳輸
      • 通常搭配使用 memory-mapped I/O 和 interrupt I/O method

# Interrupt

  • Interrupt Vector Table
  • Interrupt Prioritization
    • Maskable interrupt
      • 參照 IRQ 透過設定中斷遮罩暫存器(interrupt mask register; IMR)內的位元遮罩(bit-mask)值來讓它可被系統忽略
      • 比當前較低優先度的 interrupt 不被認定直到 pending interrupt 完成
    • Non-maskable interrupt (NMI):highest-priority, never masked
      • 無法被系統忽略的
      • NMI 通常使用於計時器(timers)
      • Often used for power-down, memory error
  • Interrupt-Driven I/O
  • CPU and device Interrupt handshake
    1. Device asserts interrupt request (IRQ)
    2. CPU 在每個 instruction cycle 的開始檢查 interrupt request line
    3. Save the status and address of interrupted process
    4. CPU 認知 interrupt,接著在 interrupt vector table 搜尋 interrupt handler routines
    5. CPU 從 interrupt handler routine 抓下一個 instruction
    6. 在完成執行 interrupt handler routine 後恢復 interrupted process

# Kernel I/O Subsystem

  • I/O 排班程式:透過 I/O queue 中任務的排序,提昇系統效能
    • 當核心支援非同步 I/O 時,必須能同一時間記錄許多 I/O 需求
    • e.g. disk I/O order scheduling
  • 緩衝 (buffer):資料在 I/O devices 之間傳輸時,將資料存在記憶體中
    • 使用原因:為了解決生產者和消費者速度不相等
    • 在不同資料傳輸大小的裝置之間做調整
    • 提供應用 I/O 的複製語法
  • 快取 (cache):持有資料的快速記憶體
    • 與緩衝的不同是,緩衝可能只有持有資料項的現備份,快取持有一個存在於其他位置之項目的複本在快速儲存體
  • spooling 和裝置預約
    • spoolilng 是一個用來保留裝置輸出的緩衝器
      • 它不能接收交錯的資料串列
      • 作業系統採用攔截輸出的方式來解決
    • 裝置預約 (Device reservation)
      • 提供對設備的互斥存取
      • 必須注意死結問題
  • 錯誤處理 (error handle)
    • 作業系統可以從錯誤中回復
    • 大部分 I/O 送回一位元的資訊,以便說明此呼叫的執行狀態為成功或失敗
    • 系統 ** 錯誤日誌 (error log)** 記錄錯誤訊息
  • I/O 保護 (I/O Protection)
    • 為了避免使用者執行非法操作,定義所有 I/O 指令為特權指令,必須由作業系統發出
    • 任何記憶體映對和 I/O 阜記憶體位置必須被保護

# Performance

I/O 在系統效能上是一個主要的因素,因其

  • 讓 CPU 執行 device driver code
  • 每 call 一層 driver,subsystem 都是不同的 process,需要 context switches
  • 因為有一堆 layer,controllers、buffer、 cache 有一堆的 data copy
  • Interrupt handling is a relatively expensive task
    當 I/O 的時間少,Busy-waiting 可以比 interrupt-driven 表現的更以效率

# Improving Performance

  • 減少 context switches 的次數
  • 減少 data copying
  • 藉由 large transfers,smart controllers,polling 減少 interrupts
  • Use DMA,CPU 可以空出來
  • Balance CPU,memory,bus,and I/O performance for highest throughput

# Intercomputer Communications

  • Network traffic could cause high context switch rate
  • Interrupt generated during keyboard & network I/O
  • Context switch occurs between prog. & kernel (drivers)

# Application I/O Interface

  • Device drivers 讓 I/O subsystem 可取得設備控制的介面,並隱藏這些 device controllers 之間的差異
  • Characteristics of I/O Devices

# I/O Device Class

  • Device class is fairly standard across different OS
    • Block I/O
    • Char-stream I/O
    • Memory-mapped file access
    • Network sockets
    • Clock & timer interfaces
  • Back-door interfaces (e.g. ioctl () ) 難以歸類到以上的類別
    • call system call:ioctl () 完後,可以去操作任何的 I/O device
    • 使 application 可以任意 access 被 device driver 實作函式,而不用透過 system call

# Block & Char Devices

  • Block devices
    • system calls:read (), write (), seek ( ),一整個 memory buffer 的概念
    • Memory-mapped file can be layered on top
    • 大的,用 interrupt 做 I/O
  • Char devices
    • system calls:get (), put (),以 bytes 為單位
    • Libraries layered on top allow line editing
    • 小的,用 polling 做 I/O

# Network Devices

  • 有很多種形式(block、character…),對於個別有自己的 interface
    • System call: send(), recv(), select()
    • select() returns which socket is waiting to send or receive, eliminates the need of busy waiting
  • Many other approaches
    • pipes, FIFOS, STREAMS, message queues

# Ref Notes

  • Kipper’s Note
  • Chang-Chia-Chi’s Note
  • 陳品媛的筆記