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 #ifdef 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
69 * This is the second half of cpu_swtch(). It is used when the current
70 * thread is either a dummy or slated to die, and we no longer care
79 * Save the current thread state, then select the next thread to run
84 /* Switch to new thread. First, save context as needed. */
85 movl PCPU(CURTHREAD),%ecx
87 /* If no thread to save, don't save it (XXX shouldn't happen). */
91 movl TD_PROC(%ecx), %eax
92 movl P_VMSPACE(%eax), %edx
93 movl PCPU(CPUID), %eax
94 btrl %eax, VM_PMAP+PM_ACTIVE(%edx)
96 movl TD_PCB(%ecx),%edx
98 movl (%esp),%eax /* Hardware registers */
99 movl %eax,PCB_EIP(%edx)
100 movl %ebx,PCB_EBX(%edx)
101 movl %esp,PCB_ESP(%edx)
102 movl %ebp,PCB_EBP(%edx)
103 movl %esi,PCB_ESI(%edx)
104 movl %edi,PCB_EDI(%edx)
105 movl %gs,PCB_GS(%edx)
109 /* Test if debug registers should be saved. */
110 testl $PCB_DBREGS,PCB_FLAGS(%edx)
111 jz 1f /* no, skip over */
112 movl %dr7,%eax /* yes, do the save */
113 movl %eax,PCB_DR7(%edx)
114 andl $0x0000fc00, %eax /* disable all watchpoints */
117 movl %eax,PCB_DR6(%edx)
119 movl %eax,PCB_DR3(%edx)
121 movl %eax,PCB_DR2(%edx)
123 movl %eax,PCB_DR1(%edx)
125 movl %eax,PCB_DR0(%edx)
129 /* XXX FIXME: we should be saving the local APIC TPR */
133 /* have we used fp, and need a save? */
134 cmpl %ecx,PCPU(FPCURTHREAD)
136 addl $PCB_SAVEFPU,%edx /* h/w bugs make saving complicated */
138 call npxsave /* do it in a big C function */
143 /* Save is done. Now choose a new thread. */
144 /* XXX still trashing space above the old "Top Of Stack". */
149 * Stop scheduling if smp_active has become zero (for rebooting) and
150 * we are not the BSP.
156 movl PCPU(IDLETHREAD), %eax
162 * Choose a new thread to schedule. choosethread() returns idlethread
163 * if it cannot find another thread to run.
165 call choosethread /* Trash ecx, edx; ret eax. */
168 testl %eax,%eax /* no thread? */
169 jz badsw3 /* no, panic */
174 movl TD_PCB(%ecx),%edx
176 #ifdef 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 #ifdef 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
317 sw0_3: .asciz "cpu_switch: choosethread returned NULL"
322 * Update pcb, saving current processor state.
328 /* Save caller's return address. Child won't execute this routine. */
330 movl %eax,PCB_EIP(%ecx)
333 movl %eax,PCB_CR3(%ecx)
335 movl %ebx,PCB_EBX(%ecx)
336 movl %esp,PCB_ESP(%ecx)
337 movl %ebp,PCB_EBP(%ecx)
338 movl %esi,PCB_ESI(%ecx)
339 movl %edi,PCB_EDI(%ecx)
340 movl %gs,PCB_GS(%ecx)
346 * If fpcurthread == NULL, then the npx h/w state is irrelevant and the
347 * state had better already be in the pcb. This is true for forks
348 * but not for dumps (the old book-keeping with FP flags in the pcb
349 * always lost for dumps because the dump pcb has 0 flags).
351 * If fpcurthread != NULL, then we have to save the npx h/w state to
352 * fpcurthread's pcb and copy it to the requested pcb, or save to the
353 * requested pcb and reload. Copying is easier because we would
354 * have to handle h/w bugs for reloading. We used to lose the
355 * parent's npx state for forks by forgetting to reload.
359 movl PCPU(FPCURTHREAD),%eax
364 movl TD_PCB(%eax),%eax
365 leal PCB_SAVEFPU(%eax),%eax
373 pushl $PCB_SAVEFPU_SIZE
374 leal PCB_SAVEFPU(%ecx),%ecx