HyperDbg Debugger
Loading...
Searching...
No Matches
HaltedCore.c File Reference

Implementation of applying events in halted cores. More...

#include "pch.h"

Functions

VOID HaltedCoreTaskTest (PROCESSOR_DEBUGGING_STATE *DbgState, PVOID Context)
 Perform the test task on halted core.
 
VOID HaltedCorePerformTargetTask (PROCESSOR_DEBUGGING_STATE *DbgState, UINT64 TargetTask, PVOID Context)
 Perform the task on halted core.
 
VOID HaltedCoreApplyTaskOnTargetCore (UINT32 TargetCoreId, UINT64 TargetTask, BOOLEAN LockAgainAfterTask, PVOID Context)
 Run the task on a single halted core.
 
VOID HaltedCoreRunTaskOnSingleCore (UINT32 TargetCoreId, UINT64 TargetTask, BOOLEAN LockAgainAfterTask, PVOID Context)
 Run the task on a single halted core.
 
BOOLEAN HaltedCoreBroadcastTaskAllCores (PROCESSOR_DEBUGGING_STATE *DbgState, UINT64 TargetTask, BOOLEAN LockAgainAfterTask, BOOLEAN Synchronize, PVOID Context)
 Broadcast tasks to halted cores.
 

Detailed Description

Implementation of applying events in halted cores.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.7
Date
2023-09-30

Function Documentation

◆ HaltedCoreApplyTaskOnTargetCore()

VOID HaltedCoreApplyTaskOnTargetCore ( UINT32 TargetCoreId,
UINT64 TargetTask,
BOOLEAN LockAgainAfterTask,
PVOID Context )

Run the task on a single halted core.

This function should be called from VMX root-mode

Parameters
TargetCoreIdThe target core's ID (to just run on this core)
TargetTaskThe target task
LockAgainAfterTaskLock the core after the task
Contextoptional parameter passed to the functions
Returns
VOID
325{
326 PROCESSOR_DEBUGGING_STATE * DbgState = &g_DbgState[TargetCoreId];
327
328 //
329 // Activate running the halted task
330 //
332
333 DbgState->HaltedCoreTask.KernelStatus = (UINT64)NULL;
334 DbgState->HaltedCoreTask.LockAgainAfterTask = LockAgainAfterTask;
335 DbgState->HaltedCoreTask.TargetTask = TargetTask;
336 DbgState->HaltedCoreTask.Context = Context;
337
338 //
339 // Unlock halted core
340 //
341 KdUnlockTheHaltedCore(DbgState);
342}
#define TRUE
Definition BasicTypes.h:55
unsigned __int64 UINT64
Definition BasicTypes.h:21
VOID KdUnlockTheHaltedCore(PROCESSOR_DEBUGGING_STATE *DbgState)
unlock the target core
Definition Kd.c:1812
NTKERNELAPI _In_opt_ PVOID Context
Definition Dpc.h:25
PROCESSOR_DEBUGGING_STATE * g_DbgState
Save the state and variables related to debugging on each to logical core.
Definition Global.h:17
BOOLEAN LockAgainAfterTask
Definition State.h:139
UINT64 KernelStatus
Definition State.h:142
PVOID Context
Definition State.h:141
BOOLEAN PerformHaltedTask
Definition State.h:138
UINT64 TargetTask
Definition State.h:140
Saves the debugger state.
Definition State.h:165
DEBUGGEE_HALTED_CORE_TASK HaltedCoreTask
Definition State.h:180

◆ HaltedCoreBroadcastTaskAllCores()

BOOLEAN HaltedCoreBroadcastTaskAllCores ( PROCESSOR_DEBUGGING_STATE * DbgState,
UINT64 TargetTask,
BOOLEAN LockAgainAfterTask,
BOOLEAN Synchronize,
PVOID Context )

Broadcast tasks to halted cores.

This function should be called from VMX root-mode

