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 KdRegisterDebuggerDataBlock(IN ULONG Tag
,
20 IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader
,
24 PLIST_ENTRY NextEntry
;
25 PDBGKD_DEBUG_DATA_HEADER64 CurrentHeader
;
27 /* Acquire the Data Lock */
28 KeAcquireSpinLock(&KdpDataSpinLock
, &OldIrql
);
30 /* Loop the debugger data list */
31 NextEntry
= KdpDebuggerDataListHead
.Flink
;
32 while (NextEntry
!= &KdpDebuggerDataListHead
)
34 /* Get the header for this entry */
35 CurrentHeader
= CONTAINING_RECORD(NextEntry
,
36 DBGKD_DEBUG_DATA_HEADER64
,
39 /* Move to the next one */
40 NextEntry
= NextEntry
->Flink
;
42 /* Check if we already have this data block */
43 if ((CurrentHeader
== DataHeader
) || (CurrentHeader
->OwnerTag
== Tag
))
45 /* Release the lock and fail */
46 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
51 /* Setup the header */
52 DataHeader
->OwnerTag
= Tag
;
53 DataHeader
->Size
= Size
;
55 /* Insert it into the list and release the lock */
56 InsertTailList(&KdpDebuggerDataListHead
, (PLIST_ENTRY
)&DataHeader
->List
);
57 KeReleaseSpinLock(&KdpDataSpinLock
, OldIrql
);
63 KdInitSystem(IN ULONG BootPhase
,
64 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
67 LPSTR CommandLine
, DebugLine
;
68 ANSI_STRING ImageName
;
69 PLDR_DATA_TABLE_ENTRY LdrEntry
;
70 PLIST_ENTRY NextEntry
;
75 /* Check if this is Phase 1 */
78 /* Just query the performance counter */
79 KeQueryPerformanceCounter(&KdPerformanceCounterRate
);
83 /* Check if we already initialized once */
84 if (KdDebuggerEnabled
) return TRUE
;
86 /* Set the Debug Routine as the Stub for now */
87 KiDebugRoutine
= KdpStub
;
89 /* Disable break after symbol load for now */
90 KdBreakAfterSymbolLoad
= FALSE
;
92 /* Check if the Debugger Data Block was already initialized */
93 if (!KdpDebuggerDataListHead
.Flink
)
95 /* It wasn't...Initialize the KD Data Listhead */
96 InitializeListHead(&KdpDebuggerDataListHead
);
98 /* Register the Debugger Data Block */
99 KdRegisterDebuggerDataBlock(KDBG_TAG
,
100 &KdDebuggerDataBlock
.Header
,
101 sizeof(KdDebuggerDataBlock
));
103 /* Fill out the KD Version Block */
104 KdVersionBlock
.MajorVersion
= (USHORT
)(NtBuildNumber
>> 28);
105 KdVersionBlock
.MinorVersion
= (USHORT
)(NtBuildNumber
& 0xFFFF);
108 /* This is an MP Build */
109 KdVersionBlock
.Flags
|= DBGKD_VERS_FLAG_MP
;
112 /* Save Pointers to Loaded Module List and Debugger Data */
113 KdVersionBlock
.PsLoadedModuleList
= (ULONGLONG
)(LONG_PTR
)&PsLoadedModuleList
;
114 KdVersionBlock
.DebuggerDataList
= (ULONGLONG
)(LONG_PTR
)&KdpDebuggerDataListHead
;
116 /* Set protocol limits */
117 KdVersionBlock
.MaxStateChange
= DbgKdMaximumStateChange
-
118 DbgKdMinimumStateChange
;
119 KdVersionBlock
.MaxManipulate
= DbgKdMaximumManipulate
-
120 DbgKdMinimumManipulate
;
121 KdVersionBlock
.Unused
[0] = 0;
123 /* Link us in the KPCR */
124 KeGetPcr()->KdVersionBlock
= &KdVersionBlock
;
127 /* Check if we have a loader block */
130 /* Get the image entry */
131 LdrEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
132 LDR_DATA_TABLE_ENTRY
,
135 /* Save the Kernel Base */
136 PsNtosImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
137 KdVersionBlock
.KernBase
= (ULONGLONG
)(LONG_PTR
)LdrEntry
->DllBase
;
139 /* Check if we have a command line */
140 CommandLine
= LoaderBlock
->LoadOptions
;
144 _strupr(CommandLine
);
146 /* Assume we'll disable KD */
149 /* Check for CRASHDEBUG and NODEBUG */
150 if (strstr(CommandLine
, "CRASHDEBUG")) KdPitchDebugger
= FALSE
;
151 if (strstr(CommandLine
, "NODEBUG")) KdPitchDebugger
= TRUE
;
153 /* Check if DEBUG was on */
154 DebugLine
= strstr(CommandLine
, "DEBUG");
160 /* Check if there was additional data */
161 if (DebugLine
[5] == '=')
163 /* FIXME: Check for NOUMEX, DISABLE, AUTOENABLE */
169 /* No command line options? Disable debugger by default */
170 KdPitchDebugger
= TRUE
;
176 /* Called from a bugcheck...Save the Kernel Base */
177 KdVersionBlock
.KernBase
= (ULONGLONG
)(LONG_PTR
)PsNtosImageBase
;
179 /* Unconditionally enable KD */
183 /* Set the Kernel Base in the Data Block */
184 KdDebuggerDataBlock
.KernBase
= (ULONGLONG
)(LONG_PTR
)KdVersionBlock
.KernBase
;
186 /* Initialize the debugger if requested */
187 if ((EnableKd
) && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock
))))
189 /* Now set our real KD routine */
190 KiDebugRoutine
= KdpTrap
;
192 /* Check if we've already initialized our structures */
193 if (!KdpDebuggerStructuresInitialized
)
195 /* Set the Debug Switch Routine and Retries*/
196 KdpContext
.KdpDefaultRetries
= 20;
197 KiDebugSwitchRoutine
= KdpSwitchProcessor
;
199 /* Initialize the Time Slip DPC */
200 KeInitializeDpc(&KdpTimeSlipDpc
, KdpTimeSlipDpcRoutine
, NULL
);
201 KeInitializeTimer(&KdpTimeSlipTimer
);
202 ExInitializeWorkItem(&KdpTimeSlipWorkItem
, KdpTimeSlipWork
, NULL
);
204 /* First-time initialization done! */
205 KdpDebuggerStructuresInitialized
= TRUE
;
208 /* Initialize the timer */
209 KdTimerStart
.QuadPart
= 0;
211 /* Officially enable KD */
212 KdPitchDebugger
= FALSE
;
213 KdDebuggerEnabled
= TRUE
;
215 /* Let user-mode know that it's enabled as well */
216 #undef KdDebuggerEnabled
217 SharedUserData
->KdDebuggerEnabled
= TRUE
;
218 #define KdDebuggerEnabled _KdDebuggerEnabled
220 /* Check if we have a loader block */
223 /* Loop boot images */
224 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
226 while ((NextEntry
!= &LoaderBlock
->LoadOrderListHead
) && (i
< 2))
228 /* Get the image entry */
229 LdrEntry
= CONTAINING_RECORD(NextEntry
,
230 LDR_DATA_TABLE_ENTRY
,
233 /* Generate the image name */
234 Name
= LdrEntry
->FullDllName
.Buffer
;
235 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
239 /* Do cheap Unicode to ANSI conversion */
240 NameBuffer
[j
++] = (CHAR
)*Name
++;
241 } while (j
< Length
);
244 NameBuffer
[j
] = ANSI_NULL
;
246 /* Load symbols for image */
247 RtlInitAnsiString(&ImageName
, NameBuffer
);
248 DbgLoadImageSymbols(&ImageName
, LdrEntry
->DllBase
, -1);
250 /* Go to the next entry */
251 NextEntry
= NextEntry
->Flink
;
256 /* Check for incoming breakin and break on symbol load if we have it*/
257 KdBreakAfterSymbolLoad
= KdPollBreakIn();
261 /* Disable debugger */
262 KdDebuggerNotPresent
= TRUE
;
265 /* Return initialized */