1#![warn(rust_2018_idioms)]
2#![allow(clippy::useless_conversion)]
3
4use std::path::PathBuf;
5
6use thiserror::Error;
7
8#[macro_use]
9extern crate log;
10
11mod arch;
12pub mod consts;
13mod fdt;
14#[cfg(target_os = "linux")]
15pub mod linux;
16#[cfg(target_os = "linux")]
17use linux as os;
18#[cfg(target_os = "macos")]
19pub mod macos;
20#[cfg(target_os = "macos")]
21use macos as os;
22mod hypercall;
23mod isolation;
24pub mod mem;
25pub(crate) mod paging;
26pub mod params;
27mod parking;
28mod serial;
29pub mod stats;
30mod vcpu;
31pub mod vm;
32
33pub use arch::*;
34
35#[derive(Debug, Error)]
36pub enum HypervisorError {
37 #[cfg(target_os = "linux")]
38 #[error("The KVM backend reported an error: {0}")]
39 BackendError(#[from] kvm_ioctls::Error),
40
41 #[cfg(target_os = "macos")]
42 #[error("The xhypervisor backend reported an error: {0}")]
43 BackendError(#[from] xhypervisor::Error),
44
45 #[error("IO Error: {0}")]
46 IOError(#[from] std::io::Error),
47
48 #[error("Invalid kernel path ({0})")]
49 InvalidKernelPath(PathBuf),
50
51 #[error(transparent)]
52 HermitImageError(#[from] crate::isolation::filemap::HermitImageError),
53
54 #[error("Unable to find Hermit image config in archive")]
55 HermitImageConfigNotFound,
56
57 #[error("Unable to parse Hermit image config: {0}")]
58 HermitImageConfigParseError(#[from] toml::de::Error),
59
60 #[error("Insufficient guest memory size: got = {got}, wanted = {wanted}")]
61 InsufficientGuestMemorySize {
62 got: byte_unit::Byte,
63 wanted: byte_unit::Byte,
64 },
65
66 #[error("Insufficient guest CPU count: got = {got}, wanted = {wanted}")]
67 InsufficientGuestCPUs { got: u32, wanted: u32 },
68
69 #[error("Kernel Loading Error: {0}")]
70 LoadedKernelError(#[from] vm::LoadKernelError),
71
72 #[error("Kernel doesn't support the necessary features: {0}")]
73 FeatureMismatch(&'static str),
74}
75
76impl HypervisorError {
77 #[cfg_attr(target_os = "macos", expect(unused))]
79 fn backend_invalid_value() -> Self {
80 Self::BackendError({
81 #[cfg(target_os = "linux")]
82 {
83 kvm_ioctls::Error::new(libc::EINVAL)
84 }
85 #[cfg(target_os = "macos")]
86 {
87 xhypervisor::Error::BadArg
88 }
89 })
90 }
91}
92
93pub type HypervisorResult<T> = Result<T, HypervisorError>;
94
95pub mod net;
96mod pci;
97mod virtio;