summary refs log tree commit diff
path: root/aarch64
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-02-13 19:39:20 +0000
committerAlyssa Ross <hi@alyssa.is>2020-06-15 09:36:06 +0000
commit83c9638a44fb1d4b6952fad6ab77992d902576ea (patch)
tree105cb2a92f60934dd31af49bdb522cab6ba740be /aarch64
parentb8f316251bc594cb7d12e4bf0e383362944b5da3 (diff)
downloadcrosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar.gz
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar.bz2
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar.lz
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar.xz
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.tar.zst
crosvm-83c9638a44fb1d4b6952fad6ab77992d902576ea.zip
arch: add public API for creating GuestMemory
For inter-guest communication, other processes need to be able to
access guest memory, without creating a VM.  GuestMemory, with its
list of memory regions, can't derive MsgOnSocket.  This means that
GuestMemory structs can't (easily) be shared over a socket.  However,
all that is required to create a GuestMemory is a size, and on x86_64,
a boolean has_bios field.

The architecture differences can be encapsulated in a struct.  It can
be opaque outside of architecture-specific crates, so the rest of the
code can remain architecture-independent.
Diffstat (limited to 'aarch64')
-rw-r--r--aarch64/Cargo.toml1
-rw-r--r--aarch64/src/lib.rs36
2 files changed, 28 insertions, 9 deletions
diff --git a/aarch64/Cargo.toml b/aarch64/Cargo.toml
index 7e346ac..c87c419 100644
--- a/aarch64/Cargo.toml
+++ b/aarch64/Cargo.toml
@@ -13,6 +13,7 @@ kernel_cmdline = { path = "../kernel_cmdline" }
 kvm = { path = "../kvm" }
 kvm_sys = { path = "../kvm_sys" }
 libc = "*"
+msg_socket = { path = "../msg_socket" }
 remain = "*"
 resources = { path = "../resources" }
 sync = { path = "../sync" }
diff --git a/aarch64/src/lib.rs b/aarch64/src/lib.rs
index 0a90fd4..e80c934 100644
--- a/aarch64/src/lib.rs
+++ b/aarch64/src/lib.rs
@@ -17,6 +17,7 @@ use arch::{
 };
 use devices::{Bus, BusError, PciAddress, PciConfigMmio, PciDevice, PciInterruptPin};
 use io_jail::Minijail;
+use msg_socket::MsgOnSocket;
 use remain::sorted;
 use resources::SystemAllocator;
 use sync::Mutex;
@@ -180,10 +181,25 @@ pub type Result<T> = std::result::Result<T, Error>;
 
 impl std::error::Error for Error {}
 
+#[derive(Clone, Copy, Debug, MsgOnSocket)]
+pub struct MemoryParams {
+    size: u64,
+}
+
+impl MemoryParams {
+    // This should never be public to prevent architecture-specific code,
+    // but pub(crate) would be okay.
+    fn new(components: &VmComponents) -> Self {
+        MemoryParams {
+            size: components.memory_size,
+        }
+    }
+}
+
 /// Returns a Vec of the valid memory addresses.
 /// These should be used to configure the GuestMemory structure for the platfrom.
-pub fn arch_memory_regions(size: u64) -> Vec<(GuestAddress, u64)> {
-    vec![(GuestAddress(AARCH64_PHYS_MEM_START), size)]
+pub fn arch_memory_regions(params: MemoryParams) -> Vec<(GuestAddress, u64)> {
+    vec![(GuestAddress(AARCH64_PHYS_MEM_START), params.size)]
 }
 
 fn fdt_offset(mem_size: u64) -> u64 {
@@ -197,6 +213,7 @@ pub struct AArch64;
 
 impl arch::LinuxArch for AArch64 {
     type Error = Error;
+    type MemoryParams = MemoryParams;
 
     fn build_vm<F, E>(
         mut components: VmComponents,
@@ -217,7 +234,8 @@ impl arch::LinuxArch for AArch64 {
     {
         let mut resources =
             Self::get_resource_allocator(components.memory_size, components.wayland_dmabuf);
-        let mem = Self::setup_memory(components.memory_size)?;
+        let mem_params = MemoryParams::new(&components);
+        let mem = Self::setup_memory(mem_params)?;
         let kvm = Kvm::new().map_err(Error::CreateKvm)?;
         let mut vm = Vm::new(&kvm, mem.clone()).map_err(Error::CreateVm)?;
 
@@ -342,6 +360,12 @@ impl arch::LinuxArch for AArch64 {
             suspend_evt,
         })
     }
+
+    fn setup_memory(params: MemoryParams) -> Result<GuestMemory> {
+        let arch_mem_regions = arch_memory_regions(params);
+        let mem = GuestMemory::new(&arch_mem_regions).map_err(Error::SetupGuestMemory)?;
+        Ok(mem)
+    }
 }
 
 impl AArch64 {
@@ -390,12 +414,6 @@ impl AArch64 {
         Ok(())
     }
 
-    fn setup_memory(mem_size: u64) -> Result<GuestMemory> {
-        let arch_mem_regions = arch_memory_regions(mem_size);
-        let mem = GuestMemory::new(&arch_mem_regions).map_err(Error::SetupGuestMemory)?;
-        Ok(mem)
-    }
-
     fn get_high_mmio_base_size(mem_size: u64) -> (u64, u64) {
         let base = AARCH64_PHYS_MEM_START + mem_size;
         let size = u64::max_value() - base;