Skip to main content

GuestMemWriter

Struct GuestMemWriter 

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

Raw pointer wrapper for guest physical memory access.

Backed by the VM-lifetime mmap of guest RAM. Device-owned descriptor buffers are exclusive to the device per the VirtIO spec — the guest will not touch them until the used ring advances.

All public methods accept guest physical addresses (GPAs) and translate them to slice offsets by subtracting gpa_base.

Implementations§

Source§

impl GuestMemWriter

Source

pub unsafe fn new(ptr: *mut u8, len: usize, gpa_base: usize) -> GuestMemWriter

Creates a new writer from the DeviceManager’s guest memory.

ptr must point to the host mapping of guest RAM, which starts at GPA gpa_base. len is the size of that mapping in bytes.

§Safety

ptr must be valid for len bytes for the lifetime of the VM.

Source

pub fn gpa_to_offset(&self, gpa: usize, access_len: usize) -> Option<usize>

Translates a GPA to a host pointer offset, returning None if the GPA falls below gpa_base (invalid) or the range exceeds the mapped region.

Source

pub unsafe fn slice_mut(&self, gpa: usize, len: usize) -> Option<&mut [u8]>

Returns a mutable slice into guest memory at the given GPA range.

§Safety

Caller must ensure no other reference (mutable or shared) to the same GPA range exists for the lifetime of the returned slice. In practice, VirtIO descriptor ownership guarantees this — each descriptor buffer is exclusive to the device that owns it.

Source

pub fn slice(&self, gpa: usize, len: usize) -> Option<&[u8]>

Returns an immutable slice into guest memory at the given GPA range.

Source

pub fn read_u16(&self, gpa: usize) -> u16

Reads a little-endian u16 from the given GPA. Returns 0 on out-of-bounds access.

Source

pub fn write_u16(&self, gpa: usize, val: u16)

Writes a little-endian u16 to the given GPA. No-op on out-of-bounds access.

Source

pub fn write_u32(&self, gpa: usize, val: u32)

Writes a little-endian u32 to the given GPA. No-op on out-of-bounds access.

Source

pub fn write_byte(&self, gpa: usize, val: u8)

Writes a single byte to the given GPA. No-op on out-of-bounds access.

Source

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

Returns the raw pointer to the start of guest memory.

Source

pub fn len(&self) -> usize

Returns the total length (in bytes) of the guest memory region.

Source

pub fn is_empty(&self) -> bool

Returns true if the guest memory region has zero length.

Source

pub fn gpa_base(&self) -> usize

Returns the GPA base address of the guest memory region.

Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more