FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

RISC-Vのトラップの処理方法の勉強

ISSでシステム命令系のパタンをパスさせようと奮闘中...

システム命令は、以下の資料で定義されている。ただし、ユーザレベル命令と同様に、命令の説明を英語の文体で書いてあるため、理解に時間がかかる。 手っ取り早く、疑似言語とかで書いてくれればいいのにな。

http://www.eecs.berkeley.edu/~krste/papers/riscv-priv-spec-1.7.pdf

RISC-Vでは、

  • マシンレベル
  • ハイパーバイザレベル
  • スーパバイザレベル
  • ユーザレベル

の4つのレベルが定義されている。このうち、ハイパーバイザレベルではまだ詳細な仕様が決められていないが、それ意外のレベルでは、ある程度システムレジスタの仕様などが決められている。

ここでは、riscv-tests の rv32si-p-csr テストパタンを通すために、システムレベル命令がどのように動作するのかを調べてみた。

github.com

  1. テスト項目: ReadOnlyのシステムレジスタに書き込みを行おうとすると、Illegal Instruction例外が発生する。

例外が発生すると、現在のマシンモードに関わらず、まずはマシンレベルに移動する。処理を行った後に元のレベルに戻るのだが、そのときに利用するのが ecall命令と eret 命令だ。

  • ECALL命令
The ECALL instruction is used to make a request to a higher privilege level. The binary interface
to the execution environment will define how parameters for the request are passed, but usually
these will be in defined locations in the integer register file. Executing an ECALL instruction causes
an Environment Call exception.
  • ERET命令
After handling a trap, the ERET instruction is used to return to the privilege level at which the
trap occurred. In addition to manipulating the privilege stack as described in Section 3.1.5, ERET
sets the pc to the value stored in the Xepc register, where X is the privilege mode (S, H, or M) in
which the ERET instruction was executed.

mstatusシステムレジスタに、現在のマシンモードの状態が書き込まれるのだが、複数回の例外を許可するために、mstatusはスタック形式になっている。

f:id:msyksphinz:20150806015049j:plain

例外が発生する度に、PRVとIEをプッシュして、前の状態を保存していく。一方で、ERETを実行する度に、PRVとIEをポップしていき、前の状態に戻っていく。