Skip to content

virt_kvm: save and restore KVM nested virtualization state - #3744

Open
Robert Nowotny (bitranox) wants to merge 5 commits into
microsoft:mainfrom
bitranox:snapshot-save-restore
Open

Robert Nowotny (bitranox) wants to merge 5 commits into
microsoft:mainfrom
bitranox:snapshot-save-restore

Conversation

@bitranox

@bitranox Robert Nowotny (bitranox) commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

The KVM backend could only put the nested state back to its reset value, so a VM running its own hypervisor could not be snapshotted. This PR implements save and restore with KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE, and makes a VM reset take the vCPU out of nested operation before its registers are reset.

Rebased onto #5085, which added Processor::reset_nested_state() and the empty-blob reset in set_nested_state(). This PR used to carry its own helper for that; it now uses #5085's and keeps the empty blob as the reset value.

Save and restore. get_nested_state() sizes its buffer from KVM_CHECK_EXTENSION(KVM_CAP_NESTED_STATE), asked on the VM fd, and returns an empty blob when the host lacks the capability. set_nested_state() writes a saved blob back. Both bounds-check the size field against the buffer, so neither a short kernel result nor a malformed snapshot can cause an out-of-bounds read. NestedState is already last in the per-VP state order, after the registers, MSRs and CPUID that KVM checks the blob against.

Reset. reset_all resets the nested state last, but KVM refuses the at-reset CR4 while the vCPU is in VMX operation, so a reset that arrives while the guest is running a hypervisor fails on the first register write. A guest-initiated reboot doesn't hit this, because the guest's hypervisor leaves VMX operation before it resets. A reset the guest didn't ask for does. reset() now calls reset_nested_state() first on a partition with nested virtualization.

SVM host save area. KVM keeps the guest's MSR_VM_HSAVE_PA outside the SVM nested state blob, so NestedState now carries it next to the blob. On AMD the KVM backend reads it when saving, writes it back when restoring, and zeroes it on reset. Other backends leave it zero.

Capability. Nested virtualization now requires KVM_CAP_NESTED_STATE (Linux 4.19), so a reset can always leave nested operation and a save can always capture the state.

PIC interrupts. On a partition with nested virtualization, every PIC interrupt used to be queued with KVM_INTERRUPT, which KVM never reports in saved state. Now the interrupt goes through the injected-interrupt slot, as on other partitions, unless the VP is running a nested guest. Then only KVM can decide whether it causes an exit from that guest, so it is still queued, and saving the VP fails until KVM reports the VP ready for another interrupt. KVM has no way to report or restore a queued interrupt, so a snapshot attempted in that window fails and can be retried instead of losing the interrupt.

This only matters on the Gen1 and x86 Linux direct-boot chipsets, the only ones with a PIC. A Gen2 UEFI VM has none, so the window never opens there and no KVM change is needed for it. On an Intel host I traced a full boot of a nested Windows Gen2 guest with VBS and HVCI and counted zero KVM_INTERRUPT calls.

Testing.

  • The prepush gate passes on the rebased branch: fmt, xtask fmt, clippy --workspace --all-targets, build -p openvmm, doc --workspace and the kvm/virt_kvm tests.
  • Unit tests check the reset header for both formats: VMX gets invalid vmxon_pa/vmcs12_pa and no flags, SVM gets KVM_STATE_NESTED_GIF_SET.
  • New KVM tests (they need /dev/kvm) cover a PIC interrupt delivered outside a nested guest on a nested partition, and tracking a queued interrupt across a stop.
  • On an Intel host with KVM, before this rebase: a Windows guest with virtualization-based security, which keeps its vCPUs in VMX operation, reset cleanly on a host-initiated reset and booted again, where it used to fail in about 0.1 s and stay halted. The same guest survived 10 live snapshot restores in a row. The rebased branch, with the reset going through reset_nested_state(), has not been booted.
  • I have no AMD host. On SVM, the reset header has a unit test, and a new KVM test saves a nonzero MSR_VM_HSAVE_PA and restores it into a fresh VM. I ran that test on Intel only, where it just checks that the field stays zero.
  • A NestedState encoded before this change still decodes, with the address zero (checked locally, no test added).

@bitranox
Robert Nowotny (bitranox) requested a review from a team as a code owner June 15, 2026 10:36
Copilot AI review requested due to automatic review settings June 15, 2026 10:36
@github-actions github-actions Bot added Guide unsafe Related to unsafe code labels Jun 15, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Enable capturing/restoring KVM nested-virtualization (VMX/SVM) state as part of x86_64 vCPU state, and document updated snapshot requirements/behavior.

Changes:

  • Implement KVM nested-state get/set plumbing and expose it through vCPU state accessors.
  • Make UEFI platform resolvers register based on device presence (including snapshot restore paths).
  • Update snapshot documentation and REPL help text for file-backed memory requirements.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs Wire nested-state into VP state access and skip empty restore ioctls.
