From c5509cde20d624fa49b90722c34ad292ab7672cb Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Fri, 13 Nov 2026 19:56:03 -0800 Subject: [PATCH] virt: hold each SYSENTER MSR in the field that names it On KVM, MSHV, and WHP, VirtualMsrs::sysenter_eip held IA32_SYSENTER_ESP and sysenter_esp held IA32_SYSENTER_EIP. names() lists SysenterEsp before SysenterEip, but get_values and set_values paired the registers with the fields in the other order. A save and a restore round-tripped, since the two swaps cancel, but anything that read the fields by name saw the wrong register: inspect output and tools that decode saved state. OpenHCL's TDX and SNP processors fill the same fields by name, so a field held different registers on different backends. Pair the registers with the fields in the order that names() lists them, and number the fields in the order of the MSRs, field 3 for IA32_SYSENTER_ESP and field 4 for IA32_SYSENTER_EIP, declaring them in that order. The saved layout does not change, so no version or marker is needed: the saved states of KVM, MSHV, and WHP have always held ESP in field 3 and EIP in field 4, so each of them restores to the right registers, and a state saved after this change restores to the right registers in a build from before it. No OpenHCL saved state holds VirtualMsrs: the TDX, SNP, and CCA processors refuse to save, and the hypervisor-backed processor saves fields of its own. HvRegisterState now documents the contract that VirtualMsrs broke: names, get_values, and set_values list the same registers in the same order. Tests: the three new tests in virt fail at 5d2a71027 and pass here. Two check that the registers that VirtualMsrs writes and reads carry the values of their own fields; the second is the issue's reproduction, registers SysenterEip and SysenterEsp holding 0x1111 and 0x2222, which reads as sysenter_eip 0x2222 and sysenter_esp 0x1111 at 5d2a71027. The third checks that a VirtualMsrs decodes a state that holds IA32_SYSENTER_ESP in field 3 and IA32_SYSENTER_EIP in field 4, and encodes one that does. The 108 tests of virt pass on Windows and on Azure Linux 3.0, with clippy and rustdoc. On nvx-kvm-test, nvx-mshv-test, and nvx-whp-test, a guest read the IA32_SYSENTER_ESP and IA32_SYSENTER_EIP of each of its one or two CPUs through /dev/cpu/N/msr, with EIP the address of entry_SYSENTER_compat, and requested a snapshot. OpenVMM built from 157866605, which lacks only the EFER.SVME restore check of 5d2a71027, saved the ESP in field 3 and the EIP in field 4 of each VP's VirtualMsrs; OpenVMM built from this commit saved the same bytes. A snapshot that either build captured, restored with the other build, kept each CPU's two MSRs. Reported in microsoft/nvx#1367. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vmm_core/virt/src/state.rs | 6 ++ vmm_core/virt/src/x86/vp.rs | 131 ++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 5 deletions(-) diff --git a/vmm_core/virt/src/state.rs b/vmm_core/virt/src/state.rs index f30ba98e145..fd747246f2d 100644 --- a/vmm_core/virt/src/state.rs +++ b/vmm_core/virt/src/state.rs @@ -29,8 +29,14 @@ pub trait StateElement: Sized + Inspect { } pub trait HvRegisterState: Default { + /// Returns the registers that hold this state, in the order in which + /// `get_values` and `set_values` pair them with values. fn names(&self) -> &'static [T; COUNT]; + /// Writes this state's value of each register that `names` lists, in that + /// order, to `it`. fn get_values<'a>(&self, it: impl Iterator); + /// Sets this state from the value of each register that `names` lists, in + /// that order, which `it` yields. fn set_values(&mut self, it: impl Iterator); } diff --git a/vmm_core/virt/src/x86/vp.rs b/vmm_core/virt/src/x86/vp.rs index a1392408eb5..01749185bd8 100644 --- a/vmm_core/virt/src/x86/vp.rs +++ b/vmm_core/virt/src/x86/vp.rs @@ -1443,6 +1443,12 @@ impl StateElement for Mtrrs { } } +/// The virtual MSRs of a VP. +/// +/// The saved state holds IA32_SYSENTER_ESP (MSR 0x175) in field 3 and +/// IA32_SYSENTER_EIP (MSR 0x176) in field 4, in the order of their MSR +/// numbers. KVM, MSHV, and WHP have always saved them there, so existing saved +/// states restore only while the fields keep their numbers. #[repr(C)] #[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)] #[mesh(package = "virt.x86")] @@ -1453,9 +1459,9 @@ pub struct VirtualMsrs { #[mesh(2)] pub sysenter_cs: u64, #[mesh(3)] - pub sysenter_eip: u64, - #[mesh(4)] pub sysenter_esp: u64, + #[mesh(4)] + pub sysenter_eip: u64, #[mesh(5)] pub star: u64, #[mesh(6)] @@ -1484,8 +1490,8 @@ impl HvRegisterState for VirtualMsrs { for (dest, src) in it.zip([ self.kernel_gs_base, self.sysenter_cs, - self.sysenter_eip, self.sysenter_esp, + self.sysenter_eip, self.star, self.lstar, self.cstar, @@ -1499,8 +1505,8 @@ impl HvRegisterState for VirtualMsrs { for (src, dest) in it.zip([ &mut self.kernel_gs_base, &mut self.sysenter_cs, - &mut self.sysenter_eip, &mut self.sysenter_esp, + &mut self.sysenter_eip, &mut self.star, &mut self.lstar, &mut self.cstar, @@ -1520,8 +1526,8 @@ impl StateElement for VirtualMsrs { Self { kernel_gs_base: 0, sysenter_cs: 0, - sysenter_eip: 0, sysenter_esp: 0, + sysenter_eip: 0, star: 0, lstar: 0, cstar: 0, @@ -2152,4 +2158,119 @@ mod tests { assert_eq!(without_tsc.take_one_shot_timer().unwrap().saved_tsc, None); assert_eq!(VpSavedState::default().take_one_shot_timer(), None); } + + // Distinct values, so that a mix-up of two registers shows. + const REGISTERS: [(HvX64RegisterName, u64); 8] = [ + (HvX64RegisterName::KernelGsBase, 0x10), + (HvX64RegisterName::SysenterCs, 0x20), + (HvX64RegisterName::SysenterEip, 0x1111), + (HvX64RegisterName::SysenterEsp, 0x2222), + (HvX64RegisterName::Star, 0x30), + (HvX64RegisterName::Lstar, 0x40), + (HvX64RegisterName::Cstar, 0x50), + (HvX64RegisterName::Sfmask, 0x60), + ]; + + /// The virtual MSRs that hold the values of `REGISTERS`. + fn virtual_msrs() -> VirtualMsrs { + VirtualMsrs { + kernel_gs_base: 0x10, + sysenter_cs: 0x20, + sysenter_eip: 0x1111, + sysenter_esp: 0x2222, + star: 0x30, + lstar: 0x40, + cstar: 0x50, + sfmask: 0x60, + } + } + + #[test] + fn virtual_msrs_write_each_register_from_its_own_field() { + let msrs = virtual_msrs(); + let mut values = [HvRegisterValue::from(0u64); 8]; + msrs.get_values(values.iter_mut()); + let written = msrs + .names() + .iter() + .copied() + .zip(values.map(|value| value.as_u64())) + .collect::>(); + assert_eq!(written.len(), REGISTERS.len()); + for register in REGISTERS { + assert!( + written.contains(®ister), + "{register:?} is not written: {written:?}" + ); + } + } + + #[test] + fn virtual_msrs_read_each_register_into_its_own_field() { + let mut msrs = VirtualMsrs::default(); + let values = msrs + .names() + .iter() + .map(|name| { + let (_, value) = REGISTERS + .iter() + .find(|(register, _)| register == name) + .expect("the test holds every register of the virtual MSRs"); + HvRegisterValue::from(*value) + }) + .collect::>(); + msrs.set_values(values.into_iter()); + assert_eq!(msrs, virtual_msrs()); + } + + /// The saved fields of a `VirtualMsrs` named for the MSRs that they hold, + /// as the saved states of KVM, MSHV, and WHP have always had them: + /// IA32_SYSENTER_ESP (0x175) in field 3 and IA32_SYSENTER_EIP (0x176) in + /// field 4. + #[derive(Debug, PartialEq, Protobuf)] + struct SavedMsrs { + #[mesh(1)] + kernel_gs_base: u64, + #[mesh(2)] + ia32_sysenter_cs: u64, + #[mesh(3)] + ia32_sysenter_esp: u64, + #[mesh(4)] + ia32_sysenter_eip: u64, + #[mesh(5)] + star: u64, + #[mesh(6)] + lstar: u64, + #[mesh(7)] + cstar: u64, + #[mesh(8)] + sfmask: u64, + } + + fn saved_msrs() -> SavedMsrs { + SavedMsrs { + kernel_gs_base: 0x10, + ia32_sysenter_cs: 0x20, + ia32_sysenter_esp: 0x2222, + ia32_sysenter_eip: 0x1111, + star: 0x30, + lstar: 0x40, + cstar: 0x50, + sfmask: 0x60, + } + } + + #[test] + fn virtual_msrs_keep_the_layout_of_earlier_saved_states() { + // A saved state of an earlier release restores to the right registers, + // and a release that restores this one's saved state does as well. + assert_eq!( + mesh_protobuf::decode::(&mesh_protobuf::encode(saved_msrs())).unwrap(), + virtual_msrs() + ); + assert_eq!( + mesh_protobuf::decode::(&mesh_protobuf::encode(virtual_msrs())).unwrap(), + saved_msrs() + ); + } }