Parameters
DbgStateThe state of the debugger on the current core
TargetTaskThe target task
LockAgainAfterTaskLock the core after the task
SynchronizeWhether the function should wait for all cores to synchronize and lock again or not
Contextoptional parameter passed to the functions
Returns
BOOLEAN
404{
405 ULONG ProcessorsCount;
406
407 ProcessorsCount = KeQueryActiveProcessorCount(0);
408
409 //
410 // Synchronization is not possible when the locking after the task is
411 // not expected
412 //
413 if (Synchronize && !LockAgainAfterTask)
414 {
415 LogWarning("Synchronization is not possible when the locking after the task is not expected");
416 return FALSE;
417 }
418
419 //
420 // Apply the task to all cores except current core
421 //
422 for (UINT32 i = 0; i < ProcessorsCount; i++)
423 {
424 if (DbgState->CoreId != i)
425 {
426 //
427 // apply task to the target core
428 //
429 HaltedCoreApplyTaskOnTargetCore(i, TargetTask, LockAgainAfterTask, Context);
430 }
431 else
432 {
433 //
434 // Perform the task for the current core
435 //
436 HaltedCorePerformTargetTask(DbgState, TargetTask, Context);
437 }
438 }
439
440 //
441 // If synchronization is expected, we need to check to make sure
442 // all cores are synchronized (locked) at this point or not
443 //
444 if (Synchronize)
445 {
446 for (size_t i = 0; i < ProcessorsCount; i++)
447 {
448 if (DbgState->CoreId != i)
449 {
450 //
451 // Wait until the core is locked again
452 //
453 while (TRUE)
454 {
455 //
456 // Keep checking to make sure the target core finished the
457 // execution its task and locked again
458 //
460 {
461 continue;
462 }
463 else
464 {
465 break;
466 }
467 }
468 }
469 }
470 }
471
472 //
473 // All cores locked again
474 //
475 return TRUE;
476}
#define FALSE
Definition BasicTypes.h:54
unsigned int UINT32
Definition BasicTypes.h:48
unsigned long ULONG
Definition BasicTypes.h:37
VOID HaltedCoreApplyTaskOnTargetCore(UINT32 TargetCoreId, UINT64 TargetTask, BOOLEAN LockAgainAfterTask, PVOID Context)
Run the task on a single halted core.
Definition HaltedCore.c:321
VOID HaltedCorePerformTargetTask(PROCESSOR_DEBUGGING_STATE *DbgState, UINT64 TargetTask, PVOID Context)
Perform the task on halted core.
Definition HaltedCore.c:45
#define LogWarning(format,...)
Log in the case of warning.
Definition HyperDbgHyperLogIntrinsics.h:99
BOOLEAN KdCheckTheHaltedCore(PROCESSOR_DEBUGGING_STATE *DbgState)
check the lock state of the target core
Definition Kd.c:1825
UINT32 CoreId
Definition State.h:169

◆ HaltedCorePerformTargetTask()

VOID HaltedCorePerformTargetTask ( PROCESSOR_DEBUGGING_STATE * DbgState,
UINT64 TargetTask,
PVOID Context )

Perform the task on halted core.

This function should be called from VMX root-mode

