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.4 Virtualizing exceptions and interrupts

A guest expects to receive IRQs, FIQs and SErrors and to program a GIC CPU interface and a timer. The hypervisor must own the physical versions of all of these. The architecture solves this in three layers: virtual exceptions in the PE (HCR_EL2.VI/VF/VSE), a virtual CPU interface in the GIC driven by list registers (GICv3), and direct injection of virtual LPIs and SGIs by the GIC itself (GICv4/4.1). The virtual timer and CNTVOFF_EL2 complete the picture. GICv3 itself is covered in 4.1–4.4, and routing in 3.3.

Virtual IRQ, FIQ and SError

With HCR_EL2.IMO=1 (FMO, AMO likewise), two things happen: physical IRQs are routed to EL2, and virtual IRQs are enabled for EL1/EL0. A virtual IRQ is pending when HCR_EL2.VI=1 or the GIC’s virtual CPU interface asserts its virtual IRQ signal. The two sources are ORed. The same holds for VF and FIQs.

Virtual exceptions follow these rules:

  • They are taken only at EL1 or EL0, to EL1, through the guest’s VBAR_EL1. They are masked by the guest’s PSTATE.I/F/A. They are never taken at EL2, and they are not taken when HCR_EL2.TGE=1.
  • HCR_EL2.VI and VF are levels. The hypervisor clears them when the guest has dealt with the source. Clearing VI does not “un-take” an exception already taken.
  • Virtual SError (HCR_EL2.VSE, enabled by AMO) is the hypervisor’s way to report an asynchronous abort to a guest, for example a RAS error in guest memory (16.8). With FEAT_RAS, VSESR_EL2 supplies the syndrome that the guest sees in ESR_EL1. VSE is cleared by hardware when the virtual SError is taken. An ESB in the guest can defer it into VDISR_EL2.
  • Superpriority under FEAT_NMI has virtual counterparts in HCRX_EL2.VINMI/VFNMI.

A hypervisor that drives everything through HCR_EL2.VI must emulate a whole interrupt controller, and every guest acknowledge and EOI traps. That is how virtualization worked before GIC virtualization support and on GICs without it. The GIC’s virtual CPU interface removes most of those traps.

GICv3 virtualization: ICV registers and list registers

When HCR_EL2.IMO/FMO=1, guest EL1 accesses to the CPU-interface registers ICC_IAR1_EL1, ICC_EOIR1_EL1, ICC_PMR_EL1, ICC_CTLR_EL1 and so on are redirected to the ICV_* registers, the virtual CPU interface, with no trap. The guest acknowledges, prioritises and EOIs virtual interrupts at full speed. The exceptions are the SGI-generation registers (ICC_SGI1R_EL1, ICC_ASGI1R_EL1, ICC_SGI0R_EL1). They have no virtual counterpart and trap to EL2, because sending an SGI to another vCPU needs the hypervisor to find where that vCPU runs.

The hypervisor feeds the virtual interface through list registers ICH_LR<n>_EL2. There are up to 16, and the implemented count is ICH_VTR_EL2.ListRegs+1.

ICH_LR<n>_EL2 fieldBitsMeaning
vINTID[31:0]The INTID the guest will read from ICV_IAR
pINTID[44:32]With HW=1, the physical INTID linked to this virtual one. With HW=0, bit 41 is EOI (request a maintenance interrupt on deactivation)
Priority[55:48]Virtual priority
Group[60]Virtual Group 0 (vFIQ) or Group 1 (vIRQ)
HW[61]Hardware-linked: guest deactivation also deactivates pINTID
State[63:62]00 invalid, 01 pending, 10 active, 11 pending and active

The flow for a passthrough or hardware-linked interrupt such as the virtual timer:

  1. The physical interrupt fires and is taken at EL2. The hypervisor acknowledges it but does not deactivate it. With ICC_CTLR_EL1.EOImode=1 at EL2, priority drop and deactivation are split.
  2. It writes a list register: vINTID, pINTID, HW=1, State=pending.
  3. On guest entry the virtual CPU interface signals a vIRQ. The guest reads ICV_IAR1_EL1, handles the interrupt, and writes ICV_EOIR1_EL1 (and ICV_DIR_EL1 if it uses EOImode=1).
  4. Because HW=1, the guest’s deactivation also deactivates the physical interrupt. No trap is needed, and the physical interrupt cannot fire again before the guest has handled it.

