Skip to content
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

Update to zerocopy 0.8.7. #162

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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 @@
}

/// 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 @@ -255,16 +255,16 @@
rsp.check_type(Command::OK_NODATA)
}

fn update_cursor(
&mut self,
resource_id: u32,
scanout_id: u32,
pos_x: u32,
pos_y: u32,
hot_x: u32,
hot_y: u32,
is_move: bool,
) -> Result {

Check warning on line 267 in src/device/gpu.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> src/device/gpu.rs:258:5 | 258 | / fn update_cursor( 259 | | &mut self, 260 | | resource_id: u32, 261 | | scanout_id: u32, ... | 266 | | is_move: bool, 267 | | ) -> Result { | |_______________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default
self.cursor_request(UpdateCursor {
header: if is_move {
CtrlHeader::with_type(Command::MOVE_CURSOR)
Expand Down Expand Up @@ -338,7 +338,7 @@
}

#[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 @@
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 @@
}

#[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 @@
}

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

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

#[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 @@
}

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

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

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

#[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 @@
}

#[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
Loading