1 // Copyright © 2019 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause 4 // 5 // Copyright © 2020, Microsoft Corporation 6 // 7 // Copyright 2018-2019 CrowdStrike, Inc. 8 // 9 // 10 use crate::vm::Vm; 11 #[cfg(target_arch = "x86_64")] 12 use crate::x86_64::CpuId; 13 #[cfg(target_arch = "x86_64")] 14 use crate::x86_64::MsrList; 15 use std::sync::Arc; 16 use thiserror::Error; 17 18 #[derive(Error, Debug)] 19 /// 20 /// 21 pub enum HypervisorError { 22 /// 23 /// hypervisor creation error 24 /// 25 #[error("Failed to create the hypervisor: {0}")] 26 HypervisorCreate(#[source] anyhow::Error), 27 /// 28 /// Vm creation failure 29 /// 30 #[error("Failed to create Vm: {0}")] 31 VmCreate(#[source] anyhow::Error), 32 /// 33 /// Vm setup failure 34 /// 35 #[error("Failed to setup Vm: {0}")] 36 VmSetup(#[source] anyhow::Error), 37 /// 38 /// API version error 39 /// 40 #[error("Failed to get API Version: {0}")] 41 GetApiVersion(#[source] anyhow::Error), 42 /// 43 /// CpuId error 44 /// 45 #[error("Failed to get cpuid: {0}")] 46 GetCpuId(#[source] anyhow::Error), 47 /// 48 /// Failed to retrieve list of MSRs. 49 /// 50 #[error("Failed to get the list of supported MSRs: {0}")] 51 GetMsrList(#[source] anyhow::Error), 52 /// 53 /// API version is not compatible 54 /// 55 #[error("Incompatible API version")] 56 IncompatibleApiVersion, 57 /// 58 /// Checking extensions failed 59 /// 60 #[error("Checking extensions:{0}")] 61 CheckExtensions(#[source] anyhow::Error), 62 } 63 64 /// 65 /// Result type for returning from a function 66 /// 67 pub type Result<T> = std::result::Result<T, HypervisorError>; 68 69 /// 70 /// Trait to represent a Hypervisor 71 /// 72 /// This crate provides a hypervisor-agnostic interfaces 73 /// 74 pub trait Hypervisor: Send + Sync { 75 /// 76 /// Create a Vm using the underlying hypervisor 77 /// Return a hypervisor-agnostic Vm trait object 78 /// 79 fn create_vm(&self) -> Result<Arc<dyn Vm>>; 80 /// 81 /// Create a Vm of a specific type using the underlying hypervisor 82 /// Return a hypervisor-agnostic Vm trait object 83 /// 84 fn create_vm_with_type(&self, _vm_type: u64) -> Result<Arc<dyn Vm>> { 85 unreachable!() 86 } 87 #[cfg(target_arch = "x86_64")] 88 /// 89 /// Get the supported CpuID 90 /// 91 fn get_cpuid(&self) -> Result<CpuId>; 92 /// 93 /// Check particular extensions if any 94 /// 95 fn check_required_extensions(&self) -> Result<()> { 96 Ok(()) 97 } 98 #[cfg(target_arch = "x86_64")] 99 /// 100 /// Retrieve the list of MSRs supported by the hypervisor. 101 /// 102 fn get_msr_list(&self) -> Result<MsrList>; 103 #[cfg(target_arch = "aarch64")] 104 /// 105 /// Retrieve AArch64 host maximum IPA size supported by KVM. 106 /// 107 fn get_host_ipa_limit(&self) -> i32; 108 } 109