Skip to content

Commit e2aa526

Browse files
author
Samuel Ortiz
committed
Define the device IO trait
Any device that supports memory based I/O needs to implement that trait in order for the VMM to be able to properly dispatch VM exits to the address ranges said device handles. Signed-off-by: Samuel Ortiz <[email protected]>
1 parent 23a6a65 commit e2aa526

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ repository = "https://github.com/rust-vmm/vm-device"
66
license = "Apache-2.0"
77

88
[dependencies]
9+
vm-memory = { git = "https://github.com/rust-vmm/vm-memory" }

src/lib.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
1+
// Copyright © 2019 Intel Corporation. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
3+
4+
//! rust-vmm device model.
5+
6+
extern crate vm_memory;
7+
8+
use vm_memory::GuestAddress;
9+
10+
/// IO Resource type.
11+
#[derive(Debug, Copy, Clone)]
12+
pub enum IoType {
13+
/// Port I/O type.
14+
Pio,
15+
16+
/// Memory mapped I/O type.
17+
Mmio,
18+
}
19+
20+
/// Device IO trait.
21+
/// A device supporting memory based I/O should implement this trait, then
22+
/// register itself against the different IO type ranges it handles.
23+
/// The VMM will then dispatch IO (PIO or MMIO) VM exits by calling into the
24+
/// registered devices read or write method from this trait.
25+
#[allow(unused_variables)]
26+
pub trait DeviceIo: Send {
27+
/// Get the device name.
28+
fn name(&self) -> String;
29+
30+
/// Read from the guest physical address `addr` to `data`.
31+
fn read(&self, addr: GuestAddress, data: &mut [u8], io_type: IoType);
32+
33+
/// Write `data` to the guest physical address `addr`.
34+
fn write(&self, addr: GuestAddress, data: &[u8], io_type: IoType);
735
}

0 commit comments

Comments
 (0)