Struct guestmem::GuestMemory

source ·
pub struct GuestMemory { /* private fields */ }
Expand description

A wrapper around a GuestMemoryAccess that provides methods for safely reading and writing guest memory.

Implementations§

source§

impl GuestMemory

source

pub fn new(debug_name: impl Into<Arc<str>>, imp: impl GuestMemoryAccess) -> Self

Returns a new instance using imp as the backing.

debug_name is used to specify which guest memory is being accessed in error messages.

source

pub fn new_multi_region( debug_name: impl Into<Arc<str>>, region_size: u64, imps: Vec<Option<impl GuestMemoryAccess>>, ) -> Result<Self, MultiRegionError>

Creates a new multi-region guest memory, made up of multiple mappings. This allows you to create a very large sparse layout (up to the limits of the VM’s physical address space) without having to allocate an enormous amount of virtual address space.

Each region will be region_size bytes and will start immediately after the last one. This must be a power of two, be at least a page in size, and cannot fill the full 64-bit address space.

imps must be a list of GuestMemoryAccess implementations, one for each region. Use None if the corresponding region is empty.

A region’s mapping cannot fully fill the region. This is necessary to avoid callers expecting to be able to access a memory range that spans two regions.

source

pub fn allocate(size: usize) -> Self

Allocates a guest memory object on the heap with the given size in bytes.

size will be rounded up to the page size. The backing buffer will be page aligned.

The debug name in errors will be “heap”. If you want to provide a different debug name, manually use GuestMemory::new with AlignedHeapMemory.

source

pub fn empty() -> Self

Returns an empty guest memory, which fails every operation.

source

pub fn subrange( &self, offset: u64, len: u64, allow_preemptive_locking: bool, ) -> Result<GuestMemory, GuestMemoryError>

source

pub fn full_mapping(&self) -> Option<(*mut u8, usize)>

Returns the mapping for all of guest memory.

Returns None if there is more than one region or if the memory is not mapped.

source

pub fn iova(&self, gpa: u64) -> Option<u64>

Gets the IO address for DMAing to gpa from a user-mode driver not going through an IOMMU.

source

pub fn write_at(&self, gpa: u64, src: &[u8]) -> Result<(), GuestMemoryError>

Writes src into guest memory at address gpa.

source

pub fn write_from_atomic( &self, gpa: u64, src: &[AtomicU8], ) -> Result<(), GuestMemoryError>

Writes src into guest memory at address gpa.

source

pub fn fill_at( &self, gpa: u64, val: u8, len: usize, ) -> Result<(), GuestMemoryError>

Writes len bytes of val into guest memory at address gpa.

source

pub fn read_at(&self, gpa: u64, dest: &mut [u8]) -> Result<(), GuestMemoryError>

Reads from guest memory address gpa into dest.

source

pub fn read_to_atomic( &self, gpa: u64, dest: &[AtomicU8], ) -> Result<(), GuestMemoryError>

Reads from guest memory address gpa into dest.

source

pub fn write_plain<T: AsBytes>( &self, gpa: u64, b: &T, ) -> Result<(), GuestMemoryError>

Writes an object to guest memory at address gpa.

If the object is 1, 2, 4, or 8 bytes and the address is naturally aligned, then the write will be performed atomically. Here, this means that concurrent readers (via read_plain) cannot observe a torn write but will observe either the old or new value.

The memory ordering of the write is unspecified.

FUTURE: once we are on Rust 1.79, add a method specifically for atomic accesses that const asserts that the size is appropriate.

source

pub fn compare_exchange<T: AsBytes + FromBytes + Copy>( &self, gpa: u64, current: T, new: T, ) -> Result<Result<T, T>, GuestMemoryError>

Attempts a sequentially-consistent compare exchange of the value at gpa.

source

pub fn compare_exchange_bytes<T: AsBytes + FromBytes + ?Sized>( &self, gpa: u64, current: &mut T, new: &T, ) -> Result<bool, GuestMemoryError>

Attempts a sequentially-consistent compare exchange of the value at gpa.

source

pub fn read_plain<T: FromBytes>(&self, gpa: u64) -> Result<T, GuestMemoryError>

Reads an object from guest memory at address gpa.

If the object is 1, 2, 4, or 8 bytes and the address is naturally aligned, then the read will be performed atomically. Here, this means that when there is a concurrent writer, callers will observe either the old or new value, but not a torn read.

The memory ordering of the read is unspecified.

FUTURE: once we are on Rust 1.79, add a method specifically for atomic accesses that const asserts that the size is appropriate.

source

pub fn lock_gpns( &self, with_kernel_access: bool, gpns: &[u64], ) -> Result<LockedPages, GuestMemoryError>

source

pub fn probe_gpns(&self, gpns: &[u64]) -> Result<(), GuestMemoryError>

source

pub fn check_gpa_readable(&self, gpa: u64) -> bool

Check if a given GPA is readable or not.

source

pub fn write_range( &self, range: &PagedRange<'_>, data: &[u8], ) -> Result<(), GuestMemoryError>

source

pub fn zero_range(&self, range: &PagedRange<'_>) -> Result<(), GuestMemoryError>

source

pub fn read_range( &self, range: &PagedRange<'_>, data: &mut [u8], ) -> Result<(), GuestMemoryError>

source

pub fn write_range_from_atomic( &self, range: &PagedRange<'_>, data: &[AtomicU8], ) -> Result<(), GuestMemoryError>

source

pub fn read_range_to_atomic( &self, range: &PagedRange<'_>, data: &[AtomicU8], ) -> Result<(), GuestMemoryError>

source

pub fn lock_range<T: LockedRange>( &self, paged_range: PagedRange<'_>, locked_range: T, ) -> Result<LockedRangeImpl<T>, GuestMemoryError>

Locks the guest pages spanned by the specified PagedRange for the 'static lifetime.

§Arguments
  • ‘paged_range’ - The guest memory range to lock.
  • ‘locked_range’ - Receives a list of VA ranges to which each contiguous physical sub-range in paged_range has been mapped. Must be initially empty.

Trait Implementations§

source§

impl Clone for GuestMemory

source§

fn clone(&self) -> GuestMemory

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for GuestMemory

source§

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

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

impl Default for GuestMemory

The default implementation is GuestMemory::empty.

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Inspect for GuestMemory

source§

fn inspect(&self, req: Request<'_>)

Inspects the object.

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

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

Uses borrowed data to replace owned data, usually by cloning. 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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T, U> Upcast<U> for T
where U: Downcast<T>,