@@ -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 }
0 commit comments