2020-06-03 19:58:00 +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-03 19:58:00 +00:00
|
|
|
//
|
2020-06-28 16:32:56 +00:00
|
|
|
// Copyright © 2020, Microsoft Corporation
|
2020-06-03 19:58:00 +00:00
|
|
|
//
|
|
|
|
// Copyright 2018-2019 CrowdStrike, Inc.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
use crate::aarch64::VcpuInit;
|
2022-05-29 08:54:23 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2022-08-30 00:49:57 +00:00
|
|
|
use crate::arch::aarch64::gic::{Vgic, VgicConfig};
|
2022-07-18 13:18:12 +00:00
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
use crate::arch::x86::CpuIdEntry;
|
2020-06-03 19:58:00 +00:00
|
|
|
use crate::cpu::Vcpu;
|
2022-07-14 14:16:49 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-06-23 09:07:06 +00:00
|
|
|
use crate::ClockData;
|
2022-07-08 21:40:49 +00:00
|
|
|
use crate::UserMemoryRegion;
|
|
|
|
use crate::{IoEventAddress, IrqRoutingEntry};
|
2022-07-20 12:52:06 +00:00
|
|
|
use std::any::Any;
|
2021-07-07 10:03:05 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
use std::fs::File;
|
2020-06-03 19:58:00 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-01 05:24:12 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
use std::sync::Mutex;
|
2020-06-03 19:58:00 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// I/O events data matches (32 or 64 bits).
|
|
|
|
///
|
2020-12-03 23:24:57 +00:00
|
|
|
#[derive(Debug)]
|
2020-06-03 19:58:00 +00:00
|
|
|
pub enum DataMatch {
|
|
|
|
DataMatch32(u32),
|
|
|
|
DataMatch64(u64),
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
impl From<DataMatch> for u64 {
|
|
|
|
fn from(dm: DataMatch) -> u64 {
|
|
|
|
match dm {
|
2020-06-03 19:58:00 +00:00
|
|
|
DataMatch::DataMatch32(dm) => dm.into(),
|
|
|
|
DataMatch::DataMatch64(dm) => dm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
///
|
|
|
|
/// Enum for VM error
|
|
|
|
pub enum HypervisorVmError {
|
|
|
|
///
|
|
|
|
/// Create Vcpu error
|
|
|
|
///
|
|
|
|
#[error("Failed to create Vcpu: {0}")]
|
|
|
|
CreateVcpu(#[source] anyhow::Error),
|
|
|
|
///
|
2021-12-04 13:15:41 +00:00
|
|
|
/// Identity map address error
|
|
|
|
///
|
|
|
|
#[error("Failed to set identity map address: {0}")]
|
|
|
|
SetIdentityMapAddress(#[source] anyhow::Error),
|
|
|
|
///
|
2020-06-03 19:58:00 +00:00
|
|
|
/// TSS address error
|
|
|
|
///
|
|
|
|
#[error("Failed to set TSS address: {0}")]
|
|
|
|
SetTssAddress(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Create interrupt controller error
|
|
|
|
///
|
|
|
|
#[error("Failed to create interrupt controller: {0}")]
|
|
|
|
CreateIrq(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Register interrupt event error
|
|
|
|
///
|
|
|
|
#[error("Failed to register interrupt event: {0}")]
|
|
|
|
RegisterIrqFd(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Un register interrupt event error
|
|
|
|
///
|
|
|
|
#[error("Failed to unregister interrupt event: {0}")]
|
|
|
|
UnregisterIrqFd(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Register IO event error
|
|
|
|
///
|
|
|
|
#[error("Failed to register IO event: {0}")]
|
|
|
|
RegisterIoEvent(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Unregister IO event error
|
|
|
|
///
|
|
|
|
#[error("Failed to unregister IO event: {0}")]
|
|
|
|
UnregisterIoEvent(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Set GSI routing error
|
|
|
|
///
|
|
|
|
#[error("Failed to set GSI routing: {0}")]
|
|
|
|
SetGsiRouting(#[source] anyhow::Error),
|
|
|
|
///
|
2021-07-03 13:58:39 +00:00
|
|
|
/// Create user memory error
|
2020-06-03 19:58:00 +00:00
|
|
|
///
|
2021-07-03 13:58:39 +00:00
|
|
|
#[error("Failed to create user memory: {0}")]
|
|
|
|
CreateUserMemory(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Remove user memory region error
|
|
|
|
///
|
|
|
|
#[error("Failed to remove user memory: {0}")]
|
|
|
|
RemoveUserMemory(#[source] anyhow::Error),
|
2020-06-03 19:58:00 +00:00
|
|
|
///
|
|
|
|
/// Create device error
|
|
|
|
///
|
|
|
|
#[error("Failed to set GSI routing: {0}")]
|
|
|
|
CreateDevice(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Get preferred target error
|
|
|
|
///
|
|
|
|
#[error("Failed to get preferred target: {0}")]
|
|
|
|
GetPreferredTarget(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Enable split Irq error
|
|
|
|
///
|
|
|
|
#[error("Failed to enable split Irq: {0}")]
|
|
|
|
EnableSplitIrq(#[source] anyhow::Error),
|
2020-06-23 09:07:06 +00:00
|
|
|
///
|
2021-07-07 10:03:05 +00:00
|
|
|
/// Enable SGX attribute error
|
|
|
|
///
|
|
|
|
#[error("Failed to enable SGX attribute: {0}")]
|
|
|
|
EnableSgxAttribute(#[source] anyhow::Error),
|
|
|
|
///
|
2020-06-23 09:07:06 +00:00
|
|
|
/// Get clock error
|
|
|
|
///
|
|
|
|
#[error("Failed to get clock: {0}")]
|
|
|
|
GetClock(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Set clock error
|
|
|
|
///
|
|
|
|
#[error("Failed to set clock: {0}")]
|
|
|
|
SetClock(#[source] anyhow::Error),
|
2020-07-17 15:16:45 +00:00
|
|
|
///
|
|
|
|
/// Create passthrough device
|
|
|
|
///
|
|
|
|
#[error("Failed to create passthrough device: {0}")]
|
|
|
|
CreatePassthroughDevice(#[source] anyhow::Error),
|
2020-09-03 20:50:56 +00:00
|
|
|
/// Write to Guest memory
|
|
|
|
///
|
|
|
|
#[error("Failed to write to guest memory: {0}")]
|
|
|
|
GuestMemWrite(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Read Guest memory
|
|
|
|
///
|
|
|
|
#[error("Failed to read guest memory: {0}")]
|
|
|
|
GuestMemRead(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Read from MMIO Bus
|
|
|
|
///
|
|
|
|
#[error("Failed to read from MMIO Bus: {0}")]
|
|
|
|
MmioBusRead(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Write to MMIO Bus
|
|
|
|
///
|
|
|
|
#[error("Failed to write to MMIO Bus: {0}")]
|
|
|
|
MmioBusWrite(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Read from IO Bus
|
|
|
|
///
|
|
|
|
#[error("Failed to read from IO Bus: {0}")]
|
|
|
|
IoBusRead(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Write to IO Bus
|
|
|
|
///
|
|
|
|
#[error("Failed to write to IO Bus: {0}")]
|
|
|
|
IoBusWrite(#[source] anyhow::Error),
|
2020-11-11 18:32:03 +00:00
|
|
|
///
|
2021-07-22 02:16:30 +00:00
|
|
|
/// Start dirty log error
|
|
|
|
///
|
|
|
|
#[error("Failed to get dirty log: {0}")]
|
|
|
|
StartDirtyLog(#[source] anyhow::Error),
|
|
|
|
///
|
|
|
|
/// Stop dirty log error
|
|
|
|
///
|
|
|
|
#[error("Failed to get dirty log: {0}")]
|
|
|
|
StopDirtyLog(#[source] anyhow::Error),
|
|
|
|
///
|
2020-11-11 18:32:03 +00:00
|
|
|
/// Get dirty log error
|
|
|
|
///
|
|
|
|
#[error("Failed to get dirty log: {0}")]
|
|
|
|
GetDirtyLog(#[source] anyhow::Error),
|
2020-12-04 18:30:29 +00:00
|
|
|
///
|
|
|
|
/// Assert virtual interrupt error
|
|
|
|
///
|
|
|
|
#[error("Failed to assert virtual Interrupt: {0}")]
|
|
|
|
AsserttVirtualInterrupt(#[source] anyhow::Error),
|
2021-02-12 15:17:18 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
///
|
|
|
|
/// Error initializing TDX on the VM
|
|
|
|
///
|
|
|
|
#[error("Failed to initialize TDX: {0}")]
|
|
|
|
InitializeTdx(#[source] std::io::Error),
|
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
///
|
|
|
|
/// Error finalizing the TDX configuration on the VM
|
|
|
|
///
|
|
|
|
#[error("Failed to finalize TDX: {0}")]
|
|
|
|
FinalizeTdx(#[source] std::io::Error),
|
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
///
|
|
|
|
/// Error initializing the TDX memory region
|
|
|
|
///
|
|
|
|
#[error("Failed to initialize memory region TDX: {0}")]
|
|
|
|
InitMemRegionTdx(#[source] std::io::Error),
|
2022-05-29 08:54:23 +00:00
|
|
|
///
|
|
|
|
/// Create Vgic error
|
|
|
|
///
|
|
|
|
#[error("Failed to create Vgic: {0}")]
|
|
|
|
CreateVgic(#[source] anyhow::Error),
|
2020-06-03 19:58:00 +00:00
|
|
|
}
|
|
|
|
///
|
|
|
|
/// Result type for returning from a function
|
|
|
|
///
|
|
|
|
pub type Result<T> = std::result::Result<T, HypervisorVmError>;
|
|
|
|
|
2022-05-10 21:23:18 +00:00
|
|
|
/// Configuration data for legacy interrupts.
|
|
|
|
///
|
|
|
|
/// On x86 platforms, legacy interrupts means those interrupts routed through PICs or IOAPICs.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct LegacyIrqSourceConfig {
|
|
|
|
pub irqchip: u32,
|
|
|
|
pub pin: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration data for MSI/MSI-X interrupts.
|
|
|
|
///
|
|
|
|
/// On x86 platforms, these interrupts are vectors delivered directly to the LAPIC.
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
pub struct MsiIrqSourceConfig {
|
|
|
|
/// High address to delivery message signaled interrupt.
|
|
|
|
pub high_addr: u32,
|
|
|
|
/// Low address to delivery message signaled interrupt.
|
|
|
|
pub low_addr: u32,
|
|
|
|
/// Data to write to delivery message signaled interrupt.
|
|
|
|
pub data: u32,
|
|
|
|
/// Unique ID of the device to delivery message signaled interrupt.
|
|
|
|
pub devid: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration data for an interrupt source.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum InterruptSourceConfig {
|
|
|
|
/// Configuration data for Legacy interrupts.
|
|
|
|
LegacyIrq(LegacyIrqSourceConfig),
|
|
|
|
/// Configuration data for PciMsi, PciMsix and generic MSI interrupts.
|
|
|
|
MsiIrq(MsiIrqSourceConfig),
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:58:00 +00:00
|
|
|
///
|
|
|
|
/// Trait to represent a Vm
|
|
|
|
///
|
|
|
|
/// This crate provides a hypervisor-agnostic interfaces for Vm
|
|
|
|
///
|
2022-07-20 12:52:06 +00:00
|
|
|
pub trait Vm: Send + Sync + Any {
|
2021-12-04 13:15:41 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Sets the address of the one-page region in the VM's address space.
|
|
|
|
fn set_identity_map_address(&self, address: u64) -> Result<()>;
|
2020-06-03 19:58:00 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Sets the address of the three-page region in the VM's address space.
|
|
|
|
fn set_tss_address(&self, offset: usize) -> Result<()>;
|
|
|
|
/// Creates an in-kernel interrupt controller.
|
|
|
|
fn create_irq_chip(&self) -> Result<()>;
|
|
|
|
/// Registers an event that will, when signaled, trigger the `gsi` IRQ.
|
|
|
|
fn register_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()>;
|
|
|
|
/// Unregister an event that will, when signaled, trigger the `gsi` IRQ.
|
|
|
|
fn unregister_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()>;
|
|
|
|
/// Creates a new KVM vCPU file descriptor and maps the memory corresponding
|
2022-05-04 14:34:56 +00:00
|
|
|
fn create_vcpu(&self, id: u8, vm_ops: Option<Arc<dyn VmOps>>) -> Result<Arc<dyn Vcpu>>;
|
2022-05-29 08:54:23 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2022-08-30 00:49:57 +00:00
|
|
|
fn create_vgic(&self, config: VgicConfig) -> Result<Arc<Mutex<dyn Vgic>>>;
|
2022-05-29 08:54:23 +00:00
|
|
|
|
2020-06-03 19:58:00 +00:00
|
|
|
/// Registers an event to be signaled whenever a certain address is written to.
|
|
|
|
fn register_ioevent(
|
|
|
|
&self,
|
|
|
|
fd: &EventFd,
|
|
|
|
addr: &IoEventAddress,
|
|
|
|
datamatch: Option<DataMatch>,
|
|
|
|
) -> Result<()>;
|
|
|
|
/// Unregister an event from a certain address it has been previously registered to.
|
|
|
|
fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> Result<()>;
|
2022-05-10 15:47:34 +00:00
|
|
|
// Construct a routing entry
|
|
|
|
fn make_routing_entry(&self, gsi: u32, config: &InterruptSourceConfig) -> IrqRoutingEntry;
|
2020-06-03 19:58:00 +00:00
|
|
|
/// Sets the GSI routing table entries, overwriting any previously set
|
2020-07-17 11:40:08 +00:00
|
|
|
fn set_gsi_routing(&self, entries: &[IrqRoutingEntry]) -> Result<()>;
|
2021-07-03 13:58:39 +00:00
|
|
|
/// Creates a memory region structure that can be used with {create/remove}_user_memory_region
|
2020-07-04 11:38:17 +00:00
|
|
|
fn make_user_memory_region(
|
|
|
|
&self,
|
|
|
|
slot: u32,
|
|
|
|
guest_phys_addr: u64,
|
|
|
|
memory_size: u64,
|
|
|
|
userspace_addr: u64,
|
|
|
|
readonly: bool,
|
2020-11-11 18:16:39 +00:00
|
|
|
log_dirty_pages: bool,
|
2022-07-08 21:40:49 +00:00
|
|
|
) -> UserMemoryRegion;
|
2021-07-03 13:58:39 +00:00
|
|
|
/// Creates a guest physical memory slot.
|
2022-07-08 21:40:49 +00:00
|
|
|
fn create_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>;
|
2021-07-03 13:58:39 +00:00
|
|
|
/// Removes a guest physical memory slot.
|
2022-07-08 21:40:49 +00:00
|
|
|
fn remove_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>;
|
2020-06-03 19:58:00 +00:00
|
|
|
/// Returns the preferred CPU target type which can be emulated by KVM on underlying host.
|
2022-06-21 03:39:54 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2020-06-03 19:58:00 +00:00
|
|
|
fn get_preferred_target(&self, kvi: &mut VcpuInit) -> Result<()>;
|
|
|
|
/// Enable split Irq capability
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
fn enable_split_irq(&self) -> Result<()>;
|
2021-07-07 10:03:05 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
fn enable_sgx_attribute(&self, file: File) -> Result<()>;
|
2020-06-23 09:07:06 +00:00
|
|
|
/// Retrieve guest clock.
|
2022-07-14 14:16:49 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-06-23 09:07:06 +00:00
|
|
|
fn get_clock(&self) -> Result<ClockData>;
|
|
|
|
/// Set guest clock.
|
2022-07-14 14:16:49 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-06-23 09:07:06 +00:00
|
|
|
fn set_clock(&self, data: &ClockData) -> Result<()>;
|
2020-07-17 15:16:45 +00:00
|
|
|
/// Create a device that is used for passthrough
|
2022-07-21 13:15:15 +00:00
|
|
|
fn create_passthrough_device(&self) -> Result<vfio_ioctls::VfioDeviceFd>;
|
2021-07-22 02:16:30 +00:00
|
|
|
/// Start logging dirty pages
|
2021-07-26 19:33:02 +00:00
|
|
|
fn start_dirty_log(&self) -> Result<()>;
|
2021-07-22 02:16:30 +00:00
|
|
|
/// Stop logging dirty pages
|
2021-07-26 19:33:02 +00:00
|
|
|
fn stop_dirty_log(&self) -> Result<()>;
|
2020-11-11 18:32:03 +00:00
|
|
|
/// Get dirty pages bitmap
|
2021-07-08 17:14:24 +00:00
|
|
|
fn get_dirty_log(&self, slot: u32, base_gpa: u64, memory_size: u64) -> Result<Vec<u64>>;
|
2021-02-12 15:17:18 +00:00
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
/// Initalize TDX on this VM
|
2022-09-27 11:28:30 +00:00
|
|
|
fn tdx_init(&self, _cpuid: &[CpuIdEntry], _max_vcpus: u32) -> Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2021-02-12 15:17:18 +00:00
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
/// Finalize the configuration of TDX on this VM
|
2022-09-27 11:28:30 +00:00
|
|
|
fn tdx_finalize(&self) -> Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2021-02-12 15:17:18 +00:00
|
|
|
#[cfg(feature = "tdx")]
|
|
|
|
/// Initalize a TDX memory region for this VM
|
|
|
|
fn tdx_init_memory_region(
|
|
|
|
&self,
|
2022-09-27 11:28:30 +00:00
|
|
|
_host_address: u64,
|
|
|
|
_guest_address: u64,
|
|
|
|
_size: u64,
|
|
|
|
_measure: bool,
|
|
|
|
) -> Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2022-07-20 12:52:06 +00:00
|
|
|
/// Downcast to the underlying hypervisor VM type
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
2020-09-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 14:34:56 +00:00
|
|
|
pub trait VmOps: Send + Sync {
|
2021-01-11 17:38:48 +00:00
|
|
|
fn guest_mem_write(&self, gpa: u64, buf: &[u8]) -> Result<usize>;
|
|
|
|
fn guest_mem_read(&self, gpa: u64, buf: &mut [u8]) -> Result<usize>;
|
|
|
|
fn mmio_read(&self, gpa: u64, data: &mut [u8]) -> Result<()>;
|
|
|
|
fn mmio_write(&self, gpa: u64, data: &[u8]) -> Result<()>;
|
2020-09-03 20:50:56 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2021-01-11 17:38:48 +00:00
|
|
|
fn pio_read(&self, port: u64, data: &mut [u8]) -> Result<()>;
|
2020-09-03 20:50:56 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2021-01-11 17:38:48 +00:00
|
|
|
fn pio_write(&self, port: u64, data: &[u8]) -> Result<()>;
|
2020-06-03 19:58:00 +00:00
|
|
|
}
|