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 34#include <machine/asmacros.h> 35#include <machine/specialreg.h> 36 37#include "assym.inc" 38 39/*****************************************************************************/ 40/* Scheduling */ 41/*****************************************************************************/ 42 43 .text 44 45/* 46 * cpu_throw() 47 * 48 * This is the second half of cpu_switch(). It is used when the current 49 * thread is either a dummy or slated to die, and we no longer care 50 * about its state. This is only a slight optimization and is probably 51 * not worth it anymore. Note that we need to clear the pm_active bits so 52 * we do need the old proc if it still exists. 53 * %rdi = oldtd 54 * %rsi = newtd 55 */ 56ENTRY(cpu_throw) 57 movq %rsi,%r12 58 movq %rsi,%rdi 59 call pmap_activate_sw 60 jmp sw1 61END(cpu_throw) 62 63/* 64 * cpu_switch(old, new, mtx) 65 * 66 * Save the current thread state, then select the next thread to run 67 * and load its state. 68 * %rdi = oldtd 69 * %rsi = newtd 70 * %rdx = mtx 71 */ 72ENTRY(cpu_switch) 73 /* Switch to new thread. First, save context. */ 74 leaq TD_MD_PCB(%rdi),%r8 75 76 movq (%rsp),%rax /* Hardware registers */ 77 movq %r15,PCB_R15(%r8) 78 movq %r14,PCB_R14(%r8) 79 movq %r13,PCB_R13(%r8) 80 movq %r12,PCB_R12(%r8) 81 movq %rbp,PCB_RBP(%r8) 82 movq %rsp,PCB_RSP(%r8) 83 movq %rbx,PCB_RBX(%r8) 84 movq %rax,PCB_RIP(%r8) 85 86 testl $PCB_FULL_IRET,PCB_FLAGS(%r8) 87 jnz 2f 88 orl $PCB_FULL_IRET,PCB_FLAGS(%r8) 89 testl $TDP_KTHREAD,TD_PFLAGS(%rdi) 90 jnz 2f 91 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip) 92 jz 2f 93 rdfsbase %rax 94 movq %rax,PCB_FSBASE(%r8) 95 movq %rdx,%r12 96 movl $MSR_KGSBASE,%ecx /* Read user gs base */ 97 rdmsr 98 shlq $32,%rdx 99 orq %rdx,%rax 100 movq %rax,PCB_GSBASE(%r8) 101 movq %r12,%rdx 102 1032: 104 testl $PCB_DBREGS,PCB_FLAGS(%r8) 105 jnz store_dr /* static predict not taken */ 106done_store_dr: 107 108 /* have we used fp, and need a save? */ 109 cmpq %rdi,PCPU(FPCURTHREAD) 110 jne ctx_switch_fpusave_done 111 movq PCB_SAVEFPU(%r8),%r9 112 clts 113 cmpl $0,use_xsave(%rip) 114 jne 1f 115 fxsave (%r9) 116 jmp ctx_switch_fpusave_done 1171: movq %rdx,%rcx 118 movl xsave_mask,%eax 119 movl xsave_mask+4,%edx 120 testl $PCB_32BIT,PCB_FLAGS(%r8) 121 jne ctx_switch_xsave32 122 .globl ctx_switch_xsave 123ctx_switch_xsave: 124 /* This is patched to xsaveopt if supported, see fpuinit_bsp1() */ 125 xsave64 (%r9) 126ctx_switch_xsave_done: 127 movq %rcx,%rdx 128ctx_switch_fpusave_done: 129 /* Save is done. Now fire up new thread. Leave old vmspace. */ 130 movq %rsi,%r12 131 movq %rdi,%r13 132 movq %rdx,%r15 133 movq %rsi,%rdi 134 callq pmap_activate_sw 135 movq %r15,TD_LOCK(%r13) /* Release the old thread */ 136sw1: 137 leaq TD_MD_PCB(%r12),%r8 138 movq $blocked_lock, %rdx 139 movq TD_LOCK(%r12),%rcx 140 cmpq %rcx, %rdx 141 je sw1wait 142sw1cont: 143 /* 144 * At this point, we've switched address spaces and are ready 145 * to load up the rest of the next context. 146 */ 147 148 /* Skip loading LDT and user fsbase/gsbase for kthreads */ 149 testl $TDP_KTHREAD,TD_PFLAGS(%r12) 150 jnz do_kthread 151 152 /* 153 * Load ldt register 154 */ 155 movq TD_PROC(%r12),%rcx 156 cmpq $0, P_MD+MD_LDT(%rcx) 157 jne do_ldt 158 xorl %eax,%eax 159ld_ldt: lldt %ax 160 161 /* Restore fs base in GDT */ 162 movl PCB_FSBASE(%r8),%eax 163 movq PCPU(FS32P),%rdx 164 movw %ax,2(%rdx) 165 shrl $16,%eax 166 movb %al,4(%rdx) 167 shrl $8,%eax 168 movb %al,7(%rdx) 169 170 /* Restore gs base in GDT */ 171 movl PCB_GSBASE(%r8),%eax 172 movq PCPU(GS32P),%rdx 173 movw %ax,2(%rdx) 174 shrl $16,%eax 175 movb %al,4(%rdx) 176 shrl $8,%eax 177 movb %al,7(%rdx) 178 179do_kthread: 180 /* Do we need to reload tss ? */ 181 movq PCPU(TSSP),%rax 182 movq PCB_TSSP(%r8),%rdx 183 movq PCPU(PRVSPACE),%r13 184 addq $PC_COMMONTSS,%r13 185 testq %rdx,%rdx 186 cmovzq %r13,%rdx 187 cmpq %rax,%rdx 188 jne do_tss 189done_tss: 190 movq %r8,PCPU(CURPCB) 191 cmpb $0,fred 192 jne 1f 193 movq TD_MD_STACK_BASE(%r12),%r9 194 movq %r9,PCPU(RSP0) 195 movq PCPU(PTI_RSP0),%rax 196 cmpq $~0,PCPU(UCR3) 197 cmove %r9,%rax 198 movq %rax,TSS_RSP0(%rdx) 199 jmp 2f 2001: movq TD_MD_STACK_BASE(%r12),%rdx 201 movl %edx,%eax 202 shrq $32,%rdx 203 movl $MSR_FRED_RSP0,%ecx 204 wrmsr 2052: movq %r12,PCPU(CURTHREAD) /* into next thread */ 206 207 /* Test if debug registers should be restored. */ 208 testl $PCB_DBREGS,PCB_FLAGS(%r8) 209 jnz load_dr /* static predict not taken */ 210done_load_dr: 211 212 /* Restore context. */ 213 movq PCB_R15(%r8),%r15 214 movq PCB_R14(%r8),%r14 215 movq PCB_R13(%r8),%r13 216 movq PCB_R12(%r8),%r12 217 movq PCB_RBP(%r8),%rbp 218 movq PCB_RSP(%r8),%rsp 219 movq PCB_RBX(%r8),%rbx 220 movq PCB_RIP(%r8),%rax 221 movq %rax,(%rsp) 222 movq PCPU(CURTHREAD),%rdi 223 call fpu_activate_sw 224 cmpb $0,cpu_flush_rsb_ctxsw(%rip) 225 jne rsb_flush 226 ret 227 228 /* 229 * We order these strangely for several reasons. 230 * 1: I wanted to use static branch prediction hints 231 * 2: Most athlon64/opteron cpus don't have them. They define 232 * a forward branch as 'predict not taken'. Intel cores have 233 * the 'rep' prefix to invert this. 234 * So, to make it work on both forms of cpu we do the detour. 235 * We use jumps rather than call in order to avoid the stack. 236 */ 237 238store_dr: 239 movq %dr7,%rax /* yes, do the save */ 240 movq %dr0,%r15 241 movq %dr1,%r14 242 movq %dr2,%r13 243 movq %dr3,%r12 244 movq %dr6,%r11 245 movq %r15,PCB_DR0(%r8) 246 movq %r14,PCB_DR1(%r8) 247 movq %r13,PCB_DR2(%r8) 248 movq %r12,PCB_DR3(%r8) 249 movq %r11,PCB_DR6(%r8) 250 movq %rax,PCB_DR7(%r8) 251 andq $0x0000fc00, %rax /* disable all watchpoints */ 252 movq %rax,%dr7 253 jmp done_store_dr 254 255load_dr: 256 movq %dr7,%rax 257 movq PCB_DR0(%r8),%r15 258 movq PCB_DR1(%r8),%r14 259 movq PCB_DR2(%r8),%r13 260 movq PCB_DR3(%r8),%r12 261 movq PCB_DR6(%r8),%r11 262 movq PCB_DR7(%r8),%rcx 263 movq %r15,%dr0 264 movq %r14,%dr1 265 /* Preserve reserved bits in %dr7 */ 266 andq $0x0000fc00,%rax 267 andq $~0x0000fc00,%rcx 268 movq %r13,%dr2 269 movq %r12,%dr3 270 orq %rcx,%rax 271 movq %r11,%dr6 272 movq %rax,%dr7 273 jmp done_load_dr 274 275do_tss: movq %rdx,PCPU(TSSP) 276 movq %rdx,%rcx 277 movq PCPU(TSS),%rax 278 movw %cx,2(%rax) 279 shrq $16,%rcx 280 movb %cl,4(%rax) 281 shrq $8,%rcx 282 movb %cl,7(%rax) 283 shrq $8,%rcx 284 movl %ecx,8(%rax) 285 movb $0x89,5(%rax) /* unset busy */ 286 movl $TSSSEL,%eax 287 ltr %ax 288 jmp done_tss 289 290do_ldt: movq PCPU(LDT),%rax 291 movq P_MD+MD_LDT_SD(%rcx),%rdx 292 movq %rdx,(%rax) 293 movq P_MD+MD_LDT_SD+8(%rcx),%rdx 294 movq %rdx,8(%rax) 295 movl $LDTSEL,%eax 296 jmp ld_ldt 297 298 .globl ctx_switch_xsave32 299ctx_switch_xsave32: 300 xsave (%r9) 301 jmp ctx_switch_xsave_done 302END(cpu_switch) 303 304/* 305 * savectx(pcb) 306 * Update pcb, saving current processor state. 307 */ 308ENTRY(savectx) 309 /* Save caller's return address. */ 310 movq (%rsp),%rax 311 movq %rax,PCB_RIP(%rdi) 312 313 movq %rbx,PCB_RBX(%rdi) 314 movq %rsp,PCB_RSP(%rdi) 315 movq %rbp,PCB_RBP(%rdi) 316 movq %r12,PCB_R12(%rdi) 317 movq %r13,PCB_R13(%rdi) 318 movq %r14,PCB_R14(%rdi) 319 movq %r15,PCB_R15(%rdi) 320 321 movq %cr0,%rax 322 movq %rax,PCB_CR0(%rdi) 323 movq %cr2,%rax 324 movq %rax,PCB_CR2(%rdi) 325 movq %cr3,%rax 326 movq %rax,PCB_CR3(%rdi) 327 movq %cr4,%rax 328 movq %rax,PCB_CR4(%rdi) 329 330 movq %dr0,%rax 331 movq %rax,PCB_DR0(%rdi) 332 movq %dr1,%rax 333 movq %rax,PCB_DR1(%rdi) 334 movq %dr2,%rax 335 movq %rax,PCB_DR2(%rdi) 336 movq %dr3,%rax 337 movq %rax,PCB_DR3(%rdi) 338 movq %dr6,%rax 339 movq %rax,PCB_DR6(%rdi) 340 movq %dr7,%rax 341 movq %rax,PCB_DR7(%rdi) 342 343 movl $MSR_FSBASE,%ecx 344 rdmsr 345 movl %eax,PCB_FSBASE(%rdi) 346 movl %edx,PCB_FSBASE+4(%rdi) 347 movl $MSR_GSBASE,%ecx 348 rdmsr 349 movl %eax,PCB_GSBASE(%rdi) 350 movl %edx,PCB_GSBASE+4(%rdi) 351 movl $MSR_KGSBASE,%ecx 352 rdmsr 353 movl %eax,PCB_KGSBASE(%rdi) 354 movl %edx,PCB_KGSBASE+4(%rdi) 355 movl $MSR_EFER,%ecx 356 rdmsr 357 movl %eax,PCB_EFER(%rdi) 358 movl %edx,PCB_EFER+4(%rdi) 359 movl $MSR_STAR,%ecx 360 rdmsr 361 movl %eax,PCB_STAR(%rdi) 362 movl %edx,PCB_STAR+4(%rdi) 363 movl $MSR_LSTAR,%ecx 364 rdmsr 365 movl %eax,PCB_LSTAR(%rdi) 366 movl %edx,PCB_LSTAR+4(%rdi) 367 movl $MSR_CSTAR,%ecx 368 rdmsr 369 movl %eax,PCB_CSTAR(%rdi) 370 movl %edx,PCB_CSTAR+4(%rdi) 371 movl $MSR_SF_MASK,%ecx 372 rdmsr 373 movl %eax,PCB_SFMASK(%rdi) 374 movl %edx,PCB_SFMASK+4(%rdi) 375 376 cmpb $0, fred 377 je 1f 378 movl $MSR_FRED_RSP0,%ecx 379 rdmsr 380 movl %eax,PCB_FRED_RSP0(%rdi) 381 movl %edx,PCB_FRED_RSP0+4(%rdi) 382 movl $MSR_FRED_RSP1,%ecx 383 rdmsr 384 movl %eax,PCB_FRED_RSP1(%rdi) 385 movl %edx,PCB_FRED_RSP1+4(%rdi) 386 movl $MSR_FRED_RSP2,%ecx 387 rdmsr 388 movl %eax,PCB_FRED_RSP2(%rdi) 389 movl %edx,PCB_FRED_RSP2+4(%rdi) 390 movl $MSR_FRED_RSP3,%ecx 391 rdmsr 392 movl %eax,PCB_FRED_RSP3(%rdi) 393 movl %edx,PCB_FRED_RSP3+4(%rdi) 394 movl $MSR_FRED_STKLVLS,%ecx 395 rdmsr 396 movl %eax,PCB_FRED_STKLVLS(%rdi) 397 movl %edx,PCB_FRED_STKLVLS+4(%rdi) 398 movl $MSR_FRED_CONFIG,%ecx 399 rdmsr 400 movl %eax,PCB_FRED_CONFIG(%rdi) 401 movl %edx,PCB_FRED_CONFIG+4(%rdi) 402 jmp 2f 403 4041: sidt PCB_IDT(%rdi) 4052: sgdt PCB_GDT(%rdi) 406 sldt PCB_LDT(%rdi) 407 str PCB_TR(%rdi) 408 409 movl $1,%eax 410 ret 411END(savectx) 412 413/* 414 * resumectx(pcb) 415 * Resuming processor state from pcb. 416 */ 417ENTRY(resumectx) 418 /* Switch to KPML5/4phys. */ 419 movq KPML4phys,%rax 420 movq KPML5phys,%rcx 421 cmpl $0, la57 422 cmovne %rcx, %rax 423 movq %rax,%cr3 424 425 /* Force kernel segment registers. */ 426 movl $KDSEL,%eax 427 movw %ax,%ds 428 movw %ax,%es 429 movw %ax,%ss 430 movl $KUF32SEL,%eax 431 movw %ax,%fs 432 movl $KUG32SEL,%eax 433 movw %ax,%gs 434 435 movl $MSR_FSBASE,%ecx 436 movl PCB_FSBASE(%rdi),%eax 437 movl 4 + PCB_FSBASE(%rdi),%edx 438 wrmsr 439 movl $MSR_GSBASE,%ecx 440 movl PCB_GSBASE(%rdi),%eax 441 movl 4 + PCB_GSBASE(%rdi),%edx 442 wrmsr 443 movl $MSR_KGSBASE,%ecx 444 movl PCB_KGSBASE(%rdi),%eax 445 movl 4 + PCB_KGSBASE(%rdi),%edx 446 wrmsr 447 448 /* Restore EFER one more time. */ 449 movl $MSR_EFER,%ecx 450 movl PCB_EFER(%rdi),%eax 451 wrmsr 452 453 /* Restore fast syscall stuff. */ 454 movl $MSR_STAR,%ecx 455 movl PCB_STAR(%rdi),%eax 456 movl 4 + PCB_STAR(%rdi),%edx 457 wrmsr 458 movl $MSR_LSTAR,%ecx 459 movl PCB_LSTAR(%rdi),%eax 460 movl 4 + PCB_LSTAR(%rdi),%edx 461 wrmsr 462 movl $MSR_CSTAR,%ecx 463 movl PCB_CSTAR(%rdi),%eax 464 movl 4 + PCB_CSTAR(%rdi),%edx 465 wrmsr 466 movl $MSR_SF_MASK,%ecx 467 movl PCB_SFMASK(%rdi),%eax 468 wrmsr 469 470 /* Restore CR0, CR2, CR4 and CR3. */ 471 movq PCB_CR0(%rdi),%rax 472 movq %rax,%cr0 473 movq PCB_CR2(%rdi),%rax 474 movq %rax,%cr2 475 movq PCB_CR4(%rdi),%rax 476 movq %rax,%cr4 477 movq PCB_CR3(%rdi),%rax 478 movq %rax,%cr3 479 480 /* Restore descriptor tables. */ 481 cmpb $0,fred 482 jne 1f 483 lidt PCB_IDT(%rdi) 484 jmp 2f 4851: movl $MSR_FRED_RSP0,%ecx 486 movl PCB_FRED_RSP0(%rdi),%eax 487 movl 4 + PCB_FRED_RSP0(%rdi),%edx 488 wrmsr 489 movl $MSR_FRED_RSP1,%ecx 490 movl PCB_FRED_RSP1(%rdi),%eax 491 movl 4 + PCB_FRED_RSP1(%rdi),%edx 492 wrmsr 493 movl $MSR_FRED_RSP2,%ecx 494 movl PCB_FRED_RSP2(%rdi),%eax 495 movl 4 + PCB_FRED_RSP2(%rdi),%edx 496 wrmsr 497 movl $MSR_FRED_RSP3,%ecx 498 movl PCB_FRED_RSP3(%rdi),%eax 499 movl 4 + PCB_FRED_RSP3(%rdi),%edx 500 wrmsr 501 movl $MSR_FRED_STKLVLS,%ecx 502 movl PCB_FRED_STKLVLS(%rdi),%eax 503 movl 4 + PCB_FRED_STKLVLS(%rdi),%edx 504 wrmsr 505 movl $MSR_FRED_CONFIG,%ecx 506 movl PCB_FRED_CONFIG(%rdi),%eax 507 movl 4 + PCB_FRED_CONFIG(%rdi),%edx 508 wrmsr 5092: lldt PCB_LDT(%rdi) 510 511#define SDT_SYSTSS 9 512#define SDT_SYSBSY 11 513 514 /* Clear "task busy" bit and reload TR. */ 515 movq PCPU(TSS),%rax 516 andb $(~SDT_SYSBSY | SDT_SYSTSS),5(%rax) 517 movw PCB_TR(%rdi),%ax 518 ltr %ax 519 520#undef SDT_SYSTSS 521#undef SDT_SYSBSY 522 523 /* Restore debug registers. */ 524 movq PCB_DR0(%rdi),%rax 525 movq %rax,%dr0 526 movq PCB_DR1(%rdi),%rax 527 movq %rax,%dr1 528 movq PCB_DR2(%rdi),%rax 529 movq %rax,%dr2 530 movq PCB_DR3(%rdi),%rax 531 movq %rax,%dr3 532 movq PCB_DR6(%rdi),%rax 533 movq %rax,%dr6 534 movq PCB_DR7(%rdi),%rax 535 movq %rax,%dr7 536 537 /* Restore other callee saved registers. */ 538 movq PCB_R15(%rdi),%r15 539 movq PCB_R14(%rdi),%r14 540 movq PCB_R13(%rdi),%r13 541 movq PCB_R12(%rdi),%r12 542 movq PCB_RBP(%rdi),%rbp 543 movq PCB_RSP(%rdi),%rsp 544 movq PCB_RBX(%rdi),%rbx 545 546 /* Restore return address. */ 547 movq PCB_RIP(%rdi),%rax 548 movq %rax,(%rsp) 549 550 xorl %eax,%eax 551 ret 552END(resumectx) 553 554/* Wait for the new thread to become unblocked */ 555sw1wait: 5561: 557 pause 558 movq TD_LOCK(%r12),%rcx 559 cmpq %rcx, %rdx 560 je 1b 561 jmp sw1cont 562 563 .section .note.GNU-stack,"",%progbits 564