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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "opt_sched.h"
38 #include <machine/asmacros.h>
42 #if defined(SMP) && defined(SCHED_ULE)
44 #define BLOCK_SPIN(reg) \
45 movl $blocked_lock,%eax ; \
48 cmpxchgl %eax,TD_LOCK(reg) ; \
55 #define BLOCK_SPIN(reg)
58 /*****************************************************************************/
60 /*****************************************************************************/
67 * This is the second half of cpu_switch(). It is used when the current
68 * thread is either a dummy or slated to die, and we no longer care
69 * about its state. This is only a slight optimization and is probably
70 * not worth it anymore. Note that we need to clear the pm_active bits so
71 * we do need the old proc if it still exists.
77 movl PCPU(CPUID), %esi
78 movl 4(%esp),%ecx /* Old thread */
79 testl %ecx,%ecx /* no thread? */
81 /* release bit from old pm_active */
82 movl PCPU(CURPMAP), %ebx
86 btrl %esi, PM_ACTIVE(%ebx) /* clear old */
88 movl 8(%esp),%ecx /* New thread */
89 movl TD_PCB(%ecx),%edx
90 movl PCB_CR3(%edx),%eax
92 /* set bit in new pm_active */
93 movl TD_PROC(%ecx),%eax
94 movl P_VMSPACE(%eax), %ebx
96 movl %ebx, PCPU(CURPMAP)
100 btsl %esi, PM_ACTIVE(%ebx) /* set new */
105 * cpu_switch(old, new)
107 * Save the current thread state, then select the next thread to run
108 * and load its state.
116 /* Switch to new thread. First, save context. */
120 testl %ecx,%ecx /* no thread? */
121 jz badsw2 /* no, panic */
124 movl TD_PCB(%ecx),%edx
126 movl (%esp),%eax /* Hardware registers */
127 movl %eax,PCB_EIP(%edx)
128 movl %ebx,PCB_EBX(%edx)
129 movl %esp,PCB_ESP(%edx)
130 movl %ebp,PCB_EBP(%edx)
131 movl %esi,PCB_ESI(%edx)
132 movl %edi,PCB_EDI(%edx)
136 /* Test if debug registers should be saved. */
137 testl $PCB_DBREGS,PCB_FLAGS(%edx)
138 jz 1f /* no, skip over */
139 movl %dr7,%eax /* yes, do the save */
140 movl %eax,PCB_DR7(%edx)
141 andl $0x0000fc00, %eax /* disable all watchpoints */
144 movl %eax,PCB_DR6(%edx)
146 movl %eax,PCB_DR3(%edx)
148 movl %eax,PCB_DR2(%edx)
150 movl %eax,PCB_DR1(%edx)
152 movl %eax,PCB_DR0(%edx)
156 /* have we used fp, and need a save? */
157 cmpl %ecx,PCPU(FPCURTHREAD)
159 pushl PCB_SAVEFPU(%edx) /* h/w bugs make saving complicated */
160 call npxsave /* do it in a big C function */
165 /* Save is done. Now fire up new thread. Leave old vmspace. */
167 movl 8(%esp),%ecx /* New thread */
168 movl 12(%esp),%esi /* New lock */
170 testl %ecx,%ecx /* no thread? */
171 jz badsw3 /* no, panic */
173 movl TD_PCB(%ecx),%edx
175 /* switch address space */
176 movl PCB_CR3(%edx),%eax
177 movl %cr3,%ebx /* The same address space? */
180 movl %eax,%cr3 /* new address space */
182 movl PCPU(CPUID),%esi
183 SETOP %eax,TD_LOCK(%edi) /* Switchout td_lock */
185 /* Release bit from old pmap->pm_active */
186 movl PCPU(CURPMAP), %ebx
190 btrl %esi, PM_ACTIVE(%ebx) /* clear old */
192 /* Set bit in new pmap->pm_active */
193 movl TD_PROC(%ecx),%eax /* newproc */
194 movl P_VMSPACE(%eax), %ebx
196 movl %ebx, PCPU(CURPMAP)
200 btsl %esi, PM_ACTIVE(%ebx) /* set new */
204 SETOP %esi,TD_LOCK(%edi) /* Switchout td_lock */
208 * At this point, we've switched address spaces and are ready
209 * to load up the rest of the next context.
211 cmpl $0, PCB_EXT(%edx) /* has pcb extension? */
212 je 1f /* If not, use the default */
213 movl $1, PCPU(PRIVATE_TSS) /* mark use of private tss */
214 movl PCB_EXT(%edx), %edi /* new tss descriptor */
215 jmp 2f /* Load it up */
218 * Use the common default TSS instead of our own.
219 * Set our stack pointer into the TSS, it's set to just
220 * below the PCB. In C, common_tss.tss_esp0 = &pcb - 16;
222 leal -16(%edx), %ebx /* leave space for vm86 */
223 movl %ebx, PCPU(COMMON_TSS) + TSS_ESP0
226 * Test this CPU's bit in the bitmap to see if this
227 * CPU was using a private TSS.
229 cmpl $0, PCPU(PRIVATE_TSS) /* Already using the common? */
230 je 3f /* if so, skip reloading */
231 movl $0, PCPU(PRIVATE_TSS)
232 PCPU_ADDR(COMMON_TSSD, %edi)
234 /* Move correct tss descriptor into GDT slot, then reload tr. */
235 movl PCPU(TSS_GDT), %ebx /* entry in GDT */
240 movl $GPROC0_SEL*8, %esi /* GSEL(GPROC0_SEL, SEL_KPL) */
244 /* Copy the %fs and %gs selectors into this pcpu gdt */
245 leal PCB_FSD(%edx), %esi
246 movl PCPU(FSGS_GDT), %edi
247 movl 0(%esi), %eax /* %fs selector */
251 movl 8(%esi), %eax /* %gs selector, comes straight after */
256 /* Restore context. */
257 movl PCB_EBX(%edx),%ebx
258 movl PCB_ESP(%edx),%esp
259 movl PCB_EBP(%edx),%ebp
260 movl PCB_ESI(%edx),%esi
261 movl PCB_EDI(%edx),%edi
262 movl PCB_EIP(%edx),%eax
267 movl %edx, PCPU(CURPCB)
268 movl TD_TID(%ecx),%eax
269 movl %ecx, PCPU(CURTHREAD) /* into next thread */
272 * Determine the LDT to use and load it if is the default one and
273 * that is not the current one.
275 movl TD_PROC(%ecx),%eax
276 cmpl $0,P_MD+MD_LDT(%eax)
278 movl _default_ldt,%eax
279 cmpl PCPU(CURRENTLDT),%eax
282 movl %eax,PCPU(CURRENTLDT)
285 /* Load the LDT when it is not the default one. */
286 pushl %edx /* Preserve pointer to pcb. */
287 addl $P_MD,%eax /* Pointer to mdproc is arg. */
294 /* This must be done after loading the user LDT. */
295 .globl cpu_switch_load_gs
299 /* Test if debug registers should be restored. */
300 testl $PCB_DBREGS,PCB_FLAGS(%edx)
304 * Restore debug registers. The special code for dr7 is to
305 * preserve the current values of its reserved bits.
307 movl PCB_DR6(%edx),%eax
309 movl PCB_DR3(%edx),%eax
311 movl PCB_DR2(%edx),%eax
313 movl PCB_DR1(%edx),%eax
315 movl PCB_DR0(%edx),%eax
318 andl $0x0000fc00,%eax
319 movl PCB_DR7(%edx),%ecx
320 andl $~0x0000fc00,%ecx
331 sw0_1: .asciz "cpu_throw: no newthread supplied"
337 sw0_2: .asciz "cpu_switch: no curthread supplied"
343 sw0_3: .asciz "cpu_switch: no newthread supplied"
349 * Update pcb, saving current processor state.
355 /* Save caller's return address. Child won't execute this routine. */
357 movl %eax,PCB_EIP(%ecx)
360 movl %eax,PCB_CR3(%ecx)
362 movl %ebx,PCB_EBX(%ecx)
363 movl %esp,PCB_ESP(%ecx)
364 movl %ebp,PCB_EBP(%ecx)
365 movl %esi,PCB_ESI(%ecx)
366 movl %edi,PCB_EDI(%ecx)
372 movl %eax,PCB_CR0(%ecx)
374 movl %eax,PCB_CR2(%ecx)
376 movl %eax,PCB_CR4(%ecx)
379 movl %eax,PCB_DR0(%ecx)
381 movl %eax,PCB_DR1(%ecx)
383 movl %eax,PCB_DR2(%ecx)
385 movl %eax,PCB_DR3(%ecx)
387 movl %eax,PCB_DR6(%ecx)
389 movl %eax,PCB_DR7(%ecx)
406 * resumectx(pcb) __fastcall
407 * Resuming processor state from pcb.
413 /* Restore segment registers */
414 movzwl PCB_DS(%ecx),%eax
416 movzwl PCB_ES(%ecx),%eax
418 movzwl PCB_FS(%ecx),%eax
420 movzwl PCB_GS(%ecx),%eax
422 movzwl PCB_SS(%ecx),%eax
425 /* Restore CR2, CR4, CR3 and CR0 */
426 movl PCB_CR2(%ecx),%eax
428 movl PCB_CR4(%ecx),%eax
430 movl PCB_CR3(%ecx),%eax
432 movl PCB_CR0(%ecx),%eax
437 /* Restore descriptor tables */
441 #define SDT_SYS386TSS 9
442 #define SDT_SYS386BSY 11
443 /* Clear "task busy" bit and reload TR */
444 movl PCPU(TSS_GDT),%eax
445 andb $(~SDT_SYS386BSY | SDT_SYS386TSS),5(%eax)
446 movzwl PCB_TR(%ecx),%eax
451 /* Restore debug registers */
452 movl PCB_DR0(%ecx),%eax
454 movl PCB_DR1(%ecx),%eax
456 movl PCB_DR2(%ecx),%eax
458 movl PCB_DR3(%ecx),%eax
460 movl PCB_DR6(%ecx),%eax
462 movl PCB_DR7(%ecx),%eax
465 /* Restore other registers */
466 movl PCB_EDI(%ecx),%edi
467 movl PCB_ESI(%ecx),%esi
468 movl PCB_EBP(%ecx),%ebp
469 movl PCB_ESP(%ecx),%esp
470 movl PCB_EBX(%ecx),%ebx
472 /* reload code selector by turning return into intersegmental return */