Skip to content

Commit

Permalink
Move fake config space to State for safe access.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Dec 4, 2024
1 parent a8bd90b commit 2d052c3
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 156 deletions.
58 changes: 27 additions & 31 deletions src/device/blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ impl<H: Hal, T: Transport> Drop for VirtIOBlk<H, T> {
}
}

#[derive(FromBytes, Immutable, IntoBytes)]
#[repr(C)]
struct BlkConfig {
/// Number of 512 Bytes sectors
Expand Down Expand Up @@ -559,12 +560,12 @@ mod tests {
},
};
use alloc::{sync::Arc, vec};
use core::{mem::size_of, ptr::NonNull};
use core::mem::size_of;
use std::{sync::Mutex, thread};

#[test]
fn config() {
let mut config_space = BlkConfig {
let config_space = BlkConfig {
capacity_low: ReadOnly::new(0x42),
capacity_high: ReadOnly::new(0x02),
size_max: ReadOnly::new(0),
Expand All @@ -578,15 +579,14 @@ mod tests {
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.into(),
device_features: BlkFeature::RO.bits(),
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let blk = VirtIOBlk::<FakeHal, FakeTransport<BlkConfig>>::new(transport).unwrap();
Expand All @@ -597,7 +597,7 @@ mod tests {

#[test]
fn read() {
let mut config_space = BlkConfig {
let config_space = BlkConfig {
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
Expand All @@ -611,15 +611,14 @@ mod tests {
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.into(),
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut blk = VirtIOBlk::<FakeHal, FakeTransport<BlkConfig>>::new(transport).unwrap();
Expand Down Expand Up @@ -667,7 +666,7 @@ mod tests {

#[test]
fn write() {
let mut config_space = BlkConfig {
let config_space = BlkConfig {
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
Expand All @@ -681,15 +680,14 @@ mod tests {
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.into(),
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut blk = VirtIOBlk::<FakeHal, FakeTransport<BlkConfig>>::new(transport).unwrap();
Expand Down Expand Up @@ -742,7 +740,7 @@ mod tests {

#[test]
fn flush() {
let mut config_space = BlkConfig {
let config_space = BlkConfig {
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
Expand All @@ -756,15 +754,14 @@ mod tests {
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.into(),
device_features: (BlkFeature::RING_INDIRECT_DESC | BlkFeature::FLUSH).bits(),
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut blk = VirtIOBlk::<FakeHal, FakeTransport<BlkConfig>>::new(transport).unwrap();
Expand Down Expand Up @@ -809,7 +806,7 @@ mod tests {

#[test]
fn device_id() {
let mut config_space = BlkConfig {
let config_space = BlkConfig {
capacity_low: ReadOnly::new(66),
capacity_high: ReadOnly::new(0),
size_max: ReadOnly::new(0),
Expand All @@ -823,15 +820,14 @@ mod tests {
min_io_size: ReadOnly::new(0),
opt_io_size: ReadOnly::new(0),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.into(),
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut blk = VirtIOBlk::<FakeHal, FakeTransport<BlkConfig>>::new(transport).unwrap();
Expand Down
60 changes: 28 additions & 32 deletions src/device/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use alloc::boxed::Box;
use bitflags::bitflags;
use core::fmt::{self, Display, Formatter, Write};
use log::error;
use zerocopy::{FromBytes, Immutable, IntoBytes};

const QUEUE_RECEIVEQ_PORT_0: u16 = 0;
const QUEUE_TRANSMITQ_PORT_0: u16 = 1;
Expand Down Expand Up @@ -265,6 +266,7 @@ impl<H: Hal, T: Transport> Drop for VirtIOConsole<H, T> {
}
}

#[derive(FromBytes, Immutable, IntoBytes)]
#[repr(C)]
struct Config {
cols: ReadOnly<u16>,
Expand Down Expand Up @@ -309,26 +311,24 @@ mod tests {
},
};
use alloc::{sync::Arc, vec};
use core::ptr::NonNull;
use std::{sync::Mutex, thread};

#[test]
fn config_info_no_features() {
let mut config_space = Config {
let config_space = Config {
cols: ReadOnly::new(80),
rows: ReadOnly::new(42),
max_nr_ports: ReadOnly::new(0),
emerg_wr: WriteOnly::default(),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default(), QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Console,
max_queue_size: 2,
device_features: 0,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let console = VirtIOConsole::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();
Expand All @@ -338,21 +338,20 @@ mod tests {

#[test]
fn config_info() {
let mut config_space = Config {
let config_space = Config {
cols: ReadOnly::new(80),
rows: ReadOnly::new(42),
max_nr_ports: ReadOnly::new(0),
emerg_wr: WriteOnly::default(),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default(), QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Console,
max_queue_size: 2,
device_features: 0x07,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let console = VirtIOConsole::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();
Expand All @@ -368,46 +367,44 @@ mod tests {

#[test]
fn emergency_write() {
let mut config_space = Config {
let config_space = Config {
cols: ReadOnly::new(0),
rows: ReadOnly::new(0),
max_nr_ports: ReadOnly::new(0),
emerg_wr: WriteOnly::default(),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default(), QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Console,
max_queue_size: 2,
device_features: 0x07,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut console = VirtIOConsole::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();

console.emergency_write(42).unwrap();
assert_eq!(config_space.emerg_wr.0, 42);
assert_eq!(state.lock().unwrap().config_space.emerg_wr.0, 42);
}

#[test]
fn receive() {
let mut config_space = Config {
let config_space = Config {
cols: ReadOnly::new(0),
rows: ReadOnly::new(0),
max_nr_ports: ReadOnly::new(0),
emerg_wr: WriteOnly::default(),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default(), QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Console,
max_queue_size: 2,
device_features: 0,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut console = VirtIOConsole::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();
Expand Down Expand Up @@ -438,21 +435,20 @@ mod tests {

#[test]
fn send() {
let mut config_space = Config {
let config_space = Config {
cols: ReadOnly::new(0),
rows: ReadOnly::new(0),
max_nr_ports: ReadOnly::new(0),
emerg_wr: WriteOnly::default(),
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let state = Arc::new(Mutex::new(State::new(
vec![QueueStatus::default(), QueueStatus::default()],
config_space,
)));
let transport = FakeTransport {
device_type: DeviceType::Console,
max_queue_size: 2,
device_features: 0,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut console = VirtIOConsole::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();
Expand Down
Loading

0 comments on commit 2d052c3

Please sign in to comment.