mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-31 14:02:05 -06:00
rust: qdev: move bridge for realize and reset functions out of pl011
Allow the DeviceImpl trait to expose safe Rust functions. rust_device_class_init<> adds thunks around the functions in DeviceImpl. Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
8c80c472da
commit
f75fb90ff2
4 changed files with 33 additions and 36 deletions
|
@ -125,9 +125,8 @@ impl DeviceImpl for PL011State {
|
||||||
fn vmsd() -> Option<&'static VMStateDescription> {
|
fn vmsd() -> Option<&'static VMStateDescription> {
|
||||||
Some(&device_class::VMSTATE_PL011)
|
Some(&device_class::VMSTATE_PL011)
|
||||||
}
|
}
|
||||||
const REALIZE: Option<unsafe extern "C" fn(*mut DeviceState, *mut *mut Error)> =
|
const REALIZE: Option<fn(&mut Self)> = Some(Self::realize);
|
||||||
Some(device_class::pl011_realize);
|
const RESET: Option<fn(&mut Self)> = Some(Self::reset);
|
||||||
const RESET: Option<unsafe extern "C" fn(*mut DeviceState)> = Some(device_class::pl011_reset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_device_class!(PL011State);
|
impl_device_class!(PL011State);
|
||||||
|
|
|
@ -92,29 +92,3 @@ qemu_api::declare_properties! {
|
||||||
default = true
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ pub trait ObjectImpl: ClassInitImpl + Sized {
|
||||||
/// Each QOM type has one such class struct.
|
/// Each QOM type has one such class struct.
|
||||||
///
|
///
|
||||||
/// The Rust implementation of methods will usually come from a trait
|
/// The Rust implementation of methods will usually come from a trait
|
||||||
/// like [`ObjectImpl`].
|
/// like [`ObjectImpl`] or [`DeviceImpl`](crate::device_class::DeviceImpl).
|
||||||
pub trait ClassInitImpl {
|
pub trait ClassInitImpl {
|
||||||
/// Function that is called after all parent class initialization
|
/// Function that is called after all parent class initialization
|
||||||
/// has occurred. On entry, the virtual method pointers are set to
|
/// has occurred. On entry, the virtual method pointers are set to
|
||||||
|
|
|
@ -17,14 +17,14 @@ pub trait DeviceImpl {
|
||||||
///
|
///
|
||||||
/// If not `None`, the parent class's `realize` method is overridden
|
/// If not `None`, the parent class's `realize` method is overridden
|
||||||
/// with the function pointed to by `REALIZE`.
|
/// 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
|
/// If not `None`, the parent class's `reset` method is overridden
|
||||||
/// with the function pointed to by `RESET`.
|
/// with the function pointed to by `RESET`.
|
||||||
///
|
///
|
||||||
/// Rust does not yet support the three-phase reset protocol; this is
|
/// Rust does not yet support the three-phase reset protocol; this is
|
||||||
/// usually okay for leaf classes.
|
/// 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
|
/// An array providing the properties that the user can set on the
|
||||||
/// device. Not a `const` because referencing statics in constants
|
/// device. Not a `const` because referencing statics in constants
|
||||||
|
@ -41,6 +41,30 @@ pub trait DeviceImpl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # 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
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// We expect the FFI user of this function to pass a valid pointer that
|
/// We expect the FFI user of this function to pass a valid pointer that
|
||||||
|
@ -53,11 +77,11 @@ pub unsafe extern "C" fn rust_device_class_init<T: DeviceImpl>(
|
||||||
let mut dc = ::core::ptr::NonNull::new(klass.cast::<DeviceClass>()).unwrap();
|
let mut dc = ::core::ptr::NonNull::new(klass.cast::<DeviceClass>()).unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
let dc = dc.as_mut();
|
let dc = dc.as_mut();
|
||||||
if let Some(realize_fn) = <T as DeviceImpl>::REALIZE {
|
if <T as DeviceImpl>::REALIZE.is_some() {
|
||||||
dc.realize = Some(realize_fn);
|
dc.realize = Some(rust_realize_fn::<T>);
|
||||||
}
|
}
|
||||||
if let Some(reset_fn) = <T as DeviceImpl>::RESET {
|
if <T as DeviceImpl>::RESET.is_some() {
|
||||||
bindings::device_class_set_legacy_reset(dc, Some(reset_fn));
|
bindings::device_class_set_legacy_reset(dc, Some(rust_reset_fn::<T>));
|
||||||
}
|
}
|
||||||
if let Some(vmsd) = <T as DeviceImpl>::vmsd() {
|
if let Some(vmsd) = <T as DeviceImpl>::vmsd() {
|
||||||
dc.vmsd = vmsd;
|
dc.vmsd = vmsd;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue