Fixed restoring of registers.
[reactos.git] / reactos / ntoskrnl / ke / i386 / ctxswitch.S
1 /*
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
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <roscfg.h>
13 #include <internal/i386/segment.h>
14 #include <internal/i386/ke.h>
15 #include <internal/i386/fpu.h>
16 #include <internal/ps.h>
17 #include <ntos/tss.h>
18 #include <internal/ntoskrnl.h>
19 .intel_syntax noprefix
20
21 #define Running 2
22 #define SIZEOF_TRAP_FRAME 0x8c
23 #define APC_LEVEL 1
24
25 /* GLOBALS ****************************************************************/
26
27 /* FUNCTIONS ****************************************************************/
28
29 /*++
30 * KiThreadStartup
31 *
32 * The KiThreadStartup routine is the beginning of any thread.
33 *
34 * Params:
35 * SystemRoutine - Pointer to the System Startup Routine. Either
36 * PspUserThreadStartup or PspSystemThreadStartup
37 *
38 * StartRoutine - For Kernel Threads only, specifies the starting execution
39 * point of the new thread.
40 *
41 * StartContext - For Kernel Threads only, specifies a pointer to variable
42 * context data to be sent to the StartRoutine above.
43 *
44 * UserThread - Indicates whether or not this is a user thread. This tells
45 * us if the thread has a context or not.
46 *
47 * TrapFrame - Pointer to the KTHREAD to which the caller wishes to
48 * switch from.
49 *
50 * Returns:
51 * Should never return for a system thread. Returns through the System Call
52 * Exit Dispatcher for a user thread.
53 *
54 * Remarks:
55 * If a return from a system thread is detected, a bug check will occur.
56 *
57 *--*/
58 .globl _KiThreadStartup@156
59 _KiThreadStartup@156:
60
61 /*
62 * Clear all the non-volatile registers, so the thread won't be tempted to
63 * expect any static data (like some badly coded usermode/win9x apps do)
64 */
65 xor ebx, ebx
66 xor esi, edi
67 xor edi, edi
68 xor ebp, ebp
69
70 /* It's now safe to go to APC */
71 mov ecx, APC_LEVEL
72 call @KfLowerIrql@4
73
74 /*
75 * Call the System Routine which is right on our stack now.
76 * After we pop the pointer, the Start Routine/Context will be on the
77 * stack, as parameters to the System Routine
78 */
79 pop eax
80 call eax
81
82 /* The thread returned... was it a user-thread? */
83 pop ecx
84 or ecx, ecx
85 jz BadThread
86
87 /* Yes it was, set our trapframe for the System Call Exit Dispatcher */
88 mov ebp, esp
89
90 /* Exit back to user-mode */
91 jmp _KiServiceExit2
92
93 BadThread:
94
95 /* A system thread returned...this is very bad! */
96 int 3
97
98 /*++
99 * KiSwapContextInternal
100 *
101 * The KiSwapContextInternal routine switches context to another thread.
102 *
103 * Params:
104 * ESI - Pointer to the KTHREAD to which the caller wishes to
105 * switch to.
106 * EDI - Pointer to the KTHREAD to which the caller wishes to
107 * switch from.
108 *
109 * Returns:
110 * None.
111 *
112 * Remarks:
113 * Absolutely all registers except ESP can be trampled here for maximum code flexibility.
114 *
115 *--*/
116 .globl @KiSwapContextInternal@0
117 @KiSwapContextInternal@0:
118 #ifdef KDBG
119 //jmp SaveTrapFrameForKDB
120 SaveTrapFrameForKDB_Return:
121 #endif
122
123 /* Get the PCR. It's faster to use ebx+offset then fs:offset */
124 mov ebx, [fs:KPCR_SELF]
125
126 /* Set the Thread to running */
127 mov byte ptr [esi+KTHREAD_STATE], Running
128
129 /* Save the Exception list */
130 push [ebx+KPCR_EXCEPTION_LIST]
131
132 /* Switching, disable interrupts now */
133 cli
134
135 #ifdef CONFIG_SMP
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
139 jz 3f
140 mov eax, [edi+KTHREAD_INITIAL_STACK]
141 cmp dword ptr _FxsrSupport, 0
142 je 1f
143 fxsave [eax-SIZEOF_FX_SAVE_AREA]
144 jmp 2f
145 1:
146 fnsave [eax-SIZEOF_FX_SAVE_AREA]
147 2:
148 mov byte ptr [edi+KTHREAD_NPX_STATE], NPX_STATE_VALID
149 3:
150 #endif /* CONFIG_SMP */
151
152 /* Save the stack pointer in this processors TSS */
153 mov ebp, [ebx+KPCR_TSS]
154 push ss:[ebp+KTSS_ESP0]
155
156 /* Switch stacks */
157 mov [edi+KTHREAD_KERNEL_STACK], esp
158 mov esp, [esi+KTHREAD_KERNEL_STACK]
159
160 /* Stack is OK, safe to enable interrupts now */
161 sti
162
163 /* Check if address space switch is needed */
164 mov eax, [edi+KTHREAD_APCSTATE_PROCESS]
165 cmp eax, [esi+KTHREAD_APCSTATE_PROCESS]
166
167 /* If they match, then use the fast-path and skip all this */
168 jz SameProcess
169
170 /* Get the new Process. */
171 mov edi, [esi+KTHREAD_APCSTATE_PROCESS]
172
173 /* Check if we need an LDT */
174 xor eax, eax
175 cmp [edi+KPROCESS_LDT_DESCRIPTOR0], eax
176 jz NoLdt
177
178 /* Write the LDT Selector */
179 mov ebp, [ebx+KPCR_GDT]
180 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR0]
181 mov [ebp+LDT_SELECTOR], eax
182 mov eax, [edi+KPROCESS_LDT_DESCRIPTOR1]
183 mov [ebp+LDT_SELECTOR+4], eax
184
185 /* Save LDT Selector */
186 mov eax, LDT_SELECTOR
187
188 NoLdt:
189
190 /* Load LDT */
191 lldt ax
192
193 /* Get the IOPM */
194 mov ecx, [edi+KPROCESS_IOPM_OFFSET]
195
196 /* Set current IOPM offset in the TSS */
197 mov [ebp+KTSS_IOMAPBASE], cx
198
199 /* Change the address space */
200 mov eax, [edi+KPROCESS_DIRECTORY_TABLE_BASE]
201 mov cr3, eax
202
203 SameProcess:
204
205 /* Set the TEB */
206 mov eax, [esi+KTHREAD_TEB]
207 mov ecx, [ebx+KPCR_GDT]
208 mov [ecx+0x3A], ax
209 shr eax, 16
210 mov [ecx+0x3C], al
211 mov [ecx+0x3F], ah
212
213 /* Increase context switches */
214 inc dword ptr [esi+KTHREAD_CONTEXT_SWITCHES]
215
216 /* Set TS in cr0 to catch FPU code and load the FPU state when needed */
217 #ifndef CONFIG_SMP
218 cmp [ebx+KPCR_NPX_THREAD], esi
219 je 4f
220 #endif /* !CONFIG_SMP */
221 mov eax, cr0
222 or eax, X86_CR0_TS
223 mov cr0, eax
224 4:
225
226 /* Restore the stack pointer in this processors TSS */
227 pop ss:[ebp+KTSS_ESP0]
228
229 /* Restore exception list */
230 pop [ebx+KPCR_EXCEPTION_LIST]
231
232 /* Return */
233 call @KeReleaseDispatcherDatabaseLockFromDpcLevel@0
234 ret
235
236 /*++
237 * KiSwapContext
238 *
239 * The KiSwapContext routine switches context to another thread.
240 *
241 * Params:
242 * TargetThread - Pointer to the KTHREAD to which the caller wishes to
243 * switch to.
244 *
245 * Returns:
246 * The WaitStatus of the Target Thread. NOT YET SUPPORTED.
247 *
248 * Remarks:
249 * This is a wrapper around KiSwapContextInternal which will save all the
250 * non-volatile registers so that the Internal function can use all of
251 * them. It will also save the old current thread and set the new one.
252 *
253 * The calling thread does not return after KiSwapContextInternal until
254 * another thread switches to IT.
255 *
256 *--*/
257 .globl @KiSwapContext@4
258 @KiSwapContext@4:
259 /* Note, we CANNOT touch ebp */
260
261 /* Save 4 registers */
262 sub esp, 4 * 4
263
264 /* Save all the non-volatile ones */
265 mov [esp+12], ebx
266 mov [esp+8], esi
267 mov [esp+4], edi
268 mov [esp+0], ebp
269
270 /* Get the Current Thread */
271 mov edi, fs:[KPCR_CURRENT_THREAD]
272
273 /* Get the New Thread */
274 mov esi, ecx
275
276 /* Save it as Current thread */
277 mov fs:[KPCR_CURRENT_THREAD], esi
278
279 /* Do the swap with the registers correctly setup */
280 call @KiSwapContextInternal@0
281
282 /* Return the registers */
283 mov ebp, [esp+0]
284 mov edi, [esp+4]
285 mov esi, [esp+8]
286 mov ebx, [esp+12]
287
288 /* Clean stack */
289 add esp, 4 * 4
290 ret
291
292 #ifdef KDBG
293
294 SaveTrapFrameForKDB:
295 /* Set up a trap frame */
296
297 /* Fake Interrupt Stack */
298 push esp // 0x74
299 pushf // 0x70
300 push cs // 0x6C
301 push [esp+12] /* EIP */ // 0x68
302 mov [esp+16], ss // 0x78
303
304 /* Trap Frame */
305 push 0 /* Error Code */ // 0x64
306 push ebp // 0x60
307 push ebx
308 push esi
309 push edi
310 push fs
311 push -1 /* Exception List */ // 0x4C
312 push 0 /* Previous Mode */ // 0x48
313 push eax
314 push ecx
315 push edx
316 push ds
317 push es
318 push gs // 0x30
319 sub esp, 0x28 /* Debug Registers */ // 0x8
320 push [esp+60] /* Debug EIP */ // 0x4
321 push ebp /* Debug EBP */ // 0x0
322
323 /* Set Stack */
324 mov ebp, esp
325
326 /* Push old Trap Frame */
327 push [edi+KTHREAD_TRAP_FRAME]
328
329 /* Save new one */
330 mov [edi+KTHREAD_TRAP_FRAME], ebp
331
332 /* Return EIP */
333 push offset RestoreTrapFrameForKDB
334
335 /* Restore EBP */
336 mov ebp, [ebp+KTRAP_FRAME_EBP]
337
338 /* Jump to normal code */
339 jmp SaveTrapFrameForKDB_Return
340
341 RestoreTrapFrameForKDB:
342
343 /* Restore the old trapframe */
344 pop [edi+KTHREAD_TRAP_FRAME]
345
346 /* Pop unused portions of the trap frame */
347 add esp, 0x30
348
349 /* Restore registers from Trap frame */
350 pop gs
351 pop es
352 pop ds
353 pop edx
354 pop ecx
355 pop eax
356 add esp, 8
357 pop fs
358 pop edi
359 pop esi
360 pop ebx
361
362 /* Remove SS:ESP from the stack */
363 mov ebp, [esp+16]
364 mov [esp+24], ebp
365 mov ebp, [esp+12]
366 mov [esp+20], ebp
367 mov ebp, [esp+8]
368 mov [esp+16], ebp
369
370 /* Restore Fake INT Stack */
371 pop ebp
372 add esp, 12
373
374 /* Return to the caller. */
375 iret
376 #endif /* KDBG */
377
378