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>
22 * - Sanitize some context fields.
23 * - Add PSEH handler when an exception occurs in an exception (KiCopyExceptionRecord).
24 * - Implement official stack trace functions (exported) and remove stuff here.
25 * - Forward exceptions to user-mode debugger.
30 Ki386AdjustEsp0(IN PKTRAP_FRAME TrapFrame
);
32 /* GLOBALS *****************************************************************/
34 #define FLAG_IF (1<<9)
37 #define STR(x) _STR(x)
40 # define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
43 extern void KiSystemService(void);
44 extern void KiDebugService(void);
46 extern VOID
KiTrap0(VOID
);
47 extern VOID
KiTrap1(VOID
);
48 extern VOID
KiTrap2(VOID
);
49 extern VOID
KiTrap3(VOID
);
50 extern VOID
KiTrap4(VOID
);
51 extern VOID
KiTrap5(VOID
);
52 extern VOID
KiTrap6(VOID
);
53 extern VOID
KiTrap7(VOID
);
54 extern VOID
KiTrap8(VOID
);
55 extern VOID
KiTrap9(VOID
);
56 extern VOID
KiTrap10(VOID
);
57 extern VOID
KiTrap11(VOID
);
58 extern VOID
KiTrap12(VOID
);
59 extern VOID
KiTrap13(VOID
);
60 extern VOID
KiTrap14(VOID
);
61 extern VOID
KiTrap15(VOID
);
62 extern VOID
KiTrap16(VOID
);
63 extern VOID
KiTrap17(VOID
);
64 extern VOID
KiTrap18(VOID
);
65 extern VOID
KiTrap19(VOID
);
66 extern VOID
KiTrapUnknown(VOID
);
68 extern ULONG init_stack
;
69 extern ULONG init_stack_top
;
71 extern BOOLEAN Ke386NoExecute
;
73 static char *ExceptionTypeStrings
[] =
80 "BOUND range exceeded",
82 "No Math Coprocessor",
86 "Segment Not Present",
87 "Stack Segment Fault",
97 NTSTATUS ExceptionToNtStatus
[] =
99 STATUS_INTEGER_DIVIDE_BY_ZERO
,
101 STATUS_ACCESS_VIOLATION
,
103 STATUS_INTEGER_OVERFLOW
,
104 STATUS_ARRAY_BOUNDS_EXCEEDED
,
105 STATUS_ILLEGAL_INSTRUCTION
,
106 STATUS_FLOAT_INVALID_OPERATION
,
107 STATUS_ACCESS_VIOLATION
,
108 STATUS_ACCESS_VIOLATION
,
109 STATUS_ACCESS_VIOLATION
,
110 STATUS_ACCESS_VIOLATION
,
111 STATUS_STACK_OVERFLOW
,
112 STATUS_ACCESS_VIOLATION
,
113 STATUS_ACCESS_VIOLATION
,
114 STATUS_ACCESS_VIOLATION
, /* RESERVED */
115 STATUS_FLOAT_INVALID_OPERATION
, /* Should not be used, the FPU can give more specific info */
116 STATUS_DATATYPE_MISALIGNMENT
,
117 STATUS_ACCESS_VIOLATION
,
118 STATUS_FLOAT_MULTIPLE_TRAPS
,
121 /* FUNCTIONS ****************************************************************/
124 KiRosPrintAddress(PVOID address
)
126 PLIST_ENTRY current_entry
;
127 PLDR_DATA_TABLE_ENTRY current
;
128 extern LIST_ENTRY ModuleListHead
;
129 ULONG_PTR RelativeAddress
;
134 current_entry
= ModuleListHead
.Flink
;
136 while (current_entry
!= &ModuleListHead
)
139 CONTAINING_RECORD(current_entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderModuleList
);
141 if (address
>= (PVOID
)current
->DllBase
&&
142 address
< (PVOID
)((ULONG_PTR
)current
->DllBase
+ current
->SizeOfImage
))
144 RelativeAddress
= (ULONG_PTR
) address
- (ULONG_PTR
) current
->DllBase
;
145 DbgPrint("<%wZ: %x>", ¤t
->FullDllName
, RelativeAddress
);
148 current_entry
= current_entry
->Flink
;
151 address
= (PVOID
)((ULONG_PTR
)address
& ~(ULONG_PTR
)MmSystemRangeStart
);
158 KiKernelTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
)
162 Er
.ExceptionFlags
= 0;
163 Er
.ExceptionRecord
= NULL
;
164 Er
.ExceptionAddress
= (PVOID
)Tf
->Eip
;
166 if (ExceptionNr
== 14)
168 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
169 Er
.NumberParameters
= 2;
170 Er
.ExceptionInformation
[0] = Tf
->ErrorCode
& 0x1;
171 Er
.ExceptionInformation
[1] = (ULONG
)Cr2
;
175 if (ExceptionNr
< ARRAY_SIZE(ExceptionToNtStatus
))
177 Er
.ExceptionCode
= ExceptionToNtStatus
[ExceptionNr
];
181 Er
.ExceptionCode
= STATUS_ACCESS_VIOLATION
;
183 Er
.NumberParameters
= 0;
186 /* FIXME: Which exceptions are noncontinuable? */
187 Er
.ExceptionFlags
= 0;
189 KiDispatchException(&Er
, NULL
, Tf
, KernelMode
, TRUE
);
195 KiDoubleFaultHandler(VOID
)
201 ULONG ExceptionNr
= 8;
207 static PVOID StackTrace
[MM_STACK_SIZE
/ sizeof(PVOID
)];
208 static ULONG StackRepeatCount
[MM_STACK_SIZE
/ sizeof(PVOID
)];
209 static ULONG StackRepeatLength
[MM_STACK_SIZE
/ sizeof(PVOID
)];
214 OldTss
= KeGetCurrentKPCR()->TSS
;
219 if (PsGetCurrentThread() != NULL
&&
220 PsGetCurrentThread()->ThreadsProcess
!= NULL
)
223 PsGetCurrentThread()->ThreadsProcess
->Pcb
.DirectoryTableBase
.QuadPart
;
231 * Check for stack underflow
233 if (PsGetCurrentThread() != NULL
&&
234 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
236 DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
237 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
);
242 * Print out the CPU registers
244 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
246 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
251 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, 0);
253 DbgPrint("CS:EIP %x:%x ", OldTss
->Cs
, OldTss
->Eip
);
254 KeRosPrintAddress((PVOID
)OldTss
->Eip
);
256 DbgPrint("cr2 %x cr3 %x ", cr2
, OldCr3
);
257 DbgPrint("Proc: %x ",PsGetCurrentProcess());
258 if (PsGetCurrentProcess() != NULL
)
260 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
261 DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName
);
263 if (PsGetCurrentThread() != NULL
)
265 DbgPrint("Thrd: %x Tid: %x",
266 PsGetCurrentThread(),
267 PsGetCurrentThread()->Cid
.UniqueThread
);
270 DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss
->Ds
, OldTss
->Es
,
271 OldTss
->Fs
, OldTss
->Gs
);
272 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss
->Eax
, OldTss
->Ebx
,
274 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\nESP: %.8x ", OldTss
->Edx
,
275 OldTss
->Ebp
, OldTss
->Esi
, Esp0
);
276 DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss
->Edi
, OldTss
->Eflags
);
277 if (OldTss
->Cs
== KERNEL_CS
)
279 DbgPrint("kESP %.8x ", Esp0
);
280 if (PsGetCurrentThread() != NULL
)
282 DbgPrint("kernel stack base %x\n",
283 PsGetCurrentThread()->Tcb
.StackLimit
);
289 DbgPrint("User ESP %.8x\n", OldTss
->Esp
);
291 if ((OldTss
->Cs
& 0xffff) == KERNEL_CS
)
293 if (PsGetCurrentThread() != NULL
)
295 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
296 StackBase
= (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
;
300 StackLimit
= (ULONG
)init_stack_top
;
301 StackBase
= (ULONG
)init_stack
;
305 Change to an #if 0 to reduce the amount of information printed on
306 a recursive stack trace.
309 DbgPrint("Frames: ");
310 Frame
= (PULONG
)OldTss
->Ebp
;
311 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
313 KeRosPrintAddress((PVOID
)Frame
[1]);
314 Frame
= (PULONG
)Frame
[0];
318 DbgPrint("Frames: ");
320 Frame
= (PULONG
)OldTss
->Ebp
;
321 while (Frame
!= NULL
&& (ULONG
)Frame
>= StackBase
)
323 StackTrace
[i
] = (PVOID
)Frame
[1];
324 Frame
= (PULONG
)Frame
[0];
330 while (i
< TraceLength
)
332 StackRepeatCount
[i
] = 0;
335 while ((j
- i
) <= (TraceLength
- j
) && FoundRepeat
== FALSE
)
337 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
338 (j
- i
) * sizeof(PVOID
)) == 0)
340 StackRepeatCount
[i
] = 2;
341 StackRepeatLength
[i
] = j
- i
;
349 if (FoundRepeat
== FALSE
)
354 j
= j
+ StackRepeatLength
[i
];
355 while ((TraceLength
- j
) >= StackRepeatLength
[i
] &&
358 if (memcmp(&StackTrace
[i
], &StackTrace
[j
],
359 StackRepeatLength
[i
] * sizeof(PVOID
)) == 0)
361 StackRepeatCount
[i
]++;
362 j
= j
+ StackRepeatLength
[i
];
373 while (i
< TraceLength
)
375 if (StackRepeatCount
[i
] == 0)
377 KeRosPrintAddress(StackTrace
[i
]);
383 if (StackRepeatLength
[i
] == 0)
387 for (j
= 0; j
< StackRepeatLength
[i
]; j
++)
389 KeRosPrintAddress(StackTrace
[i
+ j
]);
391 DbgPrint("}*%d", StackRepeatCount
[i
]);
392 i
= i
+ StackRepeatLength
[i
] * StackRepeatCount
[i
];
404 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG Parameter1
, ULONG Parameter2
)
409 ULONG ExceptionNr
= (ULONG
)Tf
->DebugArgMark
;
410 ULONG cr2
= (ULONG
)Tf
->DebugPointer
;
415 * Print out the CPU registers
417 if (ExceptionNr
< ARRAY_SIZE(ExceptionTypeStrings
))
419 DbgPrint("%s Exception: %d(%x)\n", ExceptionTypeStrings
[ExceptionNr
],
420 ExceptionNr
, Tf
->ErrorCode
&0xffff);
424 DbgPrint("Exception: %d(%x)\n", ExceptionNr
, Tf
->ErrorCode
&0xffff);
426 DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(),
427 Tf
->Cs
&0xffff, Tf
->Eip
);
428 KeRosPrintAddress((PVOID
)Tf
->Eip
);
430 Ke386GetPageTableDirectory(cr3_
);
431 DbgPrint("cr2 %x cr3 %x ", cr2
, cr3_
);
432 DbgPrint("Proc: %x ",PsGetCurrentProcess());
433 if (PsGetCurrentProcess() != NULL
)
435 DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId
);
436 DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName
);
438 if (PsGetCurrentThread() != NULL
)
440 DbgPrint("Thrd: %x Tid: %x",
441 PsGetCurrentThread(),
442 PsGetCurrentThread()->Cid
.UniqueThread
);
445 DbgPrint("DS %x ES %x FS %x GS %x\n", Tf
->Ds
&0xffff, Tf
->Es
&0xffff,
446 Tf
->Fs
&0xffff, Tf
->Gs
&0xfff);
447 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", Tf
->Eax
, Tf
->Ebx
, Tf
->Ecx
);
448 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", Tf
->Edx
,
449 Tf
->Ebp
, Tf
->Esi
, Esp0
);
450 DbgPrint("EDI: %.8x EFLAGS: %.8x ", Tf
->Edi
, Tf
->Eflags
);
451 if ((Tf
->Cs
&0xffff) == KERNEL_CS
)
453 DbgPrint("kESP %.8x ", Esp0
);
454 if (PsGetCurrentThread() != NULL
)
456 DbgPrint("kernel stack base %x\n",
457 PsGetCurrentThread()->Tcb
.StackLimit
);
462 if (PsGetCurrentThread() != NULL
)
464 StackLimit
= (ULONG
)PsGetCurrentThread()->Tcb
.StackBase
;
468 StackLimit
= (ULONG
)init_stack_top
;
472 * Dump the stack frames
474 KeDumpStackFrames((PULONG
)Tf
->Ebp
);
478 KiTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
)
480 * FUNCTION: Called by the lowlevel execption handlers to print an amusing
481 * message and halt the computer
483 * Complete CPU context
490 ASSERT(ExceptionNr
!= 14);
492 /* Store the exception number in an unused field in the trap frame. */
493 Tf
->DebugArgMark
= ExceptionNr
;
495 /* Use the address of the trap frame as approximation to the ring0 esp */
496 Esp0
= (ULONG
)&Tf
->Eip
;
500 Tf
->DebugPointer
= cr2
;
503 * If this was a V86 mode exception then handle it specially
505 if (Tf
->Eflags
& (1 << 17))
507 DPRINT("Tf->Eflags, %x, Tf->Eip %x, ExceptionNr: %d\n", Tf
->Eflags
, Tf
->Eip
, ExceptionNr
);
508 return(KeV86Exception(ExceptionNr
, Tf
, cr2
));
512 * Check for stack underflow, this may be obsolete
514 if (PsGetCurrentThread() != NULL
&&
515 Esp0
< (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
)
517 DPRINT1("Stack underflow (tf->esp %x Limit %x Eip %x)\n",
518 Esp0
, (ULONG
)PsGetCurrentThread()->Tcb
.StackLimit
, Tf
->Eip
);
522 if (ExceptionNr
== 15)
526 * This exception should never occur. The P6 has a bug, which does sometimes deliver
527 * the apic spurious interrupt as exception 15. On an athlon64, I get one exception
528 * in the early boot phase in apic mode (using the smp build). I've looked to the linux
529 * sources. Linux does ignore this exception.
533 DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
538 * Check for a breakpoint that was only for the attention of the debugger.
540 if (ExceptionNr
== 3 && Tf
->Eip
== ((ULONG
)DbgBreakPointNoBugCheck
) + 1)
543 EIP is already adjusted by the processor to point to the instruction
544 after the breakpoint.
550 * Try to handle device-not-present, math-fault and xmm-fault exceptions.
552 if (ExceptionNr
== 7 || ExceptionNr
== 16 || ExceptionNr
== 19)
554 Status
= KiHandleFpuFault(Tf
, ExceptionNr
);
555 if (NT_SUCCESS(Status
))
562 * Handle user exceptions differently
564 if ((Tf
->Cs
& 0xFFFF) == USER_CS
)
566 return(KiUserTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
570 return(KiKernelTrapHandler(Tf
, ExceptionNr
, (PVOID
)cr2
));
576 KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame
)
578 /* Check if this is user-mode or V86 */
579 if ((TrapFrame
->Cs
& 1) || (TrapFrame
->Eflags
& X86_EFLAGS_VM
))
581 /* Return it directly */
582 return TrapFrame
->Esp
;
587 if (!(TrapFrame
->Cs
& FRAME_EDITED
))
589 /* Return edited value */
590 return TrapFrame
->TempEsp
;
594 /* Virgin frame, calculate */
595 return (ULONG
)&TrapFrame
->Esp
;
602 KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
605 ULONG Previous
= KiEspFromTrapFrame(TrapFrame
);
607 /* Check if this is user-mode or V86 */
608 if ((TrapFrame
->Cs
& 1) || (TrapFrame
->Eflags
& X86_EFLAGS_VM
))
610 /* Write it directly */
611 TrapFrame
->Esp
= Esp
;
615 /* Don't allow ESP to be lowered, this is illegal */
618 KeBugCheck(SET_OF_INVALID_CONTEXT
);
621 /* Create an edit frame, check if it was alrady */
622 if (!(TrapFrame
->Cs
& FRAME_EDITED
))
624 /* Update the value */
625 TrapFrame
->TempEsp
= Esp
;
629 /* Check if ESP changed */
633 TrapFrame
->TempCs
= TrapFrame
->Cs
;
634 TrapFrame
->Cs
&= ~FRAME_EDITED
;
637 TrapFrame
->TempEsp
= Esp
;
645 KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame
)
647 /* If this was V86 Mode */
648 if (TrapFrame
->Eflags
& X86_EFLAGS_VM
)
651 return TrapFrame
->Ss
;
653 else if (TrapFrame
->Cs
& 1)
655 /* Usermode, return the User SS */
656 return TrapFrame
->Ss
| 3;
667 KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
670 /* Remove the high-bits */
673 /* If this was V86 Mode */
674 if (TrapFrame
->Eflags
& X86_EFLAGS_VM
)
679 else if (TrapFrame
->Cs
& 1)
681 /* Usermode, save the User SS */
682 TrapFrame
->Ss
= Ss
| 3;
688 KeContextToTrapFrame(IN PCONTEXT Context
,
689 IN OUT PKEXCEPTION_FRAME ExceptionFrame
,
690 IN OUT PKTRAP_FRAME TrapFrame
,
691 IN KPROCESSOR_MODE PreviousMode
)
693 BOOLEAN V86Switch
= FALSE
;
695 /* Start with the basic Registers */
696 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
698 /* Check if we went through a V86 switch */
699 if ((Context
->EFlags
& X86_EFLAGS_VM
) !=
700 (TrapFrame
->Eflags
& X86_EFLAGS_VM
))
702 /* We did, remember this for later */
706 /* Copy EFLAGS. FIXME: Needs to be sanitized */
707 TrapFrame
->Eflags
= Context
->EFlags
;
709 /* Copy EBP and EIP */
710 TrapFrame
->Ebp
= Context
->Ebp
;
711 TrapFrame
->Eip
= Context
->Eip
;
713 /* Check if we were in V86 Mode */
714 if (TrapFrame
->Eflags
& X86_EFLAGS_VM
)
716 /* Simply copy the CS value */
717 TrapFrame
->Cs
= Context
->SegCs
;
721 /* We weren't in V86, so sanitize the CS (FIXME!) */
722 TrapFrame
->Cs
= Context
->SegCs
;
724 /* Don't let it under 8, that's invalid */
725 if ((PreviousMode
!=KernelMode
) && (TrapFrame
->Cs
< 8))
727 /* Force it to User CS */
728 TrapFrame
->Cs
= USER_CS
;
732 /* Handle SS Specially for validation */
733 KiSsToTrapFrame(TrapFrame
, Context
->SegSs
);
735 /* Write ESP back; take into account Edited Trap Frames */
736 KiEspToTrapFrame(TrapFrame
, Context
->Esp
);
738 /* Handle our V86 Bias if we went through a switch */
739 if (V86Switch
) Ki386AdjustEsp0(TrapFrame
);
742 /* Process the Integer Registers */
743 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
745 TrapFrame
->Eax
= Context
->Eax
;
746 TrapFrame
->Ebx
= Context
->Ebx
;
747 TrapFrame
->Ecx
= Context
->Ecx
;
748 TrapFrame
->Edx
= Context
->Edx
;
749 TrapFrame
->Esi
= Context
->Esi
;
750 TrapFrame
->Edi
= Context
->Edi
;
753 /* Process the Context Segments */
754 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
756 /* Check if we were in V86 Mode */
757 if (TrapFrame
->Eflags
& X86_EFLAGS_VM
)
759 /* Copy the V86 Segments directlry */
760 TrapFrame
->V86_Ds
= Context
->SegDs
;
761 TrapFrame
->V86_Es
= Context
->SegEs
;
762 TrapFrame
->V86_Fs
= Context
->SegFs
;
763 TrapFrame
->V86_Gs
= Context
->SegGs
;
765 else if (!(TrapFrame
->Cs
& 1))
767 /* For user mode, write the values directly */
768 TrapFrame
->Ds
= USER_DS
;
769 TrapFrame
->Es
= USER_DS
;
770 TrapFrame
->Fs
= Context
->SegFs
;
775 /* For kernel-mode, return the values */
776 TrapFrame
->Ds
= Context
->SegDs
;
777 TrapFrame
->Es
= Context
->SegEs
;
778 TrapFrame
->Fs
= Context
->SegFs
;
780 /* Handle GS specially */
781 if (TrapFrame
->Cs
== USER_CS
)
783 /* Don't use it, if user */
788 /* Copy it if kernel */
789 TrapFrame
->Gs
= Context
->SegGs
;
794 /* Handle the Debug Registers */
795 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
797 /* FIXME: All these should be sanitized */
798 TrapFrame
->Dr0
= Context
->Dr0
;
799 TrapFrame
->Dr1
= Context
->Dr1
;
800 TrapFrame
->Dr2
= Context
->Dr2
;
801 TrapFrame
->Dr3
= Context
->Dr3
;
802 TrapFrame
->Dr6
= Context
->Dr6
;
803 TrapFrame
->Dr7
= Context
->Dr7
;
805 /* Check if usermode */
806 if (PreviousMode
!= KernelMode
)
808 /* Set the Debug Flag */
809 KeGetCurrentThread()->DispatcherHeader
.DebugActive
= (Context
->Dr7
& DR7_ACTIVE
);
813 /* Handle FPU and Extended Registers */
814 return KiContextToFxSaveArea((PFX_SAVE_AREA
)(TrapFrame
+ 1), Context
);
819 KeTrapFrameToContext(IN PKTRAP_FRAME TrapFrame
,
820 IN PKEXCEPTION_FRAME ExceptionFrame
,
821 IN OUT PCONTEXT Context
)
823 PFX_SAVE_AREA FxSaveArea
= NULL
;
825 /* Start with the Control flags */
826 if ((Context
->ContextFlags
& CONTEXT_CONTROL
) == CONTEXT_CONTROL
)
828 /* EBP, EIP and EFLAGS */
829 Context
->Ebp
= TrapFrame
->Ebp
;
830 Context
->Eip
= TrapFrame
->Eip
;
831 Context
->EFlags
= TrapFrame
->Eflags
;
833 /* Return the correct CS */
834 if (!(TrapFrame
->Cs
& FRAME_EDITED
) &&
835 !(TrapFrame
->Eflags
& X86_EFLAGS_VM
))
837 /* Get it from the Temp location */
838 Context
->SegCs
= TrapFrame
->TempCs
& 0xFFFF;
842 /* Return it directly */
843 Context
->SegCs
= TrapFrame
->Cs
& 0xFFFF;
846 /* Get the Ss and ESP */
847 Context
->SegSs
= KiSsFromTrapFrame(TrapFrame
);
848 Context
->Esp
= KiEspFromTrapFrame(TrapFrame
);
851 /* Handle the Segments */
852 if ((Context
->ContextFlags
& CONTEXT_SEGMENTS
) == CONTEXT_SEGMENTS
)
854 /* Do V86 Mode first */
855 if (TrapFrame
->Eflags
& X86_EFLAGS_VM
)
857 /* Return from the V86 location */
858 Context
->SegGs
= TrapFrame
->V86_Gs
& 0xFFFF;
859 Context
->SegFs
= TrapFrame
->V86_Fs
& 0xFFFF;
860 Context
->SegEs
= TrapFrame
->V86_Es
& 0xFFFF;
861 Context
->SegDs
= TrapFrame
->V86_Ds
& 0xFFFF;
865 /* Check if this was a Kernel Trap */
866 if (TrapFrame
->Cs
== KERNEL_CS
)
868 /* Set valid selectors */
870 TrapFrame
->Fs
= PCR_SELECTOR
;
871 TrapFrame
->Es
= USER_DS
;
872 TrapFrame
->Ds
= USER_DS
;
875 /* Return the segments */
876 Context
->SegGs
= TrapFrame
->Gs
& 0xFFFF;
877 Context
->SegFs
= TrapFrame
->Fs
& 0xFFFF;
878 Context
->SegEs
= TrapFrame
->Es
& 0xFFFF;
879 Context
->SegDs
= TrapFrame
->Ds
& 0xFFFF;
883 /* Handle the simple registers */
884 if ((Context
->ContextFlags
& CONTEXT_INTEGER
) == CONTEXT_INTEGER
)
886 /* Return them directly */
887 Context
->Eax
= TrapFrame
->Eax
;
888 Context
->Ebx
= TrapFrame
->Ebx
;
889 Context
->Ecx
= TrapFrame
->Ecx
;
890 Context
->Edx
= TrapFrame
->Edx
;
891 Context
->Esi
= TrapFrame
->Esi
;
892 Context
->Edi
= TrapFrame
->Edi
;
895 if ((Context
->ContextFlags
& CONTEXT_DEBUG_REGISTERS
) == CONTEXT_DEBUG_REGISTERS
)
898 * FIXME: Implement this case
900 Context
->ContextFlags
&= (~CONTEXT_DEBUG_REGISTERS
) | CONTEXT_i386
;
902 if ((Context
->ContextFlags
& CONTEXT_FLOATING_POINT
) == CONTEXT_FLOATING_POINT
)
904 FxSaveArea
= KiGetFpuState(KeGetCurrentThread());
905 if (FxSaveArea
!= NULL
)
907 KiFxSaveAreaToFloatingSaveArea(&Context
->FloatSave
, FxSaveArea
);
911 Context
->ContextFlags
&= (~CONTEXT_FLOATING_POINT
) | CONTEXT_i386
;
914 if ((Context
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) == CONTEXT_EXTENDED_REGISTERS
)
916 if (FxSaveArea
== NULL
)
917 FxSaveArea
= KiGetFpuState(KeGetCurrentThread());
918 if (FxSaveArea
!= NULL
)
920 memcpy(Context
->ExtendedRegisters
, &FxSaveArea
->U
.FxArea
,
921 min(sizeof (Context
->ExtendedRegisters
), sizeof (FxSaveArea
->U
.FxArea
)) );
925 Context
->ContextFlags
&= (~CONTEXT_EXTENDED_REGISTERS
) | CONTEXT_i386
;
932 KeDumpStackFrames(PULONG Frame
)
934 PULONG StackBase
, StackEnd
;
935 MEMORY_BASIC_INFORMATION mbi
;
936 ULONG ResultLength
= sizeof(mbi
);
939 DbgPrint("Frames:\n");
942 Status
= MiQueryVirtualMemory (
945 MemoryBasicInformation
,
949 if ( !NT_SUCCESS(Status
) )
951 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
956 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
958 while ( Frame
>= StackBase
&& Frame
< StackEnd
)
960 ULONG Addr
= Frame
[1];
961 if (!KeRosPrintAddress((PVOID
)Addr
))
962 DbgPrint("<%X>", Addr
);
963 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
966 Frame
= (PULONG
)Frame
[0];
978 KeRosDumpStackFrames ( PULONG Frame
, ULONG FrameCount
)
981 PULONG StackBase
, StackEnd
;
982 MEMORY_BASIC_INFORMATION mbi
;
983 ULONG ResultLength
= sizeof(mbi
);
986 DbgPrint("Frames: ");
992 __asm__("mov %%ebp, %0" : "=r" (Frame
) : );
993 #elif defined(_MSC_VER)
994 __asm mov
[Frame
], ebp
996 //Frame = (PULONG)Frame[0]; // step out of KeRosDumpStackFrames
999 Status
= MiQueryVirtualMemory (
1002 MemoryBasicInformation
,
1006 if ( !NT_SUCCESS(Status
) )
1008 DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
1013 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
1015 while ( Frame
>= StackBase
&& Frame
< StackEnd
&& i
++ < FrameCount
)
1017 ULONG Addr
= Frame
[1];
1018 if (!KeRosPrintAddress((PVOID
)Addr
))
1019 DbgPrint("<%X>", Addr
);
1020 if ( Addr
== 0 || Addr
== 0xDEADBEEF )
1023 Frame
= (PULONG
)Frame
[0];
1035 KeRosGetStackFrames ( PULONG Frames
, ULONG FrameCount
)
1038 PULONG StackBase
, StackEnd
, Frame
;
1039 MEMORY_BASIC_INFORMATION mbi
;
1040 ULONG ResultLength
= sizeof(mbi
);
1045 #if defined __GNUC__
1046 __asm__("mov %%ebp, %0" : "=r" (Frame
) : );
1047 #elif defined(_MSC_VER)
1048 __asm mov
[Frame
], ebp
1051 Status
= MiQueryVirtualMemory (
1054 MemoryBasicInformation
,
1058 if ( !NT_SUCCESS(Status
) )
1060 DPRINT1("Can't get stack frames: MiQueryVirtualMemory() failed: %x\n", Status
);
1065 StackEnd
= (PULONG
)((ULONG_PTR
)mbi
.BaseAddress
+ mbi
.RegionSize
);
1067 while ( Count
< FrameCount
&& Frame
>= StackBase
&& Frame
< StackEnd
)
1069 Frames
[Count
++] = Frame
[1];
1071 Frame
= (PULONG
)Frame
[0];
1082 set_system_call_gate(unsigned int sel
, unsigned int func
)
1084 DPRINT("sel %x %d\n",sel
,sel
);
1085 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
1087 KiIdt
[sel
].b
= 0xef00 + (((int)func
)&0xffff0000);
1088 DPRINT("idt[sel].b %x\n",KiIdt
[sel
].b
);
1091 static void set_interrupt_gate(unsigned int sel
, unsigned int func
)
1093 DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel
,func
);
1094 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
1096 KiIdt
[sel
].b
= 0x8e00 + (((int)func
)&0xffff0000);
1099 static void set_trap_gate(unsigned int sel
, unsigned int func
, unsigned int dpl
)
1101 DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel
, func
, dpl
);
1103 KiIdt
[sel
].a
= (((int)func
)&0xffff) +
1105 KiIdt
[sel
].b
= 0x8f00 + (dpl
<< 13) + (((int)func
)&0xffff0000);
1109 set_task_gate(unsigned int sel
, unsigned task_sel
)
1111 KiIdt
[sel
].a
= task_sel
<< 16;
1112 KiIdt
[sel
].b
= 0x8500;
1118 KeInitExceptions(VOID
)
1120 * FUNCTION: Initalize CPU exception handling
1125 DPRINT("KeInitExceptions()\n");
1128 * Set up the other gates
1130 set_trap_gate(0, (ULONG
)KiTrap0
, 0);
1131 set_trap_gate(1, (ULONG
)KiTrap1
, 0);
1132 set_trap_gate(2, (ULONG
)KiTrap2
, 0);
1133 set_trap_gate(3, (ULONG
)KiTrap3
, 3);
1134 set_trap_gate(4, (ULONG
)KiTrap4
, 0);
1135 set_trap_gate(5, (ULONG
)KiTrap5
, 0);
1136 set_trap_gate(6, (ULONG
)KiTrap6
, 0);
1137 set_trap_gate(7, (ULONG
)KiTrap7
, 0);
1138 set_task_gate(8, TRAP_TSS_SELECTOR
);
1139 set_trap_gate(9, (ULONG
)KiTrap9
, 0);
1140 set_trap_gate(10, (ULONG
)KiTrap10
, 0);
1141 set_trap_gate(11, (ULONG
)KiTrap11
, 0);
1142 set_trap_gate(12, (ULONG
)KiTrap12
, 0);
1143 set_trap_gate(13, (ULONG
)KiTrap13
, 0);
1144 set_interrupt_gate(14, (ULONG
)KiTrap14
);
1145 set_trap_gate(15, (ULONG
)KiTrap15
, 0);
1146 set_trap_gate(16, (ULONG
)KiTrap16
, 0);
1147 set_trap_gate(17, (ULONG
)KiTrap17
, 0);
1148 set_trap_gate(18, (ULONG
)KiTrap18
, 0);
1149 set_trap_gate(19, (ULONG
)KiTrap19
, 0);
1151 for (i
= 20; i
< 256; i
++)
1153 set_trap_gate(i
,(int)KiTrapUnknown
, 0);
1156 set_system_call_gate(0x2d,(int)KiDebugService
);
1157 set_system_call_gate(0x2e,(int)KiSystemService
);
1162 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord
,
1163 PKEXCEPTION_FRAME ExceptionFrame
,
1164 PKTRAP_FRAME TrapFrame
,
1165 KPROCESSOR_MODE PreviousMode
,
1166 BOOLEAN FirstChance
)
1169 KD_CONTINUE_TYPE Action
;
1170 ULONG_PTR Stack
, NewStack
;
1172 BOOLEAN UserDispatch
= FALSE
;
1173 DPRINT("KiDispatchException() called\n");
1175 /* Increase number of Exception Dispatches */
1176 KeGetCurrentPrcb()->KeExceptionDispatchCount
++;
1178 /* Set the context flags */
1179 Context
.ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1181 /* Check if User Mode */
1182 if (PreviousMode
== UserMode
)
1184 extern ULONG FxsrSupport
;
1185 /* Add the FPU Flag */
1186 Context
.ContextFlags
|= CONTEXT_FLOATING_POINT
;
1188 Context
.ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1192 KeTrapFrameToContext(TrapFrame
, ExceptionFrame
, &Context
);
1194 /* Handle kernel-mode first, it's simpler */
1195 if (PreviousMode
== KernelMode
)
1197 /* Check if this is a first-chance exception */
1198 if (FirstChance
== TRUE
)
1200 /* Break into the debugger for the first time */
1201 Action
= KdpEnterDebuggerException(ExceptionRecord
,
1208 /* If the debugger said continue, then continue */
1209 if (Action
== kdContinue
) goto Handled
;
1211 /* If the Debugger couldn't handle it, dispatch the exception */
1212 if (RtlDispatchException(ExceptionRecord
, &Context
))
1214 /* It was handled by an exception handler, continue */
1219 /* This is a second-chance exception, only for the debugger */
1220 Action
= KdpEnterDebuggerException(ExceptionRecord
,
1227 /* If the debugger said continue, then continue */
1228 if (Action
== kdContinue
) goto Handled
;
1230 /* Third strike; you're out */
1231 KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED
,
1232 ExceptionRecord
->ExceptionCode
,
1233 (ULONG_PTR
)ExceptionRecord
->ExceptionAddress
,
1234 ExceptionRecord
->ExceptionInformation
[0],
1235 ExceptionRecord
->ExceptionInformation
[1],
1240 /* User mode exception, was it first-chance? */
1243 /* Enter Debugger if available */
1244 Action
= KdpEnterDebuggerException(ExceptionRecord
,
1251 /* Exit if we're continuing */
1252 if (Action
== kdContinue
) goto Handled
;
1254 /* FIXME: Forward exception to user mode debugger */
1256 /* Set up the user-stack */
1259 /* Align context size and get stack pointer */
1260 Size
= (sizeof(CONTEXT
) + 3) & ~3;
1261 Stack
= (Context
.Esp
& ~3) - Size
;
1262 DPRINT("Stack: %lx\n", Stack
);
1264 /* Probe stack and copy Context */
1265 ProbeForWrite((PVOID
)Stack
, Size
, sizeof(ULONG
));
1266 RtlCopyMemory((PVOID
)Stack
, &Context
, sizeof(CONTEXT
));
1268 /* Align exception record size and get stack pointer */
1269 Size
= (sizeof(EXCEPTION_RECORD
) -
1270 (EXCEPTION_MAXIMUM_PARAMETERS
- ExceptionRecord
->NumberParameters
) *
1271 sizeof(ULONG
) + 3) & ~3;
1272 NewStack
= Stack
- Size
;
1273 DPRINT("NewStack: %lx\n", NewStack
);
1275 /* Probe stack and copy exception record. Don't forget to add the two params */
1276 ProbeForWrite((PVOID
)(NewStack
- 2 * sizeof(ULONG_PTR
)),
1277 Size
+ 2 * sizeof(ULONG_PTR
),
1279 RtlCopyMemory((PVOID
)NewStack
, ExceptionRecord
, Size
);
1281 /* Now write the two params for the user-mode dispatcher */
1282 *(PULONG_PTR
)(NewStack
- 1 * sizeof(ULONG_PTR
)) = Stack
;
1283 *(PULONG_PTR
)(NewStack
- 2 * sizeof(ULONG_PTR
)) = NewStack
;
1285 /* Set new Stack Pointer */
1286 KiEspToTrapFrame(TrapFrame
, NewStack
- 2 * sizeof(ULONG_PTR
));
1288 /* Set EIP to the User-mode Dispathcer */
1289 TrapFrame
->Eip
= (ULONG
)KeUserExceptionDispatcher
;
1290 UserDispatch
= TRUE
;
1295 /* Do second-chance */
1300 /* If we dispatch to user, return now */
1301 if (UserDispatch
) return;
1303 /* FIXME: Forward the exception to the debugger for 2nd chance */
1305 /* 3rd strike, kill the thread */
1306 DPRINT1("Unhandled UserMode exception, terminating thread\n");
1307 ZwTerminateThread(NtCurrentThread(), ExceptionRecord
->ExceptionCode
);
1308 KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED
,
1309 ExceptionRecord
->ExceptionCode
,
1310 (ULONG_PTR
)ExceptionRecord
->ExceptionAddress
,
1311 ExceptionRecord
->ExceptionInformation
[0],
1312 ExceptionRecord
->ExceptionInformation
[1],
1317 /* Convert the context back into Trap/Exception Frames */
1318 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, PreviousMode
);
1326 KeRaiseUserException(IN NTSTATUS ExceptionCode
)
1329 PKTHREAD Thread
= KeGetCurrentThread();
1332 Thread
->Teb
->ExceptionCode
= ExceptionCode
;
1334 return(ExceptionCode
);
1337 OldEip
= Thread
->TrapFrame
->Eip
;
1338 Thread
->TrapFrame
->Eip
= (ULONG_PTR
)KeRaiseUserExceptionDispatcher
;
1339 return((NTSTATUS
)OldEip
);