Little KDB update ;-) If you have any problems and/or questions let me know. I hope...
[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
208
209 cmpl $0, _PiNrThreadsAwaitingReaping
210 je 5f
211 call _PiWakeupReaperThread@0
212 5:
213
214 /*
215 * Restore the saved register and exit
216 */
217 popl %edi
218 popl %esi
219 popl %ebx
220
221 popl %ebp
222 ret
223 .endfunc
224
225
226
227 #ifdef KDBG
228
229 SaveTrapFrameForKDB:
230 /*
231 * Set up a trap frame.
232 */
233 /* Ss - space already reserved by return EIP */
234 pushl %esp /* Esp */
235 pushfl /* Eflags */
236 pushl %cs /* Cs */
237 pushl 12(%esp) /* Eip */
238 movl %ss, 16(%esp) /* Save Ss */
239 pushl $0 /* ErrorCode */
240 pushl %ebp /* Ebp */
241 pushl %ebx /* Ebx */
242 pushl %esi /* Esi */
243 pushl %edi /* Edi */
244 pushl %fs /* Fs */
245 pushl $0 /* ExceptionList */
246 pushl $0 /* PreviousMode */
247 pushl %eax /* Eax */
248 pushl %ecx /* Ecx */
249 pushl %edx /* Edx */
250 pushl %ds /* Ds */
251 pushl %es /* Es */
252 pushl %gs /* Gs */
253 movl %dr7, %eax
254 pushl %eax /* Dr7 */
255 /* Clear breakpoint enables in dr7. */
256 andl $~0xffff, %eax
257 movl %eax, %dr7
258 movl %dr6, %eax
259 pushl %eax /* Dr6 */
260 movl %dr3, %eax
261 pushl %eax /* Dr3 */
262 movl %dr2, %eax
263 pushl %eax /* Dr2 */
264 movl %dr1, %eax
265 pushl %eax /* Dr1 */
266 movl %dr0, %eax
267 pushl %eax /* Dr0 */
268 pushl $0 /* TempEip */
269 pushl $0 /* TempCs */
270 pushl $0 /* DebugPointer */
271 pushl $0xffffffff /* DebugArgMark (Exception number) */
272 pushl 0x60(%esp) /* DebugEip */
273 pushl %ebp /* DebugEbp */
274
275 movl %esp, %ebp /* Save pointer to new TrapFrame */
276
277 /* Save the old trapframe and set pointer to the new one */
278 movl 0x80(%esp), %ebx /* Get pointer to OldThread */
279 pushl KTHREAD_TRAP_FRAME(%ebx)
280 movl %ebp, KTHREAD_TRAP_FRAME(%ebx)
281
282 /* Copy the arguments which were passed to Ki386ContextSwitch */
283 pushl 0x80(%ebp) /* OldThread */
284 pushl 0x7c(%ebp) /* NewThread */
285 pushl $RestoreTrapFrameForKDB /* Return address */
286
287 /* Restore clobbered registers */
288 movl KTRAP_FRAME_EBX(%ebp), %ebx
289 movl KTRAP_FRAME_EBP(%ebp), %ebp
290
291 /* Return */
292 jmp SaveTrapFrameForKDB_Return
293
294
295 RestoreTrapFrameForKDB:
296 addl $8, %esp /* Remove NewThread and OldThread arguments from the stack */
297 movl 0x84(%esp), %ebx /* Get pointer to OldThread */
298
299 /* Restore the old trapframe */
300 popl KTHREAD_TRAP_FRAME(%ebx)
301
302 /*
303 * Pop unused portions of the trap frame:
304 * DebugEbp
305 * DebugEip
306 * DebugArgMark
307 * DebugPointer
308 * TempCs
309 * TempEip
310 * Dr0-3
311 * Dr6-7
312 */
313 addl $(12*4), %esp
314
315 /*
316 * Restore registers including any that might have been changed
317 * inside the debugger.
318 */
319 popl %gs /* Gs */
320 popl %es /* Es */
321 popl %ds /* Ds */
322 popl %edx /* Edx */
323 popl %ecx /* Ecx */
324 popl %eax /* Eax */
325 addl $4, %esp /* PreviousMode */
326 addl $4, %esp /* ExceptionList */
327 popl %fs /* Fs */
328 popl %edi /* Edi */
329 popl %esi /* Esi */
330 popl %ebx /* Ebx */
331
332 /* Remove SS:ESP from the stack */
333 movl 16(%esp), %ebp
334 movl %ebp, 24(%esp)
335 movl 12(%esp), %ebp
336 movl %ebp, 20(%esp)
337 movl 8(%esp), %ebp
338 movl %ebp, 16(%esp)
339
340 popl %ebp /* Ebp */
341 addl $12, %esp /* ErrorCode and SS:ESP */
342
343 /*
344 * Return to the caller.
345 */
346 iret
347
348 #endif /* KDBG */
349