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
impl PageTable
Sourcepub fn new() -> Self
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.
Sourcepub fn map(
&mut self,
va: Va,
pg: Page,
perm: Permission,
) -> Result<(), PageTableMappingError>
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.
Sourcepub unsafe fn do_map(
&mut self,
va: Va,
pa: Pa,
perm: Permission,
) -> Result<(), PageTableMappingError>
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.
Sourcepub fn unmap(&mut self, va: Va) -> Result<Page, PageTableMappingError>
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).
Sourcepub fn walk(&self, va: Va) -> Result<&Pte, PageTableMappingError>
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).
Sourcepub fn walk_mut(&mut self, va: Va) -> Result<Walked<'_>, PageTableMappingError>
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).
Sourcefn clear(&mut self)
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.