use axalloc::global_allocator;
use page_table::PagingIf;
use crate::mem::{phys_to_virt, virt_to_phys, MemRegionFlags, PhysAddr, VirtAddr, PAGE_SIZE_4K};
#[doc(no_inline)]
pub use page_table::{MappingFlags, PageSize, PagingError, PagingResult};
impl From<MemRegionFlags> for MappingFlags {
fn from(f: MemRegionFlags) -> Self {
let mut ret = Self::empty();
if f.contains(MemRegionFlags::READ) {
ret |= Self::READ;
}
if f.contains(MemRegionFlags::WRITE) {
ret |= Self::WRITE;
}
if f.contains(MemRegionFlags::EXECUTE) {
ret |= Self::EXECUTE;
}
if f.contains(MemRegionFlags::DEVICE) {
ret |= Self::DEVICE;
}
if f.contains(MemRegionFlags::UNCACHED) {
ret |= Self::UNCACHED;
}
ret
}
}
pub struct PagingIfImpl;
impl PagingIf for PagingIfImpl {
fn alloc_frame() -> Option<PhysAddr> {
global_allocator()
.alloc_pages(1, PAGE_SIZE_4K)
.map(|vaddr| virt_to_phys(vaddr.into()))
.ok()
}
fn dealloc_frame(paddr: PhysAddr) {
global_allocator().dealloc_pages(phys_to_virt(paddr).as_usize(), 1)
}
#[inline]
fn phys_to_virt(paddr: PhysAddr) -> VirtAddr {
phys_to_virt(paddr)
}
}
cfg_if::cfg_if! {
if #[cfg(target_arch = "x86_64")] {
pub type PageTable = page_table::x86_64::X64PageTable<PagingIfImpl>;
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
pub type PageTable = page_table::riscv::Sv39PageTable<PagingIfImpl>;
} else if #[cfg(target_arch = "aarch64")]{
pub type PageTable = page_table::aarch64::A64PageTable<PagingIfImpl>;
}
}