Skip to content

Commit 1c093df

Browse files
authored
virt: save and restore architectural TSC-deadline state (#4704)
Preserve IA32_TSC_DEADLINE independently of the LAPIC register image so snapshots retain armed deadlines. Gate the state on guest and backend support, restore it after APIC configuration and TSC, and clear it on reset and INIT. Wire native KVM, MSHV, WHP, and OpenHCL register access while excluding software APIC configurations. KVM uses direct MSR access here; partition clock freezing remains a separate change.
1 parent 38968a3 commit 1c093df

14 files changed

Lines changed: 117 additions & 7 deletions

File tree

‎Guide/src/reference/architecture/timekeeping.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ have different stop/start anchors.
219219
| Snapshot restore | Restores stopped software time and the state supported by each backend/device. A time-state setter must not implicitly thaw a supporting backend. |
220220
| VTL scrub | May reset and freeze a backing VTL clock. The partition unit thaws it before restarting VPs; this is not a freeze of every VTL. |
221221

222-
The x86 `virt` saved-state model treats reference time, reference-page
223-
configuration, TSC, and APIC state as separate elements. Reference time
224-
is included only when Hyper-V enlightenments are enabled; lifecycle
225-
freezing cannot therefore be hidden in its setter. The
226-
`can_freeze_time` capability also governs whether advancing TSC/APIC
227-
and reference-time state can be compared during state validation. A
228-
reference-clock-only implementation is not sufficient to claim that
222+
The x86 `virt` model separates reference time/page, TSC, APIC, and TSC deadline.
223+
Deadline state requires guest/backend support and restores after APIC/TSC:
224+
zero disarms; absent state is not restored. Native MSHV/WHP and KVM support
225+
it, unlike software APICs. Reference time is included only when Hyper-V
226+
enlightenments are enabled; lifecycle freezing cannot be hidden in its setter.
227+
Lifecycle freeze is separate; `can_freeze_time` gates exact state comparison.
228+
A reference-clock-only implementation is not sufficient to claim that
229229
capability.
230230

231231
Arm state coverage differs: the generic `virt` partition-state schema is

‎openhcl/virt_mshv_vtl/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,9 @@ impl UhPartition {
24992499

25002500
// Compute and validate capabilities.
25012501
let mut caps = virt::x86::X86PartitionCapabilities::from_cpuid(topology, cpuid_fn)?;
2502+
// Hardware-isolated guests use our software APIC, which does not
2503+
// implement the architectural TSC-deadline MSR.
2504+
caps.tsc_deadline &= !isolation.is_hardware_isolated();
25022505
match isolation {
25032506
IsolationType::Tdx => {
25042507
assert_eq!(caps.vtom.is_some(), !hide_isolation);

‎openhcl/virt_mshv_vtl/src/processor/mshv/x64.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,14 @@ impl AccessVpState for UhVpStateAccess<'_, '_, HypervisorBackedX86> {
16341634
self.set_register_state(value)
16351635
}
16361636

1637+
fn tsc_deadline(&mut self) -> Result<vp::TscDeadline, Self::Error> {
1638+
self.get_register_state()
1639+
}
1640+
1641+
fn set_tsc_deadline(&mut self, value: &vp::TscDeadline) -> Result<(), Self::Error> {
1642+
self.set_register_state(value)
1643+
}
1644+
16371645
fn cet(&mut self) -> Result<vp::Cet, Self::Error> {
16381646
self.get_register_state()
16391647
}

‎openhcl/virt_mshv_vtl/src/processor/snp/mod.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,14 @@ impl AccessVpState for UhVpStateAccess<'_, '_, SnpBacked> {
29272927
Err(vp_state::Error::Unimplemented("tsc"))
29282928
}
29292929

2930+
fn tsc_deadline(&mut self) -> Result<vp::TscDeadline, Self::Error> {
2931+
Err(vp_state::Error::Unimplemented("tsc_deadline"))
2932+
}
2933+
2934+
fn set_tsc_deadline(&mut self, _value: &vp::TscDeadline) -> Result<(), Self::Error> {
2935+
Err(vp_state::Error::Unimplemented("tsc_deadline"))
2936+
}
2937+
29302938
fn tsc_aux(&mut self) -> Result<vp::TscAux, Self::Error> {
29312939
let vmsa = self.vp.runner.vmsa(self.vtl);
29322940
Ok(vp::TscAux {

‎openhcl/virt_mshv_vtl/src/processor/tdx/mod.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,6 +4192,14 @@ impl AccessVpState for UhVpStateAccess<'_, '_, TdxBacked> {
41924192
Err(vp_state::Error::Unimplemented("tsc"))
41934193
}
41944194

4195+
fn tsc_deadline(&mut self) -> Result<vp::TscDeadline, Self::Error> {
4196+
Err(vp_state::Error::Unimplemented("tsc_deadline"))
4197+
}
4198+
4199+
fn set_tsc_deadline(&mut self, _value: &vp::TscDeadline) -> Result<(), Self::Error> {
4200+
Err(vp_state::Error::Unimplemented("tsc_deadline"))
4201+
}
4202+
41954203
fn tsc_aux(&mut self) -> Result<vp::TscAux, Self::Error> {
41964204
Ok(vp::TscAux {
41974205
value: self.vp.backing.vtls[self.vtl].private_regs.msr_tsc_aux,

‎vmm_core/virt/src/x86/mod.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ pub struct X86PartitionCapabilities {
7474
pub sgx: bool,
7575
/// TSC_AUX is supported
7676
pub tsc_aux: bool,
77+
/// Architectural TSC-deadline state is available. Backends without access
78+
/// to this MSR must clear this even if guest CPUID advertises the feature.
79+
pub tsc_deadline: bool,
7780
/// The address of the virtual top of memory, for encrypted VMs.
7881
///
7982
/// This is computed from the Hyper-V isolation leaf. It is guaranteed to be
@@ -133,6 +136,7 @@ impl X86PartitionCapabilities {
133136
cet_ss: false,
134137
sgx: false,
135138
tsc_aux: false,
139+
tsc_deadline: false,
136140
vtom: None,
137141
physical_address_width: max_physical_address_size_from_cpuid(&mut *f),
138142
snp_c_bit: snp_c_bit_from_cpuid(&mut *f),
@@ -156,6 +160,7 @@ impl X86PartitionCapabilities {
156160
this.reset_rdx = result[0].into();
157161
let features = result[2] as u64 | ((result[3] as u64) << 32);
158162
this.x2apic = features & (1 << 21) != 0;
163+
this.tsc_deadline = features & (1 << 24) != 0;
159164
xsave = features & (1 << 26) != 0;
160165
hypervisor = features & (1 << 31) != 0;
161166
}

‎vmm_core/virt/src/x86/vp.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,51 @@ impl StateElement<X86PartitionCapabilities, X86VpInfo> for Tsc {
16101610
}
16111611
}
16121612

1613+
/// The architectural IA32_TSC_DEADLINE MSR, in guest TSC ticks.
1614+
///
1615+
/// Zero disarms the timer. Restore after both APIC configuration and TSC;
1616+
/// changing APIC mode or rebasing TSC can otherwise change the expiration.
1617+
#[repr(C)]
1618+
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Protobuf, Inspect)]
1619+
#[mesh(package = "virt.x86")]
1620+
pub struct TscDeadline {
1621+
#[mesh(1)]
1622+
#[inspect(hex)]
1623+
pub value: u64,
1624+
}
1625+
1626+
impl HvRegisterState<HvX64RegisterName, 1> for TscDeadline {
1627+
fn names(&self) -> &'static [HvX64RegisterName; 1] {
1628+
&[HvX64RegisterName::TscDeadline]
1629+
}
1630+
1631+
fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1632+
for (dest, src) in it.zip([self.value]) {
1633+
*dest = src.into();
1634+
}
1635+
}
1636+
1637+
fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1638+
for (src, dest) in it.zip([&mut self.value]) {
1639+
*dest = src.as_u64();
1640+
}
1641+
}
1642+
}
1643+
1644+
impl StateElement<X86PartitionCapabilities, X86VpInfo> for TscDeadline {
1645+
fn is_present(caps: &X86PartitionCapabilities) -> bool {
1646+
caps.tsc_deadline
1647+
}
1648+
1649+
fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1650+
Self::default()
1651+
}
1652+
1653+
fn can_compare(caps: &X86PartitionCapabilities) -> bool {
1654+
caps.can_freeze_time
1655+
}
1656+
}
1657+
16131658
#[repr(C)]
16141659
#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
16151660
#[mesh(package = "virt.x86")]
@@ -1962,6 +2007,8 @@ state_trait! {
19622007
(12, "cet", cet, set_cet, Cet),
19632008
(13, "cet_ss", cet_ss, set_cet_ss, CetSs),
19642009
(14, "tsc_aux", tsc_aux, set_tsc_aux, TscAux),
2010+
// The deadline must be restored after APIC mode and guest TSC.
2011+
(15, "tsc_deadline", tsc_deadline, set_tsc_deadline, TscDeadline),
19652012

19662013
// Synic state
19672014
(100, "synic", synic_msrs, set_synic_msrs, SyntheticMsrs),
@@ -2015,6 +2062,9 @@ pub fn x86_init<T: AccessVpState>(access: &mut T, vp_info: &X86VpInfo) -> Result
20152062
current_apic.registers[x86defs::apic::ApicRegister::ID.0 as usize];
20162063
apic.apic_base = current_apic.apic_base;
20172064
access.set_apic(&apic)?;
2065+
if TscDeadline::is_present(access.caps()) {
2066+
access.set_tsc_deadline(&TscDeadline::at_reset(access.caps(), vp_info))?;
2067+
}
20182068

20192069
// Enable the wait-for-SIPI state.
20202070
if !vp_info.base.is_bsp() {

‎vmm_core/virt_kvm/src/arch/x86_64/regs.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub const fn register_to_msr(name: HvX64RegisterName) -> Result<u32, NoRegisterM
5353

5454
HvX64RegisterName::Tsc => x86defs::X86X_MSR_TSC,
5555
HvX64RegisterName::TscAux => x86defs::X86X_MSR_TSC_AUX,
56+
HvX64RegisterName::TscDeadline => 0x6e0, // IA32_TSC_DEADLINE
5657

5758
HvX64RegisterName::Xss => x86defs::X86X_MSR_XSS,
5859
HvX64RegisterName::UCet => x86defs::X86X_MSR_U_CET,

‎vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,14 @@ impl AccessVpState for KvmVpStateAccess<'_, '_> {
539539
self.set_register_state(tsc)
540540
}
541541

542+
fn tsc_deadline(&mut self) -> Result<vp::TscDeadline, Self::Error> {
543+
self.get_register_state()
544+
}
545+
546+
fn set_tsc_deadline(&mut self, value: &vp::TscDeadline) -> Result<(), Self::Error> {
547+
self.set_register_state(value)
548+
}
549+
542550
fn cet(&mut self) -> Result<vp::Cet, Self::Error> {
543551
self.get_register_state()
544552
}

‎vmm_core/virt_mshv/src/x86_64/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ impl MshvProtoPartition<'_> {
410410
cet_ss: false,
411411
sgx: false,
412412
tsc_aux: false,
413+
tsc_deadline: false,
413414
vtom: None,
414415
physical_address_width: self
415416
.vmfd

0 commit comments

Comments
 (0)