Skip to main content

page_table_generic/
def.rs

1pub use ax_memory_addr::{PhysAddr, VirtAddr};
2
3pub const KB: usize = 1024;
4pub const MB: usize = 1024 * KB;
5pub const GB: usize = 1024 * MB;
6
7#[derive(thiserror::Error, Clone, PartialEq, Eq)]
8pub enum PagingError {
9    #[error("Memory allocation failed")]
10    NoMemory,
11    #[error("Address alignment error: {details}")]
12    AlignmentError { details: &'static str },
13    #[error(
14        "Mapping conflict: virtual address {vaddr:#x} already mapped to physical address \
15         {existing_paddr:#x}"
16    )]
17    MappingConflict {
18        vaddr: VirtAddr,
19        existing_paddr: PhysAddr,
20    },
21    #[error("Address overflow detected: {details}")]
22    AddressOverflow { details: &'static str },
23    #[error("Invalid mapping size: {details}")]
24    InvalidSize { details: &'static str },
25    #[error("Page table hierarchy error: {details}")]
26    HierarchyError { details: &'static str },
27    #[error("Invalid address range: {details}")]
28    InvalidRange { details: &'static str },
29    #[error("Address not mapped")]
30    NotMapped,
31}
32
33impl PagingError {
34    pub fn alignment_error(msg: &'static str) -> Self {
35        Self::AlignmentError { details: msg }
36    }
37
38    pub fn mapping_conflict(vaddr: VirtAddr, existing_paddr: PhysAddr) -> Self {
39        Self::MappingConflict {
40            vaddr,
41            existing_paddr,
42        }
43    }
44
45    pub fn address_overflow(msg: &'static str) -> Self {
46        Self::AddressOverflow { details: msg }
47    }
48
49    pub fn invalid_size(msg: &'static str) -> Self {
50        Self::InvalidSize { details: msg }
51    }
52
53    pub fn hierarchy_error(msg: &'static str) -> Self {
54        Self::HierarchyError { details: msg }
55    }
56
57    pub fn invalid_range(msg: &'static str) -> Self {
58        Self::InvalidRange { details: msg }
59    }
60
61    pub fn not_mapped() -> Self {
62        Self::NotMapped
63    }
64}
65
66impl core::fmt::Debug for PagingError {
67    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68        match self {
69            Self::NoMemory => write!(f, "NoMemory"),
70            Self::AlignmentError { details } => write!(f, "AlignmentError: {details}"),
71            Self::MappingConflict {
72                vaddr,
73                existing_paddr,
74            } => {
75                write!(
76                    f,
77                    "MappingConflict: vaddr={:#x}, existing_paddr={:#x}",
78                    vaddr.as_usize(),
79                    existing_paddr.as_usize()
80                )
81            }
82            Self::AddressOverflow { details } => write!(f, "AddressOverflow: {details}"),
83            Self::InvalidSize { details } => write!(f, "InvalidSize: {details}"),
84            Self::HierarchyError { details } => write!(f, "HierarchyError: {details}"),
85            Self::InvalidRange { details } => write!(f, "InvalidRange: {details}"),
86            Self::NotMapped => write!(f, "NotMapped"),
87        }
88    }
89}