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
,
173 DbgPrint("<%ws: %x>", current
->Name
, Offset
);
177 DbgPrint("<%ws: %x>", current
->Name
,
178 address
- current
->Base
);
185 current_entry
= current_entry
->Flink
;
191 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
195 Er
.ExceptionFlags
= 0;
196 Er
.ExceptionRecord
= NULL
;
197 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
199 if (ExceptionNr
== 14)
201 Er
.NumberParameters
= 2;
202 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
203 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
207 if (ExceptionNr
< 16)
209 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
213 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
215 Er
.NumberParameters
= 0;
218 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
224 KiDoubleFaultHandler(VOID
)
230 ULONG ExceptionNr
= 8;
235 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
236 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
237 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
242 /* Use the address of the trap frame as approximation to the ring0 esp */
243 OldTss
= KeGetCurrentKPCR()->TSS
;
247 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
250 * Check for stack underflow
252 if (PsGetCurrentThread() != NULL
&&
253 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
255 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
256 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
261 * Print out the CPU registers
263 if (ExceptionNr
< 19)
265 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
270 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
272 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
273 print_address((PVOID
)OldTss
->Eip
);
275 DbgPrint("cr2 %x cr3 %x ", cr2
, OldTss
->Cr3
);
276 DbgPrint("Proc: %x ",PsGetCurrentProcess());
277 if (PsGetCurrentProcess() != NULL
)
279 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
280 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
282 if (PsGetCurrentThread() != NULL
)
284 DbgPrint("Thrd: %x Tid: %x",
285 PsGetCurrentThread(),
286 PsGetCurrentThread()->Cid
.UniqueThread
);
289 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
290 OldTss
->Fs
, OldTss
->Gs
);
291 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
293 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", OldTss
->Edx
, OldTss
->Ebp
,
295 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
296 if (OldTss
->Cs
== KERNEL_CS
)
298 DbgPrint("kESP %.8x ", Esp0
);
299 if (PsGetCurrentThread() != NULL
)
301 DbgPrint("kernel stack base %x\n",
302 PsGetCurrentThread()->Tcb
.StackLimit
);
308 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
310 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
312 DbgPrint("ESP %x\n", Esp0
);
313 if (PsGetCurrentThread() != NULL
)
315 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
316 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
320 StackLimit
= (ULONG
)&init_stack_top
;
321 StackBase
= (ULONG
)&init_stack
;
325 DbgPrint("Frames: ");
326 Frame
= (PULONG
)OldTss
->Ebp
;
327 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
329 print_address((PVOID
)Frame
[1]);
330 Frame
= (PULONG
)Frame
[0];
333 DbgPrint("Frames: ");
335 Frame
= (PULONG
)OldTss
->Ebp
;
336 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
338 StackTrace
[i
] = (PVOID
)Frame
[1];
339 Frame
= (PULONG
)Frame
[0];
345 while (i
< TraceLength
)
347 StackRepeatCount
[i
] = 0;
350 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
352 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
353 (j
- i
) * sizeof(PVOID
)) == 0)
355 StackRepeatCount
[i
] = 2;
356 StackRepeatLength
[i
] = j
- i
;
364 if (FoundRepeat
== FALSE
)
369 j
= j
+ StackRepeatLength
[i
];
370 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
373 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
374 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
376 StackRepeatCount
[i
]++;
377 j
= j
+ StackRepeatLength
[i
];
388 while (i
< TraceLength
)
390 if (StackRepeatCount
[i
] == 0)
392 print_address(StackTrace
[i
]);
398 if (StackRepeatLength
[i
] == 0)
402 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
404 print_address(StackTrace
[i
+ j
]);
406 DbgPrint("}*%d", StackRepeatCount
[i
]);
407 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
418 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, ULONG cr2
)
429 * Print out the CPU registers
431 if (ExceptionNr
< 19)
433 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
434 ExceptionNr
, Tf
->ErrorCode
&0xffff);
438 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
440 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
441 Tf
->Cs
&0xffff, Tf
->Eip
);
442 print_address((PVOID
)Tf
->Eip
);
444 __asm__("movl %%cr3,%0\n\t" : "=d" (cr3
));
445 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3
);
446 DbgPrint("Proc: %x ",PsGetCurrentProcess());
447 if (PsGetCurrentProcess() != NULL
)
449 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
450 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
452 if (PsGetCurrentThread() != NULL
)
454 DbgPrint("Thrd: %x Tid: %x",
455 PsGetCurrentThread(),
456 PsGetCurrentThread()->Cid
.UniqueThread
);
459 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
460 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
461 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
462 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", Tf
->Edx
, Tf
->Ebp
, Tf
->Esi
);
463 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
464 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
466 DbgPrint("kESP %.8x ", Esp0
);
467 if (PsGetCurrentThread() != NULL
)
469 DbgPrint("kernel stack base %x\n",
470 PsGetCurrentThread()->Tcb
.StackLimit
);
475 DbgPrint("ESP %x\n", Esp0
);
477 if (PsGetCurrentThread() != NULL
)
479 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
483 StackLimit
= (ULONG
)&init_stack_top
;
487 * Dump the stack frames
489 DbgPrint("Frames: ");
491 Frame
= (PULONG
)Tf
->Ebp
;
492 while (Frame
!= NULL
)
494 print_address((PVOID
)Frame
[1]);
495 Frame
= (PULONG
)Frame
[0];
505 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
507 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
508 * message and halt the computer
510 * Complete CPU context
517 /* Use the address of the trap frame as approximation to the ring0 esp */
518 Esp0
= (ULONG
)&Tf
->Eip
;
521 __asm__("movl %%cr2,%0\n\t" : "=d" (cr2
));
524 * If this was a V86 mode exception then handle it specially
526 if (Tf
->Eflags
& (1 << 17))
528 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
532 * Check for stack underflow, this may be obsolete
534 if (PsGetCurrentThread() != NULL
&&
535 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
537 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
538 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
543 * Maybe handle the page fault and return
545 if (ExceptionNr
== 14)
548 Status
= MmPageFault(Tf
->Cs
&0xffff,
553 if (NT_SUCCESS(Status
))
561 * Handle user exceptions differently
563 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
565 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
569 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
574 KeDumpStackFrames(PULONG Frame
)
578 DbgPrint("Frames: ");
580 while (Frame
!= NULL
)
582 print_address((PVOID
)Frame
[1]);
583 Frame
= (PULONG
)Frame
[0];
592 static void set_system_call_gate(unsigned int sel
, unsigned int func
)
594 DPRINT("sel %x %d\n",sel
,sel
);
595 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
597 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
598 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
601 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
603 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
604 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
606 KiIdt
[sel
].b
= 0x8f00 + (((int)func
)&0xffff0000);
610 set_task_gate(unsigned int sel
, unsigned task_sel
)
612 KiIdt
[sel
].a
= task_sel
<< 16;
613 KiIdt
[sel
].b
= 0x8500;
617 KeInitExceptions(VOID
)
619 * FUNCTION: Initalize CPU exception handling
624 DPRINT("KeInitExceptions()\n");
627 * Set up the other gates
629 set_interrupt_gate(0, (ULONG
)KiTrap0
);
630 set_interrupt_gate(1, (ULONG
)KiTrap1
);
631 set_interrupt_gate(2, (ULONG
)KiTrap2
);
632 set_interrupt_gate(3, (ULONG
)KiTrap3
);
633 set_interrupt_gate(4, (ULONG
)KiTrap4
);
634 set_interrupt_gate(5, (ULONG
)KiTrap5
);
635 set_interrupt_gate(6, (ULONG
)KiTrap6
);
636 set_interrupt_gate(7, (ULONG
)KiTrap7
);
637 set_task_gate(8, TRAP_TSS_SELECTOR
);
638 set_interrupt_gate(9, (ULONG
)KiTrap9
);
639 set_interrupt_gate(10, (ULONG
)KiTrap10
);
640 set_interrupt_gate(11, (ULONG
)KiTrap11
);
641 set_interrupt_gate(12, (ULONG
)KiTrap12
);
642 set_interrupt_gate(13, (ULONG
)KiTrap13
);
643 set_interrupt_gate(14, (ULONG
)KiTrap14
);
644 set_interrupt_gate(15, (ULONG
)KiTrap15
);
645 set_interrupt_gate(16, (ULONG
)KiTrap16
);
649 set_interrupt_gate(i
,(int)KiTrapUnknown
);
652 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
653 set_system_call_gate(0x2e,(int)interrupt_handler2e
);