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§
Sourcefn translate_and_get_limit(
&self,
guest_addr: GuestPhysAddr,
) -> Option<(PhysAddr, usize)>
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§
Sourcefn read_obj<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V>
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.
Sourcefn write_obj<V: Copy>(&self, guest_addr: GuestPhysAddr, val: V) -> AxResult<()>
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.
Sourcefn read_buffer(
&self,
guest_addr: GuestPhysAddr,
buffer: &mut [u8],
) -> AxResult<()>
fn read_buffer( &self, guest_addr: GuestPhysAddr, buffer: &mut [u8], ) -> AxResult<()>
Read a buffer from guest memory
Sourcefn write_buffer(&self, guest_addr: GuestPhysAddr, buffer: &[u8]) -> AxResult<()>
fn write_buffer(&self, guest_addr: GuestPhysAddr, buffer: &[u8]) -> AxResult<()>
Write a buffer to guest memory
Sourcefn read_volatile<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V>
fn read_volatile<V: Copy>(&self, guest_addr: GuestPhysAddr) -> AxResult<V>
Read a volatile value from guest memory (for device registers)
Sourcefn write_volatile<V: Copy>(
&self,
guest_addr: GuestPhysAddr,
val: V,
) -> AxResult<()>
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.