Skip to content

Commit f372a9d

Browse files
michael2012zandreeaflorescu
authored andcommitted
Enable KVM_SET/GET_VCPU_EVENTS ioctls for Aarch64.
KVM_GET_VCPU_EVENTS and KVM_SET_VCPU_EVENTS ioctls have been supported on Aarch64 from a recent kernel version. This patch enables them on Aarch64 and checked cap KVM_CAP_VCPU_EVENTS before calling them in test code to avoid error in old kernel. Change-Id: I9526b48948cf0cd5d63444a82b8b09970311a3bb Signed-off-by: Michael Zhao <[email protected]>
1 parent 1b5939c commit f372a9d

File tree

3 files changed

+65
-23
lines changed

3 files changed

+65
-23
lines changed

src/cap.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ pub enum Cap {
6262
XenHvm = KVM_CAP_XEN_HVM,
6363
AdjustClock = KVM_CAP_ADJUST_CLOCK,
6464
InternalErrorData = KVM_CAP_INTERNAL_ERROR_DATA,
65-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
65+
#[cfg(any(
66+
target_arch = "x86",
67+
target_arch = "x86_64",
68+
target_arch = "arm",
69+
target_arch = "aarch64"
70+
))]
6671
VcpuEvents = KVM_CAP_VCPU_EVENTS,
6772
S390Psw = KVM_CAP_S390_PSW,
6873
PpcSegstate = KVM_CAP_PPC_SEGSTATE,

src/ioctls/vcpu.rs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -852,14 +852,21 @@ impl VcpuFd {
852852
///
853853
/// ```rust
854854
/// # extern crate kvm_ioctls;
855-
/// # use kvm_ioctls::Kvm;
855+
/// # use kvm_ioctls::{Kvm, Cap};
856856
/// let kvm = Kvm::new().unwrap();
857-
/// let vm = kvm.create_vm().unwrap();
858-
/// let vcpu = vm.create_vcpu(0).unwrap();
859-
/// let vcpu_events = vcpu.get_vcpu_events().unwrap();
857+
/// if kvm.check_extension(Cap::VcpuEvents) {
858+
/// let vm = kvm.create_vm().unwrap();
859+
/// let vcpu = vm.create_vcpu(0).unwrap();
860+
/// let vcpu_events = vcpu.get_vcpu_events().unwrap();
861+
/// }
860862
/// ```
861863
///
862-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
864+
#[cfg(any(
865+
target_arch = "x86",
866+
target_arch = "x86_64",
867+
target_arch = "arm",
868+
target_arch = "aarch64"
869+
))]
863870
pub fn get_vcpu_events(&self) -> Result<kvm_vcpu_events> {
864871
let mut vcpu_events = Default::default();
865872
let ret = unsafe {
@@ -885,16 +892,24 @@ impl VcpuFd {
885892
///
886893
/// ```rust
887894
/// # extern crate kvm_ioctls;
888-
/// # use kvm_ioctls::Kvm;
895+
/// # use kvm_ioctls::{Kvm, Cap};
889896
/// let kvm = Kvm::new().unwrap();
890-
/// let vm = kvm.create_vm().unwrap();
891-
/// let vcpu = vm.create_vcpu(0).unwrap();
892-
/// let vcpu_events = Default::default();
893-
/// // Your `vcpu_events` manipulation here.
894-
/// vcpu.set_vcpu_events(&vcpu_events).unwrap();
897+
/// if kvm.check_extension(Cap::VcpuEvents) {
898+
/// let vm = kvm.create_vm().unwrap();
899+
/// let vcpu = vm.create_vcpu(0).unwrap();
900+
/// let vcpu_events = Default::default();
901+
/// // Your `vcpu_events` manipulation here.
902+
/// vcpu.set_vcpu_events(&vcpu_events).unwrap();
903+
/// }
895904
/// ```
896905
///
897-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
906+
#[cfg(any(
907+
target_arch = "x86",
908+
target_arch = "x86_64",
909+
target_arch = "arm",
910+
target_arch = "aarch64"
911+
))]
912+
898913
pub fn set_vcpu_events(&self, vcpu_events: &kvm_vcpu_events) -> Result<()> {
899914
let ret = unsafe {
900915
// Here we trust the kernel not to read past the end of the kvm_vcpu_events struct.
@@ -1180,7 +1195,12 @@ mod tests {
11801195
#[cfg(target_arch = "x86_64")]
11811196
use super::*;
11821197
use ioctls::system::Kvm;
1183-
#[cfg(target_arch = "x86_64")]
1198+
#[cfg(any(
1199+
target_arch = "x86",
1200+
target_arch = "x86_64",
1201+
target_arch = "arm",
1202+
target_arch = "aarch64"
1203+
))]
11841204
use Cap;
11851205

11861206
// Helper function for memory mapping `size` bytes of anonymous memory.
@@ -1391,16 +1411,23 @@ mod tests {
13911411
assert_eq!(debugregs, other_debugregs);
13921412
}
13931413

1394-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1414+
#[cfg(any(
1415+
target_arch = "x86",
1416+
target_arch = "x86_64",
1417+
target_arch = "arm",
1418+
target_arch = "aarch64"
1419+
))]
13951420
#[test]
13961421
fn vcpu_events_test() {
13971422
let kvm = Kvm::new().unwrap();
1398-
let vm = kvm.create_vm().unwrap();
1399-
let vcpu = vm.create_vcpu(0).unwrap();
1400-
let vcpu_events = vcpu.get_vcpu_events().unwrap();
1401-
vcpu.set_vcpu_events(&vcpu_events).unwrap();
1402-
let other_vcpu_events = vcpu.get_vcpu_events().unwrap();
1403-
assert_eq!(vcpu_events, other_vcpu_events);
1423+
if kvm.check_extension(Cap::VcpuEvents) {
1424+
let vm = kvm.create_vm().unwrap();
1425+
let vcpu = vm.create_vcpu(0).unwrap();
1426+
let vcpu_events = vcpu.get_vcpu_events().unwrap();
1427+
vcpu.set_vcpu_events(&vcpu_events).unwrap();
1428+
let other_vcpu_events = vcpu.get_vcpu_events().unwrap();
1429+
assert_eq!(vcpu_events, other_vcpu_events);
1430+
}
14041431
}
14051432

14061433
#[cfg(target_arch = "x86_64")]

src/kvm_ioctls.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,20 @@ ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
150150
))]
151151
ioctl_iow_nr!(KVM_SET_MP_STATE, KVMIO, 0x99, kvm_mp_state);
152152
/* Available with KVM_CAP_VCPU_EVENTS */
153-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
153+
#[cfg(any(
154+
target_arch = "x86",
155+
target_arch = "x86_64",
156+
target_arch = "arm",
157+
target_arch = "aarch64"
158+
))]
154159
ioctl_ior_nr!(KVM_GET_VCPU_EVENTS, KVMIO, 0x9f, kvm_vcpu_events);
155160
/* Available with KVM_CAP_VCPU_EVENTS */
156-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
161+
#[cfg(any(
162+
target_arch = "x86",
163+
target_arch = "x86_64",
164+
target_arch = "arm",
165+
target_arch = "aarch64"
166+
))]
157167
ioctl_iow_nr!(KVM_SET_VCPU_EVENTS, KVMIO, 0xa0, kvm_vcpu_events);
158168
/* Available with KVM_CAP_DEBUGREGS */
159169
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]

0 commit comments

Comments
 (0)