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"
96 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
105 .func KiSystemService
106 Dr_kss: DR_TRAP_FIXUP
109 /* Enter the shared system call prolog */
112 /* Jump to the actual handler */
116 .func KiFastCallEntry
117 Dr_FastCallDrSave: DR_TRAP_FIXUP
120 /* Enter the fast system call prolog */
121 FASTCALL_PROLOG FastCallDrSave
126 * Find out which table offset to use. Converts 0x1124 into 0x10.
127 * The offset is related to the Table Index as such: Offset = TableIndex x 10
130 shr edi, SERVICE_TABLE_SHIFT
131 and edi, SERVICE_TABLE_MASK
134 /* Now add the thread's base system table to the offset */
135 add edi, [esi+KTHREAD_SERVICE_TABLE]
137 /* Get the true syscall ID and check it */
139 and eax, SERVICE_NUMBER_MASK
140 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
142 /* Invalid ID, try to load Win32K Table */
143 jnb KiBBTUnexpectedRange
145 /* Check if this was Win32K */
146 cmp ecx, SERVICE_TABLE_TEST
150 mov ecx, [fs:KPCR_TEB]
152 /* Check if we should flush the User Batch */
155 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
161 //call [_KeGdiFlushUserBatch]
166 /* Increase total syscall count */
167 inc dword ptr fs:[KPCR_SYSTEM_CALLS]
170 /* Increase per-syscall count */
171 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
173 inc dword ptr [ecx+eax*4]
176 /* Users's current stack frame pointer is source */
180 /* Allocate room for argument list from kernel stack */
181 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
185 /* Get pointer to function */
186 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
189 /* Allocate space on our stack */
192 /* Set the size of the arguments and the destination */
196 /* Make sure we're within the User Probe Address */
197 cmp esi, _MmUserProbeAddress
201 /* Copy the parameters */
206 * The following lines are for the benefit of GDB. It will see the return
207 * address of the "call ebx" below, find the last label before it and
208 * thinks that that's the start of the function. It will then check to see
209 * if it starts with a standard function prolog (push ebp, mov ebp,esp1).
210 * When that standard function prolog is not found, it will stop the
211 * stack backtrace. Since we do want to backtrace into usermode, let's
212 * make GDB happy and create a standard prolog.
220 /* Do the System Call */
225 /* Make sure the user-mode call didn't return at elevated IRQL */
226 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
228 mov esi, eax /* We need to save the syscall's return val */
229 call _KeGetCurrentIrql@0
232 mov eax, esi /* Restore it */
234 /* Get our temporary current thread pointer for sanity check */
235 mov ecx, fs:[KPCR_CURRENT_THREAD]
237 /* Make sure that we are not attached and that APCs are not disabled */
238 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
241 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
248 /* Deallocate the kernel stack frame */
251 KeReturnFromSystemCall:
253 /* Get the Current Thread */
254 mov ecx, [fs:KPCR_CURRENT_THREAD]
256 /* Restore the old trap frame pointer */
257 mov edx, [ebp+KTRAP_FRAME_EDX]
258 mov [ecx+KTHREAD_TRAP_FRAME], edx
263 /* Disable interrupts */
266 /* Check for, and deliver, User-Mode APCs if needed */
267 CHECK_FOR_APC_DELIVER 1
269 /* Exit and cleanup */
270 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
273 KiBBTUnexpectedRange:
275 /* If this isn't a Win32K call, fail */
276 cmp ecx, SERVICE_TABLE_TEST
279 /* Set up Win32K Table */
282 call _PsConvertToGuiThread@0
284 /* Check return code */
287 /* Restore registers */
291 /* Reset trap frame address */
293 mov [esi+KTHREAD_TRAP_FRAME], ebp
295 /* Try the Call again, if we suceeded */
299 * The Shadow Table should have a special byte table which tells us
300 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
303 /* Get the table limit and base */
304 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
305 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
306 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
308 /* Get the table address and add our index into the array */
310 and eax, SERVICE_NUMBER_MASK
313 /* Find out what we should return */
314 movsx eax, byte ptr [edx]
317 /* Return either 0 or -1, we've set it in EAX */
318 jle KeReturnFromSystemCall
320 /* Set STATUS_INVALID_SYSTEM_SERVICE */
321 mov eax, STATUS_INVALID_SYSTEM_SERVICE
322 jmp KeReturnFromSystemCall
326 /* Invalid System Call */
327 mov eax, STATUS_INVALID_SYSTEM_SERVICE
328 jmp KeReturnFromSystemCall
332 /* Check if this came from kernel-mode */
333 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
335 /* It's fine, go ahead with it */
338 /* Caller sent invalid parameters, fail here */
339 mov eax, STATUS_ACCESS_VIOLATION
344 /* Restore ESP0 stack */
345 mov ecx, [fs:KPCR_TSS]
346 mov esp, ss:[ecx+KTSS_ESP0]
348 /* Generate V86M Stack for Trap 6 */
354 /* Generate interrupt stack for Trap 6 */
355 push KGDT_R3_DATA + RPL_MASK
358 push KGDT_R3_CODE + RPL_MASK
364 /* Save current IRQL */
367 /* Set us at passive */
368 mov dword ptr fs:[KPCR_IRQL], 0
376 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
377 call _KeBugCheckEx@20
381 /* Get the index and APC state */
382 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
383 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
390 push APC_INDEX_MISMATCH
391 call _KeBugCheckEx@20
398 /* Disable interrupts */
401 /* Check for, and deliver, User-Mode APCs if needed */
402 CHECK_FOR_APC_DELIVER 0
404 /* Exit and cleanup */
405 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
408 .func Kei386EoiHelper@0
411 /* Disable interrupts */
414 /* Check for, and deliver, User-Mode APCs if needed */
415 CHECK_FOR_APC_DELIVER 0
417 /* Exit and cleanup */
418 _Kei386EoiHelper2ndEntry:
419 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
423 /* Move to EDX position */
424 add esp, KTRAP_FRAME_EDX
426 /* Restore volatiles */
431 /* Move to non-volatiles */
432 lea esp, [ebp+KTRAP_FRAME_EDI]
438 /* Skip error code and return */
447 Dr_kids: DR_TRAP_FIXUP
448 V86_kids: V86_TRAP_FIXUP
451 /* Push error code */
457 /* Increase EIP so we skip the INT3 */
458 //inc dword ptr [ebp+KTRAP_FRAME_EIP]
460 /* Call debug service dispatcher */
461 mov eax, [ebp+KTRAP_FRAME_EAX]
462 mov ecx, [ebp+KTRAP_FRAME_ECX]
463 mov edx, [ebp+KTRAP_FRAME_EAX]
465 /* Check for V86 mode */
466 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
469 /* Check if this is kernel or user-mode */
470 test byte ptr [ebp+KTRAP_FRAME_CS], 1
472 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
475 /* Re-enable interrupts */
479 /* Call the debug routine */
488 call _KdpServiceDispatcher@12
492 /* Get the current process */
493 mov ebx, [fs:KPCR_CURRENT_THREAD]
494 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
496 /* Check if this is a VDM Process */
497 //cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
500 /* Exit through common routine */
501 jmp _Kei386EoiHelper@0
504 .func NtRaiseException@12
505 _NtRaiseException@12:
507 /* NOTE: We -must- be called by Zw* to have the right frame! */
508 /* Push the stack frame */
511 /* Get the current thread and restore its trap frame */
512 mov ebx, [fs:KPCR_CURRENT_THREAD]
513 mov edx, [ebp+KTRAP_FRAME_EDX]
514 mov [ebx+KTHREAD_TRAP_FRAME], edx
516 /* Set up stack frame */
519 /* Get the Trap Frame in EBX */
522 /* Get the exception list and restore */
523 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
524 mov [fs:KPCR_EXCEPTION_LIST], eax
526 /* Get the parameters */
527 mov edx, [ebp+16] /* Search frames */
528 mov ecx, [ebp+12] /* Context */
529 mov eax, [ebp+8] /* Exception Record */
531 /* Raise the exception */
537 call _KiRaiseException@20
539 /* Restore trap frame in EBP */
543 /* Check the result */
547 /* Restore debug registers too */
554 /* NOTE: We -must- be called by Zw* to have the right frame! */
555 /* Push the stack frame */
558 /* Get the current thread and restore its trap frame */
559 mov ebx, [fs:KPCR_CURRENT_THREAD]
560 mov edx, [ebp+KTRAP_FRAME_EDX]
561 mov [ebx+KTHREAD_TRAP_FRAME], edx
563 /* Set up stack frame */
566 /* Save the parameters */
570 /* Call KiContinue */
576 /* Check if we failed (bad context record) */
580 /* Check if test alert was requested */
581 cmp dword ptr [ebp+12], 0
584 /* Test alert for the thread */
585 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
587 call _KeTestAlertThread@4
590 /* Return to previous context */
601 /* EXCEPTION DISPATCHERS *****************************************************/
603 .func CommonDispatchException
604 _CommonDispatchException:
606 /* Make space for an exception record */
607 sub esp, EXCEPTION_RECORD_LENGTH
610 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
612 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
613 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
614 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
615 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
617 /* Check parameter count */
621 /* Get information */
622 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
629 /* Set the record in ECX and check if this was V86 */
631 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
640 /* Calculate the previous mode */
641 mov eax, [ebp+KTRAP_FRAME_CS]
645 /* Dispatch the exception */
651 call _KiDispatchException@20
655 jmp _Kei386EoiHelper@0
658 .func DispatchNoParam
660 /* Call the common dispatcher */
662 call _CommonDispatchException
665 .func DispatchOneParam
667 /* Call the common dispatcher */
670 call _CommonDispatchException
673 .func DispatchTwoParam
675 /* Call the common dispatcher */
678 call _CommonDispatchException
681 /* HARDWARE TRAP HANDLERS ****************************************************/
686 /* TODO: Routine to fixup a KTRAP_FRAME when faulting from a syscall. */
691 Dr_kit0: DR_TRAP_FIXUP
692 V86_kit0: V86_TRAP_FIXUP
694 /* Push error code */
701 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
704 /* Check if the frame was from kernelmode */
705 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
708 /* Check the old mode */
709 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
713 /* Re-enable interrupts for user-mode and send the exception */
715 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
716 mov ebx, [ebp+KTRAP_FRAME_EIP]
720 /* Check if this is a VDM process */
721 mov ebx, [fs:KPCR_CURRENT_THREAD]
722 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
723 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
726 /* We don't support this yet! */
733 Dr_kit1: DR_TRAP_FIXUP
734 V86_kit1: V86_TRAP_FIXUP
736 /* Push error code */
743 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
746 /* Check if the frame was from kernelmode */
747 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
750 /* Check the old mode */
751 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
755 /* Enable interrupts for user-mode */
759 /* Prepare the exception */
760 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
761 mov ebx, [ebp+KTRAP_FRAME_EIP]
762 mov eax, STATUS_SINGLE_STEP
766 /* Check if this is a VDM process */
767 mov ebx, [fs:KPCR_CURRENT_THREAD]
768 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
769 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
772 /* We don't support VDM! */
780 /* FIXME: This is an NMI, nothing like a normal exception */
782 jmp _KiSystemFatalException
786 Dr_kit3: DR_TRAP_FIXUP
787 V86_kit3: V86_TRAP_FIXUP
789 /* Push error code */
796 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
799 /* Check if the frame was from kernelmode */
800 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
803 /* Check the old mode */
804 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
808 /* Enable interrupts for user-mode */
812 /* Prepare the exception */
817 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
818 mov ebx, [ebp+KTRAP_FRAME_EIP]
820 mov eax, STATUS_BREAKPOINT
822 call _CommonDispatchException
825 /* Check if this is a VDM process */
826 mov ebx, [fs:KPCR_CURRENT_THREAD]
827 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
828 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
831 /* We don't support VDM! */
836 Dr_kit4: DR_TRAP_FIXUP
837 V86_kit4: V86_TRAP_FIXUP
839 /* Push error code */
846 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
849 /* Check if the frame was from kernelmode */
850 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
853 /* Check the old mode */
854 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
858 /* Re-enable interrupts for user-mode and send the exception */
860 mov eax, STATUS_INTEGER_OVERFLOW
861 mov ebx, [ebp+KTRAP_FRAME_EIP]
866 /* Check if this is a VDM process */
867 mov ebx, [fs:KPCR_CURRENT_THREAD]
868 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
869 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
872 /* We don't support this yet! */
878 Dr_kit5: DR_TRAP_FIXUP
879 V86_kit5: V86_TRAP_FIXUP
881 /* Push error code */
888 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
891 /* Check if the frame was from kernelmode */
892 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
895 /* It did, and this should never happen */
897 jmp _KiSystemFatalException
899 /* Check the old mode */
901 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
904 /* Re-enable interrupts for user-mode and send the exception */
907 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
908 mov ebx, [ebp+KTRAP_FRAME_EIP]
912 /* Check if this is a VDM process */
913 mov ebx, [fs:KPCR_CURRENT_THREAD]
914 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
915 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
918 /* We don't support this yet! */
924 Dr_kit6: DR_TRAP_FIXUP
925 V86_kit6: V86_TRAP_FIXUP
928 /* It this a V86 GPF? */
929 test dword ptr [esp+8], EFLAGS_V86_MASK
935 /* Not yet supported (Invalid OPCODE from V86) */
939 /* Push error code */
945 /* Check if this happened in kernel mode */
946 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
950 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
953 /* Check if the process is vDM */
954 mov ebx, fs:[KPCR_CURRENT_THREAD]
955 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
956 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
960 /* Get EIP and enable interrupts at this point */
961 mov esi, [ebp+KTRAP_FRAME_EIP]
964 /* Set intruction prefix length */
967 /* Setup a SEH frame */
970 push fs:[KPCR_EXCEPTION_LIST]
971 mov fs:[KPCR_EXCEPTION_LIST], esp
974 /* Get the instruction and check if it's LOCK */
984 pop fs:[KPCR_EXCEPTION_LIST]
989 /* Re-enable interrupts */
992 /* Setup illegal instruction exception and dispatch it */
993 mov ebx, [ebp+KTRAP_FRAME_EIP]
994 mov eax, STATUS_ILLEGAL_INSTRUCTION
1000 pop fs:[KPCR_EXCEPTION_LIST]
1003 /* Setup invalid lock exception and dispatch it */
1004 mov ebx, [ebp+KTRAP_FRAME_EIP]
1005 mov eax, STATUS_INVALID_LOCK_SEQUENCE
1006 jmp _DispatchNoParam
1013 /* Return to caller */
1014 jmp _Kei386EoiHelper@0
1020 pop fs:[KPCR_EXCEPTION_LIST]
1024 /* Check if this was user mode */
1025 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1034 push KMODE_EXCEPTION_NOT_HANDLED
1035 call _KeBugCheckWithTf@24
1039 Dr_kit7: DR_TRAP_FIXUP
1040 V86_kit7: V86_TRAP_FIXUP
1042 /* Push error code */
1048 /* Get the current thread and stack */
1050 mov eax, [fs:KPCR_CURRENT_THREAD]
1051 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1052 sub ecx, NPX_FRAME_LENGTH
1054 /* Check if emulation is enabled */
1055 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1056 jnz EmulationEnabled
1059 /* Check if the NPX state is loaded */
1060 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1065 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1068 /* Check the NPX thread */
1069 mov edx, [fs:KPCR_NPX_THREAD]
1073 /* Get the NPX Stack */
1074 mov esi, [edx+KTHREAD_INITIAL_STACK]
1075 sub esi, NPX_FRAME_LENGTH
1077 /* Check if we have FXSR and check which operand to use */
1078 test byte ptr _KeI386FxsrPresent, 1
1087 /* Set the thread's state to dirty */
1088 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1091 /* Check if we have FXSR and choose which operand to use */
1092 test byte ptr _KeI386FxsrPresent, 1
1101 /* Set state loaded */
1102 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1103 mov [fs:KPCR_NPX_THREAD], eax
1105 /* Enable interrupts to happen now */
1109 /* Check if CR0 needs to be reloaded due to a context switch */
1110 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1111 jz _Kei386EoiHelper@0
1113 /* We have to reload CR0... disable interrupts */
1116 /* Get CR0 and update it */
1118 or ebx, [ecx+FN_CR0_NPX_STATE]
1121 /* Restore interrupts and check if TS is back on */
1124 jz _Kei386EoiHelper@0
1126 /* Clear TS, and loop handling again */
1133 /* Set delayed error */
1134 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1136 /* Check if this happened during restore */
1137 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1140 /* Skip instruction and dispatch the exception */
1141 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1142 jmp _Kei386EoiHelper@0
1145 /* Check if TS is set */
1147 jnz TsSetOnLoadedState
1150 /* Check if the trap came from V86 mode */
1151 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1154 /* Check if it came from kernel mode */
1155 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1158 /* Check if it came from a VDM */
1159 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1163 /* Get the current thread */
1164 mov eax, fs:[KPCR_CURRENT_THREAD]
1166 /* Check NPX state */
1167 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1169 /* Get the NPX save area */
1170 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1171 lea ecx, [ecx-NPX_FRAME_LENGTH]
1178 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1181 /* Check if we have FX support */
1182 test byte ptr _KeI386FxsrPresent, 1
1185 /* Save the state */
1193 /* Make CR0 state not loaded */
1194 or ebx, NPX_STATE_NOT_LOADED
1195 or ebx, [ecx+FN_CR0_NPX_STATE]
1198 /* Update NPX state */
1199 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1200 mov dword ptr fs:[KPCR_NPX_THREAD], 0
1203 /* Clear the TS bit and re-enable interrupts */
1204 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1207 /* Check if we have FX support */
1208 test byte ptr _KeI386FxsrPresent, 1
1211 /* Get error offset, control and status words */
1212 mov ebx, [ecx+FX_ERROR_OFFSET]
1213 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1214 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1216 /* Get the faulting opcode */
1217 mov esi, [ecx+FX_DATA_OFFSET]
1221 /* Get error offset, control and status words */
1222 mov ebx, [ecx+FP_ERROR_OFFSET]
1223 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1224 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1226 /* Get the faulting opcode */
1227 mov esi, [ecx+FP_DATA_OFFSET]
1230 /* Mask exceptions */
1235 /* Check if what's left is invalid */
1239 /* Check if it was a stack fault */
1243 /* Raise exception */
1244 mov eax, STATUS_FLOAT_INVALID_OPERATION
1245 jmp _DispatchOneParam
1249 /* Raise exception */
1250 mov eax, STATUS_FLOAT_STACK_CHECK
1251 jmp _DispatchTwoParam
1255 /* Check for divide by 0 */
1259 /* Raise exception */
1260 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1261 jmp _DispatchOneParam
1264 /* Check for denormal */
1268 /* Raise exception */
1269 mov eax, STATUS_FLOAT_INVALID_OPERATION
1270 jmp _DispatchOneParam
1273 /* Check for overflow */
1277 /* Raise exception */
1278 mov eax, STATUS_FLOAT_OVERFLOW
1279 jmp _DispatchOneParam
1282 /* Check for underflow */
1286 /* Raise exception */
1287 mov eax, STATUS_FLOAT_UNDERFLOW
1288 jmp _DispatchOneParam
1291 /* Check for precision fault */
1295 /* Raise exception */
1296 mov eax, STATUS_FLOAT_INEXACT_RESULT
1297 jmp _DispatchOneParam
1301 /* Strange result, bugcheck the OS */
1308 push TRAP_CAUSE_UNKNOWN
1309 call _KeBugCheckWithTf@24
1312 /* Check if this is a VDM */
1313 mov eax, fs:[KPCR_CURRENT_THREAD]
1314 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1315 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1318 /* V86 NPX not handled */
1322 /* Did this come from kernel-mode? */
1323 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1326 /* It came from user-mode, so this would only be valid inside a VDM */
1327 /* Since we don't actually have VDMs in ROS, bugcheck. */
1331 /* TS shouldn't be set, unless this we don't have a Math Processor */
1335 /* Strange that we got a trap at all, but ignore and continue */
1337 jmp _Kei386EoiHelper@0
1340 /* Cause a bugcheck */
1345 push TRAP_CAUSE_UNKNOWN
1346 call _KeBugCheckEx@20
1353 /* Can't really do too much */
1355 jmp _KiSystemFatalException
1359 Dr_kit9: DR_TRAP_FIXUP
1360 V86_kit9: V86_TRAP_FIXUP
1362 /* Push error code */
1368 /* Enable interrupts and bugcheck */
1371 jmp _KiSystemFatalException
1375 Dr_kit10: DR_TRAP_FIXUP
1376 V86_kit10: V86_TRAP_FIXUP
1382 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1385 /* Check if the frame was from kernelmode */
1386 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1390 /* Check if OF was set during iretd */
1391 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1395 /* It was, just mask it out */
1396 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1397 jmp _Kei386EoiHelper@0
1400 /* TSS failure for some other reason: crash */
1402 jmp _KiSystemFatalException
1406 Dr_kit11: DR_TRAP_FIXUP
1407 V86_kit11: V86_TRAP_FIXUP
1412 /* FIXME: ROS Doesn't handle segment faults yet */
1414 jmp _KiSystemFatalException
1418 Dr_kit12: DR_TRAP_FIXUP
1419 V86_kit12: V86_TRAP_FIXUP
1424 /* FIXME: ROS Doesn't handle stack faults yet */
1426 jmp _KiSystemFatalException
1430 Dr_kitd: DR_TRAP_FIXUP
1431 V86_kitd: V86_TRAP_FIXUP
1434 /* It this a V86 GPF? */
1435 test dword ptr [esp+12], EFLAGS_V86_MASK
1438 /* Enter V86 Trap */
1439 V86_TRAP_PROLOG kitd
1441 /* Make sure that this is a V86 process */
1442 mov ecx, [fs: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
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 fs:[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, [fs: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]
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, fs:[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 /* Check what kind of instruction this is */
1684 mov eax, [ebp+KTRAP_FRAME_EIP]
1687 /* FIXME: Check for BOP4 */
1689 /* Check if this is POP FS */
1691 add edx, KTRAP_FRAME_FS
1695 /* Check if this is POP GS */
1696 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1700 CheckPrivilegedInstruction:
1704 CheckPrivilegedInstruction2:
1718 Dr_kit14: DR_TRAP_FIXUP
1719 V86_kit14: V86_TRAP_FIXUP
1725 /* Check if we have a VDM alert */
1726 cmp dword ptr fs:[KPCR_VDM_ALERT], 0
1729 /* Get the current thread */
1730 mov edi, fs:[KPCR_CURRENT_THREAD]
1732 /* Get the stack address of the frame */
1733 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1734 sub eax, [edi+KTHREAD_INITIAL_STACK]
1737 /* This isn't the base frame, check if it's the second */
1738 cmp eax, -KTRAP_FRAME_EFLAGS
1741 /* Check if we have a TEB */
1742 mov eax, fs:[KPCR_TEB]
1746 /* Fixup the frame */
1753 /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
1754 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1757 /* Enable interrupts and check if we got here with interrupts disabled */
1759 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1763 /* Send trap frame and check if this is kernel-mode or usermode */
1765 mov eax, [ebp+KTRAP_FRAME_CS]
1769 /* Send faulting address and check if this is read or write */
1771 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1775 /* Call the access fault handler */
1776 call _MmAccessFault@16
1780 /* Access fault handled, return to caller */
1781 jmp _Kei386EoiHelper@0
1784 /* First check if this is a fault in the S-LIST functions */
1785 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1786 cmp [ebp+KTRAP_FRAME_EIP], ecx
1789 /* Check if this is a fault in the syscall handler */
1790 mov ecx, offset CopyParams
1791 cmp [ebp+KTRAP_FRAME_EIP], ecx
1793 mov ecx, offset ReadBatch
1794 cmp [ebp+KTRAP_FRAME_EIP], ecx
1799 jmp _Kei386EoiHelper@0
1804 jmp _Kei386EoiHelper@0
1806 /* Check if the fault occured in a V86 mode */
1808 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
1811 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1814 /* Check if the fault occured in a VDM */
1815 mov esi, fs:[KPCR_CURRENT_THREAD]
1816 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
1817 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
1820 /* Check if we this was in kernel-mode */
1821 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1823 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1830 /* Save EIP and check what kind of status failure we got */
1832 mov esi, [ebp+KTRAP_FRAME_EIP]
1833 cmp eax, STATUS_ACCESS_VIOLATION
1835 cmp eax, STATUS_GUARD_PAGE_VIOLATION
1837 cmp eax, STATUS_STACK_OVERFLOW
1840 /* Setup an in-page exception to dispatch */
1846 mov eax, STATUS_IN_PAGE_ERROR
1847 call _CommonDispatchException
1850 /* Use more proper status code */
1851 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
1854 /* Setup a normal page fault exception */
1858 jmp _DispatchTwoParam
1866 /* This is completely illegal, bugcheck the system */
1872 push IRQL_NOT_LESS_OR_EQUAL
1873 call _KeBugCheckWithTf@24
1877 /* FIXME: NOT SUPPORTED */
1882 Dr_kit15: DR_TRAP_FIXUP
1883 V86_kit15: V86_TRAP_FIXUP
1885 /* Push error code */
1892 /* Raise a fatal exception */
1894 jmp _KiSystemFatalException
1898 Dr_kit16: DR_TRAP_FIXUP
1899 V86_kit16: V86_TRAP_FIXUP
1901 /* Push error code */
1907 /* Check if this is the NPX Thread */
1908 mov eax, fs:[KPCR_CURRENT_THREAD]
1909 cmp eax, fs:[KPCR_NPX_THREAD]
1911 /* Get the initial stack and NPX frame */
1912 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1913 lea ecx, [ecx-NPX_FRAME_LENGTH]
1915 /* If this is a valid fault, handle it */
1918 /* Otherwise, re-enable interrupts and set delayed error */
1920 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1921 jmp _Kei386EoiHelper@0
1925 Dr_kit17: DR_TRAP_FIXUP
1926 V86_kit17: V86_TRAP_FIXUP
1928 /* Push error code */
1934 /* FIXME: ROS Doesn't handle alignment faults yet */
1936 jmp _KiSystemFatalException
1939 .func KiSystemFatalException
1940 _KiSystemFatalException:
1942 /* Push the trap frame */
1945 /* Push empty parameters */
1950 /* Push trap number and bugcheck code */
1952 push UNEXPECTED_KERNEL_MODE_TRAP
1953 call _KeBugCheckWithTf@24
1957 .func KiCoprocessorError@0
1958 _KiCoprocessorError@0:
1960 /* Get the NPX Thread's Initial stack */
1961 mov eax, [fs:KPCR_NPX_THREAD]
1962 mov eax, [eax+KTHREAD_INITIAL_STACK]
1964 /* Make space for the FPU Save area */
1965 sub eax, SIZEOF_FX_SAVE_AREA
1967 /* Set the CR0 State */
1968 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
1975 /* Return to caller */
1979 .func Ki16BitStackException
1980 _Ki16BitStackException:
1986 /* Go to kernel mode thread stack */
1987 mov eax, fs:[KPCR_CURRENT_THREAD]
1988 add esp, [eax+KTHREAD_INITIAL_STACK]
1990 /* Switch to good stack segment */
1994 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
1996 .globl _KiStartUnexpectedRange@0
1997 _KiStartUnexpectedRange@0:
1999 GENERATE_INT_HANDLERS
2001 .globl _KiEndUnexpectedRange@0
2002 _KiEndUnexpectedRange@0:
2003 jmp _KiUnexpectedInterruptTail
2005 .func KiUnexpectedInterruptTail
2006 V86_kui: V86_TRAP_FIXUP
2007 Dr_kui: DR_TRAP_FIXUP
2008 _KiUnexpectedInterruptTail:
2010 /* Enter interrupt trap */
2011 INT_PROLOG kui, DoNotPushFakeErrorCode
2013 /* Increase interrupt count */
2014 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2016 /* Put vector in EBX and make space for KIRQL */
2020 /* Begin interrupt */
2024 call _HalBeginSystemInterrupt@12
2026 /* Check if it was spurious or not */
2030 /* Spurious, ignore it */
2032 jmp _Kei386EoiHelper2ndEntry
2035 /* Unexpected interrupt, print a message on debug builds */
2038 push offset _UnexpectedMsg
2043 /* Exit the interrupt */
2046 call _HalEndSystemInterrupt@8
2047 jmp _Kei386EoiHelper@0
2050 .globl _KiUnexpectedInterrupt
2051 _KiUnexpectedInterrupt:
2053 /* Bugcheck with invalid interrupt code */
2054 push TRAP_CAUSE_UNKNOWN
2057 /* INTERRUPT HANDLERS ********************************************************/
2059 .func KiDispatchInterrupt@0
2060 _KiDispatchInterrupt@0:
2062 /* Get the PCR and disable interrupts */
2063 mov ebx, [fs:KPCR_SELF]
2066 /* Check if we have to deliver DPCs, timers, or deferred threads */
2067 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2068 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2069 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2072 /* Save stack pointer and exception list, then clear it */
2074 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2075 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2077 /* Save the stack and switch to the DPC Stack */
2079 //mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2083 mov ecx, [ebx+KPCR_PRCB]
2084 call @KiRetireDpcList@4
2086 /* Restore stack and exception list */
2088 pop dword ptr [ebx+KPCR_EXCEPTION_LIST]
2093 /* Re-enable interrupts */
2096 /* Check if we have quantum end */
2097 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2100 /* Check if we have a thread to swap to */
2101 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2104 /* Make space on the stack to save registers */
2110 /* Get the current thread */
2111 mov edi, [ebx+KPCR_CURRENT_THREAD]
2114 #error SMP Interrupt not handled!
2117 /* Get the next thread and clear it */
2118 mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
2119 and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2121 /* Set us as the current running thread */
2122 mov [ebx+KPCR_CURRENT_THREAD], esi
2123 mov byte ptr [esi+KTHREAD_STATE], Running
2124 mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
2126 /* Put thread in ECX and get the PRCB in EDX */
2128 lea edx, [ebx+KPCR_PRCB_DATA]
2129 call @KiQueueReadyThread@8
2131 /* Set APC_LEVEL and do the swap */
2133 call @KiSwapContextInternal@0
2135 /* Restore registers */
2146 /* Disable quantum end and process it */
2147 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2148 call _KiQuantumEnd@0
2152 .func KiInterruptTemplate
2153 V86_kit: V86_TRAP_FIXUP
2154 Dr_kit: DR_TRAP_FIXUP
2155 _KiInterruptTemplate:
2157 /* Enter interrupt trap */
2158 INT_PROLOG kit, DoPushFakeErrorCode
2161 _KiInterruptTemplate2ndDispatch:
2162 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2165 _KiInterruptTemplateObject:
2166 /* Dummy jump, will be replaced by the actual jump */
2167 jmp _KeSynchronizeExecution@12
2169 _KiInterruptTemplateDispatch:
2170 /* Marks the end of the template so that the jump above can be edited */
2172 .func KiChainedDispatch2ndLvl@0
2173 _KiChainedDispatch2ndLvl@0:
2175 /* Not yet supported */
2179 .func KiChainedDispatch@0
2180 _KiChainedDispatch@0:
2182 /* Increase interrupt count */
2183 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2185 /* Save trap frame */
2188 /* Save vector and IRQL */
2189 mov eax, [edi+KINTERRUPT_VECTOR]
2190 mov ecx, [edi+KINTERRUPT_IRQL]
2196 /* Begin interrupt */
2200 call _HalBeginSystemInterrupt@12
2202 /* Check if it was handled */
2206 /* Call the 2nd-level handler */
2207 call _KiChainedDispatch2ndLvl@0
2209 /* Exit the interrupt */
2212 call _HalEndSystemInterrupt@8
2213 jmp _Kei386EoiHelper@0
2216 .func KiInterruptDispatch@0
2217 _KiInterruptDispatch@0:
2219 /* Increase interrupt count */
2220 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
2222 /* Save trap frame */
2225 /* Save vector and IRQL */
2226 mov eax, [edi+KINTERRUPT_VECTOR]
2227 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2233 /* Begin interrupt */
2237 call _HalBeginSystemInterrupt@12
2239 /* Check if it was handled */
2243 /* Acquire the lock */
2245 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2246 ACQUIRE_SPINLOCK(esi, IntSpin)
2249 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2252 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2254 /* Release the lock */
2255 RELEASE_SPINLOCK(esi)
2257 /* Exit the interrupt */
2259 call _HalEndSystemInterrupt@8
2260 jmp _Kei386EoiHelper@0
2263 /* Exit the interrupt */
2265 jmp _Kei386EoiHelper@0
2269 SPIN_ON_LOCK esi, GetIntLock