Skip to content

Commit 001403b

Browse files
michael2012zalxiord
authored andcommitted
Add unit test for KVM_CREATE_IRQCHIP on ARM.
On ARM, KVM_CREATE_IRQCHIP is a bit more complex than on X86. KVM_CREATE_IRQCHIP only creates VGICv2. It usually succeed with a GICv2 hardware. But if the hardware is GICv3, it's probable that GICv2 can't be emulated, so KVM_CREATE_IRQCHIP would fail. An issue was created for the problem: #79 To avoid hardware dependency, in the test we'd better check the ability of creating VGICv2 in advance, and assert the result of KVM_CREATE_IRQCHIP correspondingly. Change-Id: Id17339dde28dbb4abd68bf09fe7f8f0d88c14edb Signed-off-by: Michael Zhao <[email protected]>
1 parent b7eadd8 commit 001403b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/ioctls/vm.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,25 @@ impl VmFd {
146146
///
147147
/// ```rust
148148
/// # extern crate kvm_ioctls;
149+
/// # extern crate kvm_bindings;
149150
/// # use kvm_ioctls::Kvm;
150151
/// let kvm = Kvm::new().unwrap();
151152
/// let vm = kvm.create_vm().unwrap();
152153
///
153154
/// #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
154155
/// vm.create_irq_chip().unwrap();
156+
/// #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] {
157+
/// use kvm_bindings::{kvm_create_device,
158+
/// kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2, KVM_CREATE_DEVICE_TEST};
159+
/// let mut gic_device = kvm_bindings::kvm_create_device {
160+
/// type_: kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2,
161+
/// fd: 0,
162+
/// flags: KVM_CREATE_DEVICE_TEST,
163+
/// };
164+
/// if vm.create_device(&mut gic_device).is_ok() {
165+
/// vm.create_irq_chip().unwrap();
166+
/// }
167+
/// }
155168
/// ```
156169
///
157170
#[cfg(any(
@@ -1189,6 +1202,28 @@ mod tests {
11891202
unsafe { assert_eq!(irqchip.chip.pic, other_irqchip.chip.pic) };
11901203
}
11911204

1205+
#[test]
1206+
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
1207+
fn test_irq_chip() {
1208+
use Cap;
1209+
1210+
let kvm = Kvm::new().unwrap();
1211+
assert!(kvm.check_extension(Cap::Irqchip));
1212+
1213+
let vm = kvm.create_vm().unwrap();
1214+
1215+
// On ARM/arm64, a GICv2 is created. It's better to check ahead whether GICv2
1216+
// can be emulated or not.
1217+
let mut gic_device = kvm_bindings::kvm_create_device {
1218+
type_: kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2,
1219+
fd: 0,
1220+
flags: KVM_CREATE_DEVICE_TEST,
1221+
};
1222+
1223+
let vgic_v2_supported = vm.create_device(&mut gic_device).is_ok();
1224+
assert_eq!(vm.create_irq_chip().is_ok(), vgic_v2_supported);
1225+
}
1226+
11921227
#[test]
11931228
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
11941229
fn test_pit2() {

0 commit comments

Comments
 (0)