Skip to content

Commit 3bb2294

Browse files
committed
Debug commit for testing
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 3f30312 commit 3bb2294

File tree

4 files changed

+90
-50
lines changed

4 files changed

+90
-50
lines changed

src/hyperlight_host/src/hypervisor/virtual_machine/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ pub enum RegisterError {
446446
},
447447
#[error("Invalid xsave alignment")]
448448
InvalidXsaveAlignment,
449-
#[error("Failed to reset MSRs: {0}")]
450-
ResetMsrs(HypervisorError),
449+
#[error("Failed to reset MSR 0x{index:X}: {source}")]
450+
ResetMsr { index: u32, source: HypervisorError },
451451
#[error("Unknown MSR 0x{0:X}: no hypervisor register mapping")]
452452
UnknownMsr(u32),
453453
#[cfg(target_os = "windows")]

src/hyperlight_host/src/hypervisor/virtual_machine/mshv.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,20 @@ impl VirtualMachine for MshvVm {
404404
})
405405
}
406406

407-
let regs: Vec<hv_register_assoc> = MSRS_TO_RESET
408-
.iter()
409-
.map(|&(msr_index, value)| {
410-
msr_to_hv_reg(msr_index).map(|name| hv_register_assoc {
411-
name,
412-
value: hv_register_value { reg64: value },
413-
..Default::default()
414-
})
415-
})
416-
.collect::<Result<Vec<_>, _>>()?;
417-
418-
self.vcpu_fd
419-
.set_reg(&regs)
420-
.map_err(|e| RegisterError::ResetMsrs(e.into()))?;
407+
for &(msr_index, value) in MSRS_TO_RESET {
408+
let name = msr_to_hv_reg(msr_index)?;
409+
let assoc = hv_register_assoc {
410+
name,
411+
value: hv_register_value { reg64: value },
412+
..Default::default()
413+
};
414+
self.vcpu_fd
415+
.set_reg(&[assoc])
416+
.map_err(|e| RegisterError::ResetMsr {
417+
index: msr_index,
418+
source: e.into(),
419+
})?;
420+
}
421421

422422
Ok(())
423423
}

src/hyperlight_host/src/hypervisor/virtual_machine/whp.rs

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -660,46 +660,85 @@ impl VirtualMachine for WhpVm {
660660
}
661661

