2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/i386/exp.c
5 * PURPOSE: Handling exceptions
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
8 * Skywing (skywing@valhallalegends.com)
11 /* INCLUDES *****************************************************************/
16 #include <internal/debug.h>
18 /* GLOBALS *****************************************************************/
20 #define FLAG_IF (1<<9)
23 #define STR(x) _STR(x)
26 # define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
29 extern void KiSystemService(void);
30 extern void KiDebugService(void);
32 extern VOID
KiTrap0(VOID
);
33 extern VOID
KiTrap1(VOID
);
34 extern VOID
KiTrap2(VOID
);
35 extern VOID
KiTrap3(VOID
);
36 extern VOID
KiTrap4(VOID
);
37 extern VOID
KiTrap5(VOID
);
38 extern VOID
KiTrap6(VOID
);
39 extern VOID
KiTrap7(VOID
);
40 extern VOID
KiTrap8(VOID
);
41 extern VOID
KiTrap9(VOID
);
42 extern VOID
KiTrap10(VOID
);
43 extern VOID
KiTrap11(VOID
);
44 extern VOID
KiTrap12(VOID
);
45 extern VOID
KiTrap13(VOID
);
46 extern VOID
KiTrap14(VOID
);
47 extern VOID
KiTrap15(VOID
);
48 extern VOID
KiTrap16(VOID
);
49 extern VOID
KiTrap17(VOID
);
50 extern VOID
KiTrap18(VOID
);
51 extern VOID
KiTrap19(VOID
);
52 extern VOID
KiTrapUnknown(VOID
);
54 extern ULONG init_stack
;
55 extern ULONG init_stack_top
;
57 extern BOOLEAN Ke386NoExecute
;
59 static char *ExceptionTypeStrings
[] =
66 "BOUND range exceeded",
68 "No Math Coprocessor",
72 "Segment Not Present",
73 "Stack Segment Fault",
83 NTSTATUS ExceptionToNtStatus
[] =
85 STATUS_INTEGER_DIVIDE_BY_ZERO
,
87 STATUS_ACCESS_VIOLATION
,
89 STATUS_INTEGER_OVERFLOW
,
90 STATUS_ARRAY_BOUNDS_EXCEEDED
,
91 STATUS_ILLEGAL_INSTRUCTION
,
92 STATUS_FLOAT_INVALID_OPERATION
,
93 STATUS_ACCESS_VIOLATION
,
94 STATUS_ACCESS_VIOLATION
,
95 STATUS_ACCESS_VIOLATION
,
96 STATUS_ACCESS_VIOLATION
,
97 STATUS_STACK_OVERFLOW
,
98 STATUS_ACCESS_VIOLATION
,
99 STATUS_ACCESS_VIOLATION
,
100 STATUS_ACCESS_VIOLATION
, /* RESERVED */
101 STATUS_FLOAT_INVALID_OPERATION
, /* Should not be used, the FPU can give more specific info */
102 STATUS_DATATYPE_MISALIGNMENT
,
103 STATUS_ACCESS_VIOLATION
,
104 STATUS_FLOAT_MULTIPLE_TRAPS
,
107 /* FUNCTIONS ****************************************************************/
110 KiRosPrintAddress(PVOID address
)
112 PLIST_ENTRY current_entry
;
113 MODULE_TEXT_SECTION
* current
;
114 extern LIST_ENTRY ModuleTextListHead
;
115 ULONG_PTR RelativeAddress
;
120 current_entry
= ModuleTextListHead
.Flink
;
122 while (current_entry
!= &ModuleTextListHead
&&
123 current_entry
!= NULL
)
126 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
128 if (address
>= (PVOID
)current
->Base
&&
129 address
< (PVOID
)(current
->Base
+ current
->Length
))
131 RelativeAddress
= (ULONG_PTR
) address
- current
->Base
;
132 DbgPrint("<%ws: %x>", current
->Name
, RelativeAddress
);
135 current_entry
= current_entry
->Flink
;
138 address
= (PVOID
)((ULONG_PTR
)address
& ~0xC0000000);
145 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
149 Er
.ExceptionFlags
= 0;
150 Er
.ExceptionRecord
= NULL
;
151 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
153 if (ExceptionNr
== 14)
155 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
156 Er
.NumberParameters
= 2;
157 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
158 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
162 if (ExceptionNr
< ARRAY_SIZE(ExceptionToNtStatus
))
164 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
168 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
170 Er
.NumberParameters
= 0;
173 /* FIXME: Which exceptions are noncontinuable? */
174 Er
.ExceptionFlags
= 0;
176 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
182 KiDoubleFaultHandler(VOID
)
188 ULONG ExceptionNr
= 8;
194 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
195 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
196 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
201 OldTss
= KeGetCurrentKPCR()->TSS
;
206 if (PsGetCurrentThread() != NULL
&&
207 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
210 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
218 * Check for stack underflow
220 if (PsGetCurrentThread() != NULL
&&
221 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
223 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
224 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
229 * Print out the CPU registers
231 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
233 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
238 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
240 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
241 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
243 DbgPrint("cr2 %x cr3 %x ", cr2
, OldCr3
);
244 DbgPrint("Proc: %x ",PsGetCurrentProcess());
245 if (PsGetCurrentProcess() != NULL
)
247 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
248 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
250 if (PsGetCurrentThread() != NULL
)
252 DbgPrint("Thrd: %x Tid: %x",
253 PsGetCurrentThread(),
254 PsGetCurrentThread()->Cid
.UniqueThread
);
257 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
258 OldTss
->Fs
, OldTss
->Gs
);
259 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
261 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n ESP: %.8x", OldTss
->Edx
,
262 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
263 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
264 if (OldTss
->Cs
== KERNEL_CS
)
266 DbgPrint("kESP %.8x ", Esp0
);
267 if (PsGetCurrentThread() != NULL
)
269 DbgPrint("kernel stack base %x\n",
270 PsGetCurrentThread()->Tcb
.StackLimit
);
276 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
278 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
280 if (PsGetCurrentThread() != NULL
)
282 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
283 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
287 StackLimit
= (ULONG
)init_stack_top
;
288 StackBase
= (ULONG
)init_stack
;
292 Change to an #if 0 to reduce the amount of information printed on
293 a recursive stack trace.
296 DbgPrint("Frames: ");
297 Frame
= (PULONG
)OldTss
->Ebp
;
298 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
300 KeRosPrintAddress((PVOID
)Frame
[1]);
301 Frame
= (PULONG
)Frame
[0];
305 DbgPrint("Frames: ");
307 Frame
= (PULONG
)OldTss
->Ebp
;
308 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
310 StackTrace
[i
] = (PVOID
)Frame
[1];
311 Frame
= (PULONG
)Frame
[0];
317 while (i
< TraceLength
)
319 StackRepeatCount
[i
] = 0;
322 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
324 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
325 (j
- i
) * sizeof(PVOID
)) == 0)
327 StackRepeatCount
[i
] = 2;
328 StackRepeatLength
[i
] = j
- i
;
336 if (FoundRepeat
== FALSE
)
341 j
= j
+ StackRepeatLength
[i
];
342 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
345 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
346 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
348 StackRepeatCount
[i
]++;
349 j
= j
+ StackRepeatLength
[i
];
360 while (i
< TraceLength
)
362 if (StackRepeatCount
[i
] == 0)
364 KeRosPrintAddress(StackTrace
[i
]);
370 if (StackRepeatLength
[i
] == 0)
374 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
376 KeRosPrintAddress(StackTrace
[i
+ j
]);
378 DbgPrint("}*%d", StackRepeatCount
[i
]);
379 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
391 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
396 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
397 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
402 * Print out the CPU registers
404 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
406 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
407 ExceptionNr
, Tf
->ErrorCode
&0xffff);
411 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
413 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
414 Tf
->Cs
&0xffff, Tf
->Eip
);
415 KeRosPrintAddress((PVOID
)Tf
->Eip
);
417 Ke386GetPageTableDirectory(cr3_
);
418 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
419 DbgPrint("Proc: %x ",PsGetCurrentProcess());
420 if (PsGetCurrentProcess() != NULL
)
422 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
423 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
425 if (PsGetCurrentThread() != NULL
)
427 DbgPrint("Thrd: %x Tid: %x",
428 PsGetCurrentThread(),
429 PsGetCurrentThread()->Cid
.UniqueThread
);
432 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
433 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
434 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
435 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
436 Tf
->Ebp
, Tf
->Esi
, Esp0
);
437 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
438 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
440 DbgPrint("kESP %.8x ", Esp0
);
441 if (PsGetCurrentThread() != NULL
)
443 DbgPrint("kernel stack base %x\n",
444 PsGetCurrentThread()->Tcb
.StackLimit
);
449 if (PsGetCurrentThread() != NULL
)
451 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
455 StackLimit
= (ULONG
)init_stack_top
;
459 * Dump the stack frames
461 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
465 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
467 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
468 * message and halt the computer
470 * Complete CPU context
477 /* Store the exception number in an unused field in the trap frame. */
478 Tf
->DebugArgMark
= (PVOID
)ExceptionNr
;
480 /* Use the address of the trap frame as approximation to the ring0 esp */
481 Esp0
= (ULONG
)&Tf
->Eip
;
485 Tf
->DebugPointer
= (PVOID
)cr2
;
487 if (ExceptionNr
== 14 && Tf
->Eflags
& FLAG_IF
)
489 Ke386EnableInterrupts();
493 * If this was a V86 mode exception then handle it specially
495 if (Tf
->Eflags
& (1 << 17))
497 DPRINT("Tf->Eflags, %x, Tf->Eip %x, ExceptionNr: %d\n", Tf
->Eflags
, Tf
->Eip
, ExceptionNr
);
498 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
502 * Check for stack underflow, this may be obsolete
504 if (PsGetCurrentThread() != NULL
&&
505 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
507 DPRINT1("Stack underflow (tf->esp %x Limit %x)\n",
508 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
512 if (ExceptionNr
== 15)
516 * This exception should never occur. The P6 has a bug, which does sometimes deliver
517 * the apic spurious interrupt as exception 15. On an athlon64, I get one exception
518 * in the early boot phase in apic mode (using the smp build). I've looked to the linux
519 * sources. Linux does ignore this exception.
523 DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
528 * Maybe handle the page fault and return
530 if (ExceptionNr
== 14)
532 if (Ke386NoExecute
&& Tf
->ErrorCode
& 0x10 && cr2
>= KERNEL_BASE
)
534 KEBUGCHECKWITHTF(ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
, 0, 0, 0, 0, Tf
);
536 Status
= MmPageFault(Tf
->Cs
&0xffff,
541 if (NT_SUCCESS(Status
))
548 * Check for a breakpoint that was only for the attention of the debugger.
550 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
553 EIP is already adjusted by the processor to point to the instruction
554 after the breakpoint.
560 * Try to handle device-not-present, math-fault and xmm-fault exceptions.
562 if (ExceptionNr
== 7 || ExceptionNr
== 16 || ExceptionNr
== 19)
564 Status
= KiHandleFpuFault(Tf
, ExceptionNr
);
565 if (NT_SUCCESS(Status
))
572 * Handle user exceptions differently
574 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
576 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
580 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
586 KeContextToTrapFrame(PCONTEXT Context
,
587 PKTRAP_FRAME TrapFrame
)
589 /* Start with the basic Registers */
590 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
592 TrapFrame
->Esp
= Context
->Esp
;
593 TrapFrame
->Ss
= Context
->SegSs
;
594 TrapFrame
->Cs
= Context
->SegCs
;
595 TrapFrame
->Eip
= Context
->Eip
;
596 TrapFrame
->Eflags
= Context
->EFlags
;
597 TrapFrame
->Ebp
= Context
->Ebp
;
600 /* Process the Integer Registers */
601 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
603 TrapFrame
->Eax
= Context
->Eax
;
604 TrapFrame
->Ebx
= Context
->Ebx
;
605 TrapFrame
->Ecx
= Context
->Ecx
;
606 TrapFrame
->Edx
= Context
->Edx
;
607 TrapFrame
->Esi
= Context
->Esi
;
608 TrapFrame
->Edi
= Context
->Edi
;
611 /* Process the Context Segments */
612 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
614 TrapFrame
->Ds
= Context
->SegDs
;
615 TrapFrame
->Es
= Context
->SegEs
;
616 TrapFrame
->Fs
= Context
->SegFs
;
617 TrapFrame
->Gs
= Context
->SegGs
;
620 /* Handle the Debug Registers */
621 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
623 TrapFrame
->Dr0
= Context
->Dr0
;
624 TrapFrame
->Dr1
= Context
->Dr1
;
625 TrapFrame
->Dr2
= Context
->Dr2
;
626 TrapFrame
->Dr3
= Context
->Dr3
;
627 TrapFrame
->Dr6
= Context
->Dr6
;
628 TrapFrame
->Dr7
= Context
->Dr7
;
631 /* Handle FPU and Extended Registers */
632 return KiContextToFxSaveArea((PFX_SAVE_AREA
)(TrapFrame
+ 1), Context
);
636 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
639 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
641 Context
->SegSs
= TrapFrame
->Ss
;
642 Context
->Esp
= TrapFrame
->Esp
;
643 Context
->SegCs
= TrapFrame
->Cs
;
644 Context
->Eip
= TrapFrame
->Eip
;
645 Context
->EFlags
= TrapFrame
->Eflags
;
646 Context
->Ebp
= TrapFrame
->Ebp
;
648 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
650 Context
->Eax
= TrapFrame
->Eax
;
651 Context
->Ebx
= TrapFrame
->Ebx
;
652 Context
->Ecx
= TrapFrame
->Ecx
;
654 * NOTE: In the trap frame which is built on entry to a system
655 * call TrapFrame->Edx will actually hold the address of the
656 * previous TrapFrame. I don't believe leaking this information
657 * has security implications. Also EDX holds the address of the
658 * arguments to the system call in progress so it isn't of much
659 * interest to the debugger.
661 Context
->Edx
= TrapFrame
->Edx
;
662 Context
->Esi
= TrapFrame
->Esi
;
663 Context
->Edi
= TrapFrame
->Edi
;
665 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
667 Context
->SegDs
= TrapFrame
->Ds
;
668 Context
->SegEs
= TrapFrame
->Es
;
669 Context
->SegFs
= TrapFrame
->Fs
;
670 Context
->SegGs
= TrapFrame
->Gs
;
672 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
675 * FIXME: Implement this case
677 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
679 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
682 * FIXME: Implement this case
684 * I think this should only be filled for FPU exceptions, otherwise I
685 * would not know where to get it from as it can be the current state
686 * of the FPU or already saved in the thread's FPU save area.
689 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
692 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
695 * FIXME: Investigate this
697 * This is the XMM state (first 512 bytes of FXSAVE_FORMAT/FX_SAVE_AREA)
698 * This should only be filled in case of a SIMD exception I think, so
699 * this is not the right place (like for FPU the state could already be
700 * saved in the thread's FX_SAVE_AREA or still be in the CPU)
703 Context
->ContextFlags
&= ~CONTEXT_EXTENDED_REGISTERS
;
709 KeDumpStackFrames(PULONG Frame
)
711 PULONG StackBase
, StackEnd
;
712 MEMORY_BASIC_INFORMATION mbi
;
713 ULONG ResultLength
= sizeof(mbi
);
716 DbgPrint("Frames:\n");
719 Status
= MiQueryVirtualMemory (
722 MemoryBasicInformation
,
726 if ( !NT_SUCCESS(Status
) )
728 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
733 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
735 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
737 ULONG Addr
= Frame
[1];
738 if (!KeRosPrintAddress((PVOID
)Addr
))
739 DbgPrint("<%X>", Addr
);
740 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
743 Frame
= (PULONG
)Frame
[0];
755 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
758 PULONG StackBase
, StackEnd
;
759 MEMORY_BASIC_INFORMATION mbi
;
760 ULONG ResultLength
= sizeof(mbi
);
763 DbgPrint("Frames: ");
769 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
770 #elif defined(_MSC_VER)
771 __asm mov
[Frame
], ebp
773 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
776 Status
= MiQueryVirtualMemory (
779 MemoryBasicInformation
,
783 if ( !NT_SUCCESS(Status
) )
785 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
790 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
792 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
794 ULONG Addr
= Frame
[1];
795 if (!KeRosPrintAddress((PVOID
)Addr
))
796 DbgPrint("<%X>", Addr
);
797 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
800 Frame
= (PULONG
)Frame
[0];
812 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
815 PULONG StackBase
, StackEnd
, Frame
;
816 MEMORY_BASIC_INFORMATION mbi
;
817 ULONG ResultLength
= sizeof(mbi
);
823 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
824 #elif defined(_MSC_VER)
825 __asm mov
[Frame
], ebp
828 Status
= MiQueryVirtualMemory (
831 MemoryBasicInformation
,
835 if ( !NT_SUCCESS(Status
) )
837 DPRINT1("Can't get stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
842 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
844 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
846 Frames
[Count
++] = Frame
[1];
848 Frame
= (PULONG
)Frame
[0];
859 set_system_call_gate(unsigned int sel
, unsigned int func
)
861 DPRINT("sel %x %d\n",sel
,sel
);
862 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
864 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
865 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
868 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
870 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
871 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
873 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
876 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
878 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
880 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
882 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
886 set_task_gate(unsigned int sel
, unsigned task_sel
)
888 KiIdt
[sel
].a
= task_sel
<< 16;
889 KiIdt
[sel
].b
= 0x8500;
893 KeInitExceptions(VOID
)
895 * FUNCTION: Initalize CPU exception handling
900 DPRINT("KeInitExceptions()\n");
903 * Set up the other gates
905 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
906 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
907 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
908 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
909 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
910 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
911 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
912 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
913 set_task_gate(8, TRAP_TSS_SELECTOR
);
914 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
915 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
916 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
917 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
918 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
919 set_interrupt_gate(14, (ULONG
)KiTrap14
);
920 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
921 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
922 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
923 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
924 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
926 for (i
= 20; i
< 256; i
++)
928 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
931 set_system_call_gate(0x2d,(int)KiDebugService
);
932 set_system_call_gate(0x2e,(int)KiSystemService
);
939 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
942 PKTHREAD Thread
= KeGetCurrentThread();
945 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
947 return(ExceptionCode
);
950 OldEip
= Thread
->TrapFrame
->Eip
;
951 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
952 return((NTSTATUS
)OldEip
);
961 IN PEXCEPTION_RECORD ExceptionRecord
,
963 IN BOOLEAN SearchFrames
)
965 PKTHREAD Thread
= KeGetCurrentThread();
966 PKTRAP_FRAME TrapFrame
= Thread
->TrapFrame
;
967 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
969 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
971 KiDispatchException(ExceptionRecord
,
977 /* Restore the user context */
978 Thread
->TrapFrame
= PrevTrapFrame
;
979 __asm__("mov %%ebx, %%esp;\n" "jmp _KiServiceExit": : "b" (TrapFrame
));
981 /* We never get here */
982 return(STATUS_SUCCESS
);