summary refs log tree commit diff
diff options
context:
space:
mode:
authorXiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>2019-10-29 17:32:44 +0800
committerCommit Bot <commit-bot@chromium.org>2019-11-10 06:39:31 +0000
commit87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1 (patch)
tree29bc942019a2cdb32d292c4650eccb5cf0deaddf
parent279248255f9f65c6baf044f1fbff0011bcd9938a (diff)
downloadcrosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar.gz
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar.bz2
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar.lz
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar.xz
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.tar.zst
crosvm-87a3b44d9e6caa82ab521a5dd9bafdb56e8b6ed1.zip
Resource: Unify mmio allocator
Current mmio and device two allocators exist, the purpose to define
two allocator is:
Accessing to gpa from mmio allocator cause vm exit, while gpa from
device allocator doesn't cause vm exit.

Whether vm exits exist or not, dependency on whether
vm->add_device_memory() is called with gpa from allocator or not.Even
if gpa is from mmio alloator, and vm->add_device_memory() is called
with this gpa, accessing this gpa won't cause vm exit. So mmio allocator
and device allocator couldn't guarantee the original purpose.

This patch unify mmio allocator and device allocator into one mmio
allocator.

BUG=chromium:992270
TEST=this patch doesn't change function, so just run build_test

Change-Id: If87d5c2838eb122ef627fa45c394b1b3ccfafeb0
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1895233
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
-rw-r--r--devices/src/pci/ac97.rs6
-rw-r--r--devices/src/pci/vfio_pci.rs4
-rw-r--r--devices/src/usb/xhci/xhci_controller.rs4
-rw-r--r--devices/src/virtio/virtio_pci_device.rs6
-rw-r--r--resources/src/lib.rs2
-rw-r--r--resources/src/system_allocator.rs29
-rw-r--r--src/linux.rs6
-rw-r--r--vm_control/src/lib.rs6
8 files changed, 35 insertions, 28 deletions
diff --git a/devices/src/pci/ac97.rs b/devices/src/pci/ac97.rs
index 2deef03..73a5093 100644
--- a/devices/src/pci/ac97.rs
+++ b/devices/src/pci/ac97.rs
@@ -5,7 +5,7 @@
 use std::os::unix::io::RawFd;
 
 use audio_streams::StreamSource;
-use resources::{Alloc, SystemAllocator};
+use resources::{Alloc, MmioType, SystemAllocator};
 use sys_util::{error, EventFd, GuestMemory};
 
 use crate::pci::ac97_bus_master::Ac97BusMaster;
