3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/ke/i386/exp.c
22 * PURPOSE: Handling exceptions
23 * PROGRAMMER: David Welch (welch@cwcom.net)
28 /* INCLUDES *****************************************************************/
30 #include <ddk/ntddk.h>
31 #include <internal/ntoskrnl.h>
32 #include <internal/ke.h>
33 #include <internal/i386/segment.h>
34 #include <internal/i386/mm.h>
35 #include <internal/module.h>
36 #include <internal/mm.h>
37 #include <internal/ps.h>
38 #include <internal/trap.h>
41 #include <internal/debug.h>
43 /* GLOBALS *****************************************************************/
46 #define STR(x) _STR(x)
48 extern void interrupt_handler2e(void);
49 extern void interrupt_handler2d(void);
51 extern VOID
KiTrap0(VOID
);
52 extern VOID
KiTrap1(VOID
);
53 extern VOID
KiTrap2(VOID
);
54 extern VOID
KiTrap3(VOID
);
55 extern VOID
KiTrap4(VOID
);
56 extern VOID
KiTrap5(VOID
);
57 extern VOID
KiTrap6(VOID
);
58 extern VOID
KiTrap7(VOID
);
59 extern VOID
KiTrap8(VOID
);
60 extern VOID
KiTrap9(VOID
);
61 extern VOID
KiTrap10(VOID
);
62 extern VOID
KiTrap11(VOID
);
63 extern VOID
KiTrap12(VOID
);
64 extern VOID
KiTrap13(VOID
);
65 extern VOID
KiTrap14(VOID
);
66 extern VOID
KiTrap15(VOID
);
67 extern VOID
KiTrap16(VOID
);
68 extern VOID
KiTrapUnknown(VOID
);
70 extern ULONG init_stack
;
71 extern ULONG init_stack_top
;
73 /* FUNCTIONS ****************************************************************/
75 extern unsigned int _text_start__
, _text_end__
;
78 print_address(PVOID address
)
80 PLIST_ENTRY current_entry
;
81 PMODULE_OBJECT current
;
82 extern LIST_ENTRY ModuleListHead
;
84 current_entry
= ModuleListHead
.Flink
;
86 while (current_entry
!= &ModuleListHead
&&
87 current_entry
!= NULL
)
89 current
= CONTAINING_RECORD(current_entry
, MODULE_OBJECT
, ListEntry
);
91 if (address
>= current
->Base
&&
92 address
< (current
->Base
+ current
->Length
))
94 DbgPrint("<%wZ: %x>", ¤t
->FullName
,
95 address
- current
->Base
);
99 current_entry
= current_entry
->Flink
;
102 DbgPrint("<%x>", address
);
108 KiUserTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
112 if (ExceptionNr
== 0)
114 Er
.ExceptionCode
= STATUS_INTEGER_DIVIDE_BY_ZERO
;
116 else if (ExceptionNr
== 1)
118 Er
.ExceptionCode
= STATUS_SINGLE_STEP
;
120 else if (ExceptionNr
== 3)
122 Er
.ExceptionCode
= STATUS_BREAKPOINT
;
124 else if (ExceptionNr
== 4)
126 Er
.ExceptionCode
= STATUS_INTEGER_OVERFLOW
;
128 else if (ExceptionNr
== 5)
130 Er
.ExceptionCode
= STATUS_ARRAY_BOUNDS_EXCEEDED
;
132 else if (ExceptionNr
== 6)
134 Er
.ExceptionCode
= STATUS_ILLEGAL_INSTRUCTION
;
138 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
140 Er
.ExceptionFlags
= 0;
141 Er
.ExceptionRecord
= NULL
;
142 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
143 if (ExceptionNr
== 14)
145 Er
.NumberParameters
= 2;
146 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
147 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
151 Er
.NumberParameters
= 0;
155 KiDispatchException(&Er
, 0, Tf
, UserMode
, TRUE
);
160 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
162 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
163 * message and halt the computer
165 * Complete CPU context
168 unsigned int cr2
, cr3
;
170 // unsigned int j, sym;
174 static char *TypeStrings
[] =
181 "BOUND range exceeded",
183 "No Math Coprocessor",
187 "Segment Not Present",
188 "Stack Segment Fault",
189 "General Protection",
196 /* Use the address of the trap frame as approximation to the ring0 esp */
200 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
203 * If this was a V86 mode exception then handle it specially
205 if (Tf
->Eflags
& (1 << 17))
207 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
211 * Check for stack underflow
213 if (PsGetCurrentThread() != NULL
&&
214 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
216 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
217 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
221 if (ExceptionNr
== 14)
224 Status
= MmPageFault(Tf
->Cs
&0xffff,
229 if (NT_SUCCESS(Status
))
236 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
238 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
243 * Print out the CPU registers
245 if (ExceptionNr
< 19)
247 DbgPrint("%s Exception: %d(%x)\n",TypeStrings
[ExceptionNr
],
248 ExceptionNr
, Tf
->ErrorCode
&0xffff);
252 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
254 DbgPrint("CS:EIP %x:%x ", Tf
->Cs
&0xffff, Tf
->Eip
);
255 print_address((PVOID
)Tf
->Eip
);
257 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
258 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3
);
259 DbgPrint("Proc: %x ",PsGetCurrentProcess());
260 if (PsGetCurrentProcess() != NULL
)
262 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
263 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
265 if (PsGetCurrentThread() != NULL
)
267 DbgPrint("Thrd: %x Tid: %x",
268 PsGetCurrentThread(),
269 PsGetCurrentThread()->Cid
.UniqueThread
);
272 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
273 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
274 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
275 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", Tf
->Edx
, Tf
->Ebp
, Tf
->Esi
);
276 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
277 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
279 DbgPrint("kESP %.8x ", Esp0
);
280 if (PsGetCurrentThread() != NULL
)
282 DbgPrint("kernel stack base %x\n",
283 PsGetCurrentThread()->Tcb
.StackLimit
);
289 DbgPrint("User ESP %.8x\n", Tf
->Esp
);
291 if ((Tf
->Cs
& 0xffff) == KERNEL_CS
)
293 DbgPrint("ESP %x\n", Esp0
);
294 stack
= (PULONG
) (Esp0
+ 24);
295 stack
= (PULONG
)(((ULONG
)stack
) & (~0x3));
297 DbgPrint("stack<%p>: ", stack
);
299 for (i
= 0; i
< 18; i
= i
+ 6)
301 DbgPrint("%.8x %.8x %.8x %.8x\n",
302 stack
[i
], stack
[i
+1],
303 stack
[i
+2], stack
[i
+3],
304 stack
[i
+4], stack
[i
+5]);
306 DbgPrint("Frames:\n");
307 for (i
= 0; i
< 32; i
++)
309 if (stack
[i
] > ((unsigned int) &_text_start__
) &&
310 !(stack
[i
] >= ((ULONG
)&init_stack
) &&
311 stack
[i
] <= ((ULONG
)&init_stack_top
)))
313 // DbgPrint(" %.8x", stack[i]);
314 print_address((PVOID
)stack
[i
]);
322 DbgPrint("SS:ESP %x:%x\n", Tf
->Ss
, Tf
->Esp
);
323 stack
=(PULONG
)(Tf
->Esp
);
325 DbgPrint("Stack:\n");
328 if (MmIsPagePresent(NULL
,&stack
[i
]))
330 DbgPrint("%.8x ",stack
[i
]);
338 if (MmIsPagePresent(NULL
, (PVOID
)Tf
->Eip
))
340 unsigned char instrs
[512];
342 memcpy(instrs
, (PVOID
)Tf
->Eip
, 512);
344 DbgPrint("Instrs: ");
348 DbgPrint("%x ", instrs
[i
]);
355 if ((Tf
->Cs
&0xffff) == USER_CS
&&
356 Tf
->Eip
< KERNEL_BASE
)
358 DbgPrint("Killing current task\n");
359 KeLowerIrql(PASSIVE_LEVEL
);
360 if ((Tf
->Cs
&0xffff) == USER_CS
)
362 ZwTerminateProcess(NtCurrentProcess(),
363 STATUS_NONCONTINUABLE_EXCEPTION
);
369 VOID
KeDumpStackFrames(PVOID _Stack
, ULONG NrFrames
)
371 PULONG Stack
= (PULONG
)_Stack
;
374 Stack
= (PVOID
)(((ULONG
)Stack
) & (~0x3));
375 DbgPrint("Stack: %x\n", Stack
);
376 if (PsGetCurrentThread() != NULL
)
378 DbgPrint("kernel stack base %x\n",
379 PsGetCurrentThread()->Tcb
.StackLimit
);
382 DbgPrint("Frames:\n");
383 for (i
=0; i
<NrFrames
; i
++)
385 // if (Stack[i] > KERNEL_BASE && Stack[i] < ((ULONG)&etext))
386 if (Stack
[i
] > KERNEL_BASE
)
388 // DbgPrint("%.8x ",Stack[i]);
389 print_address((PVOID
)Stack
[i
]);
392 if (Stack
[i
] == 0xceafbeef)
400 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
402 DPRINT("sel %x %d\n",sel
,sel
);
403 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
405 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
406 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
409 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
411 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
412 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
414 KiIdt
[sel
].b
= 0x8f00 + (((int)func
)&0xffff0000);
417 void KeInitExceptions(void)
419 * FUNCTION: Initalize CPU exception handling
424 DPRINT("KeInitExceptions()\n",0);
426 set_interrupt_gate(0, (ULONG
)KiTrap0
);
427 set_interrupt_gate(1, (ULONG
)KiTrap1
);
428 set_interrupt_gate(2, (ULONG
)KiTrap2
);
429 set_interrupt_gate(3, (ULONG
)KiTrap3
);
430 set_interrupt_gate(4, (ULONG
)KiTrap4
);
431 set_interrupt_gate(5, (ULONG
)KiTrap5
);
432 set_interrupt_gate(6, (ULONG
)KiTrap6
);
433 set_interrupt_gate(7, (ULONG
)KiTrap7
);
434 set_interrupt_gate(8, (ULONG
)KiTrap8
);
435 set_interrupt_gate(9, (ULONG
)KiTrap9
);
436 set_interrupt_gate(10, (ULONG
)KiTrap10
);
437 set_interrupt_gate(11, (ULONG
)KiTrap11
);
438 set_interrupt_gate(12, (ULONG
)KiTrap12
);
439 set_interrupt_gate(13, (ULONG
)KiTrap13
);
440 set_interrupt_gate(14, (ULONG
)KiTrap14
);
441 set_interrupt_gate(15, (ULONG
)KiTrap15
);
442 set_interrupt_gate(16, (ULONG
)KiTrap16
);
446 set_interrupt_gate(i
,(int)KiTrapUnknown
);
449 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
450 set_system_call_gate(0x2e,(int)interrupt_handler2e
);