pub struct VirtualMemoryManager {
inner: Arc<RwLock<InnerVmm>>,
}Fields§
§inner: Arc<RwLock<InnerVmm>>Implementations§
Source§impl VirtualMemoryManager
impl VirtualMemoryManager
Sourcepub fn get_asid(&self) -> u16
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.
Sourcepub fn memmap_len(&self) -> usize
pub fn memmap_len(&self) -> usize
Sourcepub fn memmap_is_empty(&self) -> bool
pub fn memmap_is_empty(&self) -> bool
Sourcepub fn with_memmaps<R>(
&self,
f: impl FnOnce(&BTreeMap<usize, VirtualMemoryMap>) -> R,
) -> R
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.
Sourcepub fn with_memmaps_mut<R>(
&self,
f: impl FnOnce(&mut BTreeMap<usize, VirtualMemoryMap>) -> R,
) -> R
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.
Sourcepub fn memmaps_iter_with<R, F>(&self, f: F) -> Rwhere
F: for<'a> FnOnce(Values<'a, usize, VirtualMemoryMap>) -> R,
pub fn memmaps_iter_with<R, F>(&self, f: F) -> Rwhere
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.
Sourcepub fn get_memory_map_by_addr(
&self,
start_addr: usize,
) -> Option<VirtualMemoryMap>
pub fn get_memory_map_by_addr( &self, start_addr: usize, ) -> Option<VirtualMemoryMap>
Sourcepub fn add_memory_map(&self, map: VirtualMemoryMap) -> Result<(), &'static str>
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.
Sourcepub fn remove_memory_map_by_addr(
&self,
vaddr: usize,
) -> Option<VirtualMemoryMap>
pub fn remove_memory_map_by_addr( &self, vaddr: usize, ) -> Option<VirtualMemoryMap>
Sourcepub fn remove_all_memory_maps(&self) -> impl Iterator<Item = VirtualMemoryMap>
pub fn remove_all_memory_maps(&self) -> impl Iterator<Item = VirtualMemoryMap>
Sourcepub fn restore_memory_maps<I>(&self, maps: I) -> Result<(), &'static str>where
I: IntoIterator<Item = VirtualMemoryMap>,
pub fn restore_memory_maps<I>(&self, maps: I) -> Result<(), &'static str>where
I: IntoIterator<Item = VirtualMemoryMap>,
Sourcepub fn search_memory_map(&self, vaddr: usize) -> Option<VirtualMemoryMap>
pub fn search_memory_map(&self, vaddr: usize) -> Option<VirtualMemoryMap>
Sourcepub fn add_page_table(&self, page_table: Arc<PageTable>)
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.
Sourcepub fn get_root_page_table(&self) -> Option<&mut PageTable>
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.
Sourcepub fn lazy_map_page(&self, vaddr: usize) -> Result<(), &'static str>
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 pageErr(&'static str)- Failed to map (no mapping found or MMU error)
Sourcepub fn lazy_map_page_with(&self, access: AccessKind) -> Result<(), &'static str>
pub fn lazy_map_page_with(&self, access: AccessKind) -> Result<(), &'static str>
Lazy map with access context (instruction/load/store and optional size)
Sourcefn try_extend_mapping_for_access(
&self,
access: &AccessKind,
) -> Result<(), &'static str>
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.
Sourcepub fn unmap_range_from_mmu(&self, vaddr_start: usize, vaddr_end: usize)
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 rangevaddr_end- End of virtual address range (inclusive)
Sourcepub fn translate_vaddr(&self, vaddr: usize) -> Option<usize>
pub fn translate_vaddr(&self, vaddr: usize) -> Option<usize>
Sourcepub fn get_mmap_base(&self) -> usize
pub fn get_mmap_base(&self) -> usize
Sourcepub fn set_mmap_base(&self, base: usize)
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
Sourcepub fn find_unmapped_area(&self, size: usize, alignment: usize) -> Option<usize>
pub fn find_unmapped_area(&self, size: usize, alignment: usize) -> Option<usize>
Sourcepub fn add_memory_map_fixed(
&self,
map: VirtualMemoryMap,
) -> Result<Vec<VirtualMemoryMap>, &'static str>
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.
Sourcepub fn get_memory_stats(&self) -> (usize, usize, usize)
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)
Sourcepub fn coalesce_memory_maps(&self) -> usize
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
Sourcefn can_merge_memory_maps(
map1: &VirtualMemoryMap,
map2: &VirtualMemoryMap,
) -> bool
fn can_merge_memory_maps( map1: &VirtualMemoryMap, map2: &VirtualMemoryMap, ) -> bool
Trait Implementations§
Source§impl Clone for VirtualMemoryManager
impl Clone for VirtualMemoryManager
Source§fn clone(&self) -> VirtualMemoryManager
fn clone(&self) -> VirtualMemoryManager
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for VirtualMemoryManager
impl Debug for VirtualMemoryManager
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 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)