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
2 changes: 1 addition & 1 deletion .github/workflows/dep_build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ jobs:
just test ${{ inputs.config }} ${{ runner.os == 'Linux' && (inputs.hypervisor == 'mshv3' && 'mshv3,hw-interrupts' || 'kvm,hw-interrupts') || 'hw-interrupts' }}

- name: Run Rust Gdb tests
if: inputs.arch == 'X64'
if: inputs.arch == 'X64' || (inputs.arch == 'arm64' && inputs.hypervisor == 'hyperv-ws2025' && inputs.config == 'debug')
env:
RUST_LOG: debug
run: just test-rust-gdb-debugging ${{ inputs.config }}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
* Add `HYPERLIGHT_SURROGATE_EXTRACT_DIR` to configure the surrogate extraction directory on Windows.
* Add `Sandbox::recreate()`, which rebuilds a sandbox's VM and memory from a snapshot. It recovers unrecoverable sandboxes and keeps configuration, host functions, and interrupt handles by @ludfjig in https://github.com/hyperlight-dev/hyperlight/pull/1751
* Add ARM64 Linux MSHV support by @cshung in https://github.com/hyperlight-dev/hyperlight/pull/1626
* Windows ARM64 WHP guest debugging supports software and hardware breakpoints, register and memory access, and hardware single-step.

### Changed
* Sandboxes configured for debugging cannot create snapshots on x86_64 or ARM64, including after debugger detach.
* `hyperlight-host` requires Rust 1.95.
* `Snapshot::save` caches memory-layer hashes and trusts existing same-length
regular blobs by their content-addressed filenames without verifying their
Expand Down Expand Up @@ -40,6 +42,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
* Host-written snapshot memory has instruction-cache coherence before guest execution.
* Fix slow scratch zeroing on restore under KVM in default builds.
* Fix C# component bindings keeping an entry for every resource handle passed to or from the guest.
* GDB memory reads and writes translate each guest page on hosts with larger pages.
* GDB snapshot recovery handles cancellation and crash detach. Restore rejects installed software breakpoints in ready sandboxes on x86_64 and ARM64.
* WHP ARM64 cache synchronization code is read-only to guests.

### Removed
* **Breaking:** `SandboxBuilder::init_data`, `GuestEnvironment`, `GuestBlob`,
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ test-no-surrogate target=default-target:
# runs tests that exercise gdb debugging
test-rust-gdb-debugging target=default-target features="":
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --example guest-debugging {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }}
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --lib {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }} -- hypervisor::gdb::
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --lib {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }} -- hypervisor::gdb:: hypervisor::hyperlight_vm::debug::tests:: gdb_recovery_tests:: hypervisor::virtual_machine::whp::aarch64::debug_tests::

# rust test for crashdump
test-rust-crashdump target=default-target features="":
Expand Down
53 changes: 50 additions & 3 deletions docs/how-to-debug-a-hyperlight-guest.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ to start listening for a gdb connection.
## Supported features

The Hyperlight `gdb` feature enables guest debugging to:
- stop at an entry point breakpoint which is automatically set by Hyperlight
- add and remove HW breakpoints (maximum 4 set breakpoints at a time)
- stop at the entry point
- add and remove SW breakpoints
- read and write registers
- read and write addresses
- step/continue
- continue
- single-step
- get code offset from target
- stop when a crash occurs and only allow read access to the guest memory and registers

On x86_64, gdb supports up to four hardware breakpoints.
Windows ARM64 WHP supports two hardware breakpoints, four-byte `BRK` software
breakpoints, and hardware single-step. Guest exception vectors forward debug
exceptions through HVC. Guest binaries must retain the
`hyperlight_debug_vectors` symbol so the host can install these vectors before
the initial debug stop. Watchpoints are not advertised.

## Expected behavior

Below is a list describing some cases of expected behavior from a gdb debug
Expand All @@ -38,6 +45,14 @@ session of a guest binary running inside a Hyperlight sandbox.
The debug target will refuse any resume, step actions and write operations to
the guest memory and registers until the gdb client disconnects or the sandbox is stopped.

Sandboxes configured for debugging cannot create snapshots on x86_64 or ARM64,
even after debugger detach. Debugger register and memory edits can violate guest
invariants. This restriction applies regardless of installed breakpoints.

Debugging supports loading and restoring existing snapshots. Restore rejects
installed software breakpoints in a ready sandbox. Recovery of a poisoned
sandbox discards breakpoint bookkeeping before restoring clean memory.

## Example

### Sandbox configuration
Expand Down Expand Up @@ -157,6 +172,38 @@ When the gdb client requests one of the supported features mentioned above, a re
is sent over the communication channel to the main thread for the sandbox
to resolve.

### ARM64 stops and stepping

The host prepares the guest registers before reporting the initial debug stop.
This stop happens once, before the first guest instruction executes. For a
sandbox created from an initialized snapshot, it happens at the first dispatcher
entry because guest initialization has already run.

