- Tree cleanups proposed on the mailing list. Move all non-Core OS modules to rosapps...
[reactos.git] / rosapps / sysutils / utils / pice / module / init.c
1 /*++
2
3 Copyright (c) 1998-2001 Klaus P. Gerlicher
4
5 Module Name:
6
7 init.c
8
9 Abstract:
10
11 initialisation and cleanup of debugger kernel module
12
13 Environment:
14
15 Kernel mode only
16
17 Author:
18
19 Klaus P. Gerlicher
20
21 Revision History:
22
23 25-Jan-1999: created
24 15-Nov-2000: general cleanup of source files
25
26 Copyright notice:
27
28 This file may be distributed under the terms of the GNU Public License.
29
30 --*/
31
32 ////////////////////////////////////////////////////
33 // INCLUDES
34 ////
35 #include "remods.h"
36 #include "precomp.h"
37
38 ////////////////////////////////////////////////////
39 // GLOBALS
40 ULONG ulDoInitialBreak=1;
41 char szBootParams[1024]="";
42 char tempInit[256];
43
44 PDIRECTORY_OBJECT *pNameSpaceRoot = NULL;
45 PDEBUG_MODULE pdebug_module_tail = NULL;
46 PDEBUG_MODULE pdebug_module_head = NULL;
47 PMADDRESS_SPACE mm_init_mm;
48 extern LIST_ENTRY *pModuleListHead;
49
50 ULONG KeyboardIRQL;
51
52 extern void NewInt31Handler(void);
53 //*************************************************************************
54 // InitPICE()
55 //
56 //*************************************************************************
57 BOOLEAN InitPICE(void)
58 {
59 ULONG ulHandleScancode=0,ulHandleKbdEvent=0;
60 ARGS Args;
61 KIRQL Dirql;
62 KAFFINITY Affinity;
63 ULONG ulAddr;
64
65 ENTER_FUNC();
66
67 DPRINT((0,"InitPICE(): trace step 0.5\n"));
68 KeyboardIRQL = HalGetInterruptVector(Internal,
69 0,
70 0,
71 KEYBOARD_IRQ,
72 &Dirql,
73 &Affinity);
74 DPRINT((0,"KeyboardIRQL: %x\n", KeyboardIRQL));
75
76 DPRINT((0,"InitPICE(): trace step 1\n"));
77 // enable monochrome passthrough on BX type chipset
78 EnablePassThrough();
79
80 DPRINT((0,"InitPICE(): trace step 2\n"));
81 // now load all symbol files described in /etc/pice.conf
82 if(!LoadSymbolsFromConfig(FALSE))
83 {
84 DPRINT((0,"InitPICE: LoadSymbolsFromConfig() failed\n"));
85 LEAVE_FUNC();
86 return FALSE;
87 }
88
89 DPRINT((0,"InitPICE(): trace step 3\n"));
90 // init the output console
91 // this might be one of the following depending setup
92 // a) monochrome card
93 // b) serial terminal (TODO)
94 if(!ConsoleInit())
95 {
96 DPRINT((0,"InitPICE: ConsoleInit() failed\n"));
97 UnloadSymbols();
98 LEAVE_FUNC();
99 return FALSE;
100 }
101
102 DPRINT((0,"InitPICE(): trace step 4\n"));
103 // print the initial screen template
104 PrintTemplate();
105 /*
106 DPRINT((0,"InitPICE(): trace step 5\n"));
107 // ask the user if he wants to abort the debugger load
108 if(!CheckLoadAbort())
109 {
110 Print(OUTPUT_WINDOW,"pICE: ABORT (abort by user)\n");
111 UnloadSymbols();
112 ConsoleShutdown();
113 LEAVE_FUNC();
114 return FALSE;
115 }
116 */
117
118 DPRINT((0,"InitPICE(): trace step 6\n"));
119 // load the file /boot/System.map.
120 // !!! It must be consistent with the current kernel at all cost!!!
121 if(!LoadExports())
122 {
123 Print(OUTPUT_WINDOW,"pICE: failed to load exports\n");
124 Print(OUTPUT_WINDOW,"press any key to continue...\n");
125 while(!GetKeyPolled());
126 UnloadSymbols();
127 ConsoleShutdown();
128 LEAVE_FUNC();
129 return FALSE;
130 }
131
132 DPRINT((0,"InitPICE(): trace step 7\n"));
133 ScanExports("_KernelAddressSpace", &ulAddr);
134 my_init_mm = (PMADDRESS_SPACE) ulAddr;
135 DPRINT((0,"init_mm %x @ %x\n",&my_init_mm,my_init_mm));
136 if(!my_init_mm)
137 {
138 Print(OUTPUT_WINDOW,"pICE: ABORT (initial memory map not found)\n");
139 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
140 DbgPrint("pICE: ABORT (initial memory map not found)\n");
141 DbgPrint("pICE: press any key to continue...\n");
142 while(!GetKeyPolled());
143 UnloadSymbols();
144 ConsoleShutdown();
145 LEAVE_FUNC();
146 return FALSE;
147 }
148
149 DPRINT((0,"InitPICE(): trace step 7.1\n"));
150 ScanExports("_ModuleListHead",&ulAddr);
151 pModuleListHead = (LIST_ENTRY*)ulAddr;
152 DPRINT((0,"pModuleListHead @ %X\n",pModuleListHead));
153 if(!pModuleListHead)
154 {
155 Print(OUTPUT_WINDOW,"pICE: ABORT (pModuleListHead not found)\n");
156 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
157 while(!GetKeyPolled());
158 UnloadSymbols();
159 ConsoleShutdown();
160 LEAVE_FUNC();
161 return FALSE;
162 }
163
164 DPRINT((0,"InitPICE(): trace step 7.2\n"));
165 ScanExports("_PsProcessListHead",&ulAddr);
166 pPsProcessListHead = (LIST_ENTRY*)ulAddr;
167 DPRINT((0,"pPsProcessListHead @ %X\n",pPsProcessListHead));
168 if(!pPsProcessListHead)
169 {
170 Print(OUTPUT_WINDOW,"pICE: ABORT (PsProcessListHead not found)\n");
171 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
172 while(!GetKeyPolled());
173 UnloadSymbols();
174 ConsoleShutdown();
175 LEAVE_FUNC();
176 return FALSE;
177 }
178
179 DPRINT((0,"InitPICE(): trace step 8\n"));
180 // end of the kernel
181 /*
182 ScanExports("_end",(PULONG)&kernel_end);
183 if(!kernel_end)
184 {
185 Print(OUTPUT_WINDOW,"pICE: ABORT (kernel size is unknown)\n");
186 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
187 while(!GetKeyPolled());
188 UnloadExports();
189 UnloadSymbols();
190 ConsoleShutdown();
191 LEAVE_FUNC();
192 return FALSE;
193 }
194 */
195
196 DPRINT((0,"InitPICE(): trace step 9\n"));
197
198 // the loaded module list
199 ScanExports("_NameSpaceRoot", &ulAddr);
200 pNameSpaceRoot = (PDIRECTORY_OBJECT *)ulAddr;
201 DPRINT((0,"pNameSpaceRoot @ %X\n",pNameSpaceRoot));
202 if(!pNameSpaceRoot)
203 {
204 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't retreive name space root)\n");
205 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
206 while(!GetKeyPolled());
207 UnloadExports();
208 UnloadSymbols();
209 ConsoleShutdown();
210 LEAVE_FUNC();
211 return FALSE;
212 }
213
214 DPRINT((0,"InitPICE(): trace step 10\n"));
215 // setup a linked list for use in module parsing routines.
216 if(!InitModuleList(&pdebug_module_head, 100))
217 {
218 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't initialize kernel module list)\n");
219 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
220 FreeModuleList( pdebug_module_head );
221 while(!GetKeyPolled());
222 UnloadExports();
223 UnloadSymbols();
224 ConsoleShutdown();
225 LEAVE_FUNC();
226 return FALSE;
227 }
228 pdebug_module_tail = pdebug_module_head;
229
230 DPRINT((0,"InitPICE(): trace step 11\n"));
231 // do a sanity check on exports
232 if(!SanityCheckExports())
233 {
234 Print(OUTPUT_WINDOW,"pICE: ABORT (exports are conflicting with kernel symbols)\n");
235 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
236 while(!GetKeyPolled());
237 UnloadExports();
238 UnloadSymbols();
239 ConsoleShutdown();
240 LEAVE_FUNC();
241 return FALSE;
242 }
243
244 DPRINT((0,"InitPICE(): trace step 12\n"));
245
246
247 DPRINT((0,"InitPICE(): trace step 13\n"));
248 // patch the keyboard driver
249
250 if(!PatchKeyboardDriver())
251 {
252 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't patch keyboard driver)\n");
253 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
254 while(!GetKeyPolled());
255 UnloadSymbols();
256 UnloadExports();
257 ConsoleShutdown();
258 LEAVE_FUNC();
259 return FALSE;
260 }
261
262 DPRINT((0,"InitPICE(): trace step 14\n"));
263 // partial init of shadow registers
264 CurrentCS = GLOBAL_CODE_SEGMENT;
265 CurrentEIP = (ULONG)RealIsr;
266
267 CurrentDS = CurrentSS = GLOBAL_DATA_SEGMENT;
268 __asm__("\n\t \
269 mov %%esp,%%eax\n\t \
270 mov %%eax,_CurrentESP\n\t \
271 ":::"eax");
272
273
274 // display version and symbol information
275 Ver(NULL);
276
277 // disable HW breakpoints
278 __asm__("\n\t \
279 xorl %%eax,%%eax\n\t \
280 mov %%eax,%%dr6\n\t \
281 mov %%eax,%%dr7\n\t \
282 mov %%dr0,%%eax\n\t \
283 mov %%dr1,%%eax\n\t \
284 mov %%dr2,%%eax\n\t \
285 mov %%dr3,%%eax"
286 :::"eax"
287 );
288
289 DPRINT((0,"InitPICE(): trace step 15\n"));
290 TakeIdtSnapshot();
291
292 DPRINT((0,"InitPICE(): trace step 16\n"));
293 // install all hooks
294 InstallTraceHook();
295 InstallGlobalKeyboardHook();
296 InstallSyscallHook();
297 InstallInt3Hook();
298 InstallDblFltHook();
299 InstallGPFaultHook();
300 InstallIntEHook();
301 InstallPrintkHook();
302
303 DPRINT((0,"InitPICE(): trace step 16\n"));
304 if(ulDoInitialBreak)
305 {
306 DPRINT((0,"about to do initial break...\n"));
307
308 // simulate an initial break
309 __asm__("\n\t \
310 pushfl\n\t \
311 pushl %cs\n\t \
312 pushl $initialreturnpoint\n\t \
313 pushl $" STR(REASON_CTRLF) "\n\t \
314 jmp NewInt31Handler\n\t \
315 initialreturnpoint:");
316 }
317 else
318 {
319 // display register contents
320 DisplayRegs();
321
322 // display data window
323 Args.Value[0]=CurrentDS;
324 Args.Value[1]=CurrentEIP;
325 Args.Count=2;
326 DisplayMemory(&Args);
327
328 // disassembly from current address
329 Args.Value[0]=CurrentCS;
330 Args.Value[1]=CurrentEIP;
331 Args.Count=2;
332 Unassemble(&Args);
333 }
334
335 DPRINT((0,"InitPICE(): trace step 17\n"));
336 InitPiceRunningTimer();
337
338 LEAVE_FUNC();
339 return TRUE;
340 }
341
342 //*************************************************************************
343 // CleanUpPICE()
344 //
345 //*************************************************************************
346 void CleanUpPICE(void)
347 {
348 DPRINT((0,"CleanUpPICE(): trace step 1\n"));
349 RemovePiceRunningTimer();
350
351 DPRINT((0,"CleanUpPICE(): trace step 2\n"));
352 // de-install all hooks
353 DeInstallGlobalKeyboardHook();
354 DeInstallSyscallHook();
355 DeInstallInt3Hook();
356 DeInstallPrintkHook();
357 DeInstallDblFltHook();
358 DeInstallGPFaultHook();
359 DeInstallIntEHook();
360 DeInstallTraceHook();
361
362 DPRINT((0,"CleanUpPICE(): trace step 3\n"));
363 RestoreIdt();
364
365 DPRINT((0,"CleanUpPICE(): trace step 4\n"));
366 UnloadExports(); // don't use ScanExports() after this
367 UnloadSymbols();
368
369 DPRINT((0,"CleanUpPICE(): trace step 5\n"));
370 // restore patch of keyboard driver
371 RestoreKeyboardDriver();
372
373 DPRINT((0,"CleanUpPICE(): trace step 6\n"));
374 Print(OUTPUT_WINDOW,"pICE: shutting down...\n");
375
376 DPRINT((0,"CleanUpPICE(): trace step 7\n"));
377 // cleanup the console
378 ConsoleShutdown();
379 }