ReactOS  0.4.12-dev-914-g71f84a3
init.c
Go to the documentation of this file.
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 
33 // INCLUDES
35 #include "remods.h"
36 #include "precomp.h"
37 
39 // GLOBALS
41 char szBootParams[1024]="";
42 char tempInit[256];
43 
44 PDIRECTORY_OBJECT *pNameSpaceRoot = NULL;
47 PMADDRESS_SPACE mm_init_mm;
49 
51 
52 extern void NewInt31Handler(void);
53 //*************************************************************************
54 // InitPICE()
55 //
56 //*************************************************************************
58 {
59  ULONG ulHandleScancode=0,ulHandleKbdEvent=0;
60  ARGS Args;
61  KIRQL Dirql;
63  ULONG ulAddr;
64 
65  ENTER_FUNC();
66 
67  DPRINT((0,"InitPICE(): trace step 0.5\n"));
69  0,
70  0,
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
79 
80  DPRINT((0,"InitPICE(): trace step 2\n"));
81  // now load all symbol files described in /etc/pice.conf
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.
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");
221  while(!GetKeyPolled());
222  UnloadExports();
223  UnloadSymbols();
224  ConsoleShutdown();
225  LEAVE_FUNC();
226  return FALSE;
227  }
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
266 
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
297  InstallInt3Hook();
300  InstallIntEHook();
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;
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"));
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"));
350 
351  DPRINT((0,"CleanUpPICE(): trace step 2\n"));
352  // de-install all hooks
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
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 }
USHORT CurrentCS
Definition: shell.c:116
PMADDRESS_SPACE mm_init_mm
Definition: init.c:47
LIST_ENTRY * pPsProcessListHead
Definition: utils.h:301
#define TRUE
Definition: types.h:120
void DeInstallGPFaultHook(void)
Definition: gpfault.c:126
void EnablePassThrough(void)
Definition: utils.c:2194
NTHALAPI ULONG NTAPI HalGetInterruptVector(INTERFACE_TYPE, ULONG, ULONG, ULONG, PKIRQL, PKAFFINITY)
BOOLEAN ConsoleInit(void)
Definition: hardware.c:946
#define DbgPrint
Definition: loader.c:25
void CleanUpPICE(void)
Definition: init.c:346
void PrintTemplate(void)
Definition: hardware.c:741
void RestoreIdt(void)
Definition: hooks.c:156
USHORT CurrentSS
Definition: shell.c:116
void TakeIdtSnapshot(void)
Definition: hooks.c:131
void InstallDblFltHook(void)
Definition: dblflt.c:93
PDIRECTORY_OBJECT * pNameSpaceRoot
Definition: init.c:44
PDEBUG_MODULE pdebug_module_head
Definition: init.c:46
#define GLOBAL_DATA_SEGMENT
Definition: utils.h:247
void RealIsr(ULONG dwReasonForBreak)
Definition: shell.c:1016
#define LEAVE_FUNC()
Definition: debug.h:43
void NewInt31Handler(void)
void RemovePiceRunningTimer(void)
Definition: output.c:243
BOOLEAN LoadSymbolsFromConfig(BOOLEAN bIgnoreBootParams)
Definition: symbols.c:2116
void Print(USHORT Window, LPSTR p)
Definition: hardware.c:797
#define STR(x)
Definition: utils.h:34
UCHAR KIRQL
Definition: env_spec_w32.h:591
PMADDRESS_SPACE my_init_mm
Definition: utils.h:300
#define KEYBOARD_IRQ
Definition: shell.h:91
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
void InitPiceRunningTimer(void)
Definition: output.c:221
void DeInstallPrintkHook(void)
Definition: output.c:278
void InstallGlobalKeyboardHook(void)
Definition: shell.c:1657
void InstallGPFaultHook(void)
Definition: gpfault.c:102
void DeInstallSyscallHook(void)
Definition: syscall.c:218
unsigned char BOOLEAN
BOOLEAN InstallTraceHook(void)
Definition: trace.c:42
smooth NULL
Definition: ftsmooth.c:416
void DeInstallInt3Hook(void)
Definition: bp.c:736
void DPRINT(...)
Definition: polytest.cpp:61
char ** Args
Definition: acdebug.h:343
void DeInstallGlobalKeyboardHook(void)
Definition: shell.c:1677
VOID FreeModuleList(PDEBUG_MODULE pm)
Definition: symbols.c:121
LIST_ENTRY * pModuleListHead
Definition: symbols.c:100
BOOLEAN SanityCheckExports(void)
Definition: symbols.c:1860
BOOLEAN InitPICE(void)
Definition: init.c:57
USHORT CurrentDS
Definition: shell.c:116
_In_ ULONG _In_ ULONG _In_ ULONG _Out_ PKIRQL _Out_ PKAFFINITY Affinity
Definition: halfuncs.h:170
Definition: parse.h:31
BOOLEAN PatchKeyboardDriver(void)
Definition: patch.c:193
void DeInstallTraceHook(void)
Definition: trace.c:80
void InstallInt3Hook(void)
Definition: bp.c:709
void InstallPrintkHook(void)
Definition: output.c:252
Definition: typedefs.h:117
void InstallIntEHook(void)
Definition: pgflt.c:365
void DisplayRegs(void)
Definition: utils.c:1097
ULONG ulDoInitialBreak
Definition: init.c:40
BOOLEAN LoadExports(void)
Definition: symbols.c:1909
BOOLEAN InitModuleList(PDEBUG_MODULE *ppmodule, ULONG len)
Definition: symbols.c:138
UCHAR GetKeyPolled(void)
Definition: hardware.c:926
ULONG KeyboardIRQL
Definition: init.c:50
void RestoreKeyboardDriver(void)
Definition: patch.c:230
void UnloadExports(void)
Definition: symbols.c:1974
ULONG_PTR KAFFINITY
Definition: compat.h:75
BOOLEAN ScanExports(const char *pFind, PULONG pValue)
Definition: symbols.c:327
ULONG CurrentEIP
Definition: shell.c:113
void InstallSyscallHook(void)
Definition: syscall.c:194
void DeInstallDblFltHook(void)
Definition: dblflt.c:117
void ConsoleShutdown(void)
Definition: hardware.c:1013
char tempInit[256]
Definition: init.c:42
unsigned int ULONG
Definition: retypes.h:1
#define ENTER_FUNC()
Definition: debug.h:42
void UnloadSymbols()
Definition: symbols.c:2093
void DeInstallIntEHook(void)
Definition: pgflt.c:389
PDEBUG_MODULE pdebug_module_tail
Definition: init.c:45
#define GLOBAL_CODE_SEGMENT
Definition: utils.h:246
BOOLEAN(* DisplayMemory)(PARGS)
Definition: parse.c:56
char szBootParams[1024]
Definition: init.c:41
#define REASON_CTRLF
Definition: shell.h:73