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 static char *ExceptionTypeStrings
[] =
80 "BOUND range exceeded",
82 "No Math Coprocessor",
86 "Segment Not Present",
87 "Stack Segment Fault",
95 /* FUNCTIONS ****************************************************************/
97 extern unsigned int _text_start__
, _text_end__
;
100 print_address(PVOID address
)
104 PSYMBOL Symbol
, NextSymbol
;
105 BOOLEAN Printed
= FALSE
;
108 PLIST_ENTRY current_entry
;
109 MODULE_TEXT_SECTION
* current
;
110 extern LIST_ENTRY ModuleTextListHead
;
112 current_entry
= ModuleTextListHead
.Flink
;
114 while (current_entry
!= &ModuleTextListHead
&&
115 current_entry
!= NULL
)
118 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
120 if (address
>= (PVOID
)current
->Base
&&
121 address
< (PVOID
)(current
->Base
+ current
->Length
))
126 Offset
= (ULONG
)((ULONG
)address
- current
->Base
);
127 Symbol
= current
->Symbols
.Symbols
;
128 while (Symbol
!= NULL
)
130 NextSymbol
= Symbol
->Next
;
131 if (NextSymbol
!= NULL
)
132 NextAddress
= NextSymbol
->RelativeAddress
;
134 NextAddress
= current
->Length
;
136 if ((Offset
>= Symbol
->RelativeAddress
) &&
137 (Offset
< NextAddress
))
139 DbgPrint("<%ws: %x (%wZ)>", current
->Name
, Offset
, &Symbol
->Name
);
146 DbgPrint("<%ws: %x>", current
->Name
, Offset
);
150 DbgPrint("<%ws: %x>", current
->Name
,
151 address
- current
->Base
);
158 current_entry
= current_entry
->Flink
;
165 KiUserTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
169 if (ExceptionNr
== 0)
171 Er
.ExceptionCode
= STATUS_INTEGER_DIVIDE_BY_ZERO
;
173 else if (ExceptionNr
== 1)
175 Er
.ExceptionCode
= STATUS_SINGLE_STEP
;
177 else if (ExceptionNr
== 3)
179 Er
.ExceptionCode
= STATUS_BREAKPOINT
;
181 else if (ExceptionNr
== 4)
183 Er
.ExceptionCode
= STATUS_INTEGER_OVERFLOW
;
185 else if (ExceptionNr
== 5)
187 Er
.ExceptionCode
= STATUS_ARRAY_BOUNDS_EXCEEDED
;
189 else if (ExceptionNr
== 6)
191 Er
.ExceptionCode
= STATUS_ILLEGAL_INSTRUCTION
;
195 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
197 Er
.ExceptionFlags
= 0;
198 Er
.ExceptionRecord
= NULL
;
199 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
200 if (ExceptionNr
== 14)
202 Er
.NumberParameters
= 2;
203 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
204 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
208 Er
.NumberParameters
= 0;
212 KiDispatchException(&Er
, 0, Tf
, UserMode
, TRUE
);
218 KiUserTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
227 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
230 * Print out the CPU registers
232 if (ExceptionNr
< 19)
234 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
235 ExceptionNr
, Tf
->ErrorCode
&0xffff);
239 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
241 DbgPrint("CS:EIP %x:%x ", Tf
->Cs
&0xffff, Tf
->Eip
);
242 print_address((PVOID
)Tf
->Eip
);
244 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
245 DbgPrint("CR2 %x CR3 %x ", Cr2
, cr3
);
246 DbgPrint("Proc: %x ",PsGetCurrentProcess());
247 if (PsGetCurrentProcess() != NULL
)
249 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
250 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
252 if (PsGetCurrentThread() != NULL
)
254 DbgPrint("Thrd: %x Tid: %x",
255 PsGetCurrentThread(),
256 PsGetCurrentThread()->Cid
.UniqueThread
);
259 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
260 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
261 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
262 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", Tf
->Edx
, Tf
->Ebp
, Tf
->Esi
);
263 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
264 DbgPrint("SS:ESP %x:%x\n", Tf
->Ss
, Tf
->Esp
);
267 stack
=(PULONG
)(Tf
->Esp
);
269 DbgPrint("Stack:\n");
272 if (MmIsPagePresent(NULL
,&stack
[i
]))
274 DbgPrint("%.8x ",stack
[i
]);
284 if (MmIsPagePresent(NULL
, (PVOID
)Tf
->Eip
))
286 unsigned char instrs
[512];
288 memcpy(instrs
, (PVOID
)Tf
->Eip
, 512);
290 DbgPrint("Instrs: ");
294 DbgPrint("%x ", instrs
[i
]);
300 * Dump the stack frames
302 DbgPrint("Frames: ");
304 Frame
= (PULONG
)Tf
->Ebp
;
305 while (Frame
!= NULL
)
307 DbgPrint("%.8x ", Frame
[1]);
308 Frame
= (PULONG
)Frame
[0];
317 * Kill the faulting task
320 ZwTerminateProcess(NtCurrentProcess(),
321 STATUS_NONCONTINUABLE_EXCEPTION
);
324 * If terminating the process fails then bugcheck
331 KiDoubleFaultHandler(VOID
)
338 ULONG ExceptionNr
= 8;
341 /* Use the address of the trap frame as approximation to the ring0 esp */
342 OldTss
= KeGetCurrentKPCR()->TSS
;
346 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
349 * Check for stack underflow
351 if (PsGetCurrentThread() != NULL
&&
352 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
354 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
355 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
360 * Print out the CPU registers
362 if (ExceptionNr
< 19)
364 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
369 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
371 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
372 print_address((PVOID
)OldTss
->Eip
);
374 DbgPrint("cr2 %x cr3 %x ", cr2
, OldTss
->Cr3
);
375 DbgPrint("Proc: %x ",PsGetCurrentProcess());
376 if (PsGetCurrentProcess() != NULL
)
378 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
379 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
381 if (PsGetCurrentThread() != NULL
)
383 DbgPrint("Thrd: %x Tid: %x",
384 PsGetCurrentThread(),
385 PsGetCurrentThread()->Cid
.UniqueThread
);
388 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
389 OldTss
->Fs
, OldTss
->Gs
);
390 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
392 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", OldTss
->Edx
, OldTss
->Ebp
,
394 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
395 if (OldTss
->Cs
== KERNEL_CS
)
397 DbgPrint("kESP %.8x ", Esp0
);
398 if (PsGetCurrentThread() != NULL
)
400 DbgPrint("kernel stack base %x\n",
401 PsGetCurrentThread()->Tcb
.StackLimit
);
407 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
409 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
411 DbgPrint("ESP %x\n", Esp0
);
412 stack
= (PULONG
) (Esp0
+ 24);
413 stack
= (PULONG
)(((ULONG
)stack
) & (~0x3));
414 if (PsGetCurrentThread() != NULL
)
416 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
420 StackLimit
= (ULONG
)&init_stack_top
;
423 DbgPrint("stack<%p>: ", stack
);
425 for (i
= 0; i
< 18 && (((ULONG
)&stack
[i
+5]) < StackLimit
); i
= i
+ 6)
427 DbgPrint("%.8x %.8x %.8x %.8x\n",
428 stack
[i
], stack
[i
+1],
429 stack
[i
+2], stack
[i
+3],
430 stack
[i
+4], stack
[i
+5]);
432 DbgPrint("Frames:\n");
433 for (i
= 0; i
< 32 && (((ULONG
)&stack
[i
]) < StackLimit
); i
++)
435 if (stack
[i
] > ((unsigned int) &_text_start__
) &&
436 !(stack
[i
] >= ((ULONG
)&init_stack
) &&
437 stack
[i
] <= ((ULONG
)&init_stack_top
)))
439 print_address((PVOID
)stack
[i
]);
450 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
452 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
453 * message and halt the computer
455 * Complete CPU context
458 unsigned int cr2
, cr3
;
460 // unsigned int j, sym;
466 /* Use the address of the trap frame as approximation to the ring0 esp */
467 Esp0
= (ULONG
)&Tf
->Eip
;
470 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
473 * If this was a V86 mode exception then handle it specially
475 if (Tf
->Eflags
& (1 << 17))
477 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
481 * Check for stack underflow, this may be obsolete
483 if (PsGetCurrentThread() != NULL
&&
484 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
486 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
487 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
492 * Maybe handle the page fault and return
494 if (ExceptionNr
== 14)
497 Status
= MmPageFault(Tf
->Cs
&0xffff,
502 if (NT_SUCCESS(Status
))
509 * Handle user exceptions differently
511 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
513 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
517 * Print out the CPU registers
519 if (ExceptionNr
< 19)
521 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
522 ExceptionNr
, Tf
->ErrorCode
&0xffff);
526 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
528 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
529 Tf
->Cs
&0xffff, Tf
->Eip
);
530 print_address((PVOID
)Tf
->Eip
);
532 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
533 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3
);
534 DbgPrint("Proc: %x ",PsGetCurrentProcess());
535 if (PsGetCurrentProcess() != NULL
)
537 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
538 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
540 if (PsGetCurrentThread() != NULL
)
542 DbgPrint("Thrd: %x Tid: %x",
543 PsGetCurrentThread(),
544 PsGetCurrentThread()->Cid
.UniqueThread
);
547 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
548 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
549 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
550 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", Tf
->Edx
, Tf
->Ebp
, Tf
->Esi
);
551 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
552 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
554 DbgPrint("kESP %.8x ", Esp0
);
555 if (PsGetCurrentThread() != NULL
)
557 DbgPrint("kernel stack base %x\n",
558 PsGetCurrentThread()->Tcb
.StackLimit
);
563 DbgPrint("ESP %x\n", Esp0
);
565 if (PsGetCurrentThread() != NULL
)
567 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
571 StackLimit
= (ULONG
)&init_stack_top
;
575 * Dump the stack frames
577 DbgPrint("Frames: ");
579 Frame
= (PULONG
)Tf
->Ebp
;
580 while (Frame
!= NULL
)
582 print_address((PVOID
)Frame
[1]);
583 Frame
= (PULONG
)Frame
[0];
595 KeDumpStackFrames(PULONG Frame
)
599 DbgPrint("Frames: ");
601 while (Frame
!= NULL
)
603 print_address((PVOID
)Frame
[1]);
604 Frame
= (PULONG
)Frame
[0];
613 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
615 DPRINT("sel %x %d\n",sel
,sel
);
616 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
618 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
619 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
622 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
624 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
625 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
627 KiIdt
[sel
].b
= 0x8f00 + (((int)func
)&0xffff0000);
631 set_task_gate(unsigned int sel
, unsigned task_sel
)
633 KiIdt
[sel
].a
= task_sel
<< 16;
634 KiIdt
[sel
].b
= 0x8500;
638 KeInitExceptions(VOID
)
640 * FUNCTION: Initalize CPU exception handling
645 DPRINT("KeInitExceptions()\n");
648 * Set up the other gates
650 set_interrupt_gate(0, (ULONG
)KiTrap0
);
651 set_interrupt_gate(1, (ULONG
)KiTrap1
);
652 set_interrupt_gate(2, (ULONG
)KiTrap2
);
653 set_interrupt_gate(3, (ULONG
)KiTrap3
);
654 set_interrupt_gate(4, (ULONG
)KiTrap4
);
655 set_interrupt_gate(5, (ULONG
)KiTrap5
);
656 set_interrupt_gate(6, (ULONG
)KiTrap6
);
657 set_interrupt_gate(7, (ULONG
)KiTrap7
);
658 set_task_gate(8, TRAP_TSS_SELECTOR
);
659 set_interrupt_gate(9, (ULONG
)KiTrap9
);
660 set_interrupt_gate(10, (ULONG
)KiTrap10
);
661 set_interrupt_gate(11, (ULONG
)KiTrap11
);
662 set_interrupt_gate(12, (ULONG
)KiTrap12
);
663 set_interrupt_gate(13, (ULONG
)KiTrap13
);
664 set_interrupt_gate(14, (ULONG
)KiTrap14
);
665 set_interrupt_gate(15, (ULONG
)KiTrap15
);
666 set_interrupt_gate(16, (ULONG
)KiTrap16
);
670 set_interrupt_gate(i
,(int)KiTrapUnknown
);
673 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
674 set_system_call_gate(0x2e,(int)interrupt_handler2e
);