Skip to content

Commit 7dedbf8

Browse files
committed
Share vm and vcpu fds
These descriptors are required for handling the KVM_FAULT_MEMORY vmexit. Although this vmexit is only present when the tee feature is enabled, changing for a reference does not seem the right thing to do by using feratures. Use reference and protect it to be able to share these descriptors. These changes prepare the code for handling KVM_EXIT_FAULT_MEMORY Signed-off-by: Matias Ezequiel Vara Larsen <[email protected]>
1 parent 15ac9fc commit 7dedbf8

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/vmm/src/builder.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,13 +1019,13 @@ fn attach_legacy_devices(
10191019
) -> std::result::Result<(), StartMicrovmError> {
10201020
if let Some(serial) = serial {
10211021
mmio_device_manager
1022-
.register_mmio_serial(vm.fd(), kernel_cmdline, serial)
1022+
.register_mmio_serial(&vm.fd.lock().unwrap(), kernel_cmdline, serial)
10231023
.map_err(Error::RegisterMMIODevice)
10241024
.map_err(StartMicrovmError::Internal)?;
10251025
}
10261026

10271027
mmio_device_manager
1028-
.register_mmio_rtc(vm.fd())
1028+
.register_mmio_rtc(&vm.fd.lock().unwrap())
10291029
.map_err(Error::RegisterMMIODevice)
10301030
.map_err(StartMicrovmError::Internal)?;
10311031

@@ -1110,12 +1110,12 @@ fn create_vcpus_aarch64(
11101110
for cpu_index in 0..vcpu_config.vcpu_count {
11111111
let mut vcpu = Vcpu::new_aarch64(
11121112
cpu_index,
1113-
vm.fd(),
1113+
&vm.fd.lock().unwrap(),
11141114
exit_evt.try_clone().map_err(Error::EventFd)?,
11151115
)
11161116
.map_err(Error::Vcpu)?;
11171117

1118-
vcpu.configure_aarch64(vm.fd(), guest_mem, entry_addr)
1118+
vcpu.configure_aarch64(&vm.fd.lock().unwrap(), guest_mem, entry_addr)
11191119
.map_err(Error::Vcpu)?;
11201120

11211121
vcpus.push(vcpu);
@@ -1177,9 +1177,12 @@ fn attach_mmio_device(
11771177
let _cmdline = &mut vmm.kernel_cmdline;
11781178

11791179
#[cfg(target_os = "linux")]
1180-
let (_mmio_base, _irq) =
1181-
vmm.mmio_device_manager
1182-
.register_mmio_device(vmm.vm.fd(), device, type_id, id)?;
1180+
let (_mmio_base, _irq) = vmm.mmio_device_manager.register_mmio_device(
1181+
&vmm.vm.fd.lock().unwrap(),
1182+
device,
1183+
type_id,
1184+
id,
1185+
)?;
11831186
#[cfg(target_os = "macos")]
11841187
let (_mmio_base, _irq) = vmm
11851188
.mmio_device_manager

src/vmm/src/linux/vstate.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use std::io;
1414
#[cfg(feature = "tee")]
1515
use std::os::unix::io::RawFd;
1616

17+
use std::sync::Arc;
18+
use std::sync::Mutex;
19+
1720
use std::result;
1821
use std::sync::atomic::{fence, Ordering};
1922
#[cfg(not(test))]
@@ -470,7 +473,7 @@ impl KvmContext {
470473

471474
/// A wrapper around creating and using a VM.
472475
pub struct Vm {
473-
fd: VmFd,
476+
pub fd: Arc<Mutex<VmFd>>,
474477
next_mem_slot: u32,
475478

476479
// X86 specific fields.
@@ -510,7 +513,7 @@ impl Vm {
510513
arch::x86_64::msr::supported_guest_msrs(kvm).map_err(Error::GuestMSRs)?;
511514

512515
Ok(Vm {
513-
fd: vm_fd,
516+
fd: Arc::new(Mutex::new(vm_fd)),
514517
next_mem_slot: 0,
515518
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
516519
supported_cpuid,
@@ -590,6 +593,8 @@ impl Vm {
590593

591594
let id: RawFd = self
592595
.fd
596+
.lock()
597+
.unwrap()
593598
.create_guest_memfd(gmem)
594599
.map_err(Error::CreateGuestMemfd)?;
595600

@@ -611,6 +616,8 @@ impl Vm {
611616
// are not overlapping.
612617
unsafe {
613618
self.fd
619+
.lock()
620+
.unwrap()
614621
.set_user_memory_region2(memory_region)
615622
.map_err(Error::SetUserMemoryRegion2)?;
616623
};
@@ -623,7 +630,7 @@ impl Vm {
623630
attributes: KVM_MEMORY_ATTRIBUTE_PRIVATE as u64,
624631
flags: 0,
625632
};
626-
self.fd.set_memory_attributes(attr).unwrap();
633+
self.fd.lock().unwrap().set_memory_attributes(attr).unwrap();
627634
}
628635
#[cfg(not(feature = "tee"))]
629636
{
@@ -638,6 +645,8 @@ impl Vm {
638645
// are not overlapping.
639646
unsafe {
640647
self.fd
648+
.lock()
649+
.unwrap()
641650
.set_user_memory_region(memory_region)
642651
.map_err(Error::SetUserMemoryRegion)?;
643652
};
@@ -727,7 +736,8 @@ impl Vm {
727736
#[cfg(target_arch = "aarch64")]
728737
pub fn setup_irqchip(&mut self, vcpu_count: u8) -> Result<()> {
729738
self.irqchip_handle = Some(
730-
arch::aarch64::gic::create_gic(&self.fd, vcpu_count.into()).map_err(Error::SetupGIC)?,
739+
arch::aarch64::gic::create_gic(&self.fd.lock().unwrap(), vcpu_count.into())
740+
.map_err(Error::SetupGIC)?,
731741
);
732742
Ok(())
733743
}
@@ -739,11 +749,6 @@ impl Vm {
739749
self.irqchip_handle.as_ref().unwrap()
740750
}
741751

742-
/// Gets a reference to the kvm file descriptor owned by this VM.
743-
pub fn fd(&self) -> &VmFd {
744-
&self.fd
745-
}
746-
747752
#[allow(unused)]
748753
#[cfg(target_arch = "x86_64")]
749754
/// Saves and returns the Kvm Vm state.
@@ -835,7 +840,7 @@ type VcpuCell = Cell<Option<*mut Vcpu>>;
835840

836841
/// A wrapper around creating and using a kvm-based VCPU.
837842
pub struct Vcpu {
838-
fd: VcpuFd,
843+
pub fd: VcpuFd,
839844
id: u8,
840845
mmio_bus: Option<devices::Bus>,
841846
#[allow(dead_code)]

0 commit comments

Comments
 (0)