Skip to content

Commit 01d11a5

Browse files
committed
Add support for reading the mxcsr registers
Signed-off-by: James Sturtevant <[email protected]>
1 parent 70536d2 commit 01d11a5

File tree

6 files changed

+24
-1
lines changed

6 files changed

+24
-1
lines changed

src/hyperlight_host/src/hypervisor/gdb/hyperv_debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ impl GuestDebug for HypervDebug {
218218
fpu.xmm8, fpu.xmm9, fpu.xmm10, fpu.xmm11, fpu.xmm12, fpu.xmm13, fpu.xmm14,
219219
fpu.xmm15,
220220
];
221+
regs.mxcsr = fpu.mxcsr;
221222
} else {
222223
log::warn!("Failed to read FPU/XMM via WHVP for debug registers");
223224
}

src/hyperlight_host/src/hypervisor/gdb/kvm_debug.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
use std::collections::HashMap;
1818

19+
use gdbstub_arch::msp430::reg;
1920
use kvm_bindings::{
2021
KVM_GUESTDBG_ENABLE, KVM_GUESTDBG_SINGLESTEP, KVM_GUESTDBG_USE_HW_BP, KVM_GUESTDBG_USE_SW_BP,
2122
kvm_debug_exit_arch, kvm_guest_debug, kvm_regs,
@@ -194,16 +195,30 @@ impl GuestDebug for KvmDebug {
194195
regs.rflags = vcpu_regs.rflags;
195196

196197
// Read XMM registers from FPU state
198+
// note kvm get_fpu doesn't actually set or read the mxcsr value
199+
// https://elixir.bootlin.com/linux/v6.16/source/arch/x86/kvm/x86.c#L12229
197200
match vcpu_fd.get_fpu() {
198201
Ok(fpu) => {
199202
// Convert KVM XMM registers ([u8; 16] x 16) to [u128; 16]
200-
regs.xmm = fpu.xmm.map(u128::from_le_bytes);;
203+
regs.xmm = fpu.xmm.map(u128::from_le_bytes);
201204
},
202205
Err(e) => {
203206
log::warn!("Failed to read FPU state for XMM registers: {:?}", e);
204207
}
205208
}
206209

210+
// Read MXCSR from XSAVE (MXCSR is at byte offset 24 -> u32 index 6)
211+
// Todo maybe I could use xsave to read the registers too instead of a separate call to get_fpu
212+
match vcpu_fd.get_xsave() {
213+
Ok(xsave) => {
214+
regs.mxcsr = xsave.region[6];
215+
216+
}
217+
Err(e) => {
218+
log::warn!("Failed to read XSAVE for MXCSR: {:?}", e);
219+
}
220+
}
221+
207222
Ok(())
208223
}
209224

src/hyperlight_host/src/hypervisor/gdb/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub(crate) struct X86_64Regs {
109109
pub(crate) rip: u64,
110110
pub(crate) rflags: u64,
111111
pub(crate) xmm: [u128; 16],
112+
pub(crate) mxcsr: u32,
112113
}
113114

114115
/// Defines the possible reasons for which a vCPU can be stopped when debugging

src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl GuestDebug for MshvDebug {
225225
Ok(fpu) => {
226226
// MSHV exposes XMM as [[u8; 16]; 16]. Convert to [u128; 16]
227227
regs.xmm = fpu.xmm.map(u128::from_le_bytes);
228+
regs.mxcsr = fpu.mxcsr;
228229
}
229230
Err(e) => {
230231
log::warn!("Failed to read FPU state for XMM registers (MSHV): {:?}", e);

src/hyperlight_host/src/hypervisor/gdb/x86_64_target.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl SingleThreadBase for HyperlightSandboxTarget {
228228
regs.rip = read_regs.rip;
229229
regs.eflags = read_regs.rflags as u32;
230230
regs.xmm = read_regs.xmm;
231+
regs.mxcsr = read_regs.mxcsr;
231232

232233
Ok(())
233234
}
@@ -269,6 +270,7 @@ impl SingleThreadBase for HyperlightSandboxTarget {
269270
rip: regs.rip,
270271
rflags: u64::from(regs.eflags),
271272
xmm: regs.xmm,
273+
mxcsr: regs.mxcsr,
272274
};
273275

274276
match self.send_command(DebugMsg::WriteRegisters(regs))? {

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,9 @@ impl Hypervisor for KVMDriver {
591591
mxcsr: MXCSR_DEFAULT,
592592
..Default::default() // zero out the rest
593593
};
594+
595+
// note kvm set_fpu doesn't actually set or read the mxcsr value
596+
// https://elixir.bootlin.com/linux/v6.16/source/arch/x86/kvm/x86.c#L12229
594597
self.vcpu_fd.set_fpu(&fpu)?;
595598

596599
// run

0 commit comments

Comments
 (0)