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

Add macros to read or write config space in a type safe way #170

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
157 changes: 76 additions & 81 deletions src/device/blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

use crate::hal::Hal;
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::volatile::Volatile;
use crate::transport::{read_config, Transport};
use crate::volatile::ReadOnly;
use crate::{Error, Result};
use bitflags::bitflags;
use core::mem::offset_of;
use log::info;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

Expand Down Expand Up @@ -57,12 +56,8 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {

// Read configuration space.
let capacity = transport.read_consistent(|| {
Ok(
transport.read_config_space::<u32>(offset_of!(BlkConfig, capacity_low))? as u64
| (transport.read_config_space::<u32>(offset_of!(BlkConfig, capacity_high))?
as u64)
<< 32,
)
Ok(read_config!(transport, BlkConfig, capacity_low)? as u64
| (read_config!(transport, BlkConfig, capacity_high)? as u64) << 32)
})?;
info!("found a block device of size {}KB", capacity / 2);

Expand Down Expand Up @@ -396,18 +391,18 @@ impl<H: Hal, T: Transport> Drop for VirtIOBlk<H, T> {
#[repr(C)]
struct BlkConfig {
/// Number of 512 Bytes sectors
capacity_low: Volatile<u32>,
capacity_high: Volatile<u32>,
size_max: Volatile<u32>,
seg_max: Volatile<u32>,
cylinders: Volatile<u16>,
heads: Volatile<u8>,
sectors: Volatile<u8>,
blk_size: Volatile<u32>,
physical_block_exp: Volatile<u8>,
alignment_offset: Volatile<u8>,
min_io_size: Volatile<u16>,
opt_io_size: Volatile<u32>,
capacity_low: ReadOnly<u32>,
capacity_high: ReadOnly<u32>,
size_max: ReadOnly<u32>,
seg_max: ReadOnly<u32>,
cylinders: ReadOnly<u16>,
heads: ReadOnly<u8>,
sectors: ReadOnly<u8>,
blk_size: ReadOnly<u32>,
physical_block_exp: ReadOnly<u8>,
alignment_offset: ReadOnly<u8>,
min_io_size: ReadOnly<u16>,
opt_io_size: ReadOnly<u32>,
// ... ignored
}

Expand Down Expand Up @@ -570,18 +565,18 @@ mod tests {
#[test]
fn config() {
let mut config_space = BlkConfig {
capacity_low: Volatile::new(0x42),
capacity_high: Volatile::new(0x02),
size_max: Volatile::new(0),
seg_max: Volatile::new(0),
cylinders: Volatile::new(0),
heads: Volatile::new(0),
sectors: Volatile::new(0),
blk_size: Volatile::new(0),
physical_block_exp: Volatile::new(0),
alignment_offset: Volatile::new(0),
min_io_size: Volatile::new(0),
opt_io_size: Volatile::new(0),
capacity_low: ReadOnly::new(0x42),
capacity_high: ReadOnly::new(0x02),
size_max: ReadOnly::new(0),
seg_max: ReadOnly::new(0),
cylinders: ReadOnly::new(0),
heads: ReadOnly::new(0),
sectors: ReadOnly::new(0),
blk_size: ReadOnly::new(0),
physical_block_exp: ReadOnly::new(0),
alignment_offset: ReadOnly::new(0),
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
Expand All @@ -603,18 +598,18 @@ mod tests {
#[test]
fn read() {
let mut config_space = BlkConfig {
capacity_low: Volatile::new(66),
capacity_high: Volatile::new(0),
size_max: Volatile::new(0),
seg_max: Volatile::new(0),
cylinders: Volatile::new(0),
heads: Volatile::new(0),
sectors: Volatile::new(0),
blk_size: Volatile::new(0),
physical_block_exp: Volatile::new(0),
alignment_offset: Volatile::new(0),
min_io_size: Volatile::new(0),
opt_io_size: Volatile::new(0),
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
seg_max: ReadOnly::new(0),
cylinders: ReadOnly::new(0),
heads: ReadOnly::new(0),
sectors: ReadOnly::new(0),
blk_size: ReadOnly::new(0),
physical_block_exp: ReadOnly::new(0),
alignment_offset: ReadOnly::new(0),
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
Expand Down Expand Up @@ -673,18 +668,18 @@ mod tests {
#[test]
fn write() {
let mut config_space = BlkConfig {
capacity_low: Volatile::new(66),
capacity_high: Volatile::new(0),
size_max: Volatile::new(0),
seg_max: Volatile::new(0),
cylinders: Volatile::new(0),
heads: Volatile::new(0),
sectors: Volatile::new(0),
blk_size: Volatile::new(0),
physical_block_exp: Volatile::new(0),
alignment_offset: Volatile::new(0),
min_io_size: Volatile::new(0),
opt_io_size: Volatile::new(0),
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
seg_max: ReadOnly::new(0),
cylinders: ReadOnly::new(0),
heads: ReadOnly::new(0),
sectors: ReadOnly::new(0),
blk_size: ReadOnly::new(0),
physical_block_exp: ReadOnly::new(0),
alignment_offset: ReadOnly::new(0),
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
Expand Down Expand Up @@ -748,18 +743,18 @@ mod tests {
#[test]
fn flush() {
let mut config_space = BlkConfig {
capacity_low: Volatile::new(66),
capacity_high: Volatile::new(0),
size_max: Volatile::new(0),
seg_max: Volatile::new(0),
cylinders: Volatile::new(0),
heads: Volatile::new(0),
sectors: Volatile::new(0),
blk_size: Volatile::new(0),
physical_block_exp: Volatile::new(0),
alignment_offset: Volatile::new(0),
min_io_size: Volatile::new(0),
opt_io_size: Volatile::new(0),
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
seg_max: ReadOnly::new(0),
cylinders: ReadOnly::new(0),
heads: ReadOnly::new(0),
sectors: ReadOnly::new(0),
blk_size: ReadOnly::new(0),
physical_block_exp: ReadOnly::new(0),
alignment_offset: ReadOnly::new(0),
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
Expand Down Expand Up @@ -815,18 +810,18 @@ mod tests {
#[test]
fn device_id() {
let mut config_space = BlkConfig {
capacity_low: Volatile::new(66),
capacity_high: Volatile::new(0),
size_max: Volatile::new(0),
seg_max: Volatile::new(0),
cylinders: Volatile::new(0),
heads: Volatile::new(0),
sectors: Volatile::new(0),
blk_size: Volatile::new(0),
physical_block_exp: Volatile::new(0),
alignment_offset: Volatile::new(0),
min_io_size: Volatile::new(0),
opt_io_size: Volatile::new(0),
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
seg_max: ReadOnly::new(0),
cylinders: ReadOnly::new(0),
heads: ReadOnly::new(0),
sectors: ReadOnly::new(0),
blk_size: ReadOnly::new(0),
physical_block_exp: ReadOnly::new(0),
alignment_offset: ReadOnly::new(0),
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
Expand Down
10 changes: 4 additions & 6 deletions src/device/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ mod embedded_io;

use crate::hal::Hal;
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::transport::{read_config, write_config, Transport};
use crate::volatile::{ReadOnly, WriteOnly};
use crate::{Error, Result, PAGE_SIZE};
use alloc::boxed::Box;
use bitflags::bitflags;
use core::fmt::{self, Display, Formatter, Write};
use core::mem::offset_of;
use log::error;

const QUEUE_RECEIVEQ_PORT_0: u16 = 0;
Expand Down Expand Up @@ -129,8 +128,8 @@ impl<H: Hal, T: Transport> VirtIOConsole<H, T> {
if self.negotiated_features.contains(Features::SIZE) {
self.transport.read_consistent(|| {
Ok(Some(Size {
columns: self.transport.read_config_space(offset_of!(Config, cols))?,
rows: self.transport.read_config_space(offset_of!(Config, rows))?,
columns: read_config!(self.transport, Config, cols)?,
rows: read_config!(self.transport, Config, rows)?,
}))
})
} else {
Expand Down Expand Up @@ -240,8 +239,7 @@ impl<H: Hal, T: Transport> VirtIOConsole<H, T> {
/// Returns an error if the device doesn't support emergency write.
pub fn emergency_write(&mut self, chr: u8) -> Result<()> {
if self.negotiated_features.contains(Features::EMERG_WRITE) {
self.transport
.write_config_space::<u32>(offset_of!(Config, emerg_wr), chr.into())?;
write_config!(self.transport, Config, emerg_wr, chr.into())?;
Ok(())
} else {
Err(Error::Unsupported)
Expand Down
11 changes: 5 additions & 6 deletions src/device/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

use crate::hal::{BufferDirection, Dma, Hal};
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::volatile::{ReadOnly, Volatile, WriteOnly};
use crate::transport::{read_config, Transport};
use crate::volatile::{ReadOnly, WriteOnly};
use crate::{pages, Error, Result, PAGE_SIZE};
use alloc::boxed::Box;
use bitflags::bitflags;
use core::mem::offset_of;
use log::info;
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};

Expand Down Expand Up @@ -44,8 +43,8 @@
let negotiated_features = transport.begin_init(SUPPORTED_FEATURES);

// read configuration space
let events_read = transport.read_config_space::<u32>(offset_of!(Config, events_read))?;
let num_scanouts = transport.read_config_space::<u32>(offset_of!(Config, num_scanouts))?;
let events_read = read_config!(transport, Config, events_read)?;
let num_scanouts = read_config!(transport, Config, num_scanouts)?;
info!(
"events_read: {:#x}, num_scanouts: {:#x}",
events_read, num_scanouts
Expand Down Expand Up @@ -253,16 +252,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 264 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:255:5 | 255 | / fn update_cursor( 256 | | &mut self, 257 | | resource_id: u32, 258 | | scanout_id: u32, ... | 263 | | is_move: bool, 264 | | ) -> 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 @@ -303,7 +302,7 @@
/// Specifies the maximum number of scanouts supported by the device.
///
/// Minimum value is 1, maximum value is 16.
num_scanouts: Volatile<u32>,
num_scanouts: ReadOnly<u32>,
}

/// Display configuration has changed.
Expand Down
21 changes: 7 additions & 14 deletions src/device/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::common::Feature;
use crate::hal::Hal;
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::transport::{read_config, write_config, Transport};
use crate::volatile::{ReadOnly, WriteOnly};
use crate::Error;
use alloc::{boxed::Box, string::String};
Expand Down Expand Up @@ -103,11 +103,9 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
subsel: u8,
out: &mut [u8],
) -> Result<u8, Error> {
self.transport
.write_config_space(offset_of!(Config, select), select as u8)?;
self.transport
.write_config_space(offset_of!(Config, subsel), subsel)?;
let size: u8 = self.transport.read_config_space(offset_of!(Config, size))?;
write_config!(self.transport, Config, select, select as u8)?;
write_config!(self.transport, Config, subsel, subsel)?;
let size: u8 = read_config!(self.transport, Config, size)?;
// Safe because config points to a valid MMIO region for the config space.
let size_to_copy = min(usize::from(size), out.len());
for (i, out_item) in out.iter_mut().take(size_to_copy).enumerate() {
Expand All @@ -126,14 +124,9 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
select: InputConfigSelect,
subsel: u8,
) -> Result<Box<[u8]>, Error> {
self.transport
.write_config_space(offset_of!(Config, select), select as u8)?;
self.transport
.write_config_space(offset_of!(Config, subsel), subsel)?;
let size = usize::from(
self.transport
.read_config_space::<u8>(offset_of!(Config, size))?,
);
write_config!(self.transport, Config, select, select as u8)?;
write_config!(self.transport, Config, subsel, subsel)?;
let size = usize::from(read_config!(self.transport, Config, size)?);
if size > CONFIG_DATA_MAX_LENGTH {
return Err(Error::IoError);
}
Expand Down
9 changes: 3 additions & 6 deletions src/device/net/dev_raw.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use super::{Config, EthernetAddress, Features, VirtioNetHdr};
use super::{MIN_BUFFER_LEN, NET_HDR_SIZE, QUEUE_RECEIVE, QUEUE_TRANSMIT, SUPPORTED_FEATURES};
use crate::device::net::Status;
use crate::hal::Hal;
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::transport::{read_config, Transport};
use crate::{Error, Result};
use core::mem::offset_of;
use log::{debug, info, warn};
use zerocopy::IntoBytes;

Expand All @@ -31,9 +29,8 @@ impl<H: Hal, T: Transport, const QUEUE_SIZE: usize> VirtIONetRaw<H, T, QUEUE_SIZ
info!("negotiated_features {:?}", negotiated_features);

// Read configuration space.
let mac =
transport.read_consistent(|| transport.read_config_space(offset_of!(Config, mac)))?;
let status = transport.read_config_space::<Status>(offset_of!(Config, status))?;
let mac = transport.read_consistent(|| read_config!(transport, Config, mac))?;
let status = read_config!(transport, Config, status)?;
debug!("Got MAC={:02x?}, status={:?}", mac, status);

let send_queue = VirtQueue::new(
Expand Down
12 changes: 4 additions & 8 deletions src/device/socket/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use super::protocol::{
use super::DEFAULT_RX_BUFFER_SIZE;
use crate::hal::Hal;
use crate::queue::{owning::OwningQueue, VirtQueue};
use crate::transport::Transport;
use crate::transport::{read_config, Transport};
use crate::Result;
use core::mem::{offset_of, size_of};
use core::mem::size_of;
use log::debug;
use zerocopy::{FromBytes, IntoBytes};

Expand Down Expand Up @@ -249,12 +249,8 @@ impl<H: Hal, T: Transport, const RX_BUFFER_SIZE: usize> VirtIOSocket<H, T, RX_BU

let guest_cid = transport.read_consistent(|| {
Ok(
transport.read_config_space::<u32>(offset_of!(VirtioVsockConfig, guest_cid_low))?
as u64
| (transport
.read_config_space::<u32>(offset_of!(VirtioVsockConfig, guest_cid_high))?
as u64)
<< 32,
read_config!(transport, VirtioVsockConfig, guest_cid_low)? as u64
| (read_config!(transport, VirtioVsockConfig, guest_cid_high)? as u64) << 32,
)
})?;
debug!("guest cid: {guest_cid:?}");
Expand Down
Loading
Loading