def SetRegisters(self, Registers): '''Set registers in a VP.''' Success, Ret = hvplat.WHvSetVirtualProcessorRegisters( self.Handle, self.Index, Registers) assert Success, 'WHvSetVirtualProcessorRegisters failed with: %s.' % hvplat.WHvReturn( Ret)
def ClearGpaRangeDirtyPages(self, Gpa, RangeSize): '''Clear the dirty bits on a GPA range.''' Success, _, Ret = hvplat.WHvQueryGpaRangeDirtyBitmap( self.Handle, Gpa, RangeSize, True) assert Success, 'WHvQueryGpaRangeDirtyBitmap failed with: %s.' % hvplat.WHvReturn( Ret)
def Run(self): '''Run the virtual processor.''' Success, ExitContext, Ret = hvplat.WHvRunVirtualProcessor( self.Handle, self.Index) assert Success, 'WHvRunVirtualProcessor failed with: %s.' % hvplat.WHvReturn( Ret) return ExitContext, hvplat.WHvExitReason(ExitContext.ExitReason)
def GetPartitionCounters(self, Counter): '''Get a partition performance counter.''' Success, Counters, Ret = hvplat.WHvGetPartitionCounters( self.Handle, Counter) assert Success, 'WHvGetPartitionCounters failed with: %s.' % hvplat.WHvReturn( Ret) return Counters
def QueryGpaRangeDirtyBitmap(self, Gpa, RangeSize): '''Get a list of bits describing which physical guest page is dirty. One bit per page.''' Success, Bits, Ret = hvplat.WHvQueryGpaRangeDirtyBitmap( self.Handle, Gpa, RangeSize) assert Success, 'WHvQueryGpaRangeDirtyBitmap failed with: %s.' % hvplat.WHvReturn( Ret) return Bits
def GetRegisters(self, *Registers): '''Get registers of a VP.''' if isinstance(Registers[0], (tuple, list)): # This is probably the case where GetRegisters is invoked with an # array directly. It is useful at times, so we handle this case. Registers = Registers[0] Success, Registers, Ret = hvplat.WHvGetVirtualProcessorRegisters( self.Handle, self.Index, Registers) assert Success, 'GetRegisters failed with: %s.' % hvplat.WHvReturn(Ret) return Registers
def GetVpCounters(self, Counter): '''Get a virtual processor performance counter.''' Success, Counters, Ret = hvplat.WHvGetVirtualProcessorCounters( self.Handle, self.Index, Counter) assert Success, 'WHvGetVirtualProcessorCounters failed with: %s.' % hvplat.WHvReturn( Ret) Result = { whv.WHvProcessorCounterSetRuntime: Counters.Runtime, whv.WHvProcessorCounterSetIntercepts: Counters.Intercepts, whv.WHvProcessorCounterSetEvents: Counters.GuestEvents, whv.WHvProcessorCounterSetApic: Counters.Apic }.get(Counter, None) assert Result is not None, 'Counter(%x) has not been added to WHV_PROCESSOR_ALL_COUNTERS?' % Counter return Result
def TranslateGva(self, Gva, Flags=None): '''Translate a GVA into a GPA.''' if Flags is None: Flags = 're' WHvFlags = whv.WHvTranslateGvaFlagNone if 'r' in Flags: WHvFlags |= whv.WHvTranslateGvaFlagValidateRead if 'w' in Flags: WHvFlags |= whv.WHvTranslateGvaFlagValidateWrite if 'x' in Flags: WHvFlags |= whv.WHvTranslateGvaFlagValidateExecute if 'e' in Flags: WHvFlags |= whv.WHvTranslateGvaFlagPrivilegeExempt Success, ResultCode, Gpa, Ret = hvplat.WHvTranslateGva( self.Handle, self.Index, Gva, WHvFlags) assert Success, 'WHvTranslateGva failed with: %s.' % hvplat.WHvReturn( Ret) return (hvplat.WHvTranslateGvaResultCode(ResultCode), Gpa)
def MapGpaRangeWithoutContent(self, Gpa, SizeInBytes, Flags): '''Map a GPA range in the partition. This takes care of allocating memory in the host and mapping it in the guest.''' SizeInBytes = utils.Align2Page(SizeInBytes) Hva = VirtualAlloc(0, SizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) assert Hva is not None, 'VirtualAlloc failed.' if 'd' not in Flags: # Force the 'd' dirty flag that is used for save/restore. Flags += 'd' WHvFlags = whv.WHvMapGpaRangeFlagNone if 'r' in Flags: WHvFlags |= whv.WHvMapGpaRangeFlagRead if 'w' in Flags: WHvFlags |= whv.WHvMapGpaRangeFlagWrite if 'x' in Flags: WHvFlags |= whv.WHvMapGpaRangeFlagExecute if 'd' in Flags: WHvFlags |= whv.WHvMapGpaRangeFlagTrackDirtyPages Success, Ret = hvplat.WHvMapGpaRange(self.Handle, Hva, Gpa, SizeInBytes, WHvFlags) assert Success, 'WHvMapGpaRange failed with: %s.' % hvplat.WHvReturn( Ret) # Break the range into a series of pages for the translation table. for Idx in range(SizeInBytes / 0x1000): CurGpa = Gpa + (Idx * 0x1000) CurHva = Hva + (Idx * 0x1000) self.TranslationTable[CurGpa] = TranslationTableEntry_t( CurGpa, CurHva, Flags) return (Hva, Gpa, SizeInBytes)