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 PLDR_DATA_TABLE_ENTRY current
;
114 extern LIST_ENTRY ModuleListHead
;
115 ULONG_PTR RelativeAddress
;
120 current_entry
= ModuleListHead
.Flink
;
122 while (current_entry
!= &ModuleListHead
)
125 CONTAINING_RECORD(current_entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderModuleList
);
127 if (address
>= (PVOID
)current
->DllBase
&&
128 address
< (PVOID
)((ULONG_PTR
)current
->DllBase
+ current
->SizeOfImage
))
130 RelativeAddress
= (ULONG_PTR
) address
- (ULONG_PTR
) current
->DllBase
;
131 DbgPrint("<%wZ: %x>", ¤t
->FullDllName
, RelativeAddress
);
134 current_entry
= current_entry
->Flink
;
137 address
= (PVOID
)((ULONG_PTR
)address
& ~(ULONG_PTR
)MmSystemRangeStart
);
144 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
148 Er
.ExceptionFlags
= 0;
149 Er
.ExceptionRecord
= NULL
;
150 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
152 if (ExceptionNr
== 14)
154 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
155 Er
.NumberParameters
= 2;
156 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
157 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
161 if (ExceptionNr
< ARRAY_SIZE(ExceptionToNtStatus
))
163 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
167 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
169 Er
.NumberParameters
= 0;
172 /* FIXME: Which exceptions are noncontinuable? */
173 Er
.ExceptionFlags
= 0;
175 KiDispatchException(&Er
, 0, Tf
, KernelMode
, TRUE
);
181 KiDoubleFaultHandler(VOID
)
187 ULONG ExceptionNr
= 8;
193 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
194 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
195 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
200 OldTss
= KeGetCurrentKPCR()->TSS
;
205 if (PsGetCurrentThread() != NULL
&&
206 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
209 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
217 * Check for stack underflow
219 if (PsGetCurrentThread() != NULL
&&
220 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
222 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
223 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
228 * Print out the CPU registers
230 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
232 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
237 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
239 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
240 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
242 DbgPrint("cr2 %x cr3 %x ", cr2
, OldCr3
);
243 DbgPrint("Proc: %x ",PsGetCurrentProcess());
244 if (PsGetCurrentProcess() != NULL
)
246 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
247 DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName
);
249 if (PsGetCurrentThread() != NULL
)
251 DbgPrint("Thrd: %x Tid: %x",
252 PsGetCurrentThread(),
253 PsGetCurrentThread()->Cid
.UniqueThread
);
256 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
257 OldTss
->Fs
, OldTss
->Gs
);
258 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
260 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\nESP: %.8x ", OldTss
->Edx
,
261 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
262 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
263 if (OldTss
->Cs
== KERNEL_CS
)
265 DbgPrint("kESP %.8x ", Esp0
);
266 if (PsGetCurrentThread() != NULL
)
268 DbgPrint("kernel stack base %x\n",
269 PsGetCurrentThread()->Tcb
.StackLimit
);
275 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
277 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
279 if (PsGetCurrentThread() != NULL
)
281 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
282 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
286 StackLimit
= (ULONG
)init_stack_top
;
287 StackBase
= (ULONG
)init_stack
;
291 Change to an #if 0 to reduce the amount of information printed on
292 a recursive stack trace.
295 DbgPrint("Frames: ");
296 Frame
= (PULONG
)OldTss
->Ebp
;
297 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
299 KeRosPrintAddress((PVOID
)Frame
[1]);
300 Frame
= (PULONG
)Frame
[0];
304 DbgPrint("Frames: ");
306 Frame
= (PULONG
)OldTss
->Ebp
;
307 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
309 StackTrace
[i
] = (PVOID
)Frame
[1];
310 Frame
= (PULONG
)Frame
[0];
316 while (i
< TraceLength
)
318 StackRepeatCount
[i
] = 0;
321 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
323 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
324 (j
- i
) * sizeof(PVOID
)) == 0)
326 StackRepeatCount
[i
] = 2;
327 StackRepeatLength
[i
] = j
- i
;
335 if (FoundRepeat
== FALSE
)
340 j
= j
+ StackRepeatLength
[i
];
341 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
344 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
345 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
347 StackRepeatCount
[i
]++;
348 j
= j
+ StackRepeatLength
[i
];
359 while (i
< TraceLength
)
361 if (StackRepeatCount
[i
] == 0)
363 KeRosPrintAddress(StackTrace
[i
]);
369 if (StackRepeatLength
[i
] == 0)
373 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
375 KeRosPrintAddress(StackTrace
[i
+ j
]);
377 DbgPrint("}*%d", StackRepeatCount
[i
]);
378 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
389 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
394 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
395 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
400 * Print out the CPU registers
402 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
404 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
405 ExceptionNr
, Tf
->ErrorCode
&0xffff);
409 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
411 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
412 Tf
->Cs
&0xffff, Tf
->Eip
);
413 KeRosPrintAddress((PVOID
)Tf
->Eip
);
415 Ke386GetPageTableDirectory(cr3_
);
416 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
417 DbgPrint("Proc: %x ",PsGetCurrentProcess());
418 if (PsGetCurrentProcess() != NULL
)
420 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
421 DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName
);
423 if (PsGetCurrentThread() != NULL
)
425 DbgPrint("Thrd: %x Tid: %x",
426 PsGetCurrentThread(),
427 PsGetCurrentThread()->Cid
.UniqueThread
);
430 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
431 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
432 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
433 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
434 Tf
->Ebp
, Tf
->Esi
, Esp0
);
435 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
436 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
438 DbgPrint("kESP %.8x ", Esp0
);
439 if (PsGetCurrentThread() != NULL
)
441 DbgPrint("kernel stack base %x\n",
442 PsGetCurrentThread()->Tcb
.StackLimit
);
447 if (PsGetCurrentThread() != NULL
)
449 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
453 StackLimit
= (ULONG
)init_stack_top
;
457 * Dump the stack frames
459 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
463 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
465 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
466 * message and halt the computer
468 * Complete CPU context
475 ASSERT(ExceptionNr
!= 14);
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
;
488 * If this was a V86 mode exception then handle it specially
490 if (Tf
->Eflags
& (1 << 17))
492 DPRINT("Tf->Eflags, %x, Tf->Eip %x, ExceptionNr: %d\n", Tf
->Eflags
, Tf
->Eip
, ExceptionNr
);
493 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
497 * Check for stack underflow, this may be obsolete
499 if (PsGetCurrentThread() != NULL
&&
500 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
502 DPRINT1("Stack underflow (tf->esp %x Limit %x Eip %x)\n",
503 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
, Tf
->Eip
);
507 if (ExceptionNr
== 15)
511 * This exception should never occur. The P6 has a bug, which does sometimes deliver
512 * the apic spurious interrupt as exception 15. On an athlon64, I get one exception
513 * in the early boot phase in apic mode (using the smp build). I've looked to the linux
514 * sources. Linux does ignore this exception.
518 DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
523 * Check for a breakpoint that was only for the attention of the debugger.
525 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
528 EIP is already adjusted by the processor to point to the instruction
529 after the breakpoint.
535 * Try to handle device-not-present, math-fault and xmm-fault exceptions.
537 if (ExceptionNr
== 7 || ExceptionNr
== 16 || ExceptionNr
== 19)
539 Status
= KiHandleFpuFault(Tf
, ExceptionNr
);
540 if (NT_SUCCESS(Status
))
547 * Handle user exceptions differently
549 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
551 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
555 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
561 KeContextToTrapFrame(PCONTEXT Context
,
562 PKTRAP_FRAME TrapFrame
)
564 /* Start with the basic Registers */
565 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
567 TrapFrame
->Esp
= Context
->Esp
;
568 TrapFrame
->Ss
= Context
->SegSs
;
569 TrapFrame
->Cs
= Context
->SegCs
;
570 TrapFrame
->Eip
= Context
->Eip
;
571 TrapFrame
->Eflags
= Context
->EFlags
;
572 TrapFrame
->Ebp
= Context
->Ebp
;
575 /* Process the Integer Registers */
576 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
578 TrapFrame
->Eax
= Context
->Eax
;
579 TrapFrame
->Ebx
= Context
->Ebx
;
580 TrapFrame
->Ecx
= Context
->Ecx
;
581 TrapFrame
->Edx
= Context
->Edx
;
582 TrapFrame
->Esi
= Context
->Esi
;
583 TrapFrame
->Edi
= Context
->Edi
;
586 /* Process the Context Segments */
587 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
589 TrapFrame
->Ds
= Context
->SegDs
;
590 TrapFrame
->Es
= Context
->SegEs
;
591 TrapFrame
->Fs
= Context
->SegFs
;
592 TrapFrame
->Gs
= Context
->SegGs
;
595 /* Handle the Debug Registers */
596 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
598 TrapFrame
->Dr0
= Context
->Dr0
;
599 TrapFrame
->Dr1
= Context
->Dr1
;
600 TrapFrame
->Dr2
= Context
->Dr2
;
601 TrapFrame
->Dr3
= Context
->Dr3
;
602 TrapFrame
->Dr6
= Context
->Dr6
;
603 TrapFrame
->Dr7
= Context
->Dr7
;
606 /* Handle FPU and Extended Registers */
607 return KiContextToFxSaveArea((PFX_SAVE_AREA
)(TrapFrame
+ 1), Context
);
611 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
614 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
616 Context
->SegSs
= TrapFrame
->Ss
;
617 Context
->Esp
= TrapFrame
->Esp
;
618 Context
->SegCs
= TrapFrame
->Cs
;
619 Context
->Eip
= TrapFrame
->Eip
;
620 Context
->EFlags
= TrapFrame
->Eflags
;
621 Context
->Ebp
= TrapFrame
->Ebp
;
623 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
625 Context
->Eax
= TrapFrame
->Eax
;
626 Context
->Ebx
= TrapFrame
->Ebx
;
627 Context
->Ecx
= TrapFrame
->Ecx
;
629 * NOTE: In the trap frame which is built on entry to a system
630 * call TrapFrame->Edx will actually hold the address of the
631 * previous TrapFrame. I don't believe leaking this information
632 * has security implications. Also EDX holds the address of the
633 * arguments to the system call in progress so it isn't of much
634 * interest to the debugger.
636 Context
->Edx
= TrapFrame
->Edx
;
637 Context
->Esi
= TrapFrame
->Esi
;
638 Context
->Edi
= TrapFrame
->Edi
;
640 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
642 Context
->SegDs
= TrapFrame
->Ds
;
643 Context
->SegEs
= TrapFrame
->Es
;
644 Context
->SegFs
= TrapFrame
->Fs
;
645 Context
->SegGs
= TrapFrame
->Gs
;
647 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
650 * FIXME: Implement this case
652 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
654 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
657 * FIXME: Implement this case
659 * I think this should only be filled for FPU exceptions, otherwise I
660 * would not know where to get it from as it can be the current state
661 * of the FPU or already saved in the thread's FPU save area.
664 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
667 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
670 * FIXME: Investigate this
672 * This is the XMM state (first 512 bytes of FXSAVE_FORMAT/FX_SAVE_AREA)
673 * This should only be filled in case of a SIMD exception I think, so
674 * this is not the right place (like for FPU the state could already be
675 * saved in the thread's FX_SAVE_AREA or still be in the CPU)
678 Context
->ContextFlags
&= ~CONTEXT_EXTENDED_REGISTERS
;
684 KeDumpStackFrames(PULONG Frame
)
686 PULONG StackBase
, StackEnd
;
687 MEMORY_BASIC_INFORMATION mbi
;
688 ULONG ResultLength
= sizeof(mbi
);
691 DbgPrint("Frames:\n");
694 Status
= MiQueryVirtualMemory (
697 MemoryBasicInformation
,
701 if ( !NT_SUCCESS(Status
) )
703 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
708 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
710 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
712 ULONG Addr
= Frame
[1];
713 if (!KeRosPrintAddress((PVOID
)Addr
))
714 DbgPrint("<%X>", Addr
);
715 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
718 Frame
= (PULONG
)Frame
[0];
730 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
733 PULONG StackBase
, StackEnd
;
734 MEMORY_BASIC_INFORMATION mbi
;
735 ULONG ResultLength
= sizeof(mbi
);
738 DbgPrint("Frames: ");
744 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
745 #elif defined(_MSC_VER)
746 __asm mov
[Frame
], ebp
748 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
751 Status
= MiQueryVirtualMemory (
754 MemoryBasicInformation
,
758 if ( !NT_SUCCESS(Status
) )
760 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
765 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
767 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
769 ULONG Addr
= Frame
[1];
770 if (!KeRosPrintAddress((PVOID
)Addr
))
771 DbgPrint("<%X>", Addr
);
772 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
775 Frame
= (PULONG
)Frame
[0];
787 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
790 PULONG StackBase
, StackEnd
, Frame
;
791 MEMORY_BASIC_INFORMATION mbi
;
792 ULONG ResultLength
= sizeof(mbi
);
798 __asm__("mov %%ebp, %%ebx" : "=b" (Frame
) : );
799 #elif defined(_MSC_VER)
800 __asm mov
[Frame
], ebp
803 Status
= MiQueryVirtualMemory (
806 MemoryBasicInformation
,
810 if ( !NT_SUCCESS(Status
) )
812 DPRINT1("Can't get stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
817 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
819 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
821 Frames
[Count
++] = Frame
[1];
823 Frame
= (PULONG
)Frame
[0];
834 set_system_call_gate(unsigned int sel
, unsigned int func
)
836 DPRINT("sel %x %d\n",sel
,sel
);
837 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
839 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
840 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
843 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
845 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
846 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
848 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
851 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
853 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
855 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
857 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
861 set_task_gate(unsigned int sel
, unsigned task_sel
)
863 KiIdt
[sel
].a
= task_sel
<< 16;
864 KiIdt
[sel
].b
= 0x8500;
868 KeInitExceptions(VOID
)
870 * FUNCTION: Initalize CPU exception handling
875 DPRINT("KeInitExceptions()\n");
878 * Set up the other gates
880 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
881 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
882 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
883 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
884 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
885 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
886 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
887 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
888 set_task_gate(8, TRAP_TSS_SELECTOR
);
889 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
890 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
891 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
892 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
893 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
894 set_interrupt_gate(14, (ULONG
)KiTrap14
);
895 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
896 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
897 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
898 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
899 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
901 for (i
= 20; i
< 256; i
++)
903 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
906 set_system_call_gate(0x2d,(int)KiDebugService
);
907 set_system_call_gate(0x2e,(int)KiSystemService
);
914 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
917 PKTHREAD Thread
= KeGetCurrentThread();
920 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
922 return(ExceptionCode
);
925 OldEip
= Thread
->TrapFrame
->Eip
;
926 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)KeRaiseUserExceptionDispatcher
;
927 return((NTSTATUS
)OldEip
);
936 IN PEXCEPTION_RECORD ExceptionRecord
,
938 IN BOOLEAN SearchFrames
)
940 PKTHREAD Thread
= KeGetCurrentThread();
941 PKTRAP_FRAME TrapFrame
= Thread
->TrapFrame
;
942 PKTRAP_FRAME PrevTrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
944 KeGetCurrentKPCR()->Tib
.ExceptionList
= TrapFrame
->ExceptionList
;
946 KiDispatchException(ExceptionRecord
,
952 /* Restore the user context */
953 Thread
->TrapFrame
= PrevTrapFrame
;
954 __asm__("mov %%ebx, %%esp;\n" "jmp _KiServiceExit": : "b" (TrapFrame
));
956 /* We never get here */
957 return(STATUS_SUCCESS
);