diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 3f3cd665f..1fec39b31 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -549,7 +549,6 @@ impl DeviceManager { pub fn new( vm_fd: Arc, config: Arc>, - allocator: Arc>, memory_manager: Arc>, _exit_evt: &EventFd, reset_evt: &EventFd, @@ -564,7 +563,7 @@ impl DeviceManager { let mut cmdline_additions = Vec::new(); let address_manager = Arc::new(AddressManager { - allocator, + allocator: memory_manager.lock().unwrap().allocator(), io_bus: Arc::new(devices::Bus::new()), mmio_bus: Arc::new(devices::Bus::new()), vm_fd: vm_fd.clone(), diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index fffb3f01f..ffb305744 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -6,8 +6,8 @@ use crate::config::{HotplugMethod, MemoryConfig}; #[cfg(feature = "acpi")] use acpi_tables::{aml, aml::Aml}; -use arch::RegionType; -use devices::BusDevice; +use arch::{layout, RegionType}; +use devices::{ioapic, BusDevice}; use kvm_bindings::{kvm_userspace_memory_region, KVM_MEM_READONLY}; use kvm_ioctls::*; use std::convert::TryInto; @@ -16,7 +16,7 @@ use std::io; use std::os::unix::io::FromRawFd; use std::path::PathBuf; use std::sync::{Arc, Mutex}; -use vm_allocator::SystemAllocator; +use vm_allocator::{GsiApic, SystemAllocator}; use vm_memory::guest_memory::FileOffset; use vm_memory::{ mmap::MmapRegionError, Address, Error as MmapError, GuestAddress, GuestAddressSpace, @@ -25,6 +25,8 @@ use vm_memory::{ }; use vm_migration::{Migratable, Pausable, Snapshottable, Transportable}; +const X86_64_IRQ_BASE: u32 = 5; + const HOTPLUG_COUNT: usize = 8; #[derive(Default)] @@ -95,6 +97,9 @@ pub enum Error { /// Failed to virtio-mem resize VirtioMemResizeFail(vm_virtio::mem::Error), + + /// Cannot create the system allocator + CreateSystemAllocator, } pub fn get_host_cpu_phys_bits() -> u8 { @@ -206,11 +211,7 @@ impl BusDevice for MemoryManager { } impl MemoryManager { - pub fn new( - allocator: Arc>, - fd: Arc, - config: &MemoryConfig, - ) -> Result>, Error> { + pub fn new(fd: Arc, config: &MemoryConfig) -> Result>, Error> { // Init guest memory let arch_mem_regions = arch::arch_memory_regions(config.size); @@ -267,6 +268,23 @@ impl MemoryManager { let mut hotplug_slots = Vec::with_capacity(HOTPLUG_COUNT); hotplug_slots.resize_with(HOTPLUG_COUNT, HotPlugState::default); + // Let's allocate 64 GiB of addressable MMIO space, starting at 0. + let allocator = Arc::new(Mutex::new( + SystemAllocator::new( + GuestAddress(0), + 1 << 16 as GuestUsize, + GuestAddress(0), + 1 << get_host_cpu_phys_bits(), + layout::MEM_32BIT_RESERVED_START, + layout::MEM_32BIT_DEVICES_SIZE, + vec![GsiApic::new( + X86_64_IRQ_BASE, + ioapic::NUM_IOAPIC_PINS as u32 - X86_64_IRQ_BASE, + )], + ) + .ok_or(Error::CreateSystemAllocator)?, + )); + let memory_manager = Arc::new(Mutex::new(MemoryManager { guest_memory: guest_memory.clone(), next_kvm_memory_slot: 0, @@ -440,6 +458,10 @@ impl MemoryManager { self.guest_memory.clone() } + pub fn allocator(&self) -> Arc> { + self.allocator.clone() + } + pub fn start_of_device_area(&self) -> GuestAddress { self.start_of_device_area } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 7737eb95f..dc470338e 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -29,9 +29,9 @@ extern crate vm_virtio; use crate::config::{DeviceConfig, DiskConfig, HotplugMethod, NetConfig, PmemConfig, VmConfig}; use crate::cpu; use crate::device_manager::{get_win_size, Console, DeviceManager, DeviceManagerError}; -use crate::memory_manager::{get_host_cpu_phys_bits, Error as MemoryManagerError, MemoryManager}; +use crate::memory_manager::{Error as MemoryManagerError, MemoryManager}; use anyhow::anyhow; -use arch::{layout, BootProtocol, EntryPoint}; +use arch::{BootProtocol, EntryPoint}; use devices::{ioapic, HotPlugNotificationFlags}; use kvm_bindings::{kvm_enable_cap, kvm_userspace_memory_region, KVM_CAP_SPLIT_IRQCHIP}; use kvm_ioctls::*; @@ -47,17 +47,14 @@ use std::ops::Deref; use std::path::PathBuf; use std::sync::{Arc, Mutex, RwLock}; use std::{result, str, thread}; -use vm_allocator::{GsiApic, SystemAllocator}; use vm_memory::{ Address, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap, - GuestMemoryRegion, GuestUsize, + GuestMemoryRegion, }; use vm_migration::{Migratable, MigratableError, Pausable, Snapshottable, Transportable}; use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::terminal::Terminal; -const X86_64_IRQ_BASE: u32 = 5; - // 64 bit direct boot entry offset for bzImage const KERNEL_64BIT_ENTRY_OFFSET: u64 = 0x200; @@ -111,9 +108,6 @@ pub enum Error { /// Cannot setup terminal in canonical mode. SetTerminalCanon(vmm_sys_util::errno::Error), - /// Cannot create the system allocator - CreateSystemAllocator, - /// Failed parsing network parameters ParseNetworkParameters, @@ -317,35 +311,13 @@ impl Vm { .transpose() .map_err(Error::InitramfsFile)?; - // Let's allocate 64 GiB of addressable MMIO space, starting at 0. - let allocator = Arc::new(Mutex::new( - SystemAllocator::new( - GuestAddress(0), - 1 << 16 as GuestUsize, - GuestAddress(0), - 1 << get_host_cpu_phys_bits(), - layout::MEM_32BIT_RESERVED_START, - layout::MEM_32BIT_DEVICES_SIZE, - vec![GsiApic::new( - X86_64_IRQ_BASE, - ioapic::NUM_IOAPIC_PINS as u32 - X86_64_IRQ_BASE, - )], - ) - .ok_or(Error::CreateSystemAllocator)?, - )); - - let memory_manager = MemoryManager::new( - allocator.clone(), - fd.clone(), - &config.lock().unwrap().memory.clone(), - ) - .map_err(Error::MemoryManager)?; + let memory_manager = MemoryManager::new(fd.clone(), &config.lock().unwrap().memory.clone()) + .map_err(Error::MemoryManager)?; let guest_memory = memory_manager.lock().unwrap().guest_memory(); let device_manager = DeviceManager::new( fd.clone(), config.clone(), - allocator, memory_manager.clone(), &exit_evt, &reset_evt,