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
;
127 current_entry
= ModuleTextListHead
.Flink
;
129 while (current_entry
!= &ModuleTextListHead
&&
130 current_entry
!= NULL
)
133 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
135 if (address
>= (PVOID
)current
->Base
&&
136 address
< (PVOID
)(current
->Base
+ current
->Length
))
138 RelativeAddress
= (ULONG_PTR
) address
- current
->Base
;
139 DbgPrint("<%ws: %x>", current
->Name
, RelativeAddress
);
142 current_entry
= current_entry
->Flink
;
145 address
= (PVOID
)((ULONG_PTR
)address
& ~0xC0000000);
153 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
157 Er
.ExceptionFlags
= 0;
158 Er
.ExceptionRecord
= NULL
;
159 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
161 if (ExceptionNr
== 14)
163 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
164 Er
.NumberParameters
= 2;
165 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
166 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
170 if (ExceptionNr
< ARRAY_SIZE(ExceptionToNtStatus
))
172 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
176 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
178 Er
.NumberParameters
= 0;
181 /* FIXME: Which exceptions are noncontinuable? */
182 Er
.ExceptionFlags
= 0;
184 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
190 KiDoubleFaultHandler(VOID
)
196 ULONG ExceptionNr
= 8;
202 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
203 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
204 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
209 OldTss
= KeGetCurrentKPCR()->TSS
;
214 if (PsGetCurrentThread() != NULL
&&
215 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
218 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
226 * Check for stack underflow
228 if (PsGetCurrentThread() != NULL
&&
229 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
231 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
232 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
237 * Print out the CPU registers
239 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
241 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
246 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
248 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
249 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
251 DbgPrint("cr2 %x cr3 %x ", cr2
, OldCr3
);
252 DbgPrint("Proc: %x ",PsGetCurrentProcess());
253 if (PsGetCurrentProcess() != NULL
)
255 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
256 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
258 if (PsGetCurrentThread() != NULL
)
260 DbgPrint("Thrd: %x Tid: %x",
261 PsGetCurrentThread(),
262 PsGetCurrentThread()->Cid
.UniqueThread
);
265 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
266 OldTss
->Fs
, OldTss
->Gs
);
267 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
269 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n ESP: %.8x", OldTss
->Edx
,
270 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
271 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
272 if (OldTss
->Cs
== KERNEL_CS
)
274 DbgPrint("kESP %.8x ", Esp0
);
275 if (PsGetCurrentThread() != NULL
)
277 DbgPrint("kernel stack base %x\n",
278 PsGetCurrentThread()->Tcb
.StackLimit
);
284 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
286 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
288 if (PsGetCurrentThread() != NULL
)
290 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
291 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
295 StackLimit
= (ULONG
)init_stack_top
;
296 StackBase
= (ULONG
)init_stack
;
300 Change to an #if 0 to reduce the amount of information printed on
301 a recursive stack trace.
304 DbgPrint("Frames: ");
305 Frame
= (PULONG
)OldTss
->Ebp
;
306 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
308 KeRosPrintAddress((PVOID
)Frame
[1]);
309 Frame
= (PULONG
)Frame
[0];
313 DbgPrint("Frames: ");
315 Frame
= (PULONG
)OldTss
->Ebp
;
316 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
318 StackTrace
[i
] = (PVOID
)Frame
[1];
319 Frame
= (PULONG
)Frame
[0];
325 while (i
< TraceLength
)
327 StackRepeatCount
[i
] = 0;
330 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
332 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
333 (j
- i
) * sizeof(PVOID
)) == 0)
335 StackRepeatCount
[i
] = 2;
336 StackRepeatLength
[i
] = j
- i
;
344 if (FoundRepeat
== FALSE
)
349 j
= j
+ StackRepeatLength
[i
];
350 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
353 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
354 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
356 StackRepeatCount
[i
]++;
357 j
= j
+ StackRepeatLength
[i
];
368 while (i
< TraceLength
)
370 if (StackRepeatCount
[i
] == 0)
372 KeRosPrintAddress(StackTrace
[i
]);
378 if (StackRepeatLength
[i
] == 0)
382 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
384 KeRosPrintAddress(StackTrace
[i
+ j
]);
386 DbgPrint("}*%d", StackRepeatCount
[i
]);
387 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
399 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
404 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
405 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
410 * Print out the CPU registers
412 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
414 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
415 ExceptionNr
, Tf
->ErrorCode
&0xffff);
419 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
421 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
422 Tf
->Cs
&0xffff, Tf
->Eip
);
423 KeRosPrintAddress((PVOID
)Tf
->Eip
);
425 Ke386GetPageTableDirectory(cr3_
);
426 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
427 DbgPrint("Proc: %x ",PsGetCurrentProcess());
428 if (PsGetCurrentProcess() != NULL
)
430 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
431 DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName
);
433 if (PsGetCurrentThread() != NULL
)
435 DbgPrint("Thrd: %x Tid: %x",
436 PsGetCurrentThread(),
437 PsGetCurrentThread()->Cid
.UniqueThread
);
440 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
441 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
442 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
443 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
444 Tf
->Ebp
, Tf
->Esi
, Esp0
);
445 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
446 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
448 DbgPrint("kESP %.8x ", Esp0
);
449 if (PsGetCurrentThread() != NULL
)
451 DbgPrint("kernel stack base %x\n",
452 PsGetCurrentThread()->Tcb
.StackLimit
);
457 if (PsGetCurrentThread() != NULL
)
459 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
463 StackLimit
= (ULONG
)init_stack_top
;
467 * Dump the stack frames
469 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
473 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
475 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
476 * message and halt the computer
478 * Complete CPU context
485 /* Store the exception number in an unused field in the trap frame. */
486 Tf
->DebugArgMark
= (PVOID
)ExceptionNr
;
488 /* Use the address of the trap frame as approximation to the ring0 esp */
489 Esp0
= (ULONG
)&Tf
->Eip
;
493 Tf
->DebugPointer
= (PVOID
)cr2
;
495 if (ExceptionNr
== 14 && Tf
->Eflags
& FLAG_IF
)
497 Ke386EnableInterrupts();
501 * If this was a V86 mode exception then handle it specially
503 if (Tf
->Eflags
& (1 << 17))
505 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
509 * Check for stack underflow, this may be obsolete
511 if (PsGetCurrentThread() != NULL
&&
512 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
514 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
515 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
519 if (ExceptionNr
== 15)
523 * This exception should never occur. The P6 has a bug, which does sometimes deliver
524 * the apic spurious interrupt as exception 15. On an athlon64, I get one exception
525 * in the early boot phase in apic mode (using the smp build). I've looked to the linux
526 * sources. Linux does ignore this exception.
530 DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
535 * Maybe handle the page fault and return
537 if (ExceptionNr
== 14)
539 if (Ke386NoExecute
&& Tf
->ErrorCode
& 0x10 && cr2
>= KERNEL_BASE
)
541 KEBUGCHECKWITHTF(ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY
, 0, 0, 0, 0, Tf
);
543 Status
= MmPageFault(Tf
->Cs
&0xffff,
548 if (NT_SUCCESS(Status
))
555 * Check for a breakpoint that was only for the attention of the debugger.
557 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
560 EIP is already adjusted by the processor to point to the instruction
561 after the breakpoint.
567 * Try to handle device-not-present, math-fault and xmm-fault exceptions.
569 if (ExceptionNr
== 7 || ExceptionNr
== 16 || ExceptionNr
== 19)
571 Status
= KiHandleFpuFault(Tf
, ExceptionNr
);
572 if (NT_SUCCESS(Status
))
579 * Handle user exceptions differently
581 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
583 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
587 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
592 KeContextToTrapFrame(PCONTEXT Context
,
593 PKTRAP_FRAME TrapFrame
)
595 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
597 TrapFrame
->Esp
= Context
->Esp
;
598 TrapFrame
->Ss
= Context
->SegSs
;
599 TrapFrame
->Cs
= Context
->SegCs
;
600 TrapFrame
->Eip
= Context
->Eip
;
601 TrapFrame
->Eflags
= Context
->EFlags
;
602 TrapFrame
->Ebp
= Context
->Ebp
;
604 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
606 TrapFrame
->Eax
= Context
->Eax
;
607 TrapFrame
->Ebx
= Context
->Ebx
;
608 TrapFrame
->Ecx
= Context
->Ecx
;
609 TrapFrame
->Edx
= Context
->Edx
;
610 TrapFrame
->Esi
= Context
->Esi
;
611 TrapFrame
->Edi
= Context
->Edi
;
613 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
615 TrapFrame
->Ds
= Context
->SegDs
;
616 TrapFrame
->Es
= Context
->SegEs
;
617 TrapFrame
->Fs
= Context
->SegFs
;
618 TrapFrame
->Gs
= Context
->SegGs
;
620 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
625 * This should be handled separately I think.
629 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
638 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
641 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
643 Context
->SegSs
= TrapFrame
->Ss
;
644 Context
->Esp
= TrapFrame
->Esp
;
645 Context
->SegCs
= TrapFrame
->Cs
;
646 Context
->Eip
= TrapFrame
->Eip
;
647 Context
->EFlags
= TrapFrame
->Eflags
;
648 Context
->Ebp
= TrapFrame
->Ebp
;
650 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
652 Context
->Eax
= TrapFrame
->Eax
;
653 Context
->Ebx
= TrapFrame
->Ebx
;
654 Context
->Ecx
= TrapFrame
->Ecx
;
656 * NOTE: In the trap frame which is built on entry to a system
657 * call TrapFrame->Edx will actually hold the address of the
658 * previous TrapFrame. I don't believe leaking this information
659 * has security implications. Also EDX holds the address of the
660 * arguments to the system call in progress so it isn't of much
661 * interest to the debugger.
663 Context
->Edx
= TrapFrame
->Edx
;
664 Context
->Esi
= TrapFrame
->Esi
;
665 Context
->Edi
= TrapFrame
->Edi
;
667 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
669 Context
->SegDs
= TrapFrame
->Ds
;
670 Context
->SegEs
= TrapFrame
->Es
;
671 Context
->SegFs
= TrapFrame
->Fs
;
672 Context
->SegGs
= TrapFrame
->Gs
;
674 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
677 * FIXME: Implement this case
679 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
681 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
684 * FIXME: Implement this case
686 * I think this should only be filled for FPU exceptions, otherwise I
687 * would not know where to get it from as it can be the current state
688 * of the FPU or already saved in the thread's FPU save area.
691 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
694 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
697 * FIXME: Investigate this
699 * This is the XMM state (first 512 bytes of FXSAVE_FORMAT/FX_SAVE_AREA)
700 * This should only be filled in case of a SIMD exception I think, so
701 * this is not the right place (like for FPU the state could already be
702 * saved in the thread's FX_SAVE_AREA or still be in the CPU)
705 Context
->ContextFlags
&= ~CONTEXT_EXTENDED_REGISTERS
;
711 KeDumpStackFrames(PULONG Frame
)
713 PULONG StackBase
, StackEnd
;
714 MEMORY_BASIC_INFORMATION mbi
;
715 ULONG ResultLength
= sizeof(mbi
);
718 DbgPrint("Frames:\n");
721 Status
= MiQueryVirtualMemory (
724 MemoryBasicInformation
,
728 if ( !NT_SUCCESS(Status
) )
730 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
735 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
737 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
739 ULONG Addr
= Frame
[1];
740 if (!KeRosPrintAddress((PVOID
)Addr
))
741 DbgPrint("<%X>", Addr
);
742 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
745 Frame
= (PULONG
)Frame
[0];
757 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
760 PULONG StackBase
, StackEnd
;
761 MEMORY_BASIC_INFORMATION mbi
;
762 ULONG ResultLength
= sizeof(mbi
);
765 DbgPrint("Frames: ");
771 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
772 #elif defined(_MSC_VER)
773 __asm mov
[Frame
], ebp
775 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
778 Status
= MiQueryVirtualMemory (
781 MemoryBasicInformation
,
785 if ( !NT_SUCCESS(Status
) )
787 DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
792 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
794 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
796 ULONG Addr
= Frame
[1];
797 if (!KeRosPrintAddress((PVOID
)Addr
))
798 DbgPrint("<%X>", Addr
);
799 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
802 Frame
= (PULONG
)Frame
[0];
814 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
817 PULONG StackBase
, StackEnd
, Frame
;
818 MEMORY_BASIC_INFORMATION mbi
;
819 ULONG ResultLength
= sizeof(mbi
);
825 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
826 #elif defined(_MSC_VER)
827 __asm mov
[Frame
], ebp
830 Status
= MiQueryVirtualMemory (
833 MemoryBasicInformation
,
837 if ( !NT_SUCCESS(Status
) )
839 DPRINT1("Can't get stack frames: NtQueryVirtualMemory() failed: %x\n", Status
);
844 StackEnd
= mbi
.BaseAddress
+ mbi
.RegionSize
;
846 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
848 Frames
[Count
++] = Frame
[1];
850 Frame
= (PULONG
)Frame
[0];
861 set_system_call_gate(unsigned int sel
, unsigned int func
)
863 DPRINT("sel %x %d\n",sel
,sel
);
864 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
866 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
867 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
870 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
872 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
873 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
875 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
878 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
880 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
882 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
884 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
888 set_task_gate(unsigned int sel
, unsigned task_sel
)
890 KiIdt
[sel
].a
= task_sel
<< 16;
891 KiIdt
[sel
].b
= 0x8500;
895 KeInitExceptions(VOID
)
897 * FUNCTION: Initalize CPU exception handling
902 DPRINT("KeInitExceptions()\n");
905 * Set up the other gates
907 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
908 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
909 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
910 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
911 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
912 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
913 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
914 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
915 set_task_gate(8, TRAP_TSS_SELECTOR
);
916 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
917 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
918 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
919 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
920 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
921 set_interrupt_gate(14, (ULONG
)KiTrap14
);
922 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
923 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
924 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
925 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
926 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
928 for (i
= 20; i
< 256; i
++)
930 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
933 set_system_call_gate(0x2d,(int)interrupt_handler2d
);
934 set_system_call_gate(0x2e,(int)KiSystemService
);
941 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
944 PKTHREAD Thread
= KeGetCurrentThread();
947 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
949 return(ExceptionCode
);
952 OldEip
= Thread
->TrapFrame
->Eip
;
953 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)LdrpGetSystemDllRaiseExceptionDispatcher();
954 return((NTSTATUS
)OldEip
);
963 IN PEXCEPTION_RECORD ExceptionRecord
,
965 IN BOOLEAN SearchFrames
)
967 PKTHREAD Thread
= KeGetCurrentThread();
968 PKTRAP_FRAME TrapFrame
= Thread
->TrapFrame
;
969 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
971 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
973 KiDispatchException(ExceptionRecord
,
979 /* Restore the user context */
980 Thread
->TrapFrame
= PrevTrapFrame
;
981 __asm__("mov %%ebx, %%esp;\n" "jmp _KiServiceExit": : "b" (TrapFrame
));
983 /* We never get here */
984 return(STATUS_SUCCESS
);