diff --git a/Cargo.toml b/Cargo.toml index 4c91c34..6288b28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2024" [features] +default = [] +fs = ["axvirtio-blk/fs"] [dependencies] log = "0.4" @@ -11,13 +13,14 @@ cfg-if = "1.0" spin = "0.9" # System independent crates provided by ArceOS. -axerrno = "0.1.0" +axerrno = "0.2.0" memory_addr = "0.4" -axvmconfig = { version = "0.1", default-features = false } +axvmconfig = { git = "https://github.com/DINGBROK423/axvmconfig.git", branch = "merge_add_virtio_mmio", default-features = false } axaddrspace = "0.1" axdevice_base = "0.1" range-alloc = { git = "https://github.com/arceos-hypervisor/range-alloc.git" } +axvirtio-blk = { git = "https://github.com/DINGBROK423/AxVirtio-blk.git", branch = "p1", default-features = false } [target.'cfg(target_arch = "aarch64")'.dependencies] arm_vgic = { git = "https://github.com/arceos-hypervisor/arm_vgic.git", features = ["vgicv3"] } diff --git a/src/config.rs b/src/config.rs index 9dbab4c..c343d4c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,12 +5,20 @@ use axvmconfig::EmulatedDeviceConfig; pub struct AxVmDeviceConfig { /// The vector of EmulatedDeviceConfig pub emu_configs: Vec, + /// The vector of VirtioBlkMmioDeviceConfig + pub virtio_blk_configs: Vec, } /// The implemention for AxVmDeviceConfig impl AxVmDeviceConfig { /// The new function for AxVmDeviceConfig - pub fn new(emu_configs: Vec) -> Self { - Self { emu_configs } + pub fn new( + emu_configs: Vec, + virtio_blk_configs: Vec, + ) -> Self { + Self { + emu_configs, + virtio_blk_configs, + } } } diff --git a/src/device.rs b/src/device.rs index 035acce..a3acef5 100644 --- a/src/device.rs +++ b/src/device.rs @@ -13,6 +13,7 @@ use axdevice_base::{BaseDeviceOps, BaseMmioDeviceOps, BasePortDeviceOps, BaseSys use axerrno::{AxResult, ax_err}; use axvmconfig::{EmulatedDeviceConfig, EmulatedDeviceType}; use memory_addr::{PhysAddr, is_aligned_4k}; +use axvirtio_blk::VirtioBlkDevice; use crate::AxVmDeviceConfig; @@ -109,7 +110,12 @@ fn panic_device_not_found( /// The implemention for AxVmDevices impl AxVmDevices { /// According AxVmDeviceConfig to init the AxVmDevices - pub fn new(config: AxVmDeviceConfig) -> Self { + pub fn new( + config: AxVmDeviceConfig, + read_guest_mem: Arc AxResult> + Send + Sync>, + write_guest_mem: Arc AxResult<()> + Send + Sync>, + inject_irq: Arc AxResult + Send + Sync>, + ) -> Self { let mut this = Self { emu_mmio_devices: AxEmuMmioDevices::new(), emu_sys_reg_devices: AxEmuSysRegDevices::new(), @@ -118,6 +124,25 @@ impl AxVmDevices { }; Self::init(&mut this, &config.emu_configs); + + // Initialize Virtio-blk devices + for blk_config in &config.virtio_blk_configs { + match VirtioBlkDevice::new( + blk_config, + read_guest_mem.clone(), + write_guest_mem.clone(), + inject_irq.clone(), + ) { + Ok(dev) => { + info!("Virtio-blk device initialized at {}", blk_config.mmio_base); + this.add_mmio_dev(Arc::new(dev)); + } + Err(e) => { + error!("Failed to initialize Virtio-blk device: {:?}", e); + } + } + } + this } @@ -374,7 +399,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_mmio_dev(addr) { log_device_io("mmio", addr, emu_dev.address_range(), true, width); - return emu_dev.handle_read(addr, width); + return Ok(emu_dev.handle_read(addr, width)?); } panic_device_not_found("mmio", addr, true, width); } @@ -389,7 +414,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_mmio_dev(addr) { log_device_io("mmio", addr, emu_dev.address_range(), false, width); - return emu_dev.handle_write(addr, width, val); + return Ok(emu_dev.handle_write(addr, width, val)?); } panic_device_not_found("mmio", addr, false, width); } @@ -399,7 +424,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_sys_reg_dev(addr) { log_device_io("sys_reg", addr.0, emu_dev.address_range(), true, width); - return emu_dev.handle_read(addr, width); + return Ok(emu_dev.handle_read(addr, width)?); } panic_device_not_found("sys_reg", addr, true, width); } @@ -414,7 +439,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_sys_reg_dev(addr) { log_device_io("sys_reg", addr.0, emu_dev.address_range(), false, width); - return emu_dev.handle_write(addr, width, val); + return Ok(emu_dev.handle_write(addr, width, val)?); } panic_device_not_found("sys_reg", addr, false, width); } @@ -424,7 +449,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_port_dev(port) { log_device_io("port", port.0, emu_dev.address_range(), true, width); - return emu_dev.handle_read(port, width); + return Ok(emu_dev.handle_read(port, width)?); } panic_device_not_found("port", port, true, width); } @@ -434,7 +459,7 @@ impl AxVmDevices { if let Some(emu_dev) = self.find_port_dev(port) { log_device_io("port", port.0, emu_dev.address_range(), false, width); - return emu_dev.handle_write(port, width, val); + return Ok(emu_dev.handle_write(port, width, val)?); } panic_device_not_found("port", port, false, width); }