Skip to content

Commit

Permalink
Merge pull request #162 from rcore-os/zerocopy
Browse files Browse the repository at this point in the history
Update to zerocopy 0.8.7.
  • Loading branch information
qwandor authored Nov 6, 2024
2 parents c0807ba + 72edbcd commit e97f718
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 122 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ log = "0.4.22"
bitflags = "2.6.0"
enumn = "0.1.14"
embedded-io = { version = "0.6.1", optional = true }
zerocopy = { version = "0.7.35", features = ["derive"] }
zerocopy = { version = "0.8.7", features = ["derive"] }

[features]
default = ["alloc", "embedded-io"]
alloc = ["zerocopy/alloc"]
embedded-io = ["dep:embedded-io"]

[dev-dependencies]
zerocopy = { version = "0.7.35", features = ["alloc"] }
zerocopy = { version = "0.8.7", features = ["alloc"] }
24 changes: 12 additions & 12 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, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

const QUEUE: u16 = 0;
const QUEUE_SIZE: u16 = 16;
Expand Down Expand Up @@ -111,7 +111,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
let mut resp = BlkResp::default();
self.queue.add_notify_wait_pop(
&[request.as_bytes()],
&mut [resp.as_bytes_mut()],
&mut [resp.as_mut_bytes()],
&mut self.transport,
)?;
resp.status.into()
Expand All @@ -122,7 +122,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
let mut resp = BlkResp::default();
self.queue.add_notify_wait_pop(
&[request.as_bytes()],
&mut [data, resp.as_bytes_mut()],
&mut [data, resp.as_mut_bytes()],
&mut self.transport,
)?;
resp.status.into()
Expand All @@ -133,7 +133,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
let mut resp = BlkResp::default();
self.queue.add_notify_wait_pop(
&[request.as_bytes(), data],
&mut [resp.as_bytes_mut()],
&mut [resp.as_mut_bytes()],
&mut self.transport,
)?;
resp.status.into()
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
};
let token = self
.queue
.add(&[req.as_bytes()], &mut [buf, resp.as_bytes_mut()])?;
.add(&[req.as_bytes()], &mut [buf, resp.as_mut_bytes()])?;
if self.queue.should_notify() {
self.transport.notify(QUEUE);
}
Expand All @@ -282,7 +282,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
resp: &mut BlkResp,
) -> Result<()> {
self.queue
.pop_used(token, &[req.as_bytes()], &mut [buf, resp.as_bytes_mut()])?;
.pop_used(token, &[req.as_bytes()], &mut [buf, resp.as_mut_bytes()])?;
resp.status.into()
}

Expand Down Expand Up @@ -343,7 +343,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
};
let token = self
.queue
.add(&[req.as_bytes(), buf], &mut [resp.as_bytes_mut()])?;
.add(&[req.as_bytes(), buf], &mut [resp.as_mut_bytes()])?;
if self.queue.should_notify() {
self.transport.notify(QUEUE);
}
Expand All @@ -364,7 +364,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
resp: &mut BlkResp,
) -> Result<()> {
self.queue
.pop_used(token, &[req.as_bytes(), buf], &mut [resp.as_bytes_mut()])?;
.pop_used(token, &[req.as_bytes(), buf], &mut [resp.as_mut_bytes()])?;
resp.status.into()
}

