diff options
author | Dmitry Torokhov <dtor@chromium.org> | 2018-08-22 10:05:16 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-08-29 16:09:27 -0700 |
commit | 641b28b6922cda802747f929eb169af037d2361d (patch) | |
tree | dceafc86f15ee00ff20476d42c06220d4c56dc0e /crosvm_plugin | |
parent | b47c6925fb7a82696af2dae16dbde06863d584c4 (diff) | |
download | crosvm-641b28b6922cda802747f929eb169af037d2361d.tar crosvm-641b28b6922cda802747f929eb169af037d2361d.tar.gz crosvm-641b28b6922cda802747f929eb169af037d2361d.tar.bz2 crosvm-641b28b6922cda802747f929eb169af037d2361d.tar.lz crosvm-641b28b6922cda802747f929eb169af037d2361d.tar.xz crosvm-641b28b6922cda802747f929eb169af037d2361d.tar.zst crosvm-641b28b6922cda802747f929eb169af037d2361d.zip |
plugin: return number of supported MSRS even if buffer is too small
Userspace is interested in number of supported MSRs even if supplied buffer is too small, as then it can intelligently [re]allocate the buffer and repeat the call instead of doing this blindly. So let's always populate 'out_count'in crosvm_get_msr_index_list() call. Obviously if there is hard error we will not be able to supply a meaningful number, so 0 will be returned, but in case of E2BIG error we can return the real number. Also let's do the same for get_supported_cpuids() and get_emulated_cpuids() calls. BUG=b:111083877 TEST=cargo test -p kvm; cargo test --features=plugin Change-Id: I37a8d719103fac44597b88ddecb6b8af2dd54ac8 Signed-off-by: Dmitry Torokhov <dtor@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1185293 Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'crosvm_plugin')
-rw-r--r-- | crosvm_plugin/src/lib.rs | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs index c6f858a..6567ee8 100644 --- a/crosvm_plugin/src/lib.rs +++ b/crosvm_plugin/src/lib.rs @@ -380,8 +380,11 @@ impl crosvm { Ok(response.get_check_extension().has_extension) } - fn get_supported_cpuid(&mut self, cpuid_entries: &mut [kvm_cpuid_entry2]) - -> result::Result<usize, c_int> { + fn get_supported_cpuid(&mut self, cpuid_entries: &mut [kvm_cpuid_entry2], + cpuid_count: &mut usize) + -> result::Result<(), c_int> { + *cpuid_count = 0; + let mut r = MainRequest::new(); r.mut_get_supported_cpuid(); @@ -391,7 +394,9 @@ impl crosvm { } let supported_cpuids: &MainResponse_CpuidResponse = response.get_get_supported_cpuid(); - if supported_cpuids.get_entries().len() > cpuid_entries.len() { + + *cpuid_count = supported_cpuids.get_entries().len(); + if *cpuid_count > cpuid_entries.len() { return Err(E2BIG); } @@ -401,11 +406,14 @@ impl crosvm { *kvm_entry = cpuid_proto_to_kvm(proto_entry); } - Ok(supported_cpuids.get_entries().len()) + Ok(()) } - fn get_emulated_cpuid(&mut self, cpuid_entries: &mut [kvm_cpuid_entry2]) - -> result::Result<usize, c_int> { + fn get_emulated_cpuid(&mut self, cpuid_entries: &mut [kvm_cpuid_entry2], + cpuid_count: &mut usize) + -> result::Result<(), c_int> { + *cpuid_count = 0; + let mut r = MainRequest::new(); r.mut_get_emulated_cpuid(); @@ -415,7 +423,9 @@ impl crosvm { } let emulated_cpuids: &MainResponse_CpuidResponse = response.get_get_emulated_cpuid(); - if emulated_cpuids.get_entries().len() > cpuid_entries.len() { + + *cpuid_count = emulated_cpuids.get_entries().len(); + if *cpuid_count > cpuid_entries.len() { return Err(E2BIG); } @@ -425,11 +435,13 @@ impl crosvm { *kvm_entry = cpuid_proto_to_kvm(proto_entry); } - Ok(emulated_cpuids.get_entries().len()) + Ok(()) } - fn get_msr_index_list(&mut self, msr_indices: &mut [u32]) - -> result::Result<usize, c_int> { + fn get_msr_index_list(&mut self, msr_indices: &mut [u32], msr_count: &mut usize) + -> result::Result<(), c_int> { + *msr_count = 0; + let mut r = MainRequest::new(); r.mut_get_msr_index_list(); @@ -439,7 +451,9 @@ impl crosvm { } let msr_list: &MainResponse_MsrListResponse = response.get_get_msr_index_list(); - if msr_list.get_indices().len() > msr_indices.len() { + + *msr_count = msr_list.get_indices().len(); + if *msr_count > msr_indices.len() { return Err(E2BIG); } @@ -449,7 +463,7 @@ impl crosvm { *kvm_entry = *proto_entry; } - Ok(msr_list.get_indices().len()) + Ok(()) } fn reserve_range(&mut self, space: u32, start: u64, length: u64) -> result::Result<(), c_int> { @@ -1124,11 +1138,9 @@ fn crosvm_get_supported_cpuid(this: *mut crosvm, let _u = STATS.record(Stat::GetSupportedCpuid); let this = &mut *this; let cpuid_entries = from_raw_parts_mut(cpuid_entries, entry_count as usize); - let ret = this.get_supported_cpuid(cpuid_entries); - - if let Ok(num) = ret { - *out_count = num as u32; - } + let mut cpuid_count: usize = 0; + let ret = this.get_supported_cpuid(cpuid_entries, &mut cpuid_count); + *out_count = cpuid_count as u32; to_crosvm_rc(ret) } @@ -1142,11 +1154,9 @@ fn crosvm_get_emulated_cpuid(this: *mut crosvm, let _u = STATS.record(Stat::GetEmulatedCpuid); let this = &mut *this; let cpuid_entries = from_raw_parts_mut(cpuid_entries, entry_count as usize); - let ret = this.get_emulated_cpuid(cpuid_entries); - - if let Ok(num) = ret { - *out_count = num as u32; - } + let mut cpuid_count: usize = 0; + let ret = this.get_emulated_cpuid(cpuid_entries, &mut cpuid_count); + *out_count = cpuid_count as u32; to_crosvm_rc(ret) } @@ -1160,11 +1170,9 @@ fn crosvm_get_msr_index_list(this: *mut crosvm, 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; - } + let mut msr_count: usize = 0; + let ret = this.get_msr_index_list(msr_indices, &mut msr_count); + *out_count = msr_count as u32; to_crosvm_rc(ret) } |