summary refs log tree commit diff
diff options
context:
space:
mode:
authorDmitry Torokhov <dtor@chromium.org>2018-08-27 11:34:20 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-08-31 05:22:52 -0700
commitcaa2e5a4ac45c5e107b6802db99c158083321bc4 (patch)
tree2338efb6819d29bba1b3553656c40d40fa8d18d2
parent9dec40e2420d5faf170fea328b331f9e695cf7d9 (diff)
downloadcrosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar.gz
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar.bz2
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar.lz
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar.xz
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.tar.zst
crosvm-caa2e5a4ac45c5e107b6802db99c158083321bc4.zip
plugin: do not fail crosvm_vcpu_get_msrs() if we failed to fetch some
KVM_GET_MSRS may return less MSRs that were requested; do not fail but
instead let callers to know how many were fetched.

BUG=None
TEST=cargo test --features plugin

Change-Id: Ie14a3d38b66bfe34f5279543bea9c6c78423527e
Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1192232
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
-rw-r--r--crosvm_plugin/crosvm.h3
-rw-r--r--crosvm_plugin/src/lib.rs16
-rw-r--r--tests/plugins.rs7
3 files changed, 19 insertions, 7 deletions
diff --git a/crosvm_plugin/crosvm.h b/crosvm_plugin/crosvm.h
index c2139b3..39c2453 100644
--- a/crosvm_plugin/crosvm.h
+++ b/crosvm_plugin/crosvm.h
@@ -509,7 +509,8 @@ int crosvm_vcpu_set_xcrs(struct crosvm_vcpu*, const struct kvm_xcrs*);
 
 /* Gets the MSRs of the vcpu indicated by the index field of each entry. */
 int crosvm_vcpu_get_msrs(struct crosvm_vcpu*, uint32_t __msr_count,
-                         struct kvm_msr_entry *__msr_entries);
+                         struct kvm_msr_entry *__msr_entries,
+                         uint32_t *__out_count);
 /* Sets the MSRs of the vcpu indicated by the index field of each entry. */
 int crosvm_vcpu_set_msrs(struct crosvm_vcpu*, uint32_t __msr_count,
                          const struct kvm_msr_entry *__msr_entries);
diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs
index 6567ee8..06cb367 100644
--- a/crosvm_plugin/src/lib.rs
+++ b/crosvm_plugin/src/lib.rs
@@ -995,7 +995,9 @@ impl crosvm_vcpu {
         Ok(())
     }
 
-    fn get_msrs(&mut self, msr_entries: &mut [kvm_msr_entry]) -> result::Result<(), c_int> {
+    fn get_msrs(&mut self, msr_entries: &mut [kvm_msr_entry], msr_count: &mut usize)
+                -> result::Result<(), c_int> {
+        *msr_count = 0;
         let mut r = VcpuRequest::new();
         {
             let entry_indices: &mut Vec<u32> = r.mut_get_msrs().mut_entry_indices();
@@ -1008,8 +1010,9 @@ impl crosvm_vcpu {
             return Err(EPROTO);
         }
         let get_msrs: &VcpuResponse_GetMsrs = response.get_get_msrs();
-        if get_msrs.get_entry_data().len() != msr_entries.len() {
-            return Err(EPROTO);
+        *msr_count = get_msrs.get_entry_data().len();
+        if *msr_count > msr_entries.len() {
+            return Err(E2BIG);
         }
         for (&msr_data, msr_entry) in
             get_msrs
@@ -1464,12 +1467,15 @@ pub unsafe extern "C" fn crosvm_vcpu_set_xcrs(this: *mut crosvm_vcpu,
 #[no_mangle]
 pub unsafe extern "C" fn crosvm_vcpu_get_msrs(this: *mut crosvm_vcpu,
                                               msr_count: u32,
-                                              msr_entries: *mut kvm_msr_entry)
+                                              msr_entries: *mut kvm_msr_entry,
+                                              out_count: *mut u32)
                                               -> c_int {
     let _u = STATS.record(Stat::VcpuGetMsrs);
     let this = &mut *this;
     let msr_entries = from_raw_parts_mut(msr_entries, msr_count as usize);
-    let ret = this.get_msrs(msr_entries);
+    let mut count: usize = 0;
+    let ret = this.get_msrs(msr_entries, &mut count);
+    *out_count = count as u32;
     to_crosvm_rc(ret)
 }
 
diff --git a/tests/plugins.rs b/tests/plugins.rs
index 7d2d8ff..987b033 100644
--- a/tests/plugins.rs
+++ b/tests/plugins.rs
@@ -411,6 +411,7 @@ fn test_msrs() {
             #define KILL_ADDRESS 0x3000
 
             int g_kill_evt;
+            uint32_t g_msr2_count;
             struct kvm_msr_entry g_msr2;
 
             int setup_vm(struct crosvm *crosvm, void *mem) {
@@ -445,7 +446,7 @@ fn test_msrs() {
                 {
                     uint64_t dummy = 1;
                     g_msr2.index = MSR2_INDEX;
-                    crosvm_vcpu_get_msrs(vcpu, 1, &g_msr2);
+                    crosvm_vcpu_get_msrs(vcpu, 1, &g_msr2, &g_msr2_count);
                     write(g_kill_evt, &dummy, sizeof(dummy));
                     return 1;
                 }
@@ -458,6 +459,10 @@ fn test_msrs() {
                     fprintf(stderr, "msr1 has unexpected value: 0x%x\n", msr1_data);
                     return 1;
                 }
+                if (g_msr2_count != 1) {
+                    fprintf(stderr, "incorrect number of returned MSRSs: %d\n", g_msr2_count);
+                    return 1;
+                }
                 if (g_msr2.data != MSR2_DATA) {
                     fprintf(stderr, "msr2 has unexpected value: 0x%x\n", g_msr2.data);
                     return 1;