Module device_manager

Module device_manager 

Source
Expand description

System level device management.

IoManager is responsible for managing all devices of virtual machine, registering IO resources callback, deregistering devices and helping VM IO exit handling. It defines two buses, one for PIO and one for MMIO, and provides default implementations of PioManager and MmioManager.

The VMM must first allocate unique resources (such as bus ranges), and then call into the vm-device interface to register the devices with their corresponding resources.

§Examples

Registering a new device can be done using the register methods of PioManager and MmioManager with an appropriate bus range (PioRange or MmioRange).

struct NoopDevice {}

impl DevicePio for NoopDevice {
    fn pio_read(&self, base: PioAddress, offset: PioAddressOffset, data: &mut [u8]) {}
    fn pio_write(&self, base: PioAddress, offset: PioAddressOffset, data: &[u8]) {}
}

impl DeviceMmio for NoopDevice {
    fn mmio_read(&self, base: MmioAddress, offset: MmioAddressOffset, data: &mut [u8]) {}
    fn mmio_write(&self, base: MmioAddress, offset: MmioAddressOffset, data: &[u8]) {}
}

// IoManager implements both PioManager and MmioManager.
let mut manager = IoManager::new();

// Register the device on the PIO bus.
let pio_range = PioRange::new(PioAddress(0), 10).unwrap();
manager
    .register_pio(pio_range, Arc::new(NoopDevice {}))
    .unwrap();

// Register the device on the MMIO bus.
let mmio_range = MmioRange::new(MmioAddress(0), 10).unwrap();
manager
    .register_mmio(mmio_range, Arc::new(NoopDevice {}))
    .unwrap();

// Dispatch I/O on the PIO bus.
manager.pio_write(PioAddress(0), &vec![b'o', b'k']).unwrap();

// Dispatch I/O on the MMIO bus.
manager
    .mmio_write(MmioAddress(0), &vec![b'o', b'k'])
    .unwrap();

An alternative way would be to use resources and the resources registration methods of IoManager:

// Use the same NoopDevice defined above.

let mut manager = IoManager::new();

// Define a PIO address range resource.
let pio = Resource::PioAddressRange {
   base: 0,
   size: 10,
};

// Define a MMIO address range resource.
let mmio = Resource::MmioAddressRange {
   base: 0,
   size: 10,
};

// Register the PIO resource.
manager
    .register_pio_resources(Arc::new(NoopDevice {}), &vec![pio])
    .unwrap();

// Register the MMIO resource.
manager
    .register_mmio_resources(Arc::new(NoopDevice {}), &vec![mmio])
    .unwrap();

// Dispatching I/O is the same.
manager.pio_write(PioAddress(0), &vec![b'o', b'k']).unwrap();
manager.mmio_write(MmioAddress(0), &vec![b'o', b'k']).unwrap();

Structs§

IoManager
System IO manager serving for all devices management and VM exit handling.

Enums§

Error
Error type for IoManager usage.

Traits§

MmioManager
Represents an object that provides MMIO manager operations.
PioManager
Represents an object that provides PIO manager operations.