Skip to main content

virtio_accel_shared_queue/
memory.rs

1//! This side's access to the memory the two sides share.
2
3/// Why an address did not name memory this side may touch that way.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum MemoryFault {
6    /// Some byte of the range is not shared, or not for the access asked.
7    Range,
8    /// A word access named an address that is not a multiple of its size.
9    Alignment,
10}
11
12/// What an access does to the bytes it names.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum Access {
15    /// The bytes are read.
16    Read,
17    /// The bytes are written.
18    Write,
19}
20
21/// The memory a driver and a device share, as one side reaches it.
22///
23/// Addresses are **device addresses**: the numbers a driver writes into a
24/// descriptor and into the three ring-address registers. Each side maps them
25/// its own way — an offset into a window the driver lent, a guest-physical
26/// address, an I/O virtual address resolved through the driver's own mapping
27/// of its DMA memory — and that mapping, with whatever `unsafe` it needs, is
28/// the implementation's. Nothing in this crate forms a reference into the
29/// memory or holds an address across a call.
30///
31/// # What an implementation promises
32///
33/// - A range it does not [`admit`](Self::admits) is refused by every access,
34///   with nothing read and nothing written.
35/// - The other side may write any byte at any moment. [`read`](Self::read)
36///   returns a snapshot; it need not be coherent across bytes, and it must
37///   not be undefined behaviour for it to race a write.
38/// - [`load_u16`](Self::load_u16) and [`store_u16`](Self::store_u16) are the
39///   queue's publication points. A store has release ordering: every write
40///   this side made before it is visible to the other side once it has loaded
41///   the word. A load has acquire ordering. Words are little-endian whatever
42///   the host is.
43pub trait SharedMemory {
44    /// Whether `len` bytes at `at` may be accessed as `access` asks. A range
45    /// that wraps the address space is never admitted; an empty one is.
46    fn admits(&self, at: u64, len: u64, access: Access) -> bool;
47
48    /// Snapshot `target.len()` bytes at `at`.
49    fn read(&self, at: u64, target: &mut [u8]) -> Result<(), MemoryFault>;
50
51    /// Store `source` at `at`.
52    fn write(&self, at: u64, source: &[u8]) -> Result<(), MemoryFault>;
53
54    /// Load the little-endian `u16` at `at`, acquire.
55    fn load_u16(&self, at: u64) -> Result<u16, MemoryFault>;
56
57    /// Store a little-endian `u16` at `at`, release.
58    fn store_u16(&self, at: u64, value: u16) -> Result<(), MemoryFault>;
59}
60
61impl<T: SharedMemory + ?Sized> SharedMemory for &T {
62    fn admits(&self, at: u64, len: u64, access: Access) -> bool {
63        (**self).admits(at, len, access)
64    }
65
66    fn read(&self, at: u64, target: &mut [u8]) -> Result<(), MemoryFault> {
67        (**self).read(at, target)
68    }
69
70    fn write(&self, at: u64, source: &[u8]) -> Result<(), MemoryFault> {
71        (**self).write(at, source)
72    }
73
74    fn load_u16(&self, at: u64) -> Result<u16, MemoryFault> {
75        (**self).load_u16(at)
76    }
77
78    fn store_u16(&self, at: u64, value: u16) -> Result<(), MemoryFault> {
79        (**self).store_u16(at, value)
80    }
81}