Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

11.2 VHE and nested virtualization

The original Armv8.0 EL2 was designed for a type-1 hypervisor: a small program at EL2 with its own single-range translation regime, which shares nothing with EL1. A hosted hypervisor such as KVM does not fit that design. Its scheduler, memory manager and drivers are an ordinary OS kernel. FEAT_VHE (v8.1) changes EL2 so that an OS kernel can run there unmodified. FEAT_NV and FEAT_NV2 (v8.3 and v8.4) then let a guest believe it owns EL2, so that it can run its own guests.

HCR_EL2.E2H: what changes at EL2

Setting HCR_EL2.E2H=1 changes EL2 in three ways.

  1. A two-range translation regime. EL2 stops using the single-range EL2 regime and uses the EL2&0 regime instead. That regime has TTBR0_EL2 and TTBR1_EL2, ASIDs in the TTBRs, a TCR_EL2 with the TCR_EL1 layout (T0SZ/T1SZ, A1, EPD0/1, and so on), and global/non-global entries. It has the same shape as EL1&0, so a kernel’s page-table code works unchanged (5.1).
  2. Register layouts change. Several EL2 registers take on their EL1 counterpart’s format. CPTR_EL2 gets the CPACR_EL1 layout (FPEN, ZEN, SMEN) instead of the trap-bit layout (TFP, TZ). CNTHCTL_EL2 gets a CNTKCTL_EL1-like layout, with EL0PCTEN/EL0VCTEN and the EL1 controls moved to other bit positions. Code that programs these registers must know which layout is active.
  3. Register redirection. At EL2 with E2H=1, an MRS or MSR that names an EL1 register accesses the corresponding EL2 register.
Name used at EL2Register actually accessed
SCTLR_EL1, TCR_EL1, TTBR0_EL1, TTBR1_EL1, MAIR_EL1, AMAIR_EL1SCTLR_EL2, TCR_EL2, TTBR0_EL2, TTBR1_EL2, MAIR_EL2, AMAIR_EL2
VBAR_EL1, ESR_EL1, FAR_EL1, AFSR0/1_EL1, ELR_EL1, SPSR_EL1the _EL2 registers
CPACR_EL1, CONTEXTIDR_EL1, CNTKCTL_EL1, ZCR_EL1CPTR_EL2, CONTEXTIDR_EL2, CNTHCTL_EL2, ZCR_EL2
CNTP_CTL_EL0/CVAL/TVAL, CNTV_CTL_EL0/CVAL/TVALCNTHP_*_EL2, CNTHV_*_EL2 (the EL2 physical and virtual timers)

To reach the real EL1 registers, which belong to the guest, EL2 uses new encodings with the suffixes _EL12 (SCTLR_EL12, TTBR0_EL12, VBAR_EL12, ELR_EL12, SPSR_EL12, CNTKCTL_EL12, and so on) and _EL02 (CNTP_CTL_EL02, CNTV_CVAL_EL02, …). These encodings are UNDEFINED when E2H=0, and at EL1 they are UNDEFINED or trapped (see NV below).

Not every register is redirected. Registers with no EL2 meaning, or with a separate EL2 copy that the kernel uses directly, are left alone. TPIDR_EL1, MDSCR_EL1, PAR_EL1 and SP_EL1 are examples. Linux under VHE keeps its per-CPU offset in TPIDR_EL2 for this reason. The System Register XML lists, for each register, its behaviour “when HCR_EL2.E2H == 1”.

HCR_EL2.TGE: hosting EL0

E2H makes EL2 look like EL1. TGE (trap general exceptions) makes EL0 belong to EL2. With {E2H,TGE}={1,1}:

  • EL0 runs in the EL2&0 regime, so host user processes share the host kernel’s ASID-tagged regime.
  • Exceptions from EL0, including SVC, go to EL2. EL1 is not used.
  • HCR_EL2.VM and most of the other EL1-directed stage-2 and trap controls are treated as 0 for all purposes other than direct reads, so host EL0 has no stage 2. FMO/IMO/AMO are treated as 1, so physical interrupts go to EL2.
  • Fine-grained traps (11.1) do not apply, and the virtual counter offset CNTVOFF_EL2 is treated as zero for host EL0 reads, as it is for EL2 reads with E2H=1 (11.4).

