2 * FILE: ntoskrnl/ke/i386/trap.S
3 * COPYRIGHT: See COPYING in the top level directory
4 * PURPOSE: System Traps, Entrypoints and Exitpoints
5 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
6 * NOTE: See asmmacro.S for the shared entry/exit code.
9 /* INCLUDES ******************************************************************/
12 #include <internal/i386/asmmacro.S>
13 .intel_syntax noprefix
15 /* GLOBALS *******************************************************************/
20 /* This is the Software Interrupt Table that we handle in this file: */
21 idt _KiTrap0, INT_32_DPL0 /* INT 00: Divide Error (#DE) */
22 idt _KiTrap1, INT_32_DPL0 /* INT 01: Debug Exception (#DB) */
23 idt _KiTrap2, INT_32_DPL0 /* INT 02: NMI Interrupt */
24 idt _KiTrap3, INT_32_DPL3 /* INT 03: Breakpoint Exception (#BP) */
25 idt _KiTrap4, INT_32_DPL3 /* INT 04: Overflow Exception (#OF) */
26 idt _KiTrap5, INT_32_DPL0 /* INT 05: BOUND Range Exceeded (#BR) */
27 idt _KiTrap6, INT_32_DPL0 /* INT 06: Invalid Opcode Code (#UD) */
28 idt _KiTrap7, INT_32_DPL0 /* INT 07: Device Not Available (#NM) */
29 idt _KiTrap8, INT_32_DPL0 /* INT 08: Double Fault Exception (#DF) */
30 idt _KiTrap9, INT_32_DPL0 /* INT 09: RESERVED */
31 idt _KiTrap10, INT_32_DPL0 /* INT 0A: Invalid TSS Exception (#TS) */
32 idt _KiTrap11, INT_32_DPL0 /* INT 0B: Segment Not Present (#NP) */
33 idt _KiTrap12, INT_32_DPL0 /* INT 0C: Stack Fault Exception (#SS) */
34 idt _KiTrap13, INT_32_DPL0 /* INT 0D: General Protection (#GP) */
35 idt _KiTrap14, INT_32_DPL0 /* INT 0E: Page-Fault Exception (#PF) */
36 idt _KiTrap0F, INT_32_DPL0 /* INT 0F: RESERVED */
37 idt _KiTrap16, INT_32_DPL0 /* INT 10: x87 FPU Error (#MF) */
38 idt _KiTrap17, INT_32_DPL0 /* INT 11: Align Check Exception (#AC) */
39 idt _KiTrap0F, INT_32_DPL0 /* INT 12: Machine Check Exception (#MC)*/
40 idt _KiTrap0F, INT_32_DPL0 /* INT 13: SIMD FPU Exception (#XF) */
42 idt _KiTrap0F, INT_32_DPL0 /* INT 14-29: UNDEFINED INTERRUPTS */
44 idt _KiGetTickCount, INT_32_DPL3 /* INT 2A: Get Tick Count Handler */
45 idt _KiCallbackReturn, INT_32_DPL3 /* INT 2B: User-Mode Callback Return */
46 idt _KiRaiseAssertion, INT_32_DPL3 /* INT 2C: Debug Assertion Handler */
47 idt _KiDebugService, INT_32_DPL3 /* INT 2D: Debug Service Handler */
48 idt _KiSystemService, INT_32_DPL3 /* INT 2E: System Call Service Handler */
49 idt _KiTrap0F, INT_32_DPL0 /* INT 2F: RESERVED */
50 GENERATE_IDT_STUBS /* INT 30-FF: UNEXPECTED INTERRUPTS */
52 /* System call entrypoints: */
53 .globl _KiFastCallEntry
54 .globl _KiSystemService
56 /* And special system-defined software traps: */
57 .globl _NtRaiseException@12
59 .globl _KiCoprocessorError@0
60 .globl _KiDispatchInterrupt@0
62 /* Interrupt template entrypoints */
63 .globl _KiInterruptTemplate
64 .globl _KiInterruptTemplateObject
65 .globl _KiInterruptTemplateDispatch
67 /* Chained and Normal generic interrupt handlers for 1st and 2nd level entry*/
68 .globl _KiChainedDispatch2ndLvl@0
69 .globl _KiInterruptDispatch@0
70 .globl _KiChainedDispatch@0
72 /* We implement the following trap exit points: */
73 .globl _KiServiceExit /* Exit from syscall */
74 .globl _KiServiceExit2 /* Exit from syscall with complete frame*/
75 .globl _Kei386EoiHelper@0 /* Exit from interrupt or H/W trap */
76 .globl _Kei386EoiHelper2ndEntry /* Exit from unexpected interrupt */
78 .globl _KiIdtDescriptor
83 .globl _KiUnexpectedEntrySize
84 _KiUnexpectedEntrySize:
85 .long _KiUnexpectedInterrupt1 - _KiUnexpectedInterrupt0
88 .asciz "\n\x7\x7!!! Unexpected Interrupt %02lx !!!\n"
91 .asciz "\n\x7\x7!!! Unhandled or Unexpected Code at line: %lx!!!\n"
95 .byte 0xF3 /* REP INS/OUTS */
117 .byte 0x6E /* OUTS */
118 .byte 0x6F /* OUTS */
120 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
129 .func KiSystemService
130 Dr_kss: DR_TRAP_FIXUP
133 /* Enter the shared system call prolog */
136 /* Jump to the actual handler */
140 .func KiFastCallEntry
141 Dr_FastCallDrSave: DR_TRAP_FIXUP
144 /* Enter the fast system call prolog */
145 FASTCALL_PROLOG FastCallDrSave
150 * Find out which table offset to use. Converts 0x1124 into 0x10.
151 * The offset is related to the Table Index as such: Offset = TableIndex x 10
154 shr edi, SERVICE_TABLE_SHIFT
155 and edi, SERVICE_TABLE_MASK
158 /* Now add the thread's base system table to the offset */
159 add edi, [esi+KTHREAD_SERVICE_TABLE]
161 /* Get the true syscall ID and check it */
163 and eax, SERVICE_NUMBER_MASK
164 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
166 /* Invalid ID, try to load Win32K Table */
167 jnb KiBBTUnexpectedRange
169 /* Check if this was Win32K */
170 cmp ecx, SERVICE_TABLE_TEST
174 mov ecx, [fs:KPCR_TEB]
176 /* Check if we should flush the User Batch */
179 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
185 //call [_KeGdiFlushUserBatch]
190 /* Increase total syscall count */
191 inc dword ptr fs:[KPCR_SYSTEM_CALLS]
194 /* Increase per-syscall count */
195 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
197 inc dword ptr [ecx+eax*4]
200 /* Users's current stack frame pointer is source */
204 /* Allocate room for argument list from kernel stack */
205 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
209 /* Get pointer to function */
210 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
213 /* Allocate space on our stack */
216 /* Set the size of the arguments and the destination */
220 /* Make sure we're within the User Probe Address */
221 cmp esi, _MmUserProbeAddress
225 /* Copy the parameters */
230 * The following lines are for the benefit of GDB. It will see the return
231 * address of the "call ebx" below, find the last label before it and
232 * thinks that that's the start of the function. It will then check to see
233 * if it starts with a standard function prolog (push ebp, mov ebp,esp1).
234 * When that standard function prolog is not found, it will stop the
235 * stack backtrace. Since we do want to backtrace into usermode, let's
236 * make GDB happy and create a standard prolog.
244 /* Do the System Call */
249 /* Make sure the user-mode call didn't return at elevated IRQL */
250 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
252 mov esi, eax /* We need to save the syscall's return val */
253 call _KeGetCurrentIrql@0
256 mov eax, esi /* Restore it */
258 /* Get our temporary current thread pointer for sanity check */
259 mov ecx, fs:[KPCR_CURRENT_THREAD]
261 /* Make sure that we are not attached and that APCs are not disabled */
262 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
265 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
272 /* Deallocate the kernel stack frame */
275 KeReturnFromSystemCall:
277 /* Get the Current Thread */
278 mov ecx, [fs:KPCR_CURRENT_THREAD]
280 /* Restore the old trap frame pointer */
281 mov edx, [ebp+KTRAP_FRAME_EDX]
282 mov [ecx+KTHREAD_TRAP_FRAME], edx
287 /* Disable interrupts */
290 /* Check for, and deliver, User-Mode APCs if needed */
291 CHECK_FOR_APC_DELIVER 1
293 /* Exit and cleanup */
294 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
297 KiBBTUnexpectedRange:
299 /* If this isn't a Win32K call, fail */
300 cmp ecx, SERVICE_TABLE_TEST
303 /* Set up Win32K Table */
306 call _PsConvertToGuiThread@0
308 /* Check return code */
311 /* Restore registers */
315 /* Reset trap frame address */
317 mov [esi+KTHREAD_TRAP_FRAME], ebp
319 /* Try the Call again, if we suceeded */
323 * The Shadow Table should have a special byte table which tells us
324 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
327 /* Get the table limit and base */
328 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
329 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
330 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
332 /* Get the table address and add our index into the array */
334 and eax, SERVICE_NUMBER_MASK
337 /* Find out what we should return */
338 movsx eax, byte ptr [edx]
341 /* Return either 0 or -1, we've set it in EAX */
342 jle KeReturnFromSystemCall
344 /* Set STATUS_INVALID_SYSTEM_SERVICE */
345 mov eax, STATUS_INVALID_SYSTEM_SERVICE
346 jmp KeReturnFromSystemCall
350 /* Invalid System Call */
351 mov eax, STATUS_INVALID_SYSTEM_SERVICE
352 jmp KeReturnFromSystemCall
356 /* Check if this came from kernel-mode */
357 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
359 /* It's fine, go ahead with it */
362 /* Caller sent invalid parameters, fail here */
363 mov eax, STATUS_ACCESS_VIOLATION
368 /* Restore ESP0 stack */
369 mov ecx, [fs:KPCR_TSS]
370 mov esp, ss:[ecx+KTSS_ESP0]
372 /* Generate V86M Stack for Trap 6 */
378 /* Generate interrupt stack for Trap 6 */
379 push KGDT_R3_DATA + RPL_MASK
382 push KGDT_R3_CODE + RPL_MASK
388 /* Save current IRQL */
391 /* Set us at passive */
392 mov dword ptr fs:[KPCR_IRQL], 0
400 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
401 call _KeBugCheckEx@20
405 /* Get the index and APC state */
406 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
407 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
414 push APC_INDEX_MISMATCH
415 call _KeBugCheckEx@20
422 /* Disable interrupts */
425 /* Check for, and deliver, User-Mode APCs if needed */
426 CHECK_FOR_APC_DELIVER 0
428 /* Exit and cleanup */
429 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
432 .func Kei386EoiHelper@0
435 /* Disable interrupts */
438 /* Check for, and deliver, User-Mode APCs if needed */
439 CHECK_FOR_APC_DELIVER 0
441 /* Exit and cleanup */
442 _Kei386EoiHelper2ndEntry:
443 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
447 /* Move to EDX position */
448 add esp, KTRAP_FRAME_EDX
450 /* Restore volatiles */
455 /* Move to non-volatiles */
456 lea esp, [ebp+KTRAP_FRAME_EDI]
462 /* Skip error code and return */
471 Dr_kids: DR_TRAP_FIXUP
472 V86_kids: V86_TRAP_FIXUP
475 /* Push error code */
481 /* Increase EIP so we skip the INT3 */
482 //inc dword ptr [ebp+KTRAP_FRAME_EIP]
484 /* Call debug service dispatcher */
485 mov eax, [ebp+KTRAP_FRAME_EAX]
486 mov ecx, [ebp+KTRAP_FRAME_ECX]
487 mov edx, [ebp+KTRAP_FRAME_EAX]
489 /* Check for V86 mode */
490 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
493 /* Check if this is kernel or user-mode */
494 test byte ptr [ebp+KTRAP_FRAME_CS], 1
496 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
499 /* Re-enable interrupts */
503 /* Call the debug routine */
512 call _KdpServiceDispatcher@12
516 /* Get the current process */
517 mov ebx, [fs:KPCR_CURRENT_THREAD]
518 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
520 /* Check if this is a VDM Process */
521 //cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
524 /* Exit through common routine */
525 jmp _Kei386EoiHelper@0
528 .func NtRaiseException@12
529 _NtRaiseException@12:
531 /* NOTE: We -must- be called by Zw* to have the right frame! */
532 /* Push the stack frame */
535 /* Get the current thread and restore its trap frame */
536 mov ebx, [fs:KPCR_CURRENT_THREAD]
537 mov edx, [ebp+KTRAP_FRAME_EDX]
538 mov [ebx+KTHREAD_TRAP_FRAME], edx
540 /* Set up stack frame */
543 /* Get the Trap Frame in EBX */
546 /* Get the exception list and restore */
547 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
548 mov [fs:KPCR_EXCEPTION_LIST], eax
550 /* Get the parameters */
551 mov edx, [ebp+16] /* Search frames */
552 mov ecx, [ebp+12] /* Context */
553 mov eax, [ebp+8] /* Exception Record */
555 /* Raise the exception */
561 call _KiRaiseException@20
563 /* Restore trap frame in EBP */
567 /* Check the result */
571 /* Restore debug registers too */
578 /* NOTE: We -must- be called by Zw* to have the right frame! */
579 /* Push the stack frame */
582 /* Get the current thread and restore its trap frame */
583 mov ebx, [fs:KPCR_CURRENT_THREAD]
584 mov edx, [ebp+KTRAP_FRAME_EDX]
585 mov [ebx+KTHREAD_TRAP_FRAME], edx
587 /* Set up stack frame */
590 /* Save the parameters */
594 /* Call KiContinue */
600 /* Check if we failed (bad context record) */
604 /* Check if test alert was requested */
605 cmp dword ptr [ebp+12], 0
608 /* Test alert for the thread */
609 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
611 call _KeTestAlertThread@4
614 /* Return to previous context */
625 /* EXCEPTION DISPATCHERS *****************************************************/
627 .func CommonDispatchException
628 _CommonDispatchException:
630 /* Make space for an exception record */
631 sub esp, EXCEPTION_RECORD_LENGTH
634 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
636 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
637 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
638 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
639 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
641 /* Check parameter count */
645 /* Get information */
646 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
653 /* Set the record in ECX and check if this was V86 */
655 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
664 /* Calculate the previous mode */
665 mov eax, [ebp+KTRAP_FRAME_CS]
669 /* Dispatch the exception */
675 call _KiDispatchException@20
679 jmp _Kei386EoiHelper@0
682 .func DispatchNoParam
684 /* Call the common dispatcher */
686 call _CommonDispatchException
689 .func DispatchOneParam
691 /* Call the common dispatcher */
694 call _CommonDispatchException
697 .func DispatchTwoParam
699 /* Call the common dispatcher */
702 call _CommonDispatchException
705 /* HARDWARE TRAP HANDLERS ****************************************************/
710 /* TODO: Routine to fixup a KTRAP_FRAME when faulting from a syscall. */
715 Dr_kit0: DR_TRAP_FIXUP
716 V86_kit0: V86_TRAP_FIXUP
718 /* Push error code */
725 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
728 /* Check if the frame was from kernelmode */
729 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
732 /* Check the old mode */
733 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
737 /* Re-enable interrupts for user-mode and send the exception */
739 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
740 mov ebx, [ebp+KTRAP_FRAME_EIP]
744 /* Check if this is a VDM process */
745 mov ebx, [fs:KPCR_CURRENT_THREAD]
746 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
747 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
750 /* We don't support this yet! */
757 Dr_kit1: DR_TRAP_FIXUP
758 V86_kit1: V86_TRAP_FIXUP
760 /* Push error code */
767 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
770 /* Check if the frame was from kernelmode */
771 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
774 /* Check the old mode */
775 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
779 /* Enable interrupts for user-mode */
783 /* Prepare the exception */
784 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
785 mov ebx, [ebp+KTRAP_FRAME_EIP]
786 mov eax, STATUS_SINGLE_STEP
790 /* Check if this is a VDM process */
791 mov ebx, [fs:KPCR_CURRENT_THREAD]
792 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
793 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
796 /* We don't support VDM! */
804 /* FIXME: This is an NMI, nothing like a normal exception */
806 jmp _KiSystemFatalException
810 Dr_kit3: DR_TRAP_FIXUP
811 V86_kit3: V86_TRAP_FIXUP
813 /* Push error code */
820 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
823 /* Check if the frame was from kernelmode */
824 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
827 /* Check the old mode */
828 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
832 /* Enable interrupts for user-mode */
836 /* Prepare the exception */
841 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
842 mov ebx, [ebp+KTRAP_FRAME_EIP]
844 mov eax, STATUS_BREAKPOINT
846 call _CommonDispatchException
849 /* Check if this is a VDM process */
850 mov ebx, [fs:KPCR_CURRENT_THREAD]
851 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
852 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
855 /* We don't support VDM! */
860 Dr_kit4: DR_TRAP_FIXUP
861 V86_kit4: V86_TRAP_FIXUP
863 /* Push error code */
870 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
873 /* Check if the frame was from kernelmode */
874 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
877 /* Check the old mode */
878 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
882 /* Re-enable interrupts for user-mode and send the exception */
884 mov eax, STATUS_INTEGER_OVERFLOW
885 mov ebx, [ebp+KTRAP_FRAME_EIP]
890 /* Check if this is a VDM process */
891 mov ebx, [fs:KPCR_CURRENT_THREAD]
892 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
893 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
896 /* We don't support this yet! */
902 Dr_kit5: DR_TRAP_FIXUP
903 V86_kit5: V86_TRAP_FIXUP
905 /* Push error code */
912 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
915 /* Check if the frame was from kernelmode */
916 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
919 /* It did, and this should never happen */
921 jmp _KiSystemFatalException
923 /* Check the old mode */
925 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
928 /* Re-enable interrupts for user-mode and send the exception */
931 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
932 mov ebx, [ebp+KTRAP_FRAME_EIP]
936 /* Check if this is a VDM process */
937 mov ebx, [fs:KPCR_CURRENT_THREAD]
938 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
939 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
942 /* We don't support this yet! */
948 Dr_kit6: DR_TRAP_FIXUP
949 V86_kit6: V86_TRAP_FIXUP
952 /* It this a V86 GPF? */
953 test dword ptr [esp+8], EFLAGS_V86_MASK
959 /* Not yet supported (Invalid OPCODE from V86) */
963 /* Push error code */
969 /* Check if this happened in kernel mode */
970 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
974 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
977 /* Check if the process is vDM */
978 mov ebx, fs:[KPCR_CURRENT_THREAD]
979 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
980 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
984 /* Get EIP and enable interrupts at this point */
985 mov esi, [ebp+KTRAP_FRAME_EIP]
988 /* Set intruction prefix length */
991 /* Setup a SEH frame */
994 push fs:[KPCR_EXCEPTION_LIST]
995 mov fs:[KPCR_EXCEPTION_LIST], esp
998 /* Get the instruction and check if it's LOCK */
1007 /* Undo SEH frame */
1008 pop fs:[KPCR_EXCEPTION_LIST]
1013 /* Re-enable interrupts */
1016 /* Setup illegal instruction exception and dispatch it */
1017 mov ebx, [ebp+KTRAP_FRAME_EIP]
1018 mov eax, STATUS_ILLEGAL_INSTRUCTION
1019 jmp _DispatchNoParam
1023 /* Undo SEH Frame */
1024 pop fs:[KPCR_EXCEPTION_LIST]
1027 /* Setup invalid lock exception and dispatch it */
1028 mov ebx, [ebp+KTRAP_FRAME_EIP]
1029 mov eax, STATUS_INVALID_LOCK_SEQUENCE
1030 jmp _DispatchNoParam
1037 /* Return to caller */
1038 jmp _Kei386EoiHelper@0
1044 pop fs:[KPCR_EXCEPTION_LIST]
1048 /* Check if this was user mode */
1049 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1058 push KMODE_EXCEPTION_NOT_HANDLED
1059 call _KeBugCheckWithTf@24
1063 Dr_kit7: DR_TRAP_FIXUP
1064 V86_kit7: V86_TRAP_FIXUP
1066 /* Push error code */
1072 /* Get the current thread and stack */
1074 mov eax, [fs:KPCR_CURRENT_THREAD]
1075 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1076 sub ecx, NPX_FRAME_LENGTH
1078 /* Check if emulation is enabled */
1079 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1080 jnz EmulationEnabled
1083 /* Check if the NPX state is loaded */
1084 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1089 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1092 /* Check the NPX thread */
1093 mov edx, [fs:KPCR_NPX_THREAD]
1097 /* Get the NPX Stack */
1098 mov esi, [edx+KTHREAD_INITIAL_STACK]
1099 sub esi, NPX_FRAME_LENGTH
1101 /* Check if we have FXSR and check which operand to use */
1102 test byte ptr _KeI386FxsrPresent, 1
1111 /* Set the thread's state to dirty */
1112 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1115 /* Check if we have FXSR and choose which operand to use */
1116 test byte ptr _KeI386FxsrPresent, 1
1125 /* Set state loaded */
1126 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1127 mov [fs:KPCR_NPX_THREAD], eax
1129 /* Enable interrupts to happen now */
1133 /* Check if CR0 needs to be reloaded due to a context switch */
1134 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1135 jz _Kei386EoiHelper@0
1137 /* We have to reload CR0... disable interrupts */
1140 /* Get CR0 and update it */
1142 or ebx, [ecx+FN_CR0_NPX_STATE]
1145 /* Restore interrupts and check if TS is back on */
1148 jz _Kei386EoiHelper@0
1150 /* Clear TS, and loop handling again */
1157 /* Set delayed error */
1158 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1160 /* Check if this happened during restore */
1161 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1164 /* Skip instruction and dispatch the exception */
1165 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1166 jmp _Kei386EoiHelper@0
1169 /* Check if TS is set */
1171 jnz TsSetOnLoadedState
1174 /* Check if the trap came from V86 mode */
1175 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1178 /* Check if it came from kernel mode */
1179 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1182 /* Check if it came from a VDM */
1183 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1187 /* Get the current thread */
1188 mov eax, fs:[KPCR_CURRENT_THREAD]
1190 /* Check NPX state */
1191 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1193 /* Get the NPX save area */
1194 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1195 lea ecx, [ecx-NPX_FRAME_LENGTH]
1202 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1205 /* Check if we have FX support */
1206 test byte ptr _KeI386FxsrPresent, 1
1209 /* Save the state */
1217 /* Make CR0 state not loaded */
1218 or ebx, NPX_STATE_NOT_LOADED
1219 or ebx, [ecx+FN_CR0_NPX_STATE]
1222 /* Update NPX state */
1223 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1224 mov dword ptr fs:[KPCR_NPX_THREAD], 0
1227 /* Clear the TS bit and re-enable interrupts */
1228 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1231 /* Check if we have FX support */
1232 test byte ptr _KeI386FxsrPresent, 1
1235 /* Get error offset, control and status words */
1236 mov ebx, [ecx+FX_ERROR_OFFSET]
1237 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1238 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1240 /* Get the faulting opcode */
1241 mov esi, [ecx+FX_DATA_OFFSET]
1245 /* Get error offset, control and status words */
1246 mov ebx, [ecx+FP_ERROR_OFFSET]
1247 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1248 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1250 /* Get the faulting opcode */
1251 mov esi, [ecx+FP_DATA_OFFSET]
1254 /* Mask exceptions */
1259 /* Check if what's left is invalid */
1263 /* Check if it was a stack fault */
1267 /* Raise exception */
1268 mov eax, STATUS_FLOAT_INVALID_OPERATION
1269 jmp _DispatchOneParam
1273 /* Raise exception */
1274 mov eax, STATUS_FLOAT_STACK_CHECK
1275 jmp _DispatchTwoParam
1279 /* Check for divide by 0 */
1283 /* Raise exception */
1284 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1285 jmp _DispatchOneParam
1288 /* Check for denormal */
1292 /* Raise exception */
1293 mov eax, STATUS_FLOAT_INVALID_OPERATION
1294 jmp _DispatchOneParam
1297 /* Check for overflow */
1301 /* Raise exception */
1302 mov eax, STATUS_FLOAT_OVERFLOW
1303 jmp _DispatchOneParam
1306 /* Check for underflow */
1310 /* Raise exception */
1311 mov eax, STATUS_FLOAT_UNDERFLOW
1312 jmp _DispatchOneParam
1315 /* Check for precision fault */
1319 /* Raise exception */
1320 mov eax, STATUS_FLOAT_INEXACT_RESULT
1321 jmp _DispatchOneParam
1325 /* Strange result, bugcheck the OS */
1332 push TRAP_CAUSE_UNKNOWN
1333 call _KeBugCheckWithTf@24
1336 /* Check if this is a VDM */
1337 mov eax, fs:[KPCR_CURRENT_THREAD]
1338 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1339 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1342 /* V86 NPX not handled */
1346 /* Did this come from kernel-mode? */
1347 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1350 /* It came from user-mode, so this would only be valid inside a VDM */
1351 /* Since we don't actually have VDMs in ROS, bugcheck. */
1355 /* TS shouldn't be set, unless this we don't have a Math Processor */
1359 /* Strange that we got a trap at all, but ignore and continue */
1361 jmp _Kei386EoiHelper@0
1364 /* Cause a bugcheck */
1369 push TRAP_CAUSE_UNKNOWN
1370 call _KeBugCheckEx@20
1377 /* Can't really do too much */
1379 jmp _KiSystemFatalException
1383 Dr_kit9: DR_TRAP_FIXUP
1384 V86_kit9: V86_TRAP_FIXUP
1386 /* Push error code */
1392 /* Enable interrupts and bugcheck */
1395 jmp _KiSystemFatalException
1399 Dr_kit10: DR_TRAP_FIXUP
1400 V86_kit10: V86_TRAP_FIXUP
1406 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1409 /* Check if the frame was from kernelmode */
1410 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1414 /* Check if OF was set during iretd */
1415 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1419 /* It was, just mask it out */
1420 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1421 jmp _Kei386EoiHelper@0
1424 /* TSS failure for some other reason: crash */
1426 jmp _KiSystemFatalException
1430 Dr_kit11: DR_TRAP_FIXUP
1431 V86_kit11: V86_TRAP_FIXUP
1436 /* FIXME: ROS Doesn't handle segment faults yet */
1438 jmp _KiSystemFatalException
1442 Dr_kit12: DR_TRAP_FIXUP
1443 V86_kit12: V86_TRAP_FIXUP
1448 /* FIXME: ROS Doesn't handle stack faults yet */
1450 jmp _KiSystemFatalException
1453 .func KiTrapExceptHandler
1454 _KiTrapExceptHandler:
1456 /* Setup SEH handler frame */
1458 pop fs:[KPCR_EXCEPTION_LIST]
1462 /* Check if the fault came from user mode */
1463 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1466 /* Kernel fault, bugcheck */
1472 push KMODE_EXCEPTION_NOT_HANDLED
1473 call _KeBugCheckWithTf@24
1477 Dr_kitd: DR_TRAP_FIXUP
1478 V86_kitd: V86_TRAP_FIXUP
1481 /* It this a V86 GPF? */
1482 test dword ptr [esp+12], EFLAGS_V86_MASK
1485 /* Enter V86 Trap */
1486 V86_TRAP_PROLOG kitd
1488 /* Make sure that this is a V86 process */
1489 mov ecx, [fs:KPCR_CURRENT_THREAD]
1490 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1491 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1494 /* Otherwise, something is very wrong, raise an exception */
1496 mov ebx, [ebp+KTRAP_FRAME_EIP]
1498 mov eax, STATUS_ACCESS_VIOLATION
1499 jmp _DispatchTwoParam
1503 /* Go to APC level */
1507 /* Save old IRQL and enable interrupts */
1511 /* Handle the opcode */
1512 call _Ki386HandleOpcodeV86@0
1514 /* Check if this was VDM */
1523 /* Lower IRQL and disable interrupts */
1528 /* Check if this was a V86 trap */
1529 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1532 /* Exit the V86 Trap */
1537 /* Either this wasn't V86, or it was, but an APC interrupted us */
1538 jmp _Kei386EoiHelper@0
1544 /* Check if this was from kernel-mode */
1545 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1548 /* Check if we have a VDM alert */
1549 cmp dword ptr fs:[KPCR_VDM_ALERT], 0
1552 /* Check for GPF during GPF */
1553 mov eax, [ebp+KTRAP_FRAME_EIP]
1554 cmp eax, offset CheckPrivilegedInstruction
1556 cmp eax, offset CheckPrivilegedInstruction2
1561 /* Get the opcode and trap frame */
1563 mov eax, [ebp+KTRAP_FRAME_EIP]
1565 mov edx, [ebp+KTRAP_FRAME_EBP]
1567 /* We want to check if this was POP [DS/ES/FS/GS] */
1568 add edx, KTRAP_FRAME_DS
1571 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1574 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1577 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1581 /* It isn't, was it IRETD? */
1585 /* Get error code */
1586 lea edx, [ebp+KTRAP_FRAME_ESP]
1587 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1591 mov cx, word ptr [edx+4]
1596 /* This should be a Ki386CallBios return */
1597 mov eax, offset _Ki386BiosCallReturnAddress
1601 cmp ax, KGDT_R0_CODE + RPL_MASK
1604 /* Jump to return address */
1605 jmp _Ki386BiosCallReturnAddress
1608 /* Check if the thread was in kernel mode */
1609 mov ebx, [fs:KPCR_CURRENT_THREAD]
1610 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1613 /* Set RPL_MASK for check below */
1614 or word ptr [edx+4], RPL_MASK
1617 /* Check if the IRET goes to user-mode */
1618 test dword ptr [edx+4], RPL_MASK
1621 /* Setup trap frame to copy */
1622 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1623 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1627 /* Copy each field */
1633 /* Enable interrupts and adjust stack */
1638 /* Setup exception record */
1639 mov ebx, [ebp+KTRAP_FRAME_EIP]
1640 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1642 mov eax, STATUS_ACCESS_VIOLATION
1643 jmp _DispatchTwoParam
1647 /* FIXME: Handle RDMSR/WRMSR */
1652 /* Check if this was an MSR opcode */
1656 /* Check if DS is Ring 3 */
1657 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1660 /* Otherwise, fix it up */
1661 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1666 /* Check if ES is Ring 3 */
1667 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1670 /* Otherwise, fix it up */
1671 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1677 lea eax, [ebp+KTRAP_FRAME_ESP]
1682 /* Handle segment POP fault by setting it to 0 */
1685 mov dword ptr [edx], eax
1689 /* Do a trap exit */
1690 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1694 /* If the previous mode was kernel, raise a fatal exception */
1696 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1697 jz _KiSystemFatalException
1699 /* Get the process and check which CS this came from */
1700 mov ebx, fs:[KPCR_CURRENT_THREAD]
1701 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
1702 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1705 /* Check if this is a VDM */
1706 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1709 /* Enable interrupts and check if we have an error code */
1711 cmp word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
1713 jmp CheckPrivilegedInstruction
1716 /* Update EIP (will be updated below again) */
1717 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1720 /* Clear the segment, update EIP and ESP */
1721 mov dword ptr [edx], 0
1722 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1723 add dword ptr [ebp+KTRAP_FRAME_ESP], 4
1724 jmp _Kei386EoiHelper@0
1727 /* Check if this is a VDM */
1728 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1729 jz CheckPrivilegedInstruction
1731 /* Bring interrupts back */
1734 /* Check what kind of instruction this is */
1735 mov eax, [ebp+KTRAP_FRAME_EIP]
1738 /* FIXME: Check for BOP4 */
1740 /* Check if this is POP ES */
1742 add edx, KTRAP_FRAME_ES
1746 /* Check if this is POP FS */
1747 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1751 /* Check if this is POP GS */
1752 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1756 CheckPrivilegedInstruction:
1757 /* Bring interrupts back */
1760 /* Setup a SEH handler */
1762 push offset _KiTrapExceptHandler
1763 push fs:[KPCR_EXCEPTION_LIST]
1764 mov fs:[KPCR_EXCEPTION_LIST], esp
1767 mov esi, [ebp+KTRAP_FRAME_EIP]
1769 /* Setup loop count */
1773 /* Save loop count */
1776 /* Get the instruction */
1779 /* Now lookup in the prefix table */
1781 mov edi, offset _KiTrapPrefixTable
1784 /* Restore loop count */
1787 /* If it's not a prefix byte, check other instructions */
1794 /* FIXME: Check if it's a HLT */
1796 /* Check if the instruction has two bytes */
1804 /* Get EFLAGS and IOPL */
1805 mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
1809 /* Check the CS's RPL mask */
1810 mov ecx, [ebp+KTRAP_FRAME_CS]
1815 CheckPrivilegedInstruction2:
1816 /* Check if this is a CLI or STI */
1818 je IsPrivInstruction
1820 je IsPrivInstruction
1822 /* Setup I/O table lookup */
1824 mov edi, offset _KiTrapIoTable
1826 /* Loopup in the table */
1830 /* FIXME: Check IOPM!!! */
1833 /* Cleanup the SEH frame */
1834 pop fs:[KPCR_EXCEPTION_LIST]
1837 /* Setup the exception */
1838 mov ebx, [ebp+KTRAP_FRAME_EIP]
1839 mov eax, STATUS_PRIVILEGED_INSTRUCTION
1840 jmp _DispatchNoParam
1843 /* Cleanup the SEH frame */
1844 pop fs:[KPCR_EXCEPTION_LIST]
1848 /* Setup the exception */
1849 mov ebx, [ebp+KTRAP_FRAME_EIP]
1851 mov eax, STATUS_ACCESS_VIOLATION
1852 jmp _DispatchTwoParam
1860 Dr_kit14: DR_TRAP_FIXUP
1861 V86_kit14: V86_TRAP_FIXUP
1867 /* Check if we have a VDM alert */
1868 cmp dword ptr fs:[KPCR_VDM_ALERT], 0
1871 /* Get the current thread */
1872 mov edi, fs:[KPCR_CURRENT_THREAD]
1874 /* Get the stack address of the frame */
1875 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1876 sub eax, [edi+KTHREAD_INITIAL_STACK]
1879 /* This isn't the base frame, check if it's the second */
1880 cmp eax, -KTRAP_FRAME_EFLAGS
1883 /* Check if we have a TEB */
1884 mov eax, fs:[KPCR_TEB]
1888 /* Fixup the frame */
1895 /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
1896 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1899 /* Enable interrupts and check if we got here with interrupts disabled */
1901 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1905 /* Send trap frame and check if this is kernel-mode or usermode */
1907 mov eax, [ebp+KTRAP_FRAME_CS]
1911 /* Send faulting address and check if this is read or write */
1913 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1917 /* Call the access fault handler */
1918 call _MmAccessFault@16
1922 /* Access fault handled, return to caller */
1923 jmp _Kei386EoiHelper@0
1926 /* First check if this is a fault in the S-LIST functions */
1927 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1928 cmp [ebp+KTRAP_FRAME_EIP], ecx
1931 /* Check if this is a fault in the syscall handler */
1932 mov ecx, offset CopyParams
1933 cmp [ebp+KTRAP_FRAME_EIP], ecx
1935 mov ecx, offset ReadBatch
1936 cmp [ebp+KTRAP_FRAME_EIP], ecx
1941 jmp _Kei386EoiHelper@0
1946 jmp _Kei386EoiHelper@0
1948 /* Check if the fault occured in a V86 mode */
1950 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
1953 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1956 /* Check if the fault occured in a VDM */
1957 mov esi, fs:[KPCR_CURRENT_THREAD]
1958 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
1959 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
1962 /* Check if we this was in kernel-mode */
1963 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1965 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1972 /* Save EIP and check what kind of status failure we got */
1974 mov esi, [ebp+KTRAP_FRAME_EIP]
1975 cmp eax, STATUS_ACCESS_VIOLATION
1977 cmp eax, STATUS_GUARD_PAGE_VIOLATION
1979 cmp eax, STATUS_STACK_OVERFLOW
1982 /* Setup an in-page exception to dispatch */
1988 mov eax, STATUS_IN_PAGE_ERROR
1989 call _CommonDispatchException
1992 /* Use more proper status code */
1993 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
1996 /* Setup a normal page fault exception */
2000 jmp _DispatchTwoParam
2008 /* This is completely illegal, bugcheck the system */
2014 push IRQL_NOT_LESS_OR_EQUAL
2015 call _KeBugCheckWithTf@24
2019 /* FIXME: NOT SUPPORTED */
2024 Dr_kit15: DR_TRAP_FIXUP
2025 V86_kit15: V86_TRAP_FIXUP
2027 /* Push error code */
2034 /* Raise a fatal exception */
2036 jmp _KiSystemFatalException
2040 Dr_kit16: DR_TRAP_FIXUP
2041 V86_kit16: V86_TRAP_FIXUP
2043 /* Push error code */
2049 /* Check if this is the NPX Thread */
2050 mov eax, fs:[KPCR_CURRENT_THREAD]
2051 cmp eax, fs:[KPCR_NPX_THREAD]
2053 /* Get the initial stack and NPX frame */
2054 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2055 lea ecx, [ecx-NPX_FRAME_LENGTH]
2057 /* If this is a valid fault, handle it */
2060 /* Otherwise, re-enable interrupts and set delayed error */
2062 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
2063 jmp _Kei386EoiHelper@0
2067 Dr_kit17: DR_TRAP_FIXUP
2068 V86_kit17: V86_TRAP_FIXUP
2070 /* Push error code */
2076 /* FIXME: ROS Doesn't handle alignment faults yet */
2078 jmp _KiSystemFatalException
2081 .func KiSystemFatalException
2082 _KiSystemFatalException:
2084 /* Push the trap frame */
2087 /* Push empty parameters */
2092 /* Push trap number and bugcheck code */
2094 push UNEXPECTED_KERNEL_MODE_TRAP
2095 call _KeBugCheckWithTf@24
2099 .func KiCoprocessorError@0
2100 _KiCoprocessorError@0:
2102 /* Get the NPX Thread's Initial stack */
2103 mov eax, [fs:KPCR_NPX_THREAD]
2104 mov eax, [eax+KTHREAD_INITIAL_STACK]
2106 /* Make space for the FPU Save area */
2107 sub eax, SIZEOF_FX_SAVE_AREA
2109 /* Set the CR0 State */
2110 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
2117 /* Return to caller */
2121 .func Ki16BitStackException
2122 _Ki16BitStackException:
2128 /* Go to kernel mode thread stack */
2129 mov eax, fs:[KPCR_CURRENT_THREAD]
2130 add esp, [eax+KTHREAD_INITIAL_STACK]
2132 /* Switch to good stack segment */
2136 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
2138 .globl _KiStartUnexpectedRange@0
2139 _KiStartUnexpectedRange@0:
2141 GENERATE_INT_HANDLERS
2143 .globl _KiEndUnexpectedRange@0
2144 _KiEndUnexpectedRange@0:
2145 jmp _KiUnexpectedInterruptTail
2147 .func KiUnexpectedInterruptTail
2148 V86_kui: V86_TRAP_FIXUP
2149 Dr_kui: DR_TRAP_FIXUP
2150 _KiUnexpectedInterruptTail:
2152 /* Enter interrupt trap */
2153 INT_PROLOG kui, DoNotPushFakeErrorCode
2155 /* Increase interrupt count */
2156 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2158 /* Put vector in EBX and make space for KIRQL */
2162 /* Begin interrupt */
2166 call _HalBeginSystemInterrupt@12
2168 /* Check if it was spurious or not */
2172 /* Spurious, ignore it */
2174 jmp _Kei386EoiHelper2ndEntry
2177 /* Unexpected interrupt, print a message on debug builds */
2180 push offset _UnexpectedMsg
2185 /* Exit the interrupt */
2188 call _HalEndSystemInterrupt@8
2189 jmp _Kei386EoiHelper@0
2192 .globl _KiUnexpectedInterrupt
2193 _KiUnexpectedInterrupt:
2195 /* Bugcheck with invalid interrupt code */
2196 push TRAP_CAUSE_UNKNOWN
2199 /* INTERRUPT HANDLERS ********************************************************/
2201 .func KiDispatchInterrupt@0
2202 _KiDispatchInterrupt@0:
2204 /* Get the PCR and disable interrupts */
2205 mov ebx, [fs:KPCR_SELF]
2208 /* Check if we have to deliver DPCs, timers, or deferred threads */
2209 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2210 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2211 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2214 /* Save stack pointer and exception list, then clear it */
2216 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2217 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2219 /* Save the stack and switch to the DPC Stack */
2221 //mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2225 mov ecx, [ebx+KPCR_PRCB]
2226 call @KiRetireDpcList@4
2228 /* Restore stack and exception list */
2235 /* Re-enable interrupts */
2238 /* Check if we have quantum end */
2239 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2242 /* Check if we have a thread to swap to */
2243 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2246 /* FIXME: Schedule new thread */
2254 /* Disable quantum end and process it */
2255 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2256 call _KiQuantumEnd@0
2260 .func KiInterruptTemplate
2261 V86_kit: V86_TRAP_FIXUP
2262 Dr_kit: DR_TRAP_FIXUP
2263 _KiInterruptTemplate:
2265 /* Enter interrupt trap */
2266 INT_PROLOG kit, DoPushFakeErrorCode
2269 _KiInterruptTemplate2ndDispatch:
2270 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2273 _KiInterruptTemplateObject:
2274 /* Dummy jump, will be replaced by the actual jump */
2275 jmp _KeSynchronizeExecution@12
2277 _KiInterruptTemplateDispatch:
2278 /* Marks the end of the template so that the jump above can be edited */
2280 .func KiChainedDispatch2ndLvl@0
2281 _KiChainedDispatch2ndLvl@0:
2283 /* Not yet supported */
2287 .func KiChainedDispatch@0
2288 _KiChainedDispatch@0:
2290 /* Increase interrupt count */
2291 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2293 /* Save trap frame */
2296 /* Save vector and IRQL */
2297 mov eax, [edi+KINTERRUPT_VECTOR]
2298 mov ecx, [edi+KINTERRUPT_IRQL]
2304 /* Begin interrupt */
2308 call _HalBeginSystemInterrupt@12
2310 /* Check if it was handled */
2314 /* Call the 2nd-level handler */
2315 call _KiChainedDispatch2ndLvl@0
2317 /* Exit the interrupt */
2320 call _HalEndSystemInterrupt@8
2321 jmp _Kei386EoiHelper@0
2324 .func KiInterruptDispatch@0
2325 _KiInterruptDispatch@0:
2327 /* Increase interrupt count */
2328 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2330 /* Save trap frame */
2333 /* Save vector and IRQL */
2334 mov eax, [edi+KINTERRUPT_VECTOR]
2335 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2341 /* Begin interrupt */
2345 call _HalBeginSystemInterrupt@12
2347 /* Check if it was handled */
2351 /* Acquire the lock */
2353 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2354 ACQUIRE_SPINLOCK(esi, IntSpin)
2357 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2360 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2362 /* Release the lock */
2363 RELEASE_SPINLOCK(esi)
2365 /* Exit the interrupt */
2367 call _HalEndSystemInterrupt@8
2368 jmp _Kei386EoiHelper@0
2371 /* Exit the interrupt */
2373 jmp _Kei386EoiHelper@0
2377 SPIN_ON_LOCK esi, GetIntLock