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