xref: /onnv-gate/usr/src/uts/i86pc/os/trap.c (revision 12826:fca99d9e3f2f)
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 /*
2312825SJimmy.Vetayases@oracle.com  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
280Sstevel@tonic-gate /*		All Rights Reserved   				*/
290Sstevel@tonic-gate /*								*/
300Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
310Sstevel@tonic-gate /*		All Rights Reserved   				*/
320Sstevel@tonic-gate /*								*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/signal.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/disp.h>
420Sstevel@tonic-gate #include <sys/class.h>
430Sstevel@tonic-gate #include <sys/core.h>
440Sstevel@tonic-gate #include <sys/syscall.h>
450Sstevel@tonic-gate #include <sys/cpuvar.h>
460Sstevel@tonic-gate #include <sys/vm.h>
470Sstevel@tonic-gate #include <sys/sysinfo.h>
480Sstevel@tonic-gate #include <sys/fault.h>
490Sstevel@tonic-gate #include <sys/stack.h>
500Sstevel@tonic-gate #include <sys/psw.h>
510Sstevel@tonic-gate #include <sys/regset.h>
520Sstevel@tonic-gate #include <sys/fp.h>
530Sstevel@tonic-gate #include <sys/trap.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/vtrace.h>
560Sstevel@tonic-gate #include <sys/cmn_err.h>
570Sstevel@tonic-gate #include <sys/prsystm.h>
580Sstevel@tonic-gate #include <sys/mutex_impl.h>
590Sstevel@tonic-gate #include <sys/machsystm.h>
600Sstevel@tonic-gate #include <sys/archsystm.h>
610Sstevel@tonic-gate #include <sys/sdt.h>
620Sstevel@tonic-gate #include <sys/avintr.h>
630Sstevel@tonic-gate #include <sys/kobj.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <vm/hat.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <vm/seg_kmem.h>
680Sstevel@tonic-gate #include <vm/as.h>
690Sstevel@tonic-gate #include <vm/seg.h>
700Sstevel@tonic-gate #include <vm/hat_pte.h>
713446Smrj #include <vm/hat_i86.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include <sys/procfs.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <sys/reboot.h>
760Sstevel@tonic-gate #include <sys/debug.h>
770Sstevel@tonic-gate #include <sys/debugreg.h>
780Sstevel@tonic-gate #include <sys/modctl.h>
790Sstevel@tonic-gate #include <sys/aio_impl.h>
800Sstevel@tonic-gate #include <sys/tnf.h>
810Sstevel@tonic-gate #include <sys/tnf_probe.h>
820Sstevel@tonic-gate #include <sys/cred.h>
830Sstevel@tonic-gate #include <sys/mman.h>
840Sstevel@tonic-gate #include <sys/x86_archext.h>
850Sstevel@tonic-gate #include <sys/copyops.h>
860Sstevel@tonic-gate #include <c2/audit.h>
870Sstevel@tonic-gate #include <sys/ftrace.h>
880Sstevel@tonic-gate #include <sys/panic.h>
890Sstevel@tonic-gate #include <sys/traptrace.h>
900Sstevel@tonic-gate #include <sys/ontrap.h>
910Sstevel@tonic-gate #include <sys/cpc_impl.h>
923446Smrj #include <sys/bootconf.h>
933446Smrj #include <sys/bootinfo.h>
943446Smrj #include <sys/promif.h>
953446Smrj #include <sys/mach_mmu.h>
965084Sjohnlev #if defined(__xpv)
975084Sjohnlev #include <sys/hypervisor.h>
985084Sjohnlev #endif
995254Sgavinm #include <sys/contract/process_impl.h>
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1040Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1050Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1060Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1070Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1080Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static const char *trap_type[] = {
1120Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
1130Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1140Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1150Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
1160Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
1170Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
1180Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
1190Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
1200Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
1210Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
1220Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
1230Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
1240Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
1250Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
1260Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
1270Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
1280Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
1290Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
1300Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1310Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1320Sstevel@tonic-gate };
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1350Sstevel@tonic-gate 
1363939Ssethg #define	SLOW_SCALL_SIZE	2
1373939Ssethg #define	FAST_SCALL_SIZE	2
1383939Ssethg 
1390Sstevel@tonic-gate int tudebug = 0;
1400Sstevel@tonic-gate int tudebugbpt = 0;
1410Sstevel@tonic-gate int tudebugfpe = 0;
1420Sstevel@tonic-gate int tudebugsse = 0;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1450Sstevel@tonic-gate int tdebug = 0;
1460Sstevel@tonic-gate int lodebug = 0;
1470Sstevel@tonic-gate int faultdebug = 0;
1480Sstevel@tonic-gate #else
1490Sstevel@tonic-gate #define	tdebug	0
1500Sstevel@tonic-gate #define	lodebug	0
1510Sstevel@tonic-gate #define	faultdebug	0
1520Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #if defined(TRAPTRACE)
1556336Sbholler /*
1566336Sbholler  * trap trace record for cpu0 is allocated here.
1576336Sbholler  * trap trace records for non-boot cpus are allocated in mp_startup_init().
1586336Sbholler  */
1596336Sbholler static trap_trace_rec_t trap_tr0[TRAPTR_NENT];
1606336Sbholler trap_trace_ctl_t trap_trace_ctl[NCPU] = {
1616336Sbholler 	{
1626336Sbholler 	    (uintptr_t)trap_tr0,			/* next record */
1636336Sbholler 	    (uintptr_t)trap_tr0,			/* first record */
1646336Sbholler 	    (uintptr_t)(trap_tr0 + TRAPTR_NENT),	/* limit */
1656336Sbholler 	    (uintptr_t)0				/* current */
1666336Sbholler 	},
1676336Sbholler };
1686336Sbholler 
1696336Sbholler /*
1706336Sbholler  * default trap buffer size
1716336Sbholler  */
1726336Sbholler size_t trap_trace_bufsize = TRAPTR_NENT * sizeof (trap_trace_rec_t);
1736336Sbholler int trap_trace_freeze = 0;
1746336Sbholler int trap_trace_off = 0;
1756336Sbholler 
1766336Sbholler /*
1776336Sbholler  * A dummy TRAPTRACE entry to use after death.
1786336Sbholler  */
1796336Sbholler trap_trace_rec_t trap_trace_postmort;
1806336Sbholler 
1810Sstevel@tonic-gate static void dump_ttrace(void);
1820Sstevel@tonic-gate #endif	/* TRAPTRACE */
1830Sstevel@tonic-gate static void dumpregs(struct regs *);
1840Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1850Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*ARGSUSED*/
1880Sstevel@tonic-gate static int
die(uint_t type,struct regs * rp,caddr_t addr,processorid_t cpuid)1890Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1900Sstevel@tonic-gate {
1915084Sjohnlev 	struct panic_trap_info ti;
1920Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1950Sstevel@tonic-gate 		trap_name = trap_type[type];
1960Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		trap_name = "trap";
1990Sstevel@tonic-gate 		trap_mnemonic = "-";
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate #ifdef TRAPTRACE
2030Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
2040Sstevel@tonic-gate #endif
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	ti.trap_regs = rp;
2070Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
2080Sstevel@tonic-gate 	ti.trap_addr = addr;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
2130Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
2140Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
2150Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
2160Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
2170Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
2180Sstevel@tonic-gate 		    "a NULL pointer dereference" :
2190Sstevel@tonic-gate 		    "an illegal access to a user address");
2200Sstevel@tonic-gate 	} else
2210Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
2220Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
2230Sstevel@tonic-gate 	return (0);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static int
rewrite_syscall(caddr_t pc)2330Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2380Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2390Sstevel@tonic-gate 		return (1);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	return (0);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2460Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2470Sstevel@tonic-gate  * instruction.
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2500Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
2513939Ssethg  * int $T_SYSCALLINT is two bytes: 0xCD 0x91
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static int
instr_is_other_syscall(caddr_t pc,int which)2553939Ssethg instr_is_other_syscall(caddr_t pc, int which)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2580Sstevel@tonic-gate 
259*12826Skuriakose.kuruvilla@oracle.com 	ASSERT(which == X86FSET_SEP || which == X86FSET_ASYSC || which == 0xCD);
2600Sstevel@tonic-gate 
2613939Ssethg 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0)
2620Sstevel@tonic-gate 		return (0);
2630Sstevel@tonic-gate 
2643939Ssethg 	switch (which) {
265*12826Skuriakose.kuruvilla@oracle.com 	case X86FSET_SEP:
2663939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x34)
2673939Ssethg 			return (1);
2683939Ssethg 		break;
269*12826Skuriakose.kuruvilla@oracle.com 	case X86FSET_ASYSC:
2703939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x05)
2713939Ssethg 			return (1);
2723939Ssethg 		break;
2733939Ssethg 	case 0xCD:
2743939Ssethg 		if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT)
2753939Ssethg 			return (1);
2763939Ssethg 		break;
2773939Ssethg 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	return (0);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2823939Ssethg static const char *
syscall_insn_string(int syscall_insn)2833939Ssethg syscall_insn_string(int syscall_insn)
2843939Ssethg {
2853939Ssethg 	switch (syscall_insn) {
286*12826Skuriakose.kuruvilla@oracle.com 	case X86FSET_SEP:
2873939Ssethg 		return ("sysenter");
288*12826Skuriakose.kuruvilla@oracle.com 	case X86FSET_ASYSC:
2893939Ssethg 		return ("syscall");
2903939Ssethg 	case 0xCD:
2913939Ssethg 		return ("int");
2923939Ssethg 	default:
2933939Ssethg 		return ("Unknown");
2943939Ssethg 	}
2953939Ssethg }
2963939Ssethg 
2973939Ssethg static int
ldt_rewrite_syscall(struct regs * rp,proc_t * p,int syscall_insn)2983939Ssethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn)
2993939Ssethg {
3003939Ssethg 	caddr_t	linearpc;
3013939Ssethg 	int return_code = 0;
3023939Ssethg 
3033939Ssethg 	mutex_enter(&p->p_ldtlock);	/* Must be held across linear_pc() */
3043939Ssethg 
3053939Ssethg 	if (linear_pc(rp, p, &linearpc) == 0) {
3063939Ssethg 
3073939Ssethg 		/*
3083939Ssethg 		 * If another thread beat us here, it already changed
3093939Ssethg 		 * this site to the slower (int) syscall instruction.
3103939Ssethg 		 */
3113939Ssethg 		if (instr_is_other_syscall(linearpc, 0xCD)) {
3123939Ssethg 			return_code = 1;
3133939Ssethg 		} else if (instr_is_other_syscall(linearpc, syscall_insn)) {
3143939Ssethg 
3153939Ssethg 			if (rewrite_syscall(linearpc) == 0) {
3163939Ssethg 				return_code = 1;
3173939Ssethg 			}
3183939Ssethg #ifdef DEBUG
3193939Ssethg 			else
3203939Ssethg 				cmn_err(CE_WARN, "failed to rewrite %s "
3213939Ssethg 				    "instruction in process %d",
3223939Ssethg 				    syscall_insn_string(syscall_insn),
3233939Ssethg 				    p->p_pid);
3243939Ssethg #endif /* DEBUG */
3253939Ssethg 		}
3263939Ssethg 	}
3273939Ssethg 
3283939Ssethg 	mutex_exit(&p->p_ldtlock);	/* Must be held across linear_pc() */
3293939Ssethg 
3303939Ssethg 	return (return_code);
3313939Ssethg }
3323939Ssethg 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
3370Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
3380Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate #define	LCALLSIZE	7
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static int
instr_is_lcall_syscall(caddr_t pc)3443939Ssethg instr_is_lcall_syscall(caddr_t pc)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
3490Sstevel@tonic-gate 	    instr[0] == 0x9a &&
3500Sstevel@tonic-gate 	    instr[1] == 0 &&
3510Sstevel@tonic-gate 	    instr[2] == 0 &&
3520Sstevel@tonic-gate 	    instr[3] == 0 &&
3530Sstevel@tonic-gate 	    instr[4] == 0 &&
3540Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
3550Sstevel@tonic-gate 	    instr[6] == 0)
3560Sstevel@tonic-gate 		return (1);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	return (0);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3611363Sfvdl #ifdef __amd64
3621363Sfvdl 
3631363Sfvdl /*
3643446Smrj  * In the first revisions of amd64 CPUs produced by AMD, the LAHF and
3653446Smrj  * SAHF instructions were not implemented in 64-bit mode. Later revisions
3661363Sfvdl  * did implement these instructions. An extension to the cpuid instruction
3671363Sfvdl  * was added to check for the capability of executing these instructions
3683446Smrj  * in 64-bit mode.
3691363Sfvdl  *
3701363Sfvdl  * Intel originally did not implement these instructions in EM64T either,
3711363Sfvdl  * but added them in later revisions.
3721363Sfvdl  *
3731363Sfvdl  * So, there are different chip revisions by both vendors out there that
3741363Sfvdl  * may or may not implement these instructions. The easy solution is to
3751363Sfvdl  * just always emulate these instructions on demand.
3761363Sfvdl  *
3771363Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
3781363Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
3791363Sfvdl  */
3801363Sfvdl 
3811363Sfvdl #define	LSAHFSIZE 1
3821363Sfvdl 
3831363Sfvdl static int
instr_is_lsahf(caddr_t pc,uchar_t * instr)3841363Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
3851363Sfvdl {
3861363Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
3871363Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
3881363Sfvdl 		return (1);
3891363Sfvdl 	return (0);
3901363Sfvdl }
3911363Sfvdl 
3921363Sfvdl /*
3931363Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
3941363Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
3951363Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
3961363Sfvdl  *
3971363Sfvdl  * Note that %ah is bits 8-15 of %rax.
3981363Sfvdl  */
3991363Sfvdl static void
emulate_lsahf(struct regs * rp,uchar_t instr)4001363Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
4011363Sfvdl {
4021363Sfvdl 	if (instr == 0x9e) {
4031363Sfvdl 		/* sahf. Copy bits from %ah to flags. */
4041363Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
4051363Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
4061363Sfvdl 	} else {
4071363Sfvdl 		/* lahf. Copy bits from flags to %ah. */
4081363Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
4091363Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
4101363Sfvdl 	}
4111363Sfvdl 	rp->r_pc += LSAHFSIZE;
4121363Sfvdl }
4131363Sfvdl #endif /* __amd64 */
4141363Sfvdl 
4150Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
4190Sstevel@tonic-gate  *
4200Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
4210Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
42210136SGeorge.Wilson@Sun.COM  * The third byte (ModRM) contains the register field bits (bits 3-5).
42310136SGeorge.Wilson@Sun.COM  * These bits must be between 0 and 3 inclusive for regular prefetch and
42410136SGeorge.Wilson@Sun.COM  * 0 and 1 inclusive for AMD 3dnow prefetch.
4258241SJeff.Bonwick@Sun.COM  *
4268241SJeff.Bonwick@Sun.COM  * In 64-bit mode, there may be a one-byte REX prefex (0x40-0x4F).
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate static int
cmp_to_prefetch(uchar_t * p)4300Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
4310Sstevel@tonic-gate {
4328241SJeff.Bonwick@Sun.COM #ifdef _LP64
4338241SJeff.Bonwick@Sun.COM 	if ((p[0] & 0xF0) == 0x40)	/* 64-bit REX prefix */
4348241SJeff.Bonwick@Sun.COM 		p++;
4358241SJeff.Bonwick@Sun.COM #endif
43610136SGeorge.Wilson@Sun.COM 	return ((p[0] == 0x0F && p[1] == 0x18 && ((p[2] >> 3) & 7) <= 3) ||
43710136SGeorge.Wilson@Sun.COM 	    (p[0] == 0x0F && p[1] == 0x0D && ((p[2] >> 3) & 7) <= 1));
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate static int
instr_is_prefetch(caddr_t pc)4410Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
4420Sstevel@tonic-gate {
4438241SJeff.Bonwick@Sun.COM 	uchar_t instr[4];	/* optional REX prefix plus 3-byte opcode */
4440Sstevel@tonic-gate 
4458241SJeff.Bonwick@Sun.COM 	return (copyin_nowatch(pc, instr, sizeof (instr)) == 0 &&
4468241SJeff.Bonwick@Sun.COM 	    cmp_to_prefetch(instr));
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
4550Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
4563446Smrj  * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1)
4573446Smrj  * however, there are paths that arrive here with PS_IE == 0 so special care
4583446Smrj  * must be taken in those cases.
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate void
trap(struct regs * rp,caddr_t addr,processorid_t cpuid)4610Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
4620Sstevel@tonic-gate {
4633446Smrj 	kthread_t *ct = curthread;
4640Sstevel@tonic-gate 	enum seg_rw rw;
4650Sstevel@tonic-gate 	unsigned type;
4663446Smrj 	proc_t *p = ttoproc(ct);
4673446Smrj 	klwp_t *lwp = ttolwp(ct);
4680Sstevel@tonic-gate 	uintptr_t lofault;
4690Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
4700Sstevel@tonic-gate 	enum fault_type fault_type;
4710Sstevel@tonic-gate 	k_siginfo_t siginfo;
4720Sstevel@tonic-gate 	uint_t fault = 0;
4730Sstevel@tonic-gate 	int mstate;
4740Sstevel@tonic-gate 	int sicode = 0;
4750Sstevel@tonic-gate 	int watchcode;
4760Sstevel@tonic-gate 	int watchpage;
4770Sstevel@tonic-gate 	caddr_t vaddr;
4782712Snn35248 	int singlestep_twiddle;
4790Sstevel@tonic-gate 	size_t sz;
4800Sstevel@tonic-gate 	int ta;
4811363Sfvdl #ifdef __amd64
4821363Sfvdl 	uchar_t instr;
4831363Sfvdl #endif
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	type = rp->r_trapno;
4880Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
4893446Smrj 	ASSERT(ct->t_schedflag & TS_DONT_SWAP);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (type == T_PGFLT) {
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		errcode = rp->r_err;
4940Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
4950Sstevel@tonic-gate 			rw = S_WRITE;
4960Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
4970Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
4980Sstevel@tonic-gate 			rw = S_EXEC;
4990Sstevel@tonic-gate 		else
5000Sstevel@tonic-gate 			rw = S_READ;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate #if defined(__i386)
5030Sstevel@tonic-gate 		/*
5040Sstevel@tonic-gate 		 * Pentium Pro work-around
5050Sstevel@tonic-gate 		 */
5060Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
5070Sstevel@tonic-gate 			uint_t	attr;
5080Sstevel@tonic-gate 			uint_t	priv_violation;
5090Sstevel@tonic-gate 			uint_t	access_violation;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
5120Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
5130Sstevel@tonic-gate 			    == -1) {
5140Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
5150Sstevel@tonic-gate 			} else {
5160Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
5175084Sjohnlev 				    !(attr & PROT_USER);
5180Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
5195084Sjohnlev 				    !(attr & PROT_WRITE);
5200Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
5210Sstevel@tonic-gate 					goto cleanup;
5220Sstevel@tonic-gate 			}
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate #endif /* __i386 */
5250Sstevel@tonic-gate 
5263446Smrj 	} else if (type == T_SGLSTP && lwp != NULL)
5273446Smrj 		lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (tdebug)
5300Sstevel@tonic-gate 		showregs(type, rp, addr);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
5330Sstevel@tonic-gate 		/*
5340Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
5350Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
5360Sstevel@tonic-gate 		 * The current process credential applies to the thread for
5370Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
5380Sstevel@tonic-gate 		 * should already be set up.
5390Sstevel@tonic-gate 		 */
5403446Smrj 		if (ct->t_cred != p->p_cred) {
5413446Smrj 			cred_t *oldcred = ct->t_cred;
5420Sstevel@tonic-gate 			/*
5430Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
5440Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
5450Sstevel@tonic-gate 			 * allocated cred structure.
5460Sstevel@tonic-gate 			 */
5473446Smrj 			ct->t_cred = crgetcred();
5480Sstevel@tonic-gate 			crfree(oldcred);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		ASSERT(lwp != NULL);
5510Sstevel@tonic-gate 		type |= USER;
5520Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
5530Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 		switch (type) {
5560Sstevel@tonic-gate 		case T_PGFLT + USER:
5570Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
5580Sstevel@tonic-gate 				mstate = LMS_TFAULT;
5590Sstevel@tonic-gate 			else
5600Sstevel@tonic-gate 				mstate = LMS_DFAULT;
5610Sstevel@tonic-gate 			break;
5620Sstevel@tonic-gate 		default:
5630Sstevel@tonic-gate 			mstate = LMS_TRAP;
5640Sstevel@tonic-gate 			break;
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 		/* Kernel probe */
5670Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
5680Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
5693446Smrj 		mstate = new_mstate(ct, mstate);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	switch (type) {
5750Sstevel@tonic-gate 	case T_PGFLT + USER:
5760Sstevel@tonic-gate 	case T_SGLSTP:
5770Sstevel@tonic-gate 	case T_SGLSTP + USER:
5780Sstevel@tonic-gate 	case T_BPTFLT + USER:
5790Sstevel@tonic-gate 		break;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	default:
5820Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
5830Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	switch (type) {
5885849Ssethg 	case T_SIMDFPE:
5895849Ssethg 		/* Make sure we enable interrupts before die()ing */
5905849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
5915849Ssethg 		/*FALLTHROUGH*/
5920Sstevel@tonic-gate 	default:
5930Sstevel@tonic-gate 		if (type & USER) {
5940Sstevel@tonic-gate 			if (tudebug)
5950Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5960Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5970Sstevel@tonic-gate 			    type & ~USER);
5980Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5990Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
6000Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
6010Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
6020Sstevel@tonic-gate 			fault = FLTILL;
6030Sstevel@tonic-gate 			break;
6040Sstevel@tonic-gate 		} else {
6050Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6060Sstevel@tonic-gate 			/*NOTREACHED*/
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
6125254Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
6135254Sgavinm 		 * via the installed trampoline.
6140Sstevel@tonic-gate 		 */
6153446Smrj 		if ((ct->t_ontrap != NULL) &&
6163446Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
6173446Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
6185254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
6195254Sgavinm 			goto cleanup;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		/*
6230Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
6240Sstevel@tonic-gate 		 * across this. Here we assume that an address
6250Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
6260Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
6270Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
6280Sstevel@tonic-gate 		 * starting and because we know that we always have
6290Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
6300Sstevel@tonic-gate 		 */
6313446Smrj 		lofault = ct->t_lofault;
6323446Smrj 		ct->t_lofault = 0;
6330Sstevel@tonic-gate 
6343446Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6370Sstevel@tonic-gate 			res = pagefault(addr,
6380Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6390Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6400Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6410Sstevel@tonic-gate 			    grow(addr))
6420Sstevel@tonic-gate 				res = 0;
6430Sstevel@tonic-gate 		} else {
6440Sstevel@tonic-gate 			res = pagefault(addr,
6450Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6460Sstevel@tonic-gate 		}
6473446Smrj 		(void) new_mstate(ct, mstate);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
6510Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6520Sstevel@tonic-gate 		 */
6533446Smrj 		ct->t_lofault = lofault;
6540Sstevel@tonic-gate 		if (res == 0)
6550Sstevel@tonic-gate 			goto cleanup;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6580Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6590Sstevel@tonic-gate 			/*
6600Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6610Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6620Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6630Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6640Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6650Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6660Sstevel@tonic-gate 			 */
6670Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6700Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6710Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6720Sstevel@tonic-gate 				    PFN_INVALID &&
6730Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6740Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6750Sstevel@tonic-gate 					rp->r_pc = rip;
6760Sstevel@tonic-gate 					goto cleanup;
6770Sstevel@tonic-gate 				}
6780Sstevel@tonic-gate 			}
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6830Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6840Sstevel@tonic-gate 			/*
6850Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6860Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6870Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6880Sstevel@tonic-gate 			 * instruction.
6890Sstevel@tonic-gate 			 */
6900Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 			/*
6930Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6940Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6950Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6960Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6970Sstevel@tonic-gate 			 */
6980Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
6990Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
7000Sstevel@tonic-gate #ifdef DEBUG
7010Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
7020Sstevel@tonic-gate 					    "occurred: kernel prefetch"
7030Sstevel@tonic-gate 					    " at %p generated a page fault!",
7040Sstevel@tonic-gate 					    (void *)rp->r_pc);
7050Sstevel@tonic-gate #endif /* DEBUG */
7060Sstevel@tonic-gate 					goto cleanup;
7070Sstevel@tonic-gate 				}
7080Sstevel@tonic-gate 			}
7090Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 		if (lofault == 0)
7140Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		/*
7170Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
7180Sstevel@tonic-gate 		 */
7190Sstevel@tonic-gate 		if (lodebug) {
7200Sstevel@tonic-gate 			showregs(type, rp, addr);
7210Sstevel@tonic-gate 			traceregs(rp);
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
7240Sstevel@tonic-gate 			res = FC_ERRNO(res);
7250Sstevel@tonic-gate 		else
7260Sstevel@tonic-gate 			res = EFAULT;
7270Sstevel@tonic-gate 		rp->r_r0 = res;
7283446Smrj 		rp->r_pc = ct->t_lofault;
7290Sstevel@tonic-gate 		goto cleanup;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7320Sstevel@tonic-gate 		if (faultdebug) {
7330Sstevel@tonic-gate 			char *fault_str;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 			switch (rw) {
7360Sstevel@tonic-gate 			case S_READ:
7370Sstevel@tonic-gate 				fault_str = "read";
7380Sstevel@tonic-gate 				break;
7390Sstevel@tonic-gate 			case S_WRITE:
7400Sstevel@tonic-gate 				fault_str = "write";
7410Sstevel@tonic-gate 				break;
7420Sstevel@tonic-gate 			case S_EXEC:
7430Sstevel@tonic-gate 				fault_str = "exec";
7440Sstevel@tonic-gate 				break;
7450Sstevel@tonic-gate 			default:
7460Sstevel@tonic-gate 				fault_str = "";
7470Sstevel@tonic-gate 				break;
7480Sstevel@tonic-gate 			}
7490Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7500Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7560Sstevel@tonic-gate 		 *
7570Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7580Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7590Sstevel@tonic-gate 		 * address will be
7600Sstevel@tonic-gate 		 *
7610Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7620Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7630Sstevel@tonic-gate 		 *
7640Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7650Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7660Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7670Sstevel@tonic-gate 		 */
7680Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7690Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7700Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7710Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7720Sstevel@tonic-gate 			if (!opteron_erratum_100)
7730Sstevel@tonic-gate 				panic("unexpected erratum #100");
7740Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7750Sstevel@tonic-gate 				goto out;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7800Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7810Sstevel@tonic-gate #ifdef __i386
7820Sstevel@tonic-gate 		/*
7830Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
7840Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
7850Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
7860Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
7870Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
7880Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
7890Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
7900Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
7910Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
7920Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
7930Sstevel@tonic-gate 		 */
7940Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
7953939Ssethg 		    rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
7960Sstevel@tonic-gate 			extern void watch_syscall(void);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
7990Sstevel@tonic-gate 			watch_syscall();	/* never returns */
8000Sstevel@tonic-gate 			/* NOTREACHED */
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate #endif /* __i386 */
8030Sstevel@tonic-gate 		vaddr = addr;
8040Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
8050Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
8060Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
8070Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
8080Sstevel@tonic-gate 			if (ta) {
8090Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
8105084Sjohnlev 				    watchcode, rp->r_pc);
8110Sstevel@tonic-gate 				fault_type = F_INVAL;
8120Sstevel@tonic-gate 			} else {
8130Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8140Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
8150Sstevel@tonic-gate 				siginfo.si_code = watchcode;
8160Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
8170Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
8180Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
8190Sstevel@tonic-gate 				fault = FLTWATCH;
8200Sstevel@tonic-gate 				break;
8210Sstevel@tonic-gate 			}
8220Sstevel@tonic-gate 		} else {
8230Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
8240Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
8250Sstevel@tonic-gate 				goto out;
8260Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
8270Sstevel@tonic-gate 			fault_type = F_INVAL;
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		/*
8330Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8340Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate 		if (res == 0 ||
8370Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8380Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8390Sstevel@tonic-gate 		    grow(addr))) {
8400Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8410Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8420Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8430Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8440Sstevel@tonic-gate 				siginfo.si_addr = addr;
8450Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8460Sstevel@tonic-gate 			}
8470Sstevel@tonic-gate 			goto out;
8480Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8490Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8500Sstevel@tonic-gate 			report_stack_exec(p, addr);
8510Sstevel@tonic-gate 		}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8560Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8570Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8580Sstevel@tonic-gate 		 *
8590Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8600Sstevel@tonic-gate 		 */
8610Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8620Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8630Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8640Sstevel@tonic-gate #ifdef DEBUG
8650Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8660Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8670Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8680Sstevel@tonic-gate #endif /* DEBUG */
8690Sstevel@tonic-gate 			goto out;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		if (tudebug)
8740Sstevel@tonic-gate 			showregs(type, rp, addr);
8750Sstevel@tonic-gate 		/*
8760Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8770Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8780Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8790Sstevel@tonic-gate 		 */
8800Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8810Sstevel@tonic-gate 		siginfo.si_addr = addr;
8820Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8830Sstevel@tonic-gate 		case FC_HWERR:
8840Sstevel@tonic-gate 		case FC_NOSUPPORT:
8850Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8860Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8870Sstevel@tonic-gate 			fault = FLTACCESS;
8880Sstevel@tonic-gate 			break;
8890Sstevel@tonic-gate 		case FC_ALIGN:
8900Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8910Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8920Sstevel@tonic-gate 			fault = FLTACCESS;
8930Sstevel@tonic-gate 			break;
8940Sstevel@tonic-gate 		case FC_OBJERR:
8950Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8960Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8970Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8980Sstevel@tonic-gate 				fault = FLTACCESS;
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 			break;
9010Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
9020Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9030Sstevel@tonic-gate 			siginfo.si_code =
9040Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
9050Sstevel@tonic-gate 			fault = FLTBOUNDS;
9060Sstevel@tonic-gate 			break;
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 		break;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
9110Sstevel@tonic-gate 		/*
9120Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
9130Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
9140Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
9150Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
9160Sstevel@tonic-gate 		 * be to emulate that particular instruction.
9170Sstevel@tonic-gate 		 */
9180Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
919*12826Skuriakose.kuruvilla@oracle.com 		    ldt_rewrite_syscall(rp, p, X86FSET_ASYSC))
9203939Ssethg 			goto out;
9211363Sfvdl 
9221363Sfvdl #ifdef __amd64
9231363Sfvdl 		/*
9241363Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
9251363Sfvdl 		 * See the instr_is_lsahf function for details.
9261363Sfvdl 		 */
9271363Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
9281363Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
9291363Sfvdl 			emulate_lsahf(rp, instr);
9301363Sfvdl 			goto out;
9311363Sfvdl 		}
9321363Sfvdl #endif
9331363Sfvdl 
9340Sstevel@tonic-gate 		/*FALLTHROUGH*/
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		if (tudebug)
9370Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9380Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9390Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9400Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9410Sstevel@tonic-gate 		fault = FLTILL;
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9450Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9460Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9470Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9480Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9490Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9500Sstevel@tonic-gate 		fault = FLTIZDIV;
9510Sstevel@tonic-gate 		break;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9540Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9550Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9560Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9570Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9580Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9590Sstevel@tonic-gate 		fault = FLTIOVF;
9600Sstevel@tonic-gate 		break;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9630Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9640Sstevel@tonic-gate 			showregs(type, rp, addr);
9650Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
9669384SRoger.Faulkner@Sun.COM 			siginfo.si_signo = SIGILL;
9670Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
9680Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9699384SRoger.Faulkner@Sun.COM 			fault = FLTILL;
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		break;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
9740Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9750Sstevel@tonic-gate 		{
9760Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9775849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
9785849Ssethg 				sti(); /* T_EXTOVRFLT comes in via cmninttrap */
9790Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9805849Ssethg 			}
9810Sstevel@tonic-gate 			type |= USER;
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 		/*FALLTHROUGH*/
9840Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
9850Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9860Sstevel@tonic-gate 			showregs(type, rp, addr);
9870Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9880Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9890Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9900Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9910Sstevel@tonic-gate 			fault = FLTBOUNDS;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9960Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9970Sstevel@tonic-gate 		{
9980Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9995849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
10005849Ssethg 				sti(); /* T_EXTERRFLT comes in via cmninttrap */
10010Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
10025849Ssethg 			}
10030Sstevel@tonic-gate 			type |= USER;
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 		/*FALLTHROUGH*/
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
10080Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
10090Sstevel@tonic-gate 			showregs(type, rp, addr);
10100Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
10110Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10120Sstevel@tonic-gate 			siginfo.si_code  = sicode;
10130Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
10140Sstevel@tonic-gate 			fault = FLTFPE;
10150Sstevel@tonic-gate 		}
10160Sstevel@tonic-gate 		break;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
10190Sstevel@tonic-gate 		if (tudebug && tudebugsse)
10200Sstevel@tonic-gate 			showregs(type, rp, addr);
1021*12826Skuriakose.kuruvilla@oracle.com 		if (!is_x86_feature(x86_featureset, X86FSET_SSE) &&
1022*12826Skuriakose.kuruvilla@oracle.com 		    !is_x86_feature(x86_featureset, X86FSET_SSE2)) {
10230Sstevel@tonic-gate 			/*
10240Sstevel@tonic-gate 			 * There are rumours that some user instructions
10250Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
10260Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
10270Sstevel@tonic-gate 			 */
10280Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
10290Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
10300Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
10310Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
10320Sstevel@tonic-gate 			fault = FLTILL;
10330Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
10340Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10350Sstevel@tonic-gate 			siginfo.si_code = sicode;
10360Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
10370Sstevel@tonic-gate 			fault = FLTFPE;
10380Sstevel@tonic-gate 		}
10395849Ssethg 
10405849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
10410Sstevel@tonic-gate 		break;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10440Sstevel@tonic-gate 		/*
10450Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10460Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10470Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10480Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10490Sstevel@tonic-gate 		 * fatal.
10500Sstevel@tonic-gate 		 */
10510Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10520Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10550Sstevel@tonic-gate 		break;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 		/* Now evaluate how we got here */
10600Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
10610Sstevel@tonic-gate 			/*
10620Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
10630Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
10640Sstevel@tonic-gate 			 * the first instruction in privileged mode.
10650Sstevel@tonic-gate 			 *
10660Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
10670Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
10680Sstevel@tonic-gate 			 *
10690Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
10700Sstevel@tonic-gate 			 */
10712712Snn35248 			singlestep_twiddle = 0;
10722712Snn35248 			if (rp->r_pc == (uintptr_t)sys_sysenter ||
10732712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_sysenter) {
10742712Snn35248 				singlestep_twiddle = 1;
10750Sstevel@tonic-gate #if defined(__amd64)
10760Sstevel@tonic-gate 				/*
10772712Snn35248 				 * Since we are already on the kernel's
10782712Snn35248 				 * %gs, on 64-bit systems the sysenter case
10792712Snn35248 				 * needs to adjust the pc to avoid
10802712Snn35248 				 * executing the swapgs instruction at the
10812712Snn35248 				 * top of the handler.
10820Sstevel@tonic-gate 				 */
10832712Snn35248 				if (rp->r_pc == (uintptr_t)sys_sysenter)
10842712Snn35248 					rp->r_pc = (uintptr_t)
10852712Snn35248 					    _sys_sysenter_post_swapgs;
10862712Snn35248 				else
10872712Snn35248 					rp->r_pc = (uintptr_t)
10882712Snn35248 					    _brand_sys_sysenter_post_swapgs;
10890Sstevel@tonic-gate #endif
10902712Snn35248 			}
10912712Snn35248 #if defined(__i386)
10922712Snn35248 			else if (rp->r_pc == (uintptr_t)sys_call ||
10932712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_call) {
10942712Snn35248 				singlestep_twiddle = 1;
10952712Snn35248 			}
10962712Snn35248 #endif
10973003Sdmick 			else {
10983003Sdmick 				/* not on sysenter/syscall; uregs available */
10993003Sdmick 				if (tudebug && tudebugbpt)
11003003Sdmick 					showregs(type, rp, (caddr_t)0);
11013003Sdmick 			}
11022712Snn35248 			if (singlestep_twiddle) {
11030Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
11040Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
11053446Smrj 				ct->t_post_sys = 1;
11062086Ssudheer 				aston(curthread);
11070Sstevel@tonic-gate 				goto cleanup;
11080Sstevel@tonic-gate 			}
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
11110Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
11120Sstevel@tonic-gate 			debug_enter((char *)NULL);
11130Sstevel@tonic-gate 		else
11140Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11150Sstevel@tonic-gate 		break;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
11180Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
11190Sstevel@tonic-gate 		goto cleanup;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
11220Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
11230Sstevel@tonic-gate 		break;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
11260Sstevel@tonic-gate 		/*
11275639Sgavinm 		 * Any #GP that occurs during an on_trap .. no_trap bracket
11285639Sgavinm 		 * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection,
11295639Sgavinm 		 * or in a on_fault .. no_fault bracket, is forgiven
11305639Sgavinm 		 * and we trampoline.  This protection is given regardless
11315639Sgavinm 		 * of whether we are 32/64 bit etc - if a distinction is
11325639Sgavinm 		 * required then define new on_trap protection types.
11335639Sgavinm 		 *
11340Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
11355084Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
11365084Sjohnlev 		 * update_sregs while updating user segment registers.
11375084Sjohnlev 		 *
11385084Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
11395084Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
11405084Sjohnlev 		 * lives which is protected by segmentation.
11410Sstevel@tonic-gate 		 */
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		/*
11440Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
11455254Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
11465084Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
11470Sstevel@tonic-gate 		 */
11483446Smrj 		if (ct->t_ontrap != NULL) {
11495084Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
11505084Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
11513446Smrj 
11525084Sjohnlev 			if (ttype != 0) {
11535084Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
11545084Sjohnlev 				if (tudebug)
11555084Sjohnlev 					showregs(type, rp, (caddr_t)0);
11565254Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
11575254Sgavinm 				goto cleanup;
11585084Sjohnlev 			}
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 		/*
11620Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
11630Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
11640Sstevel@tonic-gate 		 */
11653446Smrj 		if (ct->t_lofault) {
11660Sstevel@tonic-gate 			/*
11670Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11680Sstevel@tonic-gate 			 */
11690Sstevel@tonic-gate 			if (lodebug) {
11700Sstevel@tonic-gate 				showregs(type, rp, addr);
11710Sstevel@tonic-gate 				traceregs(rp);
11720Sstevel@tonic-gate 			}
11730Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
11743446Smrj 			rp->r_pc = ct->t_lofault;
11750Sstevel@tonic-gate 			goto cleanup;
11760Sstevel@tonic-gate 		}
11775639Sgavinm 
11785639Sgavinm 		/*
11795639Sgavinm 		 * We fall through to the next case, which repeats
11805639Sgavinm 		 * the OT_SEGMENT_ACCESS check which we've already
11815639Sgavinm 		 * done, so we'll always fall through to the
11825639Sgavinm 		 * T_STKFLT case.
11835639Sgavinm 		 */
11840Sstevel@tonic-gate 		/*FALLTHROUGH*/
11853446Smrj 	case T_SEGFLT:	/* segment not present fault */
11863446Smrj 		/*
11873446Smrj 		 * One example of this is #NP in update_sregs while
11883446Smrj 		 * attempting to update a user segment register
11893446Smrj 		 * that points to a descriptor that is marked not
11903446Smrj 		 * present.
11913446Smrj 		 */
11923446Smrj 		if (ct->t_ontrap != NULL &&
11933446Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
11943446Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
11955084Sjohnlev 			if (tudebug)
11965084Sjohnlev 				showregs(type, rp, (caddr_t)0);
11975254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
11985254Sgavinm 			goto cleanup;
11993446Smrj 		}
12003446Smrj 		/*FALLTHROUGH*/
12010Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
12020Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
12030Sstevel@tonic-gate 		if (tudebug)
12040Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12050Sstevel@tonic-gate 		if (kern_gpfault(rp))
12060Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
12070Sstevel@tonic-gate 		goto cleanup;
12080Sstevel@tonic-gate 
12093446Smrj 	/*
12103446Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
12113446Smrj 	 * should have no need for them, so we put a stop to it here.
12123446Smrj 	 *
12133446Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
12143446Smrj 	 * a private LDT trying to do a system call. Emulate it.
12153446Smrj 	 *
12163446Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
12173446Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
12183446Smrj 	 */
12193446Smrj 
12200Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
12211217Srab 	case T_GPFLT + USER:	/* general protection violation */
12220Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12231217Srab 		if (p->p_model != DATAMODEL_NATIVE) {
12241217Srab #endif /* _SYSCALL32_IMPL */
12253939Ssethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
12261217Srab 			if (type == T_SEGFLT + USER)
12271217Srab 				ASSERT(p->p_ldt != NULL);
12281217Srab 
12291217Srab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
12301217Srab 			    type == T_SEGFLT + USER) {
12311217Srab 
12320Sstevel@tonic-gate 			/*
12331217Srab 			 * The user attempted a system call via the obsolete
12341217Srab 			 * call gate mechanism. Because the process doesn't have
12351217Srab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
12361217Srab 			 * Emulate the syscall here, just as we do above for a
12371217Srab 			 * #np trap.
12381217Srab 			 */
12391217Srab 
12401217Srab 			/*
12410Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
12420Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
12430Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
12440Sstevel@tonic-gate 			 */
12450Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
12460Sstevel@tonic-gate 			lwp->lwp_regs = rp;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 			/*
12490Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
12500Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
12510Sstevel@tonic-gate 			 * behavior here.
12520Sstevel@tonic-gate 			 */
12530Sstevel@tonic-gate 			mstate = LMS_USER;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 			dosyscall();
12560Sstevel@tonic-gate 			goto out;
12571217Srab 			}
12581217Srab 		}
12591217Srab #ifdef _SYSCALL32_IMPL
12600Sstevel@tonic-gate 		}
12610Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
12620Sstevel@tonic-gate 		/*
12630Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
12640Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
12650Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
12660Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12670Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12680Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12690Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12700Sstevel@tonic-gate 		 */
12710Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
1272*12826Skuriakose.kuruvilla@oracle.com 		    ldt_rewrite_syscall(rp, p, X86FSET_SEP))
12733939Ssethg 			goto out;
12743939Ssethg 
12750Sstevel@tonic-gate 		/*FALLTHROUGH*/
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12780Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12790Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12800Sstevel@tonic-gate 		if (tudebug)
12810Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12820Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12830Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12840Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12850Sstevel@tonic-gate 		fault = FLTBOUNDS;
12860Sstevel@tonic-gate 		break;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12890Sstevel@tonic-gate 		if (tudebug)
12900Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12910Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12920Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12930Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12940Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12950Sstevel@tonic-gate 		fault = FLTACCESS;
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
12990Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
13000Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		/* Was it single-stepping? */
13030Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
13040Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
13070Sstevel@tonic-gate 			/*
13080Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
13092712Snn35248 			 * give precedence to WATCH_STEP.  If neither is set,
13100Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
13110Sstevel@tonic-gate 			 * as NORMAL_STEP.
13120Sstevel@tonic-gate 			 */
13132712Snn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
13142712Snn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
13152712Snn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
13160Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
13170Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
13180Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13190Sstevel@tonic-gate 				fault = FLTTRACE;
13200Sstevel@tonic-gate 			}
13210Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 		break;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
13260Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
13270Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
13280Sstevel@tonic-gate 		/*
13290Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
13300Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
13310Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
13320Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
13330Sstevel@tonic-gate 		 */
13340Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
13350Sstevel@tonic-gate 			rp->r_pc--;
13360Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
13370Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
13380Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
13390Sstevel@tonic-gate 		fault = FLTBPT;
13400Sstevel@tonic-gate 		break;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	case T_AST:
13430Sstevel@tonic-gate 		/*
13440Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
13450Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
13460Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
13470Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
13480Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
13490Sstevel@tonic-gate 		 * doing the T_AST processing.
13500Sstevel@tonic-gate 		 */
13510Sstevel@tonic-gate 		goto cleanup;
13520Sstevel@tonic-gate 
13535254Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
13545254Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
13555254Sgavinm 			proc_t *p = ttoproc(curthread);
13569613SAbhinandan.Ekande@Sun.COM 			extern void print_msg_hwerr(ctid_t ct_id, proc_t *p);
13575254Sgavinm 
13585254Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
13599613SAbhinandan.Ekande@Sun.COM 			print_msg_hwerr(p->p_ct_process->conp_contract.ct_id,
13609613SAbhinandan.Ekande@Sun.COM 			    p);
13615254Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
13625254Sgavinm 			siginfo.si_signo = SIGKILL;
13635254Sgavinm 			siginfo.si_code = SI_NOINFO;
13645254Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
13650Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
13660Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13670Sstevel@tonic-gate 				/*
13680Sstevel@tonic-gate 				 * Signal performance counter overflow
13690Sstevel@tonic-gate 				 */
13700Sstevel@tonic-gate 				if (tudebug)
13710Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13720Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13730Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13740Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13750Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13760Sstevel@tonic-gate 				fault = FLTCPCOVF;
13770Sstevel@tonic-gate 			}
13780Sstevel@tonic-gate 		}
13795254Sgavinm 
13800Sstevel@tonic-gate 		break;
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	/*
13840Sstevel@tonic-gate 	 * We can't get here from a system trap
13850Sstevel@tonic-gate 	 */
13860Sstevel@tonic-gate 	ASSERT(type & USER);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	if (fault) {
13893506Saf 		/* We took a fault so abort single step. */
13903506Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13910Sstevel@tonic-gate 		/*
13920Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13930Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13940Sstevel@tonic-gate 		 */
13950Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13960Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		/*
14010Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
14020Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
14030Sstevel@tonic-gate 		 * deliver the associated signal.
14040Sstevel@tonic-gate 		 */
14050Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
14060Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
14070Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
14080Sstevel@tonic-gate 			siginfo.si_signo = 0;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	if (siginfo.si_signo)
14129384SRoger.Faulkner@Sun.COM 		trapsig(&siginfo, (fault != FLTFPE && fault != FLTCPCOVF));
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
14150Sstevel@tonic-gate 		profil_tick(rp->r_pc);
14160Sstevel@tonic-gate 
14173446Smrj 	if (ct->t_astflag | ct->t_sig_check) {
14180Sstevel@tonic-gate 		/*
14190Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
14200Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
14210Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
14220Sstevel@tonic-gate 		 * syscall.
14230Sstevel@tonic-gate 		 */
14243446Smrj 		astoff(ct);
14252086Ssudheer 		/*
14262086Ssudheer 		 * If a single-step trap occurred on a syscall (see above)
14272086Ssudheer 		 * recognize it now.  Do this before checking for signals
14282086Ssudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
14292086Ssudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
14302086Ssudheer 		 */
14312086Ssudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
14322086Ssudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
14332086Ssudheer 
14343446Smrj 		ct->t_sig_check = 0;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
14370Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
14380Sstevel@tonic-gate 			timer_lwpbind();
14390Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		/*
14440Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
14450Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
14460Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
14470Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
14480Sstevel@tonic-gate 		 * for the kaio request to complete.
14490Sstevel@tonic-gate 		 */
14500Sstevel@tonic-gate 		if (p->p_aio)
14510Sstevel@tonic-gate 			aio_cleanup(0);
14520Sstevel@tonic-gate 		/*
14530Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
14540Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
14550Sstevel@tonic-gate 		 * sets the AST flag.
14560Sstevel@tonic-gate 		 *
14570Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14580Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14590Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14600Sstevel@tonic-gate 		 */
14610Sstevel@tonic-gate 		if (ISHOLD(p))
14620Sstevel@tonic-gate 			holdlwp();
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		/*
14650Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14660Sstevel@tonic-gate 		 * set t_astflag afterwards.
14670Sstevel@tonic-gate 		 */
14683446Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14690Sstevel@tonic-gate 			if (issig(FORREAL))
14700Sstevel@tonic-gate 				psig();
14713446Smrj 			ct->t_sig_check = 1;
14720Sstevel@tonic-gate 		}
14730Sstevel@tonic-gate 
14743446Smrj 		if (ct->t_rprof != NULL) {
14759870SRoger.Faulkner@Sun.COM 			realsigprof(0, 0, 0);
14763446Smrj 			ct->t_sig_check = 1;
14770Sstevel@tonic-gate 		}
14781468Smarx 
14791468Smarx 		/*
14801468Smarx 		 * /proc can't enable/disable the trace bit itself
14811468Smarx 		 * because that could race with the call gate used by
14821468Smarx 		 * system calls via "lcall". If that happened, an
14831468Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
14841468Smarx 		 * therefore schedule an AST for the purpose.
14851468Smarx 		 */
14861468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
14871468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
14881468Smarx 			rp->r_ps |= PS_T;
14891468Smarx 		}
14901468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
14911468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
14921468Smarx 			rp->r_ps &= ~PS_T;
14931468Smarx 		}
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14970Sstevel@tonic-gate 	ASSERT(type & USER);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	if (ISHOLD(p))
15000Sstevel@tonic-gate 		holdlwp();
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
15040Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
15050Sstevel@tonic-gate 	 * restore the user-level priority.
15060Sstevel@tonic-gate 	 *
15070Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
15080Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
15090Sstevel@tonic-gate 	 * is set back to LWP_SYS).
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
15120Sstevel@tonic-gate 
15133446Smrj 	if (ct->t_trapret) {
15143446Smrj 		ct->t_trapret = 0;
15153446Smrj 		thread_lock(ct);
15163446Smrj 		CL_TRAPRET(ct);
15173446Smrj 		thread_unlock(ct);
15180Sstevel@tonic-gate 	}
15193792Sakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
15200Sstevel@tonic-gate 		preempt();
152110230SRoger.Faulkner@Sun.COM 	prunstop();
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
kpreempt(int asyncspl)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
showregs(uint_t type,struct regs * rp,caddr_t addr)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
dumpregs(struct regs * rp)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
instr_is_iret(caddr_t pc)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
instr_is_segregs_pop(caddr_t pc)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
instr_is_sys_rtt(caddr_t pc)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
kern_gpfault(struct regs * rp)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;
185311861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18565084Sjohnlev 	 * if we're not an lwp, or in the case of running native the
18575084Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
18585084Sjohnlev 	 * be die()ing horribly.
18590Sstevel@tonic-gate 	 */
18605084Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
18610Sstevel@tonic-gate 		return (1);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	/*
18640Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
18650Sstevel@tonic-gate 	 *
18660Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
18670Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
18680Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
18690Sstevel@tonic-gate 	 * _sys_rtt. Ew.
18700Sstevel@tonic-gate 	 */
18715084Sjohnlev 	if (instr_is_iret(pc)) {
18720Sstevel@tonic-gate 		/*
18735084Sjohnlev 		 * We took the #gp while trying to perform the IRET.
18740Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
18750Sstevel@tonic-gate 		 * All we know for sure is that most of the general
18760Sstevel@tonic-gate 		 * registers have been restored, including the
18770Sstevel@tonic-gate 		 * segment registers, and all we have left on the
18780Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
18790Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
18800Sstevel@tonic-gate 		 *
18810Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
18820Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
18830Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
18840Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
18850Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
18860Sstevel@tonic-gate 		 */
18870Sstevel@tonic-gate 		trp = &tmpregs;
18880Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
18890Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
18900Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
18910Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
18920Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
18930Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 		/*
18960Sstevel@tonic-gate 		 * Validate simple math
18970Sstevel@tonic-gate 		 */
18980Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18990Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
19000Sstevel@tonic-gate 
19015084Sjohnlev 
19025084Sjohnlev 
19035084Sjohnlev 	}
19045084Sjohnlev 
19055084Sjohnlev #if defined(__amd64)
19065084Sjohnlev 	if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) {
19075084Sjohnlev 
19085084Sjohnlev 		/*
19095084Sjohnlev 		 * This is the common case -- we're trying to load
19105084Sjohnlev 		 * a bad segment register value in the only section
19115084Sjohnlev 		 * of kernel code that ever loads segment registers.
19125084Sjohnlev 		 *
19135084Sjohnlev 		 * We don't need to do anything at this point because
19145084Sjohnlev 		 * the pcb contains all the pending segment register
19155084Sjohnlev 		 * state, and the regs are still intact because we
19165084Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
19175084Sjohnlev 		 * of all this, we could conceivably send a signal
19185084Sjohnlev 		 * to the lwp, rather than core-ing.
19195084Sjohnlev 		 */
19205084Sjohnlev 		trp = lwptoregs(lwp);
19215084Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate #elif defined(__i386)
19250Sstevel@tonic-gate 
19265084Sjohnlev 	if (trp == NULL && instr_is_segregs_pop(pc))
19275084Sjohnlev 		trp = lwptoregs(lwp);
19280Sstevel@tonic-gate 
19295084Sjohnlev #endif	/* __i386 */
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	if (trp == NULL)
19320Sstevel@tonic-gate 		return (1);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	/*
19350Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
19360Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
19370Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
19380Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
19390Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
19400Sstevel@tonic-gate 	 */
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	/*
19430Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
19440Sstevel@tonic-gate 	 */
19450Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
19460Sstevel@tonic-gate 	trp->r_err = rp->r_err;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
19490Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
19500Sstevel@tonic-gate 
19515084Sjohnlev 
19520Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19530Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
19540Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	/*
19575084Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
19585084Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
19590Sstevel@tonic-gate 	 */
1960390Sraf 	proc_is_exiting(p);
19610Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
19620Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19630Sstevel@tonic-gate 		lwp_exit();
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
196611861SMarek.Pospisil@Sun.COM 	if (auditing)		/* audit core dump */
19670Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
19680Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
196911861SMarek.Pospisil@Sun.COM 	if (auditing)		/* audit core dump */
19700Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
19710Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
19720Sstevel@tonic-gate 	return (0);
19730Sstevel@tonic-gate }
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate /*
19760Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
19770Sstevel@tonic-gate  */
19780Sstevel@tonic-gate 
19795084Sjohnlev #if !defined(__xpv)
19800Sstevel@tonic-gate #if defined(__amd64)
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate static void
dump_tss(void)19830Sstevel@tonic-gate dump_tss(void)
19840Sstevel@tonic-gate {
19850Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19860Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
19890Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
19900Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
19930Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
19940Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
19950Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
19960Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
19970Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
19980Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
19990Sstevel@tonic-gate }
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate #elif defined(__i386)
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate static void
dump_tss(void)20040Sstevel@tonic-gate dump_tss(void)
20050Sstevel@tonic-gate {
20060Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
20070Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
20080Sstevel@tonic-gate 
2009286Sdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
2010286Sdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
2011286Sdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
2012286Sdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
2013286Sdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
2014286Sdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
2015286Sdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
2016286Sdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
2017286Sdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
2018286Sdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
2019286Sdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
2020286Sdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
2021286Sdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
2022286Sdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
2023286Sdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate #endif	/* __amd64 */
20275084Sjohnlev #endif	/* !__xpv */
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate #if defined(TRAPTRACE)
20300Sstevel@tonic-gate 
20315084Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
20325084Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate /*
20350Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
20360Sstevel@tonic-gate  */
20370Sstevel@tonic-gate static void
dump_ttrace(void)20380Sstevel@tonic-gate dump_ttrace(void)
20390Sstevel@tonic-gate {
20400Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
20410Sstevel@tonic-gate 	trap_trace_rec_t *rec;
20420Sstevel@tonic-gate 	uintptr_t current;
20430Sstevel@tonic-gate 	int i, j, k;
20440Sstevel@tonic-gate 	int n = NCPU;
20450Sstevel@tonic-gate #if defined(__amd64)
20460Sstevel@tonic-gate 	const char banner[] =
20475084Sjohnlev 	    "\ncpu          address    timestamp "
20485084Sjohnlev 	    "type  vc  handler   pc\n";
20490Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
20500Sstevel@tonic-gate #elif defined(__i386)
20510Sstevel@tonic-gate 	const char banner[] =
20525084Sjohnlev 	    "\ncpu  address     timestamp type  vc  handler   pc\n";
20530Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
20540Sstevel@tonic-gate #endif
20550Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
20560Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	if (ttrace_nrec == 0)
20590Sstevel@tonic-gate 		return;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	printf(banner);
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20640Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
20650Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
20660Sstevel@tonic-gate 			continue;
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
20690Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
20700Sstevel@tonic-gate 			struct sysent	*sys;
20710Sstevel@tonic-gate 			struct autovec	*vec;
20720Sstevel@tonic-gate 			extern struct av_head autovect[];
20730Sstevel@tonic-gate 			int type;
20740Sstevel@tonic-gate 			ulong_t	off;
20750Sstevel@tonic-gate 			char *sym, *stype;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 			if (current < ttc->ttc_first)
20780Sstevel@tonic-gate 				current =
20790Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 			if (current == NULL)
20820Sstevel@tonic-gate 				continue;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
20870Sstevel@tonic-gate 				break;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 			switch (rec->ttr_marker) {
20920Sstevel@tonic-gate 			case TT_SYSCALL:
20930Sstevel@tonic-gate 			case TT_SYSENTER:
20940Sstevel@tonic-gate 			case TT_SYSC:
20950Sstevel@tonic-gate 			case TT_SYSC64:
20960Sstevel@tonic-gate #if defined(__amd64)
20970Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
20980Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20990Sstevel@tonic-gate 				case TT_SYSC64:
21000Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
21010Sstevel@tonic-gate 					/*FALLTHROUGH*/
21020Sstevel@tonic-gate #elif defined(__i386)
21030Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
21040Sstevel@tonic-gate 				switch (rec->ttr_marker) {
21050Sstevel@tonic-gate 				case TT_SYSC64:
21060Sstevel@tonic-gate #endif
21070Sstevel@tonic-gate 				case TT_SYSC:
21080Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
21090Sstevel@tonic-gate 					break;
21100Sstevel@tonic-gate 				case TT_SYSCALL:
21110Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
21120Sstevel@tonic-gate 					break;
21130Sstevel@tonic-gate 				case TT_SYSENTER:
21140Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
21150Sstevel@tonic-gate 					break;
21160Sstevel@tonic-gate 				default:
21170Sstevel@tonic-gate 					break;
21180Sstevel@tonic-gate 				}
21190Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
21200Sstevel@tonic-gate 				if (sys != NULL) {
21210Sstevel@tonic-gate 					sym = kobj_getsymname(
21220Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
21230Sstevel@tonic-gate 					    &off);
21240Sstevel@tonic-gate 					if (sym != NULL)
21253446Smrj 						printf(fmt3, sym);
21260Sstevel@tonic-gate 					else
21270Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
21280Sstevel@tonic-gate 				} else {
21293446Smrj 					printf(fmt3, "unknown");
21300Sstevel@tonic-gate 				}
21310Sstevel@tonic-gate 				break;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 			case TT_INTERRUPT:
21340Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
213512683SJimmy.Vetayases@oracle.com 				if (get_intr_handler != NULL)
213612683SJimmy.Vetayases@oracle.com 					vec = (struct autovec *)
213712683SJimmy.Vetayases@oracle.com 					    (*get_intr_handler)
213812683SJimmy.Vetayases@oracle.com 					    (rec->ttr_cpuid, rec->ttr_vector);
213912683SJimmy.Vetayases@oracle.com 				else
214012683SJimmy.Vetayases@oracle.com 					vec =
214112683SJimmy.Vetayases@oracle.com 					    autovect[rec->ttr_vector].avh_link;
214212683SJimmy.Vetayases@oracle.com 
21430Sstevel@tonic-gate 				if (vec != NULL) {
21440Sstevel@tonic-gate 					sym = kobj_getsymname(
21450Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
21460Sstevel@tonic-gate 					if (sym != NULL)
21473446Smrj 						printf(fmt3, sym);
21480Sstevel@tonic-gate 					else
21490Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
21500Sstevel@tonic-gate 				} else {
21513446Smrj 					printf(fmt3, "unknown ");
21520Sstevel@tonic-gate 				}
21530Sstevel@tonic-gate 				break;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 			case TT_TRAP:
21563446Smrj 			case TT_EVENT:
21570Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
21580Sstevel@tonic-gate 				printf(fmt2, "trap", type);
21593446Smrj 				if (type < TRAP_TYPES)
21603446Smrj 					printf("     #%s ",
21613446Smrj 					    trap_type_mnemonic[type]);
21623446Smrj 				else
21633446Smrj 					switch (type) {
21643446Smrj 					case T_AST:
21653446Smrj 						printf(fmt3, "ast");
21663446Smrj 						break;
21673446Smrj 					default:
21683446Smrj 						printf(fmt3, "");
21693446Smrj 						break;
21703446Smrj 					}
21713446Smrj 				break;
21723446Smrj 
21730Sstevel@tonic-gate 			default:
21740Sstevel@tonic-gate 				break;
21750Sstevel@tonic-gate 			}
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
21780Sstevel@tonic-gate 			if (sym != NULL)
21790Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
21800Sstevel@tonic-gate 			else
21810Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
21840Sstevel@tonic-gate 				int s;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
21870Sstevel@tonic-gate 					printf(
21880Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
21890Sstevel@tonic-gate 					    rec->ttr_ipl,
21900Sstevel@tonic-gate 					    rec->ttr_spl,
21910Sstevel@tonic-gate 					    rec->ttr_pri);
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
21960Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 				/*
21990Sstevel@tonic-gate 				 * print out the pc stack that we recorded
22000Sstevel@tonic-gate 				 * at trap time (if any)
22010Sstevel@tonic-gate 				 */
22020Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
22030Sstevel@tonic-gate 					uintptr_t fullpc;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
22060Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
22070Sstevel@tonic-gate 						break;
22080Sstevel@tonic-gate 					}
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
22130Sstevel@tonic-gate 					if (sym != NULL)
22140Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
22150Sstevel@tonic-gate 						    sym, off);
22160Sstevel@tonic-gate 					else
22170Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
22180Sstevel@tonic-gate 				}
22190Sstevel@tonic-gate 				printf("\n");
22200Sstevel@tonic-gate 			}
22210Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
22220Sstevel@tonic-gate 		}
22230Sstevel@tonic-gate 	}
22240Sstevel@tonic-gate }
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate #endif	/* TRAPTRACE */
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate void
22295084Sjohnlev panic_showtrap(struct panic_trap_info *tip)
22300Sstevel@tonic-gate {
22310Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate #if defined(TRAPTRACE)
22340Sstevel@tonic-gate 	dump_ttrace();
22355084Sjohnlev #endif
22360Sstevel@tonic-gate 
22375084Sjohnlev #if !defined(__xpv)
22380Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
22390Sstevel@tonic-gate 		dump_tss();
22405084Sjohnlev #endif
22410Sstevel@tonic-gate }
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate void
22445084Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
22450Sstevel@tonic-gate {
22460Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
22470Sstevel@tonic-gate }
2248