Lab3: Switch from EL2 to EL0
First exception
Exception class (EC)
0x15: Synchronous External abort on translation table walk or hardware update of translation table, level 1.0x3C: BRK instruction execution in AArch64 state.
Example code
1 | void exception_handler(unsigned long type, unsigned long esr, unsigned long elr, unsigned long spsr, unsigned long far){ |
1 | // Simple vector table |
Ref: AArch64 Exception and Interrupt Handling
Switch from EL2 to EL0
在 EL2 時要設定 HCR_EL2 的 RW 和 IMO 欄位:
RW, bit[31]
- 0b0: Lower levels are all AArch32.
- 0b1: The Execution state for EL1 is AArch64. The Execution state for EL0 is determined by the current value of PSTATE.nRW when executing at EL0.
我們希望 EL1 & EL0 跑在 AArch64 所以設 1。
IMO, bit[4]
- 0b0
- When executing at Exception levels below EL2, and EL2 is enabled in the current Security state:
- When the value of
HCR_EL2.TGEis 0, Physical IRQ interrupts are not taken to EL2. - When the value of
HCR_EL2.TGEis 1, Physical IRQ interrupts are taken to EL2 unless they are routed to EL3. - Virtual IRQ interrupts are disabled.
- When the value of
- When executing at Exception levels below EL2, and EL2 is enabled in the current Security state:
不希望 Interrupt 被導向 EL2,所以設 0。
- 0b0
Reference: Arm Architecture Reference Manual for A-profile architecture
SPSR_EL1 & SPSR_EL2
Ref: SPSR_EL1, Saved Program Status Register (EL1), Page 709 of AArch64-Reference-Manual.

SPSR_EL1 的目的是發生 EL1 Exception 時,存下原本的 process state,在返回時使用 eret 復原 process state。M[3:0] 分別為:
- 0b0000: EL0t.
- 0b0100: EL1t.
- 0b0101: EL1h.
t 代表使用 SP0 作為 stack pointer,h 是使用 SPx,參考 Stack Pointer selection。
SPSR_EL1 與 SPSR_EL2 同樣作用,但 SPSR_EL2 的 M[3:0] 有更多選擇
- 0b0000: EL0t
- 0b0100: EL1t
- 0b0101: EL1h
- 0b1000: EL2t
- 0b1001: EL2h
初始化時,依序以 EL2→EL1→EL0 順序切換:
- EL2→EL1
SPSR_EL2為EL1h,使用SP1作為 stack pointer,EL=1
- EL1→EL0
SPSR_EL1為EL0t,使用SP0作為 stack pointer,EL=0
CPACR_EL1
參考 kaiiiz 在 EL0 和 EL1 啟用 SIMD 和 Floating point 功能而不被攔截
CPACR_EL1.FPEN
- 0b11: This control does not cause execution of any instructions to be trapped.