3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
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.
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.
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.
20 * FILE: ntoskrnl/ke/i386/tskswitch.S
21 * PURPOSE: Microkernel thread support
22 * PROGRAMMER: David Welch (welch@cwcom.net)
27 /* INCLUDES ******************************************************************/
30 #include <internal/i386/segment.h>
31 #include <internal/i386/ke.h>
32 #include <internal/i386/fpu.h>
33 #include <internal/ps.h>
35 #include <internal/ntoskrnl.h>
37 /* FUNCTIONS ****************************************************************/
39 .globl _Ki386ContextSwitch
40 .func Ki386ContextSwitch
43 * FUNCTIONS: Switches to another thread's context
45 * Thread = Thread to switch to
46 * OldThread = Thread to switch from
49 jmp SaveTrapFrameForKDB
50 SaveTrapFrameForKDB_Return:
56 * Save callee save registers.
63 * This is a critical section for this processor.
69 * Get the pointer to the old thread.
73 * Save FPU state if the thread has used it.
75 movl $0, %fs:KPCR_NPX_THREAD
76 testb $NPX_STATE_DIRTY, KTHREAD_NPX_STATE(%ebx)
78 movl KTHREAD_INITIAL_STACK(%ebx), %eax
81 fxsave -SIZEOF_FX_SAVE_AREA(%eax)
84 fnsave -SIZEOF_FX_SAVE_AREA(%eax)
86 movb $NPX_STATE_VALID, KTHREAD_NPX_STATE(%ebx)
88 #endif /* CONFIG_SMP */
92 * Get the pointer to the new thread.
97 * Set the base of the TEB selector to the base of the TEB for
101 pushl KTHREAD_TEB(%ebx)
103 call _KeSetBaseGdtSelector
108 * Load the PCR selector.
110 movl $PCR_SELECTOR, %eax
114 * Set the current thread information in the PCR.
116 movl %ebx, %fs:KPCR_CURRENT_THREAD
119 * Set the current LDT
122 movl KTHREAD_APCSTATE_PROCESS(%ebx), %edi
123 testw $0xFFFF, KPROCESS_LDT_DESCRIPTOR0(%edi)
126 pushl KPROCESS_LDT_DESCRIPTOR1(%edi)
127 pushl KPROCESS_LDT_DESCRIPTOR0(%edi)
129 call _KeSetGdtSelector
132 movl $LDT_SELECTOR, %eax
138 * Get the pointer to the old thread.
143 * FIXME: Save debugging state.
147 * Load up the iomap offset for this thread in
148 * preparation for setting it below.
150 movl KPROCESS_IOPM_OFFSET(%edi), %eax
153 * Save the stack pointer in this processors TSS
155 movl %fs:KPCR_TSS, %esi
156 pushl KTSS_ESP0(%esi)
161 movl %esp, KTHREAD_KERNEL_STACK(%ebx)
163 movl KTHREAD_KERNEL_STACK(%ebx), %esp
164 movl KTHREAD_STACK_LIMIT(%ebx), %edi
166 movl %fs:KPCR_TSS, %esi
169 * Set current IOPM offset in the TSS
171 movw %ax, KTSS_IOMAPBASE(%esi)
174 * Change the address space
176 movl KTHREAD_APCSTATE_PROCESS(%ebx), %eax
177 movl KPROCESS_DIRECTORY_TABLE_BASE(%eax), %eax
181 * Restore the stack pointer in this processors TSS
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
190 cmpl %ebx, %fs:KPCR_NPX_THREAD
192 #endif /* !CONFIG_SMP */
194 orl $X86_CR0_TS, %eax
199 * FIXME: Restore debugging state
203 * Exit the critical section
207 call _KeReleaseDispatcherDatabaseLockFromDpcLevel
209 cmpl $0, _PiNrThreadsAwaitingReaping
211 call _PiWakeupReaperThread@0
215 * Restore the saved register and exit
231 * Set up a trap frame.
233 /* Ss - space already reserved by return EIP */
237 pushl 12(%esp) /* Eip */
238 movl %ss, 16(%esp) /* Save Ss */
239 pushl $0 /* ErrorCode */
245 pushl $0 /* ExceptionList */
246 pushl $0 /* PreviousMode */
255 /* Clear breakpoint enables in dr7. */
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 */
275 movl %esp, %ebp /* Save pointer to new TrapFrame */
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)
282 /* Copy the arguments which were passed to Ki386ContextSwitch */
283 pushl 0x80(%ebp) /* OldThread */
284 pushl 0x7c(%ebp) /* NewThread */
285 pushl $RestoreTrapFrameForKDB /* Return address */
287 /* Restore clobbered registers */
288 movl KTRAP_FRAME_EBX(%ebp), %ebx
289 movl KTRAP_FRAME_EBP(%ebp), %ebp
292 jmp SaveTrapFrameForKDB_Return
295 RestoreTrapFrameForKDB:
296 addl $8, %esp /* Remove NewThread and OldThread arguments from the stack */
297 movl 0x84(%esp), %ebx /* Get pointer to OldThread */
299 /* Restore the old trapframe */
300 popl KTHREAD_TRAP_FRAME(%ebx)
303 * Pop unused portions of the trap frame:
316 * Restore registers including any that might have been changed
317 * inside the debugger.
325 addl $4, %esp /* PreviousMode */
326 addl $4, %esp /* ExceptionList */
332 /* Remove SS:ESP from the stack */
341 addl $12, %esp /* ErrorCode and SS:ESP */
344 * Return to the caller.