From f7797d15b492046df49670c166b175fa28e7ed8b Mon Sep 17 00:00:00 2001 From: Zach Reizner Date: Mon, 28 Jan 2019 19:56:36 -0800 Subject: 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 Tested-by: kokoro Tested-by: Zach Reizner Reviewed-by: Dylan Reid --- kvm/src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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. -- cgit 1.4.1