summary refs log tree commit diff
diff options
context:
space:
mode:
authorDmitry Torokhov <dtor@chromium.org>2018-07-26 14:44:50 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-07-31 16:37:06 -0700
commit658357638c2fdd95525dd555c86078ecaed47458 (patch)
tree4ce39a88cb1c2d63415035e2add719dcd6c21ca9
parentc017a764d7237432f805d3b3f8f1ce7f5fdae107 (diff)
downloadcrosvm-658357638c2fdd95525dd555c86078ecaed47458.tar
crosvm-658357638c2fdd95525dd555c86078ecaed47458.tar.gz
crosvm-658357638c2fdd95525dd555c86078ecaed47458.tar.bz2
crosvm-658357638c2fdd95525dd555c86078ecaed47458.tar.lz
crosvm-658357638c2fdd95525dd555c86078ecaed47458.tar.xz
crosvm-658357638c2fdd95525dd555c86078ecaed47458.tar.zst
crosvm-658357638c2fdd95525dd555c86078ecaed47458.zip
plugin: allow retrieving list of supported MSRs
Add crossvm plugin API to allow fetching list of supported MSRs.

BUG=b:111083877
TEST=cargo test -p kvm; cargo test --features=plugin

Change-Id: I178c7bc33d606bef10422faac6bb9afb3fe0a014
Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1152229
Reviewed-by: Zach Reizner <zachr@chromium.org>
-rw-r--r--Cargo.lock10
-rw-r--r--crosvm_plugin/Cargo.toml2
-rw-r--r--crosvm_plugin/crosvm.h9
-rw-r--r--crosvm_plugin/src/lib.rs44
-rw-r--r--plugin_proto/Cargo.toml2
-rw-r--r--plugin_proto/protos/plugin.proto48
-rw-r--r--src/plugin/process.rs11
-rw-r--r--tests/plugin_msr_index_list.c49
-rw-r--r--tests/plugins.rs5
9 files changed, 152 insertions, 28 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9026742..5016afc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -53,7 +53,7 @@ dependencies = [
  "aarch64 0.1.0",
  "arch 0.1.0",
  "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "crosvm_plugin 0.15.0",
+ "crosvm_plugin 0.16.0",
  "data_model 0.1.0",
  "devices 0.1.0",
  "gpu_buffer 0.1.0",
@@ -65,7 +65,7 @@ dependencies = [
  "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
  "net_util 0.1.0",
  "p9 0.1.0",
- "plugin_proto 0.15.0",
+ "plugin_proto 0.16.0",
  "protobuf 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "qcow 0.1.0",
  "qcow_utils 0.1.0",
@@ -79,12 +79,12 @@ dependencies = [
 
 [[package]]
 name = "crosvm_plugin"
-version = "0.15.0"
+version = "0.16.0"
 dependencies = [
  "kvm 0.1.0",
  "kvm_sys 0.1.0",
  "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
- "plugin_proto 0.15.0",
+ "plugin_proto 0.16.0",
  "protobuf 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "sys_util 0.1.0",
 ]
@@ -232,7 +232,7 @@ dependencies = [
 
 [[package]]
 name = "plugin_proto"
-version = "0.15.0"
+version = "0.16.0"
 dependencies = [
  "cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "kvm_sys 0.1.0",
diff --git a/crosvm_plugin/Cargo.toml b/crosvm_plugin/Cargo.toml
index c203f4d..9406912 100644
--- a/crosvm_plugin/Cargo.toml
+++ b/crosvm_plugin/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "crosvm_plugin"
-version = "0.15.0"
+version = "0.16.0"
 authors = ["The Chromium OS Authors"]
 
 [lib]
diff --git a/crosvm_plugin/crosvm.h b/crosvm_plugin/crosvm.h
index 947a198..c2139b3 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 15
+#define CROSVM_API_MINOR 16
 #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 kvm for list of supported MSRs.
+ */
+int crosvm_get_msr_index_list(struct crosvm*, uint32_t __entry_count,
+                              uint32_t *__msr_indices,
+                              uint32_t *__out_count);
+
+/*
  * The network configuration for a crosvm instance.
  */
 struct crosvm_net_config {
diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs
index 24d99ca..c6f858a 100644
--- a/crosvm_plugin/src/lib.rs
+++ b/crosvm_plugin/src/lib.rs
@@ -139,6 +139,7 @@ enum Stat {
     CheckExtentsion,
     GetSupportedCpuid,
     GetEmulatedCpuid,
+    GetMsrIndexList,
     NetGetConfig,
     ReserveRange,
     SetIrq,
@@ -427,6 +428,30 @@ impl crosvm {
         Ok(emulated_cpuids.get_entries().len())
     }
 
+    fn get_msr_index_list(&mut self, msr_indices: &mut [u32])
+                           -> result::Result<usize, c_int> {
+        let mut r = MainRequest::new();
+        r.mut_get_msr_index_list();
+
+        let (response, _) = self.main_transaction(&r, &[])?;
+        if !response.has_get_msr_index_list() {
+            return Err(EPROTO);
+        }
+
+        let msr_list: &MainResponse_MsrListResponse = response.get_get_msr_index_list();
+        if msr_list.get_indices().len() > msr_indices.len() {
+            return Err(E2BIG);
+        }
+
+        for (proto_entry, kvm_entry) in
+            msr_list.get_indices().iter()
+                .zip(msr_indices.iter_mut()) {
+            *kvm_entry = *proto_entry;
+        }
+
+        Ok(msr_list.get_indices().len())
+    }
+
     fn reserve_range(&mut self, space: u32, start: u64, length: u64) -> result::Result<(), c_int> {
         let mut r = MainRequest::new();
         {
@@ -1126,6 +1151,25 @@ fn crosvm_get_emulated_cpuid(this: *mut crosvm,
 }
 
 #[no_mangle]
+pub unsafe extern "C"
+fn crosvm_get_msr_index_list(this: *mut crosvm,
+                             entry_count: u32,
+                             msr_indices: *mut u32,
+                             out_count: *mut u32)
+                             -> c_int {
+    let _u = STATS.record(Stat::GetMsrIndexList);
+    let this = &mut *this;
+    let msr_indices = from_raw_parts_mut(msr_indices, entry_count as usize);
+    let ret = this.get_msr_index_list(msr_indices);
+
+    if let Ok(num) = ret {
+        *out_count = num as u32;
+    }
+    to_crosvm_rc(ret)
+}
+
+
+#[no_mangle]
 pub unsafe extern "C" fn crosvm_net_get_config(self_: *mut crosvm,
                                                config: *mut crosvm_net_config)
                                                -> c_int {
diff --git a/plugin_proto/Cargo.toml b/plugin_proto/Cargo.toml
index 441cb2a..db898c7 100644
--- a/plugin_proto/Cargo.toml
+++ b/plugin_proto/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "plugin_proto"
-version = "0.15.0"
+version = "0.16.0"
 authors = ["The Chromium OS Authors"]
 build = "build.rs"
 
diff --git a/plugin_proto/protos/plugin.proto b/plugin_proto/protos/plugin.proto
index 699b478..005c314 100644
--- a/plugin_proto/protos/plugin.proto
+++ b/plugin_proto/protos/plugin.proto
@@ -75,6 +75,9 @@ message MainRequest {
     message CpuidRequest {
     }
 
+    message MsrListRequest {
+    }
+
     message GetNetConfig {}
 
     message ReserveRange {
@@ -162,16 +165,17 @@ message MainRequest {
         CheckExtension check_extension = 5;
         CpuidRequest get_supported_cpuid = 6;
         CpuidRequest get_emulated_cpuid = 7;
-        GetNetConfig get_net_config = 8;
-        ReserveRange reserve_range = 9;
-        SetIrq set_irq = 10;
-        SetIrqRouting set_irq_routing = 11;
-        GetState get_state = 12;
-        SetState set_state = 13;
-        SetIdentityMapAddr set_identity_map_addr = 14;
-        PauseVcpus pause_vcpus = 15;
-        GetVcpus get_vcpus = 16;
-        Start start = 17;
+        MsrListRequest get_msr_index_list = 8;
+        GetNetConfig get_net_config = 9;
+        ReserveRange reserve_range = 10;
+        SetIrq set_irq = 11;
+        SetIrqRouting set_irq_routing = 12;
+        GetState get_state = 13;
+        SetState set_state = 14;
+        SetIdentityMapAddr set_identity_map_addr = 15;
+        PauseVcpus pause_vcpus = 16;
+        GetVcpus get_vcpus = 17;
+        Start start = 18;
         // Method for a Memory type object for retrieving the dirty bitmap. Only valid if the memory
         // object was created with dirty_log set.
         MemoryDirtyLog dirty_log = 101;
@@ -193,6 +197,9 @@ message MainResponse {
     message CpuidResponse {
         repeated CpuidEntry entries = 1;
     }
+    message MsrListResponse {
+        repeated uint32 indices = 1;
+    }
 
     // GetNetConfig messages also return a file descriptor for the tap device.
     message GetNetConfig {
@@ -232,16 +239,17 @@ message MainResponse {
         CheckExtension check_extension = 6;
         CpuidResponse get_supported_cpuid = 7;
         CpuidResponse get_emulated_cpuid = 8;
-        GetNetConfig get_net_config = 9;
-        ReserveRange reserve_range = 10;
-        SetIrq set_irq = 11;
-        SetIrqRouting set_irq_routing = 12;
-        GetState get_state = 13;
-        SetState set_state = 14;
-        SetIdentityMapAddr set_identity_map_addr = 15;
-        PauseVcpus pause_vcpus = 16;
-        GetVcpus get_vcpus = 17;
-        Start start = 18;
+        MsrListResponse get_msr_index_list = 9;
+        GetNetConfig get_net_config = 10;
+        ReserveRange reserve_range = 11;
+        SetIrq set_irq = 12;
+        SetIrqRouting set_irq_routing = 13;
+        GetState get_state = 14;
+        SetState set_state = 15;
+        SetIdentityMapAddr set_identity_map_addr = 16;
+        PauseVcpus pause_vcpus = 17;
+        GetVcpus get_vcpus = 18;
+        Start start = 19;
         MemoryDirtyLog dirty_log = 101;
     }
 }
diff --git a/src/plugin/process.rs b/src/plugin/process.rs
index f816955..0e8a74f 100644
--- a/src/plugin/process.rs
+++ b/src/plugin/process.rs
@@ -634,6 +634,17 @@ impl Process {
                 }
                 Err(e) => Err(e),
             }
+        } else if request.has_get_msr_index_list() {
+            let msr_list_response = &mut response.mut_get_msr_index_list().indices;
+            match kvm.get_msr_index_list() {
+                Ok(indices) => {
+                    for entry in indices {
+                        msr_list_response.push(entry);
+                    }
+                    Ok(())
+                }
+                Err(e) => Err(e),
+            }
         } else {
             Err(SysError::new(ENOTTY))
         };
diff --git a/tests/plugin_msr_index_list.c b/tests/plugin_msr_index_list.c
new file mode 100644
index 0000000..0a940ae
--- /dev/null
+++ b/tests/plugin_msr_index_list.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2018 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "crosvm.h"
+
+int main(int argc, char** argv) {
+    struct crosvm *crosvm;
+    int ret = crosvm_connect(&crosvm);
+    if (ret) {
+        fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
+        return 1;
+    }
+
+    uint32_t msr_indices[256];
+    int n_entries;
+    ret = crosvm_get_msr_index_list(crosvm, 1, msr_indices, &n_entries);
+    if (ret >= 0) {
+        fprintf(stderr,
+                "expected crosvm_get_msr_index_list to fail with E2BIG\n");
+        return 1;
+    }
+
+
+    memset(msr_indices, 0, sizeof(msr_indices));
+    ret = crosvm_get_msr_index_list(crosvm, 256, msr_indices, &n_entries);
+    if (ret < 0) {
+        fprintf(stderr,
+                "unexpected failure of crosvm_get_msr_index_list: %d\n", ret);
+        return 1;
+    }
+
+    if (n_entries <= 1) {
+        fprintf(stderr,
+                "unexpected number of supported msr entries: %d\n",
+                n_entries);
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/tests/plugins.rs b/tests/plugins.rs
index c760875..7d2d8ff 100644
--- a/tests/plugins.rs
+++ b/tests/plugins.rs
@@ -242,6 +242,11 @@ fn test_supported_cpuid() {
 }
 
 #[test]
+fn test_msr_index_list() {
+    test_plugin(include_str!("plugin_msr_index_list.c"));
+}
+
+#[test]
 fn test_vm_state_manipulation() {
     test_plugin(include_str!("plugin_vm_state.c"));
 }