From 014b351f588c85577520898eb73e72bee973b7f2 Mon Sep 17 00:00:00 2001 From: Gurchetan Singh Date: Mon, 4 May 2020 09:39:43 -0700 Subject: resources: add address_from_pci_offset function Refactor current code and add tests. BUG=chromium:924405 TEST=compile and test Change-Id: I9476f3a4ffd8ae85fc95d6889ada6b056613bbfa Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2216447 Reviewed-by: Daniel Verkamp Commit-Queue: Gurchetan Singh Tested-by: Gurchetan Singh Tested-by: kokoro Auto-Submit: Gurchetan Singh --- vm_control/src/lib.rs | 50 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 40 deletions(-) (limited to 'vm_control/src') diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs index f7514c2..5336f3b 100644 --- a/vm_control/src/lib.rs +++ b/vm_control/src/lib.rs @@ -535,7 +535,7 @@ fn register_memory( allocator: &mut SystemAllocator, fd: &dyn AsRawFd, size: usize, - allocation: Option<(Alloc, u64)>, + pci_allocation: Option<(Alloc, u64)>, ) -> Result<(u64, u32)> { let mmap = match MemoryMapping::from_fd(fd, size) { Ok(v) => v, @@ -543,48 +543,18 @@ fn register_memory( _ => return Err(SysError::new(EINVAL)), }; - let addr = match allocation { - Some(( - Alloc::PciBar { - bus, - dev, - func, - bar, - }, - offset, - )) => { - match allocator - .mmio_allocator(MmioType::High) - .get(&Alloc::PciBar { - bus, - dev, - func, - bar, - }) { - Some((start_addr, length, _)) => { - let address = *start_addr + offset; - let range = *start_addr..*start_addr + *length; - let end = address + (size as u64); - match (range.contains(&address), range.contains(&end)) { - (true, true) => address, - _ => return Err(SysError::new(EINVAL)), - } - } - None => return Err(SysError::new(EINVAL)), - } - } + let addr = match pci_allocation { + Some(pci_allocation) => allocator + .mmio_allocator(MmioType::High) + .address_from_pci_offset(pci_allocation.0, pci_allocation.1, size as u64) + .map_err(|_e| SysError::new(EINVAL))?, None => { let alloc = allocator.get_anon_alloc(); - match allocator.mmio_allocator(MmioType::High).allocate( - size as u64, - alloc, - "vmcontrol_register_memory".to_string(), - ) { - Ok(a) => a, - _ => return Err(SysError::new(EINVAL)), - } + allocator + .mmio_allocator(MmioType::High) + .allocate(size as u64, alloc, "vmcontrol_register_memory".to_string()) + .map_err(|_e| SysError::new(EINVAL))? } - _ => return Err(SysError::new(EINVAL)), }; let slot = vm.add_memory_region(GuestAddress(addr), Box::new(mmap), false, false)?; -- cgit 1.4.1