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

New virtio blk driver #96

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ categories = ["hardware-support", "no-std"]
log = "0.4"
bitflags = "2.3.0"
zerocopy = "0.6.1"
spin = "0.9"


[features]
default = ["alloc"]
Expand Down
8 changes: 5 additions & 3 deletions examples/aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,18 @@ fn virtio_device(transport: impl Transport) {
}

fn virtio_blk<T: Transport>(transport: T) {
let mut blk = VirtIOBlk::<HalImpl, T>::new(transport).expect("failed to create blk driver");
let mut blk =
VirtIOBlk::<HalImpl, T>::new(transport, true).expect("failed to create blk driver");
assert!(!blk.readonly());
let mut input = [0xffu8; 512];
let mut output = [0; 512];
for i in 0..32 {
for x in input.iter_mut() {
*x = i as u8;
}
blk.write_block(i, &input).expect("failed to write");
blk.read_block(i, &mut output).expect("failed to read");
blk.write_blocks(i, &[&input]).expect("failed to write");
blk.read_blocks(i, &mut [&mut output])
.expect("failed to read");
assert_eq!(input, output);
}
info!("virtio-blk test finished");
Expand Down
16 changes: 14 additions & 2 deletions examples/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,26 @@ qemu: kernel $(img)
-kernel $(kernel) \
-global virtio-mmio.force-legacy=false \
-drive file=$(img),if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0 \
-device virtio-blk-device,packed=on,drive=x0 \
-device virtio-gpu-device \
-device virtio-mouse-device \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::5555-:5555

qemu-blk: kernel $(img)
qemu-system-$(arch) \
$(QEMU_ARGS) \
-machine virt \
-serial mon:stdio \
-bios default \
-kernel $(kernel) \
-global virtio-mmio.force-legacy=false \
-drive file=$(img),if=none,format=raw,id=x0 \
-device virtio-blk-device,packed=on,drive=x0

$(img):
dd if=/dev/zero of=$@ bs=512 count=32

run: build qemu-legacy qemu
# run: build qemu-legacy qemu
run: build qemu-blk

16 changes: 9 additions & 7 deletions examples/riscv/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
#![deny(warnings)]

#![allow(warnings, unused)]
#[macro_use]
extern crate log;

Expand Down Expand Up @@ -83,23 +83,25 @@ fn virtio_probe(node: FdtNode) {
fn virtio_device(transport: impl Transport) {
match transport.device_type() {
DeviceType::Block => virtio_blk(transport),
DeviceType::GPU => virtio_gpu(transport),
DeviceType::Input => virtio_input(transport),
DeviceType::Network => virtio_net(transport),
// DeviceType::GPU => virtio_gpu(transport),
// DeviceType::Input => virtio_input(transport),
// DeviceType::Network => virtio_net(transport),
t => warn!("Unrecognized virtio device: {:?}", t),
}
}

fn virtio_blk<T: Transport>(transport: T) {
let mut blk = VirtIOBlk::<HalImpl, T>::new(transport).expect("failed to create blk driver");
let mut blk =
VirtIOBlk::<HalImpl, T>::new(transport, true).expect("failed to create blk driver");
let mut input = vec![0xffu8; 512];
let mut output = vec![0; 512];
for i in 0..32 {
for x in input.iter_mut() {
*x = i as u8;
}
blk.write_block(i, &input).expect("failed to write");
blk.read_block(i, &mut output).expect("failed to read");
blk.write_blocks(i, &[&input]).expect("failed to write");
blk.read_blocks(i, &mut [&mut output])
.expect("failed to read");
assert_eq!(input, output);
}
info!("virtio-blk test finished");
Expand Down
8 changes: 5 additions & 3 deletions examples/x86_64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ fn virtio_device(transport: impl Transport) {
}

fn virtio_blk<T: Transport>(transport: T) {
let mut blk = VirtIOBlk::<HalImpl, T>::new(transport).expect("failed to create blk driver");
let mut blk =
VirtIOBlk::<HalImpl, T>::new(transport, true).expect("failed to create blk driver");
assert!(!blk.readonly());
let mut input = [0xffu8; 512];
let mut output = [0; 512];
for i in 0..32 {
for x in input.iter_mut() {
*x = i as u8;
}
blk.write_block(i, &input).expect("failed to write");
blk.read_block(i, &mut output).expect("failed to read");
blk.write_blocks(i, &[&input]).expect("failed to write");
blk.read_blocks(i, &mut [&mut output])
.expect("failed to read");
assert_eq!(input, output);
}
info!("virtio-blk test finished");
Expand Down
Loading