Skip to content

Commit 955bc4a

Browse files
committed
vmm: prerequisites for accessing KVM_RUN in the vCPU loop
These are the prerequisites for the upcoming (quick and dirty) solution to the problem that we might miss some events.
1 parent e3b88fe commit 955bc4a

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

hypervisor/src/cpu.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//
1212

13+
#[cfg(feature = "kvm")]
14+
use std::os::fd::RawFd;
1315
#[cfg(target_arch = "aarch64")]
1416
use std::sync::Arc;
1517

@@ -602,4 +604,11 @@ pub trait Vcpu: Send + Sync {
602604
/// Trigger NMI interrupt
603605
///
604606
fn nmi(&self) -> Result<()>;
607+
/// Returns the underlying vCPU FD of KVM.
608+
///
609+
/// # SAFETY
610+
/// This is safe if we do only use this to map the KVM_RUN structure for the
611+
/// signal handler and only use it from there.
612+
#[cfg(feature = "kvm")]
613+
unsafe fn get_kvm_vcpu_raw_fd(&self) -> RawFd;
605614
}

hypervisor/src/kvm/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::collections::HashMap;
1616
use std::fs::File;
1717
#[cfg(target_arch = "x86_64")]
1818
use std::os::unix::io::AsRawFd;
19-
#[cfg(feature = "tdx")]
19+
#[cfg(any(feature = "tdx", feature = "kvm"))]
2020
use std::os::unix::io::RawFd;
2121
use std::result;
2222
#[cfg(target_arch = "x86_64")]
@@ -2769,6 +2769,13 @@ impl cpu::Vcpu for KvmVcpu {
27692769
self.fd.lock().unwrap().set_kvm_immediate_exit(exit.into());
27702770
}
27712771

2772+
#[cfg(feature = "kvm")]
2773+
unsafe fn get_kvm_vcpu_raw_fd(&self) -> RawFd {
2774+
let kvm_vcpu = self.fd.lock().unwrap();
2775+
let kvm_vcpu = &*kvm_vcpu;
2776+
kvm_vcpu.as_raw_fd()
2777+
}
2778+
27722779
///
27732780
/// Returns the details about TDX exit reason
27742781
///

hypervisor/src/mshv/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub mod x86_64;
4343
pub mod aarch64;
4444
#[cfg(target_arch = "x86_64")]
4545
use std::fs::File;
46+
#[cfg(feature = "kvm")]
47+
use std::os::fd::RawFd;
4648
use std::os::unix::io::AsRawFd;
4749
#[cfg(target_arch = "aarch64")]
4850
use std::sync::Mutex;
@@ -1576,6 +1578,11 @@ impl cpu::Vcpu for MshvVcpu {
15761578

15771579
Ok(())
15781580
}
1581+
1582+
#[cfg(feature = "kvm")]
1583+
unsafe fn get_kvm_vcpu_raw_fd(&self) -> RawFd {
1584+
todo!()
1585+
}
15791586
}
15801587

15811588
impl MshvVcpu {

vmm/src/cpu.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use std::collections::BTreeMap;
1616
use std::io::Write;
1717
#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))]
1818
use std::mem::size_of;
19+
#[cfg(feature = "kvm")]
20+
use std::os::fd::RawFd;
1921
use std::os::unix::thread::JoinHandleExt;
2022
use std::sync::atomic::{AtomicBool, Ordering};
2123
use std::sync::{Arc, Barrier, Mutex};
@@ -487,6 +489,12 @@ impl Vcpu {
487489
.map_err(Error::VcpuSetGicrBaseAddr)?;
488490
Ok(())
489491
}
492+
493+
#[cfg(feature = "kvm")]
494+
pub fn get_kvm_vcpu_raw_fd(&self) -> RawFd {
495+
// SAFETY: We hold onto the contract.
496+
unsafe { self.vcpu.get_kvm_vcpu_raw_fd() }
497+
}
490498
}
491499

492500
impl Pausable for Vcpu {}

0 commit comments

Comments
 (0)