VirtualMemoryManager

Struct VirtualMemoryManager 

Source
pub struct VirtualMemoryManager {
    inner: Arc<RwLock<InnerVmm>>,
}

Fields§

§inner: Arc<RwLock<InnerVmm>>

Implementations§

Source§

impl VirtualMemoryManager

Source

pub fn new() -> Self

Creates a new virtual memory manager.

§Returns

A new virtual memory manager with default values.

Source

pub fn set_asid(&self, asid: u16)

Sets the ASID (Address Space ID) for the virtual memory manager.

§Arguments
  • asid - The ASID to set
Source

pub fn get_asid(&self) -> u16

Returns the ASID (Address Space ID) for the virtual memory manager.

§Returns

The ASID for the virtual memory manager.

Source

pub fn memmap_len(&self) -> usize

Returns a mutable iterator over all memory maps.

§Returns

A mutable iterator over references to all memory maps. Returns the number of memory maps.

§Returns

The number of memory maps.

Source

pub fn memmap_is_empty(&self) -> bool

Returns true if there are no memory maps.

§Returns

True if there are no memory maps.

Source

pub fn with_memmaps<R>( &self, f: impl FnOnce(&BTreeMap<usize, VirtualMemoryMap>) -> R, ) -> R

Execute a read-only operation while holding a read lock on memmaps. This avoids cloning and provides high-performance access.

Source

pub fn with_memmaps_mut<R>( &self, f: impl FnOnce(&mut BTreeMap<usize, VirtualMemoryMap>) -> R, ) -> R

Execute a mutable operation while holding a write lock on memmaps. Prefer using provided APIs; expose for advanced use-cases.

Source

pub fn memmaps_iter_with<R, F>(&self, f: F) -> R
where F: for<'a> FnOnce(Values<'a, usize, VirtualMemoryMap>) -> R,

Execute a read-only iteration over memory maps while holding the lock. This returns an iterator of &VirtualMemoryMap valid only inside the closure.

Source

pub fn get_memory_map_by_addr( &self, start_addr: usize, ) -> Option<VirtualMemoryMap>

Gets a memory map by its start address.

§Arguments
  • start_addr - The start address of the memory map
§Returns

The memory map with the given start address, if it exists.

Source

pub fn add_memory_map(&self, map: VirtualMemoryMap) -> Result<(), &'static str>

Gets a mutable memory map by its start address.

§Arguments
  • start_addr - The start address of the memory map
§Returns

The mutable memory map with the given start address, if it exists. Adds a memory map to the virtual memory manager with overlap checking.

This method performs overlap detection before adding the mapping. Use this for:

  • User-initiated memory allocation (mmap, malloc, etc.)
  • Dynamic memory allocation where overlap is possible
  • Any case where memory range conflicts are uncertain

This method uses efficient overlap detection with ordered data structures.

§Arguments
  • map - The memory map to add
§Returns

A result indicating success or failure.

Source

pub fn remove_memory_map_by_addr( &self, vaddr: usize, ) -> Option<VirtualMemoryMap>

Removes the memory map containing the given virtual address.

This method uses efficient search with caching to locate the target mapping.

§Arguments
  • vaddr - The virtual address contained in the memory map to remove
§Returns

The removed memory map, if it exists.

Source

pub fn remove_all_memory_maps(&self) -> impl Iterator<Item = VirtualMemoryMap>

Removes all memory maps.

§Returns

The removed memory maps.

§Note

This method returns an iterator instead of a cloned Vec for efficiency.

Source

pub fn restore_memory_maps<I>(&self, maps: I) -> Result<(), &'static str>
where I: IntoIterator<Item = VirtualMemoryMap>,

Restores the memory maps from a given iterator.

§Arguments
  • maps - The iterator of memory maps to restore
§Returns

A result indicating success or failure.

Source

pub fn search_memory_map(&self, vaddr: usize) -> Option<VirtualMemoryMap>

Searches for a memory map containing the given virtual address. Implements caching for efficient range search in memory mappings.

§Arguments
  • vaddr - The virtual address to search for
§Returns

The memory map containing the given virtual address, if it exists.

Source

pub fn add_page_table(&self, page_table: Arc<PageTable>)

Efficient memory map search using BTreeMap’s ordered nature

This method uses the ordered property of BTreeMap to efficiently find the memory mapping containing the given address.

§Arguments
  • vaddr - Virtual address to search for
§Returns

The memory map containing the address, if found Searches for a memory map containing the given virtual address (mutable version).

This version allows mutable access and updates the search cache.

§Arguments
  • vaddr - The virtual address to search for
§Returns

Mutable reference to the memory map containing the given virtual address, if it exists. Helper method that finds memory map and updates cache

§Arguments
  • vaddr - Virtual address to search for
§Returns

The start address key of the found memory map, if any Adds a page table to the virtual memory manager.

Source

pub fn get_root_page_table(&self) -> Option<&mut PageTable>

