* rust: fix nightly warnings

* target/i386: a smattering of fixes
 * monitor: add "info accelerators"
 * kvm: cleanups to kvm_cpu_synchronize_put()
 * target/i386: Add TSA attack variants and verw-clear feature flag
 * async: tsan bottom half fixes
 * rust: migration state wrappers with support for BQL-free devices
 -----BEGIN PGP SIGNATURE-----
 
 iQFIBAABCgAyFiEE8TM4V0tmI4mGbHaCv/vSX3jHroMFAmjuRZYUHHBib256aW5p
 QHJlZGhhdC5jb20ACgkQv/vSX3jHroPTFgf+LRXCvGJwrlJwD4cAS/TBzhzpOAMZ
 v75RZ/s2tF7nYRhT28MDtZWsXeVrjO/nrSXaThxe6WHfmKK2W+16a+BgfhbeTEGt
 wBnK3JMb84i7T2Foy91jVCc4k0igwZu6Wmnf3rOP9gpdjAK6FYLje1KWvF7FrJO1
 ackAzJJ+TiZmc5QpXLW8sjaIidmefveXsdHwMVRz67LDvlDANEhp4rixjTVmKe0Z
 UL3tzrEj/b15vvElkh3a1IrVAttexay425J94R5i3Xpz3fEBqmIdpJt4eiCt9j0L
 zL7TOXwSJWiOX+mec6aJwYh8y4ikD6Yq4f4Hc9xFBEZRcICaxx4uoOscYA==
 =FroL
 -----END PGP SIGNATURE-----

Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging

* rust: fix nightly warnings
* target/i386: a smattering of fixes
* monitor: add "info accelerators"
* kvm: cleanups to kvm_cpu_synchronize_put()
* target/i386: Add TSA attack variants and verw-clear feature flag
* async: tsan bottom half fixes
* rust: migration state wrappers with support for BQL-free devices

# -----BEGIN PGP SIGNATURE-----
#
# iQFIBAABCgAyFiEE8TM4V0tmI4mGbHaCv/vSX3jHroMFAmjuRZYUHHBib256aW5p
# QHJlZGhhdC5jb20ACgkQv/vSX3jHroPTFgf+LRXCvGJwrlJwD4cAS/TBzhzpOAMZ
# v75RZ/s2tF7nYRhT28MDtZWsXeVrjO/nrSXaThxe6WHfmKK2W+16a+BgfhbeTEGt
# wBnK3JMb84i7T2Foy91jVCc4k0igwZu6Wmnf3rOP9gpdjAK6FYLje1KWvF7FrJO1
# ackAzJJ+TiZmc5QpXLW8sjaIidmefveXsdHwMVRz67LDvlDANEhp4rixjTVmKe0Z
# UL3tzrEj/b15vvElkh3a1IrVAttexay425J94R5i3Xpz3fEBqmIdpJt4eiCt9j0L
# zL7TOXwSJWiOX+mec6aJwYh8y4ikD6Yq4f4Hc9xFBEZRcICaxx4uoOscYA==
# =FroL
# -----END PGP SIGNATURE-----
# gpg: Signature made Tue 14 Oct 2025 05:44:06 AM PDT
# gpg:                using RSA key F13338574B662389866C7682BFFBD25F78C7AE83
# gpg:                issuer "pbonzini@redhat.com"
# gpg: Good signature from "Paolo Bonzini <bonzini@gnu.org>" [unknown]
# gpg:                 aka "Paolo Bonzini <pbonzini@redhat.com>" [unknown]
# gpg: WARNING: The key's User ID is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: 46F5 9FBD 57D6 12E7 BFD4  E2F7 7E15 100C CD36 69B1
#      Subkey fingerprint: F133 3857 4B66 2389 866C  7682 BFFB D25F 78C7 AE83

* tag 'for-upstream' of https://gitlab.com/bonzini/qemu: (28 commits)
  rust: migration: implement ToMigrationState as part of impl_vmstate_bitsized
  timer: constify some functions
  rust: qemu-macros: add ToMigrationState derive macro
  rust: migration: add high-level migration wrappers
  rust: move VMState from bql to migration
  rust: migration: extract vmstate_fields_ref
  rust: migration: validate termination of subsection arrays
  rust: migration: do not store raw pointers into VMStateSubsectionsWrapper
  rust: migration: do not pass raw pointer to VMStateDescription::fields
  rust: bql: add BqlRefCell::get_mut()
  accel/kvm: Factor kvm_cpu_synchronize_put() out
  accel/kvm: Introduce KvmPutState enum
  monitor: generalize query-mshv/"info mshv" to query-accelerators/"info accelerators"
  monitor: clarify "info accel" help message
  target/i386: user: do not set up a valid LDT on reset
  async: access bottom half flags with qatomic_read
  target/i386: fix access to the T bit of the TSS
  target/i386: fix x86_64 pushw op
  i386/tcg/smm_helper: Properly apply DR values on SMM entry / exit
  i386/cpu: Prevent delivering SIPI during SMM in TCG mode
  ...

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2025-10-14 07:30:08 -07:00
commit b2d86f1c54
42 changed files with 1230 additions and 149 deletions

View file

@ -2937,22 +2937,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
}
}
static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
static bool kvm_cpu_synchronize_put(CPUState *cpu, KvmPutState state,
const char *desc)
{
Error *err = NULL;
int ret = kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE, &err);
int ret = kvm_arch_put_registers(cpu, state, &err);
if (ret) {
if (err) {
error_reportf_err(err, "Restoring resisters after reset: ");
error_reportf_err(err, "Restoring resisters %s: ", desc);
} else {
error_report("Failed to put registers after reset: %s",
error_report("Failed to put registers %s: %s", desc,
strerror(-ret));
}
cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
vm_stop(RUN_STATE_INTERNAL_ERROR);
return false;
}
cpu->vcpu_dirty = false;
return true;
}
static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
{
if (!kvm_cpu_synchronize_put(cpu, KVM_PUT_RESET_STATE, "after reset")) {
cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
vm_stop(RUN_STATE_INTERNAL_ERROR);
}
}
void kvm_cpu_synchronize_post_reset(CPUState *cpu)
@ -2966,19 +2976,9 @@ void kvm_cpu_synchronize_post_reset(CPUState *cpu)
static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
{
Error *err = NULL;
int ret = kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE, &err);
if (ret) {
if (err) {
error_reportf_err(err, "Putting registers after init: ");
} else {
error_report("Failed to put registers after init: %s",
strerror(-ret));
}
if (!kvm_cpu_synchronize_put(cpu, KVM_PUT_FULL_STATE, "after init")) {
exit(1);
}
cpu->vcpu_dirty = false;
}
void kvm_cpu_synchronize_post_init(CPUState *cpu)
@ -3168,20 +3168,11 @@ int kvm_cpu_exec(CPUState *cpu)
MemTxAttrs attrs;
if (cpu->vcpu_dirty) {
Error *err = NULL;
ret = kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE, &err);
if (ret) {
if (err) {
error_reportf_err(err, "Putting registers after init: ");
} else {
error_report("Failed to put registers after init: %s",
strerror(-ret));
}
if (!kvm_cpu_synchronize_put(cpu, KVM_PUT_RUNTIME_STATE,
"at runtime")) {
ret = -1;
break;
}
cpu->vcpu_dirty = false;
}
kvm_arch_pre_run(cpu, run);

View file

@ -155,6 +155,7 @@ module status
``hwcore::irq`` complete
``hwcore::qdev`` stable
``hwcore::sysbus`` stable
``migration::migratable`` proof of concept
``migration::vmstate`` stable
``qom`` stable
``system::memory`` stable

View file

