Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes alias type for hypervisor #963

Merged
merged 1 commit into from
Sep 3, 2024
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
18 changes: 14 additions & 4 deletions src/core/src/vmm/hv/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Ram>) -> Result<impl Hypervisor, VmmError> {
self::linux::Kvm::new(cpu, ram)
}

#[cfg(target_os = "windows")]
pub type Default = self::windows::Whp;
pub fn new(cpu: usize, ram: Arc<Ram>) -> Result<impl Hypervisor, VmmError> {
self::windows::Whp::new(cpu, ram)
}

#[cfg(target_os = "macos")]
pub type Default = self::macos::Hf;
pub fn new(cpu: usize, ram: Arc<Ram>) -> Result<impl Hypervisor, VmmError> {
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::Cpu<'_>, Self::CpuErr>;
Expand Down
22 changes: 15 additions & 7 deletions src/core/src/vmm/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Result<(), MainCpuError>>) {
fn main_cpu<H: Hypervisor>(
args: &CpuArgs<H>,
entry: usize,
map: RamMap,
status: Sender<Result<(), MainCpuError>>,
) {
// 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;
}
};
Expand Down Expand Up @@ -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<C: Cpu, H: Hypervisor>(mut cpu: C, args: &CpuArgs<H>) {
let mut devices = args
.devices
.map()
Expand Down Expand Up @@ -746,8 +754,8 @@ impl From<MsgType> for VmmLog {
}

/// Encapsulates arguments for a function to run a CPU.
struct CpuArgs {
hv: Arc<self::hv::Default>,
struct CpuArgs<H: Hypervisor> {
hv: Arc<H>,
ram: Arc<Ram>,
screen: Arc<<self::screen::Default as Screen>::Buffer>,
devices: Arc<DeviceTree>,
Expand Down Expand Up @@ -825,7 +833,7 @@ enum VmmError {
#[derive(Debug, Error)]
enum MainCpuError {
#[error("couldn't create vCPU")]
CreateCpuFailed(#[source] <self::hv::Default as Hypervisor>::CpuErr),
CreateCpuFailed(#[source] Box<dyn Error + Send>),

#[error("couldn't get vCPU states")]
GetCpuStatesFailed(#[source] Box<dyn Error + Send>),
Expand Down