1 of 25

微自幹的作業系統輕旅行

Ian Chen

2 of 25

About me

  • NYCU CS
  • Google DSC Lead @ NCTU
  • COSCUP 2021 IT Team Lead
  • 社群分享
    • SITCON
    • COSCUP
    • 六角學院
    • 快樂學程式

3 of 25

Preliminary work

  • 重新學習 C 語言
  • 學習 System Programing (CS241@ Illinois)
  • RISC-V Spec
  • RISC-V 架構與嵌入式開發快速入門
  • OS 恐龍本
  • Trace 相關專案的原始碼

4 of 25

Outline

1. RISC-V 沿革

2. Instruction Set

3. Calling Convention

4. Interrupt & Exception Handle

5. Trace the source code of mini-riscv-os

5 of 25

The Skills you got to know

1. Virtual Machine or Evaluation Board

2. RISC-V GNU Compiler Toolchain

3. Git

4. C Programming Skill

5. Assembly code

6 of 25

The History of RISC-V

  • 由 UC Berkeley 發起
  • 開源硬體
  • 有前途
  • 怎麼會接觸到它?

7 of 25

Instruction Set - RV32I

  • Bit 20 - Bit 31: 用來表示立即數 (立即尋址方式指令中給出的數)
  • Bit 15 - Bit 19: 表示 rs1 (Input 暫存器)
  • Bit 12 - Bit 14: 表示 funct (確定 Type 後,識別指令用)
  • Bit 7 - Bit 11: 表示 rd (目標暫存器)
  • Bit 0 - Bit 6: 為 opcode (用於辨別 Type)

8 of 25

Privileged architecture

  • U/S/M Mode 各自持有一組 CSR 暫存器
  • 不同的特權模式對應到不同的執行權限
    • M mode 通常用來執行 Trusted Program
    • S mode 執行 OS Kernel
    • U mode 執行 User Application
  • 本議程介紹的專案僅用到 Machine Mode

Reference: The RISC-V Instruction Set Manual

9 of 25

Calling Convention

  • Caller save?
  • Callee save?

Reference: The RISC-V Instruction Set Manual

10 of 25

Calling Convention

a0

a1

a0

Reference: The RISC-V Instruction Set Manual

11 of 25

Control and Status Register

  • U/S/M Mode 各自持有一組 CSR 暫存器
  • 用於設定或是紀錄 CPU 的運作狀況
  • 本議程只會提到 Machine Mode 下的 CSR
  • 預設情況下,任何 Mode 發生的異常都會導向 M mode
    • 可以透過設置 mideleg/medeleg 將異常委託給 S mode 處理

Reference: The RISC-V Instruction Set Manual

12 of 25

Control and Status Register

  • mtvec: 當進入異常時,PC (Program counter) 會進入 mtvec 所指向的地址繼續執行。
  • mcause: 紀載異常的原因。
  • mtval: 紀載異常訊息。
  • mepc: 進入異常前 PC 所指向的地址。若異常處理完畢,Program counter 可以讀取該位址繼續執行。
  • mstatus: 進入異常時,硬體會更新 mstatus 寄存器的某些域值。
  • mie: 決定中斷是否被處理。
  • mip: 反映不同類型中斷的等待狀態。

Reference: The RISC-V Instruction Set Manual

13 of 25

Interrupt & Exception Handle

Trap

更新 mcause

更新 mepc

更新 mtval

更新 mstatus

退出異常狀態

  • 硬體會記錄中斷發生的原因,並更新到 mcause
  • 中斷發生前,處理器本來要執行的下一條指令 (PC + 4)

https://github.com/ianchen0119/AwesomeCS/wiki/

  • 紀錄造成異常發生的暫存器訪問地址或是指令編碼
  • 中斷 Enable (MIE)、特權模式間的切換 (MPP)
  • 執行 mepc 紀錄的指令位址
  • 更新 mstatus (MIE = MPIE, MPIE = 1)
  • 作業系統會維護一份中斷向量表 (mtvec 會儲存它的位址)
  • 當中斷 or 異常發生,會跳進 mtvec 所指的位址開始執行

14 of 25

Intro: mini-riscv-os

  • 執行在 RISC-V 平台上的輕量 OS Kernel
  • 受到 mini-arm-os 啟發
  • 該專案由陳鍾誠老師發起
  • 完整的程式碼範例和中文文檔
  • 規模大概 1000 行 (易讀好學)

https://github.com/cccriscv/mini-riscv-os

15 of 25

Trace the source code !

探討目標: MINI-RISCV-OS 的中斷處理 (07-ExterInterrupt)

  • Timer interrupt (任務切換)
  • External interrupt (鍵盤輸入)

https://github.com/cccriscv/mini-riscv-os/blob/master/07-ExterInterrupt/os.c

16 of 25

Timer interrupt

  • RISC-V 有兩組暫存器用於觸發時間中斷:
    • MTIME
    • MTIMECMP
  • MTIME > MTIMECMP => 時間中斷!
  • 用途?

17 of 25

Timer interrupt

  • RISC-V 有兩組暫存器用於觸發時間中斷:
    • MTIME
    • MTIMECMP
  • MTIME > MTIMECMP => 時間中斷!
  • 用途? 待會揭曉 m(_ _)m

18 of 25

External Interrupt

  • PLIC (Platform-Level Interrupt Controller) 是為了 RISC-V 平台所打造的 PIC。
  • 設定 IRQ 的 Priority (VirtIO IRQ, UART IRQ…)
  • 0xc000000 (PLIC_BASE) + offset = Mapped Address of register

https://github.com/qemu/qemu/blob/master/hw/riscv/virt.c

19 of 25

Trap Handler

  • 如何讓系統在發生中斷時跳到 trap_handler
    • trap_init
    • trap_vector
    • trap_handler
  • mscratch 的用途?
    • 每個 mode 都有各自的 stack
    • 在其他 ISA,每個 mode 都有各自的 sp

https://github.dev/cccriscv/mini-riscv-os

20 of 25

Trap Handler (Cont′d)

  • 有點像是 Dispatcher
  • 根據 mcause 判斷中斷或是異常的類型
    • Timer interrupt
    • External interrupt

https://github.com/cccriscv/mini-riscv-os/blob/master/07-ExterInterrupt/trap.c

21 of 25

Trap Handler (Cont′d)

  • external_handler 會判斷 IRQ 再指派 ISR。
    • 擴充性
  • timer_handler 加大 MTIMECMP 的 Value。

https://github.com/cccriscv/mini-riscv-os/blob/master/07-ExterInterrupt/timer.c

22 of 25

Trap Handler (Cont′d)

  • Timer interrupt 的用途?

A: 我是無情的原始碼閱讀機器。

https://github.com/cccriscv/mini-riscv-os/blob/master/07-ExterInterrupt/trap.c

23 of 25

Why we don’t make a Shell

  • Interruption Handler
  • Disk Driver
  • File system
  • System call (fork, wait, exec…)
  • Process 的實作不夠完整
  • Shell

24 of 25

Acquisition

  • 從 RISC-V 處理器到 UNIX 作業系統
  • 文章能被看見
  • “微”懂作業系統
  • 建立基礎後可以繼續挑戰 Emulator, Compiler… 的實作

25 of 25

Thanks for your listening !

Any further question?