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
sourceimpl<B: NewBitmap> GuestMemoryMmap<B>
impl<B: NewBitmap> GuestMemoryMmap<B>
sourcepub fn from_ranges(ranges: &[(GuestAddress, usize)]) -> Result<Self, Error>
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.
sourcepub fn from_ranges_with_files<A, T>(ranges: T) -> Result<Self, Error> where
A: Borrow<(GuestAddress, usize, Option<FileOffset>)>,
T: IntoIterator<Item = A>,
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
sourceimpl<B: Bitmap> GuestMemoryMmap<B>
impl<B: Bitmap> GuestMemoryMmap<B>
sourcepub fn from_regions(regions: Vec<GuestRegionMmap<B>>) -> Result<Self, Error>
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.
sourcepub fn from_arc_regions(
regions: Vec<Arc<GuestRegionMmap<B>>>
) -> Result<Self, Error>
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 ofArc
regions. The regions shouldn’t overlap and they should be sorted by the starting address.
sourcepub fn insert_region(
&self,
region: Arc<GuestRegionMmap<B>>
) -> Result<GuestMemoryMmap<B>, Error>
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.
sourcepub fn remove_region(
&self,
base: GuestAddress,
size: GuestUsize
) -> Result<(GuestMemoryMmap<B>, Arc<GuestRegionMmap<B>>), Error>
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 removedsize
: size of the region to be removed
Trait Implementations
sourceimpl<B: Clone> Clone for GuestMemoryMmap<B>
impl<B: Clone> Clone for GuestMemoryMmap<B>
sourcefn clone(&self) -> GuestMemoryMmap<B>
fn clone(&self) -> GuestMemoryMmap<B>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<B: Debug> Debug for GuestMemoryMmap<B>
impl<B: Debug> Debug for GuestMemoryMmap<B>
sourceimpl<B: Default> Default for GuestMemoryMmap<B>
impl<B: Default> Default for GuestMemoryMmap<B>
sourcefn default() -> GuestMemoryMmap<B>
fn default() -> GuestMemoryMmap<B>
Returns the “default value” for a type. Read more
sourceimpl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B>
impl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B>
type R = GuestRegionMmap<B>
type R = GuestRegionMmap<B>
Type of objects hosted by the address space.
type I = GuestMemoryMmap<B>
type I = GuestMemoryMmap<B>
Lifetime generic associated iterators. Usually this is just Self
.
sourcefn num_regions(&self) -> usize
fn num_regions(&self) -> usize
Returns the number of regions in the collection.
sourcefn find_region(&self, addr: GuestAddress) -> Option<&GuestRegionMmap<B>>
fn find_region(&self, addr: GuestAddress) -> Option<&GuestRegionMmap<B>>
Returns the region containing the specified address or None
.
sourcefn iter(&self) -> Iter<'_, B>ⓘNotable traits for Iter<'a, B>impl<'a, B> Iterator for Iter<'a, B> type Item = &'a GuestRegionMmap<B>;
fn iter(&self) -> Iter<'_, B>ⓘNotable traits for Iter<'a, B>impl<'a, B> Iterator for Iter<'a, B> type Item = &'a GuestRegionMmap<B>;
Gets an iterator over the entries in the collection. Read more
sourcefn with_regions<F, E>(&self, cb: F) -> Result<(), E> where
F: Fn(usize, &Self::R) -> Result<(), E>,
fn with_regions<F, E>(&self, cb: F) -> Result<(), E> where
F: Fn(usize, &Self::R) -> Result<(), E>,
Use .iter()
instead
Perform the specified action on each region. Read more
sourcefn with_regions_mut<F, E>(&self, cb: F) -> Result<(), E> where
F: FnMut(usize, &Self::R) -> Result<(), E>,
fn with_regions_mut<F, E>(&self, cb: F) -> Result<(), E> where
F: FnMut(usize, &Self::R) -> Result<(), E>,
Use .iter()
instead
Perform the specified action on each region mutably. Read more
sourcefn 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,
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,
Use .iter()
instead
Applies two functions, specified as callbacks, on the inner memory regions. Read more
sourcefn last_addr(&self) -> GuestAddress
fn last_addr(&self) -> GuestAddress
Returns the maximum (inclusive) address managed by the
GuestMemory
. Read more
sourcefn to_region_addr(
&self,
addr: GuestAddress
) -> Option<(&Self::R, MemoryRegionAddress)>
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
sourcefn address_in_range(&self, addr: GuestAddress) -> bool
fn address_in_range(&self, addr: GuestAddress) -> bool
Returns true
if the given address is present within the memory of the guest.
sourcefn check_address(&self, addr: GuestAddress) -> Option<GuestAddress>
fn check_address(&self, addr: GuestAddress) -> Option<GuestAddress>
Returns the given address if it is present within the memory of the guest.
sourcefn check_range(&self, base: GuestAddress, len: usize) -> bool
fn check_range(&self, base: GuestAddress, len: usize) -> bool
Check whether the range [base, base + len) is valid.
sourcefn checked_offset(
&self,
base: GuestAddress,
offset: usize
) -> Option<GuestAddress>
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.
sourcefn try_access<F>(&self, count: usize, addr: GuestAddress, f: F) -> Result<usize> where
F: FnMut(usize, usize, MemoryRegionAddress, &Self::R) -> Result<usize>,
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
sourcefn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8>
fn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8>
Get the host virtual address corresponding to the guest address. Read more
sourcefn get_slice(
&self,
addr: GuestAddress,
count: usize
) -> Result<VolatileSlice<'_, MS<'_, Self>>>
fn get_slice(
&self,
addr: GuestAddress,
count: usize
) -> Result<VolatileSlice<'_, MS<'_, Self>>>
Returns a VolatileSlice
of count
bytes starting at
addr
. Read more
sourceimpl<'a, B: 'a> GuestMemoryIterator<'a, GuestRegionMmap<B>> for GuestMemoryMmap<B>
impl<'a, B: 'a> GuestMemoryIterator<'a, GuestRegionMmap<B>> for GuestMemoryMmap<B>
Auto Trait Implementations
impl<B> RefUnwindSafe for GuestMemoryMmap<B> where
B: RefUnwindSafe,
impl<B> Send for GuestMemoryMmap<B> where
B: Send + Sync,
impl<B> Sync for GuestMemoryMmap<B> where
B: Send + Sync,
impl<B> Unpin for GuestMemoryMmap<B>
impl<B> UnwindSafe for GuestMemoryMmap<B> where
B: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Bytes<GuestAddress> for T where
T: GuestMemory + ?Sized,
impl<T> Bytes<GuestAddress> for T where
T: GuestMemory + ?Sized,
sourcefn write_slice(&self, buf: &[u8], addr: GuestAddress) -> Result<(), Error>
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");
sourcefn read_slice(&self, buf: &mut [u8], addr: GuestAddress) -> Result<(), Error>
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");
sourcefn read_from<F>(
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<usize, Error> where
F: Read,
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");
sourcefn write_to<F>(
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<usize, Error> where
F: Write,
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");
sourcefn write_all_to<F>(
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<(), Error> where
F: Write,
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");
sourcefn write(&self, buf: &[u8], addr: GuestAddress) -> Result<usize, Error>
fn write(&self, buf: &[u8], addr: GuestAddress) -> Result<usize, Error>
Writes a slice into the container at addr
. Read more
sourcefn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize, Error>
fn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize, Error>
Reads data from the container at addr
into a slice. Read more
sourcefn read_exact_from<F>(
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<(), Error> where
F: Read,
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
sourcefn store<O>(
&self,
val: O,
addr: GuestAddress,
order: Ordering
) -> Result<(), Error> where
O: AtomicAccess,
fn store<O>(
&self,
val: O,
addr: GuestAddress,
order: Ordering
) -> Result<(), Error> where
O: AtomicAccess,
Atomically store a value at the specified address.
sourcefn load<O>(&self, addr: GuestAddress, order: Ordering) -> Result<O, Error> where
O: AtomicAccess,
fn load<O>(&self, addr: GuestAddress, order: Ordering) -> Result<O, Error> where
O: AtomicAccess,
Atomically load a value from the specified address.