2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/amd64/ctxswitch.S
5 * PURPOSE: Thread Context Switching
7 * PROGRAMMER: Timo kreuzer (timo.kreuzer@reactos.org)
10 /* INCLUDES ******************************************************************/
14 #include <ksamd64.inc>
16 /* FUNCTIONS ****************************************************************/
23 * The KiThreadStartup routine is the beginning of any thread.
26 * SystemRoutine - Pointer to the System Startup Routine. Either
27 * PspUserThreadStartup or PspSystemThreadStartup
29 * StartRoutine - For Kernel Threads only, specifies the starting execution
30 * point of the new thread.
32 * StartContext - For Kernel Threads only, specifies a pointer to variable
33 * context data to be sent to the StartRoutine above.
35 * UserThread - Indicates whether or not this is a user thread. This tells
36 * us if the thread has a context or not.
38 * TrapFrame - Pointer to the KTHREAD to which the caller wishes to
42 * Should never return for a system thread. Returns through the System Call
43 * Exit Dispatcher for a user thread.
46 * If a return from a system thread is detected, a bug check will occur.
49 PUBLIC KiThreadStartup
53 * Clear all the non-volatile registers, so the thread won't be tempted to
54 * 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 is on the
74 * stack, we pop it as parameters to the System Routine into rcx
80 /* The thread returned... was it a user-thread? */
85 /* Yes it was, set our trapframe for the System Call Exit Dispatcher */
88 /* Exit back to user-mode */
89 // jmp _KiServiceExit2
90 UNIMPLEMENTED KiThreadStartup_KiServiceExit2
94 /* A system thread returned...this is very bad! */
99 * KiSwapContextInternal
101 * The KiSwapContextInternal routine switches context to another thread.
104 * ESI - Pointer to the KTHREAD to which the caller wishes to
106 * EDI - Pointer to the KTHREAD to which the caller wishes to
113 * Absolutely all registers except ESP can be trampled here for maximum code flexibility.
116 PUBLIC KiSwapContextInternal
117 KiSwapContextInternal:
118 UNIMPLEMENTED KiSwapContextInternal
126 * The KiSwapContext routine switches context to another thread.
129 * KiSwapContext(PKTHREAD CurrentThread, PKTHREAD TargetThread);
131 * \param CurrentThread
132 * Pointer to the KTHREAD of the current thread.
134 * \param TargetThread
135 * Pointer to the KTHREAD to which the caller wishes to switch to.
138 * The WaitStatus of the Target Thread.
141 * This is a wrapper around KiSwapContextInternal which will save all the
142 * non-volatile registers so that the Internal function can use all of
143 * them. It will also save the old current thread and set the new one.
145 * The calling thread does not return after KiSwapContextInternal until
146 * another thread switches to IT.
152 /* Save 10 registers */
155 /* Save all the non-volatile ones */
169 mov rbx, gs:[KPCR_SELF]
171 /* Get the current thread */
174 /* Get the New Thread */
177 /* Get the wait IRQL */
178 movzx ecx, byte ptr [edi+KTHREAD_WAIT_IRQL]
180 /* Do the swap with the registers correctly setup */
181 call KiSwapContextInternal
183 /* Restore the registers */