@ -271,12 +271,12 @@ ERST
.name = "accel",
.args_type = "",
.params = "",
.help = "show accelerator info",
.help = "show accelerator statistics",
},
SRST
``info accel``
Show accelerator info.
Show accelerator statistics.
ERST
SRST
@ -308,16 +308,21 @@ SRST
ERST
{
.name = "mshv",
.name = "accelerators",
.args_type = "",
.params = "",
.help = "show MSHV information",
.cmd = hmp_info_mshv,
.help = "show present and enabled information",
.cmd = hmp_info_accelerators,
},
SRST
``info mshv``
Show MSHV information.
``info accelerators``
Show which accelerators are compiled into a QEMU binary, and what accelerator
is in use. For example::
kvm qtest [tcg]
indicates that TCG in use, and that KVM and qtest are also available.
ERST
{

View file

@ -163,19 +163,22 @@ void hmp_info_kvm(Monitor *mon, const QDict *qdict)
qapi_free_KvmInfo(info);
}
void hmp_info_mshv(Monitor *mon, const QDict *qdict)
void hmp_info_accelerators(Monitor *mon, const QDict *qdict)
{
MshvInfo *info;
AcceleratorInfo *info;
AcceleratorList *accel;
info = qmp_query_mshv(NULL);
monitor_printf(mon, "mshv support: ");
if (info->present) {
monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
} else {
monitor_printf(mon, "not compiled\n");
info = qmp_query_accelerators(NULL);
for (accel = info->present; accel; accel = accel->next) {
char trail = accel->next ? ' ' : '\n';
if (info->enabled == accel->value) {
monitor_printf(mon, "[%s]%c", Accelerator_str(accel->value), trail);
} else {
monitor_printf(mon, "%s%c", Accelerator_str(accel->value), trail);
}
}
qapi_free_MshvInfo(info);
qapi_free_AcceleratorInfo(info);
}
void hmp_info_uuid(Monitor *mon, const QDict *qdict)

View file

@ -31,15 +31,25 @@
#include <sys/stat.h>
/*
* QMP query for MSHV
* QMP query for enabled and present accelerators
*/
MshvInfo *qmp_query_mshv(Error **errp)
AcceleratorInfo *qmp_query_accelerators(Error **errp)
{
MshvInfo *info = g_malloc0(sizeof(*info));
AcceleratorInfo *info = g_malloc0(sizeof(*info));
AccelClass *current_class = ACCEL_GET_CLASS(current_accel());
int i;
info->enabled = mshv_enabled();
info->present = accel_find("mshv");
for (i = ACCELERATOR__MAX; i-- > 0; ) {
const char *s = Accelerator_str(i);
AccelClass *this_class = accel_find(s);
if (this_class) {
QAPI_LIST_PREPEND(info->present, i);
if (this_class == current_class) {
info->enabled = i;
}
}
}
return info;
}

View file

@ -646,8 +646,6 @@ void apic_sipi(DeviceState *dev)
{
APICCommonState *s = APIC(dev);
cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
if (!s->wait_for_sipi)
return;
cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);

View file

@ -24,7 +24,7 @@ strList *hmp_split_at_comma(const char *str);
void hmp_info_name(Monitor *mon, const QDict *qdict);
void hmp_info_version(Monitor *mon, const QDict *qdict);
void hmp_info_kvm(Monitor *mon, const QDict *qdict);
void hmp_info_mshv(Monitor *mon, const QDict *qdict);
void hmp_info_accelerators(Monitor *mon, const QDict *qdict);
void hmp_info_status(Monitor *mon, const QDict *qdict);
void hmp_info_uuid(Monitor *mon, const QDict *qdict);
void hmp_info_chardev(Monitor *mon, const QDict *qdict);

View file

@ -699,7 +699,7 @@ void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
*
* Returns: true if the timer is pending
*/
bool timer_pending(QEMUTimer *ts);
bool timer_pending(const QEMUTimer *ts);
/**
* timer_expired:
@ -710,7 +710,7 @@ bool timer_pending(QEMUTimer *ts);
*
* Returns: true if the timer has expired
*/
bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
bool timer_expired(const QEMUTimer *timer_head, int64_t current_time);
/**
* timer_expire_time_ns:
@ -720,7 +720,7 @@ bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
*
* Returns: the expiry time in nanoseconds
*/
uint64_t timer_expire_time_ns(QEMUTimer *ts);
uint64_t timer_expire_time_ns(const QEMUTimer *ts);
/**
* timer_get:

View file

@ -340,14 +340,16 @@ int kvm_arch_process_async_events(CPUState *cpu);
int kvm_arch_get_registers(CPUState *cpu, Error **errp);
/* state subset only touched by the VCPU itself during runtime */
#define KVM_PUT_RUNTIME_STATE 1
/* state subset modified during VCPU reset */
#define KVM_PUT_RESET_STATE 2
/* full state set, modified during initialization or on vmload */
#define KVM_PUT_FULL_STATE 3
typedef enum kvm_put_state {
/* state subset only touched by the VCPU itself during runtime */
KVM_PUT_RUNTIME_STATE = 1,
/* state subset modified during VCPU reset */
KVM_PUT_RESET_STATE = 2,
/* full state set, modified during initialization or on vmload */
KVM_PUT_FULL_STATE = 3,
} KvmPutState;
int kvm_arch_put_registers(CPUState *cpu, int level, Error **errp);
int kvm_arch_put_registers(CPUState *cpu, KvmPutState level, Error **errp);
int kvm_arch_get_default_type(MachineState *ms);

View file

@ -56,30 +56,55 @@
'features': [ 'unstable' ] }
##
# @MshvInfo:
# @Accelerator:
#
# Information about support for MSHV acceleration
#
# @enabled: true if MSHV acceleration is active
# @hvf: Apple Hypervisor.framework
#
# @present: true if MSHV acceleration is built into this executable
# @kvm: KVM
#
# @mshv: Hyper-V
#
# @nvmm: NetBSD NVMM
#
# @qtest: QTest (dummy accelerator)
#
# @tcg: TCG (dynamic translation)
#
# @whpx: Windows Hypervisor Platform
#
# @xen: Xen
#
# Since: 10.2.0
##
{ 'struct': 'MshvInfo', 'data': {'enabled': 'bool', 'present': 'bool'} }
{ 'enum': 'Accelerator', 'data': ['hvf', 'kvm', 'mshv', 'nvmm', 'qtest', 'tcg', 'whpx', 'xen'] }
##
# @query-mshv:
# @AcceleratorInfo:
#
# Return information about MSHV acceleration
# Information about support for various accelerators
#
# Returns: @MshvInfo
# @enabled: the accelerator that is in use
#
# Since: 10.0.92
# @present: the list of accelerators that are built into this executable
#
# Since: 10.2.0
##
{ 'struct': 'AcceleratorInfo', 'data': {'enabled': 'Accelerator', 'present': ['Accelerator']} }
##
# @query-accelerators:
#
# Return information about accelerators
#
# Returns: @AcceleratorInfo
#
# Since: 10.2.0
#
# .. qmp-example::
#
# -> { "execute": "query-mshv" }
# <- { "return": { "enabled": true, "present": true } }
# -> { "execute": "query-accelerators" }
# <- { "return": { "enabled": "mshv", "present": ["kvm", "mshv", "qtest", "tcg"] } }
##
{ 'command': 'query-mshv', 'returns': 'MshvInfo' }
{ 'command': 'query-accelerators', 'returns': 'AcceleratorInfo' }

3
rust/Cargo.lock generated
View file

@ -59,7 +59,6 @@ name = "bql"
version = "0.1.0"
dependencies = [
"glib-sys",
"migration",
]
[[package]]
@ -198,8 +197,10 @@ checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
name = "migration"
version = "0.1.0"
dependencies = [
"bql",
"common",
"glib-sys",
"qemu_macros",
"util",
]

View file

@ -13,7 +13,6 @@ repository.workspace = true
rust-version.workspace = true
[dependencies]
migration = { path = "../migration" }
glib-sys.workspace = true
[features]

View file

@ -37,7 +37,6 @@ _bql_rs = static_library(
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_abi: 'rust',
rust_args: _bql_cfg,
link_with: [_migration_rs],
dependencies: [glib_sys_rs],
)

View file

