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 VOID
KiFastCallEntry(VOID
);
16 VOID
KiFastCallEntryWithSingleStep(VOID
);
18 extern PVOID FrRestore
;
19 VOID FASTCALL
Ke386LoadFpuState(IN PFX_SAVE_AREA SaveArea
);
21 /* GLOBALS ********************************************************************/
23 UCHAR KiTrapPrefixTable
[] =
26 0xF3, /* REP INS/OUTS */
38 UCHAR KiTrapIoTable
[] =
54 PFAST_SYSTEM_CALL_EXIT KiFastCallExitHandler
;
55 #if DBG && defined(_M_IX86) && !defined(_WINKD_)
56 PKDBG_PRESERVICEHOOK KeWin32PreServiceHook
= NULL
;
57 PKDBG_POSTSERVICEHOOK KeWin32PostServiceHook
= NULL
;
60 BOOLEAN StopChecking
= FALSE
;
64 /* TRAP EXIT CODE *************************************************************/
68 KiVdmTrap(IN PKTRAP_FRAME TrapFrame
)
70 /* Either the V8086 flag is on, or this is user-mode with a VDM */
71 return ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) ||
72 ((KiUserTrap(TrapFrame
)) && (PsGetCurrentProcess()->VdmObjects
)));
77 KiV86Trap(IN PKTRAP_FRAME TrapFrame
)
79 /* Check if the V8086 flag is on */
80 return ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) != 0);
85 KiIsFrameEdited(IN PKTRAP_FRAME TrapFrame
)
87 /* An edited frame changes esp. It is marked by clearing the bits
88 defined by FRAME_EDITED in the SegCs field of the trap frame */
89 return ((TrapFrame
->SegCs
& FRAME_EDITED
) == 0);
94 KiCommonExit(IN PKTRAP_FRAME TrapFrame
, BOOLEAN SkipPreviousMode
)
96 /* Disable interrupts until we return */
99 /* Check for APC delivery */
100 KiCheckForApcDelivery(TrapFrame
);
102 /* Restore the SEH handler chain */
103 KeGetPcr()->NtTib
.ExceptionList
= TrapFrame
->ExceptionList
;
105 /* Check if there are active debug registers */
106 if (__builtin_expect(TrapFrame
->Dr7
& ~DR7_RESERVED_MASK
, 0))
108 /* Check if the frame was from user mode or v86 mode */
109 if (KiUserTrap(TrapFrame
) ||
110 (TrapFrame
->EFlags
& EFLAGS_V86_MASK
))
112 /* Handle debug registers */
113 KiHandleDebugRegistersOnTrapExit(TrapFrame
);
117 /* Debugging checks */
118 KiExitTrapDebugChecks(TrapFrame
, SkipPreviousMode
);
124 KiEoiHelper(IN PKTRAP_FRAME TrapFrame
)
126 /* Common trap exit code */
127 KiCommonExit(TrapFrame
, TRUE
);
129 /* Check if this was a V8086 trap */
130 if (TrapFrame
->EFlags
& EFLAGS_V86_MASK
) KiTrapReturnNoSegments(TrapFrame
);
132 /* Check for user mode exit */
133 if (KiUserTrap(TrapFrame
)) KiTrapReturn(TrapFrame
);
135 /* Check for edited frame */
136 if (KiIsFrameEdited(TrapFrame
)) KiEditedTrapReturn(TrapFrame
);
138 /* Check if we have single stepping enabled */
139 if (TrapFrame
->EFlags
& EFLAGS_TF
) KiTrapReturnNoSegments(TrapFrame
);
141 /* Exit the trap to kernel mode */
142 KiTrapReturnNoSegmentsRet8(TrapFrame
);
148 KiServiceExit(IN PKTRAP_FRAME TrapFrame
,
151 ASSERT((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) == 0);
152 ASSERT(!KiIsFrameEdited(TrapFrame
));
154 /* Copy the status into EAX */
155 TrapFrame
->Eax
= Status
;
157 /* Common trap exit code */
158 KiCommonExit(TrapFrame
, FALSE
);
160 /* Restore previous mode */
161 KeGetCurrentThread()->PreviousMode
= (CCHAR
)TrapFrame
->PreviousPreviousMode
;
163 /* Check for user mode exit */
164 if (KiUserTrap(TrapFrame
))
166 /* Check if we were single stepping */
167 if (TrapFrame
->EFlags
& EFLAGS_TF
)
169 /* Must use the IRET handler */
170 KiSystemCallTrapReturn(TrapFrame
);
174 /* We can use the sysexit handler */
175 KiFastCallExitHandler(TrapFrame
);
179 /* Exit to kernel mode */
180 KiSystemCallReturn(TrapFrame
);
186 KiServiceExit2(IN PKTRAP_FRAME TrapFrame
)
188 /* Common trap exit code */
189 KiCommonExit(TrapFrame
, FALSE
);
191 /* Restore previous mode */
192 KeGetCurrentThread()->PreviousMode
= (CCHAR
)TrapFrame
->PreviousPreviousMode
;
194 /* Check if this was a V8086 trap */
195 if (TrapFrame
->EFlags
& EFLAGS_V86_MASK
) KiTrapReturnNoSegments(TrapFrame
);
197 /* Check for user mode exit */
198 if (KiUserTrap(TrapFrame
)) KiTrapReturn(TrapFrame
);
200 /* Check for edited frame */
201 if (KiIsFrameEdited(TrapFrame
)) KiEditedTrapReturn(TrapFrame
);
203 /* Check if we have single stepping enabled */
204 if (TrapFrame
->EFlags
& EFLAGS_TF
) KiTrapReturnNoSegments(TrapFrame
);
206 /* Exit the trap to kernel mode */
207 KiTrapReturnNoSegmentsRet8(TrapFrame
);
211 /* TRAP HANDLERS **************************************************************/
216 KiDebugHandler(IN PKTRAP_FRAME TrapFrame
,
221 /* Check for VDM trap */
222 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
224 /* Enable interrupts if the trap came from user-mode */
225 if (KiUserTrap(TrapFrame
)) _enable();
227 /* Dispatch the exception */
228 KiDispatchExceptionFromTrapFrame(STATUS_BREAKPOINT
,
241 KiNpxHandler(IN PKTRAP_FRAME TrapFrame
,
243 IN PFX_SAVE_AREA SaveArea
)
245 ULONG Cr0
, Mask
, Error
, ErrorOffset
, DataOffset
;
247 /* Check for VDM trap */
248 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
250 /* Check for kernel trap */
251 if (!KiUserTrap(TrapFrame
))
253 /* Kernel might've tripped a delayed error */
254 SaveArea
->Cr0NpxState
|= CR0_TS
;
256 /* Only valid if it happened during a restore */
257 if ((PVOID
)TrapFrame
->Eip
== FrRestore
)
259 /* It did, so just skip the instruction */
260 TrapFrame
->Eip
+= 3; /* Size of FRSTOR instruction */
261 KiEoiHelper(TrapFrame
);
265 /* User or kernel trap -- get ready to issue an exception */
266 if (Thread
->NpxState
== NPX_STATE_NOT_LOADED
)
270 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
274 Ke386SaveFpuState(SaveArea
);
276 /* Mark CR0 state dirty */
277 Cr0
|= NPX_STATE_NOT_LOADED
;
278 Cr0
|= SaveArea
->Cr0NpxState
;
281 /* Update NPX state */
282 Thread
->NpxState
= NPX_STATE_NOT_LOADED
;
283 KeGetCurrentPrcb()->NpxThread
= NULL
;
286 /* Clear the TS bit and re-enable interrupts */
287 SaveArea
->Cr0NpxState
&= ~CR0_TS
;
290 /* Check if we should get the FN or FX error */
291 if (KeI386FxsrPresent
)
294 Mask
= SaveArea
->U
.FxArea
.ControlWord
;
295 Error
= SaveArea
->U
.FxArea
.StatusWord
;
297 /* Get the FPU exception address too */
298 ErrorOffset
= SaveArea
->U
.FxArea
.ErrorOffset
;
299 DataOffset
= SaveArea
->U
.FxArea
.DataOffset
;
304 Mask
= SaveArea
->U
.FnArea
.ControlWord
;
305 Error
= SaveArea
->U
.FnArea
.StatusWord
;
307 /* Get the FPU exception address too */
308 ErrorOffset
= SaveArea
->U
.FnArea
.ErrorOffset
;
309 DataOffset
= SaveArea
->U
.FnArea
.DataOffset
;
312 /* Get legal exceptions that software should handle */
313 /* We do this by first masking off from the Mask the bits we need, */
314 /* This is done so we can keep the FSW_STACK_FAULT bit in Error. */
315 Mask
&= (FSW_INVALID_OPERATION
|
323 /* Check for invalid operation */
324 if (Error
& FSW_INVALID_OPERATION
)
326 /* NOTE: Stack fault is handled differently than any other case. */
327 /* 1. It's only raised for invalid operation. */
328 /* 2. It's only raised if invalid operation is not masked. */
329 if (Error
& FSW_STACK_FAULT
)
331 /* Issue stack check fault */
332 KiDispatchException2Args(STATUS_FLOAT_STACK_CHECK
,
340 KiDispatchException1Args(STATUS_FLOAT_INVALID_OPERATION
,
346 /* Check for divide by zero */
347 if (Error
& FSW_ZERO_DIVIDE
)
350 KiDispatchException1Args(STATUS_FLOAT_DIVIDE_BY_ZERO
,
356 /* Check for denormal */
357 if (Error
& FSW_DENORMAL
)
360 KiDispatchException1Args(STATUS_FLOAT_INVALID_OPERATION
,
366 /* Check for overflow */
367 if (Error
& FSW_OVERFLOW
)
370 KiDispatchException1Args(STATUS_FLOAT_OVERFLOW
,
376 /* Check for underflow */
377 if (Error
& FSW_UNDERFLOW
)
380 KiDispatchException1Args(STATUS_FLOAT_UNDERFLOW
,
386 /* Check for precision fault */
387 if (Error
& FSW_PRECISION
)
390 KiDispatchException1Args(STATUS_FLOAT_INEXACT_RESULT
,
396 /* Unknown FPU fault */
397 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 1, Error
, 0, 0, TrapFrame
);
403 KiTrap00Handler(IN PKTRAP_FRAME TrapFrame
)
405 /* Save trap frame */
406 KiEnterTrap(TrapFrame
);
408 /* Check for VDM trap */
409 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
411 /* Enable interrupts */
414 /* Dispatch the exception */
415 KiDispatchException0Args(STATUS_INTEGER_DIVIDE_BY_ZERO
,
423 KiTrap01Handler(IN PKTRAP_FRAME TrapFrame
)
425 /* Save trap frame */
426 KiEnterTrap(TrapFrame
);
428 /* Check for VDM trap */
429 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
431 /* Check if this was a single step after sysenter */
432 if (TrapFrame
->Eip
== (ULONG
)KiFastCallEntry
)
434 /* Disable single stepping */
435 TrapFrame
->EFlags
&= ~EFLAGS_TF
;
437 /* Re-enter at the alternative sysenter entry point */
438 TrapFrame
->Eip
= (ULONG
)KiFastCallEntryWithSingleStep
;
441 KiEoiHelper(TrapFrame
);
444 /* Enable interrupts if the trap came from user-mode */
445 if (KiUserTrap(TrapFrame
)) _enable();
447 /* Mask out trap flag and dispatch the exception */
448 TrapFrame
->EFlags
&= ~EFLAGS_TF
;
449 KiDispatchException0Args(STATUS_SINGLE_STEP
,
463 KTRAP_FRAME TrapFrame
;
467 // In some sort of strange recursion case, we might end up here with the IF
468 // flag incorrectly on the interrupt frame -- during a normal NMI this would
469 // normally already be set.
471 // For sanity's sake, make sure interrupts are disabled for sure.
472 // NMIs will already be since the CPU does it for us.
477 // Get the current TSS, thread, and process
480 Thread
= ((PKIPCR
)PCR
)->PrcbData
.CurrentThread
;
481 Process
= Thread
->ApcState
.Process
;
484 // Save data usually not in the TSS
486 Tss
->CR3
= Process
->DirectoryTableBase
[0];
487 Tss
->IoMapBase
= Process
->IopmOffset
;
488 Tss
->LDT
= Process
->LdtDescriptor
.LimitLow
? KGDT_LDT
: 0;
491 // Now get the base address of the NMI TSS
493 TssGdt
= &((PKIPCR
)KeGetPcr())->GDT
[KGDT_NMI_TSS
/ sizeof(KGDTENTRY
)];
494 NmiTss
= (PKTSS
)(ULONG_PTR
)(TssGdt
->BaseLow
|
495 TssGdt
->HighWord
.Bytes
.BaseMid
<< 16 |
496 TssGdt
->HighWord
.Bytes
.BaseHi
<< 24);
499 // Switch to it and activate it, masking off the nested flag
501 // Note that in reality, we are already on the NMI tss -- we just need to
502 // update the PCR to reflect this
505 __writeeflags(__readeflags() &~ EFLAGS_NESTED_TASK
);
506 TssGdt
->HighWord
.Bits
.Dpl
= 0;
507 TssGdt
->HighWord
.Bits
.Pres
= 1;
508 TssGdt
->HighWord
.Bits
.Type
= I386_TSS
;
511 // Now build the trap frame based on the original TSS
513 // The CPU does a hardware "Context switch" / task switch of sorts and so it
514 // takes care of saving our context in the normal TSS.
516 // We just have to go get the values...
518 RtlZeroMemory(&TrapFrame
, sizeof(KTRAP_FRAME
));
519 TrapFrame
.HardwareSegSs
= Tss
->Ss0
;
520 TrapFrame
.HardwareEsp
= Tss
->Esp0
;
521 TrapFrame
.EFlags
= Tss
->EFlags
;
522 TrapFrame
.SegCs
= Tss
->Cs
;
523 TrapFrame
.Eip
= Tss
->Eip
;
524 TrapFrame
.Ebp
= Tss
->Ebp
;
525 TrapFrame
.Ebx
= Tss
->Ebx
;
526 TrapFrame
.Esi
= Tss
->Esi
;
527 TrapFrame
.Edi
= Tss
->Edi
;
528 TrapFrame
.SegFs
= Tss
->Fs
;
529 TrapFrame
.ExceptionList
= PCR
->NtTib
.ExceptionList
;
530 TrapFrame
.PreviousPreviousMode
= -1;
531 TrapFrame
.Eax
= Tss
->Eax
;
532 TrapFrame
.Ecx
= Tss
->Ecx
;
533 TrapFrame
.Edx
= Tss
->Edx
;
534 TrapFrame
.SegDs
= Tss
->Ds
;
535 TrapFrame
.SegEs
= Tss
->Es
;
536 TrapFrame
.SegGs
= Tss
->Gs
;
537 TrapFrame
.DbgEip
= Tss
->Eip
;
538 TrapFrame
.DbgEbp
= Tss
->Ebp
;
541 // Store the trap frame in the KPRCB
543 KiSaveProcessorState(&TrapFrame
, NULL
);
546 // Call any registered NMI handlers and see if they handled it or not
551 // They did not, so call the platform HAL routine to bugcheck the system
553 // Make sure the HAL believes it's running at HIGH IRQL... we can't use
554 // the normal APIs here as playing with the IRQL could change the system
558 PCR
->Irql
= HIGH_LEVEL
;
564 // Although the CPU disabled NMIs, we just did a BIOS Call, which could've
565 // totally changed things.
567 // We have to make sure we're still in our original NMI -- a nested NMI
568 // will point back to the NMI TSS, and in that case we're hosed.
570 if (PCR
->TSS
->Backlink
!= KGDT_NMI_TSS
)
573 // Restore original TSS
578 // Set it back to busy
580 TssGdt
->HighWord
.Bits
.Dpl
= 0;
581 TssGdt
->HighWord
.Bits
.Pres
= 1;
582 TssGdt
->HighWord
.Bits
.Type
= I386_ACTIVE_TSS
;
585 // Restore nested flag
587 __writeeflags(__readeflags() | EFLAGS_NESTED_TASK
);
590 // Handled, return from interrupt
596 // Unhandled: crash the system
598 KiSystemFatalException(EXCEPTION_NMI
, NULL
);
604 KiTrap03Handler(IN PKTRAP_FRAME TrapFrame
)
606 /* Save trap frame */
607 KiEnterTrap(TrapFrame
);
609 /* Continue with the common handler */
610 KiDebugHandler(TrapFrame
, BREAKPOINT_BREAK
, 0, 0);
616 KiTrap04Handler(IN PKTRAP_FRAME TrapFrame
)
618 /* Save trap frame */
619 KiEnterTrap(TrapFrame
);
621 /* Check for VDM trap */
622 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
624 /* Enable interrupts */
627 /* Dispatch the exception */
628 KiDispatchException0Args(STATUS_INTEGER_OVERFLOW
,
636 KiTrap05Handler(IN PKTRAP_FRAME TrapFrame
)
638 /* Save trap frame */
639 KiEnterTrap(TrapFrame
);
641 /* Check for VDM trap */
642 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
644 /* Check for kernel-mode fault */
645 if (!KiUserTrap(TrapFrame
)) KiSystemFatalException(EXCEPTION_BOUND_CHECK
, TrapFrame
);
647 /* Enable interrupts */
650 /* Dispatch the exception */
651 KiDispatchException0Args(STATUS_ARRAY_BOUNDS_EXCEEDED
,
659 KiTrap06Handler(IN PKTRAP_FRAME TrapFrame
)
665 /* Check for V86 GPF */
666 if (__builtin_expect(KiV86Trap(TrapFrame
), 1))
669 KiEnterV86Trap(TrapFrame
);
671 /* Must be a VDM process */
672 if (__builtin_expect(!PsGetCurrentProcess()->VdmObjects
, 0))
674 /* Enable interrupts */
677 /* Setup illegal instruction fault */
678 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
683 /* Go to APC level */
684 OldIrql
= KfRaiseIrql(APC_LEVEL
);
688 if (!VdmDispatchBop(TrapFrame
))
690 /* Should only happen in VDM mode */
691 UNIMPLEMENTED_FATAL();
694 /* Bring IRQL back */
695 KfLowerIrql(OldIrql
);
698 /* Do a quick V86 exit if possible */
699 KiExitV86Trap(TrapFrame
);
702 /* Save trap frame */
703 KiEnterTrap(TrapFrame
);
705 /* Enable interrupts */
706 Instruction
= (PUCHAR
)TrapFrame
->Eip
;
709 /* Check for user trap */
710 if (KiUserTrap(TrapFrame
))
714 /* Scan next 4 opcodes */
715 for (i
= 0; i
< 4; i
++)
717 /* Check for LOCK instruction */
718 if (Instruction
[i
] == 0xF0)
720 /* Send invalid lock sequence exception */
721 KiDispatchException0Args(STATUS_INVALID_LOCK_SEQUENCE
,
727 /* FIXME: SEH ends here */
730 /* Kernel-mode or user-mode fault (but not LOCK) */
731 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
740 KiTrap07Handler(IN PKTRAP_FRAME TrapFrame
)
742 PKTHREAD Thread
, NpxThread
;
743 PFX_SAVE_AREA SaveArea
, NpxSaveArea
;
746 /* Save trap frame */
747 KiEnterTrap(TrapFrame
);
749 /* Try to handle NPX delay load */
752 /* Get the current thread */
753 Thread
= KeGetCurrentThread();
755 /* Get the NPX frame */
756 SaveArea
= KiGetThreadNpxArea(Thread
);
758 /* Check if emulation is enabled */
759 if (SaveArea
->Cr0NpxState
& CR0_EM
)
761 /* Not implemented */
762 UNIMPLEMENTED_FATAL();
765 /* Save CR0 and check NPX state */
767 if (Thread
->NpxState
!= NPX_STATE_LOADED
)
770 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
773 /* Get the NPX thread */
774 NpxThread
= KeGetCurrentPrcb()->NpxThread
;
777 /* Get the NPX frame */
778 NpxSaveArea
= KiGetThreadNpxArea(NpxThread
);
781 Ke386SaveFpuState(NpxSaveArea
);
783 /* Update NPX state */
784 NpxThread
->NpxState
= NPX_STATE_NOT_LOADED
;
788 Ke386LoadFpuState(SaveArea
);
790 /* Update NPX state */
791 Thread
->NpxState
= NPX_STATE_LOADED
;
792 KeGetCurrentPrcb()->NpxThread
= Thread
;
794 /* Enable interrupts */
797 /* Check if CR0 needs to be reloaded due to context switch */
798 if (!SaveArea
->Cr0NpxState
) KiEoiHelper(TrapFrame
);
800 /* Otherwise, we need to reload CR0, disable interrupts */
805 Cr0
|= SaveArea
->Cr0NpxState
;
808 /* Now restore interrupts and check for TS */
810 if (Cr0
& CR0_TS
) KiEoiHelper(TrapFrame
);
812 /* We're still here -- clear TS and try again */
813 __writecr0(__readcr0() &~ CR0_TS
);
818 /* This is an actual fault, not a lack of FPU state */
823 /* TS should not be set */
827 * If it's incorrectly set, then maybe the state is actually still valid
828 * but we could have lost track of that due to a BIOS call.
829 * Make sure MP is still set, which should verify the theory.
833 /* Indeed, the state is actually still valid, so clear TS */
834 __writecr0(__readcr0() &~ CR0_TS
);
835 KiEoiHelper(TrapFrame
);
838 /* Otherwise, something strange is going on */
839 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 2, Cr0
, 0, 0, TrapFrame
);
842 /* It's not a delayed load, so process this trap as an NPX fault */
843 KiNpxHandler(TrapFrame
, Thread
, SaveArea
);
849 KiTrap08Handler(IN PKTRAP_FRAME TrapFrame
)
851 /* FIXME: Not handled */
852 KiSystemFatalException(EXCEPTION_DOUBLE_FAULT
, TrapFrame
);
858 KiTrap09Handler(IN PKTRAP_FRAME TrapFrame
)
860 /* Save trap frame */
861 KiEnterTrap(TrapFrame
);
863 /* Enable interrupts and kill the system */
865 KiSystemFatalException(EXCEPTION_NPX_OVERRUN
, TrapFrame
);
871 KiTrap0AHandler(IN PKTRAP_FRAME TrapFrame
)
873 /* Save trap frame */
874 KiEnterTrap(TrapFrame
);
876 /* Check for VDM trap */
877 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
879 /* Kill the system */
880 KiSystemFatalException(EXCEPTION_INVALID_TSS
, TrapFrame
);
886 KiTrap0BHandler(IN PKTRAP_FRAME TrapFrame
)
888 /* Save trap frame */
889 KiEnterTrap(TrapFrame
);
891 /* FIXME: Kill the system */
893 KiSystemFatalException(EXCEPTION_SEGMENT_NOT_PRESENT
, TrapFrame
);
899 KiTrap0CHandler(IN PKTRAP_FRAME TrapFrame
)
901 /* Save trap frame */
902 KiEnterTrap(TrapFrame
);
904 /* FIXME: Kill the system */
906 KiSystemFatalException(EXCEPTION_STACK_FAULT
, TrapFrame
);
912 KiTrap0DHandler(IN PKTRAP_FRAME TrapFrame
)
915 BOOLEAN Privileged
= FALSE
;
917 UCHAR Instruction
= 0;
920 /* Check for V86 GPF */
921 if (__builtin_expect(KiV86Trap(TrapFrame
), 1))
924 KiEnterV86Trap(TrapFrame
);
926 /* Must be a VDM process */
927 if (__builtin_expect(!PsGetCurrentProcess()->VdmObjects
, 0))
929 /* Enable interrupts */
932 /* Setup illegal instruction fault */
933 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
938 /* Go to APC level */
939 OldIrql
= KfRaiseIrql(APC_LEVEL
);
942 /* Handle the V86 opcode */
943 if (__builtin_expect(Ki386HandleOpcodeV86(TrapFrame
) == 0xFF, 0))
945 /* Should only happen in VDM mode */
946 UNIMPLEMENTED_FATAL();
949 /* Bring IRQL back */
950 KfLowerIrql(OldIrql
);
953 /* Do a quick V86 exit if possible */
954 KiExitV86Trap(TrapFrame
);
957 /* Save trap frame */
958 KiEnterTrap(TrapFrame
);
960 /* Check for user-mode GPF */
961 if (KiUserTrap(TrapFrame
))
963 /* Should not be VDM */
964 ASSERT(KiVdmTrap(TrapFrame
) == FALSE
);
966 /* Enable interrupts and check error code */
968 if (!TrapFrame
->ErrCode
)
971 Instructions
= (PUCHAR
)TrapFrame
->Eip
;
973 /* Scan next 15 bytes */
974 for (i
= 0; i
< 15; i
++)
976 /* Skip prefix instructions */
977 for (j
= 0; j
< sizeof(KiTrapPrefixTable
); j
++)
979 /* Is this a prefix instruction? */
980 if (Instructions
[i
] == KiTrapPrefixTable
[j
])
987 /* Is this NOT any prefix instruction? */
988 if (j
== sizeof(KiTrapPrefixTable
))
990 /* We can go ahead and handle the fault now */
991 Instruction
= Instructions
[i
];
996 /* If all we found was prefixes, then this instruction is too long */
999 /* Setup illegal instruction fault */
1000 KiDispatchException0Args(STATUS_ILLEGAL_INSTRUCTION
,
1005 /* Check for privileged instructions */
1006 DPRINT("Instruction (%lu) at fault: %lx %lx %lx %lx\n",
1009 Instructions
[i
+ 1],
1010 Instructions
[i
+ 2],
1011 Instructions
[i
+ 3]);
1012 if (Instruction
== 0xF4) // HLT
1014 /* HLT is privileged */
1017 else if (Instruction
== 0x0F)
1019 /* Test if it's any of the privileged two-byte opcodes */
1020 if (((Instructions
[i
+ 1] == 0x00) && // LLDT or LTR
1021 (((Instructions
[i
+ 2] & 0x38) == 0x10) || // LLDT
1022 (Instructions
[i
+ 2] == 0x18))) || // LTR
1023 ((Instructions
[i
+ 1] == 0x01) && // LGDT or LIDT or LMSW
1024 (((Instructions
[i
+ 2] & 0x38) == 0x10) || // LGDT
1025 (Instructions
[i
+ 2] == 0x18) || // LIDT
1026 (Instructions
[i
+ 2] == 0x30))) || // LMSW
1027 (Instructions
[i
+ 1] == 0x08) || // INVD
1028 (Instructions
[i
+ 1] == 0x09) || // WBINVD
1029 (Instructions
[i
+ 1] == 0x35) || // SYSEXIT
1030 (Instructions
[i
+ 1] == 0x21) || // MOV DR, XXX
1031 (Instructions
[i
+ 1] == 0x06) || // CLTS
1032 (Instructions
[i
+ 1] == 0x20) || // MOV CR, XXX
1033 (Instructions
[i
+ 1] == 0x22) || // MOV XXX, CR
1034 (Instructions
[i
+ 1] == 0x23) || // MOV YYY, DR
1035 (Instructions
[i
+ 1] == 0x30) || // WRMSR
1036 (Instructions
[i
+ 1] == 0x33)) // RDPMC
1037 // INVLPG, INVLPGA, SYSRET
1039 /* These are all privileged */
1045 /* Get the IOPL and compare with the RPL mask */
1046 Iopl
= (TrapFrame
->EFlags
& EFLAGS_IOPL
) >> 12;
1047 if ((TrapFrame
->SegCs
& RPL_MASK
) > Iopl
)
1049 /* I/O privilege error -- check for known instructions */
1050 if ((Instruction
== 0xFA) || (Instruction
== 0xFB)) // CLI or STI
1052 /* These are privileged */
1057 /* Last hope: an IN/OUT instruction */
1058 for (j
= 0; j
< sizeof(KiTrapIoTable
); j
++)
1060 /* Is this an I/O instruction? */
1061 if (Instruction
== KiTrapIoTable
[j
])
1063 /* Then it's privileged */
1072 /* So now... was the instruction privileged or not? */
1075 /* Whew! We have a privileged instruction, so dispatch the fault */
1076 KiDispatchException0Args(STATUS_PRIVILEGED_INSTRUCTION
,
1082 /* If we got here, send an access violation */
1083 KiDispatchException2Args(STATUS_ACCESS_VIOLATION
,
1091 * Check for a fault during checking of the user instruction.
1093 * Note that the SEH handler will catch invalid EIP, but we could be dealing
1094 * with an invalid CS, which will generate another GPF instead.
1097 if (((PVOID
)TrapFrame
->Eip
>= (PVOID
)KiTrap0DHandler
) &&
1098 ((PVOID
)TrapFrame
->Eip
< (PVOID
)KiTrap0DHandler
))
1100 /* Not implemented */
1101 UNIMPLEMENTED_FATAL();
1105 * NOTE: The ASM trap exit code would restore segment registers by doing
1106 * a POP <SEG>, which could cause an invalid segment if someone had messed
1107 * with the segment values.
1109 * Another case is a bogus SS, which would hit a GPF when doing the iret.
1110 * This could only be done through a buggy or malicious driver, or perhaps
1111 * the kernel debugger.
1113 * The kernel normally restores the "true" segment if this happens.
1115 * However, since we're restoring in C, not ASM, we can't detect
1116 * POP <SEG> since the actual instructions will be different.
1118 * A better technique would be to check the EIP and somehow edit the
1119 * trap frame before restarting the instruction -- but we would need to
1120 * know the extract instruction that was used first.
1122 * We could force a special instrinsic to use stack instructions, or write
1123 * a simple instruction length checker.
1125 * Nevertheless, this is a lot of work for the purpose of avoiding a crash
1126 * when the user is purposedly trying to create one from kernel-mode, so
1127 * we should probably table this for now since it's not a "real" issue.
1131 * NOTE2: Another scenario is the IRET during a V8086 restore (BIOS Call)
1132 * which will cause a GPF since the trap frame is a total mess (on purpose)
1133 * as built in KiEnterV86Mode.
1135 * The idea is to scan for IRET, scan for the known EIP adress, validate CS
1136 * and then manually issue a jump to the V8086 return EIP.
1138 Instructions
= (PUCHAR
)TrapFrame
->Eip
;
1139 if (Instructions
[0] == 0xCF)
1142 * Some evil shit is going on here -- this is not the SS:ESP you're
1143 * looking for! Instead, this is actually CS:EIP you're looking at!
1144 * Why? Because part of the trap frame actually corresponds to the IRET
1145 * stack during the trap exit!
1147 if ((TrapFrame
->HardwareEsp
== (ULONG
)Ki386BiosCallReturnAddress
) &&
1148 (TrapFrame
->HardwareSegSs
== (KGDT_R0_CODE
| RPL_MASK
)))
1150 /* Exit the V86 trap! */
1151 Ki386BiosCallReturnAddress(TrapFrame
);
1155 /* Otherwise, this is another kind of IRET fault */
1156 UNIMPLEMENTED_FATAL();
1160 /* So since we're not dealing with the above case, check for RDMSR/WRMSR */
1161 if ((Instructions
[0] == 0xF) && // 2-byte opcode
1162 ((Instructions
[1] == 0x32) || // RDMSR
1163 (Instructions
[1] == 0x30))) // WRMSR
1165 /* Unknown CPU MSR, so raise an access violation */
1166 KiDispatchException0Args(STATUS_ACCESS_VIOLATION
,
1171 /* Check for lazy segment load */
1172 if (TrapFrame
->SegDs
!= (KGDT_R3_DATA
| RPL_MASK
))
1175 TrapFrame
->SegDs
= (KGDT_R3_DATA
| RPL_MASK
);
1177 else if (TrapFrame
->SegEs
!= (KGDT_R3_DATA
| RPL_MASK
))
1180 TrapFrame
->SegEs
= (KGDT_R3_DATA
| RPL_MASK
);
1184 /* Whatever it is, we can't handle it */
1185 KiSystemFatalException(EXCEPTION_GP_FAULT
, TrapFrame
);
1188 /* Return to where we came from */
1189 KiTrapReturn(TrapFrame
);
1195 KiTrap0EHandler(IN PKTRAP_FRAME TrapFrame
)
1201 /* Save trap frame */
1202 KiEnterTrap(TrapFrame
);
1204 /* Check if this is the base frame */
1205 Thread
= KeGetCurrentThread();
1206 if (KeGetTrapFrame(Thread
) != TrapFrame
)
1208 /* It isn't, check if this is a second nested frame */
1209 if (((ULONG_PTR
)KeGetTrapFrame(Thread
) - (ULONG_PTR
)TrapFrame
) <=
1210 FIELD_OFFSET(KTRAP_FRAME
, EFlags
))
1212 /* The stack is somewhere in between frames, we need to fix it */
1213 UNIMPLEMENTED_FATAL();
1220 /* Enable interupts */
1223 /* Check if we came in with interrupts disabled */
1224 if (!(TrapFrame
->EFlags
& EFLAGS_INTERRUPT_MASK
))
1226 /* This is completely illegal, bugcheck the system */
1227 KeBugCheckWithTf(IRQL_NOT_LESS_OR_EQUAL
,
1230 TrapFrame
->ErrCode
& 2 ? TRUE
: FALSE
,
1235 /* Check for S-LIST fault in kernel mode */
1236 if (TrapFrame
->Eip
== (ULONG_PTR
)ExpInterlockedPopEntrySListFault
)
1238 PSLIST_HEADER SListHeader
;
1240 /* Sanity check that the assembly is correct:
1241 This must be mov ebx, [eax]
1242 Followed by cmpxchg8b [ebp] */
1243 ASSERT((((UCHAR
*)TrapFrame
->Eip
)[0] == 0x8B) &&
1244 (((UCHAR
*)TrapFrame
->Eip
)[1] == 0x18) &&
1245 (((UCHAR
*)TrapFrame
->Eip
)[2] == 0x0F) &&
1246 (((UCHAR
*)TrapFrame
->Eip
)[3] == 0xC7) &&
1247 (((UCHAR
*)TrapFrame
->Eip
)[4] == 0x4D) &&
1248 (((UCHAR
*)TrapFrame
->Eip
)[5] == 0x00));
1250 /* Get the pointer to the SLIST_HEADER */
1251 SListHeader
= (PSLIST_HEADER
)TrapFrame
->Ebp
;
1253 /* Check if the Next member of the SLIST_HEADER was changed */
1254 if (SListHeader
->Next
.Next
!= (PSLIST_ENTRY
)TrapFrame
->Eax
)
1256 /* Restart the operation */
1257 TrapFrame
->Eip
= (ULONG_PTR
)ExpInterlockedPopEntrySListResume
;
1259 /* Continue execution */
1260 KiEoiHelper(TrapFrame
);
1265 /* Do what windows does and issue an invalid access violation */
1266 KiDispatchException2Args(KI_EXCEPTION_ACCESS_VIOLATION
,
1268 TrapFrame
->ErrCode
& 2 ? TRUE
: FALSE
,
1275 /* Call the access fault handler */
1276 Status
= MmAccessFault(TrapFrame
->ErrCode
& 1,
1278 KiUserTrap(TrapFrame
),
1280 if (NT_SUCCESS(Status
))
1283 /* Check whether the kernel debugger has owed breakpoints to be inserted */
1284 KdSetOwedBreakpoints();
1286 /* We succeeded, return */
1287 KiEoiHelper(TrapFrame
);
1290 /* Check for syscall fault */
1292 if ((TrapFrame
->Eip
== (ULONG_PTR
)CopyParams
) ||
1293 (TrapFrame
->Eip
== (ULONG_PTR
)ReadBatch
))
1295 /* Not yet implemented */
1296 UNIMPLEMENTED_FATAL();
1299 /* Check for VDM trap */
1300 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
1302 /* Either kernel or user trap (non VDM) so dispatch exception */
1303 if (Status
== STATUS_ACCESS_VIOLATION
)
1305 /* This status code is repurposed so we can recognize it later */
1306 KiDispatchException2Args(KI_EXCEPTION_ACCESS_VIOLATION
,
1308 TrapFrame
->ErrCode
& 2 ? TRUE
: FALSE
,
1312 else if ((Status
== STATUS_GUARD_PAGE_VIOLATION
) ||
1313 (Status
== STATUS_STACK_OVERFLOW
))
1315 /* These faults only have two parameters */
1316 KiDispatchException2Args(Status
,
1318 TrapFrame
->ErrCode
& 2 ? TRUE
: FALSE
,
1323 /* Only other choice is an in-page error, with 3 parameters */
1324 KiDispatchExceptionFromTrapFrame(STATUS_IN_PAGE_ERROR
,
1328 TrapFrame
->ErrCode
& 2 ? TRUE
: FALSE
,
1337 KiTrap0FHandler(IN PKTRAP_FRAME TrapFrame
)
1339 /* Save trap frame */
1340 KiEnterTrap(TrapFrame
);
1342 /* FIXME: Kill the system */
1344 KiSystemFatalException(EXCEPTION_RESERVED_TRAP
, TrapFrame
);
1350 KiTrap10Handler(IN PKTRAP_FRAME TrapFrame
)
1353 PFX_SAVE_AREA SaveArea
;
1355 /* Save trap frame */
1356 KiEnterTrap(TrapFrame
);
1358 /* Check if this is the NPX thrad */
1359 Thread
= KeGetCurrentThread();
1360 SaveArea
= KiGetThreadNpxArea(Thread
);
1361 if (Thread
!= KeGetCurrentPrcb()->NpxThread
)
1363 /* It isn't, enable interrupts and set delayed error */
1365 SaveArea
->Cr0NpxState
|= CR0_TS
;
1368 KiEoiHelper(TrapFrame
);
1371 /* Otherwise, proceed with NPX fault handling */
1372 KiNpxHandler(TrapFrame
, Thread
, SaveArea
);
1378 KiTrap11Handler(IN PKTRAP_FRAME TrapFrame
)
1380 /* Save trap frame */
1381 KiEnterTrap(TrapFrame
);
1383 /* Enable interrupts and kill the system */
1385 KiSystemFatalException(EXCEPTION_ALIGNMENT_CHECK
, TrapFrame
);
1391 KiTrap13Handler(IN PKTRAP_FRAME TrapFrame
)
1394 PFX_SAVE_AREA SaveArea
;
1395 ULONG Cr0
, MxCsrMask
, Error
;
1397 /* Save trap frame */
1398 KiEnterTrap(TrapFrame
);
1400 /* Check if this is the NPX thrad */
1401 Thread
= KeGetCurrentThread();
1402 if (Thread
!= KeGetCurrentPrcb()->NpxThread
)
1404 /* It isn't, kill the system */
1405 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, (ULONG_PTR
)Thread
, 0, 0, TrapFrame
);
1408 /* Get the NPX frame */
1409 SaveArea
= KiGetThreadNpxArea(Thread
);
1411 /* Check for VDM trap */
1412 ASSERT((KiVdmTrap(TrapFrame
)) == FALSE
);
1414 /* Check for user trap */
1415 if (!KiUserTrap(TrapFrame
))
1417 /* Kernel should not fault on XMMI */
1418 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, 0, 0, 2, TrapFrame
);
1423 Cr0
&= ~(CR0_MP
| CR0_EM
| CR0_TS
);
1426 /* Save FPU state */
1427 Ke386SaveFpuState(SaveArea
);
1429 /* Mark CR0 state dirty */
1430 Cr0
|= NPX_STATE_NOT_LOADED
;
1431 Cr0
|= SaveArea
->Cr0NpxState
;
1434 /* Update NPX state */
1435 Thread
->NpxState
= NPX_STATE_NOT_LOADED
;
1436 KeGetCurrentPrcb()->NpxThread
= NULL
;
1438 /* Clear the TS bit and re-enable interrupts */
1439 SaveArea
->Cr0NpxState
&= ~CR0_TS
;
1442 /* Now look at MxCsr to get the mask of errors we should care about */
1443 MxCsrMask
= ~((USHORT
)SaveArea
->U
.FxArea
.MXCsr
>> 7);
1445 /* Get legal exceptions that software should handle */
1446 Error
= (USHORT
)SaveArea
->U
.FxArea
.MXCsr
& (FSW_INVALID_OPERATION
|
1454 /* Now handle any of those legal errors */
1455 if (Error
& (FSW_INVALID_OPERATION
|
1462 /* By issuing an exception */
1463 KiDispatchException1Args(STATUS_FLOAT_MULTIPLE_TRAPS
,
1469 /* Unknown XMMI fault */
1470 KeBugCheckWithTf(TRAP_CAUSE_UNKNOWN
, 13, 0, 0, 1, TrapFrame
);
1473 /* SOFTWARE SERVICES **********************************************************/
1477 KiRaiseSecurityCheckFailureHandler(IN PKTRAP_FRAME TrapFrame
)
1479 /* Save trap frame */
1480 KiEnterTrap(TrapFrame
);
1482 /* Decrement EIP to point to the INT29 instruction (2 bytes, not 1 like INT3) */
1483 TrapFrame
->Eip
-= 2;
1485 /* Check if this is a user trap */
1486 if (KiUserTrap(TrapFrame
))
1488 /* Dispatch exception to user mode */
1489 KiDispatchExceptionFromTrapFrame(STATUS_STACK_BUFFER_OVERRUN
,
1490 EXCEPTION_NONCONTINUABLE
,
1500 EXCEPTION_RECORD ExceptionRecord
;
1502 /* Bugcheck the system */
1503 ExceptionRecord
.ExceptionCode
= STATUS_STACK_BUFFER_OVERRUN
;
1504 ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
1505 ExceptionRecord
.ExceptionRecord
= NULL
;
1506 ExceptionRecord
.ExceptionAddress
= (PVOID
)TrapFrame
->Eip
;
1507 ExceptionRecord
.NumberParameters
= 1;
1508 ExceptionRecord
.ExceptionInformation
[0] = TrapFrame
->Ecx
;
1510 KeBugCheckWithTf(KERNEL_SECURITY_CHECK_FAILURE
,
1512 (ULONG_PTR
)TrapFrame
,
1513 (ULONG_PTR
)&ExceptionRecord
,
1521 KiGetTickCountHandler(IN PKTRAP_FRAME TrapFrame
)
1523 UNIMPLEMENTED_DBGBREAK();
1528 KiCallbackReturnHandler(IN PKTRAP_FRAME TrapFrame
)
1533 /* Save the SEH chain, NtCallbackReturn will restore this */
1534 TrapFrame
->ExceptionList
= KeGetPcr()->NtTib
.ExceptionList
;
1536 /* Set thread fields */
1537 Thread
= KeGetCurrentThread();
1538 Thread
->TrapFrame
= TrapFrame
;
1539 Thread
->PreviousMode
= KiUserTrap(TrapFrame
);
1540 ASSERT(Thread
->PreviousMode
!= KernelMode
);
1542 /* Pass the register parameters to NtCallbackReturn.
1543 Result pointer is in ecx, result length in edx, status in eax */
1544 Status
= NtCallbackReturn((PVOID
)TrapFrame
->Ecx
,
1548 /* If we got here, something went wrong. Return an error to the caller */
1549 KiServiceExit(TrapFrame
, Status
);
1555 KiRaiseAssertionHandler(IN PKTRAP_FRAME TrapFrame
)
1557 /* Save trap frame */
1558 KiEnterTrap(TrapFrame
);
1560 /* Decrement EIP to point to the INT2C instruction (2 bytes, not 1 like INT3) */
1561 TrapFrame
->Eip
-= 2;
1563 /* Dispatch the exception */
1564 KiDispatchException0Args(STATUS_ASSERTION_FAILURE
,
1572 KiDebugServiceHandler(IN PKTRAP_FRAME TrapFrame
)
1574 /* Save trap frame */
1575 KiEnterTrap(TrapFrame
);
1577 /* Increment EIP to skip the INT3 instruction */
1580 /* Continue with the common handler */
1581 KiDebugHandler(TrapFrame
, TrapFrame
->Eax
, TrapFrame
->Ecx
, TrapFrame
->Edx
);
1587 KiDbgPreServiceHook(ULONG SystemCallNumber
, PULONG_PTR Arguments
)
1589 #if DBG && !defined(_WINKD_)
1590 if (SystemCallNumber
>= 0x1000 && KeWin32PreServiceHook
)
1591 KeWin32PreServiceHook(SystemCallNumber
, Arguments
);
1597 KiDbgPostServiceHook(ULONG SystemCallNumber
, ULONG_PTR Result
)
1599 #if DBG && !defined(_WINKD_)
1600 if (SystemCallNumber
>= 0x1000 && KeWin32PostServiceHook
)
1601 return KeWin32PostServiceHook(SystemCallNumber
, Result
);
1609 KiSystemServiceHandler(IN PKTRAP_FRAME TrapFrame
,
1613 PKSERVICE_TABLE_DESCRIPTOR DescriptorTable
;
1614 ULONG Id
, Offset
, StackBytes
, Result
;
1616 ULONG SystemCallNumber
= TrapFrame
->Eax
;
1618 /* Get the current thread */
1619 Thread
= KeGetCurrentThread();
1621 /* Set debug header */
1622 KiFillTrapFrameDebug(TrapFrame
);
1624 /* Chain trap frames */
1625 TrapFrame
->Edx
= (ULONG_PTR
)Thread
->TrapFrame
;
1628 TrapFrame
->ErrCode
= 0;
1630 /* Save previous mode */
1631 TrapFrame
->PreviousPreviousMode
= Thread
->PreviousMode
;
1633 /* Save the SEH chain and terminate it for now */
1634 TrapFrame
->ExceptionList
= KeGetPcr()->NtTib
.ExceptionList
;
1635 KeGetPcr()->NtTib
.ExceptionList
= EXCEPTION_CHAIN_END
;
1637 /* Default to debugging disabled */
1640 /* Check if the frame was from user mode */
1641 if (KiUserTrap(TrapFrame
))
1643 /* Check for active debugging */
1644 if (KeGetCurrentThread()->Header
.DebugActive
& 0xFF)
1646 /* Handle debug registers */
1647 KiHandleDebugRegistersOnTrapEntry(TrapFrame
);
1651 /* Set thread fields */
1652 Thread
->TrapFrame
= TrapFrame
;
1653 Thread
->PreviousMode
= KiUserTrap(TrapFrame
);
1655 /* Enable interrupts */
1658 /* Decode the system call number */
1659 Offset
= (SystemCallNumber
>> SERVICE_TABLE_SHIFT
) & SERVICE_TABLE_MASK
;
1660 Id
= SystemCallNumber
& SERVICE_NUMBER_MASK
;
1662 /* Get descriptor table */
1663 DescriptorTable
= (PVOID
)((ULONG_PTR
)Thread
->ServiceTable
+ Offset
);
1665 /* Validate the system call number */
1666 if (__builtin_expect(Id
>= DescriptorTable
->Limit
, 0))
1668 /* Check if this is a GUI call */
1669 if (!(Offset
& SERVICE_TABLE_TEST
))
1672 Result
= STATUS_INVALID_SYSTEM_SERVICE
;
1676 /* Convert us to a GUI thread -- must wrap in ASM to get new EBP */
1677 Result
= KiConvertToGuiThread();
1679 /* Reload trap frame and descriptor table pointer from new stack */
1680 TrapFrame
= *(volatile PVOID
*)&Thread
->TrapFrame
;
1681 DescriptorTable
= (PVOID
)(*(volatile ULONG_PTR
*)&Thread
->ServiceTable
+ Offset
);
1683 if (!NT_SUCCESS(Result
))
1685 /* Set the last error and fail */
1689 /* Validate the system call number again */
1690 if (Id
>= DescriptorTable
->Limit
)
1693 Result
= STATUS_INVALID_SYSTEM_SERVICE
;
1698 /* Check if this is a GUI call */
1699 if (__builtin_expect(Offset
& SERVICE_TABLE_TEST
, 0))
1701 /* Get the batch count and flush if necessary */
1702 if (NtCurrentTeb()->GdiBatchCount
) KeGdiFlushUserBatch();
1705 /* Increase system call count */
1706 KeGetCurrentPrcb()->KeSystemCalls
++;
1708 /* FIXME: Increase individual counts on debug systems */
1709 //KiIncreaseSystemCallCount(DescriptorTable, Id);
1711 /* Get stack bytes */
1712 StackBytes
= DescriptorTable
->Number
[Id
];
1714 /* Probe caller stack */
1715 if (__builtin_expect((Arguments
< (PVOID
)MmUserProbeAddress
) && !(KiUserTrap(TrapFrame
)), 0))
1717 /* Access violation */
1718 UNIMPLEMENTED_FATAL();
1721 /* Call pre-service debug hook */
1722 KiDbgPreServiceHook(SystemCallNumber
, Arguments
);
1724 /* Get the handler and make the system call */
1725 Handler
= (PVOID
)DescriptorTable
->Base
[Id
];
1726 Result
= KiSystemCallTrampoline(Handler
, Arguments
, StackBytes
);
1728 /* Call post-service debug hook */
1729 Result
= KiDbgPostServiceHook(SystemCallNumber
, Result
);
1731 /* Make sure we're exiting correctly */
1732 KiExitSystemCallDebugChecks(Id
, TrapFrame
);
1734 /* Restore the old trap frame */
1736 Thread
->TrapFrame
= (PKTRAP_FRAME
)TrapFrame
->Edx
;
1738 /* Exit from system call */
1739 KiServiceExit(TrapFrame
, Result
);
1747 Kei386EoiHelper(VOID
)
1749 /* We should never see this call happening */
1750 ERROR_FATAL("Mismatched NT/HAL version");