summary refs log tree commit diff
path: root/kvm/src
diff options
context:
space:
mode:
authorMatt Delco <delco@chromium.org>2020-02-04 16:01:55 -0800
committerCommit Bot <commit-bot@chromium.org>2020-02-20 02:12:01 +0000
commitc469580e6c9b83172ba58e8305c6e5c11acfe186 (patch)
treedb725dfd751f0955b9d1788772f4e3bb208aa9af /kvm/src
parentce03437d567717bcee61ff464bc45f0a98dd3eb9 (diff)
downloadcrosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar.gz
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar.bz2
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar.lz
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar.xz
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.tar.zst
crosvm-c469580e6c9b83172ba58e8305c6e5c11acfe186.zip
crosvm: support kvm's hyper-v cpuid ioctl
Kvm can emulate the hyper-v paravirt interface.  Newer versions of kvm
can advertise the features they support via an ioctl() that reports the
cpuid leafs for this interface.  This change adds some support for the
ioctl() and plumbs it through the plugin interface so that plugins can
determine the level of support available in kvm.

BUG=b:144746965
TEST=Ran build_test on kernel that supports the ioctl.  Added temporary
code to print the cpuid leafs and verified that the output is as
expected.  Instrumented failure as expected from older kernels and
verified that results still passed.

Change-Id: I6cd7dade1793e4edb52b331d5b960685541f7ba3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2037919
Tested-by: Matt Delco <delco@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Matt Delco <delco@chromium.org>
Diffstat (limited to 'kvm/src')
-rw-r--r--kvm/src/lib.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/kvm/src/lib.rs b/kvm/src/lib.rs
index c5da845..a522478 100644
--- a/kvm/src/lib.rs
+++ b/kvm/src/lib.rs
@@ -1502,6 +1502,24 @@ impl Vcpu {
         Ok(())
     }
 
+    /// X86 specific call to get the system emulated hyper-v CPUID values
+    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+    pub fn get_hyperv_cpuid(&self) -> Result<CpuId> {
+        const MAX_KVM_CPUID_ENTRIES: usize = 256;
+        let mut cpuid = CpuId::new(MAX_KVM_CPUID_ENTRIES);
+
+        let ret = unsafe {
+            // ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
+            // allocated for the struct. The limit is read from nent, which is set to the allocated
+            // size(MAX_KVM_CPUID_ENTRIES) above.
+            ioctl_with_mut_ptr(self, KVM_GET_SUPPORTED_HV_CPUID(), cpuid.as_mut_ptr())
+        };
+        if ret < 0 {
+            return errno_result();
+        }
+        Ok(cpuid)
+    }
+
     /// X86 specific call to get the state of the "Local Advanced Programmable Interrupt Controller".
     ///
     /// See the documentation for KVM_GET_LAPIC.
@@ -2333,6 +2351,23 @@ mod tests {
 
     #[test]
     #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+    fn get_hyperv_cpuid() {
+        let kvm = Kvm::new().unwrap();
+        let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x10000)]).unwrap();
+        let vm = Vm::new(&kvm, gm).unwrap();
+        let vcpu = Vcpu::new(0, &kvm, &vm).unwrap();
+        let cpuid = vcpu.get_hyperv_cpuid();
+        // Older kernels don't support so tolerate this kind of failure.
+        match cpuid {
+            Ok(_) => {}
+            Err(e) => {
+                assert_eq!(e.errno(), EINVAL);
+            }
+        }
+    }
+
+    #[test]
+    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
     fn mp_state() {
         let kvm = Kvm::new().unwrap();
         let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x10000)]).unwrap();