Skip to main content

AddressSpace

Struct AddressSpace 

Source
pub struct AddressSpace { /* private fields */ }
Expand description

A per-process address space backed by a PML4 page table.

Kernel tasks share a single AddressSpace (the kernel AS). User tasks each get their own, with kernel entries (PML4[256..512]) cloned so that the kernel is always mapped regardless of which AS is active.

Implementations§

Source§

impl AddressSpace

Source

pub unsafe fn new_kernel() -> Self

Create the kernel address space by wrapping the current (boot) CR3.

§Safety

Must be called exactly once, during single-threaded init, after paging is initialized.

Source

pub fn new_user() -> Result<Self, &'static str>

Create a new user address space with the kernel half cloned.

Allocates a fresh PML4 frame, zeroes it, then copies entries 256..512 from the kernel PML4. This shares the kernel’s L3/L2/L1 subtrees so kernel mapping changes propagate automatically.

Source

pub fn reserve_region( &self, start: u64, page_count: usize, flags: VmaFlags, vma_type: VmaType, page_size: VmaPageSize, ) -> Result<(), &'static str>

Reserve a contiguous region of virtual pages without allocating physical frames.

The pages will be mapped lazily during page faults (Demand Paging).

Source

pub fn handle_fault(&self, fault_addr: u64) -> Result<(), &'static str>

Handle a page fault by checking if the address falls within a reserved VMA.

If it does, allocates a physical frame and maps it.

Source

pub fn map_region( &self, start: u64, page_count: usize, flags: VmaFlags, vma_type: VmaType, page_size: VmaPageSize, ) -> Result<(), &'static str>

Map a contiguous region of pages backed by newly allocated physical frames.

Frames are allocated from the buddy allocator and zero-filled. The region is tracked in the VMA list.

Source

pub fn map_shared_frames( &self, start: u64, frame_phys_addrs: &[u64], flags: VmaFlags, vma_type: VmaType, ) -> Result<(), &'static str>

Maps shared frames.

Source

pub fn unmap_region( &self, start: u64, page_count: usize, page_size: VmaPageSize, ) -> Result<(), &'static str>

Unmap a previously mapped region and free the backing frames.

Source

pub fn find_free_vma_range( &self, hint: u64, n_pages: usize, page_size: VmaPageSize, ) -> Option<u64>

Find a free virtual address range of n_pages pages of page_size starting at or after hint.

Source

pub fn has_mapping_in_range(&self, addr: u64, len: u64) -> bool

Return true if any tracked VMA overlaps [addr, addr + len).

Source

pub fn region_by_start(&self, start: u64) -> Option<VirtualMemoryRegion>

Return the tracked VMA that starts exactly at start.

Source

pub fn any_mapped_in_range( &self, addr: u64, len: u64, page_size: VmaPageSize, ) -> Result<bool, &'static str>

Returns true if any page in [addr, addr + len) is currently mapped.

Source

pub fn protect_range( &self, addr: u64, len: u64, flags: VmaFlags, ) -> Result<(), &'static str>

Performs the protect range operation.

Source

pub fn unmap_range(&self, addr: u64, len: u64) -> Result<(), &'static str>

Unmaps range.

Source

pub fn translate(&self, vaddr: VirtAddr) -> Option<PhysAddr>

Translate a virtual address to its mapped physical address.

Source

pub fn cr3(&self) -> PhysAddr

Get the physical address of this address space’s PML4 table.

Source

pub unsafe fn switch_to(&self)

Switch the CPU to this address space by writing CR3.

Skips the write if CR3 already points to this address space (avoids unnecessary TLB flush).

§Safety

The caller must ensure this address space’s page tables are valid and that the kernel half is correctly mapped.

Source

pub fn is_kernel(&self) -> bool

Whether this is the kernel address space.

Source

pub fn has_user_mappings(&self) -> bool

Check if this address space has any user-space memory mappings.

Source

pub fn unmap_all_user_regions(&self)

Unmap all tracked user regions (best-effort).

This frees user frames and clears the VMA list. Kernel mappings are untouched. Does not allocate memory.

Source

pub fn clone_cow(&self) -> Result<Arc<AddressSpace>, &'static str>

Performs the clone cow operation.

Trait Implementations§

Source§

impl Drop for AddressSpace

Source§

fn drop(&mut self)

Performs the drop operation.

Source§

impl Send for AddressSpace

Source§

impl Sync for AddressSpace

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.