@@ -11,6 +11,7 @@ use std::os::raw::c_void;
11
11
use std:: os:: raw:: { c_int, c_ulong} ;
12
12
use std:: os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ;
13
13
14
+ use cap:: Cap ;
14
15
use ioctls:: device:: new_device;
15
16
use ioctls:: device:: DeviceFd ;
16
17
use ioctls:: vcpu:: new_vcpu;
@@ -1216,6 +1217,40 @@ impl VmFd {
1216
1217
pub fn run_size ( & self ) -> usize {
1217
1218
self . run_size
1218
1219
}
1220
+
1221
+ /// Wrapper over `KVM_CHECK_EXTENSION`.
1222
+ ///
1223
+ /// Returns 0 if the capability is not available and a positive integer otherwise.
1224
+ fn check_extension_int ( & self , c : Cap ) -> i32 {
1225
+ // Safe because we know that our file is a VM fd and that the extension is one of the ones
1226
+ // defined by kernel.
1227
+ unsafe { ioctl_with_val ( self , KVM_CHECK_EXTENSION ( ) , c as c_ulong ) }
1228
+ }
1229
+
1230
+ /// Checks if a particular `Cap` is available.
1231
+ ///
1232
+ /// Returns true if the capability is supported and false otherwise.
1233
+ /// See the documentation for `KVM_CHECK_EXTENSION`.
1234
+ ///
1235
+ /// # Arguments
1236
+ ///
1237
+ /// * `c` - VM capability to check.
1238
+ ///
1239
+ /// # Example
1240
+ ///
1241
+ /// ```
1242
+ /// # use kvm_ioctls::Kvm;
1243
+ /// use kvm_ioctls::Cap;
1244
+ ///
1245
+ /// let kvm = Kvm::new().unwrap();
1246
+ /// let vm = kvm.create_vm().unwrap();
1247
+ /// // Check if `KVM_CAP_MP_STATE` is supported.
1248
+ /// assert!(vm.check_extension(Cap::MpState));
1249
+ /// ```
1250
+ ///
1251
+ pub fn check_extension ( & self , c : Cap ) -> bool {
1252
+ self . check_extension_int ( c) > 0
1253
+ }
1219
1254
}
1220
1255
1221
1256
/// Helper function to create a new `VmFd`.
@@ -1751,4 +1786,11 @@ mod tests {
1751
1786
let irq_routing = kvm_irq_routing:: default ( ) ;
1752
1787
assert ! ( vm. set_gsi_routing( & irq_routing) . is_ok( ) ) ;
1753
1788
}
1789
+
1790
+ #[ test]
1791
+ fn test_check_extension ( ) {
1792
+ let kvm = Kvm :: new ( ) . unwrap ( ) ;
1793
+ let vm = kvm. create_vm ( ) . unwrap ( ) ;
1794
+ assert ! ( vm. check_extension( Cap :: MpState ) ) ;
1795
+ }
1754
1796
}
0 commit comments