Skip to content

Commit b938220

Browse files
committed
fixup
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 8eee79a commit b938220

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/hyperlight_host/src/hypervisor/crashdump.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const NT_X86_XSTATE: u32 = 0x202;
1414
const AT_ENTRY: u64 = 9;
1515
const AT_NULL: u64 = 0;
1616

17+
/// Structure to hold the crash dump context
18+
/// This structure contains the information needed to create a core dump
1719
#[derive(Debug)]
1820
pub(crate) struct CrashDumpContext<'a> {
1921
regions: &'a [MemoryRegion],
@@ -38,6 +40,8 @@ impl<'a> CrashDumpContext<'a> {
3840
}
3941
}
4042

43+
/// Structure that contains the process information for the core dump
44+
/// This serves as a source of information for `elfcore`'s [`CoreDumpBuilder`]
4145
struct GuestView {
4246
regions: Vec<VaRegion>,
4347
threads: Vec<ThreadView>,
@@ -46,6 +50,7 @@ struct GuestView {
4650

4751
impl GuestView {
4852
fn new(ctx: &CrashDumpContext) -> Self {
53+
// Map the regions to the format `CoreDumpBuilder` expects
4954
let regions = ctx
5055
.regions
5156
.iter()
@@ -64,6 +69,7 @@ impl GuestView {
6469
})
6570
.collect();
6671

72+
// The xsave state is checked as it can be empty
6773
let mut components = vec![];
6874
if !ctx.xsave.is_empty() {
6975
components.push(ArchComponentState {
@@ -73,6 +79,10 @@ impl GuestView {
7379
data: ctx.xsave.clone(),
7480
});
7581
}
82+
83+
// Create the thread view
84+
// The thread view contains the information about the thread
85+
// NOTE: Some of these fields are not used in the current implementation
7686
let thread = ThreadView {
7787
flags: 0, // Kernel flags for the process
7888
tid: 1,
@@ -145,6 +155,9 @@ impl ProcessInfoSource for GuestView {
145155
}
146156
}
147157

158+
/// Structure that reads the guest memory
159+
/// This structure serves as a custom memory reader for `elfcore`'s
160+
/// [`CoreDumpBuilder`]
148161
struct GuestMemReader {
149162
regions: Vec<MemoryRegion>,
150163
}
@@ -164,6 +177,7 @@ impl ReadProcessMemory for GuestMemReader {
164177
buf: &mut [u8],
165178
) -> std::result::Result<usize, CoreError> {
166179
for r in self.regions.iter() {
180+
// Check if the base address is within the guest region
167181
if base >= r.guest_region.start && base < r.guest_region.end {
168182
let offset = base - r.guest_region.start;
169183
let region_slice = unsafe {
@@ -181,6 +195,8 @@ impl ReadProcessMemory for GuestMemReader {
181195

182196
// Only copy the amount that fits in both buffers
183197
buf[..copy_size].copy_from_slice(&region_slice[offset..offset + copy_size]);
198+
199+
// Return the number of bytes copied
184200
return std::result::Result::Ok(copy_size);
185201
}
186202
}
@@ -227,13 +243,13 @@ pub(crate) fn crashdump_to_tempfile(hv: &dyn Hypervisor) -> Result<()> {
227243
.write(&temp_file)
228244
.map_err(|e| new_error!("Failed to write core dump: {:?}", e))?;
229245

230-
// Persist the file with a .dmp extension
231-
let persist_path = temp_file.path().with_extension("dmp");
246+
let persist_path = temp_file.path().with_extension("elf");
232247
temp_file
233248
.persist(&persist_path)
234249
.map_err(|e| new_error!("Failed to persist core dump file: {:?}", e))?;
235250

236251
let path_string = persist_path.to_string_lossy().to_string();
252+
237253
println!("Core dump created successfully: {}", path_string);
238254
log::error!("Core dump file: {}", path_string);
239255

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ impl Hypervisor for HypervLinuxDriver {
667667
let sregs = self.vcpu_fd.get_sregs()?;
668668
let xsave = self.vcpu_fd.get_xsave()?;
669669

670+
// Set up the registers for the crash dump
670671
regs[0] = vcpu_regs.r15; // r15
671672
regs[1] = vcpu_regs.r14; // r14
672673
regs[2] = vcpu_regs.r13; // r13

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ impl Hypervisor for HypervWindowsDriver {
496496
let sregs = self.processor.get_sregs()?;
497497
let xsave = self.processor.get_xsave()?;
498498

499+
// Set the registers in the order expected by the crashdump context
499500
regs[0] = vcpu_regs.r15; // r15
500501
regs[1] = vcpu_regs.r14; // r14
501502
regs[2] = vcpu_regs.r13; // r13

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ impl Hypervisor for KVMDriver {
591591
let sregs = self.vcpu_fd.get_sregs()?;
592592
let xsave = self.vcpu_fd.get_xsave()?;
593593

594+
// Set the registers in the order expected by the crashdump context
594595
regs[0] = vcpu_regs.r15; // r15
595596
regs[1] = vcpu_regs.r14; // r14
596597
regs[2] = vcpu_regs.r13; // r13
@@ -619,6 +620,8 @@ impl Hypervisor for KVMDriver {
619620
regs[25] = sregs.fs.selector as u64; // fs
620621
regs[26] = sregs.gs.selector as u64; // gs
621622

623+
// The [`CrashDumpContext`] accepts xsave as a vector of u8, so we need to convert the
624+
// xsave region to a vector of u8
622625
Ok(crashdump::CrashDumpContext::new(
623626
&self.mem_regions,
624627
regs,

0 commit comments

Comments
 (0)