Struct vm_memory::mmap::GuestMemoryMmap

source ·
pub struct GuestMemoryMmap<B = ()> { /* private fields */ }
Expand description

GuestMemory implementation that mmaps the guest’s memory in the current process.

Represents the entire physical memory of the guest by tracking all its memory regions. Each region is an instance of GuestRegionMmap, being backed by a mapping in the virtual address space of the calling process.

Implementations§

source§

impl<B: NewBitmap> GuestMemoryMmap<B>

source

pub fn new() -> Self

Creates an empty GuestMemoryMmap instance.

source

pub fn from_ranges(ranges: &[(GuestAddress, usize)]) -> Result<Self, Error>

Creates a container and allocates anonymous memory for guest memory regions.

Valid memory regions are specified as a slice of (Address, Size) tuples sorted by Address.

source

pub fn from_ranges_with_files<A, T>(ranges: T) -> Result<Self, Error>
where A: Borrow<(GuestAddress, usize, Option<FileOffset>)>, T: IntoIterator<Item = A>,

Creates a container and allocates anonymous memory for guest memory regions.

Valid memory regions are specified as a sequence of (Address, Size, Option) tuples sorted by Address.

source§

impl<B: Bitmap> GuestMemoryMmap<B>

source

pub fn from_regions(regions: Vec<GuestRegionMmap<B>>) -> Result<Self, Error>

Creates a new GuestMemoryMmap from a vector of regions.

§Arguments
  • regions - The vector of regions. The regions shouldn’t overlap and they should be sorted by the starting address.
source

pub fn from_arc_regions( regions: Vec<Arc<GuestRegionMmap<B>>> ) -> Result<Self, Error>

Creates a new GuestMemoryMmap from a vector of Arc regions.

Similar to the constructor from_regions() as it returns a GuestMemoryMmap. The need for this constructor is to provide a way for consumer of this API to create a new GuestMemoryMmap based on existing regions coming from an existing GuestMemoryMmap instance.

§Arguments
  • regions - The vector of Arc regions. The regions shouldn’t overlap and they should be sorted by the starting address.
source

pub fn insert_region( &self, region: Arc<GuestRegionMmap<B>> ) -> Result<GuestMemoryMmap<B>, Error>

Insert a region into the GuestMemoryMmap object and return a new GuestMemoryMmap.

§Arguments
  • region: the memory region to insert into the guest memory object.
source

pub fn remove_region( &self, base: GuestAddress, size: GuestUsize ) -> Result<(GuestMemoryMmap<B>, Arc<GuestRegionMmap<B>>), Error>

Remove a region into the GuestMemoryMmap object and return a new GuestMemoryMmap on success, together with the removed region.

§Arguments
  • base: base address of the region to be removed
  • size: size of the region to be removed

Trait Implementations§

source§

impl<B: Clone> Clone for GuestMemoryMmap<B>

source§

fn clone(&self) -> GuestMemoryMmap<B>

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<B: Debug> Debug for GuestMemoryMmap<B>

source§

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

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

impl<B: Default> Default for GuestMemoryMmap<B>

source§

fn default() -> GuestMemoryMmap<B>

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

impl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B>

§

type R = GuestRegionMmap<B>

Type of objects hosted by the address space.
§

type I = GuestMemoryMmap<B>

Lifetime generic associated iterators. Usually this is just Self.
source§

fn num_regions(&self) -> usize

Returns the number of regions in the collection.
source§

fn find_region(&self, addr: GuestAddress) -> Option<&GuestRegionMmap<B>>

Returns the region containing the specified address or None.
source§

fn iter(&self) -> Iter<'_, B>

Gets an iterator over the entries in the collection. Read more
source§

fn with_regions<F, E>(&self, cb: F) -> Result<(), E>
where F: Fn(usize, &Self::R) -> Result<(), E>,

👎Deprecated since 0.6.0: Use .iter() instead
Perform the specified action on each region. Read more
source§

fn with_regions_mut<F, E>(&self, cb: F) -> Result<(), E>
where F: FnMut(usize, &Self::R) -> Result<(), E>,

👎Deprecated since 0.6.0: Use .iter() instead
Perform the specified action on each region mutably. Read more
source§

fn map_and_fold<F, G, T>(&self, init: T, mapf: F, foldf: G) -> T
where F: Fn((usize, &Self::R)) -> T, G: Fn(T, T) -> T,

👎Deprecated since 0.6.0: Use .iter() instead
Applies two functions, specified as callbacks, on the inner memory regions. Read more
source§

fn last_addr(&self) -> GuestAddress

Returns the maximum (inclusive) address managed by the GuestMemory. Read more
source§

fn to_region_addr( &self, addr: GuestAddress ) -> Option<(&Self::R, MemoryRegionAddress)>

Tries to convert an absolute address to a relative address within the corresponding region. Read more
source§

fn address_in_range(&self, addr: GuestAddress) -> bool

Returns true if the given address is present within the memory of the guest.
source§

fn check_address(&self, addr: GuestAddress) -> Option<GuestAddress>

