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
54 changes: 19 additions & 35 deletions src/hyperlight_host/src/hypervisor/crashdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use elfcore::{
ReadProcessMemory, ThreadView, VaProtection, VaRegion,
};

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

Expand Down Expand Up @@ -262,7 +262,7 @@ impl ReadProcessMemory for GuestMemReader {
///
/// # Returns
/// * `Result<()>`: Success or error
pub(crate) fn generate_crashdump(hv: &dyn Hypervisor) -> Result<()> {
pub(crate) fn generate_crashdump(hv: &HyperlightVm) -> Result<()> {
// Get crash context from hypervisor
let ctx = hv
.crashdump_context()
Expand Down Expand Up @@ -349,28 +349,23 @@ fn core_dump_file_path(dump_dir: Option<String>) -> String {
/// Returns:
/// * `Result<usize>`: The number of bytes written to the core dump file.
fn checked_core_dump(
ctx: Option<CrashDumpContext>,
ctx: CrashDumpContext,
get_writer: impl FnOnce() -> Result<Box<dyn Write>>,
) -> Result<usize> {
let mut nbytes = 0;
// If the HV returned a context it means we can create a core dump
// This is the case when the sandbox has been configured at runtime to allow core dumps
if let Some(ctx) = ctx {
log::info!("Creating core dump file...");

// Set up data sources for the core dump
let guest_view = GuestView::new(&ctx);
let memory_reader = GuestMemReader::new(&ctx);

// Create and write core dump
let core_builder = CoreDumpBuilder::from_source(guest_view, memory_reader);

let writer = get_writer()?;
// Write the core dump directly to the file
nbytes = core_builder
.write(writer)
.map_err(|e| new_error!("Failed to write core dump: {:?}", e))?;
}
log::info!("Creating core dump file...");

// Set up data sources for the core dump
let guest_view = GuestView::new(&ctx);
let memory_reader = GuestMemReader::new(&ctx);

// Create and write core dump
let core_builder = CoreDumpBuilder::from_source(guest_view, memory_reader);

let writer = get_writer()?;
// Write the core dump directly to the file
let nbytes = core_builder
.write(writer)
.map_err(|e| new_error!("Failed to write core dump: {:?}", e))?;

Ok(nbytes)
}
Expand Down Expand Up @@ -424,17 +419,6 @@ mod test {
assert!(path.starts_with(&temp_dir));
}

/// Test core is not created when the context is None
#[test]
fn test_crashdump_not_created_when_context_is_none() {
// Call the function with None context
let result = checked_core_dump(None, || Ok(Box::new(std::io::empty())));

// Check if the result is ok and the number of bytes is 0
assert!(result.is_ok());
assert_eq!(result.unwrap(), 0);
}

/// Test the core dump creation with no regions fails
#[test]
fn test_crashdump_write_fails_when_no_regions() {
Expand All @@ -451,7 +435,7 @@ mod test {
let get_writer = || Ok(Box::new(std::io::empty()) as Box<dyn Write>);

// Call the function
let result = checked_core_dump(Some(ctx), get_writer);
let result = checked_core_dump(ctx, get_writer);

// Check if the result is an error
// This should fail because there are no regions
Expand Down Expand Up @@ -482,7 +466,7 @@ mod test {
let get_writer = || Ok(Box::new(std::io::empty()) as Box<dyn Write>);

// Call the function
let result = checked_core_dump(Some(ctx), get_writer);
let result = checked_core_dump(ctx, get_writer);

// Check if the result is ok and the number of bytes is 0
assert!(result.is_ok());
Expand Down
46 changes: 18 additions & 28 deletions src/hyperlight_host/src/hypervisor/gdb/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@ limitations under the License.

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

use std::collections::HashMap;

use super::VcpuStopReason;
use crate::Result;
use crate::hypervisor::regs::CommonRegisters;
use crate::hypervisor::vm::Vm;

// Described in Table 6-1. Exceptions and Interrupts at Page 6-13 Vol. 1
// of Intel 64 and IA-32 Architectures Software Developer's Manual
/// Exception id for #DB
const DB_EX_ID: u32 = 1;
pub(crate) const DB_EX_ID: u32 = 1;
/// Exception id for #BP - triggered by the INT3 instruction
const BP_EX_ID: u32 = 3;
pub(crate) const BP_EX_ID: u32 = 3;

/// Software Breakpoint size in memory
pub(crate) const SW_BP_SIZE: usize = 1;
/// Software Breakpoint opcode - INT3
/// Check page 7-28 Vol. 3A of Intel 64 and IA-32
/// Architectures Software Developer's Manual
pub(crate) const SW_BP_OP: u8 = 0xCC;
/// Software Breakpoint written to memory
pub(crate) const SW_BP: [u8; SW_BP_SIZE] = [SW_BP_OP];
/// Maximum number of supported hardware breakpoints
pub(crate) const MAX_NO_OF_HW_BP: usize = 4;

Expand All @@ -54,58 +51,51 @@ pub(crate) const DR6_HW_BP_FLAGS_MASK: u64 = 0x0F << DR6_HW_BP_FLAGS_POS;
/// 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,
vm: &mut dyn Vm,
entrypoint: u64,
dr6: u64,
exception: u32,
hw_breakpoints: &[u64],
sw_breakpoints: &HashMap<u64, [u8; SW_BP_SIZE]>,
) -> VcpuStopReason {
) -> Result<VcpuStopReason> {
let CommonRegisters { rip, .. } = vm.regs()?;
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 {
return VcpuStopReason::DoneStep;
if dr6 & DR6_BS_FLAG_MASK != 0 {
return Ok(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;
vm.remove_hw_breakpoint(entrypoint)?;
return Ok(VcpuStopReason::EntryPointBp);
}
return VcpuStopReason::HwBp;
return Ok(VcpuStopReason::HwBp);
}
}

if BP_EX_ID == exception && sw_breakpoints.contains_key(&rip) {
return VcpuStopReason::SwBp;
if BP_EX_ID == exception {
return Ok(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
Ok(VcpuStopReason::Unknown)
}
Loading