-
Notifications
You must be signed in to change notification settings - Fork 158
virtio: switch to bitfield_struct #2207
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
Open
Brian-Perkins
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Brian-Perkins:virtio_bitfield_struct
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,13 @@ | |
|
|
||
| //! Constants defined by the virtio spec | ||
|
|
||
| use bitfield_struct::bitfield; | ||
| use inspect::Inspect; | ||
| use zerocopy::FromBytes; | ||
| use zerocopy::Immutable; | ||
| use zerocopy::IntoBytes; | ||
| use zerocopy::KnownLayout; | ||
|
|
||
| pub use packed_nums::*; | ||
|
|
||
| #[expect(non_camel_case_types)] | ||
|
|
@@ -12,19 +19,108 @@ mod packed_nums { | |
| pub type u64_le = zerocopy::U64<zerocopy::LittleEndian>; | ||
| } | ||
|
|
||
| // Device features - first bank | ||
| pub const VIRTIO_F_RING_INDIRECT_DESC: u32 = 0x10000000; | ||
| pub const VIRTIO_F_RING_EVENT_IDX: u32 = 0x20000000; | ||
| // Device features - second bank | ||
| pub const VIRTIO_F_VERSION_1: u32 = 1; | ||
|
|
||
| // Device status | ||
| pub const VIRTIO_ACKNOWLEDGE: u32 = 1; | ||
| pub const VIRTIO_DRIVER: u32 = 2; | ||
| pub const VIRTIO_DRIVER_OK: u32 = 4; | ||
| pub const VIRTIO_FEATURES_OK: u32 = 8; | ||
| // const VIRTIO_DEVICE_NEEDS_RESET: u32 = 0x40; | ||
| pub const VIRTIO_FAILED: u32 = 0x80; | ||
| #[bitfield(u32)] | ||
| #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)] | ||
| pub struct VirtioDeviceFeaturesBank0 { | ||
| #[bits(24)] | ||
| pub device_specific: u32, | ||
| #[bits(4)] | ||
| _reserved1: u8, | ||
| pub ring_indirect_desc: bool, // VIRTIO_F_INDIRECT_DESC | ||
| pub ring_event_idx: bool, // VIRTIO_F_EVENT_IDX | ||
| #[bits(2)] | ||
| _reserved2: u8, | ||
| } | ||
|
|
||
| #[bitfield(u32)] | ||
| #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)] | ||
| pub struct VirtioDeviceFeaturesBank1 { | ||
| pub version_1: bool, // VIRTIO_F_VERSION_1 | ||
| pub access_platform: bool, // VIRTIO_F_ACCESS_PLATFORM | ||
| pub ring_packed: bool, // VIRTIO_F_RING_PACKED | ||
| pub in_order: bool, // VIRTIO_F_IN_ORDER | ||
| pub order_platform: bool, // VIRTIO_F_ORDER_PLATFORM | ||
| pub sriov: bool, // VIRTIO_F_SR_IOV | ||
| pub notification_data: bool, // VIRTIO_F_NOTIFICATION_DATA | ||
| pub notif_config_data: bool, // VIRTIO_F_NOTIF_CONFIG_DATA | ||
| pub ring_reset: bool, // VIRTIO_F_RING_RESET | ||
| pub admin_vq: bool, // VIRTIO_F_ADMIN_VQ | ||
| pub device_specific_bit_42: bool, | ||
| pub suspend: bool, // VIRTIO_F_SUSPEND | ||
| #[bits(7)] | ||
| _reserved: u8, | ||
| #[bits(13)] | ||
| pub device_specific: u16, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct VirtioDeviceFeatures(Vec<u32>); | ||
| impl VirtioDeviceFeatures { | ||
| pub fn new() -> Self { | ||
| Self(Vec::with_capacity(2)) | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| pub fn len(&self) -> usize { | ||
| self.0.len() | ||
| } | ||
|
|
||
| pub fn set_bank(&mut self, index: usize, val: u32) { | ||
| if self.0.len() <= index { | ||
| self.0.resize(index + 1, 0); | ||
| } | ||
| self.0[index] = val; | ||
| } | ||
|
Comment on lines
+67
to
+72
|
||
|
|
||
| pub fn with_bank(mut self, index: usize, val: u32) -> Self { | ||
| self.set_bank(index, val); | ||
| self | ||
| } | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| pub fn with_bank0(self, bank0: VirtioDeviceFeaturesBank0) -> Self { | ||
| self.with_bank(0, bank0.into_bits()) | ||
| } | ||
|
|
||
| pub fn with_bank1(self, bank1: VirtioDeviceFeaturesBank1) -> Self { | ||
| self.with_bank(1, bank1.into_bits()) | ||
| } | ||
|
|
||
| pub fn bank(&self, index: usize) -> u32 { | ||
| self.0.get(index).map_or(0, |x| *x) | ||
| } | ||
|
|
||
| pub fn bank0(&self) -> VirtioDeviceFeaturesBank0 { | ||
| VirtioDeviceFeaturesBank0::from_bits(self.bank(0)) | ||
| } | ||
|
|
||
| pub fn bank1(&self) -> VirtioDeviceFeaturesBank1 { | ||
| VirtioDeviceFeaturesBank1::from_bits(self.bank(1)) | ||
| } | ||
| } | ||
|
|
||
| impl Default for VirtioDeviceFeatures { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| #[bitfield(u8)] | ||
| #[derive(IntoBytes, Immutable, KnownLayout, FromBytes, Inspect)] | ||
| pub struct VirtioDeviceStatus { | ||
| pub acknowledge: bool, | ||
| pub driver: bool, | ||
| pub driver_ok: bool, | ||
| pub features_ok: bool, | ||
| pub suspend: bool, | ||
| _reserved1: bool, | ||
| pub device_needs_reset: bool, | ||
| pub failed: bool, | ||
| } | ||
|
|
||
| impl VirtioDeviceStatus { | ||
| pub fn as_u32(&self) -> u32 { | ||
| self.into_bits() as u32 | ||
| } | ||
| } | ||
|
|
||
| // ACPI interrupt status flags | ||
| pub const VIRTIO_MMIO_INTERRUPT_STATUS_USED_BUFFER: u32 = 1; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these bank types be Copy too?