2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/include/i386/asmmacro.S
5 * PURPOSE: Assembly Macros for Spinlocks and common Trap Code
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES ******************************************************************/
13 // Arguments for TRAP_EPILOG
14 #define FromSystemCall 1
15 #define DoRestorePreviousMode 1
16 #define DoRestoreEverything 1
17 #define DoRestoreSegments 1
18 #define DoRestoreVolatiles 1
19 #define DoPushFakeErrorCode 1
21 #define DoFixupAbios 1
22 #define NotFromSystemCall 0
23 #define DoNotRestorePreviousMode 0
24 #define DoNotRestoreEverything 0
25 #define DoNotRestoreSegments 0
26 #define DoNotRestoreVolatiles 0
27 #define DoNotPushFakeErrorCode 0
28 #define DoNotFixupV86 0
29 #define DoNotFixupAbios 0
32 #define INT_32_DPL0 0x8E00
33 #define INT_32_DPL3 0xEE00
36 // This table contains the prefix flags that are used by V86 emulation
38 .equ PREFIX_FLAG_ES, 0x00000100
39 .equ PREFIX_FLAG_CS, 0x00000200
40 .equ PREFIX_FLAG_SS, 0x00000400
41 .equ PREFIX_FLAG_DS, 0x00000800
42 .equ PREFIX_FLAG_FS, 0x00001000
43 .equ PREFIX_FLAG_GS, 0x00002000
44 .equ PREFIX_FLAG_OPER32, 0x00004000
45 .equ PREFIX_FLAG_ADDR32, 0x00008000
46 .equ PREFIX_FLAG_LOCK, 0x00010000
47 .equ PREFIX_FLAG_REPNE, 0x00020000
48 .equ PREFIX_FLAG_REP, 0x00040000
50 .intel_syntax noprefix
53 // These macros are inlined equivalents of KiAcquire/ReleaseSpinlock, that is,
54 // they will not be compiled into non-SMP builds. Usage is as follows:
58 // ACQUIRE_SPINLOCK(reg, .spin)
59 // <thread-safe code here>
60 // RELEASE_SPINLOCK(reg)
65 // <any necessary steps to be able to jump back safely>
66 / SPIN_ON_LOCK(reg, .BeginYourFunction)
71 #define ACQUIRE_SPINLOCK(x, y) \
72 lock bts dword ptr [x], 0; \
74 #define RELEASE_SPINLOCK(x) mov byte ptr [x], 0
75 #define SPIN_ON_LOCK(x, y) \
77 test dword ptr [x], 1; \
83 #define ACQUIRE_SPINLOCK(x, y)
84 #define RELEASE_SPINLOCK(x)
88 // @name UNHANDLED_PATH
101 /* Print debug message */
103 push offset _UnhandledMsg
107 /* Loop indefinitely */
114 // This macro creates an IDT entry for the given handler
117 // Pointer to the IDT handler
120 // Descriptor Bits to associate
124 .macro idt Handler, Bits
131 // @name GENERATE_IDT_STUB
133 // This macro creates an IDT entry for an unexpected interrupt handler.
139 .macro GENERATE_IDT_STUB Number
140 idt _KiUnexpectedInterrupt&Number, INT_32_DPL0
144 // @name GENERATE_IDT_STUBS
146 // This macro creates unexpected interrupt IDT entries.
153 .macro GENERATE_IDT_STUBS
162 // @name GENERATE_INT_HANDLER
164 // This macro creates an unexpected interrupt handler.
170 .macro GENERATE_INT_HANDLER Number
171 .func KiUnexpectedInterrupt&Number
172 _KiUnexpectedInterrupt&Number:
173 push PRIMARY_VECTOR_BASE + Number
174 jmp _KiEndUnexpectedRange@0
179 // @name GENERATE_INT_HANDLERS
181 // This macro creates the unexpected interrupt handlers.
188 .macro GENERATE_INT_HANDLERS
191 GENERATE_INT_HANDLER %i
197 // @name INVALID_V86_OPCODE
199 // This macro creates one or more entries for unhandled V86 Opcodes
200 // in the V86 Opcode Table.
203 // Number of entries to generate.
207 .macro INVALID_V86_OPCODE count
214 // @name GENERATE_PREFIX_HANDLER
216 // This macro creates a prefix opcode handler.
222 .macro GENERATE_PREFIX_HANDLER Name
223 .func Opcode&Name&PrefixV86
224 _Opcode&Name&PrefixV86:
225 or ebx, PREFIX_FLAG_&Name
226 jmp _OpcodeGenericPrefixV86
231 // @name INVALID_V86_OPCODE
233 // This macro prints out visible message and hangs the computer.
237 // @remark Temporary debugging use.
239 .macro UNHANDLED_V86_OPCODE
240 /* Print debug message, breakpoint and freeze */
242 push offset V86DebugMsg
251 // This macro contains out-of-line code for various Trap Frame Fixups, such as:
253 // - DR Fixup: Loads and restores DR registers.
254 // - V86 Fixup: Loads and restores V86 segments.
255 // - ABIOS Fixup: Loads and restores the ABIOS state and stack.
259 // @remark ebp = PKTRAP_FRAME
261 .macro TRAP_FIXUPS Label, EndLabel, V86Fix, AbiosFix
264 /* Check if this was V86 mode */
265 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
268 /* Check if it was user mode */
269 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
279 mov [ebp+KTRAP_FRAME_DR0], ebx
280 mov [ebp+KTRAP_FRAME_DR1], ecx
281 mov [ebp+KTRAP_FRAME_DR2], edi
289 mov [ebp+KTRAP_FRAME_DR3], ebx
290 mov [ebp+KTRAP_FRAME_DR6], ecx
291 mov [ebp+KTRAP_FRAME_DR7], edi
298 mov edi, fs:[KPCR_PRCB]
301 mov ebx, [edi+KPRCB_DR0]
302 mov ecx, [edi+KPRCB_DR1]
309 mov ebx, [edi+KPRCB_DR2]
310 mov ecx, [edi+KPRCB_DR3]
317 mov ebx, [edi+KPRCB_DR6]
318 mov ecx, [edi+KPRCB_DR7]
333 /* Get V86 segment registers */
334 mov eax, [ebp+KTRAP_FRAME_V86_FS]
335 mov ebx, [ebp+KTRAP_FRAME_V86_GS]
336 mov ecx, [ebp+KTRAP_FRAME_V86_ES]
337 mov edx, [ebp+KTRAP_FRAME_V86_DS]
339 /* Restore them into Protected Mode trap frame */
340 mov [ebp+KTRAP_FRAME_FS], ax
341 mov [ebp+KTRAP_FRAME_GS], bx
342 mov [ebp+KTRAP_FRAME_ES], cx
343 mov [ebp+KTRAP_FRAME_DS], dx
345 /* Go back to mainline code */
351 // @name SET_TF_DEBUG_HEADER
353 // This macro sets up the debug header in the trap frame.
357 // @remark ebp = PKTRAP_FRAME.
358 // edi/ebx = Have been saved and can be used.
360 .macro SET_TF_DEBUG_HEADER
361 /* Get the Debug Trap Frame EBP/EIP */
362 mov ebx, [ebp+KTRAP_FRAME_EBP]
363 mov edi, [ebp+KTRAP_FRAME_EIP]
365 /* Write the debug data */
366 mov [ebp+KTRAP_FRAME_DEBUGPOINTER], edx
367 mov dword ptr [ebp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
368 mov [ebp+KTRAP_FRAME_DEBUGEBP], ebx
369 mov [ebp+KTRAP_FRAME_DEBUGEIP], edi
373 // @name CHECK_FOR_APC_DELIVER
375 // This macro checks if the trapframe indicates a return to user-mode,
376 // and, if so, checks if user-mode APCs should be delivered.
378 // @param PreserveEax
379 // Determines if EAX should be preserved. Implies that the segment
380 // registers will also be saved.
382 // @remark ebp = PKTRAP_FRAME.
383 // ebx = Saved and will be used.
385 .macro CHECK_FOR_APC_DELIVER PreserveEax
386 /* Check for V86 mode */
387 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
390 /* Deliver APCs only if we were called from user mode */
391 test byte ptr [ebp+KTRAP_FRAME_CS], 1
394 /* Get the current thread */
396 mov ebx, PCR[KPCR_CURRENT_THREAD]
398 /* Make it non-alerted */
399 mov byte ptr [ebx+KTHREAD_ALERTED], 0
401 /* And only if any are actually pending */
402 cmp byte ptr [ebx+KTHREAD_PENDING_USER_APC], 0
405 /* Save pointer to Trap Frame */
409 /* Save some stuff that raising IRQL will kill */
410 mov [ebx+KTRAP_FRAME_EAX], eax
411 mov dword ptr [ebx+KTRAP_FRAME_FS], KGDT_R3_TEB + RPL_MASK
412 mov dword ptr [ebx+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
413 mov dword ptr [ebx+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
414 mov dword ptr [ebx+KTRAP_FRAME_GS], 0
417 /* Raise IRQL to APC_LEVEL */
429 call _KiDeliverApc@12
431 /* Return to old IRQL */
435 /* Restore EAX (only in volatile case) */
437 mov eax, [ebx+KTRAP_FRAME_EAX]
447 // This macro creates a standard trap entry prologue.
448 // It should be used for entry into any kernel trap (KiTrapXx), but not for
449 // system calls, which require special handling.
452 // Identifying name of the caller function; will be used to append
453 // to the name V86 and DR helper functions, which must already exist.
455 // @remark Use as follows:
457 // /* Push fake error code */
460 // /* Enter common prologue */
464 // <Your Trap Code Here>
466 .macro TRAP_PROLOG Label EndLabel
467 /* Just to be safe, clear out the HIWORD, since it's reserved */
468 mov word ptr [esp+2], 0
470 /* Save the non-volatiles */
476 /* Save FS and set it to PCR */
482 /* Save exception list and bogus previous mode */
483 push fs:[KPCR_EXCEPTION_LIST]
486 /* Save volatiles and segment registers */
494 /* Set the R3 data segment */
495 mov ax, KGDT_R3_DATA + RPL_MASK
497 /* Skip debug registers and debug stuff */
500 /* Load the segment registers */
506 /* Check if this interrupt happened in 16-bit mode */
508 jb _Ki16BitStackException
513 /* Check if this was from V86 Mode */
514 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
518 /* Get current thread */
519 mov ecx, fs:[KPCR_CURRENT_THREAD]
523 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
525 /* Check if the thread was being debugged */
526 test byte ptr [ecx+KTHREAD_DEBUG_ACTIVE], 0xFF
529 /* Set the Trap Frame Debug Header */
537 // This macro creates a standard interrupt entry prologue.
538 // It should be used for entry into any interrupt, including software.
541 // Identifying name of the caller function; will be used to append
542 // to the name V86, ABIOS and DR helper functions, which must exist.
544 // @remark For software interrupts, make sure that a fake INT stack is created.
546 .macro INT_PROLOG Label EndLabel FakeErrorCode
549 /* Save fake error code */
553 /* Save the non-volatiles */
559 /* Skip debug registers and other stuff */
566 mov [esp+KTRAP_FRAME_EAX], eax
567 mov [esp+KTRAP_FRAME_ECX], ecx
568 mov [esp+KTRAP_FRAME_EDX], edx
569 mov dword ptr [esp+KTRAP_FRAME_PREVIOUS_MODE], -1
571 /* Check if this was from V86 Mode */
572 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
575 /* Check if this was kernel mode */
577 cmp word ptr [esp+KTRAP_FRAME_CS], KGDT_R0_CODE
581 mov word ptr [esp+KTRAP_FRAME_FS], fs
582 mov word ptr [esp+KTRAP_FRAME_DS], ds
583 mov word ptr [esp+KTRAP_FRAME_ES], es
584 mov [esp+KTRAP_FRAME_GS], gs
586 /* Load the segment registers (use OVERRIDE (0x66)) */
588 mov eax, KGDT_R3_DATA | RPL_MASK
597 /* Get the previous exception list */
598 mov ebx, fs:[KPCR_EXCEPTION_LIST]
600 /* Set the exception handler chain terminator */
601 mov dword ptr fs:[KPCR_EXCEPTION_LIST], -1
603 /* Save the previous exception list */
604 mov [esp+KTRAP_FRAME_EXCEPTION_LIST], ebx
607 /* Setup the 16-bit stack */
608 lea eax, [esp+KTRAP_FRAME_ERROR_CODE]
609 lea ecx, [esp+KTRAP_FRAME_EIP]
614 /* Check if this is the ABIOS stack */
615 /* cmp esp, 0x10000*/
618 /* Delete error code */
619 and dword ptr [esp+KTRAP_FRAME_ERROR_CODE], 0
621 /* Get the current thread and clear direction flag */
622 mov ecx, PCR[KPCR_CURRENT_THREAD]
626 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
628 /* Check if the thread was being debugged */
629 test byte ptr [ecx+KTHREAD_DEBUG_ACTIVE], 0xFF
636 /* Save DR registers if needed */
639 /* Set the trap frame debug header */
645 // @name SYSCALL_PROLOG
647 // This macro creates a system call entry prologue.
648 // It should be used for entry into any fast-system call (KiGetTickCount,
649 // KiCallbackReturn, KiRaiseAssertion) and the generic system call handler
653 // Unique label identifying the name of the caller function; will be
654 // used to append to the name of the DR helper function, which must
659 .macro SYSCALL_PROLOG Label EndLabel
660 /* Create a trap frame */
668 /* Load PCR Selector into fs */
673 /* Get a pointer to the current thread */
674 mov esi, PCR[KPCR_CURRENT_THREAD]
676 /* Save the previous exception list */
677 push PCR[KPCR_EXCEPTION_LIST]
679 /* Set the exception handler chain terminator */
680 mov dword ptr PCR[KPCR_EXCEPTION_LIST], -1
682 /* Save the old previous mode */
683 push [esi+KTHREAD_PREVIOUS_MODE]
685 /* Skip the other registers */
688 /* Set the new previous mode based on the saved CS selector */
691 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], bl
693 /* Go on the Kernel stack frame */
696 /* Save the old trap frame pointer where EDX would be saved */
697 mov ebx, [esi+KTHREAD_TRAP_FRAME]
698 mov [ebp+KTRAP_FRAME_EDX], ebx
701 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
703 /* Check if the thread was being debugged */
704 test byte ptr [esi+KTHREAD_DEBUG_ACTIVE], 0xFF
706 /* Set the thread's trap frame and clear direction flag */
707 mov [esi+KTHREAD_TRAP_FRAME], ebp
710 /* Save DR registers if needed */
713 /* Set the trap frame debug header */
717 /* Enable interrupts */
722 // @name FASTCALL_PROLOG
727 // Unique label identifying the name of the caller function; will be
728 // used to append to the name of the DR helper function, which must
733 .macro FASTCALL_PROLOG Label EndLabel
735 /* Set user selector */
736 mov ecx, KGDT_R3_DATA | RPL_MASK
742 /* Set DS/ES to User Selector */
746 /* Set the current stack to Kernel Stack */
747 mov ecx, PCR[KPCR_TSS]
748 mov esp, [ecx+KTSS_ESP0]
750 /* Set up a fake INT Stack. */
751 push KGDT_R3_DATA + RPL_MASK
752 push edx /* Ring 3 SS:ESP */
753 pushf /* Ring 3 EFLAGS */
754 push 2 /* Ring 0 EFLAGS */
755 add edx, 8 /* Skip user parameter list */
756 popf /* Set our EFLAGS */
757 or dword ptr [esp], EFLAGS_INTERRUPT_MASK /* Re-enable IRQs in EFLAGS, to fake INT */
758 push KGDT_R3_CODE + RPL_MASK
759 push dword ptr ds:KUSER_SHARED_SYSCALL_RET
761 /* Setup the Trap Frame stack */
767 push KGDT_R3_TEB + RPL_MASK
769 /* Save pointer to our PCR */
770 mov ebx, PCR[KPCR_SELF]
772 /* Get a pointer to the current thread */
773 mov esi, [ebx+KPCR_CURRENT_THREAD]
775 /* Set the exception handler chain terminator */
776 push [ebx+KPCR_EXCEPTION_LIST]
777 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
779 /* Use the thread's stack */
780 mov ebp, [esi+KTHREAD_INITIAL_STACK]
782 /* Push previous mode */
785 /* Skip the other registers */
788 /* Make space for us on the stack */
791 /* Write the previous mode */
792 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], UserMode
799 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
801 /* Check if the thread was being debugged */
802 test byte ptr [esi+KTHREAD_DEBUG_ACTIVE], 0xFF
804 /* Set the thread's trap frame */
805 mov [esi+KTHREAD_TRAP_FRAME], ebp
807 /* Save DR registers if needed */
810 /* Set the trap frame debug header */
814 /* Enable interrupts */
819 // @name V86_TRAP_PROLOG
821 // This macro creates a V86 Trap entry prologue.
822 // It should be used for entry into any fast-system call (KiGetTickCount,
823 // KiCallbackReturn, KiRaiseAssertion) and the generic system call handler
827 // Unique label identifying the name of the caller function; will be
828 // used to append to the name of the DR helper function, which must
833 .macro V86_TRAP_PROLOG Label EndLabel
835 /* Skip everything to the error code */
836 sub esp, KTRAP_FRAME_ERROR_CODE
838 /* Clear the error code */
839 mov word ptr [esp+KTRAP_FRAME_ERROR_CODE+2], 0
841 /* Save the registers we'll trample */
842 mov [esp+KTRAP_FRAME_EBX], ebx
843 mov [esp+KTRAP_FRAME_EAX], eax
844 mov [esp+KTRAP_FRAME_EBP], ebp
845 mov [esp+KTRAP_FRAME_ESI], esi
846 mov [esp+KTRAP_FRAME_EDI], edi
848 /* Save PCR and Ring 3 segments */
850 mov eax, KGDT_R3_DATA + RPL_MASK
852 /* Save ECX and EDX too */
853 mov [esp+KTRAP_FRAME_ECX], ecx
854 mov [esp+KTRAP_FRAME_EDX], edx
856 /* Set debugging markers */
857 mov dword ptr [esp+KTRAP_FRAME_PREVIOUS_MODE], -1
858 mov dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
860 /* Now set segments (use OVERRIDE, 0x66) */
868 /* Set the trap frame in the stack and clear the direction flag */
872 /* Save the exception list */
873 mov eax, fs:[KPCR_EXCEPTION_LIST]
874 mov [esp+KTRAP_FRAME_EXCEPTION_LIST], eax
876 /* Check if we need debugging */
878 test eax, ~DR7_RESERVED_MASK
879 mov [esp+KTRAP_FRAME_DR7], eax
886 // @name V86_TRAP_EPILOG
888 // This macro creates an epilogue for leaving V86 traps
894 .macro V86_TRAP_EPILOG
896 /* Get the current thread and make it unalerted */
898 mov ebx, PCR[KPCR_CURRENT_THREAD]
899 mov byte ptr [ebx+KTHREAD_ALERTED], 0
901 /* Check if it has User-mode APCs pending */
902 cmp byte ptr [ebx+KTHREAD_PENDING_USER_APC], 0
905 /* It doesn't, pop the frame */
906 add esp, KTRAP_FRAME_EDX
911 /* Check if DR registers should be restored */
912 test dword ptr [ebp+KTRAP_FRAME_DR7], ~DR7_RESERVED_MASK
915 /* Finish popping the rest of the frame, and return to P-mode */
929 mov esi, [ebp+KTRAP_FRAME_DR0]
930 mov edi, [ebp+KTRAP_FRAME_DR1]
935 /* Get DR2 and load DR0-2 */
936 mov ebx, [ebp+KTRAP_FRAME_DR2]
942 mov esi, [ebp+KTRAP_FRAME_DR0]
943 mov edi, [ebp+KTRAP_FRAME_DR1]
944 mov ebx, [ebp+KTRAP_FRAME_DR7]
954 /* Raise to APC level */
958 /* Save KIRQL and deliver APCs */
964 call _KiDeliverApc@12
971 /* Check if we're not in V86 anymore */
972 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
979 // This macro creates an epilogue for leaving any system trap.
980 // It is used for exiting system calls, exceptions, interrupts and generic
984 // Specifies whether this trap will exit a system call. If so, special
985 // code will be assembled to potentially use SYSEXIT instead of IRETD.
987 // @param RestorePreviousMode
988 // Specifies if the previous mode should be restored.
990 // @param RestoreSegments
991 // Specifies if the segment registers should be restored.
993 // @param RestoreVolatiles
994 // Specifies if the volatile registers should be restored.
996 // @param RestoreAllRegs
997 // Specifies if volatiles and segments should both be restored.
1001 .macro TRAP_EPILOG SystemCall, RestorePreviousMode, RestoreSegments, RestoreVolatiles, RestoreAllRegs
1003 /* Assert the flags */
1006 test edx, EFLAGS_INTERRUPT_MASK
1009 /* Assert the stack */
1013 /* Assert the trap frame */
1017 sub dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
1025 /* Assert exception list */
1026 cmp dword ptr PCR[KPCR_EXCEPTION_LIST], 0
1035 /* Get exception list */
1036 mov edx, [esp+KTRAP_FRAME_EXCEPTION_LIST]
1039 /* Assert the saved exception list */
1047 mov PCR[KPCR_EXCEPTION_LIST], edx
1049 .if \RestorePreviousMode
1050 /* Get previous mode */
1051 mov ecx, [esp+KTRAP_FRAME_PREVIOUS_MODE]
1054 /* Assert the saved previous mode */
1061 /* Restore the previous mode */
1062 mov esi, PCR[KPCR_CURRENT_THREAD]
1063 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], cl
1067 /* Assert the saved previous mode */
1068 mov ecx, [esp+KTRAP_FRAME_PREVIOUS_MODE]
1076 /* Check for debug registers */
1077 test dword ptr [esp+KTRAP_FRAME_DR7], ~DR7_RESERVED_MASK
1082 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1085 /* Check if the frame was edited */
1086 test word ptr [esp+KTRAP_FRAME_CS], FRAME_EDITED
1090 /* Check the old mode */
1091 cmp word ptr [esp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1092 bt word ptr [esp+KTRAP_FRAME_CS], 0
1097 .if \RestoreVolatiles
1098 /* Restore volatiles */
1099 mov edx, [esp+KTRAP_FRAME_EDX]
1100 mov ecx, [esp+KTRAP_FRAME_ECX]
1101 mov eax, [esp+KTRAP_FRAME_EAX]
1104 /* Check if we were called from kernel mode */
1105 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1108 .if \RestoreSegments
1109 /* Restore segment registers */
1110 lea esp, [ebp+KTRAP_FRAME_GS]
1118 lea esp, [ebp+KTRAP_FRAME_FS]
1122 /* Skip debug information and unsaved registers */
1123 lea esp, [ebp+KTRAP_FRAME_EDI]
1129 /* Check for ABIOS */
1130 cmp word ptr [esp+8], 0x80
1133 /* Pop error code */
1137 /* Check if previous CS is from user-mode */
1138 test dword ptr [esp+4], 1
1140 /* It is, so use Fast Exit */
1143 /* Jump back to stub */
1155 /* Is SYSEXIT Supported/Wanted? */
1156 cmp dword ptr ss:[_KiFastSystemCallDisable], 0
1158 test dword ptr [esp+8], EFLAGS_TF
1161 /* Restore FS to TIB */
1162 mov ecx, KGDT_R3_TEB + RPL_MASK
1165 /* We will be cleaning up the stack ourselves */
1166 pop edx /* New Ring 3 EIP */
1167 add esp, 4 /* Skip Ring 3 DS */
1168 and dword ptr [esp], 0xfffffdff /* Remove EFLAGS_INTERRUPT_MASK from EFLAGS */
1169 popf /* Restore old EFLAGS */
1170 pop ecx /* Old Ring 3 SS:ESP */
1174 * ECX points to the old User Stack.
1175 * EDX points to the instruction to execute in usermode after the sysenter
1184 mov eax, [esp+KTRAP_FRAME_EAX]
1186 /* Skip registers */
1189 /* Restore segments and volatiles */
1196 /* Jump back to mainline code */
1202 /* Fix up the mask */
1203 add dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
1211 /* Check if this was V86 mode */
1212 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1215 /* Check if it was user mode */
1216 test word ptr [esp+KTRAP_FRAME_CS], MODE_MASK
1225 mov esi, [ebp+KTRAP_FRAME_DR0]
1226 mov edi, [ebp+KTRAP_FRAME_DR1]
1227 mov ebx, [ebp+KTRAP_FRAME_DR2]
1235 mov esi, [ebp+KTRAP_FRAME_DR3]
1236 mov edi, [ebp+KTRAP_FRAME_DR6]
1237 mov ebx, [ebp+KTRAP_FRAME_DR7]
1246 /* Restore real CS value */
1247 mov ebx, [esp+KTRAP_FRAME_TEMPCS]
1248 mov [esp+KTRAP_FRAME_CS], ebx
1251 * If ESP was modified, then a special interrupt exit stack
1252 * must be created to "update" ESP's value in a legal manner
1254 mov ebx, [esp+KTRAP_FRAME_TEMPESP]
1256 mov [esp+KTRAP_FRAME_ERROR_CODE], ebx
1258 /* Copy Interrupt Stack */
1259 mov esi, [esp+KTRAP_FRAME_EFLAGS]
1261 mov esi, [esp+KTRAP_FRAME_CS]
1263 mov esi, [esp+KTRAP_FRAME_EIP]
1266 .if \RestoreVolatiles
1267 /* Restore volatiles */
1268 mov eax, [esp+KTRAP_FRAME_EAX]
1269 mov edx, [esp+KTRAP_FRAME_EDX]
1270 mov ecx, [esp+KTRAP_FRAME_ECX]
1274 add esp, KTRAP_FRAME_EDI
1286 // This macro creates an epilogue for leaving any system trap.
1287 // It is used for exiting system calls, exceptions, interrupts and generic
1290 // @param Spurious - TRUE if the interrupt was unexpected and spurious.
1294 .macro INT_EPILOG Spurious
1297 /* Just exit the trap */
1298 jmp _Kei386EoiHelper@0
1300 /* Disable interrupts */
1303 /* End the interrupt and do EOI */
1304 call _HalEndSystemInterrupt@8
1305 jmp _Kei386EoiHelper@0
1311 .macro VERIFY_INT Label
1312 /* Get the current time and mask it to 192 ticks */
1313 mov eax, _KeTickCount
1316 /* Check if we're in the same tick area */
1317 cmp eax, dword ptr [edi+KINTERRUPT_TICK_COUNT]
1321 /* If we got here, then our count is too large */
1322 dec word ptr [edi+KINTERRUPT_DISPATCH_COUNT]
1327 .macro VERIFY_INT_END Label, Info
1330 /* Decrement the dispatch count and check if we should bug check */
1331 dec word ptr [edi+KINTERRUPT_DISPATCH_COUNT+2]
1334 /* Update the tick count */
1336 mov [edi+KINTERRUPT_TICK_COUNT], eax
1340 /* Check if the debugger is enabled */
1341 cmp byte ptr __KdDebuggerEnabled, 0
1344 /* It isn't, bugcheck */
1347 push [edi+KINTERRUPT_SERVICE_CONTEXT]
1348 push [edi+KINTERRUPT_SERVICE_ROUTINE]
1349 push HARDWARE_INTERRUPT_STORM
1350 call _KeBugCheckEx@20
1353 /* Debugger enabled, do a debug print + break instead */
1354 push [edi+KINTERRUPT_SERVICE_ROUTINE]
1355 push offset _IsrOverflowMsg
1360 /* Breakpoint handled, get the new tick count */
1361 mov eax, _KeTickCount
1365 /* Reset tick count */
1366 mov dword ptr [edi+KINTERRUPT_TICK_COUNT], eax
1367 mov word ptr [edi+KINTERRUPT_DISPATCH_COUNT+2], 64
1370 /* Put default overflow count and continue */
1371 mov ax, _KiISROverflow
1372 mov word ptr [edi+KINTERRUPT_DISPATCH_COUNT], ax
1376 /* Check if we wrapped */
1378 cmp eax, [edi+KINTERRUPT_TICK_COUNT]
1381 /* We did, start over */
1382 mov eax, _KeTickCount
1388 /* We don't verify interrupts on retail builds */
1389 .macro VERIFY_INT Label
1391 .macro VERIFY_INT_END Label, Info