#[cfg_attr(target_arch = "x86_64", path = "arch/amd64/vmem.rs")]
#[cfg_attr(target_arch = "x86", path = "arch/i686/vmem.rs")]
mod arch;
pub use arch::PAGE_SIZE;
pub use arch::{PAGE_TABLE_SIZE, PageTableEntry, PhysAddr, VirtAddr};
pub const PAGE_TABLE_ENTRIES_PER_TABLE: usize =
PAGE_TABLE_SIZE / core::mem::size_of::<PageTableEntry>();
pub trait TableReadOps {
type TableAddr: Copy;
fn entry_addr(addr: Self::TableAddr, entry_offset: u64) -> Self::TableAddr;
unsafe fn read_entry(&self, addr: Self::TableAddr) -> PageTableEntry;
fn to_phys(addr: Self::TableAddr) -> PhysAddr;
fn from_phys(addr: PhysAddr) -> Self::TableAddr;
fn root_table(&self) -> Self::TableAddr;
}
pub enum Void {}
pub struct MayMoveTable {}
pub struct MayNotMoveTable {}
mod sealed {
use super::{MayMoveTable, MayNotMoveTable, TableReadOps, Void};
pub trait TableMovabilityBase<Op: TableReadOps + ?Sized> {
type TableMoveInfo;
}
impl<Op: TableReadOps> TableMovabilityBase<Op> for MayMoveTable {
type TableMoveInfo = Op::TableAddr;
}
impl<Op: TableReadOps> TableMovabilityBase<Op> for MayNotMoveTable {
type TableMoveInfo = Void;
}
}
use sealed::*;
pub trait TableMovability<Op: TableReadOps + ?Sized>:
TableMovabilityBase<Op>
+ arch::TableMovability<Op, <Self as TableMovabilityBase<Op>>::TableMoveInfo>
{
}
impl<
Op: TableReadOps,
T: TableMovabilityBase<Op>
+ arch::TableMovability<Op, <Self as TableMovabilityBase<Op>>::TableMoveInfo>,
> TableMovability<Op> for T
{
}
pub trait TableOps: TableReadOps {
type TableMovability: TableMovability<Self>;
unsafe fn alloc_table(&self) -> Self::TableAddr;
unsafe fn write_entry(
&self,
addr: Self::TableAddr,
entry: PageTableEntry,
) -> Option<<Self::TableMovability as TableMovabilityBase<Self>>::TableMoveInfo>;
unsafe fn update_root(
&self,
new_root: <Self::TableMovability as TableMovabilityBase<Self>>::TableMoveInfo,
);
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct BasicMapping {
pub readable: bool,
pub writable: bool,
pub executable: bool,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct CowMapping {
pub readable: bool,
pub executable: bool,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MappingKind {
Basic(BasicMapping),
Cow(CowMapping),
}
#[derive(Debug)]
pub struct Mapping {
pub phys_base: u64,
pub virt_base: u64,
pub len: u64,
pub kind: MappingKind,
}
pub use arch::map;
pub use arch::virt_to_phys;