summary refs log tree commit diff
path: root/vm_control
diff options
context:
space:
mode:
authorGurchetan Singh <gurchetansingh@chromium.org>2020-05-04 09:39:43 -0700
committerCommit Bot <commit-bot@chromium.org>2020-06-12 05:00:42 +0000
commit014b351f588c85577520898eb73e72bee973b7f2 (patch)
tree662344a59a712f20ea7005e73cb6e782fca2b331 /vm_control
parent173fe62df2b82f4d09a36066200f0a1727bd1d22 (diff)
downloadcrosvm-014b351f588c85577520898eb73e72bee973b7f2.tar
crosvm-014b351f588c85577520898eb73e72bee973b7f2.tar.gz
crosvm-014b351f588c85577520898eb73e72bee973b7f2.tar.bz2
crosvm-014b351f588c85577520898eb73e72bee973b7f2.tar.lz
crosvm-014b351f588c85577520898eb73e72bee973b7f2.tar.xz
crosvm-014b351f588c85577520898eb73e72bee973b7f2.tar.zst
crosvm-014b351f588c85577520898eb73e72bee973b7f2.zip
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 <dverkamp@chromium.org>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Gurchetan Singh <gurchetansingh@chromium.org>
Diffstat (limited to 'vm_control')
-rw-r--r--vm_control/src/lib.rs50
1 files changed, 10 insertions, 40 deletions
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)?;