40b449f76fc288967f103540a40f01dcf8111bd4
[reactos.git] / reactos / ntoskrnl / kd64 / kdinit.c
1 /*
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 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* FUNCTIONS *****************************************************************/
16
17 BOOLEAN
18 NTAPI
19 KdRegisterDebuggerDataBlock(IN ULONG Tag,
20 IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader,
21 IN ULONG Size)
22 {
23 KIRQL OldIrql;
24 PLIST_ENTRY NextEntry;
25 PDBGKD_DEBUG_DATA_HEADER64 CurrentHeader;
26
27 /* Acquire the Data Lock */
28 KeAcquireSpinLock(&KdpDataSpinLock, &OldIrql);
29
30 /* Loop the debugger data list */
31 NextEntry = KdpDebuggerDataListHead.Flink;
32 while (NextEntry != &KdpDebuggerDataListHead)
33 {
34 /* Get the header for this entry */
35 CurrentHeader = CONTAINING_RECORD(NextEntry,
36 DBGKD_DEBUG_DATA_HEADER64,
37 List);
38
39 /* Move to the next one */
40 NextEntry = NextEntry->Flink;
41
42 /* Check if we already have this data block */
43 if ((CurrentHeader == DataHeader) || (CurrentHeader->OwnerTag == Tag))
44 {
45 /* Release the lock and fail */
46 KeReleaseSpinLock(&KdpDataSpinLock, OldIrql);
47 return FALSE;
48 }
49 }
50
51 /* Setup the header */
52 DataHeader->OwnerTag = Tag;
53 DataHeader->Size = Size;
54
55 /* Insert it into the list and release the lock */
56 InsertTailList(&KdpDebuggerDataListHead, (PLIST_ENTRY)&DataHeader->List);
57 KeReleaseSpinLock(&KdpDataSpinLock, OldIrql);
58 return TRUE;
59 }
60
61 BOOLEAN
62 NTAPI
63 KdInitSystem(IN ULONG BootPhase,
64 IN PLOADER_PARAMETER_BLOCK LoaderBlock)
65 {
66 BOOLEAN EnableKd;
67 LPSTR CommandLine, DebugLine;
68 ANSI_STRING ImageName;
69 PLDR_DATA_TABLE_ENTRY LdrEntry;
70 PLIST_ENTRY NextEntry;
71 ULONG i;
72 CHAR NameBuffer[256];
73 return TRUE;
74
75 /* Check if this is Phase 1 */
76 if (BootPhase)
77 {
78 /* Just query the performance counter */
79 KeQueryPerformanceCounter(&KdPerformanceCounterRate);
80 return TRUE;
81 }
82
83 /* Check if we already initialized once */
84 if (KdDebuggerEnabled) return TRUE;
85
86 /* Set the Debug Routine as the Stub for now */
87 KiDebugRoutine = KdpStub;
88
89 /* Disable break after symbol load for now */
90 KdBreakAfterSymbolLoad = FALSE;
91
92 /* Check if the Debugger Data Block was already initialized */
93 if (!KdpDebuggerDataListHead.Flink)
94 {
95 /* It wasn't...Initialize the KD Data Listhead */
96 InitializeListHead(&KdpDebuggerDataListHead);
97
98 /* Register the Debugger Data Block */
99 KdRegisterDebuggerDataBlock(KDBG_TAG,
100 &KdDebuggerDataBlock.Header,
101 sizeof(KdDebuggerDataBlock));
102
103 /* Fill out the KD Version Block */
104 KdVersionBlock.MajorVersion = (USHORT)(NtBuildNumber >> 28);
105 KdVersionBlock.MinorVersion = (USHORT)(NtBuildNumber & 0xFFFF);
106
107 #ifdef CONFIG_SMP
108 /* This is an MP Build */
109 KdVersionBlock.Flags |= DBGKD_VERS_FLAG_MP;
110 #endif
111
112 /* Save Pointers to Loaded Module List and Debugger Data */
113 KdVersionBlock.PsLoadedModuleList = (ULONG64)&PsLoadedModuleList;
114 KdVersionBlock.DebuggerDataList = (ULONG64)&KdpDebuggerDataListHead;
115
116 /* Set protocol limits */
117 KdVersionBlock.MaxStateChange = DbgKdMaximumStateChange -
118 DbgKdMinimumStateChange;
119 KdVersionBlock.MaxManipulate = DbgKdMaximumManipulate -
120 DbgKdMinimumManipulate;
121 KdVersionBlock.Unused[0] = 0;
122
123 /* Link us in the KPCR */
124 KeGetPcr()->KdVersionBlock = &KdVersionBlock;
125 }
126
127 /* Check if we have a loader block */
128 if (LoaderBlock)
129 {
130 /* Save the Kernel Base */
131 KdVersionBlock.KernBase = (ULONG64)LoaderBlock->KernelStack;
132
133 /* Check if we have a command line */
134 CommandLine = LoaderBlock->LoadOptions;
135 if (CommandLine)
136 {
137 /* Upcase it */
138 _strupr(CommandLine);
139
140 /* Assume we'll disable KD */
141 EnableKd = FALSE;
142
143 /* Check for CRASHDEBUG and NODEBUG */
144 if (strstr(CommandLine, "CRASHDEBUG")) KdPitchDebugger = FALSE;
145 if (strstr(CommandLine, "NODEBUG")) KdPitchDebugger = TRUE;
146
147 /* Check if DEBUG was on */
148 DebugLine = strstr(CommandLine, "DEBUG");
149 if (DebugLine)
150 {
151 /* Enable KD */
152 EnableKd = TRUE;
153
154 /* Check if there was additional data */
155 if (DebugLine[5] == '=')
156 {
157 /* FIXME: Check for NOUMEX, DISABLE, AUTOENABLE */
158 }
159 }
160 }
161 else
162 {
163 /* No command line options? Disable debugger by default */
164 KdPitchDebugger = TRUE;
165 EnableKd = FALSE;
166 }
167 }
168 else
169 {
170 /* Called from a bugcheck...Save the Kernel Base */
171 KdVersionBlock.KernBase = PsNtosImageBase;
172
173 /* Unconditionally enable KD */
174 EnableKd = TRUE;
175 }
176
177 /* Set the Kernel Base in the Data Block */
178 KdDebuggerDataBlock.KernBase = KdVersionBlock.KernBase;
179
180 /* Initialize the debugger if requested */
181 if ((EnableKd) && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock))))
182 {
183 /* Now set our real KD routine */
184 KiDebugRoutine = KdpTrap;
185
186 /* Check if we've already initialized our structures */
187 if (!KdpDebuggerStructuresInitialized)
188 {
189 /* Set the Debug Switch Routine and Retries*/
190 KdpContext.KdpDefaultRetries = 20;
191 KiDebugSwitchRoutine = KdpSwitchProcessor;
192
193 /* Initialize the Time Slip DPC */
194 KeInitializeDpc(&KdpTimeSlipDpc, KdpTimeSlipDpcRoutine, NULL);
195 KeInitializeTimer(&KdpTimeSlipTimer);
196 ExInitializeWorkItem(&KdpTimeSlipWorkItem, KdpTimeSlipWork, NULL);
197
198 /* First-time initialization done! */
199 KdpDebuggerStructuresInitialized = TRUE;
200 }
201
202 /* Initialize the timer */
203 KdTimerStart.QuadPart = 0;
204
205 /* Officially enable KD */
206 KdPitchDebugger = FALSE;
207 KdDebuggerEnabled = TRUE;
208
209 /* Let user-mode know that it's enabled as well */
210 #undef KdDebuggerEnabled
211 SharedUserData->KdDebuggerEnabled = TRUE;
212 #define KdDebuggerEnabled _KdDebuggerEnabled
213
214 /* Check if we have a loader block */
215 if (LoaderBlock)
216 {
217 /* Loop boot images */
218 NextEntry = LoaderBlock->LoadOrderListHead.Flink;
219 i = 0;
220 while ((NextEntry != &LoaderBlock->LoadOrderListHead) && (i < 2))
221 {
222 /* Get the image entry */
223 LdrEntry = CONTAINING_RECORD(NextEntry,
224 LDR_DATA_TABLE_ENTRY,
225 InLoadOrderLinks);
226
227 /* Generate the image name */
228
229 /* Load symbols for image */
230 RtlInitAnsiString(&ImageName, NameBuffer);
231 DbgLoadImageSymbols(&ImageName, LdrEntry->DllBase, -1);
232
233 /* Go to the next entry */
234 NextEntry = NextEntry->Flink;
235 i++;
236 }
237 }
238
239 /* Check for incoming breakin and break on symbol load if we have it*/
240 KdBreakAfterSymbolLoad = KdPollBreakIn();
241 }
242 else
243 {
244 /* Disable debugger */
245 KdDebuggerNotPresent = TRUE;
246 }
247
248 /* Return initialized */
249 return TRUE;
250 }