page_table_generic/
lib.rs1#![no_std]
2
3use core::fmt::Debug;
4
5mod def;
6pub mod frame;
7mod map;
8mod table;
9mod walk;
10
11pub use def::*;
12pub use frame::Frame;
13pub use map::*;
14pub use table::*;
15pub use walk::*;
16
17pub type PagingResult<T = ()> = Result<T, PagingError>;
18
19pub trait FrameAllocator: Clone + Sync + Send + 'static {
20 fn alloc_frame(&self) -> Option<PhysAddr>;
21
22 fn dealloc_frame(&self, frame: PhysAddr);
23
24 fn phys_to_virt(&self, paddr: PhysAddr) -> *mut u8;
25}
26
27pub trait TableMeta: Sync + Send + Clone + Copy + 'static {
28 type P: PageTableEntry;
29
30 const PAGE_SIZE: usize;
32
33 const LEVEL_BITS: &[usize];
35
36 const MAX_BLOCK_LEVEL: usize;
38
39 fn flush(vaddr: Option<VirtAddr>);
41}
42
43pub trait PageTableEntry: Debug + Sync + Send + Clone + Copy + Sized + 'static {
44 fn from_config(config: PteConfig) -> Self;
52
53 fn to_config(&self, is_dir: bool) -> PteConfig;
63
64 fn valid(&self) -> bool;
65}
66
67pub trait PageTableOp: Send + 'static {
68 fn addr(&self) -> PhysAddr;
69 fn map(&mut self, config: &MapConfig) -> PagingResult;
70 fn unmap(&mut self, virt_start: VirtAddr, size: usize) -> Result<(), PagingError>;
71}
72
73impl<T: TableMeta, A: FrameAllocator> PageTableOp for PageTable<T, A> {
74 fn addr(&self) -> PhysAddr {
75 self.root_paddr()
76 }
77
78 fn map(&mut self, config: &MapConfig) -> PagingResult {
79 PageTableRef::map(self, config)
80 }
81
82 fn unmap(&mut self, virt_start: VirtAddr, size: usize) -> PagingResult {
83 PageTableRef::unmap(self, virt_start, size)
84 }
85}
86
87impl<T: TableMeta, A: FrameAllocator> PageTableOp for PageTableRef<T, A> {
88 fn addr(&self) -> PhysAddr {
89 self.root_paddr()
90 }
91
92 fn map(&mut self, config: &MapConfig) -> PagingResult {
93 self.map(config)
94 }
95
96 fn unmap(&mut self, virt_start: VirtAddr, size: usize) -> Result<(), PagingError> {
97 self.unmap(virt_start, size)
98 }
99}