summary refs log tree commit diff
path: root/kvm/src/lib.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2018-02-06 22:28:36 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-02-12 22:42:40 -0800
commitdadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd (patch)
tree038c55ce2ceac80b5a5af7518257a2c5b5497738 /kvm/src/lib.rs
parent1d4a70029c849b7aff178dd1e4a00feb7a5c483b (diff)
downloadcrosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar.gz
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar.bz2
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar.lz
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar.xz
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.tar.zst
crosvm-dadb7625ea382b0372ec8be9fcae8f0aa3f0b6fd.zip
allow plugin to query for KVM extensions
The guest may need to check for KVM extensions before blindly using
them.

TEST=cargo test --features plugin; cargo test -p kvm; ./build_test
BUG=chromium:800626

Change-Id: If87b928753cd71adeabac4fc7732c3fce7265834
Reviewed-on: https://chromium-review.googlesource.com/906008
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'kvm/src/lib.rs')
-rw-r--r--kvm/src/lib.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/kvm/src/lib.rs b/kvm/src/lib.rs
index 1c1ce2e..7eb290e 100644
--- a/kvm/src/lib.rs
+++ b/kvm/src/lib.rs
@@ -223,6 +223,18 @@ impl Vm {
         }
     }
 
+    /// Checks if a particular `Cap` is available.
+    ///
+    /// This is distinct from the `Kvm` version of this method because the some extensions depend on
+    /// the particular `Vm` existence. This method is encouraged by the kernel because it more
+    /// accurately reflects the usable capabilities.
+    pub fn check_extension(&self, c: Cap) -> bool {
+        // Safe because we know that our file is a KVM fd and that the extension is one of the ones
+        // defined by kernel.
+        unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) == 1 }
+    }
+
+
     /// Inserts the given `MemoryMapping` into the VM's address space at `guest_addr`.
     ///
     /// The slot that was assigned the device memory mapping is returned on success. The slot can be
@@ -1023,6 +1035,16 @@ mod tests {
     }
 
     #[test]
+    fn check_vm_extension() {
+        let kvm = Kvm::new().unwrap();
+        let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x1000)]).unwrap();
+        let vm = Vm::new(&kvm, gm).unwrap();
+        assert!(vm.check_extension(Cap::UserMemory));
+        // I assume nobody is testing this on s390
+        assert!(!vm.check_extension(Cap::S390UserSigp));
+    }
+
+    #[test]
     fn add_memory() {
         let kvm = Kvm::new().unwrap();
         let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x1000)]).unwrap();