Skip to content

Commit

Permalink
feat(virtio-def): add zerocopy support
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Apr 29, 2024
1 parent 4a323da commit fd3f2b6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions virtio-def/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ categories = ["no-std", "no-std::no-alloc"]
[dependencies]
bitflags = "2"
volatile = { version = "0.5.3", features = ["derive"] }
zerocopy = { version = "0.7", optional = true, default-features = false }
zerocopy-derive = { version = "0.7", optional = true }

[features]
zerocopy = ["dep:zerocopy", "dep:zerocopy-derive"]
41 changes: 26 additions & 15 deletions virtio-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,33 @@ pub mod pci;

use bitflags::bitflags;

/// Device Status Field
///
/// During device initialization by a driver,
/// the driver follows the sequence of steps specified in
/// _General Initialization And Device Operation / Device
/// Initialization_.
///
/// The `device status` field provides a simple low-level
/// indication of the completed steps of this sequence.
/// It's most useful to imagine it hooked up to traffic
/// lights on the console indicating the status of each device. The
/// following bits are defined (listed below in the order in which
/// they would be typically set):
#[cfg_attr(
feature = "zerocopy",
derive(
zerocopy_derive::FromZeroes,
zerocopy_derive::FromBytes,
zerocopy_derive::AsBytes
)
)]
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct DeviceStatus(u8);

bitflags! {
/// Device Status Field
///
/// During device initialization by a driver,
/// the driver follows the sequence of steps specified in
/// _General Initialization And Device Operation / Device
/// Initialization_.
///
/// The `device status` field provides a simple low-level
/// indication of the completed steps of this sequence.
/// It's most useful to imagine it hooked up to traffic
/// lights on the console indicating the status of each device. The
/// following bits are defined (listed below in the order in which
/// they would be typically set):
#[derive(Clone, Copy, Debug)]
pub struct DeviceStatus: u8 {
impl DeviceStatus: u8 {
/// Indicates that the guest OS has found the
/// device and recognized it as a valid virtio device.
const ACKNOWLEDGE = 1;
Expand Down
1 change: 1 addition & 0 deletions virtio-def/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ macro_rules! le_impl {
#[doc = concat!("A ", stringify!($bits), "-bit unsigned integer stored in ", $order, " byte order.")]
#[allow(non_camel_case_types)]
#[must_use]
#[cfg_attr(feature = "zerocopy", derive(zerocopy_derive::FromZeroes, zerocopy_derive::FromBytes, zerocopy_derive::AsBytes))]
#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct $SelfT($ActualT);
Expand Down
4 changes: 4 additions & 0 deletions virtio-def/src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use crate::DeviceStatus;
///
/// The common configuration structure is found at the bar and offset within the [`VIRTIO_PCI_CAP_COMMON_CFG`] capability.
#[doc(alias = "virtio_pci_common_cfg")]
#[cfg_attr(
feature = "zerocopy",
derive(zerocopy_derive::FromZeroes, zerocopy_derive::FromBytes)
)]
#[derive(VolatileFieldAccess)]
#[allow(non_camel_case_types)]
#[repr(C)]
Expand Down

0 comments on commit fd3f2b6

Please sign in to comment.