Skip to content
Merged
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ vm-core = { path = "crates/vm-core" }
vm-device = { path = "crates/vm-device" }
vm-fdt = "0.3.0"
vm-machine = { path = "crates/vm-machine" }
vm-mm = { path = "crates/vm-mm" }
vm-pci = { path = "crates/vm-pci" }
vm-virtio = { path = "crates/vm-virtio" }
zerocopy = { version = "0.8.33", features = ["derive"] }
1 change: 1 addition & 0 deletions crates/vm-bootloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ anyhow.workspace = true
thiserror.workspace = true
vm-core.workspace = true
vm-fdt.workspace = true
vm-mm.workspace = true

[target.'cfg(target_arch = "aarch64")'.dependencies]
tracing.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/boot_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::slice::Iter;
use vm_core::arch::irq::InterruptController;
use vm_core::arch::layout;
use vm_core::device::mmio::mmio_device::MmioDevice;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_core::virt::Virt;
use vm_mm::manager::MemoryAddressSpace;

pub mod arch;

Expand Down
4 changes: 2 additions & 2 deletions crates/vm-bootloader/src/boot_loader/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use vm_core::arch::aarch64::vcpu::AArch64Vcpu;
use vm_core::arch::irq::InterruptController;
use vm_core::arch::layout::MemoryLayout;
use vm_core::device::mmio::mmio_device::MmioDevice;
use vm_core::mm::allocator::MemoryContainer;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_core::virt::Virt;
use vm_fdt::FdtWriter;
use vm_mm::allocator::MemoryContainer;
use vm_mm::manager::MemoryAddressSpace;

use crate::boot_loader::BootLoader;
use crate::boot_loader::BootLoaderBuilder;
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/boot_loader/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::slice::Iter;

use vm_core::arch::irq::InterruptController;
use vm_core::device::mmio::mmio_device::MmioDevice;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_core::virt::Virt;
use vm_mm::manager::MemoryAddressSpace;

use crate::boot_loader::BootLoader;
use crate::boot_loader::BootLoaderBuilder;
Expand Down
4 changes: 2 additions & 2 deletions crates/vm-bootloader/src/initrd_loader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fs;
use std::path::Path;

use vm_core::mm::allocator::MemoryContainer;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_mm::allocator::MemoryContainer;
use vm_mm::manager::MemoryAddressSpace;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm-bootloader/src/kernel_loader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use vm_core::mm::manager::MemoryAddressSpace;
use vm_mm::manager::MemoryAddressSpace;

pub mod linux;

Expand Down
4 changes: 2 additions & 2 deletions crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs;
use std::path::Path;

use header::*;
use vm_core::mm::allocator::MemoryContainer;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_mm::allocator::MemoryContainer;
use vm_mm::manager::MemoryAddressSpace;

use crate::kernel_loader::Error;
use crate::kernel_loader::KernelLoader;
Expand Down
4 changes: 2 additions & 2 deletions crates/vm-bootloader/src/kernel_loader/linux/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::fs;
use std::path::Path;

use tracing::debug;
use vm_core::mm::allocator::MemoryContainer;
use vm_core::mm::manager::MemoryAddressSpace;
use vm_mm::allocator::MemoryContainer;
use vm_mm::manager::MemoryAddressSpace;
use zerocopy::FromBytes;

use crate::kernel_loader::Error;
Expand Down
1 change: 1 addition & 0 deletions crates/vm-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ memmap2 = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
vm-fdt = { workspace = true }
vm-mm = { workspace = true }

[target.'cfg(target_arch = "aarch64")'.dependencies]
gdbstub = { workspace = true }
Expand Down
6 changes: 6 additions & 0 deletions crates/vm-core/src/arch/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub enum Error {
DtbAlreadySet,
}

impl From<Error> for crate::error::Error {
fn from(err: Error) -> Self {
crate::error::Error::LayoutError(err)
}
}

pub type Result<T> = core::result::Result<T, Error>;

