Skip to main content

MappedMemory

Struct MappedMemory 

Source
pub struct MappedMemory { /* private fields */ }
Expand description

Guest RAM that is mapped into the current process, described by the same slot table KVM uses.

§Examples

use kvmtrace::memory::{GuestMemory, MappedMemory};

let mut ram = vec![0u8; 4096];
let mem = MappedMemory::new();
// SAFETY: `ram` outlives `mem` and is not resized.
unsafe { mem.set_region(0, 0x1000, ram.len() as u64, ram.as_mut_ptr() as usize) };
mem.write(0x1008, &[0xAA]).unwrap();
let mut b = [0u8; 1];
mem.read(0x1008, &mut b).unwrap();
assert_eq!(b[0], 0xAA);
assert!(mem.read(0x0, &mut b).is_err()); // not RAM

Implementations§

Source§

impl MappedMemory

Source

pub fn new() -> Self

An empty slot table.

Source

pub unsafe fn set_region(&self, slot: u32, gpa: u64, size: u64, host: usize)

Adds, replaces or (with size == 0) removes a slot, exactly like KVM_SET_USER_MEMORY_REGION does.

§Safety

host .. host + size must stay mapped and readable/writable for as long as the slot is registered.

Source

pub fn regions(&self) -> Vec<Region>

A copy of the slot table.

Trait Implementations§

Source§

impl Debug for MappedMemory

Source§

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

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

impl Default for MappedMemory

Source§

fn default() -> Self

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

impl GuestMemory for MappedMemory

Source§

fn read(&self, gpa: u64, buf: &mut [u8]) -> Result<()>

Copies buf.len() bytes starting at guest-physical address gpa.
Source§

fn write(&self, gpa: u64, data: &[u8]) -> Result<()>

Copies data to guest-physical address gpa.
Source§

fn read_u64(&self, gpa: u64) -> Result<u64>

Reads a little-endian u64.
Source§

fn read_u32(&self, gpa: u64) -> Result<u32>

Reads a little-endian u32.

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.