diff --git a/src/core/src/vmm/hv/mod.rs b/src/core/src/vmm/hv/mod.rs index 8fa270e33..fc20a27cb 100644 --- a/src/core/src/vmm/hv/mod.rs +++ b/src/core/src/vmm/hv/mod.rs @@ -1,4 +1,8 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +use super::hw::Ram; +use super::VmmError; use std::error::Error; +use std::sync::Arc; #[cfg(target_os = "linux")] mod linux; @@ -8,20 +12,26 @@ mod macos; mod windows; #[cfg(target_os = "linux")] -pub type Default = self::linux::Kvm; +pub fn new(cpu: usize, ram: Arc) -> Result { + self::linux::Kvm::new(cpu, ram) +} #[cfg(target_os = "windows")] -pub type Default = self::windows::Whp; +pub fn new(cpu: usize, ram: Arc) -> Result { + self::windows::Whp::new(cpu, ram) +} #[cfg(target_os = "macos")] -pub type Default = self::macos::Hf; +pub fn new(cpu: usize, ram: Arc) -> Result { + self::macos::Hf::new(cpu, ram) +} /// Underlying hypervisor (e.g. KVM on Linux). pub trait Hypervisor: Send + Sync { type Cpu<'a>: Cpu where Self: 'a; - type CpuErr: Error + Send; + type CpuErr: Error + Send + 'static; /// This method must be called by a thread that is going to drive the returned CPU. fn create_cpu(&self, id: usize) -> Result, Self::CpuErr>; diff --git a/src/core/src/vmm/mod.rs b/src/core/src/vmm/mod.rs index b940ccba0..3f4bfe4ae 100644 --- a/src/core/src/vmm/mod.rs +++ b/src/core/src/vmm/mod.rs @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 use self::hv::{Cpu, CpuExit, CpuIo, CpuStates, Hypervisor}; use self::hw::{setup_devices, Device, DeviceContext, DeviceTree, Ram, RamBuilder, RamMap}; use self::kernel::Kernel; @@ -391,7 +392,7 @@ pub unsafe extern "C" fn vmm_run( // Setup hypervisor. let ram = Arc::new(ram); - let hv = match self::hv::Default::new(8, ram.clone()) { + let hv = match self::hv::new(8, ram.clone()) { Ok(v) => Arc::new(v), Err(e) => { *err = RustError::with_source("couldn't setup a hypervisor", e); @@ -464,12 +465,19 @@ pub unsafe extern "C" fn vmm_draw(vmm: *mut Vmm) -> *mut RustError { } } -fn main_cpu(args: &CpuArgs, entry: usize, map: RamMap, status: Sender>) { +fn main_cpu( + args: &CpuArgs, + entry: usize, + map: RamMap, + status: Sender>, +) { // Create vCPU. let mut cpu = match args.hv.create_cpu(0) { Ok(v) => v, Err(e) => { - status.send(Err(MainCpuError::CreateCpuFailed(e))).unwrap(); + status + .send(Err(MainCpuError::CreateCpuFailed(Box::new(e)))) + .unwrap(); return; } }; @@ -589,7 +597,7 @@ fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(), M .map_err(|e| MainCpuError::CommitCpuStatesFailed(Box::new(e))) } -fn run_cpu(mut cpu: impl Cpu, args: &CpuArgs) { +fn run_cpu(mut cpu: C, args: &CpuArgs) { let mut devices = args .devices .map() @@ -746,8 +754,8 @@ impl From for VmmLog { } /// Encapsulates arguments for a function to run a CPU. -struct CpuArgs { - hv: Arc, +struct CpuArgs { + hv: Arc, ram: Arc, screen: Arc<::Buffer>, devices: Arc, @@ -825,7 +833,7 @@ enum VmmError { #[derive(Debug, Error)] enum MainCpuError { #[error("couldn't create vCPU")] - CreateCpuFailed(#[source] ::CpuErr), + CreateCpuFailed(#[source] Box), #[error("couldn't get vCPU states")] GetCpuStatesFailed(#[source] Box),