Software breakpoints replace one instruction with a four-byte `BRK`. GDB
controls breakpoint removal and reinsertion when stepping over that instruction.
Hyperlight handles the breakpoint packets and enables CPU single-step for a
step request. It does not predict the next instruction address.

### Windows ARM64 WHP exception forwarding

The host installs the guest's debug exception vectors before the initial stop.
A breakpoint or single-step exception saves the interrupted PC and PSTATE in
`ELR_EL1` and `SPSR_EL1`. The vector forwards the exception through
`HVC #0x485a`. WHP classifies `ESR_EL1` and exposes the interrupted registers
to GDB. Register writes update that saved context. Resuming returns through
`ERET` to the guest instruction.

Debugger memory writes also synchronize the guest instruction cache. A host
cache flush does not invalidate the vCPU's instruction cache. WHP reserves a
read-execute page at guest physical address `0x1000` containing
`DSB ISH; IC IALLU; DSB ISH; ISB; HVC #0x4858`. The host saves PC, PSTATE,
`SCTLR_EL1`, and `MDSCR_EL1`, disables translation and single-step, and runs
this code at EL1. It checks the exit's HVC immediate and PC before restoring
the saved registers. The page is guest read-only and excluded from sandbox
memory mappings so guest writes cannot replace the cache-sync code.

### Debug command flow

Below is a sequence diagram that shows the interaction between the entities
involved in the gdb debugging of a Hyperlight guest running inside a **KVM** or **MSHV** sandbox.

Expand Down
3 changes: 3 additions & 0 deletions src/hyperlight_common/src/arch/aarch64/exn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use crate::vmem::bits;

/// HVC immediate for forwarding guest exceptions to the Hyper-V debugger.
pub const DEBUG_EXCEPTION_HVC: u16 = 0x485a;

const ESR_EC_UNKNOWN: u64 = 0b000000; // udf generates this
const ESR_EC_DATA_ABORT_LOWER_EL: u64 = 0b100100;
const ESR_EC_DATA_ABORT_SAME_EL: u64 = 0b100101;
Expand Down
17 changes: 17 additions & 0 deletions src/hyperlight_guest_bin/src/arch/aarch64/exception/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,24 @@ context_restore:\n
q_off = const offset_of!(ExceptionContext, q),
);

// Only synchronous exceptions have a syndrome for debug forwarding.
// The host resumes debug events at ERET and other synchronous exceptions at the prologue.
macro_rules! vbar_entry {
($et:literal, $ef:literal) => {
concat!(
"
.if {ExceptionType_",
$et,
"} == {ExceptionType_Synchronous}\n
stp x0, x1, [sp, #-16]!\n
mrs x0, mdscr_el1\n
tbz x0, #13, 1f\n
ldp x0, x1, [sp], #16\n
hvc #{debug_hvc}\n
eret\n
1:\n
ldp x0, x1, [sp], #16\n
.endif\n
sub sp, sp, #{ctx_size}\n
str x30, [sp, #{x30_off}]\n
bl context_save\n
Expand All @@ -129,6 +143,8 @@ macro_rules! vbar_entry {
global_asm!("
.balign 0x800\n
.global vbar\n
.global hyperlight_debug_vectors\n
hyperlight_debug_vectors:\n
vbar:\n",
vbar_entry!("Synchronous", "CurrentSP0"),
vbar_entry!("IRQ", "CurrentSP0"),
Expand All @@ -148,6 +164,7 @@ vbar:\n",
vbar_entry!("SError", "LowerAArch32"),
ctx_size = const size_of::<ExceptionContext>(),
x30_off = const offset_of!(ExceptionContext, x) + 15 * 0x010,
debug_hvc = const hyperlight_common::arch::exn::DEBUG_EXCEPTION_HVC,
handler = sym super::handle::handle_exception,
ExceptionType_Synchronous = const ExceptionType::Synchronous as u64,
ExceptionType_IRQ = const ExceptionType::Irq as u64,
Expand Down
3 changes: 2 additions & 1 deletion src/hyperlight_host/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ fn main() -> Result<()> {
// Essentially the kvm and mshv3 features are ignored on windows as long as you use #[cfg(kvm)] and not #[cfg(feature = "kvm")].
// You should never use #[cfg(feature = "kvm")] or #[cfg(feature = "mshv3")] in the codebase.
cfg_aliases::cfg_aliases! {
gdb: { all(feature = "gdb", debug_assertions, target_arch = "x86_64") },
gdb_platform: { any(target_arch = "x86_64", all(target_arch = "aarch64", target_os = "windows")) },
gdb: { all(feature = "gdb", debug_assertions, gdb_platform) },
kvm: { all(feature = "kvm", target_os = "linux") },
mshv3: { all(feature = "mshv3", target_os = "linux") },
hvf: { all(feature = "hvf", target_os = "macos") },
Expand Down
Loading
Loading