From 34d1f435f4bf5e40e96951470a0111c027e8a03d Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 11 Mar 2020 09:35:12 +0100 Subject: [PATCH] vfio: pci: Implement free_bars() from the PciDevice trait In order to provide the tools for a complete cleanup whenever a VFIO PCI device is removed from the VM, the VfioPciDevice implements free_bars() method from PciDevice trait. This will take care of removing the IO and MMIO ranges previously reserved through the vm-allocator. Signed-off-by: Sebastien Boeuf --- vfio/src/vfio_pci.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vfio/src/vfio_pci.rs b/vfio/src/vfio_pci.rs index 4ebe43ada..176ac0823 100644 --- a/vfio/src/vfio_pci.rs +++ b/vfio/src/vfio_pci.rs @@ -222,6 +222,7 @@ impl Interrupt { struct MmioRegion { start: GuestAddress, length: GuestUsize, + type_: PciBarRegionType, index: u32, mem_slot: Option, host_addr: Option, @@ -799,6 +800,7 @@ impl PciDevice for VfioPciDevice { self.mmio_regions.push(MmioRegion { start: bar_addr, length: region_size, + type_: region_type, index: bar_id as u32, mem_slot: None, host_addr: None, @@ -818,6 +820,26 @@ impl PciDevice for VfioPciDevice { Ok(ranges) } + fn free_bars( + &mut self, + allocator: &mut SystemAllocator, + ) -> std::result::Result<(), PciDeviceError> { + for region in self.mmio_regions.iter() { + match region.type_ { + PciBarRegionType::IORegion => { + allocator.free_io_addresses(region.start, region.length); + } + PciBarRegionType::Memory32BitRegion => { + allocator.free_mmio_hole_addresses(region.start, region.length); + } + PciBarRegionType::Memory64BitRegion => { + allocator.free_mmio_addresses(region.start, region.length); + } + } + } + Ok(()) + } + fn write_config_register(&mut self, reg_idx: usize, offset: u64, data: &[u8]) { // When the guest wants to write to a BAR, we trap it into // our local configuration space. We're not reprogramming