陳毅
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# RISC-V::中斷與異常處理 -- 異常篇 一般在修讀 Operating System 時,都會學習到 `Interrupt` 的概念,此外,電腦在運作時也會碰到大大小小的問題。你可曾否好奇在硬體上是如何排除這些問題的呢? 在本篇文章中,筆者會向大家介紹 RISC-V 處理器是如何處理中斷以及異常問題的。 > RISC-V 其實定義了多種模式,如: `Machine Mode` 、 `User Mode`、 `Supervisor Mode` 等等,由於筆者所閱讀的技術書都是以 `Machine Mode` 去做介紹。因此,本篇章同樣也會以 `Machine Mode` 為主。 ## CSR 暫存器 RISC-V 架構定義了許多暫存器,部分暫存器被定義為控制和狀態暫存器,也就是標題所指出的 CSR (Control and status registers) ,它被用於配置或是紀錄處理器的運作狀況。 筆者先將 RISC-V 架構中 (Machine Mode)與中斷、異常有關的暫存器都列出來,方便待會兒進行解說: - CSR - mtvec 當進入異常時, PC (Program counter) 會進入 mtvec 所指向的地址並繼續運行。 - mcause 紀載異常的原因 - mtval 紀載異常訊息 - mepc 進入異常前 PC 所指向的地址。若異常處理完畢, Program counter 可以讀取該位址並繼續執行。 - mstatus 進入異常時,硬體會更新 mstatus 寄存器的某些域值。 - mie 決定中斷是否被處理。 - mip 反映不同類型中斷的等待狀態。 - Memory Address Mapped - mtime 紀錄計時器的值。 - mtimecmp 儲存計時器的比較值。 - msip 產生或結束軟體中斷。 - PLIC ## 異常 我們都知道,處理器是藉由接收指令進行運作的,若處理器在執行指令流的時候遇到無法預期的情況,便稱之為`異常`。 > 其實異常與中斷非常相似,最主要的差別是異常發生於硬體、程序上的故障。 ### 異常的類型 異常主要分成兩大類型: - 同步異常 要判斷異常屬於同步或是異步最簡單的方式是: 在同樣的程序以及環境執行 n 次都能將同樣的異常狀態重現,該異常就可以被歸類到同步異常。常見狀況有: - 非法指令產生的錯誤 - 在 Debugger 設置斷點 - 擷取指令時訪問到非法的地址空間 - 訪問地址的屬性出現錯誤 - 存取指令地址時產生的非對齊錯誤 - 異步異常 異步異常又能在被細分為兩種狀態: `精確的異步異常`以及`不精確的異步異常`。 由於簡體文字書籍對於精確/非精確異步異常的介紹相當模糊。因此,筆者直接整理下表方便大家釐清: | 異常 | 描述 | | -------- | -------- | | 精確的異步異常 | 外部中斷 | | 不精確的異步異常 | 讀寫內存時出錯| > !**讀寫內存時出錯**說明: 常見在當處理器將資料寫進快取後,等到該資料在快取中被替換(存回外部記憶體)時才發現錯誤,此時處理器已經又處理了上百萬條指令。在這個狀況中,我們很難找出罪魁禍首(出錯的指令)是誰。因此,該狀況被歸類在`不精確的異步異常`。 ### 異常處理 1. Trap 當異常發生時,處理器會停止手邊的工作,再將 Program counter 的位址指向 mtvec 所指的位址並開始執行。這樣的行為就好像是主動跳入陷阱一樣,因此,在 RISC-V 的架構中將這個動作定義為 `Trap` 。 > 補充: mtvec 是一個可讀可寫的暫存器,開發者可以透過軟體修改其值。 > ![](https://i.imgur.com/o7SYf6N.png) > ![](https://i.imgur.com/ErFi3CV.png) > - 當 MODE 為 0 時,轉跳至 BASE 值表示的位址。 > - 當 MODE 為 1 時(同步異常),會將 PC 的位址指向: > BASE + 4 X CAUSE > !這邊的 CAUSE 值等同於 mcause register 所表示的值。 2. 更新 CSR Register (mcause) 在 RISC-V 架構中有定義: 當進入異常時硬體會更新 mcause register 的值,開發者可以透過軟體去讀取 mcause 以分析造成異常的具體原因。 ![](https://i.imgur.com/zmuknQr.png) > mcause 的最高位 (MSB) 用來記錄是否為 interrupt ,其餘 31 位都用來表示異常編號。 3. 更新 CSR Register (mepc) mepc Register 被用來存放跳入 Trap 前 PC 指向的指令位址,待異常處理結束後, PC 會將 mepc 存放的指令位址讀入並開始執行。 需要注意的是, mepc 在中斷和異常發生時會有不同的行為: - 異常發生時 異常發生時, mepc 會記錄 Program Counter 指向的指令位址,也就是出現異常的那條指令。 - 中斷發生時 當中斷發生時,mepc 會記錄 Program Counter 指向的位址的下一個指令位址,例如: 1. 中斷發生! (PC = 0x1c) 2. mepc = PC + 4; 3. 異常處理 4. 處理完成,跳回正常狀態 (PC = mepc) - 特例 若異常是由 ecall 或是 ebreak 造成的,則參照中斷發生時的情況處理。否則會造成 ecall 以及 ebreak 重複呼叫形成無窮迴圈。 > 關於 ecall 以及 ebreak ,請參考 RV32I 指令集。 > 補充: mepc register 也是一個可讀可寫的暫存器。因此,我們同樣可以利用軟體修改它的值。 4. 更新 CSR Register (mtval) mtval register 又稱為 mbadaddr register ,在 RISC-V 的規格書有定義: > 當進入異常時,硬體會自動更新 mtval register 的值,以紀錄造成異常發生的暫存器訪問地址或是指令編碼。 從上面的敘述就可以知道, mtval 會有兩種寫入的 Case ,分別是: - 暫存器訪問造成的異常 包括如硬件斷點、存取指令、儲存器讀寫時造成的異常。這時,硬體會將儲存器訪問的地址記錄到 mtval 當中。 - 非法指令造成的異常 之前在[透過 RISC-V 模擬器搞懂指令管線化](#)一文已經有提到: > 除了 RVC 指令集外,其他合法 RISC-V 指令集的 OPCODE 末兩碼都是 11 。 因此,在本篇就不會再對合法指令的定義多加贅述。在這個情況中,硬體會將非法的指令編碼記錄到 mtval 當中。 5. 更新 CSR Register (mstatus) mstatus 紀載了大量的資料,根據 RISC-V 架構的規定: 當進入異常時,硬體會自動更新 mstatus 的某些域值。 ![format of mstatus](https://lists.riscv.org/g/tech-j-ext/attachment/9/0) - MIE 當 MIE 域的值為 1 時,為 Enable 。反之為 Disable 。 > 簡單來說,就是決定處理器要不要受理中斷請求。 - MPIE MPIE 用來存放異常發生前 MIE 域的值。當異常結束後就可以利用 MPIE 還原 MIE 的值。 - MPP 紀錄異常發生前的工作模式,在 RISC-V 規格書中,有以下幾種模式: 1. Machine Mode 2. User Mode 3. Supervisor Mode ![](https://pic4.zhimg.com/v2-7bf0a562e65f43fc5c696b368a76cd17_b.jpg) 6. 退出異常狀態 當異常處理程序完成後,需要從異常服務退出。在 RISC-V 架構中定義了一組用於退出異常的指令 (Trap-Return Instruction) ,包括: 1. MRET 2. SRET 3. URET 分別對應了 Machine Mode, Supervisor Mode 以及 User Mode 。 使用 MRET 指令退出異常後,硬體會做兩件事情: 1. 從 mepc 指向的指令位址開始執行 2. 更新 mstatus register 同樣以 Machine Mode 為例: - 將 MIE 更新為 MPIE 的值。 - 將 MPIE 域的值更新為 1 。 > 注意! MIE 域僅是反映中斷是否接受處理,其控制權仍取決於 MIE Register 中的 MEIE 域。 ## 總結 在先前導讀過的 [rv32emu-next](https://github.com/sysprog21/rv32emu-next) 專案中,也包含了異常的處理,如果有興趣可以在自行閱讀原始碼。經過上面的介紹後,就能輕鬆的看懂這個 RISC-V 的 Emulator 是如何運作的了(嗎?) ## Reference - [迈克老狼2012](https://www.cnblogs.com/mikewolf2002/p/11314583.html) - [RISC-V架構與嵌入式開發快速入門](#) - [RISC-V异常与中断机制概述](http://www.sunnychen.top/2019/07/06/RISC-V%E5%BC%82%E5%B8%B8%E4%B8%8E%E4%B8%AD%E6%96%AD%E6%9C%BA%E5%88%B6%E6%A6%82%E8%BF%B0/) - [RISC-V Privileged 指令集简介](https://longfangsong.github.io/2020/10/20/RISC-V-Privileged%E6%8C%87%E4%BB%A4%E9%9B%86%E7%AE%80%E4%BB%8B/)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully