2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/i386/ctxswitch.S
5 * PURPOSE: Thread Context Switching
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 * Gregor Anich (FPU Code)
11 /* INCLUDES ******************************************************************/
14 #include <internal/i386/ke.h>
16 .intel_syntax noprefix
19 #define SIZEOF_TRAP_FRAME 0x8c
22 /* GLOBALS ****************************************************************/
24 /* FUNCTIONS ****************************************************************/
29 * The KiThreadStartup routine is the beginning of any thread.
32 * SystemRoutine - Pointer to the System Startup Routine. Either
33 * PspUserThreadStartup or PspSystemThreadStartup
35 * StartRoutine - For Kernel Threads only, specifies the starting execution
36 * point of the new thread.
38 * StartContext - For Kernel Threads only, specifies a pointer to variable
39 * context data to be sent to the StartRoutine above.
41 * UserThread - Indicates whether or not this is a user thread. This tells
42 * us if the thread has a context or not.
44 * TrapFrame - Pointer to the KTHREAD to which the caller wishes to
48 * Should never return for a system thread. Returns through the System Call
49 * Exit Dispatcher for a user thread.
52 * If a return from a system thread is detected, a bug check will occur.
55 .globl _KiThreadStartup@156
59 * Clear all the non-volatile registers, so the thread won't be tempted to
60 * expect any static data (like some badly coded usermode/win9x apps do)
67 /* It's now safe to go to APC */
72 * Call the System Routine which is right on our stack now.
73 * After we pop the pointer, the Start Routine/Context will be on the
74 * stack, as parameters to the System Routine
79 /* The thread returned... was it a user-thread? */
84 /* Yes it was, set our trapframe for the System Call Exit Dispatcher */
87 /* Exit back to user-mode */
92 /* A system thread returned...this is very bad! */
96 * KiSwapContextInternal
98 * The KiSwapContextInternal routine switches context to another thread.
101 * ESI - Pointer to the KTHREAD to which the caller wishes to
103 * EDI - Pointer to the KTHREAD to which the caller wishes to
110 * Absolutely all registers except ESP can be trampled here for maximum code flexibility.
113 .globl @KiSwapContextInternal@0
114 @KiSwapContextInternal@0:
116 //jmp SaveTrapFrameForKDB
117 SaveTrapFrameForKDB_Return:
120 /* Get the PCR. It's faster to use ebx+offset then fs:offset */
121 mov ebx, [fs:KPCR_SELF]
123 /* Set the Thread to running */
124 mov byte ptr [esi+KTHREAD_STATE], Running
126 /* Save the Exception list */
127 push [ebx+KPCR_EXCEPTION_LIST]
129 /* Switching, disable interrupts now */
132 /* Save the initial stack in EAX */
133 mov eax, [edi+KTHREAD_INITIAL_STACK]
136 /* Save FPU state if the thread has used it. */
137 mov dword ptr [ebx+KPCR_NPX_THREAD], 0
138 test byte ptr [edi+KTHREAD_NPX_STATE], NPX_STATE_DIRTY
140 cmp dword ptr _FxsrSupport, 0
142 fxsave [eax-SIZEOF_FX_SAVE_AREA]
145 fnsave [eax-SIZEOF_FX_SAVE_AREA]
147 mov byte ptr [edi+KTHREAD_NPX_STATE], NPX_STATE_VALID
149 #endif /* CONFIG_SMP */
151 /* Save the stack pointer in this processors TSS */
152 mov ebp, [ebx+KPCR_TSS]
154 /* Check if this isn't V86 Mode, so we can bias the Esp0 */
155 test dword ptr [eax - KTRAP_FRAME_SIZE + KTRAP_FRAME_EFLAGS], X86_EFLAGS_VM
159 //sub dword ptr ss:[ebp+KTSS_ESP0], KTRAP_FRAME_V86_GS - KTRAP_FRAME_SS
162 /* Push ESP0 Value */
163 push ss:[ebp+KTSS_ESP0]
165 /* Check if address space switch is needed */
166 mov eax, [esi+KTHREAD_APCSTATE_PROCESS]
167 cmp eax, [edi+KTHREAD_APCSTATE_PROCESS]
168 mov eax, [eax+KPROCESS_DIRECTORY_TABLE_BASE]
171 mov [edi+KTHREAD_KERNEL_STACK], esp
172 mov esp, [esi+KTHREAD_KERNEL_STACK]
174 jz NoAddressSpaceSwitch
176 /* Switch address space */
179 NoAddressSpaceSwitch:
181 /* Stack is OK, safe to enable interrupts now */
184 /* Check if address space switch is needed (the result from above is valid) */
185 /* If they match, then use the fast-path and skip all this */
188 /* Get the new Process. */
189 mov edi, [esi+KTHREAD_APCSTATE_PROCESS]
191 /* Check if we need an LDT */
193 cmp [edi+KPROCESS_LDT_DESCRIPTOR0], eax
196 /* Write the LDT Selector */
197 mov ebp, [ebx+KPCR_GDT]
198 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR0]
199 mov [ebp+LDT_SELECTOR], eax
200 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR1]
201 mov [ebp+LDT_SELECTOR+4], eax
203 /* Save LDT Selector */
204 mov eax, LDT_SELECTOR
212 mov ecx, [edi+KPROCESS_IOPM_OFFSET]
214 /* Set current IOPM offset in the TSS */
215 mov [ebp+KTSS_IOMAPBASE], cx
220 mov eax, [esi+KTHREAD_TEB]
221 mov ecx, [ebx+KPCR_GDT]
227 /* Increase context switches */
228 inc dword ptr [esi+KTHREAD_CONTEXT_SWITCHES]
230 /* Set TS in cr0 to catch FPU code and load the FPU state when needed */
232 cmp [ebx+KPCR_NPX_THREAD], esi
234 #endif /* !CONFIG_SMP */
240 /* Restore the stack pointer in this processors TSS */
241 pop ss:[ebp+KTSS_ESP0]
243 /* Restore exception list */
244 pop [ebx+KPCR_EXCEPTION_LIST]
247 call @KeReleaseDispatcherDatabaseLockFromDpcLevel@0
253 * The KiSwapContext routine switches context to another thread.
256 * TargetThread - Pointer to the KTHREAD to which the caller wishes to
260 * The WaitStatus of the Target Thread. NOT YET SUPPORTED.
263 * This is a wrapper around KiSwapContextInternal which will save all the
264 * non-volatile registers so that the Internal function can use all of
265 * them. It will also save the old current thread and set the new one.
267 * The calling thread does not return after KiSwapContextInternal until
268 * another thread switches to IT.
271 .globl @KiSwapContext@4
273 /* Note, we CANNOT touch ebp */
275 /* Save 4 registers */
278 /* Save all the non-volatile ones */
284 /* Get the Current Thread */
285 mov edi, fs:[KPCR_CURRENT_THREAD]
287 /* Get the New Thread */
290 /* Save it as Current thread */
291 mov fs:[KPCR_CURRENT_THREAD], esi
293 /* Do the swap with the registers correctly setup */
294 call @KiSwapContextInternal@0
296 /* Return the registers */
309 /* Set up a trap frame */
312 push 0 /* Error Code */ // 0x64
316 /* Fake Interrupt Stack */
317 mov ebp, [esp+20] /* Eip */
318 mov ebx, [esp+16] /* Eflags */
320 mov ebx, [esp+12] /* Cs */
327 push -1 /* Exception List */ // 0x4C
328 push 0 /* Previous Mode */ // 0x48
338 /* Clear breakpoint enables in dr7. */
353 push eax /* TempEsp */
354 push ss /* TempSegSs */
355 push 0 /* DebugPointer */
356 push -1 /* DebugArgMark */
357 push [esp+60] /* Debug EIP */ // 0x4
358 push ebp /* Debug EBP */ // 0x0
363 /* Push old Trap Frame */
364 push [edi+KTHREAD_TRAP_FRAME]
367 mov [edi+KTHREAD_TRAP_FRAME], ebp
369 /* Restore EBP, EBX and EAX */
370 mov ebp, [ebp+KTRAP_FRAME_EBP]
371 mov ebx, [ebp+KTRAP_FRAME_EBX]
372 mov eax, [ebp+KTRAP_FRAME_EAX]
375 push offset RestoreTrapFrameForKDB
377 /* Jump to normal code */
378 jmp SaveTrapFrameForKDB_Return
380 RestoreTrapFrameForKDB:
382 /* Restore the old trapframe */
383 pop [esi+KTHREAD_TRAP_FRAME]
385 /* Pop unused portions of the trap frame */
388 /* Restore registers from Trap frame */
395 add esp, 8 /* ExceptionList and PreviousMode */
401 add esp, 4 /* ErrorCode */
403 /* Return to the caller. */