2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1990 The Regents of the University of California.
35 * All rights reserved.
37 * This code is derived from software contributed to Berkeley by
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * $FreeBSD: src/sys/i386/i386/swtch.s,v 1.89.2.10 2003/01/23 03:36:24 ps Exp $
69 * $DragonFly: src/sys/platform/pc32/i386/swtch.s,v 1.47 2007/06/29 21:54:10 dillon Exp $
74 #include <sys/rtprio.h>
76 #include <machine/asmacros.h>
77 #include <machine/segments.h>
79 #include <machine/pmap.h>
80 #include <machine_base/apic/apicreg.h>
81 #include <machine/lock.h>
86 #define MPLOCKED lock ;
94 .globl lwkt_switch_return
96 #if defined(SWTCH_OPTIM_STATS)
97 .globl swtch_optim_stats, tlb_flush_count
98 swtch_optim_stats: .long 0 /* number of _swtch_optims */
99 tlb_flush_count: .long 0
106 * cpu_heavy_switch(next_thread)
108 * Switch from the current thread to a new thread. This entry
109 * is normally called via the thread->td_switch function, and will
110 * only be called when the current thread is a heavy weight process.
112 * Some instructions have been reordered to reduce pipeline stalls.
114 * YYY disable interrupts once giant is removed.
116 ENTRY(cpu_heavy_switch)
120 movl PCPU(curthread),%ecx
121 movl (%esp),%eax /* (reorder optimization) */
122 movl TD_PCB(%ecx),%edx /* EDX = PCB */
123 movl %eax,PCB_EIP(%edx) /* return PC may be modified */
124 movl %ebx,PCB_EBX(%edx)
125 movl %esp,PCB_ESP(%edx)
126 movl %ebp,PCB_EBP(%edx)
127 movl %esi,PCB_ESI(%edx)
128 movl %edi,PCB_EDI(%edx)
129 movl 4(%esp),%edi /* EDI = newthread */
132 * Clear the cpu bit in the pmap active mask. The restore
133 * function will set the bit in the pmap active mask.
135 * Special case: when switching between threads sharing the
136 * same vmspace if we avoid clearing the bit we do not have
137 * to reload %cr3 (if we clear the bit we could race page
138 * table ops done by other threads and would have to reload
139 * %cr3, because those ops will not know to IPI us).
141 movl %ecx,%ebx /* EBX = oldthread */
142 movl TD_LWP(%ecx),%ecx /* ECX = oldlwp */
143 movl TD_LWP(%edi),%esi /* ESI = newlwp */
144 movl LWP_VMSPACE(%ecx),%ecx /* ECX = oldvmspace */
145 testl %esi,%esi /* might not be a heavy */
147 cmpl LWP_VMSPACE(%esi),%ecx /* same vmspace? */
150 movl PCPU(cpuid), %eax
151 MPLOCKED btrl %eax, VM_PMAP+PM_ACTIVE(%ecx)
154 * Push the LWKT switch restore function, which resumes a heavy
155 * weight process. Note that the LWKT switcher is based on
156 * TD_SP, while the heavy weight process switcher is based on
157 * PCB_ESP. TD_SP is usually two ints pushed relative to
158 * PCB_ESP. We push the flags for later restore by cpu_heavy_restore.
161 pushl $cpu_heavy_restore
162 movl %esp,TD_SP(%ebx)
165 * Save debug regs if necessary
167 movb PCB_FLAGS(%edx),%al
169 jz 1f /* no, skip over */
170 movl %dr7,%eax /* yes, do the save */
171 movl %eax,PCB_DR7(%edx)
172 andl $0x0000fc00, %eax /* disable all watchpoints */
175 movl %eax,PCB_DR6(%edx)
177 movl %eax,PCB_DR3(%edx)
179 movl %eax,PCB_DR2(%edx)
181 movl %eax,PCB_DR1(%edx)
183 movl %eax,PCB_DR0(%edx)
188 * Save the FP state if we have used the FP. Note that calling
189 * npxsave will NULL out PCPU(npxthread).
191 cmpl %ebx,PCPU(npxthread)
193 pushl TD_SAVEFPU(%ebx)
194 call npxsave /* do it in a big C function */
195 addl $4,%esp /* EAX, ECX, EDX trashed */
197 #endif /* NNPX > 0 */
200 * Switch to the next thread, which was passed as an argument
201 * to cpu_heavy_switch(). Due to the eflags and switch-restore
202 * function we pushed, the argument is at 12(%esp). Set the current
203 * thread, load the stack pointer, and 'ret' into the switch-restore
206 * The switch restore function expects the new thread to be in %eax
207 * and the old one to be in %ebx.
209 * There is a one-instruction window where curthread is the new
210 * thread but %esp still points to the old thread's stack, but
211 * we are protected by a critical section so it is ok.
213 movl %edi,%eax /* EAX = newtd, EBX = oldtd */
214 movl %eax,PCPU(curthread)
215 movl TD_SP(%eax),%esp
221 * The switch function is changed to this when a thread is going away
222 * for good. We have to ensure that the MMU state is not cached, and
223 * we don't bother saving the existing thread state before switching.
225 * At this point we are in a critical section and this cpu owns the
226 * thread's token, which serves as an interlock until the switchout is
229 ENTRY(cpu_exit_switch)
231 * Get us out of the vmspace
239 movl PCPU(curthread),%ebx
242 * If this is a process/lwp, deactivate the pmap after we've
245 movl TD_LWP(%ebx),%ecx
248 movl PCPU(cpuid), %eax
249 movl LWP_VMSPACE(%ecx), %ecx /* ECX = vmspace */
250 MPLOCKED btrl %eax, VM_PMAP+PM_ACTIVE(%ecx)
253 * Switch to the next thread. RET into the restore function, which
254 * expects the new thread in EAX and the old in EBX.
256 * There is a one-instruction window where curthread is the new
257 * thread but %esp still points to the old thread's stack, but
258 * we are protected by a critical section so it is ok.
261 movl %eax,PCPU(curthread)
262 movl TD_SP(%eax),%esp
266 * cpu_heavy_restore() (current thread in %eax on entry)
268 * Restore the thread after an LWKT switch. This entry is normally
269 * called via the LWKT switch restore function, which was pulled
270 * off the thread stack and jumped to.
272 * This entry is only called if the thread was previously saved
273 * using cpu_heavy_switch() (the heavy weight process thread switcher),
274 * or when a new process is initially scheduled.
276 * NOTE: The lwp may be in any state, not necessarily LSRUN, because
277 * a preemption switch may interrupt the process and then return via
280 * YYY theoretically we do not have to restore everything here, a lot
281 * of this junk can wait until we return to usermode. But for now
282 * we restore everything.
284 * YYY the PCB crap is really crap, it makes startup a bitch because
285 * we can't switch away.
287 * YYY note: spl check is done in mi_switch when it splx()'s.
290 ENTRY(cpu_heavy_restore)
292 movl TD_LWP(%eax),%ecx
294 #if defined(SWTCH_OPTIM_STATS)
295 incl _swtch_optim_stats
298 * Tell the pmap that our cpu is using the VMSPACE now. We cannot
299 * safely test/reload %cr3 until after we have set the bit in the
300 * pmap (remember, we do not hold the MP lock in the switch code).
302 * Also note that when switching between two lwps sharing the
303 * same vmspace we have already avoided clearing the cpu bit
304 * in pm_active. If we had cleared it other cpus would not know
305 * to IPI us and we would have to unconditionally reload %cr3.
307 * Also note that if the pmap is undergoing an atomic inval/mod
308 * that is unaware that our cpu has been added to it we have to
309 * wait for it to complete before we can continue.
311 movl LWP_VMSPACE(%ecx), %ecx /* ECX = vmspace */
312 movl PCPU(cpumask), %esi
313 MPLOCKED orl %esi, VM_PMAP+PM_ACTIVE(%ecx)
315 testl $CPUMASK_LOCK,VM_PMAP+PM_ACTIVE(%ecx)
319 call pmap_interlock_wait
326 * Restore the MMU address space. If it is the same as the last
327 * thread we don't have to invalidate the tlb (i.e. reload cr3).
328 * YYY which naturally also means that the PM_ACTIVE bit had better
329 * already have been set before we set it above, check? YYY
331 movl TD_PCB(%eax),%edx /* EDX = PCB */
333 movl PCB_CR3(%edx),%ecx
336 #if defined(SWTCH_OPTIM_STATS)
337 decl _swtch_optim_stats
338 incl _tlb_flush_count
343 * NOTE: %ebx is the previous thread and %eax is the new thread.
344 * %ebx is retained throughout so we can return it.
346 * lwkt_switch[_return] is responsible for handling TDF_RUNNING.
350 * Deal with the PCB extension, restore the private tss
352 movl PCB_EXT(%edx),%edi /* check for a PCB extension */
353 movl $1,%ecx /* maybe mark use of a private tss */
358 * Going back to the common_tss. We may need to update TSS_ESP0
359 * which sets the top of the supervisor stack when entering from
360 * usermode. The PCB is at the top of the stack but we need another
361 * 16 bytes to take vm86 into account.
364 movl %ecx, PCPU(common_tss) + TSS_ESP0
366 cmpl $0,PCPU(private_tss) /* don't have to reload if */
367 je 3f /* already using the common TSS */
369 subl %ecx,%ecx /* unmark use of private tss */
372 * Get the address of the common TSS descriptor for the ltr.
373 * There is no way to get the address of a segment-accessed variable
374 * so we store a self-referential pointer at the base of the per-cpu
375 * data area and add the appropriate offset.
377 movl $gd_common_tssd, %edi
381 * Move the correct TSS descriptor into the GDT slot, then reload
385 movl %ecx,PCPU(private_tss) /* mark/unmark private tss */
386 movl PCPU(tss_gdt), %ecx /* entry in GDT */
391 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
396 * Restore general registers. %ebx is restored later.
398 movl PCB_ESP(%edx),%esp
399 movl PCB_EBP(%edx),%ebp
400 movl PCB_ESI(%edx),%esi
401 movl PCB_EDI(%edx),%edi
402 movl PCB_EIP(%edx),%eax
406 * Restore the user LDT if we have one
408 cmpl $0, PCB_USERLDT(%edx)
410 movl _default_ldt,%eax
411 cmpl PCPU(currentldt),%eax
414 movl %eax,PCPU(currentldt)
421 * Restore the user TLS if we have one
428 * Restore the DEBUG register state if necessary.
430 movb PCB_FLAGS(%edx),%al
432 jz 1f /* no, skip over */
433 movl PCB_DR6(%edx),%eax /* yes, do the restore */
435 movl PCB_DR3(%edx),%eax
437 movl PCB_DR2(%edx),%eax
439 movl PCB_DR1(%edx),%eax
441 movl PCB_DR0(%edx),%eax
443 movl %dr7,%eax /* load dr7 so as not to disturb */
444 andl $0x0000fc00,%eax /* reserved bits */
445 movl PCB_DR7(%edx),%ecx
446 andl $~0x0000fc00,%ecx
450 movl %ebx,%eax /* return previous thread */
451 movl PCB_EBX(%edx),%ebx
457 * Update pcb, saving current processor state.
463 /* caller's return address - child won't execute this routine */
465 movl %eax,PCB_EIP(%ecx)
468 movl %eax,PCB_CR3(%ecx)
470 movl %ebx,PCB_EBX(%ecx)
471 movl %esp,PCB_ESP(%ecx)
472 movl %ebp,PCB_EBP(%ecx)
473 movl %esi,PCB_ESI(%ecx)
474 movl %edi,PCB_EDI(%ecx)
478 * If npxthread == NULL, then the npx h/w state is irrelevant and the
479 * state had better already be in the pcb. This is true for forks
480 * but not for dumps (the old book-keeping with FP flags in the pcb
481 * always lost for dumps because the dump pcb has 0 flags).
483 * If npxthread != NULL, then we have to save the npx h/w state to
484 * npxthread's pcb and copy it to the requested pcb, or save to the
485 * requested pcb and reload. Copying is easier because we would
486 * have to handle h/w bugs for reloading. We used to lose the
487 * parent's npx state for forks by forgetting to reload.
489 movl PCPU(npxthread),%eax
493 pushl %ecx /* target pcb */
494 movl TD_SAVEFPU(%eax),%eax /* originating savefpu area */
504 pushl $PCB_SAVEFPU_SIZE
505 leal PCB_SAVEFPU(%ecx),%ecx
510 #endif /* NNPX > 0 */
516 * cpu_idle_restore() (current thread in %eax on entry) (one-time execution)
518 * Don't bother setting up any regs other then %ebp so backtraces
519 * don't die. This restore function is used to bootstrap into the
520 * cpu_idle() LWKT only, after that cpu_lwkt_*() will be used for
523 * Clear TDF_RUNNING in old thread only after we've cleaned up %cr3.
524 * This only occurs during system boot so no special handling is
525 * required for migration.
527 * If we are an AP we have to call ap_init() before jumping to
528 * cpu_idle(). ap_init() will synchronize with the BP and finish
529 * setting up various ncpu-dependant globaldata fields. This may
530 * happen on UP as well as SMP if we happen to be simulating multiple
533 ENTRY(cpu_idle_restore)
539 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
541 orl $TDF_RUNNING,TD_FLAGS(%eax)
550 * ap_init can decide to enable interrupts early, but otherwise, or if
551 * we are UP, do it here.
557 * cpu_kthread_restore() (current thread is %eax on entry) (one-time execution)
559 * Don't bother setting up any regs other then %ebp so backtraces
560 * don't die. This restore function is used to bootstrap into an
561 * LWKT based kernel thread only. cpu_lwkt_switch() will be used
564 * Since all of our context is on the stack we are reentrant and
565 * we can release our critical section and enable interrupts early.
567 * Because this switch target does not 'return' to lwkt_switch()
568 * we have to call lwkt_switch_return(otd) to clean up otd.
571 ENTRY(cpu_kthread_restore)
574 movl TD_PCB(%eax),%esi
579 pushl %ebx /* argument to lwkt_switch_return */
580 call lwkt_switch_return
584 andl $~TDF_RUNNING,TD_FLAGS(%ebx)
585 orl $TDF_RUNNING,TD_FLAGS(%eax)
587 decl TD_CRITCOUNT(%eax)
588 popl %eax /* kthread exit function */
589 pushl PCB_EBX(%esi) /* argument to ESI function */
590 pushl %eax /* set exit func as return address */
591 movl PCB_ESI(%esi),%eax
597 * Standard LWKT switching function. Only non-scratch registers are
598 * saved and we don't bother with the MMU state or anything else.
600 * This function is always called while in a critical section.
602 * There is a one-instruction window where curthread is the new
603 * thread but %esp still points to the old thread's stack, but
604 * we are protected by a critical section so it is ok.
608 ENTRY(cpu_lwkt_switch)
609 pushl %ebp /* note: GDB hacked to locate ebp relative to td_sp */
611 movl PCPU(curthread),%ebx
615 /* warning: adjust movl into %eax below if you change the pushes */
619 * Save the FP state if we have used the FP. Note that calling
620 * npxsave will NULL out PCPU(npxthread).
622 * We have to deal with the FP state for LWKT threads in case they
623 * happen to get preempted or block while doing an optimized
624 * bzero/bcopy/memcpy.
626 cmpl %ebx,PCPU(npxthread)
628 pushl TD_SAVEFPU(%ebx)
629 call npxsave /* do it in a big C function */
630 addl $4,%esp /* EAX, ECX, EDX trashed */
632 #endif /* NNPX > 0 */
634 movl 4+20(%esp),%eax /* switch to this thread */
635 pushl $cpu_lwkt_restore
636 movl %esp,TD_SP(%ebx)
637 movl %eax,PCPU(curthread)
638 movl TD_SP(%eax),%esp
641 * eax contains new thread, ebx contains old thread.
646 * cpu_lwkt_restore() (current thread in %eax on entry)
648 * Standard LWKT restore function. This function is always called
649 * while in a critical section.
651 * Warning: due to preemption the restore function can be used to
652 * 'return' to the original thread. Interrupt disablement must be
653 * protected through the switch so we cannot run splz here.
655 * YYY we theoretically do not need to load IdlePTD into cr3, but if
656 * so we need a way to detect when the PTD we are using is being
657 * deleted due to a process exiting.
659 ENTRY(cpu_lwkt_restore)
660 movl IdlePTD,%ecx /* YYY borrow but beware desched/cpuchg/exit */
667 * NOTE: %ebx is the previous thread and %eax is the new thread.
668 * %ebx is retained throughout so we can return it.
670 * lwkt_switch[_return] is responsible for handling TDF_RUNNING.