Skip to content

Commit 21910a1

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 235f476 commit 21910a1

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Cargo.toml

+1
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

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
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 Addresses.
11+
#[derive(Debug, Copy, Clone)]
12+
pub enum IoAddress {
13+
/// Port I/O address.
14+
Pio(u16),
15+
16+
/// Memory mapped I/O address.
17+
Mmio(GuestAddress),
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+
pub trait DeviceIo: Send {
26+
/// Read from the guest physical address `addr` to `data`.
27+
fn read(&mut self, addr: IoAddress, data: &mut [u8]);
28+
29+
/// Write `data` to the guest physical address `addr`.
30+
fn write(&mut self, addr: IoAddress, data: &[u8]);
731
}

0 commit comments

Comments
 (0)