1 // Copyright 2024, Linaro Limited 2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 //! Helper macros to declare migration state for device models. 6 //! 7 //! This module includes four families of macros: 8 //! 9 //! * [`vmstate_unused!`](crate::vmstate_unused) and 10 //! [`vmstate_of!`](crate::vmstate_of), which are used to express the 11 //! migration format for a struct. This is based on the [`VMState`] trait, 12 //! which is defined by all migrateable types. 13 //! 14 //! * [`impl_vmstate_forward`](crate::impl_vmstate_forward) and 15 //! [`impl_vmstate_bitsized`](crate::impl_vmstate_bitsized), which help with 16 //! the definition of the [`VMState`] trait (respectively for transparent 17 //! structs and for `bilge`-defined types) 18 //! 19 //! * helper macros to declare a device model state struct, in particular 20 //! [`vmstate_subsections`](crate::vmstate_subsections) and 21 //! [`vmstate_fields`](crate::vmstate_fields). 22 //! 23 //! * direct equivalents to the C macros declared in 24 //! `include/migration/vmstate.h`. These are not type-safe and only provide 25 //! functionality that is missing from `vmstate_of!`. 26 27 use core::{marker::PhantomData, mem, ptr::NonNull}; 28 use std::os::raw::{c_int, c_void}; 29 30 pub use crate::bindings::{VMStateDescription, VMStateField}; 31 use crate::{ 32 bindings::VMStateFlags, callbacks::FnCall, prelude::*, qom::Owned, zeroable::Zeroable, 33 }; 34 35 /// This macro is used to call a function with a generic argument bound 36 /// to the type of a field. The function must take a 37 /// [`PhantomData`]`<T>` argument; `T` is the type of 38 /// field `$field` in the `$typ` type. 39 /// 40 /// # Examples 41 /// 42 /// ``` 43 /// # use qemu_api::call_func_with_field; 44 /// # use core::marker::PhantomData; 45 /// const fn size_of_field<T>(_: PhantomData<T>) -> usize { 46 /// std::mem::size_of::<T>() 47 /// } 48 /// 49 /// struct Foo { 50 /// x: u16, 51 /// }; 52 /// // calls size_of_field::<u16>() 53 /// assert_eq!(call_func_with_field!(size_of_field, Foo, x), 2); 54 /// ``` 55 #[macro_export] 56 macro_rules! call_func_with_field { 57 // Based on the answer by user steffahn (Frank Steffahn) at 58 // https://users.rust-lang.org/t/inferring-type-of-field/122857 59 // and used under MIT license 60 ($func:expr, $typ:ty, $($field:tt).+) => { 61 $func(loop { 62 #![allow(unreachable_code)] 63 const fn phantom__<T>(_: &T) -> ::core::marker::PhantomData<T> { ::core::marker::PhantomData } 64 // Unreachable code is exempt from checks on uninitialized values. 65 // Use that trick to infer the type of this PhantomData. 66 break ::core::marker::PhantomData; 67 break phantom__(&{ let value__: $typ; value__.$($field).+ }); 68 }) 69 }; 70 } 71 72 /// Workaround for lack of `const_refs_static`: references to global variables 73 /// can be included in a `static`, but not in a `const`; unfortunately, this 74 /// is exactly what would go in the `VMStateField`'s `info` member. 75 /// 76 /// This enum contains the contents of the `VMStateField`'s `info` member, 77 /// but as an `enum` instead of a pointer. 78 #[allow(non_camel_case_types)] 79 pub enum VMStateFieldType { 80 null, 81 vmstate_info_bool, 82 vmstate_info_int8, 83 vmstate_info_int16, 84 vmstate_info_int32, 85 vmstate_info_int64, 86 vmstate_info_uint8, 87 vmstate_info_uint16, 88 vmstate_info_uint32, 89 vmstate_info_uint64, 90 vmstate_info_timer, 91 } 92 93 /// Workaround for lack of `const_refs_static`. Converts a `VMStateFieldType` 94 /// to a `*const VMStateInfo`, for inclusion in a `VMStateField`. 95 #[macro_export] 96 macro_rules! info_enum_to_ref { 97 ($e:expr) => { 98 unsafe { 99 match $e { 100 $crate::vmstate::VMStateFieldType::null => ::core::ptr::null(), 101 $crate::vmstate::VMStateFieldType::vmstate_info_bool => { 102 ::core::ptr::addr_of!($crate::bindings::vmstate_info_bool) 103 } 104 $crate::vmstate::VMStateFieldType::vmstate_info_int8 => { 105 ::core::ptr::addr_of!($crate::bindings::vmstate_info_int8) 106 } 107 $crate::vmstate::VMStateFieldType::vmstate_info_int16 => { 108 ::core::ptr::addr_of!($crate::bindings::vmstate_info_int16) 109 } 110 $crate::vmstate::VMStateFieldType::vmstate_info_int32 => { 111 ::core::ptr::addr_of!($crate::bindings::vmstate_info_int32) 112 } 113 $crate::vmstate::VMStateFieldType::vmstate_info_int64 => { 114 ::core::ptr::addr_of!($crate::bindings::vmstate_info_int64) 115 } 116 $crate::vmstate::VMStateFieldType::vmstate_info_uint8 => { 117 ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint8) 118 } 119 $crate::vmstate::VMStateFieldType::vmstate_info_uint16 => { 120 ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint16) 121 } 122 $crate::vmstate::VMStateFieldType::vmstate_info_uint32 => { 123 ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint32) 124 } 125 $crate::vmstate::VMStateFieldType::vmstate_info_uint64 => { 126 ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint64) 127 } 128 $crate::vmstate::VMStateFieldType::vmstate_info_timer => { 129 ::core::ptr::addr_of!($crate::bindings::vmstate_info_timer) 130 } 131 } 132 } 133 }; 134 } 135 136 /// A trait for types that can be included in a device's migration stream. It 137 /// provides the base contents of a `VMStateField` (minus the name and offset). 138 /// 139 /// # Safety 140 /// 141 /// The contents of this trait go straight into structs that are parsed by C 142 /// code and used to introspect into other structs. Generally, you don't need 143 /// to implement it except via macros that do it for you, such as 144 /// `impl_vmstate_bitsized!`. 145 pub unsafe trait VMState { 146 /// The `info` member of a `VMStateField` is a pointer and as such cannot 147 /// yet be included in the [`BASE`](VMState::BASE) associated constant; 148 /// this is only allowed by Rust 1.83.0 and newer. For now, include the 149 /// member as an enum which is stored in a separate constant. 150 const SCALAR_TYPE: VMStateFieldType = VMStateFieldType::null; 151 152 /// The base contents of a `VMStateField` (minus the name and offset) for 153 /// the type that is implementing the trait. 154 const BASE: VMStateField; 155 156 /// A flag that is added to another field's `VMStateField` to specify the 157 /// length's type in a variable-sized array. If this is not a supported 158 /// type for the length (i.e. if it is not `u8`, `u16`, `u32`), using it 159 /// in a call to [`vmstate_of!`](crate::vmstate_of) will cause a 160 /// compile-time error. 161 const VARRAY_FLAG: VMStateFlags = { 162 panic!("invalid type for variable-sized array"); 163 }; 164 } 165 166 /// Internal utility function to retrieve a type's `VMStateFieldType`; 167 /// used by [`vmstate_of!`](crate::vmstate_of). 168 pub const fn vmstate_scalar_type<T: VMState>(_: PhantomData<T>) -> VMStateFieldType { 169 T::SCALAR_TYPE 170 } 171 172 /// Internal utility function to retrieve a type's `VMStateField`; 173 /// used by [`vmstate_of!`](crate::vmstate_of). 174 pub const fn vmstate_base<T: VMState>(_: PhantomData<T>) -> VMStateField { 175 T::BASE 176 } 177 178 /// Internal utility function to retrieve a type's `VMStateFlags` when it 179 /// is used as the element count of a `VMSTATE_VARRAY`; used by 180 /// [`vmstate_of!`](crate::vmstate_of). 181 pub const fn vmstate_varray_flag<T: VMState>(_: PhantomData<T>) -> VMStateFlags { 182 T::VARRAY_FLAG 183 } 184 185 /// Return the `VMStateField` for a field of a struct. The field must be 186 /// visible in the current scope. 187 /// 188 /// Only a limited set of types is supported out of the box: 189 /// * scalar types (integer and `bool`) 190 /// * the C struct `QEMUTimer` 191 /// * a transparent wrapper for any of the above (`Cell`, `UnsafeCell`, 192 /// [`BqlCell`], [`BqlRefCell`] 193 /// * a raw pointer to any of the above 194 /// * a `NonNull` pointer, a `Box` or an [`Owned`] for any of the above 195 /// * an array of any of the above 196 /// 197 /// In order to support other types, the trait `VMState` must be implemented 198 /// for them. The macros 199 /// [`impl_vmstate_bitsized!`](crate::impl_vmstate_bitsized) 200 /// and [`impl_vmstate_forward!`](crate::impl_vmstate_forward) help with this. 201 #[macro_export] 202 macro_rules! vmstate_of { 203 ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])? $(,)?) => { 204 $crate::bindings::VMStateField { 205 name: ::core::concat!(::core::stringify!($field_name), "\0") 206 .as_bytes() 207 .as_ptr() as *const ::std::os::raw::c_char, 208 offset: $crate::offset_of!($struct_name, $field_name), 209 $(num_offset: $crate::offset_of!($struct_name, $num),)? 210 // The calls to `call_func_with_field!` are the magic that 211 // computes most of the VMStateField from the type of the field. 212 info: $crate::info_enum_to_ref!($crate::call_func_with_field!( 213 $crate::vmstate::vmstate_scalar_type, 214 $struct_name, 215 $field_name 216 )), 217 ..$crate::call_func_with_field!( 218 $crate::vmstate::vmstate_base, 219 $struct_name, 220 $field_name 221 )$(.with_varray_flag($crate::call_func_with_field!( 222 $crate::vmstate::vmstate_varray_flag, 223 $struct_name, 224 $num)) 225 $(.with_varray_multiply($factor))?)? 226 } 227 }; 228 } 229 230 impl VMStateFlags { 231 const VMS_VARRAY_FLAGS: VMStateFlags = VMStateFlags( 232 VMStateFlags::VMS_VARRAY_INT32.0 233 | VMStateFlags::VMS_VARRAY_UINT8.0 234 | VMStateFlags::VMS_VARRAY_UINT16.0 235 | VMStateFlags::VMS_VARRAY_UINT32.0, 236 ); 237 } 238 239 // Add a couple builder-style methods to VMStateField, allowing 240 // easy derivation of VMStateField constants from other types. 241 impl VMStateField { 242 #[must_use] 243 pub const fn with_version_id(mut self, version_id: i32) -> Self { 244 assert!(version_id >= 0); 245 self.version_id = version_id; 246 self 247 } 248 249 #[must_use] 250 pub const fn with_array_flag(mut self, num: usize) -> Self { 251 assert!(num <= 0x7FFF_FFFFusize); 252 assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) == 0); 253 assert!((self.flags.0 & VMStateFlags::VMS_VARRAY_FLAGS.0) == 0); 254 if (self.flags.0 & VMStateFlags::VMS_POINTER.0) != 0 { 255 self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_POINTER.0); 256 self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY_OF_POINTER.0); 257 // VMS_ARRAY_OF_POINTER flag stores the size of pointer. 258 // FIXME: *const, *mut, NonNull and Box<> have the same size as usize. 259 // Resize if more smart pointers are supported. 260 self.size = std::mem::size_of::<usize>(); 261 } 262 self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_SINGLE.0); 263 self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY.0); 264 self.num = num as i32; 265 self 266 } 267 268 #[must_use] 269 pub const fn with_pointer_flag(mut self) -> Self { 270 assert!((self.flags.0 & VMStateFlags::VMS_POINTER.0) == 0); 271 self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_POINTER.0); 272 self 273 } 274 275 #[must_use] 276 pub const fn with_varray_flag_unchecked(mut self, flag: VMStateFlags) -> VMStateField { 277 self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_ARRAY.0); 278 self.flags = VMStateFlags(self.flags.0 | flag.0); 279 self.num = 0; // varray uses num_offset instead of num. 280 self 281 } 282 283 #[must_use] 284 #[allow(unused_mut)] 285 pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> VMStateField { 286 assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) != 0); 287 self.with_varray_flag_unchecked(flag) 288 } 289 290 #[must_use] 291 pub const fn with_varray_multiply(mut self, num: u32) -> VMStateField { 292 assert!(num <= 0x7FFF_FFFFu32); 293 self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_MULTIPLY_ELEMENTS.0); 294 self.num = num as i32; 295 self 296 } 297 } 298 299 /// This macro can be used (by just passing it a type) to forward the `VMState` 300 /// trait to the first field of a tuple. This is a workaround for lack of 301 /// support of nested [`offset_of`](core::mem::offset_of) until Rust 1.82.0. 302 /// 303 /// # Examples 304 /// 305 /// ``` 306 /// # use qemu_api::impl_vmstate_forward; 307 /// pub struct Fifo([u8; 16]); 308 /// impl_vmstate_forward!(Fifo); 309 /// ``` 310 #[macro_export] 311 macro_rules! impl_vmstate_forward { 312 // This is similar to impl_vmstate_transparent below, but it 313 // uses the same trick as vmstate_of! to obtain the type of 314 // the first field of the tuple 315 ($tuple:ty) => { 316 unsafe impl $crate::vmstate::VMState for $tuple { 317 const SCALAR_TYPE: $crate::vmstate::VMStateFieldType = 318 $crate::call_func_with_field!($crate::vmstate::vmstate_scalar_type, $tuple, 0); 319 const BASE: $crate::bindings::VMStateField = 320 $crate::call_func_with_field!($crate::vmstate::vmstate_base, $tuple, 0); 321 } 322 }; 323 } 324 325 // Transparent wrappers: just use the internal type 326 327 macro_rules! impl_vmstate_transparent { 328 ($type:ty where $base:tt: VMState $($where:tt)*) => { 329 unsafe impl<$base> VMState for $type where $base: VMState $($where)* { 330 const SCALAR_TYPE: VMStateFieldType = <$base as VMState>::SCALAR_TYPE; 331 const BASE: VMStateField = VMStateField { 332 size: mem::size_of::<$type>(), 333 ..<$base as VMState>::BASE 334 }; 335 const VARRAY_FLAG: VMStateFlags = <$base as VMState>::VARRAY_FLAG; 336 } 337 }; 338 } 339 340 impl_vmstate_transparent!(std::cell::Cell<T> where T: VMState); 341 impl_vmstate_transparent!(std::cell::UnsafeCell<T> where T: VMState); 342 impl_vmstate_transparent!(std::pin::Pin<T> where T: VMState); 343 impl_vmstate_transparent!(crate::cell::BqlCell<T> where T: VMState); 344 impl_vmstate_transparent!(crate::cell::BqlRefCell<T> where T: VMState); 345 impl_vmstate_transparent!(crate::cell::Opaque<T> where T: VMState); 346 347 #[macro_export] 348 macro_rules! impl_vmstate_bitsized { 349 ($type:ty) => { 350 unsafe impl $crate::vmstate::VMState for $type { 351 const SCALAR_TYPE: $crate::vmstate::VMStateFieldType = 352 <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt 353 as ::bilge::prelude::Number>::UnderlyingType 354 as $crate::vmstate::VMState>::SCALAR_TYPE; 355 const BASE: $crate::bindings::VMStateField = 356 <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt 357 as ::bilge::prelude::Number>::UnderlyingType 358 as $crate::vmstate::VMState>::BASE; 359 const VARRAY_FLAG: $crate::bindings::VMStateFlags = 360 <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt 361 as ::bilge::prelude::Number>::UnderlyingType 362 as $crate::vmstate::VMState>::VARRAY_FLAG; 363 } 364 }; 365 } 366 367 // Scalar types using predefined VMStateInfos 368 369 macro_rules! impl_vmstate_scalar { 370 ($info:ident, $type:ty$(, $varray_flag:ident)?) => { 371 unsafe impl VMState for $type { 372 const SCALAR_TYPE: VMStateFieldType = VMStateFieldType::$info; 373 const BASE: VMStateField = VMStateField { 374 size: mem::size_of::<$type>(), 375 flags: VMStateFlags::VMS_SINGLE, 376 ..Zeroable::ZERO 377 }; 378 $(const VARRAY_FLAG: VMStateFlags = VMStateFlags::$varray_flag;)? 379 } 380 }; 381 } 382 383 impl_vmstate_scalar!(vmstate_info_bool, bool); 384 impl_vmstate_scalar!(vmstate_info_int8, i8); 385 impl_vmstate_scalar!(vmstate_info_int16, i16); 386 impl_vmstate_scalar!(vmstate_info_int32, i32); 387 impl_vmstate_scalar!(vmstate_info_int64, i64); 388 impl_vmstate_scalar!(vmstate_info_uint8, u8, VMS_VARRAY_UINT8); 389 impl_vmstate_scalar!(vmstate_info_uint16, u16, VMS_VARRAY_UINT16); 390 impl_vmstate_scalar!(vmstate_info_uint32, u32, VMS_VARRAY_UINT32); 391 impl_vmstate_scalar!(vmstate_info_uint64, u64); 392 impl_vmstate_scalar!(vmstate_info_timer, crate::timer::Timer); 393 394 // Pointer types using the underlying type's VMState plus VMS_POINTER 395 // Note that references are not supported, though references to cells 396 // could be allowed. 397 398 macro_rules! impl_vmstate_pointer { 399 ($type:ty where $base:tt: VMState $($where:tt)*) => { 400 unsafe impl<$base> VMState for $type where $base: VMState $($where)* { 401 const SCALAR_TYPE: VMStateFieldType = <T as VMState>::SCALAR_TYPE; 402 const BASE: VMStateField = <$base as VMState>::BASE.with_pointer_flag(); 403 } 404 }; 405 } 406 407 impl_vmstate_pointer!(*const T where T: VMState); 408 impl_vmstate_pointer!(*mut T where T: VMState); 409 impl_vmstate_pointer!(NonNull<T> where T: VMState); 410 411 // Unlike C pointers, Box is always non-null therefore there is no need 412 // to specify VMS_ALLOC. 413 impl_vmstate_pointer!(Box<T> where T: VMState); 414 impl_vmstate_pointer!(Owned<T> where T: VMState + ObjectType); 415 416 // Arrays using the underlying type's VMState plus 417 // VMS_ARRAY/VMS_ARRAY_OF_POINTER 418 419 unsafe impl<T: VMState, const N: usize> VMState for [T; N] { 420 const SCALAR_TYPE: VMStateFieldType = <T as VMState>::SCALAR_TYPE; 421 const BASE: VMStateField = <T as VMState>::BASE.with_array_flag(N); 422 } 423 424 #[doc(alias = "VMSTATE_UNUSED")] 425 #[macro_export] 426 macro_rules! vmstate_unused { 427 ($size:expr) => {{ 428 $crate::bindings::VMStateField { 429 name: $crate::c_str!("unused").as_ptr(), 430 size: $size, 431 info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) }, 432 flags: $crate::bindings::VMStateFlags::VMS_BUFFER, 433 ..$crate::zeroable::Zeroable::ZERO 434 } 435 }}; 436 } 437 438 // FIXME: including the `vmsd` field in a `const` is not possible without 439 // the const_refs_static feature (stabilized in Rust 1.83.0). Without it, 440 // it is not possible to use VMS_STRUCT in a transparent manner using 441 // `vmstate_of!`. While VMSTATE_CLOCK can at least try to be type-safe, 442 // VMSTATE_STRUCT includes $type only for documentation purposes; it 443 // is checked against $field_name and $struct_name, but not against $vmsd 444 // which is what really would matter. 445 #[doc(alias = "VMSTATE_STRUCT")] 446 #[macro_export] 447 macro_rules! vmstate_struct { 448 ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])?, $vmsd:expr, $type:ty $(,)?) => { 449 $crate::bindings::VMStateField { 450 name: ::core::concat!(::core::stringify!($field_name), "\0") 451 .as_bytes() 452 .as_ptr() as *const ::std::os::raw::c_char, 453 $(num_offset: $crate::offset_of!($struct_name, $num),)? 454 offset: { 455 $crate::assert_field_type!($struct_name, $field_name, $type $(, num = $num)?); 456 $crate::offset_of!($struct_name, $field_name) 457 }, 458 size: ::core::mem::size_of::<$type>(), 459 flags: $crate::bindings::VMStateFlags::VMS_STRUCT, 460 vmsd: $vmsd, 461 ..$crate::zeroable::Zeroable::ZERO 462 } $(.with_varray_flag_unchecked( 463 $crate::call_func_with_field!( 464 $crate::vmstate::vmstate_varray_flag, 465 $struct_name, 466 $num 467 ) 468 ) 469 $(.with_varray_multiply($factor))?)? 470 }; 471 } 472 473 #[doc(alias = "VMSTATE_CLOCK")] 474 #[macro_export] 475 macro_rules! vmstate_clock { 476 ($struct_name:ty, $field_name:ident) => {{ 477 $crate::bindings::VMStateField { 478 name: ::core::concat!(::core::stringify!($field_name), "\0") 479 .as_bytes() 480 .as_ptr() as *const ::std::os::raw::c_char, 481 offset: { 482 $crate::assert_field_type!( 483 $struct_name, 484 $field_name, 485 $crate::qom::Owned<$crate::qdev::Clock> 486 ); 487 $crate::offset_of!($struct_name, $field_name) 488 }, 489 size: ::core::mem::size_of::<*const $crate::qdev::Clock>(), 490 flags: $crate::bindings::VMStateFlags( 491 $crate::bindings::VMStateFlags::VMS_STRUCT.0 492 | $crate::bindings::VMStateFlags::VMS_POINTER.0, 493 ), 494 vmsd: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_clock) }, 495 ..$crate::zeroable::Zeroable::ZERO 496 } 497 }}; 498 } 499 500 /// Helper macro to declare a list of 501 /// ([`VMStateField`](`crate::bindings::VMStateField`)) into a static and return 502 /// a pointer to the array of values it created. 503 #[macro_export] 504 macro_rules! vmstate_fields { 505 ($($field:expr),*$(,)*) => {{ 506 static _FIELDS: &[$crate::bindings::VMStateField] = &[ 507 $($field),*, 508 $crate::bindings::VMStateField { 509 flags: $crate::bindings::VMStateFlags::VMS_END, 510 ..$crate::zeroable::Zeroable::ZERO 511 } 512 ]; 513 _FIELDS.as_ptr() 514 }} 515 } 516 517 pub extern "C" fn rust_vms_test_field_exists<T, F: for<'a> FnCall<(&'a T, u8), bool>>( 518 opaque: *mut c_void, 519 version_id: c_int, 520 ) -> bool { 521 let owner: &T = unsafe { &*(opaque.cast::<T>()) }; 522 let version: u8 = version_id.try_into().unwrap(); 523 // SAFETY: the opaque was passed as a reference to `T`. 524 F::call((owner, version)) 525 } 526 527 pub type VMSFieldExistCb = unsafe extern "C" fn( 528 opaque: *mut std::os::raw::c_void, 529 version_id: std::os::raw::c_int, 530 ) -> bool; 531 532 #[doc(alias = "VMSTATE_VALIDATE")] 533 #[macro_export] 534 macro_rules! vmstate_validate { 535 ($struct_name:ty, $test_name:expr, $test_fn:expr $(,)?) => { 536 $crate::bindings::VMStateField { 537 name: ::std::ffi::CStr::as_ptr($test_name), 538 field_exists: { 539 const fn test_cb_builder__< 540 T, 541 F: for<'a> $crate::callbacks::FnCall<(&'a T, u8), bool>, 542 >( 543 _phantom: ::core::marker::PhantomData<F>, 544 ) -> $crate::vmstate::VMSFieldExistCb { 545 let _: () = F::ASSERT_IS_SOME; 546 $crate::vmstate::rust_vms_test_field_exists::<T, F> 547 } 548 549 const fn phantom__<T>(_: &T) -> ::core::marker::PhantomData<T> { 550 ::core::marker::PhantomData 551 } 552 Some(test_cb_builder__::<$struct_name, _>(phantom__(&$test_fn))) 553 }, 554 flags: $crate::bindings::VMStateFlags( 555 $crate::bindings::VMStateFlags::VMS_MUST_EXIST.0 556 | $crate::bindings::VMStateFlags::VMS_ARRAY.0, 557 ), 558 num: 0, // 0 elements: no data, only run test_fn callback 559 ..$crate::zeroable::Zeroable::ZERO 560 } 561 }; 562 } 563 564 /// A transparent wrapper type for the `subsections` field of 565 /// [`VMStateDescription`]. 566 /// 567 /// This is necessary to be able to declare subsection descriptions as statics, 568 /// because the only way to implement `Sync` for a foreign type (and `*const` 569 /// pointers are foreign types in Rust) is to create a wrapper struct and 570 /// `unsafe impl Sync` for it. 571 /// 572 /// This struct is used in the 573 /// [`vm_state_subsections`](crate::vmstate_subsections) macro implementation. 574 #[repr(transparent)] 575 pub struct VMStateSubsectionsWrapper(pub &'static [*const crate::bindings::VMStateDescription]); 576 577 unsafe impl Sync for VMStateSubsectionsWrapper {} 578 579 /// Helper macro to declare a list of subsections ([`VMStateDescription`]) 580 /// into a static and return a pointer to the array of pointers it created. 581 #[macro_export] 582 macro_rules! vmstate_subsections { 583 ($($subsection:expr),*$(,)*) => {{ 584 static _SUBSECTIONS: $crate::vmstate::VMStateSubsectionsWrapper = $crate::vmstate::VMStateSubsectionsWrapper(&[ 585 $({ 586 static _SUBSECTION: $crate::bindings::VMStateDescription = $subsection; 587 ::core::ptr::addr_of!(_SUBSECTION) 588 }),*, 589 ::core::ptr::null() 590 ]); 591 _SUBSECTIONS.0.as_ptr() 592 }} 593 } 594