Skip to main content

rvm_types/
error.rs

1//! Error types for the RVM microhypervisor.
2//!
3//! All failure modes across the kernel are represented by [`RvmError`].
4//! Each variant maps to a specific class of failure documented in
5//! ADR-132 (DC-14) and the partition/witness/proof subsystems.
6
7/// The unified error type for RVM operations.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum RvmError {
10    // --- Partition errors ---
11    /// The requested partition was not found.
12    PartitionNotFound,
13    /// The partition is in the wrong lifecycle state for this operation.
14    InvalidPartitionState,
15    /// Maximum partition count has been reached (DC-12).
16    PartitionLimitExceeded,
17    /// The partition split preconditions were not met.
18    SplitPreconditionFailed,
19    /// The partition merge preconditions were not met (DC-11).
20    MergePreconditionFailed,
21    /// Partition migration timed out (DC-7).
22    MigrationTimeout,
23
24    // --- vCPU errors ---
25    /// The requested vCPU was not found.
26    VcpuNotFound,
27    /// The partition has no available vCPU slots.
28    VcpuLimitReached,
29
30    // --- Capability errors ---
31    /// A capability check failed -- insufficient rights.
32    InsufficientCapability,
33    /// The capability token is stale (epoch mismatch).
34    StaleCapability,
35    /// The capability type does not match the resource.
36    CapabilityTypeMismatch,
37    /// Maximum delegation depth exceeded.
38    DelegationDepthExceeded,
39    /// The capability has already been consumed (`GRANT_ONCE`).
40    CapabilityConsumed,
41
42    // --- Witness errors ---
43    /// A witness verification failed.
44    WitnessVerificationFailed,
45    /// The witness hash chain is broken (tamper detected).
46    WitnessChainBroken,
47    /// The witness log is full and drain is not keeping up.
48    WitnessLogFull,
49
50    // --- Proof errors ---
51    /// A proof validation failed.
52    ProofInvalid,
53    /// The proof tier is insufficient for this operation.
54    ProofTierInsufficient,
55    /// Proof verification exceeded its time budget.
56    ProofBudgetExceeded,
57
58    // --- Coherence errors ---
59    /// The coherence score is below the required threshold.
60    CoherenceBelowThreshold,
61    /// The mincut budget was exceeded (DC-2 fallback triggered).
62    MinCutBudgetExceeded,
63
64    // --- Memory errors ---
65    /// The requested memory region overlaps with an existing mapping.
66    MemoryOverlap,
67    /// An address is not properly aligned.
68    AlignmentError,
69    /// The memory tier transition is invalid.
70    InvalidTierTransition,
71    /// No physical memory is available for allocation.
72    OutOfMemory,
73
74    // --- Device errors ---
75    /// The requested device lease was not found.
76    DeviceLeaseNotFound,
77    /// The device lease has expired.
78    DeviceLeaseExpired,
79    /// A conflicting device lease exists.
80    DeviceLeaseConflict,
81
82    // --- Recovery errors ---
83    /// The recovery checkpoint was not found.
84    CheckpointNotFound,
85    /// The recovery checkpoint is corrupted.
86    CheckpointCorrupted,
87    /// Failure escalated beyond recovery capability (DC-14).
88    FailureEscalated,
89
90    // --- General errors ---
91    /// The operation would exceed a configured resource limit.
92    ResourceLimitExceeded,
93    /// The operation is not supported in the current configuration.
94    Unsupported,
95    /// An internal invariant was violated (should not occur).
96    InternalError,
97}
98
99impl core::fmt::Display for RvmError {
100    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101        match self {
102            Self::PartitionNotFound => write!(f, "partition not found"),
103            Self::InvalidPartitionState => write!(f, "invalid partition state for operation"),
104            Self::PartitionLimitExceeded => write!(f, "maximum partition count reached"),
105            Self::SplitPreconditionFailed => write!(f, "split preconditions not met"),
106            Self::MergePreconditionFailed => write!(f, "merge preconditions not met"),
107            Self::MigrationTimeout => write!(f, "partition migration timed out"),
108            Self::VcpuNotFound => write!(f, "vCPU not found"),
109            Self::VcpuLimitReached => write!(f, "vCPU limit reached"),
110            Self::InsufficientCapability => write!(f, "insufficient capability rights"),
111            Self::StaleCapability => write!(f, "stale capability (epoch mismatch)"),
112            Self::CapabilityTypeMismatch => write!(f, "capability type mismatch"),
113            Self::DelegationDepthExceeded => write!(f, "delegation depth exceeded"),
114            Self::CapabilityConsumed => write!(f, "capability already consumed"),
115            Self::WitnessVerificationFailed => write!(f, "witness verification failed"),
116            Self::WitnessChainBroken => write!(f, "witness chain broken"),
117            Self::WitnessLogFull => write!(f, "witness log full"),
118            Self::ProofInvalid => write!(f, "proof invalid"),
119            Self::ProofTierInsufficient => write!(f, "proof tier insufficient"),
120            Self::ProofBudgetExceeded => write!(f, "proof budget exceeded"),
121            Self::CoherenceBelowThreshold => write!(f, "coherence below threshold"),
122            Self::MinCutBudgetExceeded => write!(f, "mincut budget exceeded"),
123            Self::MemoryOverlap => write!(f, "memory region overlap"),
124            Self::AlignmentError => write!(f, "address alignment error"),
125            Self::InvalidTierTransition => write!(f, "invalid memory tier transition"),
126            Self::OutOfMemory => write!(f, "out of memory"),
127            Self::DeviceLeaseNotFound => write!(f, "device lease not found"),
128            Self::DeviceLeaseExpired => write!(f, "device lease expired"),
129            Self::DeviceLeaseConflict => write!(f, "conflicting device lease"),
130            Self::CheckpointNotFound => write!(f, "checkpoint not found"),
131            Self::CheckpointCorrupted => write!(f, "checkpoint corrupted"),
132            Self::FailureEscalated => write!(f, "failure escalated beyond recovery"),
133            Self::ResourceLimitExceeded => write!(f, "resource limit exceeded"),
134            Self::Unsupported => write!(f, "operation unsupported"),
135            Self::InternalError => write!(f, "internal error"),
136        }
137    }
138}
139
140/// Shorthand result type for RVM operations.
141pub type RvmResult<T> = core::result::Result<T, RvmError>;