Merge from vendor branch LIBSTDC++:
[dragonfly.git] / sys / i386 / i386 / swtch.s
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  * LWKT threads Copyright (c) 2003 Matthew Dillon
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD: src/sys/i386/i386/swtch.s,v 1.89.2.10 2003/01/23 03:36:24 ps Exp $
38  * $DragonFly: src/sys/i386/i386/Attic/swtch.s,v 1.29 2003/12/20 05:52:26 dillon Exp $
39  */
40
41 #include "use_npx.h"
42
43 #include <sys/rtprio.h>
44
45 #include <machine/asmacros.h>
46 #include <machine/ipl.h>
47
48 #ifdef SMP
49 #include <machine/pmap.h>
50 #include <machine/smptests.h>           /** GRAB_LOPRIO */
51 #include <machine/apic.h>
52 #include <machine/lock.h>
53 #endif /* SMP */
54
55 #include "assym.s"
56
57 #if defined(SMP)
58 #define MPLOCKED        lock ;
59 #else
60 #define MPLOCKED
61 #endif
62
63         .data
64
65         .globl  panic
66
67 #if defined(SWTCH_OPTIM_STATS)
68         .globl  swtch_optim_stats, tlb_flush_count
69 swtch_optim_stats:      .long   0               /* number of _swtch_optims */
70 tlb_flush_count:        .long   0
71 #endif
72
73         .text
74
75
76 /*
77  * cpu_heavy_switch(next_thread)
78  *
79  *      Switch from the current thread to a new thread.  This entry
80  *      is normally called via the thread->td_switch function, and will
81  *      only be called when the current thread is a heavy weight process.
82  *
83  *      Some instructions have been reordered to reduce pipeline stalls.
84  *
85  *      YYY disable interrupts once giant is removed.
86  */
87 ENTRY(cpu_heavy_switch)
88         /*
89          * Save general regs
90          */
91         movl    PCPU(curthread),%ecx
92         movl    (%esp),%eax                     /* (reorder optimization) */
93         movl    TD_PCB(%ecx),%edx               /* EDX = PCB */
94         movl    %eax,PCB_EIP(%edx)              /* return PC may be modified */
95         movl    %ebx,PCB_EBX(%edx)
96         movl    %esp,PCB_ESP(%edx)
97         movl    %ebp,PCB_EBP(%edx)
98         movl    %esi,PCB_ESI(%edx)
99         movl    %edi,PCB_EDI(%edx)
100         movl    %gs,PCB_GS(%edx)
101
102         movl    %ecx,%ebx                       /* EBX = curthread */
103         movl    TD_PROC(%ecx),%ecx
104         movl    PCPU(cpuid), %eax
105         movl    P_VMSPACE(%ecx), %ecx           /* ECX = vmspace */
106         MPLOCKED btrl   %eax, VM_PMAP+PM_ACTIVE(%ecx)
107
108         /*
109          * Push the LWKT switch restore function, which resumes a heavy
110          * weight process.  Note that the LWKT switcher is based on
111          * TD_SP, while the heavy weight process switcher is based on
112          * PCB_ESP.  TD_SP is usually two ints pushed relative to
113          * PCB_ESP.  We push the flags for later restore by cpu_heavy_restore.
114          */
115         pushfl
116         pushl   $cpu_heavy_restore
117         movl    %esp,TD_SP(%ebx)
118
119         /*
120          * Save debug regs if necessary
121          */
122         movb    PCB_FLAGS(%edx),%al
123         andb    $PCB_DBREGS,%al
124         jz      1f                              /* no, skip over */
125         movl    %dr7,%eax                       /* yes, do the save */
126         movl    %eax,PCB_DR7(%edx)
127         andl    $0x0000fc00, %eax               /* disable all watchpoints */
128         movl    %eax,%dr7
129         movl    %dr6,%eax
130         movl    %eax,PCB_DR6(%edx)
131         movl    %dr3,%eax
132         movl    %eax,PCB_DR3(%edx)
133         movl    %dr2,%eax
134         movl    %eax,PCB_DR2(%edx)
135         movl    %dr1,%eax
136         movl    %eax,PCB_DR1(%edx)
137         movl    %dr0,%eax
138         movl    %eax,PCB_DR0(%edx)
139 1:
140  
141         /*
142          * Save the FP state if we have used the FP.  Note that calling
143          * npxsave will NULL out PCPU(npxthread).
144          */
145 #if NNPX > 0
146         cmpl    %ebx,PCPU(npxthread)
147         jne     1f
148         addl    $PCB_SAVEFPU,%edx
149         pushl   %edx
150         call    npxsave                 /* do it in a big C function */
151         addl    $4,%esp                 /* EAX, ECX, EDX trashed */
152 1:
153 #endif  /* NNPX > 0 */
154
155         /*
156          * Switch to the next thread, which was passed as an argument
157          * to cpu_heavy_switch().  Due to the eflags and switch-restore
158          * function we pushed, the argument is at 12(%esp).  Set the current
159          * thread, load the stack pointer, and 'ret' into the switch-restore
160          * function.
161          *
162          * The switch restore function expects the new thread to be in %eax
163          * and the old one to be in %ebx.
164          *
165          * There is a one-instruction window where curthread is the new
166          * thread but %esp still points to the old thread's stack, but
167          * we are protected by a critical section so it is ok.
168          */
169         movl    12(%esp),%eax           /* EAX = newtd, EBX = oldtd */
170         movl    %eax,PCPU(curthread)
171         movl    TD_SP(%eax),%esp
172         ret
173
174 /*
175  *  cpu_exit_switch()
176  *
177  *      The switch function is changed to this when a thread is going away
178  *      for good.  We have to ensure that the MMU state is not cached, and
179  *      we don't bother saving the existing thread state before switching.
180  *
181  *      At this point we are in a critical section and this cpu owns the
182  *      thread's token, which serves as an interlock until the switchout is
183  *      complete.
184  */
185 ENTRY(cpu_exit_switch)
186         /*
187          * Get us out of the vmspace
188          */
189         movl    IdlePTD,%ecx
190         movl    %cr3,%eax
191         cmpl    %ecx,%eax
192         je      1f
193         movl    %ecx,%cr3
194 1:
195         movl    PCPU(curthread),%ebx
196         /*
197          * Switch to the next thread.  RET into the restore function, which
198          * expects the new thread in EAX and the old in EBX.
199          *
200          * There is a one-instruction window where curthread is the new
201          * thread but %esp still points to the old thread's stack, but
202          * we are protected by a critical section so it is ok.
203          */
204         movl    4(%esp),%eax
205         movl    %eax,PCPU(curthread)
206         movl    TD_SP(%eax),%esp
207         ret
208
209 /*
210  * cpu_heavy_restore()  (current thread in %eax on entry)
211  *
212  *      Restore the thread after an LWKT switch.  This entry is normally
213  *      called via the LWKT switch restore function, which was pulled 
214  *      off the thread stack and jumped to.
215  *
216  *      This entry is only called if the thread was previously saved
217  *      using cpu_heavy_switch() (the heavy weight process thread switcher),
218  *      or when a new process is initially scheduled.  The first thing we
219  *      do is clear the TDF_RUNNING bit in the old thread and set it in the
220  *      new thread.
221  *
222  *      YYY theoretically we do not have to restore everything here, a lot
223  *      of this junk can wait until we return to usermode.  But for now
224  *      we restore everything.
225  *
226  *      YYY the PCB crap is really crap, it makes startup a bitch because
227  *      we can't switch away.
228  *
229  *      YYY note: spl check is done in mi_switch when it splx()'s.
230  */
231
232 ENTRY(cpu_heavy_restore)
233         popfl
234         movl    TD_PCB(%eax),%edx               /* EDX = PCB */
235         movl    TD_PROC(%eax),%ecx
236 #ifdef  DIAGNOSTIC
237         /*
238          * A heavy weight process will normally be in an SRUN state
239          * but can also be preempted while it is entering a SZOMB
240          * (zombie) state.
241          */
242         cmpb    $SRUN,P_STAT(%ecx)
243         je      1f
244         cmpb    $SZOMB,P_STAT(%ecx)
245         jne     badsw2
246 1:
247 #endif
248
249 #if defined(SWTCH_OPTIM_STATS)
250         incl    _swtch_optim_stats
251 #endif
252         /*
253          * Tell the pmap that our cpu is using the VMSPACE now.  We cannot
254          * safely test/reload %cr3 until after we have set the bit in the
255          * pmap (remember, we do not hold the MP lock in the switch code).
256          */
257         movl    P_VMSPACE(%ecx), %ecx           /* ECX = vmspace */
258         movl    PCPU(cpuid), %esi
259         MPLOCKED btsl   %esi, VM_PMAP+PM_ACTIVE(%ecx)
260
261         /*
262          * Restore the MMU address space.  If it is the same as the last
263          * thread we don't have to invalidate the tlb (i.e. reload cr3).
264          * YYY which naturally also means that the PM_ACTIVE bit had better
265          * already have been set before we set it above, check? YYY
266          */
267         movl    %cr3,%esi
268         movl    PCB_CR3(%edx),%ecx
269         cmpl    %esi,%ecx
270         je      4f
271 #if defined(SWTCH_OPTIM_STATS)
272         decl    _swtch_optim_stats
273         incl    _tlb_flush_count
274 #endif
275         movl    %ecx,%cr3
276 4:
277         /*
278          * Clear TDF_RUNNING flag in old thread only after cleaning up
279          * %cr3.  The target thread is already protected by being TDF_RUNQ
280          * so setting TDF_RUNNING isn't as big a deal.
281          */
282         andl    $~TDF_RUNNING,TD_FLAGS(%ebx)
283         orl     $TDF_RUNNING,TD_FLAGS(%eax)
284
285         /*
286          * Deal with the PCB extension, restore the private tss
287          */
288         movl    PCB_EXT(%edx),%edi      /* check for a PCB extension */
289         movl    $1,%ebx                 /* maybe mark use of a private tss */
290         testl   %edi,%edi
291         jnz     2f
292
293         /*
294          * Going back to the common_tss.  We may need to update TSS_ESP0
295          * which sets the top of the supervisor stack when entering from
296          * usermode.  The PCB is at the top of the stack but we need another
297          * 16 bytes to take vm86 into account.
298          */
299         leal    -16(%edx),%ebx
300         movl    %ebx, PCPU(common_tss) + TSS_ESP0
301
302         cmpl    $0,PCPU(private_tss)    /* don't have to reload if      */
303         je      3f                      /* already using the common TSS */
304
305         subl    %ebx,%ebx               /* unmark use of private tss */
306
307         /*
308          * Get the address of the common TSS descriptor for the ltr.
309          * There is no way to get the address of a segment-accessed variable
310          * so we store a self-referential pointer at the base of the per-cpu
311          * data area and add the appropriate offset.
312          */
313         movl    $gd_common_tssd, %edi
314         addl    %fs:0, %edi
315
316         /*
317          * Move the correct TSS descriptor into the GDT slot, then reload
318          * ltr.
319          */
320 2:
321         movl    %ebx,PCPU(private_tss)          /* mark/unmark private tss */
322         movl    PCPU(tss_gdt), %ebx             /* entry in GDT */
323         movl    0(%edi), %eax
324         movl    %eax, 0(%ebx)
325         movl    4(%edi), %eax
326         movl    %eax, 4(%ebx)
327         movl    $GPROC0_SEL*8, %esi             /* GSEL(entry, SEL_KPL) */
328         ltr     %si
329
330 3:
331         /*
332          * Restore general registers.
333          */
334         movl    PCB_EBX(%edx),%ebx
335         movl    PCB_ESP(%edx),%esp
336         movl    PCB_EBP(%edx),%ebp
337         movl    PCB_ESI(%edx),%esi
338         movl    PCB_EDI(%edx),%edi
339         movl    PCB_EIP(%edx),%eax
340         movl    %eax,(%esp)
341
342         /*
343          * Restore the user LDT if we have one
344          */
345         cmpl    $0, PCB_USERLDT(%edx)
346         jnz     1f
347         movl    _default_ldt,%eax
348         cmpl    PCPU(currentldt),%eax
349         je      2f
350         lldt    _default_ldt
351         movl    %eax,PCPU(currentldt)
352         jmp     2f
353 1:      pushl   %edx
354         call    set_user_ldt
355         popl    %edx
356 2:
357         /*
358          * Restore the %gs segment register, which must be done after
359          * loading the user LDT.  Since user processes can modify the
360          * register via procfs, this may result in a fault which is
361          * detected by checking the fault address against cpu_switch_load_gs
362          * in i386/i386/trap.c
363          */
364         .globl  cpu_switch_load_gs
365 cpu_switch_load_gs:
366         movl    PCB_GS(%edx),%gs
367
368         /*
369          * Restore the DEBUG register state if necessary.
370          */
371         movb    PCB_FLAGS(%edx),%al
372         andb    $PCB_DBREGS,%al
373         jz      1f                              /* no, skip over */
374         movl    PCB_DR6(%edx),%eax              /* yes, do the restore */
375         movl    %eax,%dr6
376         movl    PCB_DR3(%edx),%eax
377         movl    %eax,%dr3
378         movl    PCB_DR2(%edx),%eax
379         movl    %eax,%dr2
380         movl    PCB_DR1(%edx),%eax
381         movl    %eax,%dr1
382         movl    PCB_DR0(%edx),%eax
383         movl    %eax,%dr0
384         movl    %dr7,%eax                /* load dr7 so as not to disturb */
385         andl    $0x0000fc00,%eax         /*   reserved bits               */
386         pushl   %ebx
387         movl    PCB_DR7(%edx),%ebx
388         andl    $~0x0000fc00,%ebx
389         orl     %ebx,%eax
390         popl    %ebx
391         movl    %eax,%dr7
392 1:
393
394         ret
395
396 badsw2:
397         pushl   $sw0_2
398         call    panic
399
400 sw0_2:  .asciz  "cpu_switch: not SRUN"
401
402 /*
403  * savectx(pcb)
404  * Update pcb, saving current processor state.
405  */
406 ENTRY(savectx)
407         /* fetch PCB */
408         movl    4(%esp),%ecx
409
410         /* caller's return address - child won't execute this routine */
411         movl    (%esp),%eax
412         movl    %eax,PCB_EIP(%ecx)
413
414         movl    %cr3,%eax
415         movl    %eax,PCB_CR3(%ecx)
416
417         movl    %ebx,PCB_EBX(%ecx)
418         movl    %esp,PCB_ESP(%ecx)
419         movl    %ebp,PCB_EBP(%ecx)
420         movl    %esi,PCB_ESI(%ecx)
421         movl    %edi,PCB_EDI(%ecx)
422         movl    %gs,PCB_GS(%ecx)
423
424 #if NNPX > 0
425         /*
426          * If npxthread == NULL, then the npx h/w state is irrelevant and the
427          * state had better already be in the pcb.  This is true for forks
428          * but not for dumps (the old book-keeping with FP flags in the pcb
429          * always lost for dumps because the dump pcb has 0 flags).
430          *
431          * If npxthread != NULL, then we have to save the npx h/w state to
432          * npxthread's pcb and copy it to the requested pcb, or save to the
433          * requested pcb and reload.  Copying is easier because we would
434          * have to handle h/w bugs for reloading.  We used to lose the
435          * parent's npx state for forks by forgetting to reload.
436          */
437         movl    PCPU(npxthread),%eax
438         testl   %eax,%eax
439         je      1f
440
441         pushl   %ecx
442         movl    TD_PCB(%eax),%eax
443         leal    PCB_SAVEFPU(%eax),%eax
444         pushl   %eax
445         pushl   %eax
446         call    npxsave
447         addl    $4,%esp
448         popl    %eax
449         popl    %ecx
450
451         pushl   $PCB_SAVEFPU_SIZE
452         leal    PCB_SAVEFPU(%ecx),%ecx
453         pushl   %ecx
454         pushl   %eax
455         call    bcopy
456         addl    $12,%esp
457 #endif  /* NNPX > 0 */
458
459 1:
460         ret
461
462 /*
463  * cpu_idle_restore()   (current thread in %eax on entry) (one-time execution)
464  *
465  *      Don't bother setting up any regs other then %ebp so backtraces
466  *      don't die.  This restore function is used to bootstrap into the
467  *      cpu_idle() LWKT only, after that cpu_lwkt_*() will be used for
468  *      switching.
469  *
470  *      Clear TDF_RUNNING in old thread only after we've cleaned up %cr3.
471  *
472  *      If we are an AP we have to call ap_init() before jumping to
473  *      cpu_idle().  ap_init() will synchronize with the BP and finish
474  *      setting up various ncpu-dependant globaldata fields.  This may
475  *      happen on UP as well as SMP if we happen to be simulating multiple
476  *      cpus.
477  */
478 ENTRY(cpu_idle_restore)
479         /* cli */
480         movl    IdlePTD,%ecx
481         movl    $0,%ebp
482         pushl   $0
483         movl    %ecx,%cr3
484         andl    $~TDF_RUNNING,TD_FLAGS(%ebx)
485         orl     $TDF_RUNNING,TD_FLAGS(%eax)
486 #ifdef SMP
487         cmpl    $0,PCPU(cpuid)
488         je      1f
489         call    ap_init
490 1:
491 #endif
492         sti
493         jmp     cpu_idle
494
495 /*
496  * cpu_kthread_restore() (current thread is %eax on entry) (one-time execution)
497  *
498  *      Don't bother setting up any regs other then %ebp so backtraces
499  *      don't die.  This restore function is used to bootstrap into an
500  *      LWKT based kernel thread only.  cpu_lwkt_switch() will be used
501  *      after this.
502  *
503  *      Since all of our context is on the stack we are reentrant and
504  *      we can release our critical section and enable interrupts early.
505  */
506 ENTRY(cpu_kthread_restore)
507         sti
508         movl    IdlePTD,%ecx
509         movl    TD_PCB(%eax),%edx
510         movl    $0,%ebp
511         movl    %ecx,%cr3
512         andl    $~TDF_RUNNING,TD_FLAGS(%ebx)
513         orl     $TDF_RUNNING,TD_FLAGS(%eax)
514         subl    $TDPRI_CRIT,TD_PRI(%eax)
515         popl    %eax            /* kthread exit function */
516         pushl   PCB_EBX(%edx)   /* argument to ESI function */
517         pushl   %eax            /* set exit func as return address */
518         movl    PCB_ESI(%edx),%eax
519         jmp     *%eax
520
521 /*
522  * cpu_lwkt_switch()
523  *
524  *      Standard LWKT switching function.  Only non-scratch registers are
525  *      saved and we don't bother with the MMU state or anything else.
526  *
527  *      This function is always called while in a critical section.
528  *
529  *      There is a one-instruction window where curthread is the new
530  *      thread but %esp still points to the old thread's stack, but
531  *      we are protected by a critical section so it is ok.
532  *
533  *      YYY BGL, SPL
534  */
535 ENTRY(cpu_lwkt_switch)
536         movl    4(%esp),%eax
537         pushl   %ebp
538         pushl   %ebx
539         pushl   %esi
540         pushl   %edi
541         pushfl
542         movl    PCPU(curthread),%ebx
543         pushl   $cpu_lwkt_restore
544         movl    %esp,TD_SP(%ebx)
545         movl    %eax,PCPU(curthread)
546         movl    TD_SP(%eax),%esp
547
548         /*
549          * eax contains new thread, ebx contains old thread.
550          */
551         ret
552
553 /*
554  * cpu_lwkt_restore()   (current thread in %eax on entry)
555  *
556  *      Standard LWKT restore function.  This function is always called
557  *      while in a critical section.
558  *      
559  *      Warning: due to preemption the restore function can be used to 
560  *      'return' to the original thread.  Interrupt disablement must be
561  *      protected through the switch so we cannot run splz here.
562  *
563  *      YYY we theoretically do not need to load IdlePTD into cr3, but if
564  *      so we need a way to detect when the PTD we are using is being 
565  *      deleted due to a process exiting.
566  */
567 ENTRY(cpu_lwkt_restore)
568         movl    IdlePTD,%ecx    /* YYY borrow but beware desched/cpuchg/exit */
569         movl    %cr3,%edx
570         cmpl    %ecx,%edx
571         je      1f
572         movl    %ecx,%cr3
573 1:
574         andl    $~TDF_RUNNING,TD_FLAGS(%ebx)
575         orl     $TDF_RUNNING,TD_FLAGS(%eax)
576         popfl
577         popl    %edi
578         popl    %esi
579         popl    %ebx
580         popl    %ebp
581         ret
582