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
& ~(ULONG_PTR
)MmSystemRangeStart
);
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("%.16s> ", 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\nESP: %.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
];
390 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
395 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
396 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
401 * Print out the CPU registers
403 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
405 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
406 ExceptionNr
, Tf
->ErrorCode
&0xffff);
410 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
412 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
413 Tf
->Cs
&0xffff, Tf
->Eip
);
414 KeRosPrintAddress((PVOID
)Tf
->Eip
);
416 Ke386GetPageTableDirectory(cr3_
);
417 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
418 DbgPrint("Proc: %x ",PsGetCurrentProcess());
419 if (PsGetCurrentProcess() != NULL
)
421 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
422 DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName
);
424 if (PsGetCurrentThread() != NULL
)
426 DbgPrint("Thrd: %x Tid: %x",
427 PsGetCurrentThread(),
428 PsGetCurrentThread()->Cid
.UniqueThread
);
431 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
432 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
433 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
434 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
435 Tf
->Ebp
, Tf
->Esi
, Esp0
);
436 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
437 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
439 DbgPrint("kESP %.8x ", Esp0
);
440 if (PsGetCurrentThread() != NULL
)
442 DbgPrint("kernel stack base %x\n",
443 PsGetCurrentThread()->Tcb
.StackLimit
);
448 if (PsGetCurrentThread() != NULL
)
450 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
454 StackLimit
= (ULONG
)init_stack_top
;
458 * Dump the stack frames
460 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
464 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
466 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
467 * message and halt the computer
469 * Complete CPU context
476 ASSERT(ExceptionNr
!= 14);
478 /* Store the exception number in an unused field in the trap frame. */
479 Tf
->DebugArgMark
= (PVOID
)ExceptionNr
;
481 /* Use the address of the trap frame as approximation to the ring0 esp */
482 Esp0
= (ULONG
)&Tf
->Eip
;
486 Tf
->DebugPointer
= (PVOID
)cr2
;
489 * If this was a V86 mode exception then handle it specially
491 if (Tf
->Eflags
& (1 << 17))
493 DPRINT("Tf->Eflags, %x, Tf->Eip %x, ExceptionNr: %d\n", Tf
->Eflags
, Tf
->Eip
, ExceptionNr
);
494 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
498 * Check for stack underflow, this may be obsolete
500 if (PsGetCurrentThread() != NULL
&&
501 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
503 DPRINT1("Stack underflow (tf->esp %x Limit %x Eip %x)\n",
504 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
, Tf
->Eip
);
508 if (ExceptionNr
== 15)
512 * This exception should never occur. The P6 has a bug, which does sometimes deliver
513 * the apic spurious interrupt as exception 15. On an athlon64, I get one exception
514 * in the early boot phase in apic mode (using the smp build). I've looked to the linux
515 * sources. Linux does ignore this exception.
519 DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
524 * Check for a breakpoint that was only for the attention of the debugger.
526 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
529 EIP is already adjusted by the processor to point to the instruction
530 after the breakpoint.
536 * Try to handle device-not-present, math-fault and xmm-fault exceptions.
538 if (ExceptionNr
== 7 || ExceptionNr
== 16 || ExceptionNr
== 19)
540 Status
= KiHandleFpuFault(Tf
, ExceptionNr
);
541 if (NT_SUCCESS(Status
))
548 * Handle user exceptions differently
550 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
552 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
556 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
562 KeContextToTrapFrame(PCONTEXT Context
,
563 PKTRAP_FRAME TrapFrame
)
565 /* Start with the basic Registers */
566 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
568 TrapFrame
->Esp
= Context
->Esp
;
569 TrapFrame
->Ss
= Context
->SegSs
;
570 TrapFrame
->Cs
= Context
->SegCs
;
571 TrapFrame
->Eip
= Context
->Eip
;
572 TrapFrame
->Eflags
= Context
->EFlags
;
573 TrapFrame
->Ebp
= Context
->Ebp
;
576 /* Process the Integer Registers */
577 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
579 TrapFrame
->Eax
= Context
->Eax
;
580 TrapFrame
->Ebx
= Context
->Ebx
;
581 TrapFrame
->Ecx
= Context
->Ecx
;
582 TrapFrame
->Edx
= Context
->Edx
;
583 TrapFrame
->Esi
= Context
->Esi
;
584 TrapFrame
->Edi
= Context
->Edi
;
587 /* Process the Context Segments */
588 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
590 TrapFrame
->Ds
= Context
->SegDs
;
591 TrapFrame
->Es
= Context
->SegEs
;
592 TrapFrame
->Fs
= Context
->SegFs
;
593 TrapFrame
->Gs
= Context
->SegGs
;
596 /* Handle the Debug Registers */
597 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
599 TrapFrame
->Dr0
= Context
->Dr0
;
600 TrapFrame
->Dr1
= Context
->Dr1
;
601 TrapFrame
->Dr2
= Context
->Dr2
;
602 TrapFrame
->Dr3
= Context
->Dr3
;
603 TrapFrame
->Dr6
= Context
->Dr6
;
604 TrapFrame
->Dr7
= Context
->Dr7
;
607 /* Handle FPU and Extended Registers */
608 return KiContextToFxSaveArea((PFX_SAVE_AREA
)(TrapFrame
+ 1), Context
);
612 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
615 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
617 Context
->SegSs
= TrapFrame
->Ss
;
618 Context
->Esp
= TrapFrame
->Esp
;
619 Context
->SegCs
= TrapFrame
->Cs
;
620 Context
->Eip
= TrapFrame
->Eip
;
621 Context
->EFlags
= TrapFrame
->Eflags
;
622 Context
->Ebp
= TrapFrame
->Ebp
;
624 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
626 Context
->Eax
= TrapFrame
->Eax
;
627 Context
->Ebx
= TrapFrame
->Ebx
;
628 Context
->Ecx
= TrapFrame
->Ecx
;
630 * NOTE: In the trap frame which is built on entry to a system
631 * call TrapFrame->Edx will actually hold the address of the
632 * previous TrapFrame. I don't believe leaking this information
633 * has security implications. Also EDX holds the address of the
634 * arguments to the system call in progress so it isn't of much
635 * interest to the debugger.
637 Context
->Edx
= TrapFrame
->Edx
;
638 Context
->Esi
= TrapFrame
->Esi
;
639 Context
->Edi
= TrapFrame
->Edi
;
641 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
643 Context
->SegDs
= TrapFrame
->Ds
;
644 Context
->SegEs
= TrapFrame
->Es
;
645 Context
->SegFs
= TrapFrame
->Fs
;
646 Context
->SegGs
= TrapFrame
->Gs
;
648 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
651 * FIXME: Implement this case
653 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
655 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
658 * FIXME: Implement this case
660 * I think this should only be filled for FPU exceptions, otherwise I
661 * would not know where to get it from as it can be the current state
662 * of the FPU or already saved in the thread's FPU save area.
665 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
668 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
671 * FIXME: Investigate this
673 * This is the XMM state (first 512 bytes of FXSAVE_FORMAT/FX_SAVE_AREA)
674 * This should only be filled in case of a SIMD exception I think, so
675 * this is not the right place (like for FPU the state could already be
676 * saved in the thread's FX_SAVE_AREA or still be in the CPU)
679 Context
->ContextFlags
&= ~CONTEXT_EXTENDED_REGISTERS
;
685 KeDumpStackFrames(PULONG Frame
)
687 PULONG StackBase
, StackEnd
;
688 MEMORY_BASIC_INFORMATION mbi
;
689 ULONG ResultLength
= sizeof(mbi
);
692 DbgPrint("Frames:\n");
695 Status
= MiQueryVirtualMemory (
698 MemoryBasicInformation
,
702 if ( !NT_SUCCESS(Status
) )
704 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
709 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
711 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
713 ULONG Addr
= Frame
[1];
714 if (!KeRosPrintAddress((PVOID
)Addr
))
715 DbgPrint("<%X>", Addr
);
716 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
719 Frame
= (PULONG
)Frame
[0];
731 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
734 PULONG StackBase
, StackEnd
;
735 MEMORY_BASIC_INFORMATION mbi
;
736 ULONG ResultLength
= sizeof(mbi
);
739 DbgPrint("Frames: ");
745 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
746 #elif defined(_MSC_VER)
747 __asm mov
[Frame
], ebp
749 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
752 Status
= MiQueryVirtualMemory (
755 MemoryBasicInformation
,
759 if ( !NT_SUCCESS(Status
) )
761 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
766 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
768 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
770 ULONG Addr
= Frame
[1];
771 if (!KeRosPrintAddress((PVOID
)Addr
))
772 DbgPrint("<%X>", Addr
);
773 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
776 Frame
= (PULONG
)Frame
[0];
788 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
791 PULONG StackBase
, StackEnd
, Frame
;
792 MEMORY_BASIC_INFORMATION mbi
;
793 ULONG ResultLength
= sizeof(mbi
);
799 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
800 #elif defined(_MSC_VER)
801 __asm mov
[Frame
], ebp
804 Status
= MiQueryVirtualMemory (
807 MemoryBasicInformation
,
811 if ( !NT_SUCCESS(Status
) )
813 DPRINT1("Can't get stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
818 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
820 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
822 Frames
[Count
++] = Frame
[1];
824 Frame
= (PULONG
)Frame
[0];
835 set_system_call_gate(unsigned int sel
, unsigned int func
)
837 DPRINT("sel %x %d\n",sel
,sel
);
838 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
840 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
841 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
844 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
846 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
847 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
849 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
852 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
854 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
856 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
858 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
862 set_task_gate(unsigned int sel
, unsigned task_sel
)
864 KiIdt
[sel
].a
= task_sel
<< 16;
865 KiIdt
[sel
].b
= 0x8500;
869 KeInitExceptions(VOID
)
871 * FUNCTION: Initalize CPU exception handling
876 DPRINT("KeInitExceptions()\n");
879 * Set up the other gates
881 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
882 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
883 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
884 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
885 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
886 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
887 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
888 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
889 set_task_gate(8, TRAP_TSS_SELECTOR
);
890 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
891 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
892 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
893 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
894 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
895 set_interrupt_gate(14, (ULONG
)KiTrap14
);
896 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
897 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
898 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
899 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
900 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
902 for (i
= 20; i
< 256; i
++)
904 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
907 set_system_call_gate(0x2d,(int)KiDebugService
);
908 set_system_call_gate(0x2e,(int)KiSystemService
);
915 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
918 PKTHREAD Thread
= KeGetCurrentThread();
921 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
923 return(ExceptionCode
);
926 OldEip
= Thread
->TrapFrame
->Eip
;
927 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
928 return((NTSTATUS
)OldEip
);
937 IN PEXCEPTION_RECORD ExceptionRecord
,
939 IN BOOLEAN SearchFrames
)
941 PKTHREAD Thread
= KeGetCurrentThread();
942 PKTRAP_FRAME TrapFrame
= Thread
->TrapFrame
;
943 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
945 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
947 KiDispatchException(ExceptionRecord
,
953 /* Restore the user context */
954 Thread
->TrapFrame
= PrevTrapFrame
;
955 __asm__("mov %%ebx, %%esp;\n" "jmp _KiServiceExit": : "b" (TrapFrame
));
957 /* We never get here */
958 return(STATUS_SUCCESS
);