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
16 #define WrDispatchInt 0x1F
18 /* GLOBALS *******************************************************************/
23 /* This is the Software Interrupt Table that we handle in this file: */
24 idt _KiTrap0, INT_32_DPL0 /* INT 00: Divide Error (#DE) */
25 idt _KiTrap1, INT_32_DPL0 /* INT 01: Debug Exception (#DB) */
26 idt _KiTrap2, INT_32_DPL0 /* INT 02: NMI Interrupt */
27 idt _KiTrap3, INT_32_DPL3 /* INT 03: Breakpoint Exception (#BP) */
28 idt _KiTrap4, INT_32_DPL3 /* INT 04: Overflow Exception (#OF) */
29 idt _KiTrap5, INT_32_DPL0 /* INT 05: BOUND Range Exceeded (#BR) */
30 idt _KiTrap6, INT_32_DPL0 /* INT 06: Invalid Opcode Code (#UD) */
31 idt _KiTrap7, INT_32_DPL0 /* INT 07: Device Not Available (#NM) */
32 idt _KiTrap8, INT_32_DPL0 /* INT 08: Double Fault Exception (#DF) */
33 idt _KiTrap9, INT_32_DPL0 /* INT 09: RESERVED */
34 idt _KiTrap10, INT_32_DPL0 /* INT 0A: Invalid TSS Exception (#TS) */
35 idt _KiTrap11, INT_32_DPL0 /* INT 0B: Segment Not Present (#NP) */
36 idt _KiTrap12, INT_32_DPL0 /* INT 0C: Stack Fault Exception (#SS) */
37 idt _KiTrap13, INT_32_DPL0 /* INT 0D: General Protection (#GP) */
38 idt _KiTrap14, INT_32_DPL0 /* INT 0E: Page-Fault Exception (#PF) */
39 idt _KiTrap0F, INT_32_DPL0 /* INT 0F: RESERVED */
40 idt _KiTrap16, INT_32_DPL0 /* INT 10: x87 FPU Error (#MF) */
41 idt _KiTrap17, INT_32_DPL0 /* INT 11: Align Check Exception (#AC) */
42 idt _KiTrap0F, INT_32_DPL0 /* INT 12: Machine Check Exception (#MC)*/
43 idt _KiTrap0F, INT_32_DPL0 /* INT 13: SIMD FPU Exception (#XF) */
45 idt _KiTrap0F, INT_32_DPL0 /* INT 14-29: UNDEFINED INTERRUPTS */
47 idt _KiGetTickCount, INT_32_DPL3 /* INT 2A: Get Tick Count Handler */
48 idt _KiCallbackReturn, INT_32_DPL3 /* INT 2B: User-Mode Callback Return */
49 idt _KiRaiseAssertion, INT_32_DPL3 /* INT 2C: Debug Assertion Handler */
50 idt _KiDebugService, INT_32_DPL3 /* INT 2D: Debug Service Handler */
51 idt _KiSystemService, INT_32_DPL3 /* INT 2E: System Call Service Handler */
52 idt _KiTrap0F, INT_32_DPL0 /* INT 2F: RESERVED */
53 GENERATE_IDT_STUBS /* INT 30-FF: UNEXPECTED INTERRUPTS */
55 /* System call entrypoints: */
56 .globl _KiFastCallEntry
57 .globl _KiSystemService
59 /* And special system-defined software traps: */
60 .globl _NtRaiseException@12
62 .globl _KiCoprocessorError@0
63 .globl _KiDispatchInterrupt@0
65 /* Interrupt template entrypoints */
66 .globl _KiInterruptTemplate
67 .globl _KiInterruptTemplateObject
68 .globl _KiInterruptTemplateDispatch
70 /* Chained and Normal generic interrupt handlers for 1st and 2nd level entry*/
71 .globl _KiChainedDispatch2ndLvl@0
72 .globl _KiInterruptDispatch@0
73 .globl _KiChainedDispatch@0
75 /* We implement the following trap exit points: */
76 .globl _KiServiceExit /* Exit from syscall */
77 .globl _KiServiceExit2 /* Exit from syscall with complete frame*/
78 .globl _Kei386EoiHelper@0 /* Exit from interrupt or H/W trap */
79 .globl _Kei386EoiHelper2ndEntry /* Exit from unexpected interrupt */
81 .globl _KiIdtDescriptor
86 .globl _KiUnexpectedEntrySize
87 _KiUnexpectedEntrySize:
88 .long _KiUnexpectedInterrupt1 - _KiUnexpectedInterrupt0
91 .asciz "\n\x7\x7!!! Unexpected Interrupt %02lx !!!\n"
94 .asciz "\n\x7\x7!!! Unhandled or Unexpected Code at line: %lx!!!\n"
97 .asciz "\n*** ISR at %lx took over .5 second\n"
100 .asciz "\n*** ISR at %lx appears to have an interrupt storm\n"
104 .byte 0xF3 /* REP INS/OUTS */
105 .byte 0x67 /* ADDR */
106 .byte 0xF0 /* LOCK */
126 .byte 0x6E /* OUTS */
127 .byte 0x6F /* OUTS */
129 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
138 .func KiSystemService
139 TRAP_FIXUPS kss_a, kss_t, DoNotFixupV86, DoNotFixupAbios
142 /* Enter the shared system call prolog */
143 SYSCALL_PROLOG kss_a, kss_t
145 /* Jump to the actual handler */
149 .func KiFastCallEntry
150 TRAP_FIXUPS FastCallDrSave, FastCallDrReturn, DoNotFixupV86, DoNotFixupAbios
153 /* Enter the fast system call prolog */
154 FASTCALL_PROLOG FastCallDrSave, FastCallDrReturn
159 * Find out which table offset to use. Converts 0x1124 into 0x10.
160 * The offset is related to the Table Index as such: Offset = TableIndex x 10
163 shr edi, SERVICE_TABLE_SHIFT
164 and edi, SERVICE_TABLE_MASK
167 /* Now add the thread's base system table to the offset */
168 add edi, [esi+KTHREAD_SERVICE_TABLE]
170 /* Get the true syscall ID and check it */
172 and eax, SERVICE_NUMBER_MASK
173 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
175 /* Invalid ID, try to load Win32K Table */
176 jnb KiBBTUnexpectedRange
178 /* Check if this was Win32K */
179 cmp ecx, SERVICE_TABLE_TEST
183 mov ecx, PCR[KPCR_TEB]
185 /* Check if we should flush the User Batch */
188 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
194 //call [_KeGdiFlushUserBatch]
199 /* Increase total syscall count */
200 inc dword ptr PCR[KPCR_SYSTEM_CALLS]
203 /* Increase per-syscall count */
204 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
206 inc dword ptr [ecx+eax*4]
209 /* Users's current stack frame pointer is source */
213 /* Allocate room for argument list from kernel stack */
214 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
218 /* Get pointer to function */
219 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
222 /* Allocate space on our stack */
225 /* Set the size of the arguments and the destination */
229 /* Make sure we're within the User Probe Address */
230 cmp esi, _MmUserProbeAddress
234 /* Copy the parameters */
237 /* Do the System Call */
242 /* Make sure the user-mode call didn't return at elevated IRQL */
243 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
245 mov esi, eax /* We need to save the syscall's return val */
246 call _KeGetCurrentIrql@0
249 mov eax, esi /* Restore it */
251 /* Get our temporary current thread pointer for sanity check */
252 mov ecx, PCR[KPCR_CURRENT_THREAD]
254 /* Make sure that we are not attached and that APCs are not disabled */
255 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
258 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
265 /* Deallocate the kernel stack frame */
268 KeReturnFromSystemCall:
270 /* Get the Current Thread */
271 mov ecx, PCR[KPCR_CURRENT_THREAD]
273 /* Restore the old trap frame pointer */
274 mov edx, [ebp+KTRAP_FRAME_EDX]
275 mov [ecx+KTHREAD_TRAP_FRAME], edx
280 /* Disable interrupts */
283 /* Check for, and deliver, User-Mode APCs if needed */
284 CHECK_FOR_APC_DELIVER 1
286 /* Exit and cleanup */
287 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
290 KiBBTUnexpectedRange:
292 /* If this isn't a Win32K call, fail */
293 cmp ecx, SERVICE_TABLE_TEST
296 /* Set up Win32K Table */
299 call _PsConvertToGuiThread@0
301 /* Check return code */
304 /* Restore registers */
308 /* Reset trap frame address */
310 mov [esi+KTHREAD_TRAP_FRAME], ebp
312 /* Try the Call again, if we suceeded */
316 * The Shadow Table should have a special byte table which tells us
317 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
320 /* Get the table limit and base */
321 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
322 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
323 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
325 /* Get the table address and add our index into the array */
327 and eax, SERVICE_NUMBER_MASK
330 /* Find out what we should return */
331 movsx eax, byte ptr [edx]
334 /* Return either 0 or -1, we've set it in EAX */
335 jle KeReturnFromSystemCall
337 /* Set STATUS_INVALID_SYSTEM_SERVICE */
338 mov eax, STATUS_INVALID_SYSTEM_SERVICE
339 jmp KeReturnFromSystemCall
343 /* Invalid System Call */
344 mov eax, STATUS_INVALID_SYSTEM_SERVICE
345 jmp KeReturnFromSystemCall
349 /* Check if this came from kernel-mode */
350 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
352 /* It's fine, go ahead with it */
355 /* Caller sent invalid parameters, fail here */
356 mov eax, STATUS_ACCESS_VIOLATION
361 /* Restore ESP0 stack */
362 mov ecx, PCR[KPCR_TSS]
363 mov esp, ss:[ecx+KTSS_ESP0]
365 /* Generate V86M Stack for Trap 6 */
371 /* Generate interrupt stack for Trap 6 */
372 push KGDT_R3_DATA + RPL_MASK
375 push KGDT_R3_CODE + RPL_MASK
381 /* Save current IRQL */
384 /* Set us at passive */
385 mov dword ptr PCR[KPCR_IRQL], 0
393 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
394 call _KeBugCheckEx@20
398 /* Get the index and APC state */
399 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
400 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
407 push APC_INDEX_MISMATCH
408 call _KeBugCheckEx@20
415 /* Disable interrupts */
418 /* Check for, and deliver, User-Mode APCs if needed */
419 CHECK_FOR_APC_DELIVER 0
421 /* Exit and cleanup */
422 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
425 .func Kei386EoiHelper@0
428 /* Disable interrupts */
431 /* Check for, and deliver, User-Mode APCs if needed */
432 CHECK_FOR_APC_DELIVER 0
434 /* Exit and cleanup */
435 _Kei386EoiHelper2ndEntry:
436 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
440 /* Move to EDX position */
441 add esp, KTRAP_FRAME_EDX
443 /* Restore volatiles */
448 /* Move to non-volatiles */
449 lea esp, [ebp+KTRAP_FRAME_EDI]
455 /* Skip error code and return */
464 TRAP_FIXUPS kids_a, kids_t, DoFixupV86, DoFixupAbios
467 /* Push error code */
471 TRAP_PROLOG kids_a, kids_t
473 /* Increase EIP so we skip the INT3 */
474 inc dword ptr [ebp+KTRAP_FRAME_EIP]
476 /* Call debug service dispatcher */
477 mov eax, [ebp+KTRAP_FRAME_EAX]
478 mov ecx, [ebp+KTRAP_FRAME_ECX]
479 mov edx, [ebp+KTRAP_FRAME_EDX]
481 /* Jump to INT3 handler */
485 .func NtRaiseException@12
486 _NtRaiseException@12:
488 /* NOTE: We -must- be called by Zw* to have the right frame! */
489 /* Push the stack frame */
492 /* Get the current thread and restore its trap frame */
493 mov ebx, PCR[KPCR_CURRENT_THREAD]
494 mov edx, [ebp+KTRAP_FRAME_EDX]
495 mov [ebx+KTHREAD_TRAP_FRAME], edx
497 /* Set up stack frame */
500 /* Get the Trap Frame in EBX */
503 /* Get the exception list and restore */
504 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
505 mov PCR[KPCR_EXCEPTION_LIST], eax
507 /* Get the parameters */
508 mov edx, [ebp+16] /* Search frames */
509 mov ecx, [ebp+12] /* Context */
510 mov eax, [ebp+8] /* Exception Record */
512 /* Raise the exception */
518 call _KiRaiseException@20
520 /* Restore trap frame in EBP */
524 /* Check the result */
528 /* Restore debug registers too */
535 /* NOTE: We -must- be called by Zw* to have the right frame! */
536 /* Push the stack frame */
539 /* Get the current thread and restore its trap frame */
540 mov ebx, PCR[KPCR_CURRENT_THREAD]
541 mov edx, [ebp+KTRAP_FRAME_EDX]
542 mov [ebx+KTHREAD_TRAP_FRAME], edx
544 /* Set up stack frame */
547 /* Save the parameters */
551 /* Call KiContinue */
557 /* Check if we failed (bad context record) */
561 /* Check if test alert was requested */
562 cmp dword ptr [ebp+12], 0
565 /* Test alert for the thread */
566 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
568 call _KeTestAlertThread@4
571 /* Return to previous context */
582 /* EXCEPTION DISPATCHERS *****************************************************/
584 .func CommonDispatchException
585 _CommonDispatchException:
587 /* Make space for an exception record */
588 sub esp, EXCEPTION_RECORD_LENGTH
591 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
593 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
594 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
595 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
596 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
598 /* Check parameter count */
602 /* Get information */
603 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
610 /* Set the record in ECX and check if this was V86 */
612 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
621 /* Get the caller's CS */
622 mov eax, [ebp+KTRAP_FRAME_CS]
625 /* Check if it was user-mode or kernel-mode */
628 /* Dispatch the exception */
634 call _KiDispatchException@20
638 jmp _Kei386EoiHelper@0
641 .func DispatchNoParam
643 /* Call the common dispatcher */
645 call _CommonDispatchException
648 .func DispatchOneParam
650 /* Call the common dispatcher */
653 call _CommonDispatchException
656 .func DispatchTwoParam
658 /* Call the common dispatcher */
661 call _CommonDispatchException
664 /* HARDWARE TRAP HANDLERS ****************************************************/
669 /* TODO: Routine to fixup a KTRAP_FRAME when faulting from a syscall. */
674 TRAP_FIXUPS kit0_a, kit0_t, DoFixupV86, DoNotFixupAbios
676 /* Push error code */
680 TRAP_PROLOG kit0_a, kit0_t
683 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
686 /* Check if the frame was from kernelmode */
687 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
690 /* Check the old mode */
691 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
695 /* Re-enable interrupts for user-mode and send the exception */
697 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
698 mov ebx, [ebp+KTRAP_FRAME_EIP]
702 /* Check if this is a VDM process */
703 mov ebx, PCR[KPCR_CURRENT_THREAD]
704 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
705 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
708 /* We don't support this yet! */
715 TRAP_FIXUPS kit1_a, kit1_t, DoFixupV86, DoNotFixupAbios
717 /* Push error code */
721 TRAP_PROLOG kit1_a, kit1_t
724 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
727 /* Check if the frame was from kernelmode */
728 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
731 /* Check the old mode */
732 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
736 /* Enable interrupts for user-mode */
740 /* Prepare the exception */
741 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
742 mov ebx, [ebp+KTRAP_FRAME_EIP]
743 mov eax, STATUS_SINGLE_STEP
747 /* Check if this is a VDM process */
748 mov ebx, PCR[KPCR_CURRENT_THREAD]
749 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
750 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
753 /* We don't support VDM! */
761 /* FIXME: This is an NMI, nothing like a normal exception */
763 jmp _KiSystemFatalException
767 TRAP_FIXUPS kit3_a, kit3_t, DoFixupV86, DoNotFixupAbios
769 /* Push error code */
773 TRAP_PROLOG kit3_a, kit3_t
775 /* Set status code */
776 mov eax, 0 //STATUS_SUCCESS
780 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
783 /* Check if the frame was from kernelmode */
784 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
787 /* Check the old mode */
788 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
792 /* Enable interrupts for user-mode */
797 /* Prepare the exception */
802 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
803 mov ebx, [ebp+KTRAP_FRAME_EIP]
806 mov eax, STATUS_BREAKPOINT
807 call _CommonDispatchException
810 /* Check if this is a VDM process */
811 mov ebx, PCR[KPCR_CURRENT_THREAD]
812 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
813 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
816 /* We don't support VDM! */
821 TRAP_FIXUPS kit4_a, kit4_t, DoFixupV86, DoNotFixupAbios
823 /* Push error code */
827 TRAP_PROLOG kit4_a, kit4_t
830 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
833 /* Check if the frame was from kernelmode */
834 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
837 /* Check the old mode */
838 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
842 /* Re-enable interrupts for user-mode and send the exception */
844 mov eax, STATUS_INTEGER_OVERFLOW
845 mov ebx, [ebp+KTRAP_FRAME_EIP]
850 /* Check if this is a VDM process */
851 mov ebx, PCR[KPCR_CURRENT_THREAD]
852 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
853 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
856 /* We don't support this yet! */
862 TRAP_FIXUPS kit5_a, kit5_t, DoFixupV86, DoNotFixupAbios
864 /* Push error code */
868 TRAP_PROLOG kit5_a, kit5_t
871 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
874 /* Check if the frame was from kernelmode */
875 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
878 /* It did, and this should never happen */
880 jmp _KiSystemFatalException
882 /* Check the old mode */
884 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
887 /* Re-enable interrupts for user-mode and send the exception */
890 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
891 mov ebx, [ebp+KTRAP_FRAME_EIP]
895 /* Check if this is a VDM process */
896 mov ebx, PCR[KPCR_CURRENT_THREAD]
897 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
898 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
901 /* We don't support this yet! */
907 TRAP_FIXUPS kit6_a, kit6_t, DoFixupV86, DoNotFixupAbios
910 /* It this a V86 GPF? */
911 test dword ptr [esp+8], EFLAGS_V86_MASK
915 V86_TRAP_PROLOG kit6_a, kit6_v
917 /* Not yet supported (Invalid OPCODE from V86) */
921 /* Push error code */
925 TRAP_PROLOG kit6_a, kit6_t
927 /* Check if this happened in kernel mode */
928 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
932 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
935 /* Check if the process is vDM */
936 mov ebx, PCR[KPCR_CURRENT_THREAD]
937 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
938 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
942 /* Get EIP and enable interrupts at this point */
943 mov esi, [ebp+KTRAP_FRAME_EIP]
946 /* Set intruction prefix length */
949 /* Setup a SEH frame */
952 push PCR[KPCR_EXCEPTION_LIST]
953 mov PCR[KPCR_EXCEPTION_LIST], esp
956 /* Get the instruction and check if it's LOCK */
966 pop PCR[KPCR_EXCEPTION_LIST]
971 /* Re-enable interrupts */
974 /* Setup illegal instruction exception and dispatch it */
975 mov ebx, [ebp+KTRAP_FRAME_EIP]
976 mov eax, STATUS_ILLEGAL_INSTRUCTION
982 pop PCR[KPCR_EXCEPTION_LIST]
985 /* Setup invalid lock exception and dispatch it */
986 mov ebx, [ebp+KTRAP_FRAME_EIP]
987 mov eax, STATUS_INVALID_LOCK_SEQUENCE
995 /* Return to caller */
996 jmp _Kei386EoiHelper@0
1002 pop PCR[KPCR_EXCEPTION_LIST]
1006 /* Check if this was user mode */
1007 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1016 push KMODE_EXCEPTION_NOT_HANDLED
1017 call _KeBugCheckWithTf@24
1021 TRAP_FIXUPS kit7_a, kit7_t, DoFixupV86, DoNotFixupAbios
1023 /* Push error code */
1027 TRAP_PROLOG kit7_a, kit7_t
1029 /* Get the current thread and stack */
1031 mov eax, PCR[KPCR_CURRENT_THREAD]
1032 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1033 sub ecx, NPX_FRAME_LENGTH
1035 /* Check if emulation is enabled */
1036 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1037 jnz EmulationEnabled
1040 /* Check if the NPX state is loaded */
1041 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1046 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1049 /* Check the NPX thread */
1050 mov edx, PCR[KPCR_NPX_THREAD]
1054 /* Get the NPX Stack */
1055 mov esi, [edx+KTHREAD_INITIAL_STACK]
1056 sub esi, NPX_FRAME_LENGTH
1058 /* Check if we have FXSR and check which operand to use */
1059 test byte ptr _KeI386FxsrPresent, 1
1068 /* Set the thread's state to dirty */
1069 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1072 /* Check if we have FXSR and choose which operand to use */
1073 test byte ptr _KeI386FxsrPresent, 1
1082 /* Set state loaded */
1083 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1084 mov PCR[KPCR_NPX_THREAD], eax
1086 /* Enable interrupts to happen now */
1090 /* Check if CR0 needs to be reloaded due to a context switch */
1091 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1092 jz _Kei386EoiHelper@0
1094 /* We have to reload CR0... disable interrupts */
1097 /* Get CR0 and update it */
1099 or ebx, [ecx+FN_CR0_NPX_STATE]
1102 /* Restore interrupts and check if TS is back on */
1105 jz _Kei386EoiHelper@0
1107 /* Clear TS, and loop handling again */
1114 /* Set delayed error */
1115 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1117 /* Check if this happened during restore */
1118 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1121 /* Skip instruction and dispatch the exception */
1122 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1123 jmp _Kei386EoiHelper@0
1126 /* Check if TS is set */
1128 jnz TsSetOnLoadedState
1131 /* Check if the trap came from V86 mode */
1132 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1135 /* Check if it came from kernel mode */
1136 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1139 /* Check if it came from a VDM */
1140 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1144 /* Get the current thread */
1145 mov eax, PCR[KPCR_CURRENT_THREAD]
1147 /* Check NPX state */
1148 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1150 /* Get the NPX save area */
1151 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1152 lea ecx, [ecx-NPX_FRAME_LENGTH]
1159 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1162 /* Check if we have FX support */
1163 test byte ptr _KeI386FxsrPresent, 1
1166 /* Save the state */
1174 /* Make CR0 state not loaded */
1175 or ebx, NPX_STATE_NOT_LOADED
1176 or ebx, [ecx+FN_CR0_NPX_STATE]
1179 /* Update NPX state */
1180 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1181 mov dword ptr PCR[KPCR_NPX_THREAD], 0
1184 /* Clear the TS bit and re-enable interrupts */
1185 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1188 /* Check if we have FX support */
1189 test byte ptr _KeI386FxsrPresent, 1
1192 /* Get error offset, control and status words */
1193 mov ebx, [ecx+FX_ERROR_OFFSET]
1194 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1195 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1197 /* Get the faulting opcode */
1198 mov esi, [ecx+FX_DATA_OFFSET]
1202 /* Get error offset, control and status words */
1203 mov ebx, [ecx+FP_ERROR_OFFSET]
1204 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1205 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1207 /* Get the faulting opcode */
1208 mov esi, [ecx+FP_DATA_OFFSET]
1211 /* Mask exceptions */
1216 /* Check if what's left is invalid */
1220 /* Check if it was a stack fault */
1224 /* Raise exception */
1225 mov eax, STATUS_FLOAT_INVALID_OPERATION
1226 jmp _DispatchOneParam
1230 /* Raise exception */
1231 mov eax, STATUS_FLOAT_STACK_CHECK
1232 jmp _DispatchTwoParam
1236 /* Check for divide by 0 */
1240 /* Raise exception */
1241 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1242 jmp _DispatchOneParam
1245 /* Check for denormal */
1249 /* Raise exception */
1250 mov eax, STATUS_FLOAT_INVALID_OPERATION
1251 jmp _DispatchOneParam
1254 /* Check for overflow */
1258 /* Raise exception */
1259 mov eax, STATUS_FLOAT_OVERFLOW
1260 jmp _DispatchOneParam
1263 /* Check for underflow */
1267 /* Raise exception */
1268 mov eax, STATUS_FLOAT_UNDERFLOW
1269 jmp _DispatchOneParam
1272 /* Check for precision fault */
1276 /* Raise exception */
1277 mov eax, STATUS_FLOAT_INEXACT_RESULT
1278 jmp _DispatchOneParam
1282 /* Strange result, bugcheck the OS */
1289 push TRAP_CAUSE_UNKNOWN
1290 call _KeBugCheckWithTf@24
1293 /* Check if this is a VDM */
1294 mov eax, PCR[KPCR_CURRENT_THREAD]
1295 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1296 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1299 /* V86 NPX not handled */
1303 /* Did this come from kernel-mode? */
1304 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1307 /* It came from user-mode, so this would only be valid inside a VDM */
1308 /* Since we don't actually have VDMs in ROS, bugcheck. */
1312 /* TS shouldn't be set, unless this we don't have a Math Processor */
1316 /* Strange that we got a trap at all, but ignore and continue */
1318 jmp _Kei386EoiHelper@0
1321 /* Cause a bugcheck */
1326 push TRAP_CAUSE_UNKNOWN
1327 call _KeBugCheckEx@20
1334 /* Can't really do too much */
1336 jmp _KiSystemFatalException
1340 TRAP_FIXUPS kit9_a, kit9_t, DoFixupV86, DoNotFixupAbios
1342 /* Push error code */
1346 TRAP_PROLOG kit9_a, kit9_t
1348 /* Enable interrupts and bugcheck */
1351 jmp _KiSystemFatalException
1355 TRAP_FIXUPS kita_a, kita_t, DoFixupV86, DoNotFixupAbios
1358 TRAP_PROLOG kita_a, kita_t
1361 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1364 /* Check if the frame was from kernelmode */
1365 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1369 /* Check if OF was set during iretd */
1370 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1374 /* It was, just mask it out */
1375 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1376 jmp _Kei386EoiHelper@0
1379 /* TSS failure for some other reason: crash */
1381 jmp _KiSystemFatalException
1385 TRAP_FIXUPS kitb_a, kitb_t, DoFixupV86, DoNotFixupAbios
1388 TRAP_PROLOG kitb_a, kitb_t
1390 /* FIXME: ROS Doesn't handle segment faults yet */
1392 jmp _KiSystemFatalException
1396 TRAP_FIXUPS kitc_a, kitc_t, DoFixupV86, DoNotFixupAbios
1399 TRAP_PROLOG kitc_a, kitc_t
1401 /* FIXME: ROS Doesn't handle stack faults yet */
1403 jmp _KiSystemFatalException
1406 .func KiTrapExceptHandler
1407 _KiTrapExceptHandler:
1409 /* Setup SEH handler frame */
1411 pop PCR[KPCR_EXCEPTION_LIST]
1415 /* Check if the fault came from user mode */
1416 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1419 /* Kernel fault, bugcheck */
1425 push KMODE_EXCEPTION_NOT_HANDLED
1426 call _KeBugCheckWithTf@24
1430 TRAP_FIXUPS kitd_a, kitd_t, DoFixupV86, DoNotFixupAbios
1433 /* It this a V86 GPF? */
1434 test dword ptr [esp+12], EFLAGS_V86_MASK
1437 /* Enter V86 Trap */
1438 V86_TRAP_PROLOG kitd_a, kitd_v
1440 /* Make sure that this is a V86 process */
1441 mov ecx, PCR[KPCR_CURRENT_THREAD]
1442 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1443 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1446 /* Otherwise, something is very wrong, raise an exception */
1448 mov ebx, [ebp+KTRAP_FRAME_EIP]
1450 mov eax, STATUS_ACCESS_VIOLATION
1451 jmp _DispatchTwoParam
1455 /* Go to APC level */
1459 /* Save old IRQL and enable interrupts */
1463 /* Handle the opcode */
1464 call _Ki386HandleOpcodeV86@0
1466 /* Check if this was VDM */
1475 /* Lower IRQL and disable interrupts */
1480 /* Check if this was a V86 trap */
1481 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1484 /* Exit the V86 Trap */
1489 /* Either this wasn't V86, or it was, but an APC interrupted us */
1490 jmp _Kei386EoiHelper@0
1494 TRAP_PROLOG kitd_a, kitd_t
1496 /* Check if this was from kernel-mode */
1497 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1500 /* Check if we have a VDM alert */
1501 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1504 /* Check for GPF during GPF */
1505 mov eax, [ebp+KTRAP_FRAME_EIP]
1506 cmp eax, offset CheckPrivilegedInstruction
1508 cmp eax, offset CheckPrivilegedInstruction2
1513 /* Get the opcode and trap frame */
1515 mov eax, [ebp+KTRAP_FRAME_EIP]
1517 mov edx, [ebp+KTRAP_FRAME_EBP]
1519 /* We want to check if this was POP [DS/ES/FS/GS] */
1520 add edx, KTRAP_FRAME_DS
1523 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1526 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1529 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1533 /* It isn't, was it IRETD? */
1537 /* Get error code */
1538 lea edx, [ebp+KTRAP_FRAME_ESP]
1539 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1543 mov cx, word ptr [edx+4]
1548 /* This should be a Ki386CallBios return */
1549 mov eax, offset _Ki386BiosCallReturnAddress
1553 cmp ax, KGDT_R0_CODE + RPL_MASK
1556 /* Jump to return address */
1557 jmp _Ki386BiosCallReturnAddress
1560 /* Check if the thread was in kernel mode */
1561 mov ebx, PCR[KPCR_CURRENT_THREAD]
1562 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1565 /* Set RPL_MASK for check below */
1566 or word ptr [edx+4], RPL_MASK
1569 /* Check if the IRET goes to user-mode */
1570 test dword ptr [edx+4], RPL_MASK
1573 /* Setup trap frame to copy */
1574 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1575 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1579 /* Copy each field */
1585 /* Enable interrupts and adjust stack */
1590 /* Setup exception record */
1591 mov ebx, [ebp+KTRAP_FRAME_EIP]
1592 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1594 mov eax, STATUS_ACCESS_VIOLATION
1595 jmp _DispatchTwoParam
1599 /* FIXME: Handle RDMSR/WRMSR */
1604 /* Check if this was an MSR opcode */
1608 /* Check if DS is Ring 3 */
1609 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1612 /* Otherwise, fix it up */
1613 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1618 /* Check if ES is Ring 3 */
1619 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1622 /* Otherwise, fix it up */
1623 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1629 lea eax, [ebp+KTRAP_FRAME_ESP]
1634 /* Handle segment POP fault by setting it to 0 */
1637 mov dword ptr [edx], eax
1641 /* Do a trap exit */
1642 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1646 /* If the previous mode was kernel, raise a fatal exception */
1648 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1649 jz _KiSystemFatalException
1651 /* Get the process and check which CS this came from */
1652 mov ebx, PCR[KPCR_CURRENT_THREAD]
1653 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
1654 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1657 /* Check if this is a VDM */
1658 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1661 /* Enable interrupts and check if we have an error code */
1663 cmp word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
1665 jmp CheckPrivilegedInstruction
1668 /* Update EIP (will be updated below again) */
1669 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1672 /* Clear the segment, update EIP and ESP */
1673 mov dword ptr [edx], 0
1674 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1675 add dword ptr [ebp+KTRAP_FRAME_ESP], 4
1676 jmp _Kei386EoiHelper@0
1679 /* Check if this is a VDM */
1680 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1681 jz CheckPrivilegedInstruction
1683 /* Bring interrupts back */
1686 /* Check what kind of instruction this is */
1687 mov eax, [ebp+KTRAP_FRAME_EIP]
1690 /* FIXME: Check for BOP4 */
1692 /* Check if this is POP ES */
1694 add edx, KTRAP_FRAME_ES
1698 /* Check if this is POP FS */
1699 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1703 /* Check if this is POP GS */
1704 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1708 CheckPrivilegedInstruction:
1709 /* Bring interrupts back */
1712 /* Setup a SEH handler */
1714 push offset _KiTrapExceptHandler
1715 push PCR[KPCR_EXCEPTION_LIST]
1716 mov PCR[KPCR_EXCEPTION_LIST], esp
1719 mov esi, [ebp+KTRAP_FRAME_EIP]
1721 /* Setup loop count */
1725 /* Save loop count */
1728 /* Get the instruction */
1731 /* Now lookup in the prefix table */
1733 mov edi, offset _KiTrapPrefixTable
1736 /* Restore loop count */
1739 /* If it's not a prefix byte, check other instructions */
1746 /* FIXME: Check if it's a HLT */
1748 /* Check if the instruction has two bytes */
1756 /* Get EFLAGS and IOPL */
1757 mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
1761 /* Check the CS's RPL mask */
1762 mov ecx, [ebp+KTRAP_FRAME_CS]
1767 CheckPrivilegedInstruction2:
1768 /* Check if this is a CLI or STI */
1770 je IsPrivInstruction
1772 je IsPrivInstruction
1774 /* Setup I/O table lookup */
1776 mov edi, offset _KiTrapIoTable
1778 /* Loopup in the table */
1782 /* FIXME: Check IOPM!!! */
1785 /* Cleanup the SEH frame */
1786 pop PCR[KPCR_EXCEPTION_LIST]
1789 /* Setup the exception */
1790 mov ebx, [ebp+KTRAP_FRAME_EIP]
1791 mov eax, STATUS_PRIVILEGED_INSTRUCTION
1792 jmp _DispatchNoParam
1795 /* Cleanup the SEH frame */
1796 pop PCR[KPCR_EXCEPTION_LIST]
1800 /* Setup the exception */
1801 mov ebx, [ebp+KTRAP_FRAME_EIP]
1803 mov eax, STATUS_ACCESS_VIOLATION
1804 jmp _DispatchTwoParam
1812 TRAP_FIXUPS kite_a, kite_t, DoFixupV86, DoNotFixupAbios
1816 TRAP_PROLOG kite_a, kite_t
1818 /* Check if we have a VDM alert */
1819 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1822 /* Get the current thread */
1823 mov edi, PCR[KPCR_CURRENT_THREAD]
1825 /* Get the stack address of the frame */
1826 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1827 sub eax, [edi+KTHREAD_INITIAL_STACK]
1830 /* This isn't the base frame, check if it's the second */
1831 cmp eax, -KTRAP_FRAME_EFLAGS
1834 /* Check if we have a TEB */
1835 mov eax, PCR[KPCR_TEB]
1839 /* Fixup the frame */
1846 /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
1847 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1850 /* Enable interrupts and check if we got here with interrupts disabled */
1852 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1856 /* Send trap frame and check if this is kernel-mode or usermode */
1858 mov eax, [ebp+KTRAP_FRAME_CS]
1862 /* Send faulting address and check if this is read or write */
1864 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1868 /* Call the access fault handler */
1869 call _MmAccessFault@16
1873 /* Access fault handled, return to caller */
1874 jmp _Kei386EoiHelper@0
1877 /* First check if this is a fault in the S-LIST functions */
1878 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1879 cmp [ebp+KTRAP_FRAME_EIP], ecx
1882 /* Check if this is a fault in the syscall handler */
1883 mov ecx, offset CopyParams
1884 cmp [ebp+KTRAP_FRAME_EIP], ecx
1886 mov ecx, offset ReadBatch
1887 cmp [ebp+KTRAP_FRAME_EIP], ecx
1892 jmp _Kei386EoiHelper@0
1897 jmp _Kei386EoiHelper@0
1899 /* Check if the fault occured in a V86 mode */
1901 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
1904 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1907 /* Check if the fault occured in a VDM */
1908 mov esi, PCR[KPCR_CURRENT_THREAD]
1909 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
1910 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
1913 /* Check if we this was in kernel-mode */
1914 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1916 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1923 /* Save EIP and check what kind of status failure we got */
1925 mov esi, [ebp+KTRAP_FRAME_EIP]
1926 cmp eax, STATUS_ACCESS_VIOLATION
1928 cmp eax, STATUS_GUARD_PAGE_VIOLATION
1930 cmp eax, STATUS_STACK_OVERFLOW
1933 /* Setup an in-page exception to dispatch */
1939 mov eax, STATUS_IN_PAGE_ERROR
1940 call _CommonDispatchException
1943 /* Use more proper status code */
1944 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
1947 /* Setup a normal page fault exception */
1951 jmp _DispatchTwoParam
1959 /* This is completely illegal, bugcheck the system */
1965 push IRQL_NOT_LESS_OR_EQUAL
1966 call _KeBugCheckWithTf@24
1970 /* FIXME: NOT SUPPORTED */
1975 TRAP_FIXUPS kitf_a, kitf_t, DoFixupV86, DoNotFixupAbios
1977 /* Push error code */
1981 TRAP_PROLOG kitf_a, kitf_t
1984 /* Raise a fatal exception */
1986 jmp _KiSystemFatalException
1990 TRAP_FIXUPS kit10_a, kit10_t, DoFixupV86, DoNotFixupAbios
1992 /* Push error code */
1996 TRAP_PROLOG kit10_a, kit10_t
1998 /* Check if this is the NPX Thread */
1999 mov eax, PCR[KPCR_CURRENT_THREAD]
2000 cmp eax, PCR[KPCR_NPX_THREAD]
2002 /* Get the initial stack and NPX frame */
2003 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2004 lea ecx, [ecx-NPX_FRAME_LENGTH]
2006 /* If this is a valid fault, handle it */
2009 /* Otherwise, re-enable interrupts and set delayed error */
2011 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
2012 jmp _Kei386EoiHelper@0
2016 TRAP_FIXUPS kit11_a, kit11_t, DoFixupV86, DoNotFixupAbios
2018 /* Push error code */
2022 TRAP_PROLOG kit11_a, kit11_t
2024 /* FIXME: ROS Doesn't handle alignment faults yet */
2026 jmp _KiSystemFatalException
2029 .func KiSystemFatalException
2030 _KiSystemFatalException:
2032 /* Push the trap frame */
2035 /* Push empty parameters */
2040 /* Push trap number and bugcheck code */
2042 push UNEXPECTED_KERNEL_MODE_TRAP
2043 call _KeBugCheckWithTf@24
2047 .func KiCoprocessorError@0
2048 _KiCoprocessorError@0:
2050 /* Get the NPX Thread's Initial stack */
2051 mov eax, PCR[KPCR_NPX_THREAD]
2052 mov eax, [eax+KTHREAD_INITIAL_STACK]
2054 /* Make space for the FPU Save area */
2055 sub eax, SIZEOF_FX_SAVE_AREA
2057 /* Set the CR0 State */
2058 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
2065 /* Return to caller */
2069 .func Ki16BitStackException
2070 _Ki16BitStackException:
2076 /* Go to kernel mode thread stack */
2077 mov eax, PCR[KPCR_CURRENT_THREAD]
2078 add esp, [eax+KTHREAD_INITIAL_STACK]
2080 /* Switch to good stack segment */
2084 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
2086 .globl _KiStartUnexpectedRange@0
2087 _KiStartUnexpectedRange@0:
2089 GENERATE_INT_HANDLERS
2091 .globl _KiEndUnexpectedRange@0
2092 _KiEndUnexpectedRange@0:
2093 jmp _KiUnexpectedInterruptTail
2095 .func KiUnexpectedInterruptTail
2096 TRAP_FIXUPS kui_a, kui_t, DoFixupV86, DoFixupAbios
2097 _KiUnexpectedInterruptTail:
2099 /* Enter interrupt trap */
2100 INT_PROLOG kui_a, kui_t, DoNotPushFakeErrorCode
2102 /* Increase interrupt count */
2103 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2105 /* Put vector in EBX and make space for KIRQL */
2109 /* Begin interrupt */
2113 call _HalBeginSystemInterrupt@12
2115 /* Check if it was spurious or not */
2119 /* Spurious, ignore it */
2121 jmp _Kei386EoiHelper2ndEntry
2124 /* Unexpected interrupt, print a message on debug builds */
2127 push offset _UnexpectedMsg
2132 /* Exit the interrupt */
2135 call _HalEndSystemInterrupt@8
2136 jmp _Kei386EoiHelper@0
2139 .globl _KiUnexpectedInterrupt
2140 _KiUnexpectedInterrupt:
2142 /* Bugcheck with invalid interrupt code */
2143 push TRAP_CAUSE_UNKNOWN
2146 /* INTERRUPT HANDLERS ********************************************************/
2148 .func KiDispatchInterrupt@0
2149 _KiDispatchInterrupt@0:
2151 /* Get the PCR and disable interrupts */
2152 mov ebx, PCR[KPCR_SELF]
2155 /* Check if we have to deliver DPCs, timers, or deferred threads */
2156 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2157 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2158 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2161 /* Save stack pointer and exception list, then clear it */
2163 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2164 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2166 /* Save the stack and switch to the DPC Stack */
2168 mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2172 mov ecx, [ebx+KPCR_PRCB]
2173 call @KiRetireDpcList@4
2175 /* Restore stack and exception list */
2177 pop dword ptr [ebx+KPCR_EXCEPTION_LIST]
2182 /* Re-enable interrupts */
2185 /* Check if we have quantum end */
2186 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2189 /* Check if we have a thread to swap to */
2190 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2193 /* Make space on the stack to save registers */
2199 /* Get the current thread */
2200 mov edi, [ebx+KPCR_CURRENT_THREAD]
2203 #error SMP Interrupt not handled!
2206 /* Get the next thread and clear it */
2207 mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
2208 and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2210 /* Set us as the current running thread */
2211 mov [ebx+KPCR_CURRENT_THREAD], esi
2212 mov byte ptr [esi+KTHREAD_STATE], Running
2213 mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
2215 /* Put thread in ECX and get the PRCB in EDX */
2217 lea edx, [ebx+KPCR_PRCB_DATA]
2218 call @KiQueueReadyThread@8
2220 /* Set APC_LEVEL and do the swap */
2222 call @KiSwapContextInternal@0
2224 /* Restore registers */
2235 /* Disable quantum end and process it */
2236 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2237 call _KiQuantumEnd@0
2241 .func KiInterruptTemplate
2242 _KiInterruptTemplate:
2244 /* Enter interrupt trap */
2245 INT_PROLOG kit_a, kit_t, DoPushFakeErrorCode
2247 _KiInterruptTemplate2ndDispatch:
2248 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2251 _KiInterruptTemplateObject:
2252 /* Dummy jump, will be replaced by the actual jump */
2253 jmp _KeSynchronizeExecution@12
2255 _KiInterruptTemplateDispatch:
2256 /* Marks the end of the template so that the jump above can be edited */
2258 TRAP_FIXUPS kit_a, kit_t, DoFixupV86, DoFixupAbios
2261 .func KiChainedDispatch2ndLvl@0
2262 _KiChainedDispatch2ndLvl@0:
2264 /* Not yet supported */
2268 .func KiChainedDispatch@0
2269 _KiChainedDispatch@0:
2271 /* Increase interrupt count */
2272 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2274 /* Save trap frame */
2277 /* Save vector and IRQL */
2278 mov eax, [edi+KINTERRUPT_VECTOR]
2279 mov ecx, [edi+KINTERRUPT_IRQL]
2285 /* Begin interrupt */
2289 call _HalBeginSystemInterrupt@12
2291 /* Check if it was handled */
2295 /* Call the 2nd-level handler */
2296 call _KiChainedDispatch2ndLvl@0
2298 /* Exit the interrupt */
2302 .func KiInterruptDispatch@0
2303 _KiInterruptDispatch@0:
2305 /* Increase interrupt count */
2306 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2308 /* Save trap frame */
2311 /* Save vector and IRQL */
2312 mov eax, [edi+KINTERRUPT_VECTOR]
2313 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2319 /* Begin interrupt */
2323 call _HalBeginSystemInterrupt@12
2325 /* Check if it was handled */
2329 /* Acquire the lock */
2331 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2332 ACQUIRE_SPINLOCK(esi, IntSpin)
2334 /* Make sure that this interrupt isn't storming */
2337 /* Save the tick count */
2338 mov ebx, _KeTickCount
2341 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2344 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2346 /* Check if the ISR timed out */
2347 add ebx, _KiISRTimeout
2348 cmp _KeTickCount, ebx
2352 /* Release the lock */
2353 RELEASE_SPINLOCK(esi)
2355 /* Exit the interrupt */
2359 /* Exit the interrupt */
2365 SPIN_ON_LOCK esi, GetIntLock
2369 /* Print warning message */
2370 push [edi+KINTERRUPT_SERVICE_ROUTINE]
2371 push offset _IsrTimeoutMsg
2375 /* Break into debugger, then continue */
2379 /* Cleanup verification */
2380 VERIFY_INT_END kid, 0