662662
fn reset_msrs(&self) -> std::result::Result<(), RegisterError> {
663-
use super::{MSRS_TO_RESET, MSRS_TO_RESET_COUNT};
663+
use super::MSRS_TO_RESET;
664664

665-
/// Convert an MSR index to a WHV_REGISTER_NAME.
665+
/// Map an MSR index to its WHV_REGISTER_NAME.
666666
///
667-
/// The WHV encoding mirrors the HV register namespace with a shifted base:
668-
/// HV MSR registers live at `0x0008_0000 + offset`, WHV maps them to
669-
/// `0x0000_2000 + offset`.
670-
///
671-
/// For MSR indices below 0x4000_0000 the offset equals the MSR index
672-
/// itself. For indices >= 0x4000_0000 the HV encoding packs them into
673-
/// `0x0008_0000 + (msr_index - 0x4000_0000 + 0x0800)`, so the WHV name
674-
/// is `0x2000 + (msr_index - 0x4000_0000 + 0x0800)`.
667+
/// WHV register names are opaque sequential IDs assigned by the
668+
/// hypervisor — there is no arithmetic relationship to MSR indices.
669+
/// We use the named constants from the `windows` crate where
670+
/// available; DebugCtl (0x207D) has no published constant and uses
671+
/// the raw value from the hypervisor source (ValX64RegisterDebugCtl).
675672
const fn msr_index_to_whv_name(msr_index: u32) -> WHV_REGISTER_NAME {
676-
if msr_index < 0x4000_0000 {
677-
WHV_REGISTER_NAME(0x2000 + msr_index as i32)
678-
} else {
679-
WHV_REGISTER_NAME(0x2000_i32 + (msr_index as i32 - 0x4000_0000_i32) + 0x0800_i32)
673+
match msr_index {
674+
0x10 => WHvX64RegisterTsc,
675+
0x174 => WHvX64RegisterSysenterCs,
676+
0x175 => WHvX64RegisterSysenterEsp,
677+
0x176 => WHvX64RegisterSysenterEip,
678+
0x1D9 => WHV_REGISTER_NAME(0x207D), // DebugCtl (no windows crate constant)
679+
0x277 => WHvX64RegisterPat,
680+
0x2FF => WHvX64RegisterMsrMtrrDefType,
681+
// Variable-range MTRRs
682+
0x200 => WHvX64RegisterMsrMtrrPhysBase0,
683+
0x201 => WHvX64RegisterMsrMtrrPhysMask0,
684+
0x202 => WHvX64RegisterMsrMtrrPhysBase1,
685+
0x203 => WHvX64RegisterMsrMtrrPhysMask1,
686+
0x204 => WHvX64RegisterMsrMtrrPhysBase2,
687+
0x205 => WHvX64RegisterMsrMtrrPhysMask2,
688+
0x206 => WHvX64RegisterMsrMtrrPhysBase3,
689+
0x207 => WHvX64RegisterMsrMtrrPhysMask3,
690+
0x208 => WHvX64RegisterMsrMtrrPhysBase4,
691+
0x209 => WHvX64RegisterMsrMtrrPhysMask4,
692+
0x20A => WHvX64RegisterMsrMtrrPhysBase5,
693+
0x20B => WHvX64RegisterMsrMtrrPhysMask5,
694+
0x20C => WHvX64RegisterMsrMtrrPhysBase6,
695+
0x20D => WHvX64RegisterMsrMtrrPhysMask6,
696+
0x20E => WHvX64RegisterMsrMtrrPhysBase7,
697+
0x20F => WHvX64RegisterMsrMtrrPhysMask7,
698+
// Fixed-range MTRRs
699+
0x250 => WHvX64RegisterMsrMtrrFix64k00000,
700+
0x258 => WHvX64RegisterMsrMtrrFix16k80000,
701+
0x259 => WHvX64RegisterMsrMtrrFix16kA0000,
702+
0x268 => WHvX64RegisterMsrMtrrFix4kC0000,
703+
0x269 => WHvX64RegisterMsrMtrrFix4kC8000,
704+
0x26A => WHvX64RegisterMsrMtrrFix4kD0000,
705+
0x26B => WHvX64RegisterMsrMtrrFix4kD8000,
706+
0x26C => WHvX64RegisterMsrMtrrFix4kE0000,
707+
0x26D => WHvX64RegisterMsrMtrrFix4kE8000,
708+
0x26E => WHvX64RegisterMsrMtrrFix4kF0000,
709+
0x26F => WHvX64RegisterMsrMtrrFix4kF8000,
710+
// SYSCALL MSRs
711+
0xC000_0081 => WHvX64RegisterStar,
712+
0xC000_0082 => WHvX64RegisterLstar,
713+
0xC000_0083 => WHvX64RegisterCstar,
714+
0xC000_0084 => WHvX64RegisterSfmask,
715+
0xC000_0102 => WHvX64RegisterKernelGsBase,
716+
0xC000_0103 => WHvX64RegisterTscAux,
717+
// Feature-dependent MSRs
718+
0x48 => WHvX64RegisterSpecCtrl,
719+
0x6A0 => WHvX64RegisterUCet,
720+
0x6A2 => WHvX64RegisterSCet,
721+
0x6A4 => WHvX64RegisterPl0Ssp,
722+
0x6A5 => WHvX64RegisterPl1Ssp,
723+
0x6A6 => WHvX64RegisterPl2Ssp,
724+
0x6A7 => WHvX64RegisterPl3Ssp,
725+
0x6A8 => WHvX64RegisterInterruptSspTableAddr,
726+
0xDA0 => WHvX64RegisterXss,
727+
_ => panic!("MSR index has no WHV register mapping"),
680728
}
681729
}
682730

683-
const REGS: &[(WHV_REGISTER_NAME, Align16<WHV_REGISTER_VALUE>)] = &{
684-
let mut result = [(
685-
WHV_REGISTER_NAME(0),
686-
Align16(WHV_REGISTER_VALUE { Reg64: 0 }),
687-
); MSRS_TO_RESET_COUNT];
688-
let mut i = 0;
689-
while i < MSRS_TO_RESET.len() {
690-
result[i] = (
691-
msr_index_to_whv_name(MSRS_TO_RESET[i].0),
692-
Align16(WHV_REGISTER_VALUE {
693-
Reg64: MSRS_TO_RESET[i].1,
694-
}),
695-
);
696-
i += 1;
697-
}
698-
result
699-
};
700-
701-
self.set_registers(REGS)
702-
.map_err(|e| RegisterError::ResetMsrs(e.into()))?;
731+
for &(msr_index, value) in MSRS_TO_RESET {
732+
let reg = (
733+
msr_index_to_whv_name(msr_index),
734+
Align16(WHV_REGISTER_VALUE { Reg64: value }),
735+
);
736+
self.set_registers(std::slice::from_ref(&reg))
737+
.map_err(|e| RegisterError::ResetMsr {
738+
index: msr_index,
739+
source: e.into(),
740+
})?;
741+
}
703742

704743
Ok(())
705744
}

src/hyperlight_host/tests/integration_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ fn guard_page_check_2() {
665665
});
666666
}
667667

668+
#[ignore]
668669
#[test]
669670
fn execute_on_heap() {
670671
with_rust_sandbox(|mut sbox1| {

0 commit comments

Comments
 (0)