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
87 .globl _KiUnexpectedEntrySize
88 _KiUnexpectedEntrySize:
89 .long _KiUnexpectedInterrupt1 - _KiUnexpectedInterrupt0
92 .asciz "\n\x7\x7!!! Unexpected Interrupt %02lx !!!\n"
95 .asciz "\n\x7\x7!!! Unhandled or Unexpected Code at line: %lx!!!\n"
98 .asciz "\n*** ISR at %lx took over .5 second\n"
101 .asciz "\n*** ISR at %lx appears to have an interrupt storm\n"
105 .byte 0xF3 /* REP INS/OUTS */
106 .byte 0x67 /* ADDR */
107 .byte 0xF0 /* LOCK */
127 .byte 0x6E /* OUTS */
128 .byte 0x6F /* OUTS */
130 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
139 .func KiSystemService
140 TRAP_FIXUPS kss_a, kss_t, DoNotFixupV86, DoNotFixupAbios
143 /* Enter the shared system call prolog */
144 SYSCALL_PROLOG kss_a, kss_t
146 /* Jump to the actual handler */
150 .func KiFastCallEntry
151 TRAP_FIXUPS FastCallDrSave, FastCallDrReturn, DoNotFixupV86, DoNotFixupAbios
154 /* Enter the fast system call prolog */
155 FASTCALL_PROLOG FastCallDrSave, FastCallDrReturn
160 * Find out which table offset to use. Converts 0x1124 into 0x10.
161 * The offset is related to the Table Index as such: Offset = TableIndex x 10
164 shr edi, SERVICE_TABLE_SHIFT
165 and edi, SERVICE_TABLE_MASK
168 /* Now add the thread's base system table to the offset */
169 add edi, [esi+KTHREAD_SERVICE_TABLE]
171 /* Get the true syscall ID and check it */
173 and eax, SERVICE_NUMBER_MASK
174 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
176 /* Invalid ID, try to load Win32K Table */
177 jnb KiBBTUnexpectedRange
179 /* Check if this was Win32K */
180 cmp ecx, SERVICE_TABLE_TEST
184 mov ecx, PCR[KPCR_TEB]
186 /* Check if we should flush the User Batch */
189 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
195 //call [_KeGdiFlushUserBatch]
200 /* Increase total syscall count */
201 inc dword ptr PCR[KPCR_SYSTEM_CALLS]
204 /* Increase per-syscall count */
205 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
207 inc dword ptr [ecx+eax*4]
210 /* Users's current stack frame pointer is source */
214 /* Allocate room for argument list from kernel stack */
215 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
219 /* Get pointer to function */
220 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
223 /* Allocate space on our stack */
226 /* Set the size of the arguments and the destination */
230 /* Make sure we're within the User Probe Address */
231 cmp esi, _MmUserProbeAddress
235 /* Copy the parameters */
238 /* Do the System Call */
243 /* Make sure the user-mode call didn't return at elevated IRQL */
244 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
246 mov esi, eax /* We need to save the syscall's return val */
247 call _KeGetCurrentIrql@0
250 mov eax, esi /* Restore it */
252 /* Get our temporary current thread pointer for sanity check */
253 mov ecx, PCR[KPCR_CURRENT_THREAD]
255 /* Make sure that we are not attached and that APCs are not disabled */
256 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
259 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
266 /* Deallocate the kernel stack frame */
269 KeReturnFromSystemCall:
271 /* Get the Current Thread */
272 mov ecx, PCR[KPCR_CURRENT_THREAD]
274 /* Restore the old trap frame pointer */
275 mov edx, [ebp+KTRAP_FRAME_EDX]
276 mov [ecx+KTHREAD_TRAP_FRAME], edx
281 /* Disable interrupts */
284 /* Check for, and deliver, User-Mode APCs if needed */
285 CHECK_FOR_APC_DELIVER 1
287 /* Exit and cleanup */
288 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
291 KiBBTUnexpectedRange:
293 /* If this isn't a Win32K call, fail */
294 cmp ecx, SERVICE_TABLE_TEST
297 /* Set up Win32K Table */
300 call _PsConvertToGuiThread@0
302 /* Check return code */
305 /* Restore registers */
309 /* Reset trap frame address */
311 mov [esi+KTHREAD_TRAP_FRAME], ebp
313 /* Try the Call again, if we suceeded */
317 * The Shadow Table should have a special byte table which tells us
318 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
321 /* Get the table limit and base */
322 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
323 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
324 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
326 /* Get the table address and add our index into the array */
328 and eax, SERVICE_NUMBER_MASK
331 /* Find out what we should return */
332 movsx eax, byte ptr [edx]
335 /* Return either 0 or -1, we've set it in EAX */
336 jle KeReturnFromSystemCall
338 /* Set STATUS_INVALID_SYSTEM_SERVICE */
339 mov eax, STATUS_INVALID_SYSTEM_SERVICE
340 jmp KeReturnFromSystemCall
344 /* Invalid System Call */
345 mov eax, STATUS_INVALID_SYSTEM_SERVICE
346 jmp KeReturnFromSystemCall
350 /* Check if this came from kernel-mode */
351 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
353 /* It's fine, go ahead with it */
356 /* Caller sent invalid parameters, fail here */
357 mov eax, STATUS_ACCESS_VIOLATION
362 /* Restore ESP0 stack */
363 mov ecx, PCR[KPCR_TSS]
364 mov esp, ss:[ecx+KTSS_ESP0]
366 /* Generate V86M Stack for Trap 6 */
372 /* Generate interrupt stack for Trap 6 */
373 push KGDT_R3_DATA + RPL_MASK
376 push KGDT_R3_CODE + RPL_MASK
382 /* Save current IRQL */
385 /* Set us at passive */
386 mov dword ptr PCR[KPCR_IRQL], 0
394 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
395 call _KeBugCheckEx@20
399 /* Get the index and APC state */
400 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
401 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
408 push APC_INDEX_MISMATCH
409 call _KeBugCheckEx@20
416 /* Disable interrupts */
419 /* Check for, and deliver, User-Mode APCs if needed */
420 CHECK_FOR_APC_DELIVER 0
422 /* Exit and cleanup */
423 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
426 .func Kei386EoiHelper@0
429 /* Disable interrupts */
432 /* Check for, and deliver, User-Mode APCs if needed */
433 CHECK_FOR_APC_DELIVER 0
435 /* Exit and cleanup */
436 _Kei386EoiHelper2ndEntry:
437 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
441 /* Move to EDX position */
442 add esp, KTRAP_FRAME_EDX
444 /* Restore volatiles */
449 /* Move to non-volatiles */
450 lea esp, [ebp+KTRAP_FRAME_EDI]
456 /* Skip error code and return */
465 TRAP_FIXUPS kids_a, kids_t, DoFixupV86, DoFixupAbios
468 /* Push error code */
472 TRAP_PROLOG kids_a, kids_t
474 /* Increase EIP so we skip the INT3 */
475 inc dword ptr [ebp+KTRAP_FRAME_EIP]
477 /* Call debug service dispatcher */
478 mov eax, [ebp+KTRAP_FRAME_EAX]
479 mov ecx, [ebp+KTRAP_FRAME_ECX]
480 mov edx, [ebp+KTRAP_FRAME_EDX]
482 /* Jump to INT3 handler */
486 .func NtRaiseException@12
487 _NtRaiseException@12:
489 /* NOTE: We -must- be called by Zw* to have the right frame! */
490 /* Push the stack frame */
493 /* Get the current thread and restore its trap frame */
494 mov ebx, PCR[KPCR_CURRENT_THREAD]
495 mov edx, [ebp+KTRAP_FRAME_EDX]
496 mov [ebx+KTHREAD_TRAP_FRAME], edx
498 /* Set up stack frame */
501 /* Get the Trap Frame in EBX */
504 /* Get the exception list and restore */
505 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
506 mov PCR[KPCR_EXCEPTION_LIST], eax
508 /* Get the parameters */
509 mov edx, [ebp+16] /* Search frames */
510 mov ecx, [ebp+12] /* Context */
511 mov eax, [ebp+8] /* Exception Record */
513 /* Raise the exception */
519 call _KiRaiseException@20
521 /* Restore trap frame in EBP */
525 /* Check the result */
529 /* Restore debug registers too */
536 /* NOTE: We -must- be called by Zw* to have the right frame! */
537 /* Push the stack frame */
540 /* Get the current thread and restore its trap frame */
541 mov ebx, PCR[KPCR_CURRENT_THREAD]
542 mov edx, [ebp+KTRAP_FRAME_EDX]
543 mov [ebx+KTHREAD_TRAP_FRAME], edx
545 /* Set up stack frame */
548 /* Save the parameters */
552 /* Call KiContinue */
558 /* Check if we failed (bad context record) */
562 /* Check if test alert was requested */
563 cmp dword ptr [ebp+12], 0
566 /* Test alert for the thread */
567 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
569 call _KeTestAlertThread@4
572 /* Return to previous context */
583 /* EXCEPTION DISPATCHERS *****************************************************/
585 .func CommonDispatchException
586 _CommonDispatchException:
588 /* Make space for an exception record */
589 sub esp, EXCEPTION_RECORD_LENGTH
592 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
594 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
595 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
596 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
597 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
599 /* Check parameter count */
603 /* Get information */
604 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
611 /* Set the record in ECX and check if this was V86 */
613 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
622 /* Get the caller's CS */
623 mov eax, [ebp+KTRAP_FRAME_CS]
626 /* Check if it was user-mode or kernel-mode */
629 /* Dispatch the exception */
635 call _KiDispatchException@20
639 jmp _Kei386EoiHelper@0
642 .func DispatchNoParam
644 /* Call the common dispatcher */
646 call _CommonDispatchException
649 .func DispatchOneParam
651 /* Call the common dispatcher */
654 call _CommonDispatchException
657 .func DispatchTwoParam
659 /* Call the common dispatcher */
662 call _CommonDispatchException
665 /* HARDWARE TRAP HANDLERS ****************************************************/
670 /* TODO: Routine to fixup a KTRAP_FRAME when faulting from a syscall. */
675 TRAP_FIXUPS kit0_a, kit0_t, DoFixupV86, DoNotFixupAbios
677 /* Push error code */
681 TRAP_PROLOG kit0_a, kit0_t
684 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
687 /* Check if the frame was from kernelmode */
688 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
691 /* Check the old mode */
692 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
696 /* Re-enable interrupts for user-mode and send the exception */
698 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
699 mov ebx, [ebp+KTRAP_FRAME_EIP]
703 /* Check if this is a VDM process */
704 mov ebx, PCR[KPCR_CURRENT_THREAD]
705 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
706 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
709 /* We don't support this yet! */
716 TRAP_FIXUPS kit1_a, kit1_t, DoFixupV86, DoNotFixupAbios
718 /* Push error code */
722 TRAP_PROLOG kit1_a, kit1_t
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 /* Enable interrupts for user-mode */
741 /* Prepare the exception */
742 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
743 mov ebx, [ebp+KTRAP_FRAME_EIP]
744 mov eax, STATUS_SINGLE_STEP
748 /* Check if this is a VDM process */
749 mov ebx, PCR[KPCR_CURRENT_THREAD]
750 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
751 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
754 /* We don't support VDM! */
762 /* FIXME: This is an NMI, nothing like a normal exception */
764 jmp _KiSystemFatalException
768 TRAP_FIXUPS kit3_a, kit3_t, DoFixupV86, DoNotFixupAbios
770 /* Push error code */
774 TRAP_PROLOG kit3_a, kit3_t
776 /* Set status code */
777 mov eax, 0 //STATUS_SUCCESS
781 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
784 /* Check if the frame was from kernelmode */
785 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
788 /* Check the old mode */
789 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
793 /* Enable interrupts for user-mode */
798 /* Prepare the exception */
803 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
804 mov ebx, [ebp+KTRAP_FRAME_EIP]
807 mov eax, STATUS_BREAKPOINT
808 call _CommonDispatchException
811 /* Check if this is a VDM process */
812 mov ebx, PCR[KPCR_CURRENT_THREAD]
813 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
814 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
817 /* We don't support VDM! */
822 TRAP_FIXUPS kit4_a, kit4_t, DoFixupV86, DoNotFixupAbios
824 /* Push error code */
828 TRAP_PROLOG kit4_a, kit4_t
831 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
834 /* Check if the frame was from kernelmode */
835 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
838 /* Check the old mode */
839 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
843 /* Re-enable interrupts for user-mode and send the exception */
845 mov eax, STATUS_INTEGER_OVERFLOW
846 mov ebx, [ebp+KTRAP_FRAME_EIP]
851 /* Check if this is a VDM process */
852 mov ebx, PCR[KPCR_CURRENT_THREAD]
853 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
854 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
857 /* We don't support this yet! */
863 TRAP_FIXUPS kit5_a, kit5_t, DoFixupV86, DoNotFixupAbios
865 /* Push error code */
869 TRAP_PROLOG kit5_a, kit5_t
872 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
875 /* Check if the frame was from kernelmode */
876 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
879 /* It did, and this should never happen */
881 jmp _KiSystemFatalException
883 /* Check the old mode */
885 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
888 /* Re-enable interrupts for user-mode and send the exception */
891 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
892 mov ebx, [ebp+KTRAP_FRAME_EIP]
896 /* Check if this is a VDM process */
897 mov ebx, PCR[KPCR_CURRENT_THREAD]
898 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
899 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
902 /* We don't support this yet! */
908 TRAP_FIXUPS kit6_a, kit6_t, DoFixupV86, DoNotFixupAbios
911 /* It this a V86 GPF? */
912 test dword ptr [esp+8], EFLAGS_V86_MASK
916 V86_TRAP_PROLOG kit6_a, kit6_v
918 /* Not yet supported (Invalid OPCODE from V86) */
922 /* Push error code */
926 TRAP_PROLOG kit6_a, kit6_t
928 /* Check if this happened in kernel mode */
929 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
933 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
936 /* Check if the process is vDM */
937 mov ebx, PCR[KPCR_CURRENT_THREAD]
938 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
939 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
943 /* Get EIP and enable interrupts at this point */
944 mov esi, [ebp+KTRAP_FRAME_EIP]
947 /* Set intruction prefix length */
950 /* Setup a SEH frame */
953 push PCR[KPCR_EXCEPTION_LIST]
954 mov PCR[KPCR_EXCEPTION_LIST], esp
957 /* Get the instruction and check if it's LOCK */
967 pop PCR[KPCR_EXCEPTION_LIST]
972 /* Re-enable interrupts */
975 /* Setup illegal instruction exception and dispatch it */
976 mov ebx, [ebp+KTRAP_FRAME_EIP]
977 mov eax, STATUS_ILLEGAL_INSTRUCTION
983 pop PCR[KPCR_EXCEPTION_LIST]
986 /* Setup invalid lock exception and dispatch it */
987 mov ebx, [ebp+KTRAP_FRAME_EIP]
988 mov eax, STATUS_INVALID_LOCK_SEQUENCE
996 /* Return to caller */
997 jmp _Kei386EoiHelper@0
1003 pop PCR[KPCR_EXCEPTION_LIST]
1007 /* Check if this was user mode */
1008 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1017 push KMODE_EXCEPTION_NOT_HANDLED
1018 call _KeBugCheckWithTf@24
1022 TRAP_FIXUPS kit7_a, kit7_t, DoFixupV86, DoNotFixupAbios
1024 /* Push error code */
1028 TRAP_PROLOG kit7_a, kit7_t
1030 /* Get the current thread and stack */
1032 mov eax, PCR[KPCR_CURRENT_THREAD]
1033 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1034 sub ecx, NPX_FRAME_LENGTH
1036 /* Check if emulation is enabled */
1037 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1038 jnz EmulationEnabled
1041 /* Check if the NPX state is loaded */
1042 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1047 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1050 /* Check the NPX thread */
1051 mov edx, PCR[KPCR_NPX_THREAD]
1055 /* Get the NPX Stack */
1056 mov esi, [edx+KTHREAD_INITIAL_STACK]
1057 sub esi, NPX_FRAME_LENGTH
1059 /* Check if we have FXSR and check which operand to use */
1060 test byte ptr _KeI386FxsrPresent, 1
1069 /* Set the thread's state to dirty */
1070 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1073 /* Check if we have FXSR and choose which operand to use */
1074 test byte ptr _KeI386FxsrPresent, 1
1083 /* Set state loaded */
1084 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1085 mov PCR[KPCR_NPX_THREAD], eax
1087 /* Enable interrupts to happen now */
1091 /* Check if CR0 needs to be reloaded due to a context switch */
1092 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1093 jz _Kei386EoiHelper@0
1095 /* We have to reload CR0... disable interrupts */
1098 /* Get CR0 and update it */
1100 or ebx, [ecx+FN_CR0_NPX_STATE]
1103 /* Restore interrupts and check if TS is back on */
1106 jz _Kei386EoiHelper@0
1108 /* Clear TS, and loop handling again */
1115 /* Set delayed error */
1116 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1118 /* Check if this happened during restore */
1119 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1122 /* Skip instruction and dispatch the exception */
1123 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1124 jmp _Kei386EoiHelper@0
1127 /* Check if TS is set */
1129 jnz TsSetOnLoadedState
1132 /* Check if the trap came from V86 mode */
1133 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1136 /* Check if it came from kernel mode */
1137 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1140 /* Check if it came from a VDM */
1141 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1145 /* Get the current thread */
1146 mov eax, PCR[KPCR_CURRENT_THREAD]
1148 /* Check NPX state */
1149 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1151 /* Get the NPX save area */
1152 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1153 lea ecx, [ecx-NPX_FRAME_LENGTH]
1160 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1163 /* Check if we have FX support */
1164 test byte ptr _KeI386FxsrPresent, 1
1167 /* Save the state */
1175 /* Make CR0 state not loaded */
1176 or ebx, NPX_STATE_NOT_LOADED
1177 or ebx, [ecx+FN_CR0_NPX_STATE]
1180 /* Update NPX state */
1181 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1182 mov dword ptr PCR[KPCR_NPX_THREAD], 0
1185 /* Clear the TS bit and re-enable interrupts */
1186 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1189 /* Check if we have FX support */
1190 test byte ptr _KeI386FxsrPresent, 1
1193 /* Get error offset, control and status words */
1194 mov ebx, [ecx+FX_ERROR_OFFSET]
1195 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1196 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1198 /* Get the faulting opcode */
1199 mov esi, [ecx+FX_DATA_OFFSET]
1203 /* Get error offset, control and status words */
1204 mov ebx, [ecx+FP_ERROR_OFFSET]
1205 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1206 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1208 /* Get the faulting opcode */
1209 mov esi, [ecx+FP_DATA_OFFSET]
1212 /* Mask exceptions */
1217 /* Check if what's left is invalid */
1221 /* Check if it was a stack fault */
1225 /* Raise exception */
1226 mov eax, STATUS_FLOAT_INVALID_OPERATION
1227 jmp _DispatchOneParam
1231 /* Raise exception */
1232 mov eax, STATUS_FLOAT_STACK_CHECK
1233 jmp _DispatchTwoParam
1237 /* Check for divide by 0 */
1241 /* Raise exception */
1242 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1243 jmp _DispatchOneParam
1246 /* Check for denormal */
1250 /* Raise exception */
1251 mov eax, STATUS_FLOAT_INVALID_OPERATION
1252 jmp _DispatchOneParam
1255 /* Check for overflow */
1259 /* Raise exception */
1260 mov eax, STATUS_FLOAT_OVERFLOW
1261 jmp _DispatchOneParam
1264 /* Check for underflow */
1268 /* Raise exception */
1269 mov eax, STATUS_FLOAT_UNDERFLOW
1270 jmp _DispatchOneParam
1273 /* Check for precision fault */
1277 /* Raise exception */
1278 mov eax, STATUS_FLOAT_INEXACT_RESULT
1279 jmp _DispatchOneParam
1283 /* Strange result, bugcheck the OS */
1290 push TRAP_CAUSE_UNKNOWN
1291 call _KeBugCheckWithTf@24
1294 /* Check if this is a VDM */
1295 mov eax, PCR[KPCR_CURRENT_THREAD]
1296 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1297 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1300 /* V86 NPX not handled */
1304 /* Did this come from kernel-mode? */
1305 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1308 /* It came from user-mode, so this would only be valid inside a VDM */
1309 /* Since we don't actually have VDMs in ROS, bugcheck. */
1313 /* TS shouldn't be set, unless this we don't have a Math Processor */
1317 /* Strange that we got a trap at all, but ignore and continue */
1319 jmp _Kei386EoiHelper@0
1322 /* Cause a bugcheck */
1327 push TRAP_CAUSE_UNKNOWN
1328 call _KeBugCheckEx@20
1335 /* Can't really do too much */
1337 jmp _KiSystemFatalException
1341 TRAP_FIXUPS kit9_a, kit9_t, DoFixupV86, DoNotFixupAbios
1343 /* Push error code */
1347 TRAP_PROLOG kit9_a, kit9_t
1349 /* Enable interrupts and bugcheck */
1352 jmp _KiSystemFatalException
1356 TRAP_FIXUPS kita_a, kita_t, DoFixupV86, DoNotFixupAbios
1359 TRAP_PROLOG kita_a, kita_t
1362 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1365 /* Check if the frame was from kernelmode */
1366 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1370 /* Check if OF was set during iretd */
1371 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1375 /* It was, just mask it out */
1376 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1377 jmp _Kei386EoiHelper@0
1380 /* TSS failure for some other reason: crash */
1382 jmp _KiSystemFatalException
1386 TRAP_FIXUPS kitb_a, kitb_t, DoFixupV86, DoNotFixupAbios
1389 TRAP_PROLOG kitb_a, kitb_t
1391 /* FIXME: ROS Doesn't handle segment faults yet */
1393 jmp _KiSystemFatalException
1397 TRAP_FIXUPS kitc_a, kitc_t, DoFixupV86, DoNotFixupAbios
1400 TRAP_PROLOG kitc_a, kitc_t
1402 /* FIXME: ROS Doesn't handle stack faults yet */
1404 jmp _KiSystemFatalException
1407 .func KiTrapExceptHandler
1408 _KiTrapExceptHandler:
1410 /* Setup SEH handler frame */
1412 pop PCR[KPCR_EXCEPTION_LIST]
1416 /* Check if the fault came from user mode */
1417 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1420 /* Kernel fault, bugcheck */
1426 push KMODE_EXCEPTION_NOT_HANDLED
1427 call _KeBugCheckWithTf@24
1431 TRAP_FIXUPS kitd_a, kitd_t, DoFixupV86, DoNotFixupAbios
1434 /* It this a V86 GPF? */
1435 test dword ptr [esp+12], EFLAGS_V86_MASK
1438 /* Enter V86 Trap */
1439 V86_TRAP_PROLOG kitd_a, kitd_v
1441 /* Make sure that this is a V86 process */
1442 mov ecx, PCR[KPCR_CURRENT_THREAD]
1443 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1444 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1447 /* Otherwise, something is very wrong, raise an exception */
1449 mov ebx, [ebp+KTRAP_FRAME_EIP]
1451 mov eax, STATUS_ACCESS_VIOLATION
1452 jmp _DispatchTwoParam
1456 /* Go to APC level */
1460 /* Save old IRQL and enable interrupts */
1464 /* Handle the opcode */
1465 call _Ki386HandleOpcodeV86@0
1467 /* Check if this was VDM */
1476 /* Lower IRQL and disable interrupts */
1481 /* Check if this was a V86 trap */
1482 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1485 /* Exit the V86 Trap */
1490 /* Either this wasn't V86, or it was, but an APC interrupted us */
1491 jmp _Kei386EoiHelper@0
1495 TRAP_PROLOG kitd_a, kitd_t
1497 /* Check if this was from kernel-mode */
1498 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1501 /* Check if we have a VDM alert */
1502 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1505 /* Check for GPF during GPF */
1506 mov eax, [ebp+KTRAP_FRAME_EIP]
1507 cmp eax, offset CheckPrivilegedInstruction
1509 cmp eax, offset CheckPrivilegedInstruction2
1514 /* Get the opcode and trap frame */
1516 mov eax, [ebp+KTRAP_FRAME_EIP]
1518 mov edx, [ebp+KTRAP_FRAME_EBP]
1520 /* We want to check if this was POP [DS/ES/FS/GS] */
1521 add edx, KTRAP_FRAME_DS
1524 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1527 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1530 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1534 /* It isn't, was it IRETD? */
1538 /* Get error code */
1539 lea edx, [ebp+KTRAP_FRAME_ESP]
1540 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1544 mov cx, word ptr [edx+4]
1549 /* This should be a Ki386CallBios return */
1550 mov eax, offset _Ki386BiosCallReturnAddress
1554 cmp ax, KGDT_R0_CODE + RPL_MASK
1557 /* Jump to return address */
1558 jmp _Ki386BiosCallReturnAddress
1561 /* Check if the thread was in kernel mode */
1562 mov ebx, PCR[KPCR_CURRENT_THREAD]
1563 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1566 /* Set RPL_MASK for check below */
1567 or word ptr [edx+4], RPL_MASK
1570 /* Check if the IRET goes to user-mode */
1571 test dword ptr [edx+4], RPL_MASK
1574 /* Setup trap frame to copy */
1575 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1576 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1580 /* Copy each field */
1586 /* Enable interrupts and adjust stack */
1591 /* Setup exception record */
1592 mov ebx, [ebp+KTRAP_FRAME_EIP]
1593 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1595 mov eax, STATUS_ACCESS_VIOLATION
1596 jmp _DispatchTwoParam
1600 /* FIXME: Handle RDMSR/WRMSR */
1605 /* Check if this was an MSR opcode */
1609 /* Check if DS is Ring 3 */
1610 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1613 /* Otherwise, fix it up */
1614 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1619 /* Check if ES is Ring 3 */
1620 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1623 /* Otherwise, fix it up */
1624 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1630 lea eax, [ebp+KTRAP_FRAME_ESP]
1635 /* Handle segment POP fault by setting it to 0 */
1638 mov dword ptr [edx], eax
1642 /* Do a trap exit */
1643 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1647 /* If the previous mode was kernel, raise a fatal exception */
1649 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1650 jz _KiSystemFatalException
1652 /* Get the process and check which CS this came from */
1653 mov ebx, PCR[KPCR_CURRENT_THREAD]
1654 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
1655 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1658 /* Check if this is a VDM */
1659 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1662 /* Enable interrupts and check if we have an error code */
1664 cmp word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
1666 jmp CheckPrivilegedInstruction
1669 /* Update EIP (will be updated below again) */
1670 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1673 /* Clear the segment, update EIP and ESP */
1674 mov dword ptr [edx], 0
1675 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1676 add dword ptr [ebp+KTRAP_FRAME_ESP], 4
1677 jmp _Kei386EoiHelper@0
1680 /* Check if this is a VDM */
1681 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1682 jz CheckPrivilegedInstruction
1684 /* Bring interrupts back */
1687 /* Check what kind of instruction this is */
1688 mov eax, [ebp+KTRAP_FRAME_EIP]
1691 /* FIXME: Check for BOP4 */
1693 /* Check if this is POP ES */
1695 add edx, KTRAP_FRAME_ES
1699 /* Check if this is POP FS */
1700 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1704 /* Check if this is POP GS */
1705 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1709 CheckPrivilegedInstruction:
1710 /* Bring interrupts back */
1713 /* Setup a SEH handler */
1715 push offset _KiTrapExceptHandler
1716 push PCR[KPCR_EXCEPTION_LIST]
1717 mov PCR[KPCR_EXCEPTION_LIST], esp
1720 mov esi, [ebp+KTRAP_FRAME_EIP]
1722 /* Setup loop count */
1726 /* Save loop count */
1729 /* Get the instruction */
1732 /* Now lookup in the prefix table */
1734 mov edi, offset _KiTrapPrefixTable
1737 /* Restore loop count */
1740 /* If it's not a prefix byte, check other instructions */
1747 /* FIXME: Check if it's a HLT */
1749 /* Check if the instruction has two bytes */
1757 /* Get EFLAGS and IOPL */
1758 mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
1762 /* Check the CS's RPL mask */
1763 mov ecx, [ebp+KTRAP_FRAME_CS]
1768 CheckPrivilegedInstruction2:
1769 /* Check if this is a CLI or STI */
1771 je IsPrivInstruction
1773 je IsPrivInstruction
1775 /* Setup I/O table lookup */
1777 mov edi, offset _KiTrapIoTable
1779 /* Loopup in the table */
1783 /* FIXME: Check IOPM!!! */
1786 /* Cleanup the SEH frame */
1787 pop PCR[KPCR_EXCEPTION_LIST]
1790 /* Setup the exception */
1791 mov ebx, [ebp+KTRAP_FRAME_EIP]
1792 mov eax, STATUS_PRIVILEGED_INSTRUCTION
1793 jmp _DispatchNoParam
1796 /* Cleanup the SEH frame */
1797 pop PCR[KPCR_EXCEPTION_LIST]
1801 /* Setup the exception */
1802 mov ebx, [ebp+KTRAP_FRAME_EIP]
1804 mov eax, STATUS_ACCESS_VIOLATION
1805 jmp _DispatchTwoParam
1813 TRAP_FIXUPS kite_a, kite_t, DoFixupV86, DoNotFixupAbios
1817 TRAP_PROLOG kite_a, kite_t
1819 /* Check if we have a VDM alert */
1820 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1823 /* Get the current thread */
1824 mov edi, PCR[KPCR_CURRENT_THREAD]
1826 /* Get the stack address of the frame */
1827 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1828 sub eax, [edi+KTHREAD_INITIAL_STACK]
1831 /* This isn't the base frame, check if it's the second */
1832 cmp eax, -KTRAP_FRAME_EFLAGS
1835 /* Check if we have a TEB */
1836 mov eax, PCR[KPCR_TEB]
1840 /* Fixup the frame */
1847 /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
1848 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1851 /* Enable interrupts and check if we got here with interrupts disabled */
1853 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1857 /* Send trap frame and check if this is kernel-mode or usermode */
1859 mov eax, [ebp+KTRAP_FRAME_CS]
1863 /* Send faulting address and check if this is read or write */
1865 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1869 /* Call the access fault handler */
1870 call _MmAccessFault@16
1874 /* Access fault handled, return to caller */
1875 jmp _Kei386EoiHelper@0
1878 /* First check if this is a fault in the S-LIST functions */
1879 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1880 cmp [ebp+KTRAP_FRAME_EIP], ecx
1883 /* Check if this is a fault in the syscall handler */
1884 mov ecx, offset CopyParams
1885 cmp [ebp+KTRAP_FRAME_EIP], ecx
1887 mov ecx, offset ReadBatch
1888 cmp [ebp+KTRAP_FRAME_EIP], ecx
1893 jmp _Kei386EoiHelper@0
1898 jmp _Kei386EoiHelper@0
1900 /* Check if the fault occured in a V86 mode */
1902 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
1905 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1908 /* Check if the fault occured in a VDM */
1909 mov esi, PCR[KPCR_CURRENT_THREAD]
1910 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
1911 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
1914 /* Check if we this was in kernel-mode */
1915 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1917 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1924 /* Save EIP and check what kind of status failure we got */
1926 mov esi, [ebp+KTRAP_FRAME_EIP]
1927 cmp eax, STATUS_ACCESS_VIOLATION
1929 cmp eax, STATUS_GUARD_PAGE_VIOLATION
1931 cmp eax, STATUS_STACK_OVERFLOW
1934 /* Setup an in-page exception to dispatch */
1940 mov eax, STATUS_IN_PAGE_ERROR
1941 call _CommonDispatchException
1944 /* Use more proper status code */
1945 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
1948 /* Setup a normal page fault exception */
1952 jmp _DispatchTwoParam
1960 /* This is completely illegal, bugcheck the system */
1966 push IRQL_NOT_LESS_OR_EQUAL
1967 call _KeBugCheckWithTf@24
1971 /* FIXME: NOT SUPPORTED */
1976 TRAP_FIXUPS kitf_a, kitf_t, DoFixupV86, DoNotFixupAbios
1978 /* Push error code */
1982 TRAP_PROLOG kitf_a, kitf_t
1985 /* Raise a fatal exception */
1987 jmp _KiSystemFatalException
1991 TRAP_FIXUPS kit10_a, kit10_t, DoFixupV86, DoNotFixupAbios
1993 /* Push error code */
1997 TRAP_PROLOG kit10_a, kit10_t
1999 /* Check if this is the NPX Thread */
2000 mov eax, PCR[KPCR_CURRENT_THREAD]
2001 cmp eax, PCR[KPCR_NPX_THREAD]
2003 /* Get the initial stack and NPX frame */
2004 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2005 lea ecx, [ecx-NPX_FRAME_LENGTH]
2007 /* If this is a valid fault, handle it */
2010 /* Otherwise, re-enable interrupts and set delayed error */
2012 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
2013 jmp _Kei386EoiHelper@0
2017 TRAP_FIXUPS kit11_a, kit11_t, DoFixupV86, DoNotFixupAbios
2019 /* Push error code */
2023 TRAP_PROLOG kit11_a, kit11_t
2025 /* FIXME: ROS Doesn't handle alignment faults yet */
2027 jmp _KiSystemFatalException
2030 .func KiSystemFatalException
2031 _KiSystemFatalException:
2033 /* Push the trap frame */
2036 /* Push empty parameters */
2041 /* Push trap number and bugcheck code */
2043 push UNEXPECTED_KERNEL_MODE_TRAP
2044 call _KeBugCheckWithTf@24
2048 .func KiCoprocessorError@0
2049 _KiCoprocessorError@0:
2051 /* Get the NPX Thread's Initial stack */
2052 mov eax, PCR[KPCR_NPX_THREAD]
2053 mov eax, [eax+KTHREAD_INITIAL_STACK]
2055 /* Make space for the FPU Save area */
2056 sub eax, SIZEOF_FX_SAVE_AREA
2058 /* Set the CR0 State */
2059 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
2066 /* Return to caller */
2070 .func Ki16BitStackException
2071 _Ki16BitStackException:
2077 /* Go to kernel mode thread stack */
2078 mov eax, PCR[KPCR_CURRENT_THREAD]
2079 add esp, [eax+KTHREAD_INITIAL_STACK]
2081 /* Switch to good stack segment */
2085 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
2087 .globl _KiStartUnexpectedRange@0
2088 _KiStartUnexpectedRange@0:
2090 GENERATE_INT_HANDLERS
2092 .globl _KiEndUnexpectedRange@0
2093 _KiEndUnexpectedRange@0:
2094 jmp _KiUnexpectedInterruptTail
2096 .func KiUnexpectedInterruptTail
2097 TRAP_FIXUPS kui_a, kui_t, DoFixupV86, DoFixupAbios
2098 _KiUnexpectedInterruptTail:
2100 /* Enter interrupt trap */
2101 INT_PROLOG kui_a, kui_t, DoNotPushFakeErrorCode
2103 /* Increase interrupt count */
2104 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2106 /* Put vector in EBX and make space for KIRQL */
2110 /* Begin interrupt */
2114 call _HalBeginSystemInterrupt@12
2116 /* Check if it was spurious or not */
2120 /* Spurious, ignore it */
2122 jmp _Kei386EoiHelper2ndEntry
2125 /* Unexpected interrupt, print a message on debug builds */
2128 push offset _UnexpectedMsg
2133 /* Exit the interrupt */
2136 call _HalEndSystemInterrupt@8
2137 jmp _Kei386EoiHelper@0
2140 .globl _KiUnexpectedInterrupt
2141 _KiUnexpectedInterrupt:
2143 /* Bugcheck with invalid interrupt code */
2144 push TRAP_CAUSE_UNKNOWN
2147 /* INTERRUPT HANDLERS ********************************************************/
2149 .func KiDispatchInterrupt@0
2150 _KiDispatchInterrupt@0:
2152 /* Get the PCR and disable interrupts */
2153 mov ebx, PCR[KPCR_SELF]
2156 /* Check if we have to deliver DPCs, timers, or deferred threads */
2157 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2158 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2159 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2162 /* Save stack pointer and exception list, then clear it */
2164 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2165 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2167 /* Save the stack and switch to the DPC Stack */
2169 mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2173 mov ecx, [ebx+KPCR_PRCB]
2174 call @KiRetireDpcList@4
2176 /* Restore stack and exception list */
2178 pop dword ptr [ebx+KPCR_EXCEPTION_LIST]
2183 /* Re-enable interrupts */
2186 /* Check if we have quantum end */
2187 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2190 /* Check if we have a thread to swap to */
2191 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2194 /* Make space on the stack to save registers */
2200 /* Get the current thread */
2201 mov edi, [ebx+KPCR_CURRENT_THREAD]
2204 #error SMP Interrupt not handled!
2207 /* Get the next thread and clear it */
2208 mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
2209 and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2211 /* Set us as the current running thread */
2212 mov [ebx+KPCR_CURRENT_THREAD], esi
2213 mov byte ptr [esi+KTHREAD_STATE], Running
2214 mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
2216 /* Put thread in ECX and get the PRCB in EDX */
2218 lea edx, [ebx+KPCR_PRCB_DATA]
2219 call @KiQueueReadyThread@8
2221 /* Set APC_LEVEL and do the swap */
2223 call @KiSwapContextInternal@0
2225 /* Restore registers */
2236 /* Disable quantum end and process it */
2237 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2238 call _KiQuantumEnd@0
2242 .func KiInterruptTemplate
2243 _KiInterruptTemplate:
2245 /* Enter interrupt trap */
2246 INT_PROLOG kit_a, kit_t, DoPushFakeErrorCode
2248 _KiInterruptTemplate2ndDispatch:
2249 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2252 _KiInterruptTemplateObject:
2253 /* Dummy jump, will be replaced by the actual jump */
2254 jmp _KeSynchronizeExecution@12
2256 _KiInterruptTemplateDispatch:
2257 /* Marks the end of the template so that the jump above can be edited */
2259 TRAP_FIXUPS kit_a, kit_t, DoFixupV86, DoFixupAbios
2262 .func KiChainedDispatch2ndLvl@0
2263 _KiChainedDispatch2ndLvl@0:
2265 /* Not yet supported */
2269 .func KiChainedDispatch@0
2270 _KiChainedDispatch@0:
2272 /* Increase interrupt count */
2273 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2275 /* Save trap frame */
2278 /* Save vector and IRQL */
2279 mov eax, [edi+KINTERRUPT_VECTOR]
2280 mov ecx, [edi+KINTERRUPT_IRQL]
2286 /* Begin interrupt */
2290 call _HalBeginSystemInterrupt@12
2292 /* Check if it was handled */
2296 /* Call the 2nd-level handler */
2297 call _KiChainedDispatch2ndLvl@0
2299 /* Exit the interrupt */
2303 .func KiInterruptDispatch@0
2304 _KiInterruptDispatch@0:
2306 /* Increase interrupt count */
2307 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2309 /* Save trap frame */
2312 /* Save vector and IRQL */
2313 mov eax, [edi+KINTERRUPT_VECTOR]
2314 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2320 /* Begin interrupt */
2324 call _HalBeginSystemInterrupt@12
2326 /* Check if it was handled */
2330 /* Acquire the lock */
2332 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2333 ACQUIRE_SPINLOCK(esi, IntSpin)
2335 /* Make sure that this interrupt isn't storming */
2338 /* Save the tick count */
2339 mov ebx, _KeTickCount
2342 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2345 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2347 /* Check if the ISR timed out */
2348 add ebx, _KiISRTimeout
2349 cmp _KeTickCount, ebx
2353 /* Release the lock */
2354 RELEASE_SPINLOCK(esi)
2356 /* Exit the interrupt */
2360 /* Exit the interrupt */
2366 SPIN_ON_LOCK esi, GetIntLock
2370 /* Print warning message */
2371 push [edi+KINTERRUPT_SERVICE_ROUTINE]
2372 push offset _IsrTimeoutMsg
2376 /* Break into debugger, then continue */
2380 /* Cleanup verification */
2381 VERIFY_INT_END kid, 0
2384 .globl _KeSynchronizeExecution@12
2385 .func KeSynchronizeExecution@12
2386 _KeSynchronizeExecution@12:
2388 /* Save EBX and put the interrupt object in it */
2393 mov cl, [ebx+KINTERRUPT_SYNCHRONIZE_IRQL]
2396 /* Call the routine */