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 *****************************************************************/
33 #include <ddk/ntddk.h>
34 #include <internal/ntoskrnl.h>
35 #include <internal/ke.h>
36 #include <internal/i386/segment.h>
37 #include <internal/i386/mm.h>
38 #include <internal/module.h>
39 #include <internal/mm.h>
40 #include <internal/ps.h>
41 #include <internal/trap.h>
42 #include <ntdll/ldr.h>
43 #include <internal/safe.h>
44 #include <internal/kd.h>
45 #include <internal/ldr.h>
48 #include <internal/debug.h>
50 /* GLOBALS *****************************************************************/
52 #define FLAG_IF (1<<9)
55 #define STR(x) _STR(x)
57 extern void interrupt_handler2e(void);
58 extern void interrupt_handler2d(void);
60 extern VOID
KiTrap0(VOID
);
61 extern VOID
KiTrap1(VOID
);
62 extern VOID
KiTrap2(VOID
);
63 extern VOID
KiTrap3(VOID
);
64 extern VOID
KiTrap4(VOID
);
65 extern VOID
KiTrap5(VOID
);
66 extern VOID
KiTrap6(VOID
);
67 extern VOID
KiTrap7(VOID
);
68 extern VOID
KiTrap8(VOID
);
69 extern VOID
KiTrap9(VOID
);
70 extern VOID
KiTrap10(VOID
);
71 extern VOID
KiTrap11(VOID
);
72 extern VOID
KiTrap12(VOID
);
73 extern VOID
KiTrap13(VOID
);
74 extern VOID
KiTrap14(VOID
);
75 extern VOID
KiTrap15(VOID
);
76 extern VOID
KiTrap16(VOID
);
77 extern VOID
KiTrapUnknown(VOID
);
79 extern ULONG init_stack
;
80 extern ULONG init_stack_top
;
82 static char *ExceptionTypeStrings
[] =
89 "BOUND range exceeded",
91 "No Math Coprocessor",
95 "Segment Not Present",
96 "Stack Segment Fault",
104 static NTSTATUS ExceptionToNtStatus
[] =
106 STATUS_INTEGER_DIVIDE_BY_ZERO
,
108 STATUS_ACCESS_VIOLATION
,
110 STATUS_INTEGER_OVERFLOW
,
111 STATUS_ARRAY_BOUNDS_EXCEEDED
,
112 STATUS_ILLEGAL_INSTRUCTION
,
113 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
114 STATUS_ACCESS_VIOLATION
,
115 STATUS_ACCESS_VIOLATION
,
116 STATUS_ACCESS_VIOLATION
,
117 STATUS_ACCESS_VIOLATION
,
118 STATUS_STACK_OVERFLOW
,
119 STATUS_ACCESS_VIOLATION
,
120 STATUS_ACCESS_VIOLATION
,
121 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
122 STATUS_DATATYPE_MISALIGNMENT
,
123 STATUS_ACCESS_VIOLATION
126 /* FUNCTIONS ****************************************************************/
130 KeRosPrintAddress(PVOID address
)
132 KdbPrintAddress(address
);
137 KeRosPrintAddress(PVOID address
)
139 PLIST_ENTRY current_entry
;
140 MODULE_TEXT_SECTION
* current
;
141 extern LIST_ENTRY ModuleTextListHead
;
142 ULONG_PTR RelativeAddress
;
144 current_entry
= ModuleTextListHead
.Flink
;
146 while (current_entry
!= &ModuleTextListHead
&&
147 current_entry
!= NULL
)
150 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
152 if (address
>= (PVOID
)current
->Base
&&
153 address
< (PVOID
)(current
->Base
+ current
->Length
))
155 RelativeAddress
= (ULONG_PTR
) address
- current
->Base
;
156 DbgPrint("<%ws: %x>", current
->Name
, RelativeAddress
);
159 current_entry
= current_entry
->Flink
;
166 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
170 Er
.ExceptionFlags
= 0;
171 Er
.ExceptionRecord
= NULL
;
172 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
174 if (ExceptionNr
== 14)
176 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
177 Er
.NumberParameters
= 2;
178 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
179 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
183 if (ExceptionNr
< 16)
185 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
189 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
191 Er
.NumberParameters
= 0;
194 Er
.ExceptionFlags
= ((NTSTATUS
) STATUS_SINGLE_STEP
== (NTSTATUS
) Er
.ExceptionCode
195 || (NTSTATUS
) STATUS_BREAKPOINT
== (NTSTATUS
) Er
.ExceptionCode
) ?
196 EXCEPTION_NONCONTINUABLE
: 0;
198 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
204 KiDoubleFaultHandler(VOID
)
210 ULONG ExceptionNr
= 8;
216 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
217 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
218 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
223 OldTss
= KeGetCurrentKPCR()->TSS
;
227 #if defined(__GNUC__)
228 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2_
));
229 #elif defined(_MSC_VER)
233 #error Unknown compiler for inline assembler
236 if (PsGetCurrentThread() != NULL
&&
237 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
240 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
248 * Check for stack underflow
250 if (PsGetCurrentThread() != NULL
&&
251 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
253 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
254 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
259 * Print out the CPU registers
261 if (ExceptionNr
< 19)
263 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
268 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
270 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
271 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
273 DbgPrint("cr2 %x cr3 %x ", cr2_
, OldCr3
);
274 DbgPrint("Proc: %x ",PsGetCurrentProcess());
275 if (PsGetCurrentProcess() != NULL
)
277 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
278 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
280 if (PsGetCurrentThread() != NULL
)
282 DbgPrint("Thrd: %x Tid: %x",
283 PsGetCurrentThread(),
284 PsGetCurrentThread()->Cid
.UniqueThread
);
287 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
288 OldTss
->Fs
, OldTss
->Gs
);
289 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
291 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n ESP: %.8x", OldTss
->Edx
,
292 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
293 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
294 if (OldTss
->Cs
== KERNEL_CS
)
296 DbgPrint("kESP %.8x ", Esp0
);
297 if (PsGetCurrentThread() != NULL
)
299 DbgPrint("kernel stack base %x\n",
300 PsGetCurrentThread()->Tcb
.StackLimit
);
306 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
308 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
310 if (PsGetCurrentThread() != NULL
)
312 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
313 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
317 StackLimit
= (ULONG
)&init_stack_top
;
318 StackBase
= (ULONG
)&init_stack
;
322 Change to an #if 0 to reduce the amount of information printed on
323 a recursive stack trace.
326 DbgPrint("Frames: ");
327 Frame
= (PULONG
)OldTss
->Ebp
;
328 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
330 KeRosPrintAddress((PVOID
)Frame
[1]);
331 Frame
= (PULONG
)Frame
[0];
335 DbgPrint("Frames: ");
337 Frame
= (PULONG
)OldTss
->Ebp
;
338 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
340 StackTrace
[i
] = (PVOID
)Frame
[1];
341 Frame
= (PULONG
)Frame
[0];
347 while (i
< TraceLength
)
349 StackRepeatCount
[i
] = 0;
352 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
354 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
355 (j
- i
) * sizeof(PVOID
)) == 0)
357 StackRepeatCount
[i
] = 2;
358 StackRepeatLength
[i
] = j
- i
;
366 if (FoundRepeat
== FALSE
)
371 j
= j
+ StackRepeatLength
[i
];
372 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
375 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
376 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
378 StackRepeatCount
[i
]++;
379 j
= j
+ StackRepeatLength
[i
];
390 while (i
< TraceLength
)
392 if (StackRepeatCount
[i
] == 0)
394 KeRosPrintAddress(StackTrace
[i
]);
400 if (StackRepeatLength
[i
] == 0)
404 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
406 KeRosPrintAddress(StackTrace
[i
+ j
]);
408 DbgPrint("}*%d", StackRepeatCount
[i
]);
409 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
421 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
428 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
429 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
434 * Print out the CPU registers
436 if (ExceptionNr
< 19)
438 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
439 ExceptionNr
, Tf
->ErrorCode
&0xffff);
443 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
445 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
446 Tf
->Cs
&0xffff, Tf
->Eip
);
447 KeRosPrintAddress((PVOID
)Tf
->Eip
);
449 Ke386GetPageTableDirectory(cr3_
);
450 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
451 DbgPrint("Proc: %x ",PsGetCurrentProcess());
452 if (PsGetCurrentProcess() != NULL
)
454 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
455 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
457 if (PsGetCurrentThread() != NULL
)
459 DbgPrint("Thrd: %x Tid: %x",
460 PsGetCurrentThread(),
461 PsGetCurrentThread()->Cid
.UniqueThread
);
464 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
465 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
466 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
467 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
468 Tf
->Ebp
, Tf
->Esi
, Esp0
);
469 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
470 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
472 DbgPrint("kESP %.8x ", Esp0
);
473 if (PsGetCurrentThread() != NULL
)
475 DbgPrint("kernel stack base %x\n",
476 PsGetCurrentThread()->Tcb
.StackLimit
);
481 if (PsGetCurrentThread() != NULL
)
483 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
487 StackLimit
= (ULONG
)&init_stack_top
;
491 * Dump the stack frames
493 DbgPrint("Frames: ");
494 /* Change to an #if 0 if no frames are printed because of fpo. */
497 Frame
= (PULONG
)Tf
->Ebp
;
498 while (Frame
!= NULL
)
502 Status
= MmSafeCopyFromUser(&Eip
, Frame
+ 1, sizeof(Eip
));
503 if (!NT_SUCCESS(Status
))
505 DbgPrint("<INVALID>");
508 if (!KeRosPrintAddress(Eip
))
510 DbgPrint("<%X>", Eip
);
513 Status
= MmSafeCopyFromUser(&Frame
, Frame
, sizeof(Frame
));
514 if (!NT_SUCCESS(Status
))
523 Frame
= (PULONG
)((ULONG_PTR
)Esp0
+ KTRAP_FRAME_EFLAGS
);
524 while (Frame
< (PULONG
)PsGetCurrentThread()->Tcb
.StackBase
&& i
< 50)
526 ULONG Address
= *Frame
;
527 if (KeRosPrintAddress((PVOID
)Address
))
537 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
539 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
540 * message and halt the computer
542 * Complete CPU context
549 /* Store the exception number in an unused field in the trap frame. */
550 Tf
->DebugArgMark
= (PVOID
)ExceptionNr
;
552 /* Use the address of the trap frame as approximation to the ring0 esp */
553 Esp0
= (ULONG
)&Tf
->Eip
;
556 #if defined(__GNUC__)
557 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2_
));
558 #elif defined(_MSC_VER)
562 #error Unknown compiler for inline assembler
564 Tf
->DebugPointer
= (PVOID
)cr2_
;
567 * If this was a V86 mode exception then handle it specially
569 if (Tf
->Eflags
& (1 << 17))
571 return(KeV86Exception(ExceptionNr
, Tf
, cr2_
));
575 * Check for stack underflow, this may be obsolete
577 if (PsGetCurrentThread() != NULL
&&
578 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
580 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
581 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
586 * Maybe handle the page fault and return
588 if (ExceptionNr
== 14)
590 if (Tf
->Eflags
& FLAG_IF
)
592 Ke386EnableInterrupts();
594 Status
= MmPageFault(Tf
->Cs
&0xffff,
599 if (NT_SUCCESS(Status
))
606 * Check for a breakpoint that was only for the attention of the debugger.
608 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
611 EIP is already adjusted by the processor to point to the instruction
612 after the breakpoint.
618 * Handle user exceptions differently
620 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
622 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2_
));
626 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2_
));
631 KeDumpStackFrames(PULONG Frame
)
635 DbgPrint("Frames: ");
637 while ( MmIsAddressValid(Frame
) )
639 if (!KeRosPrintAddress((PVOID
)Frame
[1]))
641 DbgPrint("<%X>", (PVOID
)Frame
[1]);
643 Frame
= (PULONG
)Frame
[0];
650 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
654 DbgPrint("Frames: ");
657 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
658 Frame
= (PULONG
)Frame
[0]; // step out of KeRosDumpStackFrames
661 while ( MmIsAddressValid(Frame
) && i
++ < FrameCount
)
663 if (!KeRosPrintAddress((PVOID
)Frame
[1]))
665 DbgPrint("<%X>", (PVOID
)Frame
[1]);
667 Frame
= (PULONG
)Frame
[0];
672 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
674 DPRINT("sel %x %d\n",sel
,sel
);
675 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
677 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
678 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
681 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
683 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
684 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
686 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
689 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
691 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
693 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
695 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
699 set_task_gate(unsigned int sel
, unsigned task_sel
)
701 KiIdt
[sel
].a
= task_sel
<< 16;
702 KiIdt
[sel
].b
= 0x8500;
706 KeInitExceptions(VOID
)
708 * FUNCTION: Initalize CPU exception handling
713 DPRINT("KeInitExceptions()\n");
716 * Set up the other gates
718 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
719 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
720 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
721 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
722 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
723 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
724 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
725 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
726 set_task_gate(8, TRAP_TSS_SELECTOR
);
727 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
728 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
729 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
730 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
731 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
732 set_interrupt_gate(14, (ULONG
)KiTrap14
);
733 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
734 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
738 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
741 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
742 set_system_call_gate(0x2e,(int)interrupt_handler2e
);
750 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
752 /* FIXME: This needs SEH */
754 PKTHREAD Thread
= KeGetCurrentThread();
756 ProbeForWrite(&Thread
->Teb
->ExceptionCode
, sizeof(NTSTATUS
), sizeof(NTSTATUS
)); /* NT doesn't check this -- bad? */
757 OldEip
= Thread
->TrapFrame
->Eip
;
758 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
759 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
760 return((NTSTATUS
)OldEip
);