summary refs log tree commit diff
path: root/resources
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-06-14 11:25:18 +0000
committerAlyssa Ross <hi@alyssa.is>2020-06-14 11:25:18 +0000
commitb7966a9d2e768533acac0f37bdeb293c256109d3 (patch)
tree357a365ecc99e4bec214d084352e316769f70041 /resources
parent1e318da5b57c12f67bed3b528100dbe4ec287ac5 (diff)
parentd42d3fec7a9535b664b89d30fd48c90feda59957 (diff)
downloadcrosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar.gz
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar.bz2
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar.lz
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar.xz
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.tar.zst
crosvm-b7966a9d2e768533acac0f37bdeb293c256109d3.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'resources')
-rw-r--r--resources/src/address_allocator.rs82
-rw-r--r--resources/src/lib.rs4
2 files changed, 86 insertions, 0 deletions
diff --git a/resources/src/address_allocator.rs b/resources/src/address_allocator.rs
index 88f652a..dce904f 100644
--- a/resources/src/address_allocator.rs
+++ b/resources/src/address_allocator.rs
@@ -215,6 +215,27 @@ impl AddressAllocator {
 
         Ok(())
     }
+
+    /// Returns an address from associated PCI `alloc` given an allocation offset and size.
+    pub fn address_from_pci_offset(&self, alloc: Alloc, offset: u64, size: u64) -> Result<u64> {
+        match alloc {
+            Alloc::PciBar { .. } => (),
+            _ => return Err(Error::InvalidAlloc(alloc)),
+        };
+
+        match self.allocs.get(&alloc) {
+            Some((start_addr, length, _)) => {
+                let address = start_addr.checked_add(offset).ok_or(Error::OutOfBounds)?;
+                let range = *start_addr..*start_addr + *length;
+                let end = address.checked_add(size).ok_or(Error::OutOfBounds)?;
+                match (range.contains(&address), range.contains(&end)) {
+                    (true, true) => Ok(address),
+                    _ => return Err(Error::OutOfBounds),
+                }
+            }
+            None => return Err(Error::InvalidAlloc(alloc)),
+        }
+    }
 }
 
 #[cfg(test)]
@@ -422,4 +443,65 @@ mod tests {
             Ok(0x1000)
         );
     }
+
+    #[test]
+    fn allocate_and_verify_pci_offset() {
+        let mut pool = AddressAllocator::new(0x1000, 0x10000, None).unwrap();
+        let pci_bar0 = Alloc::PciBar {
+            bus: 1,
+            dev: 2,
+            func: 0,
+            bar: 0,
+        };
+        let pci_bar1 = Alloc::PciBar {
+            bus: 1,
+            dev: 2,
+            func: 0,
+            bar: 1,
+        };
+        let pci_bar2 = Alloc::PciBar {
+            bus: 1,
+            dev: 2,
+            func: 0,
+            bar: 2,
+        };
+        let anon = Alloc::Anon(1);
+
+        assert_eq!(
+            pool.allocate(0x800, pci_bar0, String::from("bar0")),
+            Ok(0x1000)
+        );
+        assert_eq!(
+            pool.allocate(0x800, pci_bar1, String::from("bar1")),
+            Ok(0x1800)
+        );
+        assert_eq!(pool.allocate(0x800, anon, String::from("anon")), Ok(0x2000));
+
+        assert_eq!(
+            pool.address_from_pci_offset(pci_bar0, 0x600, 0x100),
+            Ok(0x1600)
+        );
+        assert_eq!(
+            pool.address_from_pci_offset(pci_bar1, 0x600, 0x100),
+            Ok(0x1E00)
+        );
+        assert_eq!(
+            pool.address_from_pci_offset(pci_bar0, 0x7FE, 0x001),
+            Ok(0x17FE)
+        );
+        assert_eq!(
+            pool.address_from_pci_offset(pci_bar0, 0x7FF, 0x001),
+            Err(Error::OutOfBounds)
+        );
+
+        assert_eq!(
+            pool.address_from_pci_offset(pci_bar2, 0x7FF, 0x001),
+            Err(Error::InvalidAlloc(pci_bar2))
+        );
+
+        assert_eq!(
+            pool.address_from_pci_offset(anon, 0x600, 0x100),
+            Err(Error::InvalidAlloc(anon))
+        );
+    }
 }
diff --git a/resources/src/lib.rs b/resources/src/lib.rs
index 6195e91..204227b 100644
--- a/resources/src/lib.rs
+++ b/resources/src/lib.rs
@@ -40,10 +40,12 @@ pub enum Error {
     BadAlignment,
     CreateGpuAllocator(GpuAllocatorError),
     ExistingAlloc(Alloc),
+    InvalidAlloc(Alloc),
     MissingHighMMIOAddresses,
     MissingLowMMIOAddresses,
     NoIoAllocator,
     OutOfSpace,
+    OutOfBounds,
     PoolOverflow { base: u64, size: u64 },
     PoolSizeZero,
     RegionOverlap { base: u64, size: u64 },
@@ -60,10 +62,12 @@ impl Display for Error {
             BadAlignment => write!(f, "Pool alignment must be a power of 2"),
             CreateGpuAllocator(e) => write!(f, "Failed to create GPU allocator: {:?}", e),
             ExistingAlloc(tag) => write!(f, "Alloc already exists: {:?}", tag),
+            InvalidAlloc(tag) => write!(f, "Invalid Alloc: {:?}", tag),
             MissingHighMMIOAddresses => write!(f, "High MMIO address range not specified"),
             MissingLowMMIOAddresses => write!(f, "Low MMIO address range not specified"),
             NoIoAllocator => write!(f, "No IO address range specified"),
             OutOfSpace => write!(f, "Out of space"),
+            OutOfBounds => write!(f, "Out of bounds"),
             PoolOverflow { base, size } => write!(f, "base={} + size={} overflows", base, size),
             PoolSizeZero => write!(f, "Pool cannot have size of 0"),
             RegionOverlap { base, size } => {