use ax_memory_set::MappingError;
use axvm_types::GuestPhysAddr;
pub type AddrSpaceResult<T = ()> = Result<T, AddrSpaceError>;
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
pub enum AddrSpaceError {
#[error(
"guest address range [{start:#x}, +{size:#x}) is outside [{space_start:#x}, \
{space_end:#x})"
)]
OutOfRange {
start: GuestPhysAddr,
size: usize,
space_start: GuestPhysAddr,
space_end: GuestPhysAddr,
},
#[error("{subject} value {value:#x} is not aligned to {alignment:#x}")]
Unaligned {
subject: &'static str,
value: usize,
alignment: usize,
},
#[error("address range starting at {start:#x} with size {size:#x} overflows")]
AddressOverflow {
start: usize,
size: usize,
},
#[error("guest address mapping conflicts with an existing mapping")]
MappingConflict,
#[error("guest address mapping request is invalid")]
InvalidMapping,
#[error("guest address mapping state does not permit the operation")]
MappingState,
#[error("guest address {address:#x} is not mapped")]
Unmapped {
address: GuestPhysAddr,
},
#[error(
"cannot {operation} {requested} bytes at guest address {address:#x}: only {available} \
bytes are accessible"
)]
InsufficientAccess {
operation: &'static str,
address: GuestPhysAddr,
requested: usize,
available: usize,
},
}
impl From<MappingError> for AddrSpaceError {
fn from(error: MappingError) -> Self {
match error {
MappingError::InvalidParam => Self::InvalidMapping,
MappingError::AlreadyExists => Self::MappingConflict,
MappingError::BadState => Self::MappingState,
}
}
}