10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52086Ssudheer * Common Development and Distribution License (the "License"). 62086Ssudheer * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21390Sraf 220Sstevel@tonic-gate /* 239384SRoger.Faulkner@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate /* */ 310Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 320Sstevel@tonic-gate /* All Rights Reserved */ 330Sstevel@tonic-gate /* */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #include <sys/param.h> 380Sstevel@tonic-gate #include <sys/signal.h> 390Sstevel@tonic-gate #include <sys/systm.h> 400Sstevel@tonic-gate #include <sys/user.h> 410Sstevel@tonic-gate #include <sys/proc.h> 420Sstevel@tonic-gate #include <sys/disp.h> 430Sstevel@tonic-gate #include <sys/class.h> 440Sstevel@tonic-gate #include <sys/core.h> 450Sstevel@tonic-gate #include <sys/syscall.h> 460Sstevel@tonic-gate #include <sys/cpuvar.h> 470Sstevel@tonic-gate #include <sys/vm.h> 480Sstevel@tonic-gate #include <sys/sysinfo.h> 490Sstevel@tonic-gate #include <sys/fault.h> 500Sstevel@tonic-gate #include <sys/stack.h> 510Sstevel@tonic-gate #include <sys/psw.h> 520Sstevel@tonic-gate #include <sys/regset.h> 530Sstevel@tonic-gate #include <sys/fp.h> 540Sstevel@tonic-gate #include <sys/trap.h> 550Sstevel@tonic-gate #include <sys/kmem.h> 560Sstevel@tonic-gate #include <sys/vtrace.h> 570Sstevel@tonic-gate #include <sys/cmn_err.h> 580Sstevel@tonic-gate #include <sys/prsystm.h> 590Sstevel@tonic-gate #include <sys/mutex_impl.h> 600Sstevel@tonic-gate #include <sys/machsystm.h> 610Sstevel@tonic-gate #include <sys/archsystm.h> 620Sstevel@tonic-gate #include <sys/sdt.h> 630Sstevel@tonic-gate #include <sys/avintr.h> 640Sstevel@tonic-gate #include <sys/kobj.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <vm/hat.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <vm/seg_kmem.h> 690Sstevel@tonic-gate #include <vm/as.h> 700Sstevel@tonic-gate #include <vm/seg.h> 710Sstevel@tonic-gate #include <vm/hat_pte.h> 723446Smrj #include <vm/hat_i86.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <sys/procfs.h> 750Sstevel@tonic-gate 760Sstevel@tonic-gate #include <sys/reboot.h> 770Sstevel@tonic-gate #include <sys/debug.h> 780Sstevel@tonic-gate #include <sys/debugreg.h> 790Sstevel@tonic-gate #include <sys/modctl.h> 800Sstevel@tonic-gate #include <sys/aio_impl.h> 810Sstevel@tonic-gate #include <sys/tnf.h> 820Sstevel@tonic-gate #include <sys/tnf_probe.h> 830Sstevel@tonic-gate #include <sys/cred.h> 840Sstevel@tonic-gate #include <sys/mman.h> 850Sstevel@tonic-gate #include <sys/x86_archext.h> 860Sstevel@tonic-gate #include <sys/copyops.h> 870Sstevel@tonic-gate #include <c2/audit.h> 880Sstevel@tonic-gate #include <sys/ftrace.h> 890Sstevel@tonic-gate #include <sys/panic.h> 900Sstevel@tonic-gate #include <sys/traptrace.h> 910Sstevel@tonic-gate #include <sys/ontrap.h> 920Sstevel@tonic-gate #include <sys/cpc_impl.h> 933446Smrj #include <sys/bootconf.h> 943446Smrj #include <sys/bootinfo.h> 953446Smrj #include <sys/promif.h> 963446Smrj #include <sys/mach_mmu.h> 975084Sjohnlev #if defined(__xpv) 985084Sjohnlev #include <sys/hypervisor.h> 995084Sjohnlev #endif 1005254Sgavinm #include <sys/contract/process_impl.h> 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #define USER 0x10000 /* user-mode flag added to trap type */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate static const char *trap_type_mnemonic[] = { 1050Sstevel@tonic-gate "de", "db", "2", "bp", 1060Sstevel@tonic-gate "of", "br", "ud", "nm", 1070Sstevel@tonic-gate "df", "9", "ts", "np", 1080Sstevel@tonic-gate "ss", "gp", "pf", "15", 1090Sstevel@tonic-gate "mf", "ac", "mc", "xf" 1100Sstevel@tonic-gate }; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate static const char *trap_type[] = { 1130Sstevel@tonic-gate "Divide error", /* trap id 0 */ 1140Sstevel@tonic-gate "Debug", /* trap id 1 */ 1150Sstevel@tonic-gate "NMI interrupt", /* trap id 2 */ 1160Sstevel@tonic-gate "Breakpoint", /* trap id 3 */ 1170Sstevel@tonic-gate "Overflow", /* trap id 4 */ 1180Sstevel@tonic-gate "BOUND range exceeded", /* trap id 5 */ 1190Sstevel@tonic-gate "Invalid opcode", /* trap id 6 */ 1200Sstevel@tonic-gate "Device not available", /* trap id 7 */ 1210Sstevel@tonic-gate "Double fault", /* trap id 8 */ 1220Sstevel@tonic-gate "Coprocessor segment overrun", /* trap id 9 */ 1230Sstevel@tonic-gate "Invalid TSS", /* trap id 10 */ 1240Sstevel@tonic-gate "Segment not present", /* trap id 11 */ 1250Sstevel@tonic-gate "Stack segment fault", /* trap id 12 */ 1260Sstevel@tonic-gate "General protection", /* trap id 13 */ 1270Sstevel@tonic-gate "Page fault", /* trap id 14 */ 1280Sstevel@tonic-gate "Reserved", /* trap id 15 */ 1290Sstevel@tonic-gate "x87 floating point error", /* trap id 16 */ 1300Sstevel@tonic-gate "Alignment check", /* trap id 17 */ 1310Sstevel@tonic-gate "Machine check", /* trap id 18 */ 1320Sstevel@tonic-gate "SIMD floating point exception", /* trap id 19 */ 1330Sstevel@tonic-gate }; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate #define TRAP_TYPES (sizeof (trap_type) / sizeof (trap_type[0])) 1360Sstevel@tonic-gate 1373939Ssethg #define SLOW_SCALL_SIZE 2 1383939Ssethg #define FAST_SCALL_SIZE 2 1393939Ssethg 1400Sstevel@tonic-gate int tudebug = 0; 1410Sstevel@tonic-gate int tudebugbpt = 0; 1420Sstevel@tonic-gate int tudebugfpe = 0; 1430Sstevel@tonic-gate int tudebugsse = 0; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint) 1460Sstevel@tonic-gate int tdebug = 0; 1470Sstevel@tonic-gate int lodebug = 0; 1480Sstevel@tonic-gate int faultdebug = 0; 1490Sstevel@tonic-gate #else 1500Sstevel@tonic-gate #define tdebug 0 1510Sstevel@tonic-gate #define lodebug 0 1520Sstevel@tonic-gate #define faultdebug 0 1530Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */ 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate #if defined(TRAPTRACE) 1566336Sbholler /* 1576336Sbholler * trap trace record for cpu0 is allocated here. 1586336Sbholler * trap trace records for non-boot cpus are allocated in mp_startup_init(). 1596336Sbholler */ 1606336Sbholler static trap_trace_rec_t trap_tr0[TRAPTR_NENT]; 1616336Sbholler trap_trace_ctl_t trap_trace_ctl[NCPU] = { 1626336Sbholler { 1636336Sbholler (uintptr_t)trap_tr0, /* next record */ 1646336Sbholler (uintptr_t)trap_tr0, /* first record */ 1656336Sbholler (uintptr_t)(trap_tr0 + TRAPTR_NENT), /* limit */ 1666336Sbholler (uintptr_t)0 /* current */ 1676336Sbholler }, 1686336Sbholler }; 1696336Sbholler 1706336Sbholler /* 1716336Sbholler * default trap buffer size 1726336Sbholler */ 1736336Sbholler size_t trap_trace_bufsize = TRAPTR_NENT * sizeof (trap_trace_rec_t); 1746336Sbholler int trap_trace_freeze = 0; 1756336Sbholler int trap_trace_off = 0; 1766336Sbholler 1776336Sbholler /* 1786336Sbholler * A dummy TRAPTRACE entry to use after death. 1796336Sbholler */ 1806336Sbholler trap_trace_rec_t trap_trace_postmort; 1816336Sbholler 1820Sstevel@tonic-gate static void dump_ttrace(void); 1830Sstevel@tonic-gate #endif /* TRAPTRACE */ 1840Sstevel@tonic-gate static void dumpregs(struct regs *); 1850Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t); 1860Sstevel@tonic-gate static int kern_gpfault(struct regs *); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /*ARGSUSED*/ 1890Sstevel@tonic-gate static int 1900Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid) 1910Sstevel@tonic-gate { 1925084Sjohnlev struct panic_trap_info ti; 1930Sstevel@tonic-gate const char *trap_name, *trap_mnemonic; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate if (type < TRAP_TYPES) { 1960Sstevel@tonic-gate trap_name = trap_type[type]; 1970Sstevel@tonic-gate trap_mnemonic = trap_type_mnemonic[type]; 1980Sstevel@tonic-gate } else { 1990Sstevel@tonic-gate trap_name = "trap"; 2000Sstevel@tonic-gate trap_mnemonic = "-"; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate #ifdef TRAPTRACE 2040Sstevel@tonic-gate TRAPTRACE_FREEZE; 2050Sstevel@tonic-gate #endif 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate ti.trap_regs = rp; 2080Sstevel@tonic-gate ti.trap_type = type & ~USER; 2090Sstevel@tonic-gate ti.trap_addr = addr; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate curthread->t_panic_trap = &ti; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) { 2140Sstevel@tonic-gate panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p " 2150Sstevel@tonic-gate "occurred in module \"%s\" due to %s", 2160Sstevel@tonic-gate type, trap_mnemonic, trap_name, (void *)rp, (void *)addr, 2170Sstevel@tonic-gate mod_containing_pc((caddr_t)rp->r_pc), 2180Sstevel@tonic-gate addr < (caddr_t)PAGESIZE ? 2190Sstevel@tonic-gate "a NULL pointer dereference" : 2200Sstevel@tonic-gate "an illegal access to a user address"); 2210Sstevel@tonic-gate } else 2220Sstevel@tonic-gate panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p", 2230Sstevel@tonic-gate type, trap_mnemonic, trap_name, (void *)rp, (void *)addr); 2240Sstevel@tonic-gate return (0); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction. 2290Sstevel@tonic-gate * 2300Sstevel@tonic-gate * int <vector> is two bytes: 0xCD <vector> 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate static int 2340Sstevel@tonic-gate rewrite_syscall(caddr_t pc) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT }; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE, 2390Sstevel@tonic-gate (uintptr_t)pc) != 0) 2400Sstevel@tonic-gate return (1); 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate return (0); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Test to see if the instruction at pc is sysenter or syscall. The second 2470Sstevel@tonic-gate * argument should be the x86 feature flag corresponding to the expected 2480Sstevel@tonic-gate * instruction. 2490Sstevel@tonic-gate * 2500Sstevel@tonic-gate * sysenter is two bytes: 0x0F 0x34 2510Sstevel@tonic-gate * syscall is two bytes: 0x0F 0x05 2523939Ssethg * int $T_SYSCALLINT is two bytes: 0xCD 0x91 2530Sstevel@tonic-gate */ 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate static int 2563939Ssethg instr_is_other_syscall(caddr_t pc, int which) 2570Sstevel@tonic-gate { 2580Sstevel@tonic-gate uchar_t instr[FAST_SCALL_SIZE]; 2590Sstevel@tonic-gate 2603939Ssethg ASSERT(which == X86_SEP || which == X86_ASYSC || which == 0xCD); 2610Sstevel@tonic-gate 2623939Ssethg if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0) 2630Sstevel@tonic-gate return (0); 2640Sstevel@tonic-gate 2653939Ssethg switch (which) { 2663939Ssethg case X86_SEP: 2673939Ssethg if (instr[0] == 0x0F && instr[1] == 0x34) 2683939Ssethg return (1); 2693939Ssethg break; 2703939Ssethg case X86_ASYSC: 2713939Ssethg if (instr[0] == 0x0F && instr[1] == 0x05) 2723939Ssethg return (1); 2733939Ssethg break; 2743939Ssethg case 0xCD: 2753939Ssethg if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT) 2763939Ssethg return (1); 2773939Ssethg break; 2783939Ssethg } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate return (0); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2833939Ssethg static const char * 2843939Ssethg syscall_insn_string(int syscall_insn) 2853939Ssethg { 2863939Ssethg switch (syscall_insn) { 2873939Ssethg case X86_SEP: 2883939Ssethg return ("sysenter"); 2893939Ssethg case X86_ASYSC: 2903939Ssethg return ("syscall"); 2913939Ssethg case 0xCD: 2923939Ssethg return ("int"); 2933939Ssethg default: 2943939Ssethg return ("Unknown"); 2953939Ssethg } 2963939Ssethg } 2973939Ssethg 2983939Ssethg static int 2993939Ssethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn) 3003939Ssethg { 3013939Ssethg caddr_t linearpc; 3023939Ssethg int return_code = 0; 3033939Ssethg 3043939Ssethg mutex_enter(&p->p_ldtlock); /* Must be held across linear_pc() */ 3053939Ssethg 3063939Ssethg if (linear_pc(rp, p, &linearpc) == 0) { 3073939Ssethg 3083939Ssethg /* 3093939Ssethg * If another thread beat us here, it already changed 3103939Ssethg * this site to the slower (int) syscall instruction. 3113939Ssethg */ 3123939Ssethg if (instr_is_other_syscall(linearpc, 0xCD)) { 3133939Ssethg return_code = 1; 3143939Ssethg } else if (instr_is_other_syscall(linearpc, syscall_insn)) { 3153939Ssethg 3163939Ssethg if (rewrite_syscall(linearpc) == 0) { 3173939Ssethg return_code = 1; 3183939Ssethg } 3193939Ssethg #ifdef DEBUG 3203939Ssethg else 3213939Ssethg cmn_err(CE_WARN, "failed to rewrite %s " 3223939Ssethg "instruction in process %d", 3233939Ssethg syscall_insn_string(syscall_insn), 3243939Ssethg p->p_pid); 3253939Ssethg #endif /* DEBUG */ 3263939Ssethg } 3273939Ssethg } 3283939Ssethg 3293939Ssethg mutex_exit(&p->p_ldtlock); /* Must be held across linear_pc() */ 3303939Ssethg 3313939Ssethg return (return_code); 3323939Ssethg } 3333939Ssethg 3340Sstevel@tonic-gate /* 3350Sstevel@tonic-gate * Test to see if the instruction at pc is a system call instruction. 3360Sstevel@tonic-gate * 3370Sstevel@tonic-gate * The bytes of an lcall instruction used for the syscall trap. 3380Sstevel@tonic-gate * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 }; 3390Sstevel@tonic-gate * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 }; 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate #define LCALLSIZE 7 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate static int 3453939Ssethg instr_is_lcall_syscall(caddr_t pc) 3460Sstevel@tonic-gate { 3470Sstevel@tonic-gate uchar_t instr[LCALLSIZE]; 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 && 3500Sstevel@tonic-gate instr[0] == 0x9a && 3510Sstevel@tonic-gate instr[1] == 0 && 3520Sstevel@tonic-gate instr[2] == 0 && 3530Sstevel@tonic-gate instr[3] == 0 && 3540Sstevel@tonic-gate instr[4] == 0 && 3550Sstevel@tonic-gate (instr[5] == 0x7 || instr[5] == 0x27) && 3560Sstevel@tonic-gate instr[6] == 0) 3570Sstevel@tonic-gate return (1); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate return (0); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3621363Sfvdl #ifdef __amd64 3631363Sfvdl 3641363Sfvdl /* 3653446Smrj * In the first revisions of amd64 CPUs produced by AMD, the LAHF and 3663446Smrj * SAHF instructions were not implemented in 64-bit mode. Later revisions 3671363Sfvdl * did implement these instructions. An extension to the cpuid instruction 3681363Sfvdl * was added to check for the capability of executing these instructions 3693446Smrj * in 64-bit mode. 3701363Sfvdl * 3711363Sfvdl * Intel originally did not implement these instructions in EM64T either, 3721363Sfvdl * but added them in later revisions. 3731363Sfvdl * 3741363Sfvdl * So, there are different chip revisions by both vendors out there that 3751363Sfvdl * may or may not implement these instructions. The easy solution is to 3761363Sfvdl * just always emulate these instructions on demand. 3771363Sfvdl * 3781363Sfvdl * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e) 3791363Sfvdl * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f) 3801363Sfvdl */ 3811363Sfvdl 3821363Sfvdl #define LSAHFSIZE 1 3831363Sfvdl 3841363Sfvdl static int 3851363Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr) 3861363Sfvdl { 3871363Sfvdl if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 && 3881363Sfvdl (*instr == 0x9e || *instr == 0x9f)) 3891363Sfvdl return (1); 3901363Sfvdl return (0); 3911363Sfvdl } 3921363Sfvdl 3931363Sfvdl /* 3941363Sfvdl * Emulate the LAHF and SAHF instructions. The reference manuals define 3951363Sfvdl * these instructions to always load/store bit 1 as a 1, and bits 3 and 5 3961363Sfvdl * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P). 3971363Sfvdl * 3981363Sfvdl * Note that %ah is bits 8-15 of %rax. 3991363Sfvdl */ 4001363Sfvdl static void 4011363Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr) 4021363Sfvdl { 4031363Sfvdl if (instr == 0x9e) { 4041363Sfvdl /* sahf. Copy bits from %ah to flags. */ 4051363Sfvdl rp->r_ps = (rp->r_ps & ~0xff) | 4061363Sfvdl ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1; 4071363Sfvdl } else { 4081363Sfvdl /* lahf. Copy bits from flags to %ah. */ 4091363Sfvdl rp->r_rax = (rp->r_rax & ~0xff00) | 4101363Sfvdl (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8); 4111363Sfvdl } 4121363Sfvdl rp->r_pc += LSAHFSIZE; 4131363Sfvdl } 4141363Sfvdl #endif /* __amd64 */ 4151363Sfvdl 4160Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * Test to see if the instruction at pc is a prefetch instruction. 4200Sstevel@tonic-gate * 4210Sstevel@tonic-gate * The first byte of prefetch instructions is always 0x0F. 4220Sstevel@tonic-gate * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch. 4230Sstevel@tonic-gate * The third byte is between 0 and 3 inclusive. 4248241SJeff.Bonwick@Sun.COM * 4258241SJeff.Bonwick@Sun.COM * In 64-bit mode, there may be a one-byte REX prefex (0x40-0x4F). 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate static int 4290Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p) 4300Sstevel@tonic-gate { 4318241SJeff.Bonwick@Sun.COM #ifdef _LP64 4328241SJeff.Bonwick@Sun.COM if ((p[0] & 0xF0) == 0x40) /* 64-bit REX prefix */ 4338241SJeff.Bonwick@Sun.COM p++; 4348241SJeff.Bonwick@Sun.COM #endif 4358241SJeff.Bonwick@Sun.COM return (p[0] == 0x0F && (p[1] == 0x18 || p[1] == 0x0D) && p[2] <= 3); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate static int 4390Sstevel@tonic-gate instr_is_prefetch(caddr_t pc) 4400Sstevel@tonic-gate { 4418241SJeff.Bonwick@Sun.COM uchar_t instr[4]; /* optional REX prefix plus 3-byte opcode */ 4420Sstevel@tonic-gate 4438241SJeff.Bonwick@Sun.COM return (copyin_nowatch(pc, instr, sizeof (instr)) == 0 && 4448241SJeff.Bonwick@Sun.COM cmp_to_prefetch(instr)); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */ 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * Called from the trap handler when a processor trap occurs. 4510Sstevel@tonic-gate * 4520Sstevel@tonic-gate * Note: All user-level traps that might call stop() must exit 4530Sstevel@tonic-gate * trap() by 'goto out' or by falling through. 4543446Smrj * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1) 4553446Smrj * however, there are paths that arrive here with PS_IE == 0 so special care 4563446Smrj * must be taken in those cases. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate void 4590Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid) 4600Sstevel@tonic-gate { 4613446Smrj kthread_t *ct = curthread; 4620Sstevel@tonic-gate enum seg_rw rw; 4630Sstevel@tonic-gate unsigned type; 4643446Smrj proc_t *p = ttoproc(ct); 4653446Smrj klwp_t *lwp = ttolwp(ct); 4660Sstevel@tonic-gate uintptr_t lofault; 4670Sstevel@tonic-gate faultcode_t pagefault(), res, errcode; 4680Sstevel@tonic-gate enum fault_type fault_type; 4690Sstevel@tonic-gate k_siginfo_t siginfo; 4700Sstevel@tonic-gate uint_t fault = 0; 4710Sstevel@tonic-gate int mstate; 4720Sstevel@tonic-gate int sicode = 0; 4730Sstevel@tonic-gate int watchcode; 4740Sstevel@tonic-gate int watchpage; 4750Sstevel@tonic-gate caddr_t vaddr; 4762712Snn35248 int singlestep_twiddle; 4770Sstevel@tonic-gate size_t sz; 4780Sstevel@tonic-gate int ta; 4791363Sfvdl #ifdef __amd64 4801363Sfvdl uchar_t instr; 4811363Sfvdl #endif 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate type = rp->r_trapno; 4860Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, sys, trap, 1); 4873446Smrj ASSERT(ct->t_schedflag & TS_DONT_SWAP); 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate if (type == T_PGFLT) { 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate errcode = rp->r_err; 4920Sstevel@tonic-gate if (errcode & PF_ERR_WRITE) 4930Sstevel@tonic-gate rw = S_WRITE; 4940Sstevel@tonic-gate else if ((caddr_t)rp->r_pc == addr || 4950Sstevel@tonic-gate (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) 4960Sstevel@tonic-gate rw = S_EXEC; 4970Sstevel@tonic-gate else 4980Sstevel@tonic-gate rw = S_READ; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate #if defined(__i386) 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * Pentium Pro work-around 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) { 5050Sstevel@tonic-gate uint_t attr; 5060Sstevel@tonic-gate uint_t priv_violation; 5070Sstevel@tonic-gate uint_t access_violation; 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate if (hat_getattr(addr < (caddr_t)kernelbase ? 5100Sstevel@tonic-gate curproc->p_as->a_hat : kas.a_hat, addr, &attr) 5110Sstevel@tonic-gate == -1) { 5120Sstevel@tonic-gate errcode &= ~PF_ERR_PROT; 5130Sstevel@tonic-gate } else { 5140Sstevel@tonic-gate priv_violation = (errcode & PF_ERR_USER) && 5155084Sjohnlev !(attr & PROT_USER); 5160Sstevel@tonic-gate access_violation = (errcode & PF_ERR_WRITE) && 5175084Sjohnlev !(attr & PROT_WRITE); 5180Sstevel@tonic-gate if (!priv_violation && !access_violation) 5190Sstevel@tonic-gate goto cleanup; 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate #endif /* __i386 */ 5230Sstevel@tonic-gate 5243446Smrj } else if (type == T_SGLSTP && lwp != NULL) 5253446Smrj lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate if (tdebug) 5280Sstevel@tonic-gate showregs(type, rp, addr); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (USERMODE(rp->r_cs)) { 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * Set up the current cred to use during this trap. u_cred 5330Sstevel@tonic-gate * no longer exists. t_cred is used instead. 5340Sstevel@tonic-gate * The current process credential applies to the thread for 5350Sstevel@tonic-gate * the entire trap. If trapping from the kernel, this 5360Sstevel@tonic-gate * should already be set up. 5370Sstevel@tonic-gate */ 5383446Smrj if (ct->t_cred != p->p_cred) { 5393446Smrj cred_t *oldcred = ct->t_cred; 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred 5420Sstevel@tonic-gate * must always be either NULL, or point to a valid, 5430Sstevel@tonic-gate * allocated cred structure. 5440Sstevel@tonic-gate */ 5453446Smrj ct->t_cred = crgetcred(); 5460Sstevel@tonic-gate crfree(oldcred); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate ASSERT(lwp != NULL); 5490Sstevel@tonic-gate type |= USER; 5500Sstevel@tonic-gate ASSERT(lwptoregs(lwp) == rp); 5510Sstevel@tonic-gate lwp->lwp_state = LWP_SYS; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate switch (type) { 5540Sstevel@tonic-gate case T_PGFLT + USER: 5550Sstevel@tonic-gate if ((caddr_t)rp->r_pc == addr) 5560Sstevel@tonic-gate mstate = LMS_TFAULT; 5570Sstevel@tonic-gate else 5580Sstevel@tonic-gate mstate = LMS_DFAULT; 5590Sstevel@tonic-gate break; 5600Sstevel@tonic-gate default: 5610Sstevel@tonic-gate mstate = LMS_TRAP; 5620Sstevel@tonic-gate break; 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate /* Kernel probe */ 5650Sstevel@tonic-gate TNF_PROBE_1(thread_state, "thread", /* CSTYLED */, 5660Sstevel@tonic-gate tnf_microstate, state, mstate); 5673446Smrj mstate = new_mstate(ct, mstate); 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate switch (type) { 5730Sstevel@tonic-gate case T_PGFLT + USER: 5740Sstevel@tonic-gate case T_SGLSTP: 5750Sstevel@tonic-gate case T_SGLSTP + USER: 5760Sstevel@tonic-gate case T_BPTFLT + USER: 5770Sstevel@tonic-gate break; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate default: 5800Sstevel@tonic-gate FTRACE_2("trap(): type=0x%lx, regs=0x%lx", 5810Sstevel@tonic-gate (ulong_t)type, (ulong_t)rp); 5820Sstevel@tonic-gate break; 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate switch (type) { 5865849Ssethg case T_SIMDFPE: 5875849Ssethg /* Make sure we enable interrupts before die()ing */ 5885849Ssethg sti(); /* The SIMD exception comes in via cmninttrap */ 5895849Ssethg /*FALLTHROUGH*/ 5900Sstevel@tonic-gate default: 5910Sstevel@tonic-gate if (type & USER) { 5920Sstevel@tonic-gate if (tudebug) 5930Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 5940Sstevel@tonic-gate printf("trap: Unknown trap type %d in user mode\n", 5950Sstevel@tonic-gate type & ~USER); 5960Sstevel@tonic-gate siginfo.si_signo = SIGILL; 5970Sstevel@tonic-gate siginfo.si_code = ILL_ILLTRP; 5980Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 5990Sstevel@tonic-gate siginfo.si_trapno = type & ~USER; 6000Sstevel@tonic-gate fault = FLTILL; 6010Sstevel@tonic-gate break; 6020Sstevel@tonic-gate } else { 6030Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 6040Sstevel@tonic-gate /*NOTREACHED*/ 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate case T_PGFLT: /* system page fault */ 6080Sstevel@tonic-gate /* 6090Sstevel@tonic-gate * If we're under on_trap() protection (see <sys/ontrap.h>), 6105254Sgavinm * set ot_trap and bounce back to the on_trap() call site 6115254Sgavinm * via the installed trampoline. 6120Sstevel@tonic-gate */ 6133446Smrj if ((ct->t_ontrap != NULL) && 6143446Smrj (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) { 6153446Smrj ct->t_ontrap->ot_trap |= OT_DATA_ACCESS; 6165254Sgavinm rp->r_pc = ct->t_ontrap->ot_trampoline; 6175254Sgavinm goto cleanup; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * See if we can handle as pagefault. Save lofault 6220Sstevel@tonic-gate * across this. Here we assume that an address 6230Sstevel@tonic-gate * less than KERNELBASE is a user fault. 6240Sstevel@tonic-gate * We can do this as copy.s routines verify that the 6250Sstevel@tonic-gate * starting address is less than KERNELBASE before 6260Sstevel@tonic-gate * starting and because we know that we always have 6270Sstevel@tonic-gate * KERNELBASE mapped as invalid to serve as a "barrier". 6280Sstevel@tonic-gate */ 6293446Smrj lofault = ct->t_lofault; 6303446Smrj ct->t_lofault = 0; 6310Sstevel@tonic-gate 6323446Smrj mstate = new_mstate(ct, LMS_KFAULT); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if (addr < (caddr_t)kernelbase) { 6350Sstevel@tonic-gate res = pagefault(addr, 6360Sstevel@tonic-gate (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0); 6370Sstevel@tonic-gate if (res == FC_NOMAP && 6380Sstevel@tonic-gate addr < p->p_usrstack && 6390Sstevel@tonic-gate grow(addr)) 6400Sstevel@tonic-gate res = 0; 6410Sstevel@tonic-gate } else { 6420Sstevel@tonic-gate res = pagefault(addr, 6430Sstevel@tonic-gate (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1); 6440Sstevel@tonic-gate } 6453446Smrj (void) new_mstate(ct, mstate); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * Restore lofault. If we resolved the fault, exit. 6490Sstevel@tonic-gate * If we didn't and lofault wasn't set, die. 6500Sstevel@tonic-gate */ 6513446Smrj ct->t_lofault = lofault; 6520Sstevel@tonic-gate if (res == 0) 6530Sstevel@tonic-gate goto cleanup; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64) 6560Sstevel@tonic-gate if (lofault == 0 && opteron_erratum_93) { 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * Workaround for Opteron Erratum 93. On return from 6590Sstevel@tonic-gate * a System Managment Interrupt at a HLT instruction 6600Sstevel@tonic-gate * the %rip might be truncated to a 32 bit value. 6610Sstevel@tonic-gate * BIOS is supposed to fix this, but some don't. 6620Sstevel@tonic-gate * If this occurs we simply restore the high order bits. 6630Sstevel@tonic-gate * The HLT instruction is 1 byte of 0xf4. 6640Sstevel@tonic-gate */ 6650Sstevel@tonic-gate uintptr_t rip = rp->r_pc; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if ((rip & 0xfffffffful) == rip) { 6680Sstevel@tonic-gate rip |= 0xfffffffful << 32; 6690Sstevel@tonic-gate if (hat_getpfnum(kas.a_hat, (caddr_t)rip) != 6700Sstevel@tonic-gate PFN_INVALID && 6710Sstevel@tonic-gate (*(uchar_t *)rip == 0xf4 || 6720Sstevel@tonic-gate *(uchar_t *)(rip - 1) == 0xf4)) { 6730Sstevel@tonic-gate rp->r_pc = rip; 6740Sstevel@tonic-gate goto cleanup; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */ 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91 6810Sstevel@tonic-gate if (lofault == 0 && opteron_erratum_91) { 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Workaround for Opteron Erratum 91. Prefetches may 6840Sstevel@tonic-gate * generate a page fault (they're not supposed to do 6850Sstevel@tonic-gate * that!). If this occurs we simply return back to the 6860Sstevel@tonic-gate * instruction. 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate caddr_t pc = (caddr_t)rp->r_pc; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate /* 6910Sstevel@tonic-gate * If the faulting PC is not mapped, this is a 6920Sstevel@tonic-gate * legitimate kernel page fault that must result in a 6930Sstevel@tonic-gate * panic. If the faulting PC is mapped, it could contain 6940Sstevel@tonic-gate * a prefetch instruction. Check for that here. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) { 6970Sstevel@tonic-gate if (cmp_to_prefetch((uchar_t *)pc)) { 6980Sstevel@tonic-gate #ifdef DEBUG 6990Sstevel@tonic-gate cmn_err(CE_WARN, "Opteron erratum 91 " 7000Sstevel@tonic-gate "occurred: kernel prefetch" 7010Sstevel@tonic-gate " at %p generated a page fault!", 7020Sstevel@tonic-gate (void *)rp->r_pc); 7030Sstevel@tonic-gate #endif /* DEBUG */ 7040Sstevel@tonic-gate goto cleanup; 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */ 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate if (lofault == 0) 7120Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Cannot resolve fault. Return to lofault. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate if (lodebug) { 7180Sstevel@tonic-gate showregs(type, rp, addr); 7190Sstevel@tonic-gate traceregs(rp); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate if (FC_CODE(res) == FC_OBJERR) 7220Sstevel@tonic-gate res = FC_ERRNO(res); 7230Sstevel@tonic-gate else 7240Sstevel@tonic-gate res = EFAULT; 7250Sstevel@tonic-gate rp->r_r0 = res; 7263446Smrj rp->r_pc = ct->t_lofault; 7270Sstevel@tonic-gate goto cleanup; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate case T_PGFLT + USER: /* user page fault */ 7300Sstevel@tonic-gate if (faultdebug) { 7310Sstevel@tonic-gate char *fault_str; 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate switch (rw) { 7340Sstevel@tonic-gate case S_READ: 7350Sstevel@tonic-gate fault_str = "read"; 7360Sstevel@tonic-gate break; 7370Sstevel@tonic-gate case S_WRITE: 7380Sstevel@tonic-gate fault_str = "write"; 7390Sstevel@tonic-gate break; 7400Sstevel@tonic-gate case S_EXEC: 7410Sstevel@tonic-gate fault_str = "exec"; 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate default: 7440Sstevel@tonic-gate fault_str = ""; 7450Sstevel@tonic-gate break; 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate printf("user %s fault: addr=0x%lx errcode=0x%x\n", 7480Sstevel@tonic-gate fault_str, (uintptr_t)addr, errcode); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64) 7520Sstevel@tonic-gate /* 7530Sstevel@tonic-gate * Workaround for AMD erratum 100 7540Sstevel@tonic-gate * 7550Sstevel@tonic-gate * A 32-bit process may receive a page fault on a non 7560Sstevel@tonic-gate * 32-bit address by mistake. The range of the faulting 7570Sstevel@tonic-gate * address will be 7580Sstevel@tonic-gate * 7590Sstevel@tonic-gate * 0xffffffff80000000 .. 0xffffffffffffffff or 7600Sstevel@tonic-gate * 0x0000000100000000 .. 0x000000017fffffff 7610Sstevel@tonic-gate * 7620Sstevel@tonic-gate * The fault is always due to an instruction fetch, however 7630Sstevel@tonic-gate * the value of r_pc should be correct (in 32 bit range), 7640Sstevel@tonic-gate * so we ignore the page fault on the bogus address. 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate if (p->p_model == DATAMODEL_ILP32 && 7670Sstevel@tonic-gate (0xffffffff80000000 <= (uintptr_t)addr || 7680Sstevel@tonic-gate (0x100000000 <= (uintptr_t)addr && 7690Sstevel@tonic-gate (uintptr_t)addr <= 0x17fffffff))) { 7700Sstevel@tonic-gate if (!opteron_erratum_100) 7710Sstevel@tonic-gate panic("unexpected erratum #100"); 7720Sstevel@tonic-gate if (rp->r_pc <= 0xffffffff) 7730Sstevel@tonic-gate goto out; 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */ 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate ASSERT(!(curthread->t_flag & T_WATCHPT)); 7780Sstevel@tonic-gate watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw)); 7790Sstevel@tonic-gate #ifdef __i386 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * In 32-bit mode, the lcall (system call) instruction fetches 7820Sstevel@tonic-gate * one word from the stack, at the stack pointer, because of the 7830Sstevel@tonic-gate * way the call gate is constructed. This is a bogus 7840Sstevel@tonic-gate * read and should not be counted as a read watchpoint. 7850Sstevel@tonic-gate * We work around the problem here by testing to see if 7860Sstevel@tonic-gate * this situation applies and, if so, simply jumping to 7870Sstevel@tonic-gate * the code in locore.s that fields the system call trap. 7880Sstevel@tonic-gate * The registers on the stack are already set up properly 7890Sstevel@tonic-gate * due to the match between the call gate sequence and the 7900Sstevel@tonic-gate * trap gate sequence. We just have to adjust the pc. 7910Sstevel@tonic-gate */ 7920Sstevel@tonic-gate if (watchpage && addr == (caddr_t)rp->r_sp && 7933939Ssethg rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) { 7940Sstevel@tonic-gate extern void watch_syscall(void); 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate rp->r_pc += LCALLSIZE; 7970Sstevel@tonic-gate watch_syscall(); /* never returns */ 7980Sstevel@tonic-gate /* NOTREACHED */ 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate #endif /* __i386 */ 8010Sstevel@tonic-gate vaddr = addr; 8020Sstevel@tonic-gate if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0) 8030Sstevel@tonic-gate fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL; 8040Sstevel@tonic-gate else if ((watchcode = pr_is_watchpoint(&vaddr, &ta, 8050Sstevel@tonic-gate sz, NULL, rw)) != 0) { 8060Sstevel@tonic-gate if (ta) { 8070Sstevel@tonic-gate do_watch_step(vaddr, sz, rw, 8085084Sjohnlev watchcode, rp->r_pc); 8090Sstevel@tonic-gate fault_type = F_INVAL; 8100Sstevel@tonic-gate } else { 8110Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 8120Sstevel@tonic-gate siginfo.si_signo = SIGTRAP; 8130Sstevel@tonic-gate siginfo.si_code = watchcode; 8140Sstevel@tonic-gate siginfo.si_addr = vaddr; 8150Sstevel@tonic-gate siginfo.si_trapafter = 0; 8160Sstevel@tonic-gate siginfo.si_pc = (caddr_t)rp->r_pc; 8170Sstevel@tonic-gate fault = FLTWATCH; 8180Sstevel@tonic-gate break; 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate } else { 8210Sstevel@tonic-gate /* XXX pr_watch_emul() never succeeds (for now) */ 8220Sstevel@tonic-gate if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw)) 8230Sstevel@tonic-gate goto out; 8240Sstevel@tonic-gate do_watch_step(vaddr, sz, rw, 0, 0); 8250Sstevel@tonic-gate fault_type = F_INVAL; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate res = pagefault(addr, fault_type, rw, 0); 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * If pagefault() succeeded, ok. 8320Sstevel@tonic-gate * Otherwise attempt to grow the stack. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate if (res == 0 || 8350Sstevel@tonic-gate (res == FC_NOMAP && 8360Sstevel@tonic-gate addr < p->p_usrstack && 8370Sstevel@tonic-gate grow(addr))) { 8380Sstevel@tonic-gate lwp->lwp_lastfault = FLTPAGE; 8390Sstevel@tonic-gate lwp->lwp_lastfaddr = addr; 8400Sstevel@tonic-gate if (prismember(&p->p_fltmask, FLTPAGE)) { 8410Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 8420Sstevel@tonic-gate siginfo.si_addr = addr; 8430Sstevel@tonic-gate (void) stop_on_fault(FLTPAGE, &siginfo); 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate goto out; 8460Sstevel@tonic-gate } else if (res == FC_PROT && addr < p->p_usrstack && 8470Sstevel@tonic-gate (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) { 8480Sstevel@tonic-gate report_stack_exec(p, addr); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91 8520Sstevel@tonic-gate /* 8530Sstevel@tonic-gate * Workaround for Opteron Erratum 91. Prefetches may generate a 8540Sstevel@tonic-gate * page fault (they're not supposed to do that!). If this 8550Sstevel@tonic-gate * occurs we simply return back to the instruction. 8560Sstevel@tonic-gate * 8570Sstevel@tonic-gate * We rely on copyin to properly fault in the page with r_pc. 8580Sstevel@tonic-gate */ 8590Sstevel@tonic-gate if (opteron_erratum_91 && 8600Sstevel@tonic-gate addr != (caddr_t)rp->r_pc && 8610Sstevel@tonic-gate instr_is_prefetch((caddr_t)rp->r_pc)) { 8620Sstevel@tonic-gate #ifdef DEBUG 8630Sstevel@tonic-gate cmn_err(CE_WARN, "Opteron erratum 91 occurred: " 8640Sstevel@tonic-gate "prefetch at %p in pid %d generated a trap!", 8650Sstevel@tonic-gate (void *)rp->r_pc, p->p_pid); 8660Sstevel@tonic-gate #endif /* DEBUG */ 8670Sstevel@tonic-gate goto out; 8680Sstevel@tonic-gate } 8690Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */ 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate if (tudebug) 8720Sstevel@tonic-gate showregs(type, rp, addr); 8730Sstevel@tonic-gate /* 8740Sstevel@tonic-gate * In the case where both pagefault and grow fail, 8750Sstevel@tonic-gate * set the code to the value provided by pagefault. 8760Sstevel@tonic-gate * We map all errors returned from pagefault() to SIGSEGV. 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 8790Sstevel@tonic-gate siginfo.si_addr = addr; 8800Sstevel@tonic-gate switch (FC_CODE(res)) { 8810Sstevel@tonic-gate case FC_HWERR: 8820Sstevel@tonic-gate case FC_NOSUPPORT: 8830Sstevel@tonic-gate siginfo.si_signo = SIGBUS; 8840Sstevel@tonic-gate siginfo.si_code = BUS_ADRERR; 8850Sstevel@tonic-gate fault = FLTACCESS; 8860Sstevel@tonic-gate break; 8870Sstevel@tonic-gate case FC_ALIGN: 8880Sstevel@tonic-gate siginfo.si_signo = SIGBUS; 8890Sstevel@tonic-gate siginfo.si_code = BUS_ADRALN; 8900Sstevel@tonic-gate fault = FLTACCESS; 8910Sstevel@tonic-gate break; 8920Sstevel@tonic-gate case FC_OBJERR: 8930Sstevel@tonic-gate if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) { 8940Sstevel@tonic-gate siginfo.si_signo = SIGBUS; 8950Sstevel@tonic-gate siginfo.si_code = BUS_OBJERR; 8960Sstevel@tonic-gate fault = FLTACCESS; 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate break; 8990Sstevel@tonic-gate default: /* FC_NOMAP or FC_PROT */ 9000Sstevel@tonic-gate siginfo.si_signo = SIGSEGV; 9010Sstevel@tonic-gate siginfo.si_code = 9020Sstevel@tonic-gate (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR; 9030Sstevel@tonic-gate fault = FLTBOUNDS; 9040Sstevel@tonic-gate break; 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate break; 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate case T_ILLINST + USER: /* invalid opcode fault */ 9090Sstevel@tonic-gate /* 9100Sstevel@tonic-gate * If the syscall instruction is disabled due to LDT usage, a 9110Sstevel@tonic-gate * user program that attempts to execute it will trigger a #ud 9120Sstevel@tonic-gate * trap. Check for that case here. If this occurs on a CPU which 9130Sstevel@tonic-gate * doesn't even support syscall, the result of all of this will 9140Sstevel@tonic-gate * be to emulate that particular instruction. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate if (p->p_ldt != NULL && 9173939Ssethg ldt_rewrite_syscall(rp, p, X86_ASYSC)) 9183939Ssethg goto out; 9191363Sfvdl 9201363Sfvdl #ifdef __amd64 9211363Sfvdl /* 9221363Sfvdl * Emulate the LAHF and SAHF instructions if needed. 9231363Sfvdl * See the instr_is_lsahf function for details. 9241363Sfvdl */ 9251363Sfvdl if (p->p_model == DATAMODEL_LP64 && 9261363Sfvdl instr_is_lsahf((caddr_t)rp->r_pc, &instr)) { 9271363Sfvdl emulate_lsahf(rp, instr); 9281363Sfvdl goto out; 9291363Sfvdl } 9301363Sfvdl #endif 9311363Sfvdl 9320Sstevel@tonic-gate /*FALLTHROUGH*/ 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate if (tudebug) 9350Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 9360Sstevel@tonic-gate siginfo.si_signo = SIGILL; 9370Sstevel@tonic-gate siginfo.si_code = ILL_ILLOPC; 9380Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 9390Sstevel@tonic-gate fault = FLTILL; 9400Sstevel@tonic-gate break; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate case T_ZERODIV + USER: /* integer divide by zero */ 9430Sstevel@tonic-gate if (tudebug && tudebugfpe) 9440Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 9450Sstevel@tonic-gate siginfo.si_signo = SIGFPE; 9460Sstevel@tonic-gate siginfo.si_code = FPE_INTDIV; 9470Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 9480Sstevel@tonic-gate fault = FLTIZDIV; 9490Sstevel@tonic-gate break; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate case T_OVFLW + USER: /* integer overflow */ 9520Sstevel@tonic-gate if (tudebug && tudebugfpe) 9530Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 9540Sstevel@tonic-gate siginfo.si_signo = SIGFPE; 9550Sstevel@tonic-gate siginfo.si_code = FPE_INTOVF; 9560Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 9570Sstevel@tonic-gate fault = FLTIOVF; 9580Sstevel@tonic-gate break; 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate case T_NOEXTFLT + USER: /* math coprocessor not available */ 9610Sstevel@tonic-gate if (tudebug && tudebugfpe) 9620Sstevel@tonic-gate showregs(type, rp, addr); 9630Sstevel@tonic-gate if (fpnoextflt(rp)) { 9649384SRoger.Faulkner@Sun.COM siginfo.si_signo = SIGILL; 9650Sstevel@tonic-gate siginfo.si_code = ILL_ILLOPC; 9660Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 9679384SRoger.Faulkner@Sun.COM fault = FLTILL; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate break; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate case T_EXTOVRFLT: /* extension overrun fault */ 9720Sstevel@tonic-gate /* check if we took a kernel trap on behalf of user */ 9730Sstevel@tonic-gate { 9740Sstevel@tonic-gate extern void ndptrap_frstor(void); 9755849Ssethg if (rp->r_pc != (uintptr_t)ndptrap_frstor) { 9765849Ssethg sti(); /* T_EXTOVRFLT comes in via cmninttrap */ 9770Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 9785849Ssethg } 9790Sstevel@tonic-gate type |= USER; 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate /*FALLTHROUGH*/ 9820Sstevel@tonic-gate case T_EXTOVRFLT + USER: /* extension overrun fault */ 9830Sstevel@tonic-gate if (tudebug && tudebugfpe) 9840Sstevel@tonic-gate showregs(type, rp, addr); 9850Sstevel@tonic-gate if (fpextovrflt(rp)) { 9860Sstevel@tonic-gate siginfo.si_signo = SIGSEGV; 9870Sstevel@tonic-gate siginfo.si_code = SEGV_MAPERR; 9880Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 9890Sstevel@tonic-gate fault = FLTBOUNDS; 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate break; 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate case T_EXTERRFLT: /* x87 floating point exception pending */ 9940Sstevel@tonic-gate /* check if we took a kernel trap on behalf of user */ 9950Sstevel@tonic-gate { 9960Sstevel@tonic-gate extern void ndptrap_frstor(void); 9975849Ssethg if (rp->r_pc != (uintptr_t)ndptrap_frstor) { 9985849Ssethg sti(); /* T_EXTERRFLT comes in via cmninttrap */ 9990Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 10005849Ssethg } 10010Sstevel@tonic-gate type |= USER; 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate /*FALLTHROUGH*/ 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate case T_EXTERRFLT + USER: /* x87 floating point exception pending */ 10060Sstevel@tonic-gate if (tudebug && tudebugfpe) 10070Sstevel@tonic-gate showregs(type, rp, addr); 10080Sstevel@tonic-gate if (sicode = fpexterrflt(rp)) { 10090Sstevel@tonic-gate siginfo.si_signo = SIGFPE; 10100Sstevel@tonic-gate siginfo.si_code = sicode; 10110Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 10120Sstevel@tonic-gate fault = FLTFPE; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate break; 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate case T_SIMDFPE + USER: /* SSE and SSE2 exceptions */ 10170Sstevel@tonic-gate if (tudebug && tudebugsse) 10180Sstevel@tonic-gate showregs(type, rp, addr); 10190Sstevel@tonic-gate if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) { 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * There are rumours that some user instructions 10220Sstevel@tonic-gate * on older CPUs can cause this trap to occur; in 10230Sstevel@tonic-gate * which case send a SIGILL instead of a SIGFPE. 10240Sstevel@tonic-gate */ 10250Sstevel@tonic-gate siginfo.si_signo = SIGILL; 10260Sstevel@tonic-gate siginfo.si_code = ILL_ILLTRP; 10270Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 10280Sstevel@tonic-gate siginfo.si_trapno = type & ~USER; 10290Sstevel@tonic-gate fault = FLTILL; 10300Sstevel@tonic-gate } else if ((sicode = fpsimderrflt(rp)) != 0) { 10310Sstevel@tonic-gate siginfo.si_signo = SIGFPE; 10320Sstevel@tonic-gate siginfo.si_code = sicode; 10330Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 10340Sstevel@tonic-gate fault = FLTFPE; 10350Sstevel@tonic-gate } 10365849Ssethg 10375849Ssethg sti(); /* The SIMD exception comes in via cmninttrap */ 10380Sstevel@tonic-gate break; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate case T_BPTFLT: /* breakpoint trap */ 10410Sstevel@tonic-gate /* 10420Sstevel@tonic-gate * Kernel breakpoint traps should only happen when kmdb is 10430Sstevel@tonic-gate * active, and even then, it'll have interposed on the IDT, so 10440Sstevel@tonic-gate * control won't get here. If it does, we've hit a breakpoint 10450Sstevel@tonic-gate * without the debugger, which is very strange, and very 10460Sstevel@tonic-gate * fatal. 10470Sstevel@tonic-gate */ 10480Sstevel@tonic-gate if (tudebug && tudebugbpt) 10490Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 10520Sstevel@tonic-gate break; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate case T_SGLSTP: /* single step/hw breakpoint exception */ 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate /* Now evaluate how we got here */ 10570Sstevel@tonic-gate if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) { 10580Sstevel@tonic-gate /* 10590Sstevel@tonic-gate * i386 single-steps even through lcalls which 10600Sstevel@tonic-gate * change the privilege level. So we take a trap at 10610Sstevel@tonic-gate * the first instruction in privileged mode. 10620Sstevel@tonic-gate * 10630Sstevel@tonic-gate * Set a flag to indicate that upon completion of 10640Sstevel@tonic-gate * the system call, deal with the single-step trap. 10650Sstevel@tonic-gate * 10660Sstevel@tonic-gate * The same thing happens for sysenter, too. 10670Sstevel@tonic-gate */ 10682712Snn35248 singlestep_twiddle = 0; 10692712Snn35248 if (rp->r_pc == (uintptr_t)sys_sysenter || 10702712Snn35248 rp->r_pc == (uintptr_t)brand_sys_sysenter) { 10712712Snn35248 singlestep_twiddle = 1; 10720Sstevel@tonic-gate #if defined(__amd64) 10730Sstevel@tonic-gate /* 10742712Snn35248 * Since we are already on the kernel's 10752712Snn35248 * %gs, on 64-bit systems the sysenter case 10762712Snn35248 * needs to adjust the pc to avoid 10772712Snn35248 * executing the swapgs instruction at the 10782712Snn35248 * top of the handler. 10790Sstevel@tonic-gate */ 10802712Snn35248 if (rp->r_pc == (uintptr_t)sys_sysenter) 10812712Snn35248 rp->r_pc = (uintptr_t) 10822712Snn35248 _sys_sysenter_post_swapgs; 10832712Snn35248 else 10842712Snn35248 rp->r_pc = (uintptr_t) 10852712Snn35248 _brand_sys_sysenter_post_swapgs; 10860Sstevel@tonic-gate #endif 10872712Snn35248 } 10882712Snn35248 #if defined(__i386) 10892712Snn35248 else if (rp->r_pc == (uintptr_t)sys_call || 10902712Snn35248 rp->r_pc == (uintptr_t)brand_sys_call) { 10912712Snn35248 singlestep_twiddle = 1; 10922712Snn35248 } 10932712Snn35248 #endif 10943003Sdmick else { 10953003Sdmick /* not on sysenter/syscall; uregs available */ 10963003Sdmick if (tudebug && tudebugbpt) 10973003Sdmick showregs(type, rp, (caddr_t)0); 10983003Sdmick } 10992712Snn35248 if (singlestep_twiddle) { 11000Sstevel@tonic-gate rp->r_ps &= ~PS_T; /* turn off trace */ 11010Sstevel@tonic-gate lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 11023446Smrj ct->t_post_sys = 1; 11032086Ssudheer aston(curthread); 11040Sstevel@tonic-gate goto cleanup; 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate } 11070Sstevel@tonic-gate /* XXX - needs review on debugger interface? */ 11080Sstevel@tonic-gate if (boothowto & RB_DEBUG) 11090Sstevel@tonic-gate debug_enter((char *)NULL); 11100Sstevel@tonic-gate else 11110Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 11120Sstevel@tonic-gate break; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate case T_NMIFLT: /* NMI interrupt */ 11150Sstevel@tonic-gate printf("Unexpected NMI in system mode\n"); 11160Sstevel@tonic-gate goto cleanup; 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate case T_NMIFLT + USER: /* NMI interrupt */ 11190Sstevel@tonic-gate printf("Unexpected NMI in user mode\n"); 11200Sstevel@tonic-gate break; 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate case T_GPFLT: /* general protection violation */ 11230Sstevel@tonic-gate /* 11245639Sgavinm * Any #GP that occurs during an on_trap .. no_trap bracket 11255639Sgavinm * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection, 11265639Sgavinm * or in a on_fault .. no_fault bracket, is forgiven 11275639Sgavinm * and we trampoline. This protection is given regardless 11285639Sgavinm * of whether we are 32/64 bit etc - if a distinction is 11295639Sgavinm * required then define new on_trap protection types. 11305639Sgavinm * 11310Sstevel@tonic-gate * On amd64, we can get a #gp from referencing addresses 11325084Sjohnlev * in the virtual address hole e.g. from a copyin or in 11335084Sjohnlev * update_sregs while updating user segment registers. 11345084Sjohnlev * 11355084Sjohnlev * On the 32-bit hypervisor we could also generate one in 11365084Sjohnlev * mfn_to_pfn by reaching around or into where the hypervisor 11375084Sjohnlev * lives which is protected by segmentation. 11380Sstevel@tonic-gate */ 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate /* 11410Sstevel@tonic-gate * If we're under on_trap() protection (see <sys/ontrap.h>), 11425254Sgavinm * set ot_trap and trampoline back to the on_trap() call site 11435084Sjohnlev * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS. 11440Sstevel@tonic-gate */ 11453446Smrj if (ct->t_ontrap != NULL) { 11465084Sjohnlev int ttype = ct->t_ontrap->ot_prot & 11475084Sjohnlev (OT_DATA_ACCESS | OT_SEGMENT_ACCESS); 11483446Smrj 11495084Sjohnlev if (ttype != 0) { 11505084Sjohnlev ct->t_ontrap->ot_trap |= ttype; 11515084Sjohnlev if (tudebug) 11525084Sjohnlev showregs(type, rp, (caddr_t)0); 11535254Sgavinm rp->r_pc = ct->t_ontrap->ot_trampoline; 11545254Sgavinm goto cleanup; 11555084Sjohnlev } 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * If we're under lofault protection (copyin etc.), 11600Sstevel@tonic-gate * longjmp back to lofault with an EFAULT. 11610Sstevel@tonic-gate */ 11623446Smrj if (ct->t_lofault) { 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * Fault is not resolvable, so just return to lofault 11650Sstevel@tonic-gate */ 11660Sstevel@tonic-gate if (lodebug) { 11670Sstevel@tonic-gate showregs(type, rp, addr); 11680Sstevel@tonic-gate traceregs(rp); 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate rp->r_r0 = EFAULT; 11713446Smrj rp->r_pc = ct->t_lofault; 11720Sstevel@tonic-gate goto cleanup; 11730Sstevel@tonic-gate } 11745639Sgavinm 11755639Sgavinm /* 11765639Sgavinm * We fall through to the next case, which repeats 11775639Sgavinm * the OT_SEGMENT_ACCESS check which we've already 11785639Sgavinm * done, so we'll always fall through to the 11795639Sgavinm * T_STKFLT case. 11805639Sgavinm */ 11810Sstevel@tonic-gate /*FALLTHROUGH*/ 11823446Smrj case T_SEGFLT: /* segment not present fault */ 11833446Smrj /* 11843446Smrj * One example of this is #NP in update_sregs while 11853446Smrj * attempting to update a user segment register 11863446Smrj * that points to a descriptor that is marked not 11873446Smrj * present. 11883446Smrj */ 11893446Smrj if (ct->t_ontrap != NULL && 11903446Smrj ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) { 11913446Smrj ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS; 11925084Sjohnlev if (tudebug) 11935084Sjohnlev showregs(type, rp, (caddr_t)0); 11945254Sgavinm rp->r_pc = ct->t_ontrap->ot_trampoline; 11955254Sgavinm goto cleanup; 11963446Smrj } 11973446Smrj /*FALLTHROUGH*/ 11980Sstevel@tonic-gate case T_STKFLT: /* stack fault */ 11990Sstevel@tonic-gate case T_TSSFLT: /* invalid TSS fault */ 12000Sstevel@tonic-gate if (tudebug) 12010Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 12020Sstevel@tonic-gate if (kern_gpfault(rp)) 12030Sstevel@tonic-gate (void) die(type, rp, addr, cpuid); 12040Sstevel@tonic-gate goto cleanup; 12050Sstevel@tonic-gate 12063446Smrj /* 12073446Smrj * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps 12083446Smrj * should have no need for them, so we put a stop to it here. 12093446Smrj * 12103446Smrj * So: not-present fault is ONLY valid for 32-bit processes with 12113446Smrj * a private LDT trying to do a system call. Emulate it. 12123446Smrj * 12133446Smrj * #gp fault is ONLY valid for 32-bit processes also, which DO NOT 12143446Smrj * have a private LDT, and are trying to do a system call. Emulate it. 12153446Smrj */ 12163446Smrj 12170Sstevel@tonic-gate case T_SEGFLT + USER: /* segment not present fault */ 12181217Srab case T_GPFLT + USER: /* general protection violation */ 12190Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 12201217Srab if (p->p_model != DATAMODEL_NATIVE) { 12211217Srab #endif /* _SYSCALL32_IMPL */ 12223939Ssethg if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) { 12231217Srab if (type == T_SEGFLT + USER) 12241217Srab ASSERT(p->p_ldt != NULL); 12251217Srab 12261217Srab if ((p->p_ldt == NULL && type == T_GPFLT + USER) || 12271217Srab type == T_SEGFLT + USER) { 12281217Srab 12290Sstevel@tonic-gate /* 12301217Srab * The user attempted a system call via the obsolete 12311217Srab * call gate mechanism. Because the process doesn't have 12321217Srab * an LDT (i.e. the ldtr contains 0), a #gp results. 12331217Srab * Emulate the syscall here, just as we do above for a 12341217Srab * #np trap. 12351217Srab */ 12361217Srab 12371217Srab /* 12380Sstevel@tonic-gate * Since this is a not-present trap, rp->r_pc points to 12390Sstevel@tonic-gate * the trapping lcall instruction. We need to bump it 12400Sstevel@tonic-gate * to the next insn so the app can continue on. 12410Sstevel@tonic-gate */ 12420Sstevel@tonic-gate rp->r_pc += LCALLSIZE; 12430Sstevel@tonic-gate lwp->lwp_regs = rp; 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate /* 12460Sstevel@tonic-gate * Normally the microstate of the LWP is forced back to 12470Sstevel@tonic-gate * LMS_USER by the syscall handlers. Emulate that 12480Sstevel@tonic-gate * behavior here. 12490Sstevel@tonic-gate */ 12500Sstevel@tonic-gate mstate = LMS_USER; 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate dosyscall(); 12530Sstevel@tonic-gate goto out; 12541217Srab } 12551217Srab } 12561217Srab #ifdef _SYSCALL32_IMPL 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 12590Sstevel@tonic-gate /* 12600Sstevel@tonic-gate * If the current process is using a private LDT and the 12610Sstevel@tonic-gate * trapping instruction is sysenter, the sysenter instruction 12620Sstevel@tonic-gate * has been disabled on the CPU because it destroys segment 12630Sstevel@tonic-gate * registers. If this is the case, rewrite the instruction to 12640Sstevel@tonic-gate * be a safe system call and retry it. If this occurs on a CPU 12650Sstevel@tonic-gate * which doesn't even support sysenter, the result of all of 12660Sstevel@tonic-gate * this will be to emulate that particular instruction. 12670Sstevel@tonic-gate */ 12680Sstevel@tonic-gate if (p->p_ldt != NULL && 12693939Ssethg ldt_rewrite_syscall(rp, p, X86_SEP)) 12703939Ssethg goto out; 12713939Ssethg 12720Sstevel@tonic-gate /*FALLTHROUGH*/ 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate case T_BOUNDFLT + USER: /* bound fault */ 12750Sstevel@tonic-gate case T_STKFLT + USER: /* stack fault */ 12760Sstevel@tonic-gate case T_TSSFLT + USER: /* invalid TSS fault */ 12770Sstevel@tonic-gate if (tudebug) 12780Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 12790Sstevel@tonic-gate siginfo.si_signo = SIGSEGV; 12800Sstevel@tonic-gate siginfo.si_code = SEGV_MAPERR; 12810Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 12820Sstevel@tonic-gate fault = FLTBOUNDS; 12830Sstevel@tonic-gate break; 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate case T_ALIGNMENT + USER: /* user alignment error (486) */ 12860Sstevel@tonic-gate if (tudebug) 12870Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 12880Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 12890Sstevel@tonic-gate siginfo.si_signo = SIGBUS; 12900Sstevel@tonic-gate siginfo.si_code = BUS_ADRALN; 12910Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 12920Sstevel@tonic-gate fault = FLTACCESS; 12930Sstevel@tonic-gate break; 12940Sstevel@tonic-gate 12950Sstevel@tonic-gate case T_SGLSTP + USER: /* single step/hw breakpoint exception */ 12960Sstevel@tonic-gate if (tudebug && tudebugbpt) 12970Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate /* Was it single-stepping? */ 13000Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) { 13010Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate rp->r_ps &= ~PS_T; 13040Sstevel@tonic-gate /* 13050Sstevel@tonic-gate * If both NORMAL_STEP and WATCH_STEP are in effect, 13062712Snn35248 * give precedence to WATCH_STEP. If neither is set, 13070Sstevel@tonic-gate * user must have set the PS_T bit in %efl; treat this 13080Sstevel@tonic-gate * as NORMAL_STEP. 13090Sstevel@tonic-gate */ 13102712Snn35248 if ((fault = undo_watch_step(&siginfo)) == 0 && 13112712Snn35248 ((pcb->pcb_flags & NORMAL_STEP) || 13122712Snn35248 !(pcb->pcb_flags & WATCH_STEP))) { 13130Sstevel@tonic-gate siginfo.si_signo = SIGTRAP; 13140Sstevel@tonic-gate siginfo.si_code = TRAP_TRACE; 13150Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 13160Sstevel@tonic-gate fault = FLTTRACE; 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP); 13190Sstevel@tonic-gate } else { 13200Sstevel@tonic-gate cmn_err(CE_WARN, 13210Sstevel@tonic-gate "Unexpected INT 1 in user mode, dr6=%lx", 13220Sstevel@tonic-gate lwp->lwp_pcb.pcb_drstat); 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate case T_BPTFLT + USER: /* breakpoint trap */ 13270Sstevel@tonic-gate if (tudebug && tudebugbpt) 13280Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 13290Sstevel@tonic-gate /* 13300Sstevel@tonic-gate * int 3 (the breakpoint instruction) leaves the pc referring 13310Sstevel@tonic-gate * to the address one byte after the breakpointed address. 13320Sstevel@tonic-gate * If the P_PR_BPTADJ flag has been set via /proc, We adjust 13330Sstevel@tonic-gate * it back so it refers to the breakpointed address. 13340Sstevel@tonic-gate */ 13350Sstevel@tonic-gate if (p->p_proc_flag & P_PR_BPTADJ) 13360Sstevel@tonic-gate rp->r_pc--; 13370Sstevel@tonic-gate siginfo.si_signo = SIGTRAP; 13380Sstevel@tonic-gate siginfo.si_code = TRAP_BRKPT; 13390Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 13400Sstevel@tonic-gate fault = FLTBPT; 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate case T_AST: 13440Sstevel@tonic-gate /* 13450Sstevel@tonic-gate * This occurs only after the cs register has been made to 13460Sstevel@tonic-gate * look like a kernel selector, either through debugging or 13470Sstevel@tonic-gate * possibly by functions like setcontext(). The thread is 13480Sstevel@tonic-gate * about to cause a general protection fault at common_iret() 13490Sstevel@tonic-gate * in locore. We let that happen immediately instead of 13500Sstevel@tonic-gate * doing the T_AST processing. 13510Sstevel@tonic-gate */ 13520Sstevel@tonic-gate goto cleanup; 13530Sstevel@tonic-gate 13545254Sgavinm case T_AST + USER: /* profiling, resched, h/w error pseudo trap */ 13555254Sgavinm if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) { 13565254Sgavinm proc_t *p = ttoproc(curthread); 13579613SAbhinandan.Ekande@Sun.COM extern void print_msg_hwerr(ctid_t ct_id, proc_t *p); 13585254Sgavinm 13595254Sgavinm lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR; 13609613SAbhinandan.Ekande@Sun.COM print_msg_hwerr(p->p_ct_process->conp_contract.ct_id, 13619613SAbhinandan.Ekande@Sun.COM p); 13625254Sgavinm contract_process_hwerr(p->p_ct_process, p); 13635254Sgavinm siginfo.si_signo = SIGKILL; 13645254Sgavinm siginfo.si_code = SI_NOINFO; 13655254Sgavinm } else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) { 13660Sstevel@tonic-gate lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW; 13670Sstevel@tonic-gate if (kcpc_overflow_ast()) { 13680Sstevel@tonic-gate /* 13690Sstevel@tonic-gate * Signal performance counter overflow 13700Sstevel@tonic-gate */ 13710Sstevel@tonic-gate if (tudebug) 13720Sstevel@tonic-gate showregs(type, rp, (caddr_t)0); 13730Sstevel@tonic-gate bzero(&siginfo, sizeof (siginfo)); 13740Sstevel@tonic-gate siginfo.si_signo = SIGEMT; 13750Sstevel@tonic-gate siginfo.si_code = EMT_CPCOVF; 13760Sstevel@tonic-gate siginfo.si_addr = (caddr_t)rp->r_pc; 13770Sstevel@tonic-gate fault = FLTCPCOVF; 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate } 13805254Sgavinm 13810Sstevel@tonic-gate break; 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate /* 13850Sstevel@tonic-gate * We can't get here from a system trap 13860Sstevel@tonic-gate */ 13870Sstevel@tonic-gate ASSERT(type & USER); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate if (fault) { 13903506Saf /* We took a fault so abort single step. */ 13913506Saf lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP); 13920Sstevel@tonic-gate /* 13930Sstevel@tonic-gate * Remember the fault and fault adddress 13940Sstevel@tonic-gate * for real-time (SIGPROF) profiling. 13950Sstevel@tonic-gate */ 13960Sstevel@tonic-gate lwp->lwp_lastfault = fault; 13970Sstevel@tonic-gate lwp->lwp_lastfaddr = siginfo.si_addr; 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo); 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate /* 14020Sstevel@tonic-gate * If a debugger has declared this fault to be an 14030Sstevel@tonic-gate * event of interest, stop the lwp. Otherwise just 14040Sstevel@tonic-gate * deliver the associated signal. 14050Sstevel@tonic-gate */ 14060Sstevel@tonic-gate if (siginfo.si_signo != SIGKILL && 14070Sstevel@tonic-gate prismember(&p->p_fltmask, fault) && 14080Sstevel@tonic-gate stop_on_fault(fault, &siginfo) == 0) 14090Sstevel@tonic-gate siginfo.si_signo = 0; 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate if (siginfo.si_signo) 14139384SRoger.Faulkner@Sun.COM trapsig(&siginfo, (fault != FLTFPE && fault != FLTCPCOVF)); 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate if (lwp->lwp_oweupc) 14160Sstevel@tonic-gate profil_tick(rp->r_pc); 14170Sstevel@tonic-gate 14183446Smrj if (ct->t_astflag | ct->t_sig_check) { 14190Sstevel@tonic-gate /* 14200Sstevel@tonic-gate * Turn off the AST flag before checking all the conditions that 14210Sstevel@tonic-gate * may have caused an AST. This flag is on whenever a signal or 14220Sstevel@tonic-gate * unusual condition should be handled after the next trap or 14230Sstevel@tonic-gate * syscall. 14240Sstevel@tonic-gate */ 14253446Smrj astoff(ct); 14262086Ssudheer /* 14272086Ssudheer * If a single-step trap occurred on a syscall (see above) 14282086Ssudheer * recognize it now. Do this before checking for signals 14292086Ssudheer * because deferred_singlestep_trap() may generate a SIGTRAP to 14302086Ssudheer * the LWP or may otherwise mark the LWP to call issig(FORREAL). 14312086Ssudheer */ 14322086Ssudheer if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING) 14332086Ssudheer deferred_singlestep_trap((caddr_t)rp->r_pc); 14342086Ssudheer 14353446Smrj ct->t_sig_check = 0; 14360Sstevel@tonic-gate 14370Sstevel@tonic-gate mutex_enter(&p->p_lock); 14380Sstevel@tonic-gate if (curthread->t_proc_flag & TP_CHANGEBIND) { 14390Sstevel@tonic-gate timer_lwpbind(); 14400Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_CHANGEBIND; 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate mutex_exit(&p->p_lock); 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate /* 14450Sstevel@tonic-gate * for kaio requests that are on the per-process poll queue, 14460Sstevel@tonic-gate * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel 14470Sstevel@tonic-gate * should copyout their result_t to user memory. by copying 14480Sstevel@tonic-gate * out the result_t, the user can poll on memory waiting 14490Sstevel@tonic-gate * for the kaio request to complete. 14500Sstevel@tonic-gate */ 14510Sstevel@tonic-gate if (p->p_aio) 14520Sstevel@tonic-gate aio_cleanup(0); 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * If this LWP was asked to hold, call holdlwp(), which will 14550Sstevel@tonic-gate * stop. holdlwps() sets this up and calls pokelwps() which 14560Sstevel@tonic-gate * sets the AST flag. 14570Sstevel@tonic-gate * 14580Sstevel@tonic-gate * Also check TP_EXITLWP, since this is used by fresh new LWPs 14590Sstevel@tonic-gate * through lwp_rtt(). That flag is set if the lwp_create(2) 14600Sstevel@tonic-gate * syscall failed after creating the LWP. 14610Sstevel@tonic-gate */ 14620Sstevel@tonic-gate if (ISHOLD(p)) 14630Sstevel@tonic-gate holdlwp(); 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate /* 14660Sstevel@tonic-gate * All code that sets signals and makes ISSIG evaluate true must 14670Sstevel@tonic-gate * set t_astflag afterwards. 14680Sstevel@tonic-gate */ 14693446Smrj if (ISSIG_PENDING(ct, lwp, p)) { 14700Sstevel@tonic-gate if (issig(FORREAL)) 14710Sstevel@tonic-gate psig(); 14723446Smrj ct->t_sig_check = 1; 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate 14753446Smrj if (ct->t_rprof != NULL) { 1476*9870SRoger.Faulkner@Sun.COM realsigprof(0, 0, 0); 14773446Smrj ct->t_sig_check = 1; 14780Sstevel@tonic-gate } 14791468Smarx 14801468Smarx /* 14811468Smarx * /proc can't enable/disable the trace bit itself 14821468Smarx * because that could race with the call gate used by 14831468Smarx * system calls via "lcall". If that happened, an 14841468Smarx * invalid EFLAGS would result. prstep()/prnostep() 14851468Smarx * therefore schedule an AST for the purpose. 14861468Smarx */ 14871468Smarx if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) { 14881468Smarx lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP; 14891468Smarx rp->r_ps |= PS_T; 14901468Smarx } 14911468Smarx if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) { 14921468Smarx lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP; 14931468Smarx rp->r_ps &= ~PS_T; 14941468Smarx } 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate out: /* We can't get here from a system trap */ 14980Sstevel@tonic-gate ASSERT(type & USER); 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate if (ISHOLD(p)) 15010Sstevel@tonic-gate holdlwp(); 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate /* 15040Sstevel@tonic-gate * Set state to LWP_USER here so preempt won't give us a kernel 15050Sstevel@tonic-gate * priority if it occurs after this point. Call CL_TRAPRET() to 15060Sstevel@tonic-gate * restore the user-level priority. 15070Sstevel@tonic-gate * 15080Sstevel@tonic-gate * It is important that no locks (other than spinlocks) be entered 15090Sstevel@tonic-gate * after this point before returning to user mode (unless lwp_state 15100Sstevel@tonic-gate * is set back to LWP_SYS). 15110Sstevel@tonic-gate */ 15120Sstevel@tonic-gate lwp->lwp_state = LWP_USER; 15130Sstevel@tonic-gate 15143446Smrj if (ct->t_trapret) { 15153446Smrj ct->t_trapret = 0; 15163446Smrj thread_lock(ct); 15173446Smrj CL_TRAPRET(ct); 15183446Smrj thread_unlock(ct); 15190Sstevel@tonic-gate } 15203792Sakolb if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ) 15210Sstevel@tonic-gate preempt(); 15223446Smrj (void) new_mstate(ct, mstate); 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate /* Kernel probe */ 15250Sstevel@tonic-gate TNF_PROBE_1(thread_state, "thread", /* CSTYLED */, 15260Sstevel@tonic-gate tnf_microstate, state, LMS_USER); 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate return; 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate cleanup: /* system traps end up here */ 15310Sstevel@tonic-gate ASSERT(!(type & USER)); 15320Sstevel@tonic-gate } 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate /* 15350Sstevel@tonic-gate * Patch non-zero to disable preemption of threads in the kernel. 15360Sstevel@tonic-gate */ 15370Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0; /* XXX - delete this someday */ 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate struct kpreempt_cnts { /* kernel preemption statistics */ 15400Sstevel@tonic-gate int kpc_idle; /* executing idle thread */ 15410Sstevel@tonic-gate int kpc_intr; /* executing interrupt thread */ 15420Sstevel@tonic-gate int kpc_clock; /* executing clock thread */ 15430Sstevel@tonic-gate int kpc_blocked; /* thread has blocked preemption (t_preempt) */ 15440Sstevel@tonic-gate int kpc_notonproc; /* thread is surrendering processor */ 15450Sstevel@tonic-gate int kpc_inswtch; /* thread has ratified scheduling decision */ 15460Sstevel@tonic-gate int kpc_prilevel; /* processor interrupt level is too high */ 15470Sstevel@tonic-gate int kpc_apreempt; /* asynchronous preemption */ 15480Sstevel@tonic-gate int kpc_spreempt; /* synchronous preemption */ 15490Sstevel@tonic-gate } kpreempt_cnts; 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate /* 15520Sstevel@tonic-gate * kernel preemption: forced rescheduling, preempt the running kernel thread. 15530Sstevel@tonic-gate * the argument is old PIL for an interrupt, 15540Sstevel@tonic-gate * or the distingished value KPREEMPT_SYNC. 15550Sstevel@tonic-gate */ 15560Sstevel@tonic-gate void 15570Sstevel@tonic-gate kpreempt(int asyncspl) 15580Sstevel@tonic-gate { 15593446Smrj kthread_t *ct = curthread; 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate if (IGNORE_KERNEL_PREEMPTION) { 15620Sstevel@tonic-gate aston(CPU->cpu_dispthread); 15630Sstevel@tonic-gate return; 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate /* 15670Sstevel@tonic-gate * Check that conditions are right for kernel preemption 15680Sstevel@tonic-gate */ 15690Sstevel@tonic-gate do { 15703446Smrj if (ct->t_preempt) { 15710Sstevel@tonic-gate /* 15720Sstevel@tonic-gate * either a privileged thread (idle, panic, interrupt) 15738173SPramod.Batni@Sun.COM * or will check when t_preempt is lowered 15748173SPramod.Batni@Sun.COM * We need to specifically handle the case where 15758173SPramod.Batni@Sun.COM * the thread is in the middle of swtch (resume has 15768173SPramod.Batni@Sun.COM * been called) and has its t_preempt set 15778173SPramod.Batni@Sun.COM * [idle thread and a thread which is in kpreempt 15788173SPramod.Batni@Sun.COM * already] and then a high priority thread is 15798173SPramod.Batni@Sun.COM * available in the local dispatch queue. 15808173SPramod.Batni@Sun.COM * In this case the resumed thread needs to take a 15818173SPramod.Batni@Sun.COM * trap so that it can call kpreempt. We achieve 15828173SPramod.Batni@Sun.COM * this by using siron(). 15838173SPramod.Batni@Sun.COM * How do we detect this condition: 15848173SPramod.Batni@Sun.COM * idle thread is running and is in the midst of 15858173SPramod.Batni@Sun.COM * resume: curthread->t_pri == -1 && CPU->dispthread 15868173SPramod.Batni@Sun.COM * != CPU->thread 15878173SPramod.Batni@Sun.COM * Need to ensure that this happens only at high pil 15888173SPramod.Batni@Sun.COM * resume is called at high pil 15898173SPramod.Batni@Sun.COM * Only in resume_from_idle is the pil changed. 15900Sstevel@tonic-gate */ 15918173SPramod.Batni@Sun.COM if (ct->t_pri < 0) { 15920Sstevel@tonic-gate kpreempt_cnts.kpc_idle++; 15938173SPramod.Batni@Sun.COM if (CPU->cpu_dispthread != CPU->cpu_thread) 15948173SPramod.Batni@Sun.COM siron(); 15958173SPramod.Batni@Sun.COM } else if (ct->t_flag & T_INTR_THREAD) { 15960Sstevel@tonic-gate kpreempt_cnts.kpc_intr++; 15973446Smrj if (ct->t_pil == CLOCK_LEVEL) 15980Sstevel@tonic-gate kpreempt_cnts.kpc_clock++; 15998173SPramod.Batni@Sun.COM } else { 16000Sstevel@tonic-gate kpreempt_cnts.kpc_blocked++; 16018173SPramod.Batni@Sun.COM if (CPU->cpu_dispthread != CPU->cpu_thread) 16028173SPramod.Batni@Sun.COM siron(); 16038173SPramod.Batni@Sun.COM } 16040Sstevel@tonic-gate aston(CPU->cpu_dispthread); 16050Sstevel@tonic-gate return; 16060Sstevel@tonic-gate } 16073446Smrj if (ct->t_state != TS_ONPROC || 16083446Smrj ct->t_disp_queue != CPU->cpu_disp) { 16090Sstevel@tonic-gate /* this thread will be calling swtch() shortly */ 16100Sstevel@tonic-gate kpreempt_cnts.kpc_notonproc++; 16110Sstevel@tonic-gate if (CPU->cpu_thread != CPU->cpu_dispthread) { 16120Sstevel@tonic-gate /* already in swtch(), force another */ 16130Sstevel@tonic-gate kpreempt_cnts.kpc_inswtch++; 16140Sstevel@tonic-gate siron(); 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate return; 16170Sstevel@tonic-gate } 16180Sstevel@tonic-gate if (getpil() >= DISP_LEVEL) { 16190Sstevel@tonic-gate /* 16200Sstevel@tonic-gate * We can't preempt this thread if it is at 16210Sstevel@tonic-gate * a PIL >= DISP_LEVEL since it may be holding 16220Sstevel@tonic-gate * a spin lock (like sched_lock). 16230Sstevel@tonic-gate */ 16240Sstevel@tonic-gate siron(); /* check back later */ 16250Sstevel@tonic-gate kpreempt_cnts.kpc_prilevel++; 16260Sstevel@tonic-gate return; 16270Sstevel@tonic-gate } 16283446Smrj if (!interrupts_enabled()) { 16293446Smrj /* 16303446Smrj * Can't preempt while running with ints disabled 16313446Smrj */ 16323446Smrj kpreempt_cnts.kpc_prilevel++; 16333446Smrj return; 16343446Smrj } 16350Sstevel@tonic-gate if (asyncspl != KPREEMPT_SYNC) 16360Sstevel@tonic-gate kpreempt_cnts.kpc_apreempt++; 16370Sstevel@tonic-gate else 16380Sstevel@tonic-gate kpreempt_cnts.kpc_spreempt++; 16390Sstevel@tonic-gate 16403446Smrj ct->t_preempt++; 16410Sstevel@tonic-gate preempt(); 16423446Smrj ct->t_preempt--; 16430Sstevel@tonic-gate } while (CPU->cpu_kprunrun); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate /* 16470Sstevel@tonic-gate * Print out debugging info. 16480Sstevel@tonic-gate */ 16490Sstevel@tonic-gate static void 16500Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr) 16510Sstevel@tonic-gate { 16520Sstevel@tonic-gate int s; 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate s = spl7(); 16550Sstevel@tonic-gate type &= ~USER; 16563446Smrj if (PTOU(curproc)->u_comm[0]) 16573446Smrj printf("%s: ", PTOU(curproc)->u_comm); 16580Sstevel@tonic-gate if (type < TRAP_TYPES) 16590Sstevel@tonic-gate printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]); 16600Sstevel@tonic-gate else 16610Sstevel@tonic-gate switch (type) { 16620Sstevel@tonic-gate case T_SYSCALL: 16630Sstevel@tonic-gate printf("Syscall Trap:\n"); 16640Sstevel@tonic-gate break; 16650Sstevel@tonic-gate case T_AST: 16660Sstevel@tonic-gate printf("AST\n"); 16670Sstevel@tonic-gate break; 16680Sstevel@tonic-gate default: 16690Sstevel@tonic-gate printf("Bad Trap = %d\n", type); 16700Sstevel@tonic-gate break; 16710Sstevel@tonic-gate } 16720Sstevel@tonic-gate if (type == T_PGFLT) { 16730Sstevel@tonic-gate printf("Bad %s fault at addr=0x%lx\n", 16740Sstevel@tonic-gate USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr); 16750Sstevel@tonic-gate } else if (addr) { 16760Sstevel@tonic-gate printf("addr=0x%lx\n", (uintptr_t)addr); 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n", 16800Sstevel@tonic-gate (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ? 16810Sstevel@tonic-gate ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps); 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate #if defined(__lint) 16840Sstevel@tonic-gate /* 16850Sstevel@tonic-gate * this clause can be deleted when lint bug 4870403 is fixed 16860Sstevel@tonic-gate * (lint thinks that bit 32 is illegal in a %b format string) 16870Sstevel@tonic-gate */ 16880Sstevel@tonic-gate printf("cr0: %x cr4: %b\n", 16890Sstevel@tonic-gate (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4); 16900Sstevel@tonic-gate #else 16910Sstevel@tonic-gate printf("cr0: %b cr4: %b\n", 16920Sstevel@tonic-gate (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4); 16933446Smrj #endif /* __lint */ 16940Sstevel@tonic-gate 16955084Sjohnlev printf("cr2: %lx", getcr2()); 16965084Sjohnlev #if !defined(__xpv) 16975084Sjohnlev printf("cr3: %lx", getcr3()); 16980Sstevel@tonic-gate #if defined(__amd64) 16995084Sjohnlev printf("cr8: %lx\n", getcr8()); 17000Sstevel@tonic-gate #endif 17015084Sjohnlev #endif 17025084Sjohnlev printf("\n"); 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate dumpregs(rp); 17050Sstevel@tonic-gate splx(s); 17060Sstevel@tonic-gate } 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate static void 17090Sstevel@tonic-gate dumpregs(struct regs *rp) 17100Sstevel@tonic-gate { 17110Sstevel@tonic-gate #if defined(__amd64) 17120Sstevel@tonic-gate const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n"; 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx); 17150Sstevel@tonic-gate printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9); 17160Sstevel@tonic-gate printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp); 17170Sstevel@tonic-gate printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12); 17180Sstevel@tonic-gate printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15); 17190Sstevel@tonic-gate 17203446Smrj printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE), 17213446Smrj " ds", rp->r_ds); 17220Sstevel@tonic-gate printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs); 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip); 17250Sstevel@tonic-gate printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp); 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate printf("\t%3s: %16lx\n", " ss", rp->r_ss); 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate #elif defined(__i386) 17300Sstevel@tonic-gate const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n"; 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs, 17330Sstevel@tonic-gate " es", rp->r_es, " ds", rp->r_ds); 17340Sstevel@tonic-gate printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi, 17350Sstevel@tonic-gate "ebp", rp->r_ebp, "esp", rp->r_esp); 17360Sstevel@tonic-gate printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx, 17370Sstevel@tonic-gate "ecx", rp->r_ecx, "eax", rp->r_eax); 17380Sstevel@tonic-gate printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, 17390Sstevel@tonic-gate "eip", rp->r_eip, " cs", rp->r_cs); 17400Sstevel@tonic-gate printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n", 17410Sstevel@tonic-gate "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss); 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate #endif /* __i386 */ 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate /* 17475084Sjohnlev * Test to see if the instruction is iret on i386 or iretq on amd64. 17485084Sjohnlev * 17495084Sjohnlev * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true 17505084Sjohnlev * then we are in the context of hypervisor's failsafe handler because it 17515084Sjohnlev * tried to iret and failed due to a bad selector. See xen_failsafe_callback. 17525084Sjohnlev */ 17535084Sjohnlev static int 17545084Sjohnlev instr_is_iret(caddr_t pc) 17555084Sjohnlev { 17565084Sjohnlev 17575084Sjohnlev #if defined(__xpv) 17585084Sjohnlev extern void nopop_sys_rtt_syscall(void); 17595084Sjohnlev return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0); 17605084Sjohnlev 17615084Sjohnlev #else 17625084Sjohnlev 17635084Sjohnlev #if defined(__amd64) 17645084Sjohnlev static const uint8_t iret_insn[2] = { 0x48, 0xcf }; /* iretq */ 17655084Sjohnlev 17665084Sjohnlev #elif defined(__i386) 17675084Sjohnlev static const uint8_t iret_insn[1] = { 0xcf }; /* iret */ 17685084Sjohnlev #endif /* __i386 */ 17695084Sjohnlev return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0); 17705084Sjohnlev 17715084Sjohnlev #endif /* __xpv */ 17725084Sjohnlev } 17735084Sjohnlev 17745084Sjohnlev #if defined(__i386) 17755084Sjohnlev 17765084Sjohnlev /* 17775084Sjohnlev * Test to see if the instruction is part of __SEGREGS_POP 17785084Sjohnlev * 17795084Sjohnlev * Note carefully the appallingly awful dependency between 17805084Sjohnlev * the instruction sequence used in __SEGREGS_POP and these 17815084Sjohnlev * instructions encoded here. 17825084Sjohnlev */ 17835084Sjohnlev static int 17845084Sjohnlev instr_is_segregs_pop(caddr_t pc) 17855084Sjohnlev { 17865084Sjohnlev static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 }; 17875084Sjohnlev static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 }; 17885084Sjohnlev static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 }; 17895084Sjohnlev static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc }; 17905084Sjohnlev 17915084Sjohnlev if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 || 17925084Sjohnlev bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 || 17935084Sjohnlev bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 || 17945084Sjohnlev bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0) 17955084Sjohnlev return (1); 17965084Sjohnlev 17975084Sjohnlev return (0); 17985084Sjohnlev } 17995084Sjohnlev 18005084Sjohnlev #endif /* __i386 */ 18015084Sjohnlev 18025084Sjohnlev /* 18035084Sjohnlev * Test to see if the instruction is part of _sys_rtt. 18045084Sjohnlev * 18055084Sjohnlev * Again on the hypervisor if we try to IRET to user land with a bad code 18065084Sjohnlev * or stack selector we will get vectored through xen_failsafe_callback. 18075084Sjohnlev * In which case we assume we got here via _sys_rtt since we only allow 18085084Sjohnlev * IRET to user land to take place in _sys_rtt. 18095084Sjohnlev */ 18105084Sjohnlev static int 18115084Sjohnlev instr_is_sys_rtt(caddr_t pc) 18125084Sjohnlev { 18135084Sjohnlev extern void _sys_rtt(), _sys_rtt_end(); 18145084Sjohnlev 18155084Sjohnlev if ((uintptr_t)pc < (uintptr_t)_sys_rtt || 18165084Sjohnlev (uintptr_t)pc > (uintptr_t)_sys_rtt_end) 18175084Sjohnlev return (0); 18185084Sjohnlev 18195084Sjohnlev return (1); 18205084Sjohnlev } 18215084Sjohnlev 18225084Sjohnlev /* 18230Sstevel@tonic-gate * Handle #gp faults in kernel mode. 18240Sstevel@tonic-gate * 18250Sstevel@tonic-gate * One legitimate way this can happen is if we attempt to update segment 18260Sstevel@tonic-gate * registers to naughty values on the way out of the kernel. 18270Sstevel@tonic-gate * 18280Sstevel@tonic-gate * This can happen in a couple of ways: someone - either accidentally or 18290Sstevel@tonic-gate * on purpose - creates (setcontext(2), lwp_create(2)) or modifies 18300Sstevel@tonic-gate * (signal(2)) a ucontext that contains silly segment register values. 18310Sstevel@tonic-gate * Or someone - either accidentally or on purpose - modifies the prgregset_t 18320Sstevel@tonic-gate * of a subject process via /proc to contain silly segment register values. 18330Sstevel@tonic-gate * 18340Sstevel@tonic-gate * (The unfortunate part is that we can end up discovering the bad segment 18350Sstevel@tonic-gate * register value in the middle of an 'iret' after we've popped most of the 18360Sstevel@tonic-gate * stack. So it becomes quite difficult to associate an accurate ucontext 18370Sstevel@tonic-gate * with the lwp, because the act of taking the #gp trap overwrites most of 18380Sstevel@tonic-gate * what we were going to send the lwp.) 18390Sstevel@tonic-gate * 18400Sstevel@tonic-gate * OTOH if it turns out that's -not- the problem, and we're -not- an lwp 18410Sstevel@tonic-gate * trying to return to user mode and we get a #gp fault, then we need 18420Sstevel@tonic-gate * to die() -- which will happen if we return non-zero from this routine. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate static int 18450Sstevel@tonic-gate kern_gpfault(struct regs *rp) 18460Sstevel@tonic-gate { 18470Sstevel@tonic-gate kthread_t *t = curthread; 18480Sstevel@tonic-gate proc_t *p = ttoproc(t); 18490Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 18500Sstevel@tonic-gate struct regs tmpregs, *trp = NULL; 18510Sstevel@tonic-gate caddr_t pc = (caddr_t)rp->r_pc; 18520Sstevel@tonic-gate int v; 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate /* 18555084Sjohnlev * if we're not an lwp, or in the case of running native the 18565084Sjohnlev * pc range is outside _sys_rtt, then we should immediately 18575084Sjohnlev * be die()ing horribly. 18580Sstevel@tonic-gate */ 18595084Sjohnlev if (lwp == NULL || !instr_is_sys_rtt(pc)) 18600Sstevel@tonic-gate return (1); 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate /* 18630Sstevel@tonic-gate * So at least we're in the right part of the kernel. 18640Sstevel@tonic-gate * 18650Sstevel@tonic-gate * Disassemble the instruction at the faulting pc. 18660Sstevel@tonic-gate * Once we know what it is, we carefully reconstruct the stack 18670Sstevel@tonic-gate * based on the order in which the stack is deconstructed in 18680Sstevel@tonic-gate * _sys_rtt. Ew. 18690Sstevel@tonic-gate */ 18705084Sjohnlev if (instr_is_iret(pc)) { 18710Sstevel@tonic-gate /* 18725084Sjohnlev * We took the #gp while trying to perform the IRET. 18730Sstevel@tonic-gate * This means that either %cs or %ss are bad. 18740Sstevel@tonic-gate * All we know for sure is that most of the general 18750Sstevel@tonic-gate * registers have been restored, including the 18760Sstevel@tonic-gate * segment registers, and all we have left on the 18770Sstevel@tonic-gate * topmost part of the lwp's stack are the 18780Sstevel@tonic-gate * registers that the iretq was unable to consume. 18790Sstevel@tonic-gate * 18800Sstevel@tonic-gate * All the rest of the state was crushed by the #gp 18810Sstevel@tonic-gate * which pushed -its- registers atop our old save area 18820Sstevel@tonic-gate * (because we had to decrement the stack pointer, sigh) so 18830Sstevel@tonic-gate * all that we can try and do is to reconstruct the 18840Sstevel@tonic-gate * crushed frame from the #gp trap frame itself. 18850Sstevel@tonic-gate */ 18860Sstevel@tonic-gate trp = &tmpregs; 18870Sstevel@tonic-gate trp->r_ss = lwptoregs(lwp)->r_ss; 18880Sstevel@tonic-gate trp->r_sp = lwptoregs(lwp)->r_sp; 18890Sstevel@tonic-gate trp->r_ps = lwptoregs(lwp)->r_ps; 18900Sstevel@tonic-gate trp->r_cs = lwptoregs(lwp)->r_cs; 18910Sstevel@tonic-gate trp->r_pc = lwptoregs(lwp)->r_pc; 18920Sstevel@tonic-gate bcopy(rp, trp, offsetof(struct regs, r_pc)); 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * Validate simple math 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc); 18980Sstevel@tonic-gate ASSERT(trp->r_err == rp->r_err); 18990Sstevel@tonic-gate 19005084Sjohnlev 19015084Sjohnlev 19025084Sjohnlev } 19035084Sjohnlev 19045084Sjohnlev #if defined(__amd64) 19055084Sjohnlev if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) { 19065084Sjohnlev 19075084Sjohnlev /* 19085084Sjohnlev * This is the common case -- we're trying to load 19095084Sjohnlev * a bad segment register value in the only section 19105084Sjohnlev * of kernel code that ever loads segment registers. 19115084Sjohnlev * 19125084Sjohnlev * We don't need to do anything at this point because 19135084Sjohnlev * the pcb contains all the pending segment register 19145084Sjohnlev * state, and the regs are still intact because we 19155084Sjohnlev * didn't adjust the stack pointer yet. Given the fidelity 19165084Sjohnlev * of all this, we could conceivably send a signal 19175084Sjohnlev * to the lwp, rather than core-ing. 19185084Sjohnlev */ 19195084Sjohnlev trp = lwptoregs(lwp); 19205084Sjohnlev ASSERT((caddr_t)trp == (caddr_t)rp->r_sp); 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate #elif defined(__i386) 19240Sstevel@tonic-gate 19255084Sjohnlev if (trp == NULL && instr_is_segregs_pop(pc)) 19265084Sjohnlev trp = lwptoregs(lwp); 19270Sstevel@tonic-gate 19285084Sjohnlev #endif /* __i386 */ 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate if (trp == NULL) 19310Sstevel@tonic-gate return (1); 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate /* 19340Sstevel@tonic-gate * If we get to here, we're reasonably confident that we've 19350Sstevel@tonic-gate * correctly decoded what happened on the way out of the kernel. 19360Sstevel@tonic-gate * Rewrite the lwp's registers so that we can create a core dump 19370Sstevel@tonic-gate * the (at least vaguely) represents the mcontext we were 19380Sstevel@tonic-gate * being asked to restore when things went so terribly wrong. 19390Sstevel@tonic-gate */ 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate /* 19420Sstevel@tonic-gate * Make sure that we have a meaningful %trapno and %err. 19430Sstevel@tonic-gate */ 19440Sstevel@tonic-gate trp->r_trapno = rp->r_trapno; 19450Sstevel@tonic-gate trp->r_err = rp->r_err; 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate if ((caddr_t)trp != (caddr_t)lwptoregs(lwp)) 19480Sstevel@tonic-gate bcopy(trp, lwptoregs(lwp), sizeof (*trp)); 19490Sstevel@tonic-gate 19505084Sjohnlev 19510Sstevel@tonic-gate mutex_enter(&p->p_lock); 19520Sstevel@tonic-gate lwp->lwp_cursig = SIGSEGV; 19530Sstevel@tonic-gate mutex_exit(&p->p_lock); 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate /* 19565084Sjohnlev * Terminate all LWPs but don't discard them. If another lwp beat 19575084Sjohnlev * us to the punch by calling exit(), evaporate now. 19580Sstevel@tonic-gate */ 1959390Sraf proc_is_exiting(p); 19600Sstevel@tonic-gate if (exitlwps(1) != 0) { 19610Sstevel@tonic-gate mutex_enter(&p->p_lock); 19620Sstevel@tonic-gate lwp_exit(); 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate if (audit_active) /* audit core dump */ 19660Sstevel@tonic-gate audit_core_start(SIGSEGV); 19670Sstevel@tonic-gate v = core(SIGSEGV, B_FALSE); 19680Sstevel@tonic-gate if (audit_active) /* audit core dump */ 19690Sstevel@tonic-gate audit_core_finish(v ? CLD_KILLED : CLD_DUMPED); 19700Sstevel@tonic-gate exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV); 19710Sstevel@tonic-gate return (0); 19720Sstevel@tonic-gate } 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate /* 19750Sstevel@tonic-gate * dump_tss() - Display the TSS structure 19760Sstevel@tonic-gate */ 19770Sstevel@tonic-gate 19785084Sjohnlev #if !defined(__xpv) 19790Sstevel@tonic-gate #if defined(__amd64) 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate static void 19820Sstevel@tonic-gate dump_tss(void) 19830Sstevel@tonic-gate { 19840Sstevel@tonic-gate const char tss_fmt[] = "tss.%s:\t0x%p\n"; /* Format string */ 19850Sstevel@tonic-gate struct tss *tss = CPU->cpu_tss; 19860Sstevel@tonic-gate 19870Sstevel@tonic-gate printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0); 19880Sstevel@tonic-gate printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1); 19890Sstevel@tonic-gate printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2); 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1); 19920Sstevel@tonic-gate printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2); 19930Sstevel@tonic-gate printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3); 19940Sstevel@tonic-gate printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4); 19950Sstevel@tonic-gate printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5); 19960Sstevel@tonic-gate printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6); 19970Sstevel@tonic-gate printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate #elif defined(__i386) 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate static void 20030Sstevel@tonic-gate dump_tss(void) 20040Sstevel@tonic-gate { 20050Sstevel@tonic-gate const char tss_fmt[] = "tss.%s:\t0x%p\n"; /* Format string */ 20060Sstevel@tonic-gate struct tss *tss = CPU->cpu_tss; 20070Sstevel@tonic-gate 2008286Sdmick printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link); 2009286Sdmick printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0); 2010286Sdmick printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0); 2011286Sdmick printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1); 2012286Sdmick printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1); 2013286Sdmick printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2); 2014286Sdmick printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2); 2015286Sdmick printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3); 2016286Sdmick printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip); 2017286Sdmick printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags); 2018286Sdmick printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax); 2019286Sdmick printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx); 2020286Sdmick printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx); 2021286Sdmick printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx); 2022286Sdmick printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp); 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate #endif /* __amd64 */ 20265084Sjohnlev #endif /* !__xpv */ 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate #if defined(TRAPTRACE) 20290Sstevel@tonic-gate 20305084Sjohnlev int ttrace_nrec = 10; /* number of records to dump out */ 20315084Sjohnlev int ttrace_dump_nregs = 0; /* dump out this many records with regs too */ 20320Sstevel@tonic-gate 20330Sstevel@tonic-gate /* 20340Sstevel@tonic-gate * Dump out the last ttrace_nrec traptrace records on each CPU 20350Sstevel@tonic-gate */ 20360Sstevel@tonic-gate static void 20370Sstevel@tonic-gate dump_ttrace(void) 20380Sstevel@tonic-gate { 20390Sstevel@tonic-gate trap_trace_ctl_t *ttc; 20400Sstevel@tonic-gate trap_trace_rec_t *rec; 20410Sstevel@tonic-gate uintptr_t current; 20420Sstevel@tonic-gate int i, j, k; 20430Sstevel@tonic-gate int n = NCPU; 20440Sstevel@tonic-gate #if defined(__amd64) 20450Sstevel@tonic-gate const char banner[] = 20465084Sjohnlev "\ncpu address timestamp " 20475084Sjohnlev "type vc handler pc\n"; 20480Sstevel@tonic-gate const char fmt1[] = "%3d %016lx %12llx "; 20490Sstevel@tonic-gate #elif defined(__i386) 20500Sstevel@tonic-gate const char banner[] = 20515084Sjohnlev "\ncpu address timestamp type vc handler pc\n"; 20520Sstevel@tonic-gate const char fmt1[] = "%3d %08lx %12llx "; 20530Sstevel@tonic-gate #endif 20540Sstevel@tonic-gate const char fmt2[] = "%4s %3x "; 20550Sstevel@tonic-gate const char fmt3[] = "%8s "; 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate if (ttrace_nrec == 0) 20580Sstevel@tonic-gate return; 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate printf(banner); 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate for (i = 0; i < n; i++) { 20630Sstevel@tonic-gate ttc = &trap_trace_ctl[i]; 20640Sstevel@tonic-gate if (ttc->ttc_first == NULL) 20650Sstevel@tonic-gate continue; 20660Sstevel@tonic-gate 20670Sstevel@tonic-gate current = ttc->ttc_next - sizeof (trap_trace_rec_t); 20680Sstevel@tonic-gate for (j = 0; j < ttrace_nrec; j++) { 20690Sstevel@tonic-gate struct sysent *sys; 20700Sstevel@tonic-gate struct autovec *vec; 20710Sstevel@tonic-gate extern struct av_head autovect[]; 20720Sstevel@tonic-gate int type; 20730Sstevel@tonic-gate ulong_t off; 20740Sstevel@tonic-gate char *sym, *stype; 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate if (current < ttc->ttc_first) 20770Sstevel@tonic-gate current = 20780Sstevel@tonic-gate ttc->ttc_limit - sizeof (trap_trace_rec_t); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate if (current == NULL) 20810Sstevel@tonic-gate continue; 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate rec = (trap_trace_rec_t *)current; 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate if (rec->ttr_stamp == 0) 20860Sstevel@tonic-gate break; 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp); 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate switch (rec->ttr_marker) { 20910Sstevel@tonic-gate case TT_SYSCALL: 20920Sstevel@tonic-gate case TT_SYSENTER: 20930Sstevel@tonic-gate case TT_SYSC: 20940Sstevel@tonic-gate case TT_SYSC64: 20950Sstevel@tonic-gate #if defined(__amd64) 20960Sstevel@tonic-gate sys = &sysent32[rec->ttr_sysnum]; 20970Sstevel@tonic-gate switch (rec->ttr_marker) { 20980Sstevel@tonic-gate case TT_SYSC64: 20990Sstevel@tonic-gate sys = &sysent[rec->ttr_sysnum]; 21000Sstevel@tonic-gate /*FALLTHROUGH*/ 21010Sstevel@tonic-gate #elif defined(__i386) 21020Sstevel@tonic-gate sys = &sysent[rec->ttr_sysnum]; 21030Sstevel@tonic-gate switch (rec->ttr_marker) { 21040Sstevel@tonic-gate case TT_SYSC64: 21050Sstevel@tonic-gate #endif 21060Sstevel@tonic-gate case TT_SYSC: 21070Sstevel@tonic-gate stype = "sysc"; /* syscall */ 21080Sstevel@tonic-gate break; 21090Sstevel@tonic-gate case TT_SYSCALL: 21100Sstevel@tonic-gate stype = "lcal"; /* lcall */ 21110Sstevel@tonic-gate break; 21120Sstevel@tonic-gate case TT_SYSENTER: 21130Sstevel@tonic-gate stype = "syse"; /* sysenter */ 21140Sstevel@tonic-gate break; 21150Sstevel@tonic-gate default: 21160Sstevel@tonic-gate break; 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate printf(fmt2, "sysc", rec->ttr_sysnum); 21190Sstevel@tonic-gate if (sys != NULL) { 21200Sstevel@tonic-gate sym = kobj_getsymname( 21210Sstevel@tonic-gate (uintptr_t)sys->sy_callc, 21220Sstevel@tonic-gate &off); 21230Sstevel@tonic-gate if (sym != NULL) 21243446Smrj printf(fmt3, sym); 21250Sstevel@tonic-gate else 21260Sstevel@tonic-gate printf("%p ", sys->sy_callc); 21270Sstevel@tonic-gate } else { 21283446Smrj printf(fmt3, "unknown"); 21290Sstevel@tonic-gate } 21300Sstevel@tonic-gate break; 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate case TT_INTERRUPT: 21330Sstevel@tonic-gate printf(fmt2, "intr", rec->ttr_vector); 21340Sstevel@tonic-gate vec = (&autovect[rec->ttr_vector])->avh_link; 21350Sstevel@tonic-gate if (vec != NULL) { 21360Sstevel@tonic-gate sym = kobj_getsymname( 21370Sstevel@tonic-gate (uintptr_t)vec->av_vector, &off); 21380Sstevel@tonic-gate if (sym != NULL) 21393446Smrj printf(fmt3, sym); 21400Sstevel@tonic-gate else 21410Sstevel@tonic-gate printf("%p ", vec->av_vector); 21420Sstevel@tonic-gate } else { 21433446Smrj printf(fmt3, "unknown "); 21440Sstevel@tonic-gate } 21450Sstevel@tonic-gate break; 21460Sstevel@tonic-gate 21470Sstevel@tonic-gate case TT_TRAP: 21483446Smrj case TT_EVENT: 21490Sstevel@tonic-gate type = rec->ttr_regs.r_trapno; 21500Sstevel@tonic-gate printf(fmt2, "trap", type); 21513446Smrj if (type < TRAP_TYPES) 21523446Smrj printf(" #%s ", 21533446Smrj trap_type_mnemonic[type]); 21543446Smrj else 21553446Smrj switch (type) { 21563446Smrj case T_AST: 21573446Smrj printf(fmt3, "ast"); 21583446Smrj break; 21593446Smrj default: 21603446Smrj printf(fmt3, ""); 21613446Smrj break; 21623446Smrj } 21633446Smrj break; 21643446Smrj 21650Sstevel@tonic-gate default: 21660Sstevel@tonic-gate break; 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate 21690Sstevel@tonic-gate sym = kobj_getsymname(rec->ttr_regs.r_pc, &off); 21700Sstevel@tonic-gate if (sym != NULL) 21710Sstevel@tonic-gate printf("%s+%lx\n", sym, off); 21720Sstevel@tonic-gate else 21730Sstevel@tonic-gate printf("%lx\n", rec->ttr_regs.r_pc); 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate if (ttrace_dump_nregs-- > 0) { 21760Sstevel@tonic-gate int s; 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate if (rec->ttr_marker == TT_INTERRUPT) 21790Sstevel@tonic-gate printf( 21800Sstevel@tonic-gate "\t\tipl %x spl %x pri %x\n", 21810Sstevel@tonic-gate rec->ttr_ipl, 21820Sstevel@tonic-gate rec->ttr_spl, 21830Sstevel@tonic-gate rec->ttr_pri); 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate dumpregs(&rec->ttr_regs); 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate printf("\t%3s: %p\n\n", " ct", 21880Sstevel@tonic-gate (void *)rec->ttr_curthread); 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate /* 21910Sstevel@tonic-gate * print out the pc stack that we recorded 21920Sstevel@tonic-gate * at trap time (if any) 21930Sstevel@tonic-gate */ 21940Sstevel@tonic-gate for (s = 0; s < rec->ttr_sdepth; s++) { 21950Sstevel@tonic-gate uintptr_t fullpc; 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate if (s >= TTR_STACK_DEPTH) { 21980Sstevel@tonic-gate printf("ttr_sdepth corrupt\n"); 21990Sstevel@tonic-gate break; 22000Sstevel@tonic-gate } 22010Sstevel@tonic-gate 22020Sstevel@tonic-gate fullpc = (uintptr_t)rec->ttr_stack[s]; 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate sym = kobj_getsymname(fullpc, &off); 22050Sstevel@tonic-gate if (sym != NULL) 22060Sstevel@tonic-gate printf("-> %s+0x%lx()\n", 22070Sstevel@tonic-gate sym, off); 22080Sstevel@tonic-gate else 22090Sstevel@tonic-gate printf("-> 0x%lx()\n", fullpc); 22100Sstevel@tonic-gate } 22110Sstevel@tonic-gate printf("\n"); 22120Sstevel@tonic-gate } 22130Sstevel@tonic-gate current -= sizeof (trap_trace_rec_t); 22140Sstevel@tonic-gate } 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate } 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate #endif /* TRAPTRACE */ 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate void 22215084Sjohnlev panic_showtrap(struct panic_trap_info *tip) 22220Sstevel@tonic-gate { 22230Sstevel@tonic-gate showregs(tip->trap_type, tip->trap_regs, tip->trap_addr); 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate #if defined(TRAPTRACE) 22260Sstevel@tonic-gate dump_ttrace(); 22275084Sjohnlev #endif 22280Sstevel@tonic-gate 22295084Sjohnlev #if !defined(__xpv) 22300Sstevel@tonic-gate if (tip->trap_type == T_DBLFLT) 22310Sstevel@tonic-gate dump_tss(); 22325084Sjohnlev #endif 22330Sstevel@tonic-gate } 22340Sstevel@tonic-gate 22350Sstevel@tonic-gate void 22365084Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip) 22370Sstevel@tonic-gate { 22380Sstevel@tonic-gate panic_saveregs(pdp, tip->trap_regs); 22390Sstevel@tonic-gate } 2240