From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com
Subject: [PATCH 5/5] rust/hpet: Apply Migratable<> wrapper and ToMigrationState
Date: Mon, 17 Nov 2025 09:47:52 +0100 [thread overview]
Message-ID: <20251117084752.203219-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20251117084752.203219-1-pbonzini@redhat.com>
From: Zhao Liu <zhao1.liu@intel.com>
Before using Mutex<> to protect HPETRegisters, it's necessary to apply
Migratable<> wrapper and ToMigrationState first since there's no
pre-defined VMState for Mutex<>.
In addition, this allows to move data from HPETTimerRegisters to
HPETTimer, so as to preserve the original migration format of the C
implementation. To do that, HPETTimer is wrapped with Migratable<>
as well but the implementation of ToMigrationStateShared is
hand-written.
Note that even though the HPETRegistersMigration struct is
generated by ToMigrationState macro, its VMState still needs to be
implemented by hand.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-21-zhao1.liu@intel.com
[Added HPETTimer implementation and restored compatible migration format. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/timer/hpet/src/device.rs | 139 +++++++++++++++++++++++--------
1 file changed, 102 insertions(+), 37 deletions(-)
diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 373ec37bbd3..fde4469ec16 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -13,7 +13,7 @@
use bql::prelude::*;
use common::prelude::*;
use hwcore::prelude::*;
-use migration::{self, prelude::*};
+use migration::{self, prelude::*, ToMigrationStateShared};
use qom::prelude::*;
use system::{
bindings::{address_space_memory, address_space_stl_le},
@@ -176,7 +176,6 @@ fn timer_handler(t: &HPETTimer) {
t.callback(regs)
}
-#[repr(C)]
#[derive(Debug, Default)]
pub struct HPETTimerRegisters {
// Memory-mapped, software visible timer registers
@@ -650,11 +649,13 @@ fn write(
}
}
-#[repr(C)]
-#[derive(Default)]
+#[derive(Default, ToMigrationState)]
pub struct HPETRegisters {
// HPET block Registers: Memory-mapped, software visible registers
/// General Capabilities and ID Register
+ ///
+ /// Constant and therefore not migrated.
+ #[migration_state(omit)]
capability: u64,
/// General Configuration Register
config: u64,
@@ -666,9 +667,15 @@ pub struct HPETRegisters {
counter: u64,
/// HPET Timer N Registers
+ ///
+ /// Migrated as part of `Migratable<HPETTimer>`
+ #[migration_state(omit)]
tn_regs: [HPETTimerRegisters; HPET_MAX_TIMERS],
/// Offset of main counter relative to qemu clock.
+ ///
+ /// Migrated as a subsection and therefore snapshotted into [`HPETState`]
+ #[migration_state(omit)]
pub hpet_offset: u64,
}
@@ -702,7 +709,7 @@ fn is_timer_int_active(&self, index: usize) -> bool {
pub struct HPETState {
parent_obj: ParentField<SysBusDevice>,
iomem: MemoryRegion,
- regs: BqlRefCell<HPETRegisters>,
+ regs: Migratable<BqlRefCell<HPETRegisters>>,
// Internal state
/// Capabilities that QEMU HPET supports.
@@ -728,7 +735,7 @@ pub struct HPETState {
/// HPET timer array managed by this timer block.
#[doc(alias = "timer")]
- timers: [HPETTimer; HPET_MAX_TIMERS],
+ timers: [Migratable<HPETTimer>; HPET_MAX_TIMERS],
#[property(rename = "timers", default = HPET_MIN_TIMERS)]
num_timers: usize,
num_timers_save: BqlCell<u8>,
@@ -763,9 +770,12 @@ fn init_timers(this: &mut MaybeUninit<Self>) {
// Initialize in two steps, to avoid calling Timer::init_full on a
// temporary that can be moved.
- let timer = timer.write(HPETTimer::new(index.try_into().unwrap(), state));
+ let timer = timer.write(Migratable::new(HPETTimer::new(
+ index.try_into().unwrap(),
+ state,
+ )));
// SAFETY: HPETState is pinned
- let timer = unsafe { Pin::new_unchecked(timer) };
+ let timer = unsafe { Pin::new_unchecked(&mut **timer) };
HPETTimer::init_timer(timer);
}
}
@@ -1109,47 +1119,102 @@ impl ObjectImpl for HPETState {
})
.build();
-impl_vmstate_struct!(
- HPETTimerRegisters,
- VMStateDescriptionBuilder::<HPETTimerRegisters>::new()
- .name(c"hpet_timer/regs")
+#[derive(Default)]
+pub struct HPETTimerMigration {
+ index: u8,
+ config: u64,
+ cmp: u64,
+ fsb: u64,
+ period: u64,
+ wrap_flag: u8,
+ qemu_timer: i64,
+}
+
+impl ToMigrationState for HPETTimer {
+ type Migrated = HPETTimerMigration;
+
+ fn snapshot_migration_state(
+ &self,
+ target: &mut Self::Migrated,
+ ) -> Result<(), migration::Infallible> {
+ let state = self.get_state();
+ let regs = state.regs.borrow_mut();
+ let tn_regs = ®s.tn_regs[self.index as usize];
+
+ target.index = self.index;
+ target.config = tn_regs.config;
+ target.cmp = tn_regs.cmp;
+ target.fsb = tn_regs.fsb;
+ target.period = tn_regs.period;
+ target.wrap_flag = tn_regs.wrap_flag;
+ self.qemu_timer
+ .snapshot_migration_state(&mut target.qemu_timer)?;
+
+ Ok(())
+ }
+
+ fn restore_migrated_state_mut(
+ &mut self,
+ source: Self::Migrated,
+ version_id: u8,
+ ) -> Result<(), migration::Infallible> {
+ self.restore_migrated_state(source, version_id)
+ }
+}
+
+impl ToMigrationStateShared for HPETTimer {
+ fn restore_migrated_state(
+ &self,
+ source: Self::Migrated,
+ version_id: u8,
+ ) -> Result<(), migration::Infallible> {
+ let state = self.get_state();
+ let mut regs = state.regs.borrow_mut();
+ let tn_regs = &mut regs.tn_regs[self.index as usize];
+
+ tn_regs.config = source.config;
+ tn_regs.cmp = source.cmp;
+ tn_regs.fsb = source.fsb;
+ tn_regs.period = source.period;
+ tn_regs.wrap_flag = source.wrap_flag;
+ self.qemu_timer
+ .restore_migrated_state(source.qemu_timer, version_id)?;
+
+ Ok(())
+ }
+}
+
+const VMSTATE_HPET_TIMER: VMStateDescription<HPETTimerMigration> =
+ VMStateDescriptionBuilder::<HPETTimerMigration>::new()
+ .name(c"hpet_timer")
.version_id(1)
.minimum_version_id(1)
.fields(vmstate_fields! {
- vmstate_of!(HPETTimerRegisters, config),
- vmstate_of!(HPETTimerRegisters, cmp),
- vmstate_of!(HPETTimerRegisters, fsb),
- vmstate_of!(HPETTimerRegisters, period),
- vmstate_of!(HPETTimerRegisters, wrap_flag),
- })
- .build()
-);
-
-const VMSTATE_HPET_TIMER: VMStateDescription<HPETTimer> =
- VMStateDescriptionBuilder::<HPETTimer>::new()
- .name(c"hpet_timer")
- .version_id(2)
- .minimum_version_id(2)
- .fields(vmstate_fields! {
- vmstate_of!(HPETTimer, qemu_timer),
+ vmstate_of!(HPETTimerMigration, index),
+ vmstate_of!(HPETTimerMigration, config),
+ vmstate_of!(HPETTimerMigration, cmp),
+ vmstate_of!(HPETTimerMigration, fsb),
+ vmstate_of!(HPETTimerMigration, period),
+ vmstate_of!(HPETTimerMigration, wrap_flag),
+ vmstate_of!(HPETTimerMigration, qemu_timer),
})
.build();
-impl_vmstate_struct!(HPETTimer, VMSTATE_HPET_TIMER);
+impl_vmstate_struct!(HPETTimerMigration, VMSTATE_HPET_TIMER);
const VALIDATE_TIMERS_NAME: &CStr = c"num_timers must match";
+// HPETRegistersMigration is generated by ToMigrationState macro.
impl_vmstate_struct!(
- HPETRegisters,
- VMStateDescriptionBuilder::<HPETRegisters>::new()
+ HPETRegistersMigration,
+ VMStateDescriptionBuilder::<HPETRegistersMigration>::new()
.name(c"hpet/regs")
.version_id(2)
.minimum_version_id(2)
.fields(vmstate_fields! {
- vmstate_of!(HPETRegisters, config),
- vmstate_of!(HPETRegisters, int_status),
- vmstate_of!(HPETRegisters, counter),
- vmstate_of!(HPETRegisters, tn_regs),
+ vmstate_of!(HPETRegistersMigration, config),
+ vmstate_of!(HPETRegistersMigration, int_status),
+ vmstate_of!(HPETRegistersMigration, counter),
})
.build()
);
@@ -1157,8 +1222,8 @@ impl ObjectImpl for HPETState {
const VMSTATE_HPET: VMStateDescription<HPETState> =
VMStateDescriptionBuilder::<HPETState>::new()
.name(c"hpet")
- .version_id(3)
- .minimum_version_id(3)
+ .version_id(2)
+ .minimum_version_id(2)
.pre_save(&HPETState::pre_save)
.post_load(&HPETState::post_load)
.fields(vmstate_fields! {
--
2.51.1
next prev parent reply other threads:[~2025-11-17 8:48 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 8:47 [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Paolo Bonzini
2025-11-17 8:47 ` [PATCH 1/5] rust/hpet: move hidden registers to HPETTimerRegisters Paolo Bonzini
2025-11-18 8:35 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 2/5] rust/hpet: move hpet_offset to HPETRegisters Paolo Bonzini
2025-11-18 13:54 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 3/5] rust/hpet: remove BqlRefCell around HPETTimer Paolo Bonzini
2025-11-19 15:17 ` Zhao Liu
2025-11-19 22:28 ` Paolo Bonzini
2025-11-17 8:47 ` [PATCH 4/5] rust: migration: implement ToMigrationState for Timer Paolo Bonzini
2025-11-20 14:31 ` Zhao Liu
2025-11-17 8:47 ` Paolo Bonzini [this message]
2025-11-19 15:31 ` [PATCH 5/5] rust/hpet: Apply Migratable<> wrapper and ToMigrationState Zhao Liu
2025-11-19 15:59 ` [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Zhao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251117084752.203219-6-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).