@@ -149,7 +149,7 @@ impl PciDevice for Ac97Dev {
             .expect("assign_bus_dev must be called prior to allocate_io_bars");
         let mut ranges = Vec::new();
         let mixer_regs_addr = resources
-            .mmio_allocator()
+            .mmio_allocator(MmioType::Mmio)
             .allocate_with_align(
                 MIXER_REGS_SIZE,
                 Alloc::PciBar { bus, dev, bar: 0 },
@@ -167,7 +167,7 @@ impl PciDevice for Ac97Dev {
         ranges.push((mixer_regs_addr, MIXER_REGS_SIZE));
 
         let master_regs_addr = resources
-            .mmio_allocator()
+            .mmio_allocator(MmioType::Mmio)
             .allocate_with_align(
                 MASTER_REGS_SIZE,
                 Alloc::PciBar { bus, dev, bar: 1 },
diff --git a/devices/src/pci/vfio_pci.rs b/devices/src/pci/vfio_pci.rs
index a1e7a74..c10715d 100644
--- a/devices/src/pci/vfio_pci.rs
+++ b/devices/src/pci/vfio_pci.rs
@@ -8,7 +8,7 @@ use std::u32;
 
 use kvm::Datamatch;
 use msg_socket::{MsgReceiver, MsgSender};
-use resources::{Alloc, SystemAllocator};
+use resources::{Alloc, MmioType, SystemAllocator};
 use sys_util::{error, EventFd};
 
 use vfio_sys::*;
@@ -502,7 +502,7 @@ impl PciDevice for VfioPciDevice {
                 size |= u64::from(low);
                 size = !size + 1;
                 let bar_addr = resources
-                    .mmio_allocator()
+                    .mmio_allocator(MmioType::Mmio)
                     .allocate_with_align(
                         size,
                         Alloc::PciBar {
diff --git a/devices/src/usb/xhci/xhci_controller.rs b/devices/src/usb/xhci/xhci_controller.rs
index 5db31ef..dfba4a4 100644
--- a/devices/src/usb/xhci/xhci_controller.rs
+++ b/devices/src/usb/xhci/xhci_controller.rs
@@ -12,7 +12,7 @@ use crate::usb::xhci::xhci::Xhci;
 use crate::usb::xhci::xhci_backend_device_provider::XhciBackendDeviceProvider;
 use crate::usb::xhci::xhci_regs::{init_xhci_mmio_space_and_regs, XhciRegs};
 use crate::utils::FailHandle;
-use resources::{Alloc, SystemAllocator};
+use resources::{Alloc, MmioType, SystemAllocator};
 use std::mem;
 use std::os::unix::io::RawFd;
 use std::sync::atomic::{AtomicBool, Ordering};
@@ -213,7 +213,7 @@ impl PciDevice for XhciController {
             .expect("assign_bus_dev must be called prior to allocate_io_bars");
         // xHCI spec 5.2.1.
         let bar0_addr = resources
-            .mmio_allocator()
+            .mmio_allocator(MmioType::Mmio)
             .allocate_with_align(
                 XHCI_BAR0_SIZE,
                 Alloc::PciBar { bus, dev, bar: 0 },
diff --git a/devices/src/virtio/virtio_pci_device.rs b/devices/src/virtio/virtio_pci_device.rs
index 033d4ee..bd94fe3 100644
--- a/devices/src/virtio/virtio_pci_device.rs
+++ b/devices/src/virtio/virtio_pci_device.rs
@@ -10,7 +10,7 @@ use sync::Mutex;
 
 use data_model::{DataInit, Le32};
 use kvm::Datamatch;
-use resources::{Alloc, SystemAllocator};
+use resources::{Alloc, MmioType, SystemAllocator};
 use sys_util::{EventFd, GuestMemory, Result};
 
 use super::*;
@@ -384,7 +384,7 @@ impl PciDevice for VirtioPciDevice {
         // Allocate one bar for the structures pointed to by the capability structures.
         let mut ranges = Vec::new();
         let settings_config_addr = resources
-            .mmio_allocator()
+            .mmio_allocator(MmioType::Mmio)
             .allocate_with_align(
                 CAPABILITY_BAR_SIZE,
                 Alloc::PciBar { bus, dev, bar: 0 },
@@ -422,7 +422,7 @@ impl PciDevice for VirtioPciDevice {
         let mut ranges = Vec::new();
         for config in self.device.get_device_bars(bus, dev) {
             let device_addr = resources
-                .device_allocator()
+                .mmio_allocator(MmioType::Device)
                 .allocate_with_align(
                     config.get_size(),
                     Alloc::PciBar {
diff --git a/resources/src/lib.rs b/resources/src/lib.rs
index ce4855a..27f6abc 100644
--- a/resources/src/lib.rs
+++ b/resources/src/lib.rs
@@ -17,7 +17,7 @@ pub use crate::address_allocator::AddressAllocator;
 pub use crate::gpu_allocator::{
     GpuAllocatorError, GpuMemoryAllocator, GpuMemoryDesc, GpuMemoryPlaneDesc,
 };
-pub use crate::system_allocator::SystemAllocator;
+pub use crate::system_allocator::{MmioType, SystemAllocator};
 
 mod address_allocator;
 mod gpu_allocator;
diff --git a/resources/src/system_allocator.rs b/resources/src/system_allocator.rs
index 6cdb70e..b3a3062 100644
--- a/resources/src/system_allocator.rs
+++ b/resources/src/system_allocator.rs
@@ -13,7 +13,7 @@ use crate::{Alloc, Error, Result};
 /// # Example - Use the `SystemAddress` builder.
 ///
 /// ```
-/// # use resources::{Alloc, SystemAllocator};
+/// # use resources::{Alloc, MmioType, SystemAllocator};
 ///   if let Ok(mut a) = SystemAllocator::builder()
 ///           .add_io_addresses(0x1000, 0x10000)
 ///           .add_device_addresses(0x10000000, 0x10000000)
@@ -22,7 +22,7 @@ use crate::{Alloc, Error, Result};
 ///       assert_eq!(a.allocate_irq(), Some(5));
 ///       assert_eq!(a.allocate_irq(), Some(6));
 ///       assert_eq!(
-///           a.device_allocator()
+///           a.mmio_allocator(MmioType::Device)
 ///              .allocate(
 ///                  0x100,
 ///                  Alloc::PciBar { bus: 0, dev: 0, bar: 0 },
@@ -31,11 +31,20 @@ use crate::{Alloc, Error, Result};
 ///           Ok(0x10000000)
 ///       );
 ///       assert_eq!(
-///           a.device_allocator().get(&Alloc::PciBar { bus: 0, dev: 0, bar: 0 }),
+///           a.mmio_allocator(MmioType::Device).get(&Alloc::PciBar { bus: 0, dev: 0, bar: 0 }),
 ///           Some(&(0x10000000, 0x100, "bar0".to_string()))
 ///       );
 ///   }
 /// ```
+
+/// MMIO address Type
+///    Mmio: address allocated from mmio_address_space
+///    Device: address allocated from device_address_space
+pub enum MmioType {
+    Mmio,
+    Device,
+}
+
 #[derive(Debug)]
 pub struct SystemAllocator {
     io_address_space: Option<AddressAllocator>,
@@ -108,14 +117,12 @@ impl SystemAllocator {
         self.io_address_space.as_mut()
     }
 
-    /// Gets an allocator to be used for device memory.
-    pub fn device_allocator(&mut self) -> &mut AddressAllocator {
-        &mut self.device_address_space
-    }
-
-    /// Gets an allocator to be used for MMIO memory.
-    pub fn mmio_allocator(&mut self) -> &mut AddressAllocator {
-        &mut self.mmio_address_space
+    /// Gets an allocator to be used for MMIO allocation.
+    pub fn mmio_allocator(&mut self, mmio_type: MmioType) -> &mut AddressAllocator {
+        match mmio_type {
+            MmioType::Device => &mut self.device_address_space,
+            MmioType::Mmio => &mut self.mmio_address_space,
+        }
     }
 
     /// Gets an allocator to be used for GPU memory.
diff --git a/src/linux.rs b/src/linux.rs
index 0a3860e..2816d55 100644
--- a/src/linux.rs
+++ b/src/linux.rs
@@ -38,7 +38,7 @@ use msg_socket::{MsgError, MsgReceiver, MsgSender, MsgSocket};
 use net_util::{Error as NetError, MacAddress, Tap};
 use rand_ish::SimpleRng;
 use remain::sorted;
-use resources::{Alloc, SystemAllocator};
+use resources::{Alloc, MmioType, SystemAllocator};
 use sync::{Condvar, Mutex};
 use sys_util::net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener};
 
@@ -784,7 +784,7 @@ fn create_pmem_device(
     };
 
     let mapping_address = resources
-        .device_allocator()
+        .mmio_allocator(MmioType::Device)
         .allocate_with_align(
             image_size,
             Alloc::PmemDevice(index),
@@ -1424,7 +1424,7 @@ pub fn run_config(cfg: Config) -> Result<()> {
         // guest address space.
         let gpu_addr = linux
             .resources
-            .device_allocator()
+            .mmio_allocator(MmioType::Device)
             .allocate(
                 RENDER_NODE_HOST_SIZE,
                 Alloc::GpuRenderNode,
diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs
index f25ab20..71bb193 100644
--- a/vm_control/src/lib.rs
+++ b/vm_control/src/lib.rs
@@ -19,7 +19,7 @@ use libc::{EINVAL, EIO, ENODEV};
 
 use kvm::{IrqRoute, IrqSource, Vm};
 use msg_socket::{MsgOnSocket, MsgReceiver, MsgResult, MsgSender, MsgSocket};
-use resources::{Alloc, GpuMemoryDesc, SystemAllocator};
+use resources::{Alloc, GpuMemoryDesc, MmioType, SystemAllocator};
 use sys_util::{error, Error as SysError, EventFd, GuestAddress, MemoryMapping, MmapError, Result};
 
 /// A file descriptor either borrowed or owned by this.
@@ -404,7 +404,7 @@ fn register_memory(
     let addr = match allocation {
         Some((Alloc::PciBar { bus, dev, bar }, address)) => {
             match allocator
-                .device_allocator()
+                .mmio_allocator(MmioType::Device)
                 .get(&Alloc::PciBar { bus, dev, bar })
             {
                 Some((start_addr, length, _)) => {
@@ -420,7 +420,7 @@ fn register_memory(
         }
         None => {
             let alloc = allocator.get_anon_alloc();
-            match allocator.device_allocator().allocate(
+            match allocator.mmio_allocator(MmioType::Device).allocate(
                 size as u64,
                 alloc,
                 "vmcontrol_register_memory".to_string(),