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
);
73 KdInitSystem(IN ULONG BootPhase
,
74 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
76 BOOLEAN EnableKd
, DisableKdAfterInit
= FALSE
, BlockEnable
;
77 LPSTR CommandLine
, DebugLine
, DebugOptionStart
, DebugOptionEnd
;
78 ANSI_STRING ImageName
;
79 PLDR_DATA_TABLE_ENTRY LdrEntry
;
80 PLIST_ENTRY NextEntry
;
81 ULONG i
, j
, Length
, DebugOptionLength
;
90 /* Check if this is Phase 1 */
93 /* Just query the performance counter */
94 KeQueryPerformanceCounter(&KdPerformanceCounterRate
);
98 /* Check if we already initialized once */
99 if (KdDebuggerEnabled
) return TRUE
;
101 /* Set the Debug Routine as the Stub for now */
102 KiDebugRoutine
= KdpStub
;
104 /* Disable break after symbol load for now */
105 KdBreakAfterSymbolLoad
= FALSE
;
107 /* Check if the Debugger Data Block was already initialized */
108 if (!KdpDebuggerDataListHead
.Flink
)
110 /* It wasn't...Initialize the KD Data Listhead */
111 InitializeListHead(&KdpDebuggerDataListHead
);
113 /* Register the Debugger Data Block */
114 KdRegisterDebuggerDataBlock(KDBG_TAG
,
115 &KdDebuggerDataBlock
.Header
,
116 sizeof(KdDebuggerDataBlock
));
118 /* Fill out the KD Version Block */
119 KdVersionBlock
.MajorVersion
= (USHORT
)(NtBuildNumber
>> 28);
120 KdVersionBlock
.MinorVersion
= (USHORT
)(NtBuildNumber
& 0xFFFF);
123 /* This is an MP Build */
124 KdVersionBlock
.Flags
|= DBGKD_VERS_FLAG_MP
;
127 /* Save Pointers to Loaded Module List and Debugger Data */
128 KdVersionBlock
.PsLoadedModuleList
= (ULONG64
)(LONG_PTR
)&PsLoadedModuleList
;
129 KdVersionBlock
.DebuggerDataList
= (ULONG64
)(LONG_PTR
)&KdpDebuggerDataListHead
;
131 /* Set protocol limits */
132 KdVersionBlock
.MaxStateChange
= DbgKdMaximumStateChange
-
133 DbgKdMinimumStateChange
;
134 KdVersionBlock
.MaxManipulate
= DbgKdMaximumManipulate
-
135 DbgKdMinimumManipulate
;
136 KdVersionBlock
.Unused
[0] = 0;
138 /* Link us in the KPCR */
139 KeGetPcr()->KdVersionBlock
= &KdVersionBlock
;
142 /* Check if we have a loader block */
145 /* Get the image entry */
146 LdrEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
147 LDR_DATA_TABLE_ENTRY
,
150 /* Save the Kernel Base */
151 PsNtosImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
152 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)LdrEntry
->DllBase
;
154 /* Check if we have a command line */
155 CommandLine
= LoaderBlock
->LoadOptions
;
159 _strupr(CommandLine
);
161 /* Assume we'll disable KD */
164 /* Check for CRASHDEBUG and NODEBUG */
165 if (strstr(CommandLine
, "CRASHDEBUG")) KdPitchDebugger
= FALSE
;
166 if (strstr(CommandLine
, "NODEBUG")) KdPitchDebugger
= TRUE
;
168 /* Check if DEBUG was on */
169 DebugLine
= strstr(CommandLine
, "DEBUG");
175 /* Check if there are any options */
176 if (DebugLine
[5] == '=')
179 DebugOptionStart
= DebugOptionEnd
= &DebugLine
[6];
181 /* Scan the string for debug options */
184 /* Loop until we reach the end of the string */
185 while (*DebugOptionEnd
!= ANSI_NULL
)
187 /* Check if this is a comma, a space or a tab */
188 if ((*DebugOptionEnd
== ',') ||
189 (*DebugOptionEnd
== ' ') ||
190 (*DebugOptionEnd
== ' '))
193 * We reached the end of the option or
194 * the end of the string, break out
200 /* Move on to the next character */
205 /* Calculate the length of the current option */
206 DebugOptionLength
= ((ULONG_PTR
)DebugOptionEnd
-
207 (ULONG_PTR
)DebugOptionStart
);
210 * Break out if we reached the last option
211 * or if there were no options at all
213 if (!DebugOptionLength
) break;
215 /* Now check which option this is */
216 if ((DebugOptionLength
== 10) &&
217 !(strncmp(DebugOptionStart
, "AUTOENABLE", 10)))
220 * Disable the debugger, but
221 * allow it to be reenabled
223 DisableKdAfterInit
= TRUE
;
225 KdAutoEnableOnEvent
= TRUE
;
227 else if ((DebugOptionLength
== 7) &&
228 !(strncmp(DebugOptionStart
, "DISABLE", 7)))
230 /* Disable the debugger */
231 DisableKdAfterInit
= TRUE
;
233 KdAutoEnableOnEvent
= FALSE
;
235 else if ((DebugOptionLength
== 6) &&
236 !(strncmp(DebugOptionStart
, "NOUMEX", 6)))
238 /* Ignore user mode exceptions */
239 KdIgnoreUmExceptions
= TRUE
;
243 * If there are more options then
244 * the next character should be a comma
246 if (*DebugOptionEnd
!= ',')
248 /* It isn't, break out */
252 /* Move on to the next option */
254 DebugOptionStart
= DebugOptionEnd
;
261 /* No command line options? Disable debugger by default */
262 KdPitchDebugger
= TRUE
;
268 /* Called from a bugcheck or a re-enable. Save the Kernel Base */
269 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)PsNtosImageBase
;
271 /* Unconditionally enable KD */
275 /* Set the Kernel Base in the Data Block */
276 KdDebuggerDataBlock
.KernBase
= (ULONG_PTR
)KdVersionBlock
.KernBase
;
278 /* Initialize the debugger if requested */
279 if ((EnableKd
) && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock
))))
281 /* Now set our real KD routine */
282 KiDebugRoutine
= KdpTrap
;
284 /* Check if we've already initialized our structures */
285 if (!KdpDebuggerStructuresInitialized
)
287 /* Set the Debug Switch Routine and Retries*/
288 KdpContext
.KdpDefaultRetries
= 20;
289 KiDebugSwitchRoutine
= KdpSwitchProcessor
;
291 /* Initialize the Time Slip DPC */
292 KeInitializeDpc(&KdpTimeSlipDpc
, KdpTimeSlipDpcRoutine
, NULL
);
293 KeInitializeTimer(&KdpTimeSlipTimer
);
294 ExInitializeWorkItem(&KdpTimeSlipWorkItem
, KdpTimeSlipWork
, NULL
);
296 /* First-time initialization done! */
297 KdpDebuggerStructuresInitialized
= TRUE
;
300 /* Initialize the timer */
301 KdTimerStart
.QuadPart
= 0;
303 /* Officially enable KD */
304 KdPitchDebugger
= FALSE
;
305 KdDebuggerEnabled
= TRUE
;
307 /* Let user-mode know that it's enabled as well */
308 #undef KdDebuggerEnabled
309 SharedUserData
->KdDebuggerEnabled
= TRUE
;
310 #define KdDebuggerEnabled _KdDebuggerEnabled
312 /* Check if the debugger should be disabled initially */
313 if (DisableKdAfterInit
)
316 KdDisableDebuggerWithLock(FALSE
);
319 * Save the enable block state and return initialized
320 * (the debugger is active but disabled).
322 KdBlockEnable
= BlockEnable
;
326 /* Check if we have a loader block */
329 /* Loop boot images */
330 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
332 while ((NextEntry
!= &LoaderBlock
->LoadOrderListHead
) && (i
< 2))
334 /* Get the image entry */
335 LdrEntry
= CONTAINING_RECORD(NextEntry
,
336 LDR_DATA_TABLE_ENTRY
,
339 /* Generate the image name */
340 Name
= LdrEntry
->FullDllName
.Buffer
;
341 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
345 /* Do cheap Unicode to ANSI conversion */
346 NameBuffer
[j
++] = (CHAR
)*Name
++;
347 } while (j
< Length
);
350 NameBuffer
[j
] = ANSI_NULL
;
352 /* Load symbols for image */
353 RtlInitAnsiString(&ImageName
, NameBuffer
);
354 DbgLoadImageSymbols(&ImageName
, LdrEntry
->DllBase
, -1);
356 /* Go to the next entry */
357 NextEntry
= NextEntry
->Flink
;
362 /* Check for incoming breakin and break on symbol load if we have it*/
363 KdBreakAfterSymbolLoad
= KdPollBreakIn();
367 /* Disable debugger */
368 KdDebuggerNotPresent
= TRUE
;
371 /* Return initialized */