Skip to content

Commit 2baf291

Browse files
committed
vmm: Coalesce around pm_sender
Signed-off-by: Tyler Fanelli <[email protected]>
1 parent c2b6211 commit 2baf291

File tree

3 files changed

+22
-75
lines changed

3 files changed

+22
-75
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
//! Enables pre-boot setup, instantiation and booting of a Firecracker VMM.
55
6-
#[cfg(feature = "tee")]
7-
use crate::vstate::MemProperties;
86
#[cfg(target_os = "macos")]
97
use crossbeam_channel::unbounded;
108

@@ -699,8 +697,6 @@ pub fn build_microvm(
699697
&guest_memory,
700698
payload_config.entry_addr,
701699
&exit_evt,
702-
#[cfg(feature = "tee")]
703-
io_sender,
704700
)
705701
.map_err(StartMicrovmError::Internal)?;
706702

@@ -762,8 +758,6 @@ pub fn build_microvm(
762758
mmio_device_manager,
763759
#[cfg(target_arch = "x86_64")]
764760
pio_device_manager,
765-
#[cfg(feature = "tee")]
766-
guest_memfd_vec: guest_memfd,
767761
};
768762

769763
#[cfg(not(feature = "tee"))]
@@ -1468,16 +1462,13 @@ fn create_vcpus_aarch64(
14681462
guest_mem: &GuestMemoryMmap,
14691463
entry_addr: GuestAddress,
14701464
exit_evt: &EventFd,
1471-
#[cfg(feature = "tee")] sender_io: Sender<MemProperties>,
14721465
) -> super::Result<Vec<Vcpu>> {
14731466
let mut vcpus = Vec::with_capacity(vcpu_config.vcpu_count as usize);
14741467
for cpu_index in 0..vcpu_config.vcpu_count {
14751468
let mut vcpu = Vcpu::new_aarch64(
14761469
cpu_index,
14771470
vm.fd(),
14781471
exit_evt.try_clone().map_err(Error::EventFd)?,
1479-
#[cfg(feature = "tee")]
1480-
sender_io.clone(),
14811472
)
14821473
.map_err(Error::Vcpu)?;
14831474

src/vmm/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ pub struct Vmm {
207207
mmio_device_manager: MMIODeviceManager,
208208
#[cfg(target_arch = "x86_64")]
209209
pio_device_manager: PortIODeviceManager,
210-
211-
#[cfg(feature = "tee")]
212-
pub guest_memfd_vec: Vec<RawFd>,
213210
}
214211

215212
impl Vmm {

src/vmm/src/linux/vstate.rs

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ use std::io;
1313

1414
use std::os::unix::io::RawFd;
1515

16-
#[cfg(feature = "tee")]
17-
use kvm_ioctls::VcpuExit::Unsupported;
18-
19-
use std::sync::Arc;
20-
use std::sync::Mutex;
21-
2216
use std::result;
2317
use std::sync::atomic::{fence, Ordering};
2418
#[cfg(not(test))]
@@ -45,14 +39,14 @@ use kvm_bindings::{
4539
KVM_CLOCK_TSC_STABLE, KVM_IRQCHIP_IOAPIC, KVM_IRQCHIP_PIC_MASTER, KVM_IRQCHIP_PIC_SLAVE,
4640
KVM_MAX_CPUID_ENTRIES,
4741
};
48-
#[cfg(feature = "tee")]
4942
use kvm_bindings::{
50-
kvm_create_guest_memfd, kvm_memory_attributes, kvm_userspace_memory_region2, KVM_API_VERSION,
51-
KVM_MEMORY_ATTRIBUTE_PRIVATE, KVM_MEMORY_EXIT_FLAG_PRIVATE, KVM_MEM_GUEST_MEMFD, KVM_EXIT_MEMORY_FAULT
43+
kvm_create_guest_memfd, kvm_memory_attributes, kvm_userspace_memory_region,
44+
kvm_userspace_memory_region2, KVM_API_VERSION, KVM_MEMORY_ATTRIBUTE_PRIVATE,
45+
KVM_MEM_GUEST_MEMFD,
5246
};
53-
#[cfg(not(feature = "tee"))]
54-
use kvm_bindings::{kvm_userspace_memory_region, KVM_API_VERSION};
55-
use kvm_ioctls::*;
47+
#[cfg(feature = "tee")]
48+
use kvm_bindings::{kvm_enable_cap, KVM_CAP_EXIT_HYPERCALL, KVM_MEMORY_EXIT_FLAG_PRIVATE};
49+
use kvm_ioctls::{Cap::*, *};
5650
use utils::eventfd::EventFd;
5751
use utils::signal::{register_signal_handler, sigrtmin, Killable};
5852
use utils::sm::StateMachine;
@@ -777,13 +771,6 @@ pub struct VcpuConfig {
777771
pub cpu_template: Option<CpuFeaturesTemplate>,
778772
}
779773

780-
#[cfg(feature = "tee")]
781-
pub struct MemProperties {
782-
pub addr: u64,
783-
pub size: u64,
784-
pub attributes: u32,
785-
}
786-
787774
// Using this for easier explicit type-casting to help IDEs interpret the code.
788775
type VcpuCell = Cell<Option<*mut Vcpu>>;
789776

@@ -806,10 +793,6 @@ pub struct Vcpu {
806793
#[cfg(target_arch = "aarch64")]
807794
mpidr: u64,
808795

809-
// The transmitting end of the events channel which will be given to the vcpu side
810-
#[cfg(feature = "tee")]
811-
sender_io: Sender<MemProperties>,
812-
813796
// The receiving end of events channel owned by the vcpu side.
814797
event_receiver: Receiver<VcpuEvent>,
815798
// The transmitting end of the events channel which will be given to the handler.
@@ -957,12 +940,7 @@ impl Vcpu {
957940
/// * `exit_evt` - An `EventFd` that will be written into when this vcpu exits.
958941
/// * `create_ts` - A timestamp used by the vcpu to calculate its lifetime.
959942
#[cfg(target_arch = "aarch64")]
960-
pub fn new_aarch64(
961-
id: u8,
962-
vm_fd: &VmFd,
963-
exit_evt: EventFd,
964-
#[cfg(feature = "tee")] sender_io: Sender<MemProperties>,
965-
) -> Result<Self> {
943+
pub fn new_aarch64(id: u8, vm_fd: &VmFd, exit_evt: EventFd) -> Result<Self> {
966944
let kvm_vcpu = vm_fd.create_vcpu(id as u64).map_err(Error::VcpuFd)?;
967945
let (event_sender, event_receiver) = unbounded();
968946
let (response_sender, response_receiver) = unbounded();
@@ -977,8 +955,6 @@ impl Vcpu {
977955
event_sender: Some(event_sender),
978956
response_receiver: Some(response_receiver),
979957
response_sender,
980-
#[cfg(feature = "tee")]
981-
sender_io,
982958
})
983959
}
984960

@@ -1270,6 +1246,17 @@ impl Vcpu {
12701246
self.io_bus.write(0, u64::from(addr), data);
12711247
Ok(VcpuEmulation::Handled)
12721248
}
1249+
#[cfg(feature = "tee")]
1250+
VcpuExit::MemoryFault { gpa, size, flags } => {
1251+
let private = (flags & (KVM_MEMORY_EXIT_FLAG_PRIVATE as u64)) != 0;
1252+
1253+
let mem_properties = MemoryProperties { gpa, size, private };
1254+
1255+
self.pm_sender.0.send(mem_properties).unwrap();
1256+
let _ = self.pm_sender.1.read().unwrap();
1257+
1258+
Ok(VcpuEmulation::Handled)
1259+
}
12731260
VcpuExit::MmioRead(addr, data) => {
12741261
if let Some(ref mmio_bus) = self.mmio_bus {
12751262
mmio_bus.read(0, addr, data);
@@ -1290,44 +1277,16 @@ impl Vcpu {
12901277
info!("Received KVM_EXIT_SHUTDOWN signal");
12911278
Ok(VcpuEmulation::Stopped)
12921279
}
1293-
#[cfg(feature = "tee")]
1294-
VcpuExit::MemoryFault { flags, gpa, size } => {
1295-
if flags & !KVM_MEMORY_EXIT_FLAG_PRIVATE as u64 != 0 {
1296-
error!("KVM_EXIT_MEMORY_FAULT: Unknown flag {}", flags);
1297-
Err(Error::VcpuUnhandledKvmExit)
1298-
} else {
1299-
// from private to shared
1300-
let mut attr = 0;
1301-
// from shared to private
1302-
if flags & KVM_MEMORY_EXIT_FLAG_PRIVATE as u64
1303-
== KVM_MEMORY_EXIT_FLAG_PRIVATE as u64
1304-
{
1305-
attr = KVM_MEMORY_ATTRIBUTE_PRIVATE;
1306-
};
1307-
1308-
let _ = self.sender_io.try_send(MemProperties {
1309-
addr: gpa,
1310-
size,
1311-
attributes: attr,
1312-
});
1313-
Ok(VcpuEmulation::Handled)
1314-
}
1315-
}
1316-
// Documentation specifices that when KVM exists with KVM_EXIT_MEMORY_FAULT,
1317-
// userspace should assume kvm_run.exit_reason is stale/undefined for error numbers
1318-
// different than EFAULT or EHWPOISON
1319-
#[cfg(feature = "tee")]
1320-
Unsupported(KVM_EXIT_MEMORY_FAULT) => Ok(VcpuEmulation::Handled),
1321-
VcpuExit::InternalError => {
1322-
error!("Received KVM_EXIT_INTERNAL_ERROR signal");
1323-
Err(Error::VcpuUnhandledKvmExit)
1324-
}
13251280
// Documentation specifies that below kvm exits are considered
13261281
// errors.
13271282
VcpuExit::FailEntry(reason, vcpu) => {
13281283
error!("Received KVM_EXIT_FAIL_ENTRY signal: reason={reason}, vcpu={vcpu}");
13291284
Err(Error::VcpuUnhandledKvmExit)
13301285
}
1286+
VcpuExit::InternalError => {
1287+
error!("Received KVM_EXIT_INTERNAL_ERROR signal");
1288+
Err(Error::VcpuUnhandledKvmExit)
1289+
}
13311290
r => {
13321291
// TODO: Are we sure we want to finish running a vcpu upon
13331292
// receiving a vm exit that is not necessarily an error?

0 commit comments

Comments
 (0)