📝

ARM64 ハイパーバイザー: HCR_EL2レジスタ仕様

2022/09/21に公開

1. はじめに

ハイパーバイザーを実装する上で重要になるレジスタである、HCR_EL2 (Hypervisor Configuration Register for EL2) についてまとめています。よく仕様が分からないものが多いですが、随時更新します...

EL (Exception Level)

AArch64 (ARMv8) アーキテクチャの場合、レジスタなどのアクセス権限などに関するレベルがEL0からEL3まで存在します。数字が大きいほど出来ることが多く(権限が強く)なります。

EL 用途
EL0 アプリケーション
EL1 OS(通常のLinuxカーネルなど)
EL2 ハイパーバイザー
EL3 Firmware (セキュアモニタ)

hcr_el2 レジスタ

Hypervisor Configuration Register (EL2)

Provides configuration controls for virtualization, including defining whether various operations are trapped to EL2.

https://developer.arm.com/documentation/ddi0601/2021-12/AArch64-Registers/HCR-EL2--Hypervisor-Configuration-Register

何のためのレジスタか?

基本的に、OS (上位のEL1) の割り込みなどの特定の操作をトラップためのレジスタです。通常、ハードウェア等の割り込みが発生した場合、OS に直接割り込みが通知されますが、このレジスタを利用することでハイパーバイザー(EL2)でトラップし、必要があれば何か処理を行ったとに、ゲスト OS (EL1) に通知するなどの処理を行うことが出来るようになります。

2. HCR_EL2レジスタ仕様

必須で使いそうなものから随時更新しています。

2-1. 割り込み関連

割り込み関連は基本的に一旦ハイパーバイザーが受けて、そこから適切なゲストOSに通知する(ルーティング)仕組みにする必要があります。

AMO, bit [5]

Physical SError interrupt routing.

1を設定すると、EL1のSErrorがEL2にルーティングされるようになります。

IMO, bit [4]

Physical IRQ Routing.

1を設定すると、EL1のIRQがEL2にルーティングされるようになります。

FMO, bit [3]

Physical FIQ Routing.

1を設定すると、EL1のFIQがEL2にルーティングされるようになります。

2-2. 仮想割り込み関連

ハイパーバイザーからゲストOSに仮想割り込みを通知する場合に1を設定します。

VSE, bit [8]

Virtual SError interrupt.

vSErrorを発行します。

SError (System Error)とはメモリ関連(例えばバスエラー)の例外です。
参考: Learn the architecture - AArch64 Exception model

VI, bit [7]

Virtual IRQ Interrupt.

vIRQを発行します。

VF, bit [6]

Virtual FIQ Interrupt.

vFIQを発行します。

2-3. キャッシュ関連

SWIO, bit [1]

Set/Way Invalidation Override. Causes EL1 execution of the data cache invalidate by set/way instructions to perform a data cache clean and invalidate by set/way:

これの意図がよく分からない。

When the value of this bit is 1:
AArch32: DCISW performs the same invalidation as a DCCISW instruction.
AArch64: DC ISW performs the same invalidation as a DC CISW instruction.

とあるので、キャッシュ命令がそのまま機能するように1を設定しておく。

2-4. 仮想化関連

通常、OSはMMUを利用して仮想メモリを実現します。それとは別にというか、ハイパーバイザー用にさらにネストされたStage-2 MMUを利用するために利用します。

VM, bit [0]

Virtualization enable. Enables stage 2 address translation for the EL1&0 translation regime, when EL2 is enabled in the current Security state.

1を設定すると、Stage-2 MMU を有効にします。

2-5. レジスタや命令のトラップ関連

特定のレジスタ(EL1の仮装メモリ関連のレジスタなど)やWFI命令などをEL2でトラップするように設定することが可能です。

TACR, bit [21]

Trap Auxiliary Control Registers. Traps EL1 accesses to the Auxiliary Control Registers to EL2, when EL2 is enabled in the current Security state, as follows:

1を設定すると、ACTLR_EL1レジスタへのアクセスがEL2でトラップされます。

TID3, bit [18]

Trap ID group 3. Traps EL1 reads of group 3 ID registers to EL2, when EL2 is enabled in the current Security state, as follows:

1を設定すると、Trap ID グループ3に分類されているレジスタへのアクセスがEL2でトラップされます。

TID2, bit [17]

Trap ID group 2. Traps the following register accesses to EL2, when EL2 is enabled in the current Security state, as follows:

1を設定すると、Trap ID グループ2に分類されているレジスタへのアクセスがEL2でトラップされます。

TID1, bit [16]

Trap ID group 1. Traps EL1 reads of the following registers to EL2, when EL2 is enabled in the current Security state as follows:

1を設定すると、Trap ID グループ1に分類されているレジスタへのアクセスがEL2でトラップされます。

TWE, bit [14]

Traps EL0 and EL1 execution of WFE instructions to EL2, when EL2 is enabled in the current Security state, from both Execution states, reported using EC syndrome value 0x01.

1を設定すると、WFE命令がEL2でトラップされます。

TWI, bit [13]

Traps EL0 and EL1 execution of WFI instructions to EL2, when EL2 is enabled in the current Security state, from both Execution states, reported using EC syndrome value 0x01.

1を設定すると、WFI命令がEL2でトラップされます。

2-6. その他

E2H, bit [34]

EL2 Host. Enables a configuration where a Host Operating System is running in EL2, and the Host Operating System's applications are running in EL0.

ゲストOSをハイパーバイザーと同じ権限のEL2で動かすための設定っぽいので、とりあえず0を設定して無効化する。

ARM Generic TimerのHypervisor用レジスタのCNTHCTL_EL2仕様に関連しているので、要チェック。

RW, bit [31]

Execution state control for lower Exception levels:

0設定時の説明がLower levels are all AArch32.とあるので、aarch64を利用するのでとりあえず1を設定しておく。

Discussion