@ -151,8 +151,6 @@ use std::{
ptr::NonNull,
};
use migration::impl_vmstate_transparent;
/// A mutable memory location that is protected by the Big QEMU Lock.
///
/// # Memory layout
@ -364,8 +362,6 @@ impl<T: Default> BqlCell<T> {
}
}
impl_vmstate_transparent!(crate::cell::BqlCell<T> where T: VMState);
/// A mutable memory location with dynamically checked borrow rules,
/// protected by the Big QEMU Lock.
///
@ -580,6 +576,23 @@ impl<T> BqlRefCell<T> {
}
}
/// Returns a mutable reference to the underlying data in this cell,
/// while the owner already has a mutable reference to the cell.
///
/// # Examples
///
/// ```
/// use bql::BqlRefCell;
///
/// let mut c = BqlRefCell::new(5);
///
/// *c.get_mut() = 10;
/// ```
#[inline]
pub const fn get_mut(&mut self) -> &mut T {
self.value.get_mut()
}
/// Returns a raw pointer to the underlying data in this cell.
///
/// # Examples
@ -674,8 +687,6 @@ impl<T> From<T> for BqlRefCell<T> {
}
}
impl_vmstate_transparent!(crate::cell::BqlRefCell<T> where T: VMState);
struct BorrowRef<'b> {
borrow: &'b Cell<BorrowFlag>,
}

View file

@ -255,6 +255,7 @@ pub enum Mode {
#[bitsize(2)]
#[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)]
#[allow(clippy::enum_variant_names)]
/// `WLEN` Word length, field of [Line Control register](LineControl).
///
/// These bits indicate the number of data bits transmitted or received in a

View file

@ -40,7 +40,7 @@ impl HPETFwConfig {
assert!(bql::is_locked());
// SAFETY: all accesses go through these methods, which guarantee
// that the accesses are protected by the BQL.
let mut fw_cfg = unsafe { *addr_of_mut!(hpet_fw_cfg) };
let fw_cfg = unsafe { &mut *addr_of_mut!(hpet_fw_cfg) };
if fw_cfg.count == u8::MAX {
// first instance
@ -60,7 +60,7 @@ impl HPETFwConfig {
assert!(bql::is_locked());
// SAFETY: all accesses go through these methods, which guarantee
// that the accesses are protected by the BQL.
let mut fw_cfg = unsafe { *addr_of_mut!(hpet_fw_cfg) };
let fw_cfg = unsafe { &mut *addr_of_mut!(hpet_fw_cfg) };
fw_cfg.hpet[hpet_id].event_timer_block_id = timer_block_id;
fw_cfg.hpet[hpet_id].address = address;

View file

@ -29,8 +29,8 @@ subdir('qemu-macros')
subdir('common')
subdir('bits')
subdir('util')
subdir('migration')
subdir('bql')
subdir('migration')
subdir('qom')
subdir('system')
subdir('chardev')

View file

@ -13,7 +13,9 @@ repository.workspace = true
rust-version.workspace = true
[dependencies]
bql = { path = "../bql" }
common = { path = "../common" }
qemu_macros = { path = "../qemu-macros" }
util = { path = "../util" }
glib-sys.workspace = true

View file

@ -31,18 +31,19 @@ _migration_rs = static_library(
[
'src/lib.rs',
'src/bindings.rs',
'src/migratable.rs',
'src/vmstate.rs',
],
{'.' : _migration_bindings_inc_rs},
),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_abi: 'rust',
link_with: [_util_rs],
dependencies: [common_rs, glib_sys_rs],
link_with: [_util_rs, _bql_rs],
dependencies: [common_rs, glib_sys_rs, qemu_macros],
)
migration_rs = declare_dependency(link_with: [_migration_rs],
dependencies: [migration, qemuutil])
dependencies: [bql_rs, migration, qemuutil])
# Doctests are essentially integration tests, so they need the same dependencies.
# Note that running them requires the object files for C code, so place them

View file

@ -2,5 +2,10 @@
pub mod bindings;
pub use qemu_macros::ToMigrationState;
pub mod migratable;
pub use migratable::*;
pub mod vmstate;
pub use vmstate::*;

View file

@ -0,0 +1,442 @@
// Copyright 2025 Red Hat, Inc.
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
// SPDX-License-Identifier: GPL-2.0-or-later
use std::{
fmt,
mem::size_of,
ptr::{self, addr_of, NonNull},
sync::{Arc, Mutex},
};
use bql::{BqlCell, BqlRefCell};
use common::Zeroable;
use crate::{
bindings, vmstate_fields_ref, vmstate_of, InvalidError, VMState, VMStateDescriptionBuilder,
};
/// Enables QEMU migration support even when a type is wrapped with
/// synchronization primitives (like `Mutex`) that the C migration
/// code cannot directly handle. The trait provides methods to
/// extract essential state for migration and restore it after
/// migration completes.
///
/// On top of extracting data from synchronization wrappers during save
/// and restoring it during load, it's also possible to use `ToMigrationState`
/// to convert runtime representations to migration-safe formats.
///
/// # Examples
///
/// ```
/// use bql::BqlCell;
/// use migration::{InvalidError, ToMigrationState, VMState};
/// # use migration::VMStateField;
///
/// # #[derive(Debug, PartialEq, Eq)]
/// struct DeviceState {
/// counter: BqlCell<u32>,
/// enabled: bool,
/// }
///
/// # #[derive(Debug)]
/// #[derive(Default)]
/// struct DeviceMigrationState {
/// counter: u32,
/// enabled: bool,
/// }
///
/// # unsafe impl VMState for DeviceMigrationState {
/// # const BASE: VMStateField = ::common::Zeroable::ZERO;
/// # }
/// impl ToMigrationState for DeviceState {
/// type Migrated = DeviceMigrationState;
///
/// fn snapshot_migration_state(
/// &self,
/// target: &mut Self::Migrated,
/// ) -> Result<(), InvalidError> {
/// target.counter = self.counter.get();
/// target.enabled = self.enabled;
/// Ok(())
/// }
///
/// fn restore_migrated_state_mut(
/// &mut self,
/// source: Self::Migrated,
/// _version_id: u8,
/// ) -> Result<(), InvalidError> {
/// self.counter.set(source.counter);
/// self.enabled = source.enabled;
/// Ok(())
/// }
/// }
/// # bql::start_test();
/// # let dev = DeviceState { counter: 10.into(), enabled: true };
/// # let mig = dev.to_migration_state().unwrap();
/// # assert!(matches!(*mig, DeviceMigrationState { counter: 10, enabled: true }));
/// # let mut dev2 = DeviceState { counter: 42.into(), enabled: false };
/// # dev2.restore_migrated_state_mut(*mig, 1).unwrap();
/// # assert_eq!(dev2, dev);
/// ```
///
/// More commonly, the trait is derived through the
/// [`derive(ToMigrationState)`](qemu_macros::ToMigrationState) procedural
/// macro.
pub trait ToMigrationState {
/// The type used to represent the migrated state.
type Migrated: Default + VMState;
/// Capture the current state into a migration-safe format, failing
/// if the state cannot be migrated.
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), InvalidError>;
/// Restores state from a migrated representation, failing if the
/// state cannot be restored.
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError>;
/// Convenience method to combine allocation and state capture
/// into a single operation.
fn to_migration_state(&self) -> Result<Box<Self::Migrated>, InvalidError> {
let mut migrated = Box::<Self::Migrated>::default();
self.snapshot_migration_state(&mut migrated)?;
Ok(migrated)
}
}
// Implementations for primitive types. Do not use a blanket implementation
// for all Copy types, because [T; N] is Copy if T is Copy; that would conflict
// with the below implementation for arrays.
macro_rules! impl_for_primitive {
($($t:ty),*) => {
$(
impl ToMigrationState for $t {
type Migrated = Self;
fn snapshot_migration_state(
&self,
target: &mut Self::Migrated,
) -> Result<(), InvalidError> {
*target = *self;
Ok(())
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
_version_id: u8,
) -> Result<(), InvalidError> {
*self = source;
Ok(())
}
}
)*
};
}
impl_for_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, bool);
impl<T: ToMigrationState, const N: usize> ToMigrationState for [T; N]
where
[T::Migrated; N]: Default,
{
type Migrated = [T::Migrated; N];
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), InvalidError> {
for (item, target_item) in self.iter().zip(target.iter_mut()) {
item.snapshot_migration_state(target_item)?;
}
Ok(())
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
for (item, source_item) in self.iter_mut().zip(source) {
item.restore_migrated_state_mut(source_item, version_id)?;
}
Ok(())
}
}
impl<T: ToMigrationState> ToMigrationState for Mutex<T> {
type Migrated = T::Migrated;
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), InvalidError> {
self.lock().unwrap().snapshot_migration_state(target)
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
self.get_mut()
.unwrap()
.restore_migrated_state_mut(source, version_id)
}
}
impl<T: ToMigrationState> ToMigrationState for BqlRefCell<T> {
type Migrated = T::Migrated;
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), InvalidError> {
self.borrow().snapshot_migration_state(target)
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
self.get_mut()
.restore_migrated_state_mut(source, version_id)
}
}
/// Extension trait for types that support migration state restoration
/// through interior mutability.
///
/// This trait extends [`ToMigrationState`] for types that can restore
/// their state without requiring mutable access. While user structs
/// will generally use `ToMigrationState`, the device will have multiple
/// references and therefore the device struct has to employ an interior
/// mutability wrapper like [`Mutex`] or [`BqlRefCell`].
///
/// Anything that implements this trait can in turn be used within
/// [`Migratable<T>`], which makes no assumptions on how to achieve mutable
/// access to the runtime state.
///
/// # Examples
///
/// ```
/// use std::sync::Mutex;
///
/// use migration::ToMigrationStateShared;
///
/// let device_state = Mutex::new(42);
/// // Can restore without &mut access
/// device_state.restore_migrated_state(100, 1).unwrap();
/// assert_eq!(*device_state.lock().unwrap(), 100);
/// ```
pub trait ToMigrationStateShared: ToMigrationState {
/// Restores state from a migrated representation to an interior-mutable
/// object. Similar to `restore_migrated_state_mut`, but requires a
/// shared reference; therefore it can be used to restore a device's
/// state even though devices have multiple references to them.
fn restore_migrated_state(
&self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError>;
}
impl<T: ToMigrationStateShared, const N: usize> ToMigrationStateShared for [T; N]
where
[T::Migrated; N]: Default,
{
fn restore_migrated_state(
&self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
for (item, source_item) in self.iter().zip(source) {
item.restore_migrated_state(source_item, version_id)?;
}
Ok(())
}
}
// Arc requires the contained object to be interior-mutable
impl<T: ToMigrationStateShared> ToMigrationState for Arc<T> {
type Migrated = T::Migrated;
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), InvalidError> {
(**self).snapshot_migration_state(target)
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
(**self).restore_migrated_state(source, version_id)
}
}
impl<T: ToMigrationStateShared> ToMigrationStateShared for Arc<T> {
fn restore_migrated_state(
&self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
(**self).restore_migrated_state(source, version_id)
}
}
// Interior-mutable types. Note how they only require ToMigrationState for
// the inner type!
impl<T: ToMigrationState> ToMigrationStateShared for Mutex<T> {
fn restore_migrated_state(
&self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
self.lock()
.unwrap()
.restore_migrated_state_mut(source, version_id)
}
}
impl<T: ToMigrationState> ToMigrationStateShared for BqlRefCell<T> {
fn restore_migrated_state(
&self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
self.borrow_mut()
.restore_migrated_state_mut(source, version_id)
}
}
/// A wrapper that enables QEMU migration for types with shared state.
///
/// `Migratable<T>` provides a bridge between Rust types that use interior
/// mutability (like `Mutex<T>`) and QEMU's C-based migration infrastructure.
/// It manages the lifecycle of migration state and provides automatic
/// conversion between runtime and migration representations.
///
/// ```
/// # use std::sync::Mutex;
/// # use migration::{Migratable, ToMigrationState, VMState, VMStateField};
///
/// #[derive(ToMigrationState)]
/// pub struct DeviceRegs {
/// status: u32,
/// }
/// # unsafe impl VMState for DeviceRegsMigration {
/// # const BASE: VMStateField = ::common::Zeroable::ZERO;
/// # }
///
/// pub struct SomeDevice {
/// // ...
/// registers: Migratable<Mutex<DeviceRegs>>,
/// }
/// ```
#[repr(C)]
pub struct Migratable<T: ToMigrationStateShared> {
/// Pointer to migration state, valid only during migration operations.
/// C vmstate does not support NULL pointers, so no `Option<Box<>>`.
migration_state: BqlCell<*mut T::Migrated>,
/// The runtime state that can be accessed during normal operation
runtime_state: T,
}
impl<T: ToMigrationStateShared> std::ops::Deref for Migratable<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.runtime_state
}
}
impl<T: ToMigrationStateShared> std::ops::DerefMut for Migratable<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.runtime_state
}
}
impl<T: ToMigrationStateShared> Migratable<T> {
/// Creates a new `Migratable` wrapper around the given runtime state.
///
/// # Returns
/// A new `Migratable` instance ready for use and migration
pub fn new(runtime_state: T) -> Self {
Self {
migration_state: BqlCell::new(ptr::null_mut()),
runtime_state,
}
}
fn pre_save(&self) -> Result<(), InvalidError> {
let state = self.runtime_state.to_migration_state()?;
self.migration_state.set(Box::into_raw(state));
Ok(())
}
fn post_save(&self) -> Result<(), InvalidError> {
let state = unsafe { Box::from_raw(self.migration_state.replace(ptr::null_mut())) };
drop(state);
Ok(())
}
fn pre_load(&self) -> Result<(), InvalidError> {
self.migration_state
.set(Box::into_raw(Box::<T::Migrated>::default()));
Ok(())
}
fn post_load(&self, version_id: u8) -> Result<(), InvalidError> {
let state = unsafe { Box::from_raw(self.migration_state.replace(ptr::null_mut())) };
self.runtime_state
.restore_migrated_state(*state, version_id)
}
}
impl<T: ToMigrationStateShared + fmt::Debug> fmt::Debug for Migratable<T>
where
T::Migrated: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut struct_f = f.debug_struct("Migratable");
struct_f.field("runtime_state", &self.runtime_state);
let state = NonNull::new(self.migration_state.get()).map(|x| unsafe { x.as_ref() });
struct_f.field("migration_state", &state);
struct_f.finish()
}
}
impl<T: ToMigrationStateShared + Default> Default for Migratable<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: 'static + ToMigrationStateShared> Migratable<T> {
const FIELD: bindings::VMStateField = vmstate_of!(Self, migration_state);
const FIELDS: &[bindings::VMStateField] = vmstate_fields_ref! {
Migratable::<T>::FIELD
};
const VMSD: &'static bindings::VMStateDescription = VMStateDescriptionBuilder::<Self>::new()
.version_id(1)
.minimum_version_id(1)
.pre_save(&Self::pre_save)
.pre_load(&Self::pre_load)
.post_save(&Self::post_save)
.post_load(&Self::post_load)
.fields(Self::FIELDS)
.build()
.as_ref();
}
unsafe impl<T: 'static + ToMigrationStateShared> VMState for Migratable<T> {
const BASE: bindings::VMStateField = {
bindings::VMStateField {
vmsd: addr_of!(*Self::VMSD),
size: size_of::<Self>(),
flags: bindings::VMStateFlags::VMS_STRUCT,
..Zeroable::ZERO
}
};
}

