Skip to contentSkip to conversation

Next round of qemu_api patches

PATCH In review Paolo Bonzini 2 participants 26 messages Dec 20, 2024 – Dec 27, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Next round of qemu_api patches

This includes:

1) the outcome of reviewing
https://lore.kernel.org/qemu-devel/[email protected]/T/,
especially with respect to instance_finalize implementation and Rust-defined subclasses (patches 1-6)

2) the beginnings of separating instance_init/instance_post_init, making the latter take &self (patches 7-9)

3) extracting pl011's logic to convert an integer into an enum into a #[derive(TryInto)] procedural macro (patches 10-11)

4) a one-off change that I noticed while reading Cargo documentation (patch 12)

Zhao: I have placed these in rust-next, but feel free to ignore them for your next HPET submission. I have also included in my tree your test additions for usage in CI, but I'm not including them here to give you the opportunity to post them with a commit message and a signed-off-by. If you don't have time, it's in safe hands. :)

Paolo

Paolo Bonzini (12):
rust: qom: add ParentField
rust: add a utility module for compile-time type checks
rust: macros: check that the first field of a #[derive(Object)] struct
is a ParentField
rust: macros: check that #[derive(Object)] requires #[repr(C)]
rust: qom: automatically use Drop trait to implement instance_finalize
rust: qom: move device_id to PL011 class side
rust: pl011: only leave embedded object initialization in
instance_init
rust: qom: make INSTANCE_POST_INIT take a shared reference
rust: qdev: expose inherited methods to subclasses of SysBusDevice
rust: qemu-api-macros: extend error reporting facility to parse errors
rust: qemu-api-macros: add automatic TryFrom/TryInto derivation
rust: hide warnings for subprojects

 rust/hw/char/pl011/src/device.rs              |  79 +++++----
 rust/hw/char/pl011/src/lib.rs                 |  28 +---
 rust/qemu-api-macros/src/lib.rs               | 150 +++++++++++++-----
 rust/qemu-api-macros/src/utils.rs             |  26 +++
 rust/qemu-api/meson.build                     |   1 +
 rust/qemu-api/src/assertions.rs               |  90 +++++++++++
 rust/qemu-api/src/irq.rs                      |   3 +-
 rust/qemu-api/src/lib.rs                      |   1 +
 rust/qemu-api/src/prelude.rs                  |   2 +
 rust/qemu-api/src/qom.rs                      |  77 +++++++--
 rust/qemu-api/src/sysbus.rs                   |  23 ++-
 rust/qemu-api/tests/tests.rs                  |   4 +-
 subprojects/arbitrary-int-1-rs.wrap           |   3 +
 subprojects/bilge-0.2-rs.wrap                 |   3 +
 subprojects/bilge-impl-0.2-rs.wrap            |   3 +
 subprojects/either-1-rs.wrap                  |   3 +
 subprojects/itertools-0.11-rs.wrap            |   3 +
 .../arbitrary-int-1-rs/meson.build            |   1 +
 .../packagefiles/bilge-0.2-rs/meson.build     |   1 +
 .../bilge-impl-0.2-rs/meson.build             |   1 +
 .../packagefiles/either-1-rs/meson.build      |   1 +
 .../itertools-0.11-rs/meson.build             |   1 +
 .../proc-macro-error-1-rs/meson.build         |   1 +
 .../proc-macro-error-attr-1-rs/meson.build    |   1 +
 .../packagefiles/proc-macro2-1-rs/meson.build |   1 +
 .../packagefiles/quote-1-rs/meson.build       |   1 +
 subprojects/packagefiles/syn-2-rs/meson.build |   1 +
 .../unicode-ident-1-rs/meson.build            |   1 +
 subprojects/proc-macro-error-1-rs.wrap        |   3 +
 subprojects/proc-macro-error-attr-1-rs.wrap   |   3 +
 subprojects/proc-macro2-1-rs.wrap             |   3 +
 subprojects/quote-1-rs.wrap                   |   3 +
 subprojects/syn-2-rs.wrap                     |   3 +
 subprojects/unicode-ident-1-rs.wrap           |   3 +
 subprojects/unicode-ident-1-rs/meson.build    |  20 ---
 35 files changed, 402 insertions(+), 146 deletions(-)
 create mode 100644 rust/qemu-api-macros/src/utils.rs
 create mode 100644 rust/qemu-api/src/assertions.rs
 delete mode 100644 subprojects/unicode-ident-1-rs/meson.build
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/12 rust: qom: add ParentField

Add a type that, together with the C function object_deinit, ensures the correct drop order for QOM objects relative to their superclasses.

Right now it is not possible to implement the Drop trait for QOM classes that are defined in Rust, as the drop() function would not be called when the object goes away; instead what is called is ObjectImpl::INSTANCE_FINALIZE. It would be nice for INSTANCE_FINALIZE to just drop the object, but this has a problem: suppose you have

   pub struct MySuperclass {
       parent: DeviceState,
       field: Box<MyData>,
       ...
   }

   impl Drop for MySuperclass {
       ...
   }

   pub struct MySubclass {
       parent: MySuperclass,
       ...
   }

and an instance_finalize implementation that is like

    unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
        unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
    }

When instance_finalize is called for MySubclass, it will walk the struct's list of fields and call the drop method for MySuperclass. Then, object_deinit recurses to the superclass and calls the same drop method again. This will cause double-freeing of the Box<Data>.

What's happening here is that QOM wants to control the drop order of MySuperclass and MySubclass's fields. To do so, the parent field must be marked ManuallyDrop<>, which is quite ugly. Instead, add a wrapper type ParentField<> that is specific to QOM. This hides the implementation detail of *what* is special about the ParentField, and will also be easy to check in the #[derive(Object)] macro.

---
 rust/hw/char/pl011/src/device.rs |  6 ++--
 rust/qemu-api/src/qom.rs         | 56 +++++++++++++++++++++++++++++---
 rust/qemu-api/tests/tests.rs     |  4 +--
 3 files changed, 57 insertions(+), 9 deletions(-)
rust/hw/char/pl011/src/device.rs +3 -3
index 18cc122951d..689202f4550 100644
@@ -14,7 +14,7 @@
     irq::InterruptSource,
     prelude::*,
     qdev::DeviceImpl,
