xref: /cloud-hypervisor/hypervisor/src/hypervisor.rs (revision b440cb7d2330770cd415b63544a371d4caa2db3a)
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 #[cfg(target_arch = "x86_64")]
11 use crate::arch::x86::CpuIdEntry;
12 #[cfg(feature = "tdx")]
13 use crate::kvm::TdxCapabilities;
14 use crate::vm::Vm;
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     /// Failed to retrieve TDX capabilities
64     ///
65     #[error("Failed to retrieve TDX capabilities:{0}")]
66     TdxCapabilities(#[source] anyhow::Error),
67     ///
68     /// Failed to set partition property
69     ///
70     #[error("Failed to set partition property:{0}")]
71     SetPartitionProperty(#[source] anyhow::Error),
72 }
73 
74 ///
75 /// Result type for returning from a function
76 ///
77 pub type Result<T> = std::result::Result<T, HypervisorError>;
78 
79 ///
80 /// Trait to represent a Hypervisor
81 ///
82 /// This crate provides a hypervisor-agnostic interfaces
83 ///
84 pub trait Hypervisor: Send + Sync {
85     ///
86     /// Create a Vm using the underlying hypervisor
87     /// Return a hypervisor-agnostic Vm trait object
88     ///
89     fn create_vm(&self) -> Result<Arc<dyn Vm>>;
90     ///
91     /// Create a Vm of a specific type using the underlying hypervisor
92     /// Return a hypervisor-agnostic Vm trait object
93     ///
94     fn create_vm_with_type(&self, _vm_type: u64) -> Result<Arc<dyn Vm>> {
95         unreachable!()
96     }
97     #[cfg(target_arch = "x86_64")]
98     ///
99     /// Get the supported CpuID
100     ///
101     fn get_cpuid(&self) -> Result<Vec<CpuIdEntry>>;
102     ///
103     /// Check particular extensions if any
104     ///
105     fn check_required_extensions(&self) -> Result<()> {
106         Ok(())
107     }
108     #[cfg(target_arch = "aarch64")]
109     ///
110     /// Retrieve AArch64 host maximum IPA size supported by KVM.
111     ///
112     fn get_host_ipa_limit(&self) -> i32;
113     ///
114     /// Retrieve TDX capabilities
115     ///
116     #[cfg(feature = "tdx")]
117     fn tdx_capabilities(&self) -> Result<TdxCapabilities>;
118 }
119