rust: remaining parts of qdev bindings
Subsystems (2)
cover rust: remaining parts of qdev bindings
This concludes the part of the qdev bindings that is needed to convert pl011 to safe Rust, with the exception of chardev bindings and switching to the pinned_init crate.
It includes reference counting, object creation, clocks, Resettable and MemoryRegionOps. Documentation is at least decent.
The code is getting closer to the synopsis at
https://lists.nongnu.org/archive/html/qemu-rust/2024-12/msg00006.html,
except that it works (at least as far as CI is concerned) instead of just compiling. :) And since I'm citing it, a reminder that it may still be worth asking your Rust questions as replies to that message.
You can find the code in the rust-next branch of
https://gitlab.com/bonzini/qemu/.
Paolo
RFC->v1:
- improvements and fixes for docs and comments
- fixed Sync/Send declarations for Owned
- prefer NonNull::cast (could use NonNull<> arguments too in the future)
- add Send markers too, not just Sync
- use impl_zeroable! macro
- add qdev_init_clock_out() too
- add comments about when to use Owned<>
- add Owned::clone
- add conversion of pl011_create to safe Rust for more uses of Owned<>
Paolo Bonzini (12):
rust: qom: add reference counting functionality
rust: qom: add object creation functionality
rust: callbacks: allow passing optional callbacks as ()
rust: qdev: add clock creation
rust: qom: allow initializing interface vtables
rust: qdev: make ObjectImpl a supertrait of DeviceImpl
rust: qdev: switch from legacy reset to Resettable
rust: bindings: add Send and Sync markers for types that have bindings
rust: bindings for MemoryRegionOps
rust: irq: define ObjectType for IRQState
rust: chardev, qdev: add bindings to qdev_prop_set_chr
rust: pl011: convert pl011_create to safe Rust
meson.build | 1 + rust/hw/char/pl011/src/device.rs | 121 +++++++------- rust/hw/char/pl011/src/lib.rs | 1 - rust/hw/char/pl011/src/memory_ops.rs | 34 ---- rust/qemu-api/meson.build | 2 + rust/qemu-api/src/bindings.rs | 46 ++++++ rust/qemu-api/src/callbacks.rs | 97 ++++++++++++ rust/qemu-api/src/chardev.rs | 19 +++ rust/qemu-api/src/irq.rs | 18 ++- rust/qemu-api/src/lib.rs | 2 + rust/qemu-api/src/memory.rs | 191 ++++++++++++++++++++++ rust/qemu-api/src/prelude.rs | 4 + rust/qemu-api/src/qdev.rs | 227 ++++++++++++++++++++++++--- rust/qemu-api/src/qom.rs | 218 ++++++++++++++++++++++++- rust/qemu-api/src/sysbus.rs | 39 ++++- rust/qemu-api/src/vmstate.rs | 10 +- rust/qemu-api/src/zeroable.rs | 1 + rust/qemu-api/tests/tests.rs | 45 +++--- 18 files changed, 924 insertions(+), 152 deletions(-) delete mode 100644 rust/hw/char/pl011/src/memory_ops.rs create mode 100644 rust/qemu-api/src/chardev.rs create mode 100644 rust/qemu-api/src/memory.rs
Signature · 1 line
2.48.1
PATCH 1/12 rust: qom: add reference counting functionality
Add a smart pointer that allows to add and remove references from QOM objects. It's important to note that while all QOM objects have a reference count, in practice not all of them have their lifetime guarded by it. Embedded objects, specifically, are confined to the lifetime of the owner.
When writing Rust bindings this is important, because embedded objects are *never* used through the "Owned<>" smart pointer that is introduced here.
--- rust/qemu-api/src/qom.rs | 158 ++++++++++++++++++++++++++++++++++- rust/qemu-api/src/vmstate.rs | 6 +- rust/qemu-api/tests/tests.rs | 10 +++ 3 files changed, 172 insertions(+), 2 deletions(-)
rust/qemu-api/src/qom.rs +157 -1
@@ -56,6 +56,7 @@use std::{ ffi::CStr , fmt, + mem::ManuallyDrop , ops::{Deref ,DerefMut }, os::raw::c_void, ptr::NonNull ,
@@ -63,7 +64,13 @@pub use bindings::{Object ,ObjectClass }; -use crate ::bindings::{self, object_dynamic_cast, object_get_class, object_get_typename,TypeInfo }; +use crate ::{ + bindings::{ + self, object_dynamic_cast, object_get_class, object_get_typename, object_ref, object_unref, +TypeInfo , + }, + cell::bql_locked, +};/// Marker trait: `Self` can be statically upcasted to `P` (i.e. `P` is a direct /// or indirect parent of `Self`).
@@ -610,6 +617,148 @@ unsafe impl ObjectType for Object {unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_OBJECT ) }; } +/// A reference-counted pointer to a QOM object. +/// +/// `Owned<T>` wraps `T` with automatic reference counting. It increases the +/// reference count when created via [`Owned::from`] or cloned, and decreases +/// it when dropped. This ensures that the reference count remains elevated +/// as long as any `Owned<T>` references to it exist. +/// +/// `Owned<T>` can be used for two reasons: +/// * because the lifetime of the QOM object is unknown and someone else could +/// take a reference (similar to `Arc<T>`, for example): in this case, the +/// object can escape and outlive the Rust struct that contains the `Owned<T>` +/// field; +/// +/// * to ensure that the object stays alive until after `Drop::drop` is called +/// on the Rust struct: in this case, the object will always die together with +/// the Rust struct that contains the `Owned<T>` field. +/// +/// Child properties are an example of the second case: in C, an object that +/// is created with `object_initialize_child` will die *before* +/// `instance_finalize` is called, whereas Rust expects the struct to have valid +/// contents when `Drop::drop` is called. Therefore Rust structs that have +/// child properties need to keep a reference to the child object. Right now +/// this can be done with `Owned<T>`; in the future one might have a separate +/// `Child<'parent, T>` smart pointer that keeps a reference to a `T`, like +/// `Owned`, but does not allow cloning. +/// +/// Note that dropping an `Owned<T>` requires the big QEMU lock to be taken. +#[repr(transparent)] +#[derive( PartialEq , Eq , Hash , PartialOrd , Ord )] +pub struct Owned <T :ObjectType >(NonNull <T >); + +// The following rationale for safety is taken from Linux's kernel::sync::Arc. + +// SAFETY: It is safe to send `Owned<T>` to another thread when the underlying +// `T` is `Sync` because it effectively means sharing `&T` (which is safe +// because `T` is `Sync`); additionally, it needs `T` to be `Send` because any +// thread that has an `Owned<T>` may ultimately access `T` using a +// mutable reference when the reference count reaches zero and `T` is dropped. +unsafe impl <T :ObjectType +Send +Sync >Send for Owned <T > {} + +// SAFETY: It is safe to send `&Owned<T>` to another thread when the underlying +// `T` is `Sync` because it effectively means sharing `&T` (which is safe +// because `T` is `Sync`); additionally, it needs `T` to be `Send` because any +// thread that has a `&Owned<T>` may clone it and get an `Owned<T>` on that +// thread, so the thread may ultimately access `T` using a mutable reference +// when the reference count reaches zero and `T` is dropped. +unsafe impl <T :ObjectType +Sync +Send >Sync for Owned <T > {} + +impl <T :ObjectType >Owned <T > { +/// Convert a raw C pointer into an owned reference to the QOM +/// object it points to. The object's reference count will be +/// decreased when the `Owned` is dropped. +/// +/// # Panics +/// +/// Panics if `ptr` is NULL. +/// +/// # Safety +/// +/// The caller must indeed own a reference to the QOM object. +/// The object must not be embedded in another unless the outer +/// object is guaranteed to have a longer lifetime. +/// +/// A raw pointer obtained via [`Owned::into_raw()`] can always be passed +/// back to `from_raw()` (assuming the original `Owned` was valid!), +/// since the owned reference remains there between the calls to +/// `into_raw()` and `from_raw()`. +pub unsafe fn from_raw (ptr: *const T ) ->Self { +// SAFETY NOTE: while NonNull requires a mutable pointer, only +// Deref is implemented so the pointer passed to from_raw +// remains const +Owned (NonNull ::new (ptras *mut T ).unwrap ()) + } + +/// Obtain a raw C pointer from a reference. `src` is consumed +/// and the reference is leaked. +#[allow(clippy::missing_const_for_fn)] +pub fn into_raw (src:Owned <T >) -> *mut T { +let src =ManuallyDrop ::new (src); + src.0 .as_ptr () + } + +/// Increase the reference count of a QOM object and return +/// a new owned reference to it. +/// +/// # Safety +/// +/// The object must not be embedded in another, unless the outer +/// object is guaranteed to have a longer lifetime. +pub unsafe fn from (obj: &T ) ->Self { +unsafe { +object_ref (obj.as_object_mut_ptr ().cast ::<c_void >()); + +// SAFETY NOTE: while NonNull requires a mutable pointer, only +// Deref is implemented so the reference passed to from_raw +// remains shared +Owned (NonNull ::new_unchecked (obj.as_mut_ptr ())) + } + } +} + +impl <T :ObjectType >Clone for Owned <T > { +fn clone (&self) ->Self { +// SAFETY: creation method is unsafe; whoever calls it has +// responsibility that the pointer is valid, and remains valid +// throughout the lifetime of the `Owned<T>` and its clones. +unsafe {Owned ::from (self.deref ()) } + } +} + +impl <T :ObjectType >Deref for Owned <T > { +type Target =T ; + +fn deref (&self) -> &Self ::Target { +// SAFETY: creation method is unsafe; whoever calls it has +// responsibility that the pointer is valid, and remains valid +// throughout the lifetime of the `Owned<T>` and its clones. +// With that guarantee, reference counting ensures that +// the object remains alive. +unsafe { &*self.0 .as_ptr () } + } +} +impl <T :ObjectType >ObjectDeref for Owned <T > {} + +impl <T :ObjectType >Drop for Owned <T > { +fn drop (&mut self) { +assert! (bql_locked ()); +// SAFETY: creation method is unsafe, and whoever calls it has +// responsibility that the pointer is valid, and remains valid +// throughout the lifetime of the `Owned<T>` and its clones. +unsafe { +object_unref (self.as_object_mut_ptr ().cast ::<c_void >()); + } + } +} + +impl <T :IsA <Object >> fmt::Debug for Owned <T > { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + self.deref ().debug_fmt (f) + } +} +/// Trait for methods exposed by the Object class. The methods can be /// called on all objects that have the trait `IsA<Object>`. ///
@@ -641,6 +790,13 @@ fn get_class(&self) -> &'static <Self::Target as ObjectType>::Class { klass } + +/// Convenience function for implementing the Debug trait +fn debug_fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + f.debug_tuple (&self.typename ()) + .field (&(selfas *const Self )) + .finish () + } }impl <R :ObjectDeref >ObjectMethods for R where R ::Target :IsA <Object > {}
rust/qemu-api/src/vmstate.rs +5 -1
@@ -29,6 +29,8 @@pub use crate ::bindings::{VMStateDescription ,VMStateField };use crate ::{ bindings::{self,VMStateFlags }, + prelude::*, + qom::Owned , zeroable::Zeroable , };
@@ -191,7 +193,8 @@ pub const fn vmstate_varray_flag<T: VMState>(_: PhantomData<T>) -> VMStateFlags/// * a transparent wrapper for any of the above (`Cell`, `UnsafeCell`, /// [`BqlCell`](crate::cell::BqlCell), [`BqlRefCell`](crate::cell::BqlRefCell) /// * a raw pointer to any of the above -/// * a `NonNull` pointer or a `Box` for any of the above +/// * a `NonNull` pointer, a `Box` or an [`Owned`](crate::qom::Owned) for any of +/// the above /// * an array of any of the above /// /// In order to support other types, the trait `VMState` must be implemented
@@ -398,6 +401,7 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* {// Unlike C pointers, Box is always non-null therefore there is no need // to specify VMS_ALLOC. impl_vmstate_pointer! (Box <T >where T :VMState ); +impl_vmstate_pointer! (Owned <T >where T :VMState +ObjectType );// Arrays using the underlying type's VMState plus // VMS_ARRAY/VMS_ARRAY_OF_POINTER
rust/qemu-api/tests/tests.rs +10 -0
@@ -138,6 +138,16 @@ fn test_object_new() { } } +#[test] +#[allow(clippy::redundant_clone)] +/// Create, clone and then drop an instance. +fn test_clone () { +init_qom (); +let p =DummyState ::new (); +assert_eq! (p.clone ().typename (),"dummy" ); +drop (p); +} +#[test] /// Try invoking a method on an object. fn test_typename () {
Signature · 1 line
2.48.1
Re: [PATCH 01/12] rust: qom: add reference counting functionality
Quoted 69 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:12 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 01/12] rust: qom: add reference counting functionality
X-Mailer: git-send-email 2.48.1Add a smart pointer that allows to add and remove references from QOM objects. It's important to note that while all QOM objects have a reference count, in practice not all of them have their lifetime guarded by it. Embedded objects, specifically, are confined to the lifetime of the owner.
When writing Rust bindings this is important, because embedded objects are *never* used through the "Owned<>" smart pointer that is introduced here.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/qemu-api/src/qom.rs | 158 ++++++++++++++++++++++++++++++++++- rust/qemu-api/src/vmstate.rs | 6 +- rust/qemu-api/tests/tests.rs | 10 +++ 3 files changed, 172 insertions(+), 2 deletions(-)rust/qemu-api/src/qom.rs +25
-1@@ -56,6 +56,7 @@use std::{ ffi::CStr , fmt, + mem::ManuallyDrop , ops::{Deref ,DerefMut }, os::raw::c_void, ptr::NonNull ,@@ -63,7 +64,13 @@pub use bindings::{Object ,ObjectClass }; -use crate ::bindings::{self, object_dynamic_cast, object_get_class, object_get_typename,TypeInfo }; +use crate ::{ + bindings::{ + self, object_dynamic_cast, object_get_class, object_get_typename, object_ref, object_unref, +TypeInfo , + }, + cell::bql_locked, +};/// Marker trait: `Self` can be statically upcasted to `P` (i.e. `P` is a direct /// or indirect parent of `Self`). @@ -610,6 +617,148 @@ unsafe impl ObjectType for Object {unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_OBJECT ) }; } +/// A reference-counted pointer to a QOM object. +/// +/// `Owned<T>` wraps `T` with automatic reference counting. It increases the +/// reference count when created via [`Owned::from`] or cloned, and decreases +/// it when dropped. This ensures that the reference count remains elevated +/// as long as any `Owned<T>` references to it exist. +/// +/// `Owned<T>` can be used for two reasons: +/// * because the lifetime of the QOM object is unknown and someone else could +/// take a reference (similar to `Arc<T>`, for example): in this case, the +/// object can escape and outlive the Rust struct that contains the `Owned<T>` +/// field; +/// +/// * to ensure that the object stays alive until after `Drop::drop` is called +/// on the Rust struct: in this case, the object will always die together with +/// the Rust struct that contains the `Owned<T>` field. +///
rust/qemu-api/src/qom.rs unsafe impl ObjectType for Object { · line 646
Quoted 10 lines
rust/qemu-api/src/qom.rs +10
-0+/// Child properties are an example of the second case: in C, an object that +/// is created with `object_initialize_child` will die *before* +/// `instance_finalize` is called, whereas Rust expects the struct to have valid +/// contents when `Drop::drop` is called. Therefore Rust structs that have +/// child properties need to keep a reference to the child object. Right now +/// this can be done with `Owned<T>`; in the future one might have a separate +/// `Child<'parent, T>` smart pointer that keeps a reference to a `T`, like +/// `Owned`, but does not allow cloning. +/// +/// Note that dropping an `Owned<T>` requires the big QEMU lock to be taken.
Nice doc.
LGTM,
PATCH 2/12 rust: qom: add object creation functionality
In reply to Paolo BonziniThe basic object lifecycle test can now be implemented using safe code!
--- rust/hw/char/pl011/src/device.rs | 23 +++++++++++++---------- rust/qemu-api/src/prelude.rs | 1 + rust/qemu-api/src/qom.rs | 23 +++++++++++++++++++++-- rust/qemu-api/tests/tests.rs | 30 +++++++++++------------------- 4 files changed, 46 insertions(+), 31 deletions(-)
rust/hw/char/pl011/src/device.rs +13 -10
@@ -10,11 +10,11 @@use qemu_api::{ bindings::{ - error_fatal, hwaddr, memory_region_init_io, qdev_init_clock_in, qdev_new, - qdev_prop_set_chr, qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, - qemu_chr_fe_write_all, qemu_irq, sysbus_connect_irq, sysbus_mmio_map, - sysbus_realize_and_unref,CharBackend ,Chardev ,Clock ,ClockEvent ,MemoryRegion , -QEMUChrEvent ,CHR_IOCTL_SERIAL_SET_BREAK , + error_fatal, hwaddr, memory_region_init_io, qdev_init_clock_in, qdev_prop_set_chr, + qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, + qemu_chr_fe_write_all, qemu_irq, sysbus_connect_irq, sysbus_mmio_map, sysbus_realize, +CharBackend ,Chardev ,Clock ,ClockEvent ,MemoryRegion ,QEMUChrEvent , +CHR_IOCTL_SERIAL_SET_BREAK , }, c_str, impl_vmstate_forward, irq::InterruptSource ,
@@ -705,15 +705,18 @@ pub fn post_load(&self, _version_id: u32) -> Result<(), ()> { irq: qemu_irq, chr: *mut Chardev, ) -> *mut DeviceState { +let pl011 =PL011State ::new ();unsafe { -let dev: *mut DeviceState =qdev_new (PL011State ::TYPE_NAME .as_ptr ()); -let sysbus: *mut SysBusDevice = dev.cast ::<SysBusDevice >(); - +let dev = pl011.as_mut_ptr ::<DeviceState >();qdev_prop_set_chr (dev,c_str! ("chardev" ).as_ptr (), chr); -sysbus_realize_and_unref (sysbus,addr_of_mut! (error_fatal)); + +let sysbus = pl011.as_mut_ptr ::<SysBusDevice >(); +sysbus_realize (sysbus,addr_of_mut! (error_fatal));sysbus_mmio_map (sysbus,0 , addr);sysbus_connect_irq (sysbus,0 , irq); - dev + +// return the pointer, which is kept alive by the QOM tree; drop owned ref + pl011.as_mut_ptr () } }
rust/qemu-api/src/prelude.rs +1 -0
@@ -12,6 +12,7 @@pub use crate ::qom::ObjectCast ;pub use crate ::qom::ObjectCastMut ;pub use crate ::qom::ObjectDeref ; +pub use crate ::qom::ObjectClassMethods ;pub use crate ::qom::ObjectMethods ;pub use crate ::qom::ObjectType ;
rust/qemu-api/src/qom.rs +21 -2
@@ -66,8 +66,8 @@use crate ::{ bindings::{ - self, object_dynamic_cast, object_get_class, object_get_typename, object_ref, object_unref, -TypeInfo , + self, object_dynamic_cast, object_get_class, object_get_typename, object_new, object_ref, + object_unref,TypeInfo , }, cell::bql_locked, };
@@ -759,6 +759,24 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } +/// Trait for class methods exposed by the Object class. The methods can be +/// called on all objects that have the trait `IsA<Object>`. +/// +/// The trait should only be used through the blanket implementation, +/// which guarantees safety via `IsA` +pub trait ObjectClassMethods :IsA <Object > { +/// Return a new reference counted instance of this class +fn new () ->Owned <Self > { +assert! (bql_locked ()); +// SAFETY: the object created by object_new is allocated on +// the heap and has a reference count of 1 +unsafe { +let obj = &*object_new (Self ::TYPE_NAME .as_ptr ()); +Owned ::from_raw (obj.unsafe_cast ::<Self >()) + } + } +} +/// Trait for methods exposed by the Object class. The methods can be /// called on all objects that have the trait `IsA<Object>`. ///
@@ -799,4 +817,5 @@ fn debug_fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } +impl <T >ObjectClassMethods for T where T :IsA <Object > {}impl <R :ObjectDeref >ObjectMethods for R where R ::Target :IsA <Object > {}
rust/qemu-api/tests/tests.rs +11 -19
@@ -3,8 +3,7 @@// SPDX-License-Identifier: GPL-2.0-or-later use std::{ - ffi::CStr , - os::raw::c_void, + ffi::{c_void,CStr }, ptr::{addr_of, addr_of_mut}, };
@@ -132,10 +131,8 @@ fn init_qom() {/// Create and immediately drop an instance. fn test_object_new () {init_qom (); -unsafe { -object_unref (object_new (DummyState ::TYPE_NAME .as_ptr ()).cast ()); -object_unref (object_new (DummyChildState ::TYPE_NAME .as_ptr ()).cast ()); - } +drop (DummyState ::new ()); +drop (DummyChildState ::new ()); }#[test]
@@ -152,12 +149,8 @@ fn test_clone() {/// Try invoking a method on an object. fn test_typename () {init_qom (); -let p: *mut DummyState =unsafe {object_new (DummyState ::TYPE_NAME .as_ptr ()).cast () }; -let p_ref: &DummyState =unsafe { &*p }; -assert_eq! (p_ref.typename (),"dummy" ); -unsafe { -object_unref (p_ref.as_object_mut_ptr ().cast ::<c_void >()); - } +let p =DummyState ::new (); +assert_eq! (p.typename (),"dummy" ); }// a note on all "cast" tests: usually, especially for downcasts the desired
@@ -172,24 +165,23 @@ fn test_typename() {/// Test casts on shared references. fn test_cast () {init_qom (); -let p: *mut DummyState =unsafe {object_new (DummyState ::TYPE_NAME .as_ptr ()).cast () }; +let p =DummyState ::new (); +let p_ptr: *mut DummyState =unsafe { p.as_mut_ptr () }; +let p_ref: &mut DummyState =unsafe { &mut *p_ptr }; -let p_ref: &DummyState =unsafe { &*p };let obj_ref: &Object = p_ref.upcast (); -assert_eq! (addr_of! (*obj_ref), p.cast ()); +assert_eq! (addr_of! (*obj_ref), p_ptr.cast ());let sbd_ref:Option <&SysBusDevice > = obj_ref.dynamic_cast ();assert! (sbd_ref.is_none ());let dev_ref:Option <&DeviceState > = obj_ref.downcast (); -assert_eq! (addr_of! (*dev_ref.unwrap ()), p.cast ()); +assert_eq! (addr_of! (*dev_ref.unwrap ()), p_ptr.cast ());// SAFETY: the cast is wrong, but the value is only used for comparison unsafe {let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast (); -assert_eq! (addr_of! (*sbd_ref), p.cast ()); - -object_unref (p_ref.as_object_mut_ptr ().cast ::<c_void >()); +assert_eq! (addr_of! (*sbd_ref), p_ptr.cast ()); } }
Signature · 1 line
2.48.1
Re: [PATCH 02/12] rust: qom: add object creation functionality
Quoted 7 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:13 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 02/12] rust: qom: add object creation functionality
X-Mailer: git-send-email 2.48.1The basic object lifecycle test can now be implemented using safe code!
Quoted 8 lines
Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/char/pl011/src/device.rs | 23 +++++++++++++---------- rust/qemu-api/src/prelude.rs | 1 + rust/qemu-api/src/qom.rs | 23 +++++++++++++++++++++-- rust/qemu-api/tests/tests.rs | 30 +++++++++++------------------- 4 files changed, 46 insertions(+), 31 deletions(-)
PATCH 3/12 rust: callbacks: allow passing optional callbacks as ()
In reply to Paolo BonziniIn some cases, callbacks are optional. Using "Some(function)" and "None" does not work well, because when someone writes "None" the compiler does not know what to use for "F" in "Option<F>".
Therefore, adopt () to mean a "null" callback. It is possible to enforce that a callback is valid by adding a "let _: () = F::ASSERT_IS_SOME" before the invocation of F::call.
--- rust/qemu-api/src/callbacks.rs | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+)
rust/qemu-api/src/callbacks.rs +97 -0
@@ -79,6 +79,31 @@/// call_it(&move |_| String::from(x), "hello workd"); /// ``` /// +/// `()` can be used to indicate "no function": +/// +/// ``` +/// # use qemu_api::callbacks::FnCall; +/// fn optional<F: for<'a> FnCall<(&'a str,), String>>(_f: &F, s: &str) -> Option<String> { +/// if F::IS_SOME { +/// Some(F::call((s,))) +/// } else { +/// None +/// } +/// } +/// +/// assert!(optional(&(), "hello world").is_none()); +/// ``` +/// +/// Invoking `F::call` will then be a run-time error. +/// +/// ```should_panic +/// # use qemu_api::callbacks::FnCall; +/// # fn call_it<F: for<'a> FnCall<(&'a str,), String>>(_f: &F, s: &str) -> String { +/// # F::call((s,)) +/// # } +/// let s: String = call_it(&(), "hello world"); // panics +/// ``` +/// /// # Safety /// /// Because `Self` is a zero-sized type, all instances of the type are
@@ -93,10 +118,70 @@ pub unsafe trait FnCall<Args, R = ()>: 'static + Sync + Sized {/// Rust 1.79.0+. const ASSERT_ZERO_SIZED : () = {assert! (mem::size_of ::<Self >() ==0 ) }; +/// Referring to this constant asserts that the `Self` type is an actual +/// function type, which can be used to catch incorrect use of `()` +/// at compile time. +/// +/// # Examples +/// +/// ```compile_fail +/// # use qemu_api::callbacks::FnCall; +/// fn call_it<F: for<'a> FnCall<(&'a str,), String>>(_f: &F, s: &str) -> String { +/// let _: () = F::ASSERT_IS_SOME; +/// F::call((s,)) +/// } +/// +/// let s: String = call_it((), "hello world"); // does not compile +/// ``` +/// +/// Note that this can be more simply `const { assert!(F::IS_SOME) }` in +/// Rust 1.79.0 or newer. +const ASSERT_IS_SOME : () = {assert! (Self ::IS_SOME ) }; + +/// `true` if `Self` is an actual function type and not `()`. +/// +/// # Examples +/// +/// You can use `IS_SOME` to catch this at compile time: +/// +/// ```compile_fail +/// # use qemu_api::callbacks::FnCall; +/// fn call_it<F: for<'a> FnCall<(&'a str,), String>>(_f: &F, s: &str) -> String { +/// const { assert!(F::IS_SOME) } +/// F::call((s,)) +/// } +/// +/// let s: String = call_it((), "hello world"); // does not compile +/// ``` +const IS_SOME :bool ; + +/// `false` if `Self` is an actual function type, `true` if it is `()`. +fn is_none () ->bool { + !Self ::IS_SOME + } + +/// `true` if `Self` is an actual function type, `false` if it is `()`. +fn is_some () ->bool { +Self ::IS_SOME + } +/// Call the function with the arguments in args. fn call (a:Args ) ->R ; } +/// `()` acts as a "null" callback. Using `()` and `function` is nicer +/// than `None` and `Some(function)`, because the compiler is unable to +/// infer the type of just `None`. Therefore, the trait itself acts as the +/// option type, with functions [`FnCall::is_some`] and [`FnCall::is_none`]. +unsafe impl <Args ,R >FnCall <Args ,R >for () { +const IS_SOME :bool =false ; + +/// Call the function with the arguments in args. +fn call (_a:Args ) ->R { +panic! ("callback not specified" ) + } +} +macro_rules! impl_call { ($($args:ident,)* ) => (// SAFETY: because each function is treated as a separate type,
@@ -106,6 +191,8 @@ unsafe impl<F, $($args,)* R> FnCall<($($args,)*), R> for F where F: 'static + Sync + Sized + Fn($($args, )*) -> R, { +const IS_SOME :bool =true ; +#[inline(always)] fn call(a: ($($args,)*)) -> R {let _: () =Self ::ASSERT_ZERO_SIZED ;
@@ -141,4 +228,14 @@ fn do_test_call<'a, F: FnCall<(&'a str,), String>>(_f: &F) -> String {fn test_call () {assert_eq! (do_test_call (&str ::to_owned),"hello world" ) } + +// The `_f` parameter is unused but it helps the compiler infer `F`. +fn do_test_is_some <'a,F :FnCall <(&'astr ,),String >>(_f: &F ) { +assert! (F ::is_some ()); + } + +#[test] +fn test_is_some () { +do_test_is_some (&str::to_owned); + } }
Signature · 1 line
2.48.1
Re: [PATCH 03/12] rust: callbacks: allow passing optional callbacks as ()
Quoted 10 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:14 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 03/12] rust: callbacks: allow passing optional callbacks as
()
X-Mailer: git-send-email 2.48.1In some cases, callbacks are optional. Using "Some(function)" and "None" does not work well, because when someone writes "None" the compiler does not know what to use for "F" in "Option<F>".
Quoted 9 lines
Therefore, adopt () to mean a "null" callback. It is possible to enforce that a callback is valid by adding a "let _: () =
F::ASSERT_IS_SOME" before the invocation ofF::call.Signed-off-by Paolo Bonzini <[email protected]>--- rust/qemu-api/src/callbacks.rs | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+)
PATCH 4/12 rust: qdev: add clock creation
In reply to Paolo BonziniAdd a Rust version of qdev_init_clock_in, which can be used in instance_init. There are a couple differences with the C version:
- in Rust the object keeps its own reference to the clock (in addition to the one embedded in the NamedClockList), and the reference is dropped automatically by instance_finalize(); this is encoded in the signature of DeviceClassMethods::init_clock_in, which makes the lifetime of the clock independent of that of the object it holds. This goes unnoticed in the C version and is due to the existence of aliases.
- also, anything that happens during instance_init uses the pinned_init framework to operate on a partially initialized object, and is done through class methods (i.e. through DeviceClassMethods rather than DeviceMethods) because the device does not exist yet. Therefore, Rust code *must* create clocks from instance_init, which is stricter than C.
--- rust/hw/char/pl011/src/device.rs | 43 +++++------- rust/qemu-api/src/prelude.rs | 2 + rust/qemu-api/src/qdev.rs | 109 ++++++++++++++++++++++++++++++- rust/qemu-api/src/vmstate.rs | 4 +- 4 files changed, 127 insertions(+), 31 deletions(-)
rust/hw/char/pl011/src/device.rs +17 -26
@@ -10,17 +10,16 @@use qemu_api::{ bindings::{ - error_fatal, hwaddr, memory_region_init_io, qdev_init_clock_in, qdev_prop_set_chr, - qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, - qemu_chr_fe_write_all, qemu_irq, sysbus_connect_irq, sysbus_mmio_map, sysbus_realize, -CharBackend ,Chardev ,Clock ,ClockEvent ,MemoryRegion ,QEMUChrEvent , -CHR_IOCTL_SERIAL_SET_BREAK , + error_fatal, hwaddr, memory_region_init_io, qdev_prop_set_chr, qemu_chr_fe_accept_input, + qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, qemu_chr_fe_write_all, qemu_irq, + sysbus_connect_irq, sysbus_mmio_map, sysbus_realize,CharBackend ,Chardev ,MemoryRegion , +QEMUChrEvent ,CHR_IOCTL_SERIAL_SET_BREAK , }, c_str, impl_vmstate_forward, irq::InterruptSource , prelude::*, - qdev::{DeviceImpl ,DeviceState ,Property }, - qom::{ClassInitImpl ,ObjectImpl ,ParentField }, + qdev::{Clock ,ClockEvent ,DeviceImpl ,DeviceState ,Property }, + qom::{ClassInitImpl ,ObjectImpl ,Owned ,ParentField }, sysbus::{SysBusDevice ,SysBusDeviceClass }, vmstate::VMStateDescription , };
@@ -131,7 +130,7 @@ pub struct PL011State {#[doc(alias = "irq" )] pub interrupts: [InterruptSource; IRQMASK.len()],#[doc(alias = "clk" )] - pub clock: NonNull<Clock>, + pub clock: Owned<Clock>,#[doc(alias = "migrate_clk" )] pub migrate_clock: bool, }
@@ -485,8 +484,6 @@ impl PL011State {/// location/instance. All its fields are expected to hold unitialized /// values with the sole exception of `parent_obj`. unsafe fn init (&mut self) { -const CLK_NAME : &CStr =c_str! ("clk" ); -// SAFETY: // // self and self.iomem are guaranteed to be valid at this point since callers
@@ -506,22 +503,16 @@ unsafe fn init(&mut self) {// SAFETY: // -// self.clock is not initialized at this point; but since `NonNull<_>` is Copy, -// we can overwrite the undefined value without side effects. This is -// safe since all PL011State instances are created by QOM code which -// calls this function to initialize the fields; therefore no code is -// able to access an invalid self.clock value. -unsafe { -let dev: &mut DeviceState = self.upcast_mut (); - self.clock =NonNull ::new (qdev_init_clock_in ( - dev, -CLK_NAME .as_ptr (), -None ,/* pl011_clock_update */ -addr_of_mut! (*self).cast ::<c_void >(), -ClockEvent ::ClockUpdate .0 , - )) - .unwrap (); - } +// self.clock is not initialized at this point; but since `Owned<_>` is +// not Drop, we can overwrite the undefined value without side effects; +// it's not sound but, because for all PL011State instances are created +// by QOM code which calls this function to initialize the fields, at +// leastno code is able to access an invalid self.clock value. + self.clock = self.init_clock_in ("clk" , &Self ::clock_update,ClockEvent ::ClockUpdate ); + } + +const fn clock_update (&self, _event:ClockEvent ) { +/* pl011_trace_baudrate_change(s); */ }fn post_init (&self) {
rust/qemu-api/src/prelude.rs +2 -0
@@ -7,6 +7,8 @@pub use crate ::cell::BqlCell ;pub use crate ::cell::BqlRefCell ; +pub use crate ::qdev::DeviceMethods ; +pub use crate ::qom::IsA ;pub use crate ::qom::Object ;pub use crate ::qom::ObjectCast ;
rust/qemu-api/src/qdev.rs +106 -3
123 lines not on this page · raw message
rust/qemu-api/src/vmstate.rs +2 -2
@@ -470,11 +470,11 @@ macro_rules! vmstate_clock { $crate::assert_field_type!( $struct_name, $field_name, - core::ptr::NonNull <$crate ::bindings::Clock > + $crate ::qom::Owned <$crate ::bindings::Clock > ); $crate::offset_of!($struct_name, $field_name) }, - size: ::core::mem::size_of::<*const $crate::bindings::Clock>(), + size: ::core::mem::size_of::<*const $crate::qdev::Clock>(), flags: VMStateFlags(VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_POINTER.0), vmsd: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_clock) }, ..$crate::zeroable::Zeroable ::ZERO
Signature · 1 line
2.48.1
Re: [PATCH 04/12] rust: qdev: add clock creation
Quoted 22 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:15 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 04/12] rust: qdev: add clock creation
X-Mailer: git-send-email 2.48.1Add a Rust version of
qdev_init_clock_in, which can be used ininstance_init. There are a couple differences with the C version:- in Rust the object keeps its own reference to the clock (in addition to the one embedded in the NamedClockList), and the reference is dropped automatically by
instance_finalize(); this is encoded in the signature ofDeviceClassMethods::init_clock_in, which makes the lifetime of the clock independent of that of the object it holds. This goes unnoticed in the C version and is due to the existence of aliases.- also, anything that happens during
instance_inituses thepinned_initframework to operate on a partially initialized object, and is done through class methods (i.e. through DeviceClassMethods rather than DeviceMethods) because the device does not exist yet. Therefore, Rust code *must* create clocks frominstance_init, which is stricter than C.
Quoted 8 lines
Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/char/pl011/src/device.rs | 43 +++++------- rust/qemu-api/src/prelude.rs | 2 + rust/qemu-api/src/qdev.rs | 109 ++++++++++++++++++++++++++++++- rust/qemu-api/src/vmstate.rs | 4 +- 4 files changed, 127 insertions(+), 31 deletions(-)
PATCH 5/12 rust: qom: allow initializing interface vtables
In reply to Paolo BonziniUnlike regular classes, interface vtables can only be obtained via object_class_dynamic_cast. Provide a wrapper that allows accessing the vtable and pass it to a ClassInitImpl implementation, for example ClassInitImpl<ResettableClass>.
--- rust/qemu-api/src/prelude.rs | 1 + rust/qemu-api/src/qom.rs | 45 ++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-)
rust/qemu-api/src/prelude.rs +1 -0
@@ -9,6 +9,7 @@pub use crate ::qdev::DeviceMethods ; +pub use crate ::qom::InterfaceType ;pub use crate ::qom::IsA ;pub use crate ::qom::Object ;pub use crate ::qom::ObjectCast ;
rust/qemu-api/src/qom.rs +43 -2
57 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH 6/12 rust: qdev: make ObjectImpl a supertrait of DeviceImpl
In reply to Paolo BonziniIn practice it has to be implemented always in order to access an implementation of ClassInitImpl<ObjectClass>. Make the relationship explicit in the code.
--- rust/qemu-api/src/qdev.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
rust/qemu-api/src/qdev.rs +2 -2
@@ -17,12 +17,12 @@ callbacks::FnCall, cell::bql_locked, prelude::*, - qom::{ClassInitImpl, ObjectClass, Owned}, + qom::{ClassInitImpl, ObjectClass, ObjectImpl, Owned}, vmstate::VMStateDescription, };/// Trait providing the contents of [`DeviceClass`]. -pub trait DeviceImpl { +pub trait DeviceImpl :ObjectImpl {/// _Realization_ is the second stage of device creation. It contains /// all operations that depend on device properties and can fail (note: /// this is not yet supported for Rust devices).
Signature · 1 line
2.48.1
PATCH 7/12 rust: qdev: switch from legacy reset to Resettable
In reply to Paolo Bonzini--- meson.build | 1 + rust/hw/char/pl011/src/device.rs | 10 ++- rust/qemu-api/src/qdev.rs | 111 ++++++++++++++++++++++++------- rust/qemu-api/tests/tests.rs | 5 +- 4 files changed, 99 insertions(+), 28 deletions(-)
meson.build +1 -0
@@ -4072,6 +4072,7 @@ if have_rust 'MigrationPriority', 'QEMUChrEvent', 'QEMUClockType', + 'ResetType', 'device_endian', 'module_init_type', ]
rust/hw/char/pl011/src/device.rs +7 -3
@@ -18,7 +18,7 @@ c_str, impl_vmstate_forward, irq::InterruptSource, prelude::*, - qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property}, + qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesImpl}, qom::{ClassInitImpl, ObjectImpl, Owned, ParentField}, sysbus::{SysBusDevice, SysBusDeviceClass}, vmstate::VMStateDescription,
@@ -171,7 +171,10 @@ fn vmsd() -> Option<&'static VMStateDescription> {Some (&device_class::VMSTATE_PL011 ) }const REALIZE :Option <fn (&Self )> =Some (Self ::realize); -const RESET :Option <fn (&Self )> =Some (Self ::reset); +} + +impl ResettablePhasesImpl for PL011State { +const HOLD :Option <fn (&Self ,ResetType )> =Some (Self ::reset_hold); }impl PL011Registers {
@@ -622,7 +625,7 @@ pub fn realize(&self) { } } -pub fn reset (&self) { +pub fn reset_hold (&self, _type:ResetType ) { self.regs.borrow_mut ().reset (); }
@@ -737,3 +740,4 @@ impl ObjectImpl for PL011Luminary { }impl DeviceImpl for PL011Luminary {} +impl ResettablePhasesImpl for PL011Luminary {}
rust/qemu-api/src/qdev.rs +87 -24
153 lines not on this page · raw message
rust/qemu-api/tests/tests.rs +4 -1
@@ -13,7 +13,7 @@ cell::{self, BqlCell}, declare_properties, define_property, prelude::*, - qdev::{DeviceClass, DeviceImpl, DeviceState, Property}, + qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl}, qom::{ClassInitImpl, ObjectImpl, ParentField}, vmstate::VMStateDescription, zeroable::Zeroable,
@@ -61,6 +61,8 @@ impl ObjectImpl for DummyState {const ABSTRACT :bool =false ; } +impl ResettablePhasesImpl for DummyState {} +impl DeviceImpl for DummyState {fn properties () -> &'static [Property ] { &DUMMY_PROPERTIES
@@ -101,6 +103,7 @@ impl ObjectImpl for DummyChildState {const ABSTRACT :bool =false ; } +impl ResettablePhasesImpl for DummyChildState {}impl DeviceImpl for DummyChildState {}impl ClassInitImpl <DummyClass >for DummyChildState {
Signature · 1 line
2.48.1
Re: [PATCH 07/12] rust: qdev: switch from legacy reset to Resettable
Quoted 5 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:18 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 07/12] rust: qdev: switch from legacy reset to Resettable
X-Mailer: git-send-email 2.48.1
Quoted 8 lines
Signed-off-by Paolo Bonzini <[email protected]>--- meson.build | 1 + rust/hw/char/pl011/src/device.rs | 10 ++- rust/qemu-api/src/qdev.rs | 111 ++++++++++++++++++++++++------- rust/qemu-api/tests/tests.rs | 5 +- 4 files changed, 99 insertions(+), 28 deletions(-)
PATCH 8/12 rust: bindings: add Send and Sync markers for types that have bindings
In reply to Paolo BonziniThis is needed for the MemoryRegionOps<T> to be declared as static; Rust requires static elements to be Sync.
--- rust/qemu-api/src/bindings.rs | 46 +++++++++++++++++++++++++++++++++++ rust/qemu-api/src/irq.rs | 3 +++ 2 files changed, 49 insertions(+)
rust/qemu-api/src/bindings.rs +46 -0
@@ -21,9 +21,55 @@#[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`. +unsafe impl Send for BusState {} +unsafe impl Sync for BusState {} + +unsafe impl Send for CharBackend {} +unsafe impl Sync for CharBackend {} + +unsafe impl Send for Chardev {} +unsafe impl Sync for Chardev {} + +unsafe impl Send for Clock {} +unsafe impl Sync for Clock {} + +unsafe impl Send for DeviceState {} +unsafe impl Sync for DeviceState {} + +unsafe impl Send for MemoryRegion {} +unsafe impl Sync for MemoryRegion {} + +unsafe impl Send for ObjectClass {} +unsafe impl Sync for ObjectClass {} + +unsafe impl Send for Object {} +unsafe impl Sync for Object {} + +unsafe impl Send for SysBusDevice {} +unsafe impl Sync for SysBusDevice {} + +// 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 {}
rust/qemu-api/src/irq.rs +3 -0
@@ -43,6 +43,9 @@ pub struct InterruptSource<T = bool> _marker: PhantomData<T>, } +// SAFETY: the implementation asserts via `BqlCell` that the BQL is taken +unsafe impl <T >Sync for InterruptSource <T >where c_int :From <T > {} +impl InterruptSource <bool > {/// Send a low (`false`) value to the interrupt sink. pub fn lower (&self) {
Signature · 1 line
2.48.1
PATCH 9/12 rust: bindings for MemoryRegionOps
In reply to Paolo Bonzini--- rust/hw/char/pl011/src/device.rs | 51 +++---- rust/hw/char/pl011/src/lib.rs | 1 - rust/hw/char/pl011/src/memory_ops.rs | 34 ----- rust/qemu-api/meson.build | 1 + rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/memory.rs | 191 +++++++++++++++++++++++++++ rust/qemu-api/src/sysbus.rs | 7 +- rust/qemu-api/src/zeroable.rs | 1 + 8 files changed, 226 insertions(+), 61 deletions(-) delete mode 100644 rust/hw/char/pl011/src/memory_ops.rs create mode 100644 rust/qemu-api/src/memory.rs
rust/hw/char/pl011/src/device.rs +28 -23
@@ -10,13 +10,14 @@use qemu_api::{ bindings::{ - error_fatal, hwaddr, memory_region_init_io, qdev_prop_set_chr, qemu_chr_fe_accept_input, - qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, qemu_chr_fe_write_all, qemu_irq, - sysbus_connect_irq, sysbus_mmio_map, sysbus_realize,CharBackend ,Chardev ,MemoryRegion , -QEMUChrEvent ,CHR_IOCTL_SERIAL_SET_BREAK , + error_fatal, qdev_prop_set_chr, qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, + qemu_chr_fe_set_handlers, qemu_chr_fe_write_all, qemu_irq, sysbus_connect_irq, + sysbus_mmio_map, sysbus_realize,CharBackend ,Chardev ,QEMUChrEvent , +CHR_IOCTL_SERIAL_SET_BREAK , }, c_str, impl_vmstate_forward, irq::InterruptSource , + memory::{hwaddr,MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder }, prelude::*, qdev::{Clock ,ClockEvent ,DeviceImpl ,DeviceState ,Property ,ResetType ,ResettablePhasesImpl }, qom::{ClassInitImpl ,ObjectImpl ,Owned ,ParentField },
@@ -26,7 +27,6 @@use crate ::{ device_class, - memory_ops::PL011_OPS , registers::{self,Interrupt },RegisterOffset , };
@@ -487,20 +487,24 @@ impl PL011State {/// location/instance. All its fields are expected to hold unitialized /// values with the sole exception of `parent_obj`. unsafe fn init (&mut self) { +static PL011_OPS :MemoryRegionOps <PL011State > =MemoryRegionOpsBuilder ::<PL011State >::new () + .read (&PL011State ::read) + .write (&PL011State ::write) + .native_endian () + .impl_sizes (4 ,4 ) + .build (); +// SAFETY: // // self and self.iomem are guaranteed to be valid at this point since callers // must make sure the `self` reference is valid. -unsafe { -memory_region_init_io ( -addr_of_mut! (self.iomem), -addr_of_mut! (*self).cast ::<Object >(), - &PL011_OPS , -addr_of_mut! (*self).cast ::<c_void >(), -Self ::TYPE_NAME .as_ptr (), -0x1000 , - ); - } +MemoryRegion ::init_io ( +unsafe { &mut *addr_of_mut! (self.iomem) }, +addr_of_mut! (*self), + &PL011_OPS , +"pl011" , +0x1000 , + ); self.regs =Default ::default ();
@@ -525,7 +529,7 @@ fn post_init(&self) { } } -pub fn read (&mut self, offset:hwaddr , _size:u32 ) ->u64 { +pub fn read (&self, offset:hwaddr , _size:u32 ) ->u64 {match RegisterOffset ::try_from (offset) {Err (v)if (0x3f8 ..0x400 ).contains (&(v >>2 )) => {let device_id = self.get_class ().device_id;
@@ -540,7 +544,7 @@ pub fn read(&mut self, offset: hwaddr, _size: u32) -> u64 {if update_irq { self.update ();unsafe { -qemu_chr_fe_accept_input (&mut self.char_backend); +qemu_chr_fe_accept_input (addr_of! (self.char_backend)as *mut _ ); } } result.into ()
@@ -548,7 +552,7 @@ pub fn read(&mut self, offset: hwaddr, _size: u32) -> u64 { } } -pub fn write (&mut self, offset:hwaddr , value:u64 ) { +pub fn write (&self, offset:hwaddr , value:u64 , _size:u32 ) {let mut update_irq =false ;if let Ok (field) =RegisterOffset ::try_from (offset) {// qemu_chr_fe_write_all() calls into the can_receive
@@ -561,14 +565,15 @@ pub fn write(&mut self, offset: hwaddr, value: u64) {// XXX this blocks entire thread. Rewrite to use // qemu_chr_fe_write and background I/O callbacks unsafe { -qemu_chr_fe_write_all (&mut self.char_backend, &ch,1 ); +qemu_chr_fe_write_all (addr_of! (self.char_backend)as *mut _ , &ch,1 ); } } - update_irq = self - .regs - .borrow_mut () - .write (field, valueas u32 , &mut self.char_backend); + update_irq = self.regs.borrow_mut ().write ( + field, + valueas u32 , +addr_of! (self.char_backend)as *mut _ , + ); }else {eprintln! ("write bad offset {offset} value {value}" ); }
rust/hw/char/pl011/src/lib.rs +0 -1
@@ -27,7 +27,6 @@mod device;mod device_class; -mod memory_ops;pub use device::pl011_create;
rust/hw/char/pl011/src/memory_ops.rs deleted +0 -34
@@ -1,34 +0,0 @@ -// Copyright 2024, Linaro Limited -// Author(s): Manos Pitsidianakis <[email protected]> -// SPDX-License-Identifier: GPL-2.0-or-later - -use core::ptr::NonNull ; -use std::os::raw::{c_uint, c_void}; - -use qemu_api::{bindings::*, zeroable::Zeroable }; - -use crate ::device::PL011State ; - -pub static PL011_OPS :MemoryRegionOps =MemoryRegionOps { - read:Some (pl011_read), - write:Some (pl011_write), - read_with_attrs:None , - write_with_attrs:None , - endianness: device_endian::DEVICE_NATIVE_ENDIAN , - valid:Zeroable ::ZERO , - impl_:MemoryRegionOps__bindgen_ty_2 { - min_access_size:4 , - max_access_size:4 , - ..Zeroable ::ZERO - }, -}; - -unsafe extern "C" fn pl011_read (opaque: *mut c_void , addr:hwaddr , size:c_uint ) ->u64 { -let mut state =NonNull ::new (opaque).unwrap ().cast ::<PL011State >(); -unsafe { state.as_mut () }.read (addr, size) -} - -unsafe extern "C" fn pl011_write (opaque: *mut c_void , addr:hwaddr , data:u64 , _size:c_uint ) { -let mut state =NonNull ::new (opaque).unwrap ().cast ::<PL011State >(); -unsafe { state.as_mut () }.write (addr, data); -}
rust/qemu-api/meson.build +1 -0
@@ -22,6 +22,7 @@ _qemu_api_rs = static_library( 'src/cell.rs', 'src/c_str.rs', 'src/irq.rs', + 'src/memory.rs', 'src/module.rs', 'src/offset_of.rs', 'src/prelude.rs',
rust/qemu-api/src/lib.rs +1 -0
@@ -18,6 +18,7 @@pub mod callbacks;pub mod cell;pub mod irq; +pub mod memory;pub mod module;pub mod offset_of;pub mod qdev;
rust/qemu-api/src/memory.rs new file +191 -0
191 lines not on this page · raw message
rust/qemu-api/src/sysbus.rs +4 -3
@@ -2,7 +2,7 @@// Author(s): Paolo Bonzini <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -use std::{ffi::CStr , ptr::addr_of}; +use std::ffi::CStr ;pub use bindings::{SysBusDevice ,SysBusDeviceClass };
@@ -10,6 +10,7 @@ bindings, cell::bql_locked, irq::InterruptSource, + memory::MemoryRegion, prelude::*, qdev::{DeviceClass, DeviceState}, qom::ClassInitImpl,
@@ -42,10 +43,10 @@ pub trait SysBusDeviceMethods: ObjectDeref/// 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`. -fn init_mmio (&self, iomem: &bindings::MemoryRegion ) { +fn init_mmio (&self, iomem: &MemoryRegion ) {assert! (bql_locked ());unsafe { - bindings::sysbus_init_mmio (self.as_mut_ptr (),addr_of! (*iomem)as *mut _ ); + bindings::sysbus_init_mmio (self.as_mut_ptr (), iomem.as_mut_ptr ()); } }
rust/qemu-api/src/zeroable.rs +1 -0
@@ -100,3 +100,4 @@ fn default() -> Self {impl_zeroable! (crate ::bindings::VMStateDescription );impl_zeroable! (crate ::bindings::MemoryRegionOps__bindgen_ty_1 );impl_zeroable! (crate ::bindings::MemoryRegionOps__bindgen_ty_2 ); +impl_zeroable! (crate ::bindings::MemoryRegionOps );
Signature · 1 line
2.48.1
Re: [PATCH 09/12] rust: bindings for MemoryRegionOps
Quoted 6 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:20 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 09/12] rust: bindings for MemoryRegionOps
X-Mailer: git-send-email 2.48.1Signed-off-by Paolo Bonzini <[email protected]>
Quoted 13 lines
--- rust/hw/char/pl011/src/device.rs | 51 +++---- rust/hw/char/pl011/src/lib.rs | 1 - rust/hw/char/pl011/src/memory_ops.rs | 34 ----- rust/qemu-api/meson.build | 1 + rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/memory.rs | 191 +++++++++++++++++++++++++++ rust/qemu-api/src/sysbus.rs | 7 +- rust/qemu-api/src/zeroable.rs | 1 + 8 files changed, 226 insertions(+), 61 deletions(-) delete mode 100644 rust/hw/char/pl011/src/memory_ops.rs create mode 100644 rust/qemu-api/src/memory.rs
PATCH 10/12 rust: irq: define ObjectType for IRQState
In reply to Paolo BonziniThis is a small preparation in order to use an Owned<IRQState> for the argument to sysbus_connect_irq.
--- rust/qemu-api/src/irq.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
rust/qemu-api/src/irq.rs +12 -3
@@ -5,11 +5,12 @@//! Bindings for interrupt sources use core::ptr; -use std::{marker::PhantomData , os::raw::c_int}; +use std::{ffi::CStr , marker::PhantomData , os::raw::c_int};use crate ::{ - bindings::{qemu_set_irq,IRQState }, + bindings::{self, qemu_set_irq}, prelude::*, + qom::ObjectClass , };/// Interrupt sources are used by devices to pass changes to a value (typically
@@ -21,7 +22,8 @@/// method sends a `true` value to the sink. If the guest has to see a /// different polarity, that change is performed by the board between the /// device and the interrupt controller. -/// +pub type IRQState = bindings::IRQState ; +/// 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 [`SysBusDeviceMethods::init_irq`], and
@@ -91,3 +93,10 @@ fn default() -> Self { } } } + +unsafe impl ObjectType for IRQState { +type Class =ObjectClass ; +const TYPE_NAME : &'staticCStr = +unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_IRQ ) }; +} +qom_isa! (IRQState :Object );
Signature · 1 line
2.48.1
Re: [PATCH 10/12] rust: irq: define ObjectType for IRQState
rust/qemu-api/src/irq.rs fn default() -> Self { · line 102
Quoted 6 lines
rust/qemu-api/src/irq.rs +6
-0+unsafe impl ObjectType for IRQState { +type Class =ObjectClass ; +const TYPE_NAME : &'staticCStr = +unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_IRQ ) }; +} +qom_isa! (IRQState :Object );
This is necessary for Owned<>, though IRQState has been defined in C. It's another example to handle the C object in Rust (the previous one is SysbusDevice).
LGTM,
PATCH 11/12 rust: chardev, qdev: add bindings to qdev_prop_set_chr
In reply to Paolo BonziniBecause the argument to the function is an Owned<Chardev>, this also adds an ObjectType implementation to Chardev.
--- rust/hw/char/pl011/src/device.rs | 1 + rust/qemu-api/meson.build | 1 + rust/qemu-api/src/chardev.rs | 19 +++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/qdev.rs | 9 +++++++++ 5 files changed, 31 insertions(+) create mode 100644 rust/qemu-api/src/chardev.rs
rust/hw/char/pl011/src/device.rs +1 -0
@@ -15,6 +15,7 @@ sysbus_mmio_map, sysbus_realize, CharBackend, Chardev, QEMUChrEvent, CHR_IOCTL_SERIAL_SET_BREAK, }, + chardev::Chardev, c_str, impl_vmstate_forward, irq::InterruptSource, memory::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder},
rust/qemu-api/meson.build +1 -0
@@ -20,6 +20,7 @@ _qemu_api_rs = static_library( 'src/bitops.rs', 'src/callbacks.rs', 'src/cell.rs', + 'src/chardev.rs', 'src/c_str.rs', 'src/irq.rs', 'src/memory.rs',
rust/qemu-api/src/chardev.rs new file +19 -0
@@ -0,0 +1,19 @@ +// Copyright 2024 Red Hat, Inc. +// Author(s): Paolo Bonzini <[email protected]> +// SPDX-License-Identifier: GPL-2.0-or-later + +//! Bindings for character devices + +use std::ffi::CStr ; + +use crate ::{bindings, prelude::*}; + +pub type Chardev = bindings::Chardev ; +pub type ChardevClass = bindings::ChardevClass ; + +unsafe impl ObjectType for Chardev { +type Class =ChardevClass ; +const TYPE_NAME : &'staticCStr = +unsafe {CStr ::from_bytes_with_nul_unchecked (bindings::TYPE_CHARDEV ) }; +} +qom_isa! (Chardev :Object );
rust/qemu-api/src/lib.rs +1 -0
@@ -17,6 +17,7 @@pub mod c_str;pub mod callbacks;pub mod cell; +pub mod chardev;pub mod irq;pub mod memory;pub mod module;
rust/qemu-api/src/qdev.rs +9 -0
@@ -16,6 +16,7 @@ bindings::{self, Error, ResettableClass}, callbacks::FnCall, cell::bql_locked, + chardev::Chardev, prelude::*, qom::{ClassInitImpl, ObjectClass, ObjectImpl, Owned}, vmstate::VMStateDescription,
@@ -299,6 +300,14 @@ fn init_clock_out(&self, name: &str) -> Owned<Clock> {Owned ::from (&*clk) } } + +fn prop_set_chr (&self, propname: &str , chr: &Owned <Chardev >) { +assert! (bql_locked ()); +let c_propname =CString ::new (propname).unwrap (); +unsafe { + bindings::qdev_prop_set_chr (self.as_mut_ptr (), c_propname.as_ptr (), chr.as_mut_ptr ()); + } + } }impl <R :ObjectDeref >DeviceMethods for R where R ::Target :IsA <DeviceState > {}
Signature · 1 line
2.48.1
Re: [PATCH 11/12] rust: chardev, qdev: add bindings to qdev_prop_set_chr
Quoted 10 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:22 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 11/12] rust: chardev, qdev: add bindings to
qdev_prop_set_chr
X-Mailer: git-send-email 2.48.1Because the argument to the function is an Owned<Chardev>, this also adds an ObjectType implementation to Chardev.
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 9 lines
--- rust/hw/char/pl011/src/device.rs | 1 + rust/qemu-api/meson.build | 1 + rust/qemu-api/src/chardev.rs | 19 +++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/qdev.rs | 9 +++++++++ 5 files changed, 31 insertions(+) create mode 100644 rust/qemu-api/src/chardev.rs
PATCH 12/12 rust: pl011: convert pl011_create to safe Rust
In reply to Paolo BonziniNot a major change but, as a small but significant step in creating qdev bindings, show how pl011_create can be written without "unsafe" calls (apart from converting pointers to references).
This also provides a starting point for creating Error** bindings.
--- rust/hw/char/pl011/src/device.rs | 39 ++++++++++++++++---------------- rust/qemu-api/src/sysbus.rs | 34 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 23 deletions(-)
rust/hw/char/pl011/src/device.rs +19 -20
@@ -10,14 +10,12 @@use qemu_api::{ bindings::{ - error_fatal, qdev_prop_set_chr, qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, - qemu_chr_fe_set_handlers, qemu_chr_fe_write_all, qemu_irq, sysbus_connect_irq, - sysbus_mmio_map, sysbus_realize,CharBackend ,Chardev ,QEMUChrEvent , -CHR_IOCTL_SERIAL_SET_BREAK , + qemu_chr_fe_accept_input, qemu_chr_fe_ioctl, qemu_chr_fe_set_handlers, + qemu_chr_fe_write_all,CharBackend ,QEMUChrEvent ,CHR_IOCTL_SERIAL_SET_BREAK , }, chardev::Chardev , - c_str, impl_vmstate_forward, - irq::InterruptSource , + impl_vmstate_forward, + irq::{IRQState ,InterruptSource }, memory::{hwaddr,MemoryRegion ,MemoryRegionOps ,MemoryRegionOpsBuilder }, prelude::*, qdev::{Clock ,ClockEvent ,DeviceImpl ,DeviceState ,Property ,ResetType ,ResettablePhasesImpl },
@@ -698,26 +696,27 @@ pub fn post_load(&self, _version_id: u32) -> Result<(), ()> {/// # Safety /// -/// We expect the FFI user of this function to pass a valid pointer for `chr`. +/// We expect the FFI user of this function to pass a valid pointer for `chr` +/// and `irq`. #[no_mangle] pub unsafe extern "C" fn pl011_create ( addr:u64 , - irq:qemu_irq , + irq: *mut IRQState , chr: *mut Chardev , ) -> *mut DeviceState { -let pl011 =PL011State ::new (); -unsafe { -let dev = pl011.as_mut_ptr ::<DeviceState >(); -qdev_prop_set_chr (dev,c_str! ("chardev" ).as_ptr (), chr); +// SAFETY: The callers promise that they have owned references. +// They do not gift them to pl011_create, so use `Owned::from`. +let irq =unsafe {Owned ::<IRQState >::from (&*irq) }; +let chr =unsafe {Owned ::<Chardev >::from (&*chr) }; -let sysbus = pl011.as_mut_ptr ::<SysBusDevice >(); -sysbus_realize (sysbus,addr_of_mut! (error_fatal)); -sysbus_mmio_map (sysbus,0 , addr); -sysbus_connect_irq (sysbus,0 , irq); - -// return the pointer, which is kept alive by the QOM tree; drop owned ref - pl011.as_mut_ptr () - } +let dev =PL011State ::new (); + dev.prop_set_chr ("chardev" , &chr); + dev.realize (); + dev.mmio_map (0 , addr); + dev.connect_irq (0 , &irq); +// SAFETY: return the pointer, which has to be mutable and is kept alive +// by the QOM tree; drop owned ref +unsafe { dev.as_mut_ptr () } }#[repr( C )]
rust/qemu-api/src/sysbus.rs +31 -3
@@ -2,18 +2,18 @@// Author(s): Paolo Bonzini <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -use std::ffi::CStr ; +use std::{ffi::CStr , ptr::addr_of_mut};pub use bindings::{SysBusDevice ,SysBusDeviceClass };use crate ::{ bindings, cell::bql_locked, - irq::InterruptSource , + irq::{IRQState ,InterruptSource }, memory::MemoryRegion , prelude::*, qdev::{DeviceClass ,DeviceState }, - qom::ClassInitImpl , + qom::{ClassInitImpl ,Owned }, };unsafe impl ObjectType for SysBusDevice {
@@ -60,6 +60,34 @@ fn init_irq(&self, irq: &InterruptSource) { bindings::sysbus_init_irq (self.as_mut_ptr (), irq.as_ptr ()); } } + +// TODO: do we want a type like GuestAddress here? +fn mmio_map (&self, id:u32 , addr:u64 ) { +assert! (bql_locked ()); +let id:i32 = id.try_into ().unwrap (); +unsafe { + bindings::sysbus_mmio_map (self.as_mut_ptr (), id, addr); + } + } + +// Owned<> is used here because sysbus_connect_irq (via +// object_property_set_link) adds a reference to the IRQState, +// which can prolong its life +fn connect_irq (&self, id:u32 , irq: &Owned <IRQState >) { +assert! (bql_locked ()); +let id:i32 = id.try_into ().unwrap (); +unsafe { + bindings::sysbus_connect_irq (self.as_mut_ptr (), id, irq.as_mut_ptr ()); + } + } + +fn realize (&self) { +// TODO: return an Error +assert! (bql_locked ()); +unsafe { + bindings::sysbus_realize (self.as_mut_ptr (),addr_of_mut! (bindings::error_fatal)); + } + } }impl <R :ObjectDeref >SysBusDeviceMethods for R where R ::Target :IsA <SysBusDevice > {}
Signature · 1 line
2.48.1
Re: [PATCH 12/12] rust: pl011: convert pl011_create to safe Rust
Quoted 9 lines · Paolo Bonzini
Date: Fri, 7 Feb 2025 11:16:23 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 12/12] rust: pl011: convertpl011_createto safe Rust
X-Mailer: git-send-email 2.48.1Not a major change but, as a small but significant step in creating qdev bindings, show how
pl011_createcan be written without "unsafe" calls (apart from converting pointers to references).
Quoted 8 lines
This also provides a starting point for creating Error** bindings.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/char/pl011/src/device.rs | 39 ++++++++++++++++---------------- rust/qemu-api/src/sysbus.rs | 34 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 23 deletions(-)
Owned<> uses are proper, so still,