From patchwork Tue Dec 31 00:23:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C4652E77188 for ; Tue, 31 Dec 2024 00:25:32 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3B-0003O9-Hv; Mon, 30 Dec 2024 19:23:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ35-0003MZ-Qe for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ32-0001xU-7o for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1yrvskb2D803b6Tb5PEEJxzJcjtmA0kgwvizPrvOFV8=; b=RhOxg43INTddAfMiHaiSl1aCSlpCL9NyqclKYqqcLbFh4ZaHtiDz4TwuG/43549NE53cCn 888OpJ4YsmXQj1XAS1YKpqzUSUB9FaDFV5yon9Xh4it+u3BdkSs0M/wLpIPQJDpjHkTxA4 zPJcUKkyBhUg3XICfXccF2wHX7+j9MI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-340-iRKEXAKgPQq22oZrcMJoSQ-1; Mon, 30 Dec 2024 19:23:43 -0500 X-MC-Unique: iRKEXAKgPQq22oZrcMJoSQ-1 X-Mimecast-MFC-AGG-ID: iRKEXAKgPQq22oZrcMJoSQ Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385d7611ad3so5570401f8f.2 for ; Mon, 30 Dec 2024 16:23:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604622; x=1736209422; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1yrvskb2D803b6Tb5PEEJxzJcjtmA0kgwvizPrvOFV8=; b=vhPMAamWsbRIyCI8Z+F+0LbVDSHwfIQt2nX4zPj6D5yC5DqetdkCOFHw5gVEQms1wg OV8a0DAQoD6Gze2r3U65NrdJwWBExsUr3NcLFm0NNWcIK/5p2rWNlwMN/6jrZTKYbcyN qFnRTSPSN0K469/q0p8u22+n43N12QPt9/+9tfjKR80+VULKYCFmAQNoY9P5B6ujkNV+ +NEbLBXFr0sr6GVzhORrBioMy4GWERMZSMd1hRk7G8hzprrqTStKuEIbfSHoGPV8Yy9Y +DJmKX7NhN3CP65tig3TOw36kshlJPUPCIMgyPu4kngp39tgct3gv+IU0T5lynvHriE2 3o4g== X-Gm-Message-State: AOJu0YzdZuEtyf2o4k0o8VpDj4isUN6sqM9fB1Ep83//xvFD+gTVIXZU CZ6T+5sJu560DX9g5eiZI3lr6bKK1E4s/4wqWgstKFiACjXraZ/PgyHuVAgJp1tLRe6fbYEa1fV 1QUkAAcjauzLnkNBFT7dKjY5K35Uda93NRMvyFa6HmNI7ulrUgEtOXwxgllw5WJIGUlfIfctdZg 9Imp9r4Ko7PAHkT/8zySv8swxBiqKPcWDLLjh8f6k= X-Gm-Gg: ASbGncsWwnF3GKoKAPFNDiw9Xp4jIcf6bymnFTY0U2lf86xVLC2gv89sEtRHCJwhfdE WQLuFllIv9gXdVlOhzj0b/iPEclGb7un9tU47z/ye7oZfwA/GhaXVGOTc/YvcQnSaBJryvZJ/Lo adnNc2Qjul49EqpLSQu/luIrUx215mDP9EPjI/rixeydVvLuXMb49UiwR/lWrCU8+OIRqw6Yfm9 ir0IvqL5iB7mcxvhrasf7OgA5ZOIgOfpVn64Rs9FXeXJVfuD40v8lo9vmo= X-Received: by 2002:a05:6000:186d:b0:386:1cd3:8a08 with SMTP id ffacd0b85a97d-38a221ed256mr35915088f8f.5.1735604621666; Mon, 30 Dec 2024 16:23:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtl/T8uUgFQg3X/UlgbxZp9ebALLEi3aH1NNheYA7s6VWC6o0Xpyqr53UDpwCeVGVOLm+POg== X-Received: by 2002:a05:6000:186d:b0:386:1cd3:8a08 with SMTP id ffacd0b85a97d-38a221ed256mr35915070f8f.5.1735604621252; Mon, 30 Dec 2024 16:23:41 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8332absm30926426f8f.38.2024.12.30.16.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:40 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 1/9] rust: vmstate: add new type safe implementation Date: Tue, 31 Dec 2024 01:23:28 +0100 Message-ID: <20241231002336.25931-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The existing translation of the C macros for vmstate does not make any attempt to type-check vmstate declarations against the struct, so introduce a new system that computes VMStateField based on the actual struct declaration. Macros do not have full access to the type system, therefore a full implementation of this scheme requires a helper trait to analyze the type and produce a VMStateField from it; a macro "vmstate_of!" accepts arguments similar to "offset_of!" and tricks the compiler into looking up the trait for the right type. The patch introduces not just vmstate_of!, but also the slightly too clever enabling macro call_func_with_field!. The particular trick used here was proposed on the users.rust-lang.org forum, so I take no merit and all the blame. Introduce the trait and some functions to access it; the actual implementation comes later. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/prelude.rs | 2 + rust/qemu-api/src/vmstate.rs | 110 +++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/rust/qemu-api/src/prelude.rs b/rust/qemu-api/src/prelude.rs index 4ea70b9c823..2dc86e19b29 100644 --- a/rust/qemu-api/src/prelude.rs +++ b/rust/qemu-api/src/prelude.rs @@ -18,3 +18,5 @@ pub use crate::qom_isa; pub use crate::sysbus::SysBusDeviceMethods; + +pub use crate::vmstate::VMState; diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index 63c897abcdf..bfcf06e8f1d 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -4,13 +4,111 @@ //! Helper macros to declare migration state for device models. //! -//! Some macros are direct equivalents to the C macros declared in -//! `include/migration/vmstate.h` while -//! [`vmstate_subsections`](crate::vmstate_subsections) and -//! [`vmstate_fields`](crate::vmstate_fields) are meant to be used when -//! declaring a device model state struct. +//! This module includes three families of macros: +//! +//! * [`vmstate_unused!`](crate::vmstate_unused) and +//! [`vmstate_of!`](crate::vmstate_of), which are used to express the +//! migration format for a struct. This is based on the [`VMState`] trait, +//! which is defined by all migrateable types. +//! +//! * helper macros to declare a device model state struct, in particular +//! [`vmstate_subsections`](crate::vmstate_subsections) and +//! [`vmstate_fields`](crate::vmstate_fields). +//! +//! * direct equivalents to the C macros declared in +//! `include/migration/vmstate.h`. These are not type-safe and should not be +//! used if the equivalent functionality is available with `vmstate_of!`. -pub use crate::bindings::VMStateDescription; +use core::marker::PhantomData; + +pub use crate::bindings::{VMStateDescription, VMStateField}; + +/// This macro is used to call a function with a generic argument bound +/// to the type of a field. The function must take a +/// [`PhantomData`]`` argument; `T` is the type of +/// field `$field` in the `$typ` type. +/// +/// # Examples +/// +/// ``` +/// # use qemu_api::call_func_with_field; +/// # use core::marker::PhantomData; +/// const fn size_of_field(_: PhantomData) -> usize { +/// std::mem::size_of::() +/// } +/// +/// struct Foo { +/// x: u16, +/// }; +/// // calls size_of_field::() +/// assert_eq!(call_func_with_field!(size_of_field, Foo, x), 2); +/// ``` +#[macro_export] +macro_rules! call_func_with_field { + ($func:expr, $typ:ty, $($field:tt).+) => { + $func(loop { + #![allow(unreachable_code)] + const fn phantom__(_: &T) -> ::core::marker::PhantomData { ::core::marker::PhantomData } + // Unreachable code is exempt from checks on uninitialized values. + // Use that trick to infer the type of this PhantomData. + break ::core::marker::PhantomData; + break phantom__(&{ let value__: $typ; value__.$($field).+ }); + }) + }; +} + +/// A trait for types that can be included in a device's migration stream. It +/// provides the base contents of a `VMStateField` (minus the name and offset). +/// +/// # Safety +/// +/// The contents of this trait go straight into structs that are parsed by C +/// code and used to introspect into other structs. Be careful. +pub unsafe trait VMState { + /// The base contents of a `VMStateField` (minus the name and offset) for + /// the type that is implementing the trait. + const BASE: VMStateField; +} + +/// Internal utility function to retrieve a type's `VMStateField`; +/// used by [`vmstate_of!`](crate::vmstate_of). +pub const fn vmstate_base(_: PhantomData) -> VMStateField { + T::BASE +} + +/// Return the `VMStateField` for a field of a struct. The field must be +/// visible in the current scope. +/// +/// In order to support other types, the trait `VMState` must be implemented +/// for them. +#[macro_export] +macro_rules! vmstate_of { + ($struct_name:ty, $field_name:ident $(,)?) => { + $crate::bindings::VMStateField { + name: ::core::concat!(::core::stringify!($field_name), "\0") + .as_bytes() + .as_ptr() as *const ::std::os::raw::c_char, + offset: $crate::offset_of!($struct_name, $field_name), + // Compute most of the VMStateField from the type of the field. + ..$crate::call_func_with_field!( + $crate::vmstate::vmstate_base, + $struct_name, + $field_name + ) + } + }; +} + +// Add a couple builder-style methods to VMStateField, allowing +// easy derivation of VMStateField constants from other types. +impl VMStateField { + #[must_use] + pub const fn with_version_id(mut self, version_id: i32) -> Self { + assert!(version_id >= 0); + self.version_id = version_id; + self + } +} #[doc(alias = "VMSTATE_UNUSED_BUFFER")] #[macro_export] From patchwork Tue Dec 31 00:23:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923556 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D4108E77188 for ; Tue, 31 Dec 2024 00:24:50 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3A-0003NC-DI; Mon, 30 Dec 2024 19:23:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ37-0003Md-93 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ33-0001xr-V4 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604629; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3Fu9qvACQbuwh95JzWOQa80TwLOYjoE9iF7rJ1YNRcg=; b=P/ESo/DHgnKXhWwiFCysbLAnviDNFsG0f8zouK+rh8Pq/BDHagE6w+n+S4fXvKP7n41yJ8 OO3T+bzkSrH6QwzXwymBubeqi4qKcXnAtfO57xDTKM78X247oshFkqjcX3k6wQULL/gn1I T9l7onoMZUCKMQS+jdwES9oG5qbjqQo= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-492-fK_g3WJlNVmwHYTyYIMPfg-1; Mon, 30 Dec 2024 19:23:46 -0500 X-MC-Unique: fK_g3WJlNVmwHYTyYIMPfg-1 X-Mimecast-MFC-AGG-ID: fK_g3WJlNVmwHYTyYIMPfg Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38a540ea7c1so726275f8f.1 for ; Mon, 30 Dec 2024 16:23:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604624; x=1736209424; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3Fu9qvACQbuwh95JzWOQa80TwLOYjoE9iF7rJ1YNRcg=; b=szaqNSiX44S3pSkh4rg1FbOmbi46q5TH2cZIqqVATuGBVpmXinZgB540rlVvVEy2Vv +CP/997zGjFTAn6Z+0VrTC6CYmctiNiqlJ+AMedbER3ChLj/DcMpFUNyTNO4KwNhxL+c deuqf45w8Qh+gt/wQWSk5l5wSK1tF+HpMdwJH9fQwyqKVVAXmuLoEjoPCr+R4Fs0SN84 rtsVeh8PFRj1Ps1bQGheIyhqnvlWqnTD8j3RHF/VtDAKu3WNUUbWpT6RGbhwBSbqFnNS DIPnyDaJODdiL2+ljJF/gIDXy8t43D+CmgOlZEANR6KP6Rok0AFpA4Zm2E4g6ye1DMQk NLag== X-Gm-Message-State: AOJu0YzVcKqzqTHmo12EiaKCt88Bkg+EjZhA3Df0U1PnPvzsgBGla47P UtV6EYmmqfBttrQTVDRDGYqV5gkcRGDKZCc5rfGSl2VjIV4MqNZJWqfB600yP9h3G1fbeHDYisQ Ym37kKRZedrf+trD/mXBO1JQKi8zvqEkE+4CmUhICdpCwtUzX6dqkzphcl4hoTu6kIifs+NMuyp m133hF/f0nvuqcejLT4MhxRcOK2OPVssJr1zD5Wr4= X-Gm-Gg: ASbGncvU+jDeTB2h/JtDDwdIBa3FV4UDdo9t9qrqwSvy8SMP9t3gth8beTGKReEEqyG 7DfRMXs7pllKtAw6RwXZbzp9rsz7yXiiMLYJ/kKRkrG760JaSEkXzIWwPhSQ3uwrs/RDyU8YzKl jTNwoZ5YwfS/DQjZ9LCA6PgC5jBhy5OcJppl3ANqKPsEku0N92ohkjJVnT3tRPxYXXUMn+uBd/x KjaGg7km9IQRVcY9kygktssdy+6PFybF2s1ie0bjSl1nGDXA5KeDmYXSWk= X-Received: by 2002:a05:6000:154d:b0:386:3e3c:ef1 with SMTP id ffacd0b85a97d-38a221fe106mr36711157f8f.35.1735604623824; Mon, 30 Dec 2024 16:23:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IHkX0qw5aMnHDsAZCYl2xPPTUYBILaP+RU6Wv1Q5eGQxDredEQO8S1d7G6wVi3LI9tOOJR0fw== X-Received: by 2002:a05:6000:154d:b0:386:3e3c:ef1 with SMTP id ffacd0b85a97d-38a221fe106mr36711138f8f.35.1735604623358; Mon, 30 Dec 2024 16:23:43 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c828f5fsm31710670f8f.8.2024.12.30.16.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:42 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 2/9] rust: vmstate: implement VMState for non-leaf types Date: Tue, 31 Dec 2024 01:23:29 +0100 Message-ID: <20241231002336.25931-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Arrays, pointers and cells use a VMStateField that is based on that for the inner type. The implementation therefore delegates to the VMState implementation of the inner type. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 66 +++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index bfcf06e8f1d..e20f27b172b 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -19,8 +19,9 @@ //! `include/migration/vmstate.h`. These are not type-safe and should not be //! used if the equivalent functionality is available with `vmstate_of!`. -use core::marker::PhantomData; +use core::{marker::PhantomData, mem, ptr::NonNull}; +use crate::bindings::VMStateFlags; pub use crate::bindings::{VMStateDescription, VMStateField}; /// This macro is used to call a function with a generic argument bound @@ -108,6 +109,69 @@ pub const fn with_version_id(mut self, version_id: i32) -> Self { self.version_id = version_id; self } + + #[must_use] + pub const fn with_array_flag(mut self, num: usize) -> Self { + assert!(num <= 0x7FFF_FFFFusize); + assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) == 0); + if (self.flags.0 & VMStateFlags::VMS_POINTER.0) != 0 { + self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_POINTER.0); + self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY_OF_POINTER.0); + } + self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_SINGLE.0); + self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_ARRAY.0); + self.num = num as i32; + self + } + + #[must_use] + pub const fn with_pointer_flag(mut self) -> Self { + assert!((self.flags.0 & VMStateFlags::VMS_POINTER.0) == 0); + self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_POINTER.0); + self + } +} + +// Transparent wrappers: just use the internal type + +macro_rules! impl_vmstate_transparent { + ($type:ty where $base:tt: VMState $($where:tt)*) => { + unsafe impl<$base> VMState for $type where $base: VMState $($where)* { + const BASE: VMStateField = VMStateField { + size: mem::size_of::<$type>(), + ..<$base as VMState>::BASE + }; + } + }; +} + +impl_vmstate_transparent!(std::cell::Cell where T: VMState); +impl_vmstate_transparent!(std::cell::UnsafeCell where T: VMState); +impl_vmstate_transparent!(crate::cell::BqlCell where T: VMState); +impl_vmstate_transparent!(crate::cell::BqlRefCell where T: VMState); + +// Pointer types using the underlying type's VMState plus VMS_POINTER + +macro_rules! impl_vmstate_pointer { + ($type:ty where $base:tt: VMState $($where:tt)*) => { + unsafe impl<$base> VMState for $type where $base: VMState $($where)* { + const SCALAR_TYPE: VMStateFieldType = ::SCALAR_TYPE; + const BASE: VMStateField = <$base as VMState>::BASE.with_pointer_flag(); + } + }; +} + +impl_vmstate_pointer!(*const T where T: VMState); +impl_vmstate_pointer!(*mut T where T: VMState); +impl_vmstate_pointer!(NonNull where T: VMState); +impl_vmstate_pointer!(Option> where T: VMState); + +// Arrays using the underlying type's VMState plus +// VMS_ARRAY/VMS_ARRAY_OF_POINTER + +unsafe impl VMState for [T; N] { + const SCALAR_TYPE: VMStateFieldType = ::SCALAR_TYPE; + const BASE: VMStateField = ::BASE.with_array_flag(N); } #[doc(alias = "VMSTATE_UNUSED_BUFFER")] From patchwork Tue Dec 31 00:23:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 39BD7E77194 for ; Tue, 31 Dec 2024 00:25:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3B-0003Nx-BZ; Mon, 30 Dec 2024 19:23:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ38-0003N0-Iu for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:55 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ35-0001y5-I4 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604630; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V8eqX+r29sPapJhn/vLbtpBrgqJq/aUh19hK4P3NE2Y=; b=EYjWqiH05lJnIzFs7CD4vjU92c08+9iD3KxCucsTmQ22PJxaHmlQoLRlhfxqn6XVuEv+95 /RcD3wGZIQUffoweYnLeCzr5b7ePxhIEmSlh6jqzcxbu4UMpIVg66wOEiLSWEt+pm/mUAC c4T3U41U9On8aY936yBPV3PXlzYyzaQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-436-htVhSm9mNuONmhrfJZJJeQ-1; Mon, 30 Dec 2024 19:23:48 -0500 X-MC-Unique: htVhSm9mNuONmhrfJZJJeQ-1 X-Mimecast-MFC-AGG-ID: htVhSm9mNuONmhrfJZJJeQ Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-43623bf2a83so70814715e9.0 for ; Mon, 30 Dec 2024 16:23:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604626; x=1736209426; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V8eqX+r29sPapJhn/vLbtpBrgqJq/aUh19hK4P3NE2Y=; b=vXCK1Aj0dbFFIQuXneTE7G4SbVe5hMhnmx9SmxubTrDzD93JFeWb08WLjV5kVLW9N+ Uph3G2sr/OP9Ps5+kZu91orspIiU0Ev+6JAXVz9s6Gr0BE3QGPsMk5Bol25tXq2vMwEa 4I2tJVO3z9Wl0EEFvxycOfxDCjmAGP85Z62qXwn66Q0GIu8beiApHejCy/hCjHvoF6uM QLOiP6T+fqY2tDgF4PJpaoIdRuXOtbqft0OEBPWQuvy0dX0UvGf2t14cS7PFt09WzJ9j tRtwjd/l+dxdt/0g9mKCtSWerkEre/mLItOdDP8vVepmZ+31UWW2b+MshOHthqSEqhRR P1Lg== X-Gm-Message-State: AOJu0YwmUz3vjZv053cJlNGIgS+KlncXXO1ExyJr2FnbeWLj/gKA63ox XBbYNJgeXGQZrvI+7V1LrHgM5/opAIc2FzMe9tM14IuYg69JLQT0fxPFhsn9BOIxVMQtDezWog6 tLMEnxFUxW40xM13b6dx5Wpm+k0J5YuqyiQ7krw6FhlnC10rSLPYCG5llWqf1hMHBlg4ulQqdkv z/ctdT7aT26oMV8TMwXUlY7dlO36EO2rTEp8JYDqQ= X-Gm-Gg: ASbGncs/NWNs/Nojs2tnUjzHanjJmCs7CdXHjv/r1VXO9CHw3NOxToblvUwDig37L4m pz39b0lZfoptFe5xaclAeUQLZDf+xeVqZ+E1bI48mvvyScA2kMBv2CPxbzsLOM/g1F63y37svC8 TY7bvr77Lyf7MJaoYrcIXyKIGPxsD00e8iuoLVC/zRV09i2AOEZPe8TSiz/G+EC9mLz4uLVyoNA xxepBcShAts5wkDS64JsHCBLlE1qGZPOwS4bO9eCiwYg3sw8J5K1atbjqw= X-Received: by 2002:a5d:59ad:0:b0:386:32ca:aa12 with SMTP id ffacd0b85a97d-38a223feed2mr24894073f8f.49.1735604626472; Mon, 30 Dec 2024 16:23:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFZv9t08NRGO27ZZdgCK2GIiscRqxj0W9Mj/2mSZkxJaFIPLTAS5vXRDNETy4qIatVFZKL1uQ== X-Received: by 2002:a5d:59ad:0:b0:386:32ca:aa12 with SMTP id ffacd0b85a97d-38a223feed2mr24894063f8f.49.1735604626072; Mon, 30 Dec 2024 16:23:46 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e8asm30944218f8f.37.2024.12.30.16.23.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:44 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 3/9] rust: vmstate: add varray support to vmstate_of! Date: Tue, 31 Dec 2024 01:23:30 +0100 Message-ID: <20241231002336.25931-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Untested... Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 45 +++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index e20f27b172b..079c19c687b 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -69,6 +69,15 @@ pub unsafe trait VMState { /// The base contents of a `VMStateField` (minus the name and offset) for /// the type that is implementing the trait. const BASE: VMStateField; + + /// A flag that is added to another field's `VMStateField` to specify the + /// length's type in a variable-sized array. If this is not a supported + /// type for the length (i.e. if it is not `u8`, `u16`, `u32`), using it + /// in a call to [`vmstate_of!`](crate::vmstate_of) will cause a + /// compile-time error. + const VARRAY_FLAG: VMStateFlags = { + panic!("invalid type for variable-sized array"); + }; } /// Internal utility function to retrieve a type's `VMStateField`; @@ -77,6 +86,13 @@ pub const fn vmstate_base(_: PhantomData) -> VMStateField { T::BASE } +/// Internal utility function to retrieve a type's `VMStateFlags` when it +/// is used as the element count of a `VMSTATE_VARRAY`; used by +/// [`vmstate_of!`](crate::vmstate_of). +pub const fn vmstate_varray_flag(_: PhantomData) -> VMStateField { + T::BASE +} + /// Return the `VMStateField` for a field of a struct. The field must be /// visible in the current scope. /// @@ -84,18 +100,24 @@ pub const fn vmstate_base(_: PhantomData) -> VMStateField { /// for them. #[macro_export] macro_rules! vmstate_of { - ($struct_name:ty, $field_name:ident $(,)?) => { + ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])? $(,)?) => { $crate::bindings::VMStateField { name: ::core::concat!(::core::stringify!($field_name), "\0") .as_bytes() .as_ptr() as *const ::std::os::raw::c_char, offset: $crate::offset_of!($struct_name, $field_name), - // Compute most of the VMStateField from the type of the field. + $(.num_offset: $crate::offset_of!($struct_name, $num),)? + // The calls to `call_func_with_field!` are the magic that + // computes most of the VMStateField from the type of the field. ..$crate::call_func_with_field!( $crate::vmstate::vmstate_base, $struct_name, $field_name - ) + )$(.with_varray_flag($crate::call_func_with_field!( + $crate::vmstate::vmstate_varray_flag, + $struct_name, + $num)) + $(.with_varray_multiply($factor))?)? } }; } @@ -130,6 +152,22 @@ pub const fn with_pointer_flag(mut self) -> Self { self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_POINTER.0); self } + + #[must_use] + pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> VMStateField { + assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) != 0); + self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_ARRAY.0); + self.flags = VMStateFlags(self.flags.0 | flag.0); + self + } + + #[must_use] + pub const fn with_varray_multiply(mut self, num: u32) -> VMStateField { + assert!(num <= 0x7FFF_FFFFu32); + self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_MULTIPLY_ELEMENTS.0); + self.num = num as i32; + self + } } // Transparent wrappers: just use the internal type @@ -141,6 +179,7 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* { size: mem::size_of::<$type>(), ..<$base as VMState>::BASE }; + const VARRAY_FLAG: VMStateFlags = <$base as VMState>::VARRAY_FLAG; } }; } From patchwork Tue Dec 31 00:23:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923564 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 55287E7718F for ; Tue, 31 Dec 2024 00:25:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3F-0003RN-QL; Mon, 30 Dec 2024 19:24:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3D-0003PT-RY for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3A-0001yX-AZ for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BLRyhLZCkB8ac2Pq1tt07simH7oDZNcXTrRpW5qQGiA=; b=bszdViMBDzZfu5ndFMLcR9yqbZ2dUCxyH126w09xE2p/ifPnZJyb4ZGmgyVsOoLUzJ1oBq iBJaY4tuyG80o5Kq2j76Li7aDrHrVJRbFv0BEupc4FY5Q62cdpSdU8v3PgduqEFNwxcL0k beVoFnaPNRjLcFcaiEqKwA/u3ZEc4QI= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595-hpbGKioZPLWVgFOsBG_b8A-1; Mon, 30 Dec 2024 19:23:51 -0500 X-MC-Unique: hpbGKioZPLWVgFOsBG_b8A-1 X-Mimecast-MFC-AGG-ID: hpbGKioZPLWVgFOsBG_b8A Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385fdff9db5so1289758f8f.0 for ; Mon, 30 Dec 2024 16:23:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604629; x=1736209429; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BLRyhLZCkB8ac2Pq1tt07simH7oDZNcXTrRpW5qQGiA=; b=LHG4daIQr5NeEJUmLvLRgB/EVhfhDmGNpF/dTiWfNQTU7U10HG9r62rU6qIB4XLsyi 60qjpfBlbvyHO+aJtQFfVGDUpwFuis7fEFlahJZG9GOOEF9zt6CtxTVDnOO6ekGIesSP Epf4quUfmzuK1te4yWwpMGHU6g/Xb4cagLVT+0dMz1nSvhoVjtmlJxpLv+rBrx+CiJnk jIXgGy5U4hjedGi+piKMafYnq/eaXLqQ5pUlWvGgOVQ8pmDGutd86wISsWeSh5jfGHsD zGgbmvLMV0FNVynBg9krF2GIhHWuqX1oPideD29EfUfZJSCBByQ3gvrhtMDt3hnqgbCx n4NQ== X-Gm-Message-State: AOJu0Yz89ecXyu3SUDFiX1nR6n/72L73nk9gL0di0Y+fJT9xSOYmFPK1 ZQUGBoJtKyg9LHHEmSQWGuW4lEkAEBJD4l7pEhZnFZai3MShzcWZRgJKLDqI8Im+sm9ut2fOXwN +CF15QseJf/bMJoEwboP70Le2tcJaUesg6rE3uzKZ4VMS5/3uYSiTSu7NWmsYknY+2jx/FbDt7w WmL7BAP1DLPa1kUXLx1ZXHTwn+38aJk2CtICM2QOw= X-Gm-Gg: ASbGnct1YH9UWhjmacmq3iy9V3EWhcREkqFqcR0O9v6jZqQMyR9sOIdJtQrOXtVFRxX 1IYBd2ZS14n67I3/CfJxLbPye7aiOUgH/7j1sBkQ1lIjhvUML+LUhCZjICs/QM1ySy2gshqCaX+ 4bgse9kSYnG7MS87vrH3xgY/P9qkp3hKjmhnWxrl93u/l1qAqw3KsqeyFqXhF4iV8uDyN0z6XbD IDt/GpwYuifQuk+67tw6k1pa0mA3dlDru5PIAINse+OXKOblzmwGkLpqC4= X-Received: by 2002:adf:a19b:0:b0:38a:418e:21c7 with SMTP id ffacd0b85a97d-38a418e22c3mr14300670f8f.53.1735604629288; Mon, 30 Dec 2024 16:23:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFPSSd6h75+IGdiryn3rjZa15qMs3HnUtJ0l0Tv4kKFN5rJ1pSZQBZ70HWW88GqUGQEv/F1Pw== X-Received: by 2002:adf:a19b:0:b0:38a:418e:21c7 with SMTP id ffacd0b85a97d-38a418e22c3mr14300657f8f.53.1735604628829; Mon, 30 Dec 2024 16:23:48 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c89e26csm31849972f8f.78.2024.12.30.16.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:47 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 4/9] rust: vmstate: implement Zeroable for VMStateField Date: Tue, 31 Dec 2024 01:23:31 +0100 Message-ID: <20241231002336.25931-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This shortens a bit the constants. Do not bother using it in the vmstate macros since most of them will go away soon. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 18 +++--------------- rust/qemu-api/src/zeroable.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index 079c19c687b..49d0a3c31d4 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -21,8 +21,8 @@ use core::{marker::PhantomData, mem, ptr::NonNull}; -use crate::bindings::VMStateFlags; pub use crate::bindings::{VMStateDescription, VMStateField}; +use crate::bindings::VMStateFlags; /// This macro is used to call a function with a generic argument bound /// to the type of a field. The function must take a @@ -488,20 +488,8 @@ macro_rules! vmstate_fields { static _FIELDS: &[$crate::bindings::VMStateField] = &[ $($field),*, $crate::bindings::VMStateField { - name: ::core::ptr::null(), - err_hint: ::core::ptr::null(), - offset: 0, - size: 0, - start: 0, - num: 0, - num_offset: 0, - size_offset: 0, - info: ::core::ptr::null(), - flags: VMStateFlags::VMS_END, - vmsd: ::core::ptr::null(), - version_id: 0, - struct_version_id: 0, - field_exists: None, + flags: $crate::bindings::VMStateFlags::VMS_END, + ..$crate::zeroable::Zeroable::ZERO } ]; _FIELDS.as_ptr() diff --git a/rust/qemu-api/src/zeroable.rs b/rust/qemu-api/src/zeroable.rs index 6125aeed8b4..57cac96de06 100644 --- a/rust/qemu-api/src/zeroable.rs +++ b/rust/qemu-api/src/zeroable.rs @@ -49,6 +49,37 @@ unsafe impl Zeroable for crate::bindings::Property { }; } +// bindgen does not derive Default here +#[allow(clippy::derivable_impls)] +impl Default for crate::bindings::VMStateFlags { + fn default() -> Self { + Self(0) + } +} + +unsafe impl Zeroable for crate::bindings::VMStateFlags { + const ZERO: Self = Self(0); +} + +unsafe impl Zeroable for crate::bindings::VMStateField { + const ZERO: Self = Self { + name: ptr::null(), + err_hint: ptr::null(), + offset: 0, + size: 0, + start: 0, + num: 0, + num_offset: 0, + size_offset: 0, + info: ptr::null(), + flags: Zeroable::ZERO, + vmsd: ptr::null(), + version_id: 0, + struct_version_id: 0, + field_exists: None, + }; +} + unsafe impl Zeroable for crate::bindings::VMStateDescription { const ZERO: Self = Self { name: ptr::null(), From patchwork Tue Dec 31 00:23:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1A28AE77194 for ; Tue, 31 Dec 2024 00:25:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3E-0003R1-M9; Mon, 30 Dec 2024 19:24:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3D-0003Pa-24 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3A-0001z0-E4 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:23:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DQ9f8TEIbiidaBdHdliac2j4M4b1C9RXDFq4BMn1MtE=; b=Musl+y6YGMJvPh61snKWpXiTBwMJv5vP1XOQVvKGmXMTQ56i2k4k5V5QvXNrJjbIfLTTu4 HtzVL1KFGwQUdAukdT0e4+WSLkwZuVW0zLA9ouiPLbJdHOj7sW8FThMIVg9vHahq8Sjo/t YqQuVg5WP+Zy2/qCU0URuSDvlYyR0lo= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-661-rzZQWKntM6uZ2kdcR408mg-1; Mon, 30 Dec 2024 19:23:54 -0500 X-MC-Unique: rzZQWKntM6uZ2kdcR408mg-1 X-Mimecast-MFC-AGG-ID: rzZQWKntM6uZ2kdcR408mg Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43635895374so47767195e9.0 for ; Mon, 30 Dec 2024 16:23:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604632; x=1736209432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DQ9f8TEIbiidaBdHdliac2j4M4b1C9RXDFq4BMn1MtE=; b=CNn890n07pEa3k7GDnfYcspzi+hdXmcIy2hQT39Apedg05WjM8+ft3XsjieyM0j5ag yOkjQJACkFlWTEimM2BJ2jkN5S/sSPbfbwK/QjtnSmcZ8yb8sQyeaxC3+xzK1BPSAl8i c38xuXYvMLV9su8SqsQgPtE/cIX1gZ9f+mRgFpa8JMdJVoS48mqZTNGWQ0CbmIhWCAbr Qa7UpNYd0H/r57pS7yFa6KQlJMp0dDgTIwgLJliOHcpzX1ZA0aMcT/dy4ErDYfT1jMRV jTcxL2MFfMoDZBRXRam3LN05ipK81i7Z3yf8qiuy5yYJv3tU2ei2iGKyiDzwfae03Hgl 3UdQ== X-Gm-Message-State: AOJu0Ywc54kcTrT0TUT3+Y1v/63m9niKiUn2Nhg90xChQj+fz+gwoXvJ 9lCi0w5yY2hTCb8VB+CTafASwuFcXamkAzh0KybJCsq4s884YyZHSs5udNNRteRFBJkPYnn/5DX 6iy5H/ulmiK6UQSlqKx4iWvLzOi8K+dKSKY1dYh8fpShHg2PiH0H9jFjnAO1o0LKRynIRtA5aYv QZPWheaVNBcLWm8OSkAtVq0/HwcKAqg/eZwk74VU4= X-Gm-Gg: ASbGncsvHzO3Ai97LuqLaz0F/fClamL1T9LULID/CxZWINHs0MFpdzs9xW4oK8VpTwm +IDQ6UX3nnfWLr3dJ9mtNxGZ0Foil/fHg34WtruzXJL9w02wVbWJmQh4vEcAsEYl+ZDT9rCvfzt 4UZP1XUl1UqMmVZSeWMcDg7aZajLbKl4oKv0+kc9BZcsYMNSXjBQ48UIVu9IXMecgDB2HJW0xQy hjlRMGr7iiZSkbWq0eghJl93rKvuUJ2pgQCu3pgvto0RTEGpGbxqJClTjA= X-Received: by 2002:a05:6000:1569:b0:386:3327:4f21 with SMTP id ffacd0b85a97d-38a22a648f7mr27522049f8f.27.1735604631631; Mon, 30 Dec 2024 16:23:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXRkrAOdJIh9dRizoOccNa1BQJwzUhNZq9xlXOOZ6F4+nA5NEw9V7RQsz/zLaw5QqEfvQ7yQ== X-Received: by 2002:a05:6000:1569:b0:386:3327:4f21 with SMTP id ffacd0b85a97d-38a22a648f7mr27522034f8f.27.1735604631244; Mon, 30 Dec 2024 16:23:51 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c89e2f9sm32122371f8f.81.2024.12.30.16.23.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:49 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 5/9] rust: vmstate: implement VMState for scalar types Date: Tue, 31 Dec 2024 01:23:32 +0100 Message-ID: <20241231002336.25931-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Scalar types are those that have their own VMStateInfo. This poses a problem in that references to VMStateInfo can only be included in associated consts starting with Rust 1.83.0, when the const_refs_static was stabilized. Removing the requirement is done by placing a limited list of VMStateInfos in an enum, and going from enum to &VMStateInfo only when building the VMStateField. The same thing cannot be done with VMS_STRUCT because the set of VMStateDescriptions extends to structs defined by the devices. Therefore, structs and cells cannot yet use vmstate_of!. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 125 ++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index 49d0a3c31d4..edd0cbff162 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -21,8 +21,11 @@ use core::{marker::PhantomData, mem, ptr::NonNull}; -pub use crate::bindings::{VMStateDescription, VMStateField}; use crate::bindings::VMStateFlags; +pub use crate::{ + bindings::{self, VMStateDescription, VMStateField}, + zeroable::Zeroable, +}; /// This macro is used to call a function with a generic argument bound /// to the type of a field. The function must take a @@ -58,6 +61,70 @@ const fn phantom__(_: &T) -> ::core::marker::PhantomData { ::core::marker: }; } +/// Workaround for lack of `const_refs_static`: references to global variables +/// can be included in a `static`, but not in a `const`; unfortunately, this +/// is exactly what would go in the `VMStateField`'s `info` member. +/// +/// This enum contains the contents of the `VMStateField`'s `info` member, +/// but as an `enum` instead of a pointer. +#[allow(non_camel_case_types)] +pub enum VMStateFieldType { + null, + vmstate_info_bool, + vmstate_info_int8, + vmstate_info_int16, + vmstate_info_int32, + vmstate_info_int64, + vmstate_info_uint8, + vmstate_info_uint16, + vmstate_info_uint32, + vmstate_info_uint64, + vmstate_info_timer, +} + +/// Workaround for lack of `const_refs_static`. Converts a `VMStateFieldType` +/// to a `*const VMStateInfo`, for inclusion in a `VMStateField`. +#[macro_export] +macro_rules! info_enum_to_ref { + ($e:expr) => { + unsafe { + match $e { + $crate::vmstate::VMStateFieldType::null => ::core::ptr::null(), + $crate::vmstate::VMStateFieldType::vmstate_info_bool => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_bool) + } + $crate::vmstate::VMStateFieldType::vmstate_info_int8 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_int8) + } + $crate::vmstate::VMStateFieldType::vmstate_info_int16 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_int16) + } + $crate::vmstate::VMStateFieldType::vmstate_info_int32 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_int32) + } + $crate::vmstate::VMStateFieldType::vmstate_info_int64 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_int64) + } + $crate::vmstate::VMStateFieldType::vmstate_info_uint8 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint8) + } + $crate::vmstate::VMStateFieldType::vmstate_info_uint16 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint16) + } + $crate::vmstate::VMStateFieldType::vmstate_info_uint32 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint32) + } + $crate::vmstate::VMStateFieldType::vmstate_info_uint64 => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint64) + } + $crate::vmstate::VMStateFieldType::vmstate_info_timer => { + ::core::ptr::addr_of!($crate::bindings::vmstate_info_timer) + } + } + } + }; +} + /// A trait for types that can be included in a device's migration stream. It /// provides the base contents of a `VMStateField` (minus the name and offset). /// @@ -66,6 +133,12 @@ const fn phantom__(_: &T) -> ::core::marker::PhantomData { ::core::marker: /// The contents of this trait go straight into structs that are parsed by C /// code and used to introspect into other structs. Be careful. pub unsafe trait VMState { + /// The `info` member of a `VMStateField` is a pointer and as such cannot + /// yet be included in the [`BASE`](VMState::BASE) associated constant; + /// this is only allowed by Rust 1.83.0 and newer. For now, include the + /// member as an enum which is stored in a separate constant. + const SCALAR_TYPE: VMStateFieldType = VMStateFieldType::null; + /// The base contents of a `VMStateField` (minus the name and offset) for /// the type that is implementing the trait. const BASE: VMStateField; @@ -80,6 +153,12 @@ pub unsafe trait VMState { }; } +/// Internal utility function to retrieve a type's `VMStateFieldType`; +/// used by [`vmstate_of!`](crate::vmstate_of). +pub const fn vmstate_scalar_type(_: PhantomData) -> VMStateFieldType { + T::SCALAR_TYPE +} + /// Internal utility function to retrieve a type's `VMStateField`; /// used by [`vmstate_of!`](crate::vmstate_of). pub const fn vmstate_base(_: PhantomData) -> VMStateField { @@ -96,11 +175,20 @@ pub const fn vmstate_varray_flag(_: PhantomData) -> VMStateField /// Return the `VMStateField` for a field of a struct. The field must be /// visible in the current scope. /// +/// Only a limited set of types is supported out of the box: +/// * scalar types (integer and `bool`) +/// * the C struct `QEMUTimer` +/// * a transparent wrapper for any of the above (`Cell`, `UnsafeCell`, +/// [`BqlCell`](crate::cell::BqlCell), [`BqlRefCell`](crate::cell::BqlRefCell) +/// * a raw pointer to any of the above +/// * a `NonNull` pointer to any of the above, possibly wrapped with `Option` +/// * an array of any of the above +/// /// In order to support other types, the trait `VMState` must be implemented /// for them. #[macro_export] macro_rules! vmstate_of { - ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])? $(,)?) => { + ($struct_name:ty, $field_name:ident $([0 .. $num:tt $(* $factor:expr)?])? $(,)?) => { $crate::bindings::VMStateField { name: ::core::concat!(::core::stringify!($field_name), "\0") .as_bytes() @@ -109,6 +197,11 @@ macro_rules! vmstate_of { $(.num_offset: $crate::offset_of!($struct_name, $num),)? // The calls to `call_func_with_field!` are the magic that // computes most of the VMStateField from the type of the field. + info: $crate::info_enum_to_ref!($crate::call_func_with_field!( + $crate::vmstate::vmstate_scalar_type, + $struct_name, + $field_name + )), ..$crate::call_func_with_field!( $crate::vmstate::vmstate_base, $struct_name, @@ -175,6 +268,7 @@ pub const fn with_varray_multiply(mut self, num: u32) -> VMStateField { macro_rules! impl_vmstate_transparent { ($type:ty where $base:tt: VMState $($where:tt)*) => { unsafe impl<$base> VMState for $type where $base: VMState $($where)* { + const SCALAR_TYPE: VMStateFieldType = <$base as VMState>::SCALAR_TYPE; const BASE: VMStateField = VMStateField { size: mem::size_of::<$type>(), ..<$base as VMState>::BASE @@ -189,6 +283,33 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* { impl_vmstate_transparent!(crate::cell::BqlCell where T: VMState); impl_vmstate_transparent!(crate::cell::BqlRefCell where T: VMState); +// Scalar types using predefined VMStateInfos + +macro_rules! impl_vmstate_scalar { + ($info:ident, $type:ty$(, $varray_flag:ident)?) => { + unsafe impl VMState for $type { + const SCALAR_TYPE: VMStateFieldType = VMStateFieldType::$info; + const BASE: VMStateField = VMStateField { + size: mem::size_of::<$type>(), + flags: VMStateFlags::VMS_SINGLE, + ..Zeroable::ZERO + }; + $(const VARRAY_FLAG: VMStateFlags = VMStateFlags::$varray_flag;)? + } + }; +} + +impl_vmstate_scalar!(vmstate_info_bool, bool); +impl_vmstate_scalar!(vmstate_info_int8, i8); +impl_vmstate_scalar!(vmstate_info_int16, i16); +impl_vmstate_scalar!(vmstate_info_int32, i32); +impl_vmstate_scalar!(vmstate_info_int64, i64); +impl_vmstate_scalar!(vmstate_info_uint8, u8, VMS_VARRAY_UINT8); +impl_vmstate_scalar!(vmstate_info_uint16, u16, VMS_VARRAY_UINT16); +impl_vmstate_scalar!(vmstate_info_uint32, u32, VMS_VARRAY_UINT32); +impl_vmstate_scalar!(vmstate_info_uint64, u64); +impl_vmstate_scalar!(vmstate_info_timer, bindings::QEMUTimer); + // Pointer types using the underlying type's VMState plus VMS_POINTER macro_rules! impl_vmstate_pointer { From patchwork Tue Dec 31 00:23:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923558 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79370E77188 for ; Tue, 31 Dec 2024 00:25:16 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3K-0003TN-5e; Mon, 30 Dec 2024 19:24:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3H-0003Ru-Eg for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:03 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3F-0001zl-QM for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VaU78OtHlJWbowYJ8XZBMHDaIpSDaRcsvAM49uU4qxs=; b=BbyD1s6i7GsWNfpf/nUP5eVLFbRDUKhQHwNpgEaqxKlraj8uFtaGliUISVVhWGTtpMjquU nZxdecL9XICb+nXf6FJPZLM5+mua7+2yDUWI7kJCAg1Qy+HINwMmRZqG1+8gcyyzq8bJo7 vb0jvqefi2bnhVqlfOMtfZx7PQFSyLo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-3z9yWGs_Mp6SwHgJgH1E9w-1; Mon, 30 Dec 2024 19:23:57 -0500 X-MC-Unique: 3z9yWGs_Mp6SwHgJgH1E9w-1 X-Mimecast-MFC-AGG-ID: 3z9yWGs_Mp6SwHgJgH1E9w Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4359206e1e4so75214175e9.2 for ; Mon, 30 Dec 2024 16:23:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604635; x=1736209435; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VaU78OtHlJWbowYJ8XZBMHDaIpSDaRcsvAM49uU4qxs=; b=K0Adh/h/KSphLR55kz9sni1m91yJ7w4Lm+twcD6VFuqvkymIXhe6EXBx2djhMTcSQ1 qpEJTJgNWxG9xCgL2n+JAW2/rer9ReAhsJQu2/bJ+gpmFTziPFDCrZbtVgd3o+W7lU7P 7Ph/Mthq7lifcQS1q+u68aoJGMtaeA6wHh9yNtDFrR7mN42siK4wrGRL2VUBp93alvhT a18qGa31OLwalUXV+9849kvuwo1Zwu3kPCUERoo59ewTnFGcQlwHMNfkKTL0kpeZBgRc 8N72aSLysa1mUegvSh50svTVa9DV7joLChi18GDTBHO+9MqouDUEB6Q0McW7+sOFcchS 4wAQ== X-Gm-Message-State: AOJu0YwDsqSt36PWHAEaP/OX5T4q7PXS/R7sPGA7yobL2991Ttnr2Sk9 s2DXbXLqBTkvyIh3XNhL0k9TYJcTydFGcxLyFa3E94L1R6/dMHPJe/b48ZG1e+vdjQG3fMqwAni iA+dHcbVbWpBQgFn0mcx+FxwLC9pwgnqQAhQkCbLLKgtQaPllPgEUuuyxC/11eqS7YjA8c1g4BZ eBemYvAA5CJHOxN0NjnksBeBWjunhnoacPcbwFhOU= X-Gm-Gg: ASbGncuY6Hg3xwrD5DdR6rj+VTPJm8i7w4+dEseaw4GtGQz3rkUo2I31tcV6ejelOLe vmqo9GF0t/VjX2in+8TTW4EYsqQUFk2cCn+etA7XMpManSRC2+oORmu9XLN+99DGNhOywKEj0Lu BFRb6AJtD0qKWyx9S0HEWeZKHlCSQ7w6wmkoltUrImKMJftnKcwuThDrjj0ewpKjpHUlcayc8H6 QeHmdUakkcQMfchz5cgrZrReVROUTyiqBFD13pIGQ+MNr4LOLTaF8sopuQ= X-Received: by 2002:a05:600c:474d:b0:436:18d0:aa6e with SMTP id 5b1f17b1804b1-4366835c14fmr318088355e9.5.1735604635030; Mon, 30 Dec 2024 16:23:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IGv6MHM4uX/eSI1Yj4SlGnN6UDDzvj8TAuWNjWh6ZA6/Px/vud18hBBxDwj2WuqBsSw1WX/rA== X-Received: by 2002:a05:600c:474d:b0:436:18d0:aa6e with SMTP id 5b1f17b1804b1-4366835c14fmr318088235e9.5.1735604634600; Mon, 30 Dec 2024 16:23:54 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436612008b1sm367949045e9.15.2024.12.30.16.23.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:52 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 6/9] rust: vmstate: add public utility macros to implement VMState Date: Tue, 31 Dec 2024 01:23:33 +0100 Message-ID: <20241231002336.25931-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 61 ++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index edd0cbff162..b59a4b66339 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -4,13 +4,18 @@ //! Helper macros to declare migration state for device models. //! -//! This module includes three families of macros: +//! This module includes four families of macros: //! //! * [`vmstate_unused!`](crate::vmstate_unused) and //! [`vmstate_of!`](crate::vmstate_of), which are used to express the //! migration format for a struct. This is based on the [`VMState`] trait, //! which is defined by all migrateable types. //! +//! * [`impl_vmstate_forward`](crate::impl_vmstate_forward) and +//! [`impl_vmstate_bitsized`](crate::impl_vmstate_bitsized), which help with +//! the definition of the [`VMState`] trait (respectively for transparent +//! structs and for `bilge`-defined types) +//! //! * helper macros to declare a device model state struct, in particular //! [`vmstate_subsections`](crate::vmstate_subsections) and //! [`vmstate_fields`](crate::vmstate_fields). @@ -131,7 +136,9 @@ macro_rules! info_enum_to_ref { /// # Safety /// /// The contents of this trait go straight into structs that are parsed by C -/// code and used to introspect into other structs. Be careful. +/// code and used to introspect into other structs. Generally, you don't need +/// to implement it except via macros that do it for you, such as +/// `impl_vmstate_bitsized!`. pub unsafe trait VMState { /// The `info` member of a `VMStateField` is a pointer and as such cannot /// yet be included in the [`BASE`](VMState::BASE) associated constant; @@ -185,7 +192,9 @@ pub const fn vmstate_varray_flag(_: PhantomData) -> VMStateField /// * an array of any of the above /// /// In order to support other types, the trait `VMState` must be implemented -/// for them. +/// for them. The macros +/// [`impl_vmstate_bitsized!`](crate::impl_vmstate_bitsized) +/// and [`impl_vmstate_forward!`](crate::impl_vmstate_forward) help with this. #[macro_export] macro_rules! vmstate_of { ($struct_name:ty, $field_name:ident $([0 .. $num:tt $(* $factor:expr)?])? $(,)?) => { @@ -263,6 +272,32 @@ pub const fn with_varray_multiply(mut self, num: u32) -> VMStateField { } } +/// This macro can be used (by just passing it a type) to forward the `VMState` +/// trait to the first field of a tuple. This is a workaround for lack of +/// support of nested [`offset_of`](core::mem::offset_of) until Rust 1.82.0. +/// +/// # Examples +/// +/// ``` +/// # use qemu_api::vmstate::impl_vmstate_forward; +/// pub struct Fifo([u8; 16]); +/// impl_vmstate_forward!(Fifo); +/// ``` +#[macro_export] +macro_rules! impl_vmstate_forward { + // This is similar to impl_vmstate_transparent below, but it + // uses the same trick as vmstate_of! to obtain the type of + // the first field of the tuple + ($tuple:ty) => { + unsafe impl $crate::vmstate::VMState for $tuple { + const SCALAR_TYPE: $crate::vmstate::VMStateFieldType = + $crate::call_func_with_field!($crate::vmstate::vmstate_scalar_type, $tuple, 0); + const BASE: $crate::bindings::VMStateField = + $crate::call_func_with_field!($crate::vmstate::vmstate_base, $tuple, 0); + } + }; +} + // Transparent wrappers: just use the internal type macro_rules! impl_vmstate_transparent { @@ -283,6 +318,26 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* { impl_vmstate_transparent!(crate::cell::BqlCell where T: VMState); impl_vmstate_transparent!(crate::cell::BqlRefCell where T: VMState); +#[macro_export] +macro_rules! impl_vmstate_bitsized { + ($type:ty) => { + unsafe impl $crate::vmstate::VMState for $type { + const SCALAR_TYPE: $crate::vmstate::VMStateFieldType = + <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt + as ::bilge::prelude::Number>::UnderlyingType + as $crate::vmstate::VMState>::SCALAR_TYPE; + const BASE: $crate::bindings::VMStateField = + <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt + as ::bilge::prelude::Number>::UnderlyingType + as $crate::vmstate::VMState>::BASE; + const VARRAY_FLAG: $crate::bindings::VMStateFlags = + <<<$type as ::bilge::prelude::Bitsized>::ArbitraryInt + as ::bilge::prelude::Number>::UnderlyingType + as $crate::vmstate::VMState>::VARRAY_FLAG; + } + }; +} + // Scalar types using predefined VMStateInfos macro_rules! impl_vmstate_scalar { From patchwork Tue Dec 31 00:23:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1F9F7E77194 for ; Tue, 31 Dec 2024 00:25:36 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3I-0003SJ-JW; Mon, 30 Dec 2024 19:24:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3G-0003Rf-MC for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3F-000200-8c for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604640; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0uVl+dn480T+3VzLrS0d1h50fyBb5C28oZegz0tUkU0=; b=etWASUxGxPJ0RYkbzgcP8cc9f6r12QQ3CVhyDWbMXmNsoFAaniU8z43SXbfvXL5ihpalnv 36Hki5KvppLG3WOSI+tkztu5vSW7hzehhlaO8rrS5HO+Q3obkuqOzYOIBydg/ak1rk9U/z jjKToO/7A22PLqjzgPys5aZ6qu28S0g= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-331-Q3iMuuafMrOcx52ztwB1Vw-1; Mon, 30 Dec 2024 19:23:59 -0500 X-MC-Unique: Q3iMuuafMrOcx52ztwB1Vw-1 X-Mimecast-MFC-AGG-ID: Q3iMuuafMrOcx52ztwB1Vw Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385dcadffebso4815427f8f.0 for ; Mon, 30 Dec 2024 16:23:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604637; x=1736209437; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0uVl+dn480T+3VzLrS0d1h50fyBb5C28oZegz0tUkU0=; b=to74WkefGnrAhzk+4faAcY5lH6grhriUisTIb2sZjA905bez0nG0Kgwhe6cKHanOko k6ZZ3SgjdUAVp4ixwXbz3xOBXJx9OQ+TYKBnrPjUFtZybo8jFlNwR4/YNs6rtPxAzTNb 6iAdhmAf+iSQLYly4CL1+QKYSr9V5lm3xJlx8RgcNI4gKnO1+EOq4ebZ4GfMXkszX69L En2vruQ6wm69r0BkR/B5IFSJUD5vS+DuBNs4mb7Yz5Uj72KOvE7DyUfDmVB3C8u4CrHx zav2BJDAJp7HXdR4CfenPJA3zU7JqEgX5Vq7pAyU+NEj3mCAjGzs3OMAdaYD6oWFBXdO gOdw== X-Gm-Message-State: AOJu0YwfYvA20atV+XKciqkl1F2IUPz7PVPIkIb5Pha+217rYhZEhymR XWO2eF7qQzQ1DWwtAESNIt2vri7pdX00f6mJyo7ELfGLdxfm6y2khn8adLvDHnyzf0BGIu6+Qbg gCXSkwbJcbhJinoS9YWf88qpRlmcC2GKk/dRvFe34azgvjveEo0kNNhT1882e5grDd5oa/xcOYS SYghtUZssmA7oumU4MqfBRYAyncA0OJoV9QKISwzo= X-Gm-Gg: ASbGncuZOKFgbVT5Dwi7Ukn0GmT5tRF8kTOD4BfdlsoQ7dFp5MRVx+u1mwL0D8qquNC CJGnz6IaZ86zZ0+zcq4ryG+lnYk7FXwn4PY25pq9x9wt06ZGg5ouNaSt9pGzJMj594gjxGI7jKt ukmZxE5cL12EgfNUczRAhfUa0QmwbZG0BYMROh4prfoRMsPXDep+pQVkXooJ7sSNacO5XJ/G/rV rXVqAY/UDMoSyU4tM0zYpukAhMxW6UifJWHA0UiXPck/xSGo/j4iJnKVZo= X-Received: by 2002:a05:6000:4a0a:b0:385:ef39:6ce9 with SMTP id ffacd0b85a97d-38a221f1716mr36518152f8f.21.1735604637082; Mon, 30 Dec 2024 16:23:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvXzYyJWX8Fb3jJHzeylhs2LlThMR/Quu3QQyaP/zVm3en/VGZA1Gz5ARRS6Xt7WnVXVZZuA== X-Received: by 2002:a05:6000:4a0a:b0:385:ef39:6ce9 with SMTP id ffacd0b85a97d-38a221f1716mr36518138f8f.21.1735604636667; Mon, 30 Dec 2024 16:23:56 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43661219a08sm373893925e9.25.2024.12.30.16.23.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:55 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 7/9] rust: qemu_api: add vmstate_struct and vmstate_cell Date: Tue, 31 Dec 2024 01:23:34 +0100 Message-ID: <20241231002336.25931-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org These are not type safe, but they're the best that can be done without const_refs_static. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index b59a4b66339..e45c93587b2 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -610,6 +610,40 @@ macro_rules! vmstate_array_of_pointer_to_struct { }}; } +// FIXME: including the `vmsd` field in a `const` is not possible without +// the const_refs_static feature (stabilized in Rust 1.83.0). Without it, +// it is not possible to use VMS_STRUCT in a transparent manner using +// `vmstate_of!`. While VMSTATE_CLOCK can at least try to be type-safe, +// VMSTATE_STRUCT includes $type only for documentation purposes; it +// is checked against $field_name and $struct_name, but not against $vmsd +// which is what really would matter. +#[doc(alias = "VMSTATE_STRUCT")] +#[macro_export] +macro_rules! vmstate_struct { + ($field_name:ident, $struct_name:ty, $vmsd:expr, $type:ty) => {{ + $crate::bindings::VMStateField { + name: ::core::concat!(::core::stringify!($field_name), "\0") + .as_bytes() + .as_ptr() as *const ::std::os::raw::c_char, + offset: { + $crate::assert_field_type!($struct_name, $field_name, $type); + $crate::offset_of!($struct_name, $field_name) + } + size: ::core::mem::size_of::<$type>(), + flags: $crate::bindings::VMStateFlags::VMS_STRUCT, + vmsd: unsafe { $vmsd }, + ..$crate::zeroable::Zeroable::ZERO + } + }}; +} + +#[macro_export] +macro_rules! vmstate_cell { + ($field_name:ident, $struct_name:ty, $vmsd:expr, $type:ty) => { + $crate::vmstate_struct!($field_name, $struct_name, $vmsd, $type) + }; +} + #[doc(alias = "VMSTATE_CLOCK_V")] #[macro_export] macro_rules! vmstate_clock_v { From patchwork Tue Dec 31 00:23:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D78CE7718F for ; Tue, 31 Dec 2024 00:25:15 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3L-0003Tv-DR; Mon, 30 Dec 2024 19:24:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3I-0003SZ-UL for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:04 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3H-00020J-AL for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gYsX+8dH7ajdw7h4Gl+C0v6pO/Y+Rzud1yNiyL/9IMY=; b=MJt0TOVfl0ua4IesjZ7n8DiWYhxARzYEg07lrQcv+5TgJDDctwz5mOoE3uWTva5SCvXXwh aIo+OcgGBMyiANdg+2X776OBcZsgw5F1TK7xnSuSRnzXwVYSb9TrLJGHwJsUMRKM3f+xmv GLYQwVod7qdR9+yg4GqHaH02bs3JAp0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-0RM7fkT7NgGNstrNmS5kQw-1; Mon, 30 Dec 2024 19:24:01 -0500 X-MC-Unique: 0RM7fkT7NgGNstrNmS5kQw-1 X-Mimecast-MFC-AGG-ID: 0RM7fkT7NgGNstrNmS5kQw Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4359206e1e4so75214355e9.2 for ; Mon, 30 Dec 2024 16:24:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604639; x=1736209439; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gYsX+8dH7ajdw7h4Gl+C0v6pO/Y+Rzud1yNiyL/9IMY=; b=jEOSmZzBuco8/eoISqephBzAY8bj+DoZ13Ad4vdM4NMGJEFIQxig84eON7+sF3qRsh 1S/+UAPsldEBLkKiOPzs5f37DY1zXw+sLhbx503BIlsEJZoS2uP12U4VNh6ZXqHtfeQY JPGZnBITO7AitBMiJV3gqbKsPG/1OS/bH9BUNL/cgVJPwfpVH7RlD4XOPee7M+UZLZhq qo3t/7w15RvTAGee4Ojw4SfTHD4n/WLtZO1pSNkR+WEo7jCFhoT/3p+QbX4f18aNsavr ngjQRKDdKjohbN+fJn81Wb7js4RTpaqoR/Visb46sVbgff23FLjpKhuqR4kMFnl9MSU8 8ezQ== X-Gm-Message-State: AOJu0YwWCeFH5Km2SXsopgeg2D2ow2sbfLziPpp1wwM47M9mXHd6EIfC uXGQWQgGtTw6M92AsNnxCENT9hbgqypQc0NTNQPKiuHm1eyA4lHqFh6lzlayExI+MAXcBG1xXFf IlQ7ZwY+IoHfGTs7NOGx6LsR10o3DdZa0vW21fqr74kH0/nSnxplpFjMZHVVl68mFT5fTXdYqjf DASDvb8la/oQEy6R1h5lr9MbTfByEY8QF2JMczh78= X-Gm-Gg: ASbGncvD9DJDcyjLhWdsBOSnRC8A8oZ96yfYFLLg8iyYoiXnSaCSnhoJmPrP+cQbSgy BrKCUj9irpynok0s2kwTlDrspBNwvEefbnA0Yb9cJx3RCh0XN8SwsMfbfV021JKPK91DIq6EqPa JMFxVxivCJitIc2TPuMVkkm49uzZCwPhgBWyXxSuX0uAAsAi00XYxkijb1gGGmnNnoUBg798PMN Fw5UZ2jZTCjJGhZS6hyKS6aceJzyjktb/7TlaZ1t8lhfmNwXj87ChJXvbs= X-Received: by 2002:a05:600c:1c25:b0:434:f4fa:83c4 with SMTP id 5b1f17b1804b1-43668b5f691mr304872365e9.29.1735604639568; Mon, 30 Dec 2024 16:23:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxDzgunRjQHOEt3yXGNGZrj8KS6374Y+Ckge8ZDZ8iuINYuYauFPTqu7gP3FeSa0KAhsjRLA== X-Received: by 2002:a05:600c:1c25:b0:434:f4fa:83c4 with SMTP id 5b1f17b1804b1-43668b5f691mr304872215e9.29.1735604639086; Mon, 30 Dec 2024 16:23:59 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43656b3b271sm402833415e9.34.2024.12.30.16.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:23:57 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 8/9] rust: pl011: switch vmstate to new-style macros Date: Tue, 31 Dec 2024 01:23:35 +0100 Message-ID: <20241231002336.25931-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Signed-off-by: Paolo Bonzini --- rust/hw/char/pl011/src/device.rs | 3 ++- rust/hw/char/pl011/src/device_class.rs | 36 +++++++++++++------------- rust/hw/char/pl011/src/lib.rs | 6 +++++ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs index 994c2fc0593..11a87664c7a 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -10,7 +10,7 @@ use qemu_api::{ bindings::{self, *}, - c_str, + c_str, impl_vmstate_forward, irq::InterruptSource, prelude::*, qdev::DeviceImpl, @@ -54,6 +54,7 @@ impl DeviceId { #[repr(transparent)] #[derive(Debug, Default)] pub struct Fifo([registers::Data; PL011_FIFO_DEPTH as usize]); +impl_vmstate_forward!(Fifo); impl Fifo { const fn len(&self) -> u32 { diff --git a/rust/hw/char/pl011/src/device_class.rs b/rust/hw/char/pl011/src/device_class.rs index 7f3ca895071..e0d3532e956 100644 --- a/rust/hw/char/pl011/src/device_class.rs +++ b/rust/hw/char/pl011/src/device_class.rs @@ -6,11 +6,11 @@ use std::os::raw::{c_int, c_void}; use qemu_api::{ - bindings::*, c_str, vmstate_clock, vmstate_fields, vmstate_subsections, vmstate_uint32, - vmstate_uint32_array, vmstate_unused, zeroable::Zeroable, + bindings::*, c_str, vmstate_clock, vmstate_fields, vmstate_of, vmstate_subsections, + vmstate_unused, zeroable::Zeroable, }; -use crate::device::{PL011State, PL011_FIFO_DEPTH}; +use crate::device::PL011State; extern "C" fn pl011_clock_needed(opaque: *mut c_void) -> bool { unsafe { @@ -52,21 +52,21 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int { post_load: Some(pl011_post_load), fields: vmstate_fields! { vmstate_unused!(core::mem::size_of::()), - vmstate_uint32!(flags, PL011State), - vmstate_uint32!(line_control, PL011State), - vmstate_uint32!(receive_status_error_clear, PL011State), - vmstate_uint32!(control, PL011State), - vmstate_uint32!(dmacr, PL011State), - vmstate_uint32!(int_enabled, PL011State), - vmstate_uint32!(int_level, PL011State), - vmstate_uint32_array!(read_fifo, PL011State, PL011_FIFO_DEPTH), - vmstate_uint32!(ilpr, PL011State), - vmstate_uint32!(ibrd, PL011State), - vmstate_uint32!(fbrd, PL011State), - vmstate_uint32!(ifl, PL011State), - vmstate_uint32!(read_pos, PL011State), - vmstate_uint32!(read_count, PL011State), - vmstate_uint32!(read_trigger, PL011State), + vmstate_of!(PL011State, flags), + vmstate_of!(PL011State, line_control), + vmstate_of!(PL011State, receive_status_error_clear), + vmstate_of!(PL011State, control), + vmstate_of!(PL011State, dmacr), + vmstate_of!(PL011State, int_enabled), + vmstate_of!(PL011State, int_level), + vmstate_of!(PL011State, read_fifo), + vmstate_of!(PL011State, ilpr), + vmstate_of!(PL011State, ibrd), + vmstate_of!(PL011State, fbrd), + vmstate_of!(PL011State, ifl), + vmstate_of!(PL011State, read_pos), + vmstate_of!(PL011State, read_count), + vmstate_of!(PL011State, read_trigger), }, subsections: vmstate_subsections! { VMSTATE_PL011_CLOCK diff --git a/rust/hw/char/pl011/src/lib.rs b/rust/hw/char/pl011/src/lib.rs index 0a89d393e0f..f30f9850ad4 100644 --- a/rust/hw/char/pl011/src/lib.rs +++ b/rust/hw/char/pl011/src/lib.rs @@ -106,6 +106,7 @@ pub mod registers { //! Device registers exposed as typed structs which are backed by arbitrary //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. use bilge::prelude::*; + use qemu_api::impl_vmstate_bitsized; /// Receive Status Register / Data Register common error bits /// @@ -172,6 +173,7 @@ pub struct Data { pub errors: Errors, _reserved: u16, } + impl_vmstate_bitsized!(Data); impl Data { // bilge is not very const-friendly, unfortunately @@ -208,6 +210,7 @@ pub struct ReceiveStatusErrorClear { pub errors: Errors, _reserved_unpredictable: u24, } + impl_vmstate_bitsized!(ReceiveStatusErrorClear); impl ReceiveStatusErrorClear { pub fn set_from_data(&mut self, data: Data) { @@ -280,6 +283,7 @@ pub struct Flags { pub ring_indicator: bool, _reserved_zero_no_modify: u23, } + impl_vmstate_bitsized!(Flags); impl Flags { pub fn reset(&mut self) { @@ -354,6 +358,7 @@ pub struct LineControl { /// 31:8 - Reserved, do not modify, read as zero. _reserved_zero_no_modify: u24, } + impl_vmstate_bitsized!(LineControl); impl LineControl { pub fn reset(&mut self) { @@ -498,6 +503,7 @@ pub struct Control { /// 31:16 - Reserved, do not modify, read as zero. _reserved_zero_no_modify2: u16, } + impl_vmstate_bitsized!(Control); impl Control { pub fn reset(&mut self) { From patchwork Tue Dec 31 00:23:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13923559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DBA98E77188 for ; Tue, 31 Dec 2024 00:25:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSQ3N-0003V0-Rh; Mon, 30 Dec 2024 19:24:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3L-0003Tz-Fb for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tSQ3J-00020o-I6 for qemu-devel@nongnu.org; Mon, 30 Dec 2024 19:24:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735604644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w5/JvoYepda1ESoi0KW/7MoIKytb2HMTXeN82nQCUKA=; b=Ww+mVeOc7nJKzwgdaq7laaMJaPMr5PvSdKfgazmsFG9FnvWInP6zSDeIUvmzASNdZpBlPY 3WhrfYbAVSLnE8zqMDKrdLE60QzbRjW2p8NtEpYE1KymZyGwKZjfJWkjCQQSsAnfR7WHgI VQ2ctQ188jx8IDTBQd8MGhJeRPHamok= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-su8ucyPaN76hwCKHQi67jA-1; Mon, 30 Dec 2024 19:24:03 -0500 X-MC-Unique: su8ucyPaN76hwCKHQi67jA-1 X-Mimecast-MFC-AGG-ID: su8ucyPaN76hwCKHQi67jA Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385e49efd59so775546f8f.0 for ; Mon, 30 Dec 2024 16:24:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735604642; x=1736209442; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w5/JvoYepda1ESoi0KW/7MoIKytb2HMTXeN82nQCUKA=; b=gxm4IBZTYE6oBte8p0MULtbpfIq6I/KJ6Ai36wBZcu+sHSQDJt+jAi/cb99k14ROVb thtIe9XbG1rkI4jSsJD9BYtvUQsucHyPjl7li3TEkux8n4fDHBWlSDXPRraWPMxKj9fV /vpL8bDhANF8CvQ9j1SQGJ0TFY0g4ZJIxdzG740VsoUQbhU33/pPqvEUA2WHfUrxGoji khD2jjyZE0xPUPj303PwROlY8z42NexN+ujJUG+Mz5c3SB9e+TrYQPLLB+3QOV8bm8+K jefF784a+sDGyJHgAU3qZ/e5nl+SMe2dCeC18L1YxBmePIB8tyspCE680L41hYeHxVv9 jmYA== X-Gm-Message-State: AOJu0Yx0mSr1jt5uY9T25rW611LcyKwkvitmE+1fbI+uOT7BD4okD91n i826Xun3XmG2cwP9mWxhDGzLZ8lqjc6G0xYi/OkydFMAvsMxk7kd/zckC7m1xraVNhwmJ/ljcRi 9TO2/5PcKpT5Jo1dulZMZMls7Lw8sbfgfWIyro69RxJSfUV3jhPyes5VA2iQxxIxX/azy5p2Esp UopfSp+h7iGSThbsjLXuNfKwWosZLAKloTJPKpkSQ= X-Gm-Gg: ASbGncvPUz9YtAtDj4lmNt3GYCTsjeWEVnqRvmsEJkR5JQwKsfrbFpzY3DD0l/Xv8g+ vXSBK52Nv/KEwj8ja2b7WG4PxKSu/TWiaULOfn6dCWmGanOaynyEB8gvKF8MgyTrnZ+/1GPHu5K DThbRfVtliiDyDG+6yXdgc5qffJ+CyKYFwnMEff9EXX6HipAMKV6uxw+pGVb/tgFR0cI1+rDfSd lqv9foh+xLI5u1SeHsKiVM5z53TkhS88JuWEmTDhYjJoPHFale4gxEkF+w= X-Received: by 2002:a5d:64e2:0:b0:388:cacf:24b5 with SMTP id ffacd0b85a97d-38a22408dc3mr33393960f8f.54.1735604641619; Mon, 30 Dec 2024 16:24:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IFup0I6+pZg/42dLfgAiUktVSsyxEMWs7Zz2wyj1Qj2PvNaK6+LQvNa7+rjUuP13uyfl8QEPQ== X-Received: by 2002:a5d:64e2:0:b0:388:cacf:24b5 with SMTP id ffacd0b85a97d-38a22408dc3mr33393946f8f.54.1735604641239; Mon, 30 Dec 2024 16:24:01 -0800 (PST) Received: from [192.168.10.3] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43656b015absm404439075e9.13.2024.12.30.16.23.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 16:24:00 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [RFC PATCH 9/9] rust: vmstate: remove translation of C vmstate macros Date: Tue, 31 Dec 2024 01:23:36 +0100 Message-ID: <20241231002336.25931-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241231002336.25931-1-pbonzini@redhat.com> References: <20241231002336.25931-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.187, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Keep vmstate_clock!; because it uses a field of type VMStateDescription, it cannot be converted to the VMState trait without access to the const_refs_static feature. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/vmstate.rs | 274 +++-------------------------------- 1 file changed, 23 insertions(+), 251 deletions(-) diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs index e45c93587b2..68246fce043 100644 --- a/rust/qemu-api/src/vmstate.rs +++ b/rust/qemu-api/src/vmstate.rs @@ -21,8 +21,8 @@ //! [`vmstate_fields`](crate::vmstate_fields). //! //! * direct equivalents to the C macros declared in -//! `include/migration/vmstate.h`. These are not type-safe and should not be -//! used if the equivalent functionality is available with `vmstate_of!`. +//! `include/migration/vmstate.h`. These are not type-safe and only provide +//! functionality that is missing from `vmstate_of!`. use core::{marker::PhantomData, mem, ptr::NonNull}; @@ -389,223 +389,16 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* { const BASE: VMStateField = ::BASE.with_array_flag(N); } -#[doc(alias = "VMSTATE_UNUSED_BUFFER")] -#[macro_export] -macro_rules! vmstate_unused_buffer { - ($field_exists_fn:expr, $version_id:expr, $size:expr) => {{ - $crate::bindings::VMStateField { - name: c_str!("unused").as_ptr(), - err_hint: ::core::ptr::null(), - offset: 0, - size: $size, - start: 0, - num: 0, - num_offset: 0, - size_offset: 0, - info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) }, - flags: VMStateFlags::VMS_BUFFER, - vmsd: ::core::ptr::null(), - version_id: $version_id, - struct_version_id: 0, - field_exists: $field_exists_fn, - } - }}; -} - -#[doc(alias = "VMSTATE_UNUSED_V")] -#[macro_export] -macro_rules! vmstate_unused_v { - ($version_id:expr, $size:expr) => {{ - $crate::vmstate_unused_buffer!(None, $version_id, $size) - }}; -} - #[doc(alias = "VMSTATE_UNUSED")] #[macro_export] macro_rules! vmstate_unused { ($size:expr) => {{ - $crate::vmstate_unused_v!(0, $size) - }}; -} - -#[doc(alias = "VMSTATE_SINGLE_TEST")] -#[macro_export] -macro_rules! vmstate_single_test { - ($field_name:ident, $struct_name:ty, $field_exists_fn:expr, $version_id:expr, $info:expr, $size:expr) => {{ $crate::bindings::VMStateField { - name: ::core::concat!(::core::stringify!($field_name), 0) - .as_bytes() - .as_ptr() as *const ::std::os::raw::c_char, - err_hint: ::core::ptr::null(), - offset: $crate::offset_of!($struct_name, $field_name), + name: $crate::c_str!("unused").as_ptr(), size: $size, - start: 0, - num: 0, - num_offset: 0, - size_offset: 0, - info: unsafe { $info }, - flags: VMStateFlags::VMS_SINGLE, - vmsd: ::core::ptr::null(), - version_id: $version_id, - struct_version_id: 0, - field_exists: $field_exists_fn, - } - }}; -} - -#[doc(alias = "VMSTATE_SINGLE")] -#[macro_export] -macro_rules! vmstate_single { - ($field_name:ident, $struct_name:ty, $version_id:expr, $info:expr, $size:expr) => {{ - $crate::vmstate_single_test!($field_name, $struct_name, None, $version_id, $info, $size) - }}; -} - -#[doc(alias = "VMSTATE_UINT32_V")] -#[macro_export] -macro_rules! vmstate_uint32_v { - ($field_name:ident, $struct_name:ty, $version_id:expr) => {{ - $crate::vmstate_single!( - $field_name, - $struct_name, - $version_id, - ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint32), - ::core::mem::size_of::() - ) - }}; -} - -#[doc(alias = "VMSTATE_UINT32")] -#[macro_export] -macro_rules! vmstate_uint32 { - ($field_name:ident, $struct_name:ty) => {{ - $crate::vmstate_uint32_v!($field_name, $struct_name, 0) - }}; -} - -#[doc(alias = "VMSTATE_ARRAY")] -#[macro_export] -macro_rules! vmstate_array { - ($field_name:ident, $struct_name:ty, $length:expr, $version_id:expr, $info:expr, $size:expr) => {{ - $crate::bindings::VMStateField { - name: ::core::concat!(::core::stringify!($field_name), 0) - .as_bytes() - .as_ptr() as *const ::std::os::raw::c_char, - err_hint: ::core::ptr::null(), - offset: $crate::offset_of!($struct_name, $field_name), - size: $size, - start: 0, - num: $length as _, - num_offset: 0, - size_offset: 0, - info: unsafe { $info }, - flags: VMStateFlags::VMS_ARRAY, - vmsd: ::core::ptr::null(), - version_id: $version_id, - struct_version_id: 0, - field_exists: None, - } - }}; -} - -#[doc(alias = "VMSTATE_UINT32_ARRAY_V")] -#[macro_export] -macro_rules! vmstate_uint32_array_v { - ($field_name:ident, $struct_name:ty, $length:expr, $version_id:expr) => {{ - $crate::vmstate_array!( - $field_name, - $struct_name, - $length, - $version_id, - ::core::ptr::addr_of!($crate::bindings::vmstate_info_uint32), - ::core::mem::size_of::() - ) - }}; -} - -#[doc(alias = "VMSTATE_UINT32_ARRAY")] -#[macro_export] -macro_rules! vmstate_uint32_array { - ($field_name:ident, $struct_name:ty, $length:expr) => {{ - $crate::vmstate_uint32_array_v!($field_name, $struct_name, $length, 0) - }}; -} - -#[doc(alias = "VMSTATE_STRUCT_POINTER_V")] -#[macro_export] -macro_rules! vmstate_struct_pointer_v { - ($field_name:ident, $struct_name:ty, $version_id:expr, $vmsd:expr, $type:ty) => {{ - $crate::bindings::VMStateField { - name: ::core::concat!(::core::stringify!($field_name), 0) - .as_bytes() - .as_ptr() as *const ::std::os::raw::c_char, - err_hint: ::core::ptr::null(), - offset: $crate::offset_of!($struct_name, $field_name), - size: ::core::mem::size_of::<*const $type>(), - start: 0, - num: 0, - num_offset: 0, - size_offset: 0, - info: ::core::ptr::null(), - flags: VMStateFlags(VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_POINTER.0), - vmsd: unsafe { $vmsd }, - version_id: $version_id, - struct_version_id: 0, - field_exists: None, - } - }}; -} - -#[doc(alias = "VMSTATE_ARRAY_OF_POINTER")] -#[macro_export] -macro_rules! vmstate_array_of_pointer { - ($field_name:ident, $struct_name:ty, $num:expr, $version_id:expr, $info:expr, $type:ty) => {{ - $crate::bindings::VMStateField { - name: ::core::concat!(::core::stringify!($field_name), 0) - .as_bytes() - .as_ptr() as *const ::std::os::raw::c_char, - version_id: $version_id, - num: $num as _, - info: unsafe { $info }, - size: ::core::mem::size_of::<*const $type>(), - flags: VMStateFlags(VMStateFlags::VMS_ARRAY.0 | VMStateFlags::VMS_ARRAY_OF_POINTER.0), - offset: $crate::offset_of!($struct_name, $field_name), - err_hint: ::core::ptr::null(), - start: 0, - num_offset: 0, - size_offset: 0, - vmsd: ::core::ptr::null(), - struct_version_id: 0, - field_exists: None, - } - }}; -} - -#[doc(alias = "VMSTATE_ARRAY_OF_POINTER_TO_STRUCT")] -#[macro_export] -macro_rules! vmstate_array_of_pointer_to_struct { - ($field_name:ident, $struct_name:ty, $num:expr, $version_id:expr, $vmsd:expr, $type:ty) => {{ - $crate::bindings::VMStateField { - name: ::core::concat!(::core::stringify!($field_name), 0) - .as_bytes() - .as_ptr() as *const ::std::os::raw::c_char, - version_id: $version_id, - num: $num as _, - vmsd: unsafe { $vmsd }, - size: ::core::mem::size_of::<*const $type>(), - flags: VMStateFlags( - VMStateFlags::VMS_ARRAY.0 - | VMStateFlags::VMS_STRUCT.0 - | VMStateFlags::VMS_ARRAY_OF_POINTER.0, - ), - offset: $crate::offset_of!($struct_name, $field_name), - err_hint: ::core::ptr::null(), - start: 0, - num_offset: 0, - size_offset: 0, - vmsd: ::core::ptr::null(), - struct_version_id: 0, - field_exists: None, + info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) }, + flags: $crate::bindings::VMStateFlags::VMS_BUFFER, + ..$crate::zeroable::Zeroable::ZERO } }}; } @@ -644,48 +437,27 @@ macro_rules! vmstate_cell { }; } -#[doc(alias = "VMSTATE_CLOCK_V")] -#[macro_export] -macro_rules! vmstate_clock_v { - ($field_name:ident, $struct_name:ty, $version_id:expr) => {{ - $crate::vmstate_struct_pointer_v!( - $field_name, - $struct_name, - $version_id, - ::core::ptr::addr_of!($crate::bindings::vmstate_clock), - $crate::bindings::Clock - ) - }}; -} - #[doc(alias = "VMSTATE_CLOCK")] #[macro_export] macro_rules! vmstate_clock { ($field_name:ident, $struct_name:ty) => {{ - $crate::vmstate_clock_v!($field_name, $struct_name, 0) - }}; -} - -#[doc(alias = "VMSTATE_ARRAY_CLOCK_V")] -#[macro_export] -macro_rules! vmstate_array_clock_v { - ($field_name:ident, $struct_name:ty, $num:expr, $version_id:expr) => {{ - $crate::vmstate_array_of_pointer_to_struct!( - $field_name, - $struct_name, - $num, - $version_id, - ::core::ptr::addr_of!($crate::bindings::vmstate_clock), - $crate::bindings::Clock - ) - }}; -} - -#[doc(alias = "VMSTATE_ARRAY_CLOCK")] -#[macro_export] -macro_rules! vmstate_array_clock { - ($field_name:ident, $struct_name:ty, $num:expr) => {{ - $crate::vmstate_array_clock_v!($field_name, $struct_name, $name, 0) + $crate::bindings::VMStateField { + name: ::core::concat!(::core::stringify!($field_name), "\0") + .as_bytes() + .as_ptr() as *const ::std::os::raw::c_char, + offset: { + $crate::assert_field_type!( + $struct_name, + $field_name, + core::ptr::NonNull<$crate::bindings::Clock> + ); + $crate::offset_of!($struct_name, $field_name) + }, + size: ::core::mem::size_of::<*const $crate::bindings::Clock>(), + flags: VMStateFlags(VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_POINTER.0), + vmsd: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_clock) }, + ..$crate::zeroable::Zeroable::ZERO + } }}; }