The other ICH_* registers:

  • ICH_HCR_EL2: En (enable the virtual interface), maintenance-interrupt enables (UIE, LRENPIE, NPIE, VGrp0EIE/VGrp0DIE/VGrp1EIE/VGrp1DIE), trap controls (TC, TALL0, TALL1, TDIR), and EOIcount.
  • ICH_VMCR_EL2: the guest’s view of its PMR, BPR and group enables, saved and restored on vCPU switch.
  • ICH_AP0R<n>_EL2/ICH_AP1R<n>_EL2: active-priority bits, also part of vCPU state.
  • ICH_ELRSR_EL2 (empty LRs), ICH_EISR_EL2 (LRs with an EOI maintenance request), ICH_MISR_EL2 (why a maintenance interrupt fired).

Maintenance interrupts are a PPI (INTID 25 by BSA convention) raised by the virtual interface to tell the hypervisor that its list registers need attention. Typical causes: an LR with EOI=1 was deactivated (a level-triggered emulated interrupt, so the hypervisor can resample the line); the LRs are nearly empty (UIE, underflow), so more pending interrupts can be loaded; or the guest EOIed an interrupt that was not in any LR (LRENPIE, counted in EOIcount), which means it overflowed the LRs. They are what lets the hypervisor run with more pending virtual interrupts than there are list registers.

Validation note: test the HW=1 path explicitly. Inject the physical virtual-timer PPI through an LR with HW=1, let the guest EOI it, and check with GICR_ISACTIVER0 that the physical PPI’s active state cleared without an EL2 trap. A GIC or model that fails to propagate the deactivation leaves the PPI active forever, and the guest’s timer “fires once and never again”. That is a common pre-silicon GIC integration bug, and it is easy to misdiagnose as a timer problem.

GICv4 and GICv4.1: direct injection

List registers still need the hypervisor on every interrupt delivery. For a passthrough device’s MSIs, GICv4 lets the ITS (4.4) deliver virtual LPIs directly to a vPE with no hypervisor involvement.

  • The hypervisor creates a vPE (VPEID) per vCPU. VMAPP maps it to a target Redistributor and a virtual pending table. VMAPTI/VMAPI map a device’s EventID to a vINTID on that vPE. VMOVP and VMOVI move vPEs and individual interrupts, and VINVALL/VSYNC synchronise.
  • On scheduling a vCPU, the hypervisor makes its vPE resident on the physical Redistributor by writing GICR_VPENDBASER (with Valid=1) and, in v4.0, GICR_VPROPBASER. Virtual LPIs for a resident vPE are presented through the virtual CPU interface like LR-delivered interrupts, without using an LR.
  • A vLPI for a non-resident vPE is recorded in its virtual pending table. A doorbell, a physical LPI to the hypervisor, can then wake the vCPU. In GICv4.0 the doorbell is per vLPI. In GICv4.1 it is per vPE (a default doorbell), which is cheaper to manage.

GICv4.1 also restructures the residency interface. Redistributors share a vPE configuration table described through GICR_VPROPBASER (with a v4.1 layout), and GICR_VPENDBASER selects the vPE by ID. The hypervisor must poll the Dirty bit after making a vPE resident. The other major addition is direct injection of vSGIs. The sender’s ICC_SGI1R_EL1 write still traps to EL2, but the hypervisor then writes GITS_SGIR (VPEID and vINTID) and the GIC delivers the vSGI to the target vPE directly, whether or not it is resident, with no LR management on the receive side. The price is that such vSGIs have no Active state. Support is advertised by GICD_TYPER2.nASSGIcap, and the guest requests active-less SGIs through its emulated GICD_CTLR.nASSGIreq.

Bring-up note: GICv4 support needs the ITS, the Redistributors’ VLPI support (GICR_TYPER.VLPIS, plus the v4.1 bits), memory for vPE and virtual pending tables, and coherent ITS table walks. On a multi-chip GIC, VMOVP must reach every ITS that maps the vPE. Linux’s KVM keeps GICv4 direct injection disabled unless asked (kvm-arm.vgic_v4_enable), so “MSIs work in the guest” does not show that direct injection works. Check the doorbell and residency paths separately.

The virtual timer and CNTVOFF_EL2

The guest’s natural timer is the EL1 virtual timer (CNTV_CTL_EL0, CNTV_CVAL_EL0, PPI 27). Its counter is:

CNTVCT_EL0 = CNTPCT_EL0 - CNTVOFF_EL2

The hypervisor sets CNTVOFF_EL2 per VM so that guest time starts near zero and stays continuous across save/restore or migration. It also traps the physical counter and timer (CNTHCTL_EL2.EL1PCTEN/EL1PCEN, with the E2H-dependent layout from 11.2) so that guests cannot see or program physical time. FEAT_ECV (v8.6) adds CNTPOFF_EL2, a physical-counter offset enabled by CNTHCTL_EL2.ECV (with SCR_EL3.ECVEn). It lets the guest use the EL1 physical timer too, which nested virtualization needs (4.6).

