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
15 /* GLOBALS *******************************************************************/
19 /* This is the Software Interrupt Table that we handle in this file: */
20 idt _KiTrap0, INT_32_DPL0 /* INT 00: Divide Error (#DE) */
21 idt _KiTrap1, INT_32_DPL0 /* INT 01: Debug Exception (#DB) */
22 idt _KiTrap2, INT_32_DPL0 /* INT 02: NMI Interrupt */
23 idt _KiTrap3, INT_32_DPL3 /* INT 03: Breakpoint Exception (#BP) */
24 idt _KiTrap4, INT_32_DPL3 /* INT 04: Overflow Exception (#OF) */
25 idt _KiTrap5, INT_32_DPL0 /* INT 05: BOUND Range Exceeded (#BR) */
26 idt _KiTrap6, INT_32_DPL0 /* INT 06: Invalid Opcode Code (#UD) */
27 idt _KiTrap7, INT_32_DPL0 /* INT 07: Device Not Available (#NM) */
28 idt _KiTrap8, INT_32_DPL0 /* INT 08: Double Fault Exception (#DF) */
29 idt _KiTrap9, INT_32_DPL0 /* INT 09: RESERVED */
30 idt _KiTrap10, INT_32_DPL0 /* INT 0A: Invalid TSS Exception (#TS) */
31 idt _KiTrap11, INT_32_DPL0 /* INT 0B: Segment Not Present (#NP) */
32 idt _KiTrap12, INT_32_DPL0 /* INT 0C: Stack Fault Exception (#SS) */
33 idt _KiTrap13, INT_32_DPL0 /* INT 0D: General Protection (#GP) */
34 idt _KiTrap14, INT_32_DPL0 /* INT 0E: Page-Fault Exception (#PF) */
35 idt _KiTrap0F, INT_32_DPL0 /* INT 0F: RESERVED */
36 idt _KiTrap16, INT_32_DPL0 /* INT 10: x87 FPU Error (#MF) */
37 idt _KiTrap17, INT_32_DPL0 /* INT 11: Align Check Exception (#AC) */
38 idt _KiTrap0F, INT_32_DPL0 /* INT 12: Machine Check Exception (#MC)*/
39 idt _KiTrap0F, INT_32_DPL0 /* INT 13: SIMD FPU Exception (#XF) */
41 idt _KiTrap0F, INT_32_DPL0 /* INT 14-29: UNDEFINED INTERRUPTS */
43 idt _KiGetTickCount, INT_32_DPL3 /* INT 2A: Get Tick Count Handler */
44 idt _KiCallbackReturn, INT_32_DPL3 /* INT 2B: User-Mode Callback Return */
45 idt _KiRaiseAssertion, INT_32_DPL3 /* INT 2C: Debug Assertion Handler */
46 idt _KiDebugService, INT_32_DPL3 /* INT 2D: Debug Service Handler */
47 idt _KiSystemService, INT_32_DPL3 /* INT 2E: System Call Service Handler */
48 idt _KiTrap0F, INT_32_DPL0 /* INT 2F: RESERVED */
49 GENERATE_IDT_STUBS /* INT 30-FF: UNEXPECTED INTERRUPTS */
51 /* System call entrypoints: */
52 .globl _KiFastCallEntry
53 .globl _KiSystemService
55 /* And special system-defined software traps: */
56 .globl _NtRaiseException@12
59 /* Interrupt template entrypoints */
60 .globl _KiInterruptTemplate
61 .globl _KiInterruptTemplateObject
62 .globl _KiInterruptTemplateDispatch
64 /* Chained and Normal generic interrupt handlers for 1st and 2nd level entry*/
65 .globl _KiChainedDispatch2ndLvl@0
66 .globl _KiInterruptDispatch@0
67 .globl _KiChainedDispatch@0
69 /* We implement the following trap exit points: */
70 .globl _KiServiceExit /* Exit from syscall */
71 .globl _KiServiceExit2 /* Exit from syscall with complete frame*/
72 .globl _Kei386EoiHelper@0 /* Exit from interrupt or H/W trap */
73 .globl _Kei386EoiHelper2ndEntry /* Exit from unexpected interrupt */
75 .globl _KiIdtDescriptor
80 .globl _KiUnexpectedEntrySize
81 _KiUnexpectedEntrySize:
82 .long _KiUnexpectedInterrupt1 - _KiUnexpectedInterrupt0
85 .asciz "\n\x7\x7!!! Unexpected Interrupt %02lx !!!\n"
88 .asciz "\n\x7\x7!!! Broken TrapFrame. Magic: %08lx MagicB: %08lx!!!\n"
90 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
100 /* Enter the shared system call prolog */
103 /* Jump to the actual handler */
107 .func KiFastCallEntry
114 /* Set DS/ES to User Selector */
115 mov ecx, KGDT_R3_DATA | RPL_MASK
119 /* Set the current stack to Kernel Stack */
120 mov ecx, [fs:KPCR_TSS]
121 mov esp, ss:[ecx+KTSS_ESP0]
123 /* Set up a fake INT Stack. */
124 push KGDT_R3_DATA + RPL_MASK
125 push edx /* Ring 3 SS:ESP */
126 pushf /* Ring 3 EFLAGS */
127 push 2 /* Ring 0 EFLAGS */
128 add edx, 8 /* Skip user parameter list */
129 popf /* Set our EFLAGS */
130 or dword ptr [esp], EFLAGS_INTERRUPT_MASK /* Re-enable IRQs in EFLAGS, to fake INT */
131 push KGDT_R3_CODE + RPL_MASK
132 push KUSER_SHARED_SYSCALL_RET
134 /* Setup the Trap Frame stack */
140 push KGDT_R3_TEB + RPL_MASK
142 /* Save pointer to our PCR */
143 mov ebx, [fs:KPCR_SELF]
145 /* Get a pointer to the current thread */
146 mov esi, [ebx+KPCR_CURRENT_THREAD]
148 /* Set the exception handler chain terminator */
149 push [ebx+KPCR_EXCEPTION_LIST]
150 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
152 /* Use the thread's stack */
153 mov ebp, [esi+KTHREAD_INITIAL_STACK]
155 /* Push previous mode */
158 /* Skip the other registers */
161 /* Make space for us on the stack */
164 /* Write the previous mode */
165 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], UserMode
172 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
174 /* Check if the thread was being debugged */
175 test byte ptr [esi+KTHREAD_DEBUG_ACTIVE], 0xFF
177 /* Set the thread's trap frame */
178 mov [esi+KTHREAD_TRAP_FRAME], ebp
180 /* Save DR registers if needed */
181 //jnz Dr_FastCallDrSave
183 /* Set the trap frame debug header */
186 /* Enable interrupts */
192 * Find out which table offset to use. Converts 0x1124 into 0x10.
193 * The offset is related to the Table Index as such: Offset = TableIndex x 10
196 shr edi, SERVICE_TABLE_SHIFT
197 and edi, SERVICE_TABLE_MASK
200 /* Now add the thread's base system table to the offset */
201 add edi, [esi+KTHREAD_SERVICE_TABLE]
203 /* Get the true syscall ID and check it */
205 and eax, SERVICE_NUMBER_MASK
206 cmp eax, [edi+SERVICE_DESCRIPTOR_LIMIT]
208 /* Invalid ID, try to load Win32K Table */
209 jnb KiBBTUnexpectedRange
211 /* Check if this was Win32K */
212 cmp ecx, SERVICE_TABLE_TEST
216 mov ecx, [fs:KPCR_TEB]
218 /* Check if we should flush the User Batch */
220 or ebx, [ecx+TEB_GDI_BATCH_COUNT]
226 //call [_KeGdiFlushUserBatch]
231 /* Increase total syscall count */
232 inc dword ptr fs:[KPCR_SYSTEM_CALLS]
235 /* Increase per-syscall count */
236 mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
238 inc dword ptr [ecx+eax*4]
241 /* Users's current stack frame pointer is source */
245 /* Allocate room for argument list from kernel stack */
246 mov ebx, [edi+SERVICE_DESCRIPTOR_NUMBER]
250 /* Get pointer to function */
251 mov edi, [edi+SERVICE_DESCRIPTOR_BASE]
254 /* Allocate space on our stack */
257 /* Set the size of the arguments and the destination */
261 /* Make sure we're within the User Probe Address */
262 cmp esi, _MmUserProbeAddress
266 /* Copy the parameters */
271 * The following lines are for the benefit of GDB. It will see the return
272 * address of the "call ebx" below, find the last label before it and
273 * thinks that that's the start of the function. It will then check to see
274 * if it starts with a standard function prolog (push ebp, mov ebp,esp1).
275 * When that standard function prolog is not found, it will stop the
276 * stack backtrace. Since we do want to backtrace into usermode, let's
277 * make GDB happy and create a standard prolog.
285 /* Do the System Call */
290 /* Make sure the user-mode call didn't return at elevated IRQL */
291 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
293 mov esi, eax /* We need to save the syscall's return val */
294 call _KeGetCurrentIrql@0
297 mov eax, esi /* Restore it */
299 /* Get our temporary current thread pointer for sanity check */
300 mov ecx, fs:[KPCR_CURRENT_THREAD]
302 /* Make sure that we are not attached and that APCs are not disabled */
303 mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
306 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
313 /* Deallocate the kernel stack frame */
316 KeReturnFromSystemCall:
318 /* Get the Current Thread */
319 mov ecx, [fs:KPCR_CURRENT_THREAD]
321 /* Restore the old trap frame pointer */
322 mov edx, [ebp+KTRAP_FRAME_EDX]
323 mov [ecx+KTHREAD_TRAP_FRAME], edx
328 /* Disable interrupts */
331 /* Check for, and deliver, User-Mode APCs if needed */
332 CHECK_FOR_APC_DELIVER 1
334 /* Exit and cleanup */
335 TRAP_EPILOG FromSystemCall, DoRestorePreviousMode, DoNotRestoreSegments, DoNotRestoreVolatiles, DoRestoreEverything
338 KiBBTUnexpectedRange:
340 /* If this isn't a Win32K call, fail */
341 cmp ecx, SERVICE_TABLE_TEST
344 /* Set up Win32K Table */
347 call _PsConvertToGuiThread@0
349 /* Check return code */
352 /* Restore registers */
356 /* Reset trap frame address */
358 mov [esi+KTHREAD_TRAP_FRAME], ebp
360 /* Try the Call again, if we suceeded */
364 * The Shadow Table should have a special byte table which tells us
365 * whether we should return FALSE, -1 or STATUS_INVALID_SYSTEM_SERVICE.
368 /* Get the table limit and base */
369 lea edx, _KeServiceDescriptorTableShadow + SERVICE_TABLE_TEST
370 mov ecx, [edx+SERVICE_DESCRIPTOR_LIMIT]
371 mov edx, [edx+SERVICE_DESCRIPTOR_BASE]
373 /* Get the table address and add our index into the array */
375 and eax, SERVICE_NUMBER_MASK
378 /* Find out what we should return */
379 movsx eax, byte ptr [edx]
382 /* Return either 0 or -1, we've set it in EAX */
383 jle KeReturnFromSystemCall
385 /* Set STATUS_INVALID_SYSTEM_SERVICE */
386 mov eax, STATUS_INVALID_SYSTEM_SERVICE
387 jmp KeReturnFromSystemCall
391 /* Invalid System Call */
392 mov eax, STATUS_INVALID_SYSTEM_SERVICE
393 jmp KeReturnFromSystemCall
397 /* Check if this came from kernel-mode */
398 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
400 /* It's fine, go ahead with it */
403 /* Caller sent invalid parameters, fail here */
404 mov eax, STATUS_ACCESS_VIOLATION
409 /* Restore ESP0 stack */
410 mov ecx, [fs:KPCR_TSS]
411 mov esp, ss:[ecx+KTSS_ESP0]
413 /* Generate V86M Stack for Trap 6 */
419 /* Generate interrupt stack for Trap 6 */
420 push KGDT_R3_DATA + RPL_MASK
423 push KGDT_R3_CODE + RPL_MASK
429 /* Save current IRQL */
432 /* Set us at passive */
433 mov dword ptr fs:[KPCR_IRQL], 0
441 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
442 call _KeBugCheckEx@20
446 /* Get the index and APC state */
447 movzx eax, byte ptr [ecx+KTHREAD_APC_STATE_INDEX]
448 mov edx, [ecx+KTHREAD_COMBINED_APC_DISABLE]
455 push APC_INDEX_MISMATCH
456 call _KeBugCheckEx@20
463 /* Disable interrupts */
466 /* Check for, and deliver, User-Mode APCs if needed */
467 CHECK_FOR_APC_DELIVER 0
469 /* Exit and cleanup */
470 TRAP_EPILOG NotFromSystemCall, DoRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
473 .func Kei386EoiHelper@0
476 /* Disable interrupts */
479 /* Check for, and deliver, User-Mode APCs if needed */
480 CHECK_FOR_APC_DELIVER 0
482 /* Exit and cleanup */
483 _Kei386EoiHelper2ndEntry:
484 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoRestoreSegments, DoRestoreVolatiles, DoNotRestoreEverything
488 /* Move to EDX position */
489 add esp, KTRAP_FRAME_EDX
491 /* Restore volatiles */
496 /* Move to non-volatiles */
497 lea esp, [ebp+KTRAP_FRAME_EDI]
503 /* Skip error code and return */
508 /* Not yet supported */
514 /* Push error code */
520 /* Increase EIP so we skip the INT3 */
521 //inc dword ptr [ebp+KTRAP_FRAME_EIP]
523 /* Call debug service dispatcher */
524 mov eax, [ebp+KTRAP_FRAME_EAX]
525 mov ecx, [ebp+KTRAP_FRAME_ECX]
526 mov edx, [ebp+KTRAP_FRAME_EAX]
528 /* Check for V86 mode */
529 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
532 /* Check if this is kernel or user-mode */
533 test byte ptr [ebp+KTRAP_FRAME_CS], 1
535 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
538 /* Re-enable interrupts */
542 /* Call the debug routine */
551 call _KdpServiceDispatcher@12
555 /* Get the current process */
556 mov ebx, [fs:KPCR_CURRENT_THREAD]
557 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
559 /* Check if this is a VDM Process */
560 //cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
563 /* Exit through common routine */
564 jmp _Kei386EoiHelper@0
567 .func NtRaiseException@12
568 _NtRaiseException@12:
570 /* NOTE: We -must- be called by Zw* to have the right frame! */
571 /* Push the stack frame */
574 /* Get the current thread and restore its trap frame */
575 mov ebx, [fs:KPCR_CURRENT_THREAD]
576 mov edx, [ebp+KTRAP_FRAME_EDX]
577 mov [ebx+KTHREAD_TRAP_FRAME], edx
579 /* Set up stack frame */
582 /* Get the Trap Frame in EBX */
585 /* Get the exception list and restore */
586 mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
587 mov [fs:KPCR_EXCEPTION_LIST], eax
589 /* Get the parameters */
590 mov edx, [ebp+16] /* Search frames */
591 mov ecx, [ebp+12] /* Context */
592 mov eax, [ebp+8] /* Exception Record */
594 /* Raise the exception */
600 call _KiRaiseException@20
602 /* Restore trap frame in EBP */
606 /* Check the result */
610 /* Restore debug registers too */
617 /* NOTE: We -must- be called by Zw* to have the right frame! */
618 /* Push the stack frame */
621 /* Get the current thread and restore its trap frame */
622 mov ebx, [fs:KPCR_CURRENT_THREAD]
623 mov edx, [ebp+KTRAP_FRAME_EDX]
624 mov [ebx+KTHREAD_TRAP_FRAME], edx
626 /* Set up stack frame */
629 /* Save the parameters */
633 /* Call KiContinue */
639 /* Check if we failed (bad context record) */
643 /* Check if test alert was requested */
644 cmp dword ptr [ebp+12], 0
647 /* Test alert for the thread */
648 mov al, [ebx+KTHREAD_PREVIOUS_MODE]
650 call _KeTestAlertThread@4
653 /* Return to previous context */
664 /* EXCEPTION DISPATCHERS *****************************************************/
666 .func CommonDispatchException
667 _CommonDispatchException:
669 /* Make space for an exception record */
670 sub esp, EXCEPTION_RECORD_LENGTH
673 mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
675 mov [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], eax
676 mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], eax
677 mov [esp+EXCEPTION_RECORD_EXCEPTION_ADDRESS], ebx
678 mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
680 /* Check parameter count */
684 /* Get information */
685 lea ebx, [esp+SIZEOF_EXCEPTION_RECORD]
692 /* Set the record in ECX and check if this was V86 */
694 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
703 /* Calculate the previous mode */
704 mov eax, [ebp+KTRAP_FRAME_CS]
708 /* Dispatch the exception */
714 call _KiDispatchException@20
718 jmp _Kei386EoiHelper@0
721 .func DispatchNoParam
723 /* Call the common dispatcher */
725 call _CommonDispatchException
728 .func DispatchOneParam
730 /* Call the common dispatcher */
733 call _CommonDispatchException
736 .func DispatchTwoParam
738 /* Call the common dispatcher */
741 call _CommonDispatchException
744 /* HARDWARE TRAP HANDLERS ****************************************************/
748 /* Push error code */
755 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
758 /* Check if the frame was from kernelmode */
759 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
762 /* Check the old mode */
763 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
767 /* Re-enable interrupts for user-mode and send the exception */
769 mov eax, STATUS_INTEGER_DIVIDE_BY_ZERO
770 mov ebx, [ebp+KTRAP_FRAME_EIP]
774 /* Check if this is a VDM process */
775 mov ebx, [fs:KPCR_CURRENT_THREAD]
776 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
777 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
780 /* We don't support this yet! */
787 /* Push error code */
794 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
797 /* Check if the frame was from kernelmode */
798 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
801 /* Check the old mode */
802 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
806 /* Enable interrupts for user-mode */
810 /* Prepare the exception */
811 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAGS_TF
812 mov ebx, [ebp+KTRAP_FRAME_EIP]
813 mov eax, STATUS_SINGLE_STEP
817 /* Check if this is a VDM process */
818 mov ebx, [fs:KPCR_CURRENT_THREAD]
819 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
820 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
823 /* We don't support VDM! */
830 /* FIXME: This is an NMI, nothing like a normal exception */
832 jmp _KiSystemFatalException
837 /* Push error code */
844 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
847 /* Check if the frame was from kernelmode */
848 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
851 /* Check the old mode */
852 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
856 /* Enable interrupts for user-mode */
860 /* Prepare the exception */
865 /* Setup EIP, NTSTATUS and parameter count, then dispatch */
866 mov ebx, [ebp+KTRAP_FRAME_EIP]
868 mov eax, STATUS_BREAKPOINT
870 call _CommonDispatchException
873 /* Check if this is a VDM process */
874 mov ebx, [fs:KPCR_CURRENT_THREAD]
875 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
876 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
879 /* We don't support VDM! */
885 /* Push error code */
892 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
895 /* Check if the frame was from kernelmode */
896 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
899 /* Check the old mode */
900 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
904 /* Re-enable interrupts for user-mode and send the exception */
906 mov eax, STATUS_INTEGER_OVERFLOW
907 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! */
925 /* Push error code */
932 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
935 /* Check if the frame was from kernelmode */
936 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
939 /* It did, and this should never happen */
941 jmp _KiSystemFatalException
943 /* Check the old mode */
945 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
948 /* Re-enable interrupts for user-mode and send the exception */
951 mov eax, STATUS_ARRAY_BOUNDS_EXCEEDED
952 mov ebx, [ebp+KTRAP_FRAME_EIP]
956 /* Check if this is a VDM process */
957 mov ebx, [fs:KPCR_CURRENT_THREAD]
958 mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
959 cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
962 /* We don't support this yet! */
969 /* Push error code */
975 /* Not yet supported */
979 /* Return to caller */
980 jmp _Kei386EoiHelper@0
985 /* Push error code */
991 /* Get the current thread and stack */
993 mov eax, [fs:KPCR_CURRENT_THREAD]
994 mov ecx, [eax+KTHREAD_INITIAL_STACK]
995 sub ecx, NPX_FRAME_LENGTH
997 /* Check if emulation is enabled */
998 test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
1002 /* Check if the NPX state is loaded */
1003 cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1008 and ebx, ~(CR0_MP + CR0_TS + CR0_EM)
1011 /* Check the NPX thread */
1012 mov edx, [fs:KPCR_NPX_THREAD]
1016 /* Get the NPX Stack */
1017 mov esi, [edx+KTHREAD_INITIAL_STACK]
1018 sub esi, NPX_FRAME_LENGTH
1020 /* Check if we have FXSR and check which operand to use */
1021 test byte ptr _KeI386FxsrPresent, 1
1030 /* Set the thread's state to dirty */
1031 mov byte ptr [edx+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
1034 /* Check if we have FXSR and choose which operand to use */
1035 test byte ptr _KeI386FxsrPresent, 1
1044 /* Set state loaded */
1045 mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
1046 mov [fs:KPCR_NPX_THREAD], eax
1048 /* Enable interrupts to happen now */
1052 /* Check if CR0 needs to be reloaded due to a context switch */
1053 cmp dword ptr [ecx+FN_CR0_NPX_STATE], 0
1054 jz _Kei386EoiHelper@0
1056 /* We have to reload CR0... disable interrupts */
1059 /* Get CR0 and update it */
1061 or ebx, [ecx+FN_CR0_NPX_STATE]
1064 /* Restore interrupts and check if TS is back on */
1067 jz _Kei386EoiHelper@0
1069 /* Clear TS, and loop handling again */
1075 /* Check if TS is set */
1077 jnz TsSetOnLoadedState
1079 /* Check if the trap came from user-mode */
1083 /* Did this come from kernel-mode? */
1084 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1087 /* It came from user-mode, so this would only be valid inside a VDM */
1088 /* Since we don't actually have VDMs in ROS, bugcheck. */
1092 /* TS shouldn't be set, unless this we don't have a Math Processor */
1096 /* Strange that we got a trap at all, but ignore and continue */
1098 jmp _Kei386EoiHelper@0
1101 /* Cause a bugcheck */
1107 push TRAP_CAUSE_UNKNOWN
1108 call _KeBugCheckEx@20
1111 /* Cause a bugcheck */
1116 push TRAP_CAUSE_UNKNOWN
1117 call _KeBugCheckEx@20
1124 /* Can't really do too much */
1126 jmp _KiSystemFatalException
1131 /* Push error code */
1137 /* Enable interrupts and bugcheck */
1140 jmp _KiSystemFatalException
1149 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1152 /* Check if the frame was from kernelmode */
1153 test word ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1157 /* Check if OF was set during iretd */
1158 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAG_ZERO
1162 /* It was, just mask it out */
1163 and dword ptr [ebp+KTRAP_FRAME_EFLAGS], ~EFLAG_ZERO
1164 jmp _Kei386EoiHelper@0
1167 /* TSS failure for some other reason: crash */
1169 jmp _KiSystemFatalException
1177 /* FIXME: ROS Doesn't handle segment faults yet */
1179 jmp _KiSystemFatalException
1187 /* FIXME: ROS Doesn't handle stack faults yet */
1189 jmp _KiSystemFatalException
1195 /* It this a V86 GPF? */
1196 test dword ptr [esp+12], EFLAGS_V86_MASK
1199 /* Enter V86 Trap */
1200 V86_TRAP_PROLOG kitd
1202 /* Make sure that this is a V86 process */
1203 mov ecx, [fs:KPCR_CURRENT_THREAD]
1204 mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
1205 cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
1208 /* Otherwise, something is very wrong, raise an exception */
1210 mov ebx, [ebp+KTRAP_FRAME_EIP]
1212 mov eax, STATUS_ACCESS_VIOLATION
1213 jmp _DispatchTwoParam
1217 /* Go to APC level */
1221 /* Save old IRQL and enable interrupts */
1225 /* Handle the opcode */
1226 call _Ki386HandleOpcodeV86@0
1228 /* Check if this was VDM */
1237 /* Lower IRQL and disable interrupts */
1242 /* Check if this was a V86 trap */
1243 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1246 /* Exit the V86 Trap */
1251 /* Either this wasn't V86, or it was, but an APC interrupted us */
1252 jmp _Kei386EoiHelper@0
1258 /* Check if this was from kernel-mode */
1259 test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
1262 /* FIXME: Check for GPF during GPF */
1264 /* Get the opcode and trap frame */
1265 mov eax, [ebp+KTRAP_FRAME_EIP]
1267 mov edx, [ebp+KTRAP_FRAME_EBP]
1269 /* We want to check if this was POP [DS/ES/FS/GS] */
1270 add edx, KTRAP_FRAME_DS
1273 add edx, KTRAP_FRAME_ES - KTRAP_FRAME_DS
1276 add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
1279 add edx, KTRAP_FRAME_GS - KTRAP_FRAME_FS
1283 /* It isn't, was it IRETD? */
1287 /* Get error code */
1288 lea edx, [ebp+KTRAP_FRAME_ESP]
1289 mov ax, [ebp+KTRAP_FRAME_ERROR_CODE]
1293 mov cx, word ptr [edx+4]
1298 /* This should be a Ki386CallBios return */
1299 mov eax, offset _Ki386BiosCallReturnAddress
1303 cmp ax, KGDT_R0_CODE + RPL_MASK
1306 /* Jump to return address */
1307 jmp _Ki386BiosCallReturnAddress
1310 /* Check if the thread was in kernel mode */
1311 mov ebx, [fs:KPCR_CURRENT_THREAD]
1312 test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
1315 /* Set RPL_MASK for check below */
1316 or word ptr [edx+4], RPL_MASK
1319 /* Check if the IRET goes to user-mode */
1320 test dword ptr [edx+4], RPL_MASK
1323 /* Setup trap frame to copy */
1324 mov ecx, (KTRAP_FRAME_LENGTH - 12) / 4
1325 lea edx, [ebp+KTRAP_FRAME_ERROR_CODE]
1329 /* Copy each field */
1335 /* Enable interrupts and adjust stack */
1340 /* Setup exception record */
1341 mov ebx, [ebp+KTRAP_FRAME_EIP]
1342 mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
1344 mov eax, STATUS_ACCESS_VIOLATION
1345 jmp _DispatchTwoParam
1349 /* FIXME: Handle RDMSR/WRMSR */
1355 /* Check if this was an MSR opcode */
1359 /* Check if DS is Ring 3 */
1360 cmp word ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1363 /* Otherwise, fix it up */
1364 mov dword ptr [ebp+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
1369 /* Check if ES is Ring 3 */
1370 cmp word ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1373 /* Otherwise, fix it up */
1374 mov dword ptr [ebp+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
1380 lea eax, [ebp+KTRAP_FRAME_ESP]
1385 /* Handle segment POP fault by setting it to 0 */
1388 mov dword ptr [edx], eax
1392 /* Do a trap exit */
1393 TRAP_EPILOG NotFromSystemCall, DoNotRestorePreviousMode, DoNotRestoreSegments, DoRestoreVolatiles, DoRestoreEverything
1397 /* FIXME: Unhandled */
1407 /* Call the C exception handler */
1410 call _KiPageFaultHandler
1413 /* Return to caller */
1414 jmp _Kei386EoiHelper@0
1419 /* Push error code */
1426 /* Raise a fatal exception */
1428 jmp _KiSystemFatalException
1433 /* Push error code */
1439 /* FIXME: ROS Doesn't handle FPU faults yet */
1441 jmp _KiSystemFatalException
1446 /* Push error code */
1452 /* FIXME: ROS Doesn't handle alignment faults yet */
1454 jmp _KiSystemFatalException
1457 .func KiSystemFatalException
1458 _KiSystemFatalException:
1460 /* Push the trap frame */
1463 /* Push empty parameters */
1468 /* Push trap number and bugcheck code */
1470 push UNEXPECTED_KERNEL_MODE_TRAP
1471 call _KeBugCheckWithTf@24
1475 /* UNEXPECTED INTERRUPT HANDLERS **********************************************/
1477 .globl _KiStartUnexpectedRange@0
1478 _KiStartUnexpectedRange@0:
1480 GENERATE_INT_HANDLERS
1482 .globl _KiEndUnexpectedRange@0
1483 _KiEndUnexpectedRange@0:
1484 jmp _KiUnexpectedInterruptTail
1486 .func KiUnexpectedInterruptTail
1487 _KiUnexpectedInterruptTail:
1489 /* Enter interrupt trap */
1490 INT_PROLOG kui, DoNotPushFakeErrorCode
1492 /* Increase interrupt count */
1493 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
1495 /* Put vector in EBX and make space for KIRQL */
1499 /* Begin interrupt */
1503 call _HalBeginSystemInterrupt@12
1505 /* Check if it was spurious or not */
1509 /* Spurious, ignore it */
1511 jmp _Kei386EoiHelper2ndEntry
1514 /* Unexpected interrupt, print a message on debug builds */
1517 push offset _UnexpectedMsg
1522 /* Exit the interrupt */
1525 call _HalEndSystemInterrupt@8
1526 jmp _Kei386EoiHelper@0
1529 .globl _KiUnexpectedInterrupt
1530 _KiUnexpectedInterrupt:
1532 /* Bugcheck with invalid interrupt code */
1536 /* INTERRUPT HANDLERS ********************************************************/
1538 .func KiInterruptTemplate
1539 _KiInterruptTemplate:
1541 /* Enter interrupt trap */
1542 INT_PROLOG kit, DoPushFakeErrorCode
1545 _KiInterruptTemplate2ndDispatch:
1546 /* Dummy code, will be replaced by the address of the KINTERRUPT */
1549 _KiInterruptTemplateObject:
1550 /* Dummy jump, will be replaced by the actual jump */
1551 jmp _KeSynchronizeExecution@12
1553 _KiInterruptTemplateDispatch:
1554 /* Marks the end of the template so that the jump above can be edited */
1556 .func KiChainedDispatch2ndLvl@0
1557 _KiChainedDispatch2ndLvl@0:
1559 /* Not yet supported */
1563 .func KiChainedDispatch@0
1564 _KiChainedDispatch@0:
1566 /* Increase interrupt count */
1567 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
1569 /* Save trap frame */
1572 /* Save vector and IRQL */
1573 mov eax, [edi+KINTERRUPT_VECTOR]
1574 mov ecx, [edi+KINTERRUPT_IRQL]
1580 /* Begin interrupt */
1584 call _HalBeginSystemInterrupt@12
1586 /* Check if it was handled */
1590 /* Call the 2nd-level handler */
1591 call _KiChainedDispatch2ndLvl@0
1593 /* Exit the interrupt */
1596 call _HalEndSystemInterrupt@8
1597 jmp _Kei386EoiHelper@0
1600 .func KiInterruptDispatch@0
1601 _KiInterruptDispatch@0:
1603 /* Increase interrupt count */
1604 inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
1606 /* Save trap frame */
1609 /* Save vector and IRQL */
1610 mov eax, [edi+KINTERRUPT_VECTOR]
1611 mov ecx, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
1617 /* Begin interrupt */
1621 call _HalBeginSystemInterrupt@12
1623 /* Check if it was handled */
1627 /* Acquire the lock */
1629 mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
1630 ACQUIRE_SPINLOCK(esi, IntSpin)
1633 mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
1636 call [edi+KINTERRUPT_SERVICE_ROUTINE]
1638 /* Release the lock */
1639 RELEASE_SPINLOCK(esi)
1641 /* Exit the interrupt */
1644 call _HalEndSystemInterrupt@8
1645 jmp _Kei386EoiHelper@0
1648 /* Exit the interrupt */
1651 jmp _Kei386EoiHelper@0
1655 SPIN_ON_LOCK esi, GetIntLock