]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/cpu_switch.S
MFV: file 5.33
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / cpu_switch.S
1 /*-
2  * Copyright (c) 2003 Peter Wemm.
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 #include <machine/asmacros.h>
37 #include <machine/specialreg.h>
38
39 #include "assym.inc"
40 #include "opt_sched.h"
41
42 /*****************************************************************************/
43 /* Scheduling                                                                */
44 /*****************************************************************************/
45
46         .text
47
48 #ifdef SMP
49 #define LK      lock ;
50 #else
51 #define LK
52 #endif
53
54 #if defined(SCHED_ULE) && defined(SMP)
55 #define SETLK   xchgq
56 #else
57 #define SETLK   movq
58 #endif
59
60 /*
61  * cpu_throw()
62  *
63  * This is the second half of cpu_switch(). It is used when the current
64  * thread is either a dummy or slated to die, and we no longer care
65  * about its state.  This is only a slight optimization and is probably
66  * not worth it anymore.  Note that we need to clear the pm_active bits so
67  * we do need the old proc if it still exists.
68  * %rdi = oldtd
69  * %rsi = newtd
70  */
71 ENTRY(cpu_throw)
72         movq    %rsi,%r12
73         movq    %rsi,%rdi
74         call    pmap_activate_sw
75         jmp     sw1
76 END(cpu_throw)
77
78 /*
79  * cpu_switch(old, new, mtx)
80  *
81  * Save the current thread state, then select the next thread to run
82  * and load its state.
83  * %rdi = oldtd
84  * %rsi = newtd
85  * %rdx = mtx
86  */
87 ENTRY(cpu_switch)
88         /* Switch to new thread.  First, save context. */
89         movq    TD_PCB(%rdi),%r8
90
91         movq    (%rsp),%rax                     /* Hardware registers */
92         movq    %r15,PCB_R15(%r8)
93         movq    %r14,PCB_R14(%r8)
94         movq    %r13,PCB_R13(%r8)
95         movq    %r12,PCB_R12(%r8)
96         movq    %rbp,PCB_RBP(%r8)
97         movq    %rsp,PCB_RSP(%r8)
98         movq    %rbx,PCB_RBX(%r8)
99         movq    %rax,PCB_RIP(%r8)
100
101         testl   $PCB_FULL_IRET,PCB_FLAGS(%r8)
102         jnz     2f
103         orl     $PCB_FULL_IRET,PCB_FLAGS(%r8)
104         testl   $TDP_KTHREAD,TD_PFLAGS(%rdi)
105         jnz     2f
106         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
107         jz      2f
108         movl    %fs,%eax
109         cmpl    $KUF32SEL,%eax
110         jne     1f
111         rdfsbase %rax
112         movq    %rax,PCB_FSBASE(%r8)
113 1:      movl    %gs,%eax
114         cmpl    $KUG32SEL,%eax
115         jne     2f
116         movq    %rdx,%r12
117         movl    $MSR_KGSBASE,%ecx               /* Read user gs base */
118         rdmsr
119         shlq    $32,%rdx
120         orq     %rdx,%rax
121         movq    %rax,PCB_GSBASE(%r8)
122         movq    %r12,%rdx
123
124 2:
125         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
126         jnz     store_dr                        /* static predict not taken */
127 done_store_dr:
128
129         /* have we used fp, and need a save? */
130         cmpq    %rdi,PCPU(FPCURTHREAD)
131         jne     3f
132         movq    PCB_SAVEFPU(%r8),%r8
133         clts
134         cmpl    $0,use_xsave
135         jne     1f
136         fxsave  (%r8)
137         jmp     2f
138 1:      movq    %rdx,%rcx
139         movl    xsave_mask,%eax
140         movl    xsave_mask+4,%edx
141         .globl  ctx_switch_xsave
142 ctx_switch_xsave:
143         /* This is patched to xsaveopt if supported, see fpuinit_bsp1() */
144         xsave   (%r8)
145         movq    %rcx,%rdx
146 2:      smsw    %ax
147         orb     $CR0_TS,%al
148         lmsw    %ax
149         xorl    %eax,%eax
150         movq    %rax,PCPU(FPCURTHREAD)
151 3:
152         /* Save is done.  Now fire up new thread. Leave old vmspace. */
153         movq    %rsi,%r12
154         movq    %rdi,%r13
155         movq    %rdx,%r15
156         movq    %rsi,%rdi
157         callq   pmap_activate_sw
158         SETLK   %r15,TD_LOCK(%r13)              /* Release the old thread */
159 sw1:
160         movq    TD_PCB(%r12),%r8
161 #if defined(SCHED_ULE) && defined(SMP)
162         /* Wait for the new thread to become unblocked */
163         movq    $blocked_lock, %rdx
164 1:
165         movq    TD_LOCK(%r12),%rcx
166         cmpq    %rcx, %rdx
167         pause
168         je      1b
169 #endif
170         /*
171          * At this point, we've switched address spaces and are ready
172          * to load up the rest of the next context.
173          */
174
175         /* Skip loading LDT and user fsbase/gsbase for kthreads */
176         testl   $TDP_KTHREAD,TD_PFLAGS(%r12)
177         jnz     do_kthread
178
179         /*
180          * Load ldt register
181          */
182         movq    TD_PROC(%r12),%rcx
183         cmpq    $0, P_MD+MD_LDT(%rcx)
184         jne     do_ldt
185         xorl    %eax,%eax
186 ld_ldt: lldt    %ax
187
188         /* Restore fs base in GDT */
189         movl    PCB_FSBASE(%r8),%eax
190         movq    PCPU(FS32P),%rdx
191         movw    %ax,2(%rdx)
192         shrl    $16,%eax
193         movb    %al,4(%rdx)
194         shrl    $8,%eax
195         movb    %al,7(%rdx)
196
197         /* Restore gs base in GDT */
198         movl    PCB_GSBASE(%r8),%eax
199         movq    PCPU(GS32P),%rdx
200         movw    %ax,2(%rdx)
201         shrl    $16,%eax
202         movb    %al,4(%rdx)
203         shrl    $8,%eax
204         movb    %al,7(%rdx)
205
206 do_kthread:
207         /* Do we need to reload tss ? */
208         movq    PCPU(TSSP),%rax
209         movq    PCB_TSSP(%r8),%rdx
210         testq   %rdx,%rdx
211         cmovzq  PCPU(COMMONTSSP),%rdx
212         cmpq    %rax,%rdx
213         jne     do_tss
214 done_tss:
215         movq    %r8,PCPU(RSP0)
216         movq    %r8,PCPU(CURPCB)
217         /* Update the TSS_RSP0 pointer for the next interrupt */
218         cmpq    $~0,PCPU(UCR3)
219         je      1f
220         movq    PCPU(PTI_RSP0),%rax
221         movq    %rax,TSS_RSP0(%rdx)
222         jmp     2f
223 1:      movq    %r8,TSS_RSP0(%rdx)
224 2:      movq    %r12,PCPU(CURTHREAD)            /* into next thread */
225
226         /* Test if debug registers should be restored. */
227         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
228         jnz     load_dr                         /* static predict not taken */
229 done_load_dr:
230
231         /* Restore context. */
232         movq    PCB_R15(%r8),%r15
233         movq    PCB_R14(%r8),%r14
234         movq    PCB_R13(%r8),%r13
235         movq    PCB_R12(%r8),%r12
236         movq    PCB_RBP(%r8),%rbp
237         movq    PCB_RSP(%r8),%rsp
238         movq    PCB_RBX(%r8),%rbx
239         movq    PCB_RIP(%r8),%rax
240         movq    %rax,(%rsp)
241         ret
242
243         /*
244          * We order these strangely for several reasons.
245          * 1: I wanted to use static branch prediction hints
246          * 2: Most athlon64/opteron cpus don't have them.  They define
247          *    a forward branch as 'predict not taken'.  Intel cores have
248          *    the 'rep' prefix to invert this.
249          * So, to make it work on both forms of cpu we do the detour.
250          * We use jumps rather than call in order to avoid the stack.
251          */
252
253 store_dr:
254         movq    %dr7,%rax                       /* yes, do the save */
255         movq    %dr0,%r15
256         movq    %dr1,%r14
257         movq    %dr2,%r13
258         movq    %dr3,%r12
259         movq    %dr6,%r11
260         movq    %r15,PCB_DR0(%r8)
261         movq    %r14,PCB_DR1(%r8)
262         movq    %r13,PCB_DR2(%r8)
263         movq    %r12,PCB_DR3(%r8)
264         movq    %r11,PCB_DR6(%r8)
265         movq    %rax,PCB_DR7(%r8)
266         andq    $0x0000fc00, %rax               /* disable all watchpoints */
267         movq    %rax,%dr7
268         jmp     done_store_dr
269
270 load_dr:
271         movq    %dr7,%rax
272         movq    PCB_DR0(%r8),%r15
273         movq    PCB_DR1(%r8),%r14
274         movq    PCB_DR2(%r8),%r13
275         movq    PCB_DR3(%r8),%r12
276         movq    PCB_DR6(%r8),%r11
277         movq    PCB_DR7(%r8),%rcx
278         movq    %r15,%dr0
279         movq    %r14,%dr1
280         /* Preserve reserved bits in %dr7 */
281         andq    $0x0000fc00,%rax
282         andq    $~0x0000fc00,%rcx
283         movq    %r13,%dr2
284         movq    %r12,%dr3
285         orq     %rcx,%rax
286         movq    %r11,%dr6
287         movq    %rax,%dr7
288         jmp     done_load_dr
289
290 do_tss: movq    %rdx,PCPU(TSSP)
291         movq    %rdx,%rcx
292         movq    PCPU(TSS),%rax
293         movw    %cx,2(%rax)
294         shrq    $16,%rcx
295         movb    %cl,4(%rax)
296         shrq    $8,%rcx
297         movb    %cl,7(%rax)
298         shrq    $8,%rcx
299         movl    %ecx,8(%rax)
300         movb    $0x89,5(%rax)   /* unset busy */
301         movl    $TSSSEL,%eax
302         ltr     %ax
303         jmp     done_tss
304
305 do_ldt: movq    PCPU(LDT),%rax
306         movq    P_MD+MD_LDT_SD(%rcx),%rdx
307         movq    %rdx,(%rax)
308         movq    P_MD+MD_LDT_SD+8(%rcx),%rdx
309         movq    %rdx,8(%rax)
310         movl    $LDTSEL,%eax
311         jmp     ld_ldt
312 END(cpu_switch)
313
314 /*
315  * savectx(pcb)
316  * Update pcb, saving current processor state.
317  */
318 ENTRY(savectx)
319         /* Save caller's return address. */
320         movq    (%rsp),%rax
321         movq    %rax,PCB_RIP(%rdi)
322
323         movq    %rbx,PCB_RBX(%rdi)
324         movq    %rsp,PCB_RSP(%rdi)
325         movq    %rbp,PCB_RBP(%rdi)
326         movq    %r12,PCB_R12(%rdi)
327         movq    %r13,PCB_R13(%rdi)
328         movq    %r14,PCB_R14(%rdi)
329         movq    %r15,PCB_R15(%rdi)
330
331         movq    %cr0,%rax
332         movq    %rax,PCB_CR0(%rdi)
333         movq    %cr2,%rax
334         movq    %rax,PCB_CR2(%rdi)
335         movq    %cr3,%rax
336         movq    %rax,PCB_CR3(%rdi)
337         movq    %cr4,%rax
338         movq    %rax,PCB_CR4(%rdi)
339
340         movq    %dr0,%rax
341         movq    %rax,PCB_DR0(%rdi)
342         movq    %dr1,%rax
343         movq    %rax,PCB_DR1(%rdi)
344         movq    %dr2,%rax
345         movq    %rax,PCB_DR2(%rdi)
346         movq    %dr3,%rax
347         movq    %rax,PCB_DR3(%rdi)
348         movq    %dr6,%rax
349         movq    %rax,PCB_DR6(%rdi)
350         movq    %dr7,%rax
351         movq    %rax,PCB_DR7(%rdi)
352
353         movl    $MSR_FSBASE,%ecx
354         rdmsr
355         movl    %eax,PCB_FSBASE(%rdi)
356         movl    %edx,PCB_FSBASE+4(%rdi)
357         movl    $MSR_GSBASE,%ecx
358         rdmsr
359         movl    %eax,PCB_GSBASE(%rdi)
360         movl    %edx,PCB_GSBASE+4(%rdi)
361         movl    $MSR_KGSBASE,%ecx
362         rdmsr
363         movl    %eax,PCB_KGSBASE(%rdi)
364         movl    %edx,PCB_KGSBASE+4(%rdi)
365         movl    $MSR_EFER,%ecx
366         rdmsr
367         movl    %eax,PCB_EFER(%rdi)
368         movl    %edx,PCB_EFER+4(%rdi)
369         movl    $MSR_STAR,%ecx
370         rdmsr
371         movl    %eax,PCB_STAR(%rdi)
372         movl    %edx,PCB_STAR+4(%rdi)
373         movl    $MSR_LSTAR,%ecx
374         rdmsr
375         movl    %eax,PCB_LSTAR(%rdi)
376         movl    %edx,PCB_LSTAR+4(%rdi)
377         movl    $MSR_CSTAR,%ecx
378         rdmsr
379         movl    %eax,PCB_CSTAR(%rdi)
380         movl    %edx,PCB_CSTAR+4(%rdi)
381         movl    $MSR_SF_MASK,%ecx
382         rdmsr
383         movl    %eax,PCB_SFMASK(%rdi)
384         movl    %edx,PCB_SFMASK+4(%rdi)
385
386         sgdt    PCB_GDT(%rdi)
387         sidt    PCB_IDT(%rdi)
388         sldt    PCB_LDT(%rdi)
389         str     PCB_TR(%rdi)
390
391         movl    $1,%eax
392         ret
393 END(savectx)
394
395 /*
396  * resumectx(pcb)
397  * Resuming processor state from pcb.
398  */     
399 ENTRY(resumectx)
400         /* Switch to KPML4phys. */
401         movq    KPML4phys,%rax
402         movq    %rax,%cr3
403
404         /* Force kernel segment registers. */
405         movl    $KDSEL,%eax
406         movw    %ax,%ds
407         movw    %ax,%es
408         movw    %ax,%ss
409         movl    $KUF32SEL,%eax
410         movw    %ax,%fs
411         movl    $KUG32SEL,%eax
412         movw    %ax,%gs
413
414         movl    $MSR_FSBASE,%ecx
415         movl    PCB_FSBASE(%rdi),%eax
416         movl    4 + PCB_FSBASE(%rdi),%edx
417         wrmsr
418         movl    $MSR_GSBASE,%ecx
419         movl    PCB_GSBASE(%rdi),%eax
420         movl    4 + PCB_GSBASE(%rdi),%edx
421         wrmsr
422         movl    $MSR_KGSBASE,%ecx
423         movl    PCB_KGSBASE(%rdi),%eax
424         movl    4 + PCB_KGSBASE(%rdi),%edx
425         wrmsr
426
427         /* Restore EFER one more time. */
428         movl    $MSR_EFER,%ecx
429         movl    PCB_EFER(%rdi),%eax
430         wrmsr
431
432         /* Restore fast syscall stuff. */
433         movl    $MSR_STAR,%ecx
434         movl    PCB_STAR(%rdi),%eax
435         movl    4 + PCB_STAR(%rdi),%edx
436         wrmsr
437         movl    $MSR_LSTAR,%ecx
438         movl    PCB_LSTAR(%rdi),%eax
439         movl    4 + PCB_LSTAR(%rdi),%edx
440         wrmsr
441         movl    $MSR_CSTAR,%ecx
442         movl    PCB_CSTAR(%rdi),%eax
443         movl    4 + PCB_CSTAR(%rdi),%edx
444         wrmsr
445         movl    $MSR_SF_MASK,%ecx
446         movl    PCB_SFMASK(%rdi),%eax
447         wrmsr
448
449         /* Restore CR0, CR2, CR4 and CR3. */
450         movq    PCB_CR0(%rdi),%rax
451         movq    %rax,%cr0
452         movq    PCB_CR2(%rdi),%rax
453         movq    %rax,%cr2
454         movq    PCB_CR4(%rdi),%rax
455         movq    %rax,%cr4
456         movq    PCB_CR3(%rdi),%rax
457         movq    %rax,%cr3
458
459         /* Restore descriptor tables. */
460         lidt    PCB_IDT(%rdi)
461         lldt    PCB_LDT(%rdi)
462
463 #define SDT_SYSTSS      9
464 #define SDT_SYSBSY      11
465
466         /* Clear "task busy" bit and reload TR. */
467         movq    PCPU(TSS),%rax
468         andb    $(~SDT_SYSBSY | SDT_SYSTSS),5(%rax)
469         movw    PCB_TR(%rdi),%ax
470         ltr     %ax
471
472 #undef  SDT_SYSTSS
473 #undef  SDT_SYSBSY
474
475         /* Restore debug registers. */
476         movq    PCB_DR0(%rdi),%rax
477         movq    %rax,%dr0
478         movq    PCB_DR1(%rdi),%rax
479         movq    %rax,%dr1
480         movq    PCB_DR2(%rdi),%rax
481         movq    %rax,%dr2
482         movq    PCB_DR3(%rdi),%rax
483         movq    %rax,%dr3
484         movq    PCB_DR6(%rdi),%rax
485         movq    %rax,%dr6
486         movq    PCB_DR7(%rdi),%rax
487         movq    %rax,%dr7
488
489         /* Restore other callee saved registers. */
490         movq    PCB_R15(%rdi),%r15
491         movq    PCB_R14(%rdi),%r14
492         movq    PCB_R13(%rdi),%r13
493         movq    PCB_R12(%rdi),%r12
494         movq    PCB_RBP(%rdi),%rbp
495         movq    PCB_RSP(%rdi),%rsp
496         movq    PCB_RBX(%rdi),%rbx
497
498         /* Restore return address. */
499         movq    PCB_RIP(%rdi),%rax
500         movq    %rax,(%rsp)
501
502         xorl    %eax,%eax
503         ret
504 END(resumectx)