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
100 /* Print debug message */
101 push offset _UnhandledMsg
105 /* Loop indefinitely */
112 // This macro creates an IDT entry for the given handler
115 // Pointer to the IDT handler
118 // Descriptor Bits to associate
122 .macro idt Handler, Bits
129 // @name GENERATE_IDT_STUB
131 // This macro creates an IDT entry for an unexpected interrupt handler.
137 .macro GENERATE_IDT_STUB Number
138 idt _KiUnexpectedInterrupt&Number, INT_32_DPL0
142 // @name GENERATE_IDT_STUBS
144 // This macro creates unexpected interrupt IDT entries.
151 .macro GENERATE_IDT_STUBS
160 // @name GENERATE_INT_HANDLER
162 // This macro creates an unexpected interrupt handler.
168 .macro GENERATE_INT_HANDLER Number
169 .func KiUnexpectedInterrupt&Number
170 _KiUnexpectedInterrupt&Number:
171 push PRIMARY_VECTOR_BASE + Number
172 jmp _KiEndUnexpectedRange@0
177 // @name GENERATE_INT_HANDLERS
179 // This macro creates the unexpected interrupt handlers.
186 .macro GENERATE_INT_HANDLERS
189 GENERATE_INT_HANDLER %i
195 // @name INVALID_V86_OPCODE
197 // This macro creates one or more entries for unhandled V86 Opcodes
198 // in the V86 Opcode Table.
201 // Number of entries to generate.
205 .macro INVALID_V86_OPCODE count
212 // @name GENERATE_PREFIX_HANDLER
214 // This macro creates a prefix opcode handler.
220 .macro GENERATE_PREFIX_HANDLER Name
221 .func Opcode&Name&PrefixV86
222 _Opcode&Name&PrefixV86:
223 or ebx, PREFIX_FLAG_&Name
224 jmp _OpcodeGenericPrefixV86
229 // @name INVALID_V86_OPCODE
231 // This macro prints out visible message and hangs the computer.
235 // @remark Temporary debugging use.
237 .macro UNHANDLED_V86_OPCODE
238 /* Print debug message, breakpoint and freeze */
240 push offset V86DebugMsg
249 // This macro contains out-of-line code for various Trap Frame Fixups, such as:
251 // - DR Fixup: Loads and restores DR registers.
252 // - V86 Fixup: Loads and restores V86 segments.
253 // - ABIOS Fixup: Loads and restores the ABIOS state and stack.
257 // @remark ebp = PKTRAP_FRAME
259 .macro TRAP_FIXUPS Label, EndLabel, V86Fix, AbiosFix
262 /* Check if this was V86 mode */
263 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
266 /* Check if it was user mode */
267 test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
277 mov [ebp+KTRAP_FRAME_DR0], ebx
278 mov [ebp+KTRAP_FRAME_DR1], ecx
279 mov [ebp+KTRAP_FRAME_DR2], edi
287 mov [ebp+KTRAP_FRAME_DR3], ebx
288 mov [ebp+KTRAP_FRAME_DR6], ecx
289 mov [ebp+KTRAP_FRAME_DR7], edi
296 mov edi, fs:[KPCR_PRCB]
299 mov ebx, [edi+KPRCB_DR0]
300 mov ecx, [edi+KPRCB_DR1]
307 mov ebx, [edi+KPRCB_DR2]
308 mov ecx, [edi+KPRCB_DR3]
315 mov ebx, [edi+KPRCB_DR6]
316 mov ecx, [edi+KPRCB_DR7]
331 /* Get V86 segment registers */
332 mov eax, [ebp+KTRAP_FRAME_V86_FS]
333 mov ebx, [ebp+KTRAP_FRAME_V86_GS]
334 mov ecx, [ebp+KTRAP_FRAME_V86_ES]
335 mov edx, [ebp+KTRAP_FRAME_V86_DS]
337 /* Restore them into Protected Mode trap frame */
338 mov [ebp+KTRAP_FRAME_FS], ax
339 mov [ebp+KTRAP_FRAME_GS], bx
340 mov [ebp+KTRAP_FRAME_ES], cx
341 mov [ebp+KTRAP_FRAME_DS], dx
343 /* Go back to mainline code */
349 // @name SET_TF_DEBUG_HEADER
351 // This macro sets up the debug header in the trap frame.
355 // @remark ebp = PKTRAP_FRAME.
356 // edi/ebx = Have been saved and can be used.
358 .macro SET_TF_DEBUG_HEADER
359 /* Get the Debug Trap Frame EBP/EIP */
360 mov ebx, [ebp+KTRAP_FRAME_EBP]
361 mov edi, [ebp+KTRAP_FRAME_EIP]
363 /* Write the debug data */
364 mov [ebp+KTRAP_FRAME_DEBUGPOINTER], edx
365 mov dword ptr [ebp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
366 mov [ebp+KTRAP_FRAME_DEBUGEBP], ebx
367 mov [ebp+KTRAP_FRAME_DEBUGEIP], edi
371 // @name CHECK_FOR_APC_DELIVER
373 // This macro checks if the trapframe indicates a return to user-mode,
374 // and, if so, checks if user-mode APCs should be delivered.
376 // @param PreserveEax
377 // Determines if EAX should be preserved. Implies that the segment
378 // registers will also be saved.
380 // @remark ebp = PKTRAP_FRAME.
381 // ebx = Saved and will be used.
383 .macro CHECK_FOR_APC_DELIVER PreserveEax
384 /* Check for V86 mode */
385 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
388 /* Deliver APCs only if we were called from user mode */
389 test byte ptr [ebp+KTRAP_FRAME_CS], 1
392 /* Get the current thread */
394 mov ebx, PCR[KPCR_CURRENT_THREAD]
396 /* Make it non-alerted */
397 mov byte ptr [ebx+KTHREAD_ALERTED], 0
399 /* And only if any are actually pending */
400 cmp byte ptr [ebx+KTHREAD_PENDING_USER_APC], 0
403 /* Save pointer to Trap Frame */
407 /* Save some stuff that raising IRQL will kill */
408 mov [ebx+KTRAP_FRAME_EAX], eax
409 mov dword ptr [ebx+KTRAP_FRAME_FS], KGDT_R3_TEB + RPL_MASK
410 mov dword ptr [ebx+KTRAP_FRAME_DS], KGDT_R3_DATA + RPL_MASK
411 mov dword ptr [ebx+KTRAP_FRAME_ES], KGDT_R3_DATA + RPL_MASK
412 mov dword ptr [ebx+KTRAP_FRAME_GS], 0
415 /* Raise IRQL to APC_LEVEL */
427 call _KiDeliverApc@12
429 /* Return to old IRQL */
433 /* Restore EAX (only in volatile case) */
435 mov eax, [ebx+KTRAP_FRAME_EAX]
445 // This macro creates a standard trap entry prologue.
446 // It should be used for entry into any kernel trap (KiTrapXx), but not for
447 // system calls, which require special handling.
450 // Identifying name of the caller function; will be used to append
451 // to the name V86 and DR helper functions, which must already exist.
453 // @remark Use as follows:
455 // /* Push fake error code */
458 // /* Enter common prologue */
462 // <Your Trap Code Here>
464 .macro TRAP_PROLOG Label EndLabel
465 /* Just to be safe, clear out the HIWORD, since it's reserved */
466 mov word ptr [esp+2], 0
468 /* Save the non-volatiles */
474 /* Save FS and set it to PCR */
480 /* Save exception list and bogus previous mode */
481 push fs:[KPCR_EXCEPTION_LIST]
484 /* Save volatiles and segment registers */
492 /* Set the R3 data segment */
493 mov ax, KGDT_R3_DATA + RPL_MASK
495 /* Skip debug registers and debug stuff */
498 /* Load the segment registers */
504 /* Check if this interrupt happened in 16-bit mode */
506 jb _Ki16BitStackException
511 /* Check if this was from V86 Mode */
512 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
516 /* Get current thread */
517 mov ecx, fs:[KPCR_CURRENT_THREAD]
521 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
523 /* Check if the thread was being debugged */
524 test byte ptr [ecx+KTHREAD_DEBUG_ACTIVE], 0xFF
527 /* Set the Trap Frame Debug Header */
535 // This macro creates a standard interrupt entry prologue.
536 // It should be used for entry into any interrupt, including software.
539 // Identifying name of the caller function; will be used to append
540 // to the name V86, ABIOS and DR helper functions, which must exist.
542 // @remark For software interrupts, make sure that a fake INT stack is created.
544 .macro INT_PROLOG Label EndLabel FakeErrorCode
547 /* Save fake error code */
551 /* Save the non-volatiles */
557 /* Skip debug registers and other stuff */
564 mov [esp+KTRAP_FRAME_EAX], eax
565 mov [esp+KTRAP_FRAME_ECX], ecx
566 mov [esp+KTRAP_FRAME_EDX], edx
567 mov dword ptr [esp+KTRAP_FRAME_PREVIOUS_MODE], -1
569 /* Check if this was from V86 Mode */
570 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
573 /* Check if this was kernel mode */
575 cmp word ptr [esp+KTRAP_FRAME_CS], KGDT_R0_CODE
579 mov word ptr [esp+KTRAP_FRAME_FS], fs
580 mov word ptr [esp+KTRAP_FRAME_DS], ds
581 mov word ptr [esp+KTRAP_FRAME_ES], es
582 mov [esp+KTRAP_FRAME_GS], gs
584 /* Load the segment registers (use OVERRIDE (0x66)) */
586 mov eax, KGDT_R3_DATA | RPL_MASK
595 /* Get the previous exception list */
596 mov ebx, fs:[KPCR_EXCEPTION_LIST]
598 /* Set the exception handler chain terminator */
599 mov dword ptr fs:[KPCR_EXCEPTION_LIST], -1
601 /* Save the previous exception list */
602 mov [esp+KTRAP_FRAME_EXCEPTION_LIST], ebx
605 /* Setup the 16-bit stack */
606 lea eax, [esp+KTRAP_FRAME_ERROR_CODE]
607 lea ecx, [esp+KTRAP_FRAME_EIP]
612 /* Check if this is the ABIOS stack */
613 /* cmp esp, 0x10000*/
616 /* Delete error code */
617 and dword ptr [esp+KTRAP_FRAME_ERROR_CODE], 0
619 /* Get the current thread and clear direction flag */
620 mov ecx, PCR[KPCR_CURRENT_THREAD]
624 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
626 /* Check if the thread was being debugged */
627 test byte ptr [ecx+KTHREAD_DEBUG_ACTIVE], 0xFF
634 /* Save DR registers if needed */
637 /* Set the trap frame debug header */
643 // @name SYSCALL_PROLOG
645 // This macro creates a system call entry prologue.
646 // It should be used for entry into any fast-system call (KiGetTickCount,
647 // KiCallbackReturn, KiRaiseAssertion) and the generic system call handler
651 // Unique label identifying the name of the caller function; will be
652 // used to append to the name of the DR helper function, which must
657 .macro SYSCALL_PROLOG Label EndLabel
658 /* Create a trap frame */
666 /* Load PCR Selector into fs */
671 /* Get a pointer to the current thread */
672 mov esi, PCR[KPCR_CURRENT_THREAD]
674 /* Save the previous exception list */
675 push PCR[KPCR_EXCEPTION_LIST]
677 /* Set the exception handler chain terminator */
678 mov dword ptr PCR[KPCR_EXCEPTION_LIST], -1
680 /* Save the old previous mode */
681 push [esi+KTHREAD_PREVIOUS_MODE]
683 /* Skip the other registers */
686 /* Set the new previous mode based on the saved CS selector */
689 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], bl
691 /* Go on the Kernel stack frame */
694 /* Save the old trap frame pointer where EDX would be saved */
695 mov ebx, [esi+KTHREAD_TRAP_FRAME]
696 mov [ebp+KTRAP_FRAME_EDX], ebx
699 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
701 /* Check if the thread was being debugged */
702 test byte ptr [esi+KTHREAD_DEBUG_ACTIVE], 0xFF
704 /* Set the thread's trap frame and clear direction flag */
705 mov [esi+KTHREAD_TRAP_FRAME], ebp
708 /* Save DR registers if needed */
711 /* Set the trap frame debug header */
715 /* Enable interrupts */
720 // @name FASTCALL_PROLOG
725 // Unique label identifying the name of the caller function; will be
726 // used to append to the name of the DR helper function, which must
731 .macro FASTCALL_PROLOG Label EndLabel
733 /* Set user selector */
734 mov ecx, KGDT_R3_DATA | RPL_MASK
740 /* Set DS/ES to User Selector */
744 /* Set the current stack to Kernel Stack */
745 mov ecx, PCR[KPCR_TSS]
746 mov esp, [ecx+KTSS_ESP0]
748 /* Set up a fake INT Stack. */
749 push KGDT_R3_DATA + RPL_MASK
750 push edx /* Ring 3 SS:ESP */
751 pushf /* Ring 3 EFLAGS */
752 push 2 /* Ring 0 EFLAGS */
753 add edx, 8 /* Skip user parameter list */
754 popf /* Set our EFLAGS */
755 or dword ptr [esp], EFLAGS_INTERRUPT_MASK /* Re-enable IRQs in EFLAGS, to fake INT */
756 push KGDT_R3_CODE + RPL_MASK
757 push dword ptr ds:KUSER_SHARED_SYSCALL_RET
759 /* Setup the Trap Frame stack */
765 push KGDT_R3_TEB + RPL_MASK
767 /* Save pointer to our PCR */
768 mov ebx, PCR[KPCR_SELF]
770 /* Get a pointer to the current thread */
771 mov esi, [ebx+KPCR_CURRENT_THREAD]
773 /* Set the exception handler chain terminator */
774 push [ebx+KPCR_EXCEPTION_LIST]
775 mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
777 /* Use the thread's stack */
778 mov ebp, [esi+KTHREAD_INITIAL_STACK]
780 /* Push previous mode */
783 /* Skip the other registers */
786 /* Make space for us on the stack */
789 /* Write the previous mode */
790 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], UserMode
797 and dword ptr [ebp+KTRAP_FRAME_DR7], 0
799 /* Check if the thread was being debugged */
800 test byte ptr [esi+KTHREAD_DEBUG_ACTIVE], 0xFF
802 /* Set the thread's trap frame */
803 mov [esi+KTHREAD_TRAP_FRAME], ebp
805 /* Save DR registers if needed */
808 /* Set the trap frame debug header */
812 /* Enable interrupts */
817 // @name V86_TRAP_PROLOG
819 // This macro creates a V86 Trap entry prologue.
820 // It should be used for entry into any fast-system call (KiGetTickCount,
821 // KiCallbackReturn, KiRaiseAssertion) and the generic system call handler
825 // Unique label identifying the name of the caller function; will be
826 // used to append to the name of the DR helper function, which must
831 .macro V86_TRAP_PROLOG Label EndLabel
833 /* Skip everything to the error code */
834 sub esp, KTRAP_FRAME_ERROR_CODE
836 /* Clear the error code */
837 mov word ptr [esp+KTRAP_FRAME_ERROR_CODE+2], 0
839 /* Save the registers we'll trample */
840 mov [esp+KTRAP_FRAME_EBX], ebx
841 mov [esp+KTRAP_FRAME_EAX], eax
842 mov [esp+KTRAP_FRAME_EBP], ebp
843 mov [esp+KTRAP_FRAME_ESI], esi
844 mov [esp+KTRAP_FRAME_EDI], edi
846 /* Save PCR and Ring 3 segments */
848 mov eax, KGDT_R3_DATA + RPL_MASK
850 /* Save ECX and EDX too */
851 mov [esp+KTRAP_FRAME_ECX], ecx
852 mov [esp+KTRAP_FRAME_EDX], edx
854 /* Set debugging markers */
855 mov dword ptr [esp+KTRAP_FRAME_PREVIOUS_MODE], -1
856 mov dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
858 /* Now set segments (use OVERRIDE, 0x66) */
866 /* Set the trap frame in the stack and clear the direction flag */
870 /* Save the exception list */
871 mov eax, fs:[KPCR_EXCEPTION_LIST]
872 mov [esp+KTRAP_FRAME_EXCEPTION_LIST], eax
874 /* Check if we need debugging */
876 test eax, ~DR7_RESERVED_MASK
877 mov [esp+KTRAP_FRAME_DR7], eax
884 // @name V86_TRAP_EPILOG
886 // This macro creates an epilogue for leaving V86 traps
892 .macro V86_TRAP_EPILOG
894 /* Get the current thread and make it unalerted */
896 mov ebx, PCR[KPCR_CURRENT_THREAD]
897 mov byte ptr [ebx+KTHREAD_ALERTED], 0
899 /* Check if it has User-mode APCs pending */
900 cmp byte ptr [ebx+KTHREAD_PENDING_USER_APC], 0
903 /* It doesn't, pop the frame */
904 add esp, KTRAP_FRAME_EDX
909 /* Check if DR registers should be restored */
910 test dword ptr [ebp+KTRAP_FRAME_DR7], ~DR7_RESERVED_MASK
913 /* Finish popping the rest of the frame, and return to P-mode */
927 mov esi, [ebp+KTRAP_FRAME_DR0]
928 mov edi, [ebp+KTRAP_FRAME_DR1]
933 /* Get DR2 and load DR0-2 */
934 mov ebx, [ebp+KTRAP_FRAME_DR2]
940 mov esi, [ebp+KTRAP_FRAME_DR0]
941 mov edi, [ebp+KTRAP_FRAME_DR1]
942 mov ebx, [ebp+KTRAP_FRAME_DR7]
952 /* Raise to APC level */
956 /* Save KIRQL and deliver APCs */
962 call _KiDeliverApc@12
969 /* Check if we're not in V86 anymore */
970 test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
977 // This macro creates an epilogue for leaving any system trap.
978 // It is used for exiting system calls, exceptions, interrupts and generic
982 // Specifies whether this trap will exit a system call. If so, special
983 // code will be assembled to potentially use SYSEXIT instead of IRETD.
985 // @param RestorePreviousMode
986 // Specifies if the previous mode should be restored.
988 // @param RestoreSegments
989 // Specifies if the segment registers should be restored.
991 // @param RestoreVolatiles
992 // Specifies if the volatile registers should be restored.
994 // @param RestoreAllRegs
995 // Specifies if volatiles and segments should both be restored.
999 .macro TRAP_EPILOG SystemCall, RestorePreviousMode, RestoreSegments, RestoreVolatiles, RestoreAllRegs
1001 /* Assert the flags */
1004 test edx, EFLAGS_INTERRUPT_MASK
1007 /* Assert the stack */
1011 /* Assert the trap frame */
1015 sub dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
1023 /* Assert exception list */
1024 cmp dword ptr PCR[KPCR_EXCEPTION_LIST], 0
1033 /* Get exception list */
1034 mov edx, [esp+KTRAP_FRAME_EXCEPTION_LIST]
1037 /* Assert the saved exception list */
1045 mov PCR[KPCR_EXCEPTION_LIST], edx
1047 .if \RestorePreviousMode
1048 /* Get previous mode */
1049 mov ecx, [esp+KTRAP_FRAME_PREVIOUS_MODE]
1052 /* Assert the saved previous mode */
1059 /* Restore the previous mode */
1060 mov esi, PCR[KPCR_CURRENT_THREAD]
1061 mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], cl
1065 /* Assert the saved previous mode */
1066 mov ecx, [esp+KTRAP_FRAME_PREVIOUS_MODE]
1074 /* Check for debug registers */
1075 test dword ptr [esp+KTRAP_FRAME_DR7], ~DR7_RESERVED_MASK
1080 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1083 /* Check if the frame was edited */
1084 test word ptr [esp+KTRAP_FRAME_CS], FRAME_EDITED
1088 /* Check the old mode */
1089 cmp word ptr [esp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
1090 bt word ptr [esp+KTRAP_FRAME_CS], 0
1095 .if \RestoreVolatiles
1096 /* Restore volatiles */
1097 mov edx, [esp+KTRAP_FRAME_EDX]
1098 mov ecx, [esp+KTRAP_FRAME_ECX]
1099 mov eax, [esp+KTRAP_FRAME_EAX]
1102 /* Check if we were called from kernel mode */
1103 cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R0_CODE
1106 .if \RestoreSegments
1107 /* Restore segment registers */
1108 lea esp, [ebp+KTRAP_FRAME_GS]
1116 lea esp, [ebp+KTRAP_FRAME_FS]
1120 /* Skip debug information and unsaved registers */
1121 lea esp, [ebp+KTRAP_FRAME_EDI]
1127 /* Check for ABIOS */
1128 cmp word ptr [esp+8], 0x80
1131 /* Pop error code */
1135 /* Check if previous CS is from user-mode */
1136 test dword ptr [esp+4], 1
1138 /* It is, so use Fast Exit */
1141 /* Jump back to stub */
1153 /* Is SYSEXIT Supported/Wanted? */
1154 cmp dword ptr ss:[_KiFastSystemCallDisable], 0
1156 test dword ptr [esp+8], EFLAGS_TF
1159 /* Restore FS to TIB */
1160 mov ecx, KGDT_R3_TEB + RPL_MASK
1163 /* We will be cleaning up the stack ourselves */
1164 pop edx /* New Ring 3 EIP */
1165 add esp, 4 /* Skip Ring 3 DS */
1166 and dword ptr [esp], 0xfffffdff /* Remove EFLAGS_INTERRUPT_MASK from EFLAGS */
1167 popf /* Restore old EFLAGS */
1168 pop ecx /* Old Ring 3 SS:ESP */
1172 * ECX points to the old User Stack.
1173 * EDX points to the instruction to execute in usermode after the sysenter
1182 mov eax, [esp+KTRAP_FRAME_EAX]
1184 /* Skip registers */
1187 /* Restore segments and volatiles */
1194 /* Jump back to mainline code */
1200 /* Fix up the mask */
1201 add dword ptr [esp+KTRAP_FRAME_DEBUGARGMARK], 0xBADB0D00
1209 /* Check if this was V86 mode */
1210 test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
1213 /* Check if it was user mode */
1214 test word ptr [esp+KTRAP_FRAME_CS], MODE_MASK
1223 mov esi, [ebp+KTRAP_FRAME_DR0]
1224 mov edi, [ebp+KTRAP_FRAME_DR1]
1225 mov ebx, [ebp+KTRAP_FRAME_DR2]
1233 mov esi, [ebp+KTRAP_FRAME_DR3]
1234 mov edi, [ebp+KTRAP_FRAME_DR6]
1235 mov ebx, [ebp+KTRAP_FRAME_DR7]
1244 /* Restore real CS value */
1245 mov ebx, [esp+KTRAP_FRAME_TEMPCS]
1246 mov [esp+KTRAP_FRAME_CS], ebx
1249 * If ESP was modified, then a special interrupt exit stack
1250 * must be created to "update" ESP's value in a legal manner
1252 mov ebx, [esp+KTRAP_FRAME_TEMPESP]
1254 mov [esp+KTRAP_FRAME_ERROR_CODE], ebx
1256 /* Copy Interrupt Stack */
1257 mov esi, [esp+KTRAP_FRAME_EFLAGS]
1259 mov esi, [esp+KTRAP_FRAME_CS]
1261 mov esi, [esp+KTRAP_FRAME_EIP]
1264 .if \RestoreVolatiles
1265 /* Restore volatiles */
1266 mov eax, [esp+KTRAP_FRAME_EAX]
1267 mov edx, [esp+KTRAP_FRAME_EDX]
1268 mov ecx, [esp+KTRAP_FRAME_ECX]
1272 add esp, KTRAP_FRAME_EDI
1284 // This macro creates an epilogue for leaving any system trap.
1285 // It is used for exiting system calls, exceptions, interrupts and generic
1288 // @param Spurious - TRUE if the interrupt was unexpected and spurious.
1292 .macro INT_EPILOG Spurious
1295 /* Just exit the trap */
1296 jmp _Kei386EoiHelper@0
1298 /* Disable interrupts */
1301 /* End the interrupt and do EOI */
1302 call _HalEndSystemInterrupt@8
1303 jmp _Kei386EoiHelper@0
1309 .macro VERIFY_INT Label
1310 /* Get the current time and mask it to 192 ticks */
1311 mov eax, _KeTickCount
1314 /* Check if we're in the same tick area */
1315 cmp eax, dword ptr [edi+KINTERRUPT_TICK_COUNT]
1319 /* If we got here, then our count is too large */
1320 dec word ptr [edi+KINTERRUPT_DISPATCH_COUNT]
1325 .macro VERIFY_INT_END Label, Info
1328 /* Decrement the dispatch count and check if we should bug check */
1329 dec word ptr [edi+KINTERRUPT_DISPATCH_COUNT+2]
1332 /* Update the tick count */
1334 mov [edi+KINTERRUPT_TICK_COUNT], eax
1338 /* Check if the debugger is enabled */
1339 cmp byte ptr __KdDebuggerEnabled, 0
1342 /* It isn't, bugcheck */
1345 push [edi+KINTERRUPT_SERVICE_CONTEXT]
1346 push [edi+KINTERRUPT_SERVICE_ROUTINE]
1347 push HARDWARE_INTERRUPT_STORM
1348 call _KeBugCheckEx@20
1351 /* Debugger enabled, do a debug print + break instead */
1352 push [edi+KINTERRUPT_SERVICE_ROUTINE]
1353 push offset _IsrOverflowMsg
1358 /* Breakpoint handled, get the new tick count */
1359 mov eax, _KeTickCount
1363 /* Reset tick count */
1364 mov dword ptr [edi+KINTERRUPT_TICK_COUNT], eax
1365 mov word ptr [edi+KINTERRUPT_DISPATCH_COUNT+2], 64
1368 /* Put default overflow count and continue */
1369 mov ax, _KiISROverflow
1370 mov word ptr [edi+KINTERRUPT_DISPATCH_COUNT], ax
1374 /* Check if we wrapped */
1376 cmp eax, [edi+KINTERRUPT_TICK_COUNT]
1379 /* We did, start over */
1380 mov eax, _KeTickCount
1386 /* We don't verify interrupts on retail builds */
1387 .macro VERIFY_INT Label
1389 .macro VERIFY_INT_END Label, Info