Skip to content

Commit

Permalink
Extend Sv39PageTable/Sv48PageTable to support nested page tables al…
Browse files Browse the repository at this point in the history
…so (#7)

* extend sv pagetables to support nested page tables also

* simplify code

* remove a mis-commited file
  • Loading branch information
aarkegz authored Oct 10, 2024
1 parent 2193fa5 commit 9a6bb36
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions page_table_multiarch/src/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,55 @@ fn riscv_flush_tlb(vaddr: Option<memory_addr::VirtAddr>) {
}
}

/// A virtual address that can be used in RISC-V Sv39 and Sv48 page tables.
pub trait SvVirtAddr: memory_addr::MemoryAddr + Send + Sync {
/// Flush the TLB.
fn flush_tlb(vaddr: Option<Self>);
}

impl SvVirtAddr for memory_addr::VirtAddr {
#[inline]
fn flush_tlb(vaddr: Option<Self>) {
riscv_flush_tlb(vaddr.map(|vaddr| vaddr.into()))
}
}

/// Metadata of RISC-V Sv39 page tables.
pub struct Sv39MetaData;
pub struct Sv39MetaData<VA: SvVirtAddr> {
_virt_addr: core::marker::PhantomData<VA>,
}

/// Metadata of RISC-V Sv48 page tables.
pub struct Sv48MetaData;
pub struct Sv48MetaData<VA: SvVirtAddr> {
_virt_addr: core::marker::PhantomData<VA>,
}

impl PagingMetaData for Sv39MetaData {
impl<VA: SvVirtAddr> PagingMetaData for Sv39MetaData<VA> {
const LEVELS: usize = 3;
const PA_MAX_BITS: usize = 56;
const VA_MAX_BITS: usize = 39;
type VirtAddr = memory_addr::VirtAddr;
type VirtAddr = VA;

#[inline]
fn flush_tlb(vaddr: Option<memory_addr::VirtAddr>) {
riscv_flush_tlb(vaddr)
fn flush_tlb(vaddr: Option<VA>) {
<VA as SvVirtAddr>::flush_tlb(vaddr);
}
}

impl PagingMetaData for Sv48MetaData {
impl<VA: SvVirtAddr> PagingMetaData for Sv48MetaData<VA> {
const LEVELS: usize = 4;
const PA_MAX_BITS: usize = 56;
const VA_MAX_BITS: usize = 48;
type VirtAddr = memory_addr::VirtAddr;
type VirtAddr = VA;

#[inline]
fn flush_tlb(vaddr: Option<memory_addr::VirtAddr>) {
riscv_flush_tlb(vaddr)
fn flush_tlb(vaddr: Option<VA>) {
<VA as SvVirtAddr>::flush_tlb(vaddr);
}
}

/// Sv39: Page-Based 39-bit (3 levels) Virtual-Memory System.
pub type Sv39PageTable<H> = PageTable64<Sv39MetaData, Rv64PTE, H>;
pub type Sv39PageTable<H> = PageTable64<Sv39MetaData<memory_addr::VirtAddr>, Rv64PTE, H>;

/// Sv48: Page-Based 48-bit (4 levels) Virtual-Memory System.
pub type Sv48PageTable<H> = PageTable64<Sv48MetaData, Rv64PTE, H>;
pub type Sv48PageTable<H> = PageTable64<Sv48MetaData<memory_addr::VirtAddr>, Rv64PTE, H>;

0 comments on commit 9a6bb36

Please sign in to comment.