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    #[error("Huge split deposit is stale for virtual address {vaddr:#x}")]
32    StaleHugeSplit { vaddr: VirtAddr },
33    #[error("Page-table map deposit is stale for virtual address {vaddr:#x}")]
34    StaleMapDeposit { vaddr: VirtAddr },
35}
36
37impl PagingError {
38    pub fn alignment_error(msg: &'static str) -> Self {
39        Self::AlignmentError { details: msg }
40    }
41
42    pub fn mapping_conflict(vaddr: VirtAddr, existing_paddr: PhysAddr) -> Self {
43        Self::MappingConflict {
44            vaddr,
45            existing_paddr,
46        }
47    }
48
49    pub fn address_overflow(msg: &'static str) -> Self {
50        Self::AddressOverflow { details: msg }
51    }
52
53    pub fn invalid_size(msg: &'static str) -> Self {
54        Self::InvalidSize { details: msg }
55    }
56
57    pub fn hierarchy_error(msg: &'static str) -> Self {
58        Self::HierarchyError { details: msg }
59    }
60
61    pub fn invalid_range(msg: &'static str) -> Self {
62        Self::InvalidRange { details: msg }
63    }
64
65    pub fn not_mapped() -> Self {
66        Self::NotMapped
67    }
68
69    pub fn stale_huge_split(vaddr: VirtAddr) -> Self {
70        Self::StaleHugeSplit { vaddr }
71    }
72
73    pub fn stale_map_deposit(vaddr: VirtAddr) -> Self {
74        Self::StaleMapDeposit { vaddr }
75    }
76}
77
78impl core::fmt::Debug for PagingError {
79    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
80        match self {
81            Self::NoMemory => write!(f, "NoMemory"),
82            Self::AlignmentError { details } => write!(f, "AlignmentError: {details}"),
83            Self::MappingConflict {
84                vaddr,
85                existing_paddr,
86            } => {
87                write!(
88                    f,
89                    "MappingConflict: vaddr={:#x}, existing_paddr={:#x}",
90                    vaddr.as_usize(),
91                    existing_paddr.as_usize()
92                )
93            }
94            Self::AddressOverflow { details } => write!(f, "AddressOverflow: {details}"),
95            Self::InvalidSize { details } => write!(f, "InvalidSize: {details}"),
96            Self::HierarchyError { details } => write!(f, "HierarchyError: {details}"),
97            Self::InvalidRange { details } => write!(f, "InvalidRange: {details}"),
98            Self::NotMapped => write!(f, "NotMapped"),
99            Self::StaleHugeSplit { vaddr } => {
100                write!(f, "StaleHugeSplit: vaddr={:#x}", vaddr.as_usize())
101            }
102            Self::StaleMapDeposit { vaddr } => {
103                write!(f, "StaleMapDeposit: vaddr={:#x}", vaddr.as_usize())
104            }
105        }
106    }
107}