Skip to main content

MemOps

Trait MemOps 

Source
pub unsafe trait MemOps {
    type Error;

    // Required methods
    fn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>;
    fn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>;
    fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>;
    fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>;
    unsafe fn as_slice(
        &self,
        addr: u64,
        len: usize,
    ) -> Result<&[u8], Self::Error>;
    unsafe fn as_mut_slice(
        &self,
        addr: u64,
        len: usize,
    ) -> Result<&mut [u8], Self::Error>;

    // Provided methods
    fn read_val<T: Pod>(&self, addr: u64) -> Result<T, Self::Error> { ... }
    fn write_val<T: Pod>(&self, addr: u64, val: T) -> Result<(), Self::Error> { ... }
}
Expand description

Backend-provided memory access for virtqueue.

§Safety

Implementations must ensure that:

  • Addresses accepted by these methods are translated according to the backend’s memory model.
  • Invalid or inaccessible addresses are reported with Self::Error rather than causing undefined behavior.
  • Memory ordering guarantees are upheld as documented.
  • Typed reads/writes and atomic operations honor alignment and initialized memory requirements for the translated addresses.

Required Associated Types§

Required Methods§

Source

fn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>

Read bytes from physical memory.

Used for reading buffer contents pointed to by descriptors.

§Arguments
  • addr - Guest physical address to read from
  • dst - Destination buffer to fill

Implementations must return an error if addr cannot be read for at least dst.len() bytes.

Source

fn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>

Write bytes to physical memory.

§Arguments
  • addr - address to write to
  • src - Source data to write

Implementations must return an error if addr cannot be written for at least src.len() bytes.

Source

fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>

Load a u16 with acquire semantics.

Implementations must return an error if addr does not translate to a valid, aligned AtomicU16 in shared memory.

Source

fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>

Store a u16 with release semantics.

Implementations must return an error if addr does not translate to a valid, aligned AtomicU16 in shared memory.

Source

unsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error>

Get a direct read-only slice into shared memory.

§Safety

The caller must ensure:

  • addr is valid and points to at least len bytes.
  • The memory region is not concurrently modified for the lifetime of the returned slice. Caller must uphold this via protocol-level synchronisation, e.g. descriptor ownership transfer.

See also [BufferOwner]: super::BufferOwner

Source

unsafe fn as_mut_slice( &self, addr: u64, len: usize, ) -> Result<&mut [u8], Self::Error>

Get a direct mutable slice into shared memory.

§Safety

The caller must ensure:

  • addr is valid and points to at least len bytes.
  • No other references (shared or mutable) to this memory region exist for the lifetime of the returned slice.
  • Protocol-level synchronisation (e.g. descriptor ownership) guarantees exclusive access.

Provided Methods§

Source

fn read_val<T: Pod>(&self, addr: u64) -> Result<T, Self::Error>

Read a Pod type at the given pointer.

Implementations must return an error if addr is not valid, aligned, and initialized for T.

Source

fn write_val<T: Pod>(&self, addr: u64, val: T) -> Result<(), Self::Error>

Write a Pod type at the given pointer.

Implementations must return an error if addr is not valid and aligned for T.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: MemOps> MemOps for Arc<T>

Source§

type Error = <T as MemOps>::Error

Source§

fn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>

Source§

fn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>

Source§

fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>

Source§

fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>

Source§

unsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error>

Source§

unsafe fn as_mut_slice( &self, addr: u64, len: usize, ) -> Result<&mut [u8], Self::Error>

Implementors§