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 * 4. 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 /*****************************************************************************/
54 #if defined(SCHED_ULE) && defined(SMP)
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.
79 * cpu_switch(old, new, mtx)
81 * Save the current thread state, then select the next thread to run
88 /* Switch to new thread. First, save context. */
90 orl $PCB_FULL_IRET,PCB_FLAGS(%r8)
92 movq (%rsp),%rax /* Hardware registers */
93 movq %r15,PCB_R15(%r8)
94 movq %r14,PCB_R14(%r8)
95 movq %r13,PCB_R13(%r8)
96 movq %r12,PCB_R12(%r8)
97 movq %rbp,PCB_RBP(%r8)
98 movq %rsp,PCB_RSP(%r8)
99 movq %rbx,PCB_RBX(%r8)
100 movq %rax,PCB_RIP(%r8)
102 testl $PCB_DBREGS,PCB_FLAGS(%r8)
103 jnz store_dr /* static predict not taken */
106 /* have we used fp, and need a save? */
107 cmpq %rdi,PCPU(FPCURTHREAD)
109 movq PCB_SAVEFPU(%r8),%r8
117 movl xsave_mask+4,%edx
118 .globl ctx_switch_xsave
120 /* This is patched to xsaveopt if supported, see fpuinit_bsp1() */
127 movq %rax,PCPU(FPCURTHREAD)
129 /* Save is done. Now fire up new thread. Leave old vmspace. */
134 callq pmap_activate_sw
135 SETLK %r15,TD_LOCK(%r13) /* Release the old thread */
137 movq TD_PCB(%r12),%r8
138 #if defined(SCHED_ULE) && defined(SMP)
139 /* Wait for the new thread to become unblocked */
140 movq $blocked_lock, %rdx
142 movq TD_LOCK(%r12),%rcx
148 * At this point, we've switched address spaces and are ready
149 * to load up the rest of the next context.
152 /* Skip loading user fsbase/gsbase for kthreads */
153 testl $TDP_KTHREAD,TD_PFLAGS(%r12)
159 movq TD_PROC(%r12),%rcx
160 cmpq $0, P_MD+MD_LDT(%rcx)
165 /* Restore fs base in GDT */
166 movl PCB_FSBASE(%r8),%eax
167 movq PCPU(FS32P),%rdx
174 /* Restore gs base in GDT */
175 movl PCB_GSBASE(%r8),%eax
176 movq PCPU(GS32P),%rdx
184 /* Do we need to reload tss ? */
186 movq PCB_TSSP(%r8),%rdx
188 cmovzq PCPU(COMMONTSSP),%rdx
193 movq %r8,PCPU(CURPCB)
194 /* Update the TSS_RSP0 pointer for the next interrupt */
195 movq %r8,COMMON_TSS_RSP0(%rdx)
196 movq %r12,PCPU(CURTHREAD) /* into next thread */
198 /* Test if debug registers should be restored. */
199 testl $PCB_DBREGS,PCB_FLAGS(%r8)
200 jnz load_dr /* static predict not taken */
203 /* Restore context. */
204 movq PCB_R15(%r8),%r15
205 movq PCB_R14(%r8),%r14
206 movq PCB_R13(%r8),%r13
207 movq PCB_R12(%r8),%r12
208 movq PCB_RBP(%r8),%rbp
209 movq PCB_RSP(%r8),%rsp
210 movq PCB_RBX(%r8),%rbx
211 movq PCB_RIP(%r8),%rax
216 * We order these strangely for several reasons.
217 * 1: I wanted to use static branch prediction hints
218 * 2: Most athlon64/opteron cpus don't have them. They define
219 * a forward branch as 'predict not taken'. Intel cores have
220 * the 'rep' prefix to invert this.
221 * So, to make it work on both forms of cpu we do the detour.
222 * We use jumps rather than call in order to avoid the stack.
226 movq %dr7,%rax /* yes, do the save */
232 movq %r15,PCB_DR0(%r8)
233 movq %r14,PCB_DR1(%r8)
234 movq %r13,PCB_DR2(%r8)
235 movq %r12,PCB_DR3(%r8)
236 movq %r11,PCB_DR6(%r8)
237 movq %rax,PCB_DR7(%r8)
238 andq $0x0000fc00, %rax /* disable all watchpoints */
244 movq PCB_DR0(%r8),%r15
245 movq PCB_DR1(%r8),%r14
246 movq PCB_DR2(%r8),%r13
247 movq PCB_DR3(%r8),%r12
248 movq PCB_DR6(%r8),%r11
249 movq PCB_DR7(%r8),%rcx
252 /* Preserve reserved bits in %dr7 */
253 andq $0x0000fc00,%rax
254 andq $~0x0000fc00,%rcx
262 do_tss: movq %rdx,PCPU(TSSP)
272 movb $0x89,5(%rax) /* unset busy */
277 do_ldt: movq PCPU(LDT),%rax
278 movq P_MD+MD_LDT_SD(%rcx),%rdx
280 movq P_MD+MD_LDT_SD+8(%rcx),%rdx
288 * Update pcb, saving current processor state.
291 /* Save caller's return address. */
293 movq %rax,PCB_RIP(%rdi)
295 movq %rbx,PCB_RBX(%rdi)
296 movq %rsp,PCB_RSP(%rdi)
297 movq %rbp,PCB_RBP(%rdi)
298 movq %r12,PCB_R12(%rdi)
299 movq %r13,PCB_R13(%rdi)
300 movq %r14,PCB_R14(%rdi)
301 movq %r15,PCB_R15(%rdi)
304 movq %rax,PCB_CR0(%rdi)
306 movq %rax,PCB_CR2(%rdi)
308 movq %rax,PCB_CR3(%rdi)
310 movq %rax,PCB_CR4(%rdi)
313 movq %rax,PCB_DR0(%rdi)
315 movq %rax,PCB_DR1(%rdi)
317 movq %rax,PCB_DR2(%rdi)
319 movq %rax,PCB_DR3(%rdi)
321 movq %rax,PCB_DR6(%rdi)
323 movq %rax,PCB_DR7(%rdi)
325 movl $MSR_FSBASE,%ecx
327 movl %eax,PCB_FSBASE(%rdi)
328 movl %edx,PCB_FSBASE+4(%rdi)
329 movl $MSR_GSBASE,%ecx
331 movl %eax,PCB_GSBASE(%rdi)
332 movl %edx,PCB_GSBASE+4(%rdi)
333 movl $MSR_KGSBASE,%ecx
335 movl %eax,PCB_KGSBASE(%rdi)
336 movl %edx,PCB_KGSBASE+4(%rdi)
339 movl %eax,PCB_EFER(%rdi)
340 movl %edx,PCB_EFER+4(%rdi)
343 movl %eax,PCB_STAR(%rdi)
344 movl %edx,PCB_STAR+4(%rdi)
347 movl %eax,PCB_LSTAR(%rdi)
348 movl %edx,PCB_LSTAR+4(%rdi)
351 movl %eax,PCB_CSTAR(%rdi)
352 movl %edx,PCB_CSTAR+4(%rdi)
353 movl $MSR_SF_MASK,%ecx
355 movl %eax,PCB_SFMASK(%rdi)
356 movl %edx,PCB_SFMASK+4(%rdi)
369 * Resuming processor state from pcb.
372 /* Switch to KPML4phys. */
376 /* Force kernel segment registers. */
386 movl $MSR_FSBASE,%ecx
387 movl PCB_FSBASE(%rdi),%eax
388 movl 4 + PCB_FSBASE(%rdi),%edx
390 movl $MSR_GSBASE,%ecx
391 movl PCB_GSBASE(%rdi),%eax
392 movl 4 + PCB_GSBASE(%rdi),%edx
394 movl $MSR_KGSBASE,%ecx
395 movl PCB_KGSBASE(%rdi),%eax
396 movl 4 + PCB_KGSBASE(%rdi),%edx
401 movl PCB_EFER(%rdi),%eax
404 /* Restore fast syscall stuff. */
406 movl PCB_STAR(%rdi),%eax
407 movl 4 + PCB_STAR(%rdi),%edx
410 movl PCB_LSTAR(%rdi),%eax
411 movl 4 + PCB_LSTAR(%rdi),%edx
414 movl PCB_CSTAR(%rdi),%eax
415 movl 4 + PCB_CSTAR(%rdi),%edx
417 movl $MSR_SF_MASK,%ecx
418 movl PCB_SFMASK(%rdi),%eax
421 /* Restore CR0, CR2, CR4 and CR3. */
422 movq PCB_CR0(%rdi),%rax
424 movq PCB_CR2(%rdi),%rax
426 movq PCB_CR4(%rdi),%rax
428 movq PCB_CR3(%rdi),%rax
431 /* Restore descriptor tables. */
436 #define SDT_SYSBSY 11
438 /* Clear "task busy" bit and reload TR. */
440 andb $(~SDT_SYSBSY | SDT_SYSTSS),5(%rax)
441 movw PCB_TR(%rdi),%ax
447 /* Restore debug registers. */
448 movq PCB_DR0(%rdi),%rax
450 movq PCB_DR1(%rdi),%rax
452 movq PCB_DR2(%rdi),%rax
454 movq PCB_DR3(%rdi),%rax
456 movq PCB_DR6(%rdi),%rax
458 movq PCB_DR7(%rdi),%rax
461 /* Restore other callee saved registers. */
462 movq PCB_R15(%rdi),%r15
463 movq PCB_R14(%rdi),%r14
464 movq PCB_R13(%rdi),%r13
465 movq PCB_R12(%rdi),%r12
466 movq PCB_RBP(%rdi),%rbp
467 movq PCB_RSP(%rdi),%rsp
468 movq PCB_RBX(%rdi),%rbx
470 /* Restore return address. */
471 movq PCB_RIP(%rdi),%rax