Skip to content

openvmm: snapshot capture mixes CPU state from before and after INIT or SIPI #1150

Closed

Description

@tangruize

An SMP KVM guest that resets or onlines a CPU during snapshot capture can restore with pre-INIT registers and post-INIT activity, or lose a pending SIPI. The restored application processor can then wait incorrectly or execute at the wrong address, crashing the guest.

In vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs, KvmVpStateAccess::activity reads the CPU's MP state:

let mp_state = match self.kvm().get_mp_state()? {
    kvm::KVM_MP_STATE_RUNNABLE => vp::MpState::Running,
    kvm::KVM_MP_STATE_UNINITIALIZED => vp::MpState::WaitForSipi, // TODO: add a state for this
    kvm::KVM_MP_STATE_INIT_RECEIVED => vp::MpState::WaitForSipi,
    kvm::KVM_MP_STATE_HALTED => vp::MpState::Halted,
    kvm::KVM_MP_STATE_SIPI_RECEIVED => todo!("handle intermediate sipi states"),
    state => {
        panic!("unrecognized mp state {}", state);
    }
};
let events = self.kvm().get_vcpu_events()?;

KVM_GET_MP_STATE is not observationally passive: it accepts pending INIT/SIPI. The generic save_all reads registers before activity, so this call can change the registers after their old values have already been captured. Subsequent activity and APIC reads describe the post-event CPU.

Trigger

While snapshot capture stops the CPUs, have one CPU send INIT/SIPI to another. The receiver can leave KVM_RUN before the sender stops, leaving an event pending when capture reads its state. Stopping both CPUs does not itself settle that event.

Fix

fix.patch checks every stopped CPU after all CPU and device units quiesce, before capture reads CPU state. A latched INIT is detected through KVM_GET_VCPU_EVENTS without accepting it. A pending SIPI is detected from the register change when KVM_GET_MP_STATE settles it.

A detected event returns a rollback-safe capture failure, allowing the original guest to continue and retry. The check introduces no deferred-event queue and leaves ordinary captures unchanged.

