Expand description
§page_table_multiarch
This crate provides generic, unified, architecture-independent, and OS-free page table structures for various hardware architectures.
The core struct is PageTable64<M, PTE, H>. OS-functions and architecture-dependent types are provided by generic parameters:
M: The architecture-dependent metadata, requires to implement thePagingMetaDatatrait.PTE: The architecture-dependent page table entry, requires to implement theGenericPTEtrait.H: OS-functions such as physical memory allocation, requires to implement thePagingHandlertrait.
Currently supported architectures and page table structures:
- x86:
x86_64::X64PageTable - ARM:
aarch64::A64PageTable - RISC-V:
riscv::Sv39PageTable,riscv::Sv48PageTable - LoongArch64:
loongarch64:LA64PageTable
§Examples (x86_64)
use memory_addr::{MemoryAddr, PhysAddr, VirtAddr};
use page_table_multiarch::x86_64::{X64PageTable};
use page_table_multiarch::{MappingFlags, PagingHandler, PageSize};
use core::alloc::Layout;
extern crate alloc;
struct PagingHandlerImpl;
impl PagingHandler for PagingHandlerImpl {
fn alloc_frame() -> Option<PhysAddr> {
let layout = Layout::from_size_align(0x1000, 0x1000).unwrap();
let ptr = unsafe { alloc::alloc::alloc(layout) };
Some(PhysAddr::from(ptr as usize))
}
fn dealloc_frame(paddr: PhysAddr) {
let layout = Layout::from_size_align(0x1000, 0x1000).unwrap();
let ptr = paddr.as_usize() as *mut u8;
unsafe { alloc::alloc::dealloc(ptr, layout) };
}
fn phys_to_virt(paddr: PhysAddr) -> VirtAddr {
VirtAddr::from(paddr.as_usize())
}
}
let vaddr = VirtAddr::from(0xdead_beef_000);
let paddr = PhysAddr::from(0x2000);
let flags = MappingFlags::READ | MappingFlags::WRITE;
let mut pt = X64PageTable::<PagingHandlerImpl>::try_new().unwrap();
assert!(pt.root_paddr().is_aligned_4k());
assert!(pt.map(vaddr, paddr, PageSize::Size4K, flags).is_ok());
assert_eq!(pt.query(vaddr), Ok((paddr, flags, PageSize::Size4K)));Re-exports§
pub use page_table_entry::GenericPTE;pub use page_table_entry::MappingFlags;
Modules§
- aarch64
AArch64 - AArch64 specific page table structures.
- loongarch64
LoongArch LA64 - LoongArch64 specific page table structures.
- riscv
RISC-V RV32 or RISC-V RV64 - RISC-V specific page table structures.
- x86_64
x86-64 - x86 specific page table structures.
Structs§
- Page
Table64 - A generic page table struct for 64-bit platform.
- TlbFlush
- This type indicates the mapping of a virtual address has been changed.
- TlbFlush
All - This type indicates the page table mappings have been changed.
Enums§
- Page
Size - The page sizes supported by the hardware page table.
- Paging
Error - The error type for page table operation failures.
Traits§
- Paging
Handler - The low-level OS-dependent helpers that must be provided for
PageTable64. - Paging
Meta Data - The architecture-dependent metadata that must be provided for
PageTable64.
Type Aliases§
- Paging
Result - The specialized
Resulttype for page table operations.