diff --git a/Cargo.lock b/Cargo.lock index 222a243cc..679f838f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -742,7 +742,6 @@ version = "0.1.0" dependencies = [ "anyhow", "byteorder", - "devices", "hypervisor", "libc", "log 0.4.11", @@ -1525,7 +1524,6 @@ dependencies = [ "arc-swap", "block_util", "byteorder", - "devices", "epoll", "io-uring", "libc", diff --git a/devices/src/acpi.rs b/devices/src/acpi.rs index c66279526..731353b79 100644 --- a/devices/src/acpi.rs +++ b/devices/src/acpi.rs @@ -7,8 +7,8 @@ use acpi_tables::{aml, aml::Aml}; use std::sync::Arc; use std::time::Instant; use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; use vmm_sys_util::eventfd::EventFd; -use BusDevice; use HotPlugNotificationFlags; /// A device for handling ACPI shutdown and reboot diff --git a/devices/src/ioapic.rs b/devices/src/ioapic.rs index b402dea1d..efdd30da2 100644 --- a/devices/src/ioapic.rs +++ b/devices/src/ioapic.rs @@ -10,7 +10,6 @@ // See https://pdos.csail.mit.edu/6.828/2016/readings/ia32/ioapic.pdf for a specification. use super::interrupt_controller::{Error, InterruptController}; -use crate::BusDevice; use anyhow::anyhow; use byteorder::{ByteOrder, LittleEndian}; use std::result; @@ -19,6 +18,7 @@ use vm_device::interrupt::{ InterruptIndex, InterruptManager, InterruptSourceConfig, InterruptSourceGroup, MsiIrqGroupConfig, MsiIrqSourceConfig, }; +use vm_device::BusDevice; use vm_memory::GuestAddress; use vm_migration::{ Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable, diff --git a/devices/src/legacy/cmos.rs b/devices/src/legacy/cmos.rs index 094204473..6b1b5380c 100644 --- a/devices/src/legacy/cmos.rs +++ b/devices/src/legacy/cmos.rs @@ -5,8 +5,7 @@ use libc::{clock_gettime, gmtime_r, time_t, timespec, tm, CLOCK_REALTIME}; use std::cmp::min; use std::mem; - -use crate::BusDevice; +use vm_device::BusDevice; const INDEX_MASK: u8 = 0x7f; const INDEX_OFFSET: u64 = 0x0; diff --git a/devices/src/legacy/fwdebug.rs b/devices/src/legacy/fwdebug.rs index 557bcb5a9..c5efbbf06 100644 --- a/devices/src/legacy/fwdebug.rs +++ b/devices/src/legacy/fwdebug.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause // -use BusDevice; +use vm_device::BusDevice; /// Provides firmware debug output via I/O port controls #[derive(Default)] diff --git a/devices/src/legacy/i8042.rs b/devices/src/legacy/i8042.rs index a97f2f11a..d6d295467 100644 --- a/devices/src/legacy/i8042.rs +++ b/devices/src/legacy/i8042.rs @@ -4,7 +4,7 @@ use vmm_sys_util::eventfd::EventFd; -use BusDevice; +use vm_device::BusDevice; /// A i8042 PS/2 controller that emulates just enough to shutdown the machine. pub struct I8042Device { diff --git a/devices/src/legacy/rtc_pl031.rs b/devices/src/legacy/rtc_pl031.rs index 9dc98d627..1b9643283 100644 --- a/devices/src/legacy/rtc_pl031.rs +++ b/devices/src/legacy/rtc_pl031.rs @@ -12,9 +12,8 @@ use std::fmt; use std::sync::Arc; use std::time::Instant; use std::{io, result}; - -use crate::BusDevice; use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; // As you can see in https://static.docs.arm.com/ddi0224/c/real_time_clock_pl031_r1p3_technical_reference_manual_DDI0224C.pdf // at section 3.2 Summary of RTC registers, the total size occupied by this device is 0x000 -> 0xFFC + 4 = 0x1000. diff --git a/devices/src/legacy/serial.rs b/devices/src/legacy/serial.rs index 10a1ddb1b..d8aa9366f 100644 --- a/devices/src/legacy/serial.rs +++ b/devices/src/legacy/serial.rs @@ -5,12 +5,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE-BSD-3-Clause file. -use crate::BusDevice; use anyhow::anyhow; use std::collections::VecDeque; use std::sync::Arc; use std::{io, result}; use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; use vm_migration::{ Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable, Transportable, diff --git a/devices/src/lib.rs b/devices/src/lib.rs index 4677146b2..bdfdf5f5c 100644 --- a/devices/src/lib.rs +++ b/devices/src/lib.rs @@ -25,12 +25,8 @@ extern crate vmm_sys_util; extern crate serde_derive; extern crate serde_json; -use std::fs::File; -use std::io; - #[cfg(feature = "acpi")] mod acpi; -mod bus; #[cfg(target_arch = "aarch64")] pub mod gic; pub mod interrupt_controller; @@ -40,45 +36,6 @@ pub mod legacy; #[cfg(feature = "acpi")] pub use self::acpi::{AcpiGEDDevice, AcpiPMTimerDevice, AcpiShutdownDevice}; -pub use self::bus::{Bus, BusDevice, Error as BusError}; - -pub type DeviceEventT = u16; - -/// The payload is used to handle events where the internal state of the VirtIO device -/// needs to be changed. -pub enum EpollHandlerPayload { - /// DrivePayload(disk_image) - DrivePayload(File), - /// Events that do not need a payload. - Empty, -} - -type Result = std::result::Result; - -pub trait EpollHandler: Send { - fn handle_event( - &mut self, - device_event: DeviceEventT, - event_flags: u32, - payload: EpollHandlerPayload, - ) -> Result<()>; -} - -#[derive(Debug)] -pub enum Error { - FailedReadingQueue { - event_type: &'static str, - underlying: io::Error, - }, - FailedReadTap, - FailedSignalingUsedQueue(io::Error), - PayloadExpected, - UnknownEvent { - device: &'static str, - event: DeviceEventT, - }, - IoError(io::Error), -} bitflags! { pub struct HotPlugNotificationFlags: u8 { diff --git a/pci/Cargo.toml b/pci/Cargo.toml index 25f8c4a4c..779cf47c7 100644 --- a/pci/Cargo.toml +++ b/pci/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] anyhow = "1.0" byteorder = "1.3.4" -devices = { path = "../devices" } hypervisor = { path = "../hypervisor" } vfio-ioctls = { git = "https://github.com/cloud-hypervisor/vfio-ioctls", branch = "ch" } vmm-sys-util = ">=0.3.1" diff --git a/pci/src/bus.rs b/pci/src/bus.rs index 0685afbe1..84c37fe52 100644 --- a/pci/src/bus.rs +++ b/pci/src/bus.rs @@ -7,11 +7,11 @@ use crate::configuration::{ }; use crate::device::{DeviceRelocation, Error as PciDeviceError, PciDevice}; use byteorder::{ByteOrder, LittleEndian}; -use devices::BusDevice; use std::any::Any; use std::collections::HashMap; use std::ops::DerefMut; use std::sync::{Arc, Mutex}; +use vm_device::{Bus, BusDevice}; use vm_memory::{Address, GuestAddress, GuestUsize}; const VENDOR_ID_INTEL: u16 = 0x8086; @@ -26,9 +26,9 @@ pub enum PciRootError { /// Could not allocate an IRQ number. AllocateIrq, /// Could not add a device to the port io bus. - PioInsert(devices::BusError), + PioInsert(vm_device::BusError), /// Could not add a device to the mmio bus. - MmioInsert(devices::BusError), + MmioInsert(vm_device::BusError), /// Could not find an available device slot on the PCI bus. NoPciDeviceSlotAvailable, /// Invalid PCI device identifier provided. @@ -110,8 +110,8 @@ impl PciBus { pub fn register_mapping( &self, dev: Arc>, - #[cfg(target_arch = "x86_64")] io_bus: &devices::Bus, - mmio_bus: &devices::Bus, + #[cfg(target_arch = "x86_64")] io_bus: &Bus, + mmio_bus: &Bus, bars: Vec<(GuestAddress, GuestUsize, PciBarRegionType)>, ) -> Result<()> { for (address, size, type_) in bars { diff --git a/pci/src/device.rs b/pci/src/device.rs index efdd078a3..ae7bc5296 100644 --- a/pci/src/device.rs +++ b/pci/src/device.rs @@ -3,11 +3,11 @@ // found in the LICENSE-BSD-3-Clause file. use crate::configuration::{self, PciBarRegionType}; -use devices::BusDevice; use std::any::Any; use std::fmt::{self, Display}; use std::{self, io, result}; use vm_allocator::SystemAllocator; +use vm_device::BusDevice; use vm_memory::{GuestAddress, GuestUsize}; #[derive(Debug)] diff --git a/pci/src/lib.rs b/pci/src/lib.rs index 20cc879b2..02f6c901c 100644 --- a/pci/src/lib.rs +++ b/pci/src/lib.rs @@ -5,7 +5,6 @@ //! Implements pci devices and busses. #[macro_use] extern crate log; -extern crate devices; extern crate hypervisor; extern crate serde; #[macro_use] diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index dc378bad9..a500a43f7 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause // -extern crate devices; extern crate vm_allocator; use crate::{ @@ -12,7 +11,6 @@ use crate::{ PciDevice, PciDeviceError, PciHeaderType, PciSubclass, MSIX_TABLE_ENTRY_SIZE, }; use byteorder::{ByteOrder, LittleEndian}; -use devices::BusDevice; use std::any::Any; use std::ops::Deref; use std::os::unix::io::AsRawFd; @@ -25,6 +23,7 @@ use vm_allocator::SystemAllocator; use vm_device::interrupt::{ InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig, }; +use vm_device::BusDevice; use vm_memory::{ Address, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, GuestRegionMmap, GuestUsize, diff --git a/virtio-devices/Cargo.toml b/virtio-devices/Cargo.toml index d283dbb69..cda788493 100644 --- a/virtio-devices/Cargo.toml +++ b/virtio-devices/Cargo.toml @@ -15,7 +15,6 @@ anyhow = "1.0" arc-swap = ">=0.4.4" block_util = { path = "../block_util" } byteorder = "1.3.4" -devices = { path = "../devices" } epoll = ">=4.0.1" io-uring = ">=0.4.0" libc = "0.2.77" diff --git a/virtio-devices/src/transport/mmio.rs b/virtio-devices/src/transport/mmio.rs index ddc0adaba..169045b4b 100644 --- a/virtio-devices/src/transport/mmio.rs +++ b/virtio-devices/src/transport/mmio.rs @@ -10,13 +10,13 @@ use crate::{ }; use anyhow::anyhow; use byteorder::{ByteOrder, LittleEndian}; -use devices::BusDevice; use libc::EFD_NONBLOCK; use std::num::Wrapping; use std::result; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use vm_device::interrupt::InterruptSourceGroup; +use vm_device::BusDevice; use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap}; use vm_migration::{ Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable, diff --git a/virtio-devices/src/transport/pci_device.rs b/virtio-devices/src/transport/pci_device.rs index a52f9085b..062e6a935 100644 --- a/virtio-devices/src/transport/pci_device.rs +++ b/virtio-devices/src/transport/pci_device.rs @@ -6,7 +6,6 @@ // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause -extern crate devices; #[cfg(feature = "pci_support")] extern crate pci; extern crate vm_allocator; @@ -21,7 +20,6 @@ use crate::{ DEVICE_INIT, VIRTIO_MSI_NO_VECTOR, }; use anyhow::anyhow; -use devices::BusDevice; use libc::EFD_NONBLOCK; use pci::{ BarReprogrammingParams, MsixCap, MsixConfig, PciBarConfiguration, PciBarRegionType, @@ -39,6 +37,7 @@ use vm_allocator::SystemAllocator; use vm_device::interrupt::{ InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig, }; +use vm_device::BusDevice; use vm_memory::{ Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, GuestUsize, Le32, diff --git a/devices/src/bus.rs b/vm-device/src/bus.rs similarity index 100% rename from devices/src/bus.rs rename to vm-device/src/bus.rs diff --git a/vm-device/src/lib.rs b/vm-device/src/lib.rs index 65fb3ec4e..1b1c69ba7 100644 --- a/vm-device/src/lib.rs +++ b/vm-device/src/lib.rs @@ -1,9 +1,24 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + #[macro_use] extern crate serde_derive; extern crate vm_memory; +use std::io; + +mod bus; pub mod interrupt; +pub use self::bus::{Bus, BusDevice, Error as BusError}; + +#[derive(Debug)] +pub enum Error { + IoError(io::Error), +} + /// Type of Message Singaled Interrupt #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum MsiIrqType { diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index 2668e2975..d32f77621 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -27,19 +27,18 @@ use arch::x86_64::SgxEpcSection; use arch::EntryPoint; #[cfg(target_arch = "x86_64")] use arch::{CpuidPatch, CpuidReg}; -use devices::{interrupt_controller::InterruptController, BusDevice}; +use devices::interrupt_controller::InterruptController; #[cfg(target_arch = "x86_64")] use hypervisor::CpuId; use hypervisor::{CpuState, VmExit}; - use libc::{c_void, siginfo_t}; - #[cfg(target_arch = "x86_64")] use std::fmt; use std::os::unix::thread::JoinHandleExt; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Barrier, Mutex}; use std::{cmp, io, result, thread}; +use vm_device::{Bus, BusDevice}; #[cfg(target_arch = "x86_64")] use vm_memory::GuestAddress; use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; @@ -129,7 +128,7 @@ pub enum Error { ThreadCleanup(std::boxed::Box), /// Cannot add legacy device to Bus. - BusError(devices::BusError), + BusError(vm_device::BusError), /// Failed to allocate IO port AllocateIOPort, @@ -234,8 +233,8 @@ pub struct Vcpu { vcpu: Arc, id: u8, #[cfg(target_arch = "x86_64")] - io_bus: Arc, - mmio_bus: Arc, + io_bus: Arc, + mmio_bus: Arc, #[cfg_attr(target_arch = "aarch64", allow(dead_code))] interrupt_controller: Option>>, #[cfg_attr(target_arch = "aarch64", allow(dead_code))] @@ -255,8 +254,8 @@ impl Vcpu { pub fn new( id: u8, vm: &Arc, - #[cfg(target_arch = "x86_64")] io_bus: Arc, - mmio_bus: Arc, + #[cfg(target_arch = "x86_64")] io_bus: Arc, + mmio_bus: Arc, interrupt_controller: Option>>, creation_ts: std::time::Instant, ) -> Result>> { @@ -322,7 +321,7 @@ impl Vcpu { #[cfg(target_arch = "x86_64")] VmExit::IoIn(addr, data) => { if let Err(e) = self.io_bus.read(u64::from(addr), data) { - if let devices::BusError::MissingAddressRange = e { + if let vm_device::BusError::MissingAddressRange = e { warn!("Guest PIO read to unregistered address 0x{:x}", addr); } } @@ -334,7 +333,7 @@ impl Vcpu { self.log_debug_ioport(data[0]); } if let Err(e) = self.io_bus.write(u64::from(addr), data) { - if let devices::BusError::MissingAddressRange = e { + if let vm_device::BusError::MissingAddressRange = e { warn!("Guest PIO write to unregistered address 0x{:x}", addr); } } @@ -342,7 +341,7 @@ impl Vcpu { } VmExit::MmioRead(addr, data) => { if let Err(e) = self.mmio_bus.read(addr as u64, data) { - if let devices::BusError::MissingAddressRange = e { + if let vm_device::BusError::MissingAddressRange = e { warn!("Guest MMIO read to unregistered address 0x{:x}", addr); } } @@ -350,7 +349,7 @@ impl Vcpu { } VmExit::MmioWrite(addr, data) => { if let Err(e) = self.mmio_bus.write(addr as u64, data) { - if let devices::BusError::MissingAddressRange = e { + if let vm_device::BusError::MissingAddressRange = e { warn!("Guest MMIO write to unregistered address 0x{:x}", addr); } } @@ -458,9 +457,9 @@ impl Snapshottable for Vcpu { pub struct CpuManager { config: CpusConfig, #[cfg(target_arch = "x86_64")] - io_bus: Arc, + io_bus: Arc, #[cfg_attr(target_arch = "aarch64", allow(dead_code))] - mmio_bus: Arc, + mmio_bus: Arc, #[cfg_attr(target_arch = "aarch64", allow(dead_code))] interrupt_controller: Option>>, #[cfg_attr(target_arch = "aarch64", allow(dead_code))] diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 5610f94e5..f0f5b44d5 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -39,7 +39,7 @@ use devices::gic; #[cfg(target_arch = "x86_64")] use devices::ioapic; use devices::{ - interrupt_controller, interrupt_controller::InterruptController, legacy::Serial, BusDevice, + interrupt_controller, interrupt_controller::InterruptController, legacy::Serial, HotPlugNotificationFlags, }; use hypervisor::kvm_ioctls; @@ -81,7 +81,7 @@ use vm_allocator::SystemAllocator; use vm_device::interrupt::{ InterruptIndex, InterruptManager, LegacyIrqGroupConfig, MsiIrqGroupConfig, }; -use vm_device::Resource; +use vm_device::{Bus, BusDevice, Resource}; use vm_memory::guest_memory::FileOffset; use vm_memory::{ Address, GuestAddress, GuestAddressSpace, GuestRegionMmap, GuestUsize, MmapRegion, @@ -249,7 +249,7 @@ pub enum DeviceManagerError { Mmap(io::Error), /// Cannot add legacy device to Bus. - BusError(devices::BusError), + BusError(vm_device::BusError), /// Failed to allocate IO port AllocateIOPort, @@ -298,10 +298,10 @@ pub enum DeviceManagerError { RemoveDeviceFromPciBus(pci::PciRootError), /// Failed removing a bus device from the IO bus. - RemoveDeviceFromIoBus(devices::BusError), + RemoveDeviceFromIoBus(vm_device::BusError), /// Failed removing a bus device from the MMIO bus. - RemoveDeviceFromMmioBus(devices::BusError), + RemoveDeviceFromMmioBus(vm_device::BusError), /// Failed to find the device corresponding to a specific PCI b/d/f. #[cfg(feature = "pci_support")] @@ -459,8 +459,8 @@ impl Console { struct AddressManager { allocator: Arc>, #[cfg(target_arch = "x86_64")] - io_bus: Arc, - mmio_bus: Arc, + io_bus: Arc, + mmio_bus: Arc, vm: Arc, #[cfg(feature = "pci_support")] device_tree: Arc>, @@ -822,8 +822,8 @@ impl DeviceManager { let address_manager = Arc::new(AddressManager { allocator: memory_manager.lock().unwrap().allocator(), #[cfg(target_arch = "x86_64")] - io_bus: Arc::new(devices::Bus::new()), - mmio_bus: Arc::new(devices::Bus::new()), + io_bus: Arc::new(Bus::new()), + mmio_bus: Arc::new(Bus::new()), vm: vm.clone(), #[cfg(feature = "pci_support")] device_tree: Arc::clone(&device_tree), @@ -2998,11 +2998,11 @@ impl DeviceManager { } #[cfg(target_arch = "x86_64")] - pub fn io_bus(&self) -> &Arc { + pub fn io_bus(&self) -> &Arc { &self.address_manager.io_bus } - pub fn mmio_bus(&self) -> &Arc { + pub fn mmio_bus(&self) -> &Arc { &self.address_manager.mmio_bus } diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 3de8bb447..11f0383a6 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -15,8 +15,6 @@ use arch::x86_64::{SgxEpcRegion, SgxEpcSection}; use arch::{get_host_cpu_phys_bits, layout, RegionType}; #[cfg(target_arch = "x86_64")] use devices::ioapic; -use devices::BusDevice; - #[cfg(target_arch = "x86_64")] use libc::{MAP_NORESERVE, MAP_POPULATE, MAP_SHARED, PROT_READ, PROT_WRITE}; use std::collections::HashMap; @@ -33,6 +31,7 @@ use url::Url; #[cfg(target_arch = "x86_64")] use vm_allocator::GsiApic; use vm_allocator::SystemAllocator; +use vm_device::BusDevice; use vm_memory::guest_memory::FileOffset; use vm_memory::{ mmap::MmapRegionError, Address, Bytes, Error as MmapError, GuestAddress, GuestAddressSpace,