With {E2H,TGE}={1,0} the same EL2 runs a guest: EL1 and EL0 belong to the VM again, under stage 2. Switching a single bit, TGE, between the host and guest configurations is what makes the VHE world switch cheap.

Why KVM runs Linux at EL2

Without VHE (KVM’s “nVHE” mode), the host kernel runs at EL1 and a small hypervisor (KVM’s nVHE hyp code, arch/arm64/kvm/hyp/nvhe/) runs at EL2. Every guest exit then needs: an exception to EL2, saving the guest’s EL1 context, restoring the host’s EL1 context, an ERET to host EL1 so that the kernel can handle the exit, and the whole sequence reversed to re-enter the guest. The EL1 system-register file is swapped twice per exit.

With VHE the host kernel is already at EL2. A guest exit is just an exception to the kernel. Guest EL1 state is saved through the _EL12 aliases only when the vCPU is descheduled, not on every exit. Timer and interrupt handling for the host also happen without a world switch. KVM selects VHE at boot when the kernel is entered at EL2 and FEAT_VHE is present.

    // VHE: enter a guest (sketch; KVM does far more)
    msr     VTTBR_EL2, x1          // guest VMID and stage-2 base
    msr     SCTLR_EL12, x2         // guest's real SCTLR_EL1
    msr     VBAR_EL12, x3
    mrs     x0, HCR_EL2
    bic     x0, x0, #(1 << 27)     // TGE = 0: EL1/EL0 now belong to the guest
    orr     x0, x0, #1             // VM = 1: stage 2 on
    msr     HCR_EL2, x0
    isb
    msr     ELR_EL2, x4            // guest PC
    msr     SPSR_EL2, x5           // guest PSTATE, M = EL1h
    eret

Some implementations do not support E2H=0 at all, and HCR_EL2.E2H is then RES1. FEAT_E2H0 names the ability to run with E2H=0; its absence is reported by a non-zero (negative) value of ID_AA64MMFR4_EL1.E2H0. Firmware and boot code must therefore not assume that EL2 can be run in the original non-VHE mode, and they must not program CPTR_EL2 or CNTHCTL_EL2 with the non-VHE layout without checking which layout is in effect.

Security note: VHE puts the whole host kernel, with its drivers and attack surface, at the hypervisor’s privilege level. A host kernel bug is a hypervisor compromise, and guests must trust the host. Android’s protected KVM (pKVM) deliberately does the opposite: the host runs at EL1 under its own stage 2, and only a small hypervisor runs at EL2 (in nVHE form, or as “hVHE”, which uses E2H=1 for the hypervisor’s own regime but never hosts a kernel there). For EL3 reviewers, the trust question is not “is EL2 used” but “what code runs at EL2”.

Bring-up note: the Linux arm64 boot protocol asks to be entered at EL2 whenever virtualization is wanted, and the kernel itself decides between VHE and nVHE. Firmware that drops to EL1 silently disables KVM. Firmware that leaves stale HCR_EL2 bits, such as TGE or E2H with an HCR_EL2 written for another layout, produces early kernel faults that look like translation bugs.

Nested virtualization: FEAT_NV and FEAT_NV2 (in brief)

Nested virtualization runs a guest hypervisor (L1) at EL1 under the real hypervisor (L0). L1 must believe it is at EL2.

FEAT_NV (v8.3) provides the traps that make this possible. With HCR_EL2.NV=1:

  • CurrentEL read at EL1 returns EL2, so L1’s “am I at EL2” checks succeed.
  • EL1 accesses to EL2 registers (HCR_EL2, VTTBR_EL2, ESR_EL2, …) and to the _EL12/_EL02 aliases trap to L0 (EC 0x18) instead of being UNDEFINED.
  • ERET, ERETAA and ERETAB at EL1 trap to L0 (EC 0x1A), so L0 can emulate L1’s “return to its guest”.
  • EL2 TLBI and AT instructions trap.

HCR_EL2.NV1 adds the behaviour needed for an L1 that is a non-VHE hypervisor. It traps further EL1 registers that such a hypervisor uses as its “EL2” ones, and it changes some stage-1 descriptor interpretation. A VHE guest hypervisor runs with NV=1, NV1=0.

