11.1 EL2 basics and trapping
A hypervisor at EL2 controls a guest in three ways. It traps instructions and system-register accesses that the guest must not perform directly. It translates every guest physical address (an IPA) through stage 2. And it virtualizes exceptions and interrupts (11.4). This section covers the first two at the control-register level. The register-by-register map of who traps what is in 2.6; here the question is what a hypervisor has to set, and why.
HCR_EL2: the main control register
HCR_EL2 applies when EL2 is implemented and enabled in the current Security state (SCR_EL3.NS=1, or SCR_EL3.EEL2=1 for Secure EL2). The bits that matter fall into a few groups:
| Group | Bits | Effect |
|---|---|---|
| Stage 2 | VM (bit 0), PTW, DC, CD, ID, FWB | Enable the EL1&0 stage 2 and control its attributes (11.3) |
| Routing | FMO (3), IMO (4), AMO (5), TGE (27), TEA (37) | Route physical FIQ/IRQ/SError (and, with TEA, synchronous External aborts) to EL2 and enable the virtual interrupts (11.4, 3.3) |
| Virtual exceptions | VF (6), VI (7), VSE (8) | Assert a virtual FIQ, IRQ or SError to EL1 |
| Instruction traps | TWI (13), TWE (14), TSC (19), TSW (22), TPCP, TPU, TTLB, TTLBIS, TTLBOS, TICAB, TOCU | Trap WFI, WFE, SMC, set/way CMOs, PoC/PoU CMOs, TLBI and IC maintenance |
| Register traps | TID0–TID5, TID3 (18), TACR, TIDCP, TVM (26), TRVM, TLOR, TERR | Trap ID, auxiliary, IMPLEMENTATION DEFINED, MMU-control writes and reads, LORegion and RAS registers |
| Feature gates | API, APK, ATA, EnSCXT, FIEN | Inverted-sense enables: 0 traps or disables PAuth, MTE tag access, SCXTNUM, RAS fault injection |
| Execution state | RW (31) | EL1 is AArch64 (1) or AArch32 (0) |
| Host and nesting | E2H (34), TGE, NV, NV1, NV2 | VHE and nested virtualization (11.2) |
Two conventions are worth fixing in mind. Most trap bits trap when set to 1, so a zero-initialised HCR_EL2 traps nothing and leaves stage 2 off. A few feature bits (API, APK, ATA) are the reverse, so the same zeroed register does trap PAuth instructions and key-register accesses, and traps the MTE tag-control registers while disabling Allocation Tag access at EL1/EL0. A guest that suddenly takes EC 0x09 (PAuth trap) at EL2 usually points to a hypervisor that has not set HCR_EL2.API/APK.
Every trapped access arrives at EL2 with an ESR syndrome. The common ones are EC 0x01 (WFx), 0x16 (HVC), 0x17 (SMC trapped by TSC), 0x18 (MSR/MRS/system instruction, with Op0/Op1/CRn/CRm/Op2, Rt and direction in the ISS), 0x1A (ERET, under NV or FGT), and 0x20/0x24 (stage-2 instruction and data aborts). See Appendix D.
HCRX_EL2 (FEAT_HCX)
HCR_EL2 ran out of bits, so FEAT_HCX (v8.7, see DDI0487 A2) adds HCRX_EL2. Newer controls live there. Examples are EnALS/EnAS0/EnASR (the LS64 instructions, 10.3), FnXS/FGTnXS (FEAT_XS TLBI behaviour), TALLINT/VINMI/VFNMI (FEAT_NMI), TCR2En, SCTLR2En, GCSEn, D128En and MSCEn. The pattern is the one described in 2.6: EL3 must set SCR_EL3.HXEn or EL2’s own accesses to HCRX_EL2 trap to EL3, and while HXEn=0 the register behaves as if every field were 0. Many HCRX_EL2 bits are enables, so an EL2 that clears the register disables the feature for EL1 rather than allowing it. EL3 sequencing is in 17.3.
Stage 2 and the VMID
With HCR_EL2.VM=1, every EL1&0 access is translated twice: VA to IPA by the guest’s stage 1, then IPA to PA by stage 2. Stage 2 is configured by VTCR_EL2 and based at VTTBR_EL2 (11.3). VTTBR_EL2 also carries the VMID in bits [55:48] (8-bit), or bits [63:48] when VTCR_EL2.VS=1 and 16-bit VMIDs are implemented (ID_AA64MMFR1_EL1.VMIDBits).
The VMID tags every TLB entry of the EL1&0 regime, stage-1-only and combined entries alike. That lets the hypervisor switch guests by writing VTTBR_EL2, without invalidating anything. So the VMID is part of the isolation boundary. Two guests that share a VMID can hit each other’s TLB entries, and the stage-2 invalidation sequence (TLBI IPAS2E1IS followed by TLBI VMALLE1IS, 9.1) operates on the current VMID. A hypervisor that invalidates on behalf of a guest that is not running must first load that guest’s VTTBR_EL2, with an ISB.
Security note: VMID reuse needs a full invalidation (
TLBI VMALLS12E1ISfor that VMID, or a global flush on roll-over) before a new VM gets the number. Prediction resources may also be tagged by VMID, and their isolation guarantees (FEAT_CSV2 and related) are covered in 14.7.
What a hypervisor must trap
Trapping everything is slow, so the useful question is which resources a guest cannot be allowed to touch, and why.
- Physical time and timers.
CNTHCTL_EL2.EL1PCTEN/EL1PCENtrap EL1 access to the physical counter and timer, so the guest uses the virtual timer, offset byCNTVOFF_EL2(11.4). - Set/way cache maintenance (
TSW).DC ISW/CSW/CISWact on the physical cache geometry of the PE the guest happens to run on. They cannot be made correct across vCPU migration, and they can discard other guests’ dirty lines. KVM traps them and converts them into by-VA maintenance or a full flush of the guest’s memory (8.5). - SMC (
TSC). Guest PSCI calls must reach the hypervisor, not EL3, or a guest could power off a physical core. Guests normally use HVC conduit;TSCcloses the SMC path. - ID registers (
TID3and friends, plusTID2for cache ID). These let the hypervisor present one consistent CPU model across heterogeneous cores and across migration, and hide features it does not context-switch. - IMPLEMENTATION DEFINED and auxiliary registers (
TIDCP,TACR). Their effects are not architecturally scoped to a VM. - Debug and PMU (
MDCR_EL2:TDA,TDE,TPM,TPMCR,HPMN). These partition the PMU counters and keep the guest out of debug state. - FP/SIMD/SVE/SME (
CPTR_EL2). Trapping allows lazy context switching, and is required for any state the hypervisor does not save. - MMU-control writes (
TVM/TRVM), conditionally. Historically KVM trapped them to learn when a guest turned its caches on, which mattered for coherency before FEAT_S2FWB (11.3). - WFI/WFE (
TWI/TWE), as policy. Trapping them lets the hypervisor deschedule an idle or spinning vCPU. Not trapping them gives lower latency on dedicated cores.
TLB maintenance (TTLB) is normally not trapped. A guest’s TLBI is automatically scoped to its VMID by hardware, so it cannot affect other VMs. It is trapped only for nested virtualization or for errata workarounds.
Fine-grained traps: FEAT_FGT and FEAT_FGT2
The coarse traps are blunt. TID3, for example, traps the whole ID-register group, and TVM traps all of the MMU-control registers. FEAT_FGT (a v8.6 feature, see DDI0487 A2) adds per-register and per-instruction trap bits:
| Register | Traps |
|---|---|
HFGRTR_EL2 | MRS reads of individual EL1/EL0 system registers (SCTLR_EL1, TCR_EL1, TPIDR_EL0, ERX*, LOR*, and so on) |
HFGWTR_EL2 | MSR writes of the same registers, with the same bit layout where a register is writable |
HFGITR_EL2 | Individual instructions: TLBI groups, AT, DC/IC groups, ERET, SVC_EL0/SVC_EL1 and more |
HDFGRTR_EL2, HDFGWTR_EL2 | Debug, PMU, SPE, TRBE and BRBE registers |
HAFGRTR_EL2 | Activity Monitor registers (present only with FEAT_AMUv1 as well as FEAT_FGT) |
FEAT_FGT2 (introduced with the v8.9/v9.4 extensions, see DDI0487 A2) adds HFGRTR2_EL2, HFGWTR2_EL2, HFGITR2_EL2, HDFGRTR2_EL2 and HDFGWTR2_EL2 for registers added after the first set filled up.
The rules:
- FGT traps apply to accesses from EL1 and EL0 when EL2 is enabled. They do not apply to the EL2&0 host when
HCR_EL2.{E2H,TGE}={1,1}. - EL3 gating.
SCR_EL3.FGTEn(bit 27) andSCR_EL3.FGTEn2(bit 59) must be 1. Otherwise EL2’s accesses to the FGT registers trap to EL3 (EC0x18), and the fine-grained traps behave as if all bits were 0. Linux’s arm64 boot protocol requires EL3 to set both when the corresponding feature is present. - Polarity. Most bits trap when 1. Bits named
n<REG>, such asnTPIDR2_EL0,nSMPRI_EL1,nPIR_EL1,nPOR_EL1andnGCS_EL1, trap when 0. These bits cover registers added after FGT was defined. An older hypervisor that zero-fills the FGT registers therefore traps access to state it does not know how to context-switch, which is the safe default. - The FGT registers’ fields reset to architecturally UNKNOWN values, so EL2 must write every FGT register it has (including the FGT2 ones) rather than relying on reset values.
// EL2: trap guest writes to SCTLR_EL1 only (not TCR/TTBR as TVM would).
// HFGWTR_EL2_SCTLR_EL1 is a mask generated from the System Register XML.
mrs x0, HFGWTR_EL2
ldr x1, =HFGWTR_EL2_SCTLR_EL1
orr x0, x0, x1
msr HFGWTR_EL2, x0
isb
Take FGT field positions from the System Register XML (as Linux does with its generated sysreg definitions), never from memory: the registers are dense and the n bits are interleaved with ordinary ones.
Bring-up note: a kernel entered at EL2 that faults at its first
MSR HFGRTR_EL2orMSR HCRX_EL2with an exception taken to EL3 (EC0x18inESR_EL3) is the classic symptom of firmware that did not setSCR_EL3.FGTEn/FGTEn2/HXEnon a core that implements the feature. The fix is in TF-A (ENABLE_FEAT_FGT,ENABLE_FEAT_FGT2,ENABLE_FEAT_HCX), not in the kernel.
Validation note: for each trap bit you claim to support, run a directed test: guest EL1 performs the access, and EL2’s handler checks
ESR_EL2.EC, the ISS encoding of the register, andELR_EL2. Then clear the bit and confirm the access completes with no exception. A trap with the wrong priority, such as an FGT trap reported where an UNDEFINED is required for an unimplemented register, shows up only in such negative tests (18.5).
Checkpoint
Q11.1.1 A hypervisor wants to stop guests from discarding each other’s dirty data through cache maintenance, and also wants guest cache maintenance to be correct when a vCPU migrates between physical cores. Which guest instructions must it trap, and why?
- A.
DC CIVACandDC IVACwithTPCP, because by-VA maintenance to the PoC can reach other VMs’ memory - B.
TLBI VMALLE1ISwithTTLB, because broadcast TLB maintenance affects all VMIDs - C.
DC ISW/CSW/CISWwithTSW, because set/way operations act on physical cache locations, not on the guest’s addresses, and cannot follow a vCPU across cores - D. Nothing, because stage 2 confines all cache maintenance to the guest’s IPA space
Answer
C. By-VA maintenance is translated through stage 1 and stage 2, so it only reaches memory the guest maps, and an invalidate is upgraded to clean+invalidate under stage 2 where needed. Set/way operations name cache lines by set and way on the local PE. They are not translated, they can drop other owners’ dirty lines, and they say nothing about the caches of a core the vCPU ran on earlier. Guest TLBIs (B) are tagged with the current VMID and so do not cross VMs. D is wrong because set/way operations bypass translation entirely.
Read more: Arm ARM (DDI0487), HCR_EL2.TSW; “Data cache maintenance instructions” (set/way and virtualization)
Q11.1.2 An old hypervisor zero-initialises every FGT register it finds. It then runs on a newer core where HFGRTR_EL2 has an nPOR_EL1 bit, and a guest reads POR_EL1. What happens, and why was the bit defined this way?
- A. The read traps to EL2, because
n-prefixed bits trap when 0, so state the hypervisor does not context-switch is protected by default - B. The read completes, because a zero FGT register disables all fine-grained traps
- C. The read is UNDEFINED at EL1, because the hypervisor has not enabled the feature in
HCRX_EL2 - D. The read traps to EL3, because
SCR_EL3.FGTEncontrols the trap destination
Answer
A. Bits for registers added after FGT was introduced use negative polarity, so the value 0, which is what an unaware hypervisor writes, means “trap”. The guest therefore cannot use or leak POR_EL1 state that the hypervisor would fail to save on a switch. B would be true for ordinary positive-polarity bits only. SCR_EL3.FGTEn gates whether the FGT registers are effective and accessible from EL2. It does not redirect guest traps to EL3 (D).
Read more: Arm ARM (DDI0487), HFGRTR_EL2 field descriptions; SCR_EL3.FGTEn
Q11.1.3 KVM handles a stage-2 fault for VM 7 and wants to invalidate a stale IPA mapping while VM 3 is the guest loaded on this PE. Which sequence is correct?
- A.
TLBI IPAS2E1ISwith VM 7’s IPA, because the IPA uniquely identifies the entry - B.
TLBI VAE1ISwith the guest VA, because combined entries are tagged by VA - C.
TLBI ALLE1IS, the only instruction that can reach another VMID - D. Write VM 7’s
VTTBR_EL2,ISB, thenTLBI IPAS2E1IS,DSB ISH,TLBI VMALLE1IS,DSB ISH, then restore VM 3’sVTTBR_EL2
Answer
D. (The descriptor update itself is assumed to be already followed by DSB ISHST.) Stage-2 and EL1&0 TLBIs operate on the VMID currently in VTTBR_EL2, so the hypervisor must switch VMIDs, with an ISB so that the new VMID is used, before invalidating. The stage-1+2 combined entries are not invalidated by IPAS2E1IS alone, hence the VMALLE1IS. A would invalidate VM 3’s entries for that IPA. ALLE1IS (C) works, but it is a sledgehammer that flushes every VM. It is not the only option.
Read more: Arm ARM (DDI0487), TLBI IPAS2E1IS; “TLB maintenance requirements and the TLB maintenance instructions”