2020-06-03 19:59:49 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
2020-06-26 00:06:14 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
|
2020-06-03 19:59:49 +00:00
|
|
|
//
|
2020-06-28 16:32:56 +00:00
|
|
|
// Copyright © 2020, Microsoft Corporation
|
2020-06-03 19:59:49 +00:00
|
|
|
//
|
|
|
|
// Copyright 2018-2019 CrowdStrike, Inc.
|
|
|
|
//
|
|
|
|
//
|
2022-05-11 16:21:25 +00:00
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
use crate::kvm::TdxCapabilities;
|
2020-06-03 19:59:49 +00:00
|
|
|
use crate::vm::Vm;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-12-03 23:24:57 +00:00
|
|
|
use crate::x86_64::CpuId;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
use crate::x86_64::MsrList;
|
2020-06-03 19:59:49 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
///
|
|
|
|
///
|
|
|
|
pub enum HypervisorError {
|
2020-12-03 23:24:57 +00:00
|
|
|
///
|
|
|
|
/// hypervisor creation error
|
|
|
|
///
|
|
|
|
#[error("Failed to create the hypervisor: {0}")]
|
|
|
|
HypervisorCreate(#[source] anyhow::Error),
|
2020-06-03 19:59:49 +00:00
|
|
|
///
|
|
|
|
/// Vm creation failure
|
|
|
|
///
|
|
|
|
#[error("Failed to create Vm: {0}")]
|
|
|
|
VmCreate(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Vm setup failure
|
|
|
|
///
|
|
|
|
#[error("Failed to setup Vm: {0}")]
|
|
|
|
VmSetup(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// API version error
|
|
|
|
///
|
|
|
|
#[error("Failed to get API Version: {0}")]
|
|
|
|
GetApiVersion(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// CpuId error
|
|
|
|
///
|
2020-09-02 15:50:22 +00:00
|
|
|
#[error("Failed to get cpuid: {0}")]
|
2020-06-03 19:59:49 +00:00
|
|
|
GetCpuId(#[source] anyhow::Error),
|
2020-06-29 16:00:49 +00:00
|
|
|
///
|
|
|
|
/// Failed to retrieve list of MSRs.
|
|
|
|
///
|
|
|
|
#[error("Failed to get the list of supported MSRs: {0}")]
|
|
|
|
GetMsrList(#[source] anyhow::Error),
|
2020-09-07 10:10:58 +00:00
|
|
|
///
|
|
|
|
/// API version is not compatible
|
|
|
|
///
|
|
|
|
#[error("Incompatible API version")]
|
|
|
|
IncompatibleApiVersion,
|
2021-05-19 13:25:28 +00:00
|
|
|
///
|
|
|
|
/// Checking extensions failed
|
|
|
|
///
|
|
|
|
#[error("Checking extensions:{0}")]
|
|
|
|
CheckExtensions(#[source] anyhow::Error),
|
2022-03-30 10:06:52 +00:00
|
|
|
///
|
|
|
|
/// Failed to retrieve TDX capabilities
|
|
|
|
///
|
|
|
|
#[error("Failed to retrieve TDX capabilities:{0}")]
|
|
|
|
TdxCapabilities(#[source] anyhow::Error),
|
2020-06-03 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Result type for returning from a function
|
|
|
|
///
|
|
|
|
pub type Result<T> = std::result::Result<T, HypervisorError>;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Trait to represent a Hypervisor
|
|
|
|
///
|
|
|
|
/// This crate provides a hypervisor-agnostic interfaces
|
|
|
|
///
|
|
|
|
pub trait Hypervisor: Send + Sync {
|
|
|
|
///
|
|
|
|
/// Create a Vm using the underlying hypervisor
|
|
|
|
/// Return a hypervisor-agnostic Vm trait object
|
|
|
|
///
|
|
|
|
fn create_vm(&self) -> Result<Arc<dyn Vm>>;
|
|
|
|
///
|
2021-02-23 14:19:32 +00:00
|
|
|
/// Create a Vm of a specific type using the underlying hypervisor
|
|
|
|
/// Return a hypervisor-agnostic Vm trait object
|
|
|
|
///
|
2021-05-19 13:12:54 +00:00
|
|
|
fn create_vm_with_type(&self, _vm_type: u64) -> Result<Arc<dyn Vm>> {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2020-06-03 19:59:49 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
///
|
|
|
|
/// Get the supported CpuID
|
|
|
|
///
|
|
|
|
fn get_cpuid(&self) -> Result<CpuId>;
|
|
|
|
///
|
|
|
|
/// Check particular extensions if any
|
|
|
|
///
|
2021-05-19 13:09:13 +00:00
|
|
|
fn check_required_extensions(&self) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-29 16:00:49 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
///
|
|
|
|
/// Retrieve the list of MSRs supported by the hypervisor.
|
|
|
|
///
|
|
|
|
fn get_msr_list(&self) -> Result<MsrList>;
|
2021-06-09 13:31:49 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
///
|
|
|
|
/// Retrieve AArch64 host maximum IPA size supported by KVM.
|
|
|
|
///
|
|
|
|
fn get_host_ipa_limit(&self) -> i32;
|
2022-03-30 10:06:52 +00:00
|
|
|
///
|
|
|
|
/// Retrieve TDX capabilities
|
|
|
|
///
|
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
fn tdx_capabilities(&self) -> Result<TdxCapabilities>;
|
2020-06-03 19:59:49 +00:00
|
|
|
}
|