mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-01 14:53:54 -06:00
rust: qdev: switch from legacy reset to Resettable
Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
68da5402df
commit
5472a38cb9
4 changed files with 99 additions and 28 deletions
|
@ -4073,6 +4073,7 @@ if have_rust
|
||||||
'MigrationPriority',
|
'MigrationPriority',
|
||||||
'QEMUChrEvent',
|
'QEMUChrEvent',
|
||||||
'QEMUClockType',
|
'QEMUClockType',
|
||||||
|
'ResetType',
|
||||||
'device_endian',
|
'device_endian',
|
||||||
'module_init_type',
|
'module_init_type',
|
||||||
]
|
]
|
||||||
|
|
|
@ -18,7 +18,7 @@ use qemu_api::{
|
||||||
c_str, impl_vmstate_forward,
|
c_str, impl_vmstate_forward,
|
||||||
irq::InterruptSource,
|
irq::InterruptSource,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property},
|
qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesImpl},
|
||||||
qom::{ClassInitImpl, ObjectImpl, Owned, ParentField},
|
qom::{ClassInitImpl, ObjectImpl, Owned, ParentField},
|
||||||
sysbus::{SysBusDevice, SysBusDeviceClass},
|
sysbus::{SysBusDevice, SysBusDeviceClass},
|
||||||
vmstate::VMStateDescription,
|
vmstate::VMStateDescription,
|
||||||
|
@ -171,7 +171,10 @@ impl DeviceImpl for PL011State {
|
||||||
Some(&device_class::VMSTATE_PL011)
|
Some(&device_class::VMSTATE_PL011)
|
||||||
}
|
}
|
||||||
const REALIZE: Option<fn(&Self)> = Some(Self::realize);
|
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 {
|
impl PL011Registers {
|
||||||
|
@ -622,7 +625,7 @@ impl PL011State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&self) {
|
pub fn reset_hold(&self, _type: ResetType) {
|
||||||
self.regs.borrow_mut().reset();
|
self.regs.borrow_mut().reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -737,3 +740,4 @@ impl ObjectImpl for PL011Luminary {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceImpl for PL011Luminary {}
|
impl DeviceImpl for PL011Luminary {}
|
||||||
|
impl ResettablePhasesImpl for PL011Luminary {}
|
||||||
|
|
|
@ -10,10 +10,10 @@ use std::{
|
||||||
ptr::NonNull,
|
ptr::NonNull,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use bindings::{Clock, ClockEvent, DeviceClass, DeviceState, Property};
|
pub use bindings::{Clock, ClockEvent, DeviceClass, DeviceState, Property, ResetType};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bindings::{self, Error},
|
bindings::{self, Error, ResettableClass},
|
||||||
callbacks::FnCall,
|
callbacks::FnCall,
|
||||||
cell::bql_locked,
|
cell::bql_locked,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
@ -21,8 +21,70 @@ use crate::{
|
||||||
vmstate::VMStateDescription,
|
vmstate::VMStateDescription,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Trait providing the contents of the `ResettablePhases` struct,
|
||||||
|
/// which is part of the QOM `Resettable` interface.
|
||||||
|
pub trait ResettablePhasesImpl {
|
||||||
|
/// If not None, this is called when the object enters reset. It
|
||||||
|
/// can reset local state of the object, but it must not do anything that
|
||||||
|
/// has a side-effect on other objects, such as raising or lowering an
|
||||||
|
/// [`InterruptSource`](crate::irq::InterruptSource), or reading or
|
||||||
|
/// writing guest memory. It takes the reset's type as argument.
|
||||||
|
const ENTER: Option<fn(&Self, ResetType)> = None;
|
||||||
|
|
||||||
|
/// If not None, this is called when the object for entry into reset, once
|
||||||
|
/// every object in the system which is being reset has had its
|
||||||
|
/// `ResettablePhasesImpl::ENTER` method called. At this point devices
|
||||||
|
/// can do actions that affect other objects.
|
||||||
|
///
|
||||||
|
/// If in doubt, implement this method.
|
||||||
|
const HOLD: Option<fn(&Self, ResetType)> = None;
|
||||||
|
|
||||||
|
/// If not None, this phase is called when the object leaves the reset
|
||||||
|
/// state. Actions affecting other objects are permitted.
|
||||||
|
const EXIT: Option<fn(&Self, ResetType)> = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # 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_resettable_enter_fn<T: ResettablePhasesImpl>(
|
||||||
|
obj: *mut Object,
|
||||||
|
typ: ResetType,
|
||||||
|
) {
|
||||||
|
let state = NonNull::new(obj).unwrap().cast::<T>();
|
||||||
|
T::ENTER.unwrap()(unsafe { state.as_ref() }, typ);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # 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_resettable_hold_fn<T: ResettablePhasesImpl>(
|
||||||
|
obj: *mut Object,
|
||||||
|
typ: ResetType,
|
||||||
|
) {
|
||||||
|
let state = NonNull::new(obj).unwrap().cast::<T>();
|
||||||
|
T::HOLD.unwrap()(unsafe { state.as_ref() }, typ);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # 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_resettable_exit_fn<T: ResettablePhasesImpl>(
|
||||||
|
obj: *mut Object,
|
||||||
|
typ: ResetType,
|
||||||
|
) {
|
||||||
|
let state = NonNull::new(obj).unwrap().cast::<T>();
|
||||||
|
T::EXIT.unwrap()(unsafe { state.as_ref() }, typ);
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait providing the contents of [`DeviceClass`].
|
/// Trait providing the contents of [`DeviceClass`].
|
||||||
pub trait DeviceImpl: ObjectImpl {
|
pub trait DeviceImpl: ObjectImpl + ResettablePhasesImpl {
|
||||||
/// _Realization_ is the second stage of device creation. It contains
|
/// _Realization_ is the second stage of device creation. It contains
|
||||||
/// all operations that depend on device properties and can fail (note:
|
/// all operations that depend on device properties and can fail (note:
|
||||||
/// this is not yet supported for Rust devices).
|
/// this is not yet supported for Rust devices).
|
||||||
|
@ -31,13 +93,6 @@ pub trait DeviceImpl: ObjectImpl {
|
||||||
/// with the function pointed to by `REALIZE`.
|
/// with the function pointed to by `REALIZE`.
|
||||||
const REALIZE: Option<fn(&Self)> = None;
|
const REALIZE: Option<fn(&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<fn(&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
|
||||||
/// is unstable until Rust 1.83.0.
|
/// is unstable until Rust 1.83.0.
|
||||||
|
@ -65,29 +120,36 @@ unsafe extern "C" fn rust_realize_fn<T: DeviceImpl>(dev: *mut DeviceState, _errp
|
||||||
T::REALIZE.unwrap()(unsafe { state.as_ref() });
|
T::REALIZE.unwrap()(unsafe { state.as_ref() });
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
unsafe impl InterfaceType for ResettableClass {
|
||||||
///
|
const TYPE_NAME: &'static CStr =
|
||||||
/// We expect the FFI user of this function to pass a valid pointer that
|
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_RESETTABLE_INTERFACE) };
|
||||||
/// 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) {
|
impl<T> ClassInitImpl<ResettableClass> for T
|
||||||
let mut state = NonNull::new(dev).unwrap().cast::<T>();
|
where
|
||||||
T::RESET.unwrap()(unsafe { state.as_mut() });
|
T: ResettablePhasesImpl,
|
||||||
|
{
|
||||||
|
fn class_init(rc: &mut ResettableClass) {
|
||||||
|
if <T as ResettablePhasesImpl>::ENTER.is_some() {
|
||||||
|
rc.phases.enter = Some(rust_resettable_enter_fn::<T>);
|
||||||
|
}
|
||||||
|
if <T as ResettablePhasesImpl>::HOLD.is_some() {
|
||||||
|
rc.phases.hold = Some(rust_resettable_hold_fn::<T>);
|
||||||
|
}
|
||||||
|
if <T as ResettablePhasesImpl>::EXIT.is_some() {
|
||||||
|
rc.phases.exit = Some(rust_resettable_exit_fn::<T>);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ClassInitImpl<DeviceClass> for T
|
impl<T> ClassInitImpl<DeviceClass> for T
|
||||||
where
|
where
|
||||||
T: ClassInitImpl<ObjectClass> + DeviceImpl,
|
T: ClassInitImpl<ObjectClass> + ClassInitImpl<ResettableClass> + DeviceImpl,
|
||||||
{
|
{
|
||||||
fn class_init(dc: &mut DeviceClass) {
|
fn class_init(dc: &mut DeviceClass) {
|
||||||
if <T as DeviceImpl>::REALIZE.is_some() {
|
if <T as DeviceImpl>::REALIZE.is_some() {
|
||||||
dc.realize = Some(rust_realize_fn::<T>);
|
dc.realize = Some(rust_realize_fn::<T>);
|
||||||
}
|
}
|
||||||
if <T as DeviceImpl>::RESET.is_some() {
|
|
||||||
unsafe {
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -98,6 +160,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResettableClass::interface_init::<T, DeviceState>(dc);
|
||||||
<T as ClassInitImpl<ObjectClass>>::class_init(&mut dc.parent_class);
|
<T as ClassInitImpl<ObjectClass>>::class_init(&mut dc.parent_class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ use qemu_api::{
|
||||||
cell::{self, BqlCell},
|
cell::{self, BqlCell},
|
||||||
declare_properties, define_property,
|
declare_properties, define_property,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
qdev::{DeviceClass, DeviceImpl, DeviceState, Property},
|
qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
|
||||||
qom::{ClassInitImpl, ObjectImpl, ParentField},
|
qom::{ClassInitImpl, ObjectImpl, ParentField},
|
||||||
vmstate::VMStateDescription,
|
vmstate::VMStateDescription,
|
||||||
zeroable::Zeroable,
|
zeroable::Zeroable,
|
||||||
|
@ -61,6 +61,8 @@ impl ObjectImpl for DummyState {
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ResettablePhasesImpl for DummyState {}
|
||||||
|
|
||||||
impl DeviceImpl for DummyState {
|
impl DeviceImpl for DummyState {
|
||||||
fn properties() -> &'static [Property] {
|
fn properties() -> &'static [Property] {
|
||||||
&DUMMY_PROPERTIES
|
&DUMMY_PROPERTIES
|
||||||
|
@ -101,6 +103,7 @@ impl ObjectImpl for DummyChildState {
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ResettablePhasesImpl for DummyChildState {}
|
||||||
impl DeviceImpl for DummyChildState {}
|
impl DeviceImpl for DummyChildState {}
|
||||||
|
|
||||||
impl ClassInitImpl<DummyClass> for DummyChildState {
|
impl ClassInitImpl<DummyClass> for DummyChildState {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue