Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ version = "0.1.0"
edition = "2024"

[features]
default = []
fs = ["axvirtio-blk/fs"]

[dependencies]
log = "0.4"
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"] }
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ use axvmconfig::EmulatedDeviceConfig;
pub struct AxVmDeviceConfig {
/// The vector of EmulatedDeviceConfig
pub emu_configs: Vec<EmulatedDeviceConfig>,
/// The vector of VirtioBlkMmioDeviceConfig
pub virtio_blk_configs: Vec<axvmconfig::VirtioBlkMmioDeviceConfig>,
}

/// The implemention for AxVmDeviceConfig
impl AxVmDeviceConfig {
/// The new function for AxVmDeviceConfig
pub fn new(emu_configs: Vec<EmulatedDeviceConfig>) -> Self {
Self { emu_configs }
pub fn new(
emu_configs: Vec<EmulatedDeviceConfig>,
virtio_blk_configs: Vec<axvmconfig::VirtioBlkMmioDeviceConfig>,
) -> Self {
Self {
emu_configs,
virtio_blk_configs,
}
}
}
39 changes: 32 additions & 7 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<dyn Fn(GuestPhysAddr, usize) -> AxResult<Vec<u8>> + Send + Sync>,
write_guest_mem: Arc<dyn Fn(GuestPhysAddr, &[u8]) -> AxResult<()> + Send + Sync>,
inject_irq: Arc<dyn Fn(usize) -> AxResult + Send + Sync>,
) -> Self {
let mut this = Self {
emu_mmio_devices: AxEmuMmioDevices::new(),
emu_sys_reg_devices: AxEmuSysRegDevices::new(),
Expand All @@ -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
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down