#devices #virtio #driver #transport #guest #mmio #virt-io

no-std virtio-drivers-and-devices

VirtIO guest drivers and devices. Fork of rcore-os/virtio-drivers.

1 unstable release

0.1.0 Feb 27, 2025

#252 in Hardware support

MIT license

410KB
8K SLoC

VirtIO-drivers-and-devices

crates.io page

VirtIO guest drivers and devices in Rust. For no_std environment. Fork of https://github.com/rcore-os/virtio-drivers.


lib.rs:

VirtIO guest drivers.

These drivers can be used by bare-metal code (such as a bootloader or OS kernel) running in a VM to interact with VirtIO devices provided by the VMM (such as QEMU or crosvm).

Usage

You must first implement the [Hal] trait, to allocate DMA regions and translate between physical addresses (as seen by devices) and virtual addresses (as seen by your program). You can then construct the appropriate transport for the VirtIO device, e.g. for an MMIO device (perhaps discovered from the device tree):

use core::ptr::NonNull;
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};

let header = NonNull::new(mmio_device_address as *mut VirtIOHeader).unwrap();
let transport = unsafe { MmioTransport::new(header, mmio_size) }.unwrap();

You can then check what kind of VirtIO device it is and construct the appropriate driver:

use virtio_drivers::{
    device::console::VirtIOConsole,
    transport::{mmio::MmioTransport, DeviceType, Transport},
};

if transport.device_type() == DeviceType::Console {
    let mut console = VirtIOConsole::<HalImpl, _>::new(transport).unwrap();
    // Send a byte to the console.
    console.send(b'H').unwrap();
}

Dependencies

~1.2–1.7MB
~30K SLoC