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