-    qom::ObjectImpl,
+    qom::{ObjectImpl, ParentField},
 };
 
 use crate::{
@@ -86,7 +86,7 @@ fn index(&self, idx: u32) -> &Self::Output {
 #[derive(Debug, qemu_api_macros::Object, qemu_api_macros::offsets)]
 /// PL011 Device Model in QEMU
 pub struct PL011State {
-    pub parent_obj: SysBusDevice,
+    pub parent_obj: ParentField<SysBusDevice>,
     pub iomem: MemoryRegion,
     #[doc(alias = "fr")]
     pub flags: registers::Flags,
@@ -645,7 +645,7 @@ pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> {
 #[derive(Debug, qemu_api_macros::Object)]
 /// PL011 Luminary device model.
 pub struct PL011Luminary {
-    parent_obj: PL011State,
+    parent_obj: ParentField<PL011State>,
 }
 
 impl PL011Luminary {
rust/qemu-api/src/qom.rs +52 -4

80 lines not on this page · raw message

rust/qemu-api/tests/tests.rs +2 -2
index 1d2825b0986..526c3f4f8ea 100644
@@ -15,7 +15,7 @@
     declare_properties, define_property,
     prelude::*,
     qdev::{DeviceImpl, DeviceState, Property},
-    qom::ObjectImpl,
+    qom::{ObjectImpl, ParentField},
     vmstate::VMStateDescription,
     zeroable::Zeroable,
 };
@@ -31,7 +31,7 @@
 #[repr(C)]
 #[derive(qemu_api_macros::Object)]
 pub struct DummyState {
-    parent: DeviceState,
+    parent: ParentField<DeviceState>,
     migrate_clock: bool,
 }
 
Signature · 1 line
2.47.1
Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] rust: qom: add ParentField

Quoted 53 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:43 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 01/12] rust: qom: add ParentField
X-Mailer: git-send-email 2.47.1

Add a type that, together with the C function object_deinit, ensures the correct drop order for QOM objects relative to their superclasses.

Right now it is not possible to implement the Drop trait for QOM classes that are defined in Rust, as the drop() function would not be called when the object goes away; instead what is called is ObjectImpl::INSTANCE_FINALIZE. It would be nice for INSTANCE_FINALIZE to just drop the object, but this has a problem: suppose you have

   pub struct MySuperclass {
       parent: DeviceState,
       field: Box<MyData>,
       ...
   }

   impl Drop for MySuperclass {
       ...
   }

   pub struct MySubclass {
       parent: MySuperclass,
       ...
   }

and an instance_finalize implementation that is like

    unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
        unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
    }

When instance_finalize is called for MySubclass, it will walk the struct's list of fields and call the drop method for MySuperclass. Then, object_deinit recurses to the superclass and calls the same drop method again. This will cause double-freeing of the Box<Data>.

What's happening here is that QOM wants to control the drop order of MySuperclass and MySubclass's fields. To do so, the parent field must be marked ManuallyDrop<>, which is quite ugly. Instead, add a wrapper type ParentField<> that is specific to QOM. This hides the implementation detail of *what* is special about the ParentField, and will also be easy to check in the #[derive(Object)] macro.

---
 rust/hw/char/pl011/src/device.rs |  6 ++--
 rust/qemu-api/src/qom.rs         | 56 +++++++++++++++++++++++++++++---
 rust/qemu-api/tests/tests.rs     |  4 +--
 3 files changed, 57 insertions(+), 9 deletions(-)

...

Quoted 5 lines
 unsafe extern "C" fn rust_instance_init<T: ObjectImpl>(obj: *mut Object) {
     // SAFETY: obj is an instance of T, since rust_instance_init<T>
     // is called from QOM core as the instance_init function
rust/qemu-api/src/qom.rs +0 -0
@@ -151,8 +198,9 @@ fn as_ref(&self) -> &$parent {
 ///

rust/qemu-api/src/qom.rs fn as_ref(&self) -> &$parent { · line 206

Quoted 10 lines
rust/qemu-api/src/qom.rs +3 -2
 /// - the struct must be `#[repr(C)]`;
 ///
-/// - the first field of the struct must be of the instance struct corresponding
-///   to the superclass, which is `ObjectImpl::ParentType`
+/// - the first field of the struct must be of type
+///   [`ParentField<T>`](ParentField), where `T` is the parent type
+///   [`ObjectImpl::ParentType`]
 ///
 /// - likewise, the first field of the `Class` must be of the class struct
 ///   corresponding to the superclass, which is `ObjectImpl::ParentType::Class`.

I think the "likewise" word should be deleted as well, since Class' parent field doesn't need any wrapper because Class also doesn't have finalize method. The remaining description is clear enough.

Others look good to me!

#3 lore raw

PATCH 2/12 rust: add a utility module for compile-time type checks

In reply to Paolo Bonzini

It is relatively common in the low-level qemu_api code to assert that a field of a struct has a specific type; for example, it can be used to ensure that the fields match what the qemu_api and C code expects for safety.

---
 rust/qemu-api/meson.build       |  1 +
 rust/qemu-api/src/assertions.rs | 90 +++++++++++++++++++++++++++++++++
 rust/qemu-api/src/lib.rs        |  1 +
 3 files changed, 92 insertions(+)
 create mode 100644 rust/qemu-api/src/assertions.rs
rust/qemu-api/meson.build +1 -0
index 9425ba7100c..60944a657de 100644
@@ -15,6 +15,7 @@ _qemu_api_rs = static_library(
   structured_sources(
     [
       'src/lib.rs',
+      'src/assertions.rs',
       'src/bindings.rs',
       'src/bitops.rs',
       'src/callbacks.rs',
rust/qemu-api/src/assertions.rs new file +90 -0

90 lines not on this page · raw message

rust/qemu-api/src/lib.rs +1 -0
index 4b43e02c0f9..83c6a987c05 100644
@@ -12,6 +12,7 @@
 #[rustfmt::skip]
 pub mod prelude;
 
+pub mod assertions;
 pub mod bitops;
 pub mod c_str;
 pub mod callbacks;
Signature · 1 line
2.47.1
Reviewed-by on v1 2/12
#4 lore raw

Re: [PATCH 02/12] rust: add a utility module for compile-time type checks

Quoted 11 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:44 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 02/12] rust: add a utility module for compile-time type
checks
X-Mailer: git-send-email 2.47.1

It is relatively common in the low-level qemu_api code to assert that a field of a struct has a specific type; for example, it can be used to ensure that the fields match what the qemu_api and C code expects for safety.

Quoted 8 lines
---
 rust/qemu-api/meson.build       |  1 +
 rust/qemu-api/src/assertions.rs | 90 +++++++++++++++++++++++++++++++++
 rust/qemu-api/src/lib.rs        |  1 +
 3 files changed, 92 insertions(+)
 create mode 100644 rust/qemu-api/src/assertions.rs

Very useful! Previously I found qdev property macro lacks such type check, but I falied to think of a good way to implement type_check in Rust, and glad to see the correct approach! Besides qdev property, I think vmstate also needs this.

And I think we can make the examples as the unit tests.

#5 lore raw

Re: [PATCH 02/12] rust: add a utility module for compile-time type checks

Quoted 21 lines · Zhao Liu
Quoted 11 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:44 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 02/12] rust: add a utility module for compile-time type
checks
X-Mailer: git-send-email 2.47.1

It is relatively common in the low-level qemu_api code to assert that a field of a struct has a specific type; for example, it can be used to ensure that the fields match what the qemu_api and C code expects for safety.

Quoted 8 lines
---
  rust/qemu-api/meson.build       |  1 +
  rust/qemu-api/src/assertions.rs | 90 +++++++++++++++++++++++++++++++++
  rust/qemu-api/src/lib.rs        |  1 +
  3 files changed, 92 insertions(+)

create mode 100644 rust/qemu-api/src/assertions.rs

Quoted 4 lines

Very useful! Previously I found qdev property macro lacks such type check, but I falied to think of a good way to implement type_check in Rust, and glad to see the correct approach! Besides qdev property, I think vmstate also needs this.

Right, though for vmstate (and probably qdev properties too) it's probably better to go from types to C structs (PropertyInfo/VMStateField) and avoid having to do the assertions.

Quoted 1 line

And I think we can make the examples as the unit tests.

Having doctests support in "make check" is on my list; I have started contributing a rust.doctest() function to Meson for that purpose. For now they are limited to what can be run through Cargo (i.e. you cannot use libqemuutil.a functions) but we'll see if the function is accepted.

Paolo

Quoted 4 lines
#6 lore raw

PATCH 3/12 rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField

In reply to Paolo Bonzini
---
 rust/qemu-api-macros/src/lib.rs | 61 +++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 25 deletions(-)
rust/qemu-api-macros/src/lib.rs +36 -25
index 74a8bc7503e..778b698be45 100644
@@ -19,6 +19,27 @@ fn from(err: CompileError) -> Self {
     }
 }
 
+fn get_fields<'a>(
+    input: &'a DeriveInput,
+    msg: &str,
+) -> Result<&'a Punctuated<Field, Comma>, CompileError> {
+    if let Data::Struct(s) = &input.data {
+        if let Fields::Named(fs) = &s.fields {
+            Ok(&fs.named)
+        } else {
+            Err(CompileError(
+                format!("Named fields required for {}", msg),
+                input.ident.span(),
+            ))
+        }
+    } else {
+        Err(CompileError(
+            format!("Struct required for {}", msg),
+            input.ident.span(),
+        ))
+    }
+}
+
 fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
     let expected = parse_quote! { #[repr(C)] };
 
@@ -32,38 +53,28 @@ fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
     }
 }
 
-#[proc_macro_derive(Object)]
-pub fn derive_object(input: TokenStream) -> TokenStream {
-    let input = parse_macro_input!(input as DeriveInput);
-    let name = input.ident;
+fn derive_object_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError> {
+    let name = &input.ident;
+    let parent = &get_fields(&input, "#[derive(Object)]")?[0].ident;
+
+    Ok(quote! {
+        ::qemu_api::assert_field_type!(#name, #parent,
+            ::qemu_api::qom::ParentField<<#name as ::qemu_api::qom::ObjectImpl>::ParentType>);
 
-    let expanded = quote! {
         ::qemu_api::module_init! {
             MODULE_INIT_QOM => unsafe {
                 ::qemu_api::bindings::type_register_static(&<#name as ::qemu_api::qom::ObjectImpl>::TYPE_INFO);
             }
         }
-    };
-
-    TokenStream::from(expanded)
+    })
 }
 
-fn get_fields(input: &DeriveInput) -> Result<&Punctuated<Field, Comma>, CompileError> {
-    if let Data::Struct(s) = &input.data {
-        if let Fields::Named(fs) = &s.fields {
-            Ok(&fs.named)
-        } else {
-            Err(CompileError(
-                "Cannot generate offsets for unnamed fields.".to_string(),
-                input.ident.span(),
-            ))
-        }
-    } else {
-        Err(CompileError(
-            "Cannot generate offsets for union or enum.".to_string(),
-            input.ident.span(),
-        ))
-    }
+#[proc_macro_derive(Object)]
+pub fn derive_object(input: TokenStream) -> TokenStream {
+    let input = parse_macro_input!(input as DeriveInput);
+    let expanded = derive_object_or_error(input).unwrap_or_else(Into::into);
+
+    TokenStream::from(expanded)
 }
 
 #[rustfmt::skip::macros(quote)]
@@ -71,7 +82,7 @@ fn derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStrea
     is_c_repr(&input, "#[derive(offsets)]")?;
 
     let name = &input.ident;
-    let fields = get_fields(&input)?;
+    let fields = get_fields(&input, "#[derive(offsets)]")?;
     let field_names: Vec<&Ident> = fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();
     let field_types: Vec<&Type> = fields.iter().map(|f| &f.ty).collect();
     let field_vis: Vec<&Visibility> = fields.iter().map(|f| &f.vis).collect();
Signature · 1 line
2.47.1
Reviewed-by on v1 3/12
#7 lore raw

Re: [PATCH 03/12] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField

Quoted 10 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:45 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 03/12] rust: macros: check that the first field of a
#[derive(Object)] struct is a ParentField
X-Mailer: git-send-email 2.47.1

---
 rust/qemu-api-macros/src/lib.rs | 61 +++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 25 deletions(-)

Good idea!

#8 lore raw

PATCH 4/12 rust: macros: check that #[derive(Object)] requires #[repr(C)]

In reply to Paolo Bonzini
---
 rust/qemu-api-macros/src/lib.rs | 2 ++
 1 file changed, 2 insertions(+)
rust/qemu-api-macros/src/lib.rs +2 -0
index 778b698be45..0f04cca3841 100644
@@ -54,6 +54,8 @@ fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
 }
 
 fn derive_object_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError> {
+    is_c_repr(&input, "#[derive(Object)]")?;
+
     let name = &input.ident;
     let parent = &get_fields(&input, "#[derive(Object)]")?[0].ident;
 
Signature · 1 line
2.47.1
Reviewed-by on v1 4/12
#9 lore raw

Re: [PATCH 04/12] rust: macros: check that #[derive(Object)] requires #[repr(C)]

Quoted 11 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:46 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 04/12] rust: macros: check that #[derive(Object)] requires
#[repr(C)]
X-Mailer: git-send-email 2.47.1

---
 rust/qemu-api-macros/src/lib.rs | 2 ++
 1 file changed, 2 insertions(+)

LGTM,

(I think this patch would be best placed before patch 3. What do you
think?)

#10 lore raw

PATCH 5/12 rust: qom: automatically use Drop trait to implement instance_finalize

In reply to Paolo Bonzini

Replace the customizable INSTANCE_FINALIZE with a generic function that drops the Rust object.

---
 rust/qemu-api/src/qom.rs | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
rust/qemu-api/src/qom.rs +11 -2
index 1341a173893..861f1e50ac4 100644
@@ -180,6 +180,16 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
     T::class_init(unsafe { &mut *klass.cast::<T::Class>() })
 }
 
+unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
+    // SAFETY: obj is an instance of T, since drop_object<T> is called
+    // from the QOM core function object_deinit() as the instance_finalize
+    // function for class T.  Note that while object_deinit() will drop the
+    // superclass field separately after this function returns, `T` must
+    // implement the unsafe trait ObjectType; the safety rules for the
+    // trait mandate that the parent field is manually dropped.
+    unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
+}
+
 /// Trait exposed by all structs corresponding to QOM objects.
 ///
 /// # Safety
@@ -438,7 +448,6 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
 
     /// Whether the object can be instantiated
     const ABSTRACT: bool = false;
-    const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
 
     /// Function that is called to initialize an object.  The parent class will
     /// have already been initialized so the type is only responsible for
@@ -474,7 +483,7 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
             None => None,
             Some(_) => Some(rust_instance_post_init::<Self>),
         },
