Skip to content

Commit 6b4f272

Browse files
ShadowCurseJonathanWoollett-Light
authored andcommitted
feat: additional method to query kvm extension
Added method to query kvm extension using raw integer instead of an enum value. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent fadc154 commit 6b4f272

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Upcoming Release
22

3+
## Added
4+
- [[#230](https://github.com/rust-vmm/kvm-ioctls/pull/230)] Added
5+
`check_extension_raw` method to use raw integer values instead
6+
of `Cap` enum.
7+
38
# v0.14.0
49

510
## Added

src/ioctls/system.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,31 @@ impl Kvm {
176176
self.check_extension_int(Cap::DebugHwWps)
177177
}
178178

179+
/// Wrapper over `KVM_CHECK_EXTENSION`.
180+
///
181+
/// Returns 0 if the capability is not available and a positive integer otherwise.
182+
/// See the documentation for `KVM_CHECK_EXTENSION`.
183+
///
184+
/// # Arguments
185+
///
186+
/// * `c` - KVM capability to check in a form of a raw integer.
187+
///
188+
/// # Example
189+
///
190+
/// ```
191+
/// # use kvm_ioctls::Kvm;
192+
/// # use std::os::raw::c_ulong;
193+
/// use kvm_ioctls::Cap;
194+
///
195+
/// let kvm = Kvm::new().unwrap();
196+
/// assert!(kvm.check_extension_raw(Cap::MaxVcpuId as c_ulong) > 0);
197+
/// ```
198+
pub fn check_extension_raw(&self, c: c_ulong) -> i32 {
199+
// SAFETY: Safe because we know that our file is a KVM fd.
200+
// If `c` is not a known kernel extension, kernel will return 0.
201+
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c) }
202+
}
203+
179204
/// Wrapper over `KVM_CHECK_EXTENSION`.
180205
///
181206
/// Returns 0 if the capability is not available and a positive integer otherwise.
@@ -195,9 +220,7 @@ impl Kvm {
195220
/// assert!(kvm.check_extension_int(Cap::MaxVcpuId) > 0);
196221
/// ```
197222
pub fn check_extension_int(&self, c: Cap) -> i32 {
198-
// SAFETY: Safe because we know that our file is a KVM fd and that the extension is one of
199-
// the ones defined by kernel.
200-
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
223+
self.check_extension_raw(c as c_ulong)
201224
}
202225

203226
/// Checks if a particular `Cap` is available.
@@ -751,6 +774,13 @@ mod tests {
751774
assert!(kvm.check_extension(Cap::UserMemory));
752775
}
753776

777+
#[test]
778+
fn test_kvm_check_extension() {
779+
let kvm = Kvm::new().unwrap();
780+
// unsupported extension will return 0
781+
assert_eq!(kvm.check_extension_raw(696969), 0);
782+
}
783+
754784
#[test]
755785
#[cfg(target_arch = "aarch64")]
756786
fn test_get_host_ipa_limit() {

0 commit comments

Comments
 (0)