rust: add qdev Device derive macro
PATCH v3 1/1 rust: add qdev Device derive macro
Add derive macro for declaring qdev properties directly above the field definitions. To do this, we split DeviceImpl::properties method on a separate trait so we can implement only that part in the derive macro expansion (we cannot partially implement the DeviceImpl trait).
Adding a property attribute above the field declaration will generate a qemu_api::bindings::Property array member in the device's property list.
---
This patch depends on patches that haven't been merged yet to master, so you can checkout this tree if you want to test it:
https://gitlab.com/epilys/qemu/-/tree/b4/rust-qdev-properties
TODOs:
- Update hpet code to use the derive macro
Changes in v3:
- Change MacroError use to syn::Error, depends on <[email protected]>
- Add derive macro unit test, depends on <20250704-rust_add_derive_macro_unit_tests-v1-0-ebd47fa7f78f@linaro.org>
- Rename derive macro to just Device
- Fixed clippy warnings (shadowing of var declarations etc)
- Fixed unnecessary quote_spanned uses (thanks Paolo)
- Removed unnecessary .as_ref() (Paolo)
- Refactor CString conversion for property name (Paolo)
- use Derive macro for empty property impls (Paolo)
- Remove qdev_prop/bitnr attributes, add them in future work
- Expand doc comment of QdevProp trait to explain about extern static
refs
- Mark DevicePropertiesImpl trait as unsafe
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- Rewrite to take advantage of const_refs_to_static feature, we still
need to update to a newer MSRV.
- Use existing get_fields function (Paolo)
- return errors instead of panicking (Paolo)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
--- rust/hw/char/pl011/src/device.rs | 11 ++- rust/hw/char/pl011/src/device_class.rs | 26 +----- rust/hw/timer/hpet/src/device.rs | 5 +- rust/qemu-api-macros/src/lib.rs | 151 ++++++++++++++++++++++++++++++++- rust/qemu-api-macros/src/tests.rs | 111 ++++++++++++++++++++++++ rust/qemu-api/src/qdev.rs | 68 +++++++++++++-- rust/qemu-api/tests/tests.rs | 24 ++---- 7 files changed, 334 insertions(+), 62 deletions(-)
rust/hw/char/pl011/src/device.rs +5 -6
@@ -12,7 +12,7 @@ log_mask_ln, memory::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder}, prelude::*, - qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesImpl}, + qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, ResetType, ResettablePhasesImpl}, qom::{ObjectImpl, Owned, ParentField, ParentInit}, static_assert, sysbus::{SysBusDevice, SysBusDeviceImpl},
@@ -101,12 +101,13 @@ pub struct PL011Registers { }#[repr( C )] -#[derive(qemu_api_macros:: Object )] +#[derive(qemu_api_macros:: Object , qemu_api_macros:: Device )] /// PL011 Device Model in QEMU pub struct PL011State {pub parent_obj:ParentField <SysBusDevice >,pub iomem:MemoryRegion ,#[doc(alias = "chr" )] +#[property(rename = "chardev" )] pub char_backend:CharBackend ,pub regs:BqlRefCell <PL011Registers >,/// QEMU interrupts
@@ -125,6 +126,7 @@ pub struct PL011State {#[doc(alias = "clk" )] pub clock: Owned<Clock>,#[doc(alias = "migrate_clk" )] +#[property(rename = "migrate-clk" , default = true )] pub migrate_clock: bool, }
@@ -172,9 +174,6 @@ impl ObjectImpl for PL011State { }impl DeviceImpl for PL011State { -fn properties () -> &'static [Property ] { - &device_class::PL011_PROPERTIES - }fn vmsd () ->Option <&'staticVMStateDescription > {Some (&device_class::VMSTATE_PL011 ) }
@@ -686,7 +685,7 @@ pub fn post_load(&self, _version_id: u32) -> Result<(), ()> { }#[repr( C )] -#[derive(qemu_api_macros:: Object )] +#[derive(qemu_api_macros:: Object , qemu_api_macros:: Device )] /// PL011 Luminary device model. pub struct PL011Luminary { parent_obj:ParentField <PL011State >,
rust/hw/char/pl011/src/device_class.rs +2 -24
@@ -8,11 +8,8 @@ };use qemu_api::{ - bindings::{qdev_prop_bool, qdev_prop_chr}, - prelude::*, - vmstate::VMStateDescription , - vmstate_clock, vmstate_fields, vmstate_of, vmstate_struct, vmstate_subsections, vmstate_unused, - zeroable::Zeroable , + prelude::*, vmstate::VMStateDescription , vmstate_clock, vmstate_fields, vmstate_of, + vmstate_struct, vmstate_subsections, vmstate_unused, zeroable::Zeroable , };use crate ::device::{PL011Registers ,PL011State };
@@ -82,22 +79,3 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int { }, ..Zeroable ::ZERO }; - -qemu_api::declare_properties! { -PL011_PROPERTIES , - qemu_api::define_property!( -c"chardev" , -PL011State , - char_backend, -unsafe { &qdev_prop_chr }, -CharBackend - ), - qemu_api::define_property!( -c"migrate-clk" , -PL011State , - migrate_clock, -unsafe { &qdev_prop_bool }, -bool , -default =true - ), -}
rust/hw/timer/hpet/src/device.rs +4 -1
@@ -1031,11 +1031,14 @@ impl ObjectImpl for HPETState { ..Zeroable ::ZERO }; -impl DeviceImpl for HPETState { +// SAFETY: HPET_PROPERTIES is a valid Property array constructed with the qemu_api::declare_properties macro. +unsafe impl qemu_api::qdev::DevicePropertiesImpl for HPETState {fn properties () -> &'static [Property ] { &HPET_PROPERTIES } +} +impl DeviceImpl for HPETState {fn vmsd () ->Option <&'staticVMStateDescription > {Some (&VMSTATE_HPET ) }
rust/qemu-api-macros/src/lib.rs +148 -3
164 lines not on this page · raw message
rust/qemu-api-macros/src/tests.rs +111 -0
117 lines not on this page · raw message
rust/qemu-api/src/qdev.rs +59 -9
87 lines not on this page · raw message
rust/qemu-api/tests/tests.rs +5 -19
@@ -5,11 +5,10 @@use std::{ffi::CStr , ptr::addr_of};use qemu_api::{ - bindings::{module_call_init, module_init_type, qdev_prop_bool}, + bindings::{module_call_init, module_init_type}, cell::{self,BqlCell }, - declare_properties, define_property, prelude::*, - qdev::{DeviceImpl ,DeviceState ,Property ,ResettablePhasesImpl }, + qdev::{DeviceImpl ,DeviceState ,ResettablePhasesImpl }, qom::{ObjectImpl ,ParentField }, sysbus::SysBusDevice , vmstate::VMStateDescription ,
@@ -26,9 +25,10 @@ };#[repr( C )] -#[derive(qemu_api_macros:: Object )] +#[derive(qemu_api_macros:: Object , qemu_api_macros:: Device )] pub struct DummyState { parent:ParentField <DeviceState >, +#[property(rename = "migrate-clk" , default = true )] migrate_clock:bool , }
@@ -44,17 +44,6 @@ pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) { } } -declare_properties! { -DUMMY_PROPERTIES , -define_property! ( -c"migrate-clk" , -DummyState , - migrate_clock, -unsafe { &qdev_prop_bool }, -bool - ), -} -unsafe impl ObjectType for DummyState {type Class =DummyClass ;const TYPE_NAME : &'staticCStr =c"dummy" ;
@@ -69,16 +58,13 @@ impl ObjectImpl for DummyState {impl ResettablePhasesImpl for DummyState {}impl DeviceImpl for DummyState { -fn properties () -> &'static [Property ] { - &DUMMY_PROPERTIES - }fn vmsd () ->Option <&'staticVMStateDescription > {Some (&VMSTATE ) } }#[repr( C )] -#[derive(qemu_api_macros:: Object )] +#[derive(qemu_api_macros:: Object , qemu_api_macros:: Device )] pub struct DummyChildState { parent:ParentField <DummyState >, }
---
base-commit: c77283dd5d79149f4e7e9edd00f65416c648ee59
change-id: 20250522-rust-qdev-properties-728e8f6a468e
prerequisite-change-id: 20250703-rust_bindings_allow_unnecessary_transmutes-d614db4517a4:v1
prerequisite-patch-id: 570fede8eee168ade58c7c7599bdc8b94c8c1a22
prerequisite-change-id: 20250704-rust_add_derive_macro_unit_tests-3e7daf905d62:v1
prerequisite-patch-id: 08d7aa46ded5f33efaf4e9d450f1080854cb6b2d
prerequisite-patch-id: 48db905333ffc7c9725cf72d646cc265a9d9ef36
prerequisite-change-id: 20250703-rust_macros-53d9e6a0b802:v1
prerequisite-patch-id: 985f50552aa858d14dcbc709d87e65d21538747d
--
γαῖα πυρί μιχθήτω
Paolo Bonzini
AppliedQuoted 10 lines
Add derive macro for declaring qdev properties directly above the field definitions. To do this, we split
DeviceImpl::propertiesmethod on a separate trait so we can implement only that part in the derive macro expansion (we cannot partially implement the DeviceImpl trait).Adding a
propertyattribute above the field declaration will generate aqemu_api::bindings::Propertyarray member in the device's property list.Signed-off-by Manos Pitsidianakis <[email protected]>
Applied to branch rust-next of https://gitlab.com/bonzini/qemu, thanks!
Quoted 1 line
This patch depends on patches that haven't been merged yet to master
And especially on bumping the minimum supported Rust version to 1.83, like the pending vmstate work which is sitting in the branch. I fixed the conflicts and rebased it on top of this patch.
Paolo