2 * Copyright (c) 1990 The Regents of the University of California.
4 * LWKT threads Copyright (c) 2003 Matthew Dillon
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $FreeBSD: src/sys/i386/i386/swtch.s,v 1.89.2.10 2003/01/23 03:36:24 ps Exp $
38 * $DragonFly: src/sys/platform/pc32/i386/swtch.s,v 1.30 2004/02/21 06:37:07 dillon Exp $
43 #include <sys/rtprio.h>
45 #include <machine/asmacros.h>
46 #include <machine/ipl.h>
48 #include <machine/pmap.h>
49 #include <machine/smptests.h> /** GRAB_LOPRIO */
50 #include <machine/apicreg.h>
51 #include <machine/lock.h>
56 #define MPLOCKED lock ;
65 #if defined(SWTCH_OPTIM_STATS)
66 .globl swtch_optim_stats, tlb_flush_count
67 swtch_optim_stats: .long 0 /* number of _swtch_optims */
68 tlb_flush_count: .long 0
75 * cpu_heavy_switch(next_thread)
77 * Switch from the current thread to a new thread. This entry
78 * is normally called via the thread->td_switch function, and will
79 * only be called when the current thread is a heavy weight process.
81 * Some instructions have been reordered to reduce pipeline stalls.
83 * YYY disable interrupts once giant is removed.
85 ENTRY(cpu_heavy_switch)
89 movl PCPU(curthread),%ecx
90 movl (%esp),%eax /* (reorder optimization) */
91 movl TD_PCB(%ecx),%edx /* EDX = PCB */
92 movl %eax,PCB_EIP(%edx) /* return PC may be modified */
93 movl %ebx,PCB_EBX(%edx)
94 movl %esp,PCB_ESP(%edx)
95 movl %ebp,PCB_EBP(%edx)
96 movl %esi,PCB_ESI(%edx)
97 movl %edi,PCB_EDI(%edx)
100 movl %ecx,%ebx /* EBX = curthread */
101 movl TD_PROC(%ecx),%ecx
102 movl PCPU(cpuid), %eax
103 movl P_VMSPACE(%ecx), %ecx /* ECX = vmspace */
104 MPLOCKED btrl %eax, VM_PMAP+PM_ACTIVE(%ecx)
107 * Push the LWKT switch restore function, which resumes a heavy
108 * weight process. Note that the LWKT switcher is based on
109 * TD_SP, while the heavy weight process switcher is based on
110 * PCB_ESP. TD_SP is usually two ints pushed relative to
111 * PCB_ESP. We push the flags for later restore by cpu_heavy_restore.
114 pushl $cpu_heavy_restore
115 movl %esp,TD_SP(%ebx)
118 * Save debug regs if necessary
120 movb PCB_FLAGS(%edx),%al
122 jz 1f /* no, skip over */
123 movl %dr7,%eax /* yes, do the save */
124 movl %eax,PCB_DR7(%edx)
125 andl $0x0000fc00, %eax /* disable all watchpoints */
128 movl %eax,PCB_DR6(%edx)
130 movl %eax,PCB_DR3(%edx)
132 movl %eax,PCB_DR2(%edx)
134 movl %eax,PCB_DR1(%edx)
136 movl %eax,PCB_DR0(%edx)
140 * Save the FP state if we have used the FP. Note that calling
141 * npxsave will NULL out PCPU(npxthread).
144 cmpl %ebx,PCPU(npxthread)
146 addl $PCB_SAVEFPU,%edx
148 call npxsave /* do it in a big C function */
149 addl $4,%esp /* EAX, ECX, EDX trashed */
151 #endif /* NNPX > 0 */
154 * Switch to the next thread, which was passed as an argument
155 * to cpu_heavy_switch(). Due to the eflags and switch-restore
156 * function we pushed, the argument is at 12(%esp). Set the current
157 * thread, load the stack pointer, and 'ret' into the switch-restore
160 * The switch restore function expects the new thread to be in %eax
161 * and the old one to be in %ebx.
163 * There is a one-instruction window where curthread is the new
164 * thread but %esp still points to the old thread's stack, but
165 * we are protected by a critical section so it is ok.
167 movl 12(%esp),%eax /* EAX = newtd, EBX = oldtd */
168 movl %eax,PCPU(curthread)
169 movl TD_SP(%eax),%esp
175 * The switch function is changed to this when a thread is going away
176 * for good. We have to ensure that the MMU state is not cached, and
177 * we don't bother saving the existing thread state before switching.
179 * At this point we are in a critical section and this cpu owns the
180 * thread's token, which serves as an interlock until the switchout is
183 ENTRY(cpu_exit_switch)
185 * Get us out of the vmspace
193 movl PCPU(curthread),%ebx
195 * Switch to the next thread. RET into the restore function, which
196 * expects the new thread in EAX and the old in EBX.
198 * There is a one-instruction window where curthread is the new
199 * thread but %esp still points to the old thread's stack, but
200 * we are protected by a critical section so it is ok.
203 movl %eax,PCPU(curthread)
204 movl TD_SP(%eax),%esp
208 * cpu_heavy_restore() (current thread in %eax on entry)
210 * Restore the thread after an LWKT switch. This entry is normally
211 * called via the LWKT switch restore function, which was pulled
212 * off the thread stack and jumped to.
214 * This entry is only called if the thread was previously saved
215 * using cpu_heavy_switch() (the heavy weight process thread switcher),
216 * or when a new process is initially scheduled. The first thing we
217 * do is clear the TDF_RUNNING bit in the old thread and set it in the
220 * YYY theoretically we do not have to restore everything here, a lot
221 * of this junk can wait until we return to usermode. But for now
222 * we restore everything.
224 * YYY the PCB crap is really crap, it makes startup a bitch because
225 * we can't switch away.
227 * YYY note: spl check is done in mi_switch when it splx()'s.
230 ENTRY(cpu_heavy_restore)
232 movl TD_PCB(%eax),%edx /* EDX = PCB */
233 movl TD_PROC(%eax),%ecx
236 * A heavy weight process will normally be in an SRUN state
237 * but can also be preempted while it is entering a SZOMB
240 cmpb $SRUN,P_STAT(%ecx)
242 cmpb $SZOMB,P_STAT(%ecx)
247 #if defined(SWTCH_OPTIM_STATS)
248 incl _swtch_optim_stats
251 * Tell the pmap that our cpu is using the VMSPACE now. We cannot
252 * safely test/reload %cr3 until after we have set the bit in the
253 * pmap (remember, we do not hold the MP lock in the switch code).
255 movl P_VMSPACE(%ecx), %ecx /* ECX = vmspace */
256 movl PCPU(cpuid), %esi
257 MPLOCKED btsl %esi, VM_PMAP+PM_ACTIVE(%ecx)
260 * Restore the MMU address space. If it is the same as the last
261 * thread we don't have to invalidate the tlb (i.e. reload cr3).
262 * YYY which naturally also means that the PM_ACTIVE bit had better
263 * already have been set before we set it above, check? YYY
266 movl PCB_CR3(%edx),%ecx
269 #if defined(SWTCH_OPTIM_STATS)
270 decl _swtch_optim_stats
271 incl _tlb_flush_count
276 * Clear TDF_RUNNING flag in old thread only after cleaning up
277 * %cr3. The target thread is already protected by being TDF_RUNQ
278 * so setting TDF_RUNNING isn't as big a deal.
280 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
281 orl $TDF_RUNNING,TD_FLAGS(%eax)
284 * Deal with the PCB extension, restore the private tss
286 movl PCB_EXT(%edx),%edi /* check for a PCB extension */
287 movl $1,%ebx /* maybe mark use of a private tss */
292 * Going back to the common_tss. We may need to update TSS_ESP0
293 * which sets the top of the supervisor stack when entering from
294 * usermode. The PCB is at the top of the stack but we need another
295 * 16 bytes to take vm86 into account.
298 movl %ebx, PCPU(common_tss) + TSS_ESP0
300 cmpl $0,PCPU(private_tss) /* don't have to reload if */
301 je 3f /* already using the common TSS */
303 subl %ebx,%ebx /* unmark use of private tss */
306 * Get the address of the common TSS descriptor for the ltr.
307 * There is no way to get the address of a segment-accessed variable
308 * so we store a self-referential pointer at the base of the per-cpu
309 * data area and add the appropriate offset.
311 movl $gd_common_tssd, %edi
315 * Move the correct TSS descriptor into the GDT slot, then reload
319 movl %ebx,PCPU(private_tss) /* mark/unmark private tss */
320 movl PCPU(tss_gdt), %ebx /* entry in GDT */
325 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
330 * Restore general registers.
332 movl PCB_EBX(%edx),%ebx
333 movl PCB_ESP(%edx),%esp
334 movl PCB_EBP(%edx),%ebp
335 movl PCB_ESI(%edx),%esi
336 movl PCB_EDI(%edx),%edi
337 movl PCB_EIP(%edx),%eax
341 * Restore the user LDT if we have one
343 cmpl $0, PCB_USERLDT(%edx)
345 movl _default_ldt,%eax
346 cmpl PCPU(currentldt),%eax
349 movl %eax,PCPU(currentldt)
356 * Restore the %gs segment register, which must be done after
357 * loading the user LDT. Since user processes can modify the
358 * register via procfs, this may result in a fault which is
359 * detected by checking the fault address against cpu_switch_load_gs
360 * in i386/i386/trap.c
362 .globl cpu_switch_load_gs
364 movl PCB_GS(%edx),%gs
367 * Restore the DEBUG register state if necessary.
369 movb PCB_FLAGS(%edx),%al
371 jz 1f /* no, skip over */
372 movl PCB_DR6(%edx),%eax /* yes, do the restore */
374 movl PCB_DR3(%edx),%eax
376 movl PCB_DR2(%edx),%eax
378 movl PCB_DR1(%edx),%eax
380 movl PCB_DR0(%edx),%eax
382 movl %dr7,%eax /* load dr7 so as not to disturb */
383 andl $0x0000fc00,%eax /* reserved bits */
385 movl PCB_DR7(%edx),%ebx
386 andl $~0x0000fc00,%ebx
398 sw0_2: .asciz "cpu_switch: not SRUN"
402 * Update pcb, saving current processor state.
408 /* caller's return address - child won't execute this routine */
410 movl %eax,PCB_EIP(%ecx)
413 movl %eax,PCB_CR3(%ecx)
415 movl %ebx,PCB_EBX(%ecx)
416 movl %esp,PCB_ESP(%ecx)
417 movl %ebp,PCB_EBP(%ecx)
418 movl %esi,PCB_ESI(%ecx)
419 movl %edi,PCB_EDI(%ecx)
420 movl %gs,PCB_GS(%ecx)
424 * If npxthread == NULL, then the npx h/w state is irrelevant and the
425 * state had better already be in the pcb. This is true for forks
426 * but not for dumps (the old book-keeping with FP flags in the pcb
427 * always lost for dumps because the dump pcb has 0 flags).
429 * If npxthread != NULL, then we have to save the npx h/w state to
430 * npxthread's pcb and copy it to the requested pcb, or save to the
431 * requested pcb and reload. Copying is easier because we would
432 * have to handle h/w bugs for reloading. We used to lose the
433 * parent's npx state for forks by forgetting to reload.
435 movl PCPU(npxthread),%eax
440 movl TD_PCB(%eax),%eax
441 leal PCB_SAVEFPU(%eax),%eax
449 pushl $PCB_SAVEFPU_SIZE
450 leal PCB_SAVEFPU(%ecx),%ecx
455 #endif /* NNPX > 0 */
461 * cpu_idle_restore() (current thread in %eax on entry) (one-time execution)
463 * Don't bother setting up any regs other then %ebp so backtraces
464 * don't die. This restore function is used to bootstrap into the
465 * cpu_idle() LWKT only, after that cpu_lwkt_*() will be used for
468 * Clear TDF_RUNNING in old thread only after we've cleaned up %cr3.
470 * If we are an AP we have to call ap_init() before jumping to
471 * cpu_idle(). ap_init() will synchronize with the BP and finish
472 * setting up various ncpu-dependant globaldata fields. This may
473 * happen on UP as well as SMP if we happen to be simulating multiple
476 ENTRY(cpu_idle_restore)
482 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
483 orl $TDF_RUNNING,TD_FLAGS(%eax)
494 * cpu_kthread_restore() (current thread is %eax on entry) (one-time execution)
496 * Don't bother setting up any regs other then %ebp so backtraces
497 * don't die. This restore function is used to bootstrap into an
498 * LWKT based kernel thread only. cpu_lwkt_switch() will be used
501 * Since all of our context is on the stack we are reentrant and
502 * we can release our critical section and enable interrupts early.
504 ENTRY(cpu_kthread_restore)
507 movl TD_PCB(%eax),%edx
510 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
511 orl $TDF_RUNNING,TD_FLAGS(%eax)
512 subl $TDPRI_CRIT,TD_PRI(%eax)
513 popl %eax /* kthread exit function */
514 pushl PCB_EBX(%edx) /* argument to ESI function */
515 pushl %eax /* set exit func as return address */
516 movl PCB_ESI(%edx),%eax
522 * Standard LWKT switching function. Only non-scratch registers are
523 * saved and we don't bother with the MMU state or anything else.
525 * This function is always called while in a critical section.
527 * There is a one-instruction window where curthread is the new
528 * thread but %esp still points to the old thread's stack, but
529 * we are protected by a critical section so it is ok.
533 ENTRY(cpu_lwkt_switch)
540 movl PCPU(curthread),%ebx
541 pushl $cpu_lwkt_restore
542 movl %esp,TD_SP(%ebx)
543 movl %eax,PCPU(curthread)
544 movl TD_SP(%eax),%esp
547 * eax contains new thread, ebx contains old thread.
552 * cpu_lwkt_restore() (current thread in %eax on entry)
554 * Standard LWKT restore function. This function is always called
555 * while in a critical section.
557 * Warning: due to preemption the restore function can be used to
558 * 'return' to the original thread. Interrupt disablement must be
559 * protected through the switch so we cannot run splz here.
561 * YYY we theoretically do not need to load IdlePTD into cr3, but if
562 * so we need a way to detect when the PTD we are using is being
563 * deleted due to a process exiting.
565 ENTRY(cpu_lwkt_restore)
566 movl IdlePTD,%ecx /* YYY borrow but beware desched/cpuchg/exit */
572 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
573 orl $TDF_RUNNING,TD_FLAGS(%eax)