Device System

OC2R has three layers for adding devices to a virtual computer. Each serves a different use case.

VMDevice (low-level)

Registers memory-mapped IO (MMIO) regions and interrupt lines directly on the RISC-V board. The guest OS needs a kernel driver to use these devices.

Lifecycle:

  1. mount(VMTarget, VMContext) — allocate resources, register MMIO and interrupts
  2. unmount() — stop I/O, deregister
  3. dispose() — free all resources

Built-in implementations:

DevicePurpose
DiskDriveDeviceFloppy disk I/O
HardDriveDeviceBlock storage
KeyboardDeviceKeyboard input
MonitorDeviceFramebuffer display
NetworkInterfaceCardDeviceEthernet networking
InternetCardDeviceInternet connectivity
NetworkTunnelDeviceNetwork tunnel
MemoryDeviceRAM expansion
PciCardCageDevicePCI device slot
ProjectorDevice3D projection
FirmwareFlashStorageDeviceFirmware storage
SimpleFramebufferDeviceSimple framebuffer
PciRootPortDevicePCI root port

RPCDevice (high-level)

Communicates via JSON-RPC over a VirtIO serial port. No kernel driver needed — the guest runs a simple client that sends JSON method calls and receives JSON responses.

Interface:

public interface RPCDevice {
    String getTypeName();
    List<RPCMethodGroup> getMethodGroups();
    Optional<RPCInvocation> getInvocation(RPCMethod method);
}

ObjectDevice (@Callback)

A convenience implementation of RPCDevice using reflection. Annotate methods with @Callback and they are automatically exposed.

Example:

public class MyDevice extends ObjectDevice implements NamedDevice {
    @Callback
    public double getTemperature() {
        return 36.6;
    }

    @Override
    public String getDeviceName() {
        return "Thermometer";
    }
}

Available annotations:

AnnotationUsage
@CallbackExpose a method to the VM; supports synchronize (main thread), name, description
@ParameterDescribe a method parameter

Optional interfaces:

InterfacePurpose
NamedDeviceCustom device name
DocumentedDeviceExtended documentation
LifecycleAwareDeviceMount/unmount callbacks

FirmwareLoader

Special devices that provide boot firmware. The base interface adds getFirmware() returning a Firmware object (OpenSBI, Linux kernel, etc.). The lifecycle manager calls load() during initialization.

Implementations:

  • MinuxFirmware — loads the minux kernel
  • ByteBufferFlashStorageDevice — loads firmware from a byte buffer

Device Bus Integration

Devices are exposed to the bus through providers:

// BlockDeviceProvider — scan blocks for devices
public interface BlockDeviceProvider {
    void getDevices(BlockDeviceQuery query, List<Device> devices);
}

// ItemDeviceProvider — scan items for devices
public interface ItemDeviceProvider {
    void getDevices(ItemDeviceQuery query, List<Device> devices);
}

Registration:

Providers are registered via NeoForge DeferredRegister in Providers.java. Example item providers:

  • CPUItemDeviceProvider — CPUs of various tiers
  • MemoryItemDeviceProvider — RAM modules
  • HardDriveItemDeviceProvider — storage drives
  • NetworkInterfaceCardItemDeviceProvider — NIC
  • RedstoneInterfaceCardItemDeviceProvider — redstone I/O

Device Types

TypeTagExample
MEMORYoc2r:devices/memoryRAM stick
HARD_DRIVEoc2r:devices/hard_driveHDD/SSD
FLASH_MEMORYoc2r:devices/flash_memoryBIOS/EEPROM
CARDoc2r:devices/cardNetwork card
CPUoc2r:devices/cpuProcessor
ROBOT_MODULEoc2r:devices/robot_moduleRobot upgrade
FLOPPYoc2r:devices/floppyFloppy disk
NETWORK_TUNNELoc2r:devices/network_tunnelTunnel card

OC2R source · 2d55dce5a222