Parameters
DbgStateThe state of the debugger on the current core
TargetTaskThe target task
Contextoptional parameter passed to the functions
Returns
VOID
48{
49 switch (TargetTask)
50 {
52 {
53 //
54 // Perform the test task
55 //
56 HaltedCoreTaskTest(DbgState, Context);
57 break;
58 }
59
61 {
62 //
63 // Call the direct VMCALL test function
64 //
66
67 break;
68 }
70 {
71 //
72 // Enable process change detection
73 //
75
76 break;
77 }
79 {
80 //
81 // Enable alert for thread changes
82 //
84
85 break;
86 }
88 {
89 //
90 // Change MSR bitmap for read (RDMSR)
91 //
93
94 break;
95 }
97 {
98 //
99 // Change MSR bitmap for write (WRMSR)
100 //
102
103 break;
104 }
106 {
107 //
108 // Change I/O bitmap
109 //
111
112 break;
113 }
115 {
116 //
117 // Enable rdpmc exiting
118 //
120
121 break;
122 }
124 {
125 //
126 // Enable rdtsc/rdtscp exiting
127 //
129
130 break;
131 }
133 {
134 //
135 // Enable mov to debug registers exiting
136 //
138
139 break;
140 }
142 {
143 //
144 // Set exception bitmap
145 //
147
148 break;
149 }
151 {
152 //
153 // enable external interrupt exiting
154 //
156
157 break;
158 }
160 {
161 //
162 // enable mov to CR exiting
163 //
165
166 break;
167 }
169 {
170 //
171 // enable syscall hook using EFER SCE bit
172 //
174
175 break;
176 }
178 {
179 //
180 // invalidate EPT (All Contexts)
181 //
183
184 break;
185 }
187 {
188 //
189 // invalidate EPT (A Single Context)
190 //
192
193 break;
194 }
196 {
197 //
198 // unset exception bitmap on VMCS
199 //
201
202 break;
203 }
205 {
206 //
207 // restore a single EPT entry and invalidate EPT cache
208 //
210
211 break;
212 }
214 {
215 //
216 // disable external interrupt exiting only to clear !interrupt commands
217 //
219
220 break;
221 }
223 {
224 //
225 // reset MSR Bitmap Read
226 //
228
229 break;
230 }
232 {
233 //
234 // reset MSR Bitmap Write
235 //
237
238 break;
239 }
241 {
242 //
243 // reset exception bitmap on VMCS
244 //
246
247 break;
248 }
250 {
251 //
252 // reset I/O Bitmaps (A & B)
253 //
255
256 break;
257 }
259 {
260 //
261 // clear rdtsc exiting bit ONLY in the case of disabling the events for !tsc command
262 //
264
265 break;
266 }
268 {
269 //
270 // disable rdpmc exiting in primary cpu-based controls
271 //
273
274 break;
275 }
277 {
278 //
279 // disable syscall hook using EFER SCE bit
280 //
282
283 break;
284 }
286 {
287 //
288 // clear mov 2 hw dr exiting bit ONLY in the case of disabling the events for !dr command
289 //
291
292 break;
293 }
295 {
296 //
297 // clear mov 2 cr exiting bit ONLY in the case of disabling the events for !crwrite command
298 //
300
301 break;
302 }
303 default:
304 LogWarning("Warning, unknown broadcast on halted core received");
305 break;
306 }
307}
NTSTATUS DirectVmcallResetExceptionBitmapOnlyOnClearingExceptionEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for resetting exception bitmap on VMCS
Definition DirectVmcall.c:407
NTSTATUS DirectVmcallEnableMovToCrExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling mov to CR exiting
Definition DirectVmcall.c:217
NTSTATUS DirectVmcallDisableRdtscExitingForClearingTscEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for clearing rdtsc exiting bit ONLY in the case of disabling the events for !...
Definition DirectVmcall.c:446
NTSTATUS DirectVmcallInvalidateSingleContext(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for invalidating EPT (A Single Context)
Definition DirectVmcall.c:293
NTSTATUS DirectVmcallUnsetExceptionBitmap(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for unsetting exception bitmap on VMCS
Definition DirectVmcall.c:312
NTSTATUS DirectVmcallResetMsrBitmapWrite(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for resetting MSR Bitmap Write
Definition DirectVmcall.c:388
NTSTATUS DirectVmcallInvalidateEptAllContexts(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for invalidating EPT (All Contexts)
Definition DirectVmcall.c:274
NTSTATUS DirectVmcallChangeMsrBitmapWrite(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for changing MSR Bitmap (Write)
Definition DirectVmcall.c:84
NTSTATUS DirectVmcallChangeMsrBitmapRead(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for changing MSR Bitmap (Read)
Definition DirectVmcall.c:65
NTSTATUS DirectVmcallSetDisableExternalInterruptExitingOnlyOnClearingInterruptEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for disabling external interrupt exiting only to clear !interrupt commands
Definition DirectVmcall.c:350
NTSTATUS DirectVmcallSetExceptionBitmap(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for setting exception bitmap
Definition DirectVmcall.c:179
NTSTATUS DirectVmcallEnableRdtscpExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling rdtsc/rdtscp exiting
Definition DirectVmcall.c:141
NTSTATUS DirectVmcallUnhookSinglePage(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for restoring a single EPT entry and invalidating EPT cache
Definition DirectVmcall.c:331
NTSTATUS DirectVmcallEnableEferSyscall(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling syscall hook using EFER SCE bit
Definition DirectVmcall.c:236
NTSTATUS DirectVmcallDisableMov2DrExitingForClearingDrEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for clearing mov 2 hw dr exiting bit ONLY in the case of disabling the events for !...
Definition DirectVmcall.c:504
NTSTATUS DirectVmcallResetIoBitmap(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for resetting I/O Bitmaps (A & B)
Definition DirectVmcall.c:426
NTSTATUS DirectVmcallTest(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for test direct VMCALL
Definition DirectVmcall.c:25
NTSTATUS DirectVmcallResetMsrBitmapRead(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for resetting MSR Bitmap Read
Definition DirectVmcall.c:369
NTSTATUS DirectVmcallEnableRdpmcExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling rdpmc exiting
Definition DirectVmcall.c:122
NTSTATUS DirectVmcallEnableMov2DebugRegsExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling mov to debug registers exiting
Definition DirectVmcall.c:160
NTSTATUS DirectVmcallDisableMov2CrExitingForClearingCrEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for clearing mov 2 cr exiting bit ONLY in the case of disabling the events for !...
Definition DirectVmcall.c:524
NTSTATUS DirectVmcallChangeIoBitmap(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for changing IO Bitmap
Definition DirectVmcall.c:103
NTSTATUS DirectVmcallDisableEferSyscallEvents(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for disabling syscall hook using EFER SCE bit
Definition DirectVmcall.c:484
NTSTATUS DirectVmcallDisableRdpmcExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for disabling rdpmc exiting in primary cpu-based controls
Definition DirectVmcall.c:465
NTSTATUS DirectVmcallEnableExternalInterruptExiting(UINT32 CoreId, DIRECT_VMCALL_PARAMETERS *DirectVmcallOptions)
routines for enabling external interrupt exiting
Definition DirectVmcall.c:198
VOID HaltedCoreTaskTest(PROCESSOR_DEBUGGING_STATE *DbgState, PVOID Context)
Perform the test task on halted core.
Definition HaltedCore.c:24
#define DEBUGGER_HALTED_CORE_TASK_SET_EXCEPTION_BITMAP
Halted core task for setting exception bitmap.
Definition HaltedCore.h:82
#define DEBUGGER_HALTED_CORE_TASK_SET_RDTSC_EXITING
Halted core task for enabling rdtsc/rdtscp exiting.
Definition HaltedCore.h:70
#define DEBUGGER_HALTED_CORE_TASK_UNSET_RDPMC_EXITING
Halted core task for disabling rdpmc exiting in primary cpu-based controls.
Definition HaltedCore.h:167
#define DEBUGGER_HALTED_CORE_TASK_SET_PROCESS_INTERCEPTION
Halted core task for setting process interception.
Definition HaltedCore.h:34
#define DEBUGGER_HALTED_CORE_TASK_RESET_IO_BITMAP
Halted core task for resetting I/O Bitmaps (A & B)
Definition HaltedCore.h:154
#define DEBUGGER_HALTED_CORE_TASK_ENABLE_EXTERNAL_INTERRUPT_EXITING
Halted core task for enabling external interrupt exiting.
Definition HaltedCore.h:88
#define DEBUGGER_HALTED_CORE_TASK_DISABLE_RDTSC_EXITING_ONLY_FOR_TSC_EVENTS
Halted core task for clearing rdtsc exiting bit ONLY in the case of disabling the events for !...
Definition HaltedCore.h:161
#define DEBUGGER_HALTED_CORE_TASK_DISABLE_MOV_TO_CR_EXITING_ONLY_FOR_CR_EVENTS
Halted core task for clearing mov 2 cr exiting bit ONLY in the case of disabling the events for !...
Definition HaltedCore.h:187
#define DEBUGGER_HALTED_CORE_TASK_ENABLE_MOV_TO_DEBUG_REGS_EXITING
Halted core task for enabling mov to debug registers exiting.
Definition HaltedCore.h:76
#define DEBUGGER_HALTED_CORE_TASK_UNHOOK_SINGLE_PAGE
Halted core task for restoring a single EPT entry and invalidating EPT cache.
Definition HaltedCore.h:124
#define DEBUGGER_HALTED_CORE_TASK_SET_RDPMC_EXITING
Halted core task for enabling rdpmc exiting.
Definition HaltedCore.h:64
#define DEBUGGER_HALTED_CORE_TASK_RUN_VMCALL
Halted core task for running VMCALLs.
Definition HaltedCore.h:28
#define DEBUGGER_HALTED_CORE_TASK_DISABLE_MOV_TO_HW_DR_EXITING_ONLY_FOR_DR_EVENTS
Halted core task for clearing mov 2 hw dr exiting bit ONLY in the case of disabling the events for !...
Definition HaltedCore.h:180
#define DEBUGGER_HALTED_CORE_TASK_RESET_EXCEPTION_BITMAP_ONLY_ON_CLEARING_EXCEPTION_EVENTS
Halted core task for resetting exception bitmap on VMCS.
Definition HaltedCore.h:148
#define DEBUGGER_HALTED_CORE_TASK_RESET_MSR_BITMAP_READ
Halted core task for resetting MSR Bitmap Read.
Definition HaltedCore.h:136
#define DEBUGGER_HALTED_CORE_TASK_INVEPT_ALL_CONTEXTS
Halted core task for invalidating EPT (All Contexts)
Definition HaltedCore.h:106
#define DEBUGGER_HALTED_CORE_TASK_DISABLE_EXTERNAL_INTERRUPT_EXITING_ONLY_TO_CLEAR_INTERRUPT_COMMANDS
Halted core task for disabling external interrupt exiting only to clear !interrupt commands.
Definition HaltedCore.h:130
#define DEBUGGER_HALTED_CORE_TASK_TEST
Halted core task for testing purpose.
Definition HaltedCore.h:22
#define DEBUGGER_HALTED_CORE_TASK_CHANGE_IO_BITMAP
Halted core task for changing I/O Bitmaps (A & B)
Definition HaltedCore.h:58
#define DEBUGGER_HALTED_CORE_TASK_INVEPT_SINGLE_CONTEXT
Halted core task for invalidating EPT (A Single Context)
Definition HaltedCore.h:112
#define DEBUGGER_HALTED_CORE_TASK_SET_THREAD_INTERCEPTION
Halted core task for setting thread interception.
Definition HaltedCore.h:40
#define DEBUGGER_HALTED_CORE_TASK_ENABLE_SYSCALL_HOOK_EFER
Halted core task for enabling syscall hook using EFER SCE bit.
Definition HaltedCore.h:100
#define DEBUGGER_HALTED_CORE_TASK_ENABLE_MOV_TO_CONTROL_REGS_EXITING
Halted core task for enabling mov to CR exiting.
Definition HaltedCore.h:94
#define DEBUGGER_HALTED_CORE_TASK_UNSET_EXCEPTION_BITMAP
Halted core task for unsetting exception bitmap on VMCS.
Definition HaltedCore.h:118
#define DEBUGGER_HALTED_CORE_TASK_DISABLE_SYSCALL_HOOK_EFER
Halted core task for disabling syscall hook using EFER SCE bit.
Definition HaltedCore.h:173
#define DEBUGGER_HALTED_CORE_TASK_CHANGE_MSR_BITMAP_WRITE
Halted core task for changing MSR Bitmap Write.
Definition HaltedCore.h:52
#define DEBUGGER_HALTED_CORE_TASK_CHANGE_MSR_BITMAP_READ
Halted core task for changing MSR Bitmap Read.
Definition HaltedCore.h:46
#define DEBUGGER_HALTED_CORE_TASK_RESET_MSR_BITMAP_WRITE
Halted core task for resetting MSR Bitmap Write.
Definition HaltedCore.h:142
#define PVOID_TO_BOOLEAN(_var)
Definition MetaMacros.h:40
VOID ProcessEnableOrDisableThreadChangeMonitor(PROCESSOR_DEBUGGING_STATE *DbgState, BOOLEAN Enable, BOOLEAN IsSwitchByClockIntrrupt)
Enable or disable the process change monitoring detection on the running core.
Definition Process.c:220
VOID ThreadEnableOrDisableThreadChangeMonitor(PROCESSOR_DEBUGGING_STATE *DbgState, BOOLEAN Enable, BOOLEAN IsSwitchByClockIntrrupt)
Enable or disable the thread change monitoring detection on the running core.
Definition Thread.c:597
Used for sending direct VMCALLs on the VMX root-mode.
Definition DataTypes.h:294

◆ HaltedCoreRunTaskOnSingleCore()

VOID HaltedCoreRunTaskOnSingleCore ( UINT32 TargetCoreId,
UINT64 TargetTask,
BOOLEAN LockAgainAfterTask,
PVOID Context )

Run the task on a single halted core.

This function should be called from VMX root-mode

Parameters
TargetCoreIdThe target core's ID (to just run on this core)
TargetTaskThe target task
LockAgainAfterTaskLock the core after the task
Contextoptional parameter passed to the functions
Returns
VOID
360{
361 //
362 // Check if the task needs to be executed for the current
363 // core or any other cores
364 //
365 if (TargetCoreId == KeGetCurrentProcessorNumberEx(NULL))
366 {
367 //
368 // *** Perform the task for the current core ***
369 //
370 HaltedCorePerformTargetTask(&g_DbgState[TargetCoreId], TargetTask, Context);
371 }
372 else
373 {
374 //
375 // *** Perform the task for another core ***
376 //
377
378 //
379 // apply task to the target core
380 //
381 HaltedCoreApplyTaskOnTargetCore(TargetCoreId, TargetTask, LockAgainAfterTask, Context);
382 }
383}

◆ HaltedCoreTaskTest()

VOID HaltedCoreTaskTest ( PROCESSOR_DEBUGGING_STATE * DbgState,
PVOID Context )

Perform the test task on halted core.

Parameters
DbgStateThe state of the debugger on the current core
Contextoptional parameter passed to the functions
Returns
VOID
25{
26 UNREFERENCED_PARAMETER(DbgState);
27
28 //
29 // Test target task
30 //
31 LogInfo("Target test task executed on halted core, context: %llx", Context);
32}
#define LogInfo(format,...)
Define log variables.
Definition HyperDbgHyperLogIntrinsics.h:71