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
40 #include "opt_user_ldt.h"
42 #include <sys/rtprio.h>
44 #include <machine/asmacros.h>
47 #include <machine/pmap.h>
48 #include <machine/apic.h>
49 #include <machine/smptests.h> /** GRAB_LOPRIO */
50 #include <machine/ipl.h>
51 #include <machine/lock.h>
57 /*****************************************************************************/
59 /*****************************************************************************/
64 _hlt_vector: .long _default_halt /* pointer to halt routine */
69 _want_resched: .long 0 /* we need to re-run the scheduler */
70 #if defined(SWTCH_OPTIM_STATS)
71 .globl _swtch_optim_stats, _tlb_flush_count
72 _swtch_optim_stats: .long 0 /* number of _swtch_optims */
73 _tlb_flush_count: .long 0
79 * When no processes are on the runq, cpu_switch() branches to _idle
80 * to wait for something to come ready.
90 /* when called, we have the mplock, intr disabled */
91 /* use our idleproc's "context" */
96 #if defined(SWTCH_OPTIM_STATS)
97 decl _swtch_optim_stats
102 /* Keep space for nonexisting return addr, or profiling bombs */
103 movl $gd_idlestack_top-4, %ecx
107 /* update common_tss.tss_esp0 pointer */
108 movl %ecx, _common_tss + TSS_ESP0
111 btrl %esi, _private_tss
114 movl $gd_common_tssd, %edi
117 /* move correct tss descriptor into GDT slot, then reload tr */
118 movl _tss_gdt, %ebx /* entry in GDT */
123 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
130 * XXX callers of cpu_switch() do a bogus splclock(). Locking should
131 * be left to cpu_switch().
138 * _REALLY_ free the lock, no matter how deep the prior nesting.
139 * We will recover the nesting on the way out when we have a new
142 * XXX: we had damn well better be sure we had it before doing this!
144 movl $FREE_LOCK, %eax
147 /* do NOT have lock, intrs disabled */
162 cmpl $0,_do_page_zero_idle
165 /* XXX appears to cause panics */
167 * Inside zero_idle we enable interrupts and grab the mplock
168 * as needed. It needs to be careful about entry/exit mutexes.
170 call _vm_page_zero_idle /* internal locking */
175 /* enable intrs for a halt */
176 movl $0, lapic_tpr /* 1st candidate for an INT */
178 call *_hlt_vector /* wait for interrupt */
183 movl $LOPRIO_LEVEL, lapic_tpr /* arbitrate for INTs */
187 CROSSJUMP(jnz, sw1a, jz)
193 movl $HIDENAME(tmpstk),%esp
194 #if defined(OVERLY_CONSERVATIVE_PTD_MGMT)
195 #if defined(SWTCH_OPTIM_STATS)
196 incl _swtch_optim_stats
202 #if defined(SWTCH_OPTIM_STATS)
203 decl _swtch_optim_stats
204 incl _tlb_flush_count
210 /* update common_tss.tss_esp0 pointer */
211 movl %esp, _common_tss + TSS_ESP0
214 btrl %esi, _private_tss
217 movl $_common_tssd, %edi
219 /* move correct tss descriptor into GDT slot, then reload tr */
220 movl _tss_gdt, %ebx /* entry in GDT */
225 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
232 * XXX callers of cpu_switch() do a bogus splclock(). Locking should
233 * be left to cpu_switch().
242 CROSSJUMP(jnz, sw1a, jz)
243 call _vm_page_zero_idle
247 call *_hlt_vector /* wait for interrupt */
252 CROSSJUMPTARGET(_idle)
256 hlt /* XXX: until a wakeup IPI */
265 /* switch to new process. first, save context as needed */
268 /* if no process to save, don't bother */
273 movb P_ONCPU(%ecx), %al /* save "last" cpu */
274 movb %al, P_LASTCPU(%ecx)
275 movb $0xff, P_ONCPU(%ecx) /* "leave" the cpu */
277 movl P_VMSPACE(%ecx), %edx
283 btrl %eax, VM_PMAP+PM_ACTIVE(%edx)
285 movl P_ADDR(%ecx),%edx
287 movl (%esp),%eax /* Hardware registers */
288 movl %eax,PCB_EIP(%edx)
289 movl %ebx,PCB_EBX(%edx)
290 movl %esp,PCB_ESP(%edx)
291 movl %ebp,PCB_EBP(%edx)
292 movl %esi,PCB_ESI(%edx)
293 movl %edi,PCB_EDI(%edx)
294 movl %gs,PCB_GS(%edx)
296 /* test if debug regisers should be saved */
297 movb PCB_FLAGS(%edx),%al
299 jz 1f /* no, skip over */
300 movl %dr7,%eax /* yes, do the save */
301 movl %eax,PCB_DR7(%edx)
302 andl $0x0000ff00, %eax /* disable all watchpoints */
305 movl %eax,PCB_DR6(%edx)
307 movl %eax,PCB_DR3(%edx)
309 movl %eax,PCB_DR2(%edx)
311 movl %eax,PCB_DR1(%edx)
313 movl %eax,PCB_DR0(%edx)
318 /* XXX FIXME: we should be saving the local APIC TPR */
320 cmpl $FREE_LOCK, %eax /* is it free? */
321 je badsw4 /* yes, bad medicine! */
322 #endif /* DIAGNOSTIC */
323 andl $COUNT_FIELD, %eax /* clear CPU portion */
324 movl %eax, PCB_MPNEST(%edx) /* store it */
328 /* have we used fp, and need a save? */
331 addl $PCB_SAVEFPU,%edx /* h/w bugs make saving complicated */
333 call _npxsave /* do it in a big C function */
336 #endif /* NNPX > 0 */
338 movl $0,_curproc /* out of process */
340 /* save is done, now choose a new process or idle */
345 /* Stop scheduling if smp_active goes zero and we are not BSP */
349 CROSSJUMP(je, _idle, jne) /* wind down */
354 call _chooseproc /* trash ecx, edx, ret eax*/
356 CROSSJUMP(je, _idle, jne) /* if no proc, idle */
360 movl %eax,_want_resched
363 cmpl %eax,P_WCHAN(%ecx)
365 cmpb $SRUN,P_STAT(%ecx)
369 movl P_ADDR(%ecx),%edx
371 #if defined(SWTCH_OPTIM_STATS)
372 incl _swtch_optim_stats
374 /* switch address space */
376 cmpl PCB_CR3(%edx),%ebx
378 #if defined(SWTCH_OPTIM_STATS)
379 decl _swtch_optim_stats
380 incl _tlb_flush_count
382 movl PCB_CR3(%edx),%ebx
391 cmpl $0, PCB_EXT(%edx) /* has pcb extension? */
393 btsl %esi, _private_tss /* mark use of private tss */
394 movl PCB_EXT(%edx), %edi /* new tss descriptor */
398 /* update common_tss.tss_esp0 pointer */
399 movl %edx, %ebx /* pcb */
400 addl $(UPAGES * PAGE_SIZE - 16), %ebx
401 movl %ebx, _common_tss + TSS_ESP0
403 btrl %esi, _private_tss
406 movl $gd_common_tssd, %edi
409 movl $_common_tssd, %edi
412 /* move correct tss descriptor into GDT slot, then reload tr */
413 movl _tss_gdt, %ebx /* entry in GDT */
418 movl $GPROC0_SEL*8, %esi /* GSEL(entry, SEL_KPL) */
421 movl P_VMSPACE(%ecx), %ebx
427 btsl %eax, VM_PMAP+PM_ACTIVE(%ebx)
429 /* restore context */
430 movl PCB_EBX(%edx),%ebx
431 movl PCB_ESP(%edx),%esp
432 movl PCB_EBP(%edx),%ebp
433 movl PCB_ESI(%edx),%esi
434 movl PCB_EDI(%edx),%edi
435 movl PCB_EIP(%edx),%eax
439 #ifdef GRAB_LOPRIO /* hold LOPRIO for INTs */
443 andl $~APIC_TPR_PRIO, lapic_tpr
444 #endif /** CHEAP_TPR */
445 #endif /** GRAB_LOPRIO */
447 movb %al, P_ONCPU(%ecx)
450 movl %ecx, _curproc /* into next process */
453 movl _cpu_lockid, %eax
454 orl PCB_MPNEST(%edx), %eax /* add next count from PROC */
455 movl %eax, _mp_lock /* load the mp_lock */
456 /* XXX FIXME: we should be restoring the local APIC TPR */
460 cmpl $0, PCB_USERLDT(%edx)
462 movl __default_ldt,%eax
463 cmpl _currentldt,%eax
466 movl %eax,_currentldt
474 /* This must be done after loading the user LDT. */
475 .globl cpu_switch_load_gs
477 movl PCB_GS(%edx),%gs
479 /* test if debug regisers should be restored */
480 movb PCB_FLAGS(%edx),%al
482 jz 1f /* no, skip over */
483 movl PCB_DR6(%edx),%eax /* yes, do the restore */
485 movl PCB_DR3(%edx),%eax
487 movl PCB_DR2(%edx),%eax
489 movl PCB_DR1(%edx),%eax
491 movl PCB_DR0(%edx),%eax
493 movl PCB_DR7(%edx),%eax
500 CROSSJUMPTARGET(sw1a)
507 sw0_1: .asciz "cpu_switch: has wchan"
513 sw0_2: .asciz "cpu_switch: not SRUN"
516 #if defined(SMP) && defined(DIAGNOSTIC)
521 sw0_4: .asciz "cpu_switch: do not have lock"
522 #endif /* SMP && DIAGNOSTIC */
526 * Update pcb, saving current processor state.
532 /* caller's return address - child won't execute this routine */
534 movl %eax,PCB_EIP(%ecx)
536 movl %ebx,PCB_EBX(%ecx)
537 movl %esp,PCB_ESP(%ecx)
538 movl %ebp,PCB_EBP(%ecx)
539 movl %esi,PCB_ESI(%ecx)
540 movl %edi,PCB_EDI(%ecx)
541 movl %gs,PCB_GS(%ecx)
545 * If npxproc == NULL, then the npx h/w state is irrelevant and the
546 * state had better already be in the pcb. This is true for forks
547 * but not for dumps (the old book-keeping with FP flags in the pcb
548 * always lost for dumps because the dump pcb has 0 flags).
550 * If npxproc != NULL, then we have to save the npx h/w state to
551 * npxproc's pcb and copy it to the requested pcb, or save to the
552 * requested pcb and reload. Copying is easier because we would
553 * have to handle h/w bugs for reloading. We used to lose the
554 * parent's npx state for forks by forgetting to reload.
561 movl P_ADDR(%eax),%eax
562 leal PCB_SAVEFPU(%eax),%eax
570 pushl $PCB_SAVEFPU_SIZE
571 leal PCB_SAVEFPU(%ecx),%ecx
576 #endif /* NNPX > 0 */