2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * 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 the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <machine/asmacros.h>
44 #include <machine/apic.h>
45 #include <machine/smptests.h> /* CHEAP_TPR, GRAB_LOPRIO */
50 /*****************************************************************************/
52 /*****************************************************************************/
58 #if defined(SWTCH_OPTIM_STATS)
59 .globl swtch_optim_stats, tlb_flush_count
60 swtch_optim_stats: .long 0 /* number of _swtch_optims */
61 tlb_flush_count: .long 0
77 /* Switch to new thread. First, save context as needed. */
78 movl PCPU(CURTHREAD),%ecx
80 /* If no thread to save, don't save it (XXX shouldn't happen). */
84 movl TD_PROC(%ecx), %eax
85 movl P_VMSPACE(%eax), %edx
86 movl PCPU(CPUID), %eax
87 btrl %eax, VM_PMAP+PM_ACTIVE(%edx)
89 movl TD_PCB(%ecx),%edx
91 movl (%esp),%eax /* Hardware registers */
92 movl %eax,PCB_EIP(%edx)
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)
102 /* Test if debug registers should be saved. */
103 testl $PCB_DBREGS,PCB_FLAGS(%edx)
104 jz 1f /* no, skip over */
105 movl %dr7,%eax /* yes, do the save */
106 movl %eax,PCB_DR7(%edx)
107 andl $0x0000fc00, %eax /* disable all watchpoints */
110 movl %eax,PCB_DR6(%edx)
112 movl %eax,PCB_DR3(%edx)
114 movl %eax,PCB_DR2(%edx)
116 movl %eax,PCB_DR1(%edx)
118 movl %eax,PCB_DR0(%edx)
122 /* XXX FIXME: we should be saving the local APIC TPR */
126 /* have we used fp, and need a save? */
127 cmpl %ecx,PCPU(FPCURTHREAD)
129 addl $PCB_SAVEFPU,%edx /* h/w bugs make saving complicated */
131 call npxsave /* do it in a big C function */
136 /* Save is done. Now choose a new thread. */
137 /* XXX still trashing space above the old "Top Of Stack". */
142 * Stop scheduling if smp_active has become zero (for rebooting) and
143 * we are not the BSP.
149 movl PCPU(IDLETHREAD), %eax
155 * Choose a new thread to schedule. choosethread() returns idlethread
156 * if it cannot find another thread to run.
158 call choosethread /* Trash ecx, edx; ret eax. */
161 testl %eax,%eax /* no thread? */
162 jz badsw3 /* no, panic */
169 movl TD_PROC(%ecx), %eax /* XXXKSE */
170 cmpb $SRUN,P_STAT(%eax)
174 movl TD_PCB(%ecx),%edx
176 #if defined(SWTCH_OPTIM_STATS)
177 incl swtch_optim_stats
180 /* switch address space */
181 movl %cr3,%ebx /* The same address space? */
182 cmpl PCB_CR3(%edx),%ebx
183 je 4f /* Yes, skip all that cruft */
184 #if defined(SWTCH_OPTIM_STATS)
185 decl swtch_optim_stats
188 movl PCB_CR3(%edx),%ebx /* Tell the CPU about the */
189 movl %ebx,%cr3 /* new address space */
192 movl PCPU(CPUID), %esi
193 cmpl $0, PCB_EXT(%edx) /* has pcb extension? */
194 je 1f /* If not, use the default */
195 btsl %esi, private_tss /* mark use of private tss */
196 movl PCB_EXT(%edx), %edi /* new tss descriptor */
197 jmp 2f /* Load it up */
200 * Use the common default TSS instead of our own.
201 * Set our stack pointer into the TSS, it's set to just
202 * below the PCB. In C, common_tss.tss_esp0 = &pcb - 16;
204 leal -16(%edx), %ebx /* leave space for vm86 */
205 movl %ebx, PCPU(COMMON_TSS) + TSS_ESP0
208 * Test this CPU's bit in the bitmap to see if this
209 * CPU was using a private TSS.
211 btrl %esi, private_tss /* Already using the common? */
212 jae 3f /* if so, skip reloading */
213 PCPU_ADDR(COMMON_TSSD, %edi)
215 /* Move correct tss descriptor into GDT slot, then reload tr. */
216 movl PCPU(TSS_GDT), %ebx /* entry in GDT */
221 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
224 /* Note in vmspace that this cpu is using it. */
225 movl TD_PROC(%ecx),%eax
226 movl P_VMSPACE(%eax), %ebx
227 movl PCPU(CPUID), %eax
228 btsl %eax, VM_PMAP+PM_ACTIVE(%ebx)
230 /* Restore context. */
231 movl PCB_EBX(%edx),%ebx
232 movl PCB_ESP(%edx),%esp
233 movl PCB_EBP(%edx),%ebp
234 movl PCB_ESI(%edx),%esi
235 movl PCB_EDI(%edx),%edi
236 movl PCB_EIP(%edx),%eax
241 #if defined(SMP) && defined(GRAB_LOPRIO)
242 /* Hold LOPRIO for interrupts. */
244 movl $0, lapic+LA_TPR
246 andl $~APIC_TPR_PRIO, lapic+LA_TPR
249 movl %edx, PCPU(CURPCB)
250 movl %ecx, PCPU(CURTHREAD) /* into next thread */
253 /* XXX FIXME: we should be restoring the local APIC TPR */
257 * Determine the LDT to use and load it if is the default one and
258 * that is not the current one.
260 movl TD_PROC(%ecx),%eax
261 cmpl $0,P_MD+MD_LDT(%eax)
263 movl _default_ldt,%eax
264 cmpl PCPU(CURRENTLDT),%eax
267 movl %eax,PCPU(CURRENTLDT)
270 /* Load the LDT when it is not the default one. */
271 pushl %edx /* Preserve pointer to pcb. */
272 addl $P_MD,%eax /* Pointer to mdproc is arg. */
279 /* This must be done after loading the user LDT. */
280 .globl cpu_switch_load_gs
282 movl PCB_GS(%edx),%gs
284 /* Test if debug registers should be restored. */
285 testl $PCB_DBREGS,PCB_FLAGS(%edx)
289 * Restore debug registers. The special code for dr7 is to
290 * preserve the current values of its reserved bits.
292 movl PCB_DR6(%edx),%eax
294 movl PCB_DR3(%edx),%eax
296 movl PCB_DR2(%edx),%eax
298 movl PCB_DR1(%edx),%eax
300 movl PCB_DR0(%edx),%eax
303 andl $0x0000fc00,%eax
304 movl PCB_DR7(%edx),%ecx
305 andl $~0x0000fc00,%ecx
316 sw0_2: .asciz "cpu_switch: not TDS_RUNQ"
322 sw0_3: .asciz "cpu_switch: choosethread returned NULL"
327 * Update pcb, saving current processor state.
333 /* Save caller's return address. Child won't execute this routine. */
335 movl %eax,PCB_EIP(%ecx)
338 movl %eax,PCB_CR3(%ecx)
340 movl %ebx,PCB_EBX(%ecx)
341 movl %esp,PCB_ESP(%ecx)
342 movl %ebp,PCB_EBP(%ecx)
343 movl %esi,PCB_ESI(%ecx)
344 movl %edi,PCB_EDI(%ecx)
345 movl %gs,PCB_GS(%ecx)
351 * If fpcurthread == NULL, then the npx h/w state is irrelevant and the
352 * state had better already be in the pcb. This is true for forks
353 * but not for dumps (the old book-keeping with FP flags in the pcb
354 * always lost for dumps because the dump pcb has 0 flags).
356 * If fpcurthread != NULL, then we have to save the npx h/w state to
357 * fpcurthread's pcb and copy it to the requested pcb, or save to the
358 * requested pcb and reload. Copying is easier because we would
359 * have to handle h/w bugs for reloading. We used to lose the
360 * parent's npx state for forks by forgetting to reload.
364 movl PCPU(FPCURTHREAD),%eax
369 movl TD_PCB(%eax),%eax
370 leal PCB_SAVEFPU(%eax),%eax
378 pushl $PCB_SAVEFPU_SIZE
379 leal PCB_SAVEFPU(%ecx),%ecx