-        instance_finalize: Self::INSTANCE_FINALIZE,
+        instance_finalize: Some(drop_object::<Self>),
         abstract_: Self::ABSTRACT,
         class_size: core::mem::size_of::<Self::Class>(),
         class_init: Some(rust_class_init::<Self>),
Signature · 1 line
2.47.1
#11 lore raw

PATCH 6/12 rust: qom: move device_id to PL011 class side

In reply to Paolo Bonzini

There is no need to monkeypatch DeviceId::Luminary into the already-initialized PL011State. Instead, now that we can define a class hierarchy, we can define PL011Class and make device_id a field in there.

There is also no need anymore to have "Arm" as zero, so change DeviceId into a wrapper for the array; all it does is provide an Index<hwaddr> implementation because arrays can only be indexed by usize.

---
 rust/hw/char/pl011/src/device.rs | 59 +++++++++++++++-----------------
 1 file changed, 28 insertions(+), 31 deletions(-)
rust/hw/char/pl011/src/device.rs +28 -31
index 689202f4550..215f94a6e4a 100644
@@ -5,7 +5,7 @@
 use core::ptr::{addr_of_mut, NonNull};
 use std::{
     ffi::CStr,
-    os::raw::{c_int, c_uchar, c_uint, c_void},
+    os::raw::{c_int, c_uint, c_void},
 };
 
 use qemu_api::{
@@ -14,7 +14,7 @@
     irq::InterruptSource,
     prelude::*,
     qdev::DeviceImpl,
-    qom::{ObjectImpl, ParentField},
+    qom::{ClassInitImpl, ObjectImpl, ParentField},
 };
 
 use crate::{
@@ -33,27 +33,20 @@
 /// QEMU sourced constant.
 pub const PL011_FIFO_DEPTH: u32 = 16;
 
-#[derive(Clone, Copy, Debug)]
-enum DeviceId {
-    #[allow(dead_code)]
-    Arm = 0,
-    Luminary,
-}
+#[derive(Clone, Copy)]
+struct DeviceId(&'static [u8; 8]);
 
 impl std::ops::Index<hwaddr> for DeviceId {
-    type Output = c_uchar;
+    type Output = u8;
 
     fn index(&self, idx: hwaddr) -> &Self::Output {
-        match self {
-            Self::Arm => &Self::PL011_ID_ARM[idx as usize],
-            Self::Luminary => &Self::PL011_ID_LUMINARY[idx as usize],
-        }
+        &self.0[idx as usize]
     }
 }
 
 impl DeviceId {
-    const PL011_ID_ARM: [c_uchar; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1];
-    const PL011_ID_LUMINARY: [c_uchar; 8] = [0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1];
+    const ARM: Self = Self(&[0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]);
+    const LUMINARY: Self = Self(&[0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1]);
 }
 
 // FIFOs use 32-bit indices instead of usize, for compatibility with
@@ -126,17 +119,28 @@ pub struct PL011State {
     pub clock: NonNull<Clock>,
     #[doc(alias = "migrate_clk")]
     pub migrate_clock: bool,
-    /// The byte string that identifies the device.
-    device_id: DeviceId,
 }
 
 qom_isa!(PL011State : SysBusDevice, DeviceState, Object);
 
+pub struct PL011Class {
+    parent_class: <SysBusDevice as ObjectType>::Class,
+    /// The byte string that identifies the device.
+    device_id: DeviceId,
+}
+
 unsafe impl ObjectType for PL011State {
-    type Class = <SysBusDevice as ObjectType>::Class;
+    type Class = PL011Class;
     const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
 }
 
+impl ClassInitImpl<PL011Class> for PL011State {
+    fn class_init(klass: &mut PL011Class) {
+        klass.device_id = DeviceId::ARM;
+        <Self as ClassInitImpl<SysBusDeviceClass>>::class_init(&mut klass.parent_class);
+    }
+}
+
 impl ObjectImpl for PL011State {
     type ParentType = SysBusDevice;
 
@@ -214,7 +218,8 @@ pub fn read(&mut self, offset: hwaddr, _size: c_uint) -> std::ops::ControlFlow<u
 
         let value = match RegisterOffset::try_from(offset) {
             Err(v) if (0x3f8..0x400).contains(&(v >> 2)) => {
-                u32::from(self.device_id[(offset - 0xfe0) >> 2])
+                let device_id = self.get_class().device_id;
+                u32::from(device_id[(offset - 0xfe0) >> 2])
             }
             Err(_) => {
                 // qemu_log_mask(LOG_GUEST_ERROR, "pl011_read: Bad offset 0x%x\n", (int)offset);
@@ -648,16 +653,10 @@ pub struct PL011Luminary {
     parent_obj: ParentField<PL011State>,
 }
 
-impl PL011Luminary {
-    /// Initializes a pre-allocated, unitialized instance of `PL011Luminary`.
-    ///
-    /// # Safety
-    ///
-    /// We expect the FFI user of this function to pass a valid pointer, that
-    /// has the same size as [`PL011Luminary`]. We also expect the device is
-    /// readable/writeable from one thread at any time.
-    unsafe fn init(&mut self) {
-        self.parent_obj.device_id = DeviceId::Luminary;
+impl ClassInitImpl<PL011Class> for PL011Luminary {
+    fn class_init(klass: &mut PL011Class) {
+        klass.device_id = DeviceId::LUMINARY;
+        <Self as ClassInitImpl<SysBusDeviceClass>>::class_init(&mut klass.parent_class);
     }
 }
 
@@ -670,8 +669,6 @@ unsafe impl ObjectType for PL011Luminary {
 
 impl ObjectImpl for PL011Luminary {
     type ParentType = PL011State;
-
-    const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
 }
 
 impl DeviceImpl for PL011Luminary {}
Signature · 1 line
2.47.1
Reviewed-by on v1 6/12
#12 lore raw

Re: [PATCH 06/12] rust: qom: move device_id to PL011 class side

Quoted 17 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:48 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 06/12] rust: qom: move device_id to PL011 class side
X-Mailer: git-send-email 2.47.1

There is no need to monkeypatch DeviceId::Luminary into the already-initialized PL011State. Instead, now that we can define a class hierarchy, we can define PL011Class and make device_id a field in there.

There is also no need anymore to have "Arm" as zero, so change DeviceId into a wrapper for the array; all it does is provide an Index<hwaddr> implementation because arrays can only be indexed by usize.

---
 rust/hw/char/pl011/src/device.rs | 59 +++++++++++++++-----------------
 1 file changed, 28 insertions(+), 31 deletions(-)

...

rust/hw/char/pl011/src/device.rs line 50

Quoted 7 lines

impl DeviceId {

rust/hw/char/pl011/src/device.rs +2 -2
-    const PL011_ID_ARM: [c_uchar; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1];
-    const PL011_ID_LUMINARY: [c_uchar; 8] = [0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1];
+    const ARM: Self = Self(&[0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]);
+    const LUMINARY: Self = Self(&[0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1]);
 }

It seems you didn't choose to hardcode device ID to save 3 lines of code :)

Both are fine for me,

#13 lore raw

PATCH 7/12 rust: pl011: only leave embedded object initialization in instance_init

In reply to Paolo Bonzini

Leave IRQ and MMIO initialization to instance_post_init. In Rust the two callbacks are more distinct, because only instance_post_init has a fully initialized object available.

While at it, add a wrapper for sysbus_init_mmio so that accesses to the SysBusDevice correctly use shared references.

---
 rust/hw/char/pl011/src/device.rs | 18 ++++++++++--------
 rust/qemu-api/src/sysbus.rs      | 12 ++++++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)
rust/hw/char/pl011/src/device.rs +10 -8
index 215f94a6e4a..72a4cea042c 100644
@@ -145,6 +145,7 @@ impl ObjectImpl for PL011State {
     type ParentType = SysBusDevice;
 
     const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
+    const INSTANCE_POST_INIT: Option<fn(&mut Self)> = Some(Self::post_init);
 }
 
 impl DeviceImpl for PL011State {
@@ -183,14 +184,6 @@ unsafe fn init(&mut self) {
                 Self::TYPE_NAME.as_ptr(),
                 0x1000,
             );
-
-            let sbd: &mut SysBusDevice = self.upcast_mut();
-            sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
-        }
-
-        for irq in self.interrupts.iter() {
-            let sbd: &SysBusDevice = self.upcast();
-            sbd.init_irq(irq);
         }
 
         // SAFETY:
@@ -213,6 +206,15 @@ unsafe fn init(&mut self) {
         }
     }
 
+    fn post_init(&mut self) {
+        let sbd: &SysBusDevice = self.upcast();
+
+        sbd.init_mmio(&self.iomem);
+        for irq in self.interrupts.iter() {
+            sbd.init_irq(irq);
+        }
+    }
+
     pub fn read(&mut self, offset: hwaddr, _size: c_uint) -> std::ops::ControlFlow<u64, u64> {
         use RegisterOffset::*;
 
rust/qemu-api/src/sysbus.rs +12 -0
index 8193734bde4..b96eaaf25f2 100644
@@ -38,6 +38,18 @@ const fn as_mut_ptr(&self) -> *mut SysBusDevice {
         addr_of!(*self) as *mut _
     }
 
+    /// Expose a memory region to the board so that it can give it an address
+    /// in guest memory.  Note that the ordering of calls to `init_mmio` is
+    /// important, since whoever creates the sysbus device will refer to the
+    /// region with a number that corresponds to the order of calls to
+    /// `init_mmio`.
+    pub fn init_mmio(&self, iomem: &bindings::MemoryRegion) {
+        assert!(bql_locked());
+        unsafe {
+            bindings::sysbus_init_mmio(self.as_mut_ptr(), addr_of!(*iomem) as *mut _);
+        }
+    }
+
     /// Expose an interrupt source outside the device as a qdev GPIO output.
     /// Note that the ordering of calls to `init_irq` is important, since
     /// whoever creates the sysbus device will refer to the interrupts with
Signature · 1 line
2.47.1
Reviewed-by on v1 7/12
#14 lore raw

Re: [PATCH 07/12] rust: pl011: only leave embedded object initialization in instance_init

Quoted 10 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:49 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 07/12] rust: pl011: only leave embedded object
initialization in instance_init
X-Mailer: git-send-email 2.47.1

Leave IRQ and MMIO initialization to instance_post_init. In Rust the two callbacks are more distinct, because only instance_post_init has a fully initialized object available.

Quoted 9 lines

While at it, add a wrapper for sysbus_init_mmio so that accesses to the SysBusDevice correctly use shared references.

---
 rust/hw/char/pl011/src/device.rs | 18 ++++++++++--------
 rust/qemu-api/src/sysbus.rs      | 12 ++++++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)
#15 lore raw

PATCH 8/12 rust: qom: make INSTANCE_POST_INIT take a shared reference

In reply to Paolo Bonzini
---
 rust/hw/char/pl011/src/device.rs | 4 ++--
 rust/qemu-api/src/qom.rs         | 8 ++------
 2 files changed, 4 insertions(+), 8 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -2
index 72a4cea042c..6792d13fb77 100644
@@ -145,7 +145,7 @@ impl ObjectImpl for PL011State {
     type ParentType = SysBusDevice;
 
     const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
-    const INSTANCE_POST_INIT: Option<fn(&mut Self)> = Some(Self::post_init);
+    const INSTANCE_POST_INIT: Option<fn(&Self)> = Some(Self::post_init);
 }
 
 impl DeviceImpl for PL011State {
@@ -206,7 +206,7 @@ unsafe fn init(&mut self) {
         }
     }
 
-    fn post_init(&mut self) {
+    fn post_init(&self) {
         let sbd: &SysBusDevice = self.upcast();
 
         sbd.init_mmio(&self.iomem);
rust/qemu-api/src/qom.rs +2 -6
index 861f1e50ac4..5934eb0cc50 100644
@@ -163,11 +163,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
     // SAFETY: obj is an instance of T, since rust_instance_post_init<T>
     // is called from QOM core as the instance_post_init function
     // for class T
-    //
-    // FIXME: it's not really guaranteed that there are no backpointers to
-    // obj; it's quite possible that they have been created by instance_init().
-    // The receiver should be &self, not &mut self.
-    T::INSTANCE_POST_INIT.unwrap()(unsafe { &mut *obj.cast::<T>() })
+    T::INSTANCE_POST_INIT.unwrap()(unsafe { &*obj.cast::<T>() })
 }
 
 unsafe extern "C" fn rust_class_init<T: ObjectType + ClassInitImpl<T::Class>>(
@@ -459,7 +455,7 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
 
     /// Function that is called to finish initialization of an object, once
     /// `INSTANCE_INIT` functions have been called.
-    const INSTANCE_POST_INIT: Option<fn(&mut Self)> = None;
+    const INSTANCE_POST_INIT: Option<fn(&Self)> = None;
 
     /// Called on descendent classes after all parent class initialization
     /// has occurred, but before the class itself is initialized.  This
Signature · 1 line
2.47.1
Reviewed-by on v1 8/12
#16 lore raw

Re: [PATCH 08/12] rust: qom: make INSTANCE_POST_INIT take a shared reference

Quoted 6 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:50 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 08/12] rust: qom: make INSTANCE_POST_INIT take a shared
reference
X-Mailer: git-send-email 2.47.1

Quoted 6 lines
---
 rust/hw/char/pl011/src/device.rs | 4 ++--
 rust/qemu-api/src/qom.rs         | 8 ++------
 2 files changed, 4 insertions(+), 8 deletions(-)
#17 lore raw

PATCH 9/12 rust: qdev: expose inherited methods to subclasses of SysBusDevice

In reply to Paolo Bonzini

The ObjectDeref trait now provides all the magic that is required to fake inheritance. Replace the "impl SysBusDevice" block of qemu_api::sysbus with a trait, so that sysbus_init_irq() can be invoked as "self.init_irq()" without any intermediate upcast.

---
 rust/hw/char/pl011/src/device.rs |  6 ++----
 rust/qemu-api/src/irq.rs         |  3 +--
 rust/qemu-api/src/prelude.rs     |  2 ++
 rust/qemu-api/src/sysbus.rs      | 17 +++++++++--------
 4 files changed, 14 insertions(+), 14 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -4
index 6792d13fb77..994c2fc0593 100644
@@ -207,11 +207,9 @@ unsafe fn init(&mut self) {
     }
 
     fn post_init(&self) {
-        let sbd: &SysBusDevice = self.upcast();
-
-        sbd.init_mmio(&self.iomem);
+        self.init_mmio(&self.iomem);
         for irq in self.interrupts.iter() {
-            sbd.init_irq(irq);
+            self.init_irq(irq);
         }
     }
 
rust/qemu-api/src/irq.rs +1 -2
index 6258141bdf0..378e5202951 100644
@@ -24,8 +24,7 @@
 ///
 /// Interrupts are implemented as a pointer to the interrupt "sink", which has
 /// type [`IRQState`].  A device exposes its source as a QOM link property using
-/// a function such as
-/// [`SysBusDevice::init_irq`](crate::sysbus::SysBusDevice::init_irq), and
+/// a function such as [`SysBusDeviceMethods::init_irq`], and
 /// initially leaves the pointer to a NULL value, representing an unconnected
 /// interrupt. To connect it, whoever creates the device fills the pointer with
 /// the sink's `IRQState *`, for example using `sysbus_connect_irq`.  Because
rust/qemu-api/src/prelude.rs +2 -0
index 6f32deeb2ed..4ea70b9c823 100644
@@ -16,3 +16,5 @@
 pub use crate::qom::ObjectType;
 
 pub use crate::qom_isa;
+
+pub use crate::sysbus::SysBusDeviceMethods;
rust/qemu-api/src/sysbus.rs +9 -8
index b96eaaf25f2..e6762b5c145 100644
@@ -32,18 +32,17 @@ fn class_init(sdc: &mut SysBusDeviceClass) {
     }
 }
 
-impl SysBusDevice {
-    /// Return `self` cast to a mutable pointer, for use in calls to C code.
-    const fn as_mut_ptr(&self) -> *mut SysBusDevice {
-        addr_of!(*self) as *mut _
-    }
-
+/// Trait for methods of [`SysBusDevice`] and its subclasses.
+pub trait SysBusDeviceMethods: ObjectDeref
+where
+    Self::Target: IsA<SysBusDevice>,
+{
     /// Expose a memory region to the board so that it can give it an address
     /// in guest memory.  Note that the ordering of calls to `init_mmio` is
     /// important, since whoever creates the sysbus device will refer to the
     /// region with a number that corresponds to the order of calls to
     /// `init_mmio`.
-    pub fn init_mmio(&self, iomem: &bindings::MemoryRegion) {
+    fn init_mmio(&self, iomem: &bindings::MemoryRegion) {
         assert!(bql_locked());
         unsafe {
             bindings::sysbus_init_mmio(self.as_mut_ptr(), addr_of!(*iomem) as *mut _);
@@ -54,10 +53,12 @@ pub fn init_mmio(&self, iomem: &bindings::MemoryRegion) {
     /// Note that the ordering of calls to `init_irq` is important, since
     /// whoever creates the sysbus device will refer to the interrupts with
     /// a number that corresponds to the order of calls to `init_irq`.
-    pub fn init_irq(&self, irq: &InterruptSource) {
+    fn init_irq(&self, irq: &InterruptSource) {
         assert!(bql_locked());
         unsafe {
             bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
         }
     }
 }
+
+impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {}
Signature · 1 line
2.47.1
Reviewed-by on v1 9/12
#18 lore raw

Re: [PATCH 09/12] rust: qdev: expose inherited methods to subclasses of SysBusDevice

Quoted 11 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:51 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 09/12] rust: qdev: expose inherited methods to subclasses
of SysBusDevice
X-Mailer: git-send-email 2.47.1

The ObjectDeref trait now provides all the magic that is required to fake inheritance. Replace the "impl SysBusDevice" block of qemu_api::sysbus with a trait, so that sysbus_init_irq() can be invoked as "self.init_irq()" without any intermediate upcast.

Quoted 7 lines
---
 rust/hw/char/pl011/src/device.rs |  6 ++----
 rust/qemu-api/src/irq.rs         |  3 +--
 rust/qemu-api/src/prelude.rs     |  2 ++
 rust/qemu-api/src/sysbus.rs      | 17 +++++++++--------
 4 files changed, 14 insertions(+), 14 deletions(-)
#19 lore raw

PATCH 10/12 rust: qemu-api-macros: extend error reporting facility to parse errors

In reply to Paolo Bonzini

Generalize the CompileError tuple to an enum, that can be either an error message or a parse error from syn.

---
 rust/qemu-api-macros/src/lib.rs   | 27 ++++++++++-----------------
 rust/qemu-api-macros/src/utils.rs | 26 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 17 deletions(-)
 create mode 100644 rust/qemu-api-macros/src/utils.rs
rust/qemu-api-macros/src/lib.rs +10 -17
index 0f04cca3841..539c48df298 100644
@@ -3,57 +3,50 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 use proc_macro::TokenStream;
-use proc_macro2::Span;
-use quote::{quote, quote_spanned};
+use quote::quote;
 use syn::{
     parse_macro_input, parse_quote, punctuated::Punctuated, token::Comma, Data, DeriveInput, Field,
     Fields, Ident, Type, Visibility,
 };
 
-struct CompileError(String, Span);
-
-impl From<CompileError> for proc_macro2::TokenStream {
-    fn from(err: CompileError) -> Self {
-        let CompileError(msg, span) = err;
-        quote_spanned! { span => compile_error!(#msg); }
-    }
-}
+mod utils;
+use utils::MacroError;
 
 fn get_fields<'a>(
     input: &'a DeriveInput,
     msg: &str,
-) -> Result<&'a Punctuated<Field, Comma>, CompileError> {
+) -> Result<&'a Punctuated<Field, Comma>, MacroError> {
     if let Data::Struct(s) = &input.data {
         if let Fields::Named(fs) = &s.fields {
             Ok(&fs.named)
         } else {
-            Err(CompileError(
+            Err(MacroError::Message(
                 format!("Named fields required for {}", msg),
                 input.ident.span(),
             ))
         }
     } else {
-        Err(CompileError(
+        Err(MacroError::Message(
             format!("Struct required for {}", msg),
             input.ident.span(),
         ))
     }
 }
 
-fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
+fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), MacroError> {
     let expected = parse_quote! { #[repr(C)] };
 
     if input.attrs.iter().any(|attr| attr == &expected) {
         Ok(())
     } else {
-        Err(CompileError(
+        Err(MacroError::Message(
             format!("#[repr(C)] required for {}", msg),
             input.ident.span(),
         ))
     }
 }
 
-fn derive_object_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError> {
+fn derive_object_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> {
     is_c_repr(&input, "#[derive(Object)]")?;
 
     let name = &input.ident;
@@ -80,7 +73,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream {
 }
 
 #[rustfmt::skip::macros(quote)]
-fn derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError> {
+fn derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> {
     is_c_repr(&input, "#[derive(offsets)]")?;
 
     let name = &input.ident;
rust/qemu-api-macros/src/utils.rs new file +26 -0
new file mode 100644
index 00000000000..02c91aed7f6
@@ -0,0 +1,26 @@
+// Procedural macro utilities.
+// Author(s): Paolo Bonzini <[email protected]>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+use proc_macro2::Span;
+use quote::quote_spanned;
+
+pub enum MacroError {
+    Message(String, Span),
+    ParseError(syn::Error),
+}
+
+impl From<syn::Error> for MacroError {
+    fn from(err: syn::Error) -> Self {
+        MacroError::ParseError(err)
+    }
+}
+
+impl From<MacroError> for proc_macro2::TokenStream {
+    fn from(err: MacroError) -> Self {
+        match err {
+            MacroError::Message(msg, span) => quote_spanned! { span => compile_error!(#msg); },
+            MacroError::ParseError(err) => err.into_compile_error(),
+        }
+    }
+}
Signature · 1 line
2.47.1
Reviewed-by on v1 10/12
#20 lore raw

Re: [PATCH 10/12] rust: qemu-api-macros: extend error reporting facility to parse errors

Quoted 9 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:52 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 10/12] rust: qemu-api-macros: extend error reporting
facility to parse errors
X-Mailer: git-send-email 2.47.1

Generalize the CompileError tuple to an enum, that can be either an error message or a parse error from syn.

Quoted 7 lines
---
 rust/qemu-api-macros/src/lib.rs   | 27 ++++++++++-----------------
 rust/qemu-api-macros/src/utils.rs | 26 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 17 deletions(-)
 create mode 100644 rust/qemu-api-macros/src/utils.rs
#21 lore raw

PATCH 11/12 rust: qemu-api-macros: add automatic TryFrom/TryInto derivation

In reply to Paolo Bonzini

This is going to be fairly common. Using a custom procedural macro provides better error messages and automatically finds the right type.

Note that this is different from the same-named macro in the derive_more crate. That one provides conversion from e.g. tuples to enums with tuple variants, not from integers to enums.

---
 rust/hw/char/pl011/src/lib.rs   | 28 +------------
 rust/qemu-api-macros/src/lib.rs | 74 ++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 29 deletions(-)
rust/hw/char/pl011/src/lib.rs +1 -27
index 69064d6929b..0a89d393e0f 100644
@@ -45,7 +45,7 @@
 #[doc(alias = "offset")]
 #[allow(non_camel_case_types)]
 #[repr(u64)]
-#[derive(Debug)]
+#[derive(Debug, qemu_api_macros::TryInto)]
 pub enum RegisterOffset {
     /// Data Register
     ///
@@ -102,32 +102,6 @@ pub enum RegisterOffset {
     //Reserved = 0x04C,
 }
 
-impl core::convert::TryFrom<u64> for RegisterOffset {
-    type Error = u64;
-
-    fn try_from(value: u64) -> Result<Self, Self::Error> {
-        macro_rules! case {
-            ($($discriminant:ident),*$(,)*) => {
-                /* check that matching on all macro arguments compiles, which means we are not
-                 * missing any enum value; if the type definition ever changes this will stop
-                 * compiling.
-                 */
-                const fn _assert_exhaustive(val: RegisterOffset) {
-                    match val {
-                        $(RegisterOffset::$discriminant => (),)*
-                    }
-                }
-
-                match value {
-                    $(x if x == Self::$discriminant as u64 => Ok(Self::$discriminant),)*
-                     _ => Err(value),
-                }
-            }
-        }
-        case! { DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, MIS, ICR, DMACR }
-    }
-}
-
 pub mod registers {
     //! Device registers exposed as typed structs which are backed by arbitrary
     //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc.
rust/qemu-api-macros/src/lib.rs +72 -2

83 lines not on this page · raw message

Signature · 1 line
2.47.1
Reviewed-by on v1 11/12
#22 lore raw

Re: [PATCH 11/12] rust: qemu-api-macros: add automatic TryFrom/TryInto derivation

Quoted 14 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:53 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 11/12] rust: qemu-api-macros: add automatic TryFrom/TryInto
derivation
X-Mailer: git-send-email 2.47.1

This is going to be fairly common. Using a custom procedural macro provides better error messages and automatically finds the right type.

Note that this is different from the same-named macro in the derive_more crate. That one provides conversion from e.g. tuples to enums with tuple variants, not from integers to enums.

Quoted 6 lines
---
 rust/hw/char/pl011/src/lib.rs   | 28 +------------
 rust/qemu-api-macros/src/lib.rs | 74 ++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 29 deletions(-)

This improvement is also very elegant.

#23 lore raw

PATCH 12/12 rust: hide warnings for subprojects

In reply to Paolo Bonzini

This matches cargo's own usage of "--cap-lints allow" when building dependencies.

---
 subprojects/arbitrary-int-1-rs.wrap           |  3 +++
 subprojects/bilge-0.2-rs.wrap                 |  3 +++
 subprojects/bilge-impl-0.2-rs.wrap            |  3 +++
 subprojects/either-1-rs.wrap                  |  3 +++
 subprojects/itertools-0.11-rs.wrap            |  3 +++
 .../arbitrary-int-1-rs/meson.build            |  1 +
 .../packagefiles/bilge-0.2-rs/meson.build     |  1 +
 .../bilge-impl-0.2-rs/meson.build             |  1 +
 .../packagefiles/either-1-rs/meson.build      |  1 +
 .../itertools-0.11-rs/meson.build             |  1 +
 .../proc-macro-error-1-rs/meson.build         |  1 +
 .../proc-macro-error-attr-1-rs/meson.build    |  1 +
 .../packagefiles/proc-macro2-1-rs/meson.build |  1 +
 .../packagefiles/quote-1-rs/meson.build       |  1 +
 subprojects/packagefiles/syn-2-rs/meson.build |  1 +
 .../unicode-ident-1-rs/meson.build            |  1 +
 subprojects/proc-macro-error-1-rs.wrap        |  3 +++
 subprojects/proc-macro-error-attr-1-rs.wrap   |  3 +++
 subprojects/proc-macro2-1-rs.wrap             |  3 +++
 subprojects/quote-1-rs.wrap                   |  3 +++
 subprojects/syn-2-rs.wrap                     |  3 +++
 subprojects/unicode-ident-1-rs.wrap           |  3 +++
 subprojects/unicode-ident-1-rs/meson.build    | 20 -------------------
 23 files changed, 44 insertions(+), 20 deletions(-)
 delete mode 100644 subprojects/unicode-ident-1-rs/meson.build
subprojects/arbitrary-int-1-rs.wrap +3 -0
index e580538a877..a1838b20b0f 100644
@@ -5,3 +5,6 @@ source_filename = arbitrary-int-1.2.7.tar.gz
 source_hash = c84fc003e338a6f69fbd4f7fe9f92b535ff13e9af8997f3b14b6ddff8b1df46d
 #method = cargo
 patch_directory = arbitrary-int-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/bilge-0.2-rs.wrap +3 -0
index 7a4339d2989..900bb1497b9 100644
@@ -5,3 +5,6 @@ source_filename = bilge-0.2.0.tar.gz
 source_hash = dc707ed8ebf81de5cd6c7f48f54b4c8621760926cdf35a57000747c512e67b57
 #method = cargo
 patch_directory = bilge-0.2-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/bilge-impl-0.2-rs.wrap +3 -0
index b24c34a9043..d14c3dc769b 100644
@@ -6,3 +6,6 @@ source_hash = feb11e002038ad243af39c2068c8a72bcf147acf05025dcdb916fcc000adb2d8
 #method = cargo
 patch_directory = bilge-impl-0.2-rs
 diff_files = bilge-impl-1.63.0.patch
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/either-1-rs.wrap +3 -0
index 6046712036c..352e11cfee6 100644
@@ -5,3 +5,6 @@ source_filename = either-1.12.0.tar.gz
 source_hash = 3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b
 #method = cargo
 patch_directory = either-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/itertools-0.11-rs.wrap +3 -0
index 66b05252cd5..ee12d0053bc 100644
@@ -5,3 +5,6 @@ source_filename = itertools-0.11.0.tar.gz
 source_hash = b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57
 #method = cargo
 patch_directory = itertools-0.11-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/packagefiles/arbitrary-int-1-rs/meson.build +1 -0
index cff3f62ce74..00733d1faab 100644
@@ -9,6 +9,7 @@ _arbitrary_int_rs = static_library(
   files('src/lib.rs'),
   gnu_symbol_visibility: 'hidden',
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
+  rust_args: ['--cap-lints', 'allow'],
   rust_abi: 'rust',
   dependencies: [],
 )
subprojects/packagefiles/bilge-0.2-rs/meson.build +1 -0
index e69bac91b40..ce13d0fe80f 100644
@@ -17,6 +17,7 @@ lib = static_library(
   'src/lib.rs',
   override_options : ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi : 'rust',
+  rust_args: ['--cap-lints', 'allow'],
   dependencies: [
     arbitrary_int_dep,
     bilge_impl_dep,
subprojects/packagefiles/bilge-impl-0.2-rs/meson.build +1 -0
index f8f3486fc08..42b03dcd53c 100644
@@ -25,6 +25,7 @@ _bilge_impl_rs = rust.proc_macro(
   files('src/lib.rs'),
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'use_fallback',
     '--cfg', 'feature="syn-error"',
     '--cfg', 'feature="proc-macro"',
subprojects/packagefiles/either-1-rs/meson.build +1 -0
index 608e64e31fc..04c96cc5fb3 100644
@@ -11,6 +11,7 @@ _either_rs = static_library(
   override_options: ['rust_std=2018', 'build.rust_std=2018'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'feature="use_std"',
     '--cfg', 'feature="use_alloc"',
   ],
subprojects/packagefiles/itertools-0.11-rs/meson.build +1 -0
index 30982a4ee76..2a3fbe9ee5a 100644
@@ -15,6 +15,7 @@ _itertools_rs = static_library(
   override_options: ['rust_std=2018', 'build.rust_std=2018'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'feature="use_std"',
     '--cfg', 'feature="use_alloc"',
   ],
subprojects/packagefiles/proc-macro-error-1-rs/meson.build +1 -0
index ae27a696862..10c2741085c 100644
@@ -20,6 +20,7 @@ _proc_macro_error_rs = static_library(
   override_options: ['rust_std=2018', 'build.rust_std=2018'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'use_fallback',
     '--cfg', 'feature="syn-error"',
     '--cfg', 'feature="proc-macro"',
subprojects/packagefiles/proc-macro-error-attr-1-rs/meson.build +1 -0
index 3281b264333..c4c4c5e397c 100644
@@ -16,6 +16,7 @@ _proc_macro_error_attr_rs = rust.proc_macro(
   files('src/lib.rs'),
   override_options: ['rust_std=2018', 'build.rust_std=2018'],
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'use_fallback',
     '--cfg', 'feature="syn-error"',
     '--cfg', 'feature="proc-macro"'
subprojects/packagefiles/proc-macro2-1-rs/meson.build +1 -0
index f9c8675ebad..5759df3ecc9 100644
@@ -15,6 +15,7 @@ _proc_macro2_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'feature="proc-macro"',
     '--cfg', 'no_literal_byte_character',
     '--cfg', 'no_literal_c_string',
subprojects/packagefiles/quote-1-rs/meson.build +1 -0
index 7f7792569b9..bf41fad99bb 100644
@@ -15,6 +15,7 @@ _quote_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'feature="proc-macro"',
   ],
   dependencies: [
subprojects/packagefiles/syn-2-rs/meson.build +1 -0
index 2c62cf7e1b0..a0094174084 100644
@@ -19,6 +19,7 @@ _syn_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   rust_args: [
+    '--cap-lints', 'allow',
     '--cfg', 'feature="full"',
     '--cfg', 'feature="derive"',
     '--cfg', 'feature="parsing"',
subprojects/packagefiles/unicode-ident-1-rs/meson.build +1 -0
index 9d76ebbd1ad..11a5dab97df 100644
@@ -10,6 +10,7 @@ _unicode_ident_rs = static_library(
   gnu_symbol_visibility: 'hidden',
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
+  rust_args: ['--cap-lints', 'allow'],
   dependencies: [],
   native: true,
 )
subprojects/proc-macro-error-1-rs.wrap +3 -0
index b7db03b06a0..59f892f7825 100644
@@ -5,3 +5,6 @@ source_filename = proc-macro-error-1.0.4.tar.gz
 source_hash = da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c
 #method = cargo
 patch_directory = proc-macro-error-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/proc-macro-error-attr-1-rs.wrap +3 -0
index d13d8a239ac..5aeb224a103 100644
@@ -5,3 +5,6 @@ source_filename = proc-macro-error-attr-1.0.4.tar.gz
 source_hash = a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869
 #method = cargo
 patch_directory = proc-macro-error-attr-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/proc-macro2-1-rs.wrap +3 -0
index 7053e2c013c..6c9369f0df3 100644
@@ -5,3 +5,6 @@ source_filename = proc-macro2-1.0.84.0.tar.gz
 source_hash = ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6
 #method = cargo
 patch_directory = proc-macro2-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/quote-1-rs.wrap +3 -0
index 6e7ea69049f..8b721dfa00b 100644
@@ -5,3 +5,6 @@ source_filename = quote-1.0.36.0.tar.gz
 source_hash = 0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7
 #method = cargo
 patch_directory = quote-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/syn-2-rs.wrap +3 -0
index 13ffdac3c3b..d79cf750fb4 100644
@@ -5,3 +5,6 @@ source_filename = syn-2.0.66.0.tar.gz
 source_hash = c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5
 #method = cargo
 patch_directory = syn-2-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/unicode-ident-1-rs.wrap +3 -0
index 4609f96ed97..50988f612e2 100644
@@ -5,3 +5,6 @@ source_filename = unicode-ident-1.0.12.tar.gz
 source_hash = 3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b
 #method = cargo
 patch_directory = unicode-ident-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v2
subprojects/unicode-ident-1-rs/meson.build deleted +0 -20
deleted file mode 100644
index 54f23768545..00000000000
@@ -1,20 +0,0 @@
-project('unicode-ident-1-rs', 'rust',
-  version: '1.0.12',
-  license: '(MIT OR Apache-2.0) AND Unicode-DFS-2016',
-  default_options: [])
-
-_unicode_ident_rs = static_library(
-  'unicode_ident',
-  files('src/lib.rs'),
-  gnu_symbol_visibility: 'hidden',
-  override_options: ['rust_std=2021', 'build.rust_std=2021'],
-  rust_abi: 'rust',
-  dependencies: [],
-  native: true,
-)
-
-unicode_ident_dep = declare_dependency(
-  link_with: _unicode_ident_rs,
-)
-
-meson.override_dependency('unicode-ident-1-rs', unicode_ident_dep, native: true)
Signature · 1 line
2.47.1
Reviewed-by on v1 12/12
#24 lore raw

Re: [PATCH 12/12] rust: hide warnings for subprojects

Quoted 6 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:54 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 12/12] rust: hide warnings for subprojects
X-Mailer: git-send-email 2.47.1

This matches cargo's own usage of "--cap-lints allow" when building dependencies.

+Add remove the unnecessary meson.build file.

Quoted 19 lines
---
 subprojects/arbitrary-int-1-rs.wrap           |  3 +++
 subprojects/bilge-0.2-rs.wrap                 |  3 +++
 subprojects/bilge-impl-0.2-rs.wrap            |  3 +++
 subprojects/either-1-rs.wrap                  |  3 +++
 subprojects/itertools-0.11-rs.wrap            |  3 +++
 .../arbitrary-int-1-rs/meson.build            |  1 +
 .../packagefiles/bilge-0.2-rs/meson.build     |  1 +
 .../bilge-impl-0.2-rs/meson.build             |  1 +
 .../packagefiles/either-1-rs/meson.build      |  1 +
 .../itertools-0.11-rs/meson.build             |  1 +
 .../proc-macro-error-1-rs/meson.build         |  1 +
 .../proc-macro-error-attr-1-rs/meson.build    |  1 +
 .../packagefiles/proc-macro2-1-rs/meson.build |  1 +
 .../packagefiles/quote-1-rs/meson.build       |  1 +
 subprojects/packagefiles/syn-2-rs/meson.build |  1 +
 .../unicode-ident-1-rs/meson.build            |  1 +
 subprojects/proc-macro-error-1-rs.wrap        |  3 +++
Quoted 9 lines
 subprojects/proc-macro-error-attr-1-rs.wrap   |  3 +++
 subprojects/proc-macro2-1-rs.wrap             |  3 +++
 subprojects/quote-1-rs.wrap                   |  3 +++
 subprojects/syn-2-rs.wrap                     |  3 +++
 subprojects/unicode-ident-1-rs.wrap           |  3 +++
 subprojects/unicode-ident-1-rs/meson.build    | 20 -------------------
 23 files changed, 44 insertions(+), 20 deletions(-)
 delete mode 100644 subprojects/unicode-ident-1-rs/meson.build
#25 lore raw
In reply to Paolo Bonzini
Quoted 12 lines · Paolo Bonzini

Date: Fri, 20 Dec 2024 15:29:42 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 00/12] Next round of qemu_api patches
X-Mailer: git-send-email 2.47.1

This includes:

1) the outcome of reviewing
https://lore.kernel.org/qemu-devel/[email protected]/T/,
especially with respect to instance_finalize implementation and Rust-defined subclasses (patches 1-6)

Quoted 8 lines

2) the beginnings of separating instance_init/instance_post_init, making the latter take &self (patches 7-9)

3) extracting pl011's logic to convert an integer into an enum into a #[derive(TryInto)] procedural macro (patches 10-11)

4) a one-off change that I noticed while reading Cargo documentation (patch 12)

This series is the v2 of https://lore.kernel.org/qemu-devel/[email protected]/

I will go through this series this week. :)

Quoted 5 lines

Zhao: I have placed these in rust-next, but feel free to ignore them for your next HPET submission. I have also included in my tree your test additions for usage in CI, but I'm not including them here to give you the opportunity to post them with a commit message and a signed-off-by. If you don't have time, it's in safe hands. :)

If possible, I would really appreciate it if you could add it to your series :-) It seems fragmented if I send it separately.

Thanks,
Zhao