2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/ke/i386/traphdlr.c
5 * PURPOSE: Kernel Trap Handlers
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 UCHAR KiTrapPrefixTable
[] =
20 0xF3, /* REP INS/OUTS */
32 UCHAR KiTrapIoTable
[] =
48 PFAST_SYSTEM_CALL_EXIT KiFastCallExitHandler
;
51 /* TRAP EXIT CODE *************************************************************/
55 KiVdmTrap(IN PKTRAP_FRAME TrapFrame
)
57 /* Either the V8086 flag is on, or this is user-mode with a VDM */
58 return ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) ||
59 ((KiUserTrap(TrapFrame
)) && (PsGetCurrentProcess()->VdmObjects
)));
64 KiV86Trap(IN PKTRAP_FRAME TrapFrame
)
66 /* Check if the V8086 flag is on */
67 return ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) != 0);
72 KiIsFrameEdited(IN PKTRAP_FRAME TrapFrame
)
74 /* An edited frame changes esp. It is marked by clearing the bits
75 defined by FRAME_EDITED in the SegCs field of the trap frame */
76 return ((TrapFrame
->SegCs
& FRAME_EDITED
) == 0);
81 KiCommonExit(IN PKTRAP_FRAME TrapFrame
, const ULONG Flags
)
83 /* Disable interrupts until we return */
86 /* Check for APC delivery */
87 KiCheckForApcDelivery(TrapFrame
);
89 /* Debugging checks */
90 KiExitTrapDebugChecks(TrapFrame
, Flags
);
92 /* Restore the SEH handler chain */
93 KeGetPcr()->NtTib
.ExceptionList
= TrapFrame
->ExceptionList
;
95 /* Check if there are active debug registers */
96 if (__builtin_expect(TrapFrame
->Dr7
& ~DR7_RESERVED_MASK
, 0))
99 DbgPrint("Need Hardware Breakpoint Support!\n");
108 KiEoiHelper(IN PKTRAP_FRAME TrapFrame
)
110 /* Common trap exit code */
111 KiCommonExit(TrapFrame
, 0);
113 /* Check if this was a V8086 trap */
114 if (TrapFrame
->EFlags
& EFLAGS_V86_MASK
) KiTrapReturnNoSegments(TrapFrame
);
116 /* Check for user mode exit */
117 if (TrapFrame
->SegCs
& MODE_MASK
) KiTrapReturn(TrapFrame
);
119 /* Check for edited frame */
120 if (KiIsFrameEdited(TrapFrame
)) KiEditedTrapReturn(TrapFrame
);
122 /* Exit the trap to kernel mode */
123 KiTrapReturnNoSegments(TrapFrame
);
129 KiServiceExit(IN PKTRAP_FRAME TrapFrame
,
132 ASSERT((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) == 0);
133 ASSERT(!KiIsFrameEdited(TrapFrame
));
135 /* Copy the status into EAX */
136 TrapFrame
->Eax
= Status
;
138 /* Common trap exit code */
139 KiCommonExit(TrapFrame
, 0);
141 /* Restore previous mode */
142 KeGetCurrentThread()->PreviousMode
= TrapFrame
->PreviousPreviousMode
;
144 /* Check for user mode exit */
145 if (TrapFrame
->SegCs
& MODE_MASK
)
147 /* Check if we were single stepping */
148 if (TrapFrame
->EFlags
& EFLAGS_TF
)
150 /* Must use the IRET handler */
151 KiSystemCallTrapReturn(TrapFrame
);
155 /* We can use the sysexit handler */
156 KiFastCallExitHandler(TrapFrame
);
160 /* Exit to kernel mode */
161 KiSystemCallReturn(TrapFrame
);
167 KiServiceExit2(IN PKTRAP_FRAME TrapFrame
)
169 /* Common trap exit code */
170 KiCommonExit(TrapFrame
, 0);
172 /* Restore previous mode */
173 KeGetCurrentThread()->PreviousMode
= TrapFrame
->PreviousPreviousMode
;
175 /* Check if this was a V8086 trap */
176 if (TrapFrame
->EFlags
& EFLAGS_V86_MASK
) KiTrapReturnNoSegments(TrapFrame
);
178 /* Check for user mode exit */
179 if (TrapFrame
->SegCs
& MODE_MASK
) KiTrapReturn(TrapFrame
);
181 /* Check for edited frame */
182 if (KiIsFrameEdited(TrapFrame
)) KiEditedTrapReturn(TrapFrame
);
184 /* Exit the trap to kernel mode */
185 KiTrapReturnNoSegments(TrapFrame
);
189 /* TRAP HANDLERS **************************************************************/
194 KiDebugHandler(IN PKTRAP_FRAME TrapFrame
,
199 /* Check for VDM trap */
200 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
202 /* Enable interrupts if the trap came from user-mode */
203 if (KiUserTrap(TrapFrame
)) _enable();
205 /* Dispatch the exception */
206 KiDispatchExceptionFromTrapFrame(STATUS_BREAKPOINT
,
218 KiNpxHandler(IN PKTRAP_FRAME TrapFrame
,
220 IN PFX_SAVE_AREA SaveArea
)
222 ULONG Cr0
, Mask
, Error
, ErrorOffset
, DataOffset
;
224 /* Check for VDM trap */
225 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
227 /* Check for kernel trap */
228 if (!KiUserTrap(TrapFrame
))
230 /* Kernel might've tripped a delayed error */
231 SaveArea
->Cr0NpxState
|= CR0_TS
;
233 /* Only valid if it happened during a restore */
234 //if ((PVOID)TrapFrame->Eip == FrRestore)
236 /* It did, so just skip the instruction */
237 //TrapFrame->Eip += 3; /* sizeof(FRSTOR) */
238 //KiEoiHelper(TrapFrame);
242 /* User or kernel trap -- get ready to issue an exception */
243 //if (Thread->NpxState == NPX_STATE_NOT_LOADED)
247 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
251 Ke386SaveFpuState(SaveArea
);
253 /* Mark CR0 state dirty */
254 Cr0
|= NPX_STATE_NOT_LOADED
;
255 Cr0
|= SaveArea
->Cr0NpxState
;
258 /* Update NPX state */
259 Thread
->NpxState
= NPX_STATE_NOT_LOADED
;
260 KeGetCurrentPrcb()->NpxThread
= NULL
;
263 /* Clear the TS bit and re-enable interrupts */
264 SaveArea
->Cr0NpxState
&= ~CR0_TS
;
267 /* Check if we should get the FN or FX error */
268 if (KeI386FxsrPresent
)
271 Mask
= SaveArea
->U
.FxArea
.ControlWord
;
272 Error
= SaveArea
->U
.FxArea
.StatusWord
;
274 /* Get the FPU exception address too */
275 ErrorOffset
= SaveArea
->U
.FxArea
.ErrorOffset
;
276 DataOffset
= SaveArea
->U
.FxArea
.DataOffset
;
281 Mask
= SaveArea
->U
.FnArea
.ControlWord
;
282 Error
= SaveArea
->U
.FnArea
.StatusWord
;
284 /* Get the FPU exception address too */
285 ErrorOffset
= SaveArea
->U
.FnArea
.ErrorOffset
;
286 DataOffset
= SaveArea
->U
.FnArea
.DataOffset
;
289 /* Get legal exceptions that software should handle */
290 Error
&= (FSW_INVALID_OPERATION
|
298 if (Error
& FSW_STACK_FAULT
)
300 /* Issue stack check fault */
301 KiDispatchException2Args(STATUS_FLOAT_STACK_CHECK
,
308 /* Check for invalid operation */
309 if (Error
& FSW_INVALID_OPERATION
)
312 KiDispatchException1Args(STATUS_FLOAT_INVALID_OPERATION
,
318 /* Check for divide by zero */
319 if (Error
& FSW_ZERO_DIVIDE
)
322 KiDispatchException1Args(STATUS_FLOAT_DIVIDE_BY_ZERO
,
328 /* Check for denormal */
329 if (Error
& FSW_DENORMAL
)
332 KiDispatchException1Args(STATUS_FLOAT_INVALID_OPERATION
,
338 /* Check for overflow */
339 if (Error
& FSW_OVERFLOW
)
342 KiDispatchException1Args(STATUS_FLOAT_OVERFLOW
,
348 /* Check for underflow */
349 if (Error
& FSW_UNDERFLOW
)
352 KiDispatchException1Args(STATUS_FLOAT_UNDERFLOW
,
358 /* Check for precision fault */
359 if (Error
& FSW_PRECISION
)
362 KiDispatchException1Args(STATUS_FLOAT_INEXACT_RESULT
,
368 /* Unknown FPU fault */
369 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 1, Error
, 0, 0, TrapFrame
);
375 KiTrap00Handler(IN PKTRAP_FRAME TrapFrame
)
377 /* Save trap frame */
378 KiEnterTrap(TrapFrame
);
380 /* Check for VDM trap */
381 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
383 /* Enable interrupts */
386 /* Dispatch the exception */
387 KiDispatchException0Args(STATUS_INTEGER_DIVIDE_BY_ZERO
,
395 KiTrap01Handler(IN PKTRAP_FRAME TrapFrame
)
397 /* Save trap frame */
398 KiEnterTrap(TrapFrame
);
400 /* Check for VDM trap */
401 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
403 /* Enable interrupts if the trap came from user-mode */
404 if (KiUserTrap(TrapFrame
)) _enable();
406 /* Mask out trap flag and dispatch the exception */
407 TrapFrame
->EFlags
&= ~EFLAGS_TF
;
408 KiDispatchException0Args(STATUS_SINGLE_STEP
,
422 KTRAP_FRAME TrapFrame
;
426 // In some sort of strange recursion case, we might end up here with the IF
427 // flag incorrectly on the interrupt frame -- during a normal NMI this would
428 // normally already be set.
430 // For sanity's sake, make sure interrupts are disabled for sure.
431 // NMIs will already be since the CPU does it for us.
436 // Get the current TSS, thread, and process
439 Thread
= ((PKIPCR
)PCR
)->PrcbData
.CurrentThread
;
440 Process
= Thread
->ApcState
.Process
;
443 // Save data usually not in the TSS
445 Tss
->CR3
= Process
->DirectoryTableBase
[0];
446 Tss
->IoMapBase
= Process
->IopmOffset
;
447 Tss
->LDT
= Process
->LdtDescriptor
.LimitLow
? KGDT_LDT
: 0;
450 // Now get the base address of the NMI TSS
452 TssGdt
= &((PKIPCR
)KeGetPcr())->GDT
[KGDT_NMI_TSS
/ sizeof(KGDTENTRY
)];
453 NmiTss
= (PKTSS
)(ULONG_PTR
)(TssGdt
->BaseLow
|
454 TssGdt
->HighWord
.Bytes
.BaseMid
<< 16 |
455 TssGdt
->HighWord
.Bytes
.BaseHi
<< 24);
458 // Switch to it and activate it, masking off the nested flag
460 // Note that in reality, we are already on the NMI tss -- we just need to
461 // update the PCR to reflect this
464 __writeeflags(__readeflags() &~ EFLAGS_NESTED_TASK
);
465 TssGdt
->HighWord
.Bits
.Dpl
= 0;
466 TssGdt
->HighWord
.Bits
.Pres
= 1;
467 TssGdt
->HighWord
.Bits
.Type
= I386_TSS
;
470 // Now build the trap frame based on the original TSS
472 // The CPU does a hardware "Context switch" / task switch of sorts and so it
473 // takes care of saving our context in the normal TSS.
475 // We just have to go get the values...
477 RtlZeroMemory(&TrapFrame
, sizeof(KTRAP_FRAME
));
478 TrapFrame
.HardwareSegSs
= Tss
->Ss0
;
479 TrapFrame
.HardwareEsp
= Tss
->Esp0
;
480 TrapFrame
.EFlags
= Tss
->EFlags
;
481 TrapFrame
.SegCs
= Tss
->Cs
;
482 TrapFrame
.Eip
= Tss
->Eip
;
483 TrapFrame
.Ebp
= Tss
->Ebp
;
484 TrapFrame
.Ebx
= Tss
->Ebx
;
485 TrapFrame
.Esi
= Tss
->Esi
;
486 TrapFrame
.Edi
= Tss
->Edi
;
487 TrapFrame
.SegFs
= Tss
->Fs
;
488 TrapFrame
.ExceptionList
= PCR
->NtTib
.ExceptionList
;
489 TrapFrame
.PreviousPreviousMode
= -1;
490 TrapFrame
.Eax
= Tss
->Eax
;
491 TrapFrame
.Ecx
= Tss
->Ecx
;
492 TrapFrame
.Edx
= Tss
->Edx
;
493 TrapFrame
.SegDs
= Tss
->Ds
;
494 TrapFrame
.SegEs
= Tss
->Es
;
495 TrapFrame
.SegGs
= Tss
->Gs
;
496 TrapFrame
.DbgEip
= Tss
->Eip
;
497 TrapFrame
.DbgEbp
= Tss
->Ebp
;
500 // Store the trap frame in the KPRCB
502 KiSaveProcessorState(&TrapFrame
, NULL
);
505 // Call any registered NMI handlers and see if they handled it or not
510 // They did not, so call the platform HAL routine to bugcheck the system
512 // Make sure the HAL believes it's running at HIGH IRQL... we can't use
513 // the normal APIs here as playing with the IRQL could change the system
517 PCR
->Irql
= HIGH_LEVEL
;
523 // Although the CPU disabled NMIs, we just did a BIOS Call, which could've
524 // totally changed things.
526 // We have to make sure we're still in our original NMI -- a nested NMI
527 // will point back to the NMI TSS, and in that case we're hosed.
529 if (PCR
->TSS
->Backlink
!= KGDT_NMI_TSS
)
532 // Restore original TSS
537 // Set it back to busy
539 TssGdt
->HighWord
.Bits
.Dpl
= 0;
540 TssGdt
->HighWord
.Bits
.Pres
= 1;
541 TssGdt
->HighWord
.Bits
.Type
= I386_ACTIVE_TSS
;
544 // Restore nested flag
546 __writeeflags(__readeflags() | EFLAGS_NESTED_TASK
);
549 // Handled, return from interrupt
555 // Unhandled: crash the system
557 KiSystemFatalException(EXCEPTION_NMI
, NULL
);
563 KiTrap03Handler(IN PKTRAP_FRAME TrapFrame
)
565 /* Save trap frame */
566 KiEnterTrap(TrapFrame
);
568 /* Continue with the common handler */
569 KiDebugHandler(TrapFrame
, BREAKPOINT_BREAK
, 0, 0);
575 KiTrap04Handler(IN PKTRAP_FRAME TrapFrame
)
577 /* Save trap frame */
578 KiEnterTrap(TrapFrame
);
580 /* Check for VDM trap */
581 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
583 /* Enable interrupts */
586 /* Dispatch the exception */
587 KiDispatchException0Args(STATUS_INTEGER_OVERFLOW
,
595 KiTrap05Handler(IN PKTRAP_FRAME TrapFrame
)
597 /* Save trap frame */
598 KiEnterTrap(TrapFrame
);
600 /* Check for VDM trap */
601 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
603 /* Check for kernel-mode fault */
604 if (!KiUserTrap(TrapFrame
)) KiSystemFatalException(EXCEPTION_BOUND_CHECK
, TrapFrame
);
606 /* Enable interrupts */
609 /* Dispatch the exception */
610 KiDispatchException0Args(STATUS_ARRAY_BOUNDS_EXCEEDED
,
618 KiTrap06Handler(IN PKTRAP_FRAME TrapFrame
)
624 /* Check for V86 GPF */
625 if (__builtin_expect(KiV86Trap(TrapFrame
), 1))
628 KiEnterV86Trap(TrapFrame
);
630 /* Must be a VDM process */
631 if (__builtin_expect(!PsGetCurrentProcess()->VdmObjects
, 0))
633 /* Enable interrupts */
636 /* Setup illegal instruction fault */
637 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
642 /* Go to APC level */
643 OldIrql
= KfRaiseIrql(APC_LEVEL
);
647 if (!VdmDispatchBop(TrapFrame
))
649 /* Should only happen in VDM mode */
654 /* Bring IRQL back */
655 KfLowerIrql(OldIrql
);
658 /* Do a quick V86 exit if possible */
659 KiExitV86Trap(TrapFrame
);
662 /* Save trap frame */
663 KiEnterTrap(TrapFrame
);
665 /* Enable interrupts */
666 Instruction
= (PUCHAR
)TrapFrame
->Eip
;
669 /* Check for user trap */
670 if (KiUserTrap(TrapFrame
))
674 /* Scan next 4 opcodes */
675 for (i
= 0; i
< 4; i
++)
677 /* Check for LOCK instruction */
678 if (Instruction
[i
] == 0xF0)
680 /* Send invalid lock sequence exception */
681 KiDispatchException0Args(STATUS_INVALID_LOCK_SEQUENCE
,
687 /* FIXME: SEH ends here */
690 /* Kernel-mode or user-mode fault (but not LOCK) */
691 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
700 KiTrap07Handler(IN PKTRAP_FRAME TrapFrame
)
702 PKTHREAD Thread
, NpxThread
;
703 PFX_SAVE_AREA SaveArea
, NpxSaveArea
;
706 /* Save trap frame */
707 KiEnterTrap(TrapFrame
);
709 /* Try to handle NPX delay load */
712 /* Get the current thread */
713 Thread
= KeGetCurrentThread();
715 /* Get the NPX frame */
716 SaveArea
= KiGetThreadNpxArea(Thread
);
718 /* Check if emulation is enabled */
719 if (SaveArea
->Cr0NpxState
& CR0_EM
)
721 /* Not implemented */
726 /* Save CR0 and check NPX state */
728 if (Thread
->NpxState
!= NPX_STATE_LOADED
)
731 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
734 /* Get the NPX thread */
735 NpxThread
= KeGetCurrentPrcb()->NpxThread
;
738 /* Get the NPX frame */
739 NpxSaveArea
= KiGetThreadNpxArea(NpxThread
);
742 DPRINT("FIXME: Save FPU state: %p\n", NpxSaveArea
);
743 //Ke386SaveFpuState(NpxSaveArea);
745 /* Update NPX state */
746 Thread
->NpxState
= NPX_STATE_NOT_LOADED
;
750 //Ke386LoadFpuState(SaveArea);
752 /* Update NPX state */
753 Thread
->NpxState
= NPX_STATE_LOADED
;
754 KeGetCurrentPrcb()->NpxThread
= Thread
;
756 /* Enable interrupts */
759 /* Check if CR0 needs to be reloaded due to context switch */
760 if (!SaveArea
->Cr0NpxState
) KiEoiHelper(TrapFrame
);
762 /* Otherwise, we need to reload CR0, disable interrupts */
767 Cr0
|= SaveArea
->Cr0NpxState
;
770 /* Now restore interrupts and check for TS */
772 if (Cr0
& CR0_TS
) KiEoiHelper(TrapFrame
);
774 /* We're still here -- clear TS and try again */
775 __writecr0(__readcr0() &~ CR0_TS
);
780 /* This is an actual fault, not a lack of FPU state */
785 /* TS should not be set */
789 * If it's incorrectly set, then maybe the state is actually still valid
790 * but we could've lock track of that due to a BIOS call.
791 * Make sure MP is still set, which should verify the theory.
795 /* Indeed, the state is actually still valid, so clear TS */
796 __writecr0(__readcr0() &~ CR0_TS
);
797 KiEoiHelper(TrapFrame
);
800 /* Otherwise, something strange is going on */
801 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 2, Cr0
, 0, 0, TrapFrame
);
804 /* It's not a delayed load, so process this trap as an NPX fault */
805 KiNpxHandler(TrapFrame
, Thread
, SaveArea
);
811 KiTrap08Handler(IN PKTRAP_FRAME TrapFrame
)
813 /* FIXME: Not handled */
814 KiSystemFatalException(EXCEPTION_DOUBLE_FAULT
, TrapFrame
);
820 KiTrap09Handler(IN PKTRAP_FRAME TrapFrame
)
822 /* Save trap frame */
823 KiEnterTrap(TrapFrame
);
825 /* Enable interrupts and kill the system */
827 KiSystemFatalException(EXCEPTION_NPX_OVERRUN
, TrapFrame
);
833 KiTrap0AHandler(IN PKTRAP_FRAME TrapFrame
)
835 /* Save trap frame */
836 KiEnterTrap(TrapFrame
);
838 /* Check for VDM trap */
839 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
841 /* Kill the system */
842 KiSystemFatalException(EXCEPTION_INVALID_TSS
, TrapFrame
);
848 KiTrap0BHandler(IN PKTRAP_FRAME TrapFrame
)
850 /* Save trap frame */
851 KiEnterTrap(TrapFrame
);
853 /* FIXME: Kill the system */
855 KiSystemFatalException(EXCEPTION_SEGMENT_NOT_PRESENT
, TrapFrame
);
861 KiTrap0CHandler(IN PKTRAP_FRAME TrapFrame
)
863 /* Save trap frame */
864 KiEnterTrap(TrapFrame
);
866 /* FIXME: Kill the system */
868 KiSystemFatalException(EXCEPTION_STACK_FAULT
, TrapFrame
);
874 KiTrap0DHandler(IN PKTRAP_FRAME TrapFrame
)
877 BOOLEAN Privileged
= FALSE
;
879 UCHAR Instruction
= 0;
882 /* Check for V86 GPF */
883 if (__builtin_expect(KiV86Trap(TrapFrame
), 1))
886 KiEnterV86Trap(TrapFrame
);
888 /* Must be a VDM process */
889 if (__builtin_expect(!PsGetCurrentProcess()->VdmObjects
, 0))
891 /* Enable interrupts */
894 /* Setup illegal instruction fault */
895 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
900 /* Go to APC level */
901 OldIrql
= KfRaiseIrql(APC_LEVEL
);
904 /* Handle the V86 opcode */
905 if (__builtin_expect(Ki386HandleOpcodeV86(TrapFrame
) == 0xFF, 0))
907 /* Should only happen in VDM mode */
912 /* Bring IRQL back */
913 KfLowerIrql(OldIrql
);
916 /* Do a quick V86 exit if possible */
917 KiExitV86Trap(TrapFrame
);
920 /* Save trap frame */
921 KiEnterTrap(TrapFrame
);
923 /* Check for user-mode GPF */
924 if (KiUserTrap(TrapFrame
))
926 /* Should not be VDM */
927 ASSERT(KiVdmTrap(TrapFrame
) == FALSE
);
929 /* Enable interrupts and check error code */
931 if (!TrapFrame
->ErrCode
)
934 Instructions
= (PUCHAR
)TrapFrame
->Eip
;
936 /* Scan next 15 bytes */
937 for (i
= 0; i
< 15; i
++)
939 /* Skip prefix instructions */
940 for (j
= 0; j
< sizeof(KiTrapPrefixTable
); j
++)
942 /* Is this a prefix instruction? */
943 if (Instructions
[i
] == KiTrapPrefixTable
[j
])
950 /* Is this NOT any prefix instruction? */
951 if (j
== sizeof(KiTrapPrefixTable
))
953 /* We can go ahead and handle the fault now */
954 Instruction
= Instructions
[i
];
959 /* If all we found was prefixes, then this instruction is too long */
962 /* Setup illegal instruction fault */
963 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
968 /* Check for privileged instructions */
969 if (Instruction
== 0xF4) // HLT
971 /* HLT is privileged */
974 else if (Instruction
== 0x0F)
976 /* Test if it's any of the privileged two-byte opcodes */
977 if (((Instructions
[i
+ 1] == 0x00) && // LLDT or LTR
978 (((Instructions
[i
+ 2] & 0x38) == 0x10) || // LLDT
979 (Instructions
[i
+ 2] == 0x18))) || // LTR
980 ((Instructions
[i
+ 1] == 0x01) && // LGDT or LIDT or LMSW
981 (((Instructions
[i
+ 2] & 0x38) == 0x10) || // LGDT
982 (Instructions
[i
+ 2] == 0x18) || // LIDT
983 (Instructions
[i
+ 2] == 0x30))) || // LMSW
984 (Instructions
[i
+ 1] == 0x08) || // INVD
985 (Instructions
[i
+ 1] == 0x09) || // WBINVD
986 (Instructions
[i
+ 1] == 0x35) || // SYSEXIT
987 (Instructions
[i
+ 1] == 0x26) || // MOV DR, XXX
988 (Instructions
[i
+ 1] == 0x06) || // CLTS
989 (Instructions
[i
+ 1] == 0x20) || // MOV CR, XXX
990 (Instructions
[i
+ 1] == 0x24) || // MOV YYY, DR
991 (Instructions
[i
+ 1] == 0x30) || // WRMSR
992 (Instructions
[i
+ 1] == 0x33)) // RDPMC
993 // INVLPG, INVLPGA, SYSRET
995 /* These are all privileged */
1001 /* Get the IOPL and compare with the RPL mask */
1002 Iopl
= (TrapFrame
->EFlags
& EFLAGS_IOPL
) >> 12;
1003 if ((TrapFrame
->SegCs
& RPL_MASK
) > Iopl
)
1005 /* I/O privilege error -- check for known instructions */
1006 if ((Instruction
== 0xFA) || (Instruction
== 0xFB)) // CLI or STI
1008 /* These are privileged */
1013 /* Last hope: an IN/OUT instruction */
1014 for (j
= 0; j
< sizeof(KiTrapIoTable
); j
++)
1016 /* Is this an I/O instruction? */
1017 if (Instruction
== KiTrapIoTable
[j
])
1019 /* Then it's privileged */
1028 /* So now... was the instruction privileged or not? */
1031 /* Whew! We have a privileged instruction, so dispatch the fault */
1032 KiDispatchException0Args(STATUS_PRIVILEGED_INSTRUCTION
,
1038 /* If we got here, send an access violation */
1039 KiDispatchException2Args(STATUS_ACCESS_VIOLATION
,
1047 * Check for a fault during checking of the user instruction.
1049 * Note that the SEH handler will catch invalid EIP, but we could be dealing
1050 * with an invalid CS, which will generate another GPF instead.
1053 if (((PVOID
)TrapFrame
->Eip
>= (PVOID
)KiTrap0DHandler
) &&
1054 ((PVOID
)TrapFrame
->Eip
< (PVOID
)KiTrap0DHandler
))
1056 /* Not implemented */
1062 * NOTE: The ASM trap exit code would restore segment registers by doing
1063 * a POP <SEG>, which could cause an invalid segment if someone had messed
1064 * with the segment values.
1066 * Another case is a bogus SS, which would hit a GPF when doing the iret.
1067 * This could only be done through a buggy or malicious driver, or perhaps
1068 * the kernel debugger.
1070 * The kernel normally restores the "true" segment if this happens.
1072 * However, since we're restoring in C, not ASM, we can't detect
1073 * POP <SEG> since the actual instructions will be different.
1075 * A better technique would be to check the EIP and somehow edit the
1076 * trap frame before restarting the instruction -- but we would need to
1077 * know the extract instruction that was used first.
1079 * We could force a special instrinsic to use stack instructions, or write
1080 * a simple instruction length checker.
1082 * Nevertheless, this is a lot of work for the purpose of avoiding a crash
1083 * when the user is purposedly trying to create one from kernel-mode, so
1084 * we should probably table this for now since it's not a "real" issue.
1088 * NOTE2: Another scenario is the IRET during a V8086 restore (BIOS Call)
1089 * which will cause a GPF since the trap frame is a total mess (on purpose)
1090 * as built in KiEnterV86Mode.
1092 * The idea is to scan for IRET, scan for the known EIP adress, validate CS
1093 * and then manually issue a jump to the V8086 return EIP.
1095 Instructions
= (PUCHAR
)TrapFrame
->Eip
;
1096 if (Instructions
[0] == 0xCF)
1099 * Some evil shit is going on here -- this is not the SS:ESP you're
1100 * looking for! Instead, this is actually CS:EIP you're looking at!
1101 * Why? Because part of the trap frame actually corresponds to the IRET
1102 * stack during the trap exit!
1104 if ((TrapFrame
->HardwareEsp
== (ULONG
)Ki386BiosCallReturnAddress
) &&
1105 (TrapFrame
->HardwareSegSs
== (KGDT_R0_CODE
| RPL_MASK
)))
1107 /* Exit the V86 trap! */
1108 Ki386BiosCallReturnAddress(TrapFrame
);
1112 /* Otherwise, this is another kind of IRET fault */
1118 /* So since we're not dealing with the above case, check for RDMSR/WRMSR */
1119 if ((Instructions
[0] == 0xF) && // 2-byte opcode
1120 (((Instructions
[1] >> 8) == 0x30) || // RDMSR
1121 ((Instructions
[2] >> 8) == 0x32))) // WRMSR
1123 /* Unknown CPU MSR, so raise an access violation */
1124 KiDispatchException0Args(STATUS_ACCESS_VIOLATION
,
1129 /* Check for lazy segment load */
1130 if (TrapFrame
->SegDs
!= (KGDT_R3_DATA
| RPL_MASK
))
1133 TrapFrame
->SegDs
= (KGDT_R3_DATA
| RPL_MASK
);
1135 else if (TrapFrame
->SegEs
!= (KGDT_R3_DATA
| RPL_MASK
))
1138 TrapFrame
->SegEs
= (KGDT_R3_DATA
| RPL_MASK
);
1142 /* Whatever it is, we can't handle it */
1143 KiSystemFatalException(EXCEPTION_GP_FAULT
, TrapFrame
);
1146 /* Return to where we came from */
1147 KiTrapReturn(TrapFrame
);
1153 KiTrap0EHandler(IN PKTRAP_FRAME TrapFrame
)
1159 /* Save trap frame */
1160 KiEnterTrap(TrapFrame
);
1162 /* Check if this is the base frame */
1163 Thread
= KeGetCurrentThread();
1164 if (KeGetTrapFrame(Thread
) != TrapFrame
)
1166 /* It isn't, check if this is a second nested frame */
1167 if (((ULONG_PTR
)KeGetTrapFrame(Thread
) - (ULONG_PTR
)TrapFrame
) <=
1168 FIELD_OFFSET(KTRAP_FRAME
, EFlags
))
1170 /* The stack is somewhere in between frames, we need to fix it */
1179 /* Check for Pentium LOCK errata */
1180 if (KiI386PentiumLockErrataPresent
)
1182 /* Not yet implemented */
1187 /* HACK: Check if interrupts are disabled and enable them */
1188 if (!(TrapFrame
->EFlags
& EFLAGS_INTERRUPT_MASK
))
1190 /* Enable interupts */
1192 #ifdef HACK_ABOVE_FIXED
1193 if (!(TrapFrame
->EFlags
& EFLAGS_INTERRUPT_MASK
))
1195 /* This is illegal */
1196 KeBugCheckWithTf(IRQL_NOT_LESS_OR_EQUAL
,
1199 TrapFrame
->ErrCode
& 1,
1206 /* Call the access fault handler */
1207 Status
= MmAccessFault(TrapFrame
->ErrCode
& 1,
1209 TrapFrame
->SegCs
& MODE_MASK
,
1211 if (NT_SUCCESS(Status
)) KiEoiHelper(TrapFrame
);
1213 /* Check for S-LIST fault */
1214 if (TrapFrame
->Eip
== (ULONG_PTR
)ExpInterlockedPopEntrySListFault
)
1216 /* Not yet implemented */
1221 /* Check for syscall fault */
1223 if ((TrapFrame
->Eip
== (ULONG_PTR
)CopyParams
) ||
1224 (TrapFrame
->Eip
== (ULONG_PTR
)ReadBatch
))
1226 /* Not yet implemented */
1231 /* Check for VDM trap */
1232 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
1234 /* Either kernel or user trap (non VDM) so dispatch exception */
1235 if (Status
== STATUS_ACCESS_VIOLATION
)
1237 /* This status code is repurposed so we can recognize it later */
1238 KiDispatchException2Args(KI_EXCEPTION_ACCESS_VIOLATION
,
1240 TrapFrame
->ErrCode
& 1,
1244 else if ((Status
== STATUS_GUARD_PAGE_VIOLATION
) ||
1245 (Status
== STATUS_STACK_OVERFLOW
))
1247 /* These faults only have two parameters */
1248 KiDispatchException2Args(Status
,
1250 TrapFrame
->ErrCode
& 1,
1255 /* Only other choice is an in-page error, with 3 parameters */
1256 KiDispatchExceptionFromTrapFrame(STATUS_IN_PAGE_ERROR
,
1259 TrapFrame
->ErrCode
& 1,
1268 KiTrap0FHandler(IN PKTRAP_FRAME TrapFrame
)
1270 /* Save trap frame */
1271 KiEnterTrap(TrapFrame
);
1273 /* FIXME: Kill the system */
1275 KiSystemFatalException(EXCEPTION_RESERVED_TRAP
, TrapFrame
);
1281 KiTrap10Handler(IN PKTRAP_FRAME TrapFrame
)
1284 PFX_SAVE_AREA SaveArea
;
1286 /* Save trap frame */
1287 KiEnterTrap(TrapFrame
);
1289 /* Check if this is the NPX thrad */
1290 Thread
= KeGetCurrentThread();
1291 SaveArea
= KiGetThreadNpxArea(Thread
);
1292 if (Thread
!= KeGetCurrentPrcb()->NpxThread
)
1294 /* It isn't, enable interrupts and set delayed error */
1296 SaveArea
->Cr0NpxState
|= CR0_TS
;
1299 KiEoiHelper(TrapFrame
);
1302 /* Otherwise, proceed with NPX fault handling */
1303 KiNpxHandler(TrapFrame
, Thread
, SaveArea
);
1309 KiTrap11Handler(IN PKTRAP_FRAME TrapFrame
)
1311 /* Save trap frame */
1312 KiEnterTrap(TrapFrame
);
1314 /* Enable interrupts and kill the system */
1316 KiSystemFatalException(EXCEPTION_ALIGNMENT_CHECK
, TrapFrame
);
1322 KiTrap13Handler(IN PKTRAP_FRAME TrapFrame
)
1325 PFX_SAVE_AREA SaveArea
;
1326 ULONG Cr0
, MxCsrMask
, Error
;
1328 /* Save trap frame */
1329 KiEnterTrap(TrapFrame
);
1331 /* Check if this is the NPX thrad */
1332 Thread
= KeGetCurrentThread();
1333 if (Thread
!= KeGetCurrentPrcb()->NpxThread
)
1335 /* It isn't, kill the system */
1336 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, (ULONG_PTR
)Thread
, 0, 0, TrapFrame
);
1339 /* Get the NPX frame */
1340 SaveArea
= KiGetThreadNpxArea(Thread
);
1342 /* Check for VDM trap */
1343 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
1345 /* Check for user trap */
1346 if (!KiUserTrap(TrapFrame
))
1348 /* Kernel should not fault on XMMI */
1349 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, 0, 0, 2, TrapFrame
);
1354 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
1357 /* Save FPU state */
1358 Ke386SaveFpuState(SaveArea
);
1360 /* Mark CR0 state dirty */
1361 Cr0
|= NPX_STATE_NOT_LOADED
;
1362 Cr0
|= SaveArea
->Cr0NpxState
;
1365 /* Update NPX state */
1366 Thread
->NpxState
= NPX_STATE_NOT_LOADED
;
1367 KeGetCurrentPrcb()->NpxThread
= NULL
;
1369 /* Clear the TS bit and re-enable interrupts */
1370 SaveArea
->Cr0NpxState
&= ~CR0_TS
;
1373 /* Now look at MxCsr to get the mask of errors we should care about */
1374 MxCsrMask
= ~((USHORT
)SaveArea
->U
.FxArea
.MXCsr
>> 7);
1376 /* Get legal exceptions that software should handle */
1377 Error
= (USHORT
)SaveArea
->U
.FxArea
.MXCsr
& (FSW_INVALID_OPERATION
|
1385 /* Now handle any of those legal errors */
1386 if (Error
& (FSW_INVALID_OPERATION
|
1393 /* By issuing an exception */
1394 KiDispatchException1Args(STATUS_FLOAT_MULTIPLE_TRAPS
,
1400 /* Unknown XMMI fault */
1401 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, 0, 0, 1, TrapFrame
);
1404 /* SOFTWARE SERVICES **********************************************************/
1408 KiGetTickCountHandler(IN PKTRAP_FRAME TrapFrame
)
1416 KiCallbackReturnHandler(IN PKTRAP_FRAME TrapFrame
)
1425 KiRaiseAssertionHandler(IN PKTRAP_FRAME TrapFrame
)
1427 /* Save trap frame */
1428 KiEnterTrap(TrapFrame
);
1430 /* Decrement EIP to point to the INT2C instruction (2 bytes, not 1 like INT3) */
1431 TrapFrame
->Eip
-= 2;
1433 /* Dispatch the exception */
1434 KiDispatchException0Args(STATUS_ASSERTION_FAILURE
,
1442 KiDebugServiceHandler(IN PKTRAP_FRAME TrapFrame
)
1444 /* Save trap frame */
1445 KiEnterTrap(TrapFrame
);
1447 /* Increment EIP to skip the INT3 instruction */
1450 /* Continue with the common handler */
1451 KiDebugHandler(TrapFrame
, TrapFrame
->Eax
, TrapFrame
->Ecx
, TrapFrame
->Edx
);
1457 KiSystemCall(IN PKTRAP_FRAME TrapFrame
,
1461 PKSERVICE_TABLE_DESCRIPTOR DescriptorTable
;
1462 ULONG Id
, Offset
, StackBytes
, Result
;
1464 ULONG SystemCallNumber
= TrapFrame
->Eax
;
1466 /* Get the current thread */
1467 Thread
= KeGetCurrentThread();
1469 /* Set debug header */
1470 KiFillTrapFrameDebug(TrapFrame
);
1472 /* Chain trap frames */
1473 TrapFrame
->Edx
= (ULONG_PTR
)Thread
->TrapFrame
;
1476 TrapFrame
->ErrCode
= 0;
1478 /* Save previous mode */
1479 TrapFrame
->PreviousPreviousMode
= Thread
->PreviousMode
;
1481 /* Save the SEH chain and terminate it for now */
1482 TrapFrame
->ExceptionList
= KeGetPcr()->NtTib
.ExceptionList
;
1483 KeGetPcr()->NtTib
.ExceptionList
= EXCEPTION_CHAIN_END
;
1485 /* Clear DR7 and check for debugging */
1487 if (__builtin_expect(Thread
->DispatcherHeader
.DebugActive
& 0xFF, 0))
1493 /* Set thread fields */
1494 Thread
->TrapFrame
= TrapFrame
;
1495 Thread
->PreviousMode
= KiUserTrap(TrapFrame
);
1497 /* Enable interrupts */
1500 /* Decode the system call number */
1501 Offset
= (SystemCallNumber
>> SERVICE_TABLE_SHIFT
) & SERVICE_TABLE_MASK
;
1502 Id
= SystemCallNumber
& SERVICE_NUMBER_MASK
;
1504 /* Get descriptor table */
1505 DescriptorTable
= (PVOID
)((ULONG_PTR
)Thread
->ServiceTable
+ Offset
);
1507 /* Validate the system call number */
1508 if (__builtin_expect(Id
>= DescriptorTable
->Limit
, 0))
1510 /* Check if this is a GUI call */
1511 if (!(Offset
& SERVICE_TABLE_TEST
))
1514 Result
= STATUS_INVALID_SYSTEM_SERVICE
;
1518 /* Convert us to a GUI thread -- must wrap in ASM to get new EBP */
1519 Result
= KiConvertToGuiThread();
1520 if (!NT_SUCCESS(Result
))
1522 /* Set the last error and fail */
1523 //SetLastWin32Error(RtlNtStatusToDosError(Result));
1527 /* Reload trap frame and descriptor table pointer from new stack */
1528 TrapFrame
= *(volatile PVOID
*)&Thread
->TrapFrame
;
1529 DescriptorTable
= (PVOID
)(*(volatile ULONG_PTR
*)&Thread
->ServiceTable
+ Offset
);
1531 /* Validate the system call number again */
1532 if (Id
>= DescriptorTable
->Limit
)
1535 Result
= STATUS_INVALID_SYSTEM_SERVICE
;
1540 /* Check if this is a GUI call */
1541 if (__builtin_expect(Offset
& SERVICE_TABLE_TEST
, 0))
1543 /* Get the batch count and flush if necessary */
1544 if (NtCurrentTeb()->GdiBatchCount
) KeGdiFlushUserBatch();
1547 /* Increase system call count */
1548 KeGetCurrentPrcb()->KeSystemCalls
++;
1550 /* FIXME: Increase individual counts on debug systems */
1551 //KiIncreaseSystemCallCount(DescriptorTable, Id);
1553 /* Get stack bytes */
1554 StackBytes
= DescriptorTable
->Number
[Id
];
1556 /* Probe caller stack */
1557 if (__builtin_expect((Arguments
< (PVOID
)MmUserProbeAddress
) && !(KiUserTrap(TrapFrame
)), 0))
1559 /* Access violation */
1564 /* Get the handler and make the system call */
1565 Handler
= (PVOID
)DescriptorTable
->Base
[Id
];
1566 Result
= KiSystemCallTrampoline(Handler
, Arguments
, StackBytes
);
1568 /* Make sure we're exiting correctly */
1569 KiExitSystemCallDebugChecks(Id
, TrapFrame
);
1571 /* Restore the old trap frame */
1573 Thread
->TrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
1575 /* Exit from system call */
1576 KiServiceExit(TrapFrame
, Result
);
1582 KiSystemServiceHandler(IN PKTRAP_FRAME TrapFrame
,
1585 /* Call the shared handler (inline) */
1586 KiSystemCall(TrapFrame
, Arguments
);
1592 KiFastCallEntryHandler(IN PKTRAP_FRAME TrapFrame
,
1595 /* Set up a fake INT Stack and enable interrupts */
1596 TrapFrame
->HardwareSegSs
= KGDT_R3_DATA
| RPL_MASK
;
1597 TrapFrame
->HardwareEsp
= (ULONG_PTR
)Arguments
;
1598 TrapFrame
->EFlags
= __readeflags() | EFLAGS_INTERRUPT_MASK
;
1599 TrapFrame
->SegCs
= KGDT_R3_CODE
| RPL_MASK
;
1600 TrapFrame
->Eip
= SharedUserData
->SystemCallReturn
;
1601 TrapFrame
->SegFs
= KGDT_R3_TEB
| RPL_MASK
;
1604 /* Arguments are actually 2 frames down (because of the double indirection) */
1605 Arguments
= (PVOID
)(TrapFrame
->HardwareEsp
+ 8);
1607 /* Call the shared handler (inline) */
1608 KiSystemCall(TrapFrame
, Arguments
);
1616 Kei386EoiHelper(VOID
)
1618 /* We should never see this call happening */
1619 DPRINT1("Mismatched NT/HAL version");