Returns the root page table for the current address space.

§Returns

The root page table for the current address space, if it exists.

Source

pub fn lazy_map_page(&self, vaddr: usize) -> Result<(), &'static str>

Lazy map a virtual address to MMU on demand (called from page fault handler)

This method finds the memory mapping for the given virtual address and maps only the specific page to the MMU on demand.

§Arguments
  • vaddr - The virtual address that caused the page fault
§Returns
  • Ok(()) - Successfully mapped the page
  • Err(&'static str) - Failed to map (no mapping found or MMU error)
Source

pub fn lazy_map_page_with(&self, access: AccessKind) -> Result<(), &'static str>

Lazy map with access context (instruction/load/store and optional size)

Source

fn try_extend_mapping_for_access( &self, access: &AccessKind, ) -> Result<(), &'static str>

Try to extend a mapping for an access that falls outside the current vmarea

This handles the case where a SharedMemory has been resized via ftruncate but the VirtualMemoryMap.vmarea.end hasn’t been updated.

Source

pub fn unmap_range_from_mmu(&self, vaddr_start: usize, vaddr_end: usize)

Unmap a virtual address range from MMU

This method unmaps the specified virtual address range from the MMU. Used when memory mappings are removed.

§Arguments
  • vaddr_start - Start of virtual address range
  • vaddr_end - End of virtual address range (inclusive)
Source

pub fn translate_vaddr(&self, vaddr: usize) -> Option<usize>

Translate a virtual address to physical address

This method uses efficient search with caching for optimal performance.

§Arguments
  • vaddr - The virtual address to translate
§Returns

The translated physical address. Returns None if no mapping exists for the address

Source

pub fn get_mmap_base(&self) -> usize

Gets the mmap base address

§Returns

The base address for mmap operations

Source

pub fn set_mmap_base(&self, base: usize)

Sets the mmap base address This allows dynamic adjustment of the mmap region

§Arguments
  • base - New base address for mmap operations
Source

pub fn find_unmapped_area(&self, size: usize, alignment: usize) -> Option<usize>

Find a suitable address for new memory mapping

§Arguments
  • size - Size of the mapping needed
  • alignment - Required alignment (typically PAGE_SIZE)
§Returns

A suitable virtual address for the new mapping, or None if no space available

Source

pub fn add_memory_map_fixed( &self, map: VirtualMemoryMap, ) -> Result<Vec<VirtualMemoryMap>, &'static str>

Add a memory map at a fixed address, handling overlapping mappings by splitting them

This method is designed for FIXED memory mappings where the caller wants to map at a specific virtual address, potentially overwriting existing mappings. Any existing mappings that overlap with the new mapping will be properly split or removed to make room for the new mapping.

§Arguments
  • map - The memory map to add at a fixed location
§Returns
  • Ok(Vec<VirtualMemoryMap>) - Returns a vector of overwritten (intersected) memory regions that were replaced by the new mapping.
  • Err(&'static str) - Error message if the operation failed
§Design

For each existing mapping that overlaps with the new mapping:

  • The function calculates the intersection (overwritten region) between the new mapping and each overlapping existing mapping.
  • Only the intersection (overwritten part) is returned for each overlap.
  • If the new mapping completely contains the existing mapping, the entire existing mapping is returned as the intersection.
  • If the new mapping partially overlaps, only the overlapped region is returned.
  • Non-overlapping parts of existing mappings are preserved (split and kept).

The caller is responsible for handling any managed pages associated with the overwritten mappings.

Source

pub fn get_memory_stats(&self) -> (usize, usize, usize)

Get memory statistics and usage information This provides detailed information about memory usage patterns

§Returns

A tuple containing (total_maps, total_virtual_size, fragmentation_info)

Source

pub fn coalesce_memory_maps(&self) -> usize

Perform memory map coalescing optimization This attempts to merge adjacent memory maps with compatible properties

§Returns

Number of memory maps that were successfully coalesced

Source

fn can_merge_memory_maps( map1: &VirtualMemoryMap, map2: &VirtualMemoryMap, ) -> bool

Check if two memory maps can be merged

§Arguments
  • map1 - First memory map
  • map2 - Second memory map
§Returns

true if memory maps can be safely merged

Trait Implementations§

Source§

impl Clone for VirtualMemoryManager

Source§

fn clone(&self) -> VirtualMemoryManager

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VirtualMemoryManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for VirtualMemoryManager

Source§

fn drop(&mut self)

Drops the virtual memory manager, freeing the address space if it is still in use.

Auto Trait Implementations§

§

impl Freeze for VirtualMemoryManager

§

impl !RefUnwindSafe for VirtualMemoryManager

§

impl Send for VirtualMemoryManager

§

impl Sync for VirtualMemoryManager

§

impl Unpin for VirtualMemoryManager

§

impl !UnwindSafe for VirtualMemoryManager

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> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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.