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