From def98faf37a59d5e0f53a570688dbacb6ec3c525 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 29 Oct 2021 10:58:41 +0100 Subject: [PATCH] vmm, vm-allocator: Introduce an allocator for platform devices This allocator allocates 64-bit MMIO addresses for use with platform devices e.g. ACPI control devices and ensures there is no overlap with PCI address space ranges which can cause issues with PCI device remapping. Use this allocator the ACPI platform devices. Signed-off-by: Rob Bradford --- vm-allocator/src/system.rs | 29 ++++++++++++++++------------- vmm/src/cpu.rs | 2 +- vmm/src/device_manager.rs | 8 ++++++-- vmm/src/memory_manager.rs | 13 +++++++++---- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/vm-allocator/src/system.rs b/vm-allocator/src/system.rs index eb44b6f44..e879ce105 100644 --- a/vm-allocator/src/system.rs +++ b/vm-allocator/src/system.rs @@ -47,13 +47,13 @@ fn pagesize() -> usize { /// assert_eq!(allocator.allocate_irq(), Some(6)); /// #[cfg(target_arch = "aarch64")] /// assert_eq!(allocator.allocate_irq(), Some(33)); -/// assert_eq!(allocator.allocate_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); +/// assert_eq!(allocator.allocate_platform_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); /// /// ``` pub struct SystemAllocator { #[cfg(target_arch = "x86_64")] io_address_space: AddressAllocator, - mmio_address_space: AddressAllocator, + platform_mmio_address_space: AddressAllocator, mmio_hole_address_space: AddressAllocator, gsi_allocator: GsiAllocator, } @@ -64,8 +64,8 @@ impl SystemAllocator { /// /// * `io_base` - (X86) The starting address of IO memory. /// * `io_size` - (X86) The size of IO memory. - /// * `mmio_base` - The starting address of MMIO memory. - /// * `mmio_size` - The size of MMIO memory. + /// * `platform_mmio_base` - The starting address of platform MMIO memory. + /// * `platform_mmio_size` - The size of platform MMIO memory. /// * `mmio_hole_base` - The starting address of MMIO memory in 32-bit address space. /// * `mmio_hole_size` - The size of MMIO memory in 32-bit address space. /// * `apics` - (X86) Vector of APIC's. @@ -73,8 +73,8 @@ impl SystemAllocator { pub fn new( #[cfg(target_arch = "x86_64")] io_base: GuestAddress, #[cfg(target_arch = "x86_64")] io_size: GuestUsize, - mmio_base: GuestAddress, - mmio_size: GuestUsize, + platform_mmio_base: GuestAddress, + platform_mmio_size: GuestUsize, mmio_hole_base: GuestAddress, mmio_hole_size: GuestUsize, #[cfg(target_arch = "x86_64")] apics: Vec, @@ -82,7 +82,10 @@ impl SystemAllocator { Some(SystemAllocator { #[cfg(target_arch = "x86_64")] io_address_space: AddressAllocator::new(io_base, io_size)?, - mmio_address_space: AddressAllocator::new(mmio_base, mmio_size)?, + platform_mmio_address_space: AddressAllocator::new( + platform_mmio_base, + platform_mmio_size, + )?, mmio_hole_address_space: AddressAllocator::new(mmio_hole_base, mmio_hole_size)?, #[cfg(target_arch = "x86_64")] gsi_allocator: GsiAllocator::new(apics), @@ -113,14 +116,14 @@ impl SystemAllocator { .allocate(address, size, Some(align_size.unwrap_or(0x1))) } - /// Reserves a section of `size` bytes of MMIO address space. - pub fn allocate_mmio_addresses( + /// Reserves a section of `size` bytes of platform MMIO address space. + pub fn allocate_platform_mmio_addresses( &mut self, address: Option, size: GuestUsize, align_size: Option, ) -> Option { - self.mmio_address_space.allocate( + self.platform_mmio_address_space.allocate( address, size, Some(align_size.unwrap_or(pagesize() as u64)), @@ -148,10 +151,10 @@ impl SystemAllocator { self.io_address_space.free(address, size) } - /// Free an MMIO address range. + /// Free a platform MMIO address range. /// We can only free a range if it matches exactly an already allocated range. - pub fn free_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { - self.mmio_address_space.free(address, size) + pub fn free_platform_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { + self.platform_mmio_address_space.free(address, size) } /// Free an MMIO address range from the 32 bits hole. diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index e83bf8e73..4c59a32da 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -575,7 +575,7 @@ impl CpuManager { .allocator() .lock() .unwrap() - .allocate_mmio_addresses(None, CPU_MANAGER_ACPI_SIZE as u64, None) + .allocate_platform_mmio_addresses(None, CPU_MANAGER_ACPI_SIZE as u64, None) .ok_or(Error::AllocateMmmioAddress)?; #[cfg(feature = "acpi")] diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 66c4cb32a..3cf67ebff 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -975,7 +975,7 @@ impl DeviceManager { .allocator .lock() .unwrap() - .allocate_mmio_addresses(None, DEVICE_MANAGER_ACPI_SIZE as u64, None) + .allocate_platform_mmio_addresses(None, DEVICE_MANAGER_ACPI_SIZE as u64, None) .ok_or(DeviceManagerError::AllocateIoPort)?; let mut pci_irq_slots = [0; 32]; @@ -1366,7 +1366,11 @@ impl DeviceManager { .allocator .lock() .unwrap() - .allocate_mmio_addresses(None, devices::acpi::GED_DEVICE_ACPI_SIZE as u64, None) + .allocate_platform_mmio_addresses( + None, + devices::acpi::GED_DEVICE_ACPI_SIZE as u64, + None, + ) .ok_or(DeviceManagerError::AllocateMmioAddress)?; let ged_device = Arc::new(Mutex::new(devices::AcpiGedDevice::new( interrupt_group, diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index ab974a818..8f320ffe0 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -63,6 +63,9 @@ const MPOL_BIND: u32 = 2; const MPOL_MF_STRICT: u32 = 1; const MPOL_MF_MOVE: u32 = 1 << 1; +// Reserve 1 MiB for platform MMIO devices (e.g. ACPI control devices) +const PLATFORM_DEVICE_AREA_SIZE: u64 = 1 << 20; + #[derive(Clone, Default, Serialize, Deserialize, Versionize)] struct HotPlugState { base: u64, @@ -827,7 +830,9 @@ impl MemoryManager { (((mmio_address_space_size) >> 16) << 16), mmio_address_space_size ); - let end_of_device_area = GuestAddress(mmio_address_space_size - 1); + let start_of_platform_device_area = + GuestAddress(mmio_address_space_size - PLATFORM_DEVICE_AREA_SIZE); + let end_of_device_area = start_of_platform_device_area.unchecked_sub(1); let (ram_size, zones, allow_mem_hotplug) = Self::validate_memory_config(config, user_provided_zones)?; @@ -989,8 +994,8 @@ impl MemoryManager { { 1 << 16 }, - GuestAddress(0), - mmio_address_space_size, + start_of_platform_device_area, + PLATFORM_DEVICE_AREA_SIZE, layout::MEM_32BIT_DEVICES_START, layout::MEM_32BIT_DEVICES_SIZE, #[cfg(target_arch = "x86_64")] @@ -1006,7 +1011,7 @@ impl MemoryManager { let acpi_address = allocator .lock() .unwrap() - .allocate_mmio_addresses(None, MEMORY_MANAGER_ACPI_SIZE as u64, None) + .allocate_platform_mmio_addresses(None, MEMORY_MANAGER_ACPI_SIZE as u64, None) .ok_or(Error::AllocateMmioAddress)?; #[cfg(not(feature = "tdx"))]