Offset rules under VHE: CNTVOFF_EL2 is not applied to CNTVCT_EL0 reads at EL2 when HCR_EL2.E2H=1, or at EL0 when {E2H,TGE}={1,1}. The host sees the virtual counter equal to the physical one, and the offset applies only to the guest. The host’s own timers are the EL2 physical and virtual timers (CNTHP_*, PPI 26; CNTHV_*, PPI 28), reached through the redirected CNTP_*/CNTV_* names (11.2).

On a vCPU switch the hypervisor saves CNTV_CTL/CNTV_CVAL through the _EL02 aliases under VHE. If the guest timer is armed, it sets a host software timer for the same deadline to wake the vCPU. While the guest runs, the virtual timer PPI fires to EL2 and is injected through an LR with HW=1, as above.

Checkpoint

Q11.4.1 A hypervisor takes the physical virtual-timer PPI at EL2, acknowledges and deactivates it straight away (EOImode=0), and injects vINTID 27 through a list register with HW=0. The guest is never scheduled again, because EL2 is flooded with the same PPI. Why, and what is the architectural fix?

  • A. The timer PPI is level-sensitive and its condition stays true until the guest reprograms CNTV_CVAL/CTL, so it reasserts as soon as it is deactivated. Keep the physical interrupt active (EOImode=1, priority drop only) and inject with HW=1, so that the guest’s deactivation deactivates it
  • B. The virtual timer cannot be injected through list registers; it must use HCR_EL2.VI
  • C. CNTVOFF_EL2 was not set, so the timer comparison is always true
  • D. The LR priority is lower than the guest’s ICV_PMR_EL1, so the interrupt stays pending and retriggers
Answer

A. A level-sensitive interrupt that is deactivated while its line is still asserted becomes pending again at once. The line drops only when the guest handles the timer. With HW=1 the physical interrupt stays active until the guest deactivates the virtual one, so it cannot retrigger at EL2 meanwhile. KVM does this, or masks the physical timer while the interrupt is in flight. D would hold the virtual interrupt pending in the LR, but it would not re-interrupt EL2. C changes when the timer fires, not whether a handled interrupt retriggers.

Read more: GICv3/4 architecture specification (IHI0069), ICH_LR<n>_EL2 and hardware-linked virtual interrupts

Q11.4.2 In a GICv4.0 system, a passthrough NIC raises an MSI for a vCPU that is currently descheduled. What happens?

  • A. The ITS drops the vLPI, because the vPE is not resident
  • B. The vLPI is delivered to whichever physical PE last ran the vCPU, as a physical LPI
  • C. The vLPI is set pending in the vPE’s virtual pending table, and if a doorbell is configured, a physical doorbell LPI tells the hypervisor to schedule the vCPU
  • D. The ITS stalls the write until the vPE becomes resident
Answer

C. Virtual pending state lives in memory, in the virtual pending table, and is not lost. The doorbell is the mechanism that wakes a non-resident vCPU. In v4.1 the doorbell is per vPE instead of per vLPI. Nothing is delivered as a physical interrupt to the guest’s old CPU (B), and the ITS does not stall the device (D).

Read more: GICv3/4 architecture specification (IHI0069), “Direct injection of virtual LPIs” and “Doorbell interrupts”

Q11.4.3 A VHE host reads CNTVCT_EL0 at EL2 while CNTVOFF_EL2 holds a guest’s large offset. What value does it see, and why?

  • A. CNTPCT_EL0 - CNTVOFF_EL2, because the virtual counter always includes the offset
  • B. The physical count, because with E2H=1 the offset is not applied to EL2 reads, so the host is not skewed by the guest’s offset
  • C. An UNKNOWN value, because CNTVOFF_EL2 must be zero while E2H=1
  • D. The access traps, because CNTHCTL_EL2.EL1PCTEN=0
Answer

B. Under VHE, the virtual offset is ignored at EL2, and at EL0 with TGE=1, so the hypervisor can leave a guest’s CNTVOFF_EL2 loaded while the host runs. A is the rule for the guest at EL1/EL0 with TGE=0. EL1PCTEN controls EL1 access to the physical counter, not EL2 reads of the virtual one (D).

Read more: Arm ARM (DDI0487), CNTVCT_EL0 and CNTVOFF_EL2; “The Generic Timer in AArch64 state”