Thread/Process Termination/Repeaing Rewrite + Fixes
[reactos.git] / reactos / ntoskrnl / ke / i386 / tskswitch.S
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * FILE: ntoskrnl/ke/i386/tskswitch.S
21 * PURPOSE: Microkernel thread support
22 * PROGRAMMER: David Welch (welch@cwcom.net)
23 * UPDATE HISTORY:
24 * Created 09/10/00
25 */
26
27 /* INCLUDES ******************************************************************/
28
29 #include <roscfg.h>
30 #include <internal/i386/segment.h>
31 #include <internal/i386/ke.h>
32 #include <internal/i386/fpu.h>
33 #include <internal/ps.h>
34 #include <ntos/tss.h>
35 #include <internal/ntoskrnl.h>
36
37 /* FUNCTIONS ****************************************************************/
38
39 .globl _Ki386ContextSwitch
40 .func Ki386ContextSwitch
41 _Ki386ContextSwitch:
42 /*
43 * FUNCTIONS: Switches to another thread's context
44 * ARGUMENTS:
45 * Thread = Thread to switch to
46 * OldThread = Thread to switch from
47 */
48 #ifdef KDBG
49 jmp SaveTrapFrameForKDB
50 SaveTrapFrameForKDB_Return:
51 #endif
52 pushl %ebp
53 movl %esp, %ebp
54
55 /*
56 * Save callee save registers.
57 */
58 pushl %ebx
59 pushl %esi
60 pushl %edi
61
62 /*
63 * This is a critical section for this processor.
64 */
65 cli
66
67 #ifdef CONFIG_SMP
68 /*
69 * Get the pointer to the old thread.
70 */
71 movl 12(%ebp), %ebx
72 /*
73 * Save FPU state if the thread has used it.
74 */
75 movl $0, %fs:KPCR_NPX_THREAD
76 testb $NPX_STATE_DIRTY, KTHREAD_NPX_STATE(%ebx)
77 jz 3f
78 movl KTHREAD_INITIAL_STACK(%ebx), %eax
79 cmpl $0, _FxsrSupport
80 je 1f
81 fxsave -SIZEOF_FX_SAVE_AREA(%eax)
82 jmp 2f
83 1:
84 fnsave -SIZEOF_FX_SAVE_AREA(%eax)
85 2:
86 movb $NPX_STATE_VALID, KTHREAD_NPX_STATE(%ebx)
87 3:
88 #endif /* CONFIG_SMP */
89
90
91 /*
92 * Get the pointer to the new thread.
93 */
94 movl 8(%ebp), %ebx
95
96 /*
97 * Set the base of the TEB selector to the base of the TEB for
98 * this thread.
99 */
100 pushl %ebx
101 pushl KTHREAD_TEB(%ebx)
102 pushl $TEB_SELECTOR
103 call _KeSetBaseGdtSelector
104 addl $8, %esp
105 popl %ebx
106
107 /*
108 * Load the PCR selector.
109 */
110 movl $PCR_SELECTOR, %eax
111 movl %eax, %fs
112
113 /*
114 * Set the current thread information in the PCR.
115 */
116 movl %ebx, %fs:KPCR_CURRENT_THREAD
117
118 /*
119 * Set the current LDT
120 */
121 xorl %eax, %eax
122 movl KTHREAD_APCSTATE_PROCESS(%ebx), %edi
123 testw $0xFFFF, KPROCESS_LDT_DESCRIPTOR0(%edi)
124 jz 0f
125
126 pushl KPROCESS_LDT_DESCRIPTOR1(%edi)
127 pushl KPROCESS_LDT_DESCRIPTOR0(%edi)
128 pushl $LDT_SELECTOR
129 call _KeSetGdtSelector
130 addl $12, %esp
131
132 movl $LDT_SELECTOR, %eax
133
134 0:
135 lldtw %ax
136
137 /*
138 * Get the pointer to the old thread.
139 */
140 movl 12(%ebp), %ebx
141
142 /*
143 * FIXME: Save debugging state.
144 */
145
146 /*
147 * Load up the iomap offset for this thread in
148 * preparation for setting it below.
149 */
150 movl KPROCESS_IOPM_OFFSET(%edi), %eax
151
152 /*
153 * Save the stack pointer in this processors TSS
154 */
155 movl %fs:KPCR_TSS, %esi
156 pushl KTSS_ESP0(%esi)
157
158 /*
159 * Switch stacks
160 */
161 movl %esp, KTHREAD_KERNEL_STACK(%ebx)
162 movl 8(%ebp), %ebx
163 movl KTHREAD_KERNEL_STACK(%ebx), %esp
164 movl KTHREAD_STACK_LIMIT(%ebx), %edi
165
166 movl %fs:KPCR_TSS, %esi
167
168 /*
169 * Set current IOPM offset in the TSS
170 */
171 movw %ax, KTSS_IOMAPBASE(%esi)
172
173 /*
174 * Change the address space
175 */
176 movl KTHREAD_APCSTATE_PROCESS(%ebx), %eax
177 movl KPROCESS_DIRECTORY_TABLE_BASE(%eax), %eax
178 movl %eax, %cr3
179
180 /*
181 * Restore the stack pointer in this processors TSS
182 */
183 popl KTSS_ESP0(%esi)
184
185 /*
186 * Set TS in cr0 to catch FPU code and load the FPU state when needed
187 * For uni-processor we do this only if NewThread != KPCR->NpxThread
188 */
189 #ifndef CONFIG_SMP
190 cmpl %ebx, %fs:KPCR_NPX_THREAD
191 je 4f
192 #endif /* !CONFIG_SMP */
193 movl %cr0, %eax
194 orl $X86_CR0_TS, %eax
195 movl %eax, %cr0
196 4:
197
198 /*
199 * FIXME: Restore debugging state
200 */
201
202 /*
203 * Exit the critical section
204 */
205 sti
206
207 call @KeReleaseDispatcherDatabaseLockFromDpcLevel@0
208
209 /*
210 * Restore the saved register and exit
211 */
212 popl %edi
213 popl %esi
214 popl %ebx
215
216 popl %ebp
217 ret
218 .endfunc
219
220
221
222 #ifdef KDBG
223
224 SaveTrapFrameForKDB:
225 /*
226 * Set up a trap frame.
227 */
228 /* Ss - space already reserved by return EIP */
229 pushl %esp /* Esp */
230 pushfl /* Eflags */
231 pushl %cs /* Cs */
232 pushl 12(%esp) /* Eip */
233 movl %ss, 16(%esp) /* Save Ss */
234 pushl $0 /* ErrorCode */
235 pushl %ebp /* Ebp */
236 pushl %ebx /* Ebx */
237 pushl %esi /* Esi */
238 pushl %edi /* Edi */
239 pushl %fs /* Fs */
240 pushl $0 /* ExceptionList */
241 pushl $0 /* PreviousMode */
242 pushl %eax /* Eax */
243 pushl %ecx /* Ecx */
244 pushl %edx /* Edx */
245 pushl %ds /* Ds */
246 pushl %es /* Es */
247 pushl %gs /* Gs */
248 movl %dr7, %eax
249 pushl %eax /* Dr7 */
250 /* Clear breakpoint enables in dr7. */
251 andl $~0xffff, %eax
252 movl %eax, %dr7
253 movl %dr6, %eax
254 pushl %eax /* Dr6 */
255 movl %dr3, %eax
256 pushl %eax /* Dr3 */
257 movl %dr2, %eax
258 pushl %eax /* Dr2 */
259 movl %dr1, %eax
260 pushl %eax /* Dr1 */
261 movl %dr0, %eax
262 pushl %eax /* Dr0 */
263 pushl $0 /* TempEip */
264 pushl $0 /* TempCs */
265 pushl $0 /* DebugPointer */
266 pushl $0xffffffff /* DebugArgMark (Exception number) */
267 pushl 0x60(%esp) /* DebugEip */
268 pushl %ebp /* DebugEbp */
269
270 movl %esp, %ebp /* Save pointer to new TrapFrame */
271
272 /* Save the old trapframe and set pointer to the new one */
273 movl 0x80(%esp), %ebx /* Get pointer to OldThread */
274 pushl KTHREAD_TRAP_FRAME(%ebx)
275 movl %ebp, KTHREAD_TRAP_FRAME(%ebx)
276
277 /* Copy the arguments which were passed to Ki386ContextSwitch */
278 pushl 0x80(%ebp) /* OldThread */
279 pushl 0x7c(%ebp) /* NewThread */
280 pushl $RestoreTrapFrameForKDB /* Return address */
281
282 /* Restore clobbered registers */
283 movl KTRAP_FRAME_EBX(%ebp), %ebx
284 movl KTRAP_FRAME_EBP(%ebp), %ebp
285
286 /* Return */
287 jmp SaveTrapFrameForKDB_Return
288
289
290 RestoreTrapFrameForKDB:
291 addl $8, %esp /* Remove NewThread and OldThread arguments from the stack */
292 movl 0x84(%esp), %ebx /* Get pointer to OldThread */
293
294 /* Restore the old trapframe */
295 popl KTHREAD_TRAP_FRAME(%ebx)
296
297 /*
298 * Pop unused portions of the trap frame:
299 * DebugEbp
300 * DebugEip
301 * DebugArgMark
302 * DebugPointer
303 * TempCs
304 * TempEip
305 * Dr0-3
306 * Dr6-7
307 */
308 addl $(12*4), %esp
309
310 /*
311 * Restore registers including any that might have been changed
312 * inside the debugger.
313 */
314 popl %gs /* Gs */
315 popl %es /* Es */
316 popl %ds /* Ds */
317 popl %edx /* Edx */
318 popl %ecx /* Ecx */
319 popl %eax /* Eax */
320 addl $4, %esp /* PreviousMode */
321 addl $4, %esp /* ExceptionList */
322 popl %fs /* Fs */
323 popl %edi /* Edi */
324 popl %esi /* Esi */
325 popl %ebx /* Ebx */
326
327 /* Remove SS:ESP from the stack */
328 movl 16(%esp), %ebp
329 movl %ebp, 24(%esp)
330 movl 12(%esp), %ebp
331 movl %ebp, 20(%esp)
332 movl 8(%esp), %ebp
333 movl %ebp, 16(%esp)
334
335 popl %ebp /* Ebp */
336 addl $12, %esp /* ErrorCode and SS:ESP */
337
338 /*
339 * Return to the caller.
340 */
341 iret
342
343 #endif /* KDBG */
344