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