Returns the given address if it is present within the memory of the guest.
source§

fn check_range(&self, base: GuestAddress, len: usize) -> bool

Check whether the range [base, base + len) is valid.
source§

fn checked_offset( &self, base: GuestAddress, offset: usize ) -> Option<GuestAddress>

Returns the address plus the offset if it is present within the memory of the guest.
source§

fn try_access<F>(&self, count: usize, addr: GuestAddress, f: F) -> Result<usize>
where F: FnMut(usize, usize, MemoryRegionAddress, &Self::R) -> Result<usize>,

Invokes callback f to handle data in the address range [addr, addr + count). Read more
source§

fn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8>

Get the host virtual address corresponding to the guest address. Read more
source§

fn get_slice( &self, addr: GuestAddress, count: usize ) -> Result<VolatileSlice<'_, MS<'_, Self>>>

Returns a VolatileSlice of count bytes starting at addr.
source§

impl<'a, B: 'a> GuestMemoryIterator<'a, GuestRegionMmap<B>> for GuestMemoryMmap<B>

§

type Iter = Iter<'a, B>

Type of the iter method’s return value.

Auto Trait Implementations§

§

impl<B> Freeze for GuestMemoryMmap<B>

§

impl<B> RefUnwindSafe for GuestMemoryMmap<B>
where B: RefUnwindSafe,

§

impl<B> Send for GuestMemoryMmap<B>
where B: Sync + Send,

§

impl<B> Sync for GuestMemoryMmap<B>
where B: Sync + Send,

§

impl<B> Unpin for GuestMemoryMmap<B>

§

impl<B> UnwindSafe for GuestMemoryMmap<B>
where B: RefUnwindSafe,

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> Bytes<GuestAddress> for T
where T: GuestMemory + ?Sized,

source§

fn write_slice(&self, buf: &[u8], addr: GuestAddress) -> Result<(), Error>

§Examples
  • Write a slice at guestaddress 0x1000. (uses the backend-mmap feature)
gm.write_slice(&[1, 2, 3, 4, 5], start_addr)
    .expect("Could not write slice to guest memory");
source§

fn read_slice(&self, buf: &mut [u8], addr: GuestAddress) -> Result<(), Error>

§Examples
  • Read a slice of length 16 at guestaddress 0x1000. (uses the backend-mmap feature)
let start_addr = GuestAddress(0x1000);
let mut gm = GuestMemoryMmap::<()>::from_ranges(&vec![(start_addr, 0x400)])
    .expect("Could not create guest memory");
let buf = &mut [0u8; 16];

gm.read_slice(buf, start_addr)
    .expect("Could not read slice from guest memory");
source§

fn read_from<F>( &self, addr: GuestAddress, src: &mut F, count: usize ) -> Result<usize, Error>
where F: Read,

§Examples
  • Read bytes from /dev/urandom (uses the backend-mmap feature)
let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom");

gm.read_from(addr, &mut file, 128)
    .expect("Could not read from /dev/urandom into guest memory");

let read_addr = addr.checked_add(8).expect("Could not compute read address");
let rand_val: u32 = gm
    .read_obj(read_addr)
    .expect("Could not read u32 val from /dev/urandom");
source§

fn write_to<F>( &self, addr: GuestAddress, dst: &mut F, count: usize ) -> Result<usize, Error>
where F: Write,

§Examples
  • Write 128 bytes to /dev/null (uses the backend-mmap feature)
let mut file = OpenOptions::new()
    .write(true)
    .open("/dev/null")
    .expect("Could not open /dev/null");

gm.write_to(start_addr, &mut file, 128)
    .expect("Could not write 128 bytes to the provided address");
source§

fn write_all_to<F>( &self, addr: GuestAddress, dst: &mut F, count: usize ) -> Result<(), Error>
where F: Write,

§Examples
  • Write 128 bytes to /dev/null (uses the backend-mmap feature)
let mut file = OpenOptions::new()
    .write(true)
    .open("/dev/null")
    .expect("Could not open /dev/null");

gm.write_all_to(start_addr, &mut file, 128)
    .expect("Could not write 128 bytes to the provided address");
§

type E = Error

Associated error codes
source§

fn write(&self, buf: &[u8], addr: GuestAddress) -> Result<usize, Error>

Writes a slice into the container at addr. Read more
source§

fn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize, Error>

Reads data from the container at addr into a slice. Read more
source§

fn read_exact_from<F>( &self, addr: GuestAddress, src: &mut F, count: usize ) -> Result<(), Error>
where F: Read,

Reads exactly count bytes from an object and writes them into the container at addr. Read more
source§

fn store<O>( &self, val: O, addr: GuestAddress, order: Ordering ) -> Result<(), Error>
where O: AtomicAccess,

Atomically store a value at the specified address.
source§

fn load<O>(&self, addr: GuestAddress, order: Ordering) -> Result<O, Error>
where O: AtomicAccess,

Atomically load a value from the specified address.
source§

fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E>

Writes an object into the container at addr. Read more
source§

fn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E>

Reads an object from the container at addr. 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.