diff --git a/hypervisor/src/cpu.rs b/hypervisor/src/cpu.rs index 4d7497e9d..fccdb8909 100644 --- a/hypervisor/src/cpu.rs +++ b/hypervisor/src/cpu.rs @@ -272,10 +272,12 @@ pub trait Vcpu: Send + Sync { /// Setup the model-specific registers (MSR) for this vCPU. /// fn set_msrs(&self, msrs: &MsrEntries) -> Result; + #[cfg(feature = "kvm")] /// /// Returns the vcpu's current "multiprocessing state". /// fn get_mp_state(&self) -> Result; + #[cfg(feature = "kvm")] /// /// Sets the vcpu's current "multiprocessing state". /// @@ -312,7 +314,7 @@ pub trait Vcpu: Send + Sync { /// of the vcpu. /// fn set_vcpu_events(&self, events: &VcpuEvents) -> Result<()>; - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] /// /// Let the guest know that it has been paused, which prevents from /// potential soft lockups when being resumed. diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs index 88479bbdd..485e3abb2 100644 --- a/hypervisor/src/hypervisor.rs +++ b/hypervisor/src/hypervisor.rs @@ -10,7 +10,7 @@ use crate::vm::Vm; #[cfg(target_arch = "x86_64")] use crate::x86_64::{CpuId, MsrList}; -#[cfg(target_arch = "x86_64")] +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] use kvm_ioctls::Cap; use std::sync::Arc; @@ -78,19 +78,22 @@ pub trait Hypervisor: Send + Sync { /// Return a hypervisor-agnostic Vm trait object /// fn create_vm(&self) -> Result>; + #[cfg(feature = "kvm")] /// /// Returns the size of the memory mapping required to use the vcpu's structures /// fn get_vcpu_mmap_size(&self) -> Result; + #[cfg(feature = "kvm")] /// /// Gets the recommended maximum number of VCPUs per VM. /// fn get_max_vcpus(&self) -> Result; + #[cfg(feature = "kvm")] /// /// Gets the recommended number of VCPUs per VM. /// fn get_nr_vcpus(&self) -> Result; - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] /// /// Checks if a particular `Cap` is available. /// diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index cb5dba166..052095aab 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -28,6 +28,7 @@ extern crate serde_derive; extern crate serde_json; extern crate thiserror; +#[cfg(feature = "kvm")] /// KVM implementation module pub mod kvm; @@ -49,6 +50,7 @@ mod device; pub use crate::hypervisor::{Hypervisor, HypervisorError}; pub use cpu::{HypervisorCpuError, Vcpu, VmExit}; pub use device::{Device, HypervisorDeviceError}; +#[cfg(feature = "kvm")] pub use kvm::*; pub use vm::{DataMatch, HypervisorVmError, Vm}; diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index ea9a86269..3e8e818a9 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -12,10 +12,12 @@ use crate::aarch64::VcpuInit; use crate::cpu::Vcpu; use crate::device::Device; -#[cfg(target_arch = "x86_64")] +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] use crate::ClockData; +#[cfg(feature = "kvm")] use crate::KvmVmState as VmState; use crate::{CreateDevice, IoEventAddress, IrqRoutingEntry, MemoryRegion}; +#[cfg(feature = "kvm")] use kvm_ioctls::Cap; use std::sync::Arc; use thiserror::Error; @@ -198,6 +200,7 @@ pub trait Vm: Send + Sync { ) -> MemoryRegion; /// Creates/modifies a guest physical memory slot. fn set_user_memory_region(&self, user_memory_region: MemoryRegion) -> Result<()>; + #[cfg(feature = "kvm")] /// Creates an emulated device in the kernel. fn create_device(&self, device: &mut CreateDevice) -> Result>; /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. @@ -207,11 +210,12 @@ pub trait Vm: Send + Sync { #[cfg(target_arch = "x86_64")] fn enable_split_irq(&self) -> Result<()>; /// Retrieve guest clock. - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] fn get_clock(&self) -> Result; /// Set guest clock. - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] fn set_clock(&self, data: &ClockData) -> Result<()>; + #[cfg(feature = "kvm")] /// Checks if a particular `Cap` is available. fn check_extension(&self, c: Cap) -> bool; /// Create a device that is used for passthrough diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index a7e44d34a..bb598d716 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -1360,7 +1360,7 @@ impl Pausable for CpuManager { for vcpu in self.vcpus.iter() { let mut vcpu = vcpu.lock().unwrap(); vcpu.pause()?; - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] if !self.config.kvm_hyperv { vcpu.vcpu.notify_guest_clock_paused().map_err(|e| { MigratableError::Pause(anyhow!( @@ -1424,7 +1424,7 @@ impl Snapshottable for CpuManager { impl Transportable for CpuManager {} impl Migratable for CpuManager {} -#[cfg(target_arch = "x86_64")] +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] #[cfg(test)] mod tests { diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 783b19672..e58898465 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -43,7 +43,7 @@ use devices::{ interrupt_controller, interrupt_controller::InterruptController, legacy::Serial, HotPlugNotificationFlags, }; -use hypervisor::kvm_ioctls; +#[cfg(feature = "kvm")] use hypervisor::kvm_ioctls::*; #[cfg(target_arch = "aarch64")] use hypervisor::CpuState; @@ -181,7 +181,7 @@ pub enum DeviceManagerError { AllocateIrq, /// Cannot configure the IRQ. - Irq(kvm_ioctls::Error), + Irq(vmm_sys_util::errno::Error), /// Cannot allocate PCI BARs AllocateBars(pci::PciDeviceError), diff --git a/vmm/src/interrupt.rs b/vmm/src/interrupt.rs index 10d5f6c28..380891d1a 100644 --- a/vmm/src/interrupt.rs +++ b/vmm/src/interrupt.rs @@ -325,6 +325,7 @@ where } } +#[cfg(feature = "kvm")] pub mod kvm { use super::*; use hypervisor::kvm::KVM_MSI_VALID_DEVID; diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 6b624f2bd..24703d718 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -453,7 +453,7 @@ pub struct Vm { #[cfg_attr(not(feature = "kvm"), allow(dead_code))] // The hypervisor abstracted virtual machine. vm: Arc, - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] saved_clock: Option, #[cfg(feature = "acpi")] numa_nodes: NumaNodes, @@ -471,7 +471,7 @@ impl Vm { reset_evt: EventFd, seccomp_action: &SeccompAction, hypervisor: Arc, - _saved_clock: Option, + #[cfg(feature = "kvm")] _saved_clock: Option, ) -> Result { config .lock() @@ -550,7 +550,7 @@ impl Vm { cpu_manager, memory_manager, vm, - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] saved_clock: _saved_clock, #[cfg(feature = "acpi")] numa_nodes, @@ -631,7 +631,7 @@ impl Vm { seccomp_action: &SeccompAction, hypervisor: Arc, ) -> Result { - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] hypervisor.check_required_extensions().unwrap(); let vm = hypervisor.create_vm().unwrap(); #[cfg(target_arch = "x86_64")] @@ -664,6 +664,7 @@ impl Vm { reset_evt, seccomp_action, hypervisor, + #[cfg(feature = "kvm")] None, )?; @@ -688,7 +689,7 @@ impl Vm { seccomp_action: &SeccompAction, hypervisor: Arc, ) -> Result { - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] hypervisor.check_required_extensions().unwrap(); let vm = hypervisor.create_vm().unwrap(); #[cfg(target_arch = "x86_64")] @@ -727,9 +728,7 @@ impl Vm { reset_evt, seccomp_action, hypervisor, - #[cfg(target_arch = "x86_64")] - vm_snapshot.clock, - #[cfg(target_arch = "aarch64")] + #[cfg(feature = "kvm")] None, ) } @@ -741,7 +740,7 @@ impl Vm { seccomp_action: &SeccompAction, hypervisor: Arc, ) -> Result { - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] hypervisor.check_required_extensions().unwrap(); let vm = hypervisor.create_vm().unwrap(); #[cfg(target_arch = "x86_64")] @@ -764,6 +763,7 @@ impl Vm { reset_evt, seccomp_action, hypervisor, + #[cfg(feature = "kvm")] None, ) } @@ -1737,7 +1737,7 @@ impl Pausable for Vm { .valid_transition(new_state) .map_err(|e| MigratableError::Pause(anyhow!("Invalid transition: {:?}", e)))?; - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] { let mut clock = self .vm @@ -1767,7 +1767,7 @@ impl Pausable for Vm { .map_err(|e| MigratableError::Resume(anyhow!("Invalid transition: {:?}", e)))?; self.cpu_manager.lock().unwrap().resume()?; - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] { if let Some(clock) = &self.saved_clock { self.vm.set_clock(clock).map_err(|e| { @@ -1787,7 +1787,7 @@ impl Pausable for Vm { #[derive(Serialize, Deserialize)] pub struct VmSnapshot { pub config: Arc>, - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] pub clock: Option, pub state: Option, } @@ -1813,7 +1813,7 @@ impl Snapshottable for Vm { .map_err(|e| MigratableError::Snapshot(e.into()))?; let vm_snapshot_data = serde_json::to_vec(&VmSnapshot { config: self.get_config(), - #[cfg(target_arch = "x86_64")] + #[cfg(all(feature = "kvm", target_arch = "x86_64"))] clock: self.saved_clock, state: Some(vm_state), }) @@ -2016,7 +2016,7 @@ impl Transportable for Vm { } impl Migratable for Vm {} -#[cfg(target_arch = "x86_64")] +#[cfg(all(feature = "kvm", target_arch = "x86_64"))] #[cfg(test)] mod tests { use super::*;