2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/kd64/kdinit.c
5 * PURPOSE: KD64 Initialization Code
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Stefan Ginsberg (stefan.ginsberg@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* FUNCTIONS *****************************************************************/
20 KdUpdateDataBlock(VOID
)
22 /* Update the KeUserCallbackDispatcher pointer */
23 KdDebuggerDataBlock
.KeUserCallbackDispatcher
=
24 (ULONG_PTR
)KeUserCallbackDispatcher
;
29 KdRegisterDebuggerDataBlock(IN ULONG Tag
,
30 IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader
,
34 PLIST_ENTRY NextEntry
;
35 PDBGKD_DEBUG_DATA_HEADER64 CurrentHeader
;
37 /* Acquire the Data Lock */
38 KeAcquireSpinLock(&KdpDataSpinLock
, &OldIrql
);
40 /* Loop the debugger data list */
41 NextEntry
= KdpDebuggerDataListHead
.Flink
;
42 while (NextEntry
!= &KdpDebuggerDataListHead
)
44 /* Get the header for this entry */
45 CurrentHeader
= CONTAINING_RECORD(NextEntry
,
46 DBGKD_DEBUG_DATA_HEADER64
,
49 /* Move to the next one */
50 NextEntry
= NextEntry
->Flink
;
52 /* Check if we already have this data block */
53 if ((CurrentHeader
== DataHeader
) || (CurrentHeader
->OwnerTag
== Tag
))
55 /* Release the lock and fail */
56 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
61 /* Setup the header */
62 DataHeader
->OwnerTag
= Tag
;
63 DataHeader
->Size
= Size
;
65 /* Insert it into the list and release the lock */
66 InsertTailList(&KdpDebuggerDataListHead
, (PLIST_ENTRY
)&DataHeader
->List
);
67 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
74 KdInitSystem(IN ULONG BootPhase
,
75 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
77 BOOLEAN EnableKd
, DisableKdAfterInit
= FALSE
, BlockEnable
;
78 LPSTR CommandLine
, DebugLine
, DebugOptionStart
, DebugOptionEnd
;
80 PLDR_DATA_TABLE_ENTRY LdrEntry
;
81 PLIST_ENTRY NextEntry
;
82 ULONG i
, j
, Length
, DebugOptionLength
;
91 /* Check if this is Phase 1 */
94 /* Just query the performance counter */
95 KeQueryPerformanceCounter(&KdPerformanceCounterRate
);
99 /* Check if we already initialized once */
100 if (KdDebuggerEnabled
) return TRUE
;
102 /* Set the Debug Routine as the Stub for now */
103 KiDebugRoutine
= KdpStub
;
105 /* Disable break after symbol load for now */
106 KdBreakAfterSymbolLoad
= FALSE
;
108 /* Check if the Debugger Data Block was already initialized */
109 if (!KdpDebuggerDataListHead
.Flink
)
111 /* It wasn't...Initialize the KD Data Listhead */
112 InitializeListHead(&KdpDebuggerDataListHead
);
114 /* Register the Debugger Data Block */
115 KdRegisterDebuggerDataBlock(KDBG_TAG
,
116 &KdDebuggerDataBlock
.Header
,
117 sizeof(KdDebuggerDataBlock
));
119 /* Fill out the KD Version Block */
120 KdVersionBlock
.MajorVersion
= (USHORT
)((DBGKD_MAJOR_NT
<< 8) | (NtBuildNumber
>> 28));
121 KdVersionBlock
.MinorVersion
= (USHORT
)(NtBuildNumber
& 0xFFFF);
124 /* This is an MP Build */
125 KdVersionBlock
.Flags
|= DBGKD_VERS_FLAG_MP
;
128 /* Save Pointers to Loaded Module List and Debugger Data */
129 KdVersionBlock
.PsLoadedModuleList
= (ULONG64
)(LONG_PTR
)&PsLoadedModuleList
;
130 KdVersionBlock
.DebuggerDataList
= (ULONG64
)(LONG_PTR
)&KdpDebuggerDataListHead
;
132 /* Set protocol limits */
133 KdVersionBlock
.MaxStateChange
= DbgKdMaximumStateChange
-
134 DbgKdMinimumStateChange
;
135 KdVersionBlock
.MaxManipulate
= DbgKdMaximumManipulate
-
136 DbgKdMinimumManipulate
;
137 KdVersionBlock
.Unused
[0] = 0;
139 /* Link us in the KPCR */
140 KeGetPcr()->KdVersionBlock
= &KdVersionBlock
;
143 /* Check if we have a loader block */
146 /* Get the image entry */
147 LdrEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
148 LDR_DATA_TABLE_ENTRY
,
151 /* Save the Kernel Base */
152 PsNtosImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
153 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)LdrEntry
->DllBase
;
155 /* Check if we have a command line */
156 CommandLine
= LoaderBlock
->LoadOptions
;
160 _strupr(CommandLine
);
162 /* Assume we'll disable KD */
165 /* Check for CRASHDEBUG, NODEBUG and just DEBUG */
166 if (strstr(CommandLine
, "CRASHDEBUG"))
168 /* Don't enable KD now, but allow it to be enabled later */
169 KdPitchDebugger
= FALSE
;
171 else if (strstr(CommandLine
, "NODEBUG"))
173 /* Don't enable KD and don't let it be enabled later */
174 KdPitchDebugger
= TRUE
;
176 else if ((DebugLine
= strstr(CommandLine
, "DEBUG")) != NULL
)
181 /* Check if there are any options */
182 if (DebugLine
[5] == '=')
185 DebugOptionStart
= DebugOptionEnd
= &DebugLine
[6];
187 /* Scan the string for debug options */
190 /* Loop until we reach the end of the string */
191 while (*DebugOptionEnd
!= ANSI_NULL
)
193 /* Check if this is a comma, a space or a tab */
194 if ((*DebugOptionEnd
== ',') ||
195 (*DebugOptionEnd
== ' ') ||
196 (*DebugOptionEnd
== ' '))
199 * We reached the end of the option or
200 * the end of the string, break out
206 /* Move on to the next character */
211 /* Calculate the length of the current option */
212 DebugOptionLength
= ((ULONG_PTR
)DebugOptionEnd
-
213 (ULONG_PTR
)DebugOptionStart
);
216 * Break out if we reached the last option
217 * or if there were no options at all
219 if (!DebugOptionLength
) break;
221 /* Now check which option this is */
222 if ((DebugOptionLength
== 10) &&
223 !(strncmp(DebugOptionStart
, "AUTOENABLE", 10)))
226 * Disable the debugger, but
227 * allow it to be reenabled
229 DisableKdAfterInit
= TRUE
;
231 KdAutoEnableOnEvent
= TRUE
;
233 else if ((DebugOptionLength
== 7) &&
234 !(strncmp(DebugOptionStart
, "DISABLE", 7)))
236 /* Disable the debugger */
237 DisableKdAfterInit
= TRUE
;
239 KdAutoEnableOnEvent
= FALSE
;
241 else if ((DebugOptionLength
== 6) &&
242 !(strncmp(DebugOptionStart
, "NOUMEX", 6)))
244 /* Ignore user mode exceptions */
245 KdIgnoreUmExceptions
= TRUE
;
249 * If there are more options then
250 * the next character should be a comma
252 if (*DebugOptionEnd
!= ',')
254 /* It isn't, break out */
258 /* Move on to the next option */
260 DebugOptionStart
= DebugOptionEnd
;
267 /* No command line options? Disable debugger by default */
268 KdPitchDebugger
= TRUE
;
274 /* Called from a bugcheck or a re-enable. Save the Kernel Base */
275 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)PsNtosImageBase
;
277 /* Unconditionally enable KD */
281 /* Set the Kernel Base in the Data Block */
282 KdDebuggerDataBlock
.KernBase
= (ULONG_PTR
)KdVersionBlock
.KernBase
;
284 /* Initialize the debugger if requested */
285 if ((EnableKd
) && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock
))))
287 /* Now set our real KD routine */
288 KiDebugRoutine
= KdpTrap
;
290 /* Check if we've already initialized our structures */
291 if (!KdpDebuggerStructuresInitialized
)
293 /* Set the Debug Switch Routine and Retries*/
294 KdpContext
.KdpDefaultRetries
= 20;
295 KiDebugSwitchRoutine
= KdpSwitchProcessor
;
297 /* Initialize the Time Slip DPC */
298 KeInitializeDpc(&KdpTimeSlipDpc
, KdpTimeSlipDpcRoutine
, NULL
);
299 KeInitializeTimer(&KdpTimeSlipTimer
);
300 ExInitializeWorkItem(&KdpTimeSlipWorkItem
, KdpTimeSlipWork
, NULL
);
302 /* First-time initialization done! */
303 KdpDebuggerStructuresInitialized
= TRUE
;
306 /* Initialize the timer */
307 KdTimerStart
.QuadPart
= 0;
309 /* Officially enable KD */
310 KdPitchDebugger
= FALSE
;
311 KdDebuggerEnabled
= TRUE
;
313 /* Let user-mode know that it's enabled as well */
314 #undef KdDebuggerEnabled
315 SharedUserData
->KdDebuggerEnabled
= TRUE
;
316 #define KdDebuggerEnabled _KdDebuggerEnabled
318 /* Check if the debugger should be disabled initially */
319 if (DisableKdAfterInit
)
322 KdDisableDebuggerWithLock(FALSE
);
325 * Save the enable block state and return initialized
326 * (the debugger is active but disabled).
328 KdBlockEnable
= BlockEnable
;
332 /* Check if we have a loader block */
335 /* Loop boot images */
336 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
338 while ((NextEntry
!= &LoaderBlock
->LoadOrderListHead
) && (i
< 2))
340 /* Get the image entry */
341 LdrEntry
= CONTAINING_RECORD(NextEntry
,
342 LDR_DATA_TABLE_ENTRY
,
345 /* Generate the image name */
346 Name
= LdrEntry
->FullDllName
.Buffer
;
347 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
351 /* Do cheap Unicode to ANSI conversion */
352 NameBuffer
[j
++] = (CHAR
)*Name
++;
353 } while (j
< Length
);
356 NameBuffer
[j
] = ANSI_NULL
;
358 /* Load symbols for image */
359 RtlInitString(&ImageName
, NameBuffer
);
360 DbgLoadImageSymbols(&ImageName
,
362 (ULONG_PTR
)ZwCurrentProcess());
364 /* Go to the next entry */
365 NextEntry
= NextEntry
->Flink
;
370 /* Check for incoming breakin and break on symbol load if we have it*/
371 KdBreakAfterSymbolLoad
= KdPollBreakIn();
375 /* Disable debugger */
376 KdDebuggerNotPresent
= TRUE
;
379 /* Return initialized */