Skip to content

Commit 81f4653

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

File tree

5 files changed

+24
-95
lines changed

5 files changed

+24
-95
lines changed

Cargo.lock

Lines changed: 5 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libkrun/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
16011601
})
16021602
.unwrap();
16031603
}
1604-
1604+
16051605
#[cfg(feature = "tee")]
16061606
let guest_mem = _vmm.lock().unwrap().guest_memory().clone();
16071607

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

@@ -715,8 +713,6 @@ pub fn build_microvm(
715713
&guest_memory,
716714
payload_config.entry_addr,
717715
&exit_evt,
718-
#[cfg(feature = "tee")]
719-
io_sender,
720716
)
721717
.map_err(StartMicrovmError::Internal)?;
722718

@@ -778,8 +774,6 @@ pub fn build_microvm(
778774
mmio_device_manager,
779775
#[cfg(target_arch = "x86_64")]
780776
pio_device_manager,
781-
#[cfg(feature = "tee")]
782-
guest_memfd_vec: guest_memfd,
783777
};
784778

785779
#[cfg(not(feature = "tee"))]
@@ -1494,16 +1488,13 @@ fn create_vcpus_aarch64(
14941488
guest_mem: &GuestMemoryMmap,
14951489
entry_addr: GuestAddress,
14961490
exit_evt: &EventFd,
1497-
#[cfg(feature = "tee")] sender_io: Sender<MemProperties>,
14981491
) -> super::Result<Vec<Vcpu>> {
14991492
let mut vcpus = Vec::with_capacity(vcpu_config.vcpu_count as usize);
15001493
for cpu_index in 0..vcpu_config.vcpu_count {
15011494
let mut vcpu = Vcpu::new_aarch64(
15021495
cpu_index,
15031496
vm.fd(),
15041497
exit_evt.try_clone().map_err(Error::EventFd)?,
1505-
#[cfg(feature = "tee")]
1506-
sender_io.clone(),
15071498
)
15081499
.map_err(Error::Vcpu)?;
15091500

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: 18 additions & 67 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,18 +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_EXIT_MEMORY_FAULT, KVM_MEMORY_ATTRIBUTE_PRIVATE, KVM_MEMORY_EXIT_FLAG_PRIVATE,
52-
KVM_MEM_GUEST_MEMFD,
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, KVM_SYSTEM_EVENT_RESET, KVM_SYSTEM_EVENT_SHUTDOWN,
5346
};
54-
#[cfg(not(feature = "tee"))]
55-
use kvm_bindings::{kvm_userspace_memory_region, KVM_API_VERSION};
56-
use kvm_bindings::{
57-
kvm_userspace_memory_region, KVM_API_VERSION, KVM_SYSTEM_EVENT_RESET, KVM_SYSTEM_EVENT_SHUTDOWN,
58-
};
59-
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::*, *};
6050
use utils::eventfd::EventFd;
6151
use utils::signal::{register_signal_handler, sigrtmin, Killable};
6252
use utils::sm::StateMachine;
@@ -774,13 +764,6 @@ pub struct VcpuConfig {
774764
pub cpu_template: Option<CpuFeaturesTemplate>,
775765
}
776766

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

