xref: /onnv-gate/usr/src/uts/i86pc/os/trap.c (revision 5849:38f337e8c0bd)
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 /*
23*5849Ssethg  * 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)
1580Sstevel@tonic-gate static void dump_ttrace(void);
1590Sstevel@tonic-gate #endif	/* TRAPTRACE */
1600Sstevel@tonic-gate static void dumpregs(struct regs *);
1610Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1620Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*ARGSUSED*/
1650Sstevel@tonic-gate static int
1660Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1670Sstevel@tonic-gate {
1685084Sjohnlev 	struct panic_trap_info ti;
1690Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1720Sstevel@tonic-gate 		trap_name = trap_type[type];
1730Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1740Sstevel@tonic-gate 	} else {
1750Sstevel@tonic-gate 		trap_name = "trap";
1760Sstevel@tonic-gate 		trap_mnemonic = "-";
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #ifdef TRAPTRACE
1800Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
1810Sstevel@tonic-gate #endif
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	ti.trap_regs = rp;
1840Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
1850Sstevel@tonic-gate 	ti.trap_addr = addr;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
1900Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
1910Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
1920Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
1930Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
1940Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
1950Sstevel@tonic-gate 		    "a NULL pointer dereference" :
1960Sstevel@tonic-gate 		    "an illegal access to a user address");
1970Sstevel@tonic-gate 	} else
1980Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
1990Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
2000Sstevel@tonic-gate 	return (0);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate static int
2100Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2150Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2160Sstevel@tonic-gate 		return (1);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	return (0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2230Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2240Sstevel@tonic-gate  * instruction.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2270Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
2283939Ssethg  * int $T_SYSCALLINT is two bytes: 0xCD 0x91
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static int
2323939Ssethg instr_is_other_syscall(caddr_t pc, int which)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2350Sstevel@tonic-gate 
2363939Ssethg 	ASSERT(which == X86_SEP || which == X86_ASYSC || which == 0xCD);
2370Sstevel@tonic-gate 
2383939Ssethg 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0)
2390Sstevel@tonic-gate 		return (0);
2400Sstevel@tonic-gate 
2413939Ssethg 	switch (which) {
2423939Ssethg 	case X86_SEP:
2433939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x34)
2443939Ssethg 			return (1);
2453939Ssethg 		break;
2463939Ssethg 	case X86_ASYSC:
2473939Ssethg 		if (instr[0] == 0x0F && instr[1] == 0x05)
2483939Ssethg 			return (1);
2493939Ssethg 		break;
2503939Ssethg 	case 0xCD:
2513939Ssethg 		if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT)
2523939Ssethg 			return (1);
2533939Ssethg 		break;
2543939Ssethg 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	return (0);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2593939Ssethg static const char *
2603939Ssethg syscall_insn_string(int syscall_insn)
2613939Ssethg {
2623939Ssethg 	switch (syscall_insn) {
2633939Ssethg 	case X86_SEP:
2643939Ssethg 		return ("sysenter");
2653939Ssethg 	case X86_ASYSC:
2663939Ssethg 		return ("syscall");
2673939Ssethg 	case 0xCD:
2683939Ssethg 		return ("int");
2693939Ssethg 	default:
2703939Ssethg 		return ("Unknown");
2713939Ssethg 	}
2723939Ssethg }
2733939Ssethg 
2743939Ssethg static int
2753939Ssethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn)
2763939Ssethg {
2773939Ssethg 	caddr_t	linearpc;
2783939Ssethg 	int return_code = 0;
2793939Ssethg 
2803939Ssethg 	mutex_enter(&p->p_ldtlock);	/* Must be held across linear_pc() */
2813939Ssethg 
2823939Ssethg 	if (linear_pc(rp, p, &linearpc) == 0) {
2833939Ssethg 
2843939Ssethg 		/*
2853939Ssethg 		 * If another thread beat us here, it already changed
2863939Ssethg 		 * this site to the slower (int) syscall instruction.
2873939Ssethg 		 */
2883939Ssethg 		if (instr_is_other_syscall(linearpc, 0xCD)) {
2893939Ssethg 			return_code = 1;
2903939Ssethg 		} else if (instr_is_other_syscall(linearpc, syscall_insn)) {
2913939Ssethg 
2923939Ssethg 			if (rewrite_syscall(linearpc) == 0) {
2933939Ssethg 				return_code = 1;
2943939Ssethg 			}
2953939Ssethg #ifdef DEBUG
2963939Ssethg 			else
2973939Ssethg 				cmn_err(CE_WARN, "failed to rewrite %s "
2983939Ssethg 				    "instruction in process %d",
2993939Ssethg 				    syscall_insn_string(syscall_insn),
3003939Ssethg 				    p->p_pid);
3013939Ssethg #endif /* DEBUG */
3023939Ssethg 		}
3033939Ssethg 	}
3043939Ssethg 
3053939Ssethg 	mutex_exit(&p->p_ldtlock);	/* Must be held across linear_pc() */
3063939Ssethg 
3073939Ssethg 	return (return_code);
3083939Ssethg }
3093939Ssethg 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
3120Sstevel@tonic-gate  *
3130Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
3140Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
3150Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate #define	LCALLSIZE	7
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static int
3213939Ssethg instr_is_lcall_syscall(caddr_t pc)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
3260Sstevel@tonic-gate 	    instr[0] == 0x9a &&
3270Sstevel@tonic-gate 	    instr[1] == 0 &&
3280Sstevel@tonic-gate 	    instr[2] == 0 &&
3290Sstevel@tonic-gate 	    instr[3] == 0 &&
3300Sstevel@tonic-gate 	    instr[4] == 0 &&
3310Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
3320Sstevel@tonic-gate 	    instr[6] == 0)
3330Sstevel@tonic-gate 		return (1);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	return (0);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3381363Sfvdl #ifdef __amd64
3391363Sfvdl 
3401363Sfvdl /*
3413446Smrj  * In the first revisions of amd64 CPUs produced by AMD, the LAHF and
3423446Smrj  * SAHF instructions were not implemented in 64-bit mode. Later revisions
3431363Sfvdl  * did implement these instructions. An extension to the cpuid instruction
3441363Sfvdl  * was added to check for the capability of executing these instructions
3453446Smrj  * in 64-bit mode.
3461363Sfvdl  *
3471363Sfvdl  * Intel originally did not implement these instructions in EM64T either,
3481363Sfvdl  * but added them in later revisions.
3491363Sfvdl  *
3501363Sfvdl  * So, there are different chip revisions by both vendors out there that
3511363Sfvdl  * may or may not implement these instructions. The easy solution is to
3521363Sfvdl  * just always emulate these instructions on demand.
3531363Sfvdl  *
3541363Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
3551363Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
3561363Sfvdl  */
3571363Sfvdl 
3581363Sfvdl #define	LSAHFSIZE 1
3591363Sfvdl 
3601363Sfvdl static int
3611363Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
3621363Sfvdl {
3631363Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
3641363Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
3651363Sfvdl 		return (1);
3661363Sfvdl 	return (0);
3671363Sfvdl }
3681363Sfvdl 
3691363Sfvdl /*
3701363Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
3711363Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
3721363Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
3731363Sfvdl  *
3741363Sfvdl  * Note that %ah is bits 8-15 of %rax.
3751363Sfvdl  */
3761363Sfvdl static void
3771363Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
3781363Sfvdl {
3791363Sfvdl 	if (instr == 0x9e) {
3801363Sfvdl 		/* sahf. Copy bits from %ah to flags. */
3811363Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
3821363Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
3831363Sfvdl 	} else {
3841363Sfvdl 		/* lahf. Copy bits from flags to %ah. */
3851363Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
3861363Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
3871363Sfvdl 	}
3881363Sfvdl 	rp->r_pc += LSAHFSIZE;
3891363Sfvdl }
3901363Sfvdl #endif /* __amd64 */
3911363Sfvdl 
3920Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
3980Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
3990Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate #define	PREFETCHSIZE 3
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate static int
4050Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
4080Sstevel@tonic-gate 		return (1);
4090Sstevel@tonic-gate 	return (0);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate static int
4130Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
4160Sstevel@tonic-gate 	int	error;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
4210Sstevel@tonic-gate 		return (1);
4220Sstevel@tonic-gate 	return (0);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
4310Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
4323446Smrj  * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1)
4333446Smrj  * however, there are paths that arrive here with PS_IE == 0 so special care
4343446Smrj  * must be taken in those cases.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate void
4370Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
4380Sstevel@tonic-gate {
4393446Smrj 	kthread_t *ct = curthread;
4400Sstevel@tonic-gate 	enum seg_rw rw;
4410Sstevel@tonic-gate 	unsigned type;
4423446Smrj 	proc_t *p = ttoproc(ct);
4433446Smrj 	klwp_t *lwp = ttolwp(ct);
4440Sstevel@tonic-gate 	uintptr_t lofault;
4450Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
4460Sstevel@tonic-gate 	enum fault_type fault_type;
4470Sstevel@tonic-gate 	k_siginfo_t siginfo;
4480Sstevel@tonic-gate 	uint_t fault = 0;
4490Sstevel@tonic-gate 	int mstate;
4500Sstevel@tonic-gate 	int sicode = 0;
4510Sstevel@tonic-gate 	int watchcode;
4520Sstevel@tonic-gate 	int watchpage;
4530Sstevel@tonic-gate 	caddr_t vaddr;
4542712Snn35248 	int singlestep_twiddle;
4550Sstevel@tonic-gate 	size_t sz;
4560Sstevel@tonic-gate 	int ta;
4571363Sfvdl #ifdef __amd64
4581363Sfvdl 	uchar_t instr;
4591363Sfvdl #endif
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	type = rp->r_trapno;
4640Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
4653446Smrj 	ASSERT(ct->t_schedflag & TS_DONT_SWAP);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (type == T_PGFLT) {
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		errcode = rp->r_err;
4700Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
4710Sstevel@tonic-gate 			rw = S_WRITE;
4720Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
4730Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
4740Sstevel@tonic-gate 			rw = S_EXEC;
4750Sstevel@tonic-gate 		else
4760Sstevel@tonic-gate 			rw = S_READ;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate #if defined(__i386)
4790Sstevel@tonic-gate 		/*
4800Sstevel@tonic-gate 		 * Pentium Pro work-around
4810Sstevel@tonic-gate 		 */
4820Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
4830Sstevel@tonic-gate 			uint_t	attr;
4840Sstevel@tonic-gate 			uint_t	priv_violation;
4850Sstevel@tonic-gate 			uint_t	access_violation;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
4880Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
4890Sstevel@tonic-gate 			    == -1) {
4900Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
4910Sstevel@tonic-gate 			} else {
4920Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
4935084Sjohnlev 				    !(attr & PROT_USER);
4940Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
4955084Sjohnlev 				    !(attr & PROT_WRITE);
4960Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
4970Sstevel@tonic-gate 					goto cleanup;
4980Sstevel@tonic-gate 			}
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate #endif /* __i386 */
5010Sstevel@tonic-gate 
5023446Smrj 	} else if (type == T_SGLSTP && lwp != NULL)
5033446Smrj 		lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (tdebug)
5060Sstevel@tonic-gate 		showregs(type, rp, addr);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
5090Sstevel@tonic-gate 		/*
5100Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
5110Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
5120Sstevel@tonic-gate 		 * The current process credential applies to the thread for
5130Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
5140Sstevel@tonic-gate 		 * should already be set up.
5150Sstevel@tonic-gate 		 */
5163446Smrj 		if (ct->t_cred != p->p_cred) {
5173446Smrj 			cred_t *oldcred = ct->t_cred;
5180Sstevel@tonic-gate 			/*
5190Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
5200Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
5210Sstevel@tonic-gate 			 * allocated cred structure.
5220Sstevel@tonic-gate 			 */
5233446Smrj 			ct->t_cred = crgetcred();
5240Sstevel@tonic-gate 			crfree(oldcred);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		ASSERT(lwp != NULL);
5270Sstevel@tonic-gate 		type |= USER;
5280Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
5290Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		switch (type) {
5320Sstevel@tonic-gate 		case T_PGFLT + USER:
5330Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
5340Sstevel@tonic-gate 				mstate = LMS_TFAULT;
5350Sstevel@tonic-gate 			else
5360Sstevel@tonic-gate 				mstate = LMS_DFAULT;
5370Sstevel@tonic-gate 			break;
5380Sstevel@tonic-gate 		default:
5390Sstevel@tonic-gate 			mstate = LMS_TRAP;
5400Sstevel@tonic-gate 			break;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 		/* Kernel probe */
5430Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
5440Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
5453446Smrj 		mstate = new_mstate(ct, mstate);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	switch (type) {
5510Sstevel@tonic-gate 	case T_PGFLT + USER:
5520Sstevel@tonic-gate 	case T_SGLSTP:
5530Sstevel@tonic-gate 	case T_SGLSTP + USER:
5540Sstevel@tonic-gate 	case T_BPTFLT + USER:
5550Sstevel@tonic-gate 		break;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	default:
5580Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
5590Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
5600Sstevel@tonic-gate 		break;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	switch (type) {
564*5849Ssethg 	case T_SIMDFPE:
565*5849Ssethg 		/* Make sure we enable interrupts before die()ing */
566*5849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
567*5849Ssethg 		/*FALLTHROUGH*/
5680Sstevel@tonic-gate 	default:
5690Sstevel@tonic-gate 		if (type & USER) {
5700Sstevel@tonic-gate 			if (tudebug)
5710Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5720Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5730Sstevel@tonic-gate 			    type & ~USER);
5740Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5750Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
5760Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
5770Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
5780Sstevel@tonic-gate 			fault = FLTILL;
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 		} else {
5810Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5820Sstevel@tonic-gate 			/*NOTREACHED*/
5830Sstevel@tonic-gate 		}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
5885254Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
5895254Sgavinm 		 * via the installed trampoline.
5900Sstevel@tonic-gate 		 */
5913446Smrj 		if ((ct->t_ontrap != NULL) &&
5923446Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
5933446Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
5945254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
5955254Sgavinm 			goto cleanup;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
6000Sstevel@tonic-gate 		 * across this. Here we assume that an address
6010Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
6020Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
6030Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
6040Sstevel@tonic-gate 		 * starting and because we know that we always have
6050Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
6060Sstevel@tonic-gate 		 */
6073446Smrj 		lofault = ct->t_lofault;
6083446Smrj 		ct->t_lofault = 0;
6090Sstevel@tonic-gate 
6103446Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6130Sstevel@tonic-gate 			res = pagefault(addr,
6140Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6150Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6160Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6170Sstevel@tonic-gate 			    grow(addr))
6180Sstevel@tonic-gate 				res = 0;
6190Sstevel@tonic-gate 		} else {
6200Sstevel@tonic-gate 			res = pagefault(addr,
6210Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6220Sstevel@tonic-gate 		}
6233446Smrj 		(void) new_mstate(ct, mstate);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
6270Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6280Sstevel@tonic-gate 		 */
6293446Smrj 		ct->t_lofault = lofault;
6300Sstevel@tonic-gate 		if (res == 0)
6310Sstevel@tonic-gate 			goto cleanup;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6340Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6350Sstevel@tonic-gate 			/*
6360Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6370Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6380Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6390Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6400Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6410Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6420Sstevel@tonic-gate 			 */
6430Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6460Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6470Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6480Sstevel@tonic-gate 				    PFN_INVALID &&
6490Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6500Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6510Sstevel@tonic-gate 					rp->r_pc = rip;
6520Sstevel@tonic-gate 					goto cleanup;
6530Sstevel@tonic-gate 				}
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6590Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6600Sstevel@tonic-gate 			/*
6610Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6620Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6630Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6640Sstevel@tonic-gate 			 * instruction.
6650Sstevel@tonic-gate 			 */
6660Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6700Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6710Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6720Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6730Sstevel@tonic-gate 			 */
6740Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
6750Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
6760Sstevel@tonic-gate #ifdef DEBUG
6770Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
6780Sstevel@tonic-gate 					    "occurred: kernel prefetch"
6790Sstevel@tonic-gate 					    " at %p generated a page fault!",
6800Sstevel@tonic-gate 					    (void *)rp->r_pc);
6810Sstevel@tonic-gate #endif /* DEBUG */
6820Sstevel@tonic-gate 					goto cleanup;
6830Sstevel@tonic-gate 				}
6840Sstevel@tonic-gate 			}
6850Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		if (lofault == 0)
6900Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		/*
6930Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		if (lodebug) {
6960Sstevel@tonic-gate 			showregs(type, rp, addr);
6970Sstevel@tonic-gate 			traceregs(rp);
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
7000Sstevel@tonic-gate 			res = FC_ERRNO(res);
7010Sstevel@tonic-gate 		else
7020Sstevel@tonic-gate 			res = EFAULT;
7030Sstevel@tonic-gate 		rp->r_r0 = res;
7043446Smrj 		rp->r_pc = ct->t_lofault;
7050Sstevel@tonic-gate 		goto cleanup;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7080Sstevel@tonic-gate 		if (faultdebug) {
7090Sstevel@tonic-gate 			char *fault_str;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 			switch (rw) {
7120Sstevel@tonic-gate 			case S_READ:
7130Sstevel@tonic-gate 				fault_str = "read";
7140Sstevel@tonic-gate 				break;
7150Sstevel@tonic-gate 			case S_WRITE:
7160Sstevel@tonic-gate 				fault_str = "write";
7170Sstevel@tonic-gate 				break;
7180Sstevel@tonic-gate 			case S_EXEC:
7190Sstevel@tonic-gate 				fault_str = "exec";
7200Sstevel@tonic-gate 				break;
7210Sstevel@tonic-gate 			default:
7220Sstevel@tonic-gate 				fault_str = "";
7230Sstevel@tonic-gate 				break;
7240Sstevel@tonic-gate 			}
7250Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7260Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7300Sstevel@tonic-gate 		/*
7310Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7320Sstevel@tonic-gate 		 *
7330Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7340Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7350Sstevel@tonic-gate 		 * address will be
7360Sstevel@tonic-gate 		 *
7370Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7380Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7390Sstevel@tonic-gate 		 *
7400Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7410Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7420Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7450Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7460Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7470Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7480Sstevel@tonic-gate 			if (!opteron_erratum_100)
7490Sstevel@tonic-gate 				panic("unexpected erratum #100");
7500Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7510Sstevel@tonic-gate 				goto out;
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7560Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7570Sstevel@tonic-gate #ifdef __i386
7580Sstevel@tonic-gate 		/*
7590Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
7600Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
7610Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
7620Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
7630Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
7640Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
7650Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
7660Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
7670Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
7680Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
7713939Ssethg 		    rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
7720Sstevel@tonic-gate 			extern void watch_syscall(void);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
7750Sstevel@tonic-gate 			watch_syscall();	/* never returns */
7760Sstevel@tonic-gate 			/* NOTREACHED */
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate #endif /* __i386 */
7790Sstevel@tonic-gate 		vaddr = addr;
7800Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
7810Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
7820Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
7830Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
7840Sstevel@tonic-gate 			if (ta) {
7850Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
7865084Sjohnlev 				    watchcode, rp->r_pc);
7870Sstevel@tonic-gate 				fault_type = F_INVAL;
7880Sstevel@tonic-gate 			} else {
7890Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7900Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
7910Sstevel@tonic-gate 				siginfo.si_code = watchcode;
7920Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
7930Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
7940Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
7950Sstevel@tonic-gate 				fault = FLTWATCH;
7960Sstevel@tonic-gate 				break;
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 		} else {
7990Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
8000Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
8010Sstevel@tonic-gate 				goto out;
8020Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
8030Sstevel@tonic-gate 			fault_type = F_INVAL;
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8100Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8110Sstevel@tonic-gate 		 */
8120Sstevel@tonic-gate 		if (res == 0 ||
8130Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8140Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8150Sstevel@tonic-gate 		    grow(addr))) {
8160Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8170Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8180Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8190Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8200Sstevel@tonic-gate 				siginfo.si_addr = addr;
8210Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8220Sstevel@tonic-gate 			}
8230Sstevel@tonic-gate 			goto out;
8240Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8250Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8260Sstevel@tonic-gate 			report_stack_exec(p, addr);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8320Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8330Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8340Sstevel@tonic-gate 		 *
8350Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8360Sstevel@tonic-gate 		 */
8370Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8380Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8390Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8400Sstevel@tonic-gate #ifdef DEBUG
8410Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8420Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8430Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8440Sstevel@tonic-gate #endif /* DEBUG */
8450Sstevel@tonic-gate 			goto out;
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		if (tudebug)
8500Sstevel@tonic-gate 			showregs(type, rp, addr);
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8530Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8540Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8550Sstevel@tonic-gate 		 */
8560Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8570Sstevel@tonic-gate 		siginfo.si_addr = addr;
8580Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8590Sstevel@tonic-gate 		case FC_HWERR:
8600Sstevel@tonic-gate 		case FC_NOSUPPORT:
8610Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8620Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8630Sstevel@tonic-gate 			fault = FLTACCESS;
8640Sstevel@tonic-gate 			break;
8650Sstevel@tonic-gate 		case FC_ALIGN:
8660Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8670Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8680Sstevel@tonic-gate 			fault = FLTACCESS;
8690Sstevel@tonic-gate 			break;
8700Sstevel@tonic-gate 		case FC_OBJERR:
8710Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8720Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8730Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8740Sstevel@tonic-gate 				fault = FLTACCESS;
8750Sstevel@tonic-gate 			}
8760Sstevel@tonic-gate 			break;
8770Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
8780Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8790Sstevel@tonic-gate 			siginfo.si_code =
8800Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
8810Sstevel@tonic-gate 			fault = FLTBOUNDS;
8820Sstevel@tonic-gate 			break;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		break;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
8870Sstevel@tonic-gate 		/*
8880Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
8890Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
8900Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
8910Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
8920Sstevel@tonic-gate 		 * be to emulate that particular instruction.
8930Sstevel@tonic-gate 		 */
8940Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
8953939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_ASYSC))
8963939Ssethg 			goto out;
8971363Sfvdl 
8981363Sfvdl #ifdef __amd64
8991363Sfvdl 		/*
9001363Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
9011363Sfvdl 		 * See the instr_is_lsahf function for details.
9021363Sfvdl 		 */
9031363Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
9041363Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
9051363Sfvdl 			emulate_lsahf(rp, instr);
9061363Sfvdl 			goto out;
9071363Sfvdl 		}
9081363Sfvdl #endif
9091363Sfvdl 
9100Sstevel@tonic-gate 		/*FALLTHROUGH*/
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		if (tudebug)
9130Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9140Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9150Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9160Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9170Sstevel@tonic-gate 		fault = FLTILL;
9180Sstevel@tonic-gate 		break;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9210Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9220Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9230Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9240Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9250Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9260Sstevel@tonic-gate 		fault = FLTIZDIV;
9270Sstevel@tonic-gate 		break;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9300Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9310Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9320Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9330Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9340Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9350Sstevel@tonic-gate 		fault = FLTIOVF;
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9390Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9400Sstevel@tonic-gate 			showregs(type, rp, addr);
9410Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
9420Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9430Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
9440Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9450Sstevel@tonic-gate 			fault = FLTFPE;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		break;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
9500Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9510Sstevel@tonic-gate 		{
9520Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
953*5849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
954*5849Ssethg 				sti(); /* T_EXTOVRFLT comes in via cmninttrap */
9550Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
956*5849Ssethg 			}
9570Sstevel@tonic-gate 			type |= USER;
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 		/*FALLTHROUGH*/
9600Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
9610Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9620Sstevel@tonic-gate 			showregs(type, rp, addr);
9630Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9640Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9650Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9660Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9670Sstevel@tonic-gate 			fault = FLTBOUNDS;
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		break;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9720Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9730Sstevel@tonic-gate 		{
9740Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
975*5849Ssethg 			if (rp->r_pc != (uintptr_t)ndptrap_frstor) {
976*5849Ssethg 				sti(); /* T_EXTERRFLT comes in via cmninttrap */
9770Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
978*5849Ssethg 			}
9790Sstevel@tonic-gate 			type |= USER;
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 		/*FALLTHROUGH*/
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
9840Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9850Sstevel@tonic-gate 			showregs(type, rp, addr);
9860Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
9870Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9880Sstevel@tonic-gate 			siginfo.si_code  = sicode;
9890Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9900Sstevel@tonic-gate 			fault = FLTFPE;
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 		break;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
9950Sstevel@tonic-gate 		if (tudebug && tudebugsse)
9960Sstevel@tonic-gate 			showregs(type, rp, addr);
9970Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
9980Sstevel@tonic-gate 			/*
9990Sstevel@tonic-gate 			 * There are rumours that some user instructions
10000Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
10010Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
10020Sstevel@tonic-gate 			 */
10030Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
10040Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
10050Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
10060Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
10070Sstevel@tonic-gate 			fault = FLTILL;
10080Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
10090Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10100Sstevel@tonic-gate 			siginfo.si_code = sicode;
10110Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
10120Sstevel@tonic-gate 			fault = FLTFPE;
10130Sstevel@tonic-gate 		}
1014*5849Ssethg 
1015*5849Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
10160Sstevel@tonic-gate 		break;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10190Sstevel@tonic-gate 		/*
10200Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10210Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10220Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10230Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10240Sstevel@tonic-gate 		 * fatal.
10250Sstevel@tonic-gate 		 */
10260Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10270Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10300Sstevel@tonic-gate 		break;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		/* Now evaluate how we got here */
10350Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
10380Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
10390Sstevel@tonic-gate 			 * the first instruction in privileged mode.
10400Sstevel@tonic-gate 			 *
10410Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
10420Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
10430Sstevel@tonic-gate 			 *
10440Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
10450Sstevel@tonic-gate 			 */
10462712Snn35248 			singlestep_twiddle = 0;
10472712Snn35248 			if (rp->r_pc == (uintptr_t)sys_sysenter ||
10482712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_sysenter) {
10492712Snn35248 				singlestep_twiddle = 1;
10500Sstevel@tonic-gate #if defined(__amd64)
10510Sstevel@tonic-gate 				/*
10522712Snn35248 				 * Since we are already on the kernel's
10532712Snn35248 				 * %gs, on 64-bit systems the sysenter case
10542712Snn35248 				 * needs to adjust the pc to avoid
10552712Snn35248 				 * executing the swapgs instruction at the
10562712Snn35248 				 * top of the handler.
10570Sstevel@tonic-gate 				 */
10582712Snn35248 				if (rp->r_pc == (uintptr_t)sys_sysenter)
10592712Snn35248 					rp->r_pc = (uintptr_t)
10602712Snn35248 					    _sys_sysenter_post_swapgs;
10612712Snn35248 				else
10622712Snn35248 					rp->r_pc = (uintptr_t)
10632712Snn35248 					    _brand_sys_sysenter_post_swapgs;
10640Sstevel@tonic-gate #endif
10652712Snn35248 			}
10662712Snn35248 #if defined(__i386)
10672712Snn35248 			else if (rp->r_pc == (uintptr_t)sys_call ||
10682712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_call) {
10692712Snn35248 				singlestep_twiddle = 1;
10702712Snn35248 			}
10712712Snn35248 #endif
10723003Sdmick 			else {
10733003Sdmick 				/* not on sysenter/syscall; uregs available */
10743003Sdmick 				if (tudebug && tudebugbpt)
10753003Sdmick 					showregs(type, rp, (caddr_t)0);
10763003Sdmick 			}
10772712Snn35248 			if (singlestep_twiddle) {
10780Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
10790Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
10803446Smrj 				ct->t_post_sys = 1;
10812086Ssudheer 				aston(curthread);
10820Sstevel@tonic-gate 				goto cleanup;
10830Sstevel@tonic-gate 			}
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
10860Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
10870Sstevel@tonic-gate 			debug_enter((char *)NULL);
10880Sstevel@tonic-gate 		else
10890Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10900Sstevel@tonic-gate 		break;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
10930Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
10940Sstevel@tonic-gate 		goto cleanup;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
10970Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
10980Sstevel@tonic-gate 		break;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
11010Sstevel@tonic-gate 		/*
11025639Sgavinm 		 * Any #GP that occurs during an on_trap .. no_trap bracket
11035639Sgavinm 		 * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection,
11045639Sgavinm 		 * or in a on_fault .. no_fault bracket, is forgiven
11055639Sgavinm 		 * and we trampoline.  This protection is given regardless
11065639Sgavinm 		 * of whether we are 32/64 bit etc - if a distinction is
11075639Sgavinm 		 * required then define new on_trap protection types.
11085639Sgavinm 		 *
11090Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
11105084Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
11115084Sjohnlev 		 * update_sregs while updating user segment registers.
11125084Sjohnlev 		 *
11135084Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
11145084Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
11155084Sjohnlev 		 * lives which is protected by segmentation.
11160Sstevel@tonic-gate 		 */
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 		/*
11190Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
11205254Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
11215084Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
11220Sstevel@tonic-gate 		 */
11233446Smrj 		if (ct->t_ontrap != NULL) {
11245084Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
11255084Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
11263446Smrj 
11275084Sjohnlev 			if (ttype != 0) {
11285084Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
11295084Sjohnlev 				if (tudebug)
11305084Sjohnlev 					showregs(type, rp, (caddr_t)0);
11315254Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
11325254Sgavinm 				goto cleanup;
11335084Sjohnlev 			}
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
11380Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
11390Sstevel@tonic-gate 		 */
11403446Smrj 		if (ct->t_lofault) {
11410Sstevel@tonic-gate 			/*
11420Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11430Sstevel@tonic-gate 			 */
11440Sstevel@tonic-gate 			if (lodebug) {
11450Sstevel@tonic-gate 				showregs(type, rp, addr);
11460Sstevel@tonic-gate 				traceregs(rp);
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
11493446Smrj 			rp->r_pc = ct->t_lofault;
11500Sstevel@tonic-gate 			goto cleanup;
11510Sstevel@tonic-gate 		}
11525639Sgavinm 
11535639Sgavinm 		/*
11545639Sgavinm 		 * We fall through to the next case, which repeats
11555639Sgavinm 		 * the OT_SEGMENT_ACCESS check which we've already
11565639Sgavinm 		 * done, so we'll always fall through to the
11575639Sgavinm 		 * T_STKFLT case.
11585639Sgavinm 		 */
11590Sstevel@tonic-gate 		/*FALLTHROUGH*/
11603446Smrj 	case T_SEGFLT:	/* segment not present fault */
11613446Smrj 		/*
11623446Smrj 		 * One example of this is #NP in update_sregs while
11633446Smrj 		 * attempting to update a user segment register
11643446Smrj 		 * that points to a descriptor that is marked not
11653446Smrj 		 * present.
11663446Smrj 		 */
11673446Smrj 		if (ct->t_ontrap != NULL &&
11683446Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
11693446Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
11705084Sjohnlev 			if (tudebug)
11715084Sjohnlev 				showregs(type, rp, (caddr_t)0);
11725254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
11735254Sgavinm 			goto cleanup;
11743446Smrj 		}
11753446Smrj 		/*FALLTHROUGH*/
11760Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
11770Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
11780Sstevel@tonic-gate 		if (tudebug)
11790Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11800Sstevel@tonic-gate 		if (kern_gpfault(rp))
11810Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11820Sstevel@tonic-gate 		goto cleanup;
11830Sstevel@tonic-gate 
11843446Smrj 	/*
11853446Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
11863446Smrj 	 * should have no need for them, so we put a stop to it here.
11873446Smrj 	 *
11883446Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
11893446Smrj 	 * a private LDT trying to do a system call. Emulate it.
11903446Smrj 	 *
11913446Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
11923446Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
11933446Smrj 	 */
11943446Smrj 
11950Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
11961217Srab 	case T_GPFLT + USER:	/* general protection violation */
11970Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11981217Srab 		if (p->p_model != DATAMODEL_NATIVE) {
11991217Srab #endif /* _SYSCALL32_IMPL */
12003939Ssethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
12011217Srab 			if (type == T_SEGFLT + USER)
12021217Srab 				ASSERT(p->p_ldt != NULL);
12031217Srab 
12041217Srab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
12051217Srab 			    type == T_SEGFLT + USER) {
12061217Srab 
12070Sstevel@tonic-gate 			/*
12081217Srab 			 * The user attempted a system call via the obsolete
12091217Srab 			 * call gate mechanism. Because the process doesn't have
12101217Srab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
12111217Srab 			 * Emulate the syscall here, just as we do above for a
12121217Srab 			 * #np trap.
12131217Srab 			 */
12141217Srab 
12151217Srab 			/*
12160Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
12170Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
12180Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
12190Sstevel@tonic-gate 			 */
12200Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
12210Sstevel@tonic-gate 			lwp->lwp_regs = rp;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 			/*
12240Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
12250Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
12260Sstevel@tonic-gate 			 * behavior here.
12270Sstevel@tonic-gate 			 */
12280Sstevel@tonic-gate 			mstate = LMS_USER;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 			dosyscall();
12310Sstevel@tonic-gate 			goto out;
12321217Srab 			}
12331217Srab 		}
12341217Srab #ifdef _SYSCALL32_IMPL
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
12370Sstevel@tonic-gate 		/*
12380Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
12390Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
12400Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
12410Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12420Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12430Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12440Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12450Sstevel@tonic-gate 		 */
12460Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
12473939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_SEP))
12483939Ssethg 			goto out;
12493939Ssethg 
12500Sstevel@tonic-gate 		/*FALLTHROUGH*/
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12530Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12540Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12550Sstevel@tonic-gate 		if (tudebug)
12560Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12570Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12580Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12590Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12600Sstevel@tonic-gate 		fault = FLTBOUNDS;
12610Sstevel@tonic-gate 		break;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12640Sstevel@tonic-gate 		if (tudebug)
12650Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12660Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12670Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12680Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12690Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12700Sstevel@tonic-gate 		fault = FLTACCESS;
12710Sstevel@tonic-gate 		break;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
12740Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12750Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		/* Was it single-stepping? */
12780Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
12790Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
12820Sstevel@tonic-gate 			/*
12830Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
12842712Snn35248 			 * give precedence to WATCH_STEP.  If neither is set,
12850Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
12860Sstevel@tonic-gate 			 * as NORMAL_STEP.
12870Sstevel@tonic-gate 			 */
12882712Snn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
12892712Snn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
12902712Snn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
12910Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
12920Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
12930Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
12940Sstevel@tonic-gate 				fault = FLTTRACE;
12950Sstevel@tonic-gate 			}
12960Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
12970Sstevel@tonic-gate 		} else {
12980Sstevel@tonic-gate 			cmn_err(CE_WARN,
12990Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
13000Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 		break;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
13050Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
13060Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
13070Sstevel@tonic-gate 		/*
13080Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
13090Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
13100Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
13110Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
13120Sstevel@tonic-gate 		 */
13130Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
13140Sstevel@tonic-gate 			rp->r_pc--;
13150Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
13160Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
13170Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
13180Sstevel@tonic-gate 		fault = FLTBPT;
13190Sstevel@tonic-gate 		break;
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	case T_AST:
13220Sstevel@tonic-gate 		/*
13230Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
13240Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
13250Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
13260Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
13270Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
13280Sstevel@tonic-gate 		 * doing the T_AST processing.
13290Sstevel@tonic-gate 		 */
13300Sstevel@tonic-gate 		goto cleanup;
13310Sstevel@tonic-gate 
13325254Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
13335254Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
13345254Sgavinm 			proc_t *p = ttoproc(curthread);
13355254Sgavinm 
13365254Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
13375254Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
13385254Sgavinm 			siginfo.si_signo = SIGKILL;
13395254Sgavinm 			siginfo.si_code = SI_NOINFO;
13405254Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
13410Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
13420Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13430Sstevel@tonic-gate 				/*
13440Sstevel@tonic-gate 				 * Signal performance counter overflow
13450Sstevel@tonic-gate 				 */
13460Sstevel@tonic-gate 				if (tudebug)
13470Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13480Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13490Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13500Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13510Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13520Sstevel@tonic-gate 				fault = FLTCPCOVF;
13530Sstevel@tonic-gate 			}
13540Sstevel@tonic-gate 		}
13555254Sgavinm 
13560Sstevel@tonic-gate 		break;
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/*
13600Sstevel@tonic-gate 	 * We can't get here from a system trap
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	ASSERT(type & USER);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	if (fault) {
13653506Saf 		/* We took a fault so abort single step. */
13663506Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13670Sstevel@tonic-gate 		/*
13680Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13690Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13700Sstevel@tonic-gate 		 */
13710Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13720Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 		/*
13770Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
13780Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
13790Sstevel@tonic-gate 		 * deliver the associated signal.
13800Sstevel@tonic-gate 		 */
13810Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
13820Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
13830Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
13840Sstevel@tonic-gate 			siginfo.si_signo = 0;
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	if (siginfo.si_signo)
13880Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
13910Sstevel@tonic-gate 		profil_tick(rp->r_pc);
13920Sstevel@tonic-gate 
13933446Smrj 	if (ct->t_astflag | ct->t_sig_check) {
13940Sstevel@tonic-gate 		/*
13950Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
13960Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
13970Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
13980Sstevel@tonic-gate 		 * syscall.
13990Sstevel@tonic-gate 		 */
14003446Smrj 		astoff(ct);
14012086Ssudheer 		/*
14022086Ssudheer 		 * If a single-step trap occurred on a syscall (see above)
14032086Ssudheer 		 * recognize it now.  Do this before checking for signals
14042086Ssudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
14052086Ssudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
14062086Ssudheer 		 */
14072086Ssudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
14082086Ssudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
14092086Ssudheer 
14103446Smrj 		ct->t_sig_check = 0;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
14130Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
14140Sstevel@tonic-gate 			timer_lwpbind();
14150Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
14160Sstevel@tonic-gate 		}
14170Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		/*
14200Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
14210Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
14220Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
14230Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
14240Sstevel@tonic-gate 		 * for the kaio request to complete.
14250Sstevel@tonic-gate 		 */
14260Sstevel@tonic-gate 		if (p->p_aio)
14270Sstevel@tonic-gate 			aio_cleanup(0);
14280Sstevel@tonic-gate 		/*
14290Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
14300Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
14310Sstevel@tonic-gate 		 * sets the AST flag.
14320Sstevel@tonic-gate 		 *
14330Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14340Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14350Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14360Sstevel@tonic-gate 		 */
14370Sstevel@tonic-gate 		if (ISHOLD(p))
14380Sstevel@tonic-gate 			holdlwp();
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 		/*
14410Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14420Sstevel@tonic-gate 		 * set t_astflag afterwards.
14430Sstevel@tonic-gate 		 */
14443446Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14450Sstevel@tonic-gate 			if (issig(FORREAL))
14460Sstevel@tonic-gate 				psig();
14473446Smrj 			ct->t_sig_check = 1;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 
14503446Smrj 		if (ct->t_rprof != NULL) {
14510Sstevel@tonic-gate 			realsigprof(0, 0);
14523446Smrj 			ct->t_sig_check = 1;
14530Sstevel@tonic-gate 		}
14541468Smarx 
14551468Smarx 		/*
14561468Smarx 		 * /proc can't enable/disable the trace bit itself
14571468Smarx 		 * because that could race with the call gate used by
14581468Smarx 		 * system calls via "lcall". If that happened, an
14591468Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
14601468Smarx 		 * therefore schedule an AST for the purpose.
14611468Smarx 		 */
14621468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
14631468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
14641468Smarx 			rp->r_ps |= PS_T;
14651468Smarx 		}
14661468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
14671468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
14681468Smarx 			rp->r_ps &= ~PS_T;
14691468Smarx 		}
14700Sstevel@tonic-gate 	}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14730Sstevel@tonic-gate 	ASSERT(type & USER);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	if (ISHOLD(p))
14760Sstevel@tonic-gate 		holdlwp();
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/*
14790Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
14800Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
14810Sstevel@tonic-gate 	 * restore the user-level priority.
14820Sstevel@tonic-gate 	 *
14830Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
14840Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
14850Sstevel@tonic-gate 	 * is set back to LWP_SYS).
14860Sstevel@tonic-gate 	 */
14870Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
14880Sstevel@tonic-gate 
14893446Smrj 	if (ct->t_trapret) {
14903446Smrj 		ct->t_trapret = 0;
14913446Smrj 		thread_lock(ct);
14923446Smrj 		CL_TRAPRET(ct);
14933446Smrj 		thread_unlock(ct);
14940Sstevel@tonic-gate 	}
14953792Sakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
14960Sstevel@tonic-gate 		preempt();
14973446Smrj 	(void) new_mstate(ct, mstate);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	/* Kernel probe */
15000Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
15010Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	return;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate cleanup:	/* system traps end up here */
15060Sstevel@tonic-gate 	ASSERT(!(type & USER));
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate /*
15100Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
15110Sstevel@tonic-gate  */
15120Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
15150Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
15160Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
15170Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
15180Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
15190Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
15200Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
15210Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
15220Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
15230Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
15240Sstevel@tonic-gate } kpreempt_cnts;
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate /*
15270Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
15280Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
15290Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
15300Sstevel@tonic-gate  */
15310Sstevel@tonic-gate void
15320Sstevel@tonic-gate kpreempt(int asyncspl)
15330Sstevel@tonic-gate {
15343446Smrj 	kthread_t *ct = curthread;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
15370Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
15380Sstevel@tonic-gate 		return;
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	/*
15420Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	do {
15453446Smrj 		if (ct->t_preempt) {
15460Sstevel@tonic-gate 			/*
15470Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
15480Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
15490Sstevel@tonic-gate 			 */
15503446Smrj 			if (ct->t_pri < 0)
15510Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
15523446Smrj 			else if (ct->t_flag & T_INTR_THREAD) {
15530Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
15543446Smrj 				if (ct->t_pil == CLOCK_LEVEL)
15550Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
15560Sstevel@tonic-gate 			} else
15570Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
15580Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
15590Sstevel@tonic-gate 			return;
15600Sstevel@tonic-gate 		}
15613446Smrj 		if (ct->t_state != TS_ONPROC ||
15623446Smrj 		    ct->t_disp_queue != CPU->cpu_disp) {
15630Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
15640Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
15650Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
15660Sstevel@tonic-gate 				/* already in swtch(), force another */
15670Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
15680Sstevel@tonic-gate 				siron();
15690Sstevel@tonic-gate 			}
15700Sstevel@tonic-gate 			return;
15710Sstevel@tonic-gate 		}
15720Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
15730Sstevel@tonic-gate 			/*
15740Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
15750Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
15760Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
15770Sstevel@tonic-gate 			 */
15780Sstevel@tonic-gate 			siron();	/* check back later */
15790Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
15800Sstevel@tonic-gate 			return;
15810Sstevel@tonic-gate 		}
15823446Smrj 		if (!interrupts_enabled()) {
15833446Smrj 			/*
15843446Smrj 			 * Can't preempt while running with ints disabled
15853446Smrj 			 */
15863446Smrj 			kpreempt_cnts.kpc_prilevel++;
15873446Smrj 			return;
15883446Smrj 		}
15890Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
15900Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
15910Sstevel@tonic-gate 		else
15920Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
15930Sstevel@tonic-gate 
15943446Smrj 		ct->t_preempt++;
15950Sstevel@tonic-gate 		preempt();
15963446Smrj 		ct->t_preempt--;
15970Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate /*
16010Sstevel@tonic-gate  * Print out debugging info.
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate static void
16040Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	int s;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	s = spl7();
16090Sstevel@tonic-gate 	type &= ~USER;
16103446Smrj 	if (PTOU(curproc)->u_comm[0])
16113446Smrj 		printf("%s: ", PTOU(curproc)->u_comm);
16120Sstevel@tonic-gate 	if (type < TRAP_TYPES)
16130Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
16140Sstevel@tonic-gate 	else
16150Sstevel@tonic-gate 		switch (type) {
16160Sstevel@tonic-gate 		case T_SYSCALL:
16170Sstevel@tonic-gate 			printf("Syscall Trap:\n");
16180Sstevel@tonic-gate 			break;
16190Sstevel@tonic-gate 		case T_AST:
16200Sstevel@tonic-gate 			printf("AST\n");
16210Sstevel@tonic-gate 			break;
16220Sstevel@tonic-gate 		default:
16230Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
16240Sstevel@tonic-gate 			break;
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 	if (type == T_PGFLT) {
16270Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
16280Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
16290Sstevel@tonic-gate 	} else if (addr) {
16300Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
16310Sstevel@tonic-gate 	}
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
16340Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
16350Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate #if defined(__lint)
16380Sstevel@tonic-gate 	/*
16390Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
16400Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
16410Sstevel@tonic-gate 	 */
16420Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
16430Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
16440Sstevel@tonic-gate #else
16450Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
16460Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
16473446Smrj #endif	/* __lint */
16480Sstevel@tonic-gate 
16495084Sjohnlev 	printf("cr2: %lx", getcr2());
16505084Sjohnlev #if !defined(__xpv)
16515084Sjohnlev 	printf("cr3: %lx", getcr3());
16520Sstevel@tonic-gate #if defined(__amd64)
16535084Sjohnlev 	printf("cr8: %lx\n", getcr8());
16540Sstevel@tonic-gate #endif
16555084Sjohnlev #endif
16565084Sjohnlev 	printf("\n");
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	dumpregs(rp);
16590Sstevel@tonic-gate 	splx(s);
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate static void
16630Sstevel@tonic-gate dumpregs(struct regs *rp)
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate #if defined(__amd64)
16660Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
16690Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
16700Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
16710Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
16720Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
16730Sstevel@tonic-gate 
16743446Smrj 	printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE),
16753446Smrj 	    " ds", rp->r_ds);
16760Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
16790Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate #elif defined(__i386)
16840Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
16870Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
16880Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
16890Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
16900Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
16910Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
16920Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
16930Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
16940Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
16950Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate #endif	/* __i386 */
16980Sstevel@tonic-gate }
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate /*
17015084Sjohnlev  * Test to see if the instruction is iret on i386 or iretq on amd64.
17025084Sjohnlev  *
17035084Sjohnlev  * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true
17045084Sjohnlev  * then we are in the context of hypervisor's failsafe handler because it
17055084Sjohnlev  * tried to iret and failed due to a bad selector. See xen_failsafe_callback.
17065084Sjohnlev  */
17075084Sjohnlev static int
17085084Sjohnlev instr_is_iret(caddr_t pc)
17095084Sjohnlev {
17105084Sjohnlev 
17115084Sjohnlev #if defined(__xpv)
17125084Sjohnlev 	extern void nopop_sys_rtt_syscall(void);
17135084Sjohnlev 	return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0);
17145084Sjohnlev 
17155084Sjohnlev #else
17165084Sjohnlev 
17175084Sjohnlev #if defined(__amd64)
17185084Sjohnlev 	static const uint8_t iret_insn[2] = { 0x48, 0xcf };	/* iretq */
17195084Sjohnlev 
17205084Sjohnlev #elif defined(__i386)
17215084Sjohnlev 	static const uint8_t iret_insn[1] = { 0xcf };		/* iret */
17225084Sjohnlev #endif	/* __i386 */
17235084Sjohnlev 	return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0);
17245084Sjohnlev 
17255084Sjohnlev #endif	/* __xpv */
17265084Sjohnlev }
17275084Sjohnlev 
17285084Sjohnlev #if defined(__i386)
17295084Sjohnlev 
17305084Sjohnlev /*
17315084Sjohnlev  * Test to see if the instruction is part of __SEGREGS_POP
17325084Sjohnlev  *
17335084Sjohnlev  * Note carefully the appallingly awful dependency between
17345084Sjohnlev  * the instruction sequence used in __SEGREGS_POP and these
17355084Sjohnlev  * instructions encoded here.
17365084Sjohnlev  */
17375084Sjohnlev static int
17385084Sjohnlev instr_is_segregs_pop(caddr_t pc)
17395084Sjohnlev {
17405084Sjohnlev 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
17415084Sjohnlev 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
17425084Sjohnlev 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
17435084Sjohnlev 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
17445084Sjohnlev 
17455084Sjohnlev 	if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
17465084Sjohnlev 	    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
17475084Sjohnlev 	    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
17485084Sjohnlev 	    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
17495084Sjohnlev 		return (1);
17505084Sjohnlev 
17515084Sjohnlev 	return (0);
17525084Sjohnlev }
17535084Sjohnlev 
17545084Sjohnlev #endif	/* __i386 */
17555084Sjohnlev 
17565084Sjohnlev /*
17575084Sjohnlev  * Test to see if the instruction is part of _sys_rtt.
17585084Sjohnlev  *
17595084Sjohnlev  * Again on the hypervisor if we try to IRET to user land with a bad code
17605084Sjohnlev  * or stack selector we will get vectored through xen_failsafe_callback.
17615084Sjohnlev  * In which case we assume we got here via _sys_rtt since we only allow
17625084Sjohnlev  * IRET to user land to take place in _sys_rtt.
17635084Sjohnlev  */
17645084Sjohnlev static int
17655084Sjohnlev instr_is_sys_rtt(caddr_t pc)
17665084Sjohnlev {
17675084Sjohnlev 	extern void _sys_rtt(), _sys_rtt_end();
17685084Sjohnlev 
17695084Sjohnlev 	if ((uintptr_t)pc < (uintptr_t)_sys_rtt ||
17705084Sjohnlev 	    (uintptr_t)pc > (uintptr_t)_sys_rtt_end)
17715084Sjohnlev 		return (0);
17725084Sjohnlev 
17735084Sjohnlev 	return (1);
17745084Sjohnlev }
17755084Sjohnlev 
17765084Sjohnlev /*
17770Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
17780Sstevel@tonic-gate  *
17790Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
17800Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
17810Sstevel@tonic-gate  *
17820Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
17830Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
17840Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
17850Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
17860Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
17870Sstevel@tonic-gate  *
17880Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
17890Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
17900Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
17910Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
17920Sstevel@tonic-gate  * what we were going to send the lwp.)
17930Sstevel@tonic-gate  *
17940Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
17950Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
17960Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
17970Sstevel@tonic-gate  */
17980Sstevel@tonic-gate static int
17990Sstevel@tonic-gate kern_gpfault(struct regs *rp)
18000Sstevel@tonic-gate {
18010Sstevel@tonic-gate 	kthread_t *t = curthread;
18020Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
18030Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
18040Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
18050Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
18060Sstevel@tonic-gate 	int v;
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	/*
18095084Sjohnlev 	 * if we're not an lwp, or in the case of running native the
18105084Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
18115084Sjohnlev 	 * be die()ing horribly.
18120Sstevel@tonic-gate 	 */
18135084Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
18140Sstevel@tonic-gate 		return (1);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	/*
18170Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
18180Sstevel@tonic-gate 	 *
18190Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
18200Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
18210Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
18220Sstevel@tonic-gate 	 * _sys_rtt. Ew.
18230Sstevel@tonic-gate 	 */
18245084Sjohnlev 	if (instr_is_iret(pc)) {
18250Sstevel@tonic-gate 		/*
18265084Sjohnlev 		 * We took the #gp while trying to perform the IRET.
18270Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
18280Sstevel@tonic-gate 		 * All we know for sure is that most of the general
18290Sstevel@tonic-gate 		 * registers have been restored, including the
18300Sstevel@tonic-gate 		 * segment registers, and all we have left on the
18310Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
18320Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
18330Sstevel@tonic-gate 		 *
18340Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
18350Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
18360Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
18370Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
18380Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
18390Sstevel@tonic-gate 		 */
18400Sstevel@tonic-gate 		trp = &tmpregs;
18410Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
18420Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
18430Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
18440Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
18450Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
18460Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		/*
18490Sstevel@tonic-gate 		 * Validate simple math
18500Sstevel@tonic-gate 		 */
18510Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18520Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
18530Sstevel@tonic-gate 
18545084Sjohnlev 
18555084Sjohnlev 
18565084Sjohnlev 	}
18575084Sjohnlev 
18585084Sjohnlev #if defined(__amd64)
18595084Sjohnlev 	if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) {
18605084Sjohnlev 
18615084Sjohnlev 		/*
18625084Sjohnlev 		 * This is the common case -- we're trying to load
18635084Sjohnlev 		 * a bad segment register value in the only section
18645084Sjohnlev 		 * of kernel code that ever loads segment registers.
18655084Sjohnlev 		 *
18665084Sjohnlev 		 * We don't need to do anything at this point because
18675084Sjohnlev 		 * the pcb contains all the pending segment register
18685084Sjohnlev 		 * state, and the regs are still intact because we
18695084Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
18705084Sjohnlev 		 * of all this, we could conceivably send a signal
18715084Sjohnlev 		 * to the lwp, rather than core-ing.
18725084Sjohnlev 		 */
18735084Sjohnlev 		trp = lwptoregs(lwp);
18745084Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
18750Sstevel@tonic-gate 	}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate #elif defined(__i386)
18780Sstevel@tonic-gate 
18795084Sjohnlev 	if (trp == NULL && instr_is_segregs_pop(pc))
18805084Sjohnlev 		trp = lwptoregs(lwp);
18810Sstevel@tonic-gate 
18825084Sjohnlev #endif	/* __i386 */
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	if (trp == NULL)
18850Sstevel@tonic-gate 		return (1);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	/*
18880Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
18890Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
18900Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
18910Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
18920Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
18930Sstevel@tonic-gate 	 */
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	/*
18960Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
18970Sstevel@tonic-gate 	 */
18980Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
18990Sstevel@tonic-gate 	trp->r_err = rp->r_err;
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
19020Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
19030Sstevel@tonic-gate 
19045084Sjohnlev 
19050Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19060Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
19070Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	/*
19105084Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
19115084Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
19120Sstevel@tonic-gate 	 */
1913390Sraf 	proc_is_exiting(p);
19140Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
19150Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19160Sstevel@tonic-gate 		lwp_exit();
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19200Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
19210Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
19220Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19230Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
19240Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
19250Sstevel@tonic-gate 	return (0);
19260Sstevel@tonic-gate }
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate /*
19290Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
19300Sstevel@tonic-gate  */
19310Sstevel@tonic-gate 
19325084Sjohnlev #if !defined(__xpv)
19330Sstevel@tonic-gate #if defined(__amd64)
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate static void
19360Sstevel@tonic-gate dump_tss(void)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19390Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
19420Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
19430Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
19460Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
19470Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
19480Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
19490Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
19500Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
19510Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate #elif defined(__i386)
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate static void
19570Sstevel@tonic-gate dump_tss(void)
19580Sstevel@tonic-gate {
19590Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19600Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19610Sstevel@tonic-gate 
1962286Sdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
1963286Sdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
1964286Sdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
1965286Sdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
1966286Sdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
1967286Sdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
1968286Sdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
1969286Sdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
1970286Sdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
1971286Sdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
1972286Sdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
1973286Sdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
1974286Sdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
1975286Sdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
1976286Sdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate #endif	/* __amd64 */
19805084Sjohnlev #endif	/* !__xpv */
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate #if defined(TRAPTRACE)
19830Sstevel@tonic-gate 
19845084Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
19855084Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate /*
19880Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
19890Sstevel@tonic-gate  */
19900Sstevel@tonic-gate static void
19910Sstevel@tonic-gate dump_ttrace(void)
19920Sstevel@tonic-gate {
19930Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
19940Sstevel@tonic-gate 	trap_trace_rec_t *rec;
19950Sstevel@tonic-gate 	uintptr_t current;
19960Sstevel@tonic-gate 	int i, j, k;
19970Sstevel@tonic-gate 	int n = NCPU;
19980Sstevel@tonic-gate #if defined(__amd64)
19990Sstevel@tonic-gate 	const char banner[] =
20005084Sjohnlev 	    "\ncpu          address    timestamp "
20015084Sjohnlev 	    "type  vc  handler   pc\n";
20020Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
20030Sstevel@tonic-gate #elif defined(__i386)
20040Sstevel@tonic-gate 	const char banner[] =
20055084Sjohnlev 	    "\ncpu  address     timestamp type  vc  handler   pc\n";
20060Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
20070Sstevel@tonic-gate #endif
20080Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
20090Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (ttrace_nrec == 0)
20120Sstevel@tonic-gate 		return;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	printf(banner);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20170Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
20180Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
20190Sstevel@tonic-gate 			continue;
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
20220Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
20230Sstevel@tonic-gate 			struct sysent	*sys;
20240Sstevel@tonic-gate 			struct autovec	*vec;
20250Sstevel@tonic-gate 			extern struct av_head autovect[];
20260Sstevel@tonic-gate 			int type;
20270Sstevel@tonic-gate 			ulong_t	off;
20280Sstevel@tonic-gate 			char *sym, *stype;
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 			if (current < ttc->ttc_first)
20310Sstevel@tonic-gate 				current =
20320Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 			if (current == NULL)
20350Sstevel@tonic-gate 				continue;
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
20400Sstevel@tonic-gate 				break;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 			switch (rec->ttr_marker) {
20450Sstevel@tonic-gate 			case TT_SYSCALL:
20460Sstevel@tonic-gate 			case TT_SYSENTER:
20470Sstevel@tonic-gate 			case TT_SYSC:
20480Sstevel@tonic-gate 			case TT_SYSC64:
20490Sstevel@tonic-gate #if defined(__amd64)
20500Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
20510Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20520Sstevel@tonic-gate 				case TT_SYSC64:
20530Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
20540Sstevel@tonic-gate 					/*FALLTHROUGH*/
20550Sstevel@tonic-gate #elif defined(__i386)
20560Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
20570Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20580Sstevel@tonic-gate 				case TT_SYSC64:
20590Sstevel@tonic-gate #endif
20600Sstevel@tonic-gate 				case TT_SYSC:
20610Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
20620Sstevel@tonic-gate 					break;
20630Sstevel@tonic-gate 				case TT_SYSCALL:
20640Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
20650Sstevel@tonic-gate 					break;
20660Sstevel@tonic-gate 				case TT_SYSENTER:
20670Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
20680Sstevel@tonic-gate 					break;
20690Sstevel@tonic-gate 				default:
20700Sstevel@tonic-gate 					break;
20710Sstevel@tonic-gate 				}
20720Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
20730Sstevel@tonic-gate 				if (sys != NULL) {
20740Sstevel@tonic-gate 					sym = kobj_getsymname(
20750Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
20760Sstevel@tonic-gate 					    &off);
20770Sstevel@tonic-gate 					if (sym != NULL)
20783446Smrj 						printf(fmt3, sym);
20790Sstevel@tonic-gate 					else
20800Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
20810Sstevel@tonic-gate 				} else {
20823446Smrj 					printf(fmt3, "unknown");
20830Sstevel@tonic-gate 				}
20840Sstevel@tonic-gate 				break;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 			case TT_INTERRUPT:
20870Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
20880Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
20890Sstevel@tonic-gate 				if (vec != NULL) {
20900Sstevel@tonic-gate 					sym = kobj_getsymname(
20910Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
20920Sstevel@tonic-gate 					if (sym != NULL)
20933446Smrj 						printf(fmt3, sym);
20940Sstevel@tonic-gate 					else
20950Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
20960Sstevel@tonic-gate 				} else {
20973446Smrj 					printf(fmt3, "unknown ");
20980Sstevel@tonic-gate 				}
20990Sstevel@tonic-gate 				break;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 			case TT_TRAP:
21023446Smrj 			case TT_EVENT:
21030Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
21040Sstevel@tonic-gate 				printf(fmt2, "trap", type);
21053446Smrj 				if (type < TRAP_TYPES)
21063446Smrj 					printf("     #%s ",
21073446Smrj 					    trap_type_mnemonic[type]);
21083446Smrj 				else
21093446Smrj 					switch (type) {
21103446Smrj 					case T_AST:
21113446Smrj 						printf(fmt3, "ast");
21123446Smrj 						break;
21133446Smrj 					default:
21143446Smrj 						printf(fmt3, "");
21153446Smrj 						break;
21163446Smrj 					}
21173446Smrj 				break;
21183446Smrj 
21193446Smrj 			case TT_XCALL:
21203446Smrj 				printf(fmt2, "xcal",
21213446Smrj 				    rec->ttr_info.xc_entry.xce_marker);
21223446Smrj 				printf(fmt3, "");
21230Sstevel@tonic-gate 				break;
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 			default:
21260Sstevel@tonic-gate 				break;
21270Sstevel@tonic-gate 			}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
21300Sstevel@tonic-gate 			if (sym != NULL)
21310Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
21320Sstevel@tonic-gate 			else
21330Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
21360Sstevel@tonic-gate 				int s;
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
21390Sstevel@tonic-gate 					printf(
21400Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
21410Sstevel@tonic-gate 					    rec->ttr_ipl,
21420Sstevel@tonic-gate 					    rec->ttr_spl,
21430Sstevel@tonic-gate 					    rec->ttr_pri);
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
21480Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 				/*
21510Sstevel@tonic-gate 				 * print out the pc stack that we recorded
21520Sstevel@tonic-gate 				 * at trap time (if any)
21530Sstevel@tonic-gate 				 */
21540Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
21550Sstevel@tonic-gate 					uintptr_t fullpc;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
21580Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
21590Sstevel@tonic-gate 						break;
21600Sstevel@tonic-gate 					}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
21650Sstevel@tonic-gate 					if (sym != NULL)
21660Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
21670Sstevel@tonic-gate 						    sym, off);
21680Sstevel@tonic-gate 					else
21690Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
21700Sstevel@tonic-gate 				}
21710Sstevel@tonic-gate 				printf("\n");
21720Sstevel@tonic-gate 			}
21730Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
21740Sstevel@tonic-gate 		}
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate }
21770Sstevel@tonic-gate 
21783446Smrj /*
21793446Smrj  * Help with constructing traptrace records in C
21803446Smrj  */
21813446Smrj trap_trace_rec_t *
21823446Smrj trap_trace_get_traceptr(uint8_t marker, ulong_t pc, ulong_t sp)
21833446Smrj {
21843446Smrj 	trap_trace_rec_t *ttr;
21853446Smrj 
21863446Smrj 	if (trap_trace_freeze)
21873446Smrj 		ttr = &trap_trace_postmort;
21883446Smrj 	else {
21893446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[CPU->cpu_id];
21903446Smrj 
21913446Smrj 		ttr = (void *)ttc->ttc_next;
21923446Smrj 
21933446Smrj 		if (ttc->ttc_next >= ttc->ttc_limit)
21943446Smrj 			ttc->ttc_next = ttc->ttc_first;
21953446Smrj 		else
21963446Smrj 			ttc->ttc_next += sizeof (trap_trace_rec_t);
21973446Smrj 	}
21983446Smrj 
21993446Smrj 	ttr->ttr_regs.r_sp = sp;
22003446Smrj 	ttr->ttr_regs.r_pc = pc;
22013446Smrj 	ttr->ttr_cr2 = getcr2();
22023446Smrj 	ttr->ttr_curthread = (uintptr_t)curthread;
22033446Smrj 	ttr->ttr_stamp = tsc_read();
22043446Smrj 	ttr->ttr_marker = marker;
22053446Smrj 	return (ttr);
22063446Smrj }
22073446Smrj 
22080Sstevel@tonic-gate #endif	/* TRAPTRACE */
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate void
22115084Sjohnlev panic_showtrap(struct panic_trap_info *tip)
22120Sstevel@tonic-gate {
22130Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate #if defined(TRAPTRACE)
22160Sstevel@tonic-gate 	dump_ttrace();
22175084Sjohnlev #endif
22180Sstevel@tonic-gate 
22195084Sjohnlev #if !defined(__xpv)
22200Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
22210Sstevel@tonic-gate 		dump_tss();
22225084Sjohnlev #endif
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate void
22265084Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
22270Sstevel@tonic-gate {
22280Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
22290Sstevel@tonic-gate }
2230