Skip to main content

vmi_core/
error.rs

1use crate::AddressContext;
2
3/// An error that can occur when working with the VMI.
4#[derive(thiserror::Error, Debug)]
5pub enum VmiError {
6    /// An error occurred in the VMI driver.
7    #[error(transparent)]
8    Driver(Box<dyn std::error::Error + Send + Sync>),
9
10    /// An OS-specific error occurred.
11    #[error(transparent)]
12    Os(Box<dyn std::error::Error + Send + Sync>),
13
14    /// An I/O error occurred.
15    #[error(transparent)]
16    Io(#[from] std::io::Error),
17
18    /// An error occurred while parsing symbols.
19    #[error(transparent)]
20    Isr(#[from] isr_macros::Error),
21
22    /// A translation error occurred.
23    #[error("translation error ({:?}, len: {})", .0[0], .0.len())]
24    Translation(PageFaults),
25
26    /// The given address has invalid width.
27    #[error("invalid address width")]
28    InvalidAddressWidth,
29
30    /// Invalid timeout.
31    #[error("invalid timeout")]
32    InvalidTimeout,
33
34    /// Operation not supported.
35    #[error("operation not supported")]
36    NotSupported,
37
38    /// Out of bounds.
39    #[error("out of bounds")]
40    OutOfBounds,
41
42    /// Translation root not present.
43    #[error("translation root not present")]
44    RootNotPresent,
45
46    /// Operation timed out.
47    #[error("operation timed out")]
48    Timeout,
49
50    /// The view was not found.
51    #[error("view not found")]
52    ViewNotFound,
53
54    /// Other error.
55    #[error("{0}")]
56    Other(&'static str),
57}
58
59/// A collection of page faults.
60pub type PageFaults = smallvec::SmallVec<[AddressContext; 1]>;
61
62impl VmiError {
63    /// Boxes a driver-specific error into [`VmiError::Driver`].
64    pub fn driver<E>(err: E) -> Self
65    where
66        E: std::error::Error + Send + Sync + 'static,
67    {
68        Self::Driver(Box::new(err))
69    }
70
71    /// Boxes an OS-specific error into [`VmiError::Os`].
72    pub fn os<E>(err: E) -> Self
73    where
74        E: std::error::Error + Send + Sync + 'static,
75    {
76        Self::Os(Box::new(err))
77    }
78
79    /// Creates a new page fault error.
80    pub fn page_fault(pf: impl Into<AddressContext>) -> Self {
81        Self::Translation(smallvec::smallvec![pf.into()])
82    }
83
84    /// Creates a new page fault error with multiple page faults.
85    pub fn page_faults(pfs: impl IntoIterator<Item = AddressContext>) -> Self {
86        Self::Translation(pfs.into_iter().collect())
87    }
88}