Skip to main content

page_walker/walkers/
reader.rs

1//! This modules implements the [`PteReader`] struct which is a helper used to read the PTE
2//! pointing to the physical page for a given virtual address, if the virtual address is mapped.
3
4use core::marker::PhantomData;
5use core::ops::Range;
6use crate::address_space::PageTableMapper;
7use crate::PteType;
8use num_traits::{FromPrimitive, PrimInt, Unsigned};
9
10/// The [`PteReader`] struct is an implementation of a [`crate::walker::PageWalker`] used to
11/// retrieve the PTE for a given virtual address, which is used by the [`AddressSpace::read_pte`]
12/// method.
13///
14/// [`AddressSpace::read_pte`]: `super::super::AddressSpace::read_pte`
15pub struct PteReader<'a, PTE, Mapper, Error>
16where
17    PTE: FromPrimitive + PrimInt + Unsigned,
18    Mapper: PageTableMapper<PTE, Error>,
19{
20    /// The page table mapper.
21    pub mapper: &'a Mapper,
22    /// Storage for the retrieved PTE.
23    pub pte: Option<PTE>,
24    /// A marker for Error.
25    pub error: PhantomData<Error>,
26}
27
28impl<'a, PTE, Mapper, Error> crate::PageWalker<PTE, Error> for PteReader<'a, PTE, Mapper, Error>
29where
30    PTE: FromPrimitive + PrimInt + Unsigned,
31    Mapper: PageTableMapper<PTE, Error>,
32{
33    /// Reads the PTE at the given physical address.
34    fn read_pte(&self, phys_addr: PTE) -> Result<PTE, Error> {
35        self.mapper.read_pte(phys_addr)
36    }
37
38    /// Stores the PTE of the page, if the virtual address resolves to a page.
39    fn handle_pte(&mut self, pte_type: PteType, _range: Range<usize>, pte: &PTE) -> Result<(), Error> {
40        if pte_type.is_page() {
41            self.pte = Some(*pte);
42        }
43
44        Ok(())
45    }
46}