Skip to content
Draft
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
389 changes: 389 additions & 0 deletions docs/cancellation.md

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions src/hyperlight_host/src/hypervisor/crashdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use elfcore::{
ReadProcessMemory, ThreadView, VaProtection, VaRegion,
};

use super::Hypervisor;
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
use crate::{Result, new_error};

Expand Down Expand Up @@ -262,12 +261,7 @@ impl ReadProcessMemory for GuestMemReader {
///
/// # Returns
/// * `Result<()>`: Success or error
pub(crate) fn generate_crashdump(hv: &dyn Hypervisor) -> Result<()> {
// Get crash context from hypervisor
let ctx = hv
.crashdump_context()
.map_err(|e| new_error!("Failed to get crashdump context: {:?}", e))?;

pub(crate) fn generate_crashdump(ctx: Option<CrashDumpContext>) -> Result<()> {
// Get env variable for core dump directory
let core_dump_dir = std::env::var("HYPERLIGHT_CORE_DUMP_DIR").ok();

Expand Down
21 changes: 4 additions & 17 deletions src/hyperlight_host/src/hypervisor/gdb/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ limitations under the License.

//! This file contains architecture specific code for the x86_64

use std::collections::HashMap;

use super::VcpuStopReason;

// Described in Table 6-1. Exceptions and Interrupts at Page 6-13 Vol. 1
Expand Down Expand Up @@ -51,60 +49,49 @@ pub(crate) const DR6_HW_BP_FLAGS_MASK: u64 = 0x0F << DR6_HW_BP_FLAGS_POS;

/// Determine the reason the vCPU stopped
/// This is done by checking the DR6 register and the exception id
/// NOTE: Additional checks are done for the entrypoint, stored hw_breakpoints
/// and sw_breakpoints to ensure the stop reason is valid with internal state
pub(crate) fn vcpu_stop_reason(
single_step: bool,
rip: u64,
dr6: u64,
entrypoint: u64,
dr6: u64,
exception: u32,
hw_breakpoints: &[u64],
sw_breakpoints: &HashMap<u64, [u8; SW_BP_SIZE]>,
) -> VcpuStopReason {
if DB_EX_ID == exception {
// If the BS flag in DR6 register is set, it means a single step
// instruction triggered the exit
// Check page 19-4 Vol. 3B of Intel 64 and IA-32
// Architectures Software Developer's Manual
if dr6 & DR6_BS_FLAG_MASK != 0 && single_step {
if dr6 & DR6_BS_FLAG_MASK != 0 {
return VcpuStopReason::DoneStep;
}

// If any of the B0-B3 flags in DR6 register is set, it means a
// hardware breakpoint triggered the exit
// Check page 19-4 Vol. 3B of Intel 64 and IA-32
// Architectures Software Developer's Manual
if DR6_HW_BP_FLAGS_MASK & dr6 != 0 && hw_breakpoints.contains(&rip) {
if DR6_HW_BP_FLAGS_MASK & dr6 != 0 {
if rip == entrypoint {
return VcpuStopReason::EntryPointBp;
}
return VcpuStopReason::HwBp;
}
}

if BP_EX_ID == exception && sw_breakpoints.contains_key(&rip) {
if BP_EX_ID == exception {
return VcpuStopReason::SwBp;
}

// Log an error and provide internal debugging info
log::error!(
r"The vCPU exited because of an unknown reason:
single_step: {:?}
rip: {:?}
dr6: {:?}
entrypoint: {:?}
exception: {:?}
hw_breakpoints: {:?}
sw_breakpoints: {:?}
",
single_step,
rip,
dr6,
entrypoint,
exception,
hw_breakpoints,
sw_breakpoints,
);

VcpuStopReason::Unknown
Expand Down
51 changes: 2 additions & 49 deletions src/hyperlight_host/src/hypervisor/gdb/hyperv_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ limitations under the License.

use std::collections::HashMap;

use windows::Win32::System::Hypervisor::WHV_VP_EXCEPTION_CONTEXT;

use super::arch::{MAX_NO_OF_HW_BP, vcpu_stop_reason};
use super::{GuestDebug, SW_BP_SIZE, VcpuStopReason};
use super::arch::MAX_NO_OF_HW_BP;
use super::{GuestDebug, SW_BP_SIZE};
use crate::hypervisor::regs::{CommonFpu, CommonRegisters};
use crate::hypervisor::windows_hypervisor_platform::VMProcessor;
use crate::hypervisor::wrappers::WHvDebugRegisters;
Expand Down Expand Up @@ -52,15 +50,6 @@ impl HypervDebug {
}
}

/// Returns the instruction pointer from the stopped vCPU
fn get_instruction_pointer(&self, vcpu_fd: &VMProcessor) -> Result<u64> {
let regs = vcpu_fd
.regs()
.map_err(|e| new_error!("Could not retrieve registers from vCPU: {:?}", e))?;

Ok(regs.rip)
}

/// This method sets the kvm debugreg fields to enable breakpoints at
/// specific addresses
///
Expand Down Expand Up @@ -120,42 +109,6 @@ impl HypervDebug {

Ok(())
}

/// Get the reason the vCPU has stopped
pub(crate) fn get_stop_reason(
&mut self,
vcpu_fd: &VMProcessor,
exception: WHV_VP_EXCEPTION_CONTEXT,
entrypoint: u64,
) -> Result<VcpuStopReason> {
let rip = self.get_instruction_pointer(vcpu_fd)?;
let rip = self.translate_gva(vcpu_fd, rip)?;

let debug_regs = vcpu_fd
.get_debug_regs()
.map_err(|e| new_error!("Could not retrieve registers from vCPU: {:?}", e))?;

// Check if the vCPU stopped because of a hardware breakpoint
let reason = vcpu_stop_reason(
self.single_step,
rip,
debug_regs.dr6,
entrypoint,
exception.ExceptionType as u32,
&self.hw_breakpoints,
&self.sw_breakpoints,
);

if let VcpuStopReason::EntryPointBp = reason {
// In case the hw breakpoint is the entry point, remove it to
// avoid hanging here as gdb does not remove breakpoints it
// has not set.
// Gdb expects the target to be stopped when connected.
self.remove_hw_breakpoint(vcpu_fd, entrypoint)?;
}

Ok(reason)
}
}

impl GuestDebug for HypervDebug {
Expand Down
47 changes: 3 additions & 44 deletions src/hyperlight_host/src/hypervisor/gdb/kvm_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use std::collections::HashMap;

use kvm_bindings::{
KVM_GUESTDBG_ENABLE, KVM_GUESTDBG_SINGLESTEP, KVM_GUESTDBG_USE_HW_BP, KVM_GUESTDBG_USE_SW_BP,
kvm_debug_exit_arch, kvm_guest_debug,
kvm_guest_debug,
};
use kvm_ioctls::VcpuFd;

use super::arch::{MAX_NO_OF_HW_BP, SW_BP_SIZE, vcpu_stop_reason};
use super::{GuestDebug, VcpuStopReason};
use super::GuestDebug;
use super::arch::{MAX_NO_OF_HW_BP, SW_BP_SIZE};
use crate::hypervisor::regs::{CommonFpu, CommonRegisters};
use crate::{HyperlightError, Result, new_error};

Expand Down Expand Up @@ -59,15 +59,6 @@ impl KvmDebug {
}
}

/// Returns the instruction pointer from the stopped vCPU
fn get_instruction_pointer(&self, vcpu_fd: &VcpuFd) -> Result<u64> {
let regs = vcpu_fd
.get_regs()
.map_err(|e| new_error!("Could not retrieve registers from vCPU: {:?}", e))?;

Ok(regs.rip)
}

/// This method sets the kvm debugreg fields to enable breakpoints at
/// specific addresses
///
Expand Down Expand Up @@ -106,38 +97,6 @@ impl KvmDebug {

Ok(())
}

/// Get the reason the vCPU has stopped
pub(crate) fn get_stop_reason(
&mut self,
vcpu_fd: &VcpuFd,
debug_exit: kvm_debug_exit_arch,
entrypoint: u64,
) -> Result<VcpuStopReason> {
let rip = self.get_instruction_pointer(vcpu_fd)?;
let rip = self.translate_gva(vcpu_fd, rip)?;

// Check if the vCPU stopped because of a hardware breakpoint
let reason = vcpu_stop_reason(
self.single_step,
rip,
debug_exit.dr6,
entrypoint,
debug_exit.exception,
&self.hw_breakpoints,
&self.sw_breakpoints,
);

if let VcpuStopReason::EntryPointBp = reason {
// In case the hw breakpoint is the entry point, remove it to
// avoid hanging here as gdb does not remove breakpoints it
// has not set.
// Gdb expects the target to be stopped when connected.
self.remove_hw_breakpoint(vcpu_fd, entrypoint)?;
}

Ok(reason)
}
}

impl GuestDebug for KvmDebug {
Expand Down
4 changes: 2 additions & 2 deletions src/hyperlight_host/src/hypervisor/gdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

mod arch;
pub(crate) mod arch;
mod event_loop;
#[cfg(target_os = "windows")]
mod hyperv_debug;
Expand Down Expand Up @@ -235,7 +235,7 @@ impl DebugMemoryAccess {
}

/// Defines the possible reasons for which a vCPU can be stopped when debugging
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum VcpuStopReason {
Crash,
DoneStep,
Expand Down
51 changes: 2 additions & 49 deletions src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use mshv_bindings::{
};
use mshv_ioctls::VcpuFd;

use super::arch::{MAX_NO_OF_HW_BP, SW_BP_SIZE, vcpu_stop_reason};
use super::{GuestDebug, VcpuStopReason};
use super::GuestDebug;
use super::arch::{MAX_NO_OF_HW_BP, SW_BP_SIZE};
use crate::hypervisor::regs::{CommonFpu, CommonRegisters};
use crate::{HyperlightError, Result, new_error};

Expand Down Expand Up @@ -55,15 +55,6 @@ impl MshvDebug {
}
}

/// Returns the instruction pointer from the stopped vCPU
fn get_instruction_pointer(&self, vcpu_fd: &VcpuFd) -> Result<u64> {
let regs = vcpu_fd
.get_regs()
.map_err(|e| new_error!("Could not retrieve registers from vCPU: {:?}", e))?;

Ok(regs.rip)
}

/// This method sets the vCPU debug register fields to enable breakpoints at
/// specific addresses
///
Expand Down Expand Up @@ -121,44 +112,6 @@ impl MshvDebug {

Ok(())
}

/// Returns the vCPU stop reason
pub(crate) fn get_stop_reason(
&mut self,
vcpu_fd: &VcpuFd,
exception: u16,
entrypoint: u64,
) -> Result<VcpuStopReason> {
let regs = vcpu_fd
.get_debug_regs()
.map_err(|e| new_error!("Cannot retrieve debug registers from vCPU: {}", e))?;

// DR6 register contains debug state related information
let debug_status = regs.dr6;

let rip = self.get_instruction_pointer(vcpu_fd)?;
let rip = self.translate_gva(vcpu_fd, rip)?;

let reason = vcpu_stop_reason(
self.single_step,
rip,
debug_status,
entrypoint,
exception as u32,
&self.hw_breakpoints,
&self.sw_breakpoints,
);

if let VcpuStopReason::EntryPointBp = reason {
// In case the hw breakpoint is the entry point, remove it to
// avoid hanging here as gdb does not remove breakpoints it
// has not set.
// Gdb expects the target to be stopped when connected.
self.remove_hw_breakpoint(vcpu_fd, entrypoint)?;
}

Ok(reason)
}
}

impl GuestDebug for MshvDebug {
Expand Down
Loading
Loading