Skip to content

Add device IO trait #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.aarch64-unknown-linux-musl]
rustflags = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ]

3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "rust-vmm-ci"]
path = rust-vmm-ci
url = https://github.com/rust-vmm/rust-vmm-ci.git
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ repository = "https://github.com/rust-vmm/vm-device"
license = "Apache-2.0"

[dependencies]
vm-memory = { git = "https://github.com/rust-vmm/vm-memory" }
5 changes: 5 additions & 0 deletions coverage_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"coverage_score": 0,
"exclude_path": "",
"crate_features": ""
}
1 change: 1 addition & 0 deletions rust-vmm-ci
Submodule rust-vmm-ci added at bb1cd1
36 changes: 30 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
// Copyright © 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

//! rust-vmm device model.

extern crate vm_memory;

use vm_memory::GuestAddress;

/// IO Addresses.
#[derive(Debug, Copy, Clone)]
pub enum IoAddress {
/// Port I/O address.
Pio(u16),

/// Memory mapped I/O address.
Mmio(GuestAddress),
}

/// Device IO trait.
/// A device supporting memory based I/O should implement this trait, then
/// register itself against the different IO type ranges it handles.
/// The VMM will then dispatch IO (PIO or MMIO) VM exits by calling into the
/// registered devices read or write method from this trait.
pub trait DeviceIo: Send {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the option of having one trait for each type of I/O, i.e. PortIoDevice and MmioDevice traits or something along those lines. That seems a bit nicer, especially if there are only going to be two variants in the IoType enum. Also, FWIW, GuestAddresses are a bit jarring to use semantically for port IO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This design may cause some obstacles to the Bus/DeviceManager design, we need to explicitly distinguish between Arc and Arc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangliu Although I think the Device Manager could be implemented with that approach, it would make for a duplicated API: We'd need to have the same API (register_device, unregister_device, etc) duplicated for each of the variants @alexandruag is proposing. So I think the current proposal is cleaner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it clear, I agree with current design with an "io_type: IoType" parameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it clear, I agree with current design with an "io_type: IoType" parameter.

Yep. And I prefer the io_type approach as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address Andreea concern, we may enhance the io_type enum as:
enum IoType {
Pio(u16),
Mmio(GuestAddress),
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangliu You mean something like that:

/// IO Addresses.
#[derive(Debug, Copy, Clone)]
pub enum IoAddress {
    /// Port I/O address.
    Pio(u16),

    /// Memory mapped I/O address.
    Mmio(GuestAddress),
}

/// Device IO trait.
/// A device supporting memory based I/O should implement this trait, then
/// register itself against the different IO type ranges it handles.
/// The VMM will then dispatch IO (PIO or MMIO) VM exits by calling into the
/// registered devices read or write method from this trait.
pub trait DeviceIo: Send {
    /// Read from the guest physical address `addr` to `data`.
    fn read(&self, addr: IoAddress, data: &mut [u8]);

    /// Write `data` to the guest physical address `addr`.
    fn write(&self, addr: IoAddress, data: &[u8]);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly:)

/// Read from the guest physical address `addr` to `data`.
fn read(&mut self, addr: IoAddress, data: &mut [u8]);

/// Write `data` to the guest physical address `addr`.
fn write(&mut self, addr: IoAddress, data: &[u8]);
}