xref: /onnv-gate/usr/src/uts/i86pc/os/trap.c (revision 6336:4eaf084434c9)
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 /*
235849Ssethg  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
290Sstevel@tonic-gate /*		All Rights Reserved   				*/
300Sstevel@tonic-gate /*								*/
310Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
320Sstevel@tonic-gate /*		All Rights Reserved   				*/
330Sstevel@tonic-gate /*								*/
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/signal.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/user.h>
430Sstevel@tonic-gate #include <sys/proc.h>
440Sstevel@tonic-gate #include <sys/disp.h>
450Sstevel@tonic-gate #include <sys/class.h>
460Sstevel@tonic-gate #include <sys/core.h>
470Sstevel@tonic-gate #include <sys/syscall.h>
480Sstevel@tonic-gate #include <sys/cpuvar.h>
490Sstevel@tonic-gate #include <sys/vm.h>
500Sstevel@tonic-gate #include <sys/sysinfo.h>
510Sstevel@tonic-gate #include <sys/fault.h>
520Sstevel@tonic-gate #include <sys/stack.h>
530Sstevel@tonic-gate #include <sys/psw.h>
540Sstevel@tonic-gate #include <sys/regset.h>
550Sstevel@tonic-gate #include <sys/fp.h>
560Sstevel@tonic-gate #include <sys/trap.h>
570Sstevel@tonic-gate #include <sys/kmem.h>
580Sstevel@tonic-gate #include <sys/vtrace.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/prsystm.h>
610Sstevel@tonic-gate #include <sys/mutex_impl.h>
620Sstevel@tonic-gate #include <sys/machsystm.h>
630Sstevel@tonic-gate #include <sys/archsystm.h>
640Sstevel@tonic-gate #include <sys/sdt.h>
650Sstevel@tonic-gate #include <sys/avintr.h>
660Sstevel@tonic-gate #include <sys/kobj.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <vm/hat.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <vm/seg_kmem.h>
710Sstevel@tonic-gate #include <vm/as.h>
720Sstevel@tonic-gate #include <vm/seg.h>
730Sstevel@tonic-gate #include <vm/hat_pte.h>
743446Smrj #include <vm/hat_i86.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #include <sys/procfs.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #include <sys/reboot.h>
790Sstevel@tonic-gate #include <sys/debug.h>
800Sstevel@tonic-gate #include <sys/debugreg.h>
810Sstevel@tonic-gate #include <sys/modctl.h>
820Sstevel@tonic-gate #include <sys/aio_impl.h>
830Sstevel@tonic-gate #include <sys/tnf.h>
840Sstevel@tonic-gate #include <sys/tnf_probe.h>
850Sstevel@tonic-gate #include <sys/cred.h>
860Sstevel@tonic-gate #include <sys/mman.h>
870Sstevel@tonic-gate #include <sys/x86_archext.h>
880Sstevel@tonic-gate #include <sys/copyops.h>
890Sstevel@tonic-gate #include <c2/audit.h>
900Sstevel@tonic-gate #include <sys/ftrace.h>
910Sstevel@tonic-gate #include <sys/panic.h>
920Sstevel@tonic-gate #include <sys/traptrace.h>
930Sstevel@tonic-gate #include <sys/ontrap.h>
940Sstevel@tonic-gate #include <sys/cpc_impl.h>
953446Smrj #include <sys/bootconf.h>
963446Smrj #include <sys/bootinfo.h>
973446Smrj #include <sys/promif.h>
983446Smrj #include <sys/mach_mmu.h>
995084Sjohnlev #if defined(__xpv)
1005084Sjohnlev #include <sys/hypervisor.h>
1015084Sjohnlev #endif
1025254Sgavinm #include <sys/contract/process_impl.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1070Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1080Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1090Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1100Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1110Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static const char *trap_type[] = {
1150Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
1160Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1170Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1180Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
1190Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
1200Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
1210Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
1220Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
1230Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
1240Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
1250Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
1260Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
1270Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
1280Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
1290Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
1300Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
1310Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
1320Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
1330Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1340Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1350Sstevel@tonic-gate };
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1380Sstevel@tonic-gate 
1393939Ssethg #define	SLOW_SCALL_SIZE	2
1403939Ssethg #define	FAST_SCALL_SIZE	2
1413939Ssethg 
1420Sstevel@tonic-gate int tudebug = 0;
1430Sstevel@tonic-gate int tudebugbpt = 0;
1440Sstevel@tonic-gate int tudebugfpe = 0;
1450Sstevel@tonic-gate int tudebugsse = 0;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1480Sstevel@tonic-gate int tdebug = 0;
1490Sstevel@tonic-gate int lodebug = 0;
1500Sstevel@tonic-gate int faultdebug = 0;
1510Sstevel@tonic-gate #else
1520Sstevel@tonic-gate #define	tdebug	0
1530Sstevel@tonic-gate #define	lodebug	0
1540Sstevel@tonic-gate #define	faultdebug	0
1550Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate #if defined(TRAPTRACE)
158*6336Sbholler /*
159*6336Sbholler  * trap trace record for cpu0 is allocated here.
160*6336Sbholler  * trap trace records for non-boot cpus are allocated in mp_startup_init().
161*6336Sbholler  */
162*6336Sbholler static trap_trace_rec_t trap_tr0[TRAPTR_NENT];
163*6336Sbholler trap_trace_ctl_t trap_trace_ctl[NCPU] = {
164*6336Sbholler 	{
165*6336Sbholler 	    (uintptr_t)trap_tr0,			/* next record */
166*6336Sbholler 	    (uintptr_t)trap_tr0,			/* first record */
167*6336Sbholler 	    (uintptr_t)(trap_tr0 + TRAPTR_NENT),	/* limit */
168*6336Sbholler 	    (uintptr_t)0				/* current */
169*6336Sbholler 	},
170*6336Sbholler };
171*6336Sbholler 
172*6336Sbholler /*
173*6336Sbholler  * default trap buffer size
174*6336Sbholler  */
175*6336Sbholler size_t trap_trace_bufsize = TRAPTR_NENT * sizeof (trap_trace_rec_t);
176*6336Sbholler int trap_trace_freeze = 0;
177*6336Sbholler int trap_trace_off = 0;
178*6336Sbholler 
179*6336Sbholler /*
180*6336Sbholler  * A dummy TRAPTRACE entry to use after death.
181*6336Sbholler  */
182*6336Sbholler trap_trace_rec_t trap_trace_postmort;
183*6336Sbholler 
1840Sstevel@tonic-gate static void dump_ttrace(void);
1850Sstevel@tonic-gate #endif	/* TRAPTRACE */
1860Sstevel@tonic-gate static void dumpregs(struct regs *);
1870Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1880Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*ARGSUSED*/
1910Sstevel@tonic-gate static int
1920Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1930Sstevel@tonic-gate {
1945084Sjohnlev 	struct panic_trap_info ti;
1950Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1980Sstevel@tonic-gate 		trap_name = trap_type[type];
1990Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
2000Sstevel@tonic-gate 	} else {
2010Sstevel@tonic-gate 		trap_name = "trap";
2020Sstevel@tonic-gate 		trap_mnemonic = "-";
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #ifdef TRAPTRACE
2060Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	ti.trap_regs = rp;
2100Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
2110Sstevel@tonic-gate 	ti.trap_addr = addr;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
2160Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
2170Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
2180Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
2190Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
2200Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
2210Sstevel@tonic-gate 		    "a NULL pointer dereference" :
2220Sstevel@tonic-gate 		    "an illegal access to a user address");
2230Sstevel@tonic-gate 	} else
2240Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
2250Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
2260Sstevel@tonic-gate 	return (0);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2310Sstevel@tonic-gate  *
2320Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate static int
2360Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2410Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2420Sstevel@tonic-gate 		return (1);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2490Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2500Sstevel@tonic-gate  * instruction.
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2530Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
2543939Ssethg  * int $T_SYSCALLINT is two bytes: 0xCD 0x91
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static int
2583939Ssethg instr_is_other_syscall(caddr_t pc, int which)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2610Sstevel@tonic-gate 
2623939Ssethg 	ASSERT(which == X86_SEP || which == X86_ASYSC || which == 0xCD);
2630Sstevel@tonic-gate 
2643939Ssethg 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0)
2650Sstevel@tonic-gate 		return (0);
2660Sstevel@tonic-gate 
2673939Ssethg 	switch (which) {
2683939Ssethg 	case X86_SEP:
2693939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x34)
2703939Ssethg 			return (1);
2713939Ssethg 		break;
2723939Ssethg 	case X86_ASYSC:
2733939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x05)
2743939Ssethg 			return (1);
2753939Ssethg 		break;
2763939Ssethg 	case 0xCD:
2773939Ssethg 		if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT)
2783939Ssethg 			return (1);
2793939Ssethg 		break;
2803939Ssethg 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (0);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2853939Ssethg static const char *
2863939Ssethg syscall_insn_string(int syscall_insn)
2873939Ssethg {
2883939Ssethg 	switch (syscall_insn) {
2893939Ssethg 	case X86_SEP:
2903939Ssethg 		return ("sysenter");
2913939Ssethg 	case X86_ASYSC:
2923939Ssethg 		return ("syscall");
2933939Ssethg 	case 0xCD:
2943939Ssethg 		return ("int");
2953939Ssethg 	default:
2963939Ssethg 		return ("Unknown");
2973939Ssethg 	}
2983939Ssethg }
2993939Ssethg 
3003939Ssethg static int
3013939Ssethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn)
3023939Ssethg {
3033939Ssethg 	caddr_t	linearpc;
3043939Ssethg 	int return_code = 0;
3053939Ssethg 
3063939Ssethg 	mutex_enter(&p->p_ldtlock);	/* Must be held across linear_pc() */
3073939Ssethg 
3083939Ssethg 	if (linear_pc(rp, p, &linearpc) == 0) {
3093939Ssethg 
3103939Ssethg 		/*
3113939Ssethg 		 * If another thread beat us here, it already changed
3123939Ssethg 		 * this site to the slower (int) syscall instruction.
3133939Ssethg 		 */
3143939Ssethg 		if (instr_is_other_syscall(linearpc, 0xCD)) {
3153939Ssethg 			return_code = 1;
3163939Ssethg 		} else if (instr_is_other_syscall(linearpc, syscall_insn)) {
3173939Ssethg 
3183939Ssethg 			if (rewrite_syscall(linearpc) == 0) {
3193939Ssethg 				return_code = 1;
3203939Ssethg 			}
3213939Ssethg #ifdef DEBUG
3223939Ssethg 			else
3233939Ssethg 				cmn_err(CE_WARN, "failed to rewrite %s "
3243939Ssethg 				    "instruction in process %d",
3253939Ssethg 				    syscall_insn_string(syscall_insn),
3263939Ssethg 				    p->p_pid);
3273939Ssethg #endif /* DEBUG */
3283939Ssethg 		}
3293939Ssethg 	}
3303939Ssethg 
3313939Ssethg 	mutex_exit(&p->p_ldtlock);	/* Must be held across linear_pc() */
3323939Ssethg 
3333939Ssethg 	return (return_code);
3343939Ssethg }
3353939Ssethg 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
3380Sstevel@tonic-gate  *
3390Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
3400Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
3410Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate #define	LCALLSIZE	7
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate static int
3473939Ssethg instr_is_lcall_syscall(caddr_t pc)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
3520Sstevel@tonic-gate 	    instr[0] == 0x9a &&
3530Sstevel@tonic-gate 	    instr[1] == 0 &&
3540Sstevel@tonic-gate 	    instr[2] == 0 &&
3550Sstevel@tonic-gate 	    instr[3] == 0 &&
3560Sstevel@tonic-gate 	    instr[4] == 0 &&
3570Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
3580Sstevel@tonic-gate 	    instr[6] == 0)
3590Sstevel@tonic-gate 		return (1);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	return (0);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3641363Sfvdl #ifdef __amd64
3651363Sfvdl 
3661363Sfvdl /*
3673446Smrj  * In the first revisions of amd64 CPUs produced by AMD, the LAHF and
3683446Smrj  * SAHF instructions were not implemented in 64-bit mode. Later revisions
3691363Sfvdl  * did implement these instructions. An extension to the cpuid instruction
3701363Sfvdl  * was added to check for the capability of executing these instructions
3713446Smrj  * in 64-bit mode.
3721363Sfvdl  *
3731363Sfvdl  * Intel originally did not implement these instructions in EM64T either,
3741363Sfvdl  * but added them in later revisions.
3751363Sfvdl  *
3761363Sfvdl  * So, there are different chip revisions by both vendors out there that
3771363Sfvdl  * may or may not implement these instructions. The easy solution is to
3781363Sfvdl  * just always emulate these instructions on demand.
3791363Sfvdl  *
3801363Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
3811363Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
3821363Sfvdl  */
3831363Sfvdl 
3841363Sfvdl #define	LSAHFSIZE 1
3851363Sfvdl 
3861363Sfvdl static int
3871363Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
3881363Sfvdl {
3891363Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
3901363Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
3911363Sfvdl 		return (1);
3921363Sfvdl 	return (0);
3931363Sfvdl }
3941363Sfvdl 
3951363Sfvdl /*
3961363Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
3971363Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
3981363Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
3991363Sfvdl  *
4001363Sfvdl  * Note that %ah is bits 8-15 of %rax.
4011363Sfvdl  */
4021363Sfvdl static void
4031363Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
4041363Sfvdl {
4051363Sfvdl 	if (instr == 0x9e) {
4061363Sfvdl 		/* sahf. Copy bits from %ah to flags. */
4071363Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
4081363Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
4091363Sfvdl 	} else {
4101363Sfvdl 		/* lahf. Copy bits from flags to %ah. */
4111363Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
4121363Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
4131363Sfvdl 	}
4141363Sfvdl 	rp->r_pc += LSAHFSIZE;
4151363Sfvdl }
4161363Sfvdl #endif /* __amd64 */
4171363Sfvdl 
4180Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
4240Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
4250Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate #define	PREFETCHSIZE 3
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static int
4310Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
4340Sstevel@tonic-gate 		return (1);
4350Sstevel@tonic-gate 	return (0);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate static int
4390Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
4420Sstevel@tonic-gate 	int	error;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
4470Sstevel@tonic-gate 		return (1);
4480Sstevel@tonic-gate 	return (0);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
4570Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
4583446Smrj  * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1)
4593446Smrj  * however, there are paths that arrive here with PS_IE == 0 so special care
4603446Smrj  * must be taken in those cases.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate void
4630Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
4640Sstevel@tonic-gate {
4653446Smrj 	kthread_t *ct = curthread;
4660Sstevel@tonic-gate 	enum seg_rw rw;
4670Sstevel@tonic-gate 	unsigned type;
4683446Smrj 	proc_t *p = ttoproc(ct);
4693446Smrj 	klwp_t *lwp = ttolwp(ct);
4700Sstevel@tonic-gate 	uintptr_t lofault;
4710Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
4720Sstevel@tonic-gate 	enum fault_type fault_type;
4730Sstevel@tonic-gate 	k_siginfo_t siginfo;
4740Sstevel@tonic-gate 	uint_t fault = 0;
4750Sstevel@tonic-gate 	int mstate;
4760Sstevel@tonic-gate 	int sicode = 0;
4770Sstevel@tonic-gate 	int watchcode;
4780Sstevel@tonic-gate 	int watchpage;
4790Sstevel@tonic-gate 	caddr_t vaddr;
4802712Snn35248 	int singlestep_twiddle;
4810Sstevel@tonic-gate 	size_t sz;
4820Sstevel@tonic-gate 	int ta;
4831363Sfvdl #ifdef __amd64
4841363Sfvdl 	uchar_t instr;
4851363Sfvdl #endif
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	type = rp->r_trapno;
4900Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
4913446Smrj 	ASSERT(ct->t_schedflag & TS_DONT_SWAP);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (type == T_PGFLT) {
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		errcode = rp->r_err;
4960Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
4970Sstevel@tonic-gate 			rw = S_WRITE;
4980Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
4990Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
5000Sstevel@tonic-gate 			rw = S_EXEC;
5010Sstevel@tonic-gate 		else
5020Sstevel@tonic-gate 			rw = S_READ;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate #if defined(__i386)
5050Sstevel@tonic-gate 		/*
5060Sstevel@tonic-gate 		 * Pentium Pro work-around
5070Sstevel@tonic-gate 		 */
5080Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
5090Sstevel@tonic-gate 			uint_t	attr;
5100Sstevel@tonic-gate 			uint_t	priv_violation;
5110Sstevel@tonic-gate 			uint_t	access_violation;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
5140Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
5150Sstevel@tonic-gate 			    == -1) {
5160Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
5170Sstevel@tonic-gate 			} else {
5180Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
5195084Sjohnlev 				    !(attr & PROT_USER);
5200Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
5215084Sjohnlev 				    !(attr & PROT_WRITE);
5220Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
5230Sstevel@tonic-gate 					goto cleanup;
5240Sstevel@tonic-gate 			}
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate #endif /* __i386 */
5270Sstevel@tonic-gate 
5283446Smrj 	} else if (type == T_SGLSTP && lwp != NULL)
5293446Smrj 		lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (tdebug)
5320Sstevel@tonic-gate 		showregs(type, rp, addr);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
5350Sstevel@tonic-gate 		/*
5360Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
5370Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
5380Sstevel@tonic-gate 		 * The current process credential applies to the thread for
5390Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
5400Sstevel@tonic-gate 		 * should already be set up.
5410Sstevel@tonic-gate 		 */
5423446Smrj 		if (ct->t_cred != p->p_cred) {
5433446Smrj 			cred_t *oldcred = ct->t_cred;
5440Sstevel@tonic-gate 			/*
5450Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
5460Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
5470Sstevel@tonic-gate 			 * allocated cred structure.
5480Sstevel@tonic-gate 			 */
5493446Smrj 			ct->t_cred = crgetcred();
5500Sstevel@tonic-gate 			crfree(oldcred);
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 		ASSERT(lwp != NULL);
5530Sstevel@tonic-gate 		type |= USER;
5540Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
5550Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		switch (type) {
5580Sstevel@tonic-gate 		case T_PGFLT + USER:
5590Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
5600Sstevel@tonic-gate 				mstate = LMS_TFAULT;
5610Sstevel@tonic-gate 			else
5620Sstevel@tonic-gate 				mstate = LMS_DFAULT;
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 		default:
5650Sstevel@tonic-gate 			mstate = LMS_TRAP;
5660Sstevel@tonic-gate 			break;
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 		/* Kernel probe */
5690Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
5700Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
5713446Smrj 		mstate = new_mstate(ct, mstate);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	switch (type) {
5770Sstevel@tonic-gate 	case T_PGFLT + USER:
5780Sstevel@tonic-gate 	case T_SGLSTP:
5790Sstevel@tonic-gate 	case T_SGLSTP + USER:
5800Sstevel@tonic-gate 	case T_BPTFLT + USER:
5810Sstevel@tonic-gate 		break;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	default:
5840Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
5850Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
5860Sstevel@tonic-gate 		break;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	switch (type) {
5905849Ssethg 	case T_SIMDFPE:
5915849Ssethg 		/* Make sure we enable interrupts before die()ing */
5925849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
5935849Ssethg 		/*FALLTHROUGH*/
5940Sstevel@tonic-gate 	default:
5950Sstevel@tonic-gate 		if (type & USER) {
5960Sstevel@tonic-gate 			if (tudebug)
5970Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5980Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5990Sstevel@tonic-gate 			    type & ~USER);
6000Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
6010Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
6020Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
6030Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
6040Sstevel@tonic-gate 			fault = FLTILL;
6050Sstevel@tonic-gate 			break;
6060Sstevel@tonic-gate 		} else {
6070Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6080Sstevel@tonic-gate 			/*NOTREACHED*/
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
6120Sstevel@tonic-gate 		/*
6130Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
6145254Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
6155254Sgavinm 		 * via the installed trampoline.
6160Sstevel@tonic-gate 		 */
6173446Smrj 		if ((ct->t_ontrap != NULL) &&
6183446Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
6193446Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
6205254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
6215254Sgavinm 			goto cleanup;
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		/*
6250Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
6260Sstevel@tonic-gate 		 * across this. Here we assume that an address
6270Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
6280Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
6290Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
6300Sstevel@tonic-gate 		 * starting and because we know that we always have
6310Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
6320Sstevel@tonic-gate 		 */
6333446Smrj 		lofault = ct->t_lofault;
6343446Smrj 		ct->t_lofault = 0;
6350Sstevel@tonic-gate 
6363446Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6390Sstevel@tonic-gate 			res = pagefault(addr,
6400Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6410Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6420Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6430Sstevel@tonic-gate 			    grow(addr))
6440Sstevel@tonic-gate 				res = 0;
6450Sstevel@tonic-gate 		} else {
6460Sstevel@tonic-gate 			res = pagefault(addr,
6470Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6480Sstevel@tonic-gate 		}
6493446Smrj 		(void) new_mstate(ct, mstate);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
6530Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6540Sstevel@tonic-gate 		 */
6553446Smrj 		ct->t_lofault = lofault;
6560Sstevel@tonic-gate 		if (res == 0)
6570Sstevel@tonic-gate 			goto cleanup;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6600Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6610Sstevel@tonic-gate 			/*
6620Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6630Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6640Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6650Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6660Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6670Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6680Sstevel@tonic-gate 			 */
6690Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6720Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6730Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6740Sstevel@tonic-gate 				    PFN_INVALID &&
6750Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6760Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6770Sstevel@tonic-gate 					rp->r_pc = rip;
6780Sstevel@tonic-gate 					goto cleanup;
6790Sstevel@tonic-gate 				}
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6850Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6860Sstevel@tonic-gate 			/*
6870Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6880Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6890Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6900Sstevel@tonic-gate 			 * instruction.
6910Sstevel@tonic-gate 			 */
6920Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			/*
6950Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6960Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6970Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6980Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6990Sstevel@tonic-gate 			 */
7000Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
7010Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
7020Sstevel@tonic-gate #ifdef DEBUG
7030Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
7040Sstevel@tonic-gate 					    "occurred: kernel prefetch"
7050Sstevel@tonic-gate 					    " at %p generated a page fault!",
7060Sstevel@tonic-gate 					    (void *)rp->r_pc);
7070Sstevel@tonic-gate #endif /* DEBUG */
7080Sstevel@tonic-gate 					goto cleanup;
7090Sstevel@tonic-gate 				}
7100Sstevel@tonic-gate 			}
7110Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		if (lofault == 0)
7160Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
7200Sstevel@tonic-gate 		 */
7210Sstevel@tonic-gate 		if (lodebug) {
7220Sstevel@tonic-gate 			showregs(type, rp, addr);
7230Sstevel@tonic-gate 			traceregs(rp);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
7260Sstevel@tonic-gate 			res = FC_ERRNO(res);
7270Sstevel@tonic-gate 		else
7280Sstevel@tonic-gate 			res = EFAULT;
7290Sstevel@tonic-gate 		rp->r_r0 = res;
7303446Smrj 		rp->r_pc = ct->t_lofault;
7310Sstevel@tonic-gate 		goto cleanup;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7340Sstevel@tonic-gate 		if (faultdebug) {
7350Sstevel@tonic-gate 			char *fault_str;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 			switch (rw) {
7380Sstevel@tonic-gate 			case S_READ:
7390Sstevel@tonic-gate 				fault_str = "read";
7400Sstevel@tonic-gate 				break;
7410Sstevel@tonic-gate 			case S_WRITE:
7420Sstevel@tonic-gate 				fault_str = "write";
7430Sstevel@tonic-gate 				break;
7440Sstevel@tonic-gate 			case S_EXEC:
7450Sstevel@tonic-gate 				fault_str = "exec";
7460Sstevel@tonic-gate 				break;
7470Sstevel@tonic-gate 			default:
7480Sstevel@tonic-gate 				fault_str = "";
7490Sstevel@tonic-gate 				break;
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7520Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7530Sstevel@tonic-gate 		}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7560Sstevel@tonic-gate 		/*
7570Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7580Sstevel@tonic-gate 		 *
7590Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7600Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7610Sstevel@tonic-gate 		 * address will be
7620Sstevel@tonic-gate 		 *
7630Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7640Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7650Sstevel@tonic-gate 		 *
7660Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7670Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7680Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7710Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7720Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7730Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7740Sstevel@tonic-gate 			if (!opteron_erratum_100)
7750Sstevel@tonic-gate 				panic("unexpected erratum #100");
7760Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7770Sstevel@tonic-gate 				goto out;
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7820Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7830Sstevel@tonic-gate #ifdef __i386
7840Sstevel@tonic-gate 		/*
7850Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
7860Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
7870Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
7880Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
7890Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
7900Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
7910Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
7920Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
7930Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
7940Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
7950Sstevel@tonic-gate 		 */
7960Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
7973939Ssethg 		    rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
7980Sstevel@tonic-gate 			extern void watch_syscall(void);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
8010Sstevel@tonic-gate 			watch_syscall();	/* never returns */
8020Sstevel@tonic-gate 			/* NOTREACHED */
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate #endif /* __i386 */
8050Sstevel@tonic-gate 		vaddr = addr;
8060Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
8070Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
8080Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
8090Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
8100Sstevel@tonic-gate 			if (ta) {
8110Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
8125084Sjohnlev 				    watchcode, rp->r_pc);
8130Sstevel@tonic-gate 				fault_type = F_INVAL;
8140Sstevel@tonic-gate 			} else {
8150Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8160Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
8170Sstevel@tonic-gate 				siginfo.si_code = watchcode;
8180Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
8190Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
8200Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
8210Sstevel@tonic-gate 				fault = FLTWATCH;
8220Sstevel@tonic-gate 				break;
8230Sstevel@tonic-gate 			}
8240Sstevel@tonic-gate 		} else {
8250Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
8260Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
8270Sstevel@tonic-gate 				goto out;
8280Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
8290Sstevel@tonic-gate 			fault_type = F_INVAL;
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		/*
8350Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8360Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8370Sstevel@tonic-gate 		 */
8380Sstevel@tonic-gate 		if (res == 0 ||
8390Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8400Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8410Sstevel@tonic-gate 		    grow(addr))) {
8420Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8430Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8440Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8450Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8460Sstevel@tonic-gate 				siginfo.si_addr = addr;
8470Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8480Sstevel@tonic-gate 			}
8490Sstevel@tonic-gate 			goto out;
8500Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8510Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8520Sstevel@tonic-gate 			report_stack_exec(p, addr);
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8560Sstevel@tonic-gate 		/*
8570Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8580Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8590Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8600Sstevel@tonic-gate 		 *
8610Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8620Sstevel@tonic-gate 		 */
8630Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8640Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8650Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8660Sstevel@tonic-gate #ifdef DEBUG
8670Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8680Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8690Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8700Sstevel@tonic-gate #endif /* DEBUG */
8710Sstevel@tonic-gate 			goto out;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		if (tudebug)
8760Sstevel@tonic-gate 			showregs(type, rp, addr);
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8790Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8800Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8810Sstevel@tonic-gate 		 */
8820Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8830Sstevel@tonic-gate 		siginfo.si_addr = addr;
8840Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8850Sstevel@tonic-gate 		case FC_HWERR:
8860Sstevel@tonic-gate 		case FC_NOSUPPORT:
8870Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8880Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8890Sstevel@tonic-gate 			fault = FLTACCESS;
8900Sstevel@tonic-gate 			break;
8910Sstevel@tonic-gate 		case FC_ALIGN:
8920Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8930Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8940Sstevel@tonic-gate 			fault = FLTACCESS;
8950Sstevel@tonic-gate 			break;
8960Sstevel@tonic-gate 		case FC_OBJERR:
8970Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8980Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8990Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
9000Sstevel@tonic-gate 				fault = FLTACCESS;
9010Sstevel@tonic-gate 			}
9020Sstevel@tonic-gate 			break;
9030Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
9040Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9050Sstevel@tonic-gate 			siginfo.si_code =
9060Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
9070Sstevel@tonic-gate 			fault = FLTBOUNDS;
9080Sstevel@tonic-gate 			break;
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 		break;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
9130Sstevel@tonic-gate 		/*
9140Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
9150Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
9160Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
9170Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
9180Sstevel@tonic-gate 		 * be to emulate that particular instruction.
9190Sstevel@tonic-gate 		 */
9200Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
9213939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_ASYSC))
9223939Ssethg 			goto out;
9231363Sfvdl 
9241363Sfvdl #ifdef __amd64
9251363Sfvdl 		/*
9261363Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
9271363Sfvdl 		 * See the instr_is_lsahf function for details.
9281363Sfvdl 		 */
9291363Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
9301363Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
9311363Sfvdl 			emulate_lsahf(rp, instr);
9321363Sfvdl 			goto out;
9331363Sfvdl 		}
9341363Sfvdl #endif
9351363Sfvdl 
9360Sstevel@tonic-gate 		/*FALLTHROUGH*/
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		if (tudebug)
9390Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9400Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9410Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9420Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9430Sstevel@tonic-gate 		fault = FLTILL;
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9470Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9480Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9490Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9500Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9510Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9520Sstevel@tonic-gate 		fault = FLTIZDIV;
9530Sstevel@tonic-gate 		break;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9560Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9570Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9580Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9590Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9600Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9610Sstevel@tonic-gate 		fault = FLTIOVF;
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9650Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9660Sstevel@tonic-gate 			showregs(type, rp, addr);
9670Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
9680Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9690Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
9700Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9710Sstevel@tonic-gate 			fault = FLTFPE;
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 		break;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
9760Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9770Sstevel@tonic-gate 		{
9780Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9795849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
9805849Ssethg 				sti(); /* T_EXTOVRFLT comes in via cmninttrap */
9810Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9825849Ssethg 			}
9830Sstevel@tonic-gate 			type |= USER;
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 		/*FALLTHROUGH*/
9860Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
9870Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9880Sstevel@tonic-gate 			showregs(type, rp, addr);
9890Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9900Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9910Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9920Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9930Sstevel@tonic-gate 			fault = FLTBOUNDS;
9940Sstevel@tonic-gate 		}
9950Sstevel@tonic-gate 		break;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9980Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9990Sstevel@tonic-gate 		{
10000Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
10015849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
10025849Ssethg 				sti(); /* T_EXTERRFLT comes in via cmninttrap */
10030Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
10045849Ssethg 			}
10050Sstevel@tonic-gate 			type |= USER;
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 		/*FALLTHROUGH*/
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
10100Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
10110Sstevel@tonic-gate 			showregs(type, rp, addr);
10120Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
10130Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10140Sstevel@tonic-gate 			siginfo.si_code  = sicode;
10150Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
10160Sstevel@tonic-gate 			fault = FLTFPE;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		break;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
10210Sstevel@tonic-gate 		if (tudebug && tudebugsse)
10220Sstevel@tonic-gate 			showregs(type, rp, addr);
10230Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
10240Sstevel@tonic-gate 			/*
10250Sstevel@tonic-gate 			 * There are rumours that some user instructions
10260Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
10270Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
10280Sstevel@tonic-gate 			 */
10290Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
10300Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
10310Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
10320Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
10330Sstevel@tonic-gate 			fault = FLTILL;
10340Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
10350Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10360Sstevel@tonic-gate 			siginfo.si_code = sicode;
10370Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
10380Sstevel@tonic-gate 			fault = FLTFPE;
10390Sstevel@tonic-gate 		}
10405849Ssethg 
10415849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
10420Sstevel@tonic-gate 		break;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10450Sstevel@tonic-gate 		/*
10460Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10470Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10480Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10490Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10500Sstevel@tonic-gate 		 * fatal.
10510Sstevel@tonic-gate 		 */
10520Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10530Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10560Sstevel@tonic-gate 		break;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		/* Now evaluate how we got here */
10610Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
10640Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
10650Sstevel@tonic-gate 			 * the first instruction in privileged mode.
10660Sstevel@tonic-gate 			 *
10670Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
10680Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
10690Sstevel@tonic-gate 			 *
10700Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
10710Sstevel@tonic-gate 			 */
10722712Snn35248 			singlestep_twiddle = 0;
10732712Snn35248 			if (rp->r_pc == (uintptr_t)sys_sysenter ||
10742712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_sysenter) {
10752712Snn35248 				singlestep_twiddle = 1;
10760Sstevel@tonic-gate #if defined(__amd64)
10770Sstevel@tonic-gate 				/*
10782712Snn35248 				 * Since we are already on the kernel's
10792712Snn35248 				 * %gs, on 64-bit systems the sysenter case
10802712Snn35248 				 * needs to adjust the pc to avoid
10812712Snn35248 				 * executing the swapgs instruction at the
10822712Snn35248 				 * top of the handler.
10830Sstevel@tonic-gate 				 */
10842712Snn35248 				if (rp->r_pc == (uintptr_t)sys_sysenter)
10852712Snn35248 					rp->r_pc = (uintptr_t)
10862712Snn35248 					    _sys_sysenter_post_swapgs;
10872712Snn35248 				else
10882712Snn35248 					rp->r_pc = (uintptr_t)
10892712Snn35248 					    _brand_sys_sysenter_post_swapgs;
10900Sstevel@tonic-gate #endif
10912712Snn35248 			}
10922712Snn35248 #if defined(__i386)
10932712Snn35248 			else if (rp->r_pc == (uintptr_t)sys_call ||
10942712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_call) {
10952712Snn35248 				singlestep_twiddle = 1;
10962712Snn35248 			}
10972712Snn35248 #endif
10983003Sdmick 			else {
10993003Sdmick 				/* not on sysenter/syscall; uregs available */
11003003Sdmick 				if (tudebug && tudebugbpt)
11013003Sdmick 					showregs(type, rp, (caddr_t)0);
11023003Sdmick 			}
11032712Snn35248 			if (singlestep_twiddle) {
11040Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
11050Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
11063446Smrj 				ct->t_post_sys = 1;
11072086Ssudheer 				aston(curthread);
11080Sstevel@tonic-gate 				goto cleanup;
11090Sstevel@tonic-gate 			}
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
11120Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
11130Sstevel@tonic-gate 			debug_enter((char *)NULL);
11140Sstevel@tonic-gate 		else
11150Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11160Sstevel@tonic-gate 		break;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
11190Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
11200Sstevel@tonic-gate 		goto cleanup;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
11230Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
11240Sstevel@tonic-gate 		break;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
11270Sstevel@tonic-gate 		/*
11285639Sgavinm 		 * Any #GP that occurs during an on_trap .. no_trap bracket
11295639Sgavinm 		 * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection,
11305639Sgavinm 		 * or in a on_fault .. no_fault bracket, is forgiven
11315639Sgavinm 		 * and we trampoline.  This protection is given regardless
11325639Sgavinm 		 * of whether we are 32/64 bit etc - if a distinction is
11335639Sgavinm 		 * required then define new on_trap protection types.
11345639Sgavinm 		 *
11350Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
11365084Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
11375084Sjohnlev 		 * update_sregs while updating user segment registers.
11385084Sjohnlev 		 *
11395084Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
11405084Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
11415084Sjohnlev 		 * lives which is protected by segmentation.
11420Sstevel@tonic-gate 		 */
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 		/*
11450Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
11465254Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
11475084Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
11480Sstevel@tonic-gate 		 */
11493446Smrj 		if (ct->t_ontrap != NULL) {
11505084Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
11515084Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
11523446Smrj 
11535084Sjohnlev 			if (ttype != 0) {
11545084Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
11555084Sjohnlev 				if (tudebug)
11565084Sjohnlev 					showregs(type, rp, (caddr_t)0);
11575254Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
11585254Sgavinm 				goto cleanup;
11595084Sjohnlev 			}
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 		/*
11630Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
11640Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
11650Sstevel@tonic-gate 		 */
11663446Smrj 		if (ct->t_lofault) {
11670Sstevel@tonic-gate 			/*
11680Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11690Sstevel@tonic-gate 			 */
11700Sstevel@tonic-gate 			if (lodebug) {
11710Sstevel@tonic-gate 				showregs(type, rp, addr);
11720Sstevel@tonic-gate 				traceregs(rp);
11730Sstevel@tonic-gate 			}
11740Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
11753446Smrj 			rp->r_pc = ct->t_lofault;
11760Sstevel@tonic-gate 			goto cleanup;
11770Sstevel@tonic-gate 		}
11785639Sgavinm 
11795639Sgavinm 		/*
11805639Sgavinm 		 * We fall through to the next case, which repeats
11815639Sgavinm 		 * the OT_SEGMENT_ACCESS check which we've already
11825639Sgavinm 		 * done, so we'll always fall through to the
11835639Sgavinm 		 * T_STKFLT case.
11845639Sgavinm 		 */
11850Sstevel@tonic-gate 		/*FALLTHROUGH*/
11863446Smrj 	case T_SEGFLT:	/* segment not present fault */
11873446Smrj 		/*
11883446Smrj 		 * One example of this is #NP in update_sregs while
11893446Smrj 		 * attempting to update a user segment register
11903446Smrj 		 * that points to a descriptor that is marked not
11913446Smrj 		 * present.
11923446Smrj 		 */
11933446Smrj 		if (ct->t_ontrap != NULL &&
11943446Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
11953446Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
11965084Sjohnlev 			if (tudebug)
11975084Sjohnlev 				showregs(type, rp, (caddr_t)0);
11985254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
11995254Sgavinm 			goto cleanup;
12003446Smrj 		}
12013446Smrj 		/*FALLTHROUGH*/
12020Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
12030Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
12040Sstevel@tonic-gate 		if (tudebug)
12050Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12060Sstevel@tonic-gate 		if (kern_gpfault(rp))
12070Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
12080Sstevel@tonic-gate 		goto cleanup;
12090Sstevel@tonic-gate 
12103446Smrj 	/*
12113446Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
12123446Smrj 	 * should have no need for them, so we put a stop to it here.
12133446Smrj 	 *
12143446Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
12153446Smrj 	 * a private LDT trying to do a system call. Emulate it.
12163446Smrj 	 *
12173446Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
12183446Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
12193446Smrj 	 */
12203446Smrj 
12210Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
12221217Srab 	case T_GPFLT + USER:	/* general protection violation */
12230Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12241217Srab 		if (p->p_model != DATAMODEL_NATIVE) {
12251217Srab #endif /* _SYSCALL32_IMPL */
12263939Ssethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
12271217Srab 			if (type == T_SEGFLT + USER)
12281217Srab 				ASSERT(p->p_ldt != NULL);
12291217Srab 
12301217Srab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
12311217Srab 			    type == T_SEGFLT + USER) {
12321217Srab 
12330Sstevel@tonic-gate 			/*
12341217Srab 			 * The user attempted a system call via the obsolete
12351217Srab 			 * call gate mechanism. Because the process doesn't have
12361217Srab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
12371217Srab 			 * Emulate the syscall here, just as we do above for a
12381217Srab 			 * #np trap.
12391217Srab 			 */
12401217Srab 
12411217Srab 			/*
12420Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
12430Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
12440Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
12450Sstevel@tonic-gate 			 */
12460Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
12470Sstevel@tonic-gate 			lwp->lwp_regs = rp;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 			/*
12500Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
12510Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
12520Sstevel@tonic-gate 			 * behavior here.
12530Sstevel@tonic-gate 			 */
12540Sstevel@tonic-gate 			mstate = LMS_USER;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			dosyscall();
12570Sstevel@tonic-gate 			goto out;
12581217Srab 			}
12591217Srab 		}
12601217Srab #ifdef _SYSCALL32_IMPL
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
12630Sstevel@tonic-gate 		/*
12640Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
12650Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
12660Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
12670Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12680Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12690Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12700Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12710Sstevel@tonic-gate 		 */
12720Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
12733939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_SEP))
12743939Ssethg 			goto out;
12753939Ssethg 
12760Sstevel@tonic-gate 		/*FALLTHROUGH*/
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12790Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12800Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12810Sstevel@tonic-gate 		if (tudebug)
12820Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12830Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12840Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12850Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12860Sstevel@tonic-gate 		fault = FLTBOUNDS;
12870Sstevel@tonic-gate 		break;
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12900Sstevel@tonic-gate 		if (tudebug)
12910Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12920Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12930Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12940Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12950Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12960Sstevel@tonic-gate 		fault = FLTACCESS;
12970Sstevel@tonic-gate 		break;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
13000Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
13010Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		/* Was it single-stepping? */
13040Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
13050Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
13080Sstevel@tonic-gate 			/*
13090Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
13102712Snn35248 			 * give precedence to WATCH_STEP.  If neither is set,
13110Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
13120Sstevel@tonic-gate 			 * as NORMAL_STEP.
13130Sstevel@tonic-gate 			 */
13142712Snn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
13152712Snn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
13162712Snn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
13170Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
13180Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
13190Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13200Sstevel@tonic-gate 				fault = FLTTRACE;
13210Sstevel@tonic-gate 			}
13220Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13230Sstevel@tonic-gate 		} else {
13240Sstevel@tonic-gate 			cmn_err(CE_WARN,
13250Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
13260Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 		break;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
13310Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
13320Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
13330Sstevel@tonic-gate 		/*
13340Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
13350Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
13360Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
13370Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
13380Sstevel@tonic-gate 		 */
13390Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
13400Sstevel@tonic-gate 			rp->r_pc--;
13410Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
13420Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
13430Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
13440Sstevel@tonic-gate 		fault = FLTBPT;
13450Sstevel@tonic-gate 		break;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	case T_AST:
13480Sstevel@tonic-gate 		/*
13490Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
13500Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
13510Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
13520Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
13530Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
13540Sstevel@tonic-gate 		 * doing the T_AST processing.
13550Sstevel@tonic-gate 		 */
13560Sstevel@tonic-gate 		goto cleanup;
13570Sstevel@tonic-gate 
13585254Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
13595254Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
13605254Sgavinm 			proc_t *p = ttoproc(curthread);
13615254Sgavinm 
13625254Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
13635254Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
13645254Sgavinm 			siginfo.si_signo = SIGKILL;
13655254Sgavinm 			siginfo.si_code = SI_NOINFO;
13665254Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
13670Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
13680Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13690Sstevel@tonic-gate 				/*
13700Sstevel@tonic-gate 				 * Signal performance counter overflow
13710Sstevel@tonic-gate 				 */
13720Sstevel@tonic-gate 				if (tudebug)
13730Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13740Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13750Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13760Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13770Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13780Sstevel@tonic-gate 				fault = FLTCPCOVF;
13790Sstevel@tonic-gate 			}
13800Sstevel@tonic-gate 		}
13815254Sgavinm 
13820Sstevel@tonic-gate 		break;
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * We can't get here from a system trap
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	ASSERT(type & USER);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (fault) {
13913506Saf 		/* We took a fault so abort single step. */
13923506Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13930Sstevel@tonic-gate 		/*
13940Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13950Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13960Sstevel@tonic-gate 		 */
13970Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13980Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		/*
14030Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
14040Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
14050Sstevel@tonic-gate 		 * deliver the associated signal.
14060Sstevel@tonic-gate 		 */
14070Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
14080Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
14090Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
14100Sstevel@tonic-gate 			siginfo.si_signo = 0;
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (siginfo.si_signo)
14140Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
14170Sstevel@tonic-gate 		profil_tick(rp->r_pc);
14180Sstevel@tonic-gate 
14193446Smrj 	if (ct->t_astflag | ct->t_sig_check) {
14200Sstevel@tonic-gate 		/*
14210Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
14220Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
14230Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
14240Sstevel@tonic-gate 		 * syscall.
14250Sstevel@tonic-gate 		 */
14263446Smrj 		astoff(ct);
14272086Ssudheer 		/*
14282086Ssudheer 		 * If a single-step trap occurred on a syscall (see above)
14292086Ssudheer 		 * recognize it now.  Do this before checking for signals
14302086Ssudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
14312086Ssudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
14322086Ssudheer 		 */
14332086Ssudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
14342086Ssudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
14352086Ssudheer 
14363446Smrj 		ct->t_sig_check = 0;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
14390Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
14400Sstevel@tonic-gate 			timer_lwpbind();
14410Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 		/*
14460Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
14470Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
14480Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
14490Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
14500Sstevel@tonic-gate 		 * for the kaio request to complete.
14510Sstevel@tonic-gate 		 */
14520Sstevel@tonic-gate 		if (p->p_aio)
14530Sstevel@tonic-gate 			aio_cleanup(0);
14540Sstevel@tonic-gate 		/*
14550Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
14560Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
14570Sstevel@tonic-gate 		 * sets the AST flag.
14580Sstevel@tonic-gate 		 *
14590Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14600Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14610Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14620Sstevel@tonic-gate 		 */
14630Sstevel@tonic-gate 		if (ISHOLD(p))
14640Sstevel@tonic-gate 			holdlwp();
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 		/*
14670Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14680Sstevel@tonic-gate 		 * set t_astflag afterwards.
14690Sstevel@tonic-gate 		 */
14703446Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14710Sstevel@tonic-gate 			if (issig(FORREAL))
14720Sstevel@tonic-gate 				psig();
14733446Smrj 			ct->t_sig_check = 1;
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 
14763446Smrj 		if (ct->t_rprof != NULL) {
14770Sstevel@tonic-gate 			realsigprof(0, 0);
14783446Smrj 			ct->t_sig_check = 1;
14790Sstevel@tonic-gate 		}
14801468Smarx 
14811468Smarx 		/*
14821468Smarx 		 * /proc can't enable/disable the trace bit itself
14831468Smarx 		 * because that could race with the call gate used by
14841468Smarx 		 * system calls via "lcall". If that happened, an
14851468Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
14861468Smarx 		 * therefore schedule an AST for the purpose.
14871468Smarx 		 */
14881468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
14891468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
14901468Smarx 			rp->r_ps |= PS_T;
14911468Smarx 		}
14921468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
14931468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
14941468Smarx 			rp->r_ps &= ~PS_T;
14951468Smarx 		}
14960Sstevel@tonic-gate 	}
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14990Sstevel@tonic-gate 	ASSERT(type & USER);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	if (ISHOLD(p))
15020Sstevel@tonic-gate 		holdlwp();
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
15060Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
15070Sstevel@tonic-gate 	 * restore the user-level priority.
15080Sstevel@tonic-gate 	 *
15090Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
15100Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
15110Sstevel@tonic-gate 	 * is set back to LWP_SYS).
15120Sstevel@tonic-gate 	 */
15130Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
15140Sstevel@tonic-gate 
15153446Smrj 	if (ct->t_trapret) {
15163446Smrj 		ct->t_trapret = 0;
15173446Smrj 		thread_lock(ct);
15183446Smrj 		CL_TRAPRET(ct);
15193446Smrj 		thread_unlock(ct);
15200Sstevel@tonic-gate 	}
15213792Sakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
15220Sstevel@tonic-gate 		preempt();
15233446Smrj 	(void) new_mstate(ct, mstate);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	/* Kernel probe */
15260Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
15270Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	return;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate cleanup:	/* system traps end up here */
15320Sstevel@tonic-gate 	ASSERT(!(type & USER));
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate /*
15360Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
15370Sstevel@tonic-gate  */
15380Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
15410Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
15420Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
15430Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
15440Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
15450Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
15460Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
15470Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
15480Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
15490Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
15500Sstevel@tonic-gate } kpreempt_cnts;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
15540Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
15550Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
15560Sstevel@tonic-gate  */
15570Sstevel@tonic-gate void
15580Sstevel@tonic-gate kpreempt(int asyncspl)
15590Sstevel@tonic-gate {
15603446Smrj 	kthread_t *ct = curthread;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
15630Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
15640Sstevel@tonic-gate 		return;
15650Sstevel@tonic-gate 	}
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	/*
15680Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
15690Sstevel@tonic-gate 	 */
15700Sstevel@tonic-gate 	do {
15713446Smrj 		if (ct->t_preempt) {
15720Sstevel@tonic-gate 			/*
15730Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
15740Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
15750Sstevel@tonic-gate 			 */
15763446Smrj 			if (ct->t_pri < 0)
15770Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
15783446Smrj 			else if (ct->t_flag & T_INTR_THREAD) {
15790Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
15803446Smrj 				if (ct->t_pil == CLOCK_LEVEL)
15810Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
15820Sstevel@tonic-gate 			} else
15830Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
15840Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
15850Sstevel@tonic-gate 			return;
15860Sstevel@tonic-gate 		}
15873446Smrj 		if (ct->t_state != TS_ONPROC ||
15883446Smrj 		    ct->t_disp_queue != CPU->cpu_disp) {
15890Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
15900Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
15910Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
15920Sstevel@tonic-gate 				/* already in swtch(), force another */
15930Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
15940Sstevel@tonic-gate 				siron();
15950Sstevel@tonic-gate 			}
15960Sstevel@tonic-gate 			return;
15970Sstevel@tonic-gate 		}
15980Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
15990Sstevel@tonic-gate 			/*
16000Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
16010Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
16020Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
16030Sstevel@tonic-gate 			 */
16040Sstevel@tonic-gate 			siron();	/* check back later */
16050Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
16060Sstevel@tonic-gate 			return;
16070Sstevel@tonic-gate 		}
16083446Smrj 		if (!interrupts_enabled()) {
16093446Smrj 			/*
16103446Smrj 			 * Can't preempt while running with ints disabled
16113446Smrj 			 */
16123446Smrj 			kpreempt_cnts.kpc_prilevel++;
16133446Smrj 			return;
16143446Smrj 		}
16150Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
16160Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
16170Sstevel@tonic-gate 		else
16180Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
16190Sstevel@tonic-gate 
16203446Smrj 		ct->t_preempt++;
16210Sstevel@tonic-gate 		preempt();
16223446Smrj 		ct->t_preempt--;
16230Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate  * Print out debugging info.
16280Sstevel@tonic-gate  */
16290Sstevel@tonic-gate static void
16300Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	int s;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	s = spl7();
16350Sstevel@tonic-gate 	type &= ~USER;
16363446Smrj 	if (PTOU(curproc)->u_comm[0])
16373446Smrj 		printf("%s: ", PTOU(curproc)->u_comm);
16380Sstevel@tonic-gate 	if (type < TRAP_TYPES)
16390Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
16400Sstevel@tonic-gate 	else
16410Sstevel@tonic-gate 		switch (type) {
16420Sstevel@tonic-gate 		case T_SYSCALL:
16430Sstevel@tonic-gate 			printf("Syscall Trap:\n");
16440Sstevel@tonic-gate 			break;
16450Sstevel@tonic-gate 		case T_AST:
16460Sstevel@tonic-gate 			printf("AST\n");
16470Sstevel@tonic-gate 			break;
16480Sstevel@tonic-gate 		default:
16490Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
16500Sstevel@tonic-gate 			break;
16510Sstevel@tonic-gate 		}
16520Sstevel@tonic-gate 	if (type == T_PGFLT) {
16530Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
16540Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
16550Sstevel@tonic-gate 	} else if (addr) {
16560Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
16600Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
16610Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate #if defined(__lint)
16640Sstevel@tonic-gate 	/*
16650Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
16660Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
16670Sstevel@tonic-gate 	 */
16680Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
16690Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
16700Sstevel@tonic-gate #else
16710Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
16720Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
16733446Smrj #endif	/* __lint */
16740Sstevel@tonic-gate 
16755084Sjohnlev 	printf("cr2: %lx", getcr2());
16765084Sjohnlev #if !defined(__xpv)
16775084Sjohnlev 	printf("cr3: %lx", getcr3());
16780Sstevel@tonic-gate #if defined(__amd64)
16795084Sjohnlev 	printf("cr8: %lx\n", getcr8());
16800Sstevel@tonic-gate #endif
16815084Sjohnlev #endif
16825084Sjohnlev 	printf("\n");
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	dumpregs(rp);
16850Sstevel@tonic-gate 	splx(s);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate static void
16890Sstevel@tonic-gate dumpregs(struct regs *rp)
16900Sstevel@tonic-gate {
16910Sstevel@tonic-gate #if defined(__amd64)
16920Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
16950Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
16960Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
16970Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
16980Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
16990Sstevel@tonic-gate 
17003446Smrj 	printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE),
17013446Smrj 	    " ds", rp->r_ds);
17020Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
17050Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate #elif defined(__i386)
17100Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
17130Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
17140Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
17150Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
17160Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
17170Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
17180Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
17190Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
17200Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
17210Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate #endif	/* __i386 */
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate /*
17275084Sjohnlev  * Test to see if the instruction is iret on i386 or iretq on amd64.
17285084Sjohnlev  *
17295084Sjohnlev  * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true
17305084Sjohnlev  * then we are in the context of hypervisor's failsafe handler because it
17315084Sjohnlev  * tried to iret and failed due to a bad selector. See xen_failsafe_callback.
17325084Sjohnlev  */
17335084Sjohnlev static int
17345084Sjohnlev instr_is_iret(caddr_t pc)
17355084Sjohnlev {
17365084Sjohnlev 
17375084Sjohnlev #if defined(__xpv)
17385084Sjohnlev 	extern void nopop_sys_rtt_syscall(void);
17395084Sjohnlev 	return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0);
17405084Sjohnlev 
17415084Sjohnlev #else
17425084Sjohnlev 
17435084Sjohnlev #if defined(__amd64)
17445084Sjohnlev 	static const uint8_t iret_insn[2] = { 0x48, 0xcf };	/* iretq */
17455084Sjohnlev 
17465084Sjohnlev #elif defined(__i386)
17475084Sjohnlev 	static const uint8_t iret_insn[1] = { 0xcf };		/* iret */
17485084Sjohnlev #endif	/* __i386 */
17495084Sjohnlev 	return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0);
17505084Sjohnlev 
17515084Sjohnlev #endif	/* __xpv */
17525084Sjohnlev }
17535084Sjohnlev 
17545084Sjohnlev #if defined(__i386)
17555084Sjohnlev 
17565084Sjohnlev /*
17575084Sjohnlev  * Test to see if the instruction is part of __SEGREGS_POP
17585084Sjohnlev  *
17595084Sjohnlev  * Note carefully the appallingly awful dependency between
17605084Sjohnlev  * the instruction sequence used in __SEGREGS_POP and these
17615084Sjohnlev  * instructions encoded here.
17625084Sjohnlev  */
17635084Sjohnlev static int
17645084Sjohnlev instr_is_segregs_pop(caddr_t pc)
17655084Sjohnlev {
17665084Sjohnlev 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
17675084Sjohnlev 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
17685084Sjohnlev 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
17695084Sjohnlev 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
17705084Sjohnlev 
17715084Sjohnlev 	if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
17725084Sjohnlev 	    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
17735084Sjohnlev 	    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
17745084Sjohnlev 	    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
17755084Sjohnlev 		return (1);
17765084Sjohnlev 
17775084Sjohnlev 	return (0);
17785084Sjohnlev }
17795084Sjohnlev 
17805084Sjohnlev #endif	/* __i386 */
17815084Sjohnlev 
17825084Sjohnlev /*
17835084Sjohnlev  * Test to see if the instruction is part of _sys_rtt.
17845084Sjohnlev  *
17855084Sjohnlev  * Again on the hypervisor if we try to IRET to user land with a bad code
17865084Sjohnlev  * or stack selector we will get vectored through xen_failsafe_callback.
17875084Sjohnlev  * In which case we assume we got here via _sys_rtt since we only allow
17885084Sjohnlev  * IRET to user land to take place in _sys_rtt.
17895084Sjohnlev  */
17905084Sjohnlev static int
17915084Sjohnlev instr_is_sys_rtt(caddr_t pc)
17925084Sjohnlev {
17935084Sjohnlev 	extern void _sys_rtt(), _sys_rtt_end();
17945084Sjohnlev 
17955084Sjohnlev 	if ((uintptr_t)pc < (uintptr_t)_sys_rtt ||
17965084Sjohnlev 	    (uintptr_t)pc > (uintptr_t)_sys_rtt_end)
17975084Sjohnlev 		return (0);
17985084Sjohnlev 
17995084Sjohnlev 	return (1);
18005084Sjohnlev }
18015084Sjohnlev 
18025084Sjohnlev /*
18030Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
18040Sstevel@tonic-gate  *
18050Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
18060Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
18070Sstevel@tonic-gate  *
18080Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
18090Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
18100Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
18110Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
18120Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
18130Sstevel@tonic-gate  *
18140Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
18150Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
18160Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
18170Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
18180Sstevel@tonic-gate  * what we were going to send the lwp.)
18190Sstevel@tonic-gate  *
18200Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
18210Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
18220Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
18230Sstevel@tonic-gate  */
18240Sstevel@tonic-gate static int
18250Sstevel@tonic-gate kern_gpfault(struct regs *rp)
18260Sstevel@tonic-gate {
18270Sstevel@tonic-gate 	kthread_t *t = curthread;
18280Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
18290Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
18300Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
18310Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
18320Sstevel@tonic-gate 	int v;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	/*
18355084Sjohnlev 	 * if we're not an lwp, or in the case of running native the
18365084Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
18375084Sjohnlev 	 * be die()ing horribly.
18380Sstevel@tonic-gate 	 */
18395084Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
18400Sstevel@tonic-gate 		return (1);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	/*
18430Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
18440Sstevel@tonic-gate 	 *
18450Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
18460Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
18470Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
18480Sstevel@tonic-gate 	 * _sys_rtt. Ew.
18490Sstevel@tonic-gate 	 */
18505084Sjohnlev 	if (instr_is_iret(pc)) {
18510Sstevel@tonic-gate 		/*
18525084Sjohnlev 		 * We took the #gp while trying to perform the IRET.
18530Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
18540Sstevel@tonic-gate 		 * All we know for sure is that most of the general
18550Sstevel@tonic-gate 		 * registers have been restored, including the
18560Sstevel@tonic-gate 		 * segment registers, and all we have left on the
18570Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
18580Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
18590Sstevel@tonic-gate 		 *
18600Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
18610Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
18620Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
18630Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
18640Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
18650Sstevel@tonic-gate 		 */
18660Sstevel@tonic-gate 		trp = &tmpregs;
18670Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
18680Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
18690Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
18700Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
18710Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
18720Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 		/*
18750Sstevel@tonic-gate 		 * Validate simple math
18760Sstevel@tonic-gate 		 */
18770Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18780Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
18790Sstevel@tonic-gate 
18805084Sjohnlev 
18815084Sjohnlev 
18825084Sjohnlev 	}
18835084Sjohnlev 
18845084Sjohnlev #if defined(__amd64)
18855084Sjohnlev 	if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) {
18865084Sjohnlev 
18875084Sjohnlev 		/*
18885084Sjohnlev 		 * This is the common case -- we're trying to load
18895084Sjohnlev 		 * a bad segment register value in the only section
18905084Sjohnlev 		 * of kernel code that ever loads segment registers.
18915084Sjohnlev 		 *
18925084Sjohnlev 		 * We don't need to do anything at this point because
18935084Sjohnlev 		 * the pcb contains all the pending segment register
18945084Sjohnlev 		 * state, and the regs are still intact because we
18955084Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
18965084Sjohnlev 		 * of all this, we could conceivably send a signal
18975084Sjohnlev 		 * to the lwp, rather than core-ing.
18985084Sjohnlev 		 */
18995084Sjohnlev 		trp = lwptoregs(lwp);
19005084Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
19010Sstevel@tonic-gate 	}
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate #elif defined(__i386)
19040Sstevel@tonic-gate 
19055084Sjohnlev 	if (trp == NULL && instr_is_segregs_pop(pc))
19065084Sjohnlev 		trp = lwptoregs(lwp);
19070Sstevel@tonic-gate 
19085084Sjohnlev #endif	/* __i386 */
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	if (trp == NULL)
19110Sstevel@tonic-gate 		return (1);
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/*
19140Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
19150Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
19160Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
19170Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
19180Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
19190Sstevel@tonic-gate 	 */
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/*
19220Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
19230Sstevel@tonic-gate 	 */
19240Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
19250Sstevel@tonic-gate 	trp->r_err = rp->r_err;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
19280Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
19290Sstevel@tonic-gate 
19305084Sjohnlev 
19310Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19320Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
19330Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	/*
19365084Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
19375084Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
19380Sstevel@tonic-gate 	 */
1939390Sraf 	proc_is_exiting(p);
19400Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
19410Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19420Sstevel@tonic-gate 		lwp_exit();
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19460Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
19470Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
19480Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19490Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
19500Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
19510Sstevel@tonic-gate 	return (0);
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate /*
19550Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
19560Sstevel@tonic-gate  */
19570Sstevel@tonic-gate 
19585084Sjohnlev #if !defined(__xpv)
19590Sstevel@tonic-gate #if defined(__amd64)
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate static void
19620Sstevel@tonic-gate dump_tss(void)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19650Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
19680Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
19690Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
19720Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
19730Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
19740Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
19750Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
19760Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
19770Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate #elif defined(__i386)
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate static 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 
1988286Sdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
1989286Sdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
1990286Sdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
1991286Sdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
1992286Sdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
1993286Sdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
1994286Sdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
1995286Sdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
1996286Sdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
1997286Sdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
1998286Sdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
1999286Sdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
2000286Sdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
2001286Sdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
2002286Sdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate #endif	/* __amd64 */
20065084Sjohnlev #endif	/* !__xpv */
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate #if defined(TRAPTRACE)
20090Sstevel@tonic-gate 
20105084Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
20115084Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate /*
20140Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
20150Sstevel@tonic-gate  */
20160Sstevel@tonic-gate static void
20170Sstevel@tonic-gate dump_ttrace(void)
20180Sstevel@tonic-gate {
20190Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
20200Sstevel@tonic-gate 	trap_trace_rec_t *rec;
20210Sstevel@tonic-gate 	uintptr_t current;
20220Sstevel@tonic-gate 	int i, j, k;
20230Sstevel@tonic-gate 	int n = NCPU;
20240Sstevel@tonic-gate #if defined(__amd64)
20250Sstevel@tonic-gate 	const char banner[] =
20265084Sjohnlev 	    "\ncpu          address    timestamp "
20275084Sjohnlev 	    "type  vc  handler   pc\n";
20280Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
20290Sstevel@tonic-gate #elif defined(__i386)
20300Sstevel@tonic-gate 	const char banner[] =
20315084Sjohnlev 	    "\ncpu  address     timestamp type  vc  handler   pc\n";
20320Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
20330Sstevel@tonic-gate #endif
20340Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
20350Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	if (ttrace_nrec == 0)
20380Sstevel@tonic-gate 		return;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	printf(banner);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20430Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
20440Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
20450Sstevel@tonic-gate 			continue;
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
20480Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
20490Sstevel@tonic-gate 			struct sysent	*sys;
20500Sstevel@tonic-gate 			struct autovec	*vec;
20510Sstevel@tonic-gate 			extern struct av_head autovect[];
20520Sstevel@tonic-gate 			int type;
20530Sstevel@tonic-gate 			ulong_t	off;
20540Sstevel@tonic-gate 			char *sym, *stype;
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 			if (current < ttc->ttc_first)
20570Sstevel@tonic-gate 				current =
20580Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 			if (current == NULL)
20610Sstevel@tonic-gate 				continue;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
20660Sstevel@tonic-gate 				break;
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 			switch (rec->ttr_marker) {
20710Sstevel@tonic-gate 			case TT_SYSCALL:
20720Sstevel@tonic-gate 			case TT_SYSENTER:
20730Sstevel@tonic-gate 			case TT_SYSC:
20740Sstevel@tonic-gate 			case TT_SYSC64:
20750Sstevel@tonic-gate #if defined(__amd64)
20760Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
20770Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20780Sstevel@tonic-gate 				case TT_SYSC64:
20790Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
20800Sstevel@tonic-gate 					/*FALLTHROUGH*/
20810Sstevel@tonic-gate #elif defined(__i386)
20820Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
20830Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20840Sstevel@tonic-gate 				case TT_SYSC64:
20850Sstevel@tonic-gate #endif
20860Sstevel@tonic-gate 				case TT_SYSC:
20870Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
20880Sstevel@tonic-gate 					break;
20890Sstevel@tonic-gate 				case TT_SYSCALL:
20900Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
20910Sstevel@tonic-gate 					break;
20920Sstevel@tonic-gate 				case TT_SYSENTER:
20930Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
20940Sstevel@tonic-gate 					break;
20950Sstevel@tonic-gate 				default:
20960Sstevel@tonic-gate 					break;
20970Sstevel@tonic-gate 				}
20980Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
20990Sstevel@tonic-gate 				if (sys != NULL) {
21000Sstevel@tonic-gate 					sym = kobj_getsymname(
21010Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
21020Sstevel@tonic-gate 					    &off);
21030Sstevel@tonic-gate 					if (sym != NULL)
21043446Smrj 						printf(fmt3, sym);
21050Sstevel@tonic-gate 					else
21060Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
21070Sstevel@tonic-gate 				} else {
21083446Smrj 					printf(fmt3, "unknown");
21090Sstevel@tonic-gate 				}
21100Sstevel@tonic-gate 				break;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 			case TT_INTERRUPT:
21130Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
21140Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
21150Sstevel@tonic-gate 				if (vec != NULL) {
21160Sstevel@tonic-gate 					sym = kobj_getsymname(
21170Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
21180Sstevel@tonic-gate 					if (sym != NULL)
21193446Smrj 						printf(fmt3, sym);
21200Sstevel@tonic-gate 					else
21210Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
21220Sstevel@tonic-gate 				} else {
21233446Smrj 					printf(fmt3, "unknown ");
21240Sstevel@tonic-gate 				}
21250Sstevel@tonic-gate 				break;
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 			case TT_TRAP:
21283446Smrj 			case TT_EVENT:
21290Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
21300Sstevel@tonic-gate 				printf(fmt2, "trap", type);
21313446Smrj 				if (type < TRAP_TYPES)
21323446Smrj 					printf("     #%s ",
21333446Smrj 					    trap_type_mnemonic[type]);
21343446Smrj 				else
21353446Smrj 					switch (type) {
21363446Smrj 					case T_AST:
21373446Smrj 						printf(fmt3, "ast");
21383446Smrj 						break;
21393446Smrj 					default:
21403446Smrj 						printf(fmt3, "");
21413446Smrj 						break;
21423446Smrj 					}
21433446Smrj 				break;
21443446Smrj 
21453446Smrj 			case TT_XCALL:
21463446Smrj 				printf(fmt2, "xcal",
21473446Smrj 				    rec->ttr_info.xc_entry.xce_marker);
21483446Smrj 				printf(fmt3, "");
21490Sstevel@tonic-gate 				break;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 			default:
21520Sstevel@tonic-gate 				break;
21530Sstevel@tonic-gate 			}
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
21560Sstevel@tonic-gate 			if (sym != NULL)
21570Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
21580Sstevel@tonic-gate 			else
21590Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
21620Sstevel@tonic-gate 				int s;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
21650Sstevel@tonic-gate 					printf(
21660Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
21670Sstevel@tonic-gate 					    rec->ttr_ipl,
21680Sstevel@tonic-gate 					    rec->ttr_spl,
21690Sstevel@tonic-gate 					    rec->ttr_pri);
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
21740Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 				/*
21770Sstevel@tonic-gate 				 * print out the pc stack that we recorded
21780Sstevel@tonic-gate 				 * at trap time (if any)
21790Sstevel@tonic-gate 				 */
21800Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
21810Sstevel@tonic-gate 					uintptr_t fullpc;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
21840Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
21850Sstevel@tonic-gate 						break;
21860Sstevel@tonic-gate 					}
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
21910Sstevel@tonic-gate 					if (sym != NULL)
21920Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
21930Sstevel@tonic-gate 						    sym, off);
21940Sstevel@tonic-gate 					else
21950Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
21960Sstevel@tonic-gate 				}
21970Sstevel@tonic-gate 				printf("\n");
21980Sstevel@tonic-gate 			}
21990Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
22000Sstevel@tonic-gate 		}
22010Sstevel@tonic-gate 	}
22020Sstevel@tonic-gate }
22030Sstevel@tonic-gate 
22043446Smrj /*
22053446Smrj  * Help with constructing traptrace records in C
22063446Smrj  */
22073446Smrj trap_trace_rec_t *
22083446Smrj trap_trace_get_traceptr(uint8_t marker, ulong_t pc, ulong_t sp)
22093446Smrj {
22103446Smrj 	trap_trace_rec_t *ttr;
22113446Smrj 
22123446Smrj 	if (trap_trace_freeze)
22133446Smrj 		ttr = &trap_trace_postmort;
22143446Smrj 	else {
22153446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[CPU->cpu_id];
22163446Smrj 
22173446Smrj 		ttr = (void *)ttc->ttc_next;
22183446Smrj 
22193446Smrj 		if (ttc->ttc_next >= ttc->ttc_limit)
22203446Smrj 			ttc->ttc_next = ttc->ttc_first;
22213446Smrj 		else
22223446Smrj 			ttc->ttc_next += sizeof (trap_trace_rec_t);
22233446Smrj 	}
22243446Smrj 
22253446Smrj 	ttr->ttr_regs.r_sp = sp;
22263446Smrj 	ttr->ttr_regs.r_pc = pc;
22273446Smrj 	ttr->ttr_cr2 = getcr2();
22283446Smrj 	ttr->ttr_curthread = (uintptr_t)curthread;
22293446Smrj 	ttr->ttr_stamp = tsc_read();
22303446Smrj 	ttr->ttr_marker = marker;
22313446Smrj 	return (ttr);
22323446Smrj }
22333446Smrj 
22340Sstevel@tonic-gate #endif	/* TRAPTRACE */
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate void
22375084Sjohnlev panic_showtrap(struct panic_trap_info *tip)
22380Sstevel@tonic-gate {
22390Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate #if defined(TRAPTRACE)
22420Sstevel@tonic-gate 	dump_ttrace();
22435084Sjohnlev #endif
22440Sstevel@tonic-gate 
22455084Sjohnlev #if !defined(__xpv)
22460Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
22470Sstevel@tonic-gate 		dump_tss();
22485084Sjohnlev #endif
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate void
22525084Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
22530Sstevel@tonic-gate {
22540Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
22550Sstevel@tonic-gate }
2256