From: Paolo Bonzini Date: Thu, 20 Nov 2025 16:23:29 +0000 (+0100) Subject: rust: migration: implement ToMigrationState for Timer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9947bf93515a7818091f02545a4ab370b1cc0d16;p=thirdparty%2Fqemu.git rust: migration: implement ToMigrationState for Timer Timer is a complex struct, allow adding it to a struct that uses #[derive(ToMigrationState)]; similar to vmstate_timer, only the expiration time has to be preserved. In fact, because it is thread-safe, ToMigrationStateShared can also be implemented without needing a cell or mutex that wraps the timer. Signed-off-by: Paolo Bonzini --- diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs index 41d5c8fea0..375bea4c96 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -250,7 +250,6 @@ impl HPETTimerRegisters { } /// HPET Timer Abstraction -#[repr(C)] #[derive(Debug)] pub struct HPETTimer { /// timer N index within the timer block (`HPETState`) diff --git a/rust/migration/src/migratable.rs b/rust/migration/src/migratable.rs index 02efe31d72..7748aac2f2 100644 --- a/rust/migration/src/migratable.rs +++ b/rust/migration/src/migratable.rs @@ -140,6 +140,26 @@ macro_rules! impl_for_primitive { impl_for_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, bool); +impl ToMigrationState for util::timer::Timer { + type Migrated = i64; + + fn snapshot_migration_state(&self, target: &mut i64) -> Result<(), InvalidError> { + // SAFETY: as_ptr() is unsafe to ensure that the caller reasons about + // the pinning of the data inside the Opaque<>. Here all we do is + // access a field. + *target = self.expire_time_ns().unwrap_or(-1); + Ok(()) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + self.restore_migrated_state(source, version_id) + } +} + impl ToMigrationState for [T; N] where [T::Migrated; N]: Default, @@ -237,6 +257,17 @@ pub trait ToMigrationStateShared: ToMigrationState { ) -> Result<(), InvalidError>; } +impl ToMigrationStateShared for util::timer::Timer { + fn restore_migrated_state(&self, source: i64, _version_id: u8) -> Result<(), InvalidError> { + if source >= 0 { + self.modify_ns(source as u64); + } else { + self.delete(); + } + Ok(()) + } +} + impl ToMigrationStateShared for [T; N] where [T::Migrated; N]: Default,