From 89b429c76891ededb86bb9f86f1816ba23a5f7c2 Mon Sep 17 00:00:00 2001 From: SamrutGadde Date: Fri, 3 May 2024 15:23:09 -0500 Subject: [PATCH] devices: Use thiserror for errors Updated error enums in device package to use thiserror crate Signed-off-by: SamrutGadde --- devices/src/interrupt_controller.rs | 13 ++++++++++++- devices/src/legacy/gpio_pl061.rs | 24 +++++++----------------- devices/src/legacy/rtc_pl031.rs | 14 ++++---------- devices/src/legacy/uart_pl011.rs | 21 +++++++-------------- 4 files changed, 30 insertions(+), 42 deletions(-) diff --git a/devices/src/interrupt_controller.rs b/devices/src/interrupt_controller.rs index c92eadb1b..b3ab8926a 100644 --- a/devices/src/interrupt_controller.rs +++ b/devices/src/interrupt_controller.rs @@ -4,31 +4,42 @@ use std::io; use std::result; +use thiserror::Error; use vmm_sys_util::eventfd::EventFd; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Invalid trigger mode. + #[error("Invalid trigger mode")] InvalidTriggerMode, /// Invalid delivery mode. + #[error("Invalid delivery mode")] InvalidDeliveryMode, /// Failed creating the interrupt source group. + #[error("Failed creating the interrupt source group: {0}")] CreateInterruptSourceGroup(io::Error), /// Failed triggering the interrupt. + #[error("Failed triggering the interrupt: {0}")] TriggerInterrupt(io::Error), /// Failed masking the interrupt. + #[error("Failed masking the interrupt: {0}")] MaskInterrupt(io::Error), /// Failed unmasking the interrupt. + #[error("Failed unmasking the interrupt: {0}")] UnmaskInterrupt(io::Error), /// Failed updating the interrupt. + #[error("Failed updating the interrupt: {0}")] UpdateInterrupt(io::Error), /// Failed enabling the interrupt. + #[error("Failed enabling the interrupt: {0}")] EnableInterrupt(io::Error), #[cfg(target_arch = "aarch64")] /// Failed creating GIC device. + #[error("Failed creating GIC device: {0}")] CreateGic(hypervisor::HypervisorVmError), #[cfg(target_arch = "aarch64")] /// Failed restoring GIC device. + #[error("Failed restoring GIC device: {0}")] RestoreGic(hypervisor::arch::aarch64::gic::Error), } diff --git a/devices/src/legacy/gpio_pl061.rs b/devices/src/legacy/gpio_pl061.rs index 9aa34db1e..f5e0ea1bc 100644 --- a/devices/src/legacy/gpio_pl061.rs +++ b/devices/src/legacy/gpio_pl061.rs @@ -9,9 +9,10 @@ use crate::{read_le_u32, write_le_u32}; use serde::{Deserialize, Serialize}; +use std::io; use std::result; use std::sync::{Arc, Barrier}; -use std::{fmt, io}; +use thiserror::Error; use vm_device::interrupt::InterruptSourceGroup; use vm_device::BusDevice; use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; @@ -37,29 +38,18 @@ const GPIO_ID_HIGH: u64 = 0x1000; const N_GPIOS: u32 = 8; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("Bad Write Offset: {0}")] BadWriteOffset(u64), + #[error("GPIO interrupt disabled by guest driver.")] GpioInterruptDisabled, + #[error("Could not trigger GPIO interrupt: {0}.")] GpioInterruptFailure(io::Error), + #[error("Invalid GPIO Input key triggered: {0}.")] GpioTriggerKeyFailure(u32), } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::BadWriteOffset(offset) => write!(f, "Bad Write Offset: {offset}"), - Error::GpioInterruptDisabled => write!(f, "GPIO interrupt disabled by guest driver.",), - Error::GpioInterruptFailure(ref e) => { - write!(f, "Could not trigger GPIO interrupt: {e}.") - } - Error::GpioTriggerKeyFailure(key) => { - write!(f, "Invalid GPIO Input key triggered: {key}.") - } - } - } -} - type Result = result::Result; /// A GPIO device following the PL061 specification. diff --git a/devices/src/legacy/rtc_pl031.rs b/devices/src/legacy/rtc_pl031.rs index 0b2fc98a8..238c17f27 100644 --- a/devices/src/legacy/rtc_pl031.rs +++ b/devices/src/legacy/rtc_pl031.rs @@ -13,6 +13,7 @@ use std::fmt; use std::sync::{Arc, Barrier}; use std::time::Instant; use std::{io, result}; +use thiserror::Error; use vm_device::interrupt::InterruptSourceGroup; use vm_device::BusDevice; @@ -39,21 +40,14 @@ const AMBA_ID_HIGH: u64 = 0x1000; /// Constant to convert seconds to nanoseconds. pub const NANOS_PER_SECOND: u64 = 1_000_000_000; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("Bad Write Offset: {0}")] BadWriteOffset(u64), + #[error("Failed to trigger interrupt: {0}")] InterruptFailure(io::Error), } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::BadWriteOffset(offset) => write!(f, "Bad Write Offset: {offset}"), - Error::InterruptFailure(e) => write!(f, "Failed to trigger interrupt: {e}"), - } - } -} - type Result = result::Result; /// Wrapper over `libc::clockid_t` to specify Linux Kernel clock source. diff --git a/devices/src/legacy/uart_pl011.rs b/devices/src/legacy/uart_pl011.rs index 889071bc5..7f26cfd4f 100644 --- a/devices/src/legacy/uart_pl011.rs +++ b/devices/src/legacy/uart_pl011.rs @@ -9,10 +9,10 @@ use crate::{read_le_u32, write_le_u32}; use serde::{Deserialize, Serialize}; use std::collections::VecDeque; -use std::fmt; use std::sync::{Arc, Barrier}; use std::time::Instant; use std::{io, result}; +use thiserror::Error; use vm_device::interrupt::InterruptSourceGroup; use vm_device::BusDevice; use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; @@ -45,27 +45,20 @@ const PL011_ID: [u8; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]; const AMBA_ID_LOW: u64 = 0x3f8; const AMBA_ID_HIGH: u64 = 0x401; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("pl011_write: Bad Write Offset: {0}")] BadWriteOffset(u64), + #[error("pl011: DMA not implemented.")] DmaNotImplemented, + #[error("Failed to trigger interrupt: {0}")] InterruptFailure(io::Error), + #[error("Failed to write: {0}")] WriteAllFailure(io::Error), + #[error("Failed to flush: {0}")] FlushFailure(io::Error), } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::BadWriteOffset(offset) => write!(f, "pl011_write: Bad Write Offset: {offset}"), - Error::DmaNotImplemented => write!(f, "pl011: DMA not implemented."), - Error::InterruptFailure(e) => write!(f, "Failed to trigger interrupt: {e}"), - Error::WriteAllFailure(e) => write!(f, "Failed to write: {e}"), - Error::FlushFailure(e) => write!(f, "Failed to flush: {e}"), - } - } -} - type Result = result::Result; /// A PL011 device following the PL011 specification.