VirtIO Core Abstraction

Relevant source files

This document covers the core abstraction layer that bridges the external virtio-drivers crate with the axdriver ecosystem. It focuses on device probing mechanisms, type conversion, error handling, and the fundamental infrastructure that enables VirtIO device integration. For information about specific VirtIO device implementations (block, network, GPU), see VirtIO Device Implementations.

Overview

The VirtIO core abstraction in axdriver_virtio serves as an adapter layer that translates between the virtio-drivers crate's interfaces and the standardized axdriver trait system. This layer handles device discovery, type mapping, and error conversion while maintaining compatibility with both MMIO and PCI transport mechanisms.

Sources: axdriver_virtio/src/lib.rs(L1 - L98) 

Device Probing Architecture

The VirtIO subsystem provides two primary device probing mechanisms that support different hardware configurations and virtualization environments.

MMIO Device Probing

flowchart TD
A["probe_mmio_device()"]
B["reg_base: *mut u8"]
C["VirtIOHeader"]
D["MmioTransport::new()"]
E["Transport Valid?"]
F["transport.device_type()"]
G["Return None"]
H["as_dev_type()"]
I["Type Supported?"]
J["Return (DeviceType, MmioTransport)"]

A --> B
B --> C
C --> D
D --> E
E --> F
E --> G
F --> H
H --> I
I --> G
I --> J

The probe_mmio_device function creates a VirtIO MMIO transport from a memory-mapped region and identifies the device type. It validates the VirtIO header structure and extracts device type information for compatibility checking.

Sources: axdriver_virtio/src/lib.rs(L38 - L53) 

PCI Device Probing

flowchart TD
A["probe_pci_device()"]
B["PciRoot, DeviceFunction, DeviceFunctionInfo"]
C["virtio_device_type()"]
D["as_dev_type()"]
E["Type Supported?"]
F["PciTransport::new()"]
G["Return None"]
H["Transport Created?"]
I["Return (DeviceType, PciTransport)"]

A --> B
B --> C
C --> D
D --> E
E --> F
E --> G
F --> H
H --> G
H --> I

The probe_pci_device function handles PCI-based VirtIO devices using the PCI bus infrastructure. It requires a HAL implementation for DMA operations and device access.

Sources: axdriver_virtio/src/lib.rs(L55 - L69) 

Type Conversion System

The core abstraction implements bidirectional type mapping between the virtio-drivers crate and the axdriver ecosystem.

Device Type Mapping

VirtIO Device Typeaxdriver Device TypeSupport Status
VirtIoDevType::BlockDeviceType::Block✓ Supported
VirtIoDevType::NetworkDeviceType::Net✓ Supported
VirtIoDevType::GPUDeviceType::Display✓ Supported
Other typesNoneNot supported
flowchart TD
subgraph axdriver_base["axdriver_base"]
    AB["DeviceType::Block"]
    AN["DeviceType::Net"]
    AD["DeviceType::Display"]
    NONE["None"]
end
subgraph as_dev_type()["as_dev_type()"]
    CONV["Type Conversion"]
end
subgraph virtio_drivers["virtio_drivers"]
    VB["VirtIoDevType::Block"]
    VN["VirtIoDevType::Network"]
    VG["VirtIoDevType::GPU"]
    VO["Other Types"]
end

CONV --> AB
CONV --> AD
CONV --> AN
CONV --> NONE
VB --> CONV
VG --> CONV
VN --> CONV
VO --> CONV

The as_dev_type function provides compile-time type conversion that filters supported VirtIO device types and maps them to corresponding axdriver types.

Sources: axdriver_virtio/src/lib.rs(L71 - L79) 

Error Handling Bridge

The abstraction layer provides comprehensive error conversion between VirtIO-specific errors and the standardized axdriver error types.

Error Mapping Table

