15 releases (4 breaking)

Uses new Rust 2024

0.5.3 Mar 3, 2025
0.5.1 Jan 19, 2025
0.5.0 Dec 17, 2024
0.4.1 Oct 10, 2024
0.3.1 Jul 31, 2024

#112 in Operating systems

Download history 794/week @ 2024-12-13 908/week @ 2024-12-20 823/week @ 2024-12-27 474/week @ 2025-01-03 349/week @ 2025-01-10 332/week @ 2025-01-17 92/week @ 2025-01-24 236/week @ 2025-01-31 274/week @ 2025-02-07 332/week @ 2025-02-14 233/week @ 2025-02-21 589/week @ 2025-02-28 810/week @ 2025-03-07 867/week @ 2025-03-14 375/week @ 2025-03-21 468/week @ 2025-03-28

2,574 downloads per month

GPL-3.0-or-later OR Apache-2…

68KB
1K SLoC

page_table_multiarch

Crates.io Docs.rs CI

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 the PagingMetaData trait.
  • PTE: The architecture-dependent page table entry, requires to implement the GenericPTE trait.
  • H: OS-functions such as physical memory allocation, requires to implement the PagingHandler trait.

Currently supported architectures and page table structures:

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)));

Dependencies

~3.5MB
~32K SLoC