From fd3f2b62eb9dd3181fdecc5702fd8c2af39fdd29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Mon, 29 Apr 2024 17:56:00 +0200 Subject: [PATCH] feat(virtio-def): add `zerocopy` support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- Cargo.lock | 2 ++ virtio-def/Cargo.toml | 5 +++++ virtio-def/src/lib.rs | 41 ++++++++++++++++++++++++++--------------- virtio-def/src/num.rs | 1 + virtio-def/src/pci.rs | 4 ++++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bb953b58e..3a366e438f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1442,6 +1442,8 @@ version = "0.0.0" dependencies = [ "bitflags 2.5.0", "volatile 0.5.4", + "zerocopy", + "zerocopy-derive", ] [[package]] diff --git a/virtio-def/Cargo.toml b/virtio-def/Cargo.toml index 2413fa748d..399ce5426a 100644 --- a/virtio-def/Cargo.toml +++ b/virtio-def/Cargo.toml @@ -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"] diff --git a/virtio-def/src/lib.rs b/virtio-def/src/lib.rs index 03cd3a0ab9..944690c789 100644 --- a/virtio-def/src/lib.rs +++ b/virtio-def/src/lib.rs @@ -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; diff --git a/virtio-def/src/num.rs b/virtio-def/src/num.rs index bf31c95cba..f2c0dd0475 100644 --- a/virtio-def/src/num.rs +++ b/virtio-def/src/num.rs @@ -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); diff --git a/virtio-def/src/pci.rs b/virtio-def/src/pci.rs index f348377778..e426892513 100644 --- a/virtio-def/src/pci.rs +++ b/virtio-def/src/pci.rs @@ -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)]