1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 29 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 30 /* All Rights Reserved */ 31 /* */ 32 /* Copyright (c) 1987, 1988 Microsoft Corporation */ 33 /* All Rights Reserved */ 34 /* */ 35 36 #pragma ident "%Z%%M% %I% %E% SMI" 37 38 #include <sys/types.h> 39 #include <sys/sysmacros.h> 40 #include <sys/param.h> 41 #include <sys/signal.h> 42 #include <sys/systm.h> 43 #include <sys/user.h> 44 #include <sys/proc.h> 45 #include <sys/disp.h> 46 #include <sys/class.h> 47 #include <sys/core.h> 48 #include <sys/syscall.h> 49 #include <sys/cpuvar.h> 50 #include <sys/vm.h> 51 #include <sys/sysinfo.h> 52 #include <sys/fault.h> 53 #include <sys/stack.h> 54 #include <sys/mmu.h> 55 #include <sys/psw.h> 56 #include <sys/regset.h> 57 #include <sys/fp.h> 58 #include <sys/trap.h> 59 #include <sys/kmem.h> 60 #include <sys/vtrace.h> 61 #include <sys/cmn_err.h> 62 #include <sys/prsystm.h> 63 #include <sys/mutex_impl.h> 64 #include <sys/machsystm.h> 65 #include <sys/archsystm.h> 66 #include <sys/sdt.h> 67 #include <sys/avintr.h> 68 #include <sys/kobj.h> 69 70 #include <vm/hat.h> 71 72 #include <vm/seg_kmem.h> 73 #include <vm/as.h> 74 #include <vm/seg.h> 75 #include <vm/hat_pte.h> 76 77 #include <sys/procfs.h> 78 79 #include <sys/reboot.h> 80 #include <sys/debug.h> 81 #include <sys/debugreg.h> 82 #include <sys/modctl.h> 83 #include <sys/aio_impl.h> 84 #include <sys/tnf.h> 85 #include <sys/tnf_probe.h> 86 #include <sys/cred.h> 87 #include <sys/mman.h> 88 #include <sys/x86_archext.h> 89 #include <sys/copyops.h> 90 #include <c2/audit.h> 91 #include <sys/ftrace.h> 92 #include <sys/panic.h> 93 #include <sys/traptrace.h> 94 #include <sys/ontrap.h> 95 #include <sys/cpc_impl.h> 96 97 #define USER 0x10000 /* user-mode flag added to trap type */ 98 99 static const char *trap_type_mnemonic[] = { 100 "de", "db", "2", "bp", 101 "of", "br", "ud", "nm", 102 "df", "9", "ts", "np", 103 "ss", "gp", "pf", "15", 104 "mf", "ac", "mc", "xf" 105 }; 106 107 static const char *trap_type[] = { 108 "Divide error", /* trap id 0 */ 109 "Debug", /* trap id 1 */ 110 "NMI interrupt", /* trap id 2 */ 111 "Breakpoint", /* trap id 3 */ 112 "Overflow", /* trap id 4 */ 113 "BOUND range exceeded", /* trap id 5 */ 114 "Invalid opcode", /* trap id 6 */ 115 "Device not available", /* trap id 7 */ 116 "Double fault", /* trap id 8 */ 117 "Coprocessor segment overrun", /* trap id 9 */ 118 "Invalid TSS", /* trap id 10 */ 119 "Segment not present", /* trap id 11 */ 120 "Stack segment fault", /* trap id 12 */ 121 "General protection", /* trap id 13 */ 122 "Page fault", /* trap id 14 */ 123 "Reserved", /* trap id 15 */ 124 "x87 floating point error", /* trap id 16 */ 125 "Alignment check", /* trap id 17 */ 126 "Machine check", /* trap id 18 */ 127 "SIMD floating point exception", /* trap id 19 */ 128 }; 129 130 #define TRAP_TYPES (sizeof (trap_type) / sizeof (trap_type[0])) 131 132 int tudebug = 0; 133 int tudebugbpt = 0; 134 int tudebugfpe = 0; 135 int tudebugsse = 0; 136 137 #if defined(TRAPDEBUG) || defined(lint) 138 int tdebug = 0; 139 int lodebug = 0; 140 int faultdebug = 0; 141 #else 142 #define tdebug 0 143 #define lodebug 0 144 #define faultdebug 0 145 #endif /* defined(TRAPDEBUG) || defined(lint) */ 146 147 #if defined(TRAPTRACE) 148 static void dump_ttrace(void); 149 #endif /* TRAPTRACE */ 150 static void dumpregs(struct regs *); 151 static void showregs(uint_t, struct regs *, caddr_t); 152 static void dump_tss(void); 153 static int kern_gpfault(struct regs *); 154 155 struct trap_info { 156 struct regs *trap_regs; 157 uint_t trap_type; 158 caddr_t trap_addr; 159 }; 160 161 /*ARGSUSED*/ 162 static int 163 die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid) 164 { 165 struct trap_info ti; 166 const char *trap_name, *trap_mnemonic; 167 168 if (type < TRAP_TYPES) { 169 trap_name = trap_type[type]; 170 trap_mnemonic = trap_type_mnemonic[type]; 171 } else { 172 trap_name = "trap"; 173 trap_mnemonic = "-"; 174 } 175 176 #ifdef TRAPTRACE 177 TRAPTRACE_FREEZE; 178 #endif 179 180 ti.trap_regs = rp; 181 ti.trap_type = type & ~USER; 182 ti.trap_addr = addr; 183 184 curthread->t_panic_trap = &ti; 185 186 if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) { 187 panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p " 188 "occurred in module \"%s\" due to %s", 189 type, trap_mnemonic, trap_name, (void *)rp, (void *)addr, 190 mod_containing_pc((caddr_t)rp->r_pc), 191 addr < (caddr_t)PAGESIZE ? 192 "a NULL pointer dereference" : 193 "an illegal access to a user address"); 194 } else 195 panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p", 196 type, trap_mnemonic, trap_name, (void *)rp, (void *)addr); 197 return (0); 198 } 199 200 /* 201 * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction. 202 * 203 * int <vector> is two bytes: 0xCD <vector> 204 */ 205 206 #define SLOW_SCALL_SIZE 2 207 208 static int 209 rewrite_syscall(caddr_t pc) 210 { 211 uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT }; 212 213 if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE, 214 (uintptr_t)pc) != 0) 215 return (1); 216 217 return (0); 218 } 219 220 /* 221 * Test to see if the instruction at pc is sysenter or syscall. The second 222 * argument should be the x86 feature flag corresponding to the expected 223 * instruction. 224 * 225 * sysenter is two bytes: 0x0F 0x34 226 * syscall is two bytes: 0x0F 0x05 227 */ 228 229 #define FAST_SCALL_SIZE 2 230 231 static int 232 instr_is_fast_syscall(caddr_t pc, int which) 233 { 234 uchar_t instr[FAST_SCALL_SIZE]; 235 236 ASSERT(which == X86_SEP || which == X86_ASYSC); 237 238 if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0 || 239 instr[0] != 0x0F) 240 return (0); 241 242 if ((which == X86_SEP && instr[1] == 0x34) || 243 (which == X86_ASYSC && instr[1] == 0x05)) 244 return (1); 245 246 return (0); 247 } 248 249 /* 250 * Test to see if the instruction at pc is a system call instruction. 251 * 252 * The bytes of an lcall instruction used for the syscall trap. 253 * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 }; 254 * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 }; 255 */ 256 257 #define LCALLSIZE 7 258 259 static int 260 instr_is_syscall(caddr_t pc) 261 { 262 uchar_t instr[LCALLSIZE]; 263 264 if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 && 265 instr[0] == 0x9a && 266 instr[1] == 0 && 267 instr[2] == 0 && 268 instr[3] == 0 && 269 instr[4] == 0 && 270 (instr[5] == 0x7 || instr[5] == 0x27) && 271 instr[6] == 0) 272 return (1); 273 274 return (0); 275 } 276 277 #ifdef __amd64 278 279 /* 280 * In the first revisions of AMD64 CPUs produced by AMD, the LAHF and 281 * SAHF instructions were not implemented in 64bit mode. Later revisions 282 * did implement these instructions. An extension to the cpuid instruction 283 * was added to check for the capability of executing these instructions 284 * in 64bit mode. 285 * 286 * Intel originally did not implement these instructions in EM64T either, 287 * but added them in later revisions. 288 * 289 * So, there are different chip revisions by both vendors out there that 290 * may or may not implement these instructions. The easy solution is to 291 * just always emulate these instructions on demand. 292 * 293 * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e) 294 * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f) 295 */ 296 297 #define LSAHFSIZE 1 298 299 static int 300 instr_is_lsahf(caddr_t pc, uchar_t *instr) 301 { 302 if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 && 303 (*instr == 0x9e || *instr == 0x9f)) 304 return (1); 305 return (0); 306 } 307 308 /* 309 * Emulate the LAHF and SAHF instructions. The reference manuals define 310 * these instructions to always load/store bit 1 as a 1, and bits 3 and 5 311 * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P). 312 * 313 * Note that %ah is bits 8-15 of %rax. 314 */ 315 static void 316 emulate_lsahf(struct regs *rp, uchar_t instr) 317 { 318 if (instr == 0x9e) { 319 /* sahf. Copy bits from %ah to flags. */ 320 rp->r_ps = (rp->r_ps & ~0xff) | 321 ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1; 322 } else { 323 /* lahf. Copy bits from flags to %ah. */ 324 rp->r_rax = (rp->r_rax & ~0xff00) | 325 (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8); 326 } 327 rp->r_pc += LSAHFSIZE; 328 } 329 #endif /* __amd64 */ 330 331 #ifdef OPTERON_ERRATUM_91 332 333 /* 334 * Test to see if the instruction at pc is a prefetch instruction. 335 * 336 * The first byte of prefetch instructions is always 0x0F. 337 * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch. 338 * The third byte is between 0 and 3 inclusive. 339 */ 340 341 #define PREFETCHSIZE 3 342 343 static int 344 cmp_to_prefetch(uchar_t *p) 345 { 346 if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3) 347 return (1); 348 return (0); 349 } 350 351 static int 352 instr_is_prefetch(caddr_t pc) 353 { 354 uchar_t instr[PREFETCHSIZE]; 355 int error; 356 357 error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE); 358 359 if (error == 0 && cmp_to_prefetch(instr)) 360 return (1); 361 return (0); 362 } 363 364 #endif /* OPTERON_ERRATUM_91 */ 365 366 /* 367 * Called from the trap handler when a processor trap occurs. 368 * 369 * Note: All user-level traps that might call stop() must exit 370 * trap() by 'goto out' or by falling through. 371 */ 372 void 373 trap(struct regs *rp, caddr_t addr, processorid_t cpuid) 374 { 375 kthread_t *cur_thread = curthread; 376 enum seg_rw rw; 377 unsigned type; 378 extern int stop_on_fault(uint_t, k_siginfo_t *); 379 proc_t *p = ttoproc(cur_thread); 380 klwp_t *lwp = ttolwp(cur_thread); 381 uintptr_t lofault; 382 faultcode_t pagefault(), res, errcode; 383 enum fault_type fault_type; 384 k_siginfo_t siginfo; 385 uint_t fault = 0; 386 int mstate; 387 int sicode = 0; 388 int watchcode; 389 int watchpage; 390 caddr_t vaddr; 391 size_t sz; 392 int ta; 393 #ifdef __amd64 394 uchar_t instr; 395 #endif 396 397 ASSERT_STACK_ALIGNED(); 398 399 type = rp->r_trapno; 400 CPU_STATS_ADDQ(CPU, sys, trap, 1); 401 402 ASSERT(cur_thread->t_schedflag & TS_DONT_SWAP); 403 404 if (type == T_PGFLT) { 405 406 errcode = rp->r_err; 407 if (errcode & PF_ERR_WRITE) 408 rw = S_WRITE; 409 else if ((caddr_t)rp->r_pc == addr || 410 (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) 411 rw = S_EXEC; 412 else 413 rw = S_READ; 414 415 #if defined(__i386) 416 /* 417 * Pentium Pro work-around 418 */ 419 if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) { 420 uint_t attr; 421 uint_t priv_violation; 422 uint_t access_violation; 423 424 if (hat_getattr(addr < (caddr_t)kernelbase ? 425 curproc->p_as->a_hat : kas.a_hat, addr, &attr) 426 == -1) { 427 errcode &= ~PF_ERR_PROT; 428 } else { 429 priv_violation = (errcode & PF_ERR_USER) && 430 !(attr & PROT_USER); 431 access_violation = (errcode & PF_ERR_WRITE) && 432 !(attr & PROT_WRITE); 433 if (!priv_violation && !access_violation) 434 goto cleanup; 435 } 436 } 437 #endif /* __i386 */ 438 439 } 440 441 if (tdebug) 442 showregs(type, rp, addr); 443 444 if (USERMODE(rp->r_cs)) { 445 /* 446 * Set up the current cred to use during this trap. u_cred 447 * no longer exists. t_cred is used instead. 448 * The current process credential applies to the thread for 449 * the entire trap. If trapping from the kernel, this 450 * should already be set up. 451 */ 452 if (cur_thread->t_cred != p->p_cred) { 453 cred_t *oldcred = cur_thread->t_cred; 454 /* 455 * DTrace accesses t_cred in probe context. t_cred 456 * must always be either NULL, or point to a valid, 457 * allocated cred structure. 458 */ 459 cur_thread->t_cred = crgetcred(); 460 crfree(oldcred); 461 } 462 ASSERT(lwp != NULL); 463 type |= USER; 464 ASSERT(lwptoregs(lwp) == rp); 465 lwp->lwp_state = LWP_SYS; 466 467 switch (type) { 468 case T_PGFLT + USER: 469 if ((caddr_t)rp->r_pc == addr) 470 mstate = LMS_TFAULT; 471 else 472 mstate = LMS_DFAULT; 473 break; 474 default: 475 mstate = LMS_TRAP; 476 break; 477 } 478 /* Kernel probe */ 479 TNF_PROBE_1(thread_state, "thread", /* CSTYLED */, 480 tnf_microstate, state, mstate); 481 mstate = new_mstate(cur_thread, mstate); 482 483 bzero(&siginfo, sizeof (siginfo)); 484 } 485 486 switch (type) { 487 case T_PGFLT + USER: 488 case T_SGLSTP: 489 case T_SGLSTP + USER: 490 case T_BPTFLT + USER: 491 break; 492 493 default: 494 FTRACE_2("trap(): type=0x%lx, regs=0x%lx", 495 (ulong_t)type, (ulong_t)rp); 496 break; 497 } 498 499 switch (type) { 500 default: 501 if (type & USER) { 502 if (tudebug) 503 showregs(type, rp, (caddr_t)0); 504 printf("trap: Unknown trap type %d in user mode\n", 505 type & ~USER); 506 siginfo.si_signo = SIGILL; 507 siginfo.si_code = ILL_ILLTRP; 508 siginfo.si_addr = (caddr_t)rp->r_pc; 509 siginfo.si_trapno = type & ~USER; 510 fault = FLTILL; 511 break; 512 } else { 513 (void) die(type, rp, addr, cpuid); 514 /*NOTREACHED*/ 515 } 516 517 case T_PGFLT: /* system page fault */ 518 /* 519 * If we're under on_trap() protection (see <sys/ontrap.h>), 520 * set ot_trap and longjmp back to the on_trap() call site. 521 */ 522 if ((cur_thread->t_ontrap != NULL) && 523 (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) { 524 curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS; 525 longjmp(&curthread->t_ontrap->ot_jmpbuf); 526 } 527 528 /* 529 * See if we can handle as pagefault. Save lofault 530 * across this. Here we assume that an address 531 * less than KERNELBASE is a user fault. 532 * We can do this as copy.s routines verify that the 533 * starting address is less than KERNELBASE before 534 * starting and because we know that we always have 535 * KERNELBASE mapped as invalid to serve as a "barrier". 536 */ 537 lofault = cur_thread->t_lofault; 538 cur_thread->t_lofault = 0; 539 540 mstate = new_mstate(cur_thread, LMS_KFAULT); 541 542 if (addr < (caddr_t)kernelbase) { 543 res = pagefault(addr, 544 (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0); 545 if (res == FC_NOMAP && 546 addr < p->p_usrstack && 547 grow(addr)) 548 res = 0; 549 } else { 550 res = pagefault(addr, 551 (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1); 552 } 553 (void) new_mstate(cur_thread, mstate); 554 555 /* 556 * Restore lofault. If we resolved the fault, exit. 557 * If we didn't and lofault wasn't set, die. 558 */ 559 cur_thread->t_lofault = lofault; 560 if (res == 0) 561 goto cleanup; 562 563 #if defined(OPTERON_ERRATUM_93) && defined(_LP64) 564 if (lofault == 0 && opteron_erratum_93) { 565 /* 566 * Workaround for Opteron Erratum 93. On return from 567 * a System Managment Interrupt at a HLT instruction 568 * the %rip might be truncated to a 32 bit value. 569 * BIOS is supposed to fix this, but some don't. 570 * If this occurs we simply restore the high order bits. 571 * The HLT instruction is 1 byte of 0xf4. 572 */ 573 uintptr_t rip = rp->r_pc; 574 575 if ((rip & 0xfffffffful) == rip) { 576 rip |= 0xfffffffful << 32; 577 if (hat_getpfnum(kas.a_hat, (caddr_t)rip) != 578 PFN_INVALID && 579 (*(uchar_t *)rip == 0xf4 || 580 *(uchar_t *)(rip - 1) == 0xf4)) { 581 rp->r_pc = rip; 582 goto cleanup; 583 } 584 } 585 } 586 #endif /* OPTERON_ERRATUM_93 && _LP64 */ 587 588 #ifdef OPTERON_ERRATUM_91 589 if (lofault == 0 && opteron_erratum_91) { 590 /* 591 * Workaround for Opteron Erratum 91. Prefetches may 592 * generate a page fault (they're not supposed to do 593 * that!). If this occurs we simply return back to the 594 * instruction. 595 */ 596 caddr_t pc = (caddr_t)rp->r_pc; 597 598 /* 599 * If the faulting PC is not mapped, this is a 600 * legitimate kernel page fault that must result in a 601 * panic. If the faulting PC is mapped, it could contain 602 * a prefetch instruction. Check for that here. 603 */ 604 if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) { 605 if (cmp_to_prefetch((uchar_t *)pc)) { 606 #ifdef DEBUG 607 cmn_err(CE_WARN, "Opteron erratum 91 " 608 "occurred: kernel prefetch" 609 " at %p generated a page fault!", 610 (void *)rp->r_pc); 611 #endif /* DEBUG */ 612 goto cleanup; 613 } 614 } 615 (void) die(type, rp, addr, cpuid); 616 } 617 #endif /* OPTERON_ERRATUM_91 */ 618 619 if (lofault == 0) 620 (void) die(type, rp, addr, cpuid); 621 622 /* 623 * Cannot resolve fault. Return to lofault. 624 */ 625 if (lodebug) { 626 showregs(type, rp, addr); 627 traceregs(rp); 628 } 629 if (FC_CODE(res) == FC_OBJERR) 630 res = FC_ERRNO(res); 631 else 632 res = EFAULT; 633 rp->r_r0 = res; 634 rp->r_pc = cur_thread->t_lofault; 635 goto cleanup; 636 637 case T_PGFLT + USER: /* user page fault */ 638 if (faultdebug) { 639 char *fault_str; 640 641 switch (rw) { 642 case S_READ: 643 fault_str = "read"; 644 break; 645 case S_WRITE: 646 fault_str = "write"; 647 break; 648 case S_EXEC: 649 fault_str = "exec"; 650 break; 651 default: 652 fault_str = ""; 653 break; 654 } 655 printf("user %s fault: addr=0x%lx errcode=0x%x\n", 656 fault_str, (uintptr_t)addr, errcode); 657 } 658 659 #if defined(OPTERON_ERRATUM_100) && defined(_LP64) 660 /* 661 * Workaround for AMD erratum 100 662 * 663 * A 32-bit process may receive a page fault on a non 664 * 32-bit address by mistake. The range of the faulting 665 * address will be 666 * 667 * 0xffffffff80000000 .. 0xffffffffffffffff or 668 * 0x0000000100000000 .. 0x000000017fffffff 669 * 670 * The fault is always due to an instruction fetch, however 671 * the value of r_pc should be correct (in 32 bit range), 672 * so we ignore the page fault on the bogus address. 673 */ 674 if (p->p_model == DATAMODEL_ILP32 && 675 (0xffffffff80000000 <= (uintptr_t)addr || 676 (0x100000000 <= (uintptr_t)addr && 677 (uintptr_t)addr <= 0x17fffffff))) { 678 if (!opteron_erratum_100) 679 panic("unexpected erratum #100"); 680 if (rp->r_pc <= 0xffffffff) 681 goto out; 682 } 683 #endif /* OPTERON_ERRATUM_100 && _LP64 */ 684 685 ASSERT(!(curthread->t_flag & T_WATCHPT)); 686 watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw)); 687 #ifdef __i386 688 /* 689 * In 32-bit mode, the lcall (system call) instruction fetches 690 * one word from the stack, at the stack pointer, because of the 691 * way the call gate is constructed. This is a bogus 692 * read and should not be counted as a read watchpoint. 693 * We work around the problem here by testing to see if 694 * this situation applies and, if so, simply jumping to 695 * the code in locore.s that fields the system call trap. 696 * The registers on the stack are already set up properly 697 * due to the match between the call gate sequence and the 698 * trap gate sequence. We just have to adjust the pc. 699 */ 700 if (watchpage && addr == (caddr_t)rp->r_sp && 701 rw == S_READ && instr_is_syscall((caddr_t)rp->r_pc)) { 702 extern void watch_syscall(void); 703 704 rp->r_pc += LCALLSIZE; 705 watch_syscall(); /* never returns */ 706 /* NOTREACHED */ 707 } 708 #endif /* __i386 */ 709 vaddr = addr; 710 if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0) 711 fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL; 712 else if ((watchcode = pr_is_watchpoint(&vaddr, &ta, 713 sz, NULL, rw)) != 0) { 714 if (ta) { 715 do_watch_step(vaddr, sz, rw, 716 watchcode, rp->r_pc); 717 fault_type = F_INVAL; 718 } else { 719 bzero(&siginfo, sizeof (siginfo)); 720 siginfo.si_signo = SIGTRAP; 721 siginfo.si_code = watchcode; 722 siginfo.si_addr = vaddr; 723 siginfo.si_trapafter = 0; 724 siginfo.si_pc = (caddr_t)rp->r_pc; 725 fault = FLTWATCH; 726 break; 727 } 728 } else { 729 /* XXX pr_watch_emul() never succeeds (for now) */ 730 if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw)) 731 goto out; 732 do_watch_step(vaddr, sz, rw, 0, 0); 733 fault_type = F_INVAL; 734 } 735 736 res = pagefault(addr, fault_type, rw, 0); 737 738 /* 739 * If pagefault() succeeded, ok. 740 * Otherwise attempt to grow the stack. 741 */ 742 if (res == 0 || 743 (res == FC_NOMAP && 744 addr < p->p_usrstack && 745 grow(addr))) { 746 lwp->lwp_lastfault = FLTPAGE; 747 lwp->lwp_lastfaddr = addr; 748 if (prismember(&p->p_fltmask, FLTPAGE)) { 749 bzero(&siginfo, sizeof (siginfo)); 750 siginfo.si_addr = addr; 751 (void) stop_on_fault(FLTPAGE, &siginfo); 752 } 753 goto out; 754 } else if (res == FC_PROT && addr < p->p_usrstack && 755 (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) { 756 report_stack_exec(p, addr); 757 } 758 759 #ifdef OPTERON_ERRATUM_91 760 /* 761 * Workaround for Opteron Erratum 91. Prefetches may generate a 762 * page fault (they're not supposed to do that!). If this 763 * occurs we simply return back to the instruction. 764 * 765 * We rely on copyin to properly fault in the page with r_pc. 766 */ 767 if (opteron_erratum_91 && 768 addr != (caddr_t)rp->r_pc && 769 instr_is_prefetch((caddr_t)rp->r_pc)) { 770 #ifdef DEBUG 771 cmn_err(CE_WARN, "Opteron erratum 91 occurred: " 772 "prefetch at %p in pid %d generated a trap!", 773 (void *)rp->r_pc, p->p_pid); 774 #endif /* DEBUG */ 775 goto out; 776 } 777 #endif /* OPTERON_ERRATUM_91 */ 778 779 if (tudebug) 780 showregs(type, rp, addr); 781 /* 782 * In the case where both pagefault and grow fail, 783 * set the code to the value provided by pagefault. 784 * We map all errors returned from pagefault() to SIGSEGV. 785 */ 786 bzero(&siginfo, sizeof (siginfo)); 787 siginfo.si_addr = addr; 788 switch (FC_CODE(res)) { 789 case FC_HWERR: 790 case FC_NOSUPPORT: 791 siginfo.si_signo = SIGBUS; 792 siginfo.si_code = BUS_ADRERR; 793 fault = FLTACCESS; 794 break; 795 case FC_ALIGN: 796 siginfo.si_signo = SIGBUS; 797 siginfo.si_code = BUS_ADRALN; 798 fault = FLTACCESS; 799 break; 800 case FC_OBJERR: 801 if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) { 802 siginfo.si_signo = SIGBUS; 803 siginfo.si_code = BUS_OBJERR; 804 fault = FLTACCESS; 805 } 806 break; 807 default: /* FC_NOMAP or FC_PROT */ 808 siginfo.si_signo = SIGSEGV; 809 siginfo.si_code = 810 (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR; 811 fault = FLTBOUNDS; 812 break; 813 } 814 break; 815 816 case T_ILLINST + USER: /* invalid opcode fault */ 817 /* 818 * If the syscall instruction is disabled due to LDT usage, a 819 * user program that attempts to execute it will trigger a #ud 820 * trap. Check for that case here. If this occurs on a CPU which 821 * doesn't even support syscall, the result of all of this will 822 * be to emulate that particular instruction. 823 */ 824 if (p->p_ldt != NULL && 825 instr_is_fast_syscall((caddr_t)rp->r_pc, X86_ASYSC)) { 826 if (rewrite_syscall((caddr_t)rp->r_pc) == 0) 827 goto out; 828 #ifdef DEBUG 829 else 830 cmn_err(CE_WARN, "failed to rewrite syscall " 831 "instruction in process %d", 832 curthread->t_procp->p_pid); 833 #endif /* DEBUG */ 834 } 835 836 #ifdef __amd64 837 /* 838 * Emulate the LAHF and SAHF instructions if needed. 839 * See the instr_is_lsahf function for details. 840 */ 841 if (p->p_model == DATAMODEL_LP64 && 842 instr_is_lsahf((caddr_t)rp->r_pc, &instr)) { 843 emulate_lsahf(rp, instr); 844 goto out; 845 } 846 #endif 847 848 /*FALLTHROUGH*/ 849 850 if (tudebug) 851 showregs(type, rp, (caddr_t)0); 852 siginfo.si_signo = SIGILL; 853 siginfo.si_code = ILL_ILLOPC; 854 siginfo.si_addr = (caddr_t)rp->r_pc; 855 fault = FLTILL; 856 break; 857 858 case T_ZERODIV + USER: /* integer divide by zero */ 859 if (tudebug && tudebugfpe) 860 showregs(type, rp, (caddr_t)0); 861 siginfo.si_signo = SIGFPE; 862 siginfo.si_code = FPE_INTDIV; 863 siginfo.si_addr = (caddr_t)rp->r_pc; 864 fault = FLTIZDIV; 865 break; 866 867 case T_OVFLW + USER: /* integer overflow */ 868 if (tudebug && tudebugfpe) 869 showregs(type, rp, (caddr_t)0); 870 siginfo.si_signo = SIGFPE; 871 siginfo.si_code = FPE_INTOVF; 872 siginfo.si_addr = (caddr_t)rp->r_pc; 873 fault = FLTIOVF; 874 break; 875 876 case T_NOEXTFLT + USER: /* math coprocessor not available */ 877 if (tudebug && tudebugfpe) 878 showregs(type, rp, addr); 879 if (fpnoextflt(rp)) { 880 siginfo.si_signo = SIGFPE; 881 siginfo.si_code = ILL_ILLOPC; 882 siginfo.si_addr = (caddr_t)rp->r_pc; 883 fault = FLTFPE; 884 } 885 break; 886 887 case T_EXTOVRFLT: /* extension overrun fault */ 888 /* check if we took a kernel trap on behalf of user */ 889 { 890 extern void ndptrap_frstor(void); 891 if (rp->r_pc != (uintptr_t)ndptrap_frstor) 892 (void) die(type, rp, addr, cpuid); 893 type |= USER; 894 } 895 /*FALLTHROUGH*/ 896 case T_EXTOVRFLT + USER: /* extension overrun fault */ 897 if (tudebug && tudebugfpe) 898 showregs(type, rp, addr); 899 if (fpextovrflt(rp)) { 900 siginfo.si_signo = SIGSEGV; 901 siginfo.si_code = SEGV_MAPERR; 902 siginfo.si_addr = (caddr_t)rp->r_pc; 903 fault = FLTBOUNDS; 904 } 905 break; 906 907 case T_EXTERRFLT: /* x87 floating point exception pending */ 908 /* check if we took a kernel trap on behalf of user */ 909 { 910 extern void ndptrap_frstor(void); 911 if (rp->r_pc != (uintptr_t)ndptrap_frstor) 912 (void) die(type, rp, addr, cpuid); 913 type |= USER; 914 } 915 /*FALLTHROUGH*/ 916 917 case T_EXTERRFLT + USER: /* x87 floating point exception pending */ 918 if (tudebug && tudebugfpe) 919 showregs(type, rp, addr); 920 if (sicode = fpexterrflt(rp)) { 921 siginfo.si_signo = SIGFPE; 922 siginfo.si_code = sicode; 923 siginfo.si_addr = (caddr_t)rp->r_pc; 924 fault = FLTFPE; 925 } 926 break; 927 928 case T_SIMDFPE + USER: /* SSE and SSE2 exceptions */ 929 if (tudebug && tudebugsse) 930 showregs(type, rp, addr); 931 if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) { 932 /* 933 * There are rumours that some user instructions 934 * on older CPUs can cause this trap to occur; in 935 * which case send a SIGILL instead of a SIGFPE. 936 */ 937 siginfo.si_signo = SIGILL; 938 siginfo.si_code = ILL_ILLTRP; 939 siginfo.si_addr = (caddr_t)rp->r_pc; 940 siginfo.si_trapno = type & ~USER; 941 fault = FLTILL; 942 } else if ((sicode = fpsimderrflt(rp)) != 0) { 943 siginfo.si_signo = SIGFPE; 944 siginfo.si_code = sicode; 945 siginfo.si_addr = (caddr_t)rp->r_pc; 946 fault = FLTFPE; 947 } 948 break; 949 950 case T_BPTFLT: /* breakpoint trap */ 951 /* 952 * Kernel breakpoint traps should only happen when kmdb is 953 * active, and even then, it'll have interposed on the IDT, so 954 * control won't get here. If it does, we've hit a breakpoint 955 * without the debugger, which is very strange, and very 956 * fatal. 957 */ 958 if (tudebug && tudebugbpt) 959 showregs(type, rp, (caddr_t)0); 960 961 (void) die(type, rp, addr, cpuid); 962 break; 963 964 case T_SGLSTP: /* single step/hw breakpoint exception */ 965 if (tudebug && tudebugbpt) 966 showregs(type, rp, (caddr_t)0); 967 968 /* Now evaluate how we got here */ 969 if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) { 970 /* 971 * i386 single-steps even through lcalls which 972 * change the privilege level. So we take a trap at 973 * the first instruction in privileged mode. 974 * 975 * Set a flag to indicate that upon completion of 976 * the system call, deal with the single-step trap. 977 * 978 * The same thing happens for sysenter, too. 979 */ 980 #if defined(__amd64) 981 if (rp->r_pc == (uintptr_t)sys_sysenter) { 982 /* 983 * Adjust the pc so that we don't execute the 984 * swapgs instruction at the head of the 985 * handler and completely confuse things. 986 */ 987 rp->r_pc = (uintptr_t) 988 _sys_sysenter_post_swapgs; 989 #elif defined(__i386) 990 if (rp->r_pc == (uintptr_t)sys_call || 991 rp->r_pc == (uintptr_t)sys_sysenter) { 992 #endif 993 rp->r_ps &= ~PS_T; /* turn off trace */ 994 lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 995 cur_thread->t_post_sys = 1; 996 goto cleanup; 997 } 998 } 999 /* XXX - needs review on debugger interface? */ 1000 if (boothowto & RB_DEBUG) 1001 debug_enter((char *)NULL); 1002 else 1003 (void) die(type, rp, addr, cpuid); 1004 break; 1005 1006 case T_NMIFLT: /* NMI interrupt */ 1007 printf("Unexpected NMI in system mode\n"); 1008 goto cleanup; 1009 1010 case T_NMIFLT + USER: /* NMI interrupt */ 1011 printf("Unexpected NMI in user mode\n"); 1012 break; 1013 1014 case T_GPFLT: /* general protection violation */ 1015 #if defined(__amd64) 1016 /* 1017 * On amd64, we can get a #gp from referencing addresses 1018 * in the virtual address hole e.g. from a copyin. 1019 */ 1020 1021 /* 1022 * If we're under on_trap() protection (see <sys/ontrap.h>), 1023 * set ot_trap and longjmp back to the on_trap() call site. 1024 */ 1025 if ((cur_thread->t_ontrap != NULL) && 1026 (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) { 1027 curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS; 1028 longjmp(&curthread->t_ontrap->ot_jmpbuf); 1029 } 1030 1031 /* 1032 * If we're under lofault protection (copyin etc.), 1033 * longjmp back to lofault with an EFAULT. 1034 */ 1035 if (cur_thread->t_lofault) { 1036 /* 1037 * Fault is not resolvable, so just return to lofault 1038 */ 1039 if (lodebug) { 1040 showregs(type, rp, addr); 1041 traceregs(rp); 1042 } 1043 rp->r_r0 = EFAULT; 1044 rp->r_pc = cur_thread->t_lofault; 1045 goto cleanup; 1046 } 1047 /*FALLTHROUGH*/ 1048 #endif 1049 case T_STKFLT: /* stack fault */ 1050 case T_TSSFLT: /* invalid TSS fault */ 1051 case T_SEGFLT: /* segment not present fault */ 1052 if (tudebug) 1053 showregs(type, rp, (caddr_t)0); 1054 if (kern_gpfault(rp)) 1055 (void) die(type, rp, addr, cpuid); 1056 goto cleanup; 1057 /*FALLTHROUGH*/ 1058 1059 /* 1060 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps should have 1061 * no legacy need for them, so we put a stop to it here. 1062 * 1063 * So: not-present fault is ONLY valid for 32-bit processes with a private LDT 1064 * trying to do a system call. Emulate it. 1065 * 1066 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT have private 1067 * LDT, and are trying to do a system call. Emulate it. 1068 */ 1069 case T_SEGFLT + USER: /* segment not present fault */ 1070 case T_GPFLT + USER: /* general protection violation */ 1071 #ifdef _SYSCALL32_IMPL 1072 if (p->p_model != DATAMODEL_NATIVE) { 1073 #endif /* _SYSCALL32_IMPL */ 1074 if (instr_is_syscall((caddr_t)rp->r_pc)) { 1075 if (type == T_SEGFLT + USER) 1076 ASSERT(p->p_ldt != NULL); 1077 1078 if ((p->p_ldt == NULL && type == T_GPFLT + USER) || 1079 type == T_SEGFLT + USER) { 1080 1081 /* 1082 * The user attempted a system call via the obsolete 1083 * call gate mechanism. Because the process doesn't have 1084 * an LDT (i.e. the ldtr contains 0), a #gp results. 1085 * Emulate the syscall here, just as we do above for a 1086 * #np trap. 1087 */ 1088 1089 /* 1090 * Since this is a not-present trap, rp->r_pc points to 1091 * the trapping lcall instruction. We need to bump it 1092 * to the next insn so the app can continue on. 1093 */ 1094 rp->r_pc += LCALLSIZE; 1095 lwp->lwp_regs = rp; 1096 1097 /* 1098 * Normally the microstate of the LWP is forced back to 1099 * LMS_USER by the syscall handlers. Emulate that 1100 * behavior here. 1101 */ 1102 mstate = LMS_USER; 1103 1104 dosyscall(); 1105 goto out; 1106 } 1107 } 1108 #ifdef _SYSCALL32_IMPL 1109 } 1110 #endif /* _SYSCALL32_IMPL */ 1111 /* 1112 * If the current process is using a private LDT and the 1113 * trapping instruction is sysenter, the sysenter instruction 1114 * has been disabled on the CPU because it destroys segment 1115 * registers. If this is the case, rewrite the instruction to 1116 * be a safe system call and retry it. If this occurs on a CPU 1117 * which doesn't even support sysenter, the result of all of 1118 * this will be to emulate that particular instruction. 1119 */ 1120 if (p->p_ldt != NULL && 1121 instr_is_fast_syscall((caddr_t)rp->r_pc, X86_SEP)) { 1122 if (rewrite_syscall((caddr_t)rp->r_pc) == 0) 1123 goto out; 1124 #ifdef DEBUG 1125 else 1126 cmn_err(CE_WARN, "failed to rewrite sysenter " 1127 "instruction in process %d", 1128 curthread->t_procp->p_pid); 1129 #endif /* DEBUG */ 1130 } 1131 /*FALLTHROUGH*/ 1132 1133 case T_BOUNDFLT + USER: /* bound fault */ 1134 case T_STKFLT + USER: /* stack fault */ 1135 case T_TSSFLT + USER: /* invalid TSS fault */ 1136 if (tudebug) 1137 showregs(type, rp, (caddr_t)0); 1138 siginfo.si_signo = SIGSEGV; 1139 siginfo.si_code = SEGV_MAPERR; 1140 siginfo.si_addr = (caddr_t)rp->r_pc; 1141 fault = FLTBOUNDS; 1142 break; 1143 1144 case T_ALIGNMENT + USER: /* user alignment error (486) */ 1145 if (tudebug) 1146 showregs(type, rp, (caddr_t)0); 1147 bzero(&siginfo, sizeof (siginfo)); 1148 siginfo.si_signo = SIGBUS; 1149 siginfo.si_code = BUS_ADRALN; 1150 siginfo.si_addr = (caddr_t)rp->r_pc; 1151 fault = FLTACCESS; 1152 break; 1153 1154 case T_SGLSTP + USER: /* single step/hw breakpoint exception */ 1155 if (tudebug && tudebugbpt) 1156 showregs(type, rp, (caddr_t)0); 1157 1158 /* Was it single-stepping? */ 1159 if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) { 1160 pcb_t *pcb = &lwp->lwp_pcb; 1161 1162 rp->r_ps &= ~PS_T; 1163 /* 1164 * If both NORMAL_STEP and WATCH_STEP are in effect, 1165 * give precedence to NORMAL_STEP. If neither is set, 1166 * user must have set the PS_T bit in %efl; treat this 1167 * as NORMAL_STEP. 1168 */ 1169 if ((pcb->pcb_flags & NORMAL_STEP) || 1170 !(pcb->pcb_flags & WATCH_STEP)) { 1171 siginfo.si_signo = SIGTRAP; 1172 siginfo.si_code = TRAP_TRACE; 1173 siginfo.si_addr = (caddr_t)rp->r_pc; 1174 fault = FLTTRACE; 1175 if (pcb->pcb_flags & WATCH_STEP) 1176 (void) undo_watch_step(NULL); 1177 } else { 1178 fault = undo_watch_step(&siginfo); 1179 } 1180 pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP); 1181 } else { 1182 cmn_err(CE_WARN, 1183 "Unexpected INT 1 in user mode, dr6=%lx", 1184 lwp->lwp_pcb.pcb_drstat); 1185 } 1186 break; 1187 1188 case T_BPTFLT + USER: /* breakpoint trap */ 1189 if (tudebug && tudebugbpt) 1190 showregs(type, rp, (caddr_t)0); 1191 /* 1192 * int 3 (the breakpoint instruction) leaves the pc referring 1193 * to the address one byte after the breakpointed address. 1194 * If the P_PR_BPTADJ flag has been set via /proc, We adjust 1195 * it back so it refers to the breakpointed address. 1196 */ 1197 if (p->p_proc_flag & P_PR_BPTADJ) 1198 rp->r_pc--; 1199 siginfo.si_signo = SIGTRAP; 1200 siginfo.si_code = TRAP_BRKPT; 1201 siginfo.si_addr = (caddr_t)rp->r_pc; 1202 fault = FLTBPT; 1203 break; 1204 1205 case T_AST: 1206 /* 1207 * This occurs only after the cs register has been made to 1208 * look like a kernel selector, either through debugging or 1209 * possibly by functions like setcontext(). The thread is 1210 * about to cause a general protection fault at common_iret() 1211 * in locore. We let that happen immediately instead of 1212 * doing the T_AST processing. 1213 */ 1214 goto cleanup; 1215 1216 case T_AST + USER: /* profiling or resched pseudo trap */ 1217 if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) { 1218 lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW; 1219 if (kcpc_overflow_ast()) { 1220 /* 1221 * Signal performance counter overflow 1222 */ 1223 if (tudebug) 1224 showregs(type, rp, (caddr_t)0); 1225 bzero(&siginfo, sizeof (siginfo)); 1226 siginfo.si_signo = SIGEMT; 1227 siginfo.si_code = EMT_CPCOVF; 1228 siginfo.si_addr = (caddr_t)rp->r_pc; 1229 fault = FLTCPCOVF; 1230 } 1231 } 1232 break; 1233 } 1234 1235 /* 1236 * We can't get here from a system trap 1237 */ 1238 ASSERT(type & USER); 1239 1240 if (fault) { 1241 /* 1242 * Remember the fault and fault adddress 1243 * for real-time (SIGPROF) profiling. 1244 */ 1245 lwp->lwp_lastfault = fault; 1246 lwp->lwp_lastfaddr = siginfo.si_addr; 1247 1248 DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo); 1249 1250 /* 1251 * If a debugger has declared this fault to be an 1252 * event of interest, stop the lwp. Otherwise just 1253 * deliver the associated signal. 1254 */ 1255 if (siginfo.si_signo != SIGKILL && 1256 prismember(&p->p_fltmask, fault) && 1257 stop_on_fault(fault, &siginfo) == 0) 1258 siginfo.si_signo = 0; 1259 } 1260 1261 if (siginfo.si_signo) 1262 trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1); 1263 1264 if (lwp->lwp_oweupc) 1265 profil_tick(rp->r_pc); 1266 1267 if (cur_thread->t_astflag | cur_thread->t_sig_check) { 1268 /* 1269 * Turn off the AST flag before checking all the conditions that 1270 * may have caused an AST. This flag is on whenever a signal or 1271 * unusual condition should be handled after the next trap or 1272 * syscall. 1273 */ 1274 astoff(cur_thread); 1275 cur_thread->t_sig_check = 0; 1276 1277 mutex_enter(&p->p_lock); 1278 if (curthread->t_proc_flag & TP_CHANGEBIND) { 1279 timer_lwpbind(); 1280 curthread->t_proc_flag &= ~TP_CHANGEBIND; 1281 } 1282 mutex_exit(&p->p_lock); 1283 1284 /* 1285 * for kaio requests that are on the per-process poll queue, 1286 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel 1287 * should copyout their result_t to user memory. by copying 1288 * out the result_t, the user can poll on memory waiting 1289 * for the kaio request to complete. 1290 */ 1291 if (p->p_aio) 1292 aio_cleanup(0); 1293 /* 1294 * If this LWP was asked to hold, call holdlwp(), which will 1295 * stop. holdlwps() sets this up and calls pokelwps() which 1296 * sets the AST flag. 1297 * 1298 * Also check TP_EXITLWP, since this is used by fresh new LWPs 1299 * through lwp_rtt(). That flag is set if the lwp_create(2) 1300 * syscall failed after creating the LWP. 1301 */ 1302 if (ISHOLD(p)) 1303 holdlwp(); 1304 1305 /* 1306 * All code that sets signals and makes ISSIG evaluate true must 1307 * set t_astflag afterwards. 1308 */ 1309 if (ISSIG_PENDING(cur_thread, lwp, p)) { 1310 if (issig(FORREAL)) 1311 psig(); 1312 cur_thread->t_sig_check = 1; 1313 } 1314 1315 if (cur_thread->t_rprof != NULL) { 1316 realsigprof(0, 0); 1317 cur_thread->t_sig_check = 1; 1318 } 1319 } 1320 1321 out: /* We can't get here from a system trap */ 1322 ASSERT(type & USER); 1323 1324 if (ISHOLD(p)) 1325 holdlwp(); 1326 1327 /* 1328 * Set state to LWP_USER here so preempt won't give us a kernel 1329 * priority if it occurs after this point. Call CL_TRAPRET() to 1330 * restore the user-level priority. 1331 * 1332 * It is important that no locks (other than spinlocks) be entered 1333 * after this point before returning to user mode (unless lwp_state 1334 * is set back to LWP_SYS). 1335 */ 1336 lwp->lwp_state = LWP_USER; 1337 1338 if (cur_thread->t_trapret) { 1339 cur_thread->t_trapret = 0; 1340 thread_lock(cur_thread); 1341 CL_TRAPRET(cur_thread); 1342 thread_unlock(cur_thread); 1343 } 1344 if (CPU->cpu_runrun) 1345 preempt(); 1346 (void) new_mstate(cur_thread, mstate); 1347 1348 /* Kernel probe */ 1349 TNF_PROBE_1(thread_state, "thread", /* CSTYLED */, 1350 tnf_microstate, state, LMS_USER); 1351 1352 return; 1353 1354 cleanup: /* system traps end up here */ 1355 ASSERT(!(type & USER)); 1356 } 1357 1358 /* 1359 * Patch non-zero to disable preemption of threads in the kernel. 1360 */ 1361 int IGNORE_KERNEL_PREEMPTION = 0; /* XXX - delete this someday */ 1362 1363 struct kpreempt_cnts { /* kernel preemption statistics */ 1364 int kpc_idle; /* executing idle thread */ 1365 int kpc_intr; /* executing interrupt thread */ 1366 int kpc_clock; /* executing clock thread */ 1367 int kpc_blocked; /* thread has blocked preemption (t_preempt) */ 1368 int kpc_notonproc; /* thread is surrendering processor */ 1369 int kpc_inswtch; /* thread has ratified scheduling decision */ 1370 int kpc_prilevel; /* processor interrupt level is too high */ 1371 int kpc_apreempt; /* asynchronous preemption */ 1372 int kpc_spreempt; /* synchronous preemption */ 1373 } kpreempt_cnts; 1374 1375 /* 1376 * kernel preemption: forced rescheduling, preempt the running kernel thread. 1377 * the argument is old PIL for an interrupt, 1378 * or the distingished value KPREEMPT_SYNC. 1379 */ 1380 void 1381 kpreempt(int asyncspl) 1382 { 1383 kthread_t *cur_thread = curthread; 1384 1385 if (IGNORE_KERNEL_PREEMPTION) { 1386 aston(CPU->cpu_dispthread); 1387 return; 1388 } 1389 1390 /* 1391 * Check that conditions are right for kernel preemption 1392 */ 1393 do { 1394 if (cur_thread->t_preempt) { 1395 /* 1396 * either a privileged thread (idle, panic, interrupt) 1397 * or will check when t_preempt is lowered 1398 */ 1399 if (cur_thread->t_pri < 0) 1400 kpreempt_cnts.kpc_idle++; 1401 else if (cur_thread->t_flag & T_INTR_THREAD) { 1402 kpreempt_cnts.kpc_intr++; 1403 if (cur_thread->t_pil == CLOCK_LEVEL) 1404 kpreempt_cnts.kpc_clock++; 1405 } else 1406 kpreempt_cnts.kpc_blocked++; 1407 aston(CPU->cpu_dispthread); 1408 return; 1409 } 1410 if (cur_thread->t_state != TS_ONPROC || 1411 cur_thread->t_disp_queue != CPU->cpu_disp) { 1412 /* this thread will be calling swtch() shortly */ 1413 kpreempt_cnts.kpc_notonproc++; 1414 if (CPU->cpu_thread != CPU->cpu_dispthread) { 1415 /* already in swtch(), force another */ 1416 kpreempt_cnts.kpc_inswtch++; 1417 siron(); 1418 } 1419 return; 1420 } 1421 if (getpil() >= DISP_LEVEL) { 1422 /* 1423 * We can't preempt this thread if it is at 1424 * a PIL >= DISP_LEVEL since it may be holding 1425 * a spin lock (like sched_lock). 1426 */ 1427 siron(); /* check back later */ 1428 kpreempt_cnts.kpc_prilevel++; 1429 return; 1430 } 1431 1432 if (asyncspl != KPREEMPT_SYNC) 1433 kpreempt_cnts.kpc_apreempt++; 1434 else 1435 kpreempt_cnts.kpc_spreempt++; 1436 1437 cur_thread->t_preempt++; 1438 preempt(); 1439 cur_thread->t_preempt--; 1440 } while (CPU->cpu_kprunrun); 1441 } 1442 1443 /* 1444 * Print out debugging info. 1445 */ 1446 static void 1447 showregs(uint_t type, struct regs *rp, caddr_t addr) 1448 { 1449 int s; 1450 1451 s = spl7(); 1452 type &= ~USER; 1453 if (u.u_comm[0]) 1454 printf("%s: ", u.u_comm); 1455 if (type < TRAP_TYPES) 1456 printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]); 1457 else 1458 switch (type) { 1459 case T_SYSCALL: 1460 printf("Syscall Trap:\n"); 1461 break; 1462 case T_AST: 1463 printf("AST\n"); 1464 break; 1465 default: 1466 printf("Bad Trap = %d\n", type); 1467 break; 1468 } 1469 if (type == T_PGFLT) { 1470 printf("Bad %s fault at addr=0x%lx\n", 1471 USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr); 1472 } else if (addr) { 1473 printf("addr=0x%lx\n", (uintptr_t)addr); 1474 } 1475 1476 printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n", 1477 (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ? 1478 ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps); 1479 1480 #if defined(__lint) 1481 /* 1482 * this clause can be deleted when lint bug 4870403 is fixed 1483 * (lint thinks that bit 32 is illegal in a %b format string) 1484 */ 1485 printf("cr0: %x cr4: %b\n", 1486 (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4); 1487 #else 1488 printf("cr0: %b cr4: %b\n", 1489 (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4); 1490 #endif 1491 1492 #if defined(__amd64) 1493 printf("cr2: %lx cr3: %lx cr8: %lx\n", getcr2(), getcr3(), getcr8()); 1494 #elif defined(__i386) 1495 printf("cr2: %lx cr3: %lx\n", getcr2(), getcr3()); 1496 #endif 1497 1498 dumpregs(rp); 1499 splx(s); 1500 } 1501 1502 static void 1503 dumpregs(struct regs *rp) 1504 { 1505 #if defined(__amd64) 1506 const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n"; 1507 1508 printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx); 1509 printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9); 1510 printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp); 1511 printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12); 1512 printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15); 1513 1514 printf(fmt, "fsb", rp->r_fsbase, "gsb", rp->r_gsbase, " ds", rp->r_ds); 1515 printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs); 1516 1517 printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip); 1518 printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp); 1519 1520 printf("\t%3s: %16lx\n", " ss", rp->r_ss); 1521 1522 #elif defined(__i386) 1523 const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n"; 1524 1525 printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs, 1526 " es", rp->r_es, " ds", rp->r_ds); 1527 printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi, 1528 "ebp", rp->r_ebp, "esp", rp->r_esp); 1529 printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx, 1530 "ecx", rp->r_ecx, "eax", rp->r_eax); 1531 printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, 1532 "eip", rp->r_eip, " cs", rp->r_cs); 1533 printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n", 1534 "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss); 1535 1536 #endif /* __i386 */ 1537 } 1538 1539 /* 1540 * Handle #gp faults in kernel mode. 1541 * 1542 * One legitimate way this can happen is if we attempt to update segment 1543 * registers to naughty values on the way out of the kernel. 1544 * 1545 * This can happen in a couple of ways: someone - either accidentally or 1546 * on purpose - creates (setcontext(2), lwp_create(2)) or modifies 1547 * (signal(2)) a ucontext that contains silly segment register values. 1548 * Or someone - either accidentally or on purpose - modifies the prgregset_t 1549 * of a subject process via /proc to contain silly segment register values. 1550 * 1551 * (The unfortunate part is that we can end up discovering the bad segment 1552 * register value in the middle of an 'iret' after we've popped most of the 1553 * stack. So it becomes quite difficult to associate an accurate ucontext 1554 * with the lwp, because the act of taking the #gp trap overwrites most of 1555 * what we were going to send the lwp.) 1556 * 1557 * OTOH if it turns out that's -not- the problem, and we're -not- an lwp 1558 * trying to return to user mode and we get a #gp fault, then we need 1559 * to die() -- which will happen if we return non-zero from this routine. 1560 */ 1561 static int 1562 kern_gpfault(struct regs *rp) 1563 { 1564 kthread_t *t = curthread; 1565 proc_t *p = ttoproc(t); 1566 klwp_t *lwp = ttolwp(t); 1567 struct regs tmpregs, *trp = NULL; 1568 caddr_t pc = (caddr_t)rp->r_pc; 1569 int v; 1570 1571 extern void _sys_rtt(), sr_sup(); 1572 1573 #if defined(__amd64) 1574 extern void _update_sregs(), _update_sregs_done(); 1575 static const uint8_t iretq_insn[2] = { 0x48, 0xcf }; 1576 1577 #elif defined(__i386) 1578 static const uint8_t iret_insn[1] = { 0xcf }; 1579 1580 /* 1581 * Note carefully the appallingly awful dependency between 1582 * the instruction sequence used in __SEGREGS_POP and these 1583 * instructions encoded here. 1584 * 1585 * XX64 Add some commentary to locore.s/privregs.h to document this. 1586 */ 1587 static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 }; 1588 static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 }; 1589 static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 }; 1590 static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc }; 1591 #endif 1592 /* 1593 * if we're not an lwp, or the pc range is outside _sys_rtt, then 1594 * we should immediately be die()ing horribly 1595 */ 1596 if (lwp == NULL || 1597 (uintptr_t)pc < (uintptr_t)_sys_rtt || 1598 (uintptr_t)pc > (uintptr_t)sr_sup) 1599 return (1); 1600 1601 /* 1602 * So at least we're in the right part of the kernel. 1603 * 1604 * Disassemble the instruction at the faulting pc. 1605 * Once we know what it is, we carefully reconstruct the stack 1606 * based on the order in which the stack is deconstructed in 1607 * _sys_rtt. Ew. 1608 */ 1609 1610 #if defined(__amd64) 1611 1612 if (bcmp(pc, iretq_insn, sizeof (iretq_insn)) == 0) { 1613 /* 1614 * We took the #gp while trying to perform the iretq. 1615 * This means that either %cs or %ss are bad. 1616 * All we know for sure is that most of the general 1617 * registers have been restored, including the 1618 * segment registers, and all we have left on the 1619 * topmost part of the lwp's stack are the 1620 * registers that the iretq was unable to consume. 1621 * 1622 * All the rest of the state was crushed by the #gp 1623 * which pushed -its- registers atop our old save area 1624 * (because we had to decrement the stack pointer, sigh) so 1625 * all that we can try and do is to reconstruct the 1626 * crushed frame from the #gp trap frame itself. 1627 */ 1628 trp = &tmpregs; 1629 trp->r_ss = lwptoregs(lwp)->r_ss; 1630 trp->r_sp = lwptoregs(lwp)->r_sp; 1631 trp->r_ps = lwptoregs(lwp)->r_ps; 1632 trp->r_cs = lwptoregs(lwp)->r_cs; 1633 trp->r_pc = lwptoregs(lwp)->r_pc; 1634 bcopy(rp, trp, offsetof(struct regs, r_pc)); 1635 1636 /* 1637 * Validate simple math 1638 */ 1639 ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc); 1640 ASSERT(trp->r_err == rp->r_err); 1641 1642 } else if ((lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) != 0 && 1643 pc >= (caddr_t)_update_sregs && 1644 pc < (caddr_t)_update_sregs_done) { 1645 /* 1646 * This is the common case -- we're trying to load 1647 * a bad segment register value in the only section 1648 * of kernel code that ever loads segment registers. 1649 * 1650 * We don't need to do anything at this point because 1651 * the pcb contains all the pending segment register 1652 * state, and the regs are still intact because we 1653 * didn't adjust the stack pointer yet. Given the fidelity 1654 * of all this, we could conceivably send a signal 1655 * to the lwp, rather than core-ing. 1656 */ 1657 trp = lwptoregs(lwp); 1658 ASSERT((caddr_t)trp == (caddr_t)rp->r_sp); 1659 } 1660 1661 #elif defined(__i386) 1662 1663 if (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0) { 1664 /* 1665 * We took the #gp while trying to perform the iret. 1666 * This means that either %cs or %ss are bad. 1667 * All we know for sure is that most of the general 1668 * registers have been restored, including the 1669 * segment registers, and all we have left on the 1670 * topmost part of the lwp's stack are the registers that 1671 * the iret was unable to consume. 1672 * 1673 * All the rest of the state was crushed by the #gp 1674 * which pushed -its- registers atop our old save area 1675 * (because we had to decrement the stack pointer, sigh) so 1676 * all that we can try and do is to reconstruct the 1677 * crushed frame from the #gp trap frame itself. 1678 */ 1679 trp = &tmpregs; 1680 trp->r_ss = lwptoregs(lwp)->r_ss; 1681 trp->r_sp = lwptoregs(lwp)->r_sp; 1682 trp->r_ps = lwptoregs(lwp)->r_ps; 1683 trp->r_cs = lwptoregs(lwp)->r_cs; 1684 trp->r_pc = lwptoregs(lwp)->r_pc; 1685 bcopy(rp, trp, offsetof(struct regs, r_pc)); 1686 1687 ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc); 1688 ASSERT(trp->r_err == rp->r_err); 1689 1690 } else { 1691 /* 1692 * Segment registers are reloaded in _sys_rtt 1693 * via the following sequence: 1694 * 1695 * movw 0(%esp), %gs 1696 * movw 4(%esp), %fs 1697 * movw 8(%esp), %es 1698 * movw 12(%esp), %ds 1699 * addl $16, %esp 1700 * 1701 * Thus if any of them fault, we know the user 1702 * registers are left unharmed on the stack. 1703 */ 1704 if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 || 1705 bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 || 1706 bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 || 1707 bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0) 1708 trp = lwptoregs(lwp); 1709 } 1710 #endif /* __amd64 */ 1711 1712 if (trp == NULL) 1713 return (1); 1714 1715 /* 1716 * If we get to here, we're reasonably confident that we've 1717 * correctly decoded what happened on the way out of the kernel. 1718 * Rewrite the lwp's registers so that we can create a core dump 1719 * the (at least vaguely) represents the mcontext we were 1720 * being asked to restore when things went so terribly wrong. 1721 */ 1722 1723 /* 1724 * Make sure that we have a meaningful %trapno and %err. 1725 */ 1726 trp->r_trapno = rp->r_trapno; 1727 trp->r_err = rp->r_err; 1728 1729 if ((caddr_t)trp != (caddr_t)lwptoregs(lwp)) 1730 bcopy(trp, lwptoregs(lwp), sizeof (*trp)); 1731 1732 mutex_enter(&p->p_lock); 1733 lwp->lwp_cursig = SIGSEGV; 1734 mutex_exit(&p->p_lock); 1735 1736 /* 1737 * Terminate all LWPs but don't discard them. If another lwp beat us to 1738 * the punch by calling exit(), evaporate now. 1739 */ 1740 proc_is_exiting(p); 1741 if (exitlwps(1) != 0) { 1742 mutex_enter(&p->p_lock); 1743 lwp_exit(); 1744 } 1745 1746 #ifdef C2_AUDIT 1747 if (audit_active) /* audit core dump */ 1748 audit_core_start(SIGSEGV); 1749 #endif 1750 v = core(SIGSEGV, B_FALSE); 1751 #ifdef C2_AUDIT 1752 if (audit_active) /* audit core dump */ 1753 audit_core_finish(v ? CLD_KILLED : CLD_DUMPED); 1754 #endif 1755 exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV); 1756 return (0); 1757 } 1758 1759 /* 1760 * dump_tss() - Display the TSS structure 1761 */ 1762 1763 #if defined(__amd64) 1764 1765 static void 1766 dump_tss(void) 1767 { 1768 const char tss_fmt[] = "tss.%s:\t0x%p\n"; /* Format string */ 1769 struct tss *tss = CPU->cpu_tss; 1770 1771 printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0); 1772 printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1); 1773 printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2); 1774 1775 printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1); 1776 printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2); 1777 printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3); 1778 printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4); 1779 printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5); 1780 printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6); 1781 printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7); 1782 } 1783 1784 #elif defined(__i386) 1785 1786 static void 1787 dump_tss(void) 1788 { 1789 const char tss_fmt[] = "tss.%s:\t0x%p\n"; /* Format string */ 1790 struct tss *tss = CPU->cpu_tss; 1791 1792 printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link); 1793 printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0); 1794 printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0); 1795 printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1); 1796 printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1); 1797 printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2); 1798 printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2); 1799 printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3); 1800 printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip); 1801 printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags); 1802 printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax); 1803 printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx); 1804 printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx); 1805 printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx); 1806 printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp); 1807 } 1808 1809 #endif /* __amd64 */ 1810 1811 #if defined(TRAPTRACE) 1812 1813 int ttrace_nrec = 0; /* number of records to dump out */ 1814 int ttrace_dump_nregs = 5; /* dump out this many records with regs too */ 1815 1816 /* 1817 * Dump out the last ttrace_nrec traptrace records on each CPU 1818 */ 1819 static void 1820 dump_ttrace(void) 1821 { 1822 trap_trace_ctl_t *ttc; 1823 trap_trace_rec_t *rec; 1824 uintptr_t current; 1825 int i, j, k; 1826 int n = NCPU; 1827 #if defined(__amd64) 1828 const char banner[] = 1829 "\ncpu address timestamp " 1830 "type vc handler pc\n"; 1831 const char fmt1[] = "%3d %016lx %12llx "; 1832 #elif defined(__i386) 1833 const char banner[] = 1834 "\ncpu address timestamp type vc handler pc\n"; 1835 const char fmt1[] = "%3d %08lx %12llx "; 1836 #endif 1837 const char fmt2[] = "%4s %3x "; 1838 const char fmt3[] = "%8s "; 1839 1840 if (ttrace_nrec == 0) 1841 return; 1842 1843 printf(banner); 1844 1845 for (i = 0; i < n; i++) { 1846 ttc = &trap_trace_ctl[i]; 1847 if (ttc->ttc_first == NULL) 1848 continue; 1849 1850 current = ttc->ttc_next - sizeof (trap_trace_rec_t); 1851 for (j = 0; j < ttrace_nrec; j++) { 1852 struct sysent *sys; 1853 struct autovec *vec; 1854 extern struct av_head autovect[]; 1855 int type; 1856 ulong_t off; 1857 char *sym, *stype; 1858 1859 if (current < ttc->ttc_first) 1860 current = 1861 ttc->ttc_limit - sizeof (trap_trace_rec_t); 1862 1863 if (current == NULL) 1864 continue; 1865 1866 rec = (trap_trace_rec_t *)current; 1867 1868 if (rec->ttr_stamp == 0) 1869 break; 1870 1871 printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp); 1872 1873 switch (rec->ttr_marker) { 1874 case TT_SYSCALL: 1875 case TT_SYSENTER: 1876 case TT_SYSC: 1877 case TT_SYSC64: 1878 #if defined(__amd64) 1879 sys = &sysent32[rec->ttr_sysnum]; 1880 switch (rec->ttr_marker) { 1881 case TT_SYSC64: 1882 sys = &sysent[rec->ttr_sysnum]; 1883 /*FALLTHROUGH*/ 1884 #elif defined(__i386) 1885 sys = &sysent[rec->ttr_sysnum]; 1886 switch (rec->ttr_marker) { 1887 case TT_SYSC64: 1888 #endif 1889 case TT_SYSC: 1890 stype = "sysc"; /* syscall */ 1891 break; 1892 case TT_SYSCALL: 1893 stype = "lcal"; /* lcall */ 1894 break; 1895 case TT_SYSENTER: 1896 stype = "syse"; /* sysenter */ 1897 break; 1898 default: 1899 break; 1900 } 1901 printf(fmt2, "sysc", rec->ttr_sysnum); 1902 if (sys != NULL) { 1903 sym = kobj_getsymname( 1904 (uintptr_t)sys->sy_callc, 1905 &off); 1906 if (sym != NULL) 1907 printf("%s ", sym); 1908 else 1909 printf("%p ", sys->sy_callc); 1910 } else { 1911 printf("unknown "); 1912 } 1913 break; 1914 1915 case TT_INTERRUPT: 1916 printf(fmt2, "intr", rec->ttr_vector); 1917 vec = (&autovect[rec->ttr_vector])->avh_link; 1918 if (vec != NULL) { 1919 sym = kobj_getsymname( 1920 (uintptr_t)vec->av_vector, &off); 1921 if (sym != NULL) 1922 printf("%s ", sym); 1923 else 1924 printf("%p ", vec->av_vector); 1925 } else { 1926 printf("unknown "); 1927 } 1928 break; 1929 1930 case TT_TRAP: 1931 type = rec->ttr_regs.r_trapno; 1932 printf(fmt2, "trap", type); 1933 printf("#%s ", type < TRAP_TYPES ? 1934 trap_type_mnemonic[type] : "trap"); 1935 break; 1936 1937 default: 1938 break; 1939 } 1940 1941 sym = kobj_getsymname(rec->ttr_regs.r_pc, &off); 1942 if (sym != NULL) 1943 printf("%s+%lx\n", sym, off); 1944 else 1945 printf("%lx\n", rec->ttr_regs.r_pc); 1946 1947 if (ttrace_dump_nregs-- > 0) { 1948 int s; 1949 1950 if (rec->ttr_marker == TT_INTERRUPT) 1951 printf( 1952 "\t\tipl %x spl %x pri %x\n", 1953 rec->ttr_ipl, 1954 rec->ttr_spl, 1955 rec->ttr_pri); 1956 1957 dumpregs(&rec->ttr_regs); 1958 1959 printf("\t%3s: %p\n\n", " ct", 1960 (void *)rec->ttr_curthread); 1961 1962 /* 1963 * print out the pc stack that we recorded 1964 * at trap time (if any) 1965 */ 1966 for (s = 0; s < rec->ttr_sdepth; s++) { 1967 uintptr_t fullpc; 1968 1969 if (s >= TTR_STACK_DEPTH) { 1970 printf("ttr_sdepth corrupt\n"); 1971 break; 1972 } 1973 1974 fullpc = (uintptr_t)rec->ttr_stack[s]; 1975 1976 sym = kobj_getsymname(fullpc, &off); 1977 if (sym != NULL) 1978 printf("-> %s+0x%lx()\n", 1979 sym, off); 1980 else 1981 printf("-> 0x%lx()\n", fullpc); 1982 } 1983 printf("\n"); 1984 } 1985 current -= sizeof (trap_trace_rec_t); 1986 } 1987 } 1988 } 1989 1990 #endif /* TRAPTRACE */ 1991 1992 void 1993 panic_showtrap(struct trap_info *tip) 1994 { 1995 showregs(tip->trap_type, tip->trap_regs, tip->trap_addr); 1996 1997 #if defined(TRAPTRACE) 1998 dump_ttrace(); 1999 #endif /* TRAPTRACE */ 2000 2001 if (tip->trap_type == T_DBLFLT) 2002 dump_tss(); 2003 } 2004 2005 void 2006 panic_savetrap(panic_data_t *pdp, struct trap_info *tip) 2007 { 2008 panic_saveregs(pdp, tip->trap_regs); 2009 } 2010