rust: pull error_fatal out of SysbusDeviceMethods::sysbus_realize

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>
This commit is contained in:
Paolo Bonzini 2025-10-07 17:13:43 +02:00
parent 113a7f5bf3
commit 8abea41ecd
4 changed files with 39 additions and 11 deletions

View file

@ -17,7 +17,7 @@ use migration::{
};
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 unsafe extern "C" fn pl011_create(
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);

View file

@ -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 @@ where
}
}
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);
})
}
}
}

View file

@ -38,7 +38,8 @@ use std::{
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 @@ impl Error {
}
}
/// 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;

View file

@ -6,4 +6,4 @@ pub mod log;
pub mod module;
pub mod timer;
pub use error::{Error, Result};
pub use error::{Error, Result, ResultExt};