Skip to content

Commit

Permalink
Implement 'check_extension()' for Vm
Browse files Browse the repository at this point in the history
'check_extension()' was defined in Kvm to perform KVM_CHECK_EXTENSION
ioctl only, but it is also possible in Vm.

Signed-off-by: Michael Zhao <michael.zhao@arm.com>
  • Loading branch information
michael2012z authored and alxiord committed Jul 8, 2020
1 parent 53a2cec commit d8f78a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 91.3,
"coverage_score": 91.4,
"exclude_path": "",
"crate_features": ""
}
42 changes: 42 additions & 0 deletions src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::os::raw::c_void;
use std::os::raw::{c_int, c_ulong};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};

use cap::Cap;
use ioctls::device::new_device;
use ioctls::device::DeviceFd;
use ioctls::vcpu::new_vcpu;
Expand Down Expand Up @@ -1216,6 +1217,40 @@ impl VmFd {
pub fn run_size(&self) -> usize {
self.run_size
}

/// Wrapper over `KVM_CHECK_EXTENSION`.
///
/// Returns 0 if the capability is not available and a positive integer otherwise.
fn check_extension_int(&self, c: Cap) -> i32 {
// Safe because we know that our file is a VM fd and that the extension is one of the ones
// defined by kernel.
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
}

/// Checks if a particular `Cap` is available.
///
/// Returns true if the capability is supported and false otherwise.
/// See the documentation for `KVM_CHECK_EXTENSION`.
///
/// # Arguments
///
/// * `c` - VM capability to check.
///
/// # Example
///
/// ```
/// # use kvm_ioctls::Kvm;
/// use kvm_ioctls::Cap;
///
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// // Check if `KVM_CAP_MP_STATE` is supported.
/// assert!(vm.check_extension(Cap::MpState));
/// ```
///
pub fn check_extension(&self, c: Cap) -> bool {
self.check_extension_int(c) > 0
}
}

/// Helper function to create a new `VmFd`.
Expand Down Expand Up @@ -1751,4 +1786,11 @@ mod tests {
let irq_routing = kvm_irq_routing::default();
assert!(vm.set_gsi_routing(&irq_routing).is_ok());
}

#[test]
fn test_check_extension() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
assert!(vm.check_extension(Cap::MpState));
}
}

0 comments on commit d8f78a1

Please sign in to comment.