2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
36 #include <machine/asmacros.h>
37 #include <machine/specialreg.h>
40 #include "opt_sched.h"
42 /*****************************************************************************/
44 /*****************************************************************************/
51 * This is the second half of cpu_switch(). It is used when the current
52 * thread is either a dummy or slated to die, and we no longer care
53 * about its state. This is only a slight optimization and is probably
54 * not worth it anymore. Note that we need to clear the pm_active bits so
55 * we do need the old proc if it still exists.
67 * cpu_switch(old, new, mtx)
69 * Save the current thread state, then select the next thread to run
76 /* Switch to new thread. First, save context. */
77 leaq TD_MD_PCB(%rdi),%r8
79 movq (%rsp),%rax /* Hardware registers */
80 movq %r15,PCB_R15(%r8)
81 movq %r14,PCB_R14(%r8)
82 movq %r13,PCB_R13(%r8)
83 movq %r12,PCB_R12(%r8)
84 movq %rbp,PCB_RBP(%r8)
85 movq %rsp,PCB_RSP(%r8)
86 movq %rbx,PCB_RBX(%r8)
87 movq %rax,PCB_RIP(%r8)
89 testl $PCB_FULL_IRET,PCB_FLAGS(%r8)
91 orl $PCB_FULL_IRET,PCB_FLAGS(%r8)
92 testl $TDP_KTHREAD,TD_PFLAGS(%rdi)
94 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
100 movq %rax,PCB_FSBASE(%r8)
105 movl $MSR_KGSBASE,%ecx /* Read user gs base */
109 movq %rax,PCB_GSBASE(%r8)
113 testl $PCB_DBREGS,PCB_FLAGS(%r8)
114 jnz store_dr /* static predict not taken */
117 /* have we used fp, and need a save? */
118 cmpq %rdi,PCPU(FPCURTHREAD)
120 movq PCB_SAVEFPU(%r8),%r8
122 cmpl $0,use_xsave(%rip)
128 movl xsave_mask+4,%edx
129 .globl ctx_switch_xsave
131 /* This is patched to xsaveopt if supported, see fpuinit_bsp1() */
135 /* Save is done. Now fire up new thread. Leave old vmspace. */
140 callq pmap_activate_sw
141 movq %r15,TD_LOCK(%r13) /* Release the old thread */
143 leaq TD_MD_PCB(%r12),%r8
144 #if defined(SCHED_ULE) && defined(SMP)
145 movq $blocked_lock, %rdx
146 movq TD_LOCK(%r12),%rcx
152 * At this point, we've switched address spaces and are ready
153 * to load up the rest of the next context.
156 /* Skip loading LDT and user fsbase/gsbase for kthreads */
157 testl $TDP_KTHREAD,TD_PFLAGS(%r12)
163 movq TD_PROC(%r12),%rcx
164 cmpq $0, P_MD+MD_LDT(%rcx)
169 /* Restore fs base in GDT */
170 movl PCB_FSBASE(%r8),%eax
171 movq PCPU(FS32P),%rdx
178 /* Restore gs base in GDT */
179 movl PCB_GSBASE(%r8),%eax
180 movq PCPU(GS32P),%rdx
188 /* Do we need to reload tss ? */
190 movq PCB_TSSP(%r8),%rdx
192 cmovzq PCPU(COMMONTSSP),%rdx
196 movq TD_MD_STACK_BASE(%r12),%r9
198 movq %r8,PCPU(CURPCB)
199 movq PCPU(PTI_RSP0),%rax
202 movq %rax,TSS_RSP0(%rdx)
203 movq %r12,PCPU(CURTHREAD) /* into next thread */
205 /* Test if debug registers should be restored. */
206 testl $PCB_DBREGS,PCB_FLAGS(%r8)
207 jnz load_dr /* static predict not taken */
210 /* Restore context. */
211 movq PCB_R15(%r8),%r15
212 movq PCB_R14(%r8),%r14
213 movq PCB_R13(%r8),%r13
214 movq PCB_R12(%r8),%r12
215 movq PCB_RBP(%r8),%rbp
216 movq PCB_RSP(%r8),%rsp
217 movq PCB_RBX(%r8),%rbx
218 movq PCB_RIP(%r8),%rax
220 movq PCPU(CURTHREAD),%rdi
225 * We order these strangely for several reasons.
226 * 1: I wanted to use static branch prediction hints
227 * 2: Most athlon64/opteron cpus don't have them. They define
228 * a forward branch as 'predict not taken'. Intel cores have
229 * the 'rep' prefix to invert this.
230 * So, to make it work on both forms of cpu we do the detour.
231 * We use jumps rather than call in order to avoid the stack.
235 movq %dr7,%rax /* yes, do the save */
241 movq %r15,PCB_DR0(%r8)
242 movq %r14,PCB_DR1(%r8)
243 movq %r13,PCB_DR2(%r8)
244 movq %r12,PCB_DR3(%r8)
245 movq %r11,PCB_DR6(%r8)
246 movq %rax,PCB_DR7(%r8)
247 andq $0x0000fc00, %rax /* disable all watchpoints */
253 movq PCB_DR0(%r8),%r15
254 movq PCB_DR1(%r8),%r14
255 movq PCB_DR2(%r8),%r13
256 movq PCB_DR3(%r8),%r12
257 movq PCB_DR6(%r8),%r11
258 movq PCB_DR7(%r8),%rcx
261 /* Preserve reserved bits in %dr7 */
262 andq $0x0000fc00,%rax
263 andq $~0x0000fc00,%rcx
271 do_tss: movq %rdx,PCPU(TSSP)
281 movb $0x89,5(%rax) /* unset busy */
286 do_ldt: movq PCPU(LDT),%rax
287 movq P_MD+MD_LDT_SD(%rcx),%rdx
289 movq P_MD+MD_LDT_SD+8(%rcx),%rdx
297 * Update pcb, saving current processor state.
300 /* Save caller's return address. */
302 movq %rax,PCB_RIP(%rdi)
304 movq %rbx,PCB_RBX(%rdi)
305 movq %rsp,PCB_RSP(%rdi)
306 movq %rbp,PCB_RBP(%rdi)
307 movq %r12,PCB_R12(%rdi)
308 movq %r13,PCB_R13(%rdi)
309 movq %r14,PCB_R14(%rdi)
310 movq %r15,PCB_R15(%rdi)
313 movq %rax,PCB_CR0(%rdi)
315 movq %rax,PCB_CR2(%rdi)
317 movq %rax,PCB_CR3(%rdi)
319 movq %rax,PCB_CR4(%rdi)
322 movq %rax,PCB_DR0(%rdi)
324 movq %rax,PCB_DR1(%rdi)
326 movq %rax,PCB_DR2(%rdi)
328 movq %rax,PCB_DR3(%rdi)
330 movq %rax,PCB_DR6(%rdi)
332 movq %rax,PCB_DR7(%rdi)
334 movl $MSR_FSBASE,%ecx
336 movl %eax,PCB_FSBASE(%rdi)
337 movl %edx,PCB_FSBASE+4(%rdi)
338 movl $MSR_GSBASE,%ecx
340 movl %eax,PCB_GSBASE(%rdi)
341 movl %edx,PCB_GSBASE+4(%rdi)
342 movl $MSR_KGSBASE,%ecx
344 movl %eax,PCB_KGSBASE(%rdi)
345 movl %edx,PCB_KGSBASE+4(%rdi)
348 movl %eax,PCB_EFER(%rdi)
349 movl %edx,PCB_EFER+4(%rdi)
352 movl %eax,PCB_STAR(%rdi)
353 movl %edx,PCB_STAR+4(%rdi)
356 movl %eax,PCB_LSTAR(%rdi)
357 movl %edx,PCB_LSTAR+4(%rdi)
360 movl %eax,PCB_CSTAR(%rdi)
361 movl %edx,PCB_CSTAR+4(%rdi)
362 movl $MSR_SF_MASK,%ecx
364 movl %eax,PCB_SFMASK(%rdi)
365 movl %edx,PCB_SFMASK+4(%rdi)
378 * Resuming processor state from pcb.
381 /* Switch to KPML4phys. */
385 /* Force kernel segment registers. */
395 movl $MSR_FSBASE,%ecx
396 movl PCB_FSBASE(%rdi),%eax
397 movl 4 + PCB_FSBASE(%rdi),%edx
399 movl $MSR_GSBASE,%ecx
400 movl PCB_GSBASE(%rdi),%eax
401 movl 4 + PCB_GSBASE(%rdi),%edx
403 movl $MSR_KGSBASE,%ecx
404 movl PCB_KGSBASE(%rdi),%eax
405 movl 4 + PCB_KGSBASE(%rdi),%edx
408 /* Restore EFER one more time. */
410 movl PCB_EFER(%rdi),%eax
413 /* Restore fast syscall stuff. */
415 movl PCB_STAR(%rdi),%eax
416 movl 4 + PCB_STAR(%rdi),%edx
419 movl PCB_LSTAR(%rdi),%eax
420 movl 4 + PCB_LSTAR(%rdi),%edx
423 movl PCB_CSTAR(%rdi),%eax
424 movl 4 + PCB_CSTAR(%rdi),%edx
426 movl $MSR_SF_MASK,%ecx
427 movl PCB_SFMASK(%rdi),%eax
430 /* Restore CR0, CR2, CR4 and CR3. */
431 movq PCB_CR0(%rdi),%rax
433 movq PCB_CR2(%rdi),%rax
435 movq PCB_CR4(%rdi),%rax
437 movq PCB_CR3(%rdi),%rax
440 /* Restore descriptor tables. */
445 #define SDT_SYSBSY 11
447 /* Clear "task busy" bit and reload TR. */
449 andb $(~SDT_SYSBSY | SDT_SYSTSS),5(%rax)
450 movw PCB_TR(%rdi),%ax
456 /* Restore debug registers. */
457 movq PCB_DR0(%rdi),%rax
459 movq PCB_DR1(%rdi),%rax
461 movq PCB_DR2(%rdi),%rax
463 movq PCB_DR3(%rdi),%rax
465 movq PCB_DR6(%rdi),%rax
467 movq PCB_DR7(%rdi),%rax
470 /* Restore other callee saved registers. */
471 movq PCB_R15(%rdi),%r15
472 movq PCB_R14(%rdi),%r14
473 movq PCB_R13(%rdi),%r13
474 movq PCB_R12(%rdi),%r12
475 movq PCB_RBP(%rdi),%rbp
476 movq PCB_RSP(%rdi),%rsp
477 movq PCB_RBX(%rdi),%rbx
479 /* Restore return address. */
480 movq PCB_RIP(%rdi),%rax
487 /* Wait for the new thread to become unblocked */
488 #if defined(SCHED_ULE) && defined(SMP)
492 movq TD_LOCK(%r12),%rcx