Skip to main content

GuestMemoryAccessor

Trait GuestMemoryAccessor 

Source
pub trait GuestMemoryAccessor {
    // Required method
    fn translate_and_get_limit(
        &self,
        guest_addr: GuestPhysAddr,
    ) -> Option<(PhysAddr, usize)>;

    // Provided methods
    fn read_obj<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V> { ... }
    fn write_obj<V: Copy>(
        &self,
        guest_addr: GuestPhysAddr,
        val: V,
    ) -> AxResult<()> { ... }
    fn read_buffer(
        &self,
        guest_addr: GuestPhysAddr,
        buffer: &mut [u8],
    ) -> AxResult<()> { ... }
    fn write_buffer(
        &self,
        guest_addr: GuestPhysAddr,
        buffer: &[u8],
    ) -> AxResult<()> { ... }
    fn read_volatile<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V> { ... }
    fn write_volatile<V: Copy>(
        &self,
        guest_addr: GuestPhysAddr,
        val: V,
    ) -> AxResult<()> { ... }
}
Expand description

A stateful accessor to the memory space of a guest

Required Methods§

Source

fn translate_and_get_limit( &self, guest_addr: GuestPhysAddr, ) -> Option<(PhysAddr, usize)>

Translate a guest physical address to host physical address and get access limit

Returns a tuple of (host_physical_address, accessible_size) if the translation is successful. The accessible_size indicates how many bytes can be safely accessed starting from the given guest address.

Provided Methods§

Source

fn read_obj<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V>

Read a value of type V from guest memory

§Returns

Returns Err(AxError::InvalidInput) in the following cases:

  • The guest address cannot be translated to a valid host address
  • The accessible memory region starting from the guest address is smaller than the size of type V (insufficient space for the read operation)
§Safety

This function uses volatile memory access to ensure the read operation is not optimized away by the compiler, which is important for device register access and shared memory scenarios.

Source

fn write_obj<V: Copy>(&self, guest_addr: GuestPhysAddr, val: V) -> AxResult<()>

Write a value of type V to guest memory

§Returns

Returns Err(AxError::InvalidInput) in the following cases:

  • The guest address cannot be translated to a valid host address
  • The accessible memory region starting from the guest address is smaller than the size of type V (insufficient space for the write operation)
§Safety

This function uses volatile memory access to ensure the write operation is not optimized away by the compiler, which is important for device register access and shared memory scenarios.

Source

fn read_buffer( &self, guest_addr: GuestPhysAddr, buffer: &mut [u8], ) -> AxResult<()>

Read a buffer from guest memory

Source

fn write_buffer(&self, guest_addr: GuestPhysAddr, buffer: &[u8]) -> AxResult<()>

Write a buffer to guest memory

Source

fn read_volatile<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V>

Read a volatile value from guest memory (for device registers)

Source

fn write_volatile<V: Copy>( &self, guest_addr: GuestPhysAddr, val: V, ) -> AxResult<()>

Write a volatile value to guest memory (for device registers)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§