Activity

  1. tangruize commented on Nov 1, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 998b9f2402050cd16d1faa897bc831ef1434be48. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/Cargo.lock b/Cargo.lock
    index 55754c811..e67618ebc 100644
    --- a/Cargo.lock
    +++ b/Cargo.lock
    @@ -10239,6 +10239,7 @@ dependencies = [
      "libc",
      "memory_range",
      "open_enum",
    + "pal_async",
      "pal_event",
      "parking_lot",
      "pci_core",
    diff --git a/openvmm/openvmm_core/src/partition.rs b/openvmm/openvmm_core/src/partition.rs
    index 7087e8a60..e769be300 100644
    --- a/openvmm/openvmm_core/src/partition.rs
    +++ b/openvmm/openvmm_core/src/partition.rs
    @@ -404,6 +404,12 @@ fn flush_async_requests(&mut self) {
             self.0.flush_async_requests()
         }
     
    +    fn has_pending_init_sipi(
    +        &mut self,
    +    ) -> Result<bool, <T::StateAccess<'_> as AccessVpState>::Error> {
    +        self.0.has_pending_init_sipi()
    +    }
    +
         fn reset(&mut self) -> Result<(), impl std::error::Error + Send + Sync + 'static> {
             self.0.reset()
         }
    diff --git a/openvmm/openvmm_core/src/worker/dispatch/microvm.rs b/openvmm/openvmm_core/src/worker/dispatch/microvm.rs
    index 80b7a411d..babbddf80 100644
    --- a/openvmm/openvmm_core/src/worker/dispatch/microvm.rs
    +++ b/openvmm/openvmm_core/src/worker/dispatch/microvm.rs
    @@ -338,6 +338,13 @@ pub(super) async fn quiesce_for_snapshot(
             quiesce.complete("capture", "quiesce", Default::default());
             self.running = false;
     
    +        // No VP may hold a pending INIT or SIPI, which the save would accept
    +        // between reading the VP's registers and the rest of its state. Every
    +        // VP and unit is stopped now, so none can arrive before the save.
    +        self.check_no_pending_init_sipi().await.map_err(|error| {
    +            openvmm_defs::rpc::SnapshotQuiesceError::RollbackSafe(RemoteError::new(error))
    +        })?;
    +
             // The time ABI's capture checks and records: the LAPIC timers, then
             // the capture anchor and records. The PIT checks itself when saved.
             #[cfg_attr(not(guest_arch = "x86_64"), expect(unused_mut))]
    @@ -401,6 +408,20 @@ async fn capture_time_abi(&mut self) -> anyhow::Result<openvmm_defs::time_abi::T
             anyhow::bail!("microVM snapshots require an x86-64 guest")
         }
     
    +    /// Checks that no VP of the stopped VM has a pending INIT or SIPI, which
    +    /// the save would accept between reading the VP's registers and the rest
    +    /// of its state.
    +    #[cfg(guest_arch = "x86_64")]
    +    async fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()> {
    +        self.inner.partition_unit.check_no_pending_init_sipi().await
    +    }
    +
    +    /// INIT and SIPI are x86's.
    +    #[cfg(not(guest_arch = "x86_64"))]
    +    async fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()> {
    +        Ok(())
    +    }
    +
         /// Resumes the VM after a rollback-safe snapshot failure and releases the
         /// snapshot boundary.
         pub(super) async fn resume_after_failed_snapshot(
    diff --git a/vmm_core/src/partition_unit/snapshot.rs b/vmm_core/src/partition_unit/snapshot.rs
    index 9a5a06587..45aa6a62b 100644
    --- a/vmm_core/src/partition_unit/snapshot.rs
    +++ b/vmm_core/src/partition_unit/snapshot.rs
    @@ -2,8 +2,9 @@
     // Licensed under the MIT License.
     
     //! Partition unit support for snapshots: validating the instantiated VP prefix,
    -//! stopping VPs at a deferred I/O boundary for capture, and the NVX time ABI's
    -//! LAPIC timer checks and restore advance.
    +//! stopping VPs at a deferred I/O boundary for capture, the capture's check for
    +//! a pending INIT or SIPI, and the NVX time ABI's LAPIC timer checks and
    +//! restore advance.
     
     use super::Error;
     use super::PartitionRequest;
    @@ -23,6 +24,8 @@ pub(super) enum SnapshotRequest {
         AdvanceLapic(FailableRpc<(u64, u64), ()>),
         #[cfg(guest_arch = "x86_64")]
         OmitSavedTsc(mesh::rpc::Rpc<(), ()>),
    +    #[cfg(guest_arch = "x86_64")]
    +    CheckInitSipi(FailableRpc<(), ()>),
     }
     
     /// Returns `data`, a VP's saved state, without its saved TSC.
    @@ -81,6 +84,20 @@ pub async fn check_one_shot_timers(&mut self) -> anyhow::Result<()> {
             Ok(())
         }
     
    +    /// Checks that no stopped vCPU has a pending INIT or SIPI, which a save
    +    /// would accept between reading the vCPU's registers and the rest of its
    +    /// state.
    +    #[cfg(guest_arch = "x86_64")]
    +    pub async fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()> {
    +        self.req_send
    +            .call_failable(
    +                |rpc| PartitionRequest::Snapshot(SnapshotRequest::CheckInitSipi(rpc)),
    +                (),
    +            )
    +            .await?;
    +        Ok(())
    +    }
    +
         /// Advances the one-shot LAPIC timer of every stopped vCPU by
         /// `downtime_ns` at `apic_hz` and sets every vCPU's LAPIC state again,
         /// for the NVX time ABI.
    @@ -145,6 +162,11 @@ pub(super) async fn handle_snapshot(&mut self, request: SnapshotRequest) {
                 SnapshotRequest::OmitSavedTsc(rpc) => rpc.handle_sync(|()| {
                     self.omit_saved_tsc = true;
                 }),
    +            #[cfg(guest_arch = "x86_64")]
    +            SnapshotRequest::CheckInitSipi(rpc) => {
    +                rpc.handle_failable(async |()| self.vp_set.check_no_pending_init_sipi().await)
    +                    .await
    +            }
             }
         }
     }
    diff --git a/vmm_core/src/partition_unit/vp_set.rs b/vmm_core/src/partition_unit/vp_set.rs
    index b8e6d1027..6102c4824 100644
    --- a/vmm_core/src/partition_unit/vp_set.rs
    +++ b/vmm_core/src/partition_unit/vp_set.rs
    @@ -4,6 +4,8 @@
     //! Virtual processor state management.
     
     mod boundary;
    +#[cfg(guest_arch = "x86_64")]
    +mod init_sipi;
     mod prefix;
     #[cfg(guest_arch = "x86_64")]
     mod time_abi;
    @@ -91,6 +93,10 @@ fn set_initial_regs(
         #[cfg(guest_arch = "x86_64")]
         fn advance_lapic_timer(&mut self, advance: time_abi::LapicAdvance) -> anyhow::Result<()>;
     
    +    /// Checks that the stopped vCPU has no pending INIT or SIPI.
    +    #[cfg(guest_arch = "x86_64")]
    +    fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()>;
    +
         #[cfg(feature = "gdb")]
         fn debug(&mut self) -> &mut dyn DebugVp;
     
    @@ -198,6 +204,11 @@ fn advance_lapic_timer(&mut self, advance: time_abi::LapicAdvance) -> anyhow::Re
             advance.apply(self.vp)
         }
     
    +    #[cfg(guest_arch = "x86_64")]
    +    fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()> {
    +        init_sipi::check_no_pending_init_sipi(self.vp)
    +    }
    +
         fn inspect_vp(
             &mut self,
             vtl_guest_memory: &[Option<GuestMemory>; NUM_VTLS],
    @@ -1061,6 +1072,87 @@ fn restores_halt_latch_only_on_a_stopped_clear_destination() {
             vps.started = true;
             assert!(vps.restore_halt(true).is_err());
         }
    +
    +    /// A VP set of `count` VPs, with the runners whose receivers get the VPs' events.
    +    #[cfg(guest_arch = "x86_64")]
    +    fn vp_set(count: u32) -> (VpSet, Vec<VpRunner>) {
    +        let (halt, _halt_recv) = Halt::new();
    +        let mut vps = VpSet::new([None, None, None], Arc::new(halt), count as usize);
    +        let runners = (0..count)
    +            .map(|index| {
    +                vps.add(TargetVpInfo {
    +                    base: vm_topology::processor::VpInfo {
    +                        vp_index: VpIndex::new(index),
    +                        vnode: 0,
    +                    },
    +                    apic_id: index,
    +                })
    +            })
    +            .collect();
    +        (vps, runners)
    +    }
    +
    +    #[cfg(guest_arch = "x86_64")]
    +    #[test]
    +    fn init_sipi_check_keeps_started_and_fails_on_running_vps() {
    +        let (mut vps, _runners) = vp_set(0);
    +        vps.check_no_pending_init_sipi()
    +            .now_or_never()
    +            .unwrap()
    +            .unwrap();
    +        assert!(!vps.started);
    +
    +        vps.start();
    +        assert!(
    +            vps.check_no_pending_init_sipi()
    +                .now_or_never()
    +                .unwrap()
    +                .is_err()
    +        );
    +        assert!(vps.started);
    +    }
    +
    +    /// The INIT/SIPI check asks every VP, and fails when one VP refuses or its
    +    /// runner is gone, naming that VP.
    +    #[cfg(guest_arch = "x86_64")]
    +    #[test]
    +    fn init_sipi_check_asks_every_vp() {
    +        for refusing in [None, Some(0), Some(2)] {
    +            let (mut vps, mut runners) = vp_set(3);
    +            let mut check = Box::pin(vps.check_no_pending_init_sipi());
    +            assert!(check.as_mut().now_or_never().is_none());
    +            for (index, runner) in runners.iter_mut().enumerate() {
    +                let Ok(VpEvent::State(StateEvent::CheckInitSipi(rpc))) = runner.recv.try_recv()
    +                else {
    +                    panic!("expected an INIT/SIPI check request");
    +                };
    +                rpc.complete(if refusing == Some(index) {
    +                    Err(mesh::error::RemoteError::new(anyhow::anyhow!("pending")))
    +                } else {
    +                    Ok(())
    +                });
    +            }
    +            let result = check.as_mut().now_or_never().unwrap();
    +            drop(check);
    +            match refusing {
    +                None => result.unwrap(),
    +                Some(index) => assert_eq!(
    +                    result.unwrap_err().to_string(),
    +                    format!("vp{index} pending INIT or SIPI")
    +                ),
    +            }
    +            assert!(!vps.started);
    +        }
    +
    +        let (mut vps, runners) = vp_set(2);
    +        drop(runners);
    +        assert!(
    +            vps.check_no_pending_init_sipi()
    +                .now_or_never()
    +                .unwrap()
    +                .is_err()
    +        );
    +    }
     }
     
     /// Error returned when registers could not be set on a VP.
    @@ -1200,6 +1292,8 @@ enum StateEvent {
         CheckTimers(mesh::rpc::FailableRpc<(), ()>),
         #[cfg(guest_arch = "x86_64")]
         AdvanceLapic(mesh::rpc::FailableRpc<time_abi::LapicAdvance, ()>),
    +    #[cfg(guest_arch = "x86_64")]
    +    CheckInitSipi(mesh::rpc::FailableRpc<(), ()>),
         #[cfg(feature = "dump")]
         GetDumpVpState(Rpc<Vtl, anyhow::Result<hyperv_dump::VpState>>),
         #[cfg(feature = "gdb")]
    @@ -1454,6 +1548,10 @@ fn state_event(&mut self, vp: &mut dyn ControlVp, event: StateEvent) {
                 StateEvent::AdvanceLapic(rpc) => {
                     rpc.handle_failable_sync(|advance| vp.advance_lapic_timer(advance))
                 }
    +            #[cfg(guest_arch = "x86_64")]
    +            StateEvent::CheckInitSipi(rpc) => {
    +                rpc.handle_failable_sync(|()| vp.check_no_pending_init_sipi())
    +            }
                 #[cfg(feature = "dump")]
                 StateEvent::GetDumpVpState(rpc) => rpc.handle_sync(|vtl| vp.get_dump_vp_state(vtl)),
                 #[cfg(feature = "gdb")]
    diff --git a/vmm_core/src/partition_unit/vp_set/init_sipi.rs b/vmm_core/src/partition_unit/vp_set/init_sipi.rs
    new file mode 100644
    index 000000000..e6f0c2415
    --- /dev/null
    +++ b/vmm_core/src/partition_unit/vp_set/init_sipi.rs
    @@ -0,0 +1,53 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! The snapshot capture's check that no stopped VP has a pending INIT or SIPI.
    +//!
    +//! A save reads a VP's registers before its activity, and on KVM the
    +//! activity's read (`KVM_GET_MP_STATE`) accepts a pending INIT or SIPI: the
    +//! saved registers would be from before the event and the rest of the saved
    +//! state from after it. A capture therefore refuses a VP with such an event.
    +
    +use super::StateEvent;
    +use super::VpEvent;
    +use super::VpSet;
    +use anyhow::Context as _;
    +use futures::future::TryJoinAll;
    +use mesh::rpc::RpcSend;
    +use virt::Processor;
    +
    +/// Checks that the stopped VP `vp` has no pending INIT or SIPI.
    +pub(super) fn check_no_pending_init_sipi(vp: &mut impl Processor) -> anyhow::Result<()> {
    +    let pending = vp
    +        .has_pending_init_sipi()
    +        .context("failed to read the stopped VP's pending INIT and SIPI")?;
    +    anyhow::ensure!(
    +        !pending,
    +        "the guest sent the VP an INIT or SIPI that it has not accepted yet; \
    +         the snapshot is refused and the guest may request it again"
    +    );
    +    Ok(())
    +}
    +
    +impl VpSet {
    +    /// Checks that no stopped VP has a pending INIT or SIPI, before a capture
    +    /// saves the VPs.
    +    pub async fn check_no_pending_init_sipi(&mut self) -> anyhow::Result<()> {
    +        anyhow::ensure!(
    +            !self.started,
    +            "vCPUs must be stopped before checking for a pending INIT or SIPI"
    +        );
    +        self.vps
    +            .iter()
    +            .enumerate()
    +            .map(|(index, vp)| async move {
    +                vp.send
    +                    .call_failable(|rpc| VpEvent::State(StateEvent::CheckInitSipi(rpc)), ())
    +                    .await
    +                    .with_context(|| format!("vp{index} pending INIT or SIPI"))
    +            })
    +            .collect::<TryJoinAll<_>>()
    +            .await?;
    +        Ok(())
    +    }
    +}
    diff --git a/vmm_core/virt/src/generic.rs b/vmm_core/virt/src/generic.rs
    index 2d77c1d0c..fc301b2ba 100644
    --- a/vmm_core/virt/src/generic.rs
    +++ b/vmm_core/virt/src/generic.rs
    @@ -782,6 +782,20 @@ async fn run_vp(
         /// can be saved/restored correctly.
         fn flush_async_requests(&mut self);
     
    +    /// Returns whether the stopped VP holds an INIT or SIPI (x86) that it has
    +    /// not accepted yet. A save would accept such an event between reading
    +    /// the VP's registers and reading the rest of its state, so a snapshot
    +    /// capture refuses a VP for which this returns `true`.
    +    ///
    +    /// Reporting a pending SIPI may accept it, as the VP would before it runs
    +    /// again. The default reports none; a backend whose state reads accept
    +    /// pending INIT or SIPI events (KVM) overrides it.
    +    fn has_pending_init_sipi(
    +        &mut self,
    +    ) -> Result<bool, <Self::StateAccess<'_> as crate::vp::AccessVpState>::Error> {
    +        Ok(false)
    +    }
    +
         /// Returns whether the specified VTL can be inspected on this processor.
         ///
         /// VTL0 is always inspectable.
    diff --git a/vmm_core/virt_kvm/Cargo.toml b/vmm_core/virt_kvm/Cargo.toml
    index 28d2745f7..6d1686343 100644
    --- a/vmm_core/virt_kvm/Cargo.toml
    +++ b/vmm_core/virt_kvm/Cargo.toml
    @@ -36,6 +36,7 @@ tracelimit.workspace = true
     zerocopy.workspace = true
     
     [dev-dependencies]
    +pal_async.workspace = true
     test_with_tracing.workspace = true
     
     [target.'cfg(all(target_os = "linux", target_arch = "aarch64"))'.dependencies]
    diff --git a/vmm_core/virt_kvm/src/arch/x86_64/init_sipi_tests.rs b/vmm_core/virt_kvm/src/arch/x86_64/init_sipi_tests.rs
    new file mode 100644
    index 000000000..22b1d7023
    --- /dev/null
    +++ b/vmm_core/virt_kvm/src/arch/x86_64/init_sipi_tests.rs
    @@ -0,0 +1,525 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! Tests of [`Processor::has_pending_init_sipi`] on real KVM (skipped where
    +//! `/dev/kvm` is not usable), which a snapshot capture calls on every stopped
    +//! VP before it saves them.
    +//!
    +//! The guest's own INIT and SIPI, sent through the xAPIC ICR by real-mode code
    +//! on another VP, reach a VP whose thread is not in `KVM_RUN`, as at a
    +//! snapshot boundary. Each test builds two identical partitions and drives the
    +//! same guest in both: the check refuses each pending INIT or SIPI in the
    +//! first, and when both guests then continue, the VP does in the first what
    +//! it does in the second, which was not checked.
    +
    +use super::KvmProcessor;
    +use crate::Kvm;
    +use crate::KvmPartition;
    +use crate::KvmProcessorBinder;
    +use guestmem::GuestMemory;
    +use hvdef::Vtl;
    +use pal_async::DefaultPool;
    +use virt::BindProcessor;
    +use virt::Hypervisor;
    +use virt::PartitionConfig;
    +use virt::Processor;
    +use virt::ProtoPartition;
    +use virt::ProtoPartitionConfig;
    +use virt::x86::vp::AccessVpState;
    +use vm_topology::memory::MemoryLayout;
    +use vm_topology::processor::TopologyBuilder;
    +use vm_topology::processor::x86::X2ApicState;
    +use vmcore::vmtime::VmTime;
    +use vmcore::vmtime::VmTimeKeeper;
    +
    +/// The xAPIC ICR's INIT (level assert) and STARTUP delivery commands.
    +const ICR_INIT: u32 = 0x4500;
    +const ICR_STARTUP: u32 = 0x4600;
    +/// The SIPI vector: an AP starts at `SIPI_VECTOR << 12`.
    +const SIPI_VECTOR: u8 = 0x9a;
    +const SIPI_CODE_GPA: u64 = (SIPI_VECTOR as u64) << 12;
    +/// The page holding the reset vector (`0xfffffff0`), mapped so that a VP
    +/// reset by an INIT runs code.
    +const RESET_ROM_GPA: u64 = 0xffff_0000;
    +const RESET_ROM_SIZE: usize = 0x1_0000;
    +/// The real-mode code address of [`run_real_mode`].
    +const CODE_GPA: u64 = 0x1000;
    +/// The port the guest code writes when done.
    +const DONE_PORT: u16 = 0x80;
    +/// Guest code that reports where it runs: `mov eax, cr0; out DONE_PORT, al;
    +/// hlt` (the same instructions in real and 32-bit protected mode).
    +const REPORT_CODE: [u8; 6] = [0x0f, 0x20, 0xc0, 0xe6, DONE_PORT as u8, 0xf4];
    +
    +/// A partition with guest RAM at GPA 0 and a page at the reset vector.
    +struct TestVm {
    +    #[expect(dead_code)] // Held for the VPs' lifetime.
    +    partition: KvmPartition,
    +    vps: Vec<KvmProcessorBinder>,
    +    memory: GuestMemory,
    +    #[expect(dead_code)] // Mapped into the partition.
    +    reset_rom: GuestMemory,
    +}
    +
    +/// Runs `f` with two identical [`TestVm`]s of `vp_count` VPs, or skips it
    +/// when KVM is not available.
    +fn with_two_vms(vp_count: u32, f: impl FnOnce(&mut TestVm, &mut TestVm)) {
    +    if !crate::is_available().unwrap_or(false) {
    +        eprintln!("skipped: KVM is not available");
    +        return;
    +    }
    +    DefaultPool::run_with(async |driver| {
    +        let keeper = VmTimeKeeper::new(&driver, VmTime::from_100ns(0));
    +        let vmtime = keeper.builder().build(&driver).await.unwrap();
    +        let topology = TopologyBuilder::new_x86()
    +            .x2apic(X2ApicState::Supported)
    +            .build(vp_count)
    +            .unwrap();
    +        let layout = MemoryLayout::new(0x100_0000, &[], &[], &[], None).unwrap();
    +        let build = || -> TestVm {
    +            let memory = GuestMemory::allocate(layout.end_of_ram() as usize);
    +            let reset_rom = GuestMemory::allocate(RESET_ROM_SIZE);
    +            reset_rom
    +                .write_at((RESET_ROM_SIZE - 0x10) as u64, &REPORT_CODE)
    +                .unwrap();
    +            let (partition, vps) = Kvm::new()
    +                .unwrap()
    +                .new_partition(ProtoPartitionConfig {
    +                    processor_topology: &topology,
    +                    hv_config: None,
    +                    vmtime: &vmtime,
    +                    isolation: virt::ProtoPartitionIsolation::None,
    +                    nested_virt: false,
    +                    user_mode_memory_faults: false,
    +                    lazy_memory_registration: false,
    +                    time_abi: None,
    +                })
    +                .unwrap()
    +                .build(PartitionConfig {
    +                    mem_layout: &layout,
    +                    guest_memory: &memory,
    +                    cpuid: &[],
    +                    vtl0_alias_map: None,
    +                    fault_resolver: None,
    +                })
    +                .unwrap();
    +            let mapper = virt::PartitionMemoryMapper::memory_mapper(&partition, Vtl::Vtl0);
    +            for (mem, gpa) in [(&memory, 0), (&reset_rom, RESET_ROM_GPA)] {
    +                let buf = mem.inner_buf().unwrap();
    +                // SAFETY: the memory outlives the partition (both are dropped
    +                // together, the partition first).
    +                unsafe {
    +                    mapper.map_range(buf.as_ptr().cast_mut().cast(), buf.len(), gpa, true, true)
    +                }
    +                .unwrap();
    +            }
    +            TestVm {
    +                partition,
    +                vps,
    +                memory,
    +                reset_rom,
    +            }
    +        };
    +        let mut checked = build();
    +        let mut unchecked = build();
    +        f(&mut checked, &mut unchecked);
    +        drop((checked, unchecked));
    +        drop(keeper);
    +    })
    +}
    +
    +/// Runs real-mode `code` as the guest on the VP `vp` until the code writes
    +/// [`DONE_PORT`], and completes that exit as OpenVMM's VP stop does.
    +fn run_real_mode(vp: &mut KvmProcessor<'_>, memory: &GuestMemory, code: &[u8]) {
    +    memory.write_at(CODE_GPA, code).unwrap();
    +    {
    +        let mut state = vp.access_state(Vtl::Vtl0);
    +        let mut regs = state.registers().unwrap();
    +        regs.cs.base = 0;
    +        regs.cs.selector = 0;
    +        regs.rip = CODE_GPA;
    +        state.set_registers(&regs).unwrap();
    +    }
    +    assert_eq!(run_until_port(vp), DONE_PORT);
    +}
    +
    +/// Runs the VP `vp` from its current state until it writes a port, completes
    +/// that exit, and returns the port.
    +fn run_until_port(vp: &mut KvmProcessor<'_>) -> u16 {
    +    let port = loop {
    +        match vp.runner.run().unwrap() {
    +            kvm::Exit::IoOut { port, .. } => break port,
    +            kvm::Exit::Interrupted => {}
    +            exit => panic!("unexpected exit {exit:?}"),
    +        }
    +    };
    +    assert!(matches!(
    +        vp.runner.complete_exit().unwrap(),
    +        kvm::Exit::Interrupted
    +    ));
    +    port
    +}
    +
    +/// Sends an IPI as the guest on the VP `sender` does: real-mode code writes
    +/// `dest_apic_id` to the xAPIC ICR2 and `icr` to the ICR (through a real-mode
    +/// FS base at the LAPIC page).
    +fn send_ipi_as_guest(
    +    sender: &mut KvmProcessor<'_>,
    +    memory: &GuestMemory,
    +    dest_apic_id: u8,
    +    icr: u32,
    +) {
    +    {
    +        let mut state = sender.access_state(Vtl::Vtl0);
    +        let mut regs = state.registers().unwrap();
    +        regs.fs.base = 0xfee0_0000;
    +        state.set_registers(&regs).unwrap();
    +    }
    +    let mut code = vec![0x66, 0x64, 0xc7, 0x06, 0x10, 0x03]; // mov dword fs:[0x310], imm32
    +    code.extend((u32::from(dest_apic_id) << 24).to_le_bytes());
    +    code.extend([0x66, 0x64, 0xc7, 0x06, 0x00, 0x03]); // mov dword fs:[0x300], imm32
    +    code.extend(icr.to_le_bytes());
    +    code.extend([0xe6, DONE_PORT as u8, 0xf4]);
    +    run_real_mode(sender, memory, &code);
    +}
    +
    +/// Whether KVM holds an INIT latched for `vp` (`KVM_GET_VCPU_EVENTS`, which
    +/// does not accept it).
    +fn init_latched(vp: &KvmProcessor<'_>) -> bool {
    +    vp.kvm.get_vcpu_events().unwrap().smi.latched_init != 0
    +}
    +
    +/// What KVM holds for `vp`, read without accepting a pending event: the
    +/// registers, the special registers, the events and the LAPIC page but for
    +/// its timer's current count.
    +fn held(vp: &KvmProcessor<'_>) -> (kvm::kvm_regs, kvm::kvm_sregs, kvm::kvm_vcpu_events, Vec<u8>) {
    +    let mut events = vp.kvm.get_vcpu_events().unwrap();
    +    events.flags = 0;
    +    let mut lapic = [0u8; 1024];
    +    vp.kvm.get_lapic(&mut lapic).unwrap();
    +    lapic[0x390..0x394].fill(0);
    +    (
    +        vp.kvm.get_regs().unwrap(),
    +        vp.kvm.get_sregs().unwrap(),
    +        events,
    +        lapic.to_vec(),
    +    )
    +}
    +
    +/// Makes the AP `ap` look like an AP that has been running the guest: in
    +/// 32-bit protected mode at 0x10:0x5000 with TPR 0x30, runnable.
    +fn run_ap_in_protected_mode(ap: &mut KvmProcessor<'_>) {
    +    {
    +        let mut state = ap.access_state(Vtl::Vtl0);
    +        let mut regs = state.registers().unwrap();
    +        regs.cr0 = 0x11; // PE | ET
    +        regs.cs = virt::x86::SegmentRegister {
    +            base: 0,
    +            limit: 0xffff_ffff,
    +            selector: 0x10,
    +            attributes: 0xc09b, // present 32-bit code, granular
    +        };
    +        regs.rip = 0x5000;
    +        regs.rsp = 0x7000;
    +        regs.rax = 0xa5a5;
    +        state.set_registers(&regs).unwrap();
    +        let mut apic = state.apic().unwrap();
    +        apic.registers[0x8] = 0x30; // TPR
    +        state.set_apic(&apic).unwrap();
    +    }
    +    ap.kvm.set_mp_state(kvm::KVM_MP_STATE_RUNNABLE).unwrap();
    +}
    +
    +/// What the VP `vp` does when the guest continues: it runs until its next
    +/// port write, then the port and the registers it holds.
    +fn continue_guest(vp: &mut KvmProcessor<'_>) -> (u16, kvm::kvm_regs, kvm::kvm_sregs) {
    +    let port = run_until_port(vp);
    +    (
    +        port,
    +        vp.kvm.get_regs().unwrap(),
    +        vp.kvm.get_sregs().unwrap(),
    +    )
    +}
    +
    +/// The guest's INIT reaches a running AP whose VP is stopped (finding
    +/// PART-11's described case, lane f-partition's
    +/// `falsify_pu2_pending_init_restores_ap_with_stale_registers`). The check
    +/// refuses it without accepting it, and when the guest continues with its
    +/// SIPI, the AP starts at the SIPI vector in real mode, as unchecked.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn pending_init_on_an_ap_is_refused() {
    +    with_two_vms(2, |checked, unchecked| {
    +        let continued = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            vm.memory.write_at(SIPI_CODE_GPA, &REPORT_CODE).unwrap();
    +            let [bsp, ap] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut ap = ap.bind().unwrap();
    +            run_ap_in_protected_mode(&mut ap);
    +            send_ipi_as_guest(&mut bsp, &vm.memory, 1, ICR_INIT);
    +            assert!(init_latched(&ap), "the INIT is pending");
    +            if check {
    +                let before = held(&ap);
    +                assert!(ap.has_pending_init_sipi().unwrap(), "the check refuses it");
    +                assert_eq!(held(&ap), before, "the check accepted nothing");
    +                assert!(init_latched(&ap), "the INIT is still pending");
    +            }
    +            send_ipi_as_guest(
    +                &mut bsp,
    +                &vm.memory,
    +                1,
    +                ICR_STARTUP | u32::from(SIPI_VECTOR),
    +            );
    +            continue_guest(&mut ap)
    +        });
    +        assert_eq!(continued[0], continued[1], "the AP continues as unchecked");
    +        let (port, regs, sregs) = continued[0];
    +        assert_eq!(port, DONE_PORT);
    +        assert_eq!((sregs.cs.selector, sregs.cs.base), (0x9a00, SIPI_CODE_GPA));
    +        assert_eq!(regs.rax & 1, 0, "real mode: the INIT reset the AP");
    +    })
    +}
    +
    +/// The SIPI alone is pending: the AP took the guest's INIT and waits for a
    +/// SIPI, and the guest's SIPI reached it while its VP was stopped (lane
    +/// f-partition's `witness_part11_pending_sipi_restores_ap_running_at_reset_vector`).
    +/// The check refuses it; reporting it accepts the SIPI, as `KVM_RUN` does
    +/// first: the AP is then runnable at the SIPI vector, and when the guest
    +/// continues it runs there, as unchecked.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn pending_sipi_on_an_ap_is_refused() {
    +    with_two_vms(2, |checked, unchecked| {
    +        let continued = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            vm.memory.write_at(SIPI_CODE_GPA, &REPORT_CODE).unwrap();
    +            let [bsp, ap] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut ap = ap.bind().unwrap();
    +            send_ipi_as_guest(&mut bsp, &vm.memory, 1, ICR_INIT);
    +            assert_eq!(
    +                ap.kvm.get_mp_state().unwrap(),
    +                kvm::KVM_MP_STATE_INIT_RECEIVED,
    +                "the AP took the INIT"
    +            );
    +            send_ipi_as_guest(
    +                &mut bsp,
    +                &vm.memory,
    +                1,
    +                ICR_STARTUP | u32::from(SIPI_VECTOR),
    +            );
    +            assert!(!init_latched(&ap));
    +            if check {
    +                assert!(ap.has_pending_init_sipi().unwrap(), "the check refuses it");
    +                assert_eq!(ap.kvm.get_mp_state().unwrap(), kvm::KVM_MP_STATE_RUNNABLE);
    +                let (regs, sregs) = (ap.kvm.get_regs().unwrap(), ap.kvm.get_sregs().unwrap());
    +                assert_eq!(
    +                    (sregs.cs.selector, sregs.cs.base, regs.rip),
    +                    (0x9a00, SIPI_CODE_GPA, 0),
    +                    "the check accepted the SIPI"
    +                );
    +            }
    +            continue_guest(&mut ap)
    +        });
    +        assert_eq!(continued[0], continued[1], "the AP continues as unchecked");
    +        let (port, regs, sregs) = continued[0];
    +        assert_eq!(port, DONE_PORT);
    +        assert_eq!((sregs.cs.selector, sregs.cs.base), (0x9a00, SIPI_CODE_GPA));
    +        assert_eq!(regs.rax & 1, 0, "real mode");
    +    })
    +}
    +
    +/// The guest's INIT and SIPI both reached a running AP whose VP was stopped
    +/// (lane f-partition's
    +/// `witness_part11_pending_init_and_sipi_restore_ap_running_its_old_code`).
    +/// The check refuses them without accepting either, and when the guest
    +/// continues, the AP takes both and runs at the SIPI vector in real mode, as
    +/// unchecked.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn pending_init_and_sipi_on_a_running_ap_are_refused() {
    +    with_two_vms(2, |checked, unchecked| {
    +        let continued = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            vm.memory.write_at(SIPI_CODE_GPA, &REPORT_CODE).unwrap();
    +            let [bsp, ap] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut ap = ap.bind().unwrap();
    +            run_ap_in_protected_mode(&mut ap);
    +            send_ipi_as_guest(&mut bsp, &vm.memory, 1, ICR_INIT);
    +            send_ipi_as_guest(
    +                &mut bsp,
    +                &vm.memory,
    +                1,
    +                ICR_STARTUP | u32::from(SIPI_VECTOR),
    +            );
    +            assert!(init_latched(&ap));
    +            if check {
    +                let before = held(&ap);
    +                assert!(
    +                    ap.has_pending_init_sipi().unwrap(),
    +                    "the check refuses them"
    +                );
    +                assert_eq!(held(&ap), before, "the check accepted nothing");
    +            }
    +            continue_guest(&mut ap)
    +        });
    +        assert_eq!(continued[0], continued[1], "the AP continues as unchecked");
    +        let (port, regs, sregs) = continued[0];
    +        assert_eq!(port, DONE_PORT);
    +        assert_eq!((sregs.cs.selector, sregs.cs.base), (0x9a00, SIPI_CODE_GPA));
    +        assert_eq!(regs.rax & 1, 0, "real mode: the INIT reset the AP");
    +    })
    +}
    +
    +/// An INIT is pending on the BSP: the guest's AP sent it while the BSP's VP
    +/// was stopped (lane f-partition's `witness_part11_pending_init_on_bsp_is_lost`
    +/// latched it from the host). The check refuses it without accepting it, and
    +/// when the guest continues, the BSP takes the INIT and runs from the reset
    +/// vector, as unchecked.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn pending_init_on_the_bsp_is_refused() {
    +    with_two_vms(2, |checked, unchecked| {
    +        let continued = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            let [bsp, ap] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut ap = ap.bind().unwrap();
    +            // The BSP has been running the guest's real-mode code.
    +            run_real_mode(&mut bsp, &vm.memory, &[0xe6, DONE_PORT as u8, 0xf4]);
    +            ap.kvm.set_mp_state(kvm::KVM_MP_STATE_RUNNABLE).unwrap();
    +            send_ipi_as_guest(&mut ap, &vm.memory, 0, ICR_INIT);
    +            assert!(init_latched(&bsp), "the INIT is pending");
    +            if check {
    +                let before = held(&bsp);
    +                assert!(bsp.has_pending_init_sipi().unwrap(), "the check refuses it");
    +                assert_eq!(held(&bsp), before, "the check accepted nothing");
    +            }
    +            continue_guest(&mut bsp)
    +        });
    +        assert_eq!(continued[0], continued[1], "the BSP continues as unchecked");
    +        let (port, regs, sregs) = continued[0];
    +        assert_eq!(port, DONE_PORT);
    +        assert_eq!(
    +            (sregs.cs.selector, sregs.cs.base, regs.rax & 1),
    +            (0xf000, 0xffff_0000, 0),
    +            "the BSP runs from the reset vector"
    +        );
    +    })
    +}
    +
    +/// VPs without a pending INIT or SIPI are not refused, the check changes
    +/// nothing a save reads, and a save after the check is the save without it:
    +/// a BSP that ran the guest, an AP that never received INIT, an AP waiting
    +/// for SIPI, a running AP and a halted one.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn vps_without_pending_init_or_sipi_are_not_refused() {
    +    with_two_vms(5, |checked, unchecked| {
    +        let saved = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            vm.memory.write_at(SIPI_CODE_GPA, &REPORT_CODE).unwrap();
    +            let [bsp, uninitialized, waiting, running, halted] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut uninitialized = uninitialized.bind().unwrap();
    +            let mut waiting = waiting.bind().unwrap();
    +            let mut running = running.bind().unwrap();
    +            let mut halted = halted.bind().unwrap();
    +            send_ipi_as_guest(&mut bsp, &vm.memory, 2, ICR_INIT);
    +            send_ipi_as_guest(&mut bsp, &vm.memory, 3, ICR_INIT);
    +            send_ipi_as_guest(
    +                &mut bsp,
    +                &vm.memory,
    +                3,
    +                ICR_STARTUP | u32::from(SIPI_VECTOR),
    +            );
    +            assert_eq!(continue_guest(&mut running).0, DONE_PORT);
    +            assert_eq!(
    +                waiting.kvm.get_mp_state().unwrap(),
    +                kvm::KVM_MP_STATE_INIT_RECEIVED
    +            );
    +            halted.kvm.set_mp_state(kvm::KVM_MP_STATE_HALTED).unwrap();
    +            let mut saved = Vec::new();
    +            for (vp, mp_state) in [
    +                (&mut bsp, kvm::KVM_MP_STATE_RUNNABLE),
    +                (&mut uninitialized, kvm::KVM_MP_STATE_UNINITIALIZED),
    +                (&mut waiting, kvm::KVM_MP_STATE_INIT_RECEIVED),
    +                (&mut running, kvm::KVM_MP_STATE_RUNNABLE),
    +                (&mut halted, kvm::KVM_MP_STATE_HALTED),
    +            ] {
    +                if check {
    +                    let before = held(vp);
    +                    assert!(!vp.has_pending_init_sipi().unwrap(), "not refused");
    +                    assert_eq!(held(vp), before, "the check changed nothing");
    +                }
    +                assert_eq!(vp.kvm.get_mp_state().unwrap(), mp_state);
    +                // The LAPIC timer never runs here, so its current count (TMCCT)
    +                // is 0 in both partitions; only the TSC differs between them.
    +                let apic = vp.access_state(Vtl::Vtl0).apic().unwrap();
    +                assert_eq!(apic.registers[0x39], 0, "TMCCT");
    +                let mut state = vp.access_state(Vtl::Vtl0).save_all().unwrap();
    +                state.clear_tsc();
    +                saved.push(state);
    +            }
    +            saved
    +        });
    +        assert_eq!(saved[0], saved[1], "the save is the save without the check");
    +    })
    +}
    +
    +/// A SIPI to a VP that does not wait for one: KVM drops it when the VP next
    +/// runs, as the CPU does, so the save loses nothing. The check drops it too
    +/// and does not refuse, and when the guest continues, the VP runs on as
    +/// unchecked.
    +#[test]
    +#[ignore = "requires /dev/kvm"]
    +fn sipi_to_a_running_vp_is_dropped_and_not_refused() {
    +    with_two_vms(2, |checked, unchecked| {
    +        let continued = [(checked, true), (unchecked, false)].map(|(vm, check)| {
    +            let [bsp, ap] = &mut vm.vps[..] else {
    +                unreachable!()
    +            };
    +            let mut bsp = bsp.bind().unwrap();
    +            let mut ap = ap.bind().unwrap();
    +            // The AP runs the guest's real-mode code at 0x1000.
    +            vm.memory.write_at(CODE_GPA, &REPORT_CODE).unwrap();
    +            {
    +                let mut state = ap.access_state(Vtl::Vtl0);
    +                let mut regs = state.registers().unwrap();
    +                regs.cs.base = 0;
    +                regs.cs.selector = 0;
    +                regs.rip = CODE_GPA;
    +                state.set_registers(&regs).unwrap();
    +            }
    +            ap.kvm.set_mp_state(kvm::KVM_MP_STATE_RUNNABLE).unwrap();
    +            send_ipi_as_guest(
    +                &mut bsp,
    +                &vm.memory,
    +                1,
    +                ICR_STARTUP | u32::from(SIPI_VECTOR),
    +            );
    +            // The BSP's code overwrote the AP's; put the AP's back.
    +            vm.memory.write_at(CODE_GPA, &REPORT_CODE).unwrap();
    +            if check {
    +                let before = held(&ap);
    +                assert!(!ap.has_pending_init_sipi().unwrap(), "not refused");
    +                assert_eq!(held(&ap), before);
    +            }
    +            continue_guest(&mut ap)
    +        });
    +        assert_eq!(continued[0], continued[1], "the AP continues as unchecked");
    +        let (port, regs, sregs) = continued[0];
    +        assert_eq!(port, DONE_PORT);
    +        assert_eq!(
    +            (sregs.cs.selector, regs.rip),
    +            (0, CODE_GPA + 5),
    +            "the AP ran on"
    +        );
    +    })
    +}
    diff --git a/vmm_core/virt_kvm/src/arch/x86_64/mod.rs b/vmm_core/virt_kvm/src/arch/x86_64/mod.rs
    index d6b9daafa..ee45911ab 100644
    --- a/vmm_core/virt_kvm/src/arch/x86_64/mod.rs
    +++ b/vmm_core/virt_kvm/src/arch/x86_64/mod.rs
    @@ -2064,6 +2064,21 @@ async fn run_vp(
     
         fn flush_async_requests(&mut self) {}
     
    +    fn has_pending_init_sipi(&mut self) -> Result<bool, KvmError> {
    +        // KVM_GET_VCPU_EVENTS reports a latched INIT without accepting it.
    +        if self.kvm.get_vcpu_events()?.smi.latched_init != 0 {
    +            return Ok(true);
    +        }
    +        // KVM reports a pending SIPI only by accepting it: KVM_GET_MP_STATE
    +        // accepts the VP's pending INIT and SIPI (`kvm_apic_accept_events`),
    +        // as KVM_RUN does before the VP runs again. A SIPI starts a VP that
    +        // waits for one at the SIPI vector, which moves its CS and RIP, and a
    +        // SIPI to any other VP is dropped.
    +        let before = (self.kvm.get_regs()?, self.kvm.get_sregs()?);
    +        self.kvm.get_mp_state()?;
    +        Ok((self.kvm.get_regs()?, self.kvm.get_sregs()?) != before)
    +    }
    +
         fn reset(&mut self) -> Result<(), impl std::error::Error + Send + Sync + 'static> {
             let vp_info = self.inner.vp_info;
             self.access_state(Vtl::Vtl0).reset_all(&vp_info)
    @@ -2203,6 +2218,9 @@ fn signal_msi(&self, _devid: Option<u32>, address: u64, data: u32) {
         }
     }
     
    +#[cfg(test)]
    +mod init_sipi_tests;
    +
     #[cfg(test)]
     mod tests {
         use super::kvm_recommends_hyperv_apic_msrs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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