Skip to content

Commit

Permalink
Ready to publish page_table_entry to crates.io
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Jul 16, 2024
1 parent a0dc70c commit 84120cd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
37 changes: 37 additions & 0 deletions page_table_entry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# page_table_entry

This crate provides the definition of page table entry for various hardware
architectures.

Currently supported architectures and page table entry types:

- x86: [`x86_64::X64PTE`][1]
- ARM: [`aarch64::A64PTE`][2]
- RISC-V: [`riscv::Rv64PTE`][3]

All these types implement the [`GenericPTE`][4] trait, which provides unified
methods for manipulating various page table entries.

[1]: https://docs.rs/page_table_entry/latest/page_table_entry/x86_64/struct.X64PTE.html
[2]: https://docs.rs/page_table_entry/latest/page_table_entry/aarch64/struct.A64PTE.html
[3]: https://docs.rs/page_table_entry/latest/page_table_entry/riscv/struct.Rv64PTE.html
[4]: https://docs.rs/page_table_entry/latest/page_table_entry/trait.GenericPTE.html

## Examples (x86_64)

```rust
use memory_addr::PhysAddr;
use page_table_entry::{GenericPTE, MappingFlags, x86_64::X64PTE};
use x86_64::structures::paging::page_table::PageTableFlags;

let paddr = PhysAddr::from(0x233000);
let pte = X64PTE::new_page(
paddr,
/*flags:*/ MappingFlags::READ | MappingFlags::WRITE,
/*is_huge:*/ false,
);
assert!(!pte.is_unused());
assert!(pte.is_present());
assert_eq!(pte.paddr(), paddr);
assert_eq!(pte.bits(), 0x800_0000000233_003); // PRESENT | WRITE | NO_EXECUTE | paddr(0x233000)
```
3 changes: 3 additions & 0 deletions page_table_entry/src/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ impl GenericPTE for A64PTE {
self.0 = (self.0 & Self::PHYS_ADDR_MASK) | attr.bits();
}

fn bits(self) -> usize {
self.0 as usize
}
fn is_unused(&self) -> bool {
self.0 == 0
}
Expand Down
3 changes: 3 additions & 0 deletions page_table_entry/src/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ impl GenericPTE for Rv64PTE {
self.0 = (self.0 & Self::PHYS_ADDR_MASK) | flags.bits() as u64;
}

fn bits(self) -> usize {
self.0 as usize
}
fn is_unused(&self) -> bool {
self.0 == 0
}
Expand Down
3 changes: 3 additions & 0 deletions page_table_entry/src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ impl GenericPTE for X64PTE {
self.0 = (self.0 & Self::PHYS_ADDR_MASK) | flags.bits()
}

fn bits(self) -> usize {
self.0 as usize
}
fn is_unused(&self) -> bool {
self.0 == 0
}
Expand Down
15 changes: 3 additions & 12 deletions page_table_entry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
//! This crate provides the definition of page table entry for various hardware
//! architectures.
//!
//! Currently supported architectures and page table entry types:
//!
//! - x86: [`x86_64::X64PTE`]
//! - ARM: [`aarch64::A64PTE`]
//! - RISC-V: [`riscv::Rv64PTE`]
//!
//! All these types implement the [`GenericPTE`] trait, which provides unified
//! methods for manipulating various page table entries.
#![no_std]
#![feature(doc_auto_cfg)]
#![feature(doc_cfg)]
#![doc = include_str!("../README.md")]

mod arch;

Expand Down Expand Up @@ -60,6 +49,8 @@ pub trait GenericPTE: Debug + Clone + Copy + Sync + Send + Sized {
/// Set flags of the entry.
fn set_flags(&mut self, flags: MappingFlags, is_huge: bool);

/// Returns the raw bits of this entry.
fn bits(self) -> usize;
/// Returns whether this entry is zero.
fn is_unused(&self) -> bool;
/// Returns whether this entry flag indicates present.
Expand Down

0 comments on commit 84120cd

Please sign in to comment.