PageTable

Struct PageTable 

Source
pub struct PageTable(pub Box<PageTableRoot>);
Expand description

Page Table Structure for x86_64 Architecture.

This implements a 4-level page table structure for the x86_64 architecture. It includes an inner structure (PageTableRoot) that stores the actual entries for each of the 512 slots in the page table at each level. The PageTable provides methods for mapping virtual addresses (VAs) to physical addresses (PAs) with different permission levels, unmapping pages, and walking the page table to find page table entries (PTEs) for given virtual addresses. This is a fundamental part of managing virtual memory in an operating system.

Tuple Fields§

§0: Box<PageTableRoot>

Implementations§

Source§

impl PageTable

Source

pub fn new() -> Self

Create an empty page table.

This method initializes a new page table that allows to access every kernel address. The page table is represented as a Box<PageTableRoot>, which contains an array of 512 Pml4e entries. This structure is used in the 4-level paging system of x86_64 architecture.

§Returns

A new PageTable instance with all entries initialized to zero.

Source

pub fn pa(&self) -> Pa

Get physical address of this page table.

This method calculates the physical address (PA) corresponding to the current page table. It does this by converting the virtual address (VA) of the PageTable structure into a physical address.

§Returns

The physical address of the page table (Pa).

Source

pub fn map( &mut self, va: Va, pg: Page, perm: Permission, ) -> Result<(), PageTableMappingError>

Map a virtual address (va) to a physical page (pg) with the specified permissions (perm).

This method updates the page table by mapping the provided virtual address to the given physical page, along with the requested permissions. It ensures that the virtual address is correctly mapped to the physical page, enabling proper memory access.

§Arguments
  • va: The virtual address to map.
  • pg: The physical page to map to the virtual address.
  • perm: The permissions to apply to the mapping (e.g., read, write).
§Returns

A Result indicating success or failure of the mapping operation. If successful, it returns Ok(()). If there’s an error (e.g., invalid virtual address or permissions), it returns an appropriate PageTableMappingError.

Source

pub unsafe fn do_map( &mut self, va: Va, pa: Pa, perm: Permission, ) -> Result<(), PageTableMappingError>

Map a physical address (pa) to a virtual address (va) with the specified permissions (perm).

§Safety

This method is marked unsafe because it relies on the assumption that the physical address (pa) is valid.

§Arguments
  • va: The virtual address to map.
  • pa: The physical address to map to the virtual address.
  • perm: The permissions to apply to the mapping (e.g., read, write).
§Returns

A Result indicating success or failure of the mapping operation. If successful, it returns Ok(()). If there’s an error (e.g., invalid physical address or permissions), it returns an appropriate PageTableMappingError.

Source

pub fn unmap(&mut self, va: Va) -> Result<Page, PageTableMappingError>

Unmap the given virtual address (va) and return the physical page that was mapped to it.

This method removes the mapping for the specified virtual address (va) and returns the physical page (Page) that was mapped to it, if such a mapping existed.

§Arguments
  • va: The virtual address to unmap.
§Returns

A Result containing the physical page (Page) that was mapped to the given virtual address, or an error if the unmapping operation fails (e.g., the virtual address was not previously mapped).

Source

pub fn walk(&self, va: Va) -> Result<&Pte, PageTableMappingError>

Walk through the page table to find reference to the corresponding page table entry (PTE) for the given virtual address (va).

This method traverses the 4-level page table structure and returns a reference to the page table entry (Pte) for the specified virtual address, if such an entry exists.

§Arguments
  • va: The virtual address to find the corresponding page table entry for.
§Returns

A Result containing a reference to the page table entry (Pte) corresponding to the given virtual address, or an error if the entry does not exist (e.g., if the address is not mapped).

Source

pub fn walk_mut(&mut self, va: Va) -> Result<Walked<'_>, PageTableMappingError>

Walk through the page table to find mutable reference for the corresponding page table entry (PTE) for the given virtual address (va).

This method traverses the 4-level page table structure and returns a object to modify the page table entry (Walked) for the specified virtual address, if such an entry exists.

§Arguments
  • va: The virtual address to find the corresponding page table entry for.
§Returns

A Result containing a Walked referring to the page table entry (Pte) corresponding to the given virtual address, or an error if the entry does not exist (e.g., if the address is not mapped).

Source

fn clear(&mut self)

Clears all entries from the page table and deallocates associated pages.

This function traverses all levels of the page table, unmapping each mapped virtual address and deallocating the corresponding physical pages. After calling this method, the page table will be empty, including intermediate levels such as PDP, PD, and PT, except for the PML4 page itself, which remains allocated.

This method is automatically called when a PageTable is dropped. At this point, it is guaranteed that no cores are using this page table.

§Behavior
  • Unmaps all virtual addresses currently mapped in the page table.
  • Frees all allocated pages, including intermediate-level page tables.
  • Leaves only the root page (PML4) intact.
§Safety
  • Must only be called when no active process depends on the current mappings.
  • Calling this on a live page table (e.g., the currently active one) may result in undefined behavior.

Trait Implementations§

Source§

impl Default for PageTable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for PageTable

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for PageTable

§

impl RefUnwindSafe for PageTable

§

impl Send for PageTable

§

impl Sync for PageTable

§

impl Unpin for PageTable

§

impl UnwindSafe for PageTable

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.