@@ -803,10 +786,6 @@ pub struct Vcpu {
803786
#[cfg(target_arch = "aarch64")]
804787
mpidr: u64,
805788

806-
// The transmitting end of the events channel which will be given to the vcpu side
807-
#[cfg(feature = "tee")]
808-
sender_io: Sender<MemProperties>,
809-
810789
// The receiving end of events channel owned by the vcpu side.
811790
event_receiver: Receiver<VcpuEvent>,
812791
// The transmitting end of the events channel which will be given to the handler.
@@ -954,12 +933,7 @@ impl Vcpu {
954933
/// * `exit_evt` - An `EventFd` that will be written into when this vcpu exits.
955934
/// * `create_ts` - A timestamp used by the vcpu to calculate its lifetime.
956935
#[cfg(target_arch = "aarch64")]
957-
pub fn new_aarch64(
958-
id: u8,
959-
vm_fd: &VmFd,
960-
exit_evt: EventFd,
961-
#[cfg(feature = "tee")] sender_io: Sender<MemProperties>,
962-
) -> Result<Self> {
936+
pub fn new_aarch64(id: u8, vm_fd: &VmFd, exit_evt: EventFd) -> Result<Self> {
963937
let kvm_vcpu = vm_fd.create_vcpu(id as u64).map_err(Error::VcpuFd)?;
964938
let (event_sender, event_receiver) = unbounded();
965939
let (response_sender, response_receiver) = unbounded();
@@ -974,8 +948,6 @@ impl Vcpu {
974948
event_sender: Some(event_sender),
975949
response_receiver: Some(response_receiver),
976950
response_sender,
977-
#[cfg(feature = "tee")]
978-
sender_io,
979951
})
980952
}
981953

@@ -1264,6 +1236,17 @@ impl Vcpu {
12641236
self.io_bus.write(0, u64::from(addr), data);
12651237
Ok(VcpuEmulation::Handled)
12661238
}
1239+
#[cfg(feature = "tee")]
1240+
VcpuExit::MemoryFault { gpa, size, flags } => {
1241+
let private = (flags & (KVM_MEMORY_EXIT_FLAG_PRIVATE as u64)) != 0;
1242+
1243+
let mem_properties = MemoryProperties { gpa, size, private };
1244+
1245+
self.pm_sender.0.send(mem_properties).unwrap();
1246+
let _ = self.pm_sender.1.read().unwrap();
1247+
1248+
Ok(VcpuEmulation::Handled)
1249+
}
12671250
VcpuExit::MmioRead(addr, data) => {
12681251
if let Some(ref mmio_bus) = self.mmio_bus {
12691252
mmio_bus.read(0, addr, data);
@@ -1284,38 +1267,6 @@ impl Vcpu {
12841267
info!("Received KVM_EXIT_SHUTDOWN signal");
12851268
Ok(VcpuEmulation::Stopped)
12861269
}
1287-
#[cfg(feature = "tee")]
1288-
VcpuExit::MemoryFault { flags, gpa, size } => {
1289-
if flags & !KVM_MEMORY_EXIT_FLAG_PRIVATE as u64 != 0 {
1290-
error!("KVM_EXIT_MEMORY_FAULT: Unknown flag {}", flags);
1291-
Err(Error::VcpuUnhandledKvmExit)
1292-
} else {
1293-
// from private to shared
1294-
let mut attr = 0;
1295-
// from shared to private
1296-
if flags & KVM_MEMORY_EXIT_FLAG_PRIVATE as u64
1297-
== KVM_MEMORY_EXIT_FLAG_PRIVATE as u64
1298-
{
1299-
attr = KVM_MEMORY_ATTRIBUTE_PRIVATE;
1300-
};
1301-
1302-
let _ = self.sender_io.try_send(MemProperties {
1303-
addr: gpa,
1304-
size,
1305-
attributes: attr,
1306-
});
1307-
Ok(VcpuEmulation::Handled)
1308-
}
1309-
}
1310-
// Documentation specifices that when KVM exists with KVM_EXIT_MEMORY_FAULT,
1311-
// userspace should assume kvm_run.exit_reason is stale/undefined for error numbers
1312-
// different than EFAULT or EHWPOISON
1313-
#[cfg(feature = "tee")]
1314-
Unsupported(KVM_EXIT_MEMORY_FAULT) => Ok(VcpuEmulation::Handled),
1315-
VcpuExit::InternalError => {
1316-
error!("Received KVM_EXIT_INTERNAL_ERROR signal");
1317-
Err(Error::VcpuUnhandledKvmExit)
1318-
}
13191270
// Documentation specifies that below kvm exits are considered
13201271
// errors.
13211272
VcpuExit::FailEntry(reason, vcpu) => {

0 commit comments

Comments
 (0)