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>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl<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>
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 I = GuestMemoryMmap<B>
type I = GuestMemoryMmap<B>
Self
.sourcefn num_regions(&self) -> usize
fn num_regions(&self) -> usize
sourcefn find_region(&self, addr: GuestAddress) -> Option<&GuestRegionMmap<B>>
fn find_region(&self, addr: GuestAddress) -> Option<&GuestRegionMmap<B>>
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>;
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>,
.iter()
insteadsourcefn 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>,
.iter()
insteadsourcefn map_and_fold<F, G, T>(&self, init: T, mapf: F, foldf: G) -> Twhere
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) -> Twhere
F: Fn((usize, &Self::R)) -> T,
G: Fn(T, T) -> T,
.iter()
insteadsourcefn last_addr(&self) -> GuestAddress
fn last_addr(&self) -> GuestAddress
GuestMemory
. Read moresourcefn to_region_addr(
&self,
addr: GuestAddress
) -> Option<(&Self::R, MemoryRegionAddress)>
fn to_region_addr(
&self,
addr: GuestAddress
) -> Option<(&Self::R, MemoryRegionAddress)>
sourcefn address_in_range(&self, addr: GuestAddress) -> bool
fn address_in_range(&self, addr: GuestAddress) -> bool
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>
sourcefn check_range(&self, base: GuestAddress, len: usize) -> bool
fn check_range(&self, base: GuestAddress, len: usize) -> bool
sourcefn checked_offset(
&self,
base: GuestAddress,
offset: usize
) -> Option<GuestAddress>
fn checked_offset(
&self,
base: GuestAddress,
offset: usize
) -> Option<GuestAddress>
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>,
sourcefn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8>
fn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8>
sourcefn get_slice(
&self,
addr: GuestAddress,
count: usize
) -> Result<VolatileSlice<'_, MS<'_, Self>>>
fn get_slice(
&self,
addr: GuestAddress,
count: usize
) -> Result<VolatileSlice<'_, MS<'_, Self>>>
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 Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Bytes<GuestAddress> for Twhere
T: GuestMemory + ?Sized,
impl<T> Bytes<GuestAddress> for Twhere
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>
addr
. Read moresourcefn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize, Error>
fn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize, Error>
addr
into a slice. Read more