pub struct LazyPager {}Expand description
The LazyPager structure implements lazy paging, where memory pages are
mapped only when accessed (on page fault), instead of during mmap calls.
Implementations§
Source§impl LazyPager
impl LazyPager
Sourcepub fn do_copy_on_write(
&mut self,
page_table: &mut PageTable,
reason: &PageFaultReason,
) -> Result<(), KernelError>
pub fn do_copy_on_write( &mut self, page_table: &mut PageTable, reason: &PageFaultReason, ) -> Result<(), KernelError>
Handles a copy-on-write (COW) page fault by creating a private copy of the faulted page.
This method is invoked when a process attempts to write to a page that is currently shared and marked read-only as part of a copy-on-write mapping. It ensures that the faulting process receives its own writable copy of the page while preserving the original contents for other processes that may still share the original page.
§Steps:
- Find write-protected page table entry with
PageTable::walk_mut. - Allocates a new page and copies the contents of the original page into it.
- Updates the page table to point to the new page with write permissions.
- Invalidates the TLB entry for the faulting address to ensure the CPU reloads the mapping.
§Parameters
page_table: The faulting process’s page table.reason: Information about the page fault, including the faulting address and access type.
Sourcepub fn write_protect_ptes(
mm_struct: &mut MmStruct<LazyPager>,
) -> Result<MmStruct<LazyPager>, KernelError>
pub fn write_protect_ptes( mm_struct: &mut MmStruct<LazyPager>, ) -> Result<MmStruct<LazyPager>, KernelError>
Applies write-protection to all user-accessible pages in the memory layout.
This method is called during fork to prepare the address space for
copy-on-write semantics. It traverses the entire virtual memory
layout, identifies writable mappings, and rewrites their page table
entries (PTEs) as read-only. This allows parent and child
processes to safely share physical memory until one performs a write, at
which point a private copy is created.
After modifying the page tables, stale entries in the Translation
Lookaside Buffer (TLB) are invalidated to ensure that the CPU
observes the new permissions by calling tlb_shutdown.
§Parameters
mm_struct: The current process’s memory layout, including itsLazyPagerstate.
§Returns
- A new
MmStructrepresenting the forked child process, with updated page table mappings.
Source§impl LazyPager
impl LazyPager
Sourcepub fn do_lazy_load(
&mut self,
page_table: &mut PageTable,
reason: &PageFaultReason,
) -> Result<(), KernelError>
pub fn do_lazy_load( &mut self, page_table: &mut PageTable, reason: &PageFaultReason, ) -> Result<(), KernelError>
Handles a page fault by performing lazy loading of the faulting page.
This method is invoked when a page fault occurs due to demand paging— that is, when a program accesses a virtual address that is validly mapped but not yet backed by a physical page. This function allocates and installs the corresponding page into the page table on demand.
The kernel may initialize the page from a file (if the mapping was
file-backed) or zero-fill it (if anonymous). The newly loaded page
must also be mapped with the correct permissions, as defined at the
time of the original mmap.
§Parameters
page_table: The page table of the faulting process.reason: ThePageFaultReasonthat describes the faulting reason.
This must indicate a demand paging fault.
§Returns
Ok(())if the page was successfully loaded and mapped.Err(KernelError): If the faulting address is invalid, out of bounds, or if page allocation fails.
Sourcepub fn handle_page_fault(
&mut self,
page_table: &mut PageTable,
reason: &PageFaultReason,
) -> Result<(), KernelError>
pub fn handle_page_fault( &mut self, page_table: &mut PageTable, reason: &PageFaultReason, ) -> Result<(), KernelError>
Handles a page fault by allocating a physical page and updating the page table.
This function is called when a process accesses a lazily mapped page that has not been allocated yet. The function must:
- Identify the faulting virtual address from
PageFaultReason. - Check if the address was previously recorded in
mmapmetadata. - Allocate a new physical page.
- Update the page table with the new mapping.
- Invalidate the TLB entry to ensure memory consistency.
§Arguments
page_table: Mutable reference to the page table.reason: The cause of the page fault, including the faulting address.
If the faulting address was not mapped via mmap, the system should
trigger a segmentation fault, resulting process exit.
Trait Implementations§
Source§impl Pager for LazyPager
impl Pager for LazyPager
Source§fn mmap(
&mut self,
_page_table: &mut PageTable,
addr: Va,
size: usize,
prot: Permission,
file: Option<&RegularFile>,
offset: usize,
) -> Result<usize, KernelError>
fn mmap( &mut self, _page_table: &mut PageTable, addr: Va, size: usize, prot: Permission, file: Option<&RegularFile>, offset: usize, ) -> Result<usize, KernelError>
Memory map function (mmap) for lazy paging.
This function creates the metadata for memory mappings, and delegate the real mappings on page fault.
Returns an address for the mapped area.
Source§fn munmap(
&mut self,
page_table: &mut PageTable,
addr: Va,
) -> Result<usize, KernelError>
fn munmap( &mut self, page_table: &mut PageTable, addr: Va, ) -> Result<usize, KernelError>
Memory unmap function (munmap) for lazy paging.
This function would unmap a previously mapped memory region, releasing any associated resources.
§Returns
- Zero (if succeed) or an error (
KernelError).
Source§fn get_user_page(
&mut self,
page_table: &mut PageTable,
addr: Va,
) -> Option<(PageRef<'_>, Permission)>
fn get_user_page( &mut self, page_table: &mut PageTable, addr: Va, ) -> Option<(PageRef<'_>, Permission)>
Find a mapped page at the given virtual address. If the page for addr is not loaded, load it and then returns.
This function searches for a memory page mapped at addr and, if found,
returns a tuple of PageRef to the page and its corresponding
Permission flags.
§Parameters
addr: The virtual address (Va) of the page to find.
§Returns
Some(([PageRef], [Permission])): If the page is found.None: If no mapped page is found ataddr.
Source§fn access_ok(&self, va: Va, is_write: bool) -> bool
fn access_ok(&self, va: Va, is_write: bool) -> bool
Checks whether access to the given virtual address is permitted.
This function verifies that a virtual address va is part of a valid
memory mapping and that the requested access type (read or write) is
allowed by the page’s protection flags. Note that this does not trigger
the demand paging.
Auto Trait Implementations§
impl Freeze for LazyPager
impl RefUnwindSafe for LazyPager
impl Send for LazyPager
impl Sync for LazyPager
impl Unpin for LazyPager
impl UnwindSafe for LazyPager
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)