summary refs log tree commit diff
path: root/hypervisor/src/kvm/mod.rs
diff options
context:
space:
mode:
authorSteven Richman <srichman@google.com>2020-05-01 17:30:24 -0700
committerCommit Bot <commit-bot@chromium.org>2020-05-09 06:23:33 +0000
commite1f8d9187d3f16e31d0c1304030ad412cf43b2bf (patch)
tree45b8606c4d9fb31a4d4d335d8e0ea3bb31a6d9c2 /hypervisor/src/kvm/mod.rs
parenta7469062a2b25541cfe887f55441284553659ea0 (diff)
downloadcrosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar.gz
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar.bz2
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar.lz
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar.xz
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.tar.zst
crosvm-e1f8d9187d3f16e31d0c1304030ad412cf43b2bf.zip
hypervisor: add Vm/Vcpu traits and Kvm impls
Add arch-agnostic traits Vm and Vcpu.  Add arch-specific traits
HypervisorXXX, VmXXX, VcpuXXX, with impls for KVM.

BUG=chromium:1077058
TEST=added test for functions and structs interacting with the traits

Change-Id: I809f42f32a558c7835831c90e24fca82ce7744ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2176562
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Diffstat (limited to 'hypervisor/src/kvm/mod.rs')
-rw-r--r--hypervisor/src/kvm/mod.rs89
1 files changed, 80 insertions, 9 deletions
diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs
index 7058921..5d78b00 100644
--- a/hypervisor/src/kvm/mod.rs
+++ b/hypervisor/src/kvm/mod.rs
@@ -2,13 +2,23 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-use super::{CpuId, Hypervisor, HypervisorCap};
-use libc::{open, O_CLOEXEC, O_RDWR};
+#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
+mod aarch64;
+#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+mod x86_64;
+
+use std::ops::{Deref, DerefMut};
 use std::os::raw::c_char;
+
+use libc::{open, O_CLOEXEC, O_RDWR};
+
 use sys_util::{
-    errno_result, AsRawDescriptor, FromRawDescriptor, RawDescriptor, Result, SafeDescriptor,
+    errno_result, AsRawDescriptor, FromRawDescriptor, GuestMemory, RawDescriptor, Result,
+    SafeDescriptor,
 };
 
+use crate::{Hypervisor, HypervisorCap, RunnableVcpu, Vcpu, VcpuExit, Vm};
+
 pub struct Kvm {
     kvm: SafeDescriptor,
 }
@@ -39,15 +49,76 @@ impl Hypervisor for Kvm {
     fn check_capability(&self, _cap: &HypervisorCap) -> bool {
         unimplemented!("check_capability for Kvm is not yet implemented");
     }
+}
+
+/// A wrapper around creating and using a KVM VM.
+pub struct KvmVm {
+    guest_mem: GuestMemory,
+}
+
+impl KvmVm {
+    /// Constructs a new `KvmVm` using the given `Kvm` instance.
+    pub fn new(_kvm: &Kvm, guest_mem: GuestMemory) -> Result<KvmVm> {
+        Ok(KvmVm { guest_mem })
+    }
+
+    fn create_kvm_vcpu(&self, _id: usize) -> Result<KvmVcpu> {
+        Ok(KvmVcpu {})
+    }
+}
+
+impl Vm for KvmVm {
+    fn get_guest_mem(&self) -> &GuestMemory {
+        &self.guest_mem
+    }
+}
+
+/// A wrapper around creating and using a KVM Vcpu.
+pub struct KvmVcpu {}
+
+impl Vcpu for KvmVcpu {
+    type Runnable = RunnableKvmVcpu;
+
+    fn to_runnable(self) -> Result<Self::Runnable> {
+        Ok(RunnableKvmVcpu {
+            vcpu: self,
+            phantom: Default::default(),
+        })
+    }
 
-    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
-    fn get_supported_cpuid(&self) -> Result<CpuId> {
-        unimplemented!("get_supported_cpuid for Kvm is not yet implemented");
+    fn request_interrupt_window(&self) -> Result<()> {
+        Ok(())
     }
+}
+
+/// A KvmVcpu that has a thread and can be run.
+pub struct RunnableKvmVcpu {
+    vcpu: KvmVcpu,
+
+    // vcpus must stay on the same thread once they start.
+    // Add the PhantomData pointer to ensure RunnableKvmVcpu is not `Send`.
+    phantom: std::marker::PhantomData<*mut u8>,
+}
+
+impl RunnableVcpu for RunnableKvmVcpu {
+    type Vcpu = KvmVcpu;
+
+    fn run(&self) -> Result<VcpuExit> {
+        Ok(VcpuExit::Unknown)
+    }
+}
+
+impl Deref for RunnableKvmVcpu {
+    type Target = <Self as RunnableVcpu>::Vcpu;
+
+    fn deref(&self) -> &Self::Target {
+        &self.vcpu
+    }
+}
 
-    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
-    fn get_emulated_cpuid(&self) -> Result<CpuId> {
-        unimplemented!("get_emulated_cpuid for Kvm is not yet implemented");
+impl DerefMut for RunnableKvmVcpu {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.vcpu
     }
 }