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 * PROGRAMMERS: David Welch (welch@cwcom.net)
24 * Skywing (skywing@valhallalegends.com)
27 * 09/12/03: KeRaiseUserException added (Skywing).
30 /* INCLUDES *****************************************************************/
34 #include <internal/debug.h>
36 /* GLOBALS *****************************************************************/
38 #define FLAG_IF (1<<9)
41 #define STR(x) _STR(x)
43 extern void interrupt_handler2e(void);
44 extern void interrupt_handler2d(void);
46 extern VOID
KiTrap0(VOID
);
47 extern VOID
KiTrap1(VOID
);
48 extern VOID
KiTrap2(VOID
);
49 extern VOID
KiTrap3(VOID
);
50 extern VOID
KiTrap4(VOID
);
51 extern VOID
KiTrap5(VOID
);
52 extern VOID
KiTrap6(VOID
);
53 extern VOID
KiTrap7(VOID
);
54 extern VOID
KiTrap8(VOID
);
55 extern VOID
KiTrap9(VOID
);
56 extern VOID
KiTrap10(VOID
);
57 extern VOID
KiTrap11(VOID
);
58 extern VOID
KiTrap12(VOID
);
59 extern VOID
KiTrap13(VOID
);
60 extern VOID
KiTrap14(VOID
);
61 extern VOID
KiTrap15(VOID
);
62 extern VOID
KiTrap16(VOID
);
63 extern VOID
KiTrapUnknown(VOID
);
65 extern ULONG init_stack
;
66 extern ULONG init_stack_top
;
68 static char *ExceptionTypeStrings
[] =
75 "BOUND range exceeded",
77 "No Math Coprocessor",
81 "Segment Not Present",
82 "Stack Segment Fault",
90 static NTSTATUS ExceptionToNtStatus
[] =
92 STATUS_INTEGER_DIVIDE_BY_ZERO
,
94 STATUS_ACCESS_VIOLATION
,
96 STATUS_INTEGER_OVERFLOW
,
97 STATUS_ARRAY_BOUNDS_EXCEEDED
,
98 STATUS_ILLEGAL_INSTRUCTION
,
99 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
100 STATUS_ACCESS_VIOLATION
,
101 STATUS_ACCESS_VIOLATION
,
102 STATUS_ACCESS_VIOLATION
,
103 STATUS_ACCESS_VIOLATION
,
104 STATUS_STACK_OVERFLOW
,
105 STATUS_ACCESS_VIOLATION
,
106 STATUS_ACCESS_VIOLATION
,
107 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
108 STATUS_DATATYPE_MISALIGNMENT
,
109 STATUS_ACCESS_VIOLATION
112 /* FUNCTIONS ****************************************************************/
116 KeRosPrintAddress(PVOID address
)
118 KdbPrintAddress(address
);
123 KeRosPrintAddress(PVOID address
)
125 PLIST_ENTRY current_entry
;
126 MODULE_TEXT_SECTION
* current
;
127 extern LIST_ENTRY ModuleTextListHead
;
128 ULONG_PTR RelativeAddress
;
130 current_entry
= ModuleTextListHead
.Flink
;
132 while (current_entry
!= &ModuleTextListHead
&&
133 current_entry
!= NULL
)
136 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
138 if (address
>= (PVOID
)current
->Base
&&
139 address
< (PVOID
)(current
->Base
+ current
->Length
))
141 RelativeAddress
= (ULONG_PTR
) address
- current
->Base
;
142 DbgPrint("<%ws: %x>", current
->Name
, RelativeAddress
);
145 current_entry
= current_entry
->Flink
;
152 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
156 Er
.ExceptionFlags
= 0;
157 Er
.ExceptionRecord
= NULL
;
158 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
160 if (ExceptionNr
== 14)
162 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
163 Er
.NumberParameters
= 2;
164 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
165 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
169 if (ExceptionNr
< 16)
171 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
175 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
177 Er
.NumberParameters
= 0;
180 Er
.ExceptionFlags
= ((NTSTATUS
) STATUS_SINGLE_STEP
== (NTSTATUS
) Er
.ExceptionCode
181 || (NTSTATUS
) STATUS_BREAKPOINT
== (NTSTATUS
) Er
.ExceptionCode
) ?
182 EXCEPTION_NONCONTINUABLE
: 0;
184 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
190 KiDoubleFaultHandler(VOID
)
196 ULONG ExceptionNr
= 8;
202 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
203 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
204 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
209 OldTss
= KeGetCurrentKPCR()->TSS
;
213 #if defined(__GNUC__)
214 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2_
));
215 #elif defined(_MSC_VER)
219 #error Unknown compiler for inline assembler
222 if (PsGetCurrentThread() != NULL
&&
223 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
226 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
234 * Check for stack underflow
236 if (PsGetCurrentThread() != NULL
&&
237 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
239 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
240 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
245 * Print out the CPU registers
247 if (ExceptionNr
< 19)
249 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
254 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
256 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
257 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
259 DbgPrint("cr2 %x cr3 %x ", cr2_
, OldCr3
);
260 DbgPrint("Proc: %x ",PsGetCurrentProcess());
261 if (PsGetCurrentProcess() != NULL
)
263 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
264 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
266 if (PsGetCurrentThread() != NULL
)
268 DbgPrint("Thrd: %x Tid: %x",
269 PsGetCurrentThread(),
270 PsGetCurrentThread()->Cid
.UniqueThread
);
273 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
274 OldTss
->Fs
, OldTss
->Gs
);
275 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
277 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n ESP: %.8x", OldTss
->Edx
,
278 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
279 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
280 if (OldTss
->Cs
== KERNEL_CS
)
282 DbgPrint("kESP %.8x ", Esp0
);
283 if (PsGetCurrentThread() != NULL
)
285 DbgPrint("kernel stack base %x\n",
286 PsGetCurrentThread()->Tcb
.StackLimit
);
292 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
294 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
296 if (PsGetCurrentThread() != NULL
)
298 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
299 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
303 StackLimit
= (ULONG
)&init_stack_top
;
304 StackBase
= (ULONG
)&init_stack
;
308 Change to an #if 0 to reduce the amount of information printed on
309 a recursive stack trace.
312 DbgPrint("Frames: ");
313 Frame
= (PULONG
)OldTss
->Ebp
;
314 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
316 KeRosPrintAddress((PVOID
)Frame
[1]);
317 Frame
= (PULONG
)Frame
[0];
321 DbgPrint("Frames: ");
323 Frame
= (PULONG
)OldTss
->Ebp
;
324 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
326 StackTrace
[i
] = (PVOID
)Frame
[1];
327 Frame
= (PULONG
)Frame
[0];
333 while (i
< TraceLength
)
335 StackRepeatCount
[i
] = 0;
338 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
340 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
341 (j
- i
) * sizeof(PVOID
)) == 0)
343 StackRepeatCount
[i
] = 2;
344 StackRepeatLength
[i
] = j
- i
;
352 if (FoundRepeat
== FALSE
)
357 j
= j
+ StackRepeatLength
[i
];
358 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
361 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
362 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
364 StackRepeatCount
[i
]++;
365 j
= j
+ StackRepeatLength
[i
];
376 while (i
< TraceLength
)
378 if (StackRepeatCount
[i
] == 0)
380 KeRosPrintAddress(StackTrace
[i
]);
386 if (StackRepeatLength
[i
] == 0)
390 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
392 KeRosPrintAddress(StackTrace
[i
+ j
]);
394 DbgPrint("}*%d", StackRepeatCount
[i
]);
395 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
407 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
414 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
415 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
420 * Print out the CPU registers
422 if (ExceptionNr
< 19)
424 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
425 ExceptionNr
, Tf
->ErrorCode
&0xffff);
429 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
431 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
432 Tf
->Cs
&0xffff, Tf
->Eip
);
433 KeRosPrintAddress((PVOID
)Tf
->Eip
);
435 Ke386GetPageTableDirectory(cr3_
);
436 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
437 DbgPrint("Proc: %x ",PsGetCurrentProcess());
438 if (PsGetCurrentProcess() != NULL
)
440 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
441 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
443 if (PsGetCurrentThread() != NULL
)
445 DbgPrint("Thrd: %x Tid: %x",
446 PsGetCurrentThread(),
447 PsGetCurrentThread()->Cid
.UniqueThread
);
450 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
451 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
452 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
453 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
454 Tf
->Ebp
, Tf
->Esi
, Esp0
);
455 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
456 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
458 DbgPrint("kESP %.8x ", Esp0
);
459 if (PsGetCurrentThread() != NULL
)
461 DbgPrint("kernel stack base %x\n",
462 PsGetCurrentThread()->Tcb
.StackLimit
);
467 if (PsGetCurrentThread() != NULL
)
469 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
473 StackLimit
= (ULONG
)&init_stack_top
;
477 * Dump the stack frames
479 DbgPrint("Frames: ");
480 /* Change to an #if 0 if no frames are printed because of fpo. */
483 Frame
= (PULONG
)Tf
->Ebp
;
484 while (Frame
!= NULL
)
488 Status
= MmSafeCopyFromUser(&Eip
, Frame
+ 1, sizeof(Eip
));
489 if (!NT_SUCCESS(Status
))
491 DbgPrint("<INVALID>");
494 if (!KeRosPrintAddress(Eip
))
496 DbgPrint("<%X>", Eip
);
499 Status
= MmSafeCopyFromUser(&Frame
, Frame
, sizeof(Frame
));
500 if (!NT_SUCCESS(Status
))
509 Frame
= (PULONG
)((ULONG_PTR
)Esp0
+ KTRAP_FRAME_EFLAGS
);
510 while (Frame
< (PULONG
)PsGetCurrentThread()->Tcb
.StackBase
&& i
< 50)
512 ULONG Address
= *Frame
;
513 if (KeRosPrintAddress((PVOID
)Address
))
523 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
525 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
526 * message and halt the computer
528 * Complete CPU context
535 /* Store the exception number in an unused field in the trap frame. */
536 Tf
->DebugArgMark
= (PVOID
)ExceptionNr
;
538 /* Use the address of the trap frame as approximation to the ring0 esp */
539 Esp0
= (ULONG
)&Tf
->Eip
;
542 #if defined(__GNUC__)
543 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2_
));
544 #elif defined(_MSC_VER)
548 #error Unknown compiler for inline assembler
550 Tf
->DebugPointer
= (PVOID
)cr2_
;
553 * If this was a V86 mode exception then handle it specially
555 if (Tf
->Eflags
& (1 << 17))
557 return(KeV86Exception(ExceptionNr
, Tf
, cr2_
));
561 * Check for stack underflow, this may be obsolete
563 if (PsGetCurrentThread() != NULL
&&
564 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
566 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
567 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
572 * Maybe handle the page fault and return
574 if (ExceptionNr
== 14)
576 if (Tf
->Eflags
& FLAG_IF
)
578 Ke386EnableInterrupts();
580 Status
= MmPageFault(Tf
->Cs
&0xffff,
585 if (NT_SUCCESS(Status
))
592 * Check for a breakpoint that was only for the attention of the debugger.
594 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
597 EIP is already adjusted by the processor to point to the instruction
598 after the breakpoint.
604 * Handle user exceptions differently
606 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
608 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2_
));
612 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2_
));
617 KeDumpStackFrames(PULONG Frame
)
619 DbgPrint("Frames: ");
620 while ( MmIsAddressValid(Frame
) )
622 if (!KeRosPrintAddress((PVOID
)Frame
[1]))
624 DbgPrint("<%X>", (PVOID
)Frame
[1]);
626 Frame
= (PULONG
)Frame
[0];
633 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
637 DbgPrint("Frames: ");
640 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
641 Frame
= (PULONG
)Frame
[0]; // step out of KeRosDumpStackFrames
643 while ( MmIsAddressValid(Frame
) && i
++ < FrameCount
)
645 if (!KeRosPrintAddress((PVOID
)Frame
[1]))
647 DbgPrint("<%X>", (PVOID
)Frame
[1]);
649 if (Frame
[1] == 0xdeadbeef)
653 Frame
= (PULONG
)Frame
[0];
659 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
661 DPRINT("sel %x %d\n",sel
,sel
);
662 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
664 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
665 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
668 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
670 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
671 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
673 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
676 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
678 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
680 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
682 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
686 set_task_gate(unsigned int sel
, unsigned task_sel
)
688 KiIdt
[sel
].a
= task_sel
<< 16;
689 KiIdt
[sel
].b
= 0x8500;
693 KeInitExceptions(VOID
)
695 * FUNCTION: Initalize CPU exception handling
700 DPRINT("KeInitExceptions()\n");
703 * Set up the other gates
705 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
706 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
707 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
708 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
709 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
710 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
711 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
712 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
713 set_task_gate(8, TRAP_TSS_SELECTOR
);
714 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
715 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
716 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
717 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
718 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
719 set_interrupt_gate(14, (ULONG
)KiTrap14
);
720 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
721 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
725 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
728 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
729 set_system_call_gate(0x2e,(int)interrupt_handler2e
);
737 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
739 /* FIXME: This needs SEH */
741 PKTHREAD Thread
= KeGetCurrentThread();
743 ProbeForWrite(&Thread
->Teb
->ExceptionCode
, sizeof(NTSTATUS
), sizeof(NTSTATUS
)); /* NT doesn't check this -- bad? */
744 OldEip
= Thread
->TrapFrame
->Eip
;
745 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
746 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
747 return((NTSTATUS
)OldEip
);
752 KeRosTrapReturn ( PKTRAP_FRAME TrapFrame
, PKTRAP_FRAME PrevTrapFrame
);
759 IN PEXCEPTION_RECORD ExceptionRecord
,
761 IN BOOLEAN SearchFrames
)
763 PKTRAP_FRAME TrapFrame
= KeGetCurrentThread()->TrapFrame
;
764 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
766 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
768 KiDispatchException(ExceptionRecord
,
770 PsGetCurrentThread()->Tcb
.TrapFrame
,
771 (KPROCESSOR_MODE
)ExGetPreviousMode(),
774 KeRosTrapReturn ( TrapFrame
, PrevTrapFrame
);
775 return(STATUS_SUCCESS
);