Skip to content

Commit f8bde30

Browse files
MatiasVaratylerfanelli
authored andcommitted
Add support for KVM_EXIT_MEMORY_FAULT
The KVM_EXIT_MEMORY_FAULT vmexit is triggered when guest wants to switch a region of memory from private to shared and viceversa. To support this when tee is enabled, add an extra thread named sender_io that gets the parameters from the vcpu thread and triggers the set_memory_properties(). The vcpu fd is owned only by this thread. Signed-off-by: Matias Ezequiel Vara Larsen <[email protected]>
1 parent 97e6bd4 commit f8bde30

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

src/vmm/src/builder.rs

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

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

@@ -713,6 +715,8 @@ pub fn build_microvm(
713715
&guest_memory,
714716
payload_config.entry_addr,
715717
&exit_evt,
718+
#[cfg(feature = "tee")]
719+
io_sender,
716720
)
717721
.map_err(StartMicrovmError::Internal)?;
718722

@@ -1490,13 +1494,16 @@ fn create_vcpus_aarch64(
14901494
guest_mem: &GuestMemoryMmap,
14911495
entry_addr: GuestAddress,
14921496
exit_evt: &EventFd,
1497+
#[cfg(feature = "tee")] sender_io: Sender<MemProperties>,
14931498
) -> super::Result<Vec<Vcpu>> {
14941499
let mut vcpus = Vec::with_capacity(vcpu_config.vcpu_count as usize);
14951500
for cpu_index in 0..vcpu_config.vcpu_count {
14961501
let mut vcpu = Vcpu::new_aarch64(
14971502
cpu_index,
14981503
vm.fd(),
14991504
exit_evt.try_clone().map_err(Error::EventFd)?,
1505+
#[cfg(feature = "tee")]
1506+
sender_io.clone(),
15001507
)
15011508
.map_err(Error::Vcpu)?;
15021509

src/vmm/src/linux/vstate.rs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ 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+
1622
use std::result;
1723
use std::sync::atomic::{fence, Ordering};
1824
#[cfg(not(test))]
@@ -39,18 +45,18 @@ use kvm_bindings::{
3945
KVM_CLOCK_TSC_STABLE, KVM_IRQCHIP_IOAPIC, KVM_IRQCHIP_PIC_MASTER, KVM_IRQCHIP_PIC_SLAVE,
4046
KVM_MAX_CPUID_ENTRIES,
4147
};
42-
use kvm_bindings::{
43-
kvm_userspace_memory_region, KVM_API_VERSION, KVM_SYSTEM_EVENT_RESET, KVM_SYSTEM_EVENT_SHUTDOWN,
44-
};
4548
#[cfg(feature = "tee")]
4649
use kvm_bindings::{
47-
kvm_create_guest_memfd, kvm_memory_attributes, kvm_userspace_memory_region,
48-
kvm_userspace_memory_region2, KVM_API_VERSION, KVM_MEMORY_ATTRIBUTE_PRIVATE,
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,
4952
KVM_MEM_GUEST_MEMFD,
5053
};
51-
#[cfg(feature = "tee")]
52-
use kvm_bindings::{kvm_enable_cap, KVM_CAP_EXIT_HYPERCALL};
53-
use kvm_ioctls::{Cap::*, *};
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::*;
5460
use utils::eventfd::EventFd;
5561
use utils::signal::{register_signal_handler, sigrtmin, Killable};
5662
use utils::sm::StateMachine;
@@ -768,6 +774,13 @@ pub struct VcpuConfig {
768774
pub cpu_template: Option<CpuFeaturesTemplate>,
769775
}
770776

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

@@ -790,6 +803,10 @@ pub struct Vcpu {
790803
#[cfg(target_arch = "aarch64")]
791804
mpidr: u64,
792805

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+
793810
// The receiving end of events channel owned by the vcpu side.
794811
event_receiver: Receiver<VcpuEvent>,
795812
// The transmitting end of the events channel which will be given to the handler.
@@ -937,7 +954,12 @@ impl Vcpu {
937954
/// * `exit_evt` - An `EventFd` that will be written into when this vcpu exits.
938955
/// * `create_ts` - A timestamp used by the vcpu to calculate its lifetime.
939956
#[cfg(target_arch = "aarch64")]
940-
pub fn new_aarch64(id: u8, vm_fd: &VmFd, exit_evt: EventFd) -> Result<Self> {
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> {
941963
let kvm_vcpu = vm_fd.create_vcpu(id as u64).map_err(Error::VcpuFd)?;
942964
let (event_sender, event_receiver) = unbounded();
943965
let (response_sender, response_receiver) = unbounded();
@@ -952,6 +974,8 @@ impl Vcpu {
952974
event_sender: Some(event_sender),
953975
response_receiver: Some(response_receiver),
954976
response_sender,
977+
#[cfg(feature = "tee")]
978+
sender_io,
955979
})
956980
}
957981

@@ -1260,6 +1284,38 @@ impl Vcpu {
12601284
info!("Received KVM_EXIT_SHUTDOWN signal");
12611285
Ok(VcpuEmulation::Stopped)
12621286
}
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+
}
12631319
// Documentation specifies that below kvm exits are considered
12641320
// errors.
12651321
VcpuExit::FailEntry(reason, vcpu) => {

0 commit comments

Comments
 (0)