1 /*- 2 * Copyright (c) 1982, 1986 The Regents of the University of California. 3 * Copyright (c) 1989, 1990 William Jolitz 4 * Copyright (c) 1994 John Dyson 5 * Copyright (c) 2008-2018 The DragonFly Project. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department, and William Jolitz. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 41 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 42 * $FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.132.2.9 2003/01/25 19:02:23 dillon Exp $ 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/proc.h> 49 #include <sys/buf.h> 50 #include <sys/interrupt.h> 51 #include <sys/vnode.h> 52 #include <sys/vmmeter.h> 53 #include <sys/kernel.h> 54 #include <sys/sysctl.h> 55 #include <sys/unistd.h> 56 #include <sys/lwp.h> 57 58 #include <machine/clock.h> 59 #include <machine/cpu.h> 60 #include <machine/md_var.h> 61 #include <machine/smp.h> 62 #include <machine/pcb.h> 63 #include <machine/pcb_ext.h> 64 #include <machine/segments.h> 65 #include <machine/globaldata.h> /* npxthread */ 66 #include <machine/specialreg.h> 67 #include <machine/vmm.h> 68 69 #include <vm/vm.h> 70 #include <vm/vm_param.h> 71 #include <sys/lock.h> 72 #include <vm/vm_kern.h> 73 #include <vm/vm_page.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_extern.h> 76 77 #include <sys/thread2.h> 78 79 #include <bus/isa/isa.h> 80 81 static void cpu_reset_real (void); 82 83 static int spectre_mitigation = -1; 84 static int spectre_support = 0; 85 static int spectre_mode = 0; 86 SYSCTL_INT(_machdep, OID_AUTO, spectre_mode, CTLFLAG_RD, 87 &spectre_mode, 0, "current Spectre enablements"); 88 89 static int mds_mitigation = -1; 90 static int mds_support = 0; 91 static int mds_mode = 0; 92 SYSCTL_INT(_machdep, OID_AUTO, mds_mode, CTLFLAG_RD, 93 &mds_mode, 0, "current MDS enablements"); 94 95 /* 96 * Finish a fork operation, with lwp lp2 nearly set up. 97 * Copy and update the pcb, set up the stack so that the child 98 * ready to run and return to user mode. 99 */ 100 void 101 cpu_fork(struct lwp *lp1, struct lwp *lp2, int flags) 102 { 103 struct pcb *pcb2; 104 struct pmap *pmap2; 105 106 if ((flags & RFPROC) == 0) { 107 if ((flags & RFMEM) == 0) { 108 /* 109 * Unshare user LDT. > 1 test is MPSAFE. While 110 * it can potentially race a 2->1 transition, the 111 * worst that happens is that we do an unnecessary 112 * ldt replacement. 113 */ 114 struct pcb *pcb1 = lp1->lwp_thread->td_pcb; 115 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt; 116 117 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) { 118 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len); 119 user_ldt_free(pcb1); 120 pcb1->pcb_ldt = pcb_ldt; 121 set_user_ldt(pcb1); 122 } 123 } 124 return; 125 } 126 127 /* Ensure that lp1's pcb is up to date. */ 128 if (mdcpu->gd_npxthread == lp1->lwp_thread) 129 npxsave(lp1->lwp_thread->td_savefpu); 130 131 /* 132 * Copy lp1's PCB. This really only applies to the 133 * debug registers and FP state, but its faster to just copy the 134 * whole thing. Because we only save the PCB at switchout time, 135 * the register state may not be current. 136 */ 137 pcb2 = lp2->lwp_thread->td_pcb; 138 *pcb2 = *lp1->lwp_thread->td_pcb; 139 140 /* 141 * Create a new fresh stack for the new process. 142 * Copy the trap frame for the return to user mode as if from a 143 * syscall. This copies the user mode register values. 144 * 145 * pcb_rsp must allocate an additional call-return pointer below 146 * the trap frame which will be restored by cpu_heavy_restore from 147 * PCB_RIP, and the thread's td_sp pointer must allocate an 148 * additonal two quadwords below the pcb_rsp call-return pointer to 149 * hold the LWKT restore function pointer and rflags. 150 * 151 * The LWKT restore function pointer must be set to cpu_heavy_restore, 152 * which is our standard heavy-weight process switch-in function. 153 * YYY eventually we should shortcut fork_return and fork_trampoline 154 * to use the LWKT restore function directly so we can get rid of 155 * all the extra crap we are setting up. 156 */ 157 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1; 158 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs)); 159 160 /* 161 * Set registers for trampoline to user mode. Leave space for the 162 * return address on stack. These are the kernel mode register values. 163 * 164 * Set the new pmap CR3. If the new process uses isolated VM spaces, 165 * also set the isolated CR3. 166 */ 167 pmap2 = vmspace_pmap(lp2->lwp_proc->p_vmspace); 168 pcb2->pcb_cr3 = vtophys(pmap2->pm_pml4); 169 if ((pcb2->pcb_flags & PCB_ISOMMU) && pmap2->pm_pmlpv_iso) { 170 pcb2->pcb_cr3_iso = vtophys(pmap2->pm_pml4_iso); 171 } else { 172 pcb2->pcb_flags &= ~PCB_ISOMMU; 173 pcb2->pcb_cr3_iso = 0; 174 } 175 176 #if 0 177 /* 178 * Per-process spectre mitigation (future) 179 */ 180 pcb2->pcb_flags &= ~(PCB_IBRS1 | PCB_IBRS2); 181 switch (spectre_mitigation) { 182 case 1: 183 pcb2->pcb_flags |= PCB_IBRS1; 184 break; 185 case 2: 186 pcb2->pcb_flags |= PCB_IBRS2; 187 break; 188 default: 189 break; 190 } 191 #endif 192 193 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */ 194 pcb2->pcb_rbp = 0; 195 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *); 196 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */ 197 pcb2->pcb_r13 = 0; 198 pcb2->pcb_r14 = 0; 199 pcb2->pcb_r15 = 0; 200 pcb2->pcb_rip = (unsigned long)fork_trampoline; 201 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *)); 202 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER; 203 lp2->lwp_thread->td_sp -= sizeof(void *); 204 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore; 205 206 /* 207 * pcb2->pcb_ldt: duplicated below, if necessary. 208 * pcb2->pcb_savefpu: cloned above. 209 * pcb2->pcb_flags: cloned above 210 * pcb2->pcb_onfault: cloned above (always NULL here). 211 * pcb2->pcb_onfault_sp:cloned above (dont care) 212 */ 213 214 /* 215 * XXX don't copy the i/o pages. this should probably be fixed. 216 */ 217 pcb2->pcb_ext = NULL; 218 219 /* Copy the LDT, if necessary. */ 220 if (pcb2->pcb_ldt != NULL) { 221 if (flags & RFMEM) { 222 atomic_add_int(&pcb2->pcb_ldt->ldt_refcnt, 1); 223 } else { 224 pcb2->pcb_ldt = user_ldt_alloc(pcb2, 225 pcb2->pcb_ldt->ldt_len); 226 } 227 } 228 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls, 229 sizeof(lp2->lwp_thread->td_tls)); 230 /* 231 * Now, cpu_switch() can schedule the new lwp. 232 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 233 * containing the return address when exiting cpu_switch. 234 * This will normally be to fork_trampoline(), which will have 235 * %rbx loaded with the new lwp's pointer. fork_trampoline() 236 * will set up a stack to call fork_return(lp, frame); to complete 237 * the return to user-mode. 238 */ 239 } 240 241 /* 242 * Prepare new lwp to return to the address specified in params. 243 */ 244 int 245 cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params) 246 { 247 struct trapframe *regs = lp->lwp_md.md_regs; 248 void *bad_return = NULL; 249 int error; 250 251 regs->tf_rip = (long)params->lwp_func; 252 regs->tf_rsp = (long)params->lwp_stack; 253 /* Set up argument for function call */ 254 regs->tf_rdi = (long)params->lwp_arg; 255 256 /* 257 * Set up fake return address. As the lwp function may never return, 258 * we simply copy out a NULL pointer and force the lwp to receive 259 * a SIGSEGV if it returns anyways. 260 */ 261 regs->tf_rsp -= sizeof(void *); 262 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return)); 263 if (error) 264 return (error); 265 266 if (lp->lwp_proc->p_vmm) { 267 lp->lwp_thread->td_pcb->pcb_cr3 = KPML4phys; 268 cpu_set_fork_handler(lp, 269 (void (*)(void *, struct trapframe *))vmm_lwp_return, lp); 270 } else { 271 cpu_set_fork_handler(lp, 272 (void (*)(void *, struct trapframe *))generic_lwp_return, lp); 273 } 274 return (0); 275 } 276 277 /* 278 * Intercept the return address from a freshly forked process that has NOT 279 * been scheduled yet. 280 * 281 * This is needed to make kernel threads stay in kernel mode. 282 */ 283 void 284 cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *), 285 void *arg) 286 { 287 /* 288 * Note that the trap frame follows the args, so the function 289 * is really called like this: func(arg, frame); 290 */ 291 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */ 292 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */ 293 } 294 295 void 296 cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg) 297 { 298 td->td_pcb->pcb_rbx = (long)func; 299 td->td_pcb->pcb_r12 = (long)arg; 300 td->td_switch = cpu_lwkt_switch; 301 td->td_sp -= sizeof(void *); 302 *(void **)td->td_sp = rfunc; /* exit function on return */ 303 td->td_sp -= sizeof(void *); 304 *(void **)td->td_sp = cpu_kthread_restore; 305 } 306 307 void 308 cpu_lwp_exit(void) 309 { 310 struct thread *td = curthread; 311 struct pcb *pcb; 312 313 pcb = td->td_pcb; 314 315 /* Some x86 functionality was dropped */ 316 KKASSERT(pcb->pcb_ext == NULL); 317 318 /* 319 * disable all hardware breakpoints 320 */ 321 if (pcb->pcb_flags & PCB_DBREGS) { 322 reset_dbregs(); 323 pcb->pcb_flags &= ~PCB_DBREGS; 324 } 325 td->td_gd->gd_cnt.v_swtch++; 326 327 crit_enter_quick(td); 328 if (td->td_flags & TDF_TSLEEPQ) 329 tsleep_remove(td); 330 lwkt_deschedule_self(td); 331 lwkt_remove_tdallq(td); 332 cpu_thread_exit(); 333 } 334 335 /* 336 * Terminate the current thread. The caller must have already acquired 337 * the thread's rwlock and placed it on a reap list or otherwise notified 338 * a reaper of its existance. We set a special assembly switch function which 339 * releases td_rwlock after it has cleaned up the MMU state and switched 340 * out the stack. 341 * 342 * Must be caller from a critical section and with the thread descheduled. 343 */ 344 void 345 cpu_thread_exit(void) 346 { 347 npxexit(); 348 curthread->td_switch = cpu_exit_switch; 349 curthread->td_flags |= TDF_EXITING; 350 lwkt_switch(); 351 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned"); 352 } 353 354 void 355 cpu_reset(void) 356 { 357 cpu_reset_real(); 358 } 359 360 static void 361 cpu_reset_real(void) 362 { 363 /* 364 * Attempt to do a CPU reset via the keyboard controller, 365 * do not turn off the GateA20, as any machine that fails 366 * to do the reset here would then end up in no man's land. 367 */ 368 369 #if !defined(BROKEN_KEYBOARD_RESET) 370 outb(IO_KBD + 4, 0xFE); 371 DELAY(500000); /* wait 0.5 sec to see if that did it */ 372 kprintf("Keyboard reset did not work, attempting CPU shutdown\n"); 373 DELAY(1000000); /* wait 1 sec for kprintf to complete */ 374 #endif 375 #if 0 /* JG */ 376 /* force a shutdown by unmapping entire address space ! */ 377 bzero((caddr_t) PTD, PAGE_SIZE); 378 #endif 379 380 /* "good night, sweet prince .... <THUNK!>" */ 381 cpu_invltlb(); 382 /* NOTREACHED */ 383 while(1); 384 } 385 386 static void 387 swi_vm(void *arg, void *frame) 388 { 389 if (busdma_swi_pending != 0) 390 busdma_swi(); 391 } 392 393 static void 394 swi_vm_setup(void *arg) 395 { 396 register_swi_mp(SWI_VM, swi_vm, NULL, "swi_vm", NULL, 0); 397 } 398 399 SYSINIT(swi_vm_setup, SI_BOOT2_MACHDEP, SI_ORDER_ANY, swi_vm_setup, NULL); 400 401 /* 402 * NOTE: This routine is also called after a successful microcode 403 * reload on cpu 0. 404 */ 405 void mitigation_vm_setup(void *arg); 406 407 /* 408 * Check for IBPB and IBRS support 409 * 410 * This bits also specify desired modes in the spectre_mitigation sysctl. 411 */ 412 #define IBRS_SUPPORTED 0x0001 413 #define STIBP_SUPPORTED 0x0002 414 #define IBPB_SUPPORTED 0x0004 415 #define IBRS_AUTO_SUPPORTED 0x0008 416 #define STIBP_AUTO_SUPPORTED 0x0010 417 #define IBRS_PREFERRED_REQUEST 0x0020 418 419 static 420 int 421 spectre_check_support(void) 422 { 423 uint32_t p[4]; 424 int rv = 0; 425 426 /* 427 * Spectre mitigation hw bits 428 * 429 * IBRS Indirect Branch Restricted Speculation (isolation) 430 * STIBP Single Thread Indirect Branch Prediction (isolation) 431 * IBPB Branch Prediction Barrier (barrier) 432 * 433 * IBRS and STIBP must be toggled (enabled on entry to kernel, 434 * disabled on exit, as well as disabled during any MWAIT/HLT). 435 * When *_AUTO bits are available, IBRS and STIBP may be left 436 * turned on and do not have to be toggled on kernel entry/exit. 437 * Be sure to clear before going idle (else hyperthread performance 438 * will drop). 439 * 440 * All this shit has enormous overhead. IBPB in particular, and 441 * non-auto modes are disabled by default. 442 */ 443 if (cpu_vendor_id == CPU_VENDOR_INTEL) { 444 p[0] = 0; 445 p[1] = 0; 446 p[2] = 0; 447 p[3] = 0; 448 cpuid_count(7, 0, p); 449 if (p[3] & CPUID_STDEXT3_IBPB) 450 rv |= IBRS_SUPPORTED | IBPB_SUPPORTED; 451 if (p[3] & CPUID_STDEXT3_STIBP) 452 rv |= STIBP_SUPPORTED; 453 454 /* 455 * 0x80000008 p[1] bit 12 indicates IBPB support 456 * 457 * This bit might be set even though STDEXT3_IBPB is not set. 458 */ 459 p[0] = 0; 460 p[1] = 0; 461 p[2] = 0; 462 p[3] = 0; 463 do_cpuid(0x80000008U, p); 464 if (p[1] & CPUID_CAPEX_IBPB) 465 rv |= IBPB_SUPPORTED; 466 } else if (cpu_vendor_id == CPU_VENDOR_AMD) { 467 /* 468 * 0x80000008 469 * p[1] bit 12 indicates IBPB support 470 * p[1] bit 14 indicates IBRS support 471 * p[1] bit 15 indicates STIBP support 472 * 473 * p[1] bit 16 indicates IBRS auto support 474 * p[1] bit 17 indicates STIBP auto support 475 * p[1] bit 18 indicates processor prefers using 476 * IBRS instead of retpoline. 477 */ 478 p[0] = 0; 479 p[1] = 0; 480 p[2] = 0; 481 p[3] = 0; 482 do_cpuid(0x80000008U, p); 483 if (p[1] & CPUID_CAPEX_IBPB) 484 rv |= IBPB_SUPPORTED; 485 if (p[1] & CPUID_CAPEX_IBRS) 486 rv |= IBRS_SUPPORTED; 487 if (p[1] & CPUID_CAPEX_STIBP) 488 rv |= STIBP_SUPPORTED; 489 490 if (p[1] & CPUID_CAPEX_IBRS_ALWAYSON) 491 rv |= IBRS_AUTO_SUPPORTED; 492 if (p[1] & CPUID_CAPEX_STIBP_ALWAYSON) 493 rv |= STIBP_AUTO_SUPPORTED; 494 if (p[1] & CPUID_CAPEX_PREFER_IBRS) 495 rv |= IBRS_PREFERRED_REQUEST; 496 } 497 498 return rv; 499 } 500 501 /* 502 * Iterate CPUs and adjust MSR for global operations, since 503 * the KMMU* code won't do it if spectre_mitigation is 0 or 2. 504 */ 505 #define CHECK(flag) (spectre_mitigation & spectre_support & (flag)) 506 507 static 508 void 509 spectre_sysctl_changed(void) 510 { 511 globaldata_t save_gd; 512 struct trampframe *tr; 513 int spec_ctrl; 514 int spec_mask; 515 int mode; 516 int n; 517 518 spec_mask = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | 519 SPEC_CTRL_DUMMY_ENABLE | SPEC_CTRL_DUMMY_IBPB; 520 521 /* 522 * Fixup state 523 */ 524 mode = 0; 525 save_gd = mycpu; 526 for (n = 0; n < ncpus; ++n) { 527 lwkt_setcpu_self(globaldata_find(n)); 528 cpu_ccfence(); 529 tr = &pscpu->trampoline; 530 531 /* 532 * Make sure we are cleaned out. 533 * 534 * XXX cleanup, reusing globals inside the loop (they get 535 * set to the same thing each loop) 536 * 537 * [0] kernel entry (idle exit) 538 * [1] kernel exit (idle entry) 539 */ 540 tr->tr_pcb_spec_ctrl[0] &= ~spec_mask; 541 tr->tr_pcb_spec_ctrl[1] &= ~spec_mask; 542 543 /* 544 * Don't try to parse if not available 545 */ 546 if (spectre_mitigation < 0) 547 continue; 548 549 /* 550 * IBRS mode. Auto overrides toggling. 551 * 552 * Only set the ENABLE flag if we have to toggle something 553 * on entry and exit. 554 */ 555 spec_ctrl = 0; 556 if (CHECK(IBRS_AUTO_SUPPORTED)) { 557 spec_ctrl |= SPEC_CTRL_IBRS; 558 mode |= IBRS_AUTO_SUPPORTED; 559 } else if (CHECK(IBRS_SUPPORTED)) { 560 spec_ctrl |= SPEC_CTRL_IBRS | SPEC_CTRL_DUMMY_ENABLE; 561 mode |= IBRS_SUPPORTED; 562 } 563 if (CHECK(STIBP_AUTO_SUPPORTED)) { 564 spec_ctrl |= SPEC_CTRL_STIBP; 565 mode |= STIBP_AUTO_SUPPORTED; 566 } else if (CHECK(STIBP_SUPPORTED)) { 567 spec_ctrl |= SPEC_CTRL_STIBP | SPEC_CTRL_DUMMY_ENABLE; 568 mode |= STIBP_SUPPORTED; 569 } 570 571 /* 572 * IBPB requested and supported. 573 */ 574 if (CHECK(IBPB_SUPPORTED)) { 575 spec_ctrl |= SPEC_CTRL_DUMMY_IBPB; 576 mode |= IBPB_SUPPORTED; 577 } 578 579 /* 580 * Update the MSR if the cpu supports the modes to ensure 581 * proper disablement if the user disabled the mode. 582 */ 583 if (spectre_support & (IBRS_SUPPORTED | IBRS_AUTO_SUPPORTED | 584 STIBP_SUPPORTED | STIBP_AUTO_SUPPORTED)) { 585 wrmsr(MSR_SPEC_CTRL, 586 spec_ctrl & (SPEC_CTRL_IBRS|SPEC_CTRL_STIBP)); 587 } 588 589 /* 590 * Update spec_ctrl fields in the trampoline. 591 * 592 * [0] on-kernel-entry (on-idle-exit) 593 * [1] on-kernel-exit (on-idle-entry) 594 * 595 * When auto mode is supported we leave the bit set, otherwise 596 * we clear the bits. 597 */ 598 tr->tr_pcb_spec_ctrl[0] |= spec_ctrl; 599 if (CHECK(IBRS_AUTO_SUPPORTED) == 0) 600 spec_ctrl &= ~SPEC_CTRL_IBRS; 601 if (CHECK(STIBP_AUTO_SUPPORTED) == 0) 602 spec_ctrl &= ~SPEC_CTRL_STIBP; 603 tr->tr_pcb_spec_ctrl[1] |= spec_ctrl; 604 605 /* 606 * Make sure we set this on the first loop. It will be 607 * the same value on remaining loops. 608 */ 609 spectre_mode = mode; 610 } 611 lwkt_setcpu_self(save_gd); 612 cpu_ccfence(); 613 614 /* 615 * Console message on mitigation mode change 616 */ 617 kprintf("Spectre: support=("); 618 if (spectre_support == 0) { 619 kprintf(" none"); 620 } else { 621 if (spectre_support & IBRS_SUPPORTED) 622 kprintf(" IBRS"); 623 if (spectre_support & STIBP_SUPPORTED) 624 kprintf(" STIBP"); 625 if (spectre_support & IBPB_SUPPORTED) 626 kprintf(" IBPB"); 627 if (spectre_support & IBRS_AUTO_SUPPORTED) 628 kprintf(" IBRS_AUTO"); 629 if (spectre_support & STIBP_AUTO_SUPPORTED) 630 kprintf(" STIBP_AUTO"); 631 if (spectre_support & IBRS_PREFERRED_REQUEST) 632 kprintf(" IBRS_REQUESTED"); 633 } 634 kprintf(" ) req=%04x operating=(", (uint16_t)spectre_mitigation); 635 if (spectre_mode == 0) { 636 kprintf(" none"); 637 } else { 638 if (spectre_mode & IBRS_SUPPORTED) 639 kprintf(" IBRS"); 640 if (spectre_mode & STIBP_SUPPORTED) 641 kprintf(" STIBP"); 642 if (spectre_mode & IBPB_SUPPORTED) 643 kprintf(" IBPB"); 644 if (spectre_mode & IBRS_AUTO_SUPPORTED) 645 kprintf(" IBRS_AUTO"); 646 if (spectre_mode & STIBP_AUTO_SUPPORTED) 647 kprintf(" STIBP_AUTO"); 648 if (spectre_mode & IBRS_PREFERRED_REQUEST) 649 kprintf(" IBRS_REQUESTED"); 650 } 651 kprintf(" )\n"); 652 } 653 654 #undef CHECK 655 656 /* 657 * User changes sysctl value 658 */ 659 static int 660 sysctl_spectre_mitigation(SYSCTL_HANDLER_ARGS) 661 { 662 char buf[128]; 663 char *ptr; 664 char *iter; 665 size_t len; 666 int spectre; 667 int error = 0; 668 int loop = 0; 669 670 /* 671 * Return current operating mode or support. 672 */ 673 if (oidp->oid_kind & CTLFLAG_WR) 674 spectre = spectre_mode; 675 else 676 spectre = spectre_support; 677 678 spectre &= (IBRS_SUPPORTED | IBRS_AUTO_SUPPORTED | 679 STIBP_SUPPORTED | STIBP_AUTO_SUPPORTED | 680 IBPB_SUPPORTED); 681 while (spectre) { 682 if (error) 683 break; 684 if (loop++) { 685 error = SYSCTL_OUT(req, " ", 1); 686 if (error) 687 break; 688 } 689 if (spectre & IBRS_SUPPORTED) { 690 spectre &= ~IBRS_SUPPORTED; 691 error = SYSCTL_OUT(req, "IBRS", 4); 692 } else 693 if (spectre & IBRS_AUTO_SUPPORTED) { 694 spectre &= ~IBRS_AUTO_SUPPORTED; 695 error = SYSCTL_OUT(req, "IBRS_AUTO", 9); 696 } else 697 if (spectre & STIBP_SUPPORTED) { 698 spectre &= ~STIBP_SUPPORTED; 699 error = SYSCTL_OUT(req, "STIBP", 5); 700 } else 701 if (spectre & STIBP_AUTO_SUPPORTED) { 702 spectre &= ~STIBP_AUTO_SUPPORTED; 703 error = SYSCTL_OUT(req, "STIBP_AUTO", 10); 704 } else 705 if (spectre & IBPB_SUPPORTED) { 706 spectre &= ~IBPB_SUPPORTED; 707 error = SYSCTL_OUT(req, "IBPB", 4); 708 } 709 } 710 if (loop == 0) { 711 error = SYSCTL_OUT(req, "NONE", 4); 712 } 713 714 if (error || req->newptr == NULL) 715 return error; 716 if ((oidp->oid_kind & CTLFLAG_WR) == 0) 717 return error; 718 719 /* 720 * Change current operating mode 721 */ 722 len = req->newlen - req->newidx; 723 if (len >= sizeof(buf)) { 724 error = EINVAL; 725 len = 0; 726 } else { 727 error = SYSCTL_IN(req, buf, len); 728 } 729 buf[len] = 0; 730 iter = &buf[0]; 731 spectre = 0; 732 733 while (error == 0 && iter) { 734 ptr = strsep(&iter, " ,\t\r\n"); 735 if (*ptr == 0) 736 continue; 737 if (strcasecmp(ptr, "NONE") == 0) 738 spectre |= 0; 739 else if (strcasecmp(ptr, "IBRS") == 0) 740 spectre |= IBRS_SUPPORTED; 741 else if (strcasecmp(ptr, "IBRS_AUTO") == 0) 742 spectre |= IBRS_AUTO_SUPPORTED; 743 else if (strcasecmp(ptr, "STIBP") == 0) 744 spectre |= STIBP_SUPPORTED; 745 else if (strcasecmp(ptr, "STIBP_AUTO") == 0) 746 spectre |= STIBP_AUTO_SUPPORTED; 747 else if (strcasecmp(ptr, "IBPB") == 0) 748 spectre |= IBPB_SUPPORTED; 749 else 750 error = ENOENT; 751 } 752 if (error == 0) { 753 spectre_mitigation = spectre; 754 spectre_sysctl_changed(); 755 } 756 return error; 757 } 758 759 SYSCTL_PROC(_machdep, OID_AUTO, spectre_mitigation, 760 CTLTYPE_STRING | CTLFLAG_RW, 761 0, 0, sysctl_spectre_mitigation, "A", "Spectre exploit mitigation"); 762 SYSCTL_PROC(_machdep, OID_AUTO, spectre_support, 763 CTLTYPE_STRING | CTLFLAG_RD, 764 0, 0, sysctl_spectre_mitigation, "A", "Spectre supported features"); 765 766 /* 767 * NOTE: Called at SI_BOOT2_MACHDEP and also when the microcode is 768 * updated. Microcode updates must be applied to all cpus 769 * for support to be recognized. 770 */ 771 static void 772 spectre_vm_setup(void *arg) 773 { 774 int inconsistent = 0; 775 int supmask; 776 777 /* 778 * Fetch tunable in auto mode 779 */ 780 if (spectre_mitigation < 0) { 781 TUNABLE_INT_FETCH("machdep.spectre_mitigation", 782 &spectre_mitigation); 783 } 784 785 if ((supmask = spectre_check_support()) != 0) { 786 /* 787 * Must be supported on all cpus before we 788 * can enable it. Returns silently if it 789 * isn't. 790 * 791 * NOTE! arg != NULL indicates we were called 792 * from cpuctl after a successful microcode 793 * update. 794 */ 795 if (arg != NULL) { 796 globaldata_t save_gd; 797 int n; 798 799 save_gd = mycpu; 800 for (n = 0; n < ncpus; ++n) { 801 lwkt_setcpu_self(globaldata_find(n)); 802 cpu_ccfence(); 803 if (spectre_check_support() != 804 supmask) { 805 inconsistent = 1; 806 break; 807 } 808 } 809 lwkt_setcpu_self(save_gd); 810 cpu_ccfence(); 811 } 812 } 813 814 /* 815 * Be silent while microcode is being loaded on various CPUs, 816 * until all done. 817 */ 818 if (inconsistent) { 819 spectre_mitigation = -1; 820 spectre_support = 0; 821 return; 822 } 823 824 /* 825 * IBRS support 826 */ 827 spectre_support = supmask; 828 829 /* 830 * Enable spectre_mitigation, set defaults if -1, adjust 831 * tuned value according to support if not. 832 * 833 * NOTE! We do not enable IBPB for user->kernel transitions 834 * by default, so this code is commented out for now. 835 */ 836 if (spectre_support) { 837 if (spectre_mitigation < 0) { 838 spectre_mitigation = 0; 839 840 /* 841 * IBRS toggling not currently recommended as a 842 * default. 843 */ 844 if (spectre_support & IBRS_AUTO_SUPPORTED) 845 spectre_mitigation |= IBRS_AUTO_SUPPORTED; 846 else if (spectre_support & IBRS_SUPPORTED) 847 spectre_mitigation |= 0; 848 849 /* 850 * STIBP toggling not currently recommended as a 851 * default. 852 */ 853 if (spectre_support & STIBP_AUTO_SUPPORTED) 854 spectre_mitigation |= STIBP_AUTO_SUPPORTED; 855 else if (spectre_support & STIBP_SUPPORTED) 856 spectre_mitigation |= 0; 857 858 /* 859 * IBPB adds enormous (~2uS) overhead to system 860 * calls etc, we do not enable it by default. 861 */ 862 if (spectre_support & IBPB_SUPPORTED) 863 spectre_mitigation |= 0; 864 } 865 } else { 866 spectre_mitigation = -1; 867 } 868 869 /* 870 * Disallow sysctl changes when there is no support (otherwise 871 * the wrmsr will cause a protection fault). 872 */ 873 if (spectre_mitigation < 0) 874 sysctl___machdep_spectre_mitigation.oid_kind &= ~CTLFLAG_WR; 875 else 876 sysctl___machdep_spectre_mitigation.oid_kind |= CTLFLAG_WR; 877 878 spectre_sysctl_changed(); 879 } 880 881 #define MDS_AVX512_4VNNIW_SUPPORTED 0x0001 882 #define MDS_AVX512_4FMAPS_SUPPORTED 0x0002 883 #define MDS_MD_CLEAR_SUPPORTED 0x0004 884 #define MDS_TSX_FORCE_ABORT_SUPPORTED 0x0008 885 #define MDS_NOT_REQUIRED 0x8000 886 887 static 888 int 889 mds_check_support(void) 890 { 891 uint64_t msr; 892 uint32_t p[4]; 893 int rv = 0; 894 895 /* 896 * MDS mitigation hw bits 897 * 898 * MD_CLEAR Use microcode-supported verf insn. This is the 899 * only mode we really support. 900 */ 901 if (cpu_vendor_id == CPU_VENDOR_INTEL) { 902 p[0] = 0; 903 p[1] = 0; 904 p[2] = 0; 905 p[3] = 0; 906 cpuid_count(7, 0, p); 907 if (p[3] & CPUID_STDEXT3_ARCH_CAP) { 908 msr = rdmsr(MSR_IA32_ARCH_CAPABILITIES); 909 if (msr & IA32_ARCH_CAP_MDS_NO) 910 rv = MDS_NOT_REQUIRED; 911 } 912 if (p[3] & CPUID_STDEXT3_AVX5124VNNIW) 913 rv |= MDS_AVX512_4VNNIW_SUPPORTED; 914 if (p[3] & CPUID_STDEXT3_AVX5124FMAPS) 915 rv |= MDS_AVX512_4FMAPS_SUPPORTED; 916 if (p[3] & CPUID_STDEXT3_MD_CLEAR) 917 rv |= MDS_MD_CLEAR_SUPPORTED; 918 if (p[3] & CPUID_STDEXT3_TSXFA) 919 rv |= MDS_TSX_FORCE_ABORT_SUPPORTED; 920 } else { 921 rv = MDS_NOT_REQUIRED; 922 } 923 924 return rv; 925 } 926 927 /* 928 * Iterate CPUs and adjust MSR for global operations, since 929 * the KMMU* code won't do it if spectre_mitigation is 0 or 2. 930 */ 931 #define CHECK(flag) (mds_mitigation & mds_support & (flag)) 932 933 static 934 void 935 mds_sysctl_changed(void) 936 { 937 globaldata_t save_gd; 938 struct trampframe *tr; 939 int spec_ctrl; 940 int spec_mask; 941 int mode; 942 int n; 943 944 spec_mask = SPEC_CTRL_MDS_ENABLE; 945 946 /* 947 * Fixup state 948 */ 949 mode = 0; 950 save_gd = mycpu; 951 for (n = 0; n < ncpus; ++n) { 952 lwkt_setcpu_self(globaldata_find(n)); 953 cpu_ccfence(); 954 tr = &pscpu->trampoline; 955 956 /* 957 * Make sure we are cleaned out. 958 * 959 * XXX cleanup, reusing globals inside the loop (they get 960 * set to the same thing each loop) 961 * 962 * [0] kernel entry (idle exit) 963 * [1] kernel exit (idle entry) 964 */ 965 tr->tr_pcb_spec_ctrl[0] &= ~spec_mask; 966 tr->tr_pcb_spec_ctrl[1] &= ~spec_mask; 967 968 /* 969 * Don't try to parse if not available 970 */ 971 if (mds_mitigation < 0) 972 continue; 973 974 spec_ctrl = 0; 975 if (CHECK(MDS_MD_CLEAR_SUPPORTED)) { 976 spec_ctrl |= SPEC_CTRL_MDS_ENABLE; 977 mode |= MDS_MD_CLEAR_SUPPORTED; 978 } 979 980 /* 981 * Update spec_ctrl fields in the trampoline. 982 * 983 * [0] on-kernel-entry (on-idle-exit) 984 * [1] on-kernel-exit (on-idle-entry) 985 * 986 * The MDS stuff is only needed on kernel-exit or idle-entry 987 */ 988 /* tr->tr_pcb_spec_ctrl[0] |= spec_ctrl; */ 989 tr->tr_pcb_spec_ctrl[1] |= spec_ctrl; 990 991 /* 992 * Make sure we set this on the first loop. It will be 993 * the same value on remaining loops. 994 */ 995 mds_mode = mode; 996 } 997 lwkt_setcpu_self(save_gd); 998 cpu_ccfence(); 999 1000 /* 1001 * Console message on mitigation mode change 1002 */ 1003 kprintf("MDS: support=("); 1004 if (mds_support == 0) { 1005 kprintf(" none"); 1006 } else { 1007 if (mds_support & MDS_AVX512_4VNNIW_SUPPORTED) 1008 kprintf(" AVX512_4VNNIW"); 1009 if (mds_support & MDS_AVX512_4FMAPS_SUPPORTED) 1010 kprintf(" AVX512_4FMAPS"); 1011 if (mds_support & MDS_MD_CLEAR_SUPPORTED) 1012 kprintf(" MD_CLEAR"); 1013 if (mds_support & MDS_TSX_FORCE_ABORT_SUPPORTED) 1014 kprintf(" TSX_FORCE_ABORT"); 1015 if (mds_support & MDS_NOT_REQUIRED) 1016 kprintf(" MDS_NOT_REQUIRED"); 1017 } 1018 kprintf(" ) req=%04x operating=(", (uint16_t)mds_mitigation); 1019 if (mds_mode == 0) { 1020 kprintf(" none"); 1021 } else { 1022 if (mds_mode & MDS_AVX512_4VNNIW_SUPPORTED) 1023 kprintf(" AVX512_4VNNIW"); 1024 if (mds_mode & MDS_AVX512_4FMAPS_SUPPORTED) 1025 kprintf(" AVX512_4FMAPS"); 1026 if (mds_mode & MDS_MD_CLEAR_SUPPORTED) 1027 kprintf(" MD_CLEAR"); 1028 if (mds_mode & MDS_TSX_FORCE_ABORT_SUPPORTED) 1029 kprintf(" TSX_FORCE_ABORT"); 1030 if (mds_mode & MDS_NOT_REQUIRED) 1031 kprintf(" MDS_NOT_REQUIRED"); 1032 } 1033 kprintf(" )\n"); 1034 } 1035 1036 #undef CHECK 1037 1038 /* 1039 * User changes sysctl value 1040 */ 1041 static int 1042 sysctl_mds_mitigation(SYSCTL_HANDLER_ARGS) 1043 { 1044 char buf[128]; 1045 char *ptr; 1046 char *iter; 1047 size_t len; 1048 int mds; 1049 int error = 0; 1050 int loop = 0; 1051 1052 /* 1053 * Return current operating mode or support. 1054 */ 1055 if (oidp->oid_kind & CTLFLAG_WR) 1056 mds = mds_mode; 1057 else 1058 mds = mds_support; 1059 1060 mds &= MDS_AVX512_4VNNIW_SUPPORTED | 1061 MDS_AVX512_4FMAPS_SUPPORTED | 1062 MDS_MD_CLEAR_SUPPORTED | 1063 MDS_TSX_FORCE_ABORT_SUPPORTED | 1064 MDS_NOT_REQUIRED; 1065 1066 while (mds) { 1067 if (error) 1068 break; 1069 if (loop++) { 1070 error = SYSCTL_OUT(req, " ", 1); 1071 if (error) 1072 break; 1073 } 1074 if (mds & MDS_AVX512_4VNNIW_SUPPORTED) { 1075 mds &= ~MDS_AVX512_4VNNIW_SUPPORTED; 1076 error = SYSCTL_OUT(req, "AVX512_4VNNIW", 13); 1077 } else 1078 if (mds & MDS_AVX512_4FMAPS_SUPPORTED) { 1079 mds &= ~MDS_AVX512_4FMAPS_SUPPORTED; 1080 error = SYSCTL_OUT(req, "AVX512_4FMAPS", 13); 1081 } else 1082 if (mds & MDS_MD_CLEAR_SUPPORTED) { 1083 mds &= ~MDS_MD_CLEAR_SUPPORTED; 1084 error = SYSCTL_OUT(req, "MD_CLEAR", 8); 1085 } else 1086 if (mds & MDS_TSX_FORCE_ABORT_SUPPORTED) { 1087 mds &= ~MDS_TSX_FORCE_ABORT_SUPPORTED; 1088 error = SYSCTL_OUT(req, "TSX_FORCE_ABORT", 15); 1089 } else 1090 if (mds & MDS_NOT_REQUIRED) { 1091 mds &= ~MDS_NOT_REQUIRED; 1092 error = SYSCTL_OUT(req, "MDS_NOT_REQUIRED", 16); 1093 } 1094 } 1095 if (loop == 0) { 1096 error = SYSCTL_OUT(req, "NONE", 4); 1097 } 1098 1099 if (error || req->newptr == NULL) 1100 return error; 1101 if ((oidp->oid_kind & CTLFLAG_WR) == 0) 1102 return error; 1103 1104 /* 1105 * Change current operating mode 1106 */ 1107 len = req->newlen - req->newidx; 1108 if (len >= sizeof(buf)) { 1109 error = EINVAL; 1110 len = 0; 1111 } else { 1112 error = SYSCTL_IN(req, buf, len); 1113 } 1114 buf[len] = 0; 1115 iter = &buf[0]; 1116 mds = 0; 1117 1118 while (error == 0 && iter) { 1119 ptr = strsep(&iter, " ,\t\r\n"); 1120 if (*ptr == 0) 1121 continue; 1122 if (strcasecmp(ptr, "NONE") == 0) 1123 mds |= 0; 1124 else if (strcasecmp(ptr, "AVX512_4VNNIW") == 0) 1125 mds |= MDS_AVX512_4VNNIW_SUPPORTED; 1126 else if (strcasecmp(ptr, "AVX512_4FMAPS") == 0) 1127 mds |= MDS_AVX512_4FMAPS_SUPPORTED; 1128 else if (strcasecmp(ptr, "MD_CLEAR") == 0) 1129 mds |= MDS_MD_CLEAR_SUPPORTED; 1130 else if (strcasecmp(ptr, "TSX_FORCE_ABORT") == 0) 1131 mds |= MDS_TSX_FORCE_ABORT_SUPPORTED; 1132 else if (strcasecmp(ptr, "MDS_NOT_REQUIRED") == 0) 1133 mds |= MDS_NOT_REQUIRED; 1134 else 1135 error = ENOENT; 1136 } 1137 if (error == 0) { 1138 mds_mitigation = mds; 1139 mds_sysctl_changed(); 1140 } 1141 return error; 1142 } 1143 1144 SYSCTL_PROC(_machdep, OID_AUTO, mds_mitigation, 1145 CTLTYPE_STRING | CTLFLAG_RW, 1146 0, 0, sysctl_mds_mitigation, "A", "MDS exploit mitigation"); 1147 SYSCTL_PROC(_machdep, OID_AUTO, mds_support, 1148 CTLTYPE_STRING | CTLFLAG_RD, 1149 0, 0, sysctl_mds_mitigation, "A", "MDS supported features"); 1150 1151 /* 1152 * NOTE: Called at SI_BOOT2_MACHDEP and also when the microcode is 1153 * updated. Microcode updates must be applied to all cpus 1154 * for support to be recognized. 1155 */ 1156 static void 1157 mds_vm_setup(void *arg) 1158 { 1159 int inconsistent = 0; 1160 int supmask; 1161 1162 /* 1163 * Fetch tunable in auto mode 1164 */ 1165 if (mds_mitigation < 0) { 1166 TUNABLE_INT_FETCH("machdep.mds_mitigation", &mds_mitigation); 1167 } 1168 1169 if ((supmask = mds_check_support()) != 0) { 1170 /* 1171 * Must be supported on all cpus before we 1172 * can enable it. Returns silently if it 1173 * isn't. 1174 * 1175 * NOTE! arg != NULL indicates we were called 1176 * from cpuctl after a successful microcode 1177 * update. 1178 */ 1179 if (arg != NULL) { 1180 globaldata_t save_gd; 1181 int n; 1182 1183 save_gd = mycpu; 1184 for (n = 0; n < ncpus; ++n) { 1185 lwkt_setcpu_self(globaldata_find(n)); 1186 cpu_ccfence(); 1187 if (mds_check_support() != supmask) { 1188 inconsistent = 1; 1189 break; 1190 } 1191 } 1192 lwkt_setcpu_self(save_gd); 1193 cpu_ccfence(); 1194 } 1195 } 1196 1197 /* 1198 * Be silent while microcode is being loaded on various CPUs, 1199 * until all done. 1200 */ 1201 if (inconsistent) { 1202 mds_mitigation = -1; 1203 mds_support = 0; 1204 return; 1205 } 1206 1207 /* 1208 * IBRS support 1209 */ 1210 mds_support = supmask; 1211 1212 /* 1213 * Enable mds_mitigation, set defaults if -1, adjust 1214 * tuned value according to support if not. 1215 * 1216 * NOTE! MDS is not enabled by default. 1217 */ 1218 if (mds_support) { 1219 if (mds_mitigation < 0) { 1220 mds_mitigation = 0; 1221 1222 if ((mds_support & MDS_NOT_REQUIRED) == 0 && 1223 (mds_support & MDS_MD_CLEAR_SUPPORTED)) { 1224 /* mds_mitigation |= MDS_MD_CLEAR_SUPPORTED; */ 1225 } 1226 } 1227 } else { 1228 mds_mitigation = -1; 1229 } 1230 1231 /* 1232 * Disallow sysctl changes when there is no support (otherwise 1233 * the wrmsr will cause a protection fault). 1234 */ 1235 if (mds_mitigation < 0) 1236 sysctl___machdep_mds_mitigation.oid_kind &= ~CTLFLAG_WR; 1237 else 1238 sysctl___machdep_mds_mitigation.oid_kind |= CTLFLAG_WR; 1239 1240 mds_sysctl_changed(); 1241 } 1242 1243 /* 1244 * NOTE: Called at SI_BOOT2_MACHDEP and also when the microcode is 1245 * updated. Microcode updates must be applied to all cpus 1246 * for support to be recognized. 1247 */ 1248 void 1249 mitigation_vm_setup(void *arg) 1250 { 1251 spectre_vm_setup(arg); 1252 mds_vm_setup(arg); 1253 } 1254 1255 SYSINIT(mitigation_vm_setup, SI_BOOT2_MACHDEP, SI_ORDER_ANY, 1256 mitigation_vm_setup, NULL); 1257 1258 /* 1259 * platform-specific vmspace initialization (nothing for x86_64) 1260 */ 1261 void 1262 cpu_vmspace_alloc(struct vmspace *vm __unused) 1263 { 1264 } 1265 1266 void 1267 cpu_vmspace_free(struct vmspace *vm __unused) 1268 { 1269 } 1270 1271 int 1272 kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot) 1273 { 1274 vm_offset_t addr; 1275 1276 if (saddr < KvaStart) 1277 return EFAULT; 1278 if (eaddr >= KvaEnd) 1279 return EFAULT; 1280 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) { 1281 if (pmap_kextract(addr) == 0) 1282 return EFAULT; 1283 } 1284 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot)) 1285 return EFAULT; 1286 return 0; 1287 } 1288 1289 #if 0 1290 1291 void _test_frame_enter(struct trapframe *frame); 1292 void _test_frame_exit(struct trapframe *frame); 1293 1294 void 1295 _test_frame_enter(struct trapframe *frame) 1296 { 1297 thread_t td = curthread; 1298 1299 if (ISPL(frame->tf_cs) == SEL_UPL) { 1300 KKASSERT(td->td_lwp); 1301 KASSERT(td->td_lwp->lwp_md.md_regs == frame, 1302 ("_test_frame_exit: Frame mismatch %p %p", 1303 td->td_lwp->lwp_md.md_regs, frame)); 1304 td->td_lwp->lwp_saveusp = (void *)frame->tf_rsp; 1305 td->td_lwp->lwp_saveupc = (void *)frame->tf_rip; 1306 } 1307 if ((char *)frame < td->td_kstack || 1308 (char *)frame > td->td_kstack + td->td_kstack_size) { 1309 panic("_test_frame_exit: frame not on kstack %p kstack=%p", 1310 frame, td->td_kstack); 1311 } 1312 } 1313 1314 void 1315 _test_frame_exit(struct trapframe *frame) 1316 { 1317 thread_t td = curthread; 1318 1319 if (ISPL(frame->tf_cs) == SEL_UPL) { 1320 KKASSERT(td->td_lwp); 1321 KASSERT(td->td_lwp->lwp_md.md_regs == frame, 1322 ("_test_frame_exit: Frame mismatch %p %p", 1323 td->td_lwp->lwp_md.md_regs, frame)); 1324 if (td->td_lwp->lwp_saveusp != (void *)frame->tf_rsp) { 1325 kprintf("_test_frame_exit: %s:%d usp mismatch %p/%p\n", 1326 td->td_comm, td->td_proc->p_pid, 1327 td->td_lwp->lwp_saveusp, 1328 (void *)frame->tf_rsp); 1329 } 1330 if (td->td_lwp->lwp_saveupc != (void *)frame->tf_rip) { 1331 kprintf("_test_frame_exit: %s:%d upc mismatch %p/%p\n", 1332 td->td_comm, td->td_proc->p_pid, 1333 td->td_lwp->lwp_saveupc, 1334 (void *)frame->tf_rip); 1335 } 1336 1337 /* 1338 * adulterate the fields to catch entries that 1339 * don't run through test_frame_enter 1340 */ 1341 td->td_lwp->lwp_saveusp = 1342 (void *)~(intptr_t)td->td_lwp->lwp_saveusp; 1343 td->td_lwp->lwp_saveupc = 1344 (void *)~(intptr_t)td->td_lwp->lwp_saveupc; 1345 } 1346 if ((char *)frame < td->td_kstack || 1347 (char *)frame > td->td_kstack + td->td_kstack_size) { 1348 panic("_test_frame_exit: frame not on kstack %p kstack=%p", 1349 frame, td->td_kstack); 1350 } 1351 } 1352 1353 #endif 1354