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 ***********************************************/
138 .func KiSystemService
139 TRAP_FIXUPS kss_a, kss_t, DoNotFixupV86, DoNotFixupAbios
142 /* Enter the shared system call prolog */
143 SYSCALL_PROLOG kss_a, kss_t
145 /* Jump to the actual handler */
149 .func KiFastCallEntry
150 TRAP_FIXUPS FastCallDrSave, FastCallDrReturn, DoNotFixupV86, DoNotFixupAbios
153 /* Enter the fast system call prolog */
154 FASTCALL_PROLOG FastCallDrSave, FastCallDrReturn
159 * Find out which table offset to use. Converts 0x1124 into 0x10.
160 * The offset is related to the Table Index as such: Offset = TableIndex x 10
163 shr edi, SERVICE_TABLE_SHIFT
164 and edi, SERVICE_TABLE_MASK
167 /* Now add the thread's base system table to the offset */
168 add edi, [esi+KTHREAD_SERVICE_TABLE]
170 /* Get the true syscall ID and check it */
172 and eax, SERVICE_NUMBER_MASK
173 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
175 /* Invalid ID, try to load Win32K Table */
176 jnb KiBBTUnexpectedRange
178 /* Check if this was Win32K */
179 cmp ecx, SERVICE_TABLE_TEST
183 mov ecx, PCR[KPCR_TEB]
185 /* Check if we should flush the User Batch */
188 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
194 call [_KeGdiFlushUserBatch]
199 /* Increase total syscall count */
200 inc dword ptr PCR[KPCR_SYSTEM_CALLS]
203 /* Increase per-syscall count */
204 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
206 inc dword ptr [ecx+eax*4]
209 /* Users's current stack frame pointer is source */
213 /* Allocate room for argument list from kernel stack */
214 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
218 /* Get pointer to function */
219 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
222 /* Allocate space on our stack */
225 /* Set the size of the arguments and the destination */
229 /* Make sure we're within the User Probe Address */
230 cmp esi, _MmUserProbeAddress
234 /* Copy the parameters */
237 /* Do the System Call */
242 /* Make sure the user-mode call didn't return at elevated IRQL */
243 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
245 mov esi, eax /* We need to save the syscall's return val */
246 call _KeGetCurrentIrql@0
249 mov eax, esi /* Restore it */
251 /* Get our temporary current thread pointer for sanity check */
252 mov ecx, PCR[KPCR_CURRENT_THREAD]
254 /* Make sure that we are not attached and that APCs are not disabled */
255 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
258 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
265 /* Deallocate the kernel stack frame */
268 KeReturnFromSystemCall:
270 /* Get the Current Thread */
271 mov ecx, PCR[KPCR_CURRENT_THREAD]
273 /* Restore the old trap frame pointer */
274 mov edx, [ebp+KTRAP_FRAME_EDX]
275 mov [ecx+KTHREAD_TRAP_FRAME], edx
280 /* Disable interrupts */
283 /* Check for, and deliver, User-Mode APCs if needed */
284 CHECK_FOR_APC_DELIVER 1
286 /* Exit and cleanup */
287 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
290 KiBBTUnexpectedRange:
292 /* If this isn't a Win32K call, fail */
293 cmp ecx, SERVICE_TABLE_TEST
296 /* Set up Win32K Table */
299 call _PsConvertToGuiThread@0
301 /* Check return code */
304 /* Restore registers */
308 /* Reset trap frame address */
310 mov [esi+KTHREAD_TRAP_FRAME], ebp
312 /* Try the Call again, if we suceeded */
316 * The Shadow Table should have a special byte table which tells us
317 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
320 /* Get the table limit and base */
321 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
322 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
323 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
325 /* Get the table address and add our index into the array */
327 and eax, SERVICE_NUMBER_MASK
330 /* Find out what we should return */
331 movsx eax, byte ptr [edx]
334 /* Return either 0 or -1, we've set it in EAX */
335 jle KeReturnFromSystemCall
337 /* Set STATUS_INVALID_SYSTEM_SERVICE */
338 mov eax, STATUS_INVALID_SYSTEM_SERVICE
339 jmp KeReturnFromSystemCall
343 /* Invalid System Call */
344 mov eax, STATUS_INVALID_SYSTEM_SERVICE
345 jmp KeReturnFromSystemCall
349 /* Check if this came from kernel-mode */
350 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
352 /* It's fine, go ahead with it */
355 /* Caller sent invalid parameters, fail here */
356 mov eax, STATUS_ACCESS_VIOLATION
361 /* Restore ESP0 stack */
362 mov ecx, PCR[KPCR_TSS]
363 mov esp, ss:[ecx+KTSS_ESP0]
365 /* Generate V86M Stack for Trap 6 */
371 /* Generate interrupt stack for Trap 6 */
372 push KGDT_R3_DATA + RPL_MASK
375 push KGDT_R3_CODE + RPL_MASK
381 /* Save current IRQL */
384 /* Set us at passive */
385 mov dword ptr PCR[KPCR_IRQL], 0
393 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
394 call _KeBugCheckEx@20
398 /* Get the index and APC state */
399 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
400 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
407 push APC_INDEX_MISMATCH
408 call _KeBugCheckEx@20
415 /* Disable interrupts */
418 /* Check for, and deliver, User-Mode APCs if needed */
419 CHECK_FOR_APC_DELIVER 0
421 /* Exit and cleanup */
422 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
425 .func Kei386EoiHelper@0
428 /* Disable interrupts */
431 /* Check for, and deliver, User-Mode APCs if needed */
432 CHECK_FOR_APC_DELIVER 0
434 /* Exit and cleanup */
435 _Kei386EoiHelper2ndEntry:
436 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
440 /* Move to EDX position */
441 add esp, KTRAP_FRAME_EDX
443 /* Restore volatiles */
448 /* Move to non-volatiles */
449 lea esp, [ebp+KTRAP_FRAME_EDI]
455 /* Skip error code and return */
463 .func KiRaiseAssertion
464 TRAP_FIXUPS kira_a, kira_t, DoFixupV86, DoFixupAbios
467 /* Push error code */
471 TRAP_PROLOG kira_a, kira_t
474 * Modify EIP so it points to the faulting instruction and set it as the
475 * exception address. Note that the 'int 2C' instruction used for this call
476 * is 2 bytes long as opposed to 1 byte 'int 3'.
478 sub dword ptr [ebp+KTRAP_FRAME_EIP], 2
479 mov ebx, [ebp+KTRAP_FRAME_EIP]
481 /* Raise an assertion failure */
482 mov eax, STATUS_ASSERTION_FAILURE
487 TRAP_FIXUPS kids_a, kids_t, DoFixupV86, DoFixupAbios
490 /* Push error code */
494 TRAP_PROLOG kids_a, kids_t
496 /* Increase EIP so we skip the INT3 */
497 inc dword ptr [ebp+KTRAP_FRAME_EIP]
499 /* Call debug service dispatcher */
500 mov eax, [ebp+KTRAP_FRAME_EAX]
501 mov ecx, [ebp+KTRAP_FRAME_ECX]
502 mov edx, [ebp+KTRAP_FRAME_EDX]
504 /* Jump to INT3 handler */
508 .func NtRaiseException@12
509 _NtRaiseException@12:
511 /* NOTE: We -must- be called by Zw* to have the right frame! */
512 /* Push the stack frame */
515 /* Get the current thread and restore its trap frame */
516 mov ebx, PCR[KPCR_CURRENT_THREAD]
517 mov edx, [ebp+KTRAP_FRAME_EDX]
518 mov [ebx+KTHREAD_TRAP_FRAME], edx
520 /* Set up stack frame */
523 /* Get the Trap Frame in EBX */
526 /* Get the exception list and restore */
527 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
528 mov PCR[KPCR_EXCEPTION_LIST], eax
530 /* Get the parameters */
531 mov edx, [ebp+16] /* Search frames */
532 mov ecx, [ebp+12] /* Context */
533 mov eax, [ebp+8] /* Exception Record */
535 /* Raise the exception */
541 call _KiRaiseException@20
543 /* Restore trap frame in EBP */
547 /* Check the result */
551 /* Restore debug registers too */
558 /* NOTE: We -must- be called by Zw* to have the right frame! */
559 /* Push the stack frame */
562 /* Get the current thread and restore its trap frame */
563 mov ebx, PCR[KPCR_CURRENT_THREAD]
564 mov edx, [ebp+KTRAP_FRAME_EDX]
565 mov [ebx+KTHREAD_TRAP_FRAME], edx
567 /* Set up stack frame */
570 /* Save the parameters */
574 /* Call KiContinue */
580 /* Check if we failed (bad context record) */
584 /* Check if test alert was requested */
585 cmp dword ptr [ebp+12], 0
588 /* Test alert for the thread */
589 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
591 call _KeTestAlertThread@4
594 /* Return to previous context */
605 /* EXCEPTION DISPATCHERS *****************************************************/
607 .func CommonDispatchException
608 _CommonDispatchException:
610 /* Make space for an exception record */
611 sub esp, EXCEPTION_RECORD_LENGTH
614 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
616 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
617 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
618 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
619 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
621 /* Check parameter count */
625 /* Get information */
626 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
633 /* Set the record in ECX and check if this was V86 */
635 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
644 /* Get the caller's CS */
645 mov eax, [ebp+KTRAP_FRAME_CS]
648 /* Check if it was user-mode or kernel-mode */
651 /* Dispatch the exception */
657 call _KiDispatchException@20
661 jmp _Kei386EoiHelper@0
664 .func DispatchNoParam
666 /* Call the common dispatcher */
668 call _CommonDispatchException
671 .func DispatchOneParamZero
672 _DispatchOneParamZero:
673 /* Call the common dispatcher */
676 call _CommonDispatchException
679 .func DispatchTwoParamZero
680 _DispatchTwoParamZero:
681 /* Call the common dispatcher */
684 call _CommonDispatchException
687 .func DispatchTwoParam
689 /* Call the common dispatcher */
691 call _CommonDispatchException
694 /* HARDWARE TRAP HANDLERS ****************************************************/
699 /* TODO: Routine to fixup a KTRAP_FRAME when faulting from a syscall. */
704 TRAP_FIXUPS kit0_a, kit0_t, DoFixupV86, DoNotFixupAbios
706 /* Push error code */
710 TRAP_PROLOG kit0_a, kit0_t
713 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
716 /* Check if the frame was from kernelmode */
717 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
720 /* Check the old mode */
721 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
725 /* Re-enable interrupts for user-mode and send the exception */
727 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
728 mov ebx, [ebp+KTRAP_FRAME_EIP]
732 /* Check if this is a VDM process */
733 mov ebx, PCR[KPCR_CURRENT_THREAD]
734 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
735 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
738 /* We don't support this yet! */
745 TRAP_FIXUPS kit1_a, kit1_t, DoFixupV86, DoNotFixupAbios
747 /* Push error code */
751 TRAP_PROLOG kit1_a, kit1_t
754 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
757 /* Check if the frame was from kernelmode */
758 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
761 /* Check the old mode */
762 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
766 /* Enable interrupts for user-mode */
770 /* Prepare the exception */
771 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
772 mov ebx, [ebp+KTRAP_FRAME_EIP]
773 mov eax, STATUS_SINGLE_STEP
777 /* Check if this is a VDM process */
778 mov ebx, PCR[KPCR_CURRENT_THREAD]
779 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
780 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
783 /* We don't support VDM! */
791 /* FIXME: This is an NMI, nothing like a normal exception */
793 jmp _KiSystemFatalException
797 TRAP_FIXUPS kit3_a, kit3_t, DoFixupV86, DoNotFixupAbios
799 /* Push error code */
803 TRAP_PROLOG kit3_a, kit3_t
806 * Set the special code to indicate that this is a software breakpoint
807 * and not a debug service call
809 mov eax, BREAKPOINT_BREAK
813 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
816 /* Check if the frame was from kernelmode */
817 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
820 /* Check the old mode */
821 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
825 /* Enable interrupts for user-mode */
830 /* Prepare the exception */
835 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
836 mov ebx, [ebp+KTRAP_FRAME_EIP]
839 mov eax, STATUS_BREAKPOINT
840 call _CommonDispatchException
843 /* Check if this is a VDM process */
844 mov ebx, PCR[KPCR_CURRENT_THREAD]
845 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
846 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
849 /* We don't support VDM! */
854 TRAP_FIXUPS kit4_a, kit4_t, DoFixupV86, DoNotFixupAbios
856 /* Push error code */
860 TRAP_PROLOG kit4_a, kit4_t
863 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
866 /* Check if the frame was from kernelmode */
867 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
870 /* Check the old mode */
871 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
875 /* Re-enable interrupts for user-mode and send the exception */
877 mov eax, STATUS_INTEGER_OVERFLOW
878 mov ebx, [ebp+KTRAP_FRAME_EIP]
883 /* Check if this is a VDM process */
884 mov ebx, PCR[KPCR_CURRENT_THREAD]
885 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
886 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
889 /* We don't support this yet! */
895 TRAP_FIXUPS kit5_a, kit5_t, DoFixupV86, DoNotFixupAbios
897 /* Push error code */
901 TRAP_PROLOG kit5_a, kit5_t
904 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
907 /* Check if the frame was from kernelmode */
908 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
911 /* It did, and this should never happen */
913 jmp _KiSystemFatalException
915 /* Check the old mode */
917 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
920 /* Re-enable interrupts for user-mode and send the exception */
923 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
924 mov ebx, [ebp+KTRAP_FRAME_EIP]
928 /* Check if this is a VDM process */
929 mov ebx, PCR[KPCR_CURRENT_THREAD]
930 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
931 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
934 /* We don't support this yet! */
940 TRAP_FIXUPS kit6_a, kit6_t, DoFixupV86, DoNotFixupAbios
943 /* It this a V86 GPF? */
944 test dword ptr [esp+8], EFLAGS_V86_MASK
948 V86_TRAP_PROLOG kit6_a, kit6_v
951 /* Not yet supported (Invalid OPCODE from V86) */
955 /* Push error code */
959 TRAP_PROLOG kit6_a, kit6_t
962 /* Check if this happened in kernel mode */
963 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
967 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
970 /* Check if the process is vDM */
971 mov ebx, PCR[KPCR_CURRENT_THREAD]
972 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
973 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
977 /* Get EIP and enable interrupts at this point */
978 mov esi, [ebp+KTRAP_FRAME_EIP]
981 /* Set intruction prefix length */
984 /* Setup a SEH frame */
987 push PCR[KPCR_EXCEPTION_LIST]
988 mov PCR[KPCR_EXCEPTION_LIST], esp
991 /* Get the instruction and check if it's LOCK */
1000 /* Undo SEH frame */
1001 pop PCR[KPCR_EXCEPTION_LIST]
1006 /* Re-enable interrupts */
1009 /* Setup illegal instruction exception and dispatch it */
1010 mov ebx, [ebp+KTRAP_FRAME_EIP]
1011 mov eax, STATUS_ILLEGAL_INSTRUCTION
1012 jmp _DispatchNoParam
1016 /* Undo SEH Frame */
1017 pop PCR[KPCR_EXCEPTION_LIST]
1020 /* Setup invalid lock exception and dispatch it */
1021 mov ebx, [ebp+KTRAP_FRAME_EIP]
1022 mov eax, STATUS_INVALID_LOCK_SEQUENCE
1023 jmp _DispatchNoParam
1030 /* Return to caller */
1031 jmp _Kei386EoiHelper@0
1037 pop PCR[KPCR_EXCEPTION_LIST]
1041 /* Check if this was user mode */
1042 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1051 push KMODE_EXCEPTION_NOT_HANDLED
1052 call _KeBugCheckWithTf@24
1056 TRAP_FIXUPS kit7_a, kit7_t, DoFixupV86, DoNotFixupAbios
1058 /* Push error code */
1062 TRAP_PROLOG kit7_a, kit7_t
1064 /* Get the current thread and stack */
1066 mov eax, PCR[KPCR_CURRENT_THREAD]
1067 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1068 sub ecx, NPX_FRAME_LENGTH
1070 /* Check if emulation is enabled */
1071 test byte ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1072 jnz EmulationEnabled
1075 /* Check if the NPX state is loaded */
1076 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1081 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1084 /* Check the NPX thread */
1085 mov edx, PCR[KPCR_NPX_THREAD]
1089 /* Get the NPX Stack */
1090 mov esi, [edx+KTHREAD_INITIAL_STACK]
1091 sub esi, NPX_FRAME_LENGTH
1093 /* Check if we have FXSR and check which operand to use */
1094 test byte ptr _KeI386FxsrPresent, 1
1103 /* Set the thread's state to dirty */
1104 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1107 /* Check if we have FXSR and choose which operand to use */
1108 test byte ptr _KeI386FxsrPresent, 1
1117 /* Set state loaded */
1118 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1119 mov PCR[KPCR_NPX_THREAD], eax
1121 /* Enable interrupts to happen now */
1125 /* Check if CR0 needs to be reloaded due to a context switch */
1126 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1127 jz _Kei386EoiHelper@0
1129 /* We have to reload CR0... disable interrupts */
1132 /* Get CR0 and update it */
1134 or ebx, [ecx+FN_CR0_NPX_STATE]
1137 /* Restore interrupts and check if TS is back on */
1140 jz _Kei386EoiHelper@0
1142 /* Clear TS, and loop handling again */
1149 /* Set delayed error */
1150 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
1152 /* Check if this happened during restore */
1153 cmp dword ptr [ebp+KTRAP_FRAME_EIP], offset FrRestore
1156 /* Skip instruction and dispatch the exception */
1157 add dword ptr [ebp+KTRAP_FRAME_EIP], 3
1158 jmp _Kei386EoiHelper@0
1161 /* Check if TS is set */
1163 jnz TsSetOnLoadedState
1166 /* Check if the trap came from V86 mode */
1167 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1170 /* Check if it came from kernel mode */
1171 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1174 /* Check if it came from a VDM */
1175 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1179 /* Get the current thread */
1180 mov eax, PCR[KPCR_CURRENT_THREAD]
1182 /* Check NPX state */
1183 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1185 /* Get the NPX save area */
1186 mov ecx, [eax+KTHREAD_INITIAL_STACK]
1187 lea ecx, [ecx-NPX_FRAME_LENGTH]
1194 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
1197 /* Check if we have FX support */
1198 test byte ptr _KeI386FxsrPresent, 1
1201 /* Save the state */
1209 /* Make CR0 state not loaded */
1210 or ebx, NPX_STATE_NOT_LOADED
1211 or ebx, [ecx+FN_CR0_NPX_STATE]
1214 /* Update NPX state */
1215 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1216 mov dword ptr PCR[KPCR_NPX_THREAD], 0
1219 /* Clear the TS bit and re-enable interrupts */
1220 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
1223 /* Check if we have FX support */
1224 test byte ptr _KeI386FxsrPresent, 1
1227 /* Get error offset, control and status words */
1228 mov ebx, [ecx+FX_ERROR_OFFSET]
1229 movzx eax, word ptr [ecx+FX_CONTROL_WORD]
1230 movzx edx, word ptr [ecx+FX_STATUS_WORD]
1232 /* Get the faulting opcode */
1233 mov esi, [ecx+FX_DATA_OFFSET]
1237 /* Get error offset, control and status words */
1238 mov ebx, [ecx+FP_ERROR_OFFSET]
1239 movzx eax, word ptr [ecx+FP_CONTROL_WORD]
1240 movzx edx, word ptr [ecx+FP_STATUS_WORD]
1242 /* Get the faulting opcode */
1243 mov esi, [ecx+FP_DATA_OFFSET]
1246 /* Mask exceptions */
1251 /* Check if what's left is invalid */
1255 /* Check if it was a stack fault */
1259 /* Raise exception */
1260 mov eax, STATUS_FLOAT_INVALID_OPERATION
1261 jmp _DispatchOneParamZero
1265 /* Raise exception */
1266 mov eax, STATUS_FLOAT_STACK_CHECK
1267 jmp _DispatchTwoParamZero
1271 /* Check for divide by 0 */
1275 /* Raise exception */
1276 mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
1277 jmp _DispatchOneParamZero
1280 /* Check for denormal */
1284 /* Raise exception */
1285 mov eax, STATUS_FLOAT_INVALID_OPERATION
1286 jmp _DispatchOneParamZero
1289 /* Check for overflow */
1293 /* Raise exception */
1294 mov eax, STATUS_FLOAT_OVERFLOW
1295 jmp _DispatchOneParamZero
1298 /* Check for underflow */
1302 /* Raise exception */
1303 mov eax, STATUS_FLOAT_UNDERFLOW
1304 jmp _DispatchOneParamZero
1307 /* Check for precision fault */
1311 /* Raise exception */
1312 mov eax, STATUS_FLOAT_INEXACT_RESULT
1313 jmp _DispatchOneParamZero
1317 /* Strange result, bugcheck the OS */
1324 push TRAP_CAUSE_UNKNOWN
1325 call _KeBugCheckWithTf@24
1328 /* Check if this is a VDM */
1329 mov eax, PCR[KPCR_CURRENT_THREAD]
1330 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
1331 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1334 /* V86 NPX not handled */
1338 /* Did this come from kernel-mode? */
1339 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1342 /* It came from user-mode, so this would only be valid inside a VDM */
1343 /* Since we don't actually have VDMs in ROS, bugcheck. */
1347 /* TS shouldn't be set, unless this we don't have a Math Processor */
1351 /* Strange that we got a trap at all, but ignore and continue */
1353 jmp _Kei386EoiHelper@0
1356 /* Cause a bugcheck */
1361 push TRAP_CAUSE_UNKNOWN
1362 call _KeBugCheckEx@20
1368 /* Can't really do too much */
1370 jmp _KiSystemFatalException
1374 TRAP_FIXUPS kit9_a, kit9_t, DoFixupV86, DoNotFixupAbios
1376 /* Push error code */
1380 TRAP_PROLOG kit9_a, kit9_t
1382 /* Enable interrupts and bugcheck */
1385 jmp _KiSystemFatalException
1389 TRAP_FIXUPS kita_a, kita_t, DoFixupV86, DoNotFixupAbios
1392 TRAP_PROLOG kita_a, kita_t
1395 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1398 /* Check if the frame was from kernelmode */
1399 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1403 /* Check if OF was set during iretd */
1404 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1408 /* It was, just mask it out */
1409 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1410 jmp _Kei386EoiHelper@0
1413 /* TSS failure for some other reason: crash */
1415 jmp _KiSystemFatalException
1419 TRAP_FIXUPS kitb_a, kitb_t, DoFixupV86, DoNotFixupAbios
1422 TRAP_PROLOG kitb_a, kitb_t
1424 /* FIXME: ROS Doesn't handle segment faults yet */
1426 jmp _KiSystemFatalException
1430 TRAP_FIXUPS kitc_a, kitc_t, DoFixupV86, DoNotFixupAbios
1433 TRAP_PROLOG kitc_a, kitc_t
1435 /* FIXME: ROS Doesn't handle stack faults yet */
1437 jmp _KiSystemFatalException
1440 .func KiTrapExceptHandler
1441 _KiTrapExceptHandler:
1443 /* Setup SEH handler frame */
1445 pop PCR[KPCR_EXCEPTION_LIST]
1449 /* Check if the fault came from user mode */
1450 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1453 /* Kernel fault, bugcheck */
1459 push KMODE_EXCEPTION_NOT_HANDLED
1460 call _KeBugCheckWithTf@24
1464 TRAP_FIXUPS kitd_a, kitd_t, DoFixupV86, DoNotFixupAbios
1467 /* It this a V86 GPF? */
1468 test dword ptr [esp+12], EFLAGS_V86_MASK
1471 /* Enter V86 Trap */
1472 V86_TRAP_PROLOG kitd_a, kitd_v
1474 /* Make sure that this is a V86 process */
1475 mov ecx, PCR[KPCR_CURRENT_THREAD]
1476 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1477 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1480 /* Otherwise, something is very wrong, raise an exception */
1486 /* Go to APC level */
1490 /* Save old IRQL and enable interrupts */
1494 /* Handle the opcode */
1495 call _Ki386HandleOpcodeV86@0
1497 /* Check if this was VDM */
1506 /* Lower IRQL and disable interrupts */
1511 /* Check if this was a V86 trap */
1512 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1515 /* Exit the V86 Trap */
1520 /* Either this wasn't V86, or it was, but an APC interrupted us */
1521 jmp _Kei386EoiHelper@0
1525 TRAP_PROLOG kitd_a, kitd_t
1527 /* Check if this was from kernel-mode */
1528 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1531 /* Check if we have a VDM alert */
1532 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1535 /* Check for GPF during GPF */
1536 mov eax, [ebp+KTRAP_FRAME_EIP]
1537 cmp eax, offset CheckPrivilegedInstruction
1539 cmp eax, offset CheckPrivilegedInstruction2
1545 /* Get the opcode and trap frame */
1547 mov eax, [ebp+KTRAP_FRAME_EIP]
1549 mov edx, [ebp+KTRAP_FRAME_EBP]
1551 /* We want to check if this was POP [DS/ES/FS/GS] */
1552 add edx, KTRAP_FRAME_DS
1555 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1558 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1561 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1565 /* It isn't, was it IRETD? */
1569 /* Get error code */
1570 lea edx, [ebp+KTRAP_FRAME_ESP]
1571 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1575 mov cx, word ptr [edx+4]
1580 /* This should be a Ki386CallBios return */
1581 mov eax, offset _Ki386BiosCallReturnAddress
1585 cmp ax, KGDT_R0_CODE + RPL_MASK
1588 /* Jump to return address */
1589 jmp _Ki386BiosCallReturnAddress
1592 /* Check if the thread was in kernel mode */
1593 mov ebx, PCR[KPCR_CURRENT_THREAD]
1594 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1597 /* Set RPL_MASK for check below */
1598 or word ptr [edx+4], RPL_MASK
1601 /* Check if the IRET goes to user-mode */
1602 test dword ptr [edx+4], RPL_MASK
1605 /* Setup trap frame to copy */
1606 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1607 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1611 /* Copy each field */
1617 /* Enable interrupts and adjust stack */
1622 /* Setup exception record */
1623 mov ebx, [ebp+KTRAP_FRAME_EIP]
1624 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1626 mov eax, STATUS_ACCESS_VIOLATION
1627 jmp _DispatchTwoParamZero
1631 /* FIXME: Handle RDMSR/WRMSR */
1636 /* Check if this was an MSR opcode */
1640 /* Check if DS is Ring 3 */
1641 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1644 /* Otherwise, fix it up */
1645 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1650 /* Check if ES is Ring 3 */
1651 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1654 /* Otherwise, fix it up */
1655 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1661 lea eax, [ebp+KTRAP_FRAME_ESP]
1666 /* Handle segment POP fault by setting it to 0 */
1669 mov dword ptr [edx], eax
1673 /* Do a trap exit */
1674 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1678 /* If the previous mode was kernel, raise a fatal exception */
1680 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1681 jz _KiSystemFatalException
1683 /* Get the process and check which CS this came from */
1684 mov ebx, PCR[KPCR_CURRENT_THREAD]
1685 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
1686 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1689 /* Check if this is a VDM */
1690 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1693 /* Enable interrupts and check if we have an error code */
1695 cmp word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
1697 jmp CheckPrivilegedInstruction
1700 /* Update EIP (will be updated below again) */
1701 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1704 /* Clear the segment, update EIP and ESP */
1705 mov dword ptr [edx], 0
1706 add dword ptr [ebp+KTRAP_FRAME_EIP], 1
1707 add dword ptr [ebp+KTRAP_FRAME_ESP], 4
1708 jmp _Kei386EoiHelper@0
1711 /* Check if this is a VDM */
1712 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
1713 jz CheckPrivilegedInstruction
1715 /* Bring interrupts back */
1718 /* Check what kind of instruction this is */
1719 mov eax, [ebp+KTRAP_FRAME_EIP]
1722 /* FIXME: Check for BOP4 */
1724 /* Check if this is POP ES */
1726 add edx, KTRAP_FRAME_ES
1730 /* Check if this is POP FS */
1731 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1735 /* Check if this is POP GS */
1736 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1740 CheckPrivilegedInstruction:
1741 /* Bring interrupts back */
1744 /* Setup a SEH handler */
1746 push offset _KiTrapExceptHandler
1747 push PCR[KPCR_EXCEPTION_LIST]
1748 mov PCR[KPCR_EXCEPTION_LIST], esp
1751 mov esi, [ebp+KTRAP_FRAME_EIP]
1753 /* Setup loop count */
1757 /* Save loop count */
1760 /* Get the instruction */
1763 /* Now lookup in the prefix table */
1765 mov edi, offset _KiTrapPrefixTable
1768 /* Restore loop count */
1771 /* If it's not a prefix byte, check other instructions */
1777 /* Fixup the stack */
1778 pop PCR[KPCR_EXCEPTION_LIST]
1781 /* Illegal instruction */
1785 /* Check if it's a HLT */
1787 je IsPrivInstruction
1789 /* Check if the instruction has two bytes */
1793 /* Check if this is a LLDT or LTR */
1798 /* Check if this is an LLDT */
1802 je IsPrivInstruction
1804 /* Check if this is an LTR */
1806 je IsPrivInstruction
1808 /* Otherwise, access violation */
1812 /* Check if this is LGDT or LIDT or LMSW */
1816 /* Check if this is an LGDT */
1820 je IsPrivInstruction
1822 /* Check if this is an LIDT */
1824 je IsPrivInstruction
1826 /* Check if this is an LMSW */
1828 je IsPrivInstruction
1830 /* Otherwise, access violation */
1834 /* Check if it's INVD or WBINVD */
1836 je IsPrivInstruction
1838 je IsPrivInstruction
1840 /* Check if it's sysexit */
1842 je IsPrivInstruction
1844 /* Check if it's a DR move */
1846 je IsPrivInstruction
1848 /* Check if it's a CLTS */
1850 je IsPrivInstruction
1852 /* Check if it's a CR move */
1856 /* Check if it's a DR move */
1858 jbe IsPrivInstruction
1860 /* Everything else is an access violation */
1864 /* Get EFLAGS and IOPL */
1865 mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
1866 and ebx, EFLAGS_IOPL
1869 /* Check the CS's RPL mask */
1870 mov ecx, [ebp+KTRAP_FRAME_CS]
1875 CheckPrivilegedInstruction2:
1876 /* Check if this is a CLI or STI */
1878 je IsPrivInstruction
1880 je IsPrivInstruction
1882 /* Setup I/O table lookup */
1884 mov edi, offset _KiTrapIoTable
1886 /* Loopup in the table */
1890 /* FIXME: Check IOPM!!! */
1893 /* Cleanup the SEH frame */
1894 pop PCR[KPCR_EXCEPTION_LIST]
1897 /* Setup the exception */
1898 mov ebx, [ebp+KTRAP_FRAME_EIP]
1899 mov eax, STATUS_PRIVILEGED_INSTRUCTION
1900 jmp _DispatchNoParam
1903 /* Cleanup the SEH frame */
1904 pop PCR[KPCR_EXCEPTION_LIST]
1908 /* Setup the exception */
1909 mov ebx, [ebp+KTRAP_FRAME_EIP]
1911 mov eax, STATUS_ACCESS_VIOLATION
1912 jmp _DispatchTwoParamZero
1920 TRAP_FIXUPS kite_a, kite_t, DoFixupV86, DoNotFixupAbios
1924 TRAP_PROLOG kite_a, kite_t
1926 /* Check if we have a VDM alert */
1927 cmp dword ptr PCR[KPCR_VDM_ALERT], 0
1930 /* Get the current thread */
1931 mov edi, PCR[KPCR_CURRENT_THREAD]
1933 /* Get the stack address of the frame */
1934 lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
1935 sub eax, [edi+KTHREAD_INITIAL_STACK]
1938 /* This isn't the base frame, check if it's the second */
1939 cmp eax, -KTRAP_FRAME_EFLAGS
1942 /* Check if we have a TEB */
1943 mov eax, PCR[KPCR_TEB]
1947 /* Fixup the frame */
1954 /* Check if this processor has the cmpxchg8b lock errata */
1955 cmp byte ptr _KiI386PentiumLockErrataPresent, 0
1956 jnz HandleLockErrata
1959 /* HACK: Handle page faults with interrupts disabled */
1960 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1963 /* Enable interrupts and check if we got here with interrupts disabled */
1965 #ifdef HACK_ABOVE_FIXED
1966 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
1971 /* Send trap frame and check if this is kernel-mode or usermode */
1973 mov eax, [ebp+KTRAP_FRAME_CS]
1977 /* Send faulting address and check if this is read or write */
1979 mov eax, [ebp+KTRAP_FRAME_ERROR_CODE]
1983 /* Call the access fault handler */
1984 call _MmAccessFault@16
1988 /* Access fault handled, return to caller */
1989 jmp _Kei386EoiHelper@0
1992 /* First check if this is a fault in the S-LIST functions */
1993 mov ecx, offset _ExpInterlockedPopEntrySListFault@0
1994 cmp [ebp+KTRAP_FRAME_EIP], ecx
1997 /* Check if this is a fault in the syscall handler */
1998 mov ecx, offset CopyParams
1999 cmp [ebp+KTRAP_FRAME_EIP], ecx
2001 mov ecx, offset ReadBatch
2002 cmp [ebp+KTRAP_FRAME_EIP], ecx
2007 jmp _Kei386EoiHelper@0
2012 jmp _Kei386EoiHelper@0
2014 /* Check if the fault occured in a V86 mode */
2016 mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
2019 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
2022 /* Check if the fault occured in a VDM */
2023 mov esi, PCR[KPCR_CURRENT_THREAD]
2024 mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
2025 cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
2028 /* Check if we this was in kernel-mode */
2029 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
2031 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
2038 /* Save EIP and check what kind of status failure we got */
2040 mov esi, [ebp+KTRAP_FRAME_EIP]
2041 cmp eax, STATUS_ACCESS_VIOLATION
2043 cmp eax, STATUS_GUARD_PAGE_VIOLATION
2045 cmp eax, STATUS_STACK_OVERFLOW
2048 /* Setup an in-page exception to dispatch */
2054 mov eax, STATUS_IN_PAGE_ERROR
2055 call _CommonDispatchException
2058 /* Use more proper status code */
2059 mov eax, KI_EXCEPTION_ACCESS_VIOLATION
2062 /* Setup a normal page fault exception */
2066 jmp _DispatchTwoParam
2074 /* This is completely illegal, bugcheck the system */
2080 push IRQL_NOT_LESS_OR_EQUAL
2081 call _KeBugCheckWithTf@24
2085 /* FIXME: NOT SUPPORTED */
2090 /* Fail if this isn't a write fault */
2091 test word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0x4
2094 /* Also make sure the page fault is for IDT entry 6 */
2095 mov eax, PCR[KPCR_IDT]
2101 * This is a write fault to the Invalid Opcode handler entry.
2102 * We assume this is the lock errata and not a real write fault.
2105 /* Clear the error code */
2106 and dword ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
2108 /* Check if this happened in V86 mode */
2109 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
2112 /* Dispatch this to the invalid opcode handler */
2113 jmp DispatchLockErrata
2117 TRAP_FIXUPS kitf_a, kitf_t, DoFixupV86, DoNotFixupAbios
2119 /* Push error code */
2123 TRAP_PROLOG kitf_a, kitf_t
2126 /* Raise a fatal exception */
2128 jmp _KiSystemFatalException
2132 TRAP_FIXUPS kit10_a, kit10_t, DoFixupV86, DoNotFixupAbios
2134 /* Push error code */
2138 TRAP_PROLOG kit10_a, kit10_t
2140 /* Check if this is the NPX Thread */
2141 mov eax, PCR[KPCR_CURRENT_THREAD]
2142 cmp eax, PCR[KPCR_NPX_THREAD]
2144 /* Get the initial stack and NPX frame */
2145 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2146 lea ecx, [ecx-NPX_FRAME_LENGTH]
2148 /* If this is a valid fault, handle it */
2151 /* Otherwise, re-enable interrupts and set delayed error */
2153 or dword ptr [ecx+FN_CR0_NPX_STATE], CR0_TS
2154 jmp _Kei386EoiHelper@0
2158 TRAP_FIXUPS kit11_a, kit11_t, DoFixupV86, DoNotFixupAbios
2160 /* Push error code */
2164 TRAP_PROLOG kit11_a, kit11_t
2166 /* FIXME: ROS Doesn't handle alignment faults yet */
2168 jmp _KiSystemFatalException
2173 TRAP_FIXUPS kit19_a, kit19_t, DoFixupV86, DoNotFixupAbios
2175 /* Push error code */
2179 TRAP_PROLOG kit19_a, kit19_t
2181 /* Check if this is the NPX Thread */
2182 mov eax, PCR[KPCR_CURRENT_THREAD]
2183 cmp eax, PCR[KPCR_NPX_THREAD]
2185 /* If this is a valid fault, handle it */
2188 /* Otherwise, bugcheck */
2190 jmp _KiSystemFatalException
2193 /* Get the initial stack and NPX frame */
2194 mov ecx, [eax+KTHREAD_INITIAL_STACK]
2195 lea ecx, [ecx-NPX_FRAME_LENGTH]
2197 /* Check if the trap came from V86 mode */
2198 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
2201 /* Check if it came from kernel mode */
2202 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
2205 /* Check if it came from a VDM */
2206 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
2212 and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
2215 /* Check if we have FX support */
2216 test byte ptr _KeI386FxsrPresent, 1
2219 /* Save the state */
2221 jmp XmmiMakeCr0Dirty
2227 /* Make CR0 state not loaded */
2228 or ebx, NPX_STATE_NOT_LOADED
2229 or ebx, [ecx+FN_CR0_NPX_STATE]
2232 /* Update NPX state */
2233 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
2234 mov dword ptr PCR[KPCR_NPX_THREAD], 0
2236 /* Clear the TS bit and re-enable interrupts */
2237 and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
2239 /* Re-enable interrupts for user-mode and send the exception */
2241 mov ebx, [ebp+KTRAP_FRAME_EIP]
2243 /* Get MxCSR and get current mask (bits 7-12) */
2244 movzx eax, word ptr [ecx+FX_MXCSR]
2249 /* Set faulting opcode address to 0 */
2252 /* Apply legal exceptions mask */
2255 /* Apply the mask we got in MXCSR itself */
2258 /* Check for invalid operation */
2262 /* Raise exception */
2263 mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
2264 jmp _DispatchOneParamZero
2267 /* Check for zero divide */
2271 /* Raise exception */
2272 mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
2273 jmp _DispatchOneParamZero
2276 /* Check for denormal */
2280 /* Raise exception */
2281 mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
2282 jmp _DispatchOneParamZero
2285 /* Check for overflow*/
2289 /* Raise exception */
2290 mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
2291 jmp _DispatchOneParamZero
2294 /* Check for denormal */
2298 /* Raise exception */
2299 mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
2300 jmp _DispatchOneParamZero
2303 /* Check for Precision */
2307 /* Raise exception */
2308 mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
2309 jmp _DispatchOneParamZero
2312 /* Strange result, bugcheck the OS */
2319 push TRAP_CAUSE_UNKNOWN
2320 call _KeBugCheckWithTf@24
2323 /* Check if this is a VDM */
2324 mov eax, PCR[KPCR_CURRENT_THREAD]
2325 mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
2326 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
2330 /* V86 XMMI not handled */
2334 /* Another weird situation */
2340 push TRAP_CAUSE_UNKNOWN
2341 call _KeBugCheckWithTf@24
2345 .func KiSystemFatalException
2346 _KiSystemFatalException:
2348 /* Push the trap frame */
2351 /* Push empty parameters */
2356 /* Push trap number and bugcheck code */
2358 push UNEXPECTED_KERNEL_MODE_TRAP
2359 call _KeBugCheckWithTf@24
2363 .func KiCoprocessorError@0
2364 _KiCoprocessorError@0:
2366 /* Get the NPX Thread's Initial stack */
2367 mov eax, PCR[KPCR_NPX_THREAD]
2368 mov eax, [eax+KTHREAD_INITIAL_STACK]
2370 /* Make space for the FPU Save area */
2371 sub eax, SIZEOF_FX_SAVE_AREA
2373 /* Set the CR0 State */
2374 mov dword ptr [eax+FN_CR0_NPX_STATE], 8
2381 /* Return to caller */
2385 .func Ki16BitStackException
2386 _Ki16BitStackException:
2392 /* Go to kernel mode thread stack */
2393 mov eax, PCR[KPCR_CURRENT_THREAD]
2394 add esp, [eax+KTHREAD_INITIAL_STACK]
2396 /* Switch to good stack segment */
2400 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
2402 .globl _KiStartUnexpectedRange@0
2403 _KiStartUnexpectedRange@0:
2405 GENERATE_INT_HANDLERS
2407 .globl _KiEndUnexpectedRange@0
2408 _KiEndUnexpectedRange@0:
2409 jmp _KiUnexpectedInterruptTail
2411 .func KiUnexpectedInterruptTail
2412 TRAP_FIXUPS kui_a, kui_t, DoFixupV86, DoFixupAbios
2413 _KiUnexpectedInterruptTail:
2415 /* Enter interrupt trap */
2416 INT_PROLOG kui_a, kui_t, DoNotPushFakeErrorCode
2418 /* Increase interrupt count */
2419 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2421 /* Put vector in EBX and make space for KIRQL */
2425 /* Begin interrupt */
2429 call _HalBeginSystemInterrupt@12
2431 /* Check if it was spurious or not */
2435 /* Spurious, ignore it */
2437 jmp _Kei386EoiHelper2ndEntry
2440 /* Unexpected interrupt, print a message on debug builds */
2443 push offset _UnexpectedMsg
2448 /* Exit the interrupt */
2451 call _HalEndSystemInterrupt@8
2452 jmp _Kei386EoiHelper@0
2455 .globl _KiUnexpectedInterrupt
2456 _KiUnexpectedInterrupt:
2458 /* Bugcheck with invalid interrupt code */
2459 push TRAP_CAUSE_UNKNOWN
2462 /* INTERRUPT HANDLERS ********************************************************/
2464 .func KiDispatchInterrupt@0
2465 _KiDispatchInterrupt@0:
2467 /* Get the PCR and disable interrupts */
2468 mov ebx, PCR[KPCR_SELF]
2471 /* Check if we have to deliver DPCs, timers, or deferred threads */
2472 mov eax, [ebx+KPCR_PRCB_DPC_QUEUE_DEPTH]
2473 or eax, [ebx+KPCR_PRCB_TIMER_REQUEST]
2474 or eax, [ebx+KPCR_PRCB_DEFERRED_READY_LIST_HEAD]
2477 /* Save stack pointer and exception list, then clear it */
2479 push dword ptr [ebx+KPCR_EXCEPTION_LIST]
2480 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
2482 /* Save the stack and switch to the DPC Stack */
2484 mov esp, [ebx+KPCR_PRCB_DPC_STACK]
2488 mov ecx, [ebx+KPCR_PRCB]
2489 call @KiRetireDpcList@4
2491 /* Restore stack and exception list */
2493 pop dword ptr [ebx+KPCR_EXCEPTION_LIST]
2498 /* Re-enable interrupts */
2501 /* Check if we have quantum end */
2502 cmp byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2505 /* Check if we have a thread to swap to */
2506 cmp byte ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2509 /* Make space on the stack to save registers */
2515 /* Get the current thread */
2516 mov edi, [ebx+KPCR_CURRENT_THREAD]
2519 /* Raise to synch level */
2520 call _KeRaiseIrqlToSynchLevel@0
2522 /* Set context swap busy */
2523 mov byte ptr [edi+KTHREAD_SWAP_BUSY], 1
2525 /* Acquire the PRCB Lock */
2526 lock bts dword ptr [ebx+KPCR_PRCB_PRCB_LOCK], 0
2528 lea ecx, [ebx+KPCR_PRCB_PRCB_LOCK]
2529 call @KefAcquireSpinLockAtDpcLevel@4
2533 /* Get the next thread and clear it */
2534 mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
2535 and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
2537 /* Set us as the current running thread */
2538 mov [ebx+KPCR_CURRENT_THREAD], esi
2539 mov byte ptr [esi+KTHREAD_STATE_], Running
2540 mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
2542 /* Put thread in ECX and get the PRCB in EDX */
2544 lea edx, [ebx+KPCR_PRCB_DATA]
2545 call @KiQueueReadyThread@8
2547 /* Set APC_LEVEL and do the swap */
2549 call @KiSwapContextInternal@0
2552 /* Lower IRQL back to dispatch */
2553 mov cl, DISPATCH_LEVEL
2557 /* Restore registers */
2568 /* Disable quantum end and process it */
2569 mov byte ptr [ebx+KPCR_PRCB_QUANTUM_END], 0
2570 call _KiQuantumEnd@0
2574 .func KiInterruptTemplate
2575 _KiInterruptTemplate:
2577 /* Enter interrupt trap */
2578 INT_PROLOG kit_a, kit_t, DoPushFakeErrorCode
2580 _KiInterruptTemplate2ndDispatch:
2581 /* Dummy code, will be replaced by the address of the KINTERRUPT */
2584 _KiInterruptTemplateObject:
2585 /* Dummy jump, will be replaced by the actual jump */
2586 jmp _KeSynchronizeExecution@12
2588 _KiInterruptTemplateDispatch:
2589 /* Marks the end of the template so that the jump above can be edited */
2591 TRAP_FIXUPS kit_a, kit_t, DoFixupV86, DoFixupAbios
2594 .func KiChainedDispatch2ndLvl@0
2595 _KiChainedDispatch2ndLvl@0:
2598 /* Raise IRQL if necessary */
2599 mov cl, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2600 cmp cl, [edi+KINTERRUPT_IRQL]
2605 /* Acquire the lock */
2606 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2608 ACQUIRE_SPINLOCK(esi, IntSpin2)
2610 /* Make sure that this interrupt isn't storming */
2613 /* Save the tick count */
2614 mov esi, _KeTickCount
2617 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2620 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2622 /* Save the ISR result */
2625 /* Check if the ISR timed out */
2626 add esi, _KiISRTimeout
2627 cmp _KeTickCount, esi
2628 jnc ChainedIsrTimeout
2631 /* Release the lock */
2632 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2633 RELEASE_SPINLOCK(esi)
2635 /* Lower IRQL if necessary */
2636 mov cl, [edi+KINTERRUPT_IRQL]
2637 cmp cl, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2642 /* Check if the interrupt is handled */
2646 /* Try the next shared interrupt handler */
2647 mov eax, [edi+KINTERRUPT_INTERRUPT_LIST_HEAD]
2648 lea edi, [eax-KINTERRUPT_INTERRUPT_LIST_HEAD]
2656 SPIN_ON_LOCK(esi, GetIntLock2)
2660 /* Print warning message */
2661 push [edi+KINTERRUPT_SERVICE_ROUTINE]
2662 push offset _IsrTimeoutMsg
2666 /* Break into debugger, then continue */
2670 /* Cleanup verification */
2671 VERIFY_INT_END kid2, 0
2674 .func KiChainedDispatch@0
2675 _KiChainedDispatch@0:
2677 /* Increase interrupt count */
2678 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2680 /* Save trap frame */
2683 /* Save vector and IRQL */
2684 mov eax, [edi+KINTERRUPT_VECTOR]
2685 mov ecx, [edi+KINTERRUPT_IRQL]
2691 /* Begin interrupt */
2695 call _HalBeginSystemInterrupt@12
2697 /* Check if it was handled */
2701 /* Call the 2nd-level handler */
2702 call _KiChainedDispatch2ndLvl@0
2704 /* Exit the interrupt */
2708 .func KiInterruptDispatch@0
2709 _KiInterruptDispatch@0:
2711 /* Increase interrupt count */
2712 inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
2714 /* Save trap frame */
2717 /* Save vector and IRQL */
2718 mov eax, [edi+KINTERRUPT_VECTOR]
2719 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
2725 /* Begin interrupt */
2729 call _HalBeginSystemInterrupt@12
2731 /* Check if it was handled */
2735 /* Acquire the lock */
2736 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
2738 ACQUIRE_SPINLOCK(esi, IntSpin)
2740 /* Make sure that this interrupt isn't storming */
2743 /* Save the tick count */
2744 mov ebx, _KeTickCount
2747 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
2750 call [edi+KINTERRUPT_SERVICE_ROUTINE]
2752 /* Check if the ISR timed out */
2753 add ebx, _KiISRTimeout
2754 cmp _KeTickCount, ebx
2758 /* Release the lock */
2759 RELEASE_SPINLOCK(esi)
2761 /* Exit the interrupt */
2765 /* Exit the interrupt */
2771 SPIN_ON_LOCK(esi, GetIntLock)
2775 /* Print warning message */
2776 push [edi+KINTERRUPT_SERVICE_ROUTINE]
2777 push offset _IsrTimeoutMsg
2781 /* Break into debugger, then continue */
2785 /* Cleanup verification */
2786 VERIFY_INT_END kid, 0
2789 .globl _KeSynchronizeExecution@12
2790 .func KeSynchronizeExecution@12
2791 _KeSynchronizeExecution@12:
2793 /* Save EBX and put the interrupt object in it */
2798 mov cl, [ebx+KINTERRUPT_SYNCHRONIZE_IRQL]
2803 /* Acquire the interrupt spinlock FIXME: Write this in assembly */
2804 mov ecx, [ebx+KINTERRUPT_ACTUAL_LOCK]
2805 call @KefAcquireSpinLockAtDpcLevel@4
2808 /* Call the routine */
2813 /* Release the interrupt spinlock FIXME: Write this in assembly */
2815 mov ecx, [ebx+KINTERRUPT_ACTUAL_LOCK]
2816 call @KefReleaseSpinLockFromDpcLevel@4
2832 * Kii386SpinOnSpinLock
2837 * SpinLock - FILLMEIN
2848 .globl _Kii386SpinOnSpinLock@8
2849 .func Kii386SpinOnSpinLock@8
2850 _Kii386SpinOnSpinLock@8: