use super::{umem, Address};
bitflags! {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
pub struct PageType: u8 {
const NONE = 0b0000_0000;
const UNKNOWN = 0b0000_0001;
const PAGE_TABLE = 0b0000_0010;
const WRITEABLE = 0b0000_0100;
const READ_ONLY = 0b0000_1000;
const NOEXEC = 0b0001_0000;
}
}
impl PageType {
pub fn write(mut self, flag: bool) -> Self {
self &= !(PageType::WRITEABLE | PageType::READ_ONLY | PageType::UNKNOWN);
if flag {
self | PageType::WRITEABLE
} else {
self | PageType::READ_ONLY
}
}
pub fn noexec(mut self, flag: bool) -> Self {
self &= !(PageType::NOEXEC);
if flag {
self | PageType::NOEXEC
} else {
self
}
}
pub fn page_table(mut self, flag: bool) -> Self {
self &= !(PageType::PAGE_TABLE | PageType::UNKNOWN);
if flag {
self | PageType::PAGE_TABLE
} else {
self
}
}
}
impl Default for PageType {
fn default() -> Self {
PageType::UNKNOWN
}
}
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
pub struct Page {
pub page_type: PageType,
pub page_base: Address,
pub page_size: umem,
}
impl Page {
pub const INVALID: Page = Page {
page_type: PageType::UNKNOWN,
page_base: Address::INVALID,
page_size: 0,
};
pub const fn invalid() -> Self {
Self::INVALID
}
pub fn is_valid(&self) -> bool {
self.page_base.is_valid() && self.page_size != 0
}
}