VirtIO Erroraxdriver DevErrorDescription
QueueFullBadStateDevice queue at capacity
NotReadyAgainDevice not ready, retry later
WrongTokenBadStateInvalid operation token
AlreadyUsedAlreadyExistsResource already in use
InvalidParamInvalidParamInvalid parameter provided
DmaErrorNoMemoryDMA allocation failure
IoErrorIoI/O operation failure
UnsupportedUnsupportedUnsupported operation
ConfigSpaceTooSmallBadStateInsufficient config space
ConfigSpaceMissingBadStateMissing config space
DefaultBadStateGeneric error state
flowchart TD
subgraph axdriver_base::DevError["axdriver_base::DevError"]
    DE1["BadState"]
    DE2["Again"]
    DE3["NoMemory"]
    DE4["Io"]
    DE5["Unsupported"]
end
subgraph as_dev_err()["as_dev_err()"]
    ECONVERT["Error Conversion"]
end
subgraph virtio_drivers::Error["virtio_drivers::Error"]
    VE1["QueueFull"]
    VE2["NotReady"]
    VE3["DmaError"]
    VE4["IoError"]
    VE5["Unsupported"]
    VEX["Others..."]
end

ECONVERT --> DE1
ECONVERT --> DE2
ECONVERT --> DE3
ECONVERT --> DE4
ECONVERT --> DE5
VE1 --> ECONVERT
VE2 --> ECONVERT
VE3 --> ECONVERT
VE4 --> ECONVERT
VE5 --> ECONVERT
VEX --> ECONVERT

The as_dev_err function provides semantic mapping of VirtIO errors to axdriver error categories, enabling consistent error handling across the driver ecosystem.

Sources: axdriver_virtio/src/lib.rs(L81 - L97) 

Transport Abstraction Layer

The VirtIO core abstraction re-exports key transport components from the virtio-drivers crate, providing a unified interface for different VirtIO transport mechanisms.

Core Transport Types

flowchart TD
subgraph subGraph2["PCI Infrastructure"]
    PCIBUS["pci::bus module"]
    DEVFUNC["DeviceFunction"]
    DEVINFO["DeviceFunctionInfo"]
    PCIROOT["PciRoot"]
end
subgraph subGraph1["Supporting Types"]
    HAL["VirtIoHal Trait"]
    PADDR["PhysAddr"]
    BDIR["BufferDirection"]
end
subgraph subGraph0["Transport Layer"]
    TRANS["Transport Trait"]
    MMIO["MmioTransport"]
    PCI["PciTransport"]
end

HAL --> BDIR
HAL --> PADDR
MMIO --> HAL
PCI --> HAL
PCI --> PCIBUS
PCIBUS --> DEVFUNC
PCIBUS --> DEVINFO
PCIBUS --> PCIROOT
TRANS --> MMIO
TRANS --> PCI

The transport abstraction provides:

  • Transport Trait: Common interface for device communication
  • MmioTransport: Memory-mapped I/O transport for embedded systems
  • PciTransport: PCI bus transport for conventional systems
  • VirtIoHal: Hardware abstraction layer for memory management
  • PCI Infrastructure: Bus enumeration and device management

Sources: axdriver_virtio/src/lib.rs(L30 - L36) 

HAL Requirements

The VirtIO integration requires implementation of the VirtIoHal trait for hardware-specific operations:

flowchart TD
subgraph subGraph1["axdriver Integration"]
    PROBE["Device Probing"]
    TRANSPORT["Transport Creation"]
    DEVOPS["Device Operations"]
end
subgraph subGraph0["VirtIoHal Requirements"]
    DMA["DMA Memory Allocation"]
    ADDR["Physical Address Translation"]
    CACHE["Cache Coherency"]
    SYNC["Memory Synchronization"]
end

ADDR --> TRANSPORT
CACHE --> DEVOPS
DMA --> PROBE
SYNC --> DEVOPS

The HAL implementation must provide memory management capabilities that bridge between the host system's memory management and VirtIO device requirements.

Sources: axdriver_virtio/src/lib.rs(L4 - L7) 

Conditional Compilation Support

The VirtIO core abstraction supports selective compilation through feature flags:

FeaturePurpose
blockEnable VirtIO block device support
netEnable VirtIO network device support
gpuEnable VirtIO GPU device support

This design allows minimal binary size by including only required device types while maintaining the same core probing and abstraction infrastructure.

Sources: axdriver_virtio/src/lib.rs(L16 - L28)