2020-06-08 00:50:38 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
2020-06-26 00:06:14 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
|
2020-06-08 00:50:38 +00:00
|
|
|
//
|
2020-06-28 16:32:56 +00:00
|
|
|
// Copyright © 2020, Microsoft Corporation
|
2020-06-08 00:50:38 +00:00
|
|
|
//
|
|
|
|
// Copyright 2018-2019 CrowdStrike, Inc.
|
|
|
|
//
|
2020-06-03 19:23:56 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
//! A generic abstraction around hypervisor functionality
|
|
|
|
//!
|
|
|
|
//! This crate offers a trait abstraction for underlying hypervisors
|
|
|
|
//!
|
|
|
|
//! # Platform support
|
|
|
|
//!
|
|
|
|
//! - x86_64
|
|
|
|
//! - arm64
|
|
|
|
//!
|
|
|
|
|
2022-06-30 16:34:04 +00:00
|
|
|
#![allow(clippy::significant_drop_in_scrutinee)]
|
|
|
|
|
2020-08-05 10:00:56 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate anyhow;
|
2020-08-30 08:44:39 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-08-05 10:00:56 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2020-06-02 21:24:33 +00:00
|
|
|
|
2020-12-10 21:24:19 +00:00
|
|
|
/// Architecture specific definitions
|
|
|
|
#[macro_use]
|
|
|
|
pub mod arch;
|
|
|
|
|
2020-12-04 23:35:29 +00:00
|
|
|
#[cfg(feature = "kvm")]
|
2020-06-03 19:23:56 +00:00
|
|
|
/// KVM implementation module
|
|
|
|
pub mod kvm;
|
|
|
|
|
2020-12-03 22:14:04 +00:00
|
|
|
/// Microsoft Hypervisor implementation module
|
|
|
|
#[cfg(all(feature = "mshv", target_arch = "x86_64"))]
|
|
|
|
pub mod mshv;
|
|
|
|
|
2020-06-03 19:59:49 +00:00
|
|
|
/// Hypevisor related module
|
2022-05-11 15:41:37 +00:00
|
|
|
mod hypervisor;
|
2020-06-03 19:59:49 +00:00
|
|
|
|
2020-06-03 19:58:00 +00:00
|
|
|
/// Vm related module
|
2022-05-11 15:36:08 +00:00
|
|
|
mod vm;
|
2020-06-03 19:58:00 +00:00
|
|
|
|
2020-06-03 19:23:56 +00:00
|
|
|
/// CPU related module
|
|
|
|
mod cpu;
|
|
|
|
|
2020-07-16 14:50:36 +00:00
|
|
|
/// Device related module
|
|
|
|
mod device;
|
|
|
|
|
2020-07-03 14:27:53 +00:00
|
|
|
pub use cpu::{HypervisorCpuError, Vcpu, VmExit};
|
2022-07-21 13:15:15 +00:00
|
|
|
pub use device::HypervisorDeviceError;
|
2022-05-11 15:36:08 +00:00
|
|
|
pub use hypervisor::{Hypervisor, HypervisorError};
|
2022-05-29 08:54:23 +00:00
|
|
|
#[cfg(all(feature = "kvm", target_arch = "aarch64"))]
|
|
|
|
pub use kvm::{aarch64, GicState};
|
2022-05-11 16:21:25 +00:00
|
|
|
use std::sync::Arc;
|
2022-05-11 15:36:08 +00:00
|
|
|
pub use vm::{
|
|
|
|
DataMatch, HypervisorVmError, InterruptSourceConfig, LegacyIrqSourceConfig, MsiIrqSourceConfig,
|
|
|
|
Vm, VmOps,
|
|
|
|
};
|
2020-07-14 11:34:42 +00:00
|
|
|
|
2022-07-20 10:41:13 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum HypervisorType {
|
|
|
|
Kvm,
|
|
|
|
Mshv,
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:34:42 +00:00
|
|
|
pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> {
|
|
|
|
#[cfg(feature = "kvm")]
|
2022-07-22 11:10:16 +00:00
|
|
|
if kvm::KvmHypervisor::is_available()? {
|
|
|
|
return kvm::KvmHypervisor::new();
|
|
|
|
}
|
2020-07-14 11:34:42 +00:00
|
|
|
|
2020-12-03 23:24:57 +00:00
|
|
|
#[cfg(feature = "mshv")]
|
2022-07-22 11:10:16 +00:00
|
|
|
if mshv::MshvHypervisor::is_available()? {
|
|
|
|
return mshv::MshvHypervisor::new();
|
|
|
|
}
|
2020-12-03 23:24:57 +00:00
|
|
|
|
2022-07-22 11:10:16 +00:00
|
|
|
Err(HypervisorError::HypervisorCreate(anyhow!(
|
|
|
|
"no supported hypervisor"
|
|
|
|
)))
|
2020-07-14 11:34:42 +00:00
|
|
|
}
|
2021-03-10 21:26:30 +00:00
|
|
|
|
|
|
|
// Returns a `Vec<T>` with a size in bytes at least as large as `size_in_bytes`.
|
|
|
|
fn vec_with_size_in_bytes<T: Default>(size_in_bytes: usize) -> Vec<T> {
|
|
|
|
let rounded_size = (size_in_bytes + size_of::<T>() - 1) / size_of::<T>();
|
|
|
|
let mut v = Vec::with_capacity(rounded_size);
|
|
|
|
v.resize_with(rounded_size, T::default);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
|
|
|
// The kvm API has many structs that resemble the following `Foo` structure:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// #[repr(C)]
|
|
|
|
// struct Foo {
|
|
|
|
// some_data: u32
|
|
|
|
// entries: __IncompleteArrayField<__u32>,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// In order to allocate such a structure, `size_of::<Foo>()` would be too small because it would not
|
|
|
|
// include any space for `entries`. To make the allocation large enough while still being aligned
|
|
|
|
// for `Foo`, a `Vec<Foo>` is created. Only the first element of `Vec<Foo>` would actually be used
|
|
|
|
// as a `Foo`. The remaining memory in the `Vec<Foo>` is for `entries`, which must be contiguous
|
|
|
|
// with `Foo`. This function is used to make the `Vec<Foo>` with enough space for `count` entries.
|
|
|
|
use std::mem::size_of;
|
|
|
|
pub fn vec_with_array_field<T: Default, F>(count: usize) -> Vec<T> {
|
|
|
|
let element_space = count * size_of::<F>();
|
|
|
|
let vec_size_bytes = size_of::<T>() + element_space;
|
|
|
|
vec_with_size_in_bytes(vec_size_bytes)
|
|
|
|
}
|
2022-07-08 17:52:18 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
/// User memory region structure
|
|
|
|
///
|
|
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct UserMemoryRegion {
|
|
|
|
pub slot: u32,
|
|
|
|
pub guest_phys_addr: u64,
|
|
|
|
pub memory_size: u64,
|
|
|
|
pub userspace_addr: u64,
|
|
|
|
pub flags: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Flags for user memory region
|
|
|
|
///
|
|
|
|
pub const USER_MEMORY_REGION_READ: u32 = 1;
|
|
|
|
pub const USER_MEMORY_REGION_WRITE: u32 = 1 << 1;
|
|
|
|
pub const USER_MEMORY_REGION_EXECUTE: u32 = 1 << 2;
|
|
|
|
pub const USER_MEMORY_REGION_LOG_DIRTY: u32 = 1 << 3;
|
2022-07-14 15:17:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum MpState {
|
|
|
|
#[cfg(feature = "kvm")]
|
|
|
|
Kvm(kvm_bindings::kvm_mp_state),
|
|
|
|
#[cfg(all(feature = "mshv", target_arch = "x86_64"))]
|
|
|
|
Mshv, /* MSHV does not supprt MpState yet */
|
|
|
|
}
|
2022-07-14 13:06:26 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum IoEventAddress {
|
|
|
|
Pio(u64),
|
|
|
|
Mmio(u64),
|
|
|
|
}
|
2022-07-14 15:20:21 +00:00
|
|
|
|
|
|
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
2022-07-20 23:58:25 +00:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2022-07-14 15:20:21 +00:00
|
|
|
pub enum CpuState {
|
|
|
|
#[cfg(feature = "kvm")]
|
|
|
|
Kvm(kvm::VcpuKvmState),
|
|
|
|
#[cfg(all(feature = "mshv", target_arch = "x86_64"))]
|
|
|
|
Mshv(mshv::VcpuMshvState),
|
|
|
|
}
|
2022-07-14 14:16:49 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub enum ClockData {
|
|
|
|
#[cfg(feature = "kvm")]
|
|
|
|
Kvm(kvm_bindings::kvm_clock_data),
|
|
|
|
#[cfg(feature = "mshv")]
|
|
|
|
Mshv, /* MSHV does not supprt ClockData yet */
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
impl ClockData {
|
|
|
|
pub fn reset_flags(&mut self) {
|
|
|
|
match self {
|
|
|
|
#[cfg(feature = "kvm")]
|
|
|
|
ClockData::Kvm(s) => s.flags = 0,
|
|
|
|
#[allow(unreachable_patterns)]
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 18:59:13 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum IrqRoutingEntry {
|
|
|
|
#[cfg(feature = "kvm")]
|
|
|
|
Kvm(kvm_bindings::kvm_irq_routing_entry),
|
|
|
|
#[cfg(feature = "mshv")]
|
|
|
|
Mshv(mshv_bindings::mshv_msi_routing_entry),
|
|
|
|
}
|