From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Zhao Liu <zhao1.liu@intel.com>
Subject: [PULL 5/8] rust: pull error_fatal out of SysbusDeviceMethods::sysbus_realize
Date: Tue, 4 Nov 2025 17:30:59 +0100 [thread overview]
Message-ID: <20251104163102.738889-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20251104163102.738889-1-pbonzini@redhat.com>
Return a Result<()> from the method, and "unwrap" it into error_fatal
in the caller.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 4 ++--
rust/hw/core/src/sysbus.rs | 13 ++++++-------
rust/util/src/error.rs | 31 ++++++++++++++++++++++++++++++-
rust/util/src/lib.rs | 2 +-
4 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index 5e9b13fdf92..04155dabe1a 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -17,7 +17,7 @@
};
use qom::{prelude::*, ObjectImpl, Owned, ParentField, ParentInit};
use system::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder};
-use util::{log::Log, log_mask_ln};
+use util::{log::Log, log_mask_ln, ResultExt};
use crate::registers::{self, Interrupt, RegisterOffset};
@@ -697,7 +697,7 @@ pub fn post_load(&self, _version_id: u8) -> Result<(), migration::InvalidError>
let chr = unsafe { Owned::<Chardev>::from(&*chr) };
dev.prop_set_chr("chardev", &chr);
}
- dev.sysbus_realize();
+ dev.sysbus_realize().unwrap_fatal();
dev.mmio_map(0, addr);
dev.connect_irq(0, &irq);
diff --git a/rust/hw/core/src/sysbus.rs b/rust/hw/core/src/sysbus.rs
index 282315fce99..68165e89295 100644
--- a/rust/hw/core/src/sysbus.rs
+++ b/rust/hw/core/src/sysbus.rs
@@ -4,12 +4,13 @@
//! Bindings to access `sysbus` functionality from Rust.
-use std::{ffi::CStr, ptr::addr_of_mut};
+use std::ffi::CStr;
pub use bindings::SysBusDeviceClass;
use common::Opaque;
use qom::{prelude::*, Owned};
use system::MemoryRegion;
+use util::{Error, Result};
use crate::{
bindings,
@@ -107,14 +108,12 @@ fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) {
}
}
- fn sysbus_realize(&self) {
- // TODO: return an Error
+ fn sysbus_realize(&self) -> Result<()> {
assert!(bql::is_locked());
unsafe {
- bindings::sysbus_realize(
- self.upcast().as_mut_ptr(),
- addr_of_mut!(util::bindings::error_fatal),
- );
+ Error::with_errp(|errp| {
+ bindings::sysbus_realize(self.upcast().as_mut_ptr(), errp);
+ })
}
}
}
diff --git a/rust/util/src/error.rs b/rust/util/src/error.rs
index 346577e2e53..4edceff42f3 100644
--- a/rust/util/src/error.rs
+++ b/rust/util/src/error.rs
@@ -38,7 +38,8 @@
ffi::{c_char, c_int, c_void, CStr},
fmt::{self, Display},
ops::Deref,
- panic, ptr,
+ panic,
+ ptr::{self, addr_of_mut},
};
use foreign::{prelude::*, OwnedPointer};
@@ -231,6 +232,34 @@ pub unsafe fn with_errp<T, F: FnOnce(&mut *mut bindings::Error) -> T>(f: F) -> R
}
}
+/// Extension trait for `std::result::Result`, providing extra
+/// methods when the error type can be converted into a QEMU
+/// Error.
+pub trait ResultExt {
+ /// The success type `T` in `Result<T, E>`.
+ type OkType;
+
+ /// Report a fatal error and exit QEMU, or return the success value.
+ /// Note that, unlike [`unwrap()`](std::result::Result::unwrap), this
+ /// is not an abort and can be used for user errors.
+ fn unwrap_fatal(self) -> Self::OkType;
+}
+
+impl<T, E> ResultExt for std::result::Result<T, E>
+where
+ Error: From<E>,
+{
+ type OkType = T;
+
+ fn unwrap_fatal(self) -> T {
+ // SAFETY: errp is valid
+ self.map_err(|err| unsafe {
+ Error::from(err).propagate(addr_of_mut!(bindings::error_fatal))
+ })
+ .unwrap()
+ }
+}
+
impl FreeForeign for Error {
type Foreign = bindings::Error;
diff --git a/rust/util/src/lib.rs b/rust/util/src/lib.rs
index 16c89b95174..d14aa14ca77 100644
--- a/rust/util/src/lib.rs
+++ b/rust/util/src/lib.rs
@@ -6,4 +6,4 @@
pub mod module;
pub mod timer;
-pub use error::{Error, Result};
+pub use error::{Error, Result, ResultExt};
--
2.51.1
next prev parent reply other threads:[~2025-11-04 16:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 16:30 [PULL 0/8] Cleanup patches for QEMU 10.2 soft freeze Paolo Bonzini
2025-11-04 16:30 ` [PULL 1/8] scripts/checkpatch.pl: remove bogus patch prefix warning Paolo Bonzini
2025-11-04 16:30 ` [PULL 2/8] rust/util: add ensure macro Paolo Bonzini
2025-11-04 16:30 ` [PULL 3/8] rust/util: use anyhow's native chaining capabilities Paolo Bonzini
2025-11-04 16:30 ` [PULL 4/8] rust/util: replace Error::err_or_unit/err_or_else with Error::with_errp Paolo Bonzini
2025-11-04 16:30 ` Paolo Bonzini [this message]
2025-11-04 16:31 ` [PULL 6/8] rust: do not add qemuutil to Rust crates Paolo Bonzini
2025-11-04 16:31 ` [PULL 7/8] rust: migration: allow nested offset_of Paolo Bonzini
2025-11-04 16:31 ` [PULL 8/8] rust: add back to Ubuntu 22.04 jobs Paolo Bonzini
2025-11-05 13:46 ` [PULL 0/8] Cleanup patches for QEMU 10.2 soft freeze Richard Henderson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251104163102.738889-6-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).