summary refs log tree commit diff
path: root/crosvm_plugin
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 /crosvm_plugin
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 'crosvm_plugin')
-rw-r--r--crosvm_plugin/crosvm.h9
-rw-r--r--crosvm_plugin/src/lib.rs50
2 files changed, 58 insertions, 1 deletions
diff --git a/crosvm_plugin/crosvm.h b/crosvm_plugin/crosvm.h
index 63763f1..ef7759d 100644
--- a/crosvm_plugin/crosvm.h
+++ b/crosvm_plugin/crosvm.h
@@ -47,7 +47,7 @@ extern "C" {
  * do not indicate anything about what version of crosvm is running.
  */
 #define CROSVM_API_MAJOR 0
-#define CROSVM_API_MINOR 19
+#define CROSVM_API_MINOR 20
 #define CROSVM_API_PATCH 0
 
 enum crosvm_address_space {
@@ -130,6 +130,13 @@ int crosvm_get_emulated_cpuid(struct crosvm*, uint32_t __entry_count,
                               uint32_t *__out_count);
 
 /*
+ * Queries x86 hyper-v cpuid features which are emulated by kvm.
+ */
+int crosvm_get_hyperv_cpuid(struct crosvm_vcpu*, uint32_t __entry_count,
+                            struct kvm_cpuid_entry2 *__cpuid_entries,
+                            uint32_t *__out_count);
+
+/*
  * Queries kvm for list of supported MSRs.
  */
 int crosvm_get_msr_index_list(struct crosvm*, uint32_t __entry_count,
diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs
index eb30e4b..13b3d9f 100644
--- a/crosvm_plugin/src/lib.rs
+++ b/crosvm_plugin/src/lib.rs
@@ -168,6 +168,7 @@ pub enum Stat {
     CheckExtentsion,
     GetSupportedCpuid,
     GetEmulatedCpuid,
+    GetHypervCpuid,
     GetMsrIndexList,
     NetGetConfig,
     ReserveRange,
@@ -1202,6 +1203,39 @@ impl crosvm_vcpu {
         Ok(())
     }
 
+    fn get_hyperv_cpuid(
+        &mut self,
+        cpuid_entries: &mut [kvm_cpuid_entry2],
+        cpuid_count: &mut usize,
+    ) -> result::Result<(), c_int> {
+        *cpuid_count = 0;
+
+        let mut r = VcpuRequest::new();
+        r.mut_get_hyperv_cpuid();
+
+        let response = self.vcpu_transaction(&r)?;
+        if !response.has_get_hyperv_cpuid() {
+            return Err(EPROTO);
+        }
+
+        let hyperv_cpuids: &VcpuResponse_CpuidResponse = response.get_get_hyperv_cpuid();
+
+        *cpuid_count = hyperv_cpuids.get_entries().len();
+        if *cpuid_count > cpuid_entries.len() {
+            return Err(E2BIG);
+        }
+
+        for (proto_entry, kvm_entry) in hyperv_cpuids
+            .get_entries()
+            .iter()
+            .zip(cpuid_entries.iter_mut())
+        {
+            *kvm_entry = cpuid_proto_to_kvm(proto_entry);
+        }
+
+        Ok(())
+    }
+
     fn get_msrs(
         &mut self,
         msr_entries: &mut [kvm_msr_entry],
@@ -1804,6 +1838,22 @@ pub unsafe extern "C" fn crosvm_vcpu_set_xcrs(
 }
 
 #[no_mangle]
+pub unsafe extern "C" fn crosvm_get_hyperv_cpuid(
+    this: *mut crosvm_vcpu,
+    entry_count: u32,
+    cpuid_entries: *mut kvm_cpuid_entry2,
+    out_count: *mut u32,
+) -> c_int {
+    let _u = record(Stat::GetHypervCpuid);
+    let this = &mut *this;
+    let cpuid_entries = from_raw_parts_mut(cpuid_entries, entry_count as usize);
+    let mut cpuid_count: usize = 0;
+    let ret = this.get_hyperv_cpuid(cpuid_entries, &mut cpuid_count);
+    *out_count = cpuid_count as u32;
+    to_crosvm_rc(ret)
+}
+
+#[no_mangle]
 pub unsafe extern "C" fn crosvm_vcpu_get_msrs(
     this: *mut crosvm_vcpu,
     msr_count: u32,