Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/hyperlight_host/src/hypervisor/hyperlight_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ use std::sync::{Arc, Mutex};

use log::LevelFilter;
use tracing::{Span, instrument};
#[cfg(feature = "trace_guest")]
use tracing_opentelemetry::OpenTelemetrySpanExt;

#[cfg(gdb)]
use super::gdb::arch::VcpuStopReasonError;
Expand Down Expand Up @@ -623,13 +621,13 @@ impl HyperlightVm {
{
Ok(VmExit::Cancelled())
} else {
#[cfg(feature = "trace_guest")]
tc.setup_guest_trace(Span::current().context());

// ==== KILL() TIMING POINT 3: Before calling run() ====
// If kill() is called and ran to completion BEFORE this line executes:
// - Will still do a VM entry, but signals will be sent until VM exits
let result = self.vm.run_vcpu();
let result = self.vm.run_vcpu(
#[cfg(feature = "trace_guest")]
&mut tc,
);

// End current host trace by closing the current span that captures traces
// happening when a guest exits and re-enters.
Expand Down
12 changes: 11 additions & 1 deletion src/hyperlight_host/src/hypervisor/virtual_machine/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use kvm_bindings::{kvm_debugregs, kvm_fpu, kvm_regs, kvm_sregs, kvm_userspace_me
use kvm_ioctls::Cap::UserMemory;
use kvm_ioctls::{Kvm, VcpuExit, VcpuFd, VmFd};
use tracing::{Span, instrument};
#[cfg(feature = "trace_guest")]
use tracing_opentelemetry::OpenTelemetrySpanExt;

#[cfg(gdb)]
use crate::hypervisor::gdb::{DebugError, DebuggableVm};
Expand All @@ -33,6 +35,8 @@ use crate::hypervisor::virtual_machine::{
VmExit,
};
use crate::mem::memory_region::MemoryRegion;
#[cfg(feature = "trace_guest")]
use crate::sandbox::trace::TraceContext as SandboxTraceContext;

/// Return `true` if the KVM API is available, version 12, and has UserMemory capability, or `false` otherwise
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
Expand Down Expand Up @@ -117,7 +121,13 @@ impl VirtualMachine for KvmVm {
.map_err(|e| UnmapMemoryError::Hypervisor(e.into()))
}

fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
fn run_vcpu(
&mut self,
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
) -> std::result::Result<VmExit, RunVcpuError> {
#[cfg(feature = "trace_guest")]
tc.setup_guest_trace(Span::current().context());

match self.vcpu_fd.run() {
Ok(VcpuExit::Hlt) => Ok(VmExit::Halt()),
Ok(VcpuExit::IoOut(port, data)) => Ok(VmExit::IoOut(port, data.to_vec())),
Expand Down
7 changes: 6 additions & 1 deletion src/hyperlight_host/src/hypervisor/virtual_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::hypervisor::regs::{
CommonDebugRegs, CommonFpu, CommonRegisters, CommonSpecialRegisters,
};
use crate::mem::memory_region::MemoryRegion;
#[cfg(feature = "trace_guest")]
use crate::sandbox::trace::TraceContext as SandboxTraceContext;

/// KVM (Kernel-based Virtual Machine) functionality (linux)
#[cfg(kvm)]
Expand Down Expand Up @@ -287,7 +289,10 @@ pub(crate) trait VirtualMachine: Debug + Send {

/// Runs the vCPU until it exits.
/// Note: this function should not emit any traces or spans as it is called after guest span is setup
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError>;
fn run_vcpu(
&mut self,
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
) -> std::result::Result<VmExit, RunVcpuError>;

/// Get regs
#[allow(dead_code)]
Expand Down
11 changes: 10 additions & 1 deletion src/hyperlight_host/src/hypervisor/virtual_machine/mshv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use mshv_bindings::{
};
use mshv_ioctls::{Mshv, VcpuFd, VmFd};
use tracing::{Span, instrument};
#[cfg(feature = "trace_guest")]
use tracing_opentelemetry::OpenTelemetrySpanExt;

#[cfg(gdb)]
use crate::hypervisor::gdb::{DebugError, DebuggableVm};
Expand All @@ -41,6 +43,8 @@ use crate::hypervisor::virtual_machine::{
VmExit,
};
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
#[cfg(feature = "trace_guest")]
use crate::sandbox::trace::TraceContext as SandboxTraceContext;

/// Determine whether the HyperV for Linux hypervisor API is present
/// and functional.
Expand Down Expand Up @@ -121,7 +125,12 @@ impl VirtualMachine for MshvVm {
.map_err(|e| UnmapMemoryError::Hypervisor(e.into()))
}

fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
fn run_vcpu(
&mut self,
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
) -> std::result::Result<VmExit, RunVcpuError> {
#[cfg(feature = "trace_guest")]
tc.setup_guest_trace(Span::current().context());
Comment on lines +132 to +133
Copy link
Contributor

@ludfjig ludfjig Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this! Just a minor comment: Would you mind moving this right before the call to vcpu.run() in each driver? And a comment would be great as well that it's important that setup_guest_trace is called right before the run call.

const HALT_MESSAGE: hv_message_type = hv_message_type_HVMSG_X64_HALT;
const IO_PORT_INTERCEPT_MESSAGE: hv_message_type =
hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT;
Expand Down
12 changes: 11 additions & 1 deletion src/hyperlight_host/src/hypervisor/virtual_machine/whp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
use std::os::raw::c_void;

use hyperlight_common::mem::PAGE_SIZE_USIZE;
use tracing::Span;
#[cfg(feature = "trace_guest")]
use tracing_opentelemetry::OpenTelemetrySpanExt;
use windows::Win32::Foundation::{FreeLibrary, HANDLE};
use windows::Win32::System::Hypervisor::*;
use windows::Win32::System::LibraryLoader::*;
Expand All @@ -38,6 +41,8 @@ use crate::hypervisor::virtual_machine::{
};
use crate::hypervisor::wrappers::HandleWrapper;
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
#[cfg(feature = "trace_guest")]
use crate::sandbox::trace::TraceContext as SandboxTraceContext;

#[allow(dead_code)] // Will be used for runtime hypervisor detection
pub(crate) fn is_hypervisor_present() -> bool {
Expand Down Expand Up @@ -239,7 +244,12 @@ impl VirtualMachine for WhpVm {
}

#[expect(non_upper_case_globals, reason = "Windows API constant are lower case")]
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
fn run_vcpu(
&mut self,
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
) -> std::result::Result<VmExit, RunVcpuError> {
#[cfg(feature = "trace_guest")]
tc.setup_guest_trace(Span::current().context());
let mut exit_context: WHV_RUN_VP_EXIT_CONTEXT = Default::default();

unsafe {
Expand Down