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
// Copyright © 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//! This crate provides:
//! * device traits defining read and write operations on specialized buses
//! * device manager (bus-specific traits and a concrete implementation) for
//! operating devices and dispatching I/O
//! * abstractions for defining resources and their constraints (e.g. a specific bus
//! address range, IRQ number, etc)
//!
//! [`MutDevicePio`] and [`MutDeviceMmio`] traits help with composite inner mutability
//! (i.e. if we have a `Mutex` that holds a `T` which implements [`MutDevicePio`],
//! then the `Mutex` can implement [`DevicePio`] based on its inner
//! mutability properties).
//!
//! # Example
//!
//! Implement a simple log PIO device, register it with
//! [`IoManager`](device_manager/struct.IoManager.html)
//! and dispatch a write operation to the device.
//!```
//! use std::sync::{Arc, Mutex};
//! use vm_device::bus::{PioAddress, PioAddressOffset, PioRange};
//! use vm_device::device_manager::{IoManager, PioManager};
//! use vm_device::MutDevicePio;
//!
//! struct LogDevice {}
//!
//! impl MutDevicePio for LogDevice {
//! fn pio_read(&mut self, base: PioAddress, offset: PioAddressOffset, _data: &mut [u8]) {
//! println!("mut pio_read: base {:?}, offset {}", base, offset);
//! }
//! fn pio_write(&mut self, base: PioAddress, offset: PioAddressOffset, data: &[u8]) {
//! println!(
//! "mut pio_write: base {:?}, offset {}, data {:?}",
//! base, offset, data
//! );
//! }
//! }
//!
//! // IoManager implements PioManager trait.
//! let mut manager = IoManager::new();
//! let device = LogDevice {};
//! let bus_range = PioRange::new(PioAddress(0), 10).unwrap();
//! manager
//! .register_pio(bus_range, Arc::new(Mutex::new(device)))
//! .unwrap();
//! manager.pio_write(PioAddress(0), &vec![b'o', b'k']).unwrap();
//! ```
use Deref;
use ;
use ;
/// Allows a device to be attached to a
/// [PIO](https://en.wikipedia.org/wiki/Programmed_input%E2%80%93output) bus.
///
/// # Example
/// ```
/// # use std::sync::Mutex;
/// # use vm_device::{DevicePio, bus::{PioAddress, PioAddressOffset}};
/// struct DummyDevice {
/// config: Mutex<u32>,
/// }
///
/// impl DevicePio for DummyDevice {
/// fn pio_read(&self, _base: PioAddress, _offset: PioAddressOffset, data: &mut [u8]) {
/// if data.len() > 4 {
/// return;
/// }
/// for (idx, iter) in data.iter_mut().enumerate() {
/// let config = self.config.lock().expect("failed to acquire lock");
/// *iter = (*config >> (idx * 8) & 0xff) as u8;
/// }
/// }
///
/// fn pio_write(&self, _base: PioAddress, _offset: PioAddressOffset, data: &[u8]) {
/// let mut config = self.config.lock().expect("failed to acquire lock");
/// *config = u32::from(data[0]) & 0xff;
/// }
/// }
/// ```
/// Allows a device to be attached to a
/// [MMIO](https://en.wikipedia.org/wiki/Memory-mapped_I/O) bus.
///
/// # Example
/// ```
/// # use std::sync::Mutex;
/// # use vm_device::{DeviceMmio, bus::{MmioAddress, MmioAddressOffset}};
/// struct DummyDevice {
/// config: Mutex<u32>,
/// }
///
/// impl DeviceMmio for DummyDevice {
/// fn mmio_read(&self, _base: MmioAddress, _offset: MmioAddressOffset, data: &mut [u8]) {
/// if data.len() > 4 {
/// return;
/// }
/// for (idx, iter) in data.iter_mut().enumerate() {
/// let config = self.config.lock().expect("failed to acquire lock");
/// *iter = (*config >> (idx * 8) & 0xff) as u8;
/// }
/// }
///
/// fn mmio_write(&self, _base: MmioAddress, _offset: MmioAddressOffset, data: &[u8]) {
/// let mut config = self.config.lock().expect("failed to acquire lock");
/// *config = u32::from(data[0]) & 0xff;
/// }
/// }
/// ```
/// Same as [DevicePio] but the methods are invoked with a mutable self borrow.
///
/// # Example
/// ```
/// # use vm_device::{MutDevicePio, bus::{PioAddress, PioAddressOffset}};
/// struct DummyDevice {
/// config: u32,
/// }
///
/// impl MutDevicePio for DummyDevice {
/// fn pio_read(&mut self, _base: PioAddress, _offset: PioAddressOffset, data: &mut [u8]) {
/// if data.len() > 4 {
/// return;
/// }
/// for (idx, iter) in data.iter_mut().enumerate() {
/// *iter = (self.config >> (idx * 8) & 0xff) as u8;
/// }
/// }
///
/// fn pio_write(&mut self, _base: PioAddress, _offset: PioAddressOffset, data: &[u8]) {
/// self.config = u32::from(data[0]) & 0xff;
/// }
/// }
/// ```
/// Same as [DeviceMmio] but the methods are invoked with a mutable self borrow.
/// # Example
/// ```
/// # use vm_device::{MutDeviceMmio, bus::{MmioAddress, MmioAddressOffset}};
/// struct DummyDevice {
/// config: u32,
/// }
///
/// impl MutDeviceMmio for DummyDevice {
/// fn mmio_read(&mut self, _base: MmioAddress, _offset: MmioAddressOffset, data: &mut [u8]) {
/// if data.len() > 4 {
/// return;
/// }
/// for (idx, iter) in data.iter_mut().enumerate() {
/// *iter = (self.config >> (idx * 8) & 0xff) as u8;
/// }
/// }
///
/// fn mmio_write(&mut self, _base: MmioAddress, _offset: MmioAddressOffset, data: &[u8]) {
/// self.config = u32::from(data[0]) & 0xff;
/// }
/// }
/// ```
// Blanket implementations for Arc<T>.
// Blanket implementations for Mutex<T>.