View file

@ -72,6 +72,7 @@ macro_rules! call_func_with_field {
($func:expr, $typ:ty, $($field:tt).+) => {
$func(loop {
#![allow(unreachable_code)]
#![allow(unused_variables)]
const fn phantom__<T>(_: &T) -> ::core::marker::PhantomData<T> { ::core::marker::PhantomData }
// Unreachable code is exempt from checks on uninitialized values.
// Use that trick to infer the type of this PhantomData.
@ -275,6 +276,8 @@ macro_rules! impl_vmstate_transparent {
};
}
impl_vmstate_transparent!(bql::BqlCell<T> where T: VMState);
impl_vmstate_transparent!(bql::BqlRefCell<T> where T: VMState);
impl_vmstate_transparent!(std::cell::Cell<T> where T: VMState);
impl_vmstate_transparent!(std::cell::UnsafeCell<T> where T: VMState);
impl_vmstate_transparent!(std::pin::Pin<T> where T: VMState);
@ -293,6 +296,25 @@ macro_rules! impl_vmstate_bitsized {
as ::bilge::prelude::Number>::UnderlyingType
as $crate::vmstate::VMState>::VARRAY_FLAG;
}
impl $crate::migratable::ToMigrationState for $type {
type Migrated = <<$type as ::bilge::prelude::Bitsized>::ArbitraryInt
as ::bilge::prelude::Number>::UnderlyingType;
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), $crate::InvalidError> {
*target = Self::Migrated::from(*self);
Ok(())
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), $crate::InvalidError> {
*self = Self::from(source);
Ok(())
}
}
};
}
@ -411,20 +433,31 @@ macro_rules! vmstate_exist_fn {
}};
}
/// Add a terminator to the fields in the arguments, and return
/// a reference to the resulting array of values.
#[macro_export]
macro_rules! vmstate_fields_ref {
($($field:expr),*$(,)*) => {
&[
$($field),*,
$crate::bindings::VMStateField {
flags: $crate::bindings::VMStateFlags::VMS_END,
..::common::zeroable::Zeroable::ZERO
}
]
}
}
/// Helper macro to declare a list of
/// ([`VMStateField`](`crate::bindings::VMStateField`)) into a static and return
/// a pointer to the array of values it created.
#[macro_export]
macro_rules! vmstate_fields {
($($field:expr),*$(,)*) => {{
static _FIELDS: &[$crate::bindings::VMStateField] = &[
static _FIELDS: &[$crate::bindings::VMStateField] = $crate::vmstate_fields_ref!(
$($field),*,
$crate::bindings::VMStateField {
flags: $crate::bindings::VMStateFlags::VMS_END,
..::common::zeroable::Zeroable::ZERO
}
];
_FIELDS.as_ptr()
);
_FIELDS
}}
}
@ -469,33 +502,21 @@ macro_rules! impl_vmstate_struct {
};
}
/// A transparent wrapper type for the `subsections` field of
/// [`VMStateDescription`].
///
/// This is necessary to be able to declare subsection descriptions as statics,
/// because the only way to implement `Sync` for a foreign type (and `*const`
/// pointers are foreign types in Rust) is to create a wrapper struct and
/// `unsafe impl Sync` for it.
///
/// This struct is used in the
/// [`vm_state_subsections`](crate::vmstate_subsections) macro implementation.
#[repr(transparent)]
pub struct VMStateSubsectionsWrapper(pub &'static [*const crate::bindings::VMStateDescription]);
unsafe impl Sync for VMStateSubsectionsWrapper {}
/// The type returned by [`vmstate_subsections!`](crate::vmstate_subsections).
pub type VMStateSubsections = &'static [Option<&'static crate::bindings::VMStateDescription>];
/// Helper macro to declare a list of subsections ([`VMStateDescription`])
/// into a static and return a pointer to the array of pointers it created.
#[macro_export]
macro_rules! vmstate_subsections {
($($subsection:expr),*$(,)*) => {{
static _SUBSECTIONS: $crate::vmstate::VMStateSubsectionsWrapper = $crate::vmstate::VMStateSubsectionsWrapper(&[
static _SUBSECTIONS: $crate::vmstate::VMStateSubsections = &[
$({
static _SUBSECTION: $crate::bindings::VMStateDescription = $subsection.get();
::core::ptr::addr_of!(_SUBSECTION)
Some(&_SUBSECTION)
}),*,
::core::ptr::null()
]);
None,
];
&_SUBSECTIONS
}}
}
@ -676,14 +697,21 @@ impl<T> VMStateDescriptionBuilder<T> {
}
#[must_use]
pub const fn fields(mut self, fields: *const VMStateField) -> Self {
self.0.fields = fields;
pub const fn fields(mut self, fields: &'static [VMStateField]) -> Self {
if fields[fields.len() - 1].flags.0 != VMStateFlags::VMS_END.0 {
panic!("fields are not terminated, use vmstate_fields!");
}
self.0.fields = fields.as_ptr();
self
}
#[must_use]
pub const fn subsections(mut self, subs: &'static VMStateSubsectionsWrapper) -> Self {
self.0.subsections = subs.0.as_ptr();
pub const fn subsections(mut self, subs: &'static VMStateSubsections) -> Self {
if subs[subs.len() - 1].is_some() {
panic!("subsections are not terminated, use vmstate_subsections!");
}
let subs: *const Option<&bindings::VMStateDescription> = subs.as_ptr();
self.0.subsections = subs.cast::<*const bindings::VMStateDescription>();
self
}

View file

@ -13,9 +13,13 @@ use syn::{
Attribute, Data, DeriveInput, Error, Field, Fields, FieldsUnnamed, Ident, Meta, Path, Token,
Variant,
};
mod bits;
use bits::BitsConstInternal;
mod migration_state;
use migration_state::MigrationStateDerive;
#[cfg(test)]
mod tests;
@ -401,7 +405,98 @@ pub fn bits_const_internal(ts: TokenStream) -> TokenStream {
let ts = proc_macro2::TokenStream::from(ts);
let mut it = ts.into_iter();
BitsConstInternal::parse(&mut it)
let out = BitsConstInternal::parse(&mut it).unwrap_or_else(syn::Error::into_compile_error);
// https://github.com/rust-lang/rust-clippy/issues/15852
quote! {
{
#[allow(clippy::double_parens)]
#out
}
}
.into()
}
/// Derive macro for generating migration state structures and trait
/// implementations.
///
/// This macro generates a migration state struct and implements the
/// `ToMigrationState` trait for the annotated struct, enabling state
/// serialization and restoration. Note that defining a `VMStateDescription`
/// for the migration state struct is left to the user.
///
/// # Container attributes
///
/// The following attributes can be applied to the struct:
///
/// - `#[migration_state(rename = CustomName)]` - Customizes the name of the
/// generated migration struct. By default, the generated struct is named
/// `{OriginalName}Migration`.
///
/// # Field attributes
///
/// The following attributes can be applied to individual fields:
///
/// - `#[migration_state(omit)]` - Excludes the field from the migration state
/// entirely.
///
/// - `#[migration_state(into(Type))]` - Converts the field using `.into()`
/// during both serialization and restoration.
///
/// - `#[migration_state(try_into(Type))]` - Converts the field using
/// `.try_into()` during both serialization and restoration. Returns
/// `InvalidError` on conversion failure.
///
/// - `#[migration_state(clone)]` - Clones the field value.
///
/// Fields without any attributes use `ToMigrationState` recursively; note that
/// this is a simple copy for types that implement `Copy`.
///
/// # Attribute compatibility
///
/// - `omit` cannot be used with any other attributes
/// - only one of `into(Type)`, `try_into(Type)` can be used, but they can be
/// coupled with `clone`.
///
/// # Examples
///
/// Basic usage:
/// ```ignore
/// #[derive(ToMigrationState)]
/// struct MyStruct {
/// field1: u32,
/// field2: Timer,
/// }
/// ```
///
/// With attributes:
/// ```ignore
/// #[derive(ToMigrationState)]
/// #[migration_state(rename = CustomMigration)]
/// struct MyStruct {
/// #[migration_state(omit)]
/// runtime_field: u32,
///
/// #[migration_state(clone)]
/// shared_data: String,
///
/// #[migration_state(into(Cow<'static, str>), clone)]
/// converted_field: String,
///
/// #[migration_state(try_into(i8))]
/// fallible_field: u32,
///
/// // Default: use ToMigrationState trait recursively
/// nested_field: NestedStruct,
///
/// // Primitive types have a default implementation of ToMigrationState
/// simple_field: u32,
/// }
/// ```
#[proc_macro_derive(ToMigrationState, attributes(migration_state))]
pub fn derive_to_migration_state(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
MigrationStateDerive::expand(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

View file

@ -0,0 +1,298 @@
use std::borrow::Cow;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{spanned::Spanned, DeriveInput, Error, Field, Ident, Result, Type};
use crate::get_fields;
#[derive(Debug, Default)]
enum ConversionMode {
#[default]
None,
Omit,
Into(Type),
TryInto(Type),
ToMigrationState,
}
impl ConversionMode {
fn target_type(&self, original_type: &Type) -> TokenStream {
match self {
ConversionMode::Into(ty) | ConversionMode::TryInto(ty) => ty.to_token_stream(),
ConversionMode::ToMigrationState => {
quote! { <#original_type as ToMigrationState>::Migrated }
}
_ => original_type.to_token_stream(),
}
}
}
#[derive(Debug, Default)]
struct ContainerAttrs {
rename: Option<Ident>,
}
impl ContainerAttrs {
fn parse_from(&mut self, attrs: &[syn::Attribute]) -> Result<()> {
use attrs::{set, with, Attrs};
Attrs::new()
.once("rename", with::eq(set::parse(&mut self.rename)))
.parse_attrs("migration_state", attrs)?;
Ok(())
}
fn parse(attrs: &[syn::Attribute]) -> Result<Self> {
let mut container_attrs = Self::default();
container_attrs.parse_from(attrs)?;
Ok(container_attrs)
}
}
#[derive(Debug, Default)]
struct FieldAttrs {
conversion: ConversionMode,
clone: bool,
}
impl FieldAttrs {
fn parse_from(&mut self, attrs: &[syn::Attribute]) -> Result<()> {
let mut omit_flag = false;
let mut into_type: Option<Type> = None;
let mut try_into_type: Option<Type> = None;
use attrs::{set, with, Attrs};
Attrs::new()
.once("omit", set::flag(&mut omit_flag))
.once("into", with::paren(set::parse(&mut into_type)))
.once("try_into", with::paren(set::parse(&mut try_into_type)))
.once("clone", set::flag(&mut self.clone))
.parse_attrs("migration_state", attrs)?;
self.conversion = match (omit_flag, into_type, try_into_type, self.clone) {
// Valid combinations of attributes first...
(true, None, None, false) => ConversionMode::Omit,
(false, Some(ty), None, _) => ConversionMode::Into(ty),
(false, None, Some(ty), _) => ConversionMode::TryInto(ty),
(false, None, None, true) => ConversionMode::None, // clone without conversion
(false, None, None, false) => ConversionMode::ToMigrationState, // default behavior
// ... then the error cases
(true, _, _, _) => {
return Err(Error::new(
attrs[0].span(),
"ToMigrationState: omit cannot be used with other attributes",
));
}
(_, Some(_), Some(_), _) => {
return Err(Error::new(
attrs[0].span(),
"ToMigrationState: into and try_into attributes cannot be used together",
));
}
};
Ok(())
}
fn parse(attrs: &[syn::Attribute]) -> Result<Self> {
let mut field_attrs = Self::default();
field_attrs.parse_from(attrs)?;
Ok(field_attrs)
}
}
#[derive(Debug)]
struct MigrationStateField {
name: Ident,
original_type: Type,
attrs: FieldAttrs,
}
impl MigrationStateField {
fn maybe_clone(&self, mut value: TokenStream) -> TokenStream {
if self.attrs.clone {
value = quote! { #value.clone() };
}
value
}
fn generate_migration_state_field(&self) -> TokenStream {
let name = &self.name;
let field_type = self.attrs.conversion.target_type(&self.original_type);
quote! {
pub #name: #field_type,
}
}
fn generate_snapshot_field(&self) -> TokenStream {
let name = &self.name;
let value = self.maybe_clone(quote! { self.#name });
match &self.attrs.conversion {
ConversionMode::Omit => {
unreachable!("Omitted fields are filtered out during processing")
}
ConversionMode::None => quote! {
target.#name = #value;
},
ConversionMode::Into(_) => quote! {
target.#name = #value.into();
},
ConversionMode::TryInto(_) => quote! {
target.#name = #value.try_into().map_err(|_| migration::InvalidError)?;
},
ConversionMode::ToMigrationState => quote! {
self.#name.snapshot_migration_state(&mut target.#name)?;
},
}
}
fn generate_restore_field(&self) -> TokenStream {
let name = &self.name;
match &self.attrs.conversion {
ConversionMode::Omit => {
unreachable!("Omitted fields are filtered out during processing")
}
ConversionMode::None => quote! {
self.#name = #name;
},
ConversionMode::Into(_) => quote! {
self.#name = #name.into();
},
ConversionMode::TryInto(_) => quote! {
self.#name = #name.try_into().map_err(|_| migration::InvalidError)?;
},
ConversionMode::ToMigrationState => quote! {
self.#name.restore_migrated_state_mut(#name, _version_id)?;
},
}
}
}
#[derive(Debug)]
pub struct MigrationStateDerive {
input: DeriveInput,
fields: Vec<MigrationStateField>,
container_attrs: ContainerAttrs,
}
impl MigrationStateDerive {
fn parse(input: DeriveInput) -> Result<Self> {
let container_attrs = ContainerAttrs::parse(&input.attrs)?;
let fields = get_fields(&input, "ToMigrationState")?;
let fields = Self::process_fields(fields)?;
Ok(Self {
input,
fields,
container_attrs,
})
}
fn process_fields(
fields: &syn::punctuated::Punctuated<Field, syn::token::Comma>,
) -> Result<Vec<MigrationStateField>> {
let processed = fields
.iter()
.map(|field| {
let attrs = FieldAttrs::parse(&field.attrs)?;
Ok((field, attrs))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.filter(|(_, attrs)| !matches!(attrs.conversion, ConversionMode::Omit))
.map(|(field, attrs)| MigrationStateField {
name: field.ident.as_ref().unwrap().clone(),
original_type: field.ty.clone(),
attrs,
})
.collect();
Ok(processed)
}
fn migration_state_name(&self) -> Cow<'_, Ident> {
match &self.container_attrs.rename {
Some(rename) => Cow::Borrowed(rename),
None => Cow::Owned(format_ident!("{}Migration", &self.input.ident)),
}
}
fn generate_migration_state_struct(&self) -> TokenStream {
let name = self.migration_state_name();
let fields = self
.fields
.iter()
.map(MigrationStateField::generate_migration_state_field);
quote! {
#[derive(Default)]
pub struct #name {
#(#fields)*
}
}
}
fn generate_snapshot_migration_state(&self) -> TokenStream {
let fields = self
.fields
.iter()
.map(MigrationStateField::generate_snapshot_field);
quote! {
fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Result<(), migration::InvalidError> {
#(#fields)*
Ok(())
}
}
}
fn generate_restore_migrated_state(&self) -> TokenStream {
let names: Vec<_> = self.fields.iter().map(|f| &f.name).collect();
let fields = self
.fields
.iter()
.map(MigrationStateField::generate_restore_field);
// version_id could be used or not depending on conversion attributes
quote! {
#[allow(clippy::used_underscore_binding)]
fn restore_migrated_state_mut(&mut self, source: Self::Migrated, _version_id: u8) -> Result<(), migration::InvalidError> {
let Self::Migrated { #(#names),* } = source;
#(#fields)*
Ok(())
}
}
}
fn generate(&self) -> TokenStream {
let struct_name = &self.input.ident;
let generics = &self.input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let name = self.migration_state_name();
let migration_state_struct = self.generate_migration_state_struct();
let snapshot_impl = self.generate_snapshot_migration_state();
let restore_impl = self.generate_restore_migrated_state();
quote! {
#migration_state_struct
impl #impl_generics ToMigrationState for #struct_name #ty_generics #where_clause {
type Migrated = #name;
#snapshot_impl
#restore_impl
}
}
}
pub fn expand(input: DeriveInput) -> Result<TokenStream> {
let tokens = Self::parse(input)?.generate();
Ok(tokens)
}
}

View file

@ -7,7 +7,7 @@ use quote::quote;
use super::*;
macro_rules! derive_compile_fail {
($derive_fn:ident, $input:expr, $($error_msg:expr),+ $(,)?) => {{
($derive_fn:path, $input:expr, $($error_msg:expr),+ $(,)?) => {{
let input: proc_macro2::TokenStream = $input;
let error_msg = &[$( quote! { ::core::compile_error! { $error_msg } } ),*];
let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, syn::Error> =
@ -24,7 +24,7 @@ macro_rules! derive_compile_fail {
}
macro_rules! derive_compile {
($derive_fn:ident, $input:expr, $($expected:tt)*) => {{
($derive_fn:path, $input:expr, $($expected:tt)*) => {{
let input: proc_macro2::TokenStream = $input;
let expected: proc_macro2::TokenStream = $($expected)*;
let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, syn::Error> =
@ -345,3 +345,112 @@ fn test_derive_tryinto() {
}
);
}
#[test]
fn test_derive_to_migration_state() {
derive_compile_fail!(
MigrationStateDerive::expand,
quote! {
struct MyStruct {
#[migration_state(omit, clone)]
bad: u32,
}
},
"ToMigrationState: omit cannot be used with other attributes"
);
derive_compile_fail!(
MigrationStateDerive::expand,
quote! {
struct MyStruct {
#[migration_state(into)]
bad: u32,
}
},
"unexpected end of input, expected parentheses"
);
derive_compile_fail!(
MigrationStateDerive::expand,
quote! {
struct MyStruct {
#[migration_state(into(String), try_into(String))]
bad: &'static str,
}
},
"ToMigrationState: into and try_into attributes cannot be used together"
);
derive_compile!(
MigrationStateDerive::expand,
quote! {
#[migration_state(rename = CustomMigration)]
struct MyStruct {
#[migration_state(omit)]
runtime_field: u32,
#[migration_state(clone)]
shared_data: String,
#[migration_state(into(Cow<'static, str>), clone)]
converted_field: String,
#[migration_state(try_into(i8))]
fallible_field: u32,
nested_field: NestedStruct,
simple_field: u32,
}
},
quote! {
#[derive(Default)]
pub struct CustomMigration {
pub shared_data: String,
pub converted_field: Cow<'static, str>,
pub fallible_field: i8,
pub nested_field: <NestedStruct as ToMigrationState>::Migrated,
pub simple_field: <u32 as ToMigrationState>::Migrated,
}
impl ToMigrationState for MyStruct {
type Migrated = CustomMigration;
fn snapshot_migration_state(
&self,
target: &mut Self::Migrated
) -> Result<(), migration::InvalidError> {
target.shared_data = self.shared_data.clone();
target.converted_field = self.converted_field.clone().into();
target.fallible_field = self
.fallible_field
.try_into()
.map_err(|_| migration::InvalidError)?;
self.nested_field
.snapshot_migration_state(&mut target.nested_field)?;
self.simple_field
.snapshot_migration_state(&mut target.simple_field)?;
Ok(())
}
#[allow(clippy::used_underscore_binding)]
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
_version_id: u8
) -> Result<(), migration::InvalidError> {
let Self::Migrated {
shared_data,
converted_field,
fallible_field,
nested_field,
simple_field
} = source;
self.shared_data = shared_data;
self.converted_field = converted_field.into();
self.fallible_field = fallible_field
.try_into()
.map_err(|_| migration::InvalidError)?;
self.nested_field
.restore_migrated_state_mut(nested_field, _version_id)?;
self.simple_field
.restore_migrated_state_mut(simple_field, _version_id)?;
Ok(())
}
}
}
);
}

View file

@ -2123,7 +2123,7 @@ static int kvm_arch_put_sve(CPUState *cs)
return 0;
}
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
uint64_t val;
uint32_t fpr;

View file

@ -1397,7 +1397,7 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
.type = CPUID_FEATURE_WORD,
.feat_names = {
"no-nested-data-bp", "fs-gs-base-ns", "lfence-always-serializing", NULL,
NULL, NULL, "null-sel-clr-base", NULL,
NULL, "verw-clear", "null-sel-clr-base", NULL,
"auto-ibrs", NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
@ -1415,6 +1415,22 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
.tcg_features = 0,
.unmigratable_flags = 0,
},
[FEAT_8000_0021_ECX] = {
.type = CPUID_FEATURE_WORD,
.feat_names = {
NULL, "tsa-sq-no", "tsa-l1-no", NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
},
.cpuid = { .eax = 0x80000021, .reg = R_ECX, },
.tcg_features = 0,
.unmigratable_flags = 0,
},
[FEAT_8000_0022_EAX] = {
.type = CPUID_FEATURE_WORD,
.feat_names = {
@ -8526,6 +8542,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
*eax = *ebx = *ecx = *edx = 0;
*eax = env->features[FEAT_8000_0021_EAX];
*ebx = env->features[FEAT_8000_0021_EBX];
*ecx = env->features[FEAT_8000_0021_ECX];
break;
case 0x80000022:
*eax = *ebx = *ecx = *edx = 0;
@ -8632,7 +8649,11 @@ static void x86_cpu_reset_hold(Object *obj, ResetType type)
env->idt.limit = 0xffff;
env->gdt.limit = 0xffff;
#if defined(CONFIG_USER_ONLY)
env->ldt.limit = 0;
#else
env->ldt.limit = 0xffff;
#endif
env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
env->tr.limit = 0xffff;
env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);

View file

@ -643,6 +643,7 @@ typedef enum FeatureWord {
FEAT_8000_0008_EBX, /* CPUID[8000_0008].EBX */
FEAT_8000_0021_EAX, /* CPUID[8000_0021].EAX */
FEAT_8000_0021_EBX, /* CPUID[8000_0021].EBX */
FEAT_8000_0021_ECX, /* CPUID[8000_0021].ECX */
FEAT_8000_0022_EAX, /* CPUID[8000_0022].EAX */
FEAT_C000_0001_EDX, /* CPUID[C000_0001].EDX */
FEAT_KVM, /* CPUID[4000_0001].EAX (KVM_CPUID_FEATURES) */
@ -1103,6 +1104,8 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);
#define CPUID_8000_0021_EAX_FS_GS_BASE_NS (1U << 1)
/* LFENCE is always serializing */
#define CPUID_8000_0021_EAX_LFENCE_ALWAYS_SERIALIZING (1U << 2)
/* Memory form of VERW mitigates TSA */
#define CPUID_8000_0021_EAX_VERW_CLEAR (1U << 5)
/* Null Selector Clears Base */
#define CPUID_8000_0021_EAX_NULL_SEL_CLR_BASE (1U << 6)
/* Automatic IBRS */
@ -1126,6 +1129,11 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);
*/
#define CPUID_8000_0021_EBX_RAPSIZE (8U << 16)
/* CPU is not vulnerable TSA SA-SQ attack */
#define CPUID_8000_0021_ECX_TSA_SQ_NO (1U << 1)
/* CPU is not vulnerable TSA SA-L1 attack */
#define CPUID_8000_0021_ECX_TSA_L1_NO (1U << 2)
/* Performance Monitoring Version 2 */
#define CPUID_8000_0022_EAX_PERFMON_V2 (1U << 0)

View file

@ -621,6 +621,10 @@ void do_cpu_init(X86CPU *cpu)
void do_cpu_sipi(X86CPU *cpu)
{
CPUX86State *env = &cpu->env;
if (env->hflags & HF_SMM_MASK) {
return;
}
apic_sipi(cpu->apic_state);
}

View file

@ -653,6 +653,23 @@ uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index)
must_be_one = (uint32_t)value;
can_be_one = (uint32_t)(value >> 32);
return can_be_one & ~must_be_one;
case MSR_IA32_ARCH_CAPABILITIES:
/*
* Special handling for fb-clear bit in ARCH_CAPABILITIES MSR.
* KVM will only report the bit if it is enabled in the host,
* but, for live migration capability purposes, we want to
* expose the bit to the guest even if it is disabled in the
* host, as long as the host itself is not vulnerable to
* the issue that the fb-clear bit is meant to mitigate.
*/
if ((value & MSR_ARCH_CAP_MDS_NO) &&
(value & MSR_ARCH_CAP_TAA_NO) &&
(value & MSR_ARCH_CAP_SBDR_SSDP_NO) &&
(value & MSR_ARCH_CAP_FBSDP_NO) &&
(value & MSR_ARCH_CAP_PSDP_NO)) {
value |= MSR_ARCH_CAP_FB_CLEAR;
}
return value;
default:
return value;
@ -3907,7 +3924,7 @@ static void kvm_init_msrs(X86CPU *cpu)
assert(kvm_buf_set_msrs(cpu) == 0);
}
static int kvm_put_msrs(X86CPU *cpu, int level)
static int kvm_put_msrs(X86CPU *cpu, KvmPutState level)
{
CPUX86State *env = &cpu->env;
int i;
@ -5027,7 +5044,7 @@ static int kvm_get_apic(X86CPU *cpu)
return 0;
}
static int kvm_put_vcpu_events(X86CPU *cpu, int level)
static int kvm_put_vcpu_events(X86CPU *cpu, KvmPutState level)
{
CPUState *cs = CPU(cpu);
CPUX86State *env = &cpu->env;
@ -5270,7 +5287,7 @@ static int kvm_get_nested_state(X86CPU *cpu)
return ret;
}
int kvm_arch_put_registers(CPUState *cpu, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cpu, KvmPutState level, Error **errp)
{
X86CPU *x86_cpu = X86_CPU(cpu);
int ret;

View file

@ -1541,7 +1541,7 @@ static void decode_group4_5(DisasContext *s, CPUX86State *env, X86OpEntry *entry
[0x0b] = X86_OP_ENTRYr(CALLF_m, M,p),
[0x0c] = X86_OP_ENTRYr(JMP_m, E,f64, zextT0),
[0x0d] = X86_OP_ENTRYr(JMPF_m, M,p),
[0x0e] = X86_OP_ENTRYr(PUSH, E,f64),
[0x0e] = X86_OP_ENTRYr(PUSH, E,d64),
};
int w = (*b & 1);

View file

@ -456,7 +456,7 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
new_segs[i] = access_ldw(&new, tss_base + (0x48 + i * 4));
}
new_ldt = access_ldw(&new, tss_base + 0x60);
new_trap = access_ldl(&new, tss_base + 0x64);
new_trap = access_ldw(&new, tss_base + 0x64) & 1;
} else {
/* 16 bit */
new_cr3 = 0;

View file

@ -592,7 +592,8 @@ static bool get_physical_address(CPUX86State *env, vaddr addr,
if (sext != 0 && sext != -1) {
*err = (TranslateFault){
.exception_index = EXCP0D_GPF,
.cr2 = addr,
/* non-canonical #GP doesn't change CR2 */
.cr2 = env->cr[2],
};
return false;
}

View file

@ -182,6 +182,7 @@ bool x86_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
apic_poll_irq(cpu->apic_state);
break;
case CPU_INTERRUPT_SIPI:
cpu_reset_interrupt(cs, CPU_INTERRUPT_SIPI);
do_cpu_sipi(cpu);
break;
case CPU_INTERRUPT_SMI:

View file

@ -168,7 +168,7 @@ void do_smm_enter(X86CPU *cpu)
env->cr[0] & ~(CR0_PE_MASK | CR0_EM_MASK | CR0_TS_MASK |
CR0_PG_MASK));
cpu_x86_update_cr4(env, 0);
env->dr[7] = 0x00000400;
helper_set_dr(env, 7, 0x00000400);
cpu_x86_load_seg_cache(env, R_CS, (env->smbase >> 4) & 0xffff, env->smbase,
0xffffffff,
@ -233,8 +233,8 @@ void helper_rsm(CPUX86State *env)
env->eip = x86_ldq_phys(cs, sm_state + 0x7f78);
cpu_load_eflags(env, x86_ldl_phys(cs, sm_state + 0x7f70),
~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
env->dr[6] = x86_ldl_phys(cs, sm_state + 0x7f68);
env->dr[7] = x86_ldl_phys(cs, sm_state + 0x7f60);
helper_set_dr(env, 6, x86_ldl_phys(cs, sm_state + 0x7f68));
helper_set_dr(env, 7, x86_ldl_phys(cs, sm_state + 0x7f60));
cpu_x86_update_cr4(env, x86_ldl_phys(cs, sm_state + 0x7f48));
cpu_x86_update_cr3(env, x86_ldq_phys(cs, sm_state + 0x7f50));
@ -268,8 +268,8 @@ void helper_rsm(CPUX86State *env)
env->regs[R_EDX] = x86_ldl_phys(cs, sm_state + 0x7fd8);
env->regs[R_ECX] = x86_ldl_phys(cs, sm_state + 0x7fd4);
env->regs[R_EAX] = x86_ldl_phys(cs, sm_state + 0x7fd0);
env->dr[6] = x86_ldl_phys(cs, sm_state + 0x7fcc);
env->dr[7] = x86_ldl_phys(cs, sm_state + 0x7fc8);
helper_set_dr(env, 6, x86_ldl_phys(cs, sm_state + 0x7fcc));
helper_set_dr(env, 7, x86_ldl_phys(cs, sm_state + 0x7fc8));
env->tr.selector = x86_ldl_phys(cs, sm_state + 0x7fc4) & 0xffff;
env->tr.base = x86_ldl_phys(cs, sm_state + 0x7f64);

View file

@ -325,7 +325,7 @@ static int kvm_loongarch_get_csr(CPUState *cs)
return ret;
}
static int kvm_loongarch_put_csr(CPUState *cs, int level)
static int kvm_loongarch_put_csr(CPUState *cs, KvmPutState level)
{
int ret = 0;
CPULoongArchState *env = cpu_env(cs);
@ -763,7 +763,7 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)
return ret;
}
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
int ret;
static int once;

View file

@ -590,7 +590,7 @@ static void kvm_mips_update_state(void *opaque, bool running, RunState state)
}
}
static int kvm_mips_put_fpu_registers(CPUState *cs, int level)
static int kvm_mips_put_fpu_registers(CPUState *cs, KvmPutState level)
{
CPUMIPSState *env = cpu_env(cs);
int err, ret = 0;
@ -749,7 +749,7 @@ static int kvm_mips_get_fpu_registers(CPUState *cs)
}
static int kvm_mips_put_cp0_registers(CPUState *cs, int level)
static int kvm_mips_put_cp0_registers(CPUState *cs, KvmPutState level)
{
CPUMIPSState *env = cpu_env(cs);
int err, ret = 0;
@ -1177,7 +1177,7 @@ static int kvm_mips_get_cp0_registers(CPUState *cs)
return ret;
}
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
CPUMIPSState *env = cpu_env(cs);
struct kvm_regs regs;

View file

@ -907,7 +907,7 @@ int kvmppc_put_books_sregs(PowerPCCPU *cpu)
return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
}
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
PowerPCCPU *cpu = POWERPC_CPU(cs);
CPUPPCState *env = &cpu->env;

View file

@ -1369,7 +1369,7 @@ int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state)
return 0;
}
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
int ret = 0;

View file

@ -468,7 +468,7 @@ static int can_sync_regs(CPUState *cs, int regs)
#define KVM_SYNC_REQUIRED_REGS (KVM_SYNC_GPRS | KVM_SYNC_ACRS | \
KVM_SYNC_CRS | KVM_SYNC_PREFIX)
int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)
int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
{
CPUS390XState *env = cpu_env(cs);
struct kvm_fpu fpu = {};

View file

@ -256,8 +256,9 @@ static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
QEMUBH *bh;
QSLIST_FOREACH_RCU(bh, head, next) {
if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
if (bh->flags & BH_IDLE) {
int flags = qatomic_load_acquire(&bh->flags);
if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
if (flags & BH_IDLE) {
/* idle bottom halves will be polled at least
* every 10ms */
timeout = 10000000;
@ -335,14 +336,16 @@ aio_ctx_check(GSource *source)
aio_notify_accept(ctx);
QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
int flags = qatomic_load_acquire(&bh->flags);
if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
return true;
}
}
QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
QSLIST_FOREACH_RCU(bh, &s->bh_list, next) {
if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
int flags = qatomic_load_acquire(&bh->flags);
if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
return true;
}
}

View file

@ -89,7 +89,7 @@ static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
return &qemu_clocks[type];
}
static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
static bool timer_expired_ns(const QEMUTimer *timer_head, int64_t current_time)
{
return timer_head && (timer_head->expire_time <= current_time);
}
@ -475,12 +475,12 @@ void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
timer_mod_anticipate_ns(ts, expire_time * ts->scale);
}
bool timer_pending(QEMUTimer *ts)
bool timer_pending(const QEMUTimer *ts)
{
return ts->expire_time >= 0;
}
bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
bool timer_expired(const QEMUTimer *timer_head, int64_t current_time)
{
return timer_expired_ns(timer_head, current_time * timer_head->scale);
}
@ -649,7 +649,7 @@ void init_clocks(QEMUTimerListNotifyCB *notify_cb)
#endif
}
uint64_t timer_expire_time_ns(QEMUTimer *ts)
uint64_t timer_expire_time_ns(const QEMUTimer *ts)
{
return timer_pending(ts) ? ts->expire_time : -1;
}