From 267b65bfb32dd26bb0488564a43cd202df0acb6d Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Fri, 13 Nov 2026 14:47:42 -0600 Subject: [PATCH 1/8] virt_kvm: reset nested state on reboot Reset empty nested state with KVM_SET_NESTED_STATE for VMX and SVM. Keep nested save/restore unsupported. Add reset-header unit tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- vm/kvm/src/lib.rs | 63 +++++++++++++++++++ vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs | 10 ++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/vm/kvm/src/lib.rs b/vm/kvm/src/lib.rs index f9d8d89279e..1dd915969f1 100644 --- a/vm/kvm/src/lib.rs +++ b/vm/kvm/src/lib.rs @@ -107,6 +107,8 @@ mod ioctl { ioctl_read!(kvm_get_xcrs, KVMIO, 0xa6, kvm_xcrs); #[cfg(target_arch = "x86_64")] ioctl_write_ptr!(kvm_set_xcrs, KVMIO, 0xa7, kvm_xcrs); + #[cfg(target_arch = "x86_64")] + ioctl_write_ptr!(kvm_set_nested_state, KVMIO, 0xbf, kvm_nested_state); ioctl_write_ptr!(kvm_get_reg, KVMIO, 0xab, kvm_one_reg); ioctl_write_ptr!(kvm_set_reg, KVMIO, 0xac, kvm_one_reg); #[cfg(target_arch = "aarch64")] @@ -313,6 +315,9 @@ pub enum Error { GetXsave(#[source] nix::Error), #[error("SetXsave")] SetXsave(#[source] nix::Error), + #[cfg(target_arch = "x86_64")] + #[error("SetNestedState")] + SetNestedState(#[source] nix::Error), #[error("GetDebugRegs")] GetDebugRegs(#[source] nix::Error), #[error("SetDebugRegs")] @@ -1271,6 +1276,27 @@ pub enum RoutingEntry { pub struct Processor<'a>(&'a Partition, u32); +#[cfg(target_arch = "x86_64")] +fn nested_state_at_reset(vmx: bool) -> kvm_nested_state { + let mut state = kvm_nested_state { + size: size_of::() as u32, + ..Default::default() + }; + if vmx { + state.format = KVM_STATE_NESTED_FORMAT_VMX as u16; + // No VMXON region or current VMCS exists after reset. + state.hdr.vmx = kvm_vmx_nested_state_hdr { + vmxon_pa: u64::MAX, + vmcs12_pa: u64::MAX, + ..Default::default() + }; + } else { + state.format = KVM_STATE_NESTED_FORMAT_SVM as u16; + state.flags = KVM_STATE_NESTED_GIF_SET as u16; + } + state +} + impl<'a> Processor<'a> { pub fn enable_synic(&self) -> Result<()> { // TODO: We are not checking KVM_CAP_ENABLE_CAP_VM first. @@ -1567,6 +1593,19 @@ impl<'a> Processor<'a> { Ok(()) } + /// Leaves nested guest mode and resets VMX or SVM state. + #[cfg(target_arch = "x86_64")] + pub fn reset_nested_state(&self, vmx: bool) -> Result<()> { + let state = nested_state_at_reset(vmx); + // SAFETY: The reset state advertises only the initialized header's + // size and has no nested guest payload for KVM to read. + unsafe { + ioctl::kvm_set_nested_state(self.get().vcpu.as_raw_fd(), &state) + .map_err(Error::SetNestedState)?; + } + Ok(()) + } + #[cfg(target_arch = "x86_64")] pub fn set_debug_regs(&self, regs: &DebugRegisters) -> Result<()> { let data = kvm_debugregs { @@ -2236,6 +2275,30 @@ pub struct DebugRegisters { mod tests { use super::*; + #[test] + fn nested_vmx_reset_has_no_vmxon_region_or_vmcs() { + let state = nested_state_at_reset(true); + assert_eq!(state.size as usize, size_of::()); + assert_eq!(u32::from(state.format), KVM_STATE_NESTED_FORMAT_VMX); + assert_eq!(state.flags, 0); + // SAFETY: The reset state uses the VMX format. + let vmx = unsafe { state.hdr.vmx }; + assert_eq!(vmx.vmxon_pa, u64::MAX); + assert_eq!(vmx.vmcs12_pa, u64::MAX); + assert_eq!(vmx.smm.flags, 0); + assert_eq!(vmx.flags, 0); + } + + #[test] + fn nested_svm_reset_leaves_guest_mode_with_gif_set() { + let state = nested_state_at_reset(false); + assert_eq!(state.size as usize, size_of::()); + assert_eq!(u32::from(state.format), KVM_STATE_NESTED_FORMAT_SVM); + assert_eq!(u32::from(state.flags), KVM_STATE_NESTED_GIF_SET); + // SAFETY: The reset state uses the SVM format. + assert_eq!(unsafe { state.hdr.svm.vmcb_pa }, 0); + } + #[test] fn sev_snp_page_type_values_match_kvm_uapi() { assert_eq!(SevSnpPageType::Normal.as_uapi(), 1); diff --git a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs index 59dd45bac5d..b11a18b73c1 100644 --- a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs +++ b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs @@ -704,7 +704,13 @@ impl AccessVpState for KvmVpStateAccess<'_, '_> { Err(KvmError::NotSupported) } - fn set_nested_state(&mut self, _value: &vp::NestedState) -> Result<(), Self::Error> { - Err(KvmError::NotSupported) + fn set_nested_state(&mut self, value: &vp::NestedState) -> Result<(), Self::Error> { + if !value.data.is_empty() { + return Err(KvmError::NotSupported); + } + // The generic reset path represents initial nested state with an empty blob. + self.kvm() + .reset_nested_state(self.vp.partition.caps.vendor.is_intel_compatible())?; + Ok(()) } } From fb10bebbdac3fe9c4ac743c7832db5e3c701c1ec Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Fri, 13 Nov 2026 14:47:46 -0600 Subject: [PATCH 2/8] vmm_tests: cover nested-enabled Linux reboots Reboot a two-VP guest three times with nesting enabled. Check boot IDs, agent connectivity, and VMX/SVM flags. Document KVM/MSHV prerequisites. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- Guide/src/dev_guide/tests/vmm.md | 29 ++++++++++++ vmm_tests/vmm_tests/tests/tests/nested.rs | 56 +++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/Guide/src/dev_guide/tests/vmm.md b/Guide/src/dev_guide/tests/vmm.md index 4ea3eb1c6a9..cbf73a62c39 100644 --- a/Guide/src/dev_guide/tests/vmm.md +++ b/Guide/src/dev_guide/tests/vmm.md @@ -335,6 +335,35 @@ Hyper-V Administrators group. To see all available options: `cargo xflowey vmm-tests-run --help`. +### Nested-enabled Linux reboot on KVM or MSHV + +The `nested::openvmm_linux_x64_reboot_nested_virt` test boots a two-VP Linux +guest with nested virtualization enabled and VMBus disabled. It reboots the +guest three times, checking agent connectivity, a changed Linux boot ID, and +VMX or SVM exposure on both VPs after each reboot. It does not launch an L2 VM. + +Run just this test from the repository root on an x86-64 Linux KVM host: + +```bash +cargo xflowey vmm-tests-run --target linux-x64 \ + --filter 'test(nested::openvmm_linux_x64_reboot_nested_virt)' \ + --dir /tmp/ov-nested-reboot +``` + +The host must expose VMX or SVM and permit nested virtualization. Petri and +OpenVMM automatically prefer MSHV when `/dev/mshv` is accessible; otherwise +they use KVM when `/dev/kvm` is accessible. Use the same command on an MSHV +host to validate that backend, which requires kernel support for partition +scrub. A successful KVM run does not validate the MSHV reset path. Inspect +the OpenVMM logs in the output directory to confirm the selected backend. + +KVM resets nested state through `KVM_SET_NESTED_STATE`, using an inactive VMX +state or an inactive SVM state with GIF set. This requires +`KVM_CAP_NESTED_STATE`; nested-state snapshot save/restore remains unsupported. + +Add `--build-only` to compile the test and prepare artifacts without running +the VM. + ### Lazy Fetch (Default) By default, VHD/ISO disk images are streamed on demand via HTTP and cached diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index d4f29717d4a..343694e88bb 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -43,6 +43,62 @@ pub(crate) async fn vm_host_mshv( Ok(()) } +/// Reboot a nested-enabled Linux guest and verify virtualization remains exposed. +#[cfg(target_os = "linux")] +#[vmm_test_macros::vmm_test_with(openvmm, requires(nested_virt), configs(linux_direct_x64))] +async fn reboot_nested_virt(config: PetriVmBuilder) -> anyhow::Result<()> { + let (mut vm, mut agent) = config + .with_nested_virt() + .with_no_vmbus() + .with_processor_topology(petri::ProcessorTopology { + vp_count: 2, + ..Default::default() + }) + .modify_backend(|b| b.with_pcie_root_topology(1, 1, 1)) + .run() + .await?; + + let mut previous_boot_id = None; + for boot in 0..=3 { + agent.ping().await?; + let shell = agent.unix_shell(); + let boot_id = shell.read_file("/proc/sys/kernel/random/boot_id").await?; + anyhow::ensure!( + previous_boot_id.as_ref() != Some(&boot_id), + "boot {boot}: guest did not reboot" + ); + previous_boot_id = Some(boot_id); + + let cpuinfo = shell.read_file("/proc/cpuinfo").await?; + let flags: Vec<_> = cpuinfo + .lines() + .filter_map(|line| line.split_once(':')) + .filter(|(name, _)| name.trim() == "flags") + .map(|(_, flags)| flags) + .collect(); + anyhow::ensure!( + flags.len() == 2, + "boot {boot}: expected flags for 2 CPUs, found {}", + flags.len() + ); + anyhow::ensure!( + flags.iter().all(|flags| flags + .split_whitespace() + .any(|flag| matches!(flag, "vmx" | "svm"))), + "boot {boot}: nested virtualization is not exposed on every CPU" + ); + + if boot != 3 { + agent.reboot().await?; + agent = vm.wait_for_reset().await?; + } + } + + agent.power_off().await?; + vm.wait_for_clean_teardown().await?; + Ok(()) +} + #[cfg(target_os = "linux")] #[vmm_test_macros::vmm_test( qemu_linux_direct_aarch64, From 6e512540dc600ea8e5b151a62fe6b2105cb1abe7 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Fri, 13 Nov 2026 17:54:29 -0600 Subject: [PATCH 3/8] vmm_tests: gate nested reboot on MSHV scrub support Probe MSHV partition scrub support and require nested_reset. Skip unsupported hosts while surfacing unexpected probe errors. Validated capability tests and KVM/MSHV guest reboots. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- Guide/src/dev_guide/tests/vmm.md | 7 +++- openvmm/openvmm_hypervisors/src/lib.rs | 12 ++++++ petri/petri_artifacts_common/src/lib.rs | 4 ++ petri/src/requirements.rs | 45 +++++++++++++++++++++++ petri/src/test.rs | 1 + vmm_core/virt_mshv/src/lib.rs | 16 ++++++++ vmm_tests/vmm_tests/tests/tests/nested.rs | 6 ++- 7 files changed, 88 insertions(+), 3 deletions(-) diff --git a/Guide/src/dev_guide/tests/vmm.md b/Guide/src/dev_guide/tests/vmm.md index cbf73a62c39..266ee46bfed 100644 --- a/Guide/src/dev_guide/tests/vmm.md +++ b/Guide/src/dev_guide/tests/vmm.md @@ -353,8 +353,11 @@ cargo xflowey vmm-tests-run --target linux-x64 \ The host must expose VMX or SVM and permit nested virtualization. Petri and OpenVMM automatically prefer MSHV when `/dev/mshv` is accessible; otherwise they use KVM when `/dev/kvm` is accessible. Use the same command on an MSHV -host to validate that backend, which requires kernel support for partition -scrub. A successful KVM run does not validate the MSHV reset path. Inspect +host to validate that backend. The test requires both `nested_virt` and +`nested_reset`. Petri probes partition scrub on a temporary MSHV partition +and skips the test when the kernel does not implement the scrub ioctl. +Other probe errors fail host discovery rather than silently skipping tests. +A successful KVM run does not validate the MSHV reset path. Inspect the OpenVMM logs in the output directory to confirm the selected backend. KVM resets nested state through `KVM_SET_NESTED_STATE`, using an inactive VMX diff --git a/openvmm/openvmm_hypervisors/src/lib.rs b/openvmm/openvmm_hypervisors/src/lib.rs index 2f0e0c8eb72..a4fa629a32a 100644 --- a/openvmm/openvmm_hypervisors/src/lib.rs +++ b/openvmm/openvmm_hypervisors/src/lib.rs @@ -45,6 +45,18 @@ pub fn mshv_snp_available() -> bool { } } +/// Returns whether MSHV supports partition scrub on this machine. +pub fn mshv_partition_scrub_available() -> anyhow::Result { + #[cfg(all(target_os = "linux", feature = "virt_mshv", guest_is_native))] + { + virt_mshv::is_partition_scrub_available() + } + #[cfg(not(all(target_os = "linux", feature = "virt_mshv", guest_is_native)))] + { + Ok(false) + } +} + #[expect(clippy::allow_attributes, reason = "lots of conditions")] #[allow(dead_code)] pub(crate) fn parse_bool_param(key: &str, val: &str) -> anyhow::Result { diff --git a/petri/petri_artifacts_common/src/lib.rs b/petri/petri_artifacts_common/src/lib.rs index de0dc918b11..e50f752c37f 100644 --- a/petri/petri_artifacts_common/src/lib.rs +++ b/petri/petri_artifacts_common/src/lib.rs @@ -15,12 +15,16 @@ pub mod capabilities { /// Support for nested virtualization pub const NESTED_VIRT: &str = "nested_virt"; + /// Support for resetting a partition with nested virtualization enabled. + pub const NESTED_RESET: &str = "nested_reset"; + /// All capability names known to petri, including those defined by /// tests. pub const KNOWN_CAPABILITIES: &[&str] = &[ VPCI, WINDOWS_PARTITION_RESET, NESTED_VIRT, + NESTED_RESET, "test_disk", "edu_initiator", "ivshmem_target", diff --git a/petri/src/requirements.rs b/petri/src/requirements.rs index 102d7a9bde6..685e17e1f8a 100644 --- a/petri/src/requirements.rs +++ b/petri/src/requirements.rs @@ -108,6 +108,8 @@ pub struct HostContext { pub execution_environment: ExecutionEnvironment, /// Hypervisor backend that OpenVMM will select on this host. pub openvmm_hypervisor: Option, + /// Whether MSHV implements partition scrub, required for nested reset. + pub mshv_partition_scrub_supported: bool, } impl HostContext { @@ -153,6 +155,14 @@ impl HostContext { _ => None, }; + let mshv_partition_scrub_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) + { + openvmm_hypervisors::mshv_partition_scrub_available() + .expect("failed to probe MSHV partition scrub support") + } else { + false + }; + let vm_host_info = cfg_select! { windows => crate::vm::hyperv::powershell::run_get_vm_host() .await @@ -186,6 +196,7 @@ impl HostContext { ExecutionEnvironment::Baremetal }, openvmm_hypervisor, + mshv_partition_scrub_supported, } } } @@ -337,6 +348,14 @@ fn available_capabilities(context: &HostContext, vmm: VmmType) -> BTreeSet<&'sta capabilities.insert(capabilities::NESTED_VIRT); } + if vmm == VmmType::OpenVmm + && (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) + || (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) + && context.mshv_partition_scrub_supported)) + { + capabilities.insert(capabilities::NESTED_RESET); + } + match std::env::var("PETRI_CAPABILITIES") { Ok(env_capabilities) => { for capability in env_capabilities.split(',').map(str::trim) { @@ -391,6 +410,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: Some(openvmm_hypervisor), + mshv_partition_scrub_supported: false, } } @@ -418,6 +438,31 @@ mod tests { assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf))); } + #[test] + fn nested_reset_requires_mshv_partition_scrub() { + let requirement = TestRequirement::RequiresCapability { + name: capabilities::NESTED_RESET, + vmm: VmmType::OpenVmm, + }; + let mut mshv = host_context(OpenVmmHypervisor::Mshv); + assert!(!requirement.is_satisfied(&mshv)); + mshv.mshv_partition_scrub_supported = true; + assert!(requirement.is_satisfied(&mshv)); + + assert!(requirement.is_satisfied(&host_context(OpenVmmHypervisor::Kvm))); + assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Whp))); + assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf))); + mshv.openvmm_hypervisor = None; + assert!(!requirement.is_satisfied(&mshv)); + assert!( + !TestRequirement::RequiresCapability { + name: capabilities::NESTED_RESET, + vmm: VmmType::Qemu, + } + .is_satisfied(&host_context(OpenVmmHypervisor::Kvm)) + ); + } + #[test] fn openvmm_hypervisor_requirement_matches_the_selected_backend() { let requirement = TestRequirement::OpenVmmHypervisor(OpenVmmHypervisor::Mshv); diff --git a/petri/src/test.rs b/petri/src/test.rs index 5d5ec88206f..d6a11f6712b 100644 --- a/petri/src/test.rs +++ b/petri/src/test.rs @@ -693,6 +693,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: hypervisor, + mshv_partition_scrub_supported: false, } } diff --git a/vmm_core/virt_mshv/src/lib.rs b/vmm_core/virt_mshv/src/lib.rs index 98c0fa3dd51..3d898e010e8 100644 --- a/vmm_core/virt_mshv/src/lib.rs +++ b/vmm_core/virt_mshv/src/lib.rs @@ -247,6 +247,22 @@ pub fn is_available() -> Result { } } +/// Probes partition scrub support using a temporary, non-isolated partition. +/// Only an unimplemented scrub ioctl is treated as unsupported. +pub fn is_partition_scrub_available() -> anyhow::Result { + use anyhow::Context as _; + + let hv = LinuxMshv::new().context("open MSHV for partition scrub probe")?; + let vmfd = hv + .mshv + .create_vm() + .context("create scrub probe partition")?; + vmfd.initialize() + .context("initialize scrub probe partition")?; + let _vp = vmfd.create_vcpu(0).context("create scrub probe VP")?; + scrub_partition(&vmfd).context("scrub probe partition") +} + /// Returns whether MSHV can create SEV-SNP partitions on this machine. /// This function currently probes SNP availability by attempting to create /// a temporary SNP partition. diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index 343694e88bb..950e1493967 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -45,7 +45,11 @@ pub(crate) async fn vm_host_mshv( /// Reboot a nested-enabled Linux guest and verify virtualization remains exposed. #[cfg(target_os = "linux")] -#[vmm_test_macros::vmm_test_with(openvmm, requires(nested_virt), configs(linux_direct_x64))] +#[vmm_test_macros::vmm_test_with( + openvmm, + requires(nested_virt, nested_reset), + configs(linux_direct_x64) +)] async fn reboot_nested_virt(config: PetriVmBuilder) -> anyhow::Result<()> { let (mut vm, mut agent) = config .with_nested_virt() From dc1c2e8102797ced2bf2b3f4967d52e54559bb77 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Sat, 14 Nov 2026 00:07:23 -0600 Subject: [PATCH 4/8] petri: gate nested reset on KVM nested-state support Require KVM_CAP_NESTED_STATE before advertising nested_reset. Cover unsupported KVM hosts and preserve MSHV scrub gating. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- Cargo.lock | 1 + Guide/src/dev_guide/tests/vmm.md | 2 ++ openvmm/openvmm_hypervisors/Cargo.toml | 3 +- openvmm/openvmm_hypervisors/src/lib.rs | 28 +++++++++++++++++++ petri/src/requirements.rs | 38 ++++++++++++++++++++++++-- petri/src/test.rs | 1 + 6 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 287106eb353..d7181670728 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6383,6 +6383,7 @@ dependencies = [ "build_rs_guest_arch", "fs-err", "hypervisor_resources", + "kvm", "test_with_tracing", "virt_mshv", "virt_whp", diff --git a/Guide/src/dev_guide/tests/vmm.md b/Guide/src/dev_guide/tests/vmm.md index 266ee46bfed..2f196c29367 100644 --- a/Guide/src/dev_guide/tests/vmm.md +++ b/Guide/src/dev_guide/tests/vmm.md @@ -356,6 +356,8 @@ they use KVM when `/dev/kvm` is accessible. Use the same command on an MSHV host to validate that backend. The test requires both `nested_virt` and `nested_reset`. Petri probes partition scrub on a temporary MSHV partition and skips the test when the kernel does not implement the scrub ioctl. +On KVM, Petri requires a positive `KVM_CHECK_EXTENSION(KVM_CAP_NESTED_STATE)` +result before advertising `nested_reset`; otherwise the test is skipped. Other probe errors fail host discovery rather than silently skipping tests. A successful KVM run does not validate the MSHV reset path. Inspect the OpenVMM logs in the output directory to confirm the selected backend. diff --git a/openvmm/openvmm_hypervisors/Cargo.toml b/openvmm/openvmm_hypervisors/Cargo.toml index 0dc63b0dbb2..f214170fbb8 100644 --- a/openvmm/openvmm_hypervisors/Cargo.toml +++ b/openvmm/openvmm_hypervisors/Cargo.toml @@ -11,7 +11,7 @@ rust-version.workspace = true virt_mshv = ["dep:virt_mshv"] # Enable building with KVM support. -virt_kvm = [] +virt_kvm = ["dep:kvm"] # Enable building with WHP support. virt_whp = ["dep:virt_whp"] @@ -26,6 +26,7 @@ hypervisor_resources.workspace = true vm_resource.workspace = true [target.'cfg(target_os = "linux")'.dependencies] +kvm = { workspace = true, optional = true } virt_mshv = { workspace = true, optional = true } [target.'cfg(windows)'.dependencies] diff --git a/openvmm/openvmm_hypervisors/src/lib.rs b/openvmm/openvmm_hypervisors/src/lib.rs index a4fa629a32a..66b92a02e23 100644 --- a/openvmm/openvmm_hypervisors/src/lib.rs +++ b/openvmm/openvmm_hypervisors/src/lib.rs @@ -45,6 +45,34 @@ pub fn mshv_snp_available() -> bool { } } +/// Returns whether KVM supports the nested-state ioctls on this machine. +pub fn kvm_nested_state_available() -> anyhow::Result { + #[cfg(all( + target_os = "linux", + feature = "virt_kvm", + guest_is_native, + guest_arch = "x86_64" + ))] + { + use anyhow::Context as _; + + let kvm = ::kvm::Kvm::new().context("open KVM for nested-state probe")?; + Ok(kvm + .check_extension(::kvm::KVM_CAP_NESTED_STATE) + .context("query KVM_CAP_NESTED_STATE")? + > 0) + } + #[cfg(not(all( + target_os = "linux", + feature = "virt_kvm", + guest_is_native, + guest_arch = "x86_64" + )))] + { + Ok(false) + } +} + /// Returns whether MSHV supports partition scrub on this machine. pub fn mshv_partition_scrub_available() -> anyhow::Result { #[cfg(all(target_os = "linux", feature = "virt_mshv", guest_is_native))] diff --git a/petri/src/requirements.rs b/petri/src/requirements.rs index 685e17e1f8a..cc3d940f48d 100644 --- a/petri/src/requirements.rs +++ b/petri/src/requirements.rs @@ -108,6 +108,8 @@ pub struct HostContext { pub execution_environment: ExecutionEnvironment, /// Hypervisor backend that OpenVMM will select on this host. pub openvmm_hypervisor: Option, + /// Whether KVM supports nested-state ioctls, required for nested reset. + pub kvm_nested_state_supported: bool, /// Whether MSHV implements partition scrub, required for nested reset. pub mshv_partition_scrub_supported: bool, } @@ -155,6 +157,13 @@ impl HostContext { _ => None, }; + let kvm_nested_state_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) { + openvmm_hypervisors::kvm_nested_state_available() + .expect("failed to probe KVM nested-state support") + } else { + false + }; + let mshv_partition_scrub_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) { openvmm_hypervisors::mshv_partition_scrub_available() @@ -196,6 +205,7 @@ impl HostContext { ExecutionEnvironment::Baremetal }, openvmm_hypervisor, + kvm_nested_state_supported, mshv_partition_scrub_supported, } } @@ -349,7 +359,8 @@ fn available_capabilities(context: &HostContext, vmm: VmmType) -> BTreeSet<&'sta } if vmm == VmmType::OpenVmm - && (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) + && ((context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) + && context.kvm_nested_state_supported) || (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) && context.mshv_partition_scrub_supported)) { @@ -410,6 +421,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: Some(openvmm_hypervisor), + kvm_nested_state_supported: false, mshv_partition_scrub_supported: false, } } @@ -449,7 +461,6 @@ mod tests { mshv.mshv_partition_scrub_supported = true; assert!(requirement.is_satisfied(&mshv)); - assert!(requirement.is_satisfied(&host_context(OpenVmmHypervisor::Kvm))); assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Whp))); assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf))); mshv.openvmm_hypervisor = None; @@ -463,6 +474,29 @@ mod tests { ); } + #[test] + fn nested_reset_requires_kvm_nested_state() { + let requirement = TestRequirement::RequiresCapability { + name: capabilities::NESTED_RESET, + vmm: VmmType::OpenVmm, + }; + let mut kvm = host_context(OpenVmmHypervisor::Kvm); + assert!( + TestRequirement::RequiresCapability { + name: capabilities::NESTED_VIRT, + vmm: VmmType::OpenVmm, + } + .is_satisfied(&kvm) + ); + assert!(!requirement.is_satisfied(&kvm)); + kvm.kvm_nested_state_supported = true; + assert!(requirement.is_satisfied(&kvm)); + kvm.openvmm_hypervisor = Some(OpenVmmHypervisor::Mshv); + assert!(!requirement.is_satisfied(&kvm)); + kvm.openvmm_hypervisor = None; + assert!(!requirement.is_satisfied(&kvm)); + } + #[test] fn openvmm_hypervisor_requirement_matches_the_selected_backend() { let requirement = TestRequirement::OpenVmmHypervisor(OpenVmmHypervisor::Mshv); diff --git a/petri/src/test.rs b/petri/src/test.rs index d6a11f6712b..0c588dd2a0b 100644 --- a/petri/src/test.rs +++ b/petri/src/test.rs @@ -693,6 +693,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: hypervisor, + kvm_nested_state_supported: false, mshv_partition_scrub_supported: false, } } From 64c84ebd1542e92abf0c764151f20d2903fbf637 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Mon, 16 Nov 2026 14:27:28 -0600 Subject: [PATCH 5/8] vmm_tests: address nested reboot review feedback Simplify nested-reset discovery and keep backend probes infallible. Clarify reset-only state handling and trim redundant tests and docs. Add separate L2 reboot coverage using the nested test infrastructure. Validated all three reboot tests on KVM and MSHV. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- Cargo.lock | 2 +- Guide/src/dev_guide/tests/vmm.md | 34 -------- openvmm/openvmm_hypervisors/Cargo.toml | 4 +- openvmm/openvmm_hypervisors/src/lib.rs | 16 ++-- petri/src/requirements.rs | 83 ++----------------- petri/src/test.rs | 3 +- vm/kvm/src/lib.rs | 26 +----- vmm_core/virt/src/x86/vp.rs | 2 + vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs | 5 +- vmm_core/virt_kvm/src/lib.rs | 24 ++++++ vmm_core/virt_mshv/src/lib.rs | 33 +++++--- vmm_tests/vmm_tests/tests/tests/nested.rs | 37 ++++++++- 12 files changed, 105 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7181670728..7926c93bbfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6383,8 +6383,8 @@ dependencies = [ "build_rs_guest_arch", "fs-err", "hypervisor_resources", - "kvm", "test_with_tracing", + "virt_kvm", "virt_mshv", "virt_whp", "vm_resource", diff --git a/Guide/src/dev_guide/tests/vmm.md b/Guide/src/dev_guide/tests/vmm.md index 2f196c29367..4ea3eb1c6a9 100644 --- a/Guide/src/dev_guide/tests/vmm.md +++ b/Guide/src/dev_guide/tests/vmm.md @@ -335,40 +335,6 @@ Hyper-V Administrators group. To see all available options: `cargo xflowey vmm-tests-run --help`. -### Nested-enabled Linux reboot on KVM or MSHV - -The `nested::openvmm_linux_x64_reboot_nested_virt` test boots a two-VP Linux -guest with nested virtualization enabled and VMBus disabled. It reboots the -guest three times, checking agent connectivity, a changed Linux boot ID, and -VMX or SVM exposure on both VPs after each reboot. It does not launch an L2 VM. - -Run just this test from the repository root on an x86-64 Linux KVM host: - -```bash -cargo xflowey vmm-tests-run --target linux-x64 \ - --filter 'test(nested::openvmm_linux_x64_reboot_nested_virt)' \ - --dir /tmp/ov-nested-reboot -``` - -The host must expose VMX or SVM and permit nested virtualization. Petri and -OpenVMM automatically prefer MSHV when `/dev/mshv` is accessible; otherwise -they use KVM when `/dev/kvm` is accessible. Use the same command on an MSHV -host to validate that backend. The test requires both `nested_virt` and -`nested_reset`. Petri probes partition scrub on a temporary MSHV partition -and skips the test when the kernel does not implement the scrub ioctl. -On KVM, Petri requires a positive `KVM_CHECK_EXTENSION(KVM_CAP_NESTED_STATE)` -result before advertising `nested_reset`; otherwise the test is skipped. -Other probe errors fail host discovery rather than silently skipping tests. -A successful KVM run does not validate the MSHV reset path. Inspect -the OpenVMM logs in the output directory to confirm the selected backend. - -KVM resets nested state through `KVM_SET_NESTED_STATE`, using an inactive VMX -state or an inactive SVM state with GIF set. This requires -`KVM_CAP_NESTED_STATE`; nested-state snapshot save/restore remains unsupported. - -Add `--build-only` to compile the test and prepare artifacts without running -the VM. - ### Lazy Fetch (Default) By default, VHD/ISO disk images are streamed on demand via HTTP and cached diff --git a/openvmm/openvmm_hypervisors/Cargo.toml b/openvmm/openvmm_hypervisors/Cargo.toml index f214170fbb8..26e47039e5b 100644 --- a/openvmm/openvmm_hypervisors/Cargo.toml +++ b/openvmm/openvmm_hypervisors/Cargo.toml @@ -11,7 +11,7 @@ rust-version.workspace = true virt_mshv = ["dep:virt_mshv"] # Enable building with KVM support. -virt_kvm = ["dep:kvm"] +virt_kvm = ["dep:virt_kvm"] # Enable building with WHP support. virt_whp = ["dep:virt_whp"] @@ -26,7 +26,7 @@ hypervisor_resources.workspace = true vm_resource.workspace = true [target.'cfg(target_os = "linux")'.dependencies] -kvm = { workspace = true, optional = true } +virt_kvm = { workspace = true, optional = true } virt_mshv = { workspace = true, optional = true } [target.'cfg(windows)'.dependencies] diff --git a/openvmm/openvmm_hypervisors/src/lib.rs b/openvmm/openvmm_hypervisors/src/lib.rs index 66b92a02e23..239c7df9254 100644 --- a/openvmm/openvmm_hypervisors/src/lib.rs +++ b/openvmm/openvmm_hypervisors/src/lib.rs @@ -46,7 +46,7 @@ pub fn mshv_snp_available() -> bool { } /// Returns whether KVM supports the nested-state ioctls on this machine. -pub fn kvm_nested_state_available() -> anyhow::Result { +pub fn kvm_nested_state_available() -> bool { #[cfg(all( target_os = "linux", feature = "virt_kvm", @@ -54,13 +54,7 @@ pub fn kvm_nested_state_available() -> anyhow::Result { guest_arch = "x86_64" ))] { - use anyhow::Context as _; - - let kvm = ::kvm::Kvm::new().context("open KVM for nested-state probe")?; - Ok(kvm - .check_extension(::kvm::KVM_CAP_NESTED_STATE) - .context("query KVM_CAP_NESTED_STATE")? - > 0) + virt_kvm::is_nested_state_available() } #[cfg(not(all( target_os = "linux", @@ -69,19 +63,19 @@ pub fn kvm_nested_state_available() -> anyhow::Result { guest_arch = "x86_64" )))] { - Ok(false) + false } } /// Returns whether MSHV supports partition scrub on this machine. -pub fn mshv_partition_scrub_available() -> anyhow::Result { +pub fn mshv_partition_scrub_available() -> bool { #[cfg(all(target_os = "linux", feature = "virt_mshv", guest_is_native))] { virt_mshv::is_partition_scrub_available() } #[cfg(not(all(target_os = "linux", feature = "virt_mshv", guest_is_native)))] { - Ok(false) + false } } diff --git a/petri/src/requirements.rs b/petri/src/requirements.rs index cc3d940f48d..9c68b683da4 100644 --- a/petri/src/requirements.rs +++ b/petri/src/requirements.rs @@ -108,10 +108,8 @@ pub struct HostContext { pub execution_environment: ExecutionEnvironment, /// Hypervisor backend that OpenVMM will select on this host. pub openvmm_hypervisor: Option, - /// Whether KVM supports nested-state ioctls, required for nested reset. - pub kvm_nested_state_supported: bool, - /// Whether MSHV implements partition scrub, required for nested reset. - pub mshv_partition_scrub_supported: bool, + /// Whether the selected OpenVMM backend supports nested partition reset. + pub nested_reset_supported: bool, } impl HostContext { @@ -157,19 +155,10 @@ impl HostContext { _ => None, }; - let kvm_nested_state_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) { - openvmm_hypervisors::kvm_nested_state_available() - .expect("failed to probe KVM nested-state support") - } else { - false - }; - - let mshv_partition_scrub_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) - { - openvmm_hypervisors::mshv_partition_scrub_available() - .expect("failed to probe MSHV partition scrub support") - } else { - false + let nested_reset_supported = match openvmm_hypervisor { + Some(OpenVmmHypervisor::Kvm) => openvmm_hypervisors::kvm_nested_state_available(), + Some(OpenVmmHypervisor::Mshv) => openvmm_hypervisors::mshv_partition_scrub_available(), + _ => false, }; let vm_host_info = cfg_select! { @@ -205,8 +194,7 @@ impl HostContext { ExecutionEnvironment::Baremetal }, openvmm_hypervisor, - kvm_nested_state_supported, - mshv_partition_scrub_supported, + nested_reset_supported, } } } @@ -358,12 +346,7 @@ fn available_capabilities(context: &HostContext, vmm: VmmType) -> BTreeSet<&'sta capabilities.insert(capabilities::NESTED_VIRT); } - if vmm == VmmType::OpenVmm - && ((context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) - && context.kvm_nested_state_supported) - || (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv) - && context.mshv_partition_scrub_supported)) - { + if vmm == VmmType::OpenVmm && context.nested_reset_supported { capabilities.insert(capabilities::NESTED_RESET); } @@ -421,8 +404,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: Some(openvmm_hypervisor), - kvm_nested_state_supported: false, - mshv_partition_scrub_supported: false, + nested_reset_supported: false, } } @@ -450,53 +432,6 @@ mod tests { assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf))); } - #[test] - fn nested_reset_requires_mshv_partition_scrub() { - let requirement = TestRequirement::RequiresCapability { - name: capabilities::NESTED_RESET, - vmm: VmmType::OpenVmm, - }; - let mut mshv = host_context(OpenVmmHypervisor::Mshv); - assert!(!requirement.is_satisfied(&mshv)); - mshv.mshv_partition_scrub_supported = true; - assert!(requirement.is_satisfied(&mshv)); - - assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Whp))); - assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf))); - mshv.openvmm_hypervisor = None; - assert!(!requirement.is_satisfied(&mshv)); - assert!( - !TestRequirement::RequiresCapability { - name: capabilities::NESTED_RESET, - vmm: VmmType::Qemu, - } - .is_satisfied(&host_context(OpenVmmHypervisor::Kvm)) - ); - } - - #[test] - fn nested_reset_requires_kvm_nested_state() { - let requirement = TestRequirement::RequiresCapability { - name: capabilities::NESTED_RESET, - vmm: VmmType::OpenVmm, - }; - let mut kvm = host_context(OpenVmmHypervisor::Kvm); - assert!( - TestRequirement::RequiresCapability { - name: capabilities::NESTED_VIRT, - vmm: VmmType::OpenVmm, - } - .is_satisfied(&kvm) - ); - assert!(!requirement.is_satisfied(&kvm)); - kvm.kvm_nested_state_supported = true; - assert!(requirement.is_satisfied(&kvm)); - kvm.openvmm_hypervisor = Some(OpenVmmHypervisor::Mshv); - assert!(!requirement.is_satisfied(&kvm)); - kvm.openvmm_hypervisor = None; - assert!(!requirement.is_satisfied(&kvm)); - } - #[test] fn openvmm_hypervisor_requirement_matches_the_selected_backend() { let requirement = TestRequirement::OpenVmmHypervisor(OpenVmmHypervisor::Mshv); diff --git a/petri/src/test.rs b/petri/src/test.rs index 0c588dd2a0b..04511cd5fce 100644 --- a/petri/src/test.rs +++ b/petri/src/test.rs @@ -693,8 +693,7 @@ mod tests { vendor: Vendor::Intel, execution_environment: ExecutionEnvironment::Baremetal, openvmm_hypervisor: hypervisor, - kvm_nested_state_supported: false, - mshv_partition_scrub_supported: false, + nested_reset_supported: false, } } diff --git a/vm/kvm/src/lib.rs b/vm/kvm/src/lib.rs index 1dd915969f1..7ae105a2587 100644 --- a/vm/kvm/src/lib.rs +++ b/vm/kvm/src/lib.rs @@ -1277,6 +1277,8 @@ pub enum RoutingEntry { pub struct Processor<'a>(&'a Partition, u32); #[cfg(target_arch = "x86_64")] +// Architectural reset discards any prior L2 state. Only the CPU vendor +// determines the inactive VMX/SVM header; no VMCS/VMCB payload survives. fn nested_state_at_reset(vmx: bool) -> kvm_nested_state { let mut state = kvm_nested_state { size: size_of::() as u32, @@ -2275,30 +2277,6 @@ pub struct DebugRegisters { mod tests { use super::*; - #[test] - fn nested_vmx_reset_has_no_vmxon_region_or_vmcs() { - let state = nested_state_at_reset(true); - assert_eq!(state.size as usize, size_of::()); - assert_eq!(u32::from(state.format), KVM_STATE_NESTED_FORMAT_VMX); - assert_eq!(state.flags, 0); - // SAFETY: The reset state uses the VMX format. - let vmx = unsafe { state.hdr.vmx }; - assert_eq!(vmx.vmxon_pa, u64::MAX); - assert_eq!(vmx.vmcs12_pa, u64::MAX); - assert_eq!(vmx.smm.flags, 0); - assert_eq!(vmx.flags, 0); - } - - #[test] - fn nested_svm_reset_leaves_guest_mode_with_gif_set() { - let state = nested_state_at_reset(false); - assert_eq!(state.size as usize, size_of::()); - assert_eq!(u32::from(state.format), KVM_STATE_NESTED_FORMAT_SVM); - assert_eq!(u32::from(state.flags), KVM_STATE_NESTED_GIF_SET); - // SAFETY: The reset state uses the SVM format. - assert_eq!(unsafe { state.hdr.svm.vmcb_pa }, 0); - } - #[test] fn sev_snp_page_type_values_match_kvm_uapi() { assert_eq!(SevSnpPageType::Normal.as_uapi(), 1); diff --git a/vmm_core/virt/src/x86/vp.rs b/vmm_core/virt/src/x86/vp.rs index 1ad1de35d86..d755c248106 100644 --- a/vmm_core/virt/src/x86/vp.rs +++ b/vmm_core/virt/src/x86/vp.rs @@ -1957,6 +1957,8 @@ impl StateElement for SynicEventFlagsPage { } /// Opaque nested-virtualization state blob for save/restore. +/// +/// An empty blob denotes architectural reset, not a serialized hardware state. #[derive(Debug, Clone, Default, PartialEq, Eq, Protobuf, Inspect)] #[mesh(package = "virt.x86")] #[inspect(skip)] diff --git a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs index b11a18b73c1..08207d5bbfb 100644 --- a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs +++ b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs @@ -706,9 +706,10 @@ impl AccessVpState for KvmVpStateAccess<'_, '_> { fn set_nested_state(&mut self, value: &vp::NestedState) -> Result<(), Self::Error> { if !value.data.is_empty() { - return Err(KvmError::NotSupported); + return Err(KvmError::NestedStateRestoreUnsupported); } - // The generic reset path represents initial nested state with an empty blob. + // Empty is NestedState::at_reset's sentinel. There is no saved payload + // to restore; synthesize the vendor's architectural reset state. self.kvm() .reset_nested_state(self.vp.partition.caps.vendor.is_intel_compatible())?; Ok(()) diff --git a/vmm_core/virt_kvm/src/lib.rs b/vmm_core/virt_kvm/src/lib.rs index a7276e4b6da..71f2b93a46c 100644 --- a/vmm_core/virt_kvm/src/lib.rs +++ b/vmm_core/virt_kvm/src/lib.rs @@ -25,6 +25,8 @@ cfg_select! { pub use arch::Kvm; pub use memory::MemoryError; +#[cfg(guest_arch = "x86_64")] +use anyhow::Context as _; use guestmem::GuestMemory; use inspect::Inspect; use memory::KvmMemoryBackingMode; @@ -44,6 +46,25 @@ pub fn is_available() -> Result { } } +/// Returns whether KVM supports the nested-state ioctls on this machine. +#[cfg(guest_arch = "x86_64")] +pub fn is_nested_state_available() -> bool { + let probe = || -> anyhow::Result { + let kvm = kvm::Kvm::new().context("open KVM for nested-state probe")?; + Ok(kvm + .check_extension(kvm::KVM_CAP_NESTED_STATE) + .context("query KVM_CAP_NESTED_STATE")? + > 0) + }; + match probe() { + Ok(supported) => supported, + Err(err) => { + tracing::warn!(error = &*err, "KVM nested-state probe failed"); + false + } + } +} + use arch::KvmVpInner; use std::sync::atomic::Ordering; use virt::VpIndex; @@ -89,6 +110,9 @@ pub enum KvmError { #[error("nested virtualization was requested but the host does not support it")] NestedVirtUnsupported, #[cfg(guest_arch = "x86_64")] + #[error("restoring saved nested state is not supported; only reset is implemented")] + NestedStateRestoreUnsupported, + #[cfg(guest_arch = "x86_64")] #[error("unsupported CPU vendor")] UnsupportedCpuVendor, #[cfg(guest_arch = "x86_64")] diff --git a/vmm_core/virt_mshv/src/lib.rs b/vmm_core/virt_mshv/src/lib.rs index 3d898e010e8..462866871c8 100644 --- a/vmm_core/virt_mshv/src/lib.rs +++ b/vmm_core/virt_mshv/src/lib.rs @@ -22,6 +22,7 @@ cfg_select! { // x86_64 and aarch64. pub mod irqfd; +use anyhow::Context as _; use guestmem::DoorbellRegistration; use guestmem::GuestMemory; use hv1_emulator::message_queues::MessageQueues; @@ -248,19 +249,25 @@ pub fn is_available() -> Result { } /// Probes partition scrub support using a temporary, non-isolated partition. -/// Only an unimplemented scrub ioctl is treated as unsupported. -pub fn is_partition_scrub_available() -> anyhow::Result { - use anyhow::Context as _; - - let hv = LinuxMshv::new().context("open MSHV for partition scrub probe")?; - let vmfd = hv - .mshv - .create_vm() - .context("create scrub probe partition")?; - vmfd.initialize() - .context("initialize scrub probe partition")?; - let _vp = vmfd.create_vcpu(0).context("create scrub probe VP")?; - scrub_partition(&vmfd).context("scrub probe partition") +pub fn is_partition_scrub_available() -> bool { + let probe = || -> anyhow::Result { + let hv = LinuxMshv::new().context("open MSHV for partition scrub probe")?; + let vmfd = hv + .mshv + .create_vm() + .context("create scrub probe partition")?; + vmfd.initialize() + .context("initialize scrub probe partition")?; + let _vp = vmfd.create_vcpu(0).context("create scrub probe VP")?; + scrub_partition(&vmfd).context("scrub probe partition") + }; + match probe() { + Ok(supported) => supported, + Err(err) => { + tracing::warn!(error = &*err, "MSHV partition scrub probe failed"); + false + } + } } /// Returns whether MSHV can create SEV-SNP partitions on this machine. diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index 950e1493967..acb2ac1bc70 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -7,6 +7,8 @@ use petri::PetriVmBuilder; #[cfg(target_os = "linux")] use petri::PetriVmmBackend; use petri::openvmm::OpenVmmPetriBackend; +#[cfg(target_os = "linux")] +use petri_artifacts_vmm_test::artifacts::host_tools::NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL; pub(crate) async fn vm_host(config: PetriVmBuilder) -> anyhow::Result<()> { let (vm, agent) = config @@ -43,7 +45,9 @@ pub(crate) async fn vm_host_mshv( Ok(()) } -/// Reboot a nested-enabled Linux guest and verify virtualization remains exposed. +/// Reboot a two-VP Linux L1 three times with nesting enabled and VMBus disabled. +/// Check changed boot IDs, connectivity, and VMX/SVM exposure on both VPs. +/// This does not launch an L2; `reboot_l2` separately exercises an L2 reboot. #[cfg(target_os = "linux")] #[vmm_test_macros::vmm_test_with( openvmm, @@ -103,6 +107,37 @@ async fn reboot_nested_virt(config: PetriVmBuilder) -> anyh Ok(()) } +/// Keep the nested-enabled L1 running while rebooting a Linux L2 three times. +#[cfg(target_os = "linux")] +#[vmm_test_macros::vmm_test(nested( + (vm_host, openvmm_linux_direct_x64), + (NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL, "tests", "nested", openvmm_linux_direct_x64), +))] +async fn reboot_l2(config: PetriVmBuilder) -> anyhow::Result<()> { + let (mut vm, mut agent) = config.run().await?; + let mut previous_boot_id = None; + for boot in 0..=3 { + agent.ping().await?; + let boot_id = agent + .unix_shell() + .read_file("/proc/sys/kernel/random/boot_id") + .await?; + anyhow::ensure!( + previous_boot_id.as_ref() != Some(&boot_id), + "boot {boot}: L2 guest did not reboot" + ); + previous_boot_id = Some(boot_id); + if boot != 3 { + agent.reboot().await?; + agent = vm.wait_for_reset().await?; + } + } + + agent.power_off().await?; + vm.wait_for_clean_teardown().await?; + Ok(()) +} + #[cfg(target_os = "linux")] #[vmm_test_macros::vmm_test( qemu_linux_direct_aarch64, From 957635820ebbec1bd3d15dc67a4f0fee284c9d95 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Mon, 16 Nov 2026 15:40:12 -0600 Subject: [PATCH 6/8] vmm_tests: verify L2 relaunch after L1 reboot Run an L2 before and after rebooting L1 to exercise used nested state. Gate the L1 wrapper on nested-reset support and keep per-run logs. Validated all three nested reboot tests on KVM and MSHV. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- petri/src/vm/mod.rs | 31 ++++++++++-- vmm_tests/vmm_test_macros/src/lib.rs | 24 ++++++++- vmm_tests/vmm_tests/tests/tests/nested.rs | 61 +++++++++++++++++++++-- 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/petri/src/vm/mod.rs b/petri/src/vm/mod.rs index 5cc0b1c34d3..c0bb3e3e20e 100644 --- a/petri/src/vm/mod.rs +++ b/petri/src/vm/mod.rs @@ -2698,8 +2698,25 @@ impl PetriVm { &self, client: &PipetteClient, env: BTreeMap, + ) -> anyhow::Result<()> { + self.run_nested_test_with_suffix(client, env, "").await + } + + /// Run the nested test with separate results for each invocation. + /// + /// Use a unique suffix when running the same nested test more than once. + pub async fn run_nested_test_with_suffix( + &self, + client: &PipetteClient, + env: BTreeMap, + suffix: &str, ) -> anyhow::Result<()> { let nested_test = self.nested_test.as_ref().context("no nested test")?; + let results_dir = if self.guest_shares.contains_key(NESTED_CONTENT_TAG) { + NESTED_RESULTS_DIR.to_owned() + } else { + format!("{NESTED_RESULTS_DIR}{suffix}") + }; let test_binary_path = if let Some(test_content_dir) = self.guest_shares.get(NESTED_CONTENT_TAG) { @@ -2730,7 +2747,7 @@ impl PetriVm { ) .env( TEST_OUTPUT_PATH, - self.agent_disk_path(&[NESTED_TEST_CONTENT_DIR.into(), NESTED_RESULTS_DIR.into()]), + self.agent_disk_path(&[NESTED_TEST_CONTENT_DIR.into(), results_dir.clone()]), ) .stdout(crate::pipette::process::Stdio::piped()) .stderr(crate::pipette::process::Stdio::piped()); @@ -2740,7 +2757,9 @@ impl PetriVm { } if self.guest_shares.contains_key(NESTED_CONTENT_TAG) { - cmd.env(PETRI_NESTED_TEST_PARENT, &self.name); + // Keep shared results under the normal results directory so the + // host collects them, but give each invocation its own test name. + cmd.env(PETRI_NESTED_TEST_PARENT, format!("{}{suffix}", self.name)); } if let Ok(v) = std::env::var(PETRI_REMOTE_ARTIFACTS) { @@ -2755,7 +2774,9 @@ impl PetriVm { .spawn( "log_nested", crate::log_task( - self.resources.log_source.log_file("stdout")?, + self.resources + .log_source + .log_file(&format!("stdout{suffix}"))?, child.stdout.take().unwrap(), "nested stdout", ), @@ -2768,7 +2789,7 @@ impl PetriVm { if !self.guest_shares.contains_key(NESTED_CONTENT_TAG) { let test_results_dir = self.agent_disk_path(&[ NESTED_TEST_CONTENT_DIR.into(), - NESTED_RESULTS_DIR.into(), + results_dir, nested_test.test_name.replace("::", "__"), ]); @@ -2779,7 +2800,7 @@ impl PetriVm { let mut output = self .resources .log_source - .create_attachment_no_prefix(&file)?; + .create_attachment_no_prefix(&format!("{suffix}{file}"))?; output.write_all(&data)?; } } diff --git a/vmm_tests/vmm_test_macros/src/lib.rs b/vmm_tests/vmm_test_macros/src/lib.rs index ac27250556a..1394d493d74 100644 --- a/vmm_tests/vmm_test_macros/src/lib.rs +++ b/vmm_tests/vmm_test_macros/src/lib.rs @@ -600,8 +600,11 @@ impl NestedConfig { impl MaybeNestedConfig { fn resolve(self, overrides: &ParsedOverrides) -> syn::Result { let mut default_overrides = ParsedOverrides::new(); - let (l1_overrides, l2_overrides) = if self.nested_config.is_some() { + let (l1_overrides, l2_overrides) = if let Some(nested_config) = &self.nested_config { default_overrides.add_capability(self.l1_config.span, "nested_virt")?; + for capability in &nested_config.l1_requires { + default_overrides.add_capability(capability.span, capability.name)?; + } (&default_overrides, overrides) } else { (overrides, &default_overrides) @@ -1134,6 +1137,7 @@ struct NestedConfig { test_module: syn::LitStr, inner_module: syn::LitStr, test_fn: Ident, + l1_requires: Vec, l2_config: Config, } @@ -1151,6 +1155,19 @@ fn parse_nested(input: ParseStream<'_>) -> syn::Result { )); } let _: Option = parens.parse()?; + let l1_requires = if parens.is_empty() { + Vec::new() + } else { + let requires = parens.parse::()?; + if requires != "requires" { + return Err(Error::new(requires.span(), "expected `requires(...)`")); + } + let capabilities; + syn::parenthesized!(capabilities in parens); + let capabilities = parse_required_capabilities(&capabilities)?; + let _: Option = parens.parse()?; + capabilities + }; if !parens.is_empty() { return Err(parens.error("unexpected tokens after the L1 config")); } @@ -1181,6 +1198,7 @@ fn parse_nested(input: ParseStream<'_>) -> syn::Result { test_module, inner_module, test_fn, + l1_requires, l2_config, }), }) @@ -1269,6 +1287,10 @@ fn parse_nested(input: ParseStream<'_>) -> syn::Result { /// ), /// ``` /// +/// The L1 tuple optionally accepts `requires(...)` after its config to require +/// host capabilities independently of the L2 test, for example +/// `(my_vm_host, l1_config, requires(nested_reset))`. +/// #[proc_macro_attribute] pub fn vmm_test( attr: proc_macro::TokenStream, diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index acb2ac1bc70..55a75052f4a 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -3,6 +3,8 @@ //! Nested vm host definitions +#[cfg(target_os = "linux")] +use anyhow::Context as _; use petri::PetriVmBuilder; #[cfg(target_os = "linux")] use petri::PetriVmmBackend; @@ -45,6 +47,48 @@ pub(crate) async fn vm_host_mshv( Ok(()) } +/// Run the L2 test before and after an L1 reboot to exercise used nested state. +#[cfg(target_os = "linux")] +async fn reboot_l1(config: PetriVmBuilder) -> anyhow::Result<()> { + let (mut vm, mut agent) = config + .with_nested_virt() + .with_no_vmbus() + .modify_backend(|b| b.with_pcie_root_topology(1, 1, 1)) + .run() + .await?; + + let mut previous_boot_id = None; + for boot in 0..=1 { + agent.ping().await?; + let boot_id = agent + .unix_shell() + .read_file("/proc/sys/kernel/random/boot_id") + .await?; + anyhow::ensure!( + previous_boot_id.as_ref() != Some(&boot_id), + "boot {boot}: L1 guest did not reboot" + ); + previous_boot_id = Some(boot_id); + tracing::info!(boot, "running L2 test"); + vm.run_nested_test_with_suffix( + &agent, + Default::default(), + if boot == 0 { "_before" } else { "_after" }, + ) + .await + .with_context(|| format!("boot {boot}: L2 test failed"))?; + + if boot == 0 { + agent.reboot().await?; + agent = vm.wait_for_reset().await?; + } + } + + agent.power_off().await?; + vm.wait_for_clean_teardown().await?; + Ok(()) +} + /// Reboot a two-VP Linux L1 three times with nesting enabled and VMBus disabled. /// Check changed boot IDs, connectivity, and VMX/SVM exposure on both VPs. /// This does not launch an L2; `reboot_l2` separately exercises an L2 reboot. @@ -107,12 +151,19 @@ async fn reboot_nested_virt(config: PetriVmBuilder) -> anyh Ok(()) } -/// Keep the nested-enabled L1 running while rebooting a Linux L2 three times. +/// Reboot a Linux L2 three times. The `reboot_l1` variant reruns this after +/// rebooting L1, proving L2 can launch after previously used nested state resets. #[cfg(target_os = "linux")] -#[vmm_test_macros::vmm_test(nested( - (vm_host, openvmm_linux_direct_x64), - (NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL, "tests", "nested", openvmm_linux_direct_x64), -))] +#[vmm_test_macros::vmm_test( + nested( + (vm_host, openvmm_linux_direct_x64), + (NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL, "tests", "nested", openvmm_linux_direct_x64), + ), + nested( + (reboot_l1, openvmm_linux_direct_x64, requires(nested_reset)), + (NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL, "tests", "nested", openvmm_linux_direct_x64), + ), +)] async fn reboot_l2(config: PetriVmBuilder) -> anyhow::Result<()> { let (mut vm, mut agent) = config.run().await?; let mut previous_boot_id = None; From c79bd97dd06c31be0da0652f521659ef39d15b61 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Tue, 17 Nov 2026 14:13:24 -0600 Subject: [PATCH 7/8] vmm_tests: broaden nested reboot test registration Remove the Linux-only guard from the nested-enabled reboot test. Note when the MSHV scrub capability probe can be removed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- openvmm/openvmm_hypervisors/src/lib.rs | 1 + vmm_tests/vmm_tests/tests/tests/nested.rs | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/openvmm/openvmm_hypervisors/src/lib.rs b/openvmm/openvmm_hypervisors/src/lib.rs index 239c7df9254..6b5d02ad808 100644 --- a/openvmm/openvmm_hypervisors/src/lib.rs +++ b/openvmm/openvmm_hypervisors/src/lib.rs @@ -68,6 +68,7 @@ pub fn kvm_nested_state_available() -> bool { } /// Returns whether MSHV supports partition scrub on this machine. +/// TODO: Remove when dom0 Kernel ships with this IOCTL pub fn mshv_partition_scrub_available() -> bool { #[cfg(all(target_os = "linux", feature = "virt_mshv", guest_is_native))] { diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index 55a75052f4a..c3d0afff27a 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -48,7 +48,6 @@ pub(crate) async fn vm_host_mshv( } /// Run the L2 test before and after an L1 reboot to exercise used nested state. -#[cfg(target_os = "linux")] async fn reboot_l1(config: PetriVmBuilder) -> anyhow::Result<()> { let (mut vm, mut agent) = config .with_nested_virt() @@ -92,7 +91,6 @@ async fn reboot_l1(config: PetriVmBuilder) -> anyhow::Resul /// Reboot a two-VP Linux L1 three times with nesting enabled and VMBus disabled. /// Check changed boot IDs, connectivity, and VMX/SVM exposure on both VPs. /// This does not launch an L2; `reboot_l2` separately exercises an L2 reboot. -#[cfg(target_os = "linux")] #[vmm_test_macros::vmm_test_with( openvmm, requires(nested_virt, nested_reset), From 6411587cb7ecfa4f1bcff22fbd84828edcce8f36 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Wed, 18 Nov 2026 13:08:20 -0600 Subject: [PATCH 8/8] vmm_tests: enable nested reboot tests on Windows Detect WHP nested reset support using WHvResetPartition availability. Make the reboot test platform independent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam --- openvmm/openvmm_hypervisors/src/lib.rs | 22 ++++++++++++++++++++++ petri/src/requirements.rs | 1 + vmm_core/virt_whp/src/lib.rs | 6 ++++++ vmm_tests/vmm_tests/tests/tests/nested.rs | 3 --- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/openvmm/openvmm_hypervisors/src/lib.rs b/openvmm/openvmm_hypervisors/src/lib.rs index 6b5d02ad808..f0dc39ceffc 100644 --- a/openvmm/openvmm_hypervisors/src/lib.rs +++ b/openvmm/openvmm_hypervisors/src/lib.rs @@ -80,6 +80,28 @@ pub fn mshv_partition_scrub_available() -> bool { } } +/// Returns whether WHP supports nested partition reset on this machine. +pub fn whp_nested_reset_available() -> bool { + #[cfg(all( + target_os = "windows", + feature = "virt_whp", + guest_is_native, + guest_arch = "x86_64" + ))] + { + virt_whp::is_nested_reset_available() + } + #[cfg(not(all( + target_os = "windows", + feature = "virt_whp", + guest_is_native, + guest_arch = "x86_64" + )))] + { + false + } +} + #[expect(clippy::allow_attributes, reason = "lots of conditions")] #[allow(dead_code)] pub(crate) fn parse_bool_param(key: &str, val: &str) -> anyhow::Result { diff --git a/petri/src/requirements.rs b/petri/src/requirements.rs index 9c68b683da4..15e14d20f43 100644 --- a/petri/src/requirements.rs +++ b/petri/src/requirements.rs @@ -158,6 +158,7 @@ impl HostContext { let nested_reset_supported = match openvmm_hypervisor { Some(OpenVmmHypervisor::Kvm) => openvmm_hypervisors::kvm_nested_state_available(), Some(OpenVmmHypervisor::Mshv) => openvmm_hypervisors::mshv_partition_scrub_available(), + Some(OpenVmmHypervisor::Whp) => openvmm_hypervisors::whp_nested_reset_available(), _ => false, }; diff --git a/vmm_core/virt_whp/src/lib.rs b/vmm_core/virt_whp/src/lib.rs index 7c925fd61bc..93a9f2cd3ab 100644 --- a/vmm_core/virt_whp/src/lib.rs +++ b/vmm_core/virt_whp/src/lib.rs @@ -971,6 +971,12 @@ pub fn is_available() -> Result { whp::capabilities::hypervisor_present().for_op("query hypervisor presence") } +/// Returns whether WHP can reset partitions with nested virtualization enabled. +#[cfg(guest_arch = "x86_64")] +pub fn is_nested_reset_available() -> bool { + whp::capabilities::reset_partition() +} + /// The prototype partition. pub struct WhpProtoPartition<'a> { vtl0: VtlPartition, diff --git a/vmm_tests/vmm_tests/tests/tests/nested.rs b/vmm_tests/vmm_tests/tests/tests/nested.rs index c3d0afff27a..42930f57fbe 100644 --- a/vmm_tests/vmm_tests/tests/tests/nested.rs +++ b/vmm_tests/vmm_tests/tests/tests/nested.rs @@ -3,13 +3,11 @@ //! Nested vm host definitions -#[cfg(target_os = "linux")] use anyhow::Context as _; use petri::PetriVmBuilder; #[cfg(target_os = "linux")] use petri::PetriVmmBackend; use petri::openvmm::OpenVmmPetriBackend; -#[cfg(target_os = "linux")] use petri_artifacts_vmm_test::artifacts::host_tools::NEXTEST_VMM_TESTS_ARCHIVE_LINUX_X64_MUSL; pub(crate) async fn vm_host(config: PetriVmBuilder) -> anyhow::Result<()> { @@ -151,7 +149,6 @@ async fn reboot_nested_virt(config: PetriVmBuilder) -> anyh /// Reboot a Linux L2 three times. The `reboot_l1` variant reruns this after /// rebooting L1, proving L2 can launch after previously used nested state resets. -#[cfg(target_os = "linux")] #[vmm_test_macros::vmm_test( nested( (vm_host, openvmm_linux_direct_x64),