pub trait MemoryLayout: Clone {
Expand Down
12 changes: 12 additions & 0 deletions crates/vm-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub enum Error {
#[cfg(feature = "hvp")]
#[error("{0}")]
ApplevisorError(applevisor::error::HypervisorError),
#[error("{0}")]
MemoryError(vm_mm::error::Error),
#[error("{0}")]
LayoutError(crate::arch::layout::Error),
#[error("{0}")]
Internal(String),
#[error("Unknown error: {0}")]
Unknown(String),
}
Expand All @@ -28,4 +34,10 @@ impl From<applevisor::error::HypervisorError> for Error {
}
}

impl From<vm_mm::error::Error> for Error {
fn from(err: vm_mm::error::Error) -> Self {
Error::MemoryError(err)
}
}

pub type Result<T> = std::result::Result<T, Error>;
1 change: 0 additions & 1 deletion crates/vm-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ pub mod arch;
pub mod debug;
pub mod device;
pub mod error;
pub mod mm;
pub mod virt;
4 changes: 0 additions & 4 deletions crates/vm-core/src/vcpu.rs

This file was deleted.

19 changes: 10 additions & 9 deletions crates/vm-core/src/virt.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::sync::Arc;
use std::sync::Mutex;

use vm_mm::allocator::MemoryContainer;
use vm_mm::manager::MemoryAddressSpace;

use crate::arch::Arch;
use crate::arch::irq::InterruptController;
use crate::arch::vcpu::Vcpu;
use crate::device::mmio::MmioLayout;
use crate::device::vm_exit::DeviceVmExitHandler;
use crate::error::Result;
use crate::mm::allocator::MemoryContainer;
use crate::mm::manager::MemoryAddressSpace;

#[cfg(feature = "kvm")]
pub mod kvm;
Expand All @@ -23,22 +24,22 @@ pub trait Virt: Sized {

fn new(num_vcpus: usize) -> Result<Self>;

fn init_irq(&mut self) -> anyhow::Result<Arc<dyn InterruptController>>;
fn init_irq(&mut self) -> Result<Arc<dyn InterruptController>>;
fn init_memory(
&mut self,
mmio_layout: &MmioLayout,
memory: &mut MemoryAddressSpace<Self::Memory>,
memory_size: u64,
) -> anyhow::Result<()>;
fn post_init(&mut self) -> anyhow::Result<()>;
) -> Result<()>;
fn post_init(&mut self) -> Result<()>;

fn get_layout(&self) -> &<Self::Arch as Arch>::Layout;
fn get_layout_mut(&mut self) -> &mut <Self::Arch as Arch>::Layout;

fn get_vcpu_number(&self) -> usize;
fn get_vcpu_mut(&mut self, vcpu: u64) -> anyhow::Result<Option<&mut Self::Vcpu>>;
fn get_vcpus(&self) -> anyhow::Result<&Vec<Self::Vcpu>>;
fn get_vcpus_mut(&mut self) -> anyhow::Result<&mut Vec<Self::Vcpu>>;
fn get_vcpu_mut(&mut self, vcpu: u64) -> Result<Option<&mut Self::Vcpu>>;
fn get_vcpus(&self) -> Result<&Vec<Self::Vcpu>>;
fn get_vcpus_mut(&mut self) -> Result<&mut Vec<Self::Vcpu>>;

fn run(&mut self, device_manager: Arc<Mutex<dyn DeviceVmExitHandler>>) -> anyhow::Result<()>;
fn run(&mut self, device_manager: Arc<Mutex<dyn DeviceVmExitHandler>>) -> Result<()>;
}
42 changes: 17 additions & 25 deletions crates/vm-core/src/virt/hvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;

use anyhow::anyhow;
use applevisor::gic::GicConfig;
use applevisor::memory::MemPerms;
use applevisor::vm::GicEnabled;
use applevisor::vm::VirtualMachine;
use applevisor::vm::VirtualMachineConfig;
use applevisor::vm::VirtualMachineInstance;
use vm_mm::manager::MemoryAddressSpace;

