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)
9 /* INCLUDES ******************************************************************/
15 /* FUNCTIONS *****************************************************************/
19 KdUpdateDataBlock(VOID
)
21 /* Update the KeUserCallbackDispatcher pointer */
22 KdDebuggerDataBlock
.KeUserCallbackDispatcher
=
23 (ULONG_PTR
)KeUserCallbackDispatcher
;
28 KdRegisterDebuggerDataBlock(IN ULONG Tag
,
29 IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader
,
33 PLIST_ENTRY NextEntry
;
34 PDBGKD_DEBUG_DATA_HEADER64 CurrentHeader
;
36 /* Acquire the Data Lock */
37 KeAcquireSpinLock(&KdpDataSpinLock
, &OldIrql
);
39 /* Loop the debugger data list */
40 NextEntry
= KdpDebuggerDataListHead
.Flink
;
41 while (NextEntry
!= &KdpDebuggerDataListHead
)
43 /* Get the header for this entry */
44 CurrentHeader
= CONTAINING_RECORD(NextEntry
,
45 DBGKD_DEBUG_DATA_HEADER64
,
48 /* Move to the next one */
49 NextEntry
= NextEntry
->Flink
;
51 /* Check if we already have this data block */
52 if ((CurrentHeader
== DataHeader
) || (CurrentHeader
->OwnerTag
== Tag
))
54 /* Release the lock and fail */
55 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
60 /* Setup the header */
61 DataHeader
->OwnerTag
= Tag
;
62 DataHeader
->Size
= Size
;
64 /* Insert it into the list and release the lock */
65 InsertTailList(&KdpDebuggerDataListHead
, (PLIST_ENTRY
)&DataHeader
->List
);
66 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
72 KdInitSystem(IN ULONG BootPhase
,
73 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
76 LPSTR CommandLine
, DebugLine
;
77 ANSI_STRING ImageName
;
78 PLDR_DATA_TABLE_ENTRY LdrEntry
;
79 PLIST_ENTRY NextEntry
;
84 /* Check if this is Phase 1 */
87 /* Just query the performance counter */
88 KeQueryPerformanceCounter(&KdPerformanceCounterRate
);
92 /* Check if we already initialized once */
93 if (KdDebuggerEnabled
) return TRUE
;
95 /* Set the Debug Routine as the Stub for now */
96 KiDebugRoutine
= KdpStub
;
98 /* Disable break after symbol load for now */
99 KdBreakAfterSymbolLoad
= FALSE
;
101 /* Check if the Debugger Data Block was already initialized */
102 if (!KdpDebuggerDataListHead
.Flink
)
104 /* It wasn't...Initialize the KD Data Listhead */
105 InitializeListHead(&KdpDebuggerDataListHead
);
107 /* Register the Debugger Data Block */
108 KdRegisterDebuggerDataBlock(KDBG_TAG
,
109 &KdDebuggerDataBlock
.Header
,
110 sizeof(KdDebuggerDataBlock
));
112 /* Fill out the KD Version Block */
113 KdVersionBlock
.MajorVersion
= (USHORT
)(NtBuildNumber
>> 28);
114 KdVersionBlock
.MinorVersion
= (USHORT
)(NtBuildNumber
& 0xFFFF);
117 /* This is an MP Build */
118 KdVersionBlock
.Flags
|= DBGKD_VERS_FLAG_MP
;
121 /* Save Pointers to Loaded Module List and Debugger Data */
122 KdVersionBlock
.PsLoadedModuleList
= (ULONG64
)(LONG_PTR
)&PsLoadedModuleList
;
123 KdVersionBlock
.DebuggerDataList
= (ULONG64
)(LONG_PTR
)&KdpDebuggerDataListHead
;
125 /* Set protocol limits */
126 KdVersionBlock
.MaxStateChange
= DbgKdMaximumStateChange
-
127 DbgKdMinimumStateChange
;
128 KdVersionBlock
.MaxManipulate
= DbgKdMaximumManipulate
-
129 DbgKdMinimumManipulate
;
130 KdVersionBlock
.Unused
[0] = 0;
132 /* Link us in the KPCR */
133 KeGetPcr()->KdVersionBlock
= &KdVersionBlock
;
136 /* Check if we have a loader block */
139 /* Get the image entry */
140 LdrEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
141 LDR_DATA_TABLE_ENTRY
,
144 /* Save the Kernel Base */
145 PsNtosImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
146 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)LdrEntry
->DllBase
;
148 /* Check if we have a command line */
149 CommandLine
= LoaderBlock
->LoadOptions
;
153 _strupr(CommandLine
);
155 /* Assume we'll disable KD */
158 /* Check for CRASHDEBUG and NODEBUG */
159 if (strstr(CommandLine
, "CRASHDEBUG")) KdPitchDebugger
= FALSE
;
160 if (strstr(CommandLine
, "NODEBUG")) KdPitchDebugger
= TRUE
;
162 /* Check if DEBUG was on */
163 DebugLine
= strstr(CommandLine
, "DEBUG");
169 /* Check if there was additional data */
170 if (DebugLine
[5] == '=')
172 /* FIXME: Check for NOUMEX, DISABLE, AUTOENABLE */
178 /* No command line options? Disable debugger by default */
179 KdPitchDebugger
= TRUE
;
185 /* Called from a bugcheck...Save the Kernel Base */
186 KdVersionBlock
.KernBase
= (ULONG64
)(LONG_PTR
)PsNtosImageBase
;
188 /* Unconditionally enable KD */
192 /* Set the Kernel Base in the Data Block */
193 KdDebuggerDataBlock
.KernBase
= (ULONG_PTR
)KdVersionBlock
.KernBase
;
195 /* Initialize the debugger if requested */
196 if ((EnableKd
) && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock
))))
198 /* Now set our real KD routine */
199 KiDebugRoutine
= KdpTrap
;
201 /* Check if we've already initialized our structures */
202 if (!KdpDebuggerStructuresInitialized
)
204 /* Set the Debug Switch Routine and Retries*/
205 KdpContext
.KdpDefaultRetries
= 20;
206 KiDebugSwitchRoutine
= KdpSwitchProcessor
;
208 /* Initialize the Time Slip DPC */
209 KeInitializeDpc(&KdpTimeSlipDpc
, KdpTimeSlipDpcRoutine
, NULL
);
210 KeInitializeTimer(&KdpTimeSlipTimer
);
211 ExInitializeWorkItem(&KdpTimeSlipWorkItem
, KdpTimeSlipWork
, NULL
);
213 /* First-time initialization done! */
214 KdpDebuggerStructuresInitialized
= TRUE
;
217 /* Initialize the timer */
218 KdTimerStart
.QuadPart
= 0;
220 /* Officially enable KD */
221 KdPitchDebugger
= FALSE
;
222 KdDebuggerEnabled
= TRUE
;
224 /* Let user-mode know that it's enabled as well */
225 #undef KdDebuggerEnabled
226 SharedUserData
->KdDebuggerEnabled
= TRUE
;
227 #define KdDebuggerEnabled _KdDebuggerEnabled
229 /* Check if we have a loader block */
232 /* Loop boot images */
233 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
235 while ((NextEntry
!= &LoaderBlock
->LoadOrderListHead
) && (i
< 2))
237 /* Get the image entry */
238 LdrEntry
= CONTAINING_RECORD(NextEntry
,
239 LDR_DATA_TABLE_ENTRY
,
242 /* Generate the image name */
243 Name
= LdrEntry
->FullDllName
.Buffer
;
244 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
248 /* Do cheap Unicode to ANSI conversion */
249 NameBuffer
[j
++] = (CHAR
)*Name
++;
250 } while (j
< Length
);
253 NameBuffer
[j
] = ANSI_NULL
;
255 /* Load symbols for image */
256 RtlInitAnsiString(&ImageName
, NameBuffer
);
257 DbgLoadImageSymbols(&ImageName
, LdrEntry
->DllBase
, -1);
259 /* Go to the next entry */
260 NextEntry
= NextEntry
->Flink
;
265 /* Check for incoming breakin and break on symbol load if we have it*/
266 KdBreakAfterSymbolLoad
= KdPollBreakIn();
270 /* Disable debugger */
271 KdDebuggerNotPresent
= TRUE
;
274 /* Return initialized */