| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #![allow( |
| 3 | dead_code, |
| 4 | improper_ctypes_definitions, |
| 5 | improper_ctypes, |
| 6 | non_camel_case_types, |
| 7 | non_snake_case, |
| 8 | non_upper_case_globals, |
| 9 | unnecessary_transmutes, |
| 10 | unsafe_op_in_unsafe_fn, |
| 11 | clippy::pedantic, |
| 12 | clippy::restriction, |
| 13 | clippy::style, |
| 14 | clippy::missing_const_for_fn, |
| 15 | clippy::ptr_offset_with_cast, |
| 16 | clippy::useless_transmute, |
| 17 | clippy::missing_safety_doc, |
| 18 | clippy::too_many_arguments |
| 19 | )] |
| 20 | |
| 21 | use common::Zeroable; |
| 22 | use util_sys::{Error, JSONWriter, QEMUFile}; |
| 23 | |
| 24 | #[cfg(MESON)] |
| 25 | include!("bindings.inc.rs"); |
| 26 | |
| 27 | #[cfg(not(MESON))] |
| 28 | include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs")); |
| 29 | |
| 30 | unsafe impl Send for VMStateDescription {} |
| 31 | unsafe impl Sync for VMStateDescription {} |
| 32 | |
| 33 | unsafe impl Send for VMStateField {} |
| 34 | unsafe impl Sync for VMStateField {} |
| 35 | |
| 36 | unsafe impl Send for VMStateInfo {} |
| 37 | unsafe impl Sync for VMStateInfo {} |
| 38 | |
| 39 | // bindgen does not derive Default here |
| 40 | #[allow(clippy::derivable_impls)] |
| 41 | impl Default for VMStateFlags { |
| 42 | fn default() -> Self { |
| 43 | Self(0) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | unsafe impl Zeroable for VMStateFlags {} |
| 48 | unsafe impl Zeroable for VMStateField {} |
| 49 | unsafe impl Zeroable for VMStateDescription {} |
| 50 | unsafe impl Zeroable for VMStateStructMember {} |
| 51 | |
| 52 | // The following higher-level helpers could be in "migration" |
| 53 | // crate when Rust has const trait impl. |
| 54 | |
| 55 | pub trait VMStateFlagsExt { |
| 56 | const VMS_VARRAY_FLAGS: VMStateFlags; |
| 57 | } |
| 58 | |
| 59 | impl VMStateFlagsExt for VMStateFlags { |
| 60 | const VMS_VARRAY_FLAGS: VMStateFlags = VMStateFlags(VMStateFlags::VMS_VARRAY.0); |
| 61 | } |
| 62 | |
| 63 | // Add a couple builder-style methods to VMStateField, allowing |
| 64 | // easy derivation of VMStateField constants from other types. |
| 65 | impl VMStateField { |
| 66 | #[must_use] |
| 67 | pub const fn with_version_id(mut self, version_id: i32) -> Self { |
| 68 | assert!(version_id >= 0); |
| 69 | self.version_id = version_id; |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | #[must_use] |
| 74 | pub const fn with_array_flag(mut self, num: usize) -> Self { |
| 75 | assert!(num <= 0x7FFF_FFFFusize); |
| 76 | assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) == 0); |
| 77 | assert!((self.flags.0 & VMStateFlags::VMS_VARRAY_FLAGS.0) == 0); |
| 78 | if (self.flags.0 & VMStateFlags::VMS_POINTER.0) != 0 { |
| 79 | self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_POINTER.0); |
| 80 | self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY_OF_POINTER.0); |
| 81 | // VMS_ARRAY_OF_POINTER flag stores the size of pointer. |
| 82 | // FIXME: *const, *mut, NonNull and Box<> have the same size as usize. |
| 83 | // Resize if more smart pointers are supported. |
| 84 | self.size = std::mem::size_of::<usize>(); |
| 85 | } |
| 86 | self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_SINGLE.0); |
| 87 | self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY.0); |
| 88 | self.num = num as i32; |
| 89 | self |
| 90 | } |
| 91 | |
| 92 | #[must_use] |
| 93 | pub const fn with_pointer_flag(mut self) -> Self { |
| 94 | assert!((self.flags.0 & VMStateFlags::VMS_POINTER.0) == 0); |
| 95 | self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_POINTER.0); |
| 96 | self |
| 97 | } |
| 98 | |
| 99 | #[must_use] |
| 100 | pub const fn with_varray_flag_unchecked(mut self, flag: VMStateFlags) -> Self { |
| 101 | self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_ARRAY.0); |
| 102 | self.flags = VMStateFlags(self.flags.0 | flag.0); |
| 103 | self.num = 0; // varray uses num_offset instead of num. |
| 104 | self |
| 105 | } |
| 106 | |
| 107 | #[must_use] |
| 108 | #[allow(unused_mut)] |
| 109 | pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> Self { |
| 110 | assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) != 0); |
| 111 | self.with_varray_flag_unchecked(flag) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl VMStateStructMember { |
| 116 | pub const fn new(off: usize, size: usize) -> Self { |
| 117 | Self { |
| 118 | offset: off as u32, |
| 119 | size: size as u8, |
| 120 | } |
| 121 | } |
| 122 | } |