Skip to content

Commit 091fe7f

Browse files
authored
Code refactor to set trace context in run_vcpu function. (#1184)
Signed-off-by: Shailesh Vashishth <shavashishth@gmail.com>
1 parent ddcf39e commit 091fe7f

5 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/hyperlight_host/src/hypervisor/hyperlight_vm.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ use std::sync::{Arc, Mutex};
2727

2828
use log::LevelFilter;
2929
use tracing::{Span, instrument};
30-
#[cfg(feature = "trace_guest")]
31-
use tracing_opentelemetry::OpenTelemetrySpanExt;
3230

3331
#[cfg(gdb)]
3432
use super::gdb::arch::VcpuStopReasonError;
@@ -660,13 +658,13 @@ impl HyperlightVm {
660658
{
661659
Ok(VmExit::Cancelled())
662660
} else {
663-
#[cfg(feature = "trace_guest")]
664-
tc.setup_guest_trace(Span::current().context());
665-
666661
// ==== KILL() TIMING POINT 3: Before calling run() ====
667662
// If kill() is called and ran to completion BEFORE this line executes:
668663
// - Will still do a VM entry, but signals will be sent until VM exits
669-
let result = self.vm.run_vcpu();
664+
let result = self.vm.run_vcpu(
665+
#[cfg(feature = "trace_guest")]
666+
&mut tc,
667+
);
670668

671669
// End current host trace by closing the current span that captures traces
672670
// happening when a guest exits and re-enters.

src/hyperlight_host/src/hypervisor/virtual_machine/kvm.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use kvm_bindings::{kvm_debugregs, kvm_fpu, kvm_regs, kvm_sregs, kvm_userspace_me
2222
use kvm_ioctls::Cap::UserMemory;
2323
use kvm_ioctls::{Kvm, VcpuExit, VcpuFd, VmFd};
2424
use tracing::{Span, instrument};
25+
#[cfg(feature = "trace_guest")]
26+
use tracing_opentelemetry::OpenTelemetrySpanExt;
2527

2628
#[cfg(gdb)]
2729
use crate::hypervisor::gdb::{DebugError, DebuggableVm};
@@ -33,6 +35,8 @@ use crate::hypervisor::virtual_machine::{
3335
VmExit,
3436
};
3537
use crate::mem::memory_region::MemoryRegion;
38+
#[cfg(feature = "trace_guest")]
39+
use crate::sandbox::trace::TraceContext as SandboxTraceContext;
3640

3741
/// Return `true` if the KVM API is available, version 12, and has UserMemory capability, or `false` otherwise
3842
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
@@ -134,7 +138,14 @@ impl VirtualMachine for KvmVm {
134138
.map_err(|e| UnmapMemoryError::Hypervisor(e.into()))
135139
}
136140

137-
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
141+
fn run_vcpu(
142+
&mut self,
143+
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
144+
) -> std::result::Result<VmExit, RunVcpuError> {
145+
// setup_trace_guest must be called right before vcpu_run.run() call, because
146+
// it sets the guest span, no other traces or spans must be setup in between these calls.
147+
#[cfg(feature = "trace_guest")]
148+
tc.setup_guest_trace(Span::current().context());
138149
match self.vcpu_fd.run() {
139150
Ok(VcpuExit::Hlt) => Ok(VmExit::Halt()),
140151
Ok(VcpuExit::IoOut(port, data)) => Ok(VmExit::IoOut(port, data.to_vec())),

src/hyperlight_host/src/hypervisor/virtual_machine/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::hypervisor::regs::{
2525
CommonDebugRegs, CommonFpu, CommonRegisters, CommonSpecialRegisters,
2626
};
2727
use crate::mem::memory_region::MemoryRegion;
28+
#[cfg(feature = "trace_guest")]
29+
use crate::sandbox::trace::TraceContext as SandboxTraceContext;
2830

2931
/// KVM (Kernel-based Virtual Machine) functionality (linux)
3032
#[cfg(kvm)]
@@ -286,8 +288,12 @@ pub(crate) trait VirtualMachine: Debug + Send {
286288
) -> std::result::Result<(), UnmapMemoryError>;
287289

288290
/// Runs the vCPU until it exits.
289-
/// Note: this function should not emit any traces or spans as it is called after guest span is setup
290-
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError>;
291+
/// Note: this function emits traces spans for guests
292+
/// and the span setup is called right before the run virtual processor call of each hypervisor
293+
fn run_vcpu(
294+
&mut self,
295+
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
296+
) -> std::result::Result<VmExit, RunVcpuError>;
291297

292298
/// Get regs
293299
#[allow(dead_code)]

src/hyperlight_host/src/hypervisor/virtual_machine/mshv.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use mshv_bindings::{
3030
};
3131
use mshv_ioctls::{Mshv, VcpuFd, VmFd};
3232
use tracing::{Span, instrument};
33+
#[cfg(feature = "trace_guest")]
34+
use tracing_opentelemetry::OpenTelemetrySpanExt;
3335

3436
#[cfg(gdb)]
3537
use crate::hypervisor::gdb::{DebugError, DebuggableVm};
@@ -41,6 +43,8 @@ use crate::hypervisor::virtual_machine::{
4143
VmExit,
4244
};
4345
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
46+
#[cfg(feature = "trace_guest")]
47+
use crate::sandbox::trace::TraceContext as SandboxTraceContext;
4448

4549
/// Determine whether the HyperV for Linux hypervisor API is present
4650
/// and functional.
@@ -121,7 +125,10 @@ impl VirtualMachine for MshvVm {
121125
.map_err(|e| UnmapMemoryError::Hypervisor(e.into()))
122126
}
123127

124-
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
128+
fn run_vcpu(
129+
&mut self,
130+
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
131+
) -> std::result::Result<VmExit, RunVcpuError> {
125132
const HALT_MESSAGE: hv_message_type = hv_message_type_HVMSG_X64_HALT;
126133
const IO_PORT_INTERCEPT_MESSAGE: hv_message_type =
127134
hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT;
@@ -130,6 +137,10 @@ impl VirtualMachine for MshvVm {
130137
#[cfg(gdb)]
131138
const EXCEPTION_INTERCEPT: hv_message_type = hv_message_type_HVMSG_X64_EXCEPTION_INTERCEPT;
132139

140+
// setup_trace_guest must be called right before vcpu_run.run() call, because
141+
// it sets the guest span, no other traces or spans must be setup in between these calls.
142+
#[cfg(feature = "trace_guest")]
143+
tc.setup_guest_trace(Span::current().context());
133144
let exit_reason = self.vcpu_fd.run();
134145

135146
let result = match exit_reason {

src/hyperlight_host/src/hypervisor/virtual_machine/whp.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ limitations under the License.
1616

1717
use std::os::raw::c_void;
1818

19+
use tracing::Span;
20+
#[cfg(feature = "trace_guest")]
21+
use tracing_opentelemetry::OpenTelemetrySpanExt;
1922
use windows::Win32::Foundation::{FreeLibrary, HANDLE};
2023
use windows::Win32::System::Hypervisor::*;
2124
use windows::Win32::System::LibraryLoader::*;
@@ -36,6 +39,8 @@ use crate::hypervisor::virtual_machine::{
3639
VirtualMachine, VmExit,
3740
};
3841
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
42+
#[cfg(feature = "trace_guest")]
43+
use crate::sandbox::trace::TraceContext as SandboxTraceContext;
3944

4045
#[allow(dead_code)] // Will be used for runtime hypervisor detection
4146
pub(crate) fn is_hypervisor_present() -> bool {
@@ -213,9 +218,16 @@ impl VirtualMachine for WhpVm {
213218
}
214219

215220
#[expect(non_upper_case_globals, reason = "Windows API constant are lower case")]
216-
fn run_vcpu(&mut self) -> std::result::Result<VmExit, RunVcpuError> {
221+
fn run_vcpu(
222+
&mut self,
223+
#[cfg(feature = "trace_guest")] tc: &mut SandboxTraceContext,
224+
) -> std::result::Result<VmExit, RunVcpuError> {
217225
let mut exit_context: WHV_RUN_VP_EXIT_CONTEXT = Default::default();
218226

227+
// setup_trace_guest must be called right before WHvRunVirtualProcessor() call, because
228+
// it sets the guest span, no other traces or spans must be setup in between these calls.
229+
#[cfg(feature = "trace_guest")]
230+
tc.setup_guest_trace(Span::current().context());
219231
unsafe {
220232
WHvRunVirtualProcessor(
221233
self.partition,
@@ -225,7 +237,6 @@ impl VirtualMachine for WhpVm {
225237
)
226238
.map_err(|e| RunVcpuError::Unknown(e.into()))?;
227239
}
228-
229240
let result = match exit_context.ExitReason {
230241
WHvRunVpExitReasonX64IoPortAccess => unsafe {
231242
let instruction_length = exit_context.VpContext._bitfield & 0xF;

0 commit comments

Comments
 (0)