-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmod.rs
More file actions
262 lines (216 loc) · 7.72 KB
/
mod.rs
File metadata and controls
262 lines (216 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Virtqueue definitions
#[cfg(feature = "alloc")]
mod alloc;
use core::alloc::Layout;
use core::ptr::{NonNull, addr_of_mut};
use core::{mem, ptr};
use crate::{le16, le32, le64};
/// Split Virtqueue Descriptor
#[doc(alias = "virtq_desc")]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Desc {
/// Address (guest-physical).
pub addr: le64,
/// Length.
pub len: le32,
/// The flags as indicated in [`DescF`].
pub flags: DescF,
/// Next field if flags & NEXT
pub next: le16,
}
endian_bitflags! {
/// Virtqueue descriptor flags
#[doc(alias = "VIRTQ_DESC_F")]
pub struct DescF: le16 {
/// This marks a buffer as continuing via the next field.
#[doc(alias = "VIRTQ_DESC_F_NEXT")]
const NEXT = 1;
/// This marks a buffer as device write-only (otherwise device read-only).
#[doc(alias = "VIRTQ_DESC_F_WRITE")]
const WRITE = 2;
/// This means the buffer contains a list of buffer descriptors.
#[doc(alias = "VIRTQ_DESC_F_INDIRECT")]
const INDIRECT = 4;
#[doc(alias = "VIRTQ_DESC_F_AVAIL")]
const AVAIL = 1 << 7;
#[doc(alias = "VIRTQ_DESC_F_USED")]
const USED = 1 << 15;
}
}
/// The Virtqueue Available Ring
#[doc(alias = "virtq_avail")]
#[derive(Debug)]
#[repr(C)]
pub struct Avail {
pub flags: AvailF,
pub idx: le16,
ring_and_used_event: [le16],
}
impl Avail {
pub fn layout(queue_size: u16, has_event_idx: bool) -> Layout {
Layout::array::<le16>(2 + usize::from(queue_size) + usize::from(has_event_idx)).unwrap()
}
pub fn from_ptr(ptr: NonNull<[u8]>) -> Option<NonNull<Self>> {
let len = ptr.as_ptr().len();
// FIXME: use ptr::as_mut_ptr once stable
// https://github.com/rust-lang/rust/issues/74265
let ptr = ptr.as_ptr() as *mut u8;
if !ptr.cast::<le16>().is_aligned() {
return None;
}
if !len.is_multiple_of(mem::size_of::<le16>()) {
return None;
}
let len = len / mem::size_of::<le16>() - 2;
let ptr = ptr::slice_from_raw_parts_mut(ptr, len) as *mut Self;
Some(NonNull::new(ptr).unwrap())
}
pub fn ring_ptr(this: NonNull<Self>, has_event_idx: bool) -> NonNull<[le16]> {
let ptr = unsafe { addr_of_mut!((*this.as_ptr()).ring_and_used_event) };
let len = if cfg!(debug_assertions) {
ptr.len()
.checked_sub(usize::from(has_event_idx))
.expect("`has_event_idx` cannot be true if it was not true at creation")
} else {
ptr.len().saturating_sub(usize::from(has_event_idx))
};
let ptr = NonNull::new(ptr).unwrap().cast::<le16>();
NonNull::slice_from_raw_parts(ptr, len)
}
pub fn ring(&self, has_event_idx: bool) -> &[le16] {
let ptr = Self::ring_ptr(NonNull::from(self), has_event_idx);
unsafe { ptr.as_ref() }
}
pub fn ring_mut(&mut self, has_event_idx: bool) -> &mut [le16] {
let mut ptr = Self::ring_ptr(NonNull::from(self), has_event_idx);
unsafe { ptr.as_mut() }
}
pub fn used_event_ptr(this: NonNull<Self>, has_event_idx: bool) -> Option<NonNull<le16>> {
if !has_event_idx {
return None;
}
let ptr = unsafe { addr_of_mut!((*this.as_ptr()).ring_and_used_event) };
let len = ptr.len();
if len == 0 {
return None;
}
let ptr = NonNull::new(ptr).unwrap().cast::<le16>();
let ptr = unsafe { ptr.add(len - 1) };
Some(ptr)
}
pub fn used_event(&self, has_event_idx: bool) -> Option<&le16> {
Self::used_event_ptr(NonNull::from(self), has_event_idx).map(|ptr| unsafe { ptr.as_ref() })
}
pub fn used_event_mut(&mut self, has_event_idx: bool) -> Option<&mut le16> {
Self::used_event_ptr(NonNull::from(self), has_event_idx)
.map(|mut ptr| unsafe { ptr.as_mut() })
}
}
endian_bitflags! {
/// Virtqueue available ring flags
#[doc(alias = "VIRTQ_AVAIL_F")]
pub struct AvailF: le16 {
/// The driver uses this in avail->flags to advise the device: don’t
/// interrupt me when you consume a buffer. It’s unreliable, so it’s
/// simply an optimization.
#[doc(alias = "VIRTQ_AVAIL_F_NO_INTERRUPT")]
const NO_INTERRUPT = 1;
}
}
/// The Virtqueue Used Ring
#[doc(alias = "virtq_used")]
#[derive(Debug)]
#[repr(C)]
#[repr(align(4))] // mem::align_of::<UsedElem>
pub struct Used {
pub flags: UsedF,
pub idx: le16,
ring_and_avail_event: [le16],
}
impl Used {
pub fn layout(queue_size: u16, has_event_idx: bool) -> Layout {
let event_idx_layout = if has_event_idx {
Layout::new::<le16>()
} else {
Layout::new::<()>()
};
Layout::array::<le16>(2)
.unwrap()
.extend(Layout::array::<UsedElem>(queue_size.into()).unwrap())
.unwrap()
.0
.extend(event_idx_layout)
.unwrap()
.0
.pad_to_align()
}
pub fn from_ptr(ptr: NonNull<[u8]>, has_event_idx: bool) -> Option<NonNull<Self>> {
let len = ptr.len();
let ptr = ptr.cast::<u8>().as_ptr();
if !ptr.cast::<UsedElem>().is_aligned() {
return None;
}
if len % mem::size_of::<UsedElem>() != usize::from(!has_event_idx) * mem::size_of::<le32>()
{
return None;
}
let len = len / mem::size_of::<le16>() - 2 - usize::from(has_event_idx);
let ptr = ptr::slice_from_raw_parts(ptr, len) as *mut Used;
Some(NonNull::new(ptr).unwrap())
}
pub fn ring_ptr(this: NonNull<Self>) -> NonNull<[UsedElem]> {
let ptr = unsafe { addr_of_mut!((*this.as_ptr()).ring_and_avail_event) };
let len = ptr.len() * mem::size_of::<le16>() / mem::size_of::<UsedElem>();
let ptr = NonNull::new(ptr).unwrap().cast::<UsedElem>();
NonNull::slice_from_raw_parts(ptr, len)
}
pub fn ring(&self) -> &[UsedElem] {
let ptr = Self::ring_ptr(NonNull::from(self));
unsafe { ptr.as_ref() }
}
pub fn ring_mut(&mut self) -> &mut [UsedElem] {
let mut ptr = Self::ring_ptr(NonNull::from(self));
unsafe { ptr.as_mut() }
}
pub fn avail_event_ptr(this: NonNull<Self>) -> Option<NonNull<le16>> {
let ptr = unsafe { addr_of_mut!((*this.as_ptr()).ring_and_avail_event) };
if ptr.len() * mem::size_of::<le16>() % mem::size_of::<UsedElem>() != mem::size_of::<le16>()
{
return None;
}
let start = ptr as *mut le16;
let ptr = unsafe { start.add(ptr.len() - 1) };
Some(NonNull::new(ptr).unwrap())
}
pub fn avail_event(&self) -> Option<&le16> {
Self::avail_event_ptr(NonNull::from(self)).map(|ptr| unsafe { ptr.as_ref() })
}
pub fn avail_event_mut(&mut self) -> Option<&mut le16> {
Self::avail_event_ptr(NonNull::from(self)).map(|mut ptr| unsafe { ptr.as_mut() })
}
}
endian_bitflags! {
/// Virtqueue used ring flags
#[doc(alias = "VIRTQ_USED_F")]
pub struct UsedF: le16 {
/// The device uses this in used->flags to advise the driver: don’t kick me
/// when you add a buffer. It’s unreliable, so it’s simply an
/// optimization.
#[doc(alias = "VIRTQ_USED_F_NO_NOTIFY")]
const NO_NOTIFY = 1;
}
}
/// Used Ring Entry
#[doc(alias = "virtq_used_elem")]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct UsedElem {
/// Index of start of used descriptor chain.
///
/// [`le32`] is used here for ids for padding reasons.
pub id: le32,
/// The number of bytes written into the device writable portion of
/// the buffer described by the descriptor chain.
pub len: le32,
}