From f9f12fffc6bc4c7d12a02c809a8237fccf6f452d Mon Sep 17 00:00:00 2001 From: Yoda Lee Date: Tue, 2 May 2023 22:09:40 +0800 Subject: [PATCH] add virtio header of regsiter map, type and Status --- src/main.rs | 1 + src/virtio/header.rs | 105 +++++++++++++++++++++++++++++++++++++++++++ src/virtio/mod.rs | 1 + 3 files changed, 107 insertions(+) create mode 100644 src/virtio/header.rs create mode 100644 src/virtio/mod.rs diff --git a/src/main.rs b/src/main.rs index 5d7bf38..e386058 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ mod start; mod syscall; mod trap; mod uart; +mod virtio; mod vm; use crate::cpu::{get_cpuid, init_cpu}; diff --git a/src/virtio/header.rs b/src/virtio/header.rs new file mode 100644 index 0000000..1e825d9 --- /dev/null +++ b/src/virtio/header.rs @@ -0,0 +1,105 @@ +use bitflags::bitflags; +use volatile_register::{RO, RW, WO}; + +/// MMIO Device Register Layout +/// +/// Ref: 4.2.2 MMIO Device Register Layout +pub struct VirtioHeader { + // 0x00 + magic: RO, + version: RO, + device_id: RO, + vendor_id: RO, + // 0x10 + device_features: RO, + device_features_sel: WO, + _r0: [u32; 2], + // 0x20 + driver_features: WO, + driver_features_sel: WO, + _r1: [u32; 2], + // 0x30 + queue_sel: WO, + queue_num_max: RO, + queue_num: WO, + _r2: [u32; 1], + // 0x40 + _r3: [u32; 1], + queue_ready: RW, + _r4: [u32; 2], + // 0x50 + queue_notify: WO, + _r5: [u32; 3], + // 0x60 + interrupt_status: RO, + interrupt_ack: WO, + _r6: [u32; 2], + // 0x70 + status: RW, + _r7: [u32; 3], + // 0x80 + queue_desc_low: WO, + queue_desc_high: WO, + _r8: [u32; 2], + // 0x90 + queue_driver_low: WO, + queue_driver_high: WO, + _r9: [u32; 2], + // 0xa0 + queue_device_low: WO, + queue_device_high: WO, + _r10: [u32; 21], + // 0xfc + config_generation: RO, +} + +impl VirtioHeader { + /// Verify header + pub fn verify(&self) -> bool { + self.magic.read() == 0x74726976 + && self.version.read() == 2 + && self.device_id.read() == 2 + && self.vendor_id.read() == 0x554D4551 + } +} + +bitflags! { + pub struct DeviceStatus: u32 { + const ACKNOWLEDGE = 1; + const DRIVER = 2; + const DRIVER_OK = 4; + const FEATURES_OK = 8; + const DEVICE_NEEDS_RESET = 64; + const FAILED = 128; + } +} + +/// Types of virtio devices. +#[repr(u8)] +#[derive(Debug, Eq, PartialEq)] +#[allow(unused)] +pub enum DeviceType { + Invalid = 0, + Network = 1, + Block = 2, + Console = 3, + EntropySource = 4, + MemoryBallooning = 5, + IoMemory = 6, + Rpmsg = 7, + ScsiHost = 8, + _9pTransport = 9, + Mac80211 = 10, + RprocSerial = 11, + VirtioCAIF = 12, + MemoryBalloon = 13, + GPU = 16, + TimerClock = 17, + Input = 18, + Socket = 19, + Crypto = 20, + SignalDistributionModule = 21, + Pstore = 22, + IOMMU = 23, + Memory = 24, +} diff --git a/src/virtio/mod.rs b/src/virtio/mod.rs new file mode 100644 index 0000000..f505d68 --- /dev/null +++ b/src/virtio/mod.rs @@ -0,0 +1 @@ +pub mod header;