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 .extern _DispatcherDatabaseLock
26 /* FUNCTIONS ****************************************************************/
31 * The KiThreadStartup routine is the beginning of any thread.
34 * SystemRoutine - Pointer to the System Startup Routine. Either
35 * PspUserThreadStartup or PspSystemThreadStartup
37 * StartRoutine - For Kernel Threads only, specifies the starting execution
38 * point of the new thread.
40 * StartContext - For Kernel Threads only, specifies a pointer to variable
41 * context data to be sent to the StartRoutine above.
43 * UserThread - Indicates whether or not this is a user thread. This tells
44 * us if the thread has a context or not.
46 * TrapFrame - Pointer to the KTHREAD to which the caller wishes to
50 * Should never return for a system thread. Returns through the System Call
51 * Exit Dispatcher for a user thread.
54 * If a return from a system thread is detected, a bug check will occur.
57 .globl _KiThreadStartup@156
61 * Clear all the non-volatile registers, so the thread won't be tempted to
62 * expect any static data (like some badly coded usermode/win9x apps do)
69 /* It's now safe to go to APC */
74 * Call the System Routine which is right on our stack now.
75 * After we pop the pointer, the Start Routine/Context will be on the
76 * stack, as parameters to the System Routine
81 /* The thread returned... was it a user-thread? */
86 /* Yes it was, set our trapframe for the System Call Exit Dispatcher */
89 /* Exit back to user-mode */
94 /* A system thread returned...this is very bad! */
98 * KiSwapContextInternal
100 * The KiSwapContextInternal routine switches context to another thread.
103 * ESI - Pointer to the KTHREAD to which the caller wishes to
105 * EDI - Pointer to the KTHREAD to which the caller wishes to
112 * Absolutely all registers except ESP can be trampled here for maximum code flexibility.
115 .globl @KiSwapContextInternal@0
116 @KiSwapContextInternal@0:
118 //jmp SaveTrapFrameForKDB
119 SaveTrapFrameForKDB_Return:
122 /* Get the PCR. It's faster to use ebx+offset then fs:offset */
123 mov ebx, [fs:KPCR_SELF]
125 /* Set the Thread to running */
126 mov byte ptr [esi+KTHREAD_STATE], Running
128 /* Save the Exception list */
129 push [ebx+KPCR_EXCEPTION_LIST]
131 /* Switching, disable interrupts now */
134 /* Save the initial stack in EAX */
135 mov eax, [edi+KTHREAD_INITIAL_STACK]
138 /* Save FPU state if the thread has used it. */
139 mov dword ptr [ebx+KPCR_NPX_THREAD], 0
140 test byte ptr [edi+KTHREAD_NPX_STATE], NPX_STATE_DIRTY
142 cmp dword ptr _FxsrSupport, 0
144 fxsave [eax-SIZEOF_FX_SAVE_AREA]
147 fnsave [eax-SIZEOF_FX_SAVE_AREA]
149 mov byte ptr [edi+KTHREAD_NPX_STATE], NPX_STATE_VALID
151 #endif /* CONFIG_SMP */
153 /* Save the stack pointer in this processors TSS */
154 mov ebp, [ebx+KPCR_TSS]
156 /* Check if this isn't V86 Mode, so we can bias the Esp0 */
157 test dword ptr [eax - KTRAP_FRAME_SIZE + KTRAP_FRAME_EFLAGS], X86_EFLAGS_VM
161 //sub dword ptr ss:[ebp+KTSS_ESP0], KTRAP_FRAME_V86_GS - KTRAP_FRAME_SS
164 /* Push ESP0 Value */
165 push ss:[ebp+KTSS_ESP0]
167 /* Check if address space switch is needed */
168 mov eax, [esi+KTHREAD_APCSTATE_PROCESS]
169 cmp eax, [edi+KTHREAD_APCSTATE_PROCESS]
170 mov eax, [eax+KPROCESS_DIRECTORY_TABLE_BASE]
173 mov [edi+KTHREAD_KERNEL_STACK], esp
174 mov esp, [esi+KTHREAD_KERNEL_STACK]
176 jz NoAddressSpaceSwitch
178 /* Switch address space */
181 NoAddressSpaceSwitch:
183 /* Stack is OK, safe to enable interrupts now */
186 /* Check if address space switch is needed (the result from above is valid) */
187 /* If they match, then use the fast-path and skip all this */
190 /* Get the new Process. */
191 mov edi, [esi+KTHREAD_APCSTATE_PROCESS]
193 /* Check if we need an LDT */
195 cmp [edi+KPROCESS_LDT_DESCRIPTOR0], eax
198 /* Write the LDT Selector */
199 mov ebp, [ebx+KPCR_GDT]
200 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR0]
201 mov [ebp+KGDT_LDT], eax
202 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR1]
203 mov [ebp+KGDT_LDT+4], eax
205 /* Save LDT Selector */
214 mov ecx, [edi+KPROCESS_IOPM_OFFSET]
216 /* Set current IOPM offset in the TSS */
217 mov [ebp+KTSS_IOMAPBASE], cx
222 mov eax, [esi+KTHREAD_TEB]
223 mov ecx, [ebx+KPCR_GDT]
229 /* Increase context switches */
230 inc dword ptr [esi+KTHREAD_CONTEXT_SWITCHES]
232 /* Set TS in cr0 to catch FPU code and load the FPU state when needed */
234 cmp [ebx+KPCR_NPX_THREAD], esi
236 #endif /* !CONFIG_SMP */
242 /* Restore the stack pointer in this processors TSS */
243 pop ss:[ebp+KTSS_ESP0]
245 /* Restore exception list */
246 pop [ebx+KPCR_EXCEPTION_LIST]
250 mov ecx, _DispatcherDatabaseLock
251 call @KefReleaseSpinLockFromDpcLevel@4
258 * The KiSwapContext routine switches context to another thread.
261 * TargetThread - Pointer to the KTHREAD to which the caller wishes to
265 * The WaitStatus of the Target Thread. NOT YET SUPPORTED.
268 * This is a wrapper around KiSwapContextInternal which will save all the
269 * non-volatile registers so that the Internal function can use all of
270 * them. It will also save the old current thread and set the new one.
272 * The calling thread does not return after KiSwapContextInternal until
273 * another thread switches to IT.
276 .globl @KiSwapContext@4
278 /* Note, we CANNOT touch ebp */
280 /* Save 4 registers */
283 /* Save all the non-volatile ones */
289 /* Get the Current Thread */
290 mov edi, fs:[KPCR_CURRENT_THREAD]
292 /* Get the New Thread */
295 /* Save it as Current thread */
296 mov fs:[KPCR_CURRENT_THREAD], esi
298 /* Do the swap with the registers correctly setup */
299 call @KiSwapContextInternal@0
301 /* Return the registers */
314 /* Set up a trap frame */
317 push 0 /* Error Code */ // 0x64
321 /* Fake Interrupt Stack */
322 mov ebp, [esp+20] /* Eip */
323 mov ebx, [esp+16] /* Eflags */
325 mov ebx, [esp+12] /* Cs */
332 push -1 /* Exception List */ // 0x4C
333 push 0 /* Previous Mode */ // 0x48
343 /* Clear breakpoint enables in dr7. */
358 push eax /* TempEsp */
359 push ss /* TempSegSs */
360 push 0 /* DebugPointer */
361 push -1 /* DebugArgMark */
362 push [esp+60] /* Debug EIP */ // 0x4
363 push ebp /* Debug EBP */ // 0x0
368 /* Push old Trap Frame */
369 push [edi+KTHREAD_TRAP_FRAME]
372 mov [edi+KTHREAD_TRAP_FRAME], ebp
374 /* Restore EBP, EBX and EAX */
375 mov ebp, [ebp+KTRAP_FRAME_EBP]
376 mov ebx, [ebp+KTRAP_FRAME_EBX]
377 mov eax, [ebp+KTRAP_FRAME_EAX]
380 push offset RestoreTrapFrameForKDB
382 /* Jump to normal code */
383 jmp SaveTrapFrameForKDB_Return
385 RestoreTrapFrameForKDB:
387 /* Restore the old trapframe */
388 pop [esi+KTHREAD_TRAP_FRAME]
390 /* Pop unused portions of the trap frame */
393 /* Restore registers from Trap frame */
400 add esp, 8 /* ExceptionList and PreviousMode */
406 add esp, 4 /* ErrorCode */
408 /* Return to the caller. */