use crate::arch::Arch;
use crate::arch::aarch64::AArch64;
Expand All @@ -30,7 +30,7 @@ use crate::arch::vcpu::Vcpu;
use crate::device::mmio::MmioLayout;
use crate::device::vm_exit::DeviceVmExitHandler;
use crate::error::Error;
use crate::mm::manager::MemoryAddressSpace;
use crate::error::Result;
use crate::virt::Virt;
use crate::virt::hvp::irq_chip::HvpGicV3;
use crate::virt::hvp::mm::HvpAllocator;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Virt for Hvp {
type Vcpu = HvpVcpu;
type Memory = MemoryWrapper;

fn new(num_vcpus: usize) -> Result<Self, Error> {
fn new(num_vcpus: usize) -> Result<Self> {
let layout = AArch64Layout::default();

let mut vm_config = VirtualMachineConfig::default();
Expand Down Expand Up @@ -266,7 +266,7 @@ impl Virt for Hvp {
})
}

fn init_irq(&mut self) -> anyhow::Result<Arc<dyn InterruptController>> {
fn init_irq(&mut self) -> Result<Arc<dyn InterruptController>> {
Ok(self.gic_chip.clone())
}

Expand All @@ -275,7 +275,7 @@ impl Virt for Hvp {
_mmio_layout: &MmioLayout,
memory: &mut MemoryAddressSpace<MemoryWrapper>,
memory_size: u64,
) -> anyhow::Result<()> {
) -> Result<()> {
let allocator = HvpAllocator { vm: &self.vm };

for region in memory {
Expand All @@ -290,7 +290,7 @@ impl Virt for Hvp {
Ok(())
}

fn post_init(&mut self) -> anyhow::Result<()> {
fn post_init(&mut self) -> Result<()> {
Ok(())
}

Expand All @@ -306,35 +306,27 @@ impl Virt for Hvp {
self.num_vcpus
}

fn get_vcpu_mut(&mut self, vcpu_id: u64) -> anyhow::Result<Option<&mut HvpVcpu>> {
let vcpu = self
fn get_vcpu_mut(&mut self, vcpu_id: u64) -> Result<Option<&mut HvpVcpu>> {
Ok(self
.vcpus
.get_mut()
.ok_or_else(|| anyhow!("vcpu is not initialized"))?
.get_mut(vcpu_id as usize);

Ok(vcpu)
.ok_or_else(|| Error::Internal("vcpu is not initialized".to_string()))?
.get_mut(vcpu_id as usize))
}

fn get_vcpus(&self) -> anyhow::Result<&Vec<Self::Vcpu>> {
let vcpus = self
.vcpus
fn get_vcpus(&self) -> Result<&Vec<Self::Vcpu>> {
self.vcpus
.get()
.ok_or_else(|| anyhow!("vcpu is not initialized"))?;

Ok(vcpus)
.ok_or_else(|| Error::Internal("vcpu is not initialized".to_string()))
}

fn get_vcpus_mut(&mut self) -> anyhow::Result<&mut Vec<Self::Vcpu>> {
let vcpus = self
.vcpus
fn get_vcpus_mut(&mut self) -> Result<&mut Vec<Self::Vcpu>> {
self.vcpus
.get_mut()
.ok_or_else(|| anyhow!("vcpu is not initialized"))?;

Ok(vcpus)
.ok_or_else(|| Error::Internal("vcpu is not initialized".to_string()))
}

fn run(&mut self, device_manager: Arc<Mutex<dyn DeviceVmExitHandler>>) -> anyhow::Result<()> {
fn run(&mut self, device_manager: Arc<Mutex<dyn DeviceVmExitHandler>>) -> Result<()> {
let mmio_layout = device_manager.lock().unwrap().mmio_layout();

thread::scope(|s| {
Expand Down
7 changes: 3 additions & 4 deletions crates/vm-core/src/virt/hvp/mm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use applevisor::memory::Memory;
use applevisor::vm::VirtualMachineInstance;

use crate::mm::Error;
use crate::mm::allocator::Allocator;
use crate::mm::allocator::MemoryContainer;
use vm_mm::allocator::Allocator;
use vm_mm::allocator::MemoryContainer;
use vm_mm::error::Error;

pub struct MemoryWrapper(pub Memory);

Expand Down
Loading
Loading