Struct vm_memory::mmap::GuestRegionMmap

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

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

Represents a continuous region of the guest’s physical memory that is backed by a mapping in the virtual address space of the calling process.

Implementations§

source§

impl<B: Bitmap> GuestRegionMmap<B>

source

pub fn new( mapping: MmapRegion<B>, guest_base: GuestAddress, ) -> Result<Self, Error>

Create a new memory-mapped memory region for the guest’s physical memory.

Methods from Deref<Target = MmapRegion<B>>§

source

pub fn as_ptr(&self) -> *mut u8

Returns a pointer to the beginning of the memory region. Mutable accesses performed using the resulting pointer are not automatically accounted for by the dirty bitmap tracking functionality.

Should only be used for passing this region to ioctls for setting guest memory.

source

pub fn size(&self) -> usize

Returns the size of this region.

source

pub fn file_offset(&self) -> Option<&FileOffset>

Returns information regarding the offset into the file backing this region (if any).

source

pub fn prot(&self) -> i32

Returns the value of the prot parameter passed to mmap when mapping this region.

source

pub fn flags(&self) -> i32

Returns the value of the flags parameter passed to mmap when mapping this region.

source

pub fn owned(&self) -> bool

Returns true if the mapping is owned by this MmapRegion instance.

source

pub fn fds_overlap<T: Bitmap>(&self, other: &MmapRegion<T>) -> bool

Checks whether this region and other are backed by overlapping FileOffset objects.

This is mostly a sanity check available for convenience, as different file descriptors can alias the same file.

source

pub fn is_hugetlbfs(&self) -> Option<bool>

Returns true if the region is hugetlbfs

source

pub fn bitmap(&self) -> &B

Returns a reference to the inner bitmap object.

Trait Implementations§

source§

impl<B: Bitmap> Bytes<MemoryRegionAddress> for GuestRegionMmap<B>

source§

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

§Examples
  • Write a slice at guest address 0x1200.
let res = gm
    .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
    .expect("Could not write to guest memory");
assert_eq!(5, res);
source§

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

§Examples
  • Read a slice of length 16 at guestaddress 0x1200.
let buf = &mut [0u8; 16];
let res = gm
    .read(buf, GuestAddress(0x1200))
    .expect("Could not read from guest memory");
assert_eq!(16, res);
source§

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

§Examples
  • Read bytes from /dev/urandom
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 read_exact_from<F>( &self, addr: MemoryRegionAddress, src: &mut F, count: usize, ) -> Result<()>
where F: Read,

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

gm.read_exact_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: MemoryRegionAddress, dst: &mut F, count: usize, ) -> Result<usize>
where F: Write,

Writes data from the region to a writable object.

§Examples
  • Write 128 bytes to a /dev/null file
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 to file from guest memory");
source§

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

Writes data from the region to a writable object.

§Examples
  • Write 128 bytes to a /dev/null file
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 to file from guest memory");
§

type E = Error

Associated error codes
source§

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

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

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

Reads data from the container at addr to fill an entire slice. Read more
source§

fn store<T: AtomicAccess>( &self, val: T, addr: MemoryRegionAddress, order: Ordering, ) -> Result<()>

Atomically store a value at the specified address.
source§

fn load<T: AtomicAccess>( &self, addr: MemoryRegionAddress, order: Ordering, ) -> Result<T>

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

source§

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

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

impl<B> Deref for GuestRegionMmap<B>

§

type Target = MmapRegion<B>

The resulting type after dereferencing.
source§

fn deref(&self) -> &MmapRegion<B>

Dereferences the value.
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.
source§

impl<B: Bitmap> GuestMemoryRegion for GuestRegionMmap<B>

§

type B = B

Type used for dirty memory tracking.
source§

fn len(&self) -> GuestUsize

Returns the size of the region.
source§

fn start_addr(&self) -> GuestAddress

Returns the minimum (inclusive) address managed by the region.
source§

fn bitmap(&self) -> &Self::B

Borrow the associated Bitmap object.
source§

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

Returns the host virtual address corresponding to the region address. Read more
source§

fn file_offset(&self) -> Option<&FileOffset>

Returns information regarding the file and offset backing this memory region.
source§

unsafe fn as_slice(&self) -> Option<&[u8]>

Returns a slice corresponding to the data in the region. Read more
source§

unsafe fn as_mut_slice(&self) -> Option<&mut [u8]>

Returns a mutable slice corresponding to the data in the region. Read more
source§

fn get_slice( &self, offset: MemoryRegionAddress, count: usize, ) -> Result<VolatileSlice<'_, BS<'_, B>>>

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

fn is_hugetlbfs(&self) -> Option<bool>

Show if the region is based on the HugeTLBFS. Returns Some(true) if the region is backed by hugetlbfs. None represents that no information is available. Read more
source§

fn last_addr(&self) -> GuestAddress

Returns the maximum (inclusive) address managed by the region.
source§

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

Returns the given address if it is within this region.
source§

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

Returns true if the given address is within this region.
source§

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

Returns the address plus the offset if it is in this region.
source§

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

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

fn as_volatile_slice(&self) -> Result<VolatileSlice<'_, BS<'_, Self::B>>>

Gets a slice of memory for the entire region that supports volatile access. Read more

Auto Trait Implementations§

§

impl<B> Freeze for GuestRegionMmap<B>
where B: Freeze,

§

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

§

impl<B> Send for GuestRegionMmap<B>
where B: Send,

§

impl<B> Sync for GuestRegionMmap<B>
where B: Sync,

§

impl<B> Unpin for GuestRegionMmap<B>
where B: Unpin,

§

impl<B> UnwindSafe for GuestRegionMmap<B>
where B: UnwindSafe,

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