Expand Down Expand Up @@ -410,7 +410,7 @@ struct BlkConfig {

/// A VirtIO block device request.
#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
pub struct BlkReq {
type_: ReqType,
reserved: u32,
Expand All @@ -429,7 +429,7 @@ impl Default for BlkReq {

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

#[repr(u32)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
enum ReqType {
In = 0,
Out = 1,
Expand All @@ -456,7 +456,7 @@ enum ReqType {

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

impl RespStatus {
Expand Down
36 changes: 18 additions & 18 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, FromZeroes};
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};

const QUEUE_SIZE: u16 = 2;
const SUPPORTED_FEATURES: Features = Features::RING_EVENT_IDX.union(Features::RING_INDIRECT_DESC);
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 = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);
let queue_buf_recv = FromZeroes::new_box_slice_zeroed(PAGE_SIZE);
let queue_buf_send = FromZeros::new_box_zeroed_with_elems(PAGE_SIZE).unwrap();
let queue_buf_recv = FromZeros::new_box_zeroed_with_elems(PAGE_SIZE).unwrap();

transport.finish_init();

Expand Down Expand Up @@ -173,18 +173,18 @@ impl<H: Hal, T: Transport> VirtIOGpu<H, T> {
}

/// Send a request to the device and block for a response.
fn request<Req: AsBytes, Rsp: FromBytes>(&mut self, req: Req) -> Result<Rsp> {
fn request<Req: IntoBytes + Immutable, Rsp: FromBytes>(&mut self, req: Req) -> Result<Rsp> {
req.write_to_prefix(&mut self.queue_buf_send).unwrap();
self.control_queue.add_notify_wait_pop(
&[&self.queue_buf_send],
&mut [&mut self.queue_buf_recv],
&mut self.transport,
)?;
Ok(Rsp::read_from_prefix(&self.queue_buf_recv).unwrap())
Ok(Rsp::read_from_prefix(&self.queue_buf_recv).unwrap().0)
}

/// Send a mouse cursor operation request to the device and block for a response.
fn cursor_request<Req: AsBytes>(&mut self, req: Req) -> Result {
fn cursor_request<Req: IntoBytes + Immutable>(&mut self, req: Req) -> Result {
req.write_to_prefix(&mut self.queue_buf_send).unwrap();
self.cursor_queue.add_notify_wait_pop(
&[&self.queue_buf_send],
Expand Down Expand Up @@ -338,7 +338,7 @@ bitflags! {
}

#[repr(transparent)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, PartialEq, FromBytes, FromZeroes)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
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, FromZeroes)]
#[derive(Debug, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout)]
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, FromZeroes)]
#[derive(Debug, Copy, Clone, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct Rect {
x: u32,
y: u32,
Expand All @@ -411,7 +411,7 @@ struct Rect {
}

#[repr(C)]
#[derive(Debug, FromBytes, FromZeroes)]
#[derive(Debug, FromBytes, Immutable, KnownLayout)]
struct RespDisplayInfo {
header: CtrlHeader,
rect: Rect,
Expand All @@ -420,7 +420,7 @@ struct RespDisplayInfo {
}

#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
struct ResourceCreate2D {
header: CtrlHeader,
resource_id: u32,
Expand All @@ -430,13 +430,13 @@ struct ResourceCreate2D {
}

#[repr(u32)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
enum Format {
B8G8R8A8UNORM = 1,
}

#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
struct ResourceAttachBacking {
header: CtrlHeader,
resource_id: u32,
Expand All @@ -447,7 +447,7 @@ struct ResourceAttachBacking {
}

#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
struct SetScanout {
header: CtrlHeader,
rect: Rect,
Expand All @@ -456,7 +456,7 @@ struct SetScanout {
}

#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
struct TransferToHost2D {
header: CtrlHeader,
rect: Rect,
Expand All @@ -466,7 +466,7 @@ struct TransferToHost2D {
}

#[repr(C)]
#[derive(AsBytes, Debug)]
#[derive(Debug, Immutable, IntoBytes, KnownLayout)]
struct ResourceFlush {
header: CtrlHeader,
rect: Rect,
Expand All @@ -475,7 +475,7 @@ struct ResourceFlush {
}

#[repr(C)]
#[derive(AsBytes, Debug, Clone, Copy)]
#[derive(Copy, Clone, Debug, Immutable, IntoBytes, KnownLayout)]
struct CursorPos {
scanout_id: u32,
x: u32,
Expand All @@ -484,7 +484,7 @@ struct CursorPos {
}

#[repr(C)]
#[derive(AsBytes, Debug, Clone, Copy)]
#[derive(Copy, Clone, Debug, Immutable, IntoBytes, KnownLayout)]
struct UpdateCursor {
header: CtrlHeader,
pos: CursorPos,
Expand Down
20 changes: 10 additions & 10 deletions src/device/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloc::{boxed::Box, string::String};
use core::cmp::min;
use core::mem::size_of;
use core::ptr::{addr_of, NonNull};
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};

/// Virtual human interface devices such as keyboards, mice and tablets.
///
Expand Down Expand Up @@ -48,7 +48,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
)?;
for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
// Safe because the buffer lasts as long as the queue.
let token = unsafe { event_queue.add(&[], &mut [event.as_bytes_mut()])? };
let token = unsafe { event_queue.add(&[], &mut [event.as_mut_bytes()])? };
assert_eq!(token, i as u16);
}
if event_queue.should_notify() {
Expand Down Expand Up @@ -79,13 +79,13 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
// is still valid.
unsafe {
self.event_queue
.pop_used(token, &[], &mut [event.as_bytes_mut()])
.pop_used(token, &[], &mut [event.as_mut_bytes()])
.ok()?;
}
let event_saved = *event;
// requeue
// Safe because buffer lasts as long as the queue.
if let Ok(new_token) = unsafe { self.event_queue.add(&[], &mut [event.as_bytes_mut()]) }
if let Ok(new_token) = unsafe { self.event_queue.add(&[], &mut [event.as_mut_bytes()]) }
{
// This only works because nothing happen between `pop_used` and `add` that affects
// the list of free descriptors in the queue, so `add` reuses the descriptor which
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
if size > CONFIG_DATA_MAX_LENGTH {
return Err(Error::IoError);
}
let mut buf = u8::new_box_slice_zeroed(size);
let mut buf = <[u8]>::new_box_zeroed_with_elems(size).unwrap();
for i in 0..size {
buf[i] = addr_of!((*self.config.as_ptr()).data[i]).vread();
}
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
/// Queries and returns the ID information of the device.
pub fn ids(&mut self) -> Result<DevIDs, Error> {
let mut ids = DevIDs::default();
let size = self.query_config_select(InputConfigSelect::IdDevids, 0, ids.as_bytes_mut());
let size = self.query_config_select(InputConfigSelect::IdDevids, 0, ids.as_mut_bytes());
if usize::from(size) == size_of::<DevIDs>() {
Ok(ids)
} else {
Expand All @@ -195,7 +195,7 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
/// Queries and returns information about the given axis of the device.
pub fn abs_info(&mut self, axis: u8) -> Result<AbsInfo, Error> {
let mut info = AbsInfo::default();
let size = self.query_config_select(InputConfigSelect::AbsInfo, axis, info.as_bytes_mut());
let size = self.query_config_select(InputConfigSelect::AbsInfo, axis, info.as_mut_bytes());
if usize::from(size) == size_of::<AbsInfo>() {
Ok(info)
} else {
Expand Down Expand Up @@ -263,7 +263,7 @@ struct Config {

/// Information about an axis of an input device, typically a joystick.
#[repr(C)]
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
#[derive(Clone, Debug, Default, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
pub struct AbsInfo {
/// The minimum value for the axis.
pub min: u32,
Expand All @@ -279,7 +279,7 @@ pub struct AbsInfo {

/// The identifiers of a VirtIO input device.
#[repr(C)]
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
#[derive(Clone, Debug, Default, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
pub struct DevIDs {
/// The bustype identifier.
pub bustype: u16,
Expand All @@ -294,7 +294,7 @@ pub 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, FromZeroes)]
#[derive(Clone, Copy, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
Expand Down
2 changes: 1 addition & 1 deletion src/device/net/dev_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::transport::Transport;
use crate::volatile::volread;
use crate::{Error, Result};
use log::{debug, info, warn};
use zerocopy::AsBytes;
use zerocopy::IntoBytes;

/// Raw driver for a VirtIO network device.
///
Expand Down
Loading

0 comments on commit e97f718

Please sign in to comment.