Skip to content

Commit 111f11a

Browse files
00xcJonathanWoollett-Light
authored andcommitted
Add support for userspace NMI injection (KVM_NMI)
Add support for the KVM_NMI ioctl, which allows a VMM to inject a non maskable interrupt into the guest. Signed-off-by: Carlos López <carlos.lopez@suse.com>
1 parent c612850 commit 111f11a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ reg_size as a public method.
1111
for SMI injection via `Vcpu::smi()` (`KVM_SMI` ioctl).
1212
- [[#241](https://github.com/rust-vmm/kvm-ioctls/pull/241)] Add support for
1313
userspace MSR handling.
14+
- [[#246](https://github.com/rust-vmm/kvm-ioctls/pull/246)] Add support for
15+
userspace NMI injection (`KVM_NMI` ioctl).
1416

1517
# v0.15.0
1618

src/ioctls/vcpu.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,32 @@ impl VcpuFd {
18231823
_ => Err(errno::Error::last()),
18241824
}
18251825
}
1826+
1827+
/// Queues an NMI on the thread's vcpu. Only usable if `KVM_CAP_USER_NMI`
1828+
/// is available.
1829+
///
1830+
/// See the documentation for `KVM_NMI`.
1831+
///
1832+
/// # Example
1833+
///
1834+
/// ```rust
1835+
/// # use kvm_ioctls::{Kvm, Cap};
1836+
/// let kvm = Kvm::new().unwrap();
1837+
/// let vm = kvm.create_vm().unwrap();
1838+
/// let vcpu = vm.create_vcpu(0).unwrap();
1839+
/// if kvm.check_extension(Cap::UserNmi) {
1840+
/// vcpu.nmi().unwrap();
1841+
/// }
1842+
/// ```
1843+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1844+
pub fn nmi(&self) -> Result<()> {
1845+
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
1846+
let ret = unsafe { ioctl(self, KVM_NMI()) };
1847+
match ret {
1848+
0 => Ok(()),
1849+
_ => Err(errno::Error::last()),
1850+
}
1851+
}
18261852
}
18271853

18281854
/// Helper function to create a new `VcpuFd`.

src/kvm_ioctls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
172172
target_arch = "s390"
173173
))]
174174
ioctl_iow_nr!(KVM_SET_MP_STATE, KVMIO, 0x99, kvm_mp_state);
175+
/* Available with KVM_CAP_USER_NMI */
176+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
177+
ioctl_io_nr!(KVM_NMI, KVMIO, 0x9a);
175178
/* Available with KVM_CAP_VCPU_EVENTS */
176179
#[cfg(any(
177180
target_arch = "x86",

0 commit comments

Comments
 (0)