Porting pice. Added keyboard hook and some file functions.
[reactos.git] / reactos / apps / 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 #include <linux/fs.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41 #include <asm/delay.h>
42
43 ////////////////////////////////////////////////////
44 // GLOBALS
45 ULONG ulDoInitialBreak=1;
46 char szBootParams[1024]="";
47 char tempInit[256];
48
49 //*************************************************************************
50 // InitPICE()
51 //
52 //*************************************************************************
53 BOOLEAN InitPICE(void)
54 {
55 ULONG ulHandleScancode=0,ulHandleKbdEvent=0;
56 ARGS Args;
57
58 ENTER_FUNC();
59
60 DPRINT((0,"InitPICE(): trace step 1\n"));
61 // enable monochrome passthrough on BX type chipset
62 EnablePassThrough();
63
64 DPRINT((0,"InitPICE(): trace step 2\n"));
65 // now load all symbol files described in /etc/pice.conf
66 if(!LoadSymbolsFromConfig(FALSE))
67 {
68 DPRINT((0,"InitPICE: LoadSymbolsFromConfig() failed\n"));
69 LEAVE_FUNC();
70 return FALSE;
71 }
72
73 DPRINT((0,"InitPICE(): trace step 3\n"));
74 // init the output console
75 // this might be one of the following depending setup
76 // a) monochrome card
77 // b) serial terminal (TODO)
78 if(!ConsoleInit())
79 {
80 DPRINT((0,"InitPICE: ConsoleInit() failed\n"));
81 UnloadSymbols();
82 LEAVE_FUNC();
83 return FALSE;
84 }
85
86 DPRINT((0,"InitPICE(): trace step 4\n"));
87 // print the initial screen template
88 PrintTemplate();
89
90 DPRINT((0,"InitPICE(): trace step 5\n"));
91 // ask the user if he wants to abort the debugger load
92 if(!CheckLoadAbort())
93 {
94 Print(OUTPUT_WINDOW,"pICE: ABORT (abort by user)\n");
95 UnloadSymbols();
96 ConsoleShutdown();
97 LEAVE_FUNC();
98 return FALSE;
99 }
100
101 DPRINT((0,"InitPICE(): trace step 6\n"));
102 // get kernel mm_struct
103 my_init_mm = GetInitMm();
104 if(!my_init_mm)
105 {
106 Print(OUTPUT_WINDOW,"pICE: ABORT (initial memory map not found)\n");
107 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
108 while(!GetKeyPolled());
109 UnloadSymbols();
110 ConsoleShutdown();
111 LEAVE_FUNC();
112 return FALSE;
113 }
114 DPRINT((0,"init_mm @ %X\n",my_init_mm));
115
116 DPRINT((0,"InitPICE(): trace step 7\n"));
117 // load the file /boot/System.map.
118 // !!! It must be consistent with the current kernel at all cost!!!
119 if(!LoadExports())
120 {
121 Print(OUTPUT_WINDOW,"pICE: failed to load exports\n");
122 Print(OUTPUT_WINDOW,"press any key to continue...\n");
123 while(!GetKeyPolled());
124 UnloadSymbols();
125 ConsoleShutdown();
126 LEAVE_FUNC();
127 return FALSE;
128 }
129
130 DPRINT((0,"InitPICE(): trace step 8\n"));
131 // end of the kernel
132 ScanExports("_end",(PULONG)&kernel_end);
133 if(!kernel_end)
134 {
135 Print(OUTPUT_WINDOW,"pICE: ABORT (kernel size is unknown)\n");
136 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
137 while(!GetKeyPolled());
138 UnloadExports();
139 UnloadSymbols();
140 ConsoleShutdown();
141 LEAVE_FUNC();
142 return FALSE;
143 }
144
145 DPRINT((0,"InitPICE(): trace step 9\n"));
146 // the loaded module list
147 ScanExports("module_list",(PULONG)&pmodule_list);
148 if(!pmodule_list)
149 {
150 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't retreive kernel module list)\n");
151 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
152 while(!GetKeyPolled());
153 UnloadExports();
154 UnloadSymbols();
155 ConsoleShutdown();
156 LEAVE_FUNC();
157 return FALSE;
158 }
159
160 DPRINT((0,"InitPICE(): trace step 10\n"));
161 // setup a fake module struct for use by symbol routines
162 if(!InitFakeKernelModule())
163 {
164 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't initialize kernel module)\n");
165 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
166 while(!GetKeyPolled());
167 UnloadExports();
168 UnloadSymbols();
169 ConsoleShutdown();
170 LEAVE_FUNC();
171 return FALSE;
172 }
173
174 DPRINT((0,"InitPICE(): trace step 11\n"));
175 // do a sanity check on exports
176 if(!SanityCheckExports())
177 {
178 Print(OUTPUT_WINDOW,"pICE: ABORT (exports are conflicting with kernel symbols)\n");
179 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
180 while(!GetKeyPolled());
181 UnloadExports();
182 UnloadSymbols();
183 ConsoleShutdown();
184 LEAVE_FUNC();
185 return FALSE;
186 }
187
188 DPRINT((0,"InitPICE(): trace step 12\n"));
189
190
191 DPRINT((0,"InitPICE(): trace step 13\n"));
192 // patch the keyboard driver
193
194 if(PatchKeyboardDriver())
195 {
196 Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't patch keyboard driver)\n");
197 Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n");
198 while(!GetKeyPolled());
199 UnloadSymbols();
200 UnloadExports();
201 ConsoleShutdown();
202 LEAVE_FUNC();
203 return FALSE;
204 }
205
206 DPRINT((0,"InitPICE(): trace step 14\n"));
207 // partial init of shadow registers
208 CurrentCS = GLOBAL_CODE_SEGMENT;
209 CurrentEIP = (ULONG)RealIsr;
210
211 CurrentDS = CurrentSS = GLOBAL_DATA_SEGMENT;
212 __asm__("
213 mov %%esp,%%eax
214 mov %%eax,CurrentESP
215 ":::"eax");
216
217
218 // display version and symbol information
219 Ver(NULL);
220
221 // disable HW breakpoints
222 __asm__("
223 xorl %%eax,%%eax
224 mov %%eax,%%dr6
225 mov %%eax,%%dr7
226 mov %%dr0,%%eax
227 mov %%dr1,%%eax
228 mov %%dr2,%%eax
229 mov %%dr3,%%eax"
230 :::"eax"
231 );
232
233 DPRINT((0,"InitPICE(): trace step 15\n"));
234 TakeIdtSnapshot();
235
236 DPRINT((0,"InitPICE(): trace step 16\n"));
237 // install all hooks
238 InstallTraceHook();
239 InstallGlobalKeyboardHook();
240 InstallSyscallHook();
241 InstallInt3Hook();
242 InstallPrintkHook();
243 InstallDblFltHook();
244 InstallGPFaultHook();
245 InstallIntEHook();
246
247 DPRINT((0,"InitPICE(): trace step 16\n"));
248 if(ulDoInitialBreak)
249 {
250 DPRINT((0,"about to do initial break...\n"));
251
252 // simulate an initial break
253 __asm__("
254 pushfl
255 pushl %cs
256 pushl $initialreturnpoint
257 pushl $" STR(REASON_CTRLF) "
258 jmp NewInt31Handler
259 initialreturnpoint:");
260 }
261 else
262 {
263 // display register contents
264 DisplayRegs();
265
266 // display data window
267 Args.Value[0]=CurrentDS;
268 Args.Value[1]=CurrentEIP;
269 Args.Count=2;
270 DisplayMemory(&Args);
271
272 // disassembly from current address
273 Args.Value[0]=CurrentCS;
274 Args.Value[1]=CurrentEIP;
275 Args.Count=2;
276 Unassemble(&Args);
277 }
278
279 DPRINT((0,"InitPICE(): trace step 17\n"));
280 InitPiceRunningTimer();
281
282 LEAVE_FUNC();
283 return TRUE;
284 }
285
286 //*************************************************************************
287 // CleanUpPICE()
288 //
289 //*************************************************************************
290 void CleanUpPICE(void)
291 {
292 DPRINT((0,"CleanUpPICE(): trace step 1\n"));
293 RemovePiceRunningTimer();
294
295 DPRINT((0,"CleanUpPICE(): trace step 2\n"));
296 // de-install all hooks
297 DeInstallGlobalKeyboardHook();
298 DeInstallSyscallHook();
299 DeInstallInt3Hook();
300 DeInstallPrintkHook();
301 DeInstallDblFltHook();
302 DeInstallGPFaultHook();
303 DeInstallIntEHook();
304 DeInstallTraceHook();
305
306 DPRINT((0,"CleanUpPICE(): trace step 3\n"));
307 RestoreIdt();
308
309 DPRINT((0,"CleanUpPICE(): trace step 4\n"));
310 UnloadExports(); // don't use ScanExports() after this
311 UnloadSymbols();
312
313 DPRINT((0,"CleanUpPICE(): trace step 5\n"));
314 // restore patch of keyboard driver
315 RestoreKeyboardDriver();
316
317 DPRINT((0,"CleanUpPICE(): trace step 6\n"));
318 Print(OUTPUT_WINDOW,"pICE: shutting down...\n");
319
320 DPRINT((0,"CleanUpPICE(): trace step 7\n"));
321 // cleanup the console
322 ConsoleShutdown();
323 }