rust: qom: move bridge for TypeInfo and DeviceClass functions to common code
cover rust: qom: move bridge for TypeInfo and DeviceClass functions to common code
This is a first step towards modeling QOM in the Rust type system. This is by no means the final state of the bindings; rather, the one thing it focuses on is moving unsafe code out of devices. This in principle removes code duplication, though with only one device the duplication is still theoretical.
In particular the pl011 device includes bridge functions for instance_init, DeviceClass::realize and DeviceClass::unrealize that take raw pointers and call to (usually safe, though not always) functions that take Rust references.
These functions would be the same for all devices; parameterize them on a generic type, so that the implementation of TypeInfo.class_init can reuse them. Likewise implement TypeInfo.instance_finalize simply as std::ptr::drop_in_place, which automatically bridges to the Drop trait if the class implements it.
Some of the traits resemble those in Manos's implementation at
https://lore.kernel.org/qemu-devel/[email protected]/;
in particular DeviceImpl is an extension of his same-named trait, also covering properties and vmstate, and ClassInitImpl for now is the same as his ClassImplUnsafe. I have not added an explicit "Unsafe" suffix because I don't plan to have separate safe and unsafe versions of the traits, just like there is no DeviceImplUnsafe. The main difference lies in the usage of generics and monomorphization over macros; generics are always compiled and any compiler error messages point at the implementation rather than the invocation of the macro.
While at it, the series starts adding documentation to the QOM machinery. A lot of the extra lines of code in fact is documentation and/or safety comments.
Paolo
Paolo Bonzini (8):
rust: qom: add default definitions for ObjectImpl
rust: qom: rename Class trait to ClassInitImpl
rust: qom: convert type_info! macro to an associated const
rust: qom: move ClassInitImpl to the instance side
rust: qdev: move device_class_init! body to generic function,
ClassInitImpl implementation to macro
rust: qdev: move bridge for realize and reset functions out of pl011
rust: qom: automatically use Drop trait to implement instance_finalize
rust: qom: move bridge for TypeInfo functions out of pl011
rust/hw/char/pl011/src/device.rs | 85 ++++++--------- rust/hw/char/pl011/src/device_class.rs | 34 ------ rust/qemu-api/src/definitions.rs | 140 ++++++++++++++++++------- rust/qemu-api/src/device_class.rs | 114 +++++++++++++++++--- rust/qemu-api/tests/tests.rs | 33 +++--- 5 files changed, 246 insertions(+), 160 deletions(-)
Signature · 1 line
2.47.0
PATCH 1/8 rust: qom: add default definitions for ObjectImpl
Remove a bunch of duplicate const definitions.
--- rust/hw/char/pl011/src/device.rs | 6 ------ rust/qemu-api/src/definitions.rs | 8 ++++---- rust/qemu-api/tests/tests.rs | 4 ---- 3 files changed, 4 insertions(+), 14 deletions(-)
rust/hw/char/pl011/src/device.rs +0 -6
@@ -109,10 +109,7 @@ impl ObjectImpl for PL011State {const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011 ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (TYPE_SYS_BUS_DEVICE ); -const ABSTRACT :bool =false ;const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_init); -const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; -const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; }#[repr( C )]
@@ -666,8 +663,5 @@ impl ObjectImpl for PL011Luminary {const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011_LUMINARY ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (crate ::TYPE_PL011 ); -const ABSTRACT :bool =false ;const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_luminary_init); -const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; -const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; }
rust/qemu-api/src/definitions.rs +4 -4
@@ -14,10 +14,10 @@ pub trait ObjectImpl {const TYPE_INFO :TypeInfo ;const TYPE_NAME : &'staticCStr ;const PARENT_TYPE_NAME :Option <&'staticCStr >; -const ABSTRACT :bool ; -const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )>; -const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )>; -const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )>; +const ABSTRACT :bool =false ; +const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; +const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; +const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; }pub trait Class {
rust/qemu-api/tests/tests.rs +0 -4
@@ -58,10 +58,6 @@ impl ObjectImpl for DummyState {const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =c_str! ("dummy" );const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (device_class::TYPE_DEVICE ); -const ABSTRACT :bool =false ; -const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; -const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; -const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; }impl Class for DummyClass {
Signature · 1 line
2.47.0
PATCH 2/8 rust: qom: rename Class trait to ClassInitImpl
In reply to Paolo BonziniWhile at it, document it.
--- rust/hw/char/pl011/src/device.rs | 4 ++-- rust/qemu-api/src/definitions.rs | 25 ++++++++++++++++++++++--- rust/qemu-api/tests/tests.rs | 4 ++-- 3 files changed, 26 insertions(+), 7 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -2
@@ -117,7 +117,7 @@ pub struct PL011Class { _inner: [u8; 0], } -impl qemu_api::definitions::Class for PL011Class { +impl qemu_api::definitions::ClassInitImpl for PL011Class {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =Some (crate ::device_class::pl011_class_init); const CLASS_BASE_INIT: Option<
@@ -650,7 +650,7 @@ pub struct PL011LuminaryClass { } } -impl qemu_api::definitions::Class for PL011LuminaryClass { +impl qemu_api::definitions::ClassInitImpl for PL011LuminaryClass {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =None ; const CLASS_BASE_INIT: Option<
rust/qemu-api/src/definitions.rs +22 -3
@@ -20,8 +20,27 @@ pub trait ObjectImpl {const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; } -pub trait Class { +/// Trait used to fill in a class struct. +/// +/// Each QOM class that has virtual methods describes them in a +/// _class struct_. Class structs include a parent field corresponding +/// to the vtable of the parent class, all the way up to [`ObjectClass`]. +/// Each QOM type has one such class struct. +/// +/// The Rust implementation of methods will usually come from a trait +/// like [`ObjectImpl`]. +pub trait ClassInitImpl { +/// Function that is called after all parent class initialization +/// has occurred. On entry, the virtual method pointers are set to +/// the default values coming from the parent classes; the function +/// can change them to override virtual methods of a parent class. const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )>; + +/// Called on descendent classes after all parent class initialization +/// has occurred, but before the class itself is initialized. This +/// is only useful if a class is not a leaf, and can be used to undo +/// the effects of copying the contents of the parent's class struct +/// to the descendants. const CLASS_BASE_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void ), >;
@@ -82,8 +101,8 @@ macro_rules! type_info { instance_finalize: <$t as $crate::definitions::ObjectImpl>::INSTANCE_FINALIZE, abstract_: <$t as $crate::definitions::ObjectImpl>::ABSTRACT, class_size: ::core::mem::size_of::<<$t as $crate::definitions::ObjectImpl>::Class>(), - class_init: <<$t as $crate::definitions::ObjectImpl>::Class as $crate::definitions::Class>::CLASS_INIT, - class_base_init: <<$t as $crate::definitions::ObjectImpl>::Class as $crate::definitions::Class>::CLASS_BASE_INIT, + class_init: <<$t as $crate::definitions::ObjectImpl>::Class as $crate::definitions::ClassInitImpl>::CLASS_INIT, + class_base_init: <<$t as $crate::definitions::ObjectImpl>::Class as $crate::definitions::ClassInitImpl>::CLASS_BASE_INIT, class_data: ::core::ptr::null_mut(), interfaces: ::core::ptr::null_mut(), };
rust/qemu-api/tests/tests.rs +2 -2
@@ -7,7 +7,7 @@use qemu_api::{ bindings::*, c_str, declare_properties, define_property, - definitions::{Class ,ObjectImpl }, + definitions::{ClassInitImpl ,ObjectImpl }, device_class, device_class_init, zeroable::Zeroable , };
@@ -60,7 +60,7 @@ impl ObjectImpl for DummyState {const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (device_class::TYPE_DEVICE ); } -impl Class for DummyClass { +impl ClassInitImpl for DummyClass {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =Some (dummy_class_init); const CLASS_BASE_INIT: Option<
Signature · 1 line
2.47.0
PATCH 3/8 rust: qom: convert type_info! macro to an associated const
In reply to Paolo Bonzinitype_info! is only used in the definition of ObjectImpl::TYPE_INFO, and in fact in all of them. Pull type_info!'s definition into the ObjectImpl trait, thus simplifying the external interface of qemu_api::definitions.
--- rust/hw/char/pl011/src/device.rs | 6 ++-- rust/qemu-api/src/definitions.rs | 50 ++++++++++++++------------------ rust/qemu-api/tests/tests.rs | 1 - 3 files changed, 24 insertions(+), 33 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -4
@@ -106,7 +106,6 @@ pub struct PL011State {impl ObjectImpl for PL011State {type Class =PL011Class ; -const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011 ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (TYPE_SYS_BUS_DEVICE );const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_init);
@@ -149,7 +148,7 @@ unsafe fn init(&mut self) { addr_of_mut!(*self).cast::<Object>(), &PL011_OPS, addr_of_mut!(*self).cast::<c_void>(), - Self::TYPE_INFO.name, + Self::TYPE_NAME.as_ptr(), 0x1000, );sysbus_init_mmio (sbd,addr_of_mut! (self.iomem));
@@ -598,7 +597,7 @@ pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> { chr: *mut Chardev, ) -> *mut DeviceState {unsafe { -let dev: *mut DeviceState =qdev_new (PL011State ::TYPE_INFO .name); +let dev: *mut DeviceState =qdev_new (PL011State ::TYPE_NAME .as_ptr ());let sysbus: *mut SysBusDevice = dev.cast ::<SysBusDevice >();qdev_prop_set_chr (dev,c_str! ("chardev" ).as_ptr (), chr);
@@ -660,7 +659,6 @@ impl qemu_api::definitions::ClassInitImpl for PL011LuminaryClass {impl ObjectImpl for PL011Luminary {type Class =PL011LuminaryClass ; -const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011_LUMINARY ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (crate ::TYPE_PL011 );const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_luminary_init);
rust/qemu-api/src/definitions.rs +22 -28
65 lines not on this page · raw message
rust/qemu-api/tests/tests.rs +0 -1
@@ -55,7 +55,6 @@ pub struct DummyClass {impl ObjectImpl for DummyState {type Class =DummyClass ; -const TYPE_INFO : qemu_api::bindings::TypeInfo = qemu_api::type_info! {Self };const TYPE_NAME : &'staticCStr =c_str! ("dummy" );const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (device_class::TYPE_DEVICE ); }
Signature · 1 line
2.47.0
PATCH 4/8 rust: qom: move ClassInitImpl to the instance side
In reply to Paolo BonziniPut all traits on the instance struct, which makes it possible to reuse class structs if no new virtual methods or class fields are added. This is almost always the case for devices (because they are leaf classes), which is the primary use case for Rust.
This is also simpler: soon we will find the implemented methods without macros, and this removes the need to go from the class struct to the instance struct to find the implementation of the *Impl traits.
--- rust/hw/char/pl011/src/device.rs | 4 ++-- rust/qemu-api/src/definitions.rs | 8 ++++---- rust/qemu-api/tests/tests.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -2
@@ -116,7 +116,7 @@ pub struct PL011Class { _inner: [u8; 0], } -impl qemu_api::definitions::ClassInitImpl for PL011Class { +impl qemu_api::definitions::ClassInitImpl for PL011State {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =Some (crate ::device_class::pl011_class_init); const CLASS_BASE_INIT: Option<
@@ -649,7 +649,7 @@ pub struct PL011LuminaryClass { } } -impl qemu_api::definitions::ClassInitImpl for PL011LuminaryClass { +impl qemu_api::definitions::ClassInitImpl for PL011Luminary {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =None ; const CLASS_BASE_INIT: Option<
rust/qemu-api/src/definitions.rs +4 -4
@@ -9,8 +9,8 @@use crate ::bindings::{Object ,ObjectClass ,TypeInfo };/// Trait a type must implement to be registered with QEMU. -pub trait ObjectImpl :Sized { -type Class :ClassInitImpl ; +pub trait ObjectImpl :ClassInitImpl +Sized { +type Class ;const TYPE_NAME : &'staticCStr ;const PARENT_TYPE_NAME :Option <&'staticCStr >;const ABSTRACT :bool =false ;
@@ -32,8 +32,8 @@ pub trait ObjectImpl: Sized { instance_finalize: Self::INSTANCE_FINALIZE, abstract_: Self::ABSTRACT, class_size: core::mem::size_of::<Self::Class>(), - class_init: <Self::Class as ClassInitImpl>::CLASS_INIT, - class_base_init: <Self::Class as ClassInitImpl>::CLASS_BASE_INIT, + class_init: <Self as ClassInitImpl>::CLASS_INIT, + class_base_init: <Self as ClassInitImpl>::CLASS_BASE_INIT, class_data: core::ptr::null_mut(), interfaces: core::ptr::null_mut(), };
rust/qemu-api/tests/tests.rs +1 -1
@@ -59,7 +59,7 @@ impl ObjectImpl for DummyState {const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (device_class::TYPE_DEVICE ); } -impl ClassInitImpl for DummyClass { +impl ClassInitImpl for DummyState {const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> =Some (dummy_class_init); const CLASS_BASE_INIT: Option<
Signature · 1 line
2.47.0
PATCH 5/8 rust: qdev: move device_class_init! body to generic function, ClassInitImpl implementation to macro
In reply to Paolo BonziniUse a trait to access the former parameters to device_class_init!. This allows hiding the details of the class_init implementation behind a generic function and makes higher-level functionality available from qemu_api.
The implementation of ClassInitImpl is then the same for all devices and is easily macroized. Later on, we can remove the need to implement ClassInitImpl by hand for all device types, and stop making rust_device_class_init<>() public.
--- rust/hw/char/pl011/src/device.rs | 34 ++++++----- rust/hw/char/pl011/src/device_class.rs | 8 --- rust/qemu-api/src/device_class.rs | 85 +++++++++++++++++++++----- rust/qemu-api/tests/tests.rs | 28 ++++----- 4 files changed, 100 insertions(+), 55 deletions(-)
rust/hw/char/pl011/src/device.rs +19 -15
@@ -12,11 +12,13 @@ bindings::{self, *}, c_str, definitions::ObjectImpl, - device_class::TYPE_SYS_BUS_DEVICE, + device_class::{DeviceImpl, TYPE_SYS_BUS_DEVICE}, + impl_device_class, irq::InterruptSource, };use crate ::{ + device_class, memory_ops::PL011_OPS , registers::{self,Interrupt },RegisterOffset ,
@@ -116,14 +118,20 @@ pub struct PL011Class { _inner: [u8; 0], } -impl qemu_api::definitions::ClassInitImpl for PL011State { -const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> = -Some (crate ::device_class::pl011_class_init); -const CLASS_BASE_INIT :Option < -unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void ), - > =None ; +impl DeviceImpl for PL011State { +fn properties () -> &'static [Property ] { + &device_class::PL011_PROPERTIES + } +fn vmsd () ->Option <&'staticVMStateDescription > { +Some (&device_class::VMSTATE_PL011 ) + } +const REALIZE :Option <unsafe extern "C" fn (*mut DeviceState , *mut *mut Error )> = +Some (device_class::pl011_realize); +const RESET :Option <unsafe extern "C" fn (*mut DeviceState )> =Some (device_class::pl011_reset); } +impl_device_class! (PL011State ); +impl PL011State {/// Initializes a pre-allocated, unitialized instance of `PL011State`. ///
@@ -649,17 +657,13 @@ pub struct PL011LuminaryClass { } } -impl qemu_api::definitions::ClassInitImpl for PL011Luminary { -const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> = -None ; -const CLASS_BASE_INIT :Option < -unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void ), - > =None ; -} -impl ObjectImpl for PL011Luminary {type Class =PL011LuminaryClass ;const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011_LUMINARY ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (crate ::TYPE_PL011 );const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_luminary_init); } + +impl DeviceImpl for PL011Luminary {} + +impl_device_class! (PL011Luminary );
rust/hw/char/pl011/src/device_class.rs +0 -8
@@ -93,14 +93,6 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int { ), } -qemu_api::device_class_init! { - pl011_class_init, - props =>PL011_PROPERTIES , - realize_fn =>Some (pl011_realize), - legacy_reset_fn =>Some (pl011_reset), - vmsd =>VMSTATE_PL011 , -} -/// # Safety /// /// We expect the FFI user of this function to pass a valid pointer, that has
rust/qemu-api/src/device_class.rs +69 -16
94 lines not on this page · raw message
rust/qemu-api/tests/tests.rs +12 -16
@@ -7,8 +7,9 @@use qemu_api::{ bindings::*, c_str, declare_properties, define_property, - definitions::{ClassInitImpl ,ObjectImpl }, - device_class, device_class_init, + definitions::ObjectImpl , + device_class::{self,DeviceImpl }, + impl_device_class, zeroable::Zeroable , };
@@ -45,28 +46,23 @@ pub struct DummyClass { ), } -device_class_init! { - dummy_class_init, - props =>DUMMY_PROPERTIES , - realize_fn =>None , - legacy_reset_fn =>None , - vmsd =>VMSTATE , - } -impl ObjectImpl for DummyState {type Class =DummyClass ;const TYPE_NAME : &'staticCStr =c_str! ("dummy" );const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (device_class::TYPE_DEVICE ); } -impl ClassInitImpl for DummyState { -const CLASS_INIT :Option <unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void )> = -Some (dummy_class_init); -const CLASS_BASE_INIT :Option < -unsafe extern "C" fn (klass: *mut ObjectClass , data: *mut c_void ), - > =None ; +impl DeviceImpl for DummyState { +fn properties () -> &'static [Property ] { + &DUMMY_PROPERTIES + } +fn vmsd () ->Option <&'staticVMStateDescription > { +Some (&VMSTATE ) + } } +impl_device_class! (DummyState ); +unsafe {module_call_init (module_init_type::MODULE_INIT_QOM );object_unref (object_new (DummyState ::TYPE_NAME .as_ptr ()).cast ());
Signature · 1 line
2.47.0
PATCH 6/8 rust: qdev: move bridge for realize and reset functions out of pl011
In reply to Paolo BonziniAllow the DeviceImpl trait to expose safe Rust functions. rust_device_class_init<> adds thunks around the functions in DeviceImpl.
--- rust/hw/char/pl011/src/device.rs | 5 ++-- rust/hw/char/pl011/src/device_class.rs | 26 ---------------- rust/qemu-api/src/definitions.rs | 2 +- rust/qemu-api/src/device_class.rs | 41 ++++++++++++++++++++++---- 4 files changed, 38 insertions(+), 36 deletions(-)
rust/hw/char/pl011/src/device.rs +2 -3
@@ -125,9 +125,8 @@ fn properties() -> &'static [Property] {fn vmsd () ->Option <&'staticVMStateDescription > {Some (&device_class::VMSTATE_PL011 ) } -const REALIZE :Option <unsafe extern "C" fn (*mut DeviceState , *mut *mut Error )> = -Some (device_class::pl011_realize); -const RESET :Option <unsafe extern "C" fn (*mut DeviceState )> =Some (device_class::pl011_reset); +const REALIZE :Option <fn (&mut Self )> =Some (Self ::realize); +const RESET :Option <fn (&mut Self )> =Some (Self ::reset); }impl_device_class! (PL011State );
rust/hw/char/pl011/src/device_class.rs +0 -26
@@ -92,29 +92,3 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int { default =true ), } - -/// # Safety -/// -/// We expect the FFI user of this function to pass a valid pointer, that has -/// the same size as [`PL011State`]. We also expect the device is -/// readable/writeable from one thread at any time. -pub unsafe extern "C" fn pl011_realize (dev: *mut DeviceState , _errp: *mut *mut Error ) { -unsafe { -assert! (!dev.is_null ()); -let mut state =NonNull ::new_unchecked (dev.cast ::<PL011State >()); - state.as_mut ().realize (); - } -} - -/// # Safety -/// -/// We expect the FFI user of this function to pass a valid pointer, that has -/// the same size as [`PL011State`]. We also expect the device is -/// readable/writeable from one thread at any time. -pub unsafe extern "C" fn pl011_reset (dev: *mut DeviceState ) { -unsafe { -assert! (!dev.is_null ()); -let mut state =NonNull ::new_unchecked (dev.cast ::<PL011State >()); - state.as_mut ().reset (); - } -}
rust/qemu-api/src/definitions.rs +1 -1
@@ -47,7 +47,7 @@ pub trait ObjectImpl: ClassInitImpl + Sized {/// Each QOM type has one such class struct. /// /// The Rust implementation of methods will usually come from a trait -/// like [`ObjectImpl`]. +/// like [`ObjectImpl`] or [`DeviceImpl`](crate::device_class::DeviceImpl). pub trait ClassInitImpl {/// Function that is called after all parent class initialization /// has occurred. On entry, the virtual method pointers are set to
rust/qemu-api/src/device_class.rs +31 -6
@@ -8,6 +8,7 @@ self, DeviceClass, DeviceState, Error, ObjectClass, Property, VMStateDescription, }; +/// Trait providing the contents of [`DeviceClass`]. pub trait DeviceImpl {/// _Realization_ is the second stage of device creation. It contains /// all operations that depend on device properties and can fail (note:
@@ -15,14 +16,14 @@ pub trait DeviceImpl {/// /// If not `None`, the parent class's `realize` method is overridden /// with the function pointed to by `REALIZE`. -const REALIZE :Option <unsafe extern "C" fn (*mut DeviceState , *mut *mut Error )> =None ; +const REALIZE :Option <fn (&mut Self )> =None ;/// If not `None`, the parent class's `reset` method is overridden /// with the function pointed to by `RESET`. /// /// Rust does not yet support the three-phase reset protocol; this is /// usually okay for leaf classes. -const RESET :Option <unsafe extern "C" fn (dev: *mut DeviceState )> =None ; +const RESET :Option <fn (&mut Self )> =None ;/// An array providing the properties that the user can set on the /// device. Not a `const` because referencing statics in constants
@@ -39,6 +40,30 @@ fn vmsd() -> Option<&'static VMStateDescription> { } } +/// # Safety +/// +/// This function is only called through the QOM machinery and +/// the `impl_device_class!` macro. +/// We expect the FFI user of this function to pass a valid pointer that +/// can be downcasted to type `T`. We also expect the device is +/// readable/writeable from one thread at any time. +unsafe extern "C" fn rust_realize_fn <T :DeviceImpl >(dev: *mut DeviceState , _errp: *mut *mut Error ) { +assert! (!dev.is_null ()); +let state = dev.cast ::<T >(); +T ::REALIZE .unwrap ()(unsafe { &mut *state }); +} + +/// # Safety +/// +/// We expect the FFI user of this function to pass a valid pointer that +/// can be downcasted to type `T`. We also expect the device is +/// readable/writeable from one thread at any time. +unsafe extern "C" fn rust_reset_fn <T :DeviceImpl >(dev: *mut DeviceState ) { +assert! (!dev.is_null ()); +let state = dev.cast ::<T >(); +T ::RESET .unwrap ()(unsafe { &mut *state }); +} +/// # Safety /// /// We expect the FFI user of this function to pass a valid pointer that
@@ -51,11 +80,11 @@ fn vmsd() -> Option<&'static VMStateDescription> {let mut dc = ::core::ptr::NonNull ::new (klass.cast ::<DeviceClass >()).unwrap ();unsafe {let dc = dc.as_mut (); -if let Some (realize_fn) = <T as DeviceImpl >::REALIZE { - dc.realize =Some (realize_fn); +if <T as DeviceImpl >::REALIZE .is_some () { + dc.realize =Some (rust_realize_fn ::<T >); } -if let Some (reset_fn) = <T as DeviceImpl >::RESET { - bindings::device_class_set_legacy_reset (dc,Some (reset_fn)); +if <T as DeviceImpl >::RESET .is_some () { + bindings::device_class_set_legacy_reset (dc,Some (rust_reset_fn ::<T >)); }if let Some (vmsd) = <T as DeviceImpl >::vmsd () { dc.vmsd = vmsd;
Signature · 1 line
2.47.0
PATCH 7/8 rust: qom: automatically use Drop trait to implement instance_finalize
In reply to Paolo BonziniReplace the customizable INSTANCE_FINALIZE with a generic function that drops the Rust object.
--- rust/qemu-api/src/definitions.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
rust/qemu-api/src/definitions.rs +8 -2
@@ -8,6 +8,13 @@use crate ::bindings::{Object ,ObjectClass ,TypeInfo }; +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 QOM core as the instance_finalize function +// for class T +unsafe { std::ptr::drop_in_place (obj.cast ::<T >()) } +} +/// Trait a type must implement to be registered with QEMU. pub trait ObjectImpl :ClassInitImpl +Sized {type Class ;
@@ -16,7 +23,6 @@ pub trait ObjectImpl: ClassInitImpl + Sized {const ABSTRACT :bool =false ;const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ;const INSTANCE_POST_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =None ; -const INSTANCE_FINALIZE :Option <unsafe extern "C" fn (obj: *mut Object )> =None ;const TYPE_INFO :TypeInfo =TypeInfo { name:Self ::TYPE_NAME .as_ptr (),
@@ -29,7 +35,7 @@ pub trait ObjectImpl: ClassInitImpl + Sized { instance_align: core::mem::align_of::<Self>(), instance_init: Self::INSTANCE_INIT, instance_post_init: Self::INSTANCE_POST_INIT, - instance_finalize: Self::INSTANCE_FINALIZE, + instance_finalize: Some(drop_object::<Self>), abstract_: Self::ABSTRACT, class_size: core::mem::size_of::<Self::Class>(), class_init: <Self as ClassInitImpl>::CLASS_INIT,
Signature · 1 line
2.47.0
PATCH 8/8 rust: qom: move bridge for TypeInfo functions out of pl011
In reply to Paolo BonziniAllow the ObjectImpl trait to expose Rust functions that avoid raw pointers (though INSTANCE_INIT for example is still unsafe). ObjectImpl::TYPE_INFO adds thunks around the functions in ObjectImpl.
While at it, document TypeInfo.
--- rust/hw/char/pl011/src/device.rs | 40 +++++++--------------- rust/qemu-api/src/definitions.rs | 57 +++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 32 deletions(-)
rust/hw/char/pl011/src/device.rs +12 -28
@@ -110,7 +110,7 @@ impl ObjectImpl for PL011State {type Class =PL011Class ;const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011 ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (TYPE_SYS_BUS_DEVICE ); -const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_init); +const INSTANCE_INIT :Option <unsafe fn (&mut Self )> =Some (Self ::init); }#[repr( C )]
@@ -615,19 +615,6 @@ pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> { } } -/// # Safety -/// -/// We expect the FFI user of this function to pass a valid pointer, that has -/// the same size as [`PL011State`]. We also expect the device is -/// readable/writeable from one thread at any time. -pub unsafe extern "C" fn pl011_init (obj: *mut Object ) { -unsafe { -debug_assert! (!obj.is_null ()); -let mut state =NonNull ::new_unchecked (obj.cast ::<PL011State >()); - state.as_mut ().init (); - } -} -#[repr( C )] #[derive( Debug , qemu_api_macros:: Object )] /// PL011 Luminary device model.
@@ -640,19 +627,16 @@ pub struct PL011LuminaryClass { _inner: [u8; 0], } -/// 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. -pub unsafe extern "C" fn pl011_luminary_init (obj: *mut Object ) { -unsafe { -debug_assert! (!obj.is_null ()); -let mut state =NonNull ::new_unchecked (obj.cast ::<PL011Luminary >()); -let state = state.as_mut (); - state.parent_obj.device_id =DeviceId ::Luminary ; +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 ; } }
@@ -660,7 +644,7 @@ impl ObjectImpl for PL011Luminary {type Class =PL011LuminaryClass ;const TYPE_NAME : &'staticCStr =crate ::TYPE_PL011_LUMINARY ;const PARENT_TYPE_NAME :Option <&'staticCStr > =Some (crate ::TYPE_PL011 ); -const INSTANCE_INIT :Option <unsafe extern "C" fn (obj: *mut Object )> =Some (pl011_luminary_init); +const INSTANCE_INIT :Option <unsafe fn (&mut Self )> =Some (Self ::init); }impl DeviceImpl for PL011Luminary {}
rust/qemu-api/src/definitions.rs +53 -4
80 lines not on this page · raw message
Signature · 1 line
2.47.0