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