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>
32 #include <internal/ntoskrnl.h>
33 #include <internal/ke.h>
34 #include <internal/i386/segment.h>
35 #include <internal/i386/mm.h>
36 #include <internal/module.h>
37 #include <internal/mm.h>
38 #include <internal/ps.h>
39 #include <internal/trap.h>
40 #include <ntdll/ldr.h>
41 #include <internal/safe.h>
44 #include <internal/debug.h>
46 /* GLOBALS *****************************************************************/
49 #define STR(x) _STR(x)
51 extern void interrupt_handler2e(void);
52 extern void interrupt_handler2d(void);
54 extern VOID
KiTrap0(VOID
);
55 extern VOID
KiTrap1(VOID
);
56 extern VOID
KiTrap2(VOID
);
57 extern VOID
KiTrap3(VOID
);
58 extern VOID
KiTrap4(VOID
);
59 extern VOID
KiTrap5(VOID
);
60 extern VOID
KiTrap6(VOID
);
61 extern VOID
KiTrap7(VOID
);
62 extern VOID
KiTrap8(VOID
);
63 extern VOID
KiTrap9(VOID
);
64 extern VOID
KiTrap10(VOID
);
65 extern VOID
KiTrap11(VOID
);
66 extern VOID
KiTrap12(VOID
);
67 extern VOID
KiTrap13(VOID
);
68 extern VOID
KiTrap14(VOID
);
69 extern VOID
KiTrap15(VOID
);
70 extern VOID
KiTrap16(VOID
);
71 extern VOID
KiTrapUnknown(VOID
);
73 extern ULONG init_stack
;
74 extern ULONG init_stack_top
;
76 static char *ExceptionTypeStrings
[] =
83 "BOUND range exceeded",
85 "No Math Coprocessor",
89 "Segment Not Present",
90 "Stack Segment Fault",
98 static NTSTATUS ExceptionToNtStatus
[] =
100 STATUS_INTEGER_DIVIDE_BY_ZERO
,
102 STATUS_ACCESS_VIOLATION
,
104 STATUS_INTEGER_OVERFLOW
,
105 STATUS_ARRAY_BOUNDS_EXCEEDED
,
106 STATUS_ILLEGAL_INSTRUCTION
,
107 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
108 STATUS_ACCESS_VIOLATION
,
109 STATUS_ACCESS_VIOLATION
,
110 STATUS_ACCESS_VIOLATION
,
111 STATUS_ACCESS_VIOLATION
,
112 STATUS_STACK_OVERFLOW
,
113 STATUS_ACCESS_VIOLATION
,
114 STATUS_ACCESS_VIOLATION
,
115 STATUS_ACCESS_VIOLATION
, /* STATUS_FLT_INVALID_OPERATION */
116 STATUS_DATATYPE_MISALIGNMENT
,
117 STATUS_ACCESS_VIOLATION
121 /* FUNCTIONS ****************************************************************/
123 extern unsigned int _text_start__
, _text_end__
;
126 print_address(PVOID address
)
130 PSYMBOL Symbol
, NextSymbol
;
131 BOOLEAN Printed
= FALSE
;
134 PLIST_ENTRY current_entry
;
135 MODULE_TEXT_SECTION
* current
;
136 extern LIST_ENTRY ModuleTextListHead
;
138 current_entry
= ModuleTextListHead
.Flink
;
140 while (current_entry
!= &ModuleTextListHead
&&
141 current_entry
!= NULL
)
144 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
146 if (address
>= (PVOID
)current
->Base
&&
147 address
< (PVOID
)(current
->Base
+ current
->Length
))
152 Offset
= (ULONG
)((ULONG
)address
- current
->Base
);
153 Symbol
= current
->Symbols
.Symbols
;
154 while (Symbol
!= NULL
)
156 NextSymbol
= Symbol
->Next
;
157 if (NextSymbol
!= NULL
)
158 NextAddress
= NextSymbol
->RelativeAddress
;
160 NextAddress
= current
->Length
;
162 if ((Offset
>= Symbol
->RelativeAddress
) &&
163 (Offset
< NextAddress
))
165 DbgPrint("<%ws: %x (%wZ)>", current
->Name
, Offset
, &Symbol
->Name
);
172 DbgPrint("<%ws: %x>", current
->Name
, Offset
);
176 DbgPrint("<%ws: %x>", current
->Name
,
177 address
- current
->Base
);
184 current_entry
= current_entry
->Flink
;
190 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
194 Er
.ExceptionFlags
= 0;
195 Er
.ExceptionRecord
= NULL
;
196 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
198 if (ExceptionNr
== 14)
200 Er
.NumberParameters
= 2;
201 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
202 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
206 if (ExceptionNr
< 16)
208 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
212 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
214 Er
.NumberParameters
= 0;
217 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
223 KiDoubleFaultHandler(VOID
)
230 ULONG ExceptionNr
= 8;
233 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
234 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
235 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
239 /* Use the address of the trap frame as approximation to the ring0 esp */
240 OldTss
= KeGetCurrentKPCR()->TSS
;
244 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
247 * Check for stack underflow
249 if (PsGetCurrentThread() != NULL
&&
250 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
252 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
253 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
258 * Print out the CPU registers
260 if (ExceptionNr
< 19)
262 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
267 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
269 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
270 print_address((PVOID
)OldTss
->Eip
);
272 DbgPrint("cr2 %x cr3 %x ", cr2
, OldTss
->Cr3
);
273 DbgPrint("Proc: %x ",PsGetCurrentProcess());
274 if (PsGetCurrentProcess() != NULL
)
276 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
277 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
279 if (PsGetCurrentThread() != NULL
)
281 DbgPrint("Thrd: %x Tid: %x",
282 PsGetCurrentThread(),
283 PsGetCurrentThread()->Cid
.UniqueThread
);
286 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
287 OldTss
->Fs
, OldTss
->Gs
);
288 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
290 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", OldTss
->Edx
, OldTss
->Ebp
,
292 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
293 if (OldTss
->Cs
== KERNEL_CS
)
295 DbgPrint("kESP %.8x ", Esp0
);
296 if (PsGetCurrentThread() != NULL
)
298 DbgPrint("kernel stack base %x\n",
299 PsGetCurrentThread()->Tcb
.StackLimit
);
305 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
307 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
309 DbgPrint("ESP %x\n", Esp0
);
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
;
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 print_address(StackTrace
[i
]);
386 if (StackRepeatLength
[i
] == 0)
390 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
392 print_address(StackTrace
[i
+ j
]);
394 DbgPrint("}*%d", StackRepeatCount
[i
]);
395 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
405 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
407 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
408 * message and halt the computer
410 * Complete CPU context
421 // unsigned int j, sym;
425 /* Use the address of the trap frame as approximation to the ring0 esp */
426 Esp0
= (ULONG
)&Tf
->Eip
;
429 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
432 * If this was a V86 mode exception then handle it specially
434 if (Tf
->Eflags
& (1 << 17))
436 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
440 * Check for stack underflow, this may be obsolete
442 if (PsGetCurrentThread() != NULL
&&
443 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
445 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
446 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
451 * Maybe handle the page fault and return
453 if (ExceptionNr
== 14)
456 Status
= MmPageFault(Tf
->Cs
&0xffff,
461 if (NT_SUCCESS(Status
))
469 * Handle user exceptions differently
471 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
473 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
478 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
486 * Print out the CPU registers
488 if (ExceptionNr
< 19)
490 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
491 ExceptionNr
, Tf
->ErrorCode
&0xffff);
495 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
497 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
498 Tf
->Cs
&0xffff, Tf
->Eip
);
499 print_address((PVOID
)Tf
->Eip
);
501 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
502 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3
);
503 DbgPrint("Proc: %x ",PsGetCurrentProcess());
504 if (PsGetCurrentProcess() != NULL
)
506 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
507 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
509 if (PsGetCurrentThread() != NULL
)
511 DbgPrint("Thrd: %x Tid: %x",
512 PsGetCurrentThread(),
513 PsGetCurrentThread()->Cid
.UniqueThread
);
516 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
517 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
518 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
519 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", Tf
->Edx
, Tf
->Ebp
, Tf
->Esi
);
520 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
521 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
523 DbgPrint("kESP %.8x ", Esp0
);
524 if (PsGetCurrentThread() != NULL
)
526 DbgPrint("kernel stack base %x\n",
527 PsGetCurrentThread()->Tcb
.StackLimit
);
532 DbgPrint("ESP %x\n", Esp0
);
534 if (PsGetCurrentThread() != NULL
)
536 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
540 StackLimit
= (ULONG
)&init_stack_top
;
544 * Dump the stack frames
546 DbgPrint("Frames: ");
548 Frame
= (PULONG
)Tf
->Ebp
;
549 while (Frame
!= NULL
)
551 print_address((PVOID
)Frame
[1]);
552 Frame
= (PULONG
)Frame
[0];
567 KeDumpStackFrames(PULONG Frame
)
571 DbgPrint("Frames: ");
573 while (Frame
!= NULL
)
575 print_address((PVOID
)Frame
[1]);
576 Frame
= (PULONG
)Frame
[0];
585 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
587 DPRINT("sel %x %d\n",sel
,sel
);
588 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
590 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
591 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
594 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
596 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
597 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
599 KiIdt
[sel
].b
= 0x8f00 + (((int)func
)&0xffff0000);
603 set_task_gate(unsigned int sel
, unsigned task_sel
)
605 KiIdt
[sel
].a
= task_sel
<< 16;
606 KiIdt
[sel
].b
= 0x8500;
610 KeInitExceptions(VOID
)
612 * FUNCTION: Initalize CPU exception handling
617 DPRINT("KeInitExceptions()\n");
620 * Set up the other gates
622 set_interrupt_gate(0, (ULONG
)KiTrap0
);
623 set_interrupt_gate(1, (ULONG
)KiTrap1
);
624 set_interrupt_gate(2, (ULONG
)KiTrap2
);
625 set_interrupt_gate(3, (ULONG
)KiTrap3
);
626 set_interrupt_gate(4, (ULONG
)KiTrap4
);
627 set_interrupt_gate(5, (ULONG
)KiTrap5
);
628 set_interrupt_gate(6, (ULONG
)KiTrap6
);
629 set_interrupt_gate(7, (ULONG
)KiTrap7
);
630 set_task_gate(8, TRAP_TSS_SELECTOR
);
631 set_interrupt_gate(9, (ULONG
)KiTrap9
);
632 set_interrupt_gate(10, (ULONG
)KiTrap10
);
633 set_interrupt_gate(11, (ULONG
)KiTrap11
);
634 set_interrupt_gate(12, (ULONG
)KiTrap12
);
635 set_interrupt_gate(13, (ULONG
)KiTrap13
);
636 set_interrupt_gate(14, (ULONG
)KiTrap14
);
637 set_interrupt_gate(15, (ULONG
)KiTrap15
);
638 set_interrupt_gate(16, (ULONG
)KiTrap16
);
642 set_interrupt_gate(i
,(int)KiTrapUnknown
);
645 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
646 set_system_call_gate(0x2e,(int)interrupt_handler2e
);