vm/kvm/src/lib.rs Add KVM nested-state ioctls, errors, and Processor get/set APIs.
openvmm/openvmm_entry/src/repl.rs Clarify snapshot CLI prerequisite wording.
openvmm/openvmm_core/src/worker/dispatch.rs Register UEFI platform resolvers whenever the UEFI device exists.
Guide/src/user_guide/openvmm/snapshots.md Document that KVM snapshots include nested-virtualization state.

Comment thread vm/kvm/src/lib.rs Resolved
Comment thread vm/kvm/src/lib.rs Resolved
Comment thread vm/kvm/src/lib.rs Resolved
@github-actions

Copy link
Copy Markdown

Copilot AI review requested due to automatic review settings June 15, 2026 20:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread vm/kvm/src/lib.rs Resolved
Comment thread vm/kvm/src/lib.rs Resolved
Comment thread vm/kvm/src/lib.rs Resolved
Comment thread vm/kvm/src/lib.rs Resolved
@github-actions

Copy link
Copy Markdown

@benhillis

Copy link
Copy Markdown
Member

Robert Nowotny (@bitranox), parts of this snapshot work may still be useful, but it needs rebasing and splitting from the UEFI work now covered by #4147. Could you refresh the remaining KVM work if you plan to continue? Thanks.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 11:31
@bitranox Robert Nowotny (bitranox) changed the title Support snapshotting UEFI and nested-virtualization VMs virt_kvm: save and restore KVM nested virtualization state Oct 12, 2026
@bitranox

Copy link
Copy Markdown
Contributor Author

Ben Hillis (@benhillis) sorry for the slow reply. I rebased this onto current main and dropped the UEFI part, since #4147 covers it. What's left is the KVM nested-state save/restore.

While I had it open, I fixed a bug in the original version: a VM reset didn't take the vCPU out of nested operation, so resetting a guest that runs its own hypervisor failed. The description has the details. I also rewrote it to match the smaller scope.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 13:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A queued PIC interrupt can still be omitted from nested-guest snapshots and permanently lost after restore.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 12, 2026 14:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Low-level KVM state handling affects reset and interrupt correctness, while the SVM path lacks hardware validation.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@github-actions

Copy link
Copy Markdown

@bitranox

Copy link
Copy Markdown
Contributor Author

On the SVM point: as the Testing section says, I have no AMD host. The x64-linux-amd-kvm vmm-tests lane did pass on this head, including openvmm_linux_x64_reboot, and since reset() now clears nested state on every vCPU, that test runs the SVM clear on an AMD host that reports nested state. But nothing there puts a vCPU into nested SVM operation first, so clearing a live SVM guest is still covered by the unit test only. If someone with an AMD setup can boot a nested guest and reset it, that would close the gap.

The KVM backend could restore only the at-reset nested state, so a VM that
runs its own hypervisor could not be snapshotted. Implement save and restore
with KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE. The buffer is sized from
KVM_CAP_NESTED_STATE, and both the length the kernel returns and the size
stored in a saved blob are bounds-checked before use. An empty blob still
means the architectural reset state.

A reset also has to take the vCPU out of nested operation before the
registers are reset. KVM rejects the at-reset CR4 while the vCPU is in VMX
operation, and reset_all writes the nested state last, so resetting a VM
whose guest was running a hypervisor failed on the first register write.
Call Processor::reset_nested_state() first.
Without the capability, nested state can be neither saved nor cleared, so a
reset of a vCPU in nested operation still fails and a snapshot silently
drops its nested state. Refuse nested_virt on such hosts.
On a partition with nested virtualization, every PIC interrupt was queued
with KVM_INTERRUPT. KVM does not report a queued interrupt through
KVM_GET_VCPU_EVENTS, so a VP stopped and saved before the guest took it
lost the interrupt, and the PIC kept it in service.

Queue the interrupt only while the VP is running a nested guest, where KVM
must decide whether it causes an exit from that guest. Otherwise inject it
through the injected-interrupt slot, as on other partitions, so that it is
saved and restored with the rest of the VP state.
While the VP runs a nested guest, a PIC interrupt is queued with
KVM_INTERRUPT. KVM reports a queued interrupt in neither the VP state nor
the nested state, and it can keep holding it after a nested exit, until the
L1 hypervisor enables interrupts. A save taken in that window lost the
interrupt, which the PIC had already acknowledged.

Track the queued interrupt in the VP runner until KVM reports the VP ready
for another interrupt, which it does only once nothing is queued, and fail
the save while it may still be queued.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 20:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

AMD nested-hypervisor snapshot restoration loses the guest-programmed MSR_VM_HSAVE_PA address because it is not serialized, requiring save/restore support and a nonzero-address round-trip test.

1 open issue

Review effort: Balanced

KVM keeps the guest's host save area address outside the SVM nested
state blob, so a nested hypervisor restored from a snapshot lost it and
took a #GP on its next VMRUN. Save it in NestedState, write it back on
restore, and zero it on reset.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 21:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Vendor-specific nested-state save, restore, and reset behavior needs hardware-backed validation because the rebased reset path is untested in a booted guest and SVM host-save-area restore lacks AMD-host execution evidence.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Guide unsafe Related to unsafe code virt-kvm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants