use axalloc::global_allocator;
use page_table_multiarch::PagingHandler;
use crate::mem::{phys_to_virt, virt_to_phys, MemRegionFlags, PhysAddr, VirtAddr, PAGE_SIZE_4K};
#[doc(no_inline)]
pub use page_table_multiarch::{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 PagingHandlerImpl;
impl PagingHandler for PagingHandlerImpl {
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_multiarch::x86_64::X64PageTable<PagingHandlerImpl>;
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
pub type PageTable = page_table_multiarch::riscv::Sv39PageTable<PagingHandlerImpl>;
} else if #[cfg(target_arch = "aarch64")]{
pub type PageTable = page_table_multiarch::aarch64::A64PageTable<PagingHandlerImpl>;
}
}