HyperDbg Debugger
Loading...
Searching...
No Matches
DpcRoutines.h File Reference

headers of all the dpc routines which relates to executing on a single core More...

Go to the source code of this file.

Functions

NTSTATUS DpcRoutineRunTaskOnSingleCore (UINT32 CoreNumber, PVOID Routine, PVOID DeferredContext)
 This function synchronize the function execution for a single core You should only used it for one core, not in multiple threads simultaneously The function that needs to use this feature (Routine parameter function) should have the when it ends :
VOID DpcRoutinePerformWriteMsr (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 Broadcast msr write.
VOID DpcRoutinePerformReadMsr (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 Broadcast msr read.
VOID DpcRoutineWriteMsrToAllCores (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 Broadcast Msr Write.
VOID DpcRoutineReadMsrToAllCores (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 Broadcast Msr read.
VOID DpcRoutineVmExitAndHaltSystemAllCores (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 vm-exit and halt the system
BOOLEAN DpcRoutineSetHardwareDebugRegisters (KDPC *Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
 Broadcast applying hardware debug register.

Detailed Description

headers of all the dpc routines which relates to executing on a single core

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-29

Function Documentation

◆ DpcRoutinePerformReadMsr()

VOID DpcRoutinePerformReadMsr ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Broadcast msr read.

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
VOID
161{
162 ULONG CurrentCore = KeGetCurrentProcessorNumberEx(NULL);
163 PROCESSOR_DEBUGGING_STATE * CurrentDebuggingState = &g_DbgState[CurrentCore];
164
165 UNREFERENCED_PARAMETER(Dpc);
166 UNREFERENCED_PARAMETER(DeferredContext);
167 UNREFERENCED_PARAMETER(SystemArgument1);
168 UNREFERENCED_PARAMETER(SystemArgument2);
169
170 //
171 // read on MSR
172 //
173 CurrentDebuggingState->MsrState.Value = CpuReadMsr((ULONG)CurrentDebuggingState->MsrState.Msr);
174
175 //
176 // As this function is designed for a single,
177 // we have to release the synchronization lock here
178 //
180}
UINT64 CpuReadMsr(ULONG MsrAddress)
Read an MSR.
Definition PlatformIntrinsics.c:213
VOID SpinlockUnlock(volatile LONG *Lock)
Release the lock.
Definition Spinlock.c:162
unsigned long ULONG
Definition BasicTypes.h:31
PROCESSOR_DEBUGGING_STATE * g_DbgState
Save the state and variables related to debugging on each to logical core.
Definition Global.h:17
volatile LONG OneCoreLock
lock for one core execution
Definition DpcRoutines.c:19
struct _PROCESSOR_DEBUGGING_STATE PROCESSOR_DEBUGGING_STATE
Saves the debugger state.
UINT64 Msr
Definition State.h:24
UINT64 Value
Definition State.h:25
PROCESSOR_DEBUGGING_MSR_READ_OR_WRITE MsrState
Definition State.h:175

◆ DpcRoutinePerformWriteMsr()

VOID DpcRoutinePerformWriteMsr ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Broadcast msr write.

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
VOID
129{
130 ULONG CurrentCore = KeGetCurrentProcessorNumberEx(NULL);
131 PROCESSOR_DEBUGGING_STATE * CurrentDebuggingState = &g_DbgState[CurrentCore];
132
133 UNREFERENCED_PARAMETER(Dpc);
134 UNREFERENCED_PARAMETER(DeferredContext);
135 UNREFERENCED_PARAMETER(SystemArgument1);
136 UNREFERENCED_PARAMETER(SystemArgument2);
137
138 //
139 // write on MSR
140 //
141 CpuWriteMsr((ULONG)CurrentDebuggingState->MsrState.Msr, CurrentDebuggingState->MsrState.Value);
142
143 //
144 // As this function is designed for a single,
145 // we have to release the synchronization lock here
146 //
148}
VOID CpuWriteMsr(ULONG MsrAddress, UINT64 MsrValue)
Write an MSR.
Definition PlatformIntrinsics.c:233

◆ DpcRoutineReadMsrToAllCores()

VOID DpcRoutineReadMsrToAllCores ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Broadcast Msr read.

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
VOID
222{
223 ULONG CurrentCore = KeGetCurrentProcessorNumberEx(NULL);
224 PROCESSOR_DEBUGGING_STATE * CurrentDebuggingState = &g_DbgState[CurrentCore];
225
226 UNREFERENCED_PARAMETER(Dpc);
227 UNREFERENCED_PARAMETER(DeferredContext);
228
229 //
230 // read msr
231 //
232 CurrentDebuggingState->MsrState.Value = CpuReadMsr((ULONG)CurrentDebuggingState->MsrState.Msr);
233
234 // ------------------------------------------------------------------------------
235 // Synchronize the end of this routine with the caller
236 //
237 PlatformBroadcastSynchronizeEndOfRoutine(SystemArgument1, SystemArgument2);
238}
VOID PlatformBroadcastSynchronizeEndOfRoutine(PVOID SystemArgument1, PVOID SystemArgument2)
This function synchronize the function execution for a single core.
Definition PlatformBroadcast.c:24

◆ DpcRoutineRunTaskOnSingleCore()

NTSTATUS DpcRoutineRunTaskOnSingleCore ( UINT32 CoreNumber,
PVOID Routine,
PVOID DeferredContext )

This function synchronize the function execution for a single core You should only used it for one core, not in multiple threads simultaneously The function that needs to use this feature (Routine parameter function) should have the when it ends :

         SpinlockUnlock(&OneCoreLock);
Parameters
CoreNumbercore number that the target function should run on it
Routinethe target function that should be ran
DeferredContextan optional parameter to Routine
Returns
NTSTATUS
36{
37 PRKDPC Dpc;
38 ULONG ProcessorsCount;
39
40 ProcessorsCount = KeQueryActiveProcessorCount(0);
41
42 //
43 // Check if the core number is not invalid
44 //
45 if (CoreNumber >= ProcessorsCount)
46 {
47 return STATUS_INVALID_PARAMETER;
48 }
49
50 //
51 // Allocate Memory for DPC
52 //
53 Dpc = PlatformMemAllocateNonPagedPool(sizeof(KDPC));
54
55 if (!Dpc)
56 {
57 return STATUS_INSUFFICIENT_RESOURCES;
58 }
59
60 //
61 // Creating a DPC that will run on the target process
62 //
63 KeInitializeDpc(Dpc, // Dpc
64 (PKDEFERRED_ROUTINE)Routine, // DeferredRoutine
65 DeferredContext // DeferredContext
66 );
67
68 //
69 // Set the target core
70 //
71 KeSetTargetProcessorDpc(Dpc, (CCHAR)CoreNumber);
72
73 //
74 // it's sure will be executed, but we want to free the above
75 // pool, so we have to wait on a spinlock that will be release
76 // by the DPC routine, actually Affinity Thread but that
77 // won't support more than 64 logical cores, I create a discussion
78 // here, and explained the problem, but no one answers
79 // link: https://community.osr.com/discussion/292064/putting-a-barrier-for-dpc-before-continuing-the-rest-of-code
80 // we also can't use the spinlock routine of Windows as this function
81 // raises the IRQL to DPC and we want to execute at DPC, means that
82 // If we're currently on the right core, we never find a chance to
83 // release the spinlock so a deadlock happens, all in all it's complicated :)
84 //
85
86 //
87 // Set the lock to be freed by the other DPC routine
88 //
90 {
91 //
92 // We can't get the lock, probably sth goes wrong !
93 //
96 }
97
98 //
99 // Fire the DPC
100 //
101 KeInsertQueueDpc(Dpc, NULL, NULL);
102
103 //
104 // spin on lock to be release, immediately after we get the lock, we'll
105 // release it for because there is no need to it anymore and DPC is finished
106 //
109
110 //
111 // Now it's safe to deallocate the bugger
112 //
114
115 return STATUS_SUCCESS;
116}
PVOID PlatformMemAllocateNonPagedPool(SIZE_T NumberOfBytes)
Allocates non-paged pool memory.
Definition PlatformMem.c:208
PVOID PlatformMemFreePool(PVOID BufferAddress)
Frees a memory pool.
Definition PlatformMem.c:269
BOOLEAN SpinlockTryLock(volatile LONG *Lock)
Tries to get the lock otherwise returns.
Definition Spinlock.c:41
VOID SpinlockLock(volatile LONG *Lock)
Tries to get the lock and won't return until successfully get the lock.
Definition Spinlock.c:53
#define STATUS_UNSUCCESSFUL
Definition Windows.h:172

◆ DpcRoutineSetHardwareDebugRegisters()

BOOLEAN DpcRoutineSetHardwareDebugRegisters ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Broadcast applying hardware debug register.

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
BOOLEAN
277{
278 UNREFERENCED_PARAMETER(Dpc);
279 UNREFERENCED_PARAMETER(DeferredContext);
280
281 //
282 // Apply hardware debug registers
283 //
284 UdApplyHardwareDebugRegister(DeferredContext);
285
286 // ------------------------------------------------------------------------------
287 // Synchronize the end of this routine with the caller
288 //
289 PlatformBroadcastSynchronizeEndOfRoutine(SystemArgument1, SystemArgument2);
290
291 return TRUE;
292}
VOID UdApplyHardwareDebugRegister(PVOID TargetAddress)
Apply hardware debug registers to all cores.
Definition Ud.c:180
#define TRUE
Definition BasicTypes.h:114

◆ DpcRoutineVmExitAndHaltSystemAllCores()

VOID DpcRoutineVmExitAndHaltSystemAllCores ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

vm-exit and halt the system

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
VOID
251{
252 UNREFERENCED_PARAMETER(Dpc);
253 UNREFERENCED_PARAMETER(DeferredContext);
254
255 //
256 // vm-exit and halt current core
257 //
259
260 // ------------------------------------------------------------------------------
261 // Synchronize the end of this routine with the caller
262 //
263 PlatformBroadcastSynchronizeEndOfRoutine(SystemArgument1, SystemArgument2);
264}
#define DEBUGGER_VMCALL_VM_EXIT_HALT_SYSTEM
VMCALL to cause vm-exit and halt the system.
Definition DebuggerVmcalls.h:22
IMPORT_EXPORT_VMM NTSTATUS VmFuncVmxVmcall(UINT64 VmcallNumber, UINT64 OptionalParam1, UINT64 OptionalParam2, UINT64 OptionalParam3)
Export for running VMX VMCALLs.
Definition Export.c:957

◆ DpcRoutineWriteMsrToAllCores()

VOID DpcRoutineWriteMsrToAllCores ( KDPC * Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Broadcast Msr Write.

Parameters
Dpc
DeferredContext
SystemArgument1
SystemArgument2
Returns
VOID
193{
194 ULONG CurrentCore = KeGetCurrentProcessorNumberEx(NULL);
195 PROCESSOR_DEBUGGING_STATE * CurrentDebuggingState = &g_DbgState[CurrentCore];
196
197 UNREFERENCED_PARAMETER(Dpc);
198 UNREFERENCED_PARAMETER(DeferredContext);
199
200 //
201 // write on MSR
202 //
203 CpuWriteMsr((ULONG)CurrentDebuggingState->MsrState.Msr, CurrentDebuggingState->MsrState.Value);
204
205 // ------------------------------------------------------------------------------
206 // Synchronize the end of this routine with the caller
207 //
208 PlatformBroadcastSynchronizeEndOfRoutine(SystemArgument1, SystemArgument2);
209}