Skip to content

openvmm: with vGIF, a guest without SVM can clear GIF, and a snapshot loses it #1391

Closed

Description

@tangruize

On an AMD host whose kvm_amd runs with vGIF (the default where the CPU has VGIF), Linux before 7.1 lets a guest whose EFER.SVME is clear run CLGI and clear GIF, although its CPUID has no SVM and the architecture makes CLGI #UD for it. OpenVMM saves no GIF, so a snapshot taken while GIF is clear restores the guest with GIF set: the interrupts and NMIs the guest was holding off are delivered at once, and an INIT that KVM holds latched behind the clear GIF is lost. Only the guest that ran CLGI is affected. Intel hosts and AMD hosts without VGIF are not.

Affected host kernels

Host kernel CLGI by a guest whose EFER.SVME is clear GIF = 0 with EFER.SVME = 0 through KVM_SET_NESTED_STATE
7.1 and later raises #UD accepted
7.0 runs and clears GIF accepted
before 7.0, including 6.18.y and 6.12.y (checked at 6.18.60 and 6.12.115) runs and clears GIF refused

OpenVMM saves no nested state, so the last column matters only to a VMM that does.

Reproduction

Not reproduced; it needs an AMD host with VGIF. On such a host with Linux 6.18, boot a guest whose CPUID has no SVM, and at CPL 0 run CLGI, then wait for a timer interrupt. Expected: #UD at the CLGI, as on 7.1. Actual: no fault, and the interrupt waits for STGI. Take a snapshot and restore it before the guest runs STGI: the restored guest takes the interrupt at once. An INIT sent to that vCPU before the snapshot is gone after the restore, whereas without the snapshot the guest takes it once it sets GIF again.

Cause

Before 7.1, KVM runs the guest with SVME set in the VMCB's EFER (svm_set_efer), and with vGIF on it drops the CLGI and STGI intercepts whatever the guest's EFER.SVME (init_vmcb). So the CPU executes the guest's CLGI as if SVM were enabled:

// arch/x86/kvm/svm/svm.c (Linux 6.17)
	svm->vmcb->save.efer = efer | EFER_SVME;
// ...
	if (vgif) {
		svm_clr_intercept(svm, INTERCEPT_STGI);
		svm_clr_intercept(svm, INTERCEPT_CLGI);
		svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
	}

Before 7.0, svm_set_nested_state also refuses GIF = 0 while EFER.SVME = 0 ("GIF=1 and no guest mode are required if SVME=0"), so no VMM could restore that state. OpenVMM's x86 VP state has no GIF, and a restored vCPU starts with GIF set. A clear GIF also blocks INIT, and KVM then holds it latched and reports it in smi.latched_init of KVM_GET_VCPU_EVENTS. OpenVMM's KVM save does not read it, and the restored vCPU is fresh, so it has no latched INIT.

Fix

  • Linux: fixed upstream. 69f779f79e0d ("KVM: SVM: Move STGI and CLGI intercept handling") and 460c7eb2e759 ("KVM: SVM: Recalc instructions intercepts when EFER.SVME is toggled"), first in 7.1, make CLGI and STGI raise #UD while EFER.SVME is clear. 8312f1b9dd71 ("KVM: SVM: Don't set GIF when clearing EFER.SVME") and 6f4d3ebc24c6 ("KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0"), in 7.0, make the state restorable. None of them is in 6.12.y or 6.18.y. Backporting them is not a plain cherry-pick: they build on the intercept-recalc rework (for example 55780d8a1dcc, which is not in 6.18.y), and 6.12.y also lacks KVM_REQ_RECALC_INTERCEPTS (6057497336bb).
  • OpenVMM: no patch is attached. The fix proposed in openvmm: snapshot capture mixes CPU state from before and after INIT or SIPI #1150 refuses a capture while KVM reports a latched INIT, which covers an INIT held behind a clear GIF too. The patch proposed in openvmm: KVM snapshots silently lose pending SMI and SMM execution state #1158 leaves latched_init unchecked, because it assumes that only SMM holds an INIT past a stop; on these hosts a clear GIF does as well. GIF itself has no saved element, so until the host runs 7.1, a guest that clears GIF this way can lose it across a snapshot.

Related: #1150, #1158.

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions