From 334c9a352b7fe5551e6446682c3719028b05790a Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 16 Jun 2020 16:45:34 +0200 Subject: [PATCH] ioctls: vcpu: Add KVM_KVMCLOCK_CTRL support The KVM_KVMCLOCK_CTRL lets the guest know that it has been paused, which will prevent from detecting soft lockups due to pausing and resuming operations. Signed-off-by: Sebastien Boeuf --- coverage_config_x86_64.json | 2 +- src/ioctls/vcpu.rs | 20 ++++++++++++++++++++ src/kvm_ioctls.rs | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index eba81b31..debc8fb4 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1,5 +1,5 @@ { - "coverage_score": 91.3, + "coverage_score": 91.2, "exclude_path": "", "crate_features": "" } diff --git a/src/ioctls/vcpu.rs b/src/ioctls/vcpu.rs index 5db1d383..dd1146f7 100644 --- a/src/ioctls/vcpu.rs +++ b/src/ioctls/vcpu.rs @@ -1049,6 +1049,22 @@ impl VcpuFd { Ok(reg_value) } + /// Notify the guest about the vCPU being paused. + /// + /// See the documentation for `KVM_KVMCLOCK_CTRL` in the + /// [KVM API documentation](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt). + /// + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + pub fn kvmclock_ctrl(&self) -> Result<()> { + // Safe because we know that our file is a KVM fd and that the request + // is one of the ones defined by kernel. + let ret = unsafe { ioctl(self, KVM_KVMCLOCK_CTRL()) }; + if ret != 0 { + return Err(errno::Error::last()); + } + Ok(()) + } + /// Triggers the running of the current virtual CPU returning an exit reason. /// /// See documentation for `KVM_RUN`. @@ -1877,6 +1893,10 @@ mod tests { badf_errno ); assert_eq!(faulty_vcpu_fd.run().unwrap_err().errno(), badf_errno); + assert_eq!( + faulty_vcpu_fd.kvmclock_ctrl().unwrap_err().errno(), + badf_errno + ); } #[test] diff --git a/src/kvm_ioctls.rs b/src/kvm_ioctls.rs index f8a25a95..21d16b96 100644 --- a/src/kvm_ioctls.rs +++ b/src/kvm_ioctls.rs @@ -190,6 +190,9 @@ ioctl_ior_nr!(KVM_GET_XCRS, KVMIO, 0xa6, kvm_xcrs); /* Available with KVM_CAP_XCRS */ #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] ioctl_iow_nr!(KVM_SET_XCRS, KVMIO, 0xa7, kvm_xcrs); +/* Available with KVM_CAP_KVMCLOCK_CTRL */ +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +ioctl_io_nr!(KVM_KVMCLOCK_CTRL, KVMIO, 0xad); /* Available with KVM_CAP_ENABLE_CAP */ #[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]