Skip to main content

virt_kvm/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! KVM implementation of the virt::generic interfaces.
5
6#![cfg(all(target_os = "linux", guest_is_native))]
7#![expect(missing_docs)]
8// UNSAFETY: Calling KVM APIs and manually managing memory.
9#![expect(unsafe_code)]
10#![expect(clippy::undocumented_unsafe_blocks)]
11
12mod arch;
13mod gsi;
14mod memory;
15#[cfg(guest_arch = "x86_64")]
16mod snp;
17
18pub use arch::Kvm;
19pub use memory::MemoryError;
20#[cfg(guest_arch = "x86_64")]
21pub use snp::SnpError;
22
23use guestmem::GuestMemory;
24use inspect::Inspect;
25use memory::KvmMemoryBackingMode;
26use memory::KvmMemoryRangeState;
27use memory_range::MemoryRange;
28use parking_lot::Mutex;
29use std::sync::Arc;
30use thiserror::Error;
31use virt::state::StateError;
32
33/// Returns whether KVM is available on this machine.
34pub fn is_available() -> Result<bool, KvmError> {
35    match std::fs::metadata("/dev/kvm") {
36        Ok(_) => Ok(true),
37        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
38        Err(err) => Err(KvmError::AvailableCheck(err)),
39    }
40}
41
42use arch::KvmVpInner;
43#[cfg(guest_arch = "x86_64")]
44use snp::SnpLaunchState;
45use std::sync::atomic::Ordering;
46use virt::VpIndex;
47use vmcore::vmtime::VmTimeAccess;
48
49#[derive(Error, Debug)]
50pub enum KvmError {
51    #[error("operation not supported")]
52    NotSupported,
53    #[error("vtl2 is not supported on this hypervisor")]
54    Vtl2NotSupported,
55    #[error("isolation is not supported on this hypervisor")]
56    IsolationNotSupported,
57    #[error("kvm error")]
58    Kvm(#[from] kvm::Error),
59    #[error(transparent)]
60    Memory(#[from] MemoryError),
61    #[cfg(guest_arch = "x86_64")]
62    #[error(transparent)]
63    Snp(#[from] SnpError),
64    #[error("failed to stat /dev/kvm")]
65    AvailableCheck(#[source] std::io::Error),
66    #[error(transparent)]
67    State(#[from] Box<StateError<KvmError>>),
68    #[error("invalid state while restoring: {0}")]
69    InvalidState(&'static str),
70    #[error("unsupported isolation configuration: {0}")]
71    UnsupportedIsolationConfiguration(&'static str),
72    #[error("misaligned gic base address")]
73    Misaligned,
74    #[error("host does not support GICv2 or GICv3")]
75    NoGic,
76    #[error("host does not support required cpu capabilities")]
77    Capabilities(virt::PartitionCapabilitiesError),
78    #[cfg(guest_arch = "x86_64")]
79    #[error("nested virtualization was requested but the host does not support it")]
80    NestedVirtUnsupported,
81    #[cfg(guest_arch = "x86_64")]
82    #[error("unsupported CPU vendor")]
83    UnsupportedCpuVendor,
84    #[cfg(guest_arch = "x86_64")]
85    #[error("failed to compute topology cpuid")]
86    TopologyCpuid(#[source] virt::x86::topology::UnknownVendor),
87}
88
89#[derive(Inspect)]
90pub struct KvmPartition {
91    #[inspect(flatten)]
92    inner: Arc<KvmPartitionInner>,
93    #[cfg(guest_arch = "x86_64")]
94    #[inspect(skip)]
95    synic_ports: Arc<virt::synic::SynicPorts<KvmPartitionInner>>,
96    #[inspect(skip)]
97    irqfd_state: Arc<gsi::KvmIrqFdState>,
98}
99
100#[derive(Inspect)]
101struct KvmPartitionInner {
102    #[inspect(skip)]
103    kvm: kvm::Partition,
104    #[cfg(guest_arch = "x86_64")]
105    #[inspect(skip)]
106    sev: Option<std::fs::File>,
107    #[cfg(guest_arch = "x86_64")]
108    #[inspect(skip)]
109    snp_launch_state: Mutex<SnpLaunchState>,
110    memory: Mutex<KvmMemoryRangeState>,
111    memory_backing_mode: KvmMemoryBackingMode,
112    #[inspect(iter_by_index)]
113    ram_ranges: Vec<MemoryRange>,
114    hv1_enabled: bool,
115    gm: GuestMemory,
116    #[cfg(guest_arch = "x86_64")]
117    #[inspect(skip)]
118    bsp_cpuid: Vec<kvm::kvm_cpuid_entry2>,
119    #[inspect(skip)]
120    vps: Vec<KvmVpInner>,
121    #[inspect(skip)]
122    gsi_routing: Mutex<gsi::GsiRouting>,
123    caps: virt::PartitionCapabilities,
124
125    // This is used for debugging via Inspect
126    #[cfg(guest_arch = "x86_64")]
127    cpuid: virt::CpuidLeafSet,
128
129    #[cfg(guest_arch = "x86_64")]
130    reserved_vps_per_socket: u32,
131
132    /// Whether the host allows advertising `MCG_CMCI_P` in the guest's
133    /// `IA32_MCG_CAP` (required for KVM to expose the CMCI LVT register).
134    #[cfg(guest_arch = "x86_64")]
135    mce_cmci_supported: bool,
136
137    /// The GIC device fd, kept alive for the VM lifetime.
138    #[cfg(guest_arch = "aarch64")]
139    #[inspect(skip)]
140    _gic_device: kvm::Device,
141    /// The ITS device fd, kept alive for the VM lifetime.
142    #[cfg(guest_arch = "aarch64")]
143    #[inspect(skip)]
144    _its_device: Option<kvm::Device>,
145    /// MSI controller configuration (v2m, ITS, or none).
146    #[cfg(guest_arch = "aarch64")]
147    #[inspect(skip)]
148    gic_msi: vm_topology::processor::aarch64::GicMsiController,
149    /// Total configured GIC interrupt count (SGIs + PPIs + SPIs).
150    #[cfg(guest_arch = "aarch64")]
151    gic_nr_irqs: u32,
152    #[cfg(guest_arch = "x86_64")]
153    synic_ports: virt::synic::SynicPortMap,
154}
155
156// TODO: Chunk this up into smaller types.
157#[derive(Debug, Error)]
158enum KvmRunVpError {
159    #[error("KVM internal error: {0:#x}")]
160    InternalError(u32),
161    #[error("invalid vp state")]
162    InvalidVpState,
163    #[error("failed to run VP")]
164    Run(#[source] kvm::Error),
165    #[error("unhandled system event type: {0:#x}")]
166    UnhandledSystemEvent(u32),
167    #[cfg(guest_arch = "x86_64")]
168    #[error("unhandled KVM hypercall: nr={nr:#x}, flags={flags:#x}")]
169    UnhandledHypercall { nr: u64, flags: u64 },
170    #[cfg(guest_arch = "x86_64")]
171    #[error(
172        "SEV guest requested termination: ghcb_msr={ghcb_msr:#x} reason_set={reason_set:#x} reason={reason:#x}"
173    )]
174    SevTermination {
175        ghcb_msr: u64,
176        reason_set: u64,
177        reason: u64,
178    },
179    #[cfg(guest_arch = "x86_64")]
180    #[error("failed to inject an extint interrupt")]
181    ExtintInterrupt(#[source] kvm::Error),
182}
183
184pub struct KvmProcessorBinder {
185    partition: Arc<KvmPartitionInner>,
186    vpindex: VpIndex,
187    vmtime: VmTimeAccess,
188}
189
190impl KvmPartitionInner {
191    #[cfg(guest_arch = "x86_64")]
192    fn bsp(&self) -> &KvmVpInner {
193        &self.vps[0]
194    }
195
196    fn vp(&self, vp_index: VpIndex) -> Option<&KvmVpInner> {
197        self.vps.get(vp_index.index() as usize)
198    }
199
200    fn evaluate_vp(&self, vp_index: VpIndex) {
201        let Some(vp) = self.vp(vp_index) else { return };
202        vp.set_eval(true, Ordering::Relaxed);
203
204        #[cfg(guest_arch = "x86_64")]
205        self.kvm.vp(vp.vp_info().apic_id).force_exit();
206
207        #[cfg(guest_arch = "aarch64")]
208        self.kvm.vp(vp.vp_info().base.vp_index.index()).force_exit();
209    }
210}