From c017a764d7237432f805d3b3f8f1ce7f5fdae107 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Thu, 26 Jul 2018 13:48:56 -0700 Subject: [PATCH] kvm: plumb accessor for KVM_GET_MSR_INDEX_LIST Plumb in KVM_GET_MSR_INDEX_LIST to allow clients to figure out what MSRs are supported. BUG=b:111083877 TEST=cargo test -p kvm Change-Id: I69deba32a21fb9360f8cd53001d717338523fb71 Signed-off-by: Dmitry Torokhov Reviewed-on: https://chromium-review.googlesource.com/1152228 Reviewed-by: Zach Reizner --- kvm/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/kvm/src/lib.rs b/kvm/src/lib.rs index ea288eb702..2d858db9e5 100644 --- a/kvm/src/lib.rs +++ b/kvm/src/lib.rs @@ -160,6 +160,45 @@ impl Kvm { pub fn get_emulated_cpuid(&self) -> Result { self.get_cpuid(KVM_GET_EMULATED_CPUID()) } + + /// X86 specific call to get list of supported MSRS + /// + /// See the documentation for KVM_GET_MSR_INDEX_LIST. + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + pub fn get_msr_index_list(&self) -> Result> { + const MAX_KVM_MSR_ENTRIES: usize = 256; + + let vec_size_bytes = size_of::() + + MAX_KVM_MSR_ENTRIES * size_of::(); + let bytes: Vec = vec![0; vec_size_bytes]; + let msr_list: &mut kvm_msr_list = unsafe { + // We have ensured in new that there is enough space for the structure so this + // conversion is safe. + &mut *(bytes.as_ptr() as *mut kvm_msr_list) + }; + msr_list.nmsrs = MAX_KVM_MSR_ENTRIES as u32; + + 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 nmsrs, which is set to the allocated + // size (MAX_KVM_MSR_ENTRIES) above. + ioctl_with_mut_ref(self, KVM_GET_MSR_INDEX_LIST(), msr_list) + }; + if ret < 0 { + return errno_result(); + } + + // Mapping the unsized array to a slice is unsafe because the length isn't known. Using + // the length we originally allocated with eliminates the possibility of overflow. + let indices: &[u32] = unsafe { + if msr_list.nmsrs > MAX_KVM_MSR_ENTRIES as u32 { + msr_list.nmsrs = MAX_KVM_MSR_ENTRIES as u32; + } + msr_list.indices.as_slice(msr_list.nmsrs as usize) + }; + + Ok(indices.to_vec()) + } } impl AsRawFd for Kvm { @@ -1391,6 +1430,14 @@ mod tests { kvm.get_emulated_cpuid().unwrap(); } + #[test] + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn get_msr_index_list() { + let kvm = Kvm::new().unwrap(); + let msr_list = kvm.get_msr_index_list().unwrap(); + assert!(msr_list.len() >= 2); + } + #[test] fn add_memory() { let kvm = Kvm::new().unwrap();