xref: /cloud-hypervisor/hypervisor/src/hypervisor.rs (revision 686e6d50824fcc7403a51b91545899a6301d6216)
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 crate::HypervisorType;
16 use std::sync::Arc;
17 use thiserror::Error;
18 
19 #[derive(Error, Debug)]
20 ///
21 ///
22 pub enum HypervisorError {
23     ///
24     /// Hypervisor availability check error
25     ///
26     #[error("Failed to check availability of the hypervisor: {0}")]
27     HypervisorAvailableCheck(#[source] anyhow::Error),
28     ///
29     /// hypervisor creation error
30     ///
31     #[error("Failed to create the hypervisor: {0}")]
32     HypervisorCreate(#[source] anyhow::Error),
33     ///
34     /// Vm creation failure
35     ///
36     #[error("Failed to create Vm: {0}")]
37     VmCreate(#[source] anyhow::Error),
38     ///
39     /// Vm setup failure
40     ///
41     #[error("Failed to setup Vm: {0}")]
42     VmSetup(#[source] anyhow::Error),
43     ///
44     /// API version error
45     ///
46     #[error("Failed to get API Version: {0}")]
47     GetApiVersion(#[source] anyhow::Error),
48     ///
49     /// CpuId error
50     ///
51     #[error("Failed to get cpuid: {0}")]
52     GetCpuId(#[source] anyhow::Error),
53     ///
54     /// Failed to retrieve list of MSRs.
55     ///
56     #[error("Failed to get the list of supported MSRs: {0}")]
57     GetMsrList(#[source] anyhow::Error),
58     ///
59     /// API version is not compatible
60     ///
61     #[error("Incompatible API version")]
62     IncompatibleApiVersion,
63     ///
64     /// Checking extensions failed
65     ///
66     #[error("Checking extensions:{0}")]
67     CheckExtensions(#[source] anyhow::Error),
68     ///
69     /// Failed to retrieve TDX capabilities
70     ///
71     #[error("Failed to retrieve TDX capabilities:{0}")]
72     TdxCapabilities(#[source] anyhow::Error),
73     ///
74     /// Failed to set partition property
75     ///
76     #[error("Failed to set partition property:{0}")]
77     SetPartitionProperty(#[source] anyhow::Error),
78 }
79 
80 ///
81 /// Result type for returning from a function
82 ///
83 pub type Result<T> = std::result::Result<T, HypervisorError>;
84 
85 ///
86 /// Trait to represent a Hypervisor
87 ///
88 /// This crate provides a hypervisor-agnostic interfaces
89 ///
90 pub trait Hypervisor: Send + Sync {
91     ///
92     /// Returns the type of the hypervisor
93     ///
94     fn hypervisor_type(&self) -> HypervisorType;
95     ///
96     /// Create a Vm using the underlying hypervisor
97     /// Return a hypervisor-agnostic Vm trait object
98     ///
99     fn create_vm(&self) -> Result<Arc<dyn Vm>>;
100     ///
101     /// Create a Vm of a specific type using the underlying hypervisor
102     /// Return a hypervisor-agnostic Vm trait object
103     ///
104     fn create_vm_with_type(&self, _vm_type: u64) -> Result<Arc<dyn Vm>> {
105         unreachable!()
106     }
107     #[cfg(target_arch = "x86_64")]
108     ///
109     /// Get the supported CpuID
110     ///
111     fn get_cpuid(&self) -> Result<Vec<CpuIdEntry>>;
112     ///
113     /// Check particular extensions if any
114     ///
115     fn check_required_extensions(&self) -> Result<()> {
116         Ok(())
117     }
118     #[cfg(target_arch = "aarch64")]
119     ///
120     /// Retrieve AArch64 host maximum IPA size supported by KVM.
121     ///
122     fn get_host_ipa_limit(&self) -> i32;
123     ///
124     /// Retrieve TDX capabilities
125     ///
126     #[cfg(feature = "tdx")]
127     fn tdx_capabilities(&self) -> Result<TdxCapabilities>;
128 }
129