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