FEAT_NV alone is functional but slow, because an L1 hypervisor touches EL2 registers on every world switch, and each access is a trap to L0.

FEAT_NV2 (v8.4) removes most of those traps. With HCR_EL2.{NV,NV2}=1, EL1 accesses to many EL2 registers, and to EL1 registers that L1 uses on behalf of its own guest, become loads and stores to memory. The memory page is the one at VNCR_EL2.BADDR, and each register has an architecturally fixed offset in it. L0 reads that page when it needs L1’s view of a register, for example when emulating L1’s ERET into L2. Some other EL2 registers, such as ELR_EL2 and SPSR_EL2, are instead redirected to their EL1 counterparts, which L1 can use directly because L0 owns the real EL2 copies. Registers with immediate side effects still trap. A fault on one of these redirected accesses is reported as a data abort with ESR_ELx.ISS.VNCR=1 (5.8).

L0 also has to merge stage 2: L1’s stage 2 for L2 (IPA_L2 to IPA_L1) composed with L0’s own (IPA_L1 to PA) into a “shadow” stage 2 that the hardware walks, and it has to keep that shadow coherent with L1’s TLBIs. This is a software problem, and it is why NV support in KVM has taken years. The architecture provides the traps and VNCR, not the shadow tables.

Checkpoint

Q11.2.1 A VHE host kernel at EL2 executes MSR VBAR_EL1, x0 and then MSR VBAR_EL12, x1. Which registers are written?

  • A. VBAR_EL1, then VBAR_EL1 again, because the _EL12 alias is only a second name
  • B. VBAR_EL2 (redirected), then the real VBAR_EL1, which is the guest’s vector base
  • C. The real VBAR_EL1, then VBAR_EL2
  • D. VBAR_EL2, then an UNDEFINED exception, because _EL12 names exist only under FEAT_NV
Answer

B. With E2H=1, EL1 register names used at EL2 are redirected to the EL2 registers, so the unmodified kernel’s VBAR_EL1 write sets its own vector table. _EL12 names are the VHE way to reach the real EL1 registers, which belong to the guest. They are defined by FEAT_VHE and are UNDEFINED only when E2H=0 (D confuses them with the NV trap behaviour).

Read more: Arm ARM (DDI0487), “Virtualization Host Extensions”, VBAR_EL12

Q11.2.2 Why is a VHE guest exit cheaper than an nVHE one in KVM?

  • A. VHE disables stage 2 during exits, so no TLB maintenance is needed
  • B. VHE implementations save EL1 state to memory in hardware
  • C. With VHE the guest runs at EL2 too, so no exception is taken
  • D. The host kernel already runs at EL2, so an exit does not need a round trip to host EL1 with two swaps of the EL1 register file. Guest EL1 state can stay loaded until the vCPU is descheduled
Answer

D. In nVHE mode the host is at EL1, so handling any exit in the kernel requires swapping the guest’s EL1 context out and the host’s in, then the reverse. Under VHE the kernel handles the exit directly at EL2. B describes FEAT_NV2’s VNCR memory redirection, which is a different feature for a different problem, and is still software-managed. The guest never runs at EL2 (C).

Read more: Arm ARM (DDI0487), HCR_EL2.E2H and HCR_EL2.TGE; Arm, “AArch64 Virtualization” guide

Q11.2.3 An L1 guest hypervisor runs under L0 with HCR_EL2.{NV,NV2}=1. L1 executes MSR HCR_EL2, x0 while preparing to run its own guest. What typically happens?

  • A. The write becomes a store to L1’s VNCR page at a fixed offset, with no trap. L0 consumes the value when it emulates L1’s ERET into L2
  • B. The write traps to L0 with EC 0x18, as it would under FEAT_NV alone
  • C. The write updates the real HCR_EL2, because NV2 grants L1 direct access
  • D. The write is UNDEFINED because L1 is at EL1
Answer

A. NV2 converts accesses to registers like HCR_EL2 into memory accesses relative to VNCR_EL2.BADDR, which avoids a trap per access. B is FEAT_NV without NV2. L1 never writes the real EL2 registers (C). L0 merges L1’s values with its own when it runs L2. D would be true without FEAT_NV.

Read more: Arm ARM (DDI0487), VNCR_EL2; “Enhanced support for nested virtualization” (FEAT_NV2)