]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/swtch.s
MFV r282150
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / swtch.s
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #include "opt_npx.h"
36 #include "opt_sched.h"
37
38 #include <machine/asmacros.h>
39
40 #include "assym.s"
41
42 #if defined(SMP) && defined(SCHED_ULE)
43 #define SETOP           xchgl
44 #define BLOCK_SPIN(reg)                                                 \
45                 movl            $blocked_lock,%eax ;                    \
46         100: ;                                                          \
47                 lock ;                                                  \
48                 cmpxchgl        %eax,TD_LOCK(reg) ;                     \
49                 jne             101f ;                                  \
50                 pause ;                                                 \
51                 jmp             100b ;                                  \
52         101:
53 #else
54 #define SETOP           movl
55 #define BLOCK_SPIN(reg)
56 #endif
57
58 /*****************************************************************************/
59 /* Scheduling                                                                */
60 /*****************************************************************************/
61
62         .text
63
64 /*
65  * cpu_throw()
66  *
67  * This is the second half of cpu_switch(). It is used when the current
68  * thread is either a dummy or slated to die, and we no longer care
69  * about its state.  This is only a slight optimization and is probably
70  * not worth it anymore.  Note that we need to clear the pm_active bits so
71  * we do need the old proc if it still exists.
72  * 0(%esp) = ret
73  * 4(%esp) = oldtd
74  * 8(%esp) = newtd
75  */
76 ENTRY(cpu_throw)
77         movl    PCPU(CPUID), %esi
78         movl    4(%esp),%ecx                    /* Old thread */
79         testl   %ecx,%ecx                       /* no thread? */
80         jz      1f
81         /* release bit from old pm_active */
82         movl    PCPU(CURPMAP), %ebx
83 #ifdef SMP
84         lock
85 #endif
86         btrl    %esi, PM_ACTIVE(%ebx)           /* clear old */
87 1:
88         movl    8(%esp),%ecx                    /* New thread */
89         movl    TD_PCB(%ecx),%edx
90         movl    PCB_CR3(%edx),%eax
91         LOAD_CR3(%eax)
92         /* set bit in new pm_active */
93         movl    TD_PROC(%ecx),%eax
94         movl    P_VMSPACE(%eax), %ebx
95         addl    $VM_PMAP, %ebx
96         movl    %ebx, PCPU(CURPMAP)
97 #ifdef SMP
98         lock
99 #endif
100         btsl    %esi, PM_ACTIVE(%ebx)           /* set new */
101         jmp     sw1
102 END(cpu_throw)
103
104 /*
105  * cpu_switch(old, new)
106  *
107  * Save the current thread state, then select the next thread to run
108  * and load its state.
109  * 0(%esp) = ret
110  * 4(%esp) = oldtd
111  * 8(%esp) = newtd
112  * 12(%esp) = newlock
113  */
114 ENTRY(cpu_switch)
115
116         /* Switch to new thread.  First, save context. */
117         movl    4(%esp),%ecx
118
119 #ifdef INVARIANTS
120         testl   %ecx,%ecx                       /* no thread? */
121         jz      badsw2                          /* no, panic */
122 #endif
123
124         movl    TD_PCB(%ecx),%edx
125
126         movl    (%esp),%eax                     /* Hardware registers */
127         movl    %eax,PCB_EIP(%edx)
128         movl    %ebx,PCB_EBX(%edx)
129         movl    %esp,PCB_ESP(%edx)
130         movl    %ebp,PCB_EBP(%edx)
131         movl    %esi,PCB_ESI(%edx)
132         movl    %edi,PCB_EDI(%edx)
133         mov     %gs,PCB_GS(%edx)
134         pushfl                                  /* PSL */
135         popl    PCB_PSL(%edx)
136         /* Test if debug registers should be saved. */
137         testl   $PCB_DBREGS,PCB_FLAGS(%edx)
138         jz      1f                              /* no, skip over */
139         movl    %dr7,%eax                       /* yes, do the save */
140         movl    %eax,PCB_DR7(%edx)
141         andl    $0x0000fc00, %eax               /* disable all watchpoints */
142         movl    %eax,%dr7
143         movl    %dr6,%eax
144         movl    %eax,PCB_DR6(%edx)
145         movl    %dr3,%eax
146         movl    %eax,PCB_DR3(%edx)
147         movl    %dr2,%eax
148         movl    %eax,PCB_DR2(%edx)
149         movl    %dr1,%eax
150         movl    %eax,PCB_DR1(%edx)
151         movl    %dr0,%eax
152         movl    %eax,PCB_DR0(%edx)
153 1:
154
155 #ifdef DEV_NPX
156         /* have we used fp, and need a save? */
157         cmpl    %ecx,PCPU(FPCURTHREAD)
158         jne     1f
159         pushl   PCB_SAVEFPU(%edx)               /* h/w bugs make saving complicated */
160         call    npxsave                         /* do it in a big C function */
161         popl    %eax
162 1:
163 #endif
164
165         /* Save is done.  Now fire up new thread. Leave old vmspace. */
166         movl    4(%esp),%edi
167         movl    8(%esp),%ecx                    /* New thread */
168         movl    12(%esp),%esi                   /* New lock */
169 #ifdef INVARIANTS
170         testl   %ecx,%ecx                       /* no thread? */
171         jz      badsw3                          /* no, panic */
172 #endif
173         movl    TD_PCB(%ecx),%edx
174
175         /* switch address space */
176         movl    PCB_CR3(%edx),%eax
177         READ_CR3(%ebx)                          /* The same address space? */
178         cmpl    %ebx,%eax
179         je      sw0
180         LOAD_CR3(%eax)                          /* new address space */
181         movl    %esi,%eax
182         movl    PCPU(CPUID),%esi
183         SETOP   %eax,TD_LOCK(%edi)              /* Switchout td_lock */
184
185         /* Release bit from old pmap->pm_active */
186         movl    PCPU(CURPMAP), %ebx
187 #ifdef SMP
188         lock
189 #endif
190         btrl    %esi, PM_ACTIVE(%ebx)           /* clear old */
191
192         /* Set bit in new pmap->pm_active */
193         movl    TD_PROC(%ecx),%eax              /* newproc */
194         movl    P_VMSPACE(%eax), %ebx
195         addl    $VM_PMAP, %ebx
196         movl    %ebx, PCPU(CURPMAP)
197 #ifdef SMP
198         lock
199 #endif
200         btsl    %esi, PM_ACTIVE(%ebx)           /* set new */
201         jmp     sw1
202
203 sw0:
204         SETOP   %esi,TD_LOCK(%edi)              /* Switchout td_lock */
205 sw1:
206         BLOCK_SPIN(%ecx)
207 #ifdef XEN
208         pushl   %eax
209         pushl   %ecx
210         pushl   %edx
211         call    xen_handle_thread_switch
212         popl    %edx
213         popl    %ecx
214         popl    %eax
215         /*
216          * XXX set IOPL
217          */
218 #else           
219         /*
220          * At this point, we've switched address spaces and are ready
221          * to load up the rest of the next context.
222          */
223         cmpl    $0, PCB_EXT(%edx)               /* has pcb extension? */
224         je      1f                              /* If not, use the default */
225         movl    $1, PCPU(PRIVATE_TSS)           /* mark use of private tss */
226         movl    PCB_EXT(%edx), %edi             /* new tss descriptor */
227         jmp     2f                              /* Load it up */
228
229 1:      /*
230          * Use the common default TSS instead of our own.
231          * Set our stack pointer into the TSS, it's set to just
232          * below the PCB.  In C, common_tss.tss_esp0 = &pcb - 16;
233          */
234         leal    -16(%edx), %ebx                 /* leave space for vm86 */
235         movl    %ebx, PCPU(COMMON_TSS) + TSS_ESP0
236
237         /*
238          * Test this CPU's  bit in the bitmap to see if this
239          * CPU was using a private TSS.
240          */
241         cmpl    $0, PCPU(PRIVATE_TSS)           /* Already using the common? */
242         je      3f                              /* if so, skip reloading */
243         movl    $0, PCPU(PRIVATE_TSS)
244         PCPU_ADDR(COMMON_TSSD, %edi)
245 2:
246         /* Move correct tss descriptor into GDT slot, then reload tr. */
247         movl    PCPU(TSS_GDT), %ebx             /* entry in GDT */
248         movl    0(%edi), %eax
249         movl    4(%edi), %esi
250         movl    %eax, 0(%ebx)
251         movl    %esi, 4(%ebx)
252         movl    $GPROC0_SEL*8, %esi             /* GSEL(GPROC0_SEL, SEL_KPL) */
253         ltr     %si
254 3:
255
256         /* Copy the %fs and %gs selectors into this pcpu gdt */
257         leal    PCB_FSD(%edx), %esi
258         movl    PCPU(FSGS_GDT), %edi
259         movl    0(%esi), %eax           /* %fs selector */
260         movl    4(%esi), %ebx
261         movl    %eax, 0(%edi)
262         movl    %ebx, 4(%edi)
263         movl    8(%esi), %eax           /* %gs selector, comes straight after */
264         movl    12(%esi), %ebx
265         movl    %eax, 8(%edi)
266         movl    %ebx, 12(%edi)
267 #endif
268         /* Restore context. */
269         movl    PCB_EBX(%edx),%ebx
270         movl    PCB_ESP(%edx),%esp
271         movl    PCB_EBP(%edx),%ebp
272         movl    PCB_ESI(%edx),%esi
273         movl    PCB_EDI(%edx),%edi
274         movl    PCB_EIP(%edx),%eax
275         movl    %eax,(%esp)
276         pushl   PCB_PSL(%edx)
277         popfl
278
279         movl    %edx, PCPU(CURPCB)
280         movl    TD_TID(%ecx),%eax
281         movl    %ecx, PCPU(CURTHREAD)           /* into next thread */
282
283         /*
284          * Determine the LDT to use and load it if is the default one and
285          * that is not the current one.
286          */
287         movl    TD_PROC(%ecx),%eax
288         cmpl    $0,P_MD+MD_LDT(%eax)
289         jnz     1f
290         movl    _default_ldt,%eax
291         cmpl    PCPU(CURRENTLDT),%eax
292         je      2f
293         LLDT(_default_ldt)
294         movl    %eax,PCPU(CURRENTLDT)
295         jmp     2f
296 1:
297         /* Load the LDT when it is not the default one. */
298         pushl   %edx                            /* Preserve pointer to pcb. */
299         addl    $P_MD,%eax                      /* Pointer to mdproc is arg. */
300         pushl   %eax
301         call    set_user_ldt
302         addl    $4,%esp
303         popl    %edx
304 2:
305
306         /* This must be done after loading the user LDT. */
307         .globl  cpu_switch_load_gs
308 cpu_switch_load_gs:
309         mov     PCB_GS(%edx),%gs
310
311         /* Test if debug registers should be restored. */
312         testl   $PCB_DBREGS,PCB_FLAGS(%edx)
313         jz      1f
314
315         /*
316          * Restore debug registers.  The special code for dr7 is to
317          * preserve the current values of its reserved bits.
318          */
319         movl    PCB_DR6(%edx),%eax
320         movl    %eax,%dr6
321         movl    PCB_DR3(%edx),%eax
322         movl    %eax,%dr3
323         movl    PCB_DR2(%edx),%eax
324         movl    %eax,%dr2
325         movl    PCB_DR1(%edx),%eax
326         movl    %eax,%dr1
327         movl    PCB_DR0(%edx),%eax
328         movl    %eax,%dr0
329         movl    %dr7,%eax
330         andl    $0x0000fc00,%eax
331         movl    PCB_DR7(%edx),%ecx
332         andl    $~0x0000fc00,%ecx
333         orl     %ecx,%eax
334         movl    %eax,%dr7
335 1:
336         ret
337
338 #ifdef INVARIANTS
339 badsw1:
340         pushal
341         pushl   $sw0_1
342         call    panic
343 sw0_1:  .asciz  "cpu_throw: no newthread supplied"
344
345 badsw2:
346         pushal
347         pushl   $sw0_2
348         call    panic
349 sw0_2:  .asciz  "cpu_switch: no curthread supplied"
350
351 badsw3:
352         pushal
353         pushl   $sw0_3
354         call    panic
355 sw0_3:  .asciz  "cpu_switch: no newthread supplied"
356 #endif
357 END(cpu_switch)
358
359 /*
360  * savectx(pcb)
361  * Update pcb, saving current processor state.
362  */
363 ENTRY(savectx)
364         /* Fetch PCB. */
365         movl    4(%esp),%ecx
366
367         /* Save caller's return address.  Child won't execute this routine. */
368         movl    (%esp),%eax
369         movl    %eax,PCB_EIP(%ecx)
370
371         movl    %cr3,%eax
372         movl    %eax,PCB_CR3(%ecx)
373
374         movl    %ebx,PCB_EBX(%ecx)
375         movl    %esp,PCB_ESP(%ecx)
376         movl    %ebp,PCB_EBP(%ecx)
377         movl    %esi,PCB_ESI(%ecx)
378         movl    %edi,PCB_EDI(%ecx)
379         mov     %gs,PCB_GS(%ecx)
380         pushfl
381         popl    PCB_PSL(%ecx)
382
383         movl    %cr0,%eax
384         movl    %eax,PCB_CR0(%ecx)
385         movl    %cr2,%eax
386         movl    %eax,PCB_CR2(%ecx)
387         movl    %cr4,%eax
388         movl    %eax,PCB_CR4(%ecx)
389
390         movl    %dr0,%eax
391         movl    %eax,PCB_DR0(%ecx)
392         movl    %dr1,%eax
393         movl    %eax,PCB_DR1(%ecx)
394         movl    %dr2,%eax
395         movl    %eax,PCB_DR2(%ecx)
396         movl    %dr3,%eax
397         movl    %eax,PCB_DR3(%ecx)
398         movl    %dr6,%eax
399         movl    %eax,PCB_DR6(%ecx)
400         movl    %dr7,%eax
401         movl    %eax,PCB_DR7(%ecx)
402
403         mov     %ds,PCB_DS(%ecx)
404         mov     %es,PCB_ES(%ecx)
405         mov     %fs,PCB_FS(%ecx)
406         mov     %ss,PCB_SS(%ecx)
407         
408         sgdt    PCB_GDT(%ecx)
409         sidt    PCB_IDT(%ecx)
410         sldt    PCB_LDT(%ecx)
411         str     PCB_TR(%ecx)
412
413         movl    $1,%eax
414         ret
415 END(savectx)
416
417 /*
418  * resumectx(pcb) __fastcall
419  * Resuming processor state from pcb.
420  */
421 ENTRY(resumectx)
422         /* Restore GDT. */
423         lgdt    PCB_GDT(%ecx)
424
425         /* Restore segment registers */
426         movzwl  PCB_DS(%ecx),%eax
427         mov     %ax,%ds
428         movzwl  PCB_ES(%ecx),%eax
429         mov     %ax,%es
430         movzwl  PCB_FS(%ecx),%eax
431         mov     %ax,%fs
432         movzwl  PCB_GS(%ecx),%eax
433         movw    %ax,%gs
434         movzwl  PCB_SS(%ecx),%eax
435         mov     %ax,%ss
436
437         /* Restore CR2, CR4, CR3 and CR0 */
438         movl    PCB_CR2(%ecx),%eax
439         movl    %eax,%cr2
440         movl    PCB_CR4(%ecx),%eax
441         movl    %eax,%cr4
442         movl    PCB_CR3(%ecx),%eax
443         movl    %eax,%cr3
444         movl    PCB_CR0(%ecx),%eax
445         movl    %eax,%cr0
446         jmp     1f
447 1:
448
449         /* Restore descriptor tables */
450         lidt    PCB_IDT(%ecx)
451         lldt    PCB_LDT(%ecx)
452
453 #define SDT_SYS386TSS   9
454 #define SDT_SYS386BSY   11
455         /* Clear "task busy" bit and reload TR */
456         movl    PCPU(TSS_GDT),%eax
457         andb    $(~SDT_SYS386BSY | SDT_SYS386TSS),5(%eax)
458         movzwl  PCB_TR(%ecx),%eax
459         ltr     %ax
460 #undef SDT_SYS386TSS
461 #undef SDT_SYS386BSY
462
463         /* Restore debug registers */
464         movl    PCB_DR0(%ecx),%eax
465         movl    %eax,%dr0
466         movl    PCB_DR1(%ecx),%eax
467         movl    %eax,%dr1
468         movl    PCB_DR2(%ecx),%eax
469         movl    %eax,%dr2
470         movl    PCB_DR3(%ecx),%eax
471         movl    %eax,%dr3
472         movl    PCB_DR6(%ecx),%eax
473         movl    %eax,%dr6
474         movl    PCB_DR7(%ecx),%eax
475         movl    %eax,%dr7
476
477         /* Restore other registers */
478         movl    PCB_EDI(%ecx),%edi
479         movl    PCB_ESI(%ecx),%esi
480         movl    PCB_EBP(%ecx),%ebp
481         movl    PCB_ESP(%ecx),%esp
482         movl    PCB_EBX(%ecx),%ebx
483
484         /* reload code selector by turning return into intersegmental return */
485         pushl   PCB_EIP(%ecx)
486         movl    $KCSEL,4(%esp)
487         xorl    %eax,%eax
488         lret
489 END(resumectx)