mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 12:23:53 -06:00
rust/qdev: Make REALIZE safe
A safe REALIZE accepts immutable reference. Since current PL011's realize() only calls a char binding function ( qemu_chr_fe_set_handlers), it is possible to convert mutable reference (&mut self) to immutable reference (&self), which only needs to convert the pointers passed to C to mutable pointers. Thus, make REALIZE accept immutable reference. Signed-off-by: Zhao Liu <zhao1.liu@intel.com> Link: https://lore.kernel.org/r/20250121140457.84631-2-zhao1.liu@intel.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
8113dbbcda
commit
0f9eb0ff2b
2 changed files with 6 additions and 6 deletions
|
@ -2,7 +2,7 @@
|
||||||
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
use core::ptr::{addr_of_mut, NonNull};
|
use core::ptr::{addr_of, addr_of_mut, NonNull};
|
||||||
use std::{
|
use std::{
|
||||||
ffi::CStr,
|
ffi::CStr,
|
||||||
os::raw::{c_int, c_uint, c_void},
|
os::raw::{c_int, c_uint, c_void},
|
||||||
|
@ -156,7 +156,7 @@ 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<fn(&mut Self)> = Some(Self::realize);
|
const REALIZE: Option<fn(&Self)> = Some(Self::realize);
|
||||||
const RESET: Option<fn(&mut Self)> = Some(Self::reset);
|
const RESET: Option<fn(&mut Self)> = Some(Self::reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,17 +439,17 @@ impl PL011State {
|
||||||
self.read_trigger = 1;
|
self.read_trigger = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn realize(&mut self) {
|
pub fn realize(&self) {
|
||||||
// SAFETY: self.char_backend has the correct size and alignment for a
|
// SAFETY: self.char_backend has the correct size and alignment for a
|
||||||
// CharBackend object, and its callbacks are of the correct types.
|
// CharBackend object, and its callbacks are of the correct types.
|
||||||
unsafe {
|
unsafe {
|
||||||
qemu_chr_fe_set_handlers(
|
qemu_chr_fe_set_handlers(
|
||||||
addr_of_mut!(self.char_backend),
|
addr_of!(self.char_backend) as *mut CharBackend,
|
||||||
Some(pl011_can_receive),
|
Some(pl011_can_receive),
|
||||||
Some(pl011_receive),
|
Some(pl011_receive),
|
||||||
Some(pl011_event),
|
Some(pl011_event),
|
||||||
None,
|
None,
|
||||||
addr_of_mut!(*self).cast::<c_void>(),
|
addr_of!(*self).cast::<c_void>() as *mut c_void,
|
||||||
core::ptr::null_mut(),
|
core::ptr::null_mut(),
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
|
@ -23,7 +23,7 @@ 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<fn(&mut Self)> = None;
|
const REALIZE: Option<fn(&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`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue