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