LekKit / RVVM

The RISC-V Virtual Machine
GNU General Public License v3.0
911 stars 65 forks source link

need help translating kvm/vbox/qemu/ect PCI registration to RVVM #157

Open ZLangJIT opened 4 days ago

ZLangJIT commented 4 days ago

im struggling to figure out how i might implement a very basic virtio-pci device in rvvm for virtio-over-pci transport based kernel drivers

the virtio spec does not appear to specify exactly how to do such, and qemu appears to use a pci virtio-pci device which manages sub virtio devices (all virtio devices plug into virtio-pci itself which virtio-pci itself plugs into pci) however the spec also does not clarify such as it appears to only talk about virtio-pci devices such as the ones that plug into qemu's virtio-pci

and i am not sure how to begin porting the qemu's virtio-pci over to rvvm as both use quite a different pci interface

ZLangJIT commented 4 days ago

hmm i think i may have found one that more closely resembles rvvm in comparison to qemu

unfortunately github complains the repository has not yet been indexed (so we cannot search globally for symbols)

https://github.com/matthias-prangl/VirtualBox6/blob/2db58873a4141dd42b3f7d34dbd713dca2fa1984/src/VBox/Devices/VirtIO/Virtio.cpp#L832-#L905

however we do have an indexable mirror

https://github.com/mirror/vbox/blob/master/src/VBox/Devices/VirtIO/VirtioCore.h

along with a gpu device https://github.com/mirror/vbox/blob/master/src/VBox/Devices/EFI/Firmware/OvmfPkg/VirtioGpuDxe/Commands.c

ZLangJIT commented 4 days ago

unfortunately it seems almost every "virtio-pci" device uses a different interface for registering with the PCI itself

https://github.com/isclouder/microv/blob/2992853ddc8cc1328ec2dca8f621ef8c09560807/virtio-pci.c#L253

https://github.com/sysprog21/riscv-emu/blob/7cc6525934fc8983c67b81868c78610f3ad3797b/virtio.c#L220

https://github.com/mhhf/jslinux-truebit/blob/fbd09ab2a7f3a59a010f1ad44b22434ca6d8e88f/src/virtio.c#L220

https://github.com/mirror/vbox/blob/master/src/VBox/Devices/VirtIO/VirtioCore.cpp#L2822

meanwhile RVVM's pci interface is almost entirely different

typedef struct {
    uint16_t vendor_id;
    uint16_t device_id;
    uint16_t class_code;
    uint8_t  prog_if;
    uint8_t  rev;
    uint8_t  irq_pin;
    rvvm_mmio_dev_t bar[PCI_FUNC_BARS];
} pci_func_desc_t;
ZLangJIT commented 4 days ago

https://github.com/tyler274/magenta/blob/3f71c5b1c71a9802d0316a4f51e70da9c0e0014a/system/ulib/hypervisor/virtio.c#L162 hmm this appears to be simpler

ZLangJIT commented 4 days ago

OK i found one

RVVM contains a Realtek NIC network card that attaches VIA PCI https://github.com/LekKit/RVVM/blob/staging/src/devices/rtl8169.c#L481

and likewise qemu also contains a Realtek NIC network card that attaches VIA PCI https://github.com/qemu/qemu/blob/master/hw/net/rtl8139.c#L3359

all other devices which are in both RVVM and QEMU either connect via the PCI in qemu but the MMIO in RVVM or vice versa or are passthrough in QEMU

so the Realtek network card is the ONLY device in which BOTH RVVM and QEMU connect via PCI

LekKit commented 3 days ago

Please have a look at NVMe device example: https://github.com/LekKit/RVVM/blob/419e1796692a3d6cd838f8933224f354f7a4169b/src/devices/nvme.c#L586

It should be fairly straightforward to use, PCI only defines a device identification mechanism (Device/Vendor IDs, device class). The actual device BARs are simply MMIO regions, so all the usual RVVM API conventions apply in the same way (MMIO read/write callbacks, mappings, alignment, etc from the doc)

LekKit commented 3 days ago

I also want to ask you to stop opening more issues because it is very confusing to navigate them, and some questions you asked lately are not easily understandable.

LekKit commented 1 day ago

PCI Device class codes explanation: https://admin.pci-ids.ucw.cz/read/PD/ PCI Vendor/Device ID listing: https://admin.pci-ids.ucw.cz/read/PC/

Dump PCI device address / class code / vendor & device ID / revision:

lspci -n

image

Remove -n argument to see decoded device model names (From the PCI ID repository mentioned above)