Skip to content

Commit

Permalink
Update to zerocopy 0.7.5 (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Frederick Mayle <[email protected]>
  • Loading branch information
fkm3 and fkm3 authored Oct 2, 2023
1 parent fd54a1b commit 150740b
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ categories = ["hardware-support", "no-std"]
[dependencies]
log = "0.4"
bitflags = "2.3.0"
zerocopy = "0.6.1"
zerocopy = { version = "0.7.5", features = ["derive"] }

[features]
default = ["alloc"]
alloc = ["zerocopy/alloc"]

[dev-dependencies]
zerocopy = { version = "0.6.1", features = ["alloc"] }
zerocopy = { version = "0.7.5", features = ["alloc"] }
6 changes: 3 additions & 3 deletions src/device/blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::volatile::{volread, Volatile};
use crate::{Error, Result};
use bitflags::bitflags;
use log::info;
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

const QUEUE: u16 = 0;
const QUEUE_SIZE: u16 = 16;
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Default for BlkReq {

/// Response of a VirtIOBlk request.
#[repr(C)]
#[derive(AsBytes, Debug, FromBytes)]
#[derive(AsBytes, Debug, FromBytes, FromZeroes)]
pub struct BlkResp {
status: RespStatus,
}
Expand All @@ -446,7 +446,7 @@ enum ReqType {

/// Status of a VirtIOBlk request.
#[repr(transparent)]
#[derive(AsBytes, Copy, Clone, Debug, Eq, FromBytes, PartialEq)]
#[derive(AsBytes, Copy, Clone, Debug, Eq, FromBytes, FromZeroes, PartialEq)]
pub struct RespStatus(u8);

impl RespStatus {
Expand Down
14 changes: 7 additions & 7 deletions src/device/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{pages, Error, Result, PAGE_SIZE};
use alloc::boxed::Box;
use bitflags::bitflags;
use log::info;
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

const QUEUE_SIZE: u16 = 2;
const SUPPORTED_FEATURES: Features = Features::RING_EVENT_IDX;
Expand Down Expand Up @@ -66,8 +66,8 @@ impl<H: Hal, T: Transport> VirtIOGpu<H, T> {
negotiated_features.contains(Features::RING_EVENT_IDX),
)?;

let queue_buf_send = FromBytes::new_box_slice_zeroed(PAGE_SIZE);
let queue_buf_recv = FromBytes::new_box_slice_zeroed(PAGE_SIZE);
let queue_buf_send = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);
let queue_buf_recv = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);

transport.finish_init();

Expand Down Expand Up @@ -338,7 +338,7 @@ bitflags! {
}

#[repr(transparent)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, PartialEq, FromBytes)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, PartialEq, FromBytes, FromZeroes)]
struct Command(u32);

impl Command {
Expand Down Expand Up @@ -371,7 +371,7 @@ impl Command {
const GPU_FLAG_FENCE: u32 = 1 << 0;

#[repr(C)]
#[derive(AsBytes, Debug, Clone, Copy, FromBytes)]
#[derive(AsBytes, Debug, Clone, Copy, FromBytes, FromZeroes)]
struct CtrlHeader {
hdr_type: Command,
flags: u32,
Expand Down Expand Up @@ -402,7 +402,7 @@ impl CtrlHeader {
}

#[repr(C)]
#[derive(AsBytes, Debug, Copy, Clone, Default, FromBytes)]
#[derive(AsBytes, Debug, Copy, Clone, Default, FromBytes, FromZeroes)]
struct Rect {
x: u32,
y: u32,
Expand All @@ -411,7 +411,7 @@ struct Rect {
}

#[repr(C)]
#[derive(Debug, FromBytes)]
#[derive(Debug, FromBytes, FromZeroes)]
struct RespDisplayInfo {
header: CtrlHeader,
rect: Rect,
Expand Down
4 changes: 2 additions & 2 deletions src/device/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::volatile::{volread, volwrite, ReadOnly, WriteOnly};
use crate::Result;
use alloc::boxed::Box;
use core::ptr::NonNull;
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

/// Virtual human interface devices such as keyboards, mice and tablets.
///
Expand Down Expand Up @@ -185,7 +185,7 @@ struct DevIDs {
/// Both queues use the same `virtio_input_event` struct. `type`, `code` and `value`
/// are filled according to the Linux input layer (evdev) interface.
#[repr(C)]
#[derive(AsBytes, Clone, Copy, Debug, Default, FromBytes)]
#[derive(AsBytes, Clone, Copy, Debug, Default, FromBytes, FromZeroes)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
Expand Down
8 changes: 4 additions & 4 deletions src/device/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloc::{vec, vec::Vec};
use bitflags::bitflags;
use core::{convert::TryInto, mem::size_of};
use log::{debug, warn};
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

const MAX_BUFFER_LEN: usize = 65535;
const MIN_BUFFER_LEN: usize = 1526;
Expand Down Expand Up @@ -370,7 +370,7 @@ type EthernetAddress = [u8; 6];
/// and buffers for incoming packets are placed in the receiveq1. . .receiveqN.
/// In each case, the packet itself is preceded by a header.
#[repr(C)]
#[derive(AsBytes, Debug, Default, FromBytes)]
#[derive(AsBytes, Debug, Default, FromBytes, FromZeroes)]
pub struct VirtioNetHdr {
flags: Flags,
gso_type: GsoType,
Expand All @@ -382,7 +382,7 @@ pub struct VirtioNetHdr {
// payload starts from here
}

#[derive(AsBytes, Copy, Clone, Debug, Default, Eq, FromBytes, PartialEq)]
#[derive(AsBytes, Copy, Clone, Debug, Default, Eq, FromBytes, FromZeroes, PartialEq)]
#[repr(transparent)]
struct Flags(u8);

Expand All @@ -395,7 +395,7 @@ bitflags! {
}

#[repr(transparent)]
#[derive(AsBytes, Debug, Copy, Clone, Default, Eq, FromBytes, PartialEq)]
#[derive(AsBytes, Debug, Copy, Clone, Default, Eq, FromBytes, FromZeroes, PartialEq)]
struct GsoType(u8);

impl GsoType {
Expand Down
4 changes: 2 additions & 2 deletions src/device/socket/connectionmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::cmp::min;
use core::convert::TryInto;
use core::hint::spin_loop;
use log::debug;
use zerocopy::FromBytes;
use zerocopy::FromZeroes;

const PER_CONNECTION_BUFFER_CAPACITY: usize = 1024;

Expand Down Expand Up @@ -313,7 +313,7 @@ struct RingBuffer {
impl RingBuffer {
pub fn new(capacity: usize) -> Self {
Self {
buffer: FromBytes::new_box_slice_zeroed(capacity),
buffer: FromZeroes::new_box_slice_zeroed(capacity),
used: 0,
start: 0,
}
Expand Down
6 changes: 3 additions & 3 deletions src/device/socket/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{
};
use zerocopy::{
byteorder::{LittleEndian, U16, U32, U64},
AsBytes, FromBytes,
AsBytes, FromBytes, FromZeroes,
};

/// Well-known CID for the host.
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct VirtioVsockConfig {

/// The message header for data packets sent on the tx/rx queues
#[repr(packed)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, FromBytes, PartialEq)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, FromBytes, FromZeroes, PartialEq)]
pub struct VirtioVsockHdr {
pub src_cid: U64<LittleEndian>,
pub dst_cid: U64<LittleEndian>,
Expand Down Expand Up @@ -122,7 +122,7 @@ pub struct VsockAddr {
}

/// An event sent to the event queue
#[derive(Copy, Clone, Debug, Default, AsBytes, FromBytes)]
#[derive(Copy, Clone, Debug, Default, AsBytes, FromBytes, FromZeroes)]
#[repr(C)]
pub struct VirtioVsockEvent {
// ID from the virtio_vsock_event_id struct in the virtio spec
Expand Down
4 changes: 2 additions & 2 deletions src/device/socket/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use alloc::boxed::Box;
use core::mem::size_of;
use core::ptr::{null_mut, NonNull};
use log::debug;
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

pub(crate) const RX_QUEUE_IDX: u16 = 0;
pub(crate) const TX_QUEUE_IDX: u16 = 1;
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<H: Hal, T: Transport> VirtIOSocket<H, T> {
// Allocate and add buffers for the RX queue.
let mut rx_queue_buffers = [null_mut(); QUEUE_SIZE];
for (i, rx_queue_buffer) in rx_queue_buffers.iter_mut().enumerate() {
let mut buffer: Box<[u8; RX_BUFFER_SIZE]> = FromBytes::new_box_zeroed();
let mut buffer: Box<[u8; RX_BUFFER_SIZE]> = FromZeroes::new_box_zeroed();
// Safe because the buffer lives as long as the queue, as specified in the function
// safety requirement, and we don't access it until it is popped.
let token = unsafe { rx.add(&[], &mut [buffer.as_mut_slice()]) }?;
Expand Down
2 changes: 1 addition & 1 deletion src/hal/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{
alloc::Layout,
ptr::{self, NonNull},
};
use zerocopy::FromBytes;
use zerocopy::FromZeroes;

#[derive(Debug)]
pub struct FakeHal;
Expand Down
11 changes: 5 additions & 6 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::mem::{size_of, take};
use core::ptr;
use core::ptr::NonNull;
use core::sync::atomic::{fence, Ordering};
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

/// The mechanism for bulk data transport on virtio devices.
///
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<H: Hal, const SIZE: usize> VirtQueue<H, SIZE> {
let avail = layout.avail_vaddr().cast();
let used = layout.used_vaddr().cast();

let mut desc_shadow: [Descriptor; SIZE] = FromBytes::new_zeroed();
let mut desc_shadow: [Descriptor; SIZE] = FromZeroes::new_zeroed();
// Link descriptors together.
for i in 0..(size - 1) {
desc_shadow[i as usize].next = i + 1;
Expand Down Expand Up @@ -668,7 +668,7 @@ fn queue_part_sizes(queue_size: u16) -> (usize, usize, usize) {
}

#[repr(C, align(16))]
#[derive(AsBytes, Clone, Debug, FromBytes)]
#[derive(AsBytes, Clone, Debug, FromBytes, FromZeroes)]
pub(crate) struct Descriptor {
addr: u64,
len: u32,
Expand Down Expand Up @@ -723,7 +723,7 @@ impl Descriptor {
}

/// Descriptor flags
#[derive(AsBytes, Copy, Clone, Debug, Default, Eq, FromBytes, PartialEq)]
#[derive(AsBytes, Copy, Clone, Debug, Default, Eq, FromBytes, FromZeroes, PartialEq)]
#[repr(transparent)]
struct DescFlags(u16);

Expand Down Expand Up @@ -818,7 +818,6 @@ pub(crate) fn fake_read_write_queue<const QUEUE_SIZE: usize>(
handler: impl FnOnce(Vec<u8>) -> Vec<u8>,
) {
use core::{ops::Deref, slice};
use zerocopy::LayoutVerified;

let available_ring = queue_driver_area as *const AvailRing<QUEUE_SIZE>;
let used_ring = queue_device_area as *mut UsedRing<QUEUE_SIZE>;
Expand All @@ -842,7 +841,7 @@ pub(crate) fn fake_read_write_queue<const QUEUE_SIZE: usize>(

// Loop through all input descriptors in the indirect descriptor list, reading data from
// them.
let indirect_descriptor_list: &[Descriptor] = LayoutVerified::new_slice(
let indirect_descriptor_list: &[Descriptor] = zerocopy::Ref::new_slice(
slice::from_raw_parts(descriptor.addr as *const u8, descriptor.len as usize),
)
.unwrap()
Expand Down

0 comments on commit 150740b

Please sign in to comment.