[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 21/48] rust/qdev: Make REALIZE safe
From: |
Paolo Bonzini |
Subject: |
[PULL 21/48] rust/qdev: Make REALIZE safe |
Date: |
Fri, 24 Jan 2025 10:44:15 +0100 |
From: Zhao Liu <zhao1.liu@intel.com>
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: 20250121140457.84631-2-zhao1.liu@intel.com">https://lore.kernel.org/r/20250121140457.84631-2-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 10 +++++-----
rust/qemu-api/src/qdev.rs | 2 +-
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index 65a1234b9ff..a0e0fbdd9dd 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -2,7 +2,7 @@
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
// 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::{
ffi::CStr,
os::raw::{c_int, c_uint, c_void},
@@ -156,7 +156,7 @@ fn properties() -> &'static [Property] {
fn vmsd() -> Option<&'static VMStateDescription> {
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);
}
@@ -439,17 +439,17 @@ fn set_read_trigger(&mut self) {
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
// CharBackend object, and its callbacks are of the correct types.
unsafe {
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_receive),
Some(pl011_event),
None,
- addr_of_mut!(*self).cast::<c_void>(),
+ addr_of!(*self).cast::<c_void>() as *mut c_void,
core::ptr::null_mut(),
true,
);
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 686054e737a..a5121e31a37 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -23,7 +23,7 @@ pub trait DeviceImpl {
///
/// If not `None`, the parent class's `realize` method is overridden
/// 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
/// with the function pointed to by `RESET`.
--
2.48.1
- [PULL 29/48] rust: vmstate: implement VMState for scalar types, (continued)
- [PULL 29/48] rust: vmstate: implement VMState for scalar types, Paolo Bonzini, 2025/01/24
- [PULL 42/48] rust: pl011: wrap registers with BqlRefCell, Paolo Bonzini, 2025/01/24
- [PULL 43/48] rust: pl011: remove duplicate definitions, Paolo Bonzini, 2025/01/24
- [PULL 24/48] memattrs: Check the size of MemTxAttrs, Paolo Bonzini, 2025/01/24
- [PULL 34/48] rust: vmstate: make order of parameters consistent in vmstate_clock, Paolo Bonzini, 2025/01/24
- [PULL 19/48] docs: Add GNR, SRF and CWF CPU models, Paolo Bonzini, 2025/01/24
- [PULL 30/48] rust: vmstate: add public utility macros to implement VMState, Paolo Bonzini, 2025/01/24
- [PULL 36/48] rust: pl011: remove unnecessary "extern crate", Paolo Bonzini, 2025/01/24
- [PULL 37/48] rust: pl011: hide unnecessarily "pub" items from outside pl011::device, Paolo Bonzini, 2025/01/24
- [PULL 48/48] rust: qemu-api: add sub-subclass to the integration tests, Paolo Bonzini, 2025/01/24
- [PULL 21/48] rust/qdev: Make REALIZE safe,
Paolo Bonzini <=
- [PULL 45/48] rust: pl011: drop use of ControlFlow, Paolo Bonzini, 2025/01/24
- [PULL 40/48] rust: pl011: pull interrupt updates out of read/write ops, Paolo Bonzini, 2025/01/24
- [PULL 32/48] rust: pl011: switch vmstate to new-style macros, Paolo Bonzini, 2025/01/24
- [PULL 38/48] rust: pl011: extract conversion to RegisterOffset, Paolo Bonzini, 2025/01/24
- [PULL 47/48] rust/zeroable: Implement Zeroable with const_zero macro, Paolo Bonzini, 2025/01/24
- [PULL 27/48] rust: vmstate: add varray support to vmstate_of!, Paolo Bonzini, 2025/01/24
- [PULL 28/48] rust: vmstate: implement Zeroable for VMStateField, Paolo Bonzini, 2025/01/24
- [PULL 31/48] rust: qemu_api: add vmstate_struct, Paolo Bonzini, 2025/01/24
- [PULL 39/48] rust: pl011: extract CharBackend receive logic into a separate function, Paolo Bonzini, 2025/01/24
- [PULL 44/48] rust: pl011: pull device-specific code out of MemoryRegionOps callbacks, Paolo Bonzini, 2025/01/24