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