From nobody Sun Sep 28 17:08:40 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1757329148; cv=none; d=zohomail.com; s=zohoarc; b=UqEhVjvHNs7oPmqC/h1serdezqQBd3i2RWjNOjbKj8qDhZ7fFQ0EcQmvLEAf9qrX9hhySVG/lbBrYFwqZcXEK5PvWxp1ZvmMo8oWZFt6MC3jldk1UR5ia6VhJYSQmvs5dc6jjKlkkjp2i5cKnGw19udOfHu/ViEPtrSfg4XHr3Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757329148; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YqaXyHZphr7laaSuNH4W+NAAdi5D2+B70oR/bN2XPCw=; b=HVUZLiIUeuVQ0x1C1LKdmxhp+FoJL/blZqbAxHts4m7aTXAPNP0I8YUQXWIHMDzr99ycOCFmGIF7mruEamyKq3TU1yPJVD6CRisGpTpXtwuTS9nGn4nmEezbOoLuPw6wINs+KJOvLML2HKqwnnTv5ofCNmaqm3kkqPStqO6rb08= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757329148545898.9090642594645; Mon, 8 Sep 2025 03:59:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvZTD-0006oo-8z; Mon, 08 Sep 2025 06:51:35 -0400 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 1uvZT9-0006n0-7g for qemu-devel@nongnu.org; Mon, 08 Sep 2025 06:51:31 -0400 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 1uvZSq-00068Y-LS for qemu-devel@nongnu.org; Mon, 08 Sep 2025 06:51:28 -0400 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-189-sqKqe7BJPZi0USg-80UICg-1; Mon, 08 Sep 2025 06:51:06 -0400 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-45dda7c0e5eso24082775e9.0 for ; Mon, 08 Sep 2025 03:51:06 -0700 (PDT) Received: from [192.168.10.48] ([151.95.56.250]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45dda202112sm137492455e9.5.2025.09.08.03.51.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Sep 2025 03:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757328668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YqaXyHZphr7laaSuNH4W+NAAdi5D2+B70oR/bN2XPCw=; b=ank6DaleoD9D7AlIFNbvVM77eX7EYhUDSqJ4PmvdmCx21jky5TJ99MFKg6hQoDEYyMU1Iz 2SHUKyxottI80Br6+W8LvR/u4SozTc9gsBGOJglQCoTLSnPIMPfWAgNUOhkfh0NPl8vchs 6vPbChX0OlfjiW8eO/RaqTwViSOAGPk= X-MC-Unique: sqKqe7BJPZi0USg-80UICg-1 X-Mimecast-MFC-AGG-ID: sqKqe7BJPZi0USg-80UICg_1757328665 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757328665; x=1757933465; 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=YqaXyHZphr7laaSuNH4W+NAAdi5D2+B70oR/bN2XPCw=; b=Y1l9byyA6DffsX1NiFhkL+MzMX11l/7N7o2jsB8S0Hrw/X7mZYevDxVLe/m97alY8y FmoXSzOfxRdBAIQzPVoboJYth+UyMNaFfjA5pXgU0RQnbuGInbt57HysvKKJYYHwpOIh X/IQz6JCLtiNukbHpGKEV1YG/mE4w+Id/pYJpvsqID1k2CI7fxWllkAu3x+aeOs7wGd/ joUSbxJ2aJLtjm3A9d4taA3RA2XZhIoXP+F0OnC1h2pOofDbm+bj5ldz1OeANIEvH/3n etPjHKXw9fljMw8rLRgcKTmOTX81u3c3DqbxjS1Rz/PxqqbV7f2xu+3XrM6L/pyf7MDe IzXw== X-Gm-Message-State: AOJu0YwT+8KR1ucK1qrSq6JQXn5QLprZpOre5xlV3hBgkcWqA2fZ90+Q 8VWCswDhthRpIFUZgXGG2iDmOC1FmlRb3/USEyxldCIG3C21M8AEP2OztcBPCrUN97OFjRbt7j/ UF4SA9i2fb/8Heqhlp4pAuQyavEg2FxHsM4q02Yxqui0UMTfSzUmgQuzS/91NToH43/RqctN2LD dT1YRGX3PeMFdOtyAWr+WVDctmHKr8KzJbeoqRyOVa X-Gm-Gg: ASbGncsWYf3zfg8b7I2Ph0M6CV/LpChHBbAgwpycPbeSisrJdsYeU/r7hA8DEAjci3/ hDBKyVi9bgxm3AGBNxY+taLtqbDq6LfctGSlSebgRWREa/obfoGfl0BdOKJpFGXjfvvHD2rZMuu 4dw0i8V7npP5SwMpXsrTbwVsy1Io7OGXK5TNiGFPQ12C9G9z7u25nJd6MSOY5+evQeaZWnemy43 QrOSfkbSXTaAQ9fAWgfyNlOAPHXTZgKVA6Og0EUfj+jKFr4VQt4NAGwF555sq/rFUKX1ETvClrE Z8rY7RufE1y2VS4CMHMWiCgYAk5yaDNZ2gcAPEz9BixYX3XsICFwfo6l9luuth9aHeaQO3gir9l 2tlqRgvJDS8w8N1MvtfO7SlW0ikz+fb0maV2thj0HlbA= X-Received: by 2002:a05:600c:1991:b0:45b:5f3d:aa3d with SMTP id 5b1f17b1804b1-45dddeee57cmr53236995e9.21.1757328664606; Mon, 08 Sep 2025 03:51:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhYZ97ItqQXylxGX+4NYQleyfk3zQuDES8aEeWMDlOJjlNfybUgM6OQiUmFzBrrIWGibz7Cg== X-Received: by 2002:a05:600c:1991:b0:45b:5f3d:aa3d with SMTP id 5b1f17b1804b1-45dddeee57cmr53236585e9.21.1757328663759; Mon, 08 Sep 2025 03:51:03 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [PATCH 24/33] rust: split "hwcore" crate Date: Mon, 8 Sep 2025 12:49:56 +0200 Message-ID: <20250908105005.2119297-25-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250908105005.2119297-1-pbonzini@redhat.com> References: <20250908105005.2119297-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01, T_SPF_TEMPERROR=0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1757329150970116600 From: Marc-Andr=C3=A9 Lureau Signed-off-by: Marc-Andr=C3=A9 Lureau Link: https://lore.kernel.org/r/20250827104147.717203-16-marcandre.lureau@r= edhat.com Signed-off-by: Paolo Bonzini Reviewed-by: Zhao Liu --- MAINTAINERS | 1 + rust/hw/core/wrapper.h | 32 +++++++++ rust/qemu-api/wrapper.h | 6 -- rust/Cargo.lock | 17 +++++ rust/Cargo.toml | 1 + rust/bindings/src/lib.rs | 64 ++++++++++++++++++ rust/hw/char/pl011/Cargo.toml | 1 + rust/hw/char/pl011/meson.build | 1 + rust/hw/char/pl011/src/device.rs | 10 ++- rust/hw/core/Cargo.toml | 26 ++++++++ rust/hw/core/build.rs | 1 + rust/hw/core/meson.build | 80 +++++++++++++++++++++++ rust/hw/core/src/bindings.rs | 41 ++++++++++++ rust/{qemu-api =3D> hw/core}/src/irq.rs | 0 rust/hw/core/src/lib.rs | 14 ++++ rust/{qemu-api =3D> hw/core}/src/qdev.rs | 2 +- rust/{qemu-api =3D> hw/core}/src/sysbus.rs | 0 rust/{qemu-api =3D> hw/core}/tests/tests.rs | 7 +- rust/hw/timer/hpet/Cargo.toml | 1 + rust/hw/timer/hpet/meson.build | 1 + rust/hw/timer/hpet/src/device.rs | 26 ++++---- rust/meson.build | 1 + rust/qemu-api-macros/src/lib.rs | 10 +-- rust/qemu-api-macros/src/tests.rs | 20 +++--- rust/qemu-api/Cargo.toml | 1 + rust/qemu-api/meson.build | 17 +---- rust/qemu-api/src/bindings.rs | 10 --- rust/qemu-api/src/lib.rs | 4 -- rust/qemu-api/src/prelude.rs | 4 -- 29 files changed, 318 insertions(+), 81 deletions(-) create mode 100644 rust/hw/core/wrapper.h create mode 100644 rust/bindings/src/lib.rs create mode 100644 rust/hw/core/Cargo.toml create mode 120000 rust/hw/core/build.rs create mode 100644 rust/hw/core/meson.build create mode 100644 rust/hw/core/src/bindings.rs rename rust/{qemu-api =3D> hw/core}/src/irq.rs (100%) create mode 100644 rust/hw/core/src/lib.rs rename rust/{qemu-api =3D> hw/core}/src/qdev.rs (99%) rename rust/{qemu-api =3D> hw/core}/src/sysbus.rs (100%) rename rust/{qemu-api =3D> hw/core}/tests/tests.rs (97%) diff --git a/MAINTAINERS b/MAINTAINERS index 2b5be3c45c3..50dda5d119e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3516,6 +3516,7 @@ S: Maintained F: rust/bql/ F: rust/chardev/ F: rust/common/ +F: rust/hw/core/ F: rust/migration/ F: rust/qemu-api F: rust/qemu-api-macros diff --git a/rust/hw/core/wrapper.h b/rust/hw/core/wrapper.h new file mode 100644 index 00000000000..3bdbd1249e4 --- /dev/null +++ b/rust/hw/core/wrapper.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * This header file is meant to be used as input to the `bindgen` applicat= ion + * in order to generate C FFI compatible Rust bindings. + */ + +#ifndef __CLANG_STDATOMIC_H +#define __CLANG_STDATOMIC_H +/* + * Fix potential missing stdatomic.h error in case bindgen does not insert= the + * correct libclang header paths on its own. We do not use stdatomic.h sym= bols + * in QEMU code, so it's fine to declare dummy types instead. + */ +typedef enum memory_order { + memory_order_relaxed, + memory_order_consume, + memory_order_acquire, + memory_order_release, + memory_order_acq_rel, + memory_order_seq_cst, +} memory_order; +#endif /* __CLANG_STDATOMIC_H */ + +#include "qemu/osdep.h" + +#include "hw/sysbus.h" +#include "hw/clock.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/irq.h" diff --git a/rust/qemu-api/wrapper.h b/rust/qemu-api/wrapper.h index 564733b9035..7c9c20b14fe 100644 --- a/rust/qemu-api/wrapper.h +++ b/rust/qemu-api/wrapper.h @@ -49,11 +49,5 @@ typedef enum memory_order { =20 #include "qemu/osdep.h" #include "qemu-io.h" -#include "hw/sysbus.h" -#include "hw/clock.h" -#include "hw/qdev-clock.h" -#include "hw/qdev-properties.h" -#include "hw/qdev-properties-system.h" -#include "hw/irq.h" #include "exec/memattrs.h" #include "hw/char/pl011.h" diff --git a/rust/Cargo.lock b/rust/Cargo.lock index e6b75f30bef..77118e882b5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -91,6 +91,7 @@ version =3D "0.1.0" dependencies =3D [ "bql", "common", + "hwcore", "migration", "qemu_api", "qemu_api_macros", @@ -99,6 +100,20 @@ dependencies =3D [ "util", ] =20 +[[package]] +name =3D "hwcore" +version =3D "0.1.0" +dependencies =3D [ + "bql", + "chardev", + "common", + "migration", + "qemu_api_macros", + "qom", + "system", + "util", +] + [[package]] name =3D "itertools" version =3D "0.11.0" @@ -133,6 +148,7 @@ dependencies =3D [ "bql", "chardev", "common", + "hwcore", "migration", "qemu_api", "qemu_api_macros", @@ -180,6 +196,7 @@ dependencies =3D [ "bql", "chardev", "common", + "hwcore", "migration", "qemu_api_macros", "qom", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8e210d277a2..8ec07d20651 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -9,6 +9,7 @@ members =3D [ "qemu-api", "qom", "system", + "hw/core", "hw/char/pl011", "hw/timer/hpet", "util", diff --git a/rust/bindings/src/lib.rs b/rust/bindings/src/lib.rs new file mode 100644 index 00000000000..5bf03b13706 --- /dev/null +++ b/rust/bindings/src/lib.rs @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#![allow( + dead_code, + improper_ctypes_definitions, + improper_ctypes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unsafe_op_in_unsafe_fn, + clippy::pedantic, + clippy::restriction, + clippy::style, + clippy::missing_const_for_fn, + clippy::ptr_offset_with_cast, + clippy::useless_transmute, + clippy::missing_safety_doc +)] + +//! `bindgen`-generated declarations. + +#[cfg(MESON)] +include!("bindings.inc.rs"); + +#[cfg(not(MESON))] +include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs")); + +// SAFETY: these are implemented in C; the bindings need to assert that the +// BQL is taken, either directly or via `BqlCell` and `BqlRefCell`. +// When bindings for character devices are introduced, this can be +// moved to the Opaque<> wrapper in src/chardev.rs. +unsafe impl Send for CharBackend {} +unsafe impl Sync for CharBackend {} + +// SAFETY: this is a pure data struct +unsafe impl Send for CoalescedMemoryRange {} +unsafe impl Sync for CoalescedMemoryRange {} + +// SAFETY: these are constants and vtables; the Send and Sync requirements +// are deferred to the unsafe callbacks that they contain +unsafe impl Send for MemoryRegionOps {} +unsafe impl Sync for MemoryRegionOps {} + +unsafe impl Send for Property {} +unsafe impl Sync for Property {} + +unsafe impl Send for TypeInfo {} +unsafe impl Sync for TypeInfo {} + +unsafe impl Send for VMStateDescription {} +unsafe impl Sync for VMStateDescription {} + +unsafe impl Send for VMStateField {} +unsafe impl Sync for VMStateField {} + +unsafe impl Send for VMStateInfo {} +unsafe impl Sync for VMStateInfo {} + +// bindgen does not derive Default here +#[allow(clippy::derivable_impls)] +impl Default for VMStateFlags { + fn default() -> Self { + Self(0) + } +} diff --git a/rust/hw/char/pl011/Cargo.toml b/rust/hw/char/pl011/Cargo.toml index e4b1c3f1eb8..830d88586bb 100644 --- a/rust/hw/char/pl011/Cargo.toml +++ b/rust/hw/char/pl011/Cargo.toml @@ -23,6 +23,7 @@ migration =3D { path =3D "../../../migration" } qom =3D { path =3D "../../../qom" } chardev =3D { path =3D "../../../chardev" } system =3D { path =3D "../../../system" } +hwcore =3D { path =3D "../../../hw/core" } qemu_api =3D { path =3D "../../../qemu-api" } qemu_api_macros =3D { path =3D "../../../qemu-api-macros" } =20 diff --git a/rust/hw/char/pl011/meson.build b/rust/hw/char/pl011/meson.build index fae6e1b9c9d..fac04321133 100644 --- a/rust/hw/char/pl011/meson.build +++ b/rust/hw/char/pl011/meson.build @@ -16,6 +16,7 @@ _libpl011_rs =3D static_library( qom_rs, chardev_rs, system_rs, + hwcore_rs, ], ) =20 diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/devi= ce.rs index c65db5a5174..a6a17d9f2dc 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -7,16 +7,14 @@ use bql::BqlRefCell; use chardev::{CharBackend, Chardev, Event}; use common::{static_assert, uninit_field_mut}; +use hwcore::{ + Clock, ClockEvent, DeviceImpl, DeviceMethods, DeviceState, IRQState, I= nterruptSource, + ResetType, ResettablePhasesImpl, SysBusDevice, SysBusDeviceImpl, SysBu= sDeviceMethods, +}; use migration::{ self, impl_vmstate_forward, impl_vmstate_struct, vmstate_fields, vmsta= te_of, vmstate_subsections, vmstate_unused, VMStateDescription, VMStateDescri= ptionBuilder, }; -use qemu_api::{ - irq::{IRQState, InterruptSource}, - prelude::*, - qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, ResetType, Resettab= lePhasesImpl}, - sysbus::{SysBusDevice, SysBusDeviceImpl}, -}; use qom::{prelude::*, ObjectImpl, Owned, ParentField, ParentInit}; use system::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder= }; use util::{log::Log, log_mask_ln}; diff --git a/rust/hw/core/Cargo.toml b/rust/hw/core/Cargo.toml new file mode 100644 index 00000000000..0b353802644 --- /dev/null +++ b/rust/hw/core/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name =3D "hwcore" +version =3D "0.1.0" +description =3D "Rust bindings for QEMU/hwcore" +resolver =3D "2" +publish =3D false + +authors.workspace =3D true +edition.workspace =3D true +homepage.workspace =3D true +license.workspace =3D true +repository.workspace =3D true +rust-version.workspace =3D true + +[dependencies] +common =3D { path =3D "../../common" } +bql =3D { path =3D "../../bql" } +qom =3D { path =3D "../../qom" } +chardev =3D { path =3D "../../chardev" } +migration =3D { path =3D "../../migration" } +system =3D { path =3D "../../system" } +util =3D { path =3D "../../util" } +qemu_api_macros =3D { path =3D "../../qemu-api-macros" } + +[lints] +workspace =3D true diff --git a/rust/hw/core/build.rs b/rust/hw/core/build.rs new file mode 120000 index 00000000000..2a79ee31b8c --- /dev/null +++ b/rust/hw/core/build.rs @@ -0,0 +1 @@ +../../util/build.rs \ No newline at end of file diff --git a/rust/hw/core/meson.build b/rust/hw/core/meson.build new file mode 100644 index 00000000000..7dd1ade6f0f --- /dev/null +++ b/rust/hw/core/meson.build @@ -0,0 +1,80 @@ +_hwcore_bindgen_args =3D [] +c_enums =3D [ + 'DeviceCategory', + 'GpioPolarity', + 'MachineInitPhase', + 'ResetType', +] +foreach enum : c_enums + _hwcore_bindgen_args +=3D ['--rustified-enum', enum] +endforeach + +blocked_type =3D [ + 'Chardev', + 'Error', + 'ObjectClass', + 'MemoryRegion', + 'VMStateDescription', +] +foreach type: blocked_type + _hwcore_bindgen_args +=3D ['--blocklist-type', type] +endforeach + +c_bitfields =3D [ + 'ClockEvent', +] +foreach enum : c_bitfields + _hwcore_bindgen_args +=3D ['--bitfield-enum', enum] +endforeach + +# TODO: Remove this comment when the clang/libclang mismatch issue is solv= ed. +# +# Rust bindings generation with `bindgen` might fail in some cases where t= he +# detected `libclang` does not match the expected `clang` version/target. = In +# this case you must pass the path to `clang` and `libclang` to your build +# command invocation using the environment variables CLANG_PATH and +# LIBCLANG_PATH +_hwcore_bindings_inc_rs =3D rust.bindgen( + input: 'wrapper.h', + dependencies: common_ss.all_dependencies(), + output: 'bindings.inc.rs', + include_directories: bindings_incdir, + bindgen_version: ['>=3D0.60.0'], + args: bindgen_args_common + _hwcore_bindgen_args, +) + +_hwcore_rs =3D static_library( + 'hwcore', + structured_sources( + [ + 'src/lib.rs', + 'src/bindings.rs', + 'src/irq.rs', + 'src/qdev.rs', + 'src/sysbus.rs', + ], + {'.': _hwcore_bindings_inc_rs} + ), + override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], + rust_abi: 'rust', + link_with: [_bql_rs, _chardev_rs, _migration_rs, _qom_rs, _system_rs, _u= til_rs], + dependencies: [qemu_api_macros, common_rs], +) + +hwcore_rs =3D declare_dependency(link_with: [_hwcore_rs], + dependencies: [qom_rs, hwcore]) + +test('rust-hwcore-rs-integration', + executable( + 'rust-hwcore-rs-integration', + files('tests/tests.rs'), + override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], + rust_args: ['--test'], + install: false, + dependencies: [common_rs, hwcore_rs, bql_rs, migration_rs, qemu_ap= i_macros, util_rs]), + args: [ + '--test', '--test-threads', '1', + '--format', 'pretty', + ], + protocol: 'rust', + suite: ['unit', 'rust']) diff --git a/rust/hw/core/src/bindings.rs b/rust/hw/core/src/bindings.rs new file mode 100644 index 00000000000..919c02b56ae --- /dev/null +++ b/rust/hw/core/src/bindings.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#![allow( + dead_code, + improper_ctypes_definitions, + improper_ctypes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unnecessary_transmutes, + unsafe_op_in_unsafe_fn, + clippy::pedantic, + clippy::restriction, + clippy::style, + clippy::missing_const_for_fn, + clippy::ptr_offset_with_cast, + clippy::useless_transmute, + clippy::missing_safety_doc, + clippy::too_many_arguments +)] + +use chardev::bindings::Chardev; +use common::Zeroable; +use migration::bindings::VMStateDescription; +use qom::bindings::ObjectClass; +use system::bindings::MemoryRegion; +use util::bindings::Error; + +#[cfg(MESON)] +include!("bindings.inc.rs"); + +#[cfg(not(MESON))] +include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs")); + +unsafe impl Send for Property {} +unsafe impl Sync for Property {} + +unsafe impl Send for TypeInfo {} +unsafe impl Sync for TypeInfo {} + +unsafe impl Zeroable for Property__bindgen_ty_1 {} +unsafe impl Zeroable for Property {} diff --git a/rust/qemu-api/src/irq.rs b/rust/hw/core/src/irq.rs similarity index 100% rename from rust/qemu-api/src/irq.rs rename to rust/hw/core/src/irq.rs diff --git a/rust/hw/core/src/lib.rs b/rust/hw/core/src/lib.rs new file mode 100644 index 00000000000..c5588d9bc27 --- /dev/null +++ b/rust/hw/core/src/lib.rs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +pub use qom; + +pub mod bindings; + +mod irq; +pub use irq::*; + +mod qdev; +pub use qdev::*; + +mod sysbus; +pub use sysbus::*; diff --git a/rust/qemu-api/src/qdev.rs b/rust/hw/core/src/qdev.rs similarity index 99% rename from rust/qemu-api/src/qdev.rs rename to rust/hw/core/src/qdev.rs index 7efc796f502..8e9702ce0bb 100644 --- a/rust/qemu-api/src/qdev.rs +++ b/rust/hw/core/src/qdev.rs @@ -9,13 +9,13 @@ ptr::NonNull, }; =20 -pub use bindings::{ClockEvent, DeviceClass, Property, ResetType}; use chardev::Chardev; use common::{callbacks::FnCall, Opaque}; use migration::{impl_vmstate_c_struct, VMStateDescription}; use qom::{prelude::*, ObjectClass, ObjectImpl, Owned, ParentInit}; use util::{Error, Result}; =20 +pub use crate::bindings::{ClockEvent, DeviceClass, Property, ResetType}; use crate::{ bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out, ResettableClas= s}, irq::InterruptSource, diff --git a/rust/qemu-api/src/sysbus.rs b/rust/hw/core/src/sysbus.rs similarity index 100% rename from rust/qemu-api/src/sysbus.rs rename to rust/hw/core/src/sysbus.rs diff --git a/rust/qemu-api/tests/tests.rs b/rust/hw/core/tests/tests.rs similarity index 97% rename from rust/qemu-api/tests/tests.rs rename to rust/hw/core/tests/tests.rs index f2e5eb9f4f4..21ee301fa68 100644 --- a/rust/qemu-api/tests/tests.rs +++ b/rust/hw/core/tests/tests.rs @@ -5,16 +5,11 @@ use std::{ffi::CStr, ptr::addr_of}; =20 use bql::BqlCell; +use hwcore::{DeviceImpl, DeviceState, ResettablePhasesImpl, SysBusDevice}; use migration::{VMStateDescription, VMStateDescriptionBuilder}; -use qemu_api::{ - qdev::{DeviceImpl, DeviceState, ResettablePhasesImpl}, - sysbus::SysBusDevice, -}; use qom::{prelude::*, ObjectImpl, ParentField}; use util::bindings::{module_call_init, module_init_type}; =20 -mod vmstate_tests; - // Test that macros can compile. pub const VMSTATE: VMStateDescription =3D VMStateDescriptionBu= ilder::::new() .name(c"name") diff --git a/rust/hw/timer/hpet/Cargo.toml b/rust/hw/timer/hpet/Cargo.toml index a95b1271c64..e28d66f6457 100644 --- a/rust/hw/timer/hpet/Cargo.toml +++ b/rust/hw/timer/hpet/Cargo.toml @@ -19,6 +19,7 @@ qom =3D { path =3D "../../../qom" } system =3D { path =3D "../../../system" } qemu_api =3D { path =3D "../../../qemu-api" } qemu_api_macros =3D { path =3D "../../../qemu-api-macros" } +hwcore =3D { path =3D "../../../hw/core" } =20 [lints] workspace =3D true diff --git a/rust/hw/timer/hpet/meson.build b/rust/hw/timer/hpet/meson.build index c4ffe020f6b..e6f99b67785 100644 --- a/rust/hw/timer/hpet/meson.build +++ b/rust/hw/timer/hpet/meson.build @@ -12,6 +12,7 @@ _libhpet_rs =3D static_library( qemu_api_macros, qom_rs, system_rs, + hwcore_rs, ], ) =20 diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/devi= ce.rs index 841c2ba3375..3031539744f 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -12,17 +12,15 @@ =20 use bql::{BqlCell, BqlRefCell}; use common::{bitops::IntegerExt, uninit_field_mut}; +use hwcore::{ + bindings::{qdev_prop_bit, qdev_prop_bool, qdev_prop_uint32, qdev_prop_= usize}, + declare_properties, define_property, DeviceImpl, DeviceMethods, Device= State, InterruptSource, + Property, ResetType, ResettablePhasesImpl, SysBusDevice, SysBusDeviceI= mpl, SysBusDeviceMethods, +}; use migration::{ self, impl_vmstate_struct, vmstate_fields, vmstate_of, vmstate_subsect= ions, vmstate_validate, VMStateDescription, VMStateDescriptionBuilder, }; -use qemu_api::{ - bindings::{qdev_prop_bit, qdev_prop_bool, qdev_prop_uint32, qdev_prop_= usize}, - irq::InterruptSource, - prelude::*, - qdev::{DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesI= mpl}, - sysbus::{SysBusDevice, SysBusDeviceImpl}, -}; use qom::{prelude::*, ObjectImpl, ParentField, ParentInit}; use system::{ bindings::{address_space_memory, address_space_stl_le, hwaddr}, @@ -904,9 +902,9 @@ impl ObjectImpl for HPETState { } =20 // TODO: Make these properties user-configurable! -qemu_api::declare_properties! { +declare_properties! { HPET_PROPERTIES, - qemu_api::define_property!( + define_property!( c"timers", HPETState, num_timers, @@ -914,7 +912,7 @@ impl ObjectImpl for HPETState { u8, default =3D HPET_MIN_TIMERS ), - qemu_api::define_property!( + define_property!( c"msi", HPETState, flags, @@ -923,7 +921,7 @@ impl ObjectImpl for HPETState { bit =3D HPET_FLAG_MSI_SUPPORT_SHIFT as u8, default =3D false, ), - qemu_api::define_property!( + define_property!( c"hpet-intcap", HPETState, int_route_cap, @@ -931,7 +929,7 @@ impl ObjectImpl for HPETState { u32, default =3D 0 ), - qemu_api::define_property!( + define_property!( c"hpet-offset-saved", HPETState, hpet_offset_saved, @@ -1004,8 +1002,8 @@ impl ObjectImpl for HPETState { .build(); =20 // SAFETY: HPET_PROPERTIES is a valid Property array constructed with the -// qemu_api::declare_properties macro. -unsafe impl qemu_api::qdev::DevicePropertiesImpl for HPETState { +// hwcore::declare_properties macro. +unsafe impl hwcore::DevicePropertiesImpl for HPETState { const PROPERTIES: &'static [Property] =3D &HPET_PROPERTIES; } =20 diff --git a/rust/meson.build b/rust/meson.build index d8b71f55061..041b0a473e5 100644 --- a/rust/meson.build +++ b/rust/meson.build @@ -31,6 +31,7 @@ subdir('bql') subdir('qom') subdir('system') subdir('chardev') +subdir('hw/core') subdir('qemu-api') =20 subdir('hw') diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib= .rs index e643e57ebdf..830b4326985 100644 --- a/rust/qemu-api-macros/src/lib.rs +++ b/rust/qemu-api-macros/src/lib.rs @@ -272,24 +272,24 @@ macro_rules! str_to_c_str { }, )?; let field_ty =3D field.ty.clone(); - let qdev_prop =3D quote! { <#field_ty as ::qemu_api::qdev::QDevPro= p>::VALUE }; + let qdev_prop =3D quote! { <#field_ty as ::hwcore::QDevProp>::VALU= E }; let set_default =3D defval.is_some(); let defval =3D defval.unwrap_or(syn::Expr::Verbatim(quote! { 0 })); properties_expanded.push(quote! { - ::qemu_api::bindings::Property { + ::hwcore::bindings::Property { name: ::std::ffi::CStr::as_ptr(#prop_name), info: #qdev_prop , offset: ::core::mem::offset_of!(#name, #field_name) as isi= ze, set_default: #set_default, - defval: ::qemu_api::bindings::Property__bindgen_ty_1 { u: = #defval as u64 }, + defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: #d= efval as u64 }, ..::common::Zeroable::ZERO } }); } =20 Ok(quote_spanned! {input.span() =3D> - unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for #name { - const PROPERTIES: &'static [::qemu_api::bindings::Property] = =3D &[ + unsafe impl ::hwcore::DevicePropertiesImpl for #name { + const PROPERTIES: &'static [::hwcore::bindings::Property] =3D = &[ #(#properties_expanded),* ]; } diff --git a/rust/qemu-api-macros/src/tests.rs b/rust/qemu-api-macros/src/t= ests.rs index 76e6c57479e..9ab7eab7f37 100644 --- a/rust/qemu-api-macros/src/tests.rs +++ b/rust/qemu-api-macros/src/tests.rs @@ -100,14 +100,14 @@ pub struct DummyState { } }, quote! { - unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for DummySt= ate { - const PROPERTIES: &'static [::qemu_api::bindings::Property= ] =3D &[ - ::qemu_api::bindings::Property { + unsafe impl ::hwcore::DevicePropertiesImpl for DummyState { + const PROPERTIES: &'static [::hwcore::bindings::Property] = =3D &[ + ::hwcore::bindings::Property { name: ::std::ffi::CStr::as_ptr(c"migrate_clock"), - info: ::VALUE, + info: ::VALUE, offset: ::core::mem::offset_of!(DummyState, migrat= e_clock) as isize, set_default: true, - defval: ::qemu_api::bindings::Property__bindgen_ty= _1 { u: true as u64 }, + defval: ::hwcore::bindings::Property__bindgen_ty_1= { u: true as u64 }, ..::common::Zeroable::ZERO } ]; @@ -127,14 +127,14 @@ pub struct DummyState { } }, quote! { - unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for DummySt= ate { - const PROPERTIES: &'static [::qemu_api::bindings::Property= ] =3D &[ - ::qemu_api::bindings::Property { + unsafe impl ::hwcore::DevicePropertiesImpl for DummyState { + const PROPERTIES: &'static [::hwcore::bindings::Property] = =3D &[ + ::hwcore::bindings::Property { name: ::std::ffi::CStr::as_ptr(c"migrate-clk"), - info: ::VALUE, + info: ::VALUE, offset: ::core::mem::offset_of!(DummyState, migrat= e_clock) as isize, set_default: true, - defval: ::qemu_api::bindings::Property__bindgen_ty= _1 { u: true as u64 }, + defval: ::hwcore::bindings::Property__bindgen_ty_1= { u: true as u64 }, ..::common::Zeroable::ZERO } ]; diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml index 2884c1d460b..9e7afc7e3ad 100644 --- a/rust/qemu-api/Cargo.toml +++ b/rust/qemu-api/Cargo.toml @@ -16,6 +16,7 @@ rust-version.workspace =3D true [dependencies] common =3D { path =3D "../common" } chardev =3D { path =3D "../chardev" } +hwcore =3D { path =3D "../hw/core" } migration =3D { path =3D "../migration" } util =3D { path =3D "../util" } bql =3D { path =3D "../bql" } diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build index 92e2581a64e..2dc638782ca 100644 --- a/rust/qemu-api/meson.build +++ b/rust/qemu-api/meson.build @@ -3,22 +3,12 @@ _qemu_api_cfg =3D run_command(rustc_args, capture: true, check: true).stdout().strip().splitlines() =20 c_enums =3D [ - 'DeviceCategory', - 'GpioPolarity', - 'MachineInitPhase', 'MemoryDeviceInfoKind', - 'ResetType', ] _qemu_api_bindgen_args =3D [] foreach enum : c_enums _qemu_api_bindgen_args +=3D ['--rustified-enum', enum] endforeach -c_bitfields =3D [ - 'ClockEvent', -] -foreach enum : c_bitfields - _qemu_api_bindgen_args +=3D ['--bitfield-enum', enum] -endforeach =20 blocked_type =3D [ 'Chardev', @@ -55,17 +45,14 @@ _qemu_api_rs =3D static_library( [ 'src/lib.rs', 'src/bindings.rs', - 'src/irq.rs', 'src/prelude.rs', - 'src/qdev.rs', - 'src/sysbus.rs', ], {'.' : _qemu_api_bindings_inc_rs}, ), override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, - dependencies: [anyhow_rs, bql_rs, chardev_rs, common_rs, foreign_rs, lib= c_rs, migration_rs, qemu_api_macros, + dependencies: [anyhow_rs, bql_rs, chardev_rs, common_rs, foreign_rs, hwc= ore_rs, libc_rs, migration_rs, qemu_api_macros, qom_rs, system_rs, util_rs, hwcore], ) =20 @@ -75,7 +62,7 @@ qemu_api_rs =3D declare_dependency(link_with: [_qemu_api_= rs], test('rust-qemu-api-integration', executable( 'rust-qemu-api-integration', - files('tests/tests.rs', 'tests/vmstate_tests.rs'), + files('tests/vmstate_tests.rs'), override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], rust_args: ['--test'], install: false, diff --git a/rust/qemu-api/src/bindings.rs b/rust/qemu-api/src/bindings.rs index 63b805c76e4..9c863e9b5b4 100644 --- a/rust/qemu-api/src/bindings.rs +++ b/rust/qemu-api/src/bindings.rs @@ -21,7 +21,6 @@ //! `bindgen`-generated declarations. =20 use chardev::bindings::Chardev; -use common::Zeroable; use migration::bindings::VMStateDescription; use qom::bindings::ObjectClass; use system::bindings::{device_endian, MemTxAttrs, MemoryRegion}; @@ -32,12 +31,3 @@ =20 #[cfg(not(MESON))] include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs")); - -unsafe impl Send for Property {} -unsafe impl Sync for Property {} - -unsafe impl Send for TypeInfo {} -unsafe impl Sync for TypeInfo {} - -unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {} -unsafe impl Zeroable for crate::bindings::Property {} diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs index 8d574404789..21b886035f3 100644 --- a/rust/qemu-api/src/lib.rs +++ b/rust/qemu-api/src/lib.rs @@ -13,10 +13,6 @@ #[rustfmt::skip] pub mod prelude; =20 -pub mod irq; -pub mod qdev; -pub mod sysbus; - // Allow proc-macros to refer to `::qemu_api` inside the `qemu_api` crate = (this // crate). extern crate self as qemu_api; diff --git a/rust/qemu-api/src/prelude.rs b/rust/qemu-api/src/prelude.rs index 9e9d1c82474..8db56f9f817 100644 --- a/rust/qemu-api/src/prelude.rs +++ b/rust/qemu-api/src/prelude.rs @@ -3,7 +3,3 @@ // SPDX-License-Identifier: GPL-2.0-or-later =20 //! Commonly used traits and types for QEMU. - -pub use crate::qdev::DeviceMethods; - -pub use crate::sysbus::SysBusDeviceMethods; --=20 2.51.0