summary refs log tree commit diff
path: root/kvm
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-01-28 19:56:36 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-01-30 01:25:56 -0800
commitf7797d15b492046df49670c166b175fa28e7ed8b (patch)
tree9613dcb9fd74794d5f8b42d569d7df0d0a414fbb /kvm
parent4133b0120d1e16cafbb373b2ae17a214b594038b (diff)
downloadcrosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar.gz
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar.bz2
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar.lz
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar.xz
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.tar.zst
crosvm-f7797d15b492046df49670c166b175fa28e7ed8b.zip
kvm: take a reference to GuestMemory in Vcpu
Logically, each VCPU is using GuestMemory, which holds a ref count to
the underlying memory mappings. This change formalizes this by giving an
actual reference of GuestMemory to each Vcpu struct.

This change is needed because the Vm can go out of scope and clean up
its reference, but the Vcpus may still be running, triggering an EFAULT
and a lot of confused days spent debugging.

TEST=With the unwind panic handler, trigger a panic right after the
     final vcpu thread barrier. If the VCPU threads do not complain
     about EFAULT (errno 14), this change worked.
BUG=None

Change-Id: I6289147de0adde61c81630357701487937b17ade
Reviewed-on: https://chromium-review.googlesource.com/1441355
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'kvm')
-rw-r--r--kvm/src/lib.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/kvm/src/lib.rs b/kvm/src/lib.rs
index 17d5a74..a9f2e8a 100644
--- a/kvm/src/lib.rs
+++ b/kvm/src/lib.rs
@@ -990,6 +990,7 @@ pub enum VcpuExit {
 pub struct Vcpu {
     vcpu: File,
     run_mmap: MemoryMapping,
+    guest_mem: GuestMemory,
 }
 
 impl Vcpu {
@@ -1012,7 +1013,21 @@ impl Vcpu {
         let run_mmap =
             MemoryMapping::from_fd(&vcpu, run_mmap_size).map_err(|_| Error::new(ENOSPC))?;
 
-        Ok(Vcpu { vcpu, run_mmap })
+        let guest_mem = vm.guest_mem.clone();
+
+        Ok(Vcpu {
+            vcpu,
+            run_mmap,
+            guest_mem,
+        })
+    }
+
+    /// Gets a reference to the guest memory owned by this VM of this VCPU.
+    ///
+    /// Note that `GuestMemory` does not include any device memory that may have been added after
+    /// this VM was constructed.
+    pub fn get_memory(&self) -> &GuestMemory {
+        &self.guest_mem
     }
 
     /// Sets the data received by an mmio or ioport read/in instruction.