-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvolatile.rs
More file actions
309 lines (270 loc) · 7.53 KB
/
volatile.rs
File metadata and controls
309 lines (270 loc) · 7.53 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Volatile Pointer Types.
use core::marker::PhantomData;
use volatile::VolatilePtr;
use volatile::access::{Readable, Writable};
use crate::{DeviceStatus, Id, be32, be64, le16, le32, le64};
/// A wide volatile pointer for 64-bit fields.
///
/// In virtio, 64-bit fields are to be treated as two 32-bit fields, with low 32 bit part followed by the high 32 bit part.
/// This type mimics [`VolatilePtr`], and allows easy access to 64-bit fields.
pub struct WideVolatilePtr<'a, T, A>
where
T: ?Sized,
{
low: VolatilePtr<'a, T, A>,
high: VolatilePtr<'a, T, A>,
}
impl<'a, T, A> Copy for WideVolatilePtr<'a, T, A> where T: ?Sized {}
impl<'a, T, A> Clone for WideVolatilePtr<'a, T, A>
where
T: ?Sized,
{
fn clone(&self) -> Self {
*self
}
}
impl<'a, T, A> WideVolatilePtr<'a, T, A> {
/// Creates a new wide volatile pointer from pointers to the low and to the high part.
pub fn from_low_high(low: VolatilePtr<'a, T, A>, high: VolatilePtr<'a, T, A>) -> Self {
Self { low, high }
}
}
impl<'a, A> WideVolatilePtr<'a, le32, A> {
/// Performs a volatile read of the contained value.
///
/// See [`VolatilePtr::read`].
pub fn read(self) -> le64
where
A: Readable,
{
let low = self.low.read();
let high = self.high.read();
le64::from([low, high])
}
/// Performs a volatile write, setting the contained value to the given `value`.
///
/// See [`VolatilePtr::write`].
pub fn write(self, value: le64)
where
A: Writable,
{
let [low, high] = value.into();
self.low.write(low);
self.high.write(high);
}
/// Updates the contained value using the given closure and volatile instructions.
///
/// See [`VolatilePtr::update`].
pub fn update(self, f: impl FnOnce(le64) -> le64)
where
A: Readable + Writable,
{
let new = f(self.read());
self.write(new);
}
}
impl<'a, A> WideVolatilePtr<'a, be32, A> {
/// Performs a volatile read of the contained value.
///
/// See [`VolatilePtr::read`].
pub fn read(self) -> be64
where
A: Readable,
{
let low = self.low.read();
let high = self.high.read();
be64::from([low, high])
}
/// Performs a volatile write, setting the contained value to the given `value`.
///
/// See [`VolatilePtr::write`].
pub fn write(self, value: be64)
where
A: Writable,
{
let [low, high] = value.into();
self.low.write(low);
self.high.write(high);
}
/// Updates the contained value using the given closure and volatile instructions.
///
/// See [`VolatilePtr::update`].
pub fn update(self, f: impl FnOnce(be64) -> be64)
where
A: Readable + Writable,
{
let new = f(self.read());
self.write(new);
}
}
#[cfg(any(feature = "mmio", feature = "pci"))]
macro_rules! impl_wide_field_access {
(
$(#[$outer:meta])*
$vis:vis trait $Trait:ident<'a, A>: $T:ty {
$(
$(#[doc = $doc:literal])*
$(#[doc(alias = $alias:literal)])?
#[access($Access:ty)]
$field:ident: $field_low:ident, $field_high:ident;
)*
}
) => {
$(#[$outer])*
$vis trait $Trait<'a, A> {
$(
$(#[doc = $doc])*
$(#[doc(alias = $alias)])?
fn $field(self) -> WideVolatilePtr<'a, le32, A::Restricted>
where
A: RestrictAccess<$Access>;
)*
}
impl<'a, A> $Trait<'a, A> for VolatilePtr<'a, $T, A> {
$(
fn $field(self) -> WideVolatilePtr<'a, le32, A::Restricted>
where
A: RestrictAccess<$Access>,
{
WideVolatilePtr::from_low_high(self.$field_low(), self.$field_high())
}
)*
}
};
}
/// An overaligned volatile pointer for fields that require wider access operations.
///
/// In virtio, some fields require wider access operations than their type indicate, such as for [`mmio::DeviceRegisters`].
///
/// [`mmio::DeviceRegisters`]: crate::mmio::DeviceRegisters
pub struct OveralignedVolatilePtr<'a, T, F, A>
where
T: ?Sized,
F: ?Sized,
{
ptr: VolatilePtr<'a, F, A>,
ty: PhantomData<VolatilePtr<'a, T, A>>,
}
impl<'a, T, F, A> Copy for OveralignedVolatilePtr<'a, T, F, A>
where
T: ?Sized,
F: ?Sized,
{
}
impl<'a, T, F, A> Clone for OveralignedVolatilePtr<'a, T, F, A>
where
T: ?Sized,
F: ?Sized,
{
fn clone(&self) -> Self {
*self
}
}
impl<'a, T, F, A> OveralignedVolatilePtr<'a, T, F, A>
where
T: OveralignedField<F>,
F: Copy,
{
/// Creates a new overaligned volatile pointer.
pub fn new(ptr: VolatilePtr<'a, F, A>) -> Self {
Self {
ptr,
ty: PhantomData,
}
}
/// Performs a volatile read of the contained value.
///
/// See [`VolatilePtr::read`].
pub fn read(self) -> T
where
A: Readable,
{
T::from_field(self.ptr.read())
}
/// Performs a volatile write, setting the contained value to the given `value`.
///
/// See [`VolatilePtr::write`].
pub fn write(self, value: T)
where
A: Writable,
{
self.ptr.write(value.into_field())
}
/// Updates the contained value using the given closure and volatile instructions.
///
/// See [`VolatilePtr::update`].
pub fn update(self, f: impl FnOnce(T) -> T)
where
A: Readable + Writable,
{
let new = f(self.read());
self.write(new);
}
}
/// A trait for fields that can be accessed via [`OveralignedVolatilePtr`].
pub trait OveralignedField<F>: private::Sealed<F> {
/// Converts to this type from the overaligned field.
fn from_field(field: F) -> Self;
/// Converts this type into the overaligned field.
fn into_field(self) -> F;
}
impl OveralignedField<le32> for le16 {
fn from_field(field: le32) -> Self {
field.try_into().unwrap()
}
fn into_field(self) -> le32 {
self.into()
}
}
impl OveralignedField<le32> for bool {
fn from_field(field: le32) -> Self {
field.to_ne() == 1
}
fn into_field(self) -> le32 {
le32::from_ne(self as u32)
}
}
impl OveralignedField<le32> for u8 {
fn from_field(field: le32) -> Self {
field.to_ne().try_into().unwrap()
}
fn into_field(self) -> le32 {
le32::from_ne(self.into())
}
}
impl OveralignedField<le32> for Id {
fn from_field(field: le32) -> Self {
Self::from(u8::from_field(field))
}
fn into_field(self) -> le32 {
u8::from(self).into_field()
}
}
impl OveralignedField<le32> for DeviceStatus {
fn from_field(field: le32) -> Self {
Self::from_bits_retain(u8::from_field(field))
}
fn into_field(self) -> le32 {
self.bits().into_field()
}
}
#[cfg(feature = "mmio")]
impl OveralignedField<le32> for crate::mmio::InterruptStatus {
fn from_field(field: le32) -> Self {
Self::from_bits_retain(u8::from_field(field))
}
fn into_field(self) -> le32 {
self.bits().into_field()
}
}
mod private {
use crate::{DeviceStatus, Id, le16, le32};
pub trait Sealed<T> {}
impl Sealed<le32> for bool {}
impl Sealed<le32> for u8 {}
impl Sealed<le32> for le16 {}
impl Sealed<le32> for Id {}
impl Sealed<le32> for DeviceStatus {}
#[cfg(feature = "mmio")]
impl Sealed<le32> for crate::mmio::InterruptStatus {}
}