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 interrupt_handler2d(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 ****************************************************************/
109 #if defined(DBG) || defined(KDBG)
111 KeRosPrintAddress(PVOID address
)
113 return KdbSymPrintAddress(address
);
117 KeRosPrintAddress(PVOID address
)
119 PLIST_ENTRY current_entry
;
120 MODULE_TEXT_SECTION
* current
;
121 extern LIST_ENTRY ModuleTextListHead
;
122 ULONG_PTR RelativeAddress
;
124 current_entry
= ModuleTextListHead
.Flink
;
126 while (current_entry
!= &ModuleTextListHead
&&
127 current_entry
!= NULL
)
130 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
132 if (address
>= (PVOID
)current
->Base
&&
133 address
< (PVOID
)(current
->Base
+ current
->Length
))
135 RelativeAddress
= (ULONG_PTR
) address
- current
->Base
;
136 DbgPrint("<%ws: %x>", current
->Name
, RelativeAddress
);
139 current_entry
= current_entry
->Flink
;
146 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
150 Er
.ExceptionFlags
= 0;
151 Er
.ExceptionRecord
= NULL
;
152 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
154 if (ExceptionNr
== 14)
156 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
157 Er
.NumberParameters
= 2;
158 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
159 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
163 if (ExceptionNr
< ARRAY_SIZE(ExceptionToNtStatus
))
165 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
169 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
171 Er
.NumberParameters
= 0;
174 /* FIXME: Which exceptions are noncontinuable? */
175 Er
.ExceptionFlags
= 0;
177 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
183 KiDoubleFaultHandler(VOID
)
189 ULONG ExceptionNr
= 8;
195 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
196 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
197 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
202 OldTss
= KeGetCurrentKPCR()->TSS
;
207 if (PsGetCurrentThread() != NULL
&&
208 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
211 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
219 * Check for stack underflow
221 if (PsGetCurrentThread() != NULL
&&
222 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
224 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
225 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
230 * Print out the CPU registers
232 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
234 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
239 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
241 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
242 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
244 DbgPrint("cr2 %x cr3 %x ", cr2
, OldCr3
);
245 DbgPrint("Proc: %x ",PsGetCurrentProcess());
246 if (PsGetCurrentProcess() != NULL
)
248 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
249 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
251 if (PsGetCurrentThread() != NULL
)
253 DbgPrint("Thrd: %x Tid: %x",
254 PsGetCurrentThread(),
255 PsGetCurrentThread()->Cid
.UniqueThread
);
258 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
259 OldTss
->Fs
, OldTss
->Gs
);
260 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
262 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n ESP: %.8x", OldTss
->Edx
,
263 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
264 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
265 if (OldTss
->Cs
== KERNEL_CS
)
267 DbgPrint("kESP %.8x ", Esp0
);
268 if (PsGetCurrentThread() != NULL
)
270 DbgPrint("kernel stack base %x\n",
271 PsGetCurrentThread()->Tcb
.StackLimit
);
277 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
279 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
281 if (PsGetCurrentThread() != NULL
)
283 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
284 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
288 StackLimit
= (ULONG
)init_stack_top
;
289 StackBase
= (ULONG
)init_stack
;
293 Change to an #if 0 to reduce the amount of information printed on
294 a recursive stack trace.
297 DbgPrint("Frames: ");
298 Frame
= (PULONG
)OldTss
->Ebp
;
299 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
301 KeRosPrintAddress((PVOID
)Frame
[1]);
302 Frame
= (PULONG
)Frame
[0];
306 DbgPrint("Frames: ");
308 Frame
= (PULONG
)OldTss
->Ebp
;
309 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
311 StackTrace
[i
] = (PVOID
)Frame
[1];
312 Frame
= (PULONG
)Frame
[0];
318 while (i
< TraceLength
)
320 StackRepeatCount
[i
] = 0;
323 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
325 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
326 (j
- i
) * sizeof(PVOID
)) == 0)
328 StackRepeatCount
[i
] = 2;
329 StackRepeatLength
[i
] = j
- i
;
337 if (FoundRepeat
== FALSE
)
342 j
= j
+ StackRepeatLength
[i
];
343 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
346 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
347 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
349 StackRepeatCount
[i
]++;
350 j
= j
+ StackRepeatLength
[i
];
361 while (i
< TraceLength
)
363 if (StackRepeatCount
[i
] == 0)
365 KeRosPrintAddress(StackTrace
[i
]);
371 if (StackRepeatLength
[i
] == 0)
375 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
377 KeRosPrintAddress(StackTrace
[i
+ j
]);
379 DbgPrint("}*%d", StackRepeatCount
[i
]);
380 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
392 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
397 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
398 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
403 * Print out the CPU registers
405 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
407 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
408 ExceptionNr
, Tf
->ErrorCode
&0xffff);
412 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
414 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
415 Tf
->Cs
&0xffff, Tf
->Eip
);
416 KeRosPrintAddress((PVOID
)Tf
->Eip
);
418 Ke386GetPageTableDirectory(cr3_
);
419 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
420 DbgPrint("Proc: %x ",PsGetCurrentProcess());
421 if (PsGetCurrentProcess() != NULL
)
423 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
424 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
426 if (PsGetCurrentThread() != NULL
)
428 DbgPrint("Thrd: %x Tid: %x",
429 PsGetCurrentThread(),
430 PsGetCurrentThread()->Cid
.UniqueThread
);
433 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
434 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
435 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
436 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
437 Tf
->Ebp
, Tf
->Esi
, Esp0
);
438 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
439 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
441 DbgPrint("kESP %.8x ", Esp0
);
442 if (PsGetCurrentThread() != NULL
)
444 DbgPrint("kernel stack base %x\n",
445 PsGetCurrentThread()->Tcb
.StackLimit
);
450 if (PsGetCurrentThread() != NULL
)
452 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
456 StackLimit
= (ULONG
)init_stack_top
;
460 * Dump the stack frames
462 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
466 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
468 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
469 * message and halt the computer
471 * Complete CPU context
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
;
488 if (ExceptionNr
== 14 && Tf
->Eflags
& FLAG_IF
)
490 Ke386EnableInterrupts();
494 * If this was a V86 mode exception then handle it specially
496 if (Tf
->Eflags
& (1 << 17))
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 DbgPrint("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
));
585 KeContextToTrapFrame(PCONTEXT Context
,
586 PKTRAP_FRAME TrapFrame
)
588 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
590 TrapFrame
->Esp
= Context
->Esp
;
591 TrapFrame
->Ss
= Context
->SegSs
;
592 TrapFrame
->Cs
= Context
->SegCs
;
593 TrapFrame
->Eip
= Context
->Eip
;
594 TrapFrame
->Eflags
= Context
->EFlags
;
595 TrapFrame
->Ebp
= Context
->Ebp
;
597 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
599 TrapFrame
->Eax
= Context
->Eax
;
600 TrapFrame
->Ebx
= Context
->Ebx
;
601 TrapFrame
->Ecx
= Context
->Ecx
;
602 TrapFrame
->Edx
= Context
->Edx
;
603 TrapFrame
->Esi
= Context
->Esi
;
604 TrapFrame
->Edi
= Context
->Edi
;
606 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
608 TrapFrame
->Ds
= Context
->SegDs
;
609 TrapFrame
->Es
= Context
->SegEs
;
610 TrapFrame
->Fs
= Context
->SegFs
;
611 TrapFrame
->Gs
= Context
->SegGs
;
613 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
618 * This should be handled separately I think.
622 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
631 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
634 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
636 Context
->SegSs
= TrapFrame
->Ss
;
637 Context
->Esp
= TrapFrame
->Esp
;
638 Context
->SegCs
= TrapFrame
->Cs
;
639 Context
->Eip
= TrapFrame
->Eip
;
640 Context
->EFlags
= TrapFrame
->Eflags
;
641 Context
->Ebp
= TrapFrame
->Ebp
;
643 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
645 Context
->Eax
= TrapFrame
->Eax
;
646 Context
->Ebx
= TrapFrame
->Ebx
;
647 Context
->Ecx
= TrapFrame
->Ecx
;
649 * NOTE: In the trap frame which is built on entry to a system
650 * call TrapFrame->Edx will actually hold the address of the
651 * previous TrapFrame. I don't believe leaking this information
652 * has security implications. Also EDX holds the address of the
653 * arguments to the system call in progress so it isn't of much
654 * interest to the debugger.
656 Context
->Edx
= TrapFrame
->Edx
;
657 Context
->Esi
= TrapFrame
->Esi
;
658 Context
->Edi
= TrapFrame
->Edi
;
660 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
662 Context
->SegDs
= TrapFrame
->Ds
;
663 Context
->SegEs
= TrapFrame
->Es
;
664 Context
->SegFs
= TrapFrame
->Fs
;
665 Context
->SegGs
= TrapFrame
->Gs
;
667 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
670 * FIXME: Implement this case
672 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
674 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
677 * FIXME: Implement this case
679 * I think this should only be filled for FPU exceptions, otherwise I
680 * would not know where to get it from as it can be the current state
681 * of the FPU or already saved in the thread's FPU save area.
684 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
687 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
690 * FIXME: Investigate this
692 * This is the XMM state (first 512 bytes of FXSAVE_FORMAT/FX_SAVE_AREA)
693 * This should only be filled in case of a SIMD exception I think, so
694 * this is not the right place (like for FPU the state could already be
695 * saved in the thread's FX_SAVE_AREA or still be in the CPU)
698 Context
->ContextFlags
&= ~CONTEXT_EXTENDED_REGISTERS
;
704 KeDumpStackFrames(PULONG Frame
)
706 PULONG StackBase
, StackEnd
;
707 MEMORY_BASIC_INFORMATION mbi
;
708 ULONG ResultLength
= sizeof(mbi
);
711 DbgPrint("Frames:\n");
714 Status
= MiQueryVirtualMemory (
717 MemoryBasicInformation
,
721 if ( !NT_SUCCESS(Status
) )
723 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
728 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
730 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
732 ULONG Addr
= Frame
[1];
733 if (!KeRosPrintAddress((PVOID
)Addr
))
734 DbgPrint("<%X>", Addr
);
735 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
738 Frame
= (PULONG
)Frame
[0];
750 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
753 PULONG StackBase
, StackEnd
;
754 MEMORY_BASIC_INFORMATION mbi
;
755 ULONG ResultLength
= sizeof(mbi
);
758 DbgPrint("Frames: ");
764 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
765 #elif defined(_MSC_VER)
766 __asm mov
[Frame
], ebp
768 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
771 Status
= MiQueryVirtualMemory (
774 MemoryBasicInformation
,
778 if ( !NT_SUCCESS(Status
) )
780 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
785 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
787 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
789 ULONG Addr
= Frame
[1];
790 if (!KeRosPrintAddress((PVOID
)Addr
))
791 DbgPrint("<%X>", Addr
);
792 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
795 Frame
= (PULONG
)Frame
[0];
807 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
810 PULONG StackBase
, StackEnd
, Frame
;
811 MEMORY_BASIC_INFORMATION mbi
;
812 ULONG ResultLength
= sizeof(mbi
);
818 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
819 #elif defined(_MSC_VER)
820 __asm mov
[Frame
], ebp
823 Status
= MiQueryVirtualMemory (
826 MemoryBasicInformation
,
830 if ( !NT_SUCCESS(Status
) )
832 DPRINT1("Can't get stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
837 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
839 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
841 Frames
[Count
++] = Frame
[1];
843 Frame
= (PULONG
)Frame
[0];
854 set_system_call_gate(unsigned int sel
, unsigned int func
)
856 DPRINT("sel %x %d\n",sel
,sel
);
857 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
859 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
860 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
863 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
865 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
866 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
868 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
871 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
873 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
875 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
877 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
881 set_task_gate(unsigned int sel
, unsigned task_sel
)
883 KiIdt
[sel
].a
= task_sel
<< 16;
884 KiIdt
[sel
].b
= 0x8500;
888 KeInitExceptions(VOID
)
890 * FUNCTION: Initalize CPU exception handling
895 DPRINT("KeInitExceptions()\n");
898 * Set up the other gates
900 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
901 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
902 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
903 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
904 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
905 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
906 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
907 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
908 set_task_gate(8, TRAP_TSS_SELECTOR
);
909 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
910 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
911 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
912 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
913 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
914 set_interrupt_gate(14, (ULONG
)KiTrap14
);
915 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
916 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
917 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
918 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
919 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
921 for (i
= 20; i
< 256; i
++)
923 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
926 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
927 set_system_call_gate(0x2e,(int)KiSystemService
);
934 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
937 PKTHREAD Thread
= KeGetCurrentThread();
940 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
942 return(ExceptionCode
);
945 OldEip
= Thread
->TrapFrame
->Eip
;
946 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
947 return((NTSTATUS
)OldEip
);
956 IN PEXCEPTION_RECORD ExceptionRecord
,
958 IN BOOLEAN SearchFrames
)
960 PKTHREAD Thread
= KeGetCurrentThread();
961 PKTRAP_FRAME TrapFrame
= Thread
->TrapFrame
;
962 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
964 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
966 KiDispatchException(ExceptionRecord
,
972 /* Restore the user context */
973 Thread
->TrapFrame
= PrevTrapFrame
;
974 __asm__("mov %%ebx, %%esp;\n" "jmp _KiServiceExit": : "b" (TrapFrame
));
976 /* We never get here */
977 return(STATUS_SUCCESS
);