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 Dr_kss: DR_TRAP_FIXUP
142 /* Enter the shared system call prolog */
145 /* Jump to the actual handler */
149 .func KiFastCallEntry
150 Dr_FastCallDrSave: DR_TRAP_FIXUP
153 /* Enter the fast system call prolog */
154 FASTCALL_PROLOG FastCallDrSave
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 Dr_kids: DR_TRAP_FIXUP
465 V86_kids: V86_TRAP_FIXUP
468 /* Push error code */
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 Dr_kit0: DR_TRAP_FIXUP
676 V86_kit0: V86_TRAP_FIXUP
678 /* Push error code */
685 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
688 /* Check if the frame was from kernelmode */
689 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
692 /* Check the old mode */
693 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
697 /* Re-enable interrupts for user-mode and send the exception */
699 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
700 mov ebx, [ebp+KTRAP_FRAME_EIP]
704 /* Check if this is a VDM process */
705 mov ebx, PCR[KPCR_CURRENT_THREAD]
706 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
707 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
710 /* We don't support this yet! */
717 Dr_kit1: DR_TRAP_FIXUP
718 V86_kit1: V86_TRAP_FIXUP
720 /* Push error code */
727 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
730 /* Check if the frame was from kernelmode */
731 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
734 /* Check the old mode */
735 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
739 /* Enable interrupts for user-mode */
743 /* Prepare the exception */
744 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
745 mov ebx, [ebp+KTRAP_FRAME_EIP]
746 mov eax, STATUS_SINGLE_STEP
750 /* Check if this is a VDM process */
751 mov ebx, PCR[KPCR_CURRENT_THREAD]
752 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
753 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
756 /* We don't support VDM! */
764 /* FIXME: This is an NMI, nothing like a normal exception */
766 jmp _KiSystemFatalException
770 Dr_kit3: DR_TRAP_FIXUP
771 V86_kit3: V86_TRAP_FIXUP
773 /* Push error code */
779 /* Set status code */
780 mov eax, 0 //STATUS_SUCCESS
784 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
787 /* Check if the frame was from kernelmode */
788 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
791 /* Check the old mode */
792 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
796 /* Enable interrupts for user-mode */
801 /* Prepare the exception */
806 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
807 mov ebx, [ebp+KTRAP_FRAME_EIP]
810 mov eax, STATUS_BREAKPOINT
811 call _CommonDispatchException
814 /* Check if this is a VDM process */
815 mov ebx, PCR[KPCR_CURRENT_THREAD]
816 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
817 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
820 /* We don't support VDM! */
825 Dr_kit4: DR_TRAP_FIXUP
826 V86_kit4: V86_TRAP_FIXUP
828 /* Push error code */
835 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
838 /* Check if the frame was from kernelmode */
839 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
842 /* Check the old mode */
843 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
847 /* Re-enable interrupts for user-mode and send the exception */
849 mov eax, STATUS_INTEGER_OVERFLOW
850 mov ebx, [ebp+KTRAP_FRAME_EIP]
855 /* Check if this is a VDM process */
856 mov ebx, PCR[KPCR_CURRENT_THREAD]
857 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
858 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
861 /* We don't support this yet! */
867 Dr_kit5: DR_TRAP_FIXUP
868 V86_kit5: V86_TRAP_FIXUP
870 /* Push error code */
877 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
880 /* Check if the frame was from kernelmode */
881 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
884 /* It did, and this should never happen */
886 jmp _KiSystemFatalException
888 /* Check the old mode */
890 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
893 /* Re-enable interrupts for user-mode and send the exception */
896 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
897 mov ebx, [ebp+KTRAP_FRAME_EIP]
901 /* Check if this is a VDM process */
902 mov ebx, PCR[KPCR_CURRENT_THREAD]
903 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
904 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
907 /* We don't support this yet! */
913 Dr_kit6: DR_TRAP_FIXUP
914 V86_kit6: V86_TRAP_FIXUP
917 /* It this a V86 GPF? */
918 test dword ptr [esp+8], EFLAGS_V86_MASK
924 /* Not yet supported (Invalid OPCODE from V86) */
928 /* Push error code */
934 /* Check if this happened in kernel mode */
935 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
939 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
942 /* Check if the process is vDM */
943 mov ebx, PCR[KPCR_CURRENT_THREAD]
944 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
945 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
949 /* Get EIP and enable interrupts at this point */
950 mov esi, [ebp+KTRAP_FRAME_EIP]
953 /* Set intruction prefix length */
956 /* Setup a SEH frame */
959 push PCR[KPCR_EXCEPTION_LIST]
960 mov PCR[KPCR_EXCEPTION_LIST], esp
963 /* Get the instruction and check if it's LOCK */
973 pop PCR[KPCR_EXCEPTION_LIST]
978 /* Re-enable interrupts */
981 /* Setup illegal instruction exception and dispatch it */
982 mov ebx, [ebp+KTRAP_FRAME_EIP]
983 mov eax, STATUS_ILLEGAL_INSTRUCTION
989 pop PCR[KPCR_EXCEPTION_LIST]
992 /* Setup invalid lock exception and dispatch it */
993 mov ebx, [ebp+KTRAP_FRAME_EIP]
994 mov eax, STATUS_INVALID_LOCK_SEQUENCE
1002 /* Return to caller */
1003 jmp _Kei386EoiHelper@0
1009 pop PCR[KPCR_EXCEPTION_LIST]
1013 /* Check if this was user mode */
1014 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1023 push KMODE_EXCEPTION_NOT_HANDLED
1024 call _KeBugCheckWithTf@24
1028 Dr_kit7: DR_TRAP_FIXUP
1029 V86_kit7: V86_TRAP_FIXUP
1031 /* Push error code */
1037 /* Get the current thread and stack */
1039 mov eax, PCR[KPCR_CURRENT_THREAD]
1040 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1041 sub ecx, NPX_FRAME_LENGTH
1043 /* Check if emulation is enabled */
1044 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1045 jnz EmulationEnabled
1048 /* Check if the NPX state is loaded */
1049 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1054 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1057 /* Check the NPX thread */
1058 mov edx, PCR[KPCR_NPX_THREAD]
1062 /* Get the NPX Stack */
1063 mov esi, [edx+KTHREAD_INITIAL_STACK]
1064 sub esi, NPX_FRAME_LENGTH
1066 /* Check if we have FXSR and check which operand to use */
1067 test byte ptr _KeI386FxsrPresent, 1
1076 /* Set the thread's state to dirty */
1077 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1080 /* Check if we have FXSR and choose which operand to use */
1081 test byte ptr _KeI386FxsrPresent, 1
1090 /* Set state loaded */
1091 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1092 mov PCR[KPCR_NPX_THREAD], eax
1094 /* Enable interrupts to happen now */
1098 /* Check if CR0 needs to be reloaded due to a context switch */
1099 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1100 jz _Kei386EoiHelper@0
1102 /* We have to reload CR0... disable interrupts */
1105 /* Get CR0 and update it */
1107 or ebx, [ecx+FN_CR0_NPX_STATE]
1110 /* Restore interrupts and check if TS is back on */
1113 jz _Kei386EoiHelper@0
1115 /* Clear TS, and loop handling again */
1122 /* Set delayed error */
1123 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1125 /* Check if this happened during restore */
1126 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1129 /* Skip instruction and dispatch the exception */
1130 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1131 jmp _Kei386EoiHelper@0
1134 /* Check if TS is set */
1136 jnz TsSetOnLoadedState
1139 /* Check if the trap came from V86 mode */
1140 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1143 /* Check if it came from kernel mode */
1144 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1147 /* Check if it came from a VDM */
1148 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1152 /* Get the current thread */
1153 mov eax, PCR[KPCR_CURRENT_THREAD]
1155 /* Check NPX state */
1156 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1158 /* Get the NPX save area */
1159 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1160 lea ecx, [ecx-NPX_FRAME_LENGTH]
1167 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1170 /* Check if we have FX support */
1171 test byte ptr _KeI386FxsrPresent, 1
1174 /* Save the state */
1182 /* Make CR0 state not loaded */
1183 or ebx, NPX_STATE_NOT_LOADED
1184 or ebx, [ecx+FN_CR0_NPX_STATE]
1187 /* Update NPX state */
1188 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1189 mov dword ptr PCR[KPCR_NPX_THREAD], 0
1192 /* Clear the TS bit and re-enable interrupts */
1193 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1196 /* Check if we have FX support */
1197 test byte ptr _KeI386FxsrPresent, 1
1200 /* Get error offset, control and status words */
1201 mov ebx, [ecx+FX_ERROR_OFFSET]
1202 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1203 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1205 /* Get the faulting opcode */
1206 mov esi, [ecx+FX_DATA_OFFSET]
1210 /* Get error offset, control and status words */
1211 mov ebx, [ecx+FP_ERROR_OFFSET]
1212 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1213 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1215 /* Get the faulting opcode */
1216 mov esi, [ecx+FP_DATA_OFFSET]
1219 /* Mask exceptions */
1224 /* Check if what's left is invalid */
1228 /* Check if it was a stack fault */
1232 /* Raise exception */
1233 mov eax, STATUS_FLOAT_INVALID_OPERATION
1234 jmp _DispatchOneParam
1238 /* Raise exception */
1239 mov eax, STATUS_FLOAT_STACK_CHECK
1240 jmp _DispatchTwoParam
1244 /* Check for divide by 0 */
1248 /* Raise exception */
1249 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1250 jmp _DispatchOneParam
1253 /* Check for denormal */
1257 /* Raise exception */
1258 mov eax, STATUS_FLOAT_INVALID_OPERATION
1259 jmp _DispatchOneParam
1262 /* Check for overflow */
1266 /* Raise exception */
1267 mov eax, STATUS_FLOAT_OVERFLOW
1268 jmp _DispatchOneParam
1271 /* Check for underflow */
1275 /* Raise exception */
1276 mov eax, STATUS_FLOAT_UNDERFLOW
1277 jmp _DispatchOneParam
1280 /* Check for precision fault */
1284 /* Raise exception */
1285 mov eax, STATUS_FLOAT_INEXACT_RESULT
1286 jmp _DispatchOneParam
1290 /* Strange result, bugcheck the OS */
1297 push TRAP_CAUSE_UNKNOWN
1298 call _KeBugCheckWithTf@24
1301 /* Check if this is a VDM */
1302 mov eax, PCR[KPCR_CURRENT_THREAD]
1303 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1304 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1307 /* V86 NPX not handled */
1311 /* Did this come from kernel-mode? */
1312 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1315 /* It came from user-mode, so this would only be valid inside a VDM */
1316 /* Since we don't actually have VDMs in ROS, bugcheck. */
1320 /* TS shouldn't be set, unless this we don't have a Math Processor */
1324 /* Strange that we got a trap at all, but ignore and continue */
1326 jmp _Kei386EoiHelper@0
1329 /* Cause a bugcheck */
1334 push TRAP_CAUSE_UNKNOWN
1335 call _KeBugCheckEx@20
1342 /* Can't really do too much */
1344 jmp _KiSystemFatalException
1348 Dr_kit9: DR_TRAP_FIXUP
1349 V86_kit9: V86_TRAP_FIXUP
1351 /* Push error code */
1357 /* Enable interrupts and bugcheck */
1360 jmp _KiSystemFatalException
1364 Dr_kit10: DR_TRAP_FIXUP
1365 V86_kit10: V86_TRAP_FIXUP
1371 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1374 /* Check if the frame was from kernelmode */
1375 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1379 /* Check if OF was set during iretd */
1380 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1384 /* It was, just mask it out */
1385 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1386 jmp _Kei386EoiHelper@0
1389 /* TSS failure for some other reason: crash */
1391 jmp _KiSystemFatalException
1395 Dr_kit11: DR_TRAP_FIXUP
1396 V86_kit11: V86_TRAP_FIXUP
1401 /* FIXME: ROS Doesn't handle segment faults yet */
1403 jmp _KiSystemFatalException
1407 Dr_kit12: DR_TRAP_FIXUP
1408 V86_kit12: V86_TRAP_FIXUP
1413 /* FIXME: ROS Doesn't handle stack faults yet */
1415 jmp _KiSystemFatalException
1418 .func KiTrapExceptHandler
1419 _KiTrapExceptHandler:
1421 /* Setup SEH handler frame */
1423 pop PCR[KPCR_EXCEPTION_LIST]
1427 /* Check if the fault came from user mode */
1428 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1431 /* Kernel fault, bugcheck */
1437 push KMODE_EXCEPTION_NOT_HANDLED
1438 call _KeBugCheckWithTf@24
1442 Dr_kitd: DR_TRAP_FIXUP
1443 V86_kitd: V86_TRAP_FIXUP
1446 /* It this a V86 GPF? */
1447 test dword ptr [esp+12], EFLAGS_V86_MASK
1450 /* Enter V86 Trap */
1451 V86_TRAP_PROLOG kitd
1453 /* Make sure that this is a V86 process */
1454 mov ecx, PCR[KPCR_CURRENT_THREAD]
1455 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1456 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1459 /* Otherwise, something is very wrong, raise an exception */
1461 mov ebx, [ebp+KTRAP_FRAME_EIP]
1463 mov eax, STATUS_ACCESS_VIOLATION
1464 jmp _DispatchTwoParam
1468 /* Go to APC level */
1472 /* Save old IRQL and enable interrupts */
1476 /* Handle the opcode */
1477 call _Ki386HandleOpcodeV86@0
1479 /* Check if this was VDM */
1488 /* Lower IRQL and disable interrupts */
1493 /* Check if this was a V86 trap */
1494 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1497 /* Exit the V86 Trap */
1502 /* Either this wasn't V86, or it was, but an APC interrupted us */
1503 jmp _Kei386EoiHelper@0
1509 /* Check if this was from kernel-mode */
1510 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1513 /* Check if we have a VDM alert */
1514 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1517 /* Check for GPF during GPF */
1518 mov eax, [ebp+KTRAP_FRAME_EIP]
1519 cmp eax, offset CheckPrivilegedInstruction
1521 cmp eax, offset CheckPrivilegedInstruction2
1526 /* Get the opcode and trap frame */
1528 mov eax, [ebp+KTRAP_FRAME_EIP]
1530 mov edx, [ebp+KTRAP_FRAME_EBP]
1532 /* We want to check if this was POP [DS/ES/FS/GS] */
1533 add edx, KTRAP_FRAME_DS
1536 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1539 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1542 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1546 /* It isn't, was it IRETD? */
1550 /* Get error code */
1551 lea edx, [ebp+KTRAP_FRAME_ESP]
1552 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1556 mov cx, word ptr [edx+4]
1561 /* This should be a Ki386CallBios return */
1562 mov eax, offset _Ki386BiosCallReturnAddress
1566 cmp ax, KGDT_R0_CODE + RPL_MASK
1569 /* Jump to return address */
1570 jmp _Ki386BiosCallReturnAddress
1573 /* Check if the thread was in kernel mode */
1574 mov ebx, PCR[KPCR_CURRENT_THREAD]
1575 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1578 /* Set RPL_MASK for check below */
1579 or word ptr [edx+4], RPL_MASK
1582 /* Check if the IRET goes to user-mode */
1583 test dword ptr [edx+4], RPL_MASK
1586 /* Setup trap frame to copy */
1587 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1588 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1592 /* Copy each field */
1598 /* Enable interrupts and adjust stack */
1603 /* Setup exception record */
1604 mov ebx, [ebp+KTRAP_FRAME_EIP]
1605 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1607 mov eax, STATUS_ACCESS_VIOLATION
1608 jmp _DispatchTwoParam
1612 /* FIXME: Handle RDMSR/WRMSR */
1617 /* Check if this was an MSR opcode */
1621 /* Check if DS is Ring 3 */
1622 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1625 /* Otherwise, fix it up */
1626 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1631 /* Check if ES is Ring 3 */
1632 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1635 /* Otherwise, fix it up */
1636 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1642 lea eax, [ebp+KTRAP_FRAME_ESP]
1647 /* Handle segment POP fault by setting it to 0 */
1650 mov dword ptr [edx], eax
1654 /* Do a trap exit */
1655 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1659 /* If the previous mode was kernel, raise a fatal exception */
1661 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1662 jz _KiSystemFatalException
1664 /* Get the process and check which CS this came from */
1665 mov ebx, PCR[KPCR_CURRENT_THREAD]
1666 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
1667 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1670 /* Check if this is a VDM */
1671 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1674 /* Enable interrupts and check if we have an error code */
1676 cmp word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
1678 jmp CheckPrivilegedInstruction
1681 /* Update EIP (will be updated below again) */
1682 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1685 /* Clear the segment, update EIP and ESP */
1686 mov dword ptr [edx], 0
1687 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1688 add dword ptr [ebp+KTRAP_FRAME_ESP], 4
1689 jmp _Kei386EoiHelper@0
1692 /* Check if this is a VDM */
1693 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1694 jz CheckPrivilegedInstruction
1696 /* Bring interrupts back */
1699 /* Check what kind of instruction this is */
1700 mov eax, [ebp+KTRAP_FRAME_EIP]
1703 /* FIXME: Check for BOP4 */
1705 /* Check if this is POP ES */
1707 add edx, KTRAP_FRAME_ES
1711 /* Check if this is POP FS */
1712 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1716 /* Check if this is POP GS */
1717 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1721 CheckPrivilegedInstruction:
1722 /* Bring interrupts back */
1725 /* Setup a SEH handler */
1727 push offset _KiTrapExceptHandler
1728 push PCR[KPCR_EXCEPTION_LIST]
1729 mov PCR[KPCR_EXCEPTION_LIST], esp
1732 mov esi, [ebp+KTRAP_FRAME_EIP]
1734 /* Setup loop count */
1738 /* Save loop count */
1741 /* Get the instruction */
1744 /* Now lookup in the prefix table */
1746 mov edi, offset _KiTrapPrefixTable
1749 /* Restore loop count */
1752 /* If it's not a prefix byte, check other instructions */
1759 /* FIXME: Check if it's a HLT */
1761 /* Check if the instruction has two bytes */
1769 /* Get EFLAGS and IOPL */
1770 mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
1774 /* Check the CS's RPL mask */
1775 mov ecx, [ebp+KTRAP_FRAME_CS]
1780 CheckPrivilegedInstruction2:
1781 /* Check if this is a CLI or STI */
1783 je IsPrivInstruction
1785 je IsPrivInstruction
1787 /* Setup I/O table lookup */
1789 mov edi, offset _KiTrapIoTable
1791 /* Loopup in the table */
1795 /* FIXME: Check IOPM!!! */
1798 /* Cleanup the SEH frame */
1799 pop PCR[KPCR_EXCEPTION_LIST]
1802 /* Setup the exception */
1803 mov ebx, [ebp+KTRAP_FRAME_EIP]
1804 mov eax, STATUS_PRIVILEGED_INSTRUCTION
1805 jmp _DispatchNoParam
1808 /* Cleanup the SEH frame */
1809 pop PCR[KPCR_EXCEPTION_LIST]
1813 /* Setup the exception */
1814 mov ebx, [ebp+KTRAP_FRAME_EIP]
1816 mov eax, STATUS_ACCESS_VIOLATION
1817 jmp _DispatchTwoParam
1825 Dr_kit14: DR_TRAP_FIXUP
1826 V86_kit14: V86_TRAP_FIXUP
1832 /* Check if we have a VDM alert */
1833 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1836 /* Get the current thread */
1837 mov edi, PCR[KPCR_CURRENT_THREAD]
1839 /* Get the stack address of the frame */
1840 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1841 sub eax, [edi+KTHREAD_INITIAL_STACK]
1844 /* This isn't the base frame, check if it's the second */
1845 cmp eax, -KTRAP_FRAME_EFLAGS
1848 /* Check if we have a TEB */
1849 mov eax, PCR[KPCR_TEB]
1853 /* Fixup the frame */
1860 /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
1861 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1864 /* Enable interrupts and check if we got here with interrupts disabled */
1866 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1870 /* Send trap frame and check if this is kernel-mode or usermode */
1872 mov eax, [ebp+KTRAP_FRAME_CS]
1876 /* Send faulting address and check if this is read or write */
1878 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1882 /* Call the access fault handler */
1883 call _MmAccessFault@16
1887 /* Access fault handled, return to caller */
1888 jmp _Kei386EoiHelper@0
1891 /* First check if this is a fault in the S-LIST functions */
1892 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1893 cmp [ebp+KTRAP_FRAME_EIP], ecx
1896 /* Check if this is a fault in the syscall handler */
1897 mov ecx, offset CopyParams
1898 cmp [ebp+KTRAP_FRAME_EIP], ecx
1900 mov ecx, offset ReadBatch
1901 cmp [ebp+KTRAP_FRAME_EIP], ecx
1906 jmp _Kei386EoiHelper@0
1911 jmp _Kei386EoiHelper@0
1913 /* Check if the fault occured in a V86 mode */
1915 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
1918 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1921 /* Check if the fault occured in a VDM */
1922 mov esi, PCR[KPCR_CURRENT_THREAD]
1923 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
1924 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
1927 /* Check if we this was in kernel-mode */
1928 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1930 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1937 /* Save EIP and check what kind of status failure we got */
1939 mov esi, [ebp+KTRAP_FRAME_EIP]
1940 cmp eax, STATUS_ACCESS_VIOLATION
1942 cmp eax, STATUS_GUARD_PAGE_VIOLATION
1944 cmp eax, STATUS_STACK_OVERFLOW
1947 /* Setup an in-page exception to dispatch */
1953 mov eax, STATUS_IN_PAGE_ERROR
1954 call _CommonDispatchException
1957 /* Use more proper status code */
1958 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
1961 /* Setup a normal page fault exception */
1965 jmp _DispatchTwoParam
1973 /* This is completely illegal, bugcheck the system */
1979 push IRQL_NOT_LESS_OR_EQUAL
1980 call _KeBugCheckWithTf@24
1984 /* FIXME: NOT SUPPORTED */
1989 Dr_kit15: DR_TRAP_FIXUP
1990 V86_kit15: V86_TRAP_FIXUP
1992 /* Push error code */
1999 /* Raise a fatal exception */
2001 jmp _KiSystemFatalException
2005 Dr_kit16: DR_TRAP_FIXUP
2006 V86_kit16: V86_TRAP_FIXUP
2008 /* Push error code */
2014 /* Check if this is the NPX Thread */
2015 mov eax, PCR[KPCR_CURRENT_THREAD]
2016 cmp eax, PCR[KPCR_NPX_THREAD]
2018 /* Get the initial stack and NPX frame */
2019 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2020 lea ecx, [ecx-NPX_FRAME_LENGTH]
2022 /* If this is a valid fault, handle it */
2025 /* Otherwise, re-enable interrupts and set delayed error */
2027 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
2028 jmp _Kei386EoiHelper@0
2032 Dr_kit17: DR_TRAP_FIXUP
2033 V86_kit17: V86_TRAP_FIXUP
2035 /* Push error code */
2041 /* FIXME: ROS Doesn't handle alignment faults yet */
2043 jmp _KiSystemFatalException
2046 .func KiSystemFatalException
2047 _KiSystemFatalException:
2049 /* Push the trap frame */
2052 /* Push empty parameters */
2057 /* Push trap number and bugcheck code */
2059 push UNEXPECTED_KERNEL_MODE_TRAP
2060 call _KeBugCheckWithTf@24
2064 .func KiCoprocessorError@0
2065 _KiCoprocessorError@0:
2067 /* Get the NPX Thread's Initial stack */
2068 mov eax, PCR[KPCR_NPX_THREAD]
2069 mov eax, [eax+KTHREAD_INITIAL_STACK]
2071 /* Make space for the FPU Save area */
2072 sub eax, SIZEOF_FX_SAVE_AREA
2074 /* Set the CR0 State */
2075 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
2082 /* Return to caller */
2086 .func Ki16BitStackException
2087 _Ki16BitStackException:
2093 /* Go to kernel mode thread stack */
2094 mov eax, PCR[KPCR_CURRENT_THREAD]
2095 add esp, [eax+KTHREAD_INITIAL_STACK]
2097 /* Switch to good stack segment */
2101 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
2103 .globl _KiStartUnexpectedRange@0
2104 _KiStartUnexpectedRange@0:
2106 GENERATE_INT_HANDLERS
2108 .globl _KiEndUnexpectedRange@0
2109 _KiEndUnexpectedRange@0:
2110 jmp _KiUnexpectedInterruptTail
2112 .func KiUnexpectedInterruptTail
2113 V86_kui: V86_TRAP_FIXUP
2114 Dr_kui: DR_TRAP_FIXUP
2115 _KiUnexpectedInterruptTail:
2117 /* Enter interrupt trap */
2118 INT_PROLOG kui, DoNotPushFakeErrorCode
2120 /* Increase interrupt count */
2121 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2123 /* Put vector in EBX and make space for KIRQL */
2127 /* Begin interrupt */
2131 call _HalBeginSystemInterrupt@12
2133 /* Check if it was spurious or not */
2137 /* Spurious, ignore it */
2139 jmp _Kei386EoiHelper2ndEntry
2142 /* Unexpected interrupt, print a message on debug builds */
2145 push offset _UnexpectedMsg
2150 /* Exit the interrupt */
2153 call _HalEndSystemInterrupt@8
2154 jmp _Kei386EoiHelper@0
2157 .globl _KiUnexpectedInterrupt
2158 _KiUnexpectedInterrupt:
2160 /* Bugcheck with invalid interrupt code */
2161 push TRAP_CAUSE_UNKNOWN
2164 /* INTERRUPT HANDLERS ********************************************************/
2166 .func KiDispatchInterrupt@0
2167 _KiDispatchInterrupt@0:
2169 /* Get the PCR and disable interrupts */
2170 mov ebx, PCR[KPCR_SELF]
2173 /* Check if we have to deliver DPCs, timers, or deferred threads */
2174 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2175 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2176 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2179 /* Save stack pointer and exception list, then clear it */
2181 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2182 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2184 /* Save the stack and switch to the DPC Stack */
2186 //mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2190 mov ecx, [ebx+KPCR_PRCB]
2191 call @KiRetireDpcList@4
2193 /* Restore stack and exception list */
2195 pop dword ptr [ebx+KPCR_EXCEPTION_LIST]
2200 /* Re-enable interrupts */
2203 /* Check if we have quantum end */
2204 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2207 /* Check if we have a thread to swap to */
2208 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2211 /* Make space on the stack to save registers */
2217 /* Get the current thread */
2218 mov edi, [ebx+KPCR_CURRENT_THREAD]
2221 #error SMP Interrupt not handled!
2224 /* Get the next thread and clear it */
2225 mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
2226 and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2228 /* Set us as the current running thread */
2229 mov [ebx+KPCR_CURRENT_THREAD], esi
2230 mov byte ptr [esi+KTHREAD_STATE], Running
2231 mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
2233 /* Put thread in ECX and get the PRCB in EDX */
2235 lea edx, [ebx+KPCR_PRCB_DATA]
2236 call @KiQueueReadyThread@8
2238 /* Set APC_LEVEL and do the swap */
2240 call @KiSwapContextInternal@0
2242 /* Restore registers */
2253 /* Disable quantum end and process it */
2254 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2255 call _KiQuantumEnd@0
2259 .func KiInterruptTemplate
2260 V86_kit: V86_TRAP_FIXUP
2261 Dr_kit: DR_TRAP_FIXUP
2262 _KiInterruptTemplate:
2264 /* Enter interrupt trap */
2265 INT_PROLOG kit, DoPushFakeErrorCode
2268 _KiInterruptTemplate2ndDispatch:
2269 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2272 _KiInterruptTemplateObject:
2273 /* Dummy jump, will be replaced by the actual jump */
2274 jmp _KeSynchronizeExecution@12
2276 _KiInterruptTemplateDispatch:
2277 /* Marks the end of the template so that the jump above can be edited */
2279 .func KiChainedDispatch2ndLvl@0
2280 _KiChainedDispatch2ndLvl@0:
2282 /* Not yet supported */
2286 .func KiChainedDispatch@0
2287 _KiChainedDispatch@0:
2289 /* Increase interrupt count */
2290 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2292 /* Save trap frame */
2295 /* Save vector and IRQL */
2296 mov eax, [edi+KINTERRUPT_VECTOR]
2297 mov ecx, [edi+KINTERRUPT_IRQL]
2303 /* Begin interrupt */
2307 call _HalBeginSystemInterrupt@12
2309 /* Check if it was handled */
2313 /* Call the 2nd-level handler */
2314 call _KiChainedDispatch2ndLvl@0
2316 /* Exit the interrupt */
2320 .func KiInterruptDispatch@0
2321 _KiInterruptDispatch@0:
2323 /* Increase interrupt count */
2324 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2326 /* Save trap frame */
2329 /* Save vector and IRQL */
2330 mov eax, [edi+KINTERRUPT_VECTOR]
2331 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2337 /* Begin interrupt */
2341 call _HalBeginSystemInterrupt@12
2343 /* Check if it was handled */
2347 /* Acquire the lock */
2349 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2350 ACQUIRE_SPINLOCK(esi, IntSpin)
2352 /* Make sure that this interrupt isn't storming */
2355 /* Save the tick count */
2356 mov ebx, _KeTickCount
2359 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2362 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2364 /* Check if the ISR timed out */
2365 add ebx, _KiISRTimeout
2366 cmp _KeTickCount, ebx
2370 /* Release the lock */
2371 RELEASE_SPINLOCK(esi)
2373 /* Exit the interrupt */
2377 /* Exit the interrupt */
2383 SPIN_ON_LOCK esi, GetIntLock
2387 /* Print warning message */
2388 push [edi+KINTERRUPT_SERVICE_ROUTINE]
2389 push offset _IsrTimeoutMsg
2393 /* Break into debugger, then continue */
2397 /* Cleanup verification */
2398 VERIFY_INT_END kid, 0