xref: /onnv-gate/usr/src/uts/i86pc/os/trap.c (revision 5254:38162db71c7d)
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 /*
233446Smrj  * Copyright 2007 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
102*5254Sgavinm #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) {
5640Sstevel@tonic-gate 	default:
5650Sstevel@tonic-gate 		if (type & USER) {
5660Sstevel@tonic-gate 			if (tudebug)
5670Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5680Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5690Sstevel@tonic-gate 			    type & ~USER);
5700Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5710Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
5720Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
5730Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
5740Sstevel@tonic-gate 			fault = FLTILL;
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 		} else {
5770Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5780Sstevel@tonic-gate 			/*NOTREACHED*/
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
5820Sstevel@tonic-gate 		/*
5830Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
584*5254Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
585*5254Sgavinm 		 * via the installed trampoline.
5860Sstevel@tonic-gate 		 */
5873446Smrj 		if ((ct->t_ontrap != NULL) &&
5883446Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
5893446Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
590*5254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
591*5254Sgavinm 			goto cleanup;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
5960Sstevel@tonic-gate 		 * across this. Here we assume that an address
5970Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
5980Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
5990Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
6000Sstevel@tonic-gate 		 * starting and because we know that we always have
6010Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
6020Sstevel@tonic-gate 		 */
6033446Smrj 		lofault = ct->t_lofault;
6043446Smrj 		ct->t_lofault = 0;
6050Sstevel@tonic-gate 
6063446Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6090Sstevel@tonic-gate 			res = pagefault(addr,
6100Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6110Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6120Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6130Sstevel@tonic-gate 			    grow(addr))
6140Sstevel@tonic-gate 				res = 0;
6150Sstevel@tonic-gate 		} else {
6160Sstevel@tonic-gate 			res = pagefault(addr,
6170Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6180Sstevel@tonic-gate 		}
6193446Smrj 		(void) new_mstate(ct, mstate);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/*
6220Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
6230Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6240Sstevel@tonic-gate 		 */
6253446Smrj 		ct->t_lofault = lofault;
6260Sstevel@tonic-gate 		if (res == 0)
6270Sstevel@tonic-gate 			goto cleanup;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6300Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6310Sstevel@tonic-gate 			/*
6320Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6330Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6340Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6350Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6360Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6370Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6380Sstevel@tonic-gate 			 */
6390Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6420Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6430Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6440Sstevel@tonic-gate 				    PFN_INVALID &&
6450Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6460Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6470Sstevel@tonic-gate 					rp->r_pc = rip;
6480Sstevel@tonic-gate 					goto cleanup;
6490Sstevel@tonic-gate 				}
6500Sstevel@tonic-gate 			}
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6550Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6560Sstevel@tonic-gate 			/*
6570Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6580Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6590Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6600Sstevel@tonic-gate 			 * instruction.
6610Sstevel@tonic-gate 			 */
6620Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 			/*
6650Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6660Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6670Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6680Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6690Sstevel@tonic-gate 			 */
6700Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
6710Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
6720Sstevel@tonic-gate #ifdef DEBUG
6730Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
6740Sstevel@tonic-gate 					    "occurred: kernel prefetch"
6750Sstevel@tonic-gate 					    " at %p generated a page fault!",
6760Sstevel@tonic-gate 					    (void *)rp->r_pc);
6770Sstevel@tonic-gate #endif /* DEBUG */
6780Sstevel@tonic-gate 					goto cleanup;
6790Sstevel@tonic-gate 				}
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if (lofault == 0)
6860Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		/*
6890Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
6900Sstevel@tonic-gate 		 */
6910Sstevel@tonic-gate 		if (lodebug) {
6920Sstevel@tonic-gate 			showregs(type, rp, addr);
6930Sstevel@tonic-gate 			traceregs(rp);
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
6960Sstevel@tonic-gate 			res = FC_ERRNO(res);
6970Sstevel@tonic-gate 		else
6980Sstevel@tonic-gate 			res = EFAULT;
6990Sstevel@tonic-gate 		rp->r_r0 = res;
7003446Smrj 		rp->r_pc = ct->t_lofault;
7010Sstevel@tonic-gate 		goto cleanup;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7040Sstevel@tonic-gate 		if (faultdebug) {
7050Sstevel@tonic-gate 			char *fault_str;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 			switch (rw) {
7080Sstevel@tonic-gate 			case S_READ:
7090Sstevel@tonic-gate 				fault_str = "read";
7100Sstevel@tonic-gate 				break;
7110Sstevel@tonic-gate 			case S_WRITE:
7120Sstevel@tonic-gate 				fault_str = "write";
7130Sstevel@tonic-gate 				break;
7140Sstevel@tonic-gate 			case S_EXEC:
7150Sstevel@tonic-gate 				fault_str = "exec";
7160Sstevel@tonic-gate 				break;
7170Sstevel@tonic-gate 			default:
7180Sstevel@tonic-gate 				fault_str = "";
7190Sstevel@tonic-gate 				break;
7200Sstevel@tonic-gate 			}
7210Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7220Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7260Sstevel@tonic-gate 		/*
7270Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7280Sstevel@tonic-gate 		 *
7290Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7300Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7310Sstevel@tonic-gate 		 * address will be
7320Sstevel@tonic-gate 		 *
7330Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7340Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7350Sstevel@tonic-gate 		 *
7360Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7370Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7380Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7390Sstevel@tonic-gate 		 */
7400Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7410Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7420Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7430Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7440Sstevel@tonic-gate 			if (!opteron_erratum_100)
7450Sstevel@tonic-gate 				panic("unexpected erratum #100");
7460Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7470Sstevel@tonic-gate 				goto out;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7520Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7530Sstevel@tonic-gate #ifdef __i386
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
7560Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
7570Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
7580Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
7590Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
7600Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
7610Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
7620Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
7630Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
7640Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
7650Sstevel@tonic-gate 		 */
7660Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
7673939Ssethg 		    rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
7680Sstevel@tonic-gate 			extern void watch_syscall(void);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
7710Sstevel@tonic-gate 			watch_syscall();	/* never returns */
7720Sstevel@tonic-gate 			/* NOTREACHED */
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate #endif /* __i386 */
7750Sstevel@tonic-gate 		vaddr = addr;
7760Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
7770Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
7780Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
7790Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
7800Sstevel@tonic-gate 			if (ta) {
7810Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
7825084Sjohnlev 				    watchcode, rp->r_pc);
7830Sstevel@tonic-gate 				fault_type = F_INVAL;
7840Sstevel@tonic-gate 			} else {
7850Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7860Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
7870Sstevel@tonic-gate 				siginfo.si_code = watchcode;
7880Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
7890Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
7900Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
7910Sstevel@tonic-gate 				fault = FLTWATCH;
7920Sstevel@tonic-gate 				break;
7930Sstevel@tonic-gate 			}
7940Sstevel@tonic-gate 		} else {
7950Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
7960Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
7970Sstevel@tonic-gate 				goto out;
7980Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
7990Sstevel@tonic-gate 			fault_type = F_INVAL;
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		/*
8050Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8060Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8070Sstevel@tonic-gate 		 */
8080Sstevel@tonic-gate 		if (res == 0 ||
8090Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8100Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8110Sstevel@tonic-gate 		    grow(addr))) {
8120Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8130Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8140Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8150Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8160Sstevel@tonic-gate 				siginfo.si_addr = addr;
8170Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8180Sstevel@tonic-gate 			}
8190Sstevel@tonic-gate 			goto out;
8200Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8210Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8220Sstevel@tonic-gate 			report_stack_exec(p, addr);
8230Sstevel@tonic-gate 		}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8280Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8290Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8300Sstevel@tonic-gate 		 *
8310Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8340Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8350Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8360Sstevel@tonic-gate #ifdef DEBUG
8370Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8380Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8390Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8400Sstevel@tonic-gate #endif /* DEBUG */
8410Sstevel@tonic-gate 			goto out;
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		if (tudebug)
8460Sstevel@tonic-gate 			showregs(type, rp, addr);
8470Sstevel@tonic-gate 		/*
8480Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8490Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8500Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8510Sstevel@tonic-gate 		 */
8520Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8530Sstevel@tonic-gate 		siginfo.si_addr = addr;
8540Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8550Sstevel@tonic-gate 		case FC_HWERR:
8560Sstevel@tonic-gate 		case FC_NOSUPPORT:
8570Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8580Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8590Sstevel@tonic-gate 			fault = FLTACCESS;
8600Sstevel@tonic-gate 			break;
8610Sstevel@tonic-gate 		case FC_ALIGN:
8620Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8630Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8640Sstevel@tonic-gate 			fault = FLTACCESS;
8650Sstevel@tonic-gate 			break;
8660Sstevel@tonic-gate 		case FC_OBJERR:
8670Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8680Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8690Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8700Sstevel@tonic-gate 				fault = FLTACCESS;
8710Sstevel@tonic-gate 			}
8720Sstevel@tonic-gate 			break;
8730Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
8740Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8750Sstevel@tonic-gate 			siginfo.si_code =
8760Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
8770Sstevel@tonic-gate 			fault = FLTBOUNDS;
8780Sstevel@tonic-gate 			break;
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 		break;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
8830Sstevel@tonic-gate 		/*
8840Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
8850Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
8860Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
8870Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
8880Sstevel@tonic-gate 		 * be to emulate that particular instruction.
8890Sstevel@tonic-gate 		 */
8900Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
8913939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_ASYSC))
8923939Ssethg 			goto out;
8931363Sfvdl 
8941363Sfvdl #ifdef __amd64
8951363Sfvdl 		/*
8961363Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
8971363Sfvdl 		 * See the instr_is_lsahf function for details.
8981363Sfvdl 		 */
8991363Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
9001363Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
9011363Sfvdl 			emulate_lsahf(rp, instr);
9021363Sfvdl 			goto out;
9031363Sfvdl 		}
9041363Sfvdl #endif
9051363Sfvdl 
9060Sstevel@tonic-gate 		/*FALLTHROUGH*/
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (tudebug)
9090Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9100Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9110Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9120Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9130Sstevel@tonic-gate 		fault = FLTILL;
9140Sstevel@tonic-gate 		break;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9170Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9180Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9190Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9200Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9210Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9220Sstevel@tonic-gate 		fault = FLTIZDIV;
9230Sstevel@tonic-gate 		break;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9260Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9270Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9280Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9290Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9300Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9310Sstevel@tonic-gate 		fault = FLTIOVF;
9320Sstevel@tonic-gate 		break;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9350Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9360Sstevel@tonic-gate 			showregs(type, rp, addr);
9370Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
9380Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9390Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
9400Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9410Sstevel@tonic-gate 			fault = FLTFPE;
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		break;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
9460Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9470Sstevel@tonic-gate 		{
9480Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9490Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
9500Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9510Sstevel@tonic-gate 			type |= USER;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		/*FALLTHROUGH*/
9540Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
9550Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9560Sstevel@tonic-gate 			showregs(type, rp, addr);
9570Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9580Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9590Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9600Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9610Sstevel@tonic-gate 			fault = FLTBOUNDS;
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 		break;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9660Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9670Sstevel@tonic-gate 		{
9680Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9690Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
9700Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9710Sstevel@tonic-gate 			type |= USER;
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 		/*FALLTHROUGH*/
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
9760Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9770Sstevel@tonic-gate 			showregs(type, rp, addr);
9780Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
9790Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9800Sstevel@tonic-gate 			siginfo.si_code  = sicode;
9810Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9820Sstevel@tonic-gate 			fault = FLTFPE;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		break;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
9870Sstevel@tonic-gate 		if (tudebug && tudebugsse)
9880Sstevel@tonic-gate 			showregs(type, rp, addr);
9890Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
9900Sstevel@tonic-gate 			/*
9910Sstevel@tonic-gate 			 * There are rumours that some user instructions
9920Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
9930Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
9940Sstevel@tonic-gate 			 */
9950Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
9960Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
9970Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9980Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
9990Sstevel@tonic-gate 			fault = FLTILL;
10000Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
10010Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10020Sstevel@tonic-gate 			siginfo.si_code = sicode;
10030Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
10040Sstevel@tonic-gate 			fault = FLTFPE;
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 		break;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10090Sstevel@tonic-gate 		/*
10100Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10110Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10120Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10130Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10140Sstevel@tonic-gate 		 * fatal.
10150Sstevel@tonic-gate 		 */
10160Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10170Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10200Sstevel@tonic-gate 		break;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 		/* Now evaluate how we got here */
10250Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
10260Sstevel@tonic-gate 			/*
10270Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
10280Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
10290Sstevel@tonic-gate 			 * the first instruction in privileged mode.
10300Sstevel@tonic-gate 			 *
10310Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
10320Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
10330Sstevel@tonic-gate 			 *
10340Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
10350Sstevel@tonic-gate 			 */
10362712Snn35248 			singlestep_twiddle = 0;
10372712Snn35248 			if (rp->r_pc == (uintptr_t)sys_sysenter ||
10382712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_sysenter) {
10392712Snn35248 				singlestep_twiddle = 1;
10400Sstevel@tonic-gate #if defined(__amd64)
10410Sstevel@tonic-gate 				/*
10422712Snn35248 				 * Since we are already on the kernel's
10432712Snn35248 				 * %gs, on 64-bit systems the sysenter case
10442712Snn35248 				 * needs to adjust the pc to avoid
10452712Snn35248 				 * executing the swapgs instruction at the
10462712Snn35248 				 * top of the handler.
10470Sstevel@tonic-gate 				 */
10482712Snn35248 				if (rp->r_pc == (uintptr_t)sys_sysenter)
10492712Snn35248 					rp->r_pc = (uintptr_t)
10502712Snn35248 					    _sys_sysenter_post_swapgs;
10512712Snn35248 				else
10522712Snn35248 					rp->r_pc = (uintptr_t)
10532712Snn35248 					    _brand_sys_sysenter_post_swapgs;
10540Sstevel@tonic-gate #endif
10552712Snn35248 			}
10562712Snn35248 #if defined(__i386)
10572712Snn35248 			else if (rp->r_pc == (uintptr_t)sys_call ||
10582712Snn35248 			    rp->r_pc == (uintptr_t)brand_sys_call) {
10592712Snn35248 				singlestep_twiddle = 1;
10602712Snn35248 			}
10612712Snn35248 #endif
10623003Sdmick 			else {
10633003Sdmick 				/* not on sysenter/syscall; uregs available */
10643003Sdmick 				if (tudebug && tudebugbpt)
10653003Sdmick 					showregs(type, rp, (caddr_t)0);
10663003Sdmick 			}
10672712Snn35248 			if (singlestep_twiddle) {
10680Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
10690Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
10703446Smrj 				ct->t_post_sys = 1;
10712086Ssudheer 				aston(curthread);
10720Sstevel@tonic-gate 				goto cleanup;
10730Sstevel@tonic-gate 			}
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
10760Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
10770Sstevel@tonic-gate 			debug_enter((char *)NULL);
10780Sstevel@tonic-gate 		else
10790Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10800Sstevel@tonic-gate 		break;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
10830Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
10840Sstevel@tonic-gate 		goto cleanup;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
10870Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
10880Sstevel@tonic-gate 		break;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
10915084Sjohnlev #if defined(__amd64) || defined(__xpv)
10920Sstevel@tonic-gate 		/*
10930Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
10945084Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
10955084Sjohnlev 		 * update_sregs while updating user segment registers.
10965084Sjohnlev 		 *
10975084Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
10985084Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
10995084Sjohnlev 		 * lives which is protected by segmentation.
11000Sstevel@tonic-gate 		 */
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		/*
11030Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
1104*5254Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
11055084Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
11060Sstevel@tonic-gate 		 */
11073446Smrj 		if (ct->t_ontrap != NULL) {
11085084Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
11095084Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
11103446Smrj 
11115084Sjohnlev 			if (ttype != 0) {
11125084Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
11135084Sjohnlev 				if (tudebug)
11145084Sjohnlev 					showregs(type, rp, (caddr_t)0);
1115*5254Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
1116*5254Sgavinm 				goto cleanup;
11175084Sjohnlev 			}
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		/*
11210Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
11220Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
11230Sstevel@tonic-gate 		 */
11243446Smrj 		if (ct->t_lofault) {
11250Sstevel@tonic-gate 			/*
11260Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11270Sstevel@tonic-gate 			 */
11280Sstevel@tonic-gate 			if (lodebug) {
11290Sstevel@tonic-gate 				showregs(type, rp, addr);
11300Sstevel@tonic-gate 				traceregs(rp);
11310Sstevel@tonic-gate 			}
11320Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
11333446Smrj 			rp->r_pc = ct->t_lofault;
11340Sstevel@tonic-gate 			goto cleanup;
11350Sstevel@tonic-gate 		}
11360Sstevel@tonic-gate 		/*FALLTHROUGH*/
11375084Sjohnlev #endif	/* __amd64 || __xpv */
11383446Smrj 	case T_SEGFLT:	/* segment not present fault */
11393446Smrj #if defined(__amd64)
11403446Smrj 		/*
11413446Smrj 		 * One example of this is #NP in update_sregs while
11423446Smrj 		 * attempting to update a user segment register
11433446Smrj 		 * that points to a descriptor that is marked not
11443446Smrj 		 * present.
11453446Smrj 		 */
11463446Smrj 		if (ct->t_ontrap != NULL &&
11473446Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
11483446Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
11495084Sjohnlev 			if (tudebug)
11505084Sjohnlev 				showregs(type, rp, (caddr_t)0);
1151*5254Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
1152*5254Sgavinm 			goto cleanup;
11533446Smrj 		}
11543446Smrj #endif	/* __amd64 */
11553446Smrj 		/*FALLTHROUGH*/
11560Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
11570Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
11580Sstevel@tonic-gate 		if (tudebug)
11590Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11600Sstevel@tonic-gate 		if (kern_gpfault(rp))
11610Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11620Sstevel@tonic-gate 		goto cleanup;
11631217Srab 		/*FALLTHROUGH*/
11640Sstevel@tonic-gate 
11653446Smrj 	/*
11663446Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
11673446Smrj 	 * should have no need for them, so we put a stop to it here.
11683446Smrj 	 *
11693446Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
11703446Smrj 	 * a private LDT trying to do a system call. Emulate it.
11713446Smrj 	 *
11723446Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
11733446Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
11743446Smrj 	 */
11753446Smrj 
11760Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
11771217Srab 	case T_GPFLT + USER:	/* general protection violation */
11780Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11791217Srab 		if (p->p_model != DATAMODEL_NATIVE) {
11801217Srab #endif /* _SYSCALL32_IMPL */
11813939Ssethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
11821217Srab 			if (type == T_SEGFLT + USER)
11831217Srab 				ASSERT(p->p_ldt != NULL);
11841217Srab 
11851217Srab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
11861217Srab 			    type == T_SEGFLT + USER) {
11871217Srab 
11880Sstevel@tonic-gate 			/*
11891217Srab 			 * The user attempted a system call via the obsolete
11901217Srab 			 * call gate mechanism. Because the process doesn't have
11911217Srab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
11921217Srab 			 * Emulate the syscall here, just as we do above for a
11931217Srab 			 * #np trap.
11941217Srab 			 */
11951217Srab 
11961217Srab 			/*
11970Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
11980Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
11990Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
12000Sstevel@tonic-gate 			 */
12010Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
12020Sstevel@tonic-gate 			lwp->lwp_regs = rp;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 			/*
12050Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
12060Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
12070Sstevel@tonic-gate 			 * behavior here.
12080Sstevel@tonic-gate 			 */
12090Sstevel@tonic-gate 			mstate = LMS_USER;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 			dosyscall();
12120Sstevel@tonic-gate 			goto out;
12131217Srab 			}
12141217Srab 		}
12151217Srab #ifdef _SYSCALL32_IMPL
12160Sstevel@tonic-gate 		}
12170Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
12180Sstevel@tonic-gate 		/*
12190Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
12200Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
12210Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
12220Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12230Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12240Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12250Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12260Sstevel@tonic-gate 		 */
12270Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
12283939Ssethg 		    ldt_rewrite_syscall(rp, p, X86_SEP))
12293939Ssethg 			goto out;
12303939Ssethg 
12310Sstevel@tonic-gate 		/*FALLTHROUGH*/
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12340Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12350Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12360Sstevel@tonic-gate 		if (tudebug)
12370Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12380Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12390Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12400Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12410Sstevel@tonic-gate 		fault = FLTBOUNDS;
12420Sstevel@tonic-gate 		break;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12450Sstevel@tonic-gate 		if (tudebug)
12460Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12470Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12480Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12490Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12500Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12510Sstevel@tonic-gate 		fault = FLTACCESS;
12520Sstevel@tonic-gate 		break;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
12550Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12560Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 		/* Was it single-stepping? */
12590Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
12600Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
12630Sstevel@tonic-gate 			/*
12640Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
12652712Snn35248 			 * give precedence to WATCH_STEP.  If neither is set,
12660Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
12670Sstevel@tonic-gate 			 * as NORMAL_STEP.
12680Sstevel@tonic-gate 			 */
12692712Snn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
12702712Snn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
12712712Snn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
12720Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
12730Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
12740Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
12750Sstevel@tonic-gate 				fault = FLTTRACE;
12760Sstevel@tonic-gate 			}
12770Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
12780Sstevel@tonic-gate 		} else {
12790Sstevel@tonic-gate 			cmn_err(CE_WARN,
12800Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
12810Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 		break;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
12860Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12870Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12880Sstevel@tonic-gate 		/*
12890Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
12900Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
12910Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
12920Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
12930Sstevel@tonic-gate 		 */
12940Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
12950Sstevel@tonic-gate 			rp->r_pc--;
12960Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
12970Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
12980Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12990Sstevel@tonic-gate 		fault = FLTBPT;
13000Sstevel@tonic-gate 		break;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	case T_AST:
13030Sstevel@tonic-gate 		/*
13040Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
13050Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
13060Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
13070Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
13080Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
13090Sstevel@tonic-gate 		 * doing the T_AST processing.
13100Sstevel@tonic-gate 		 */
13110Sstevel@tonic-gate 		goto cleanup;
13120Sstevel@tonic-gate 
1313*5254Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
1314*5254Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
1315*5254Sgavinm 			proc_t *p = ttoproc(curthread);
1316*5254Sgavinm 
1317*5254Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
1318*5254Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
1319*5254Sgavinm 			siginfo.si_signo = SIGKILL;
1320*5254Sgavinm 			siginfo.si_code = SI_NOINFO;
1321*5254Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
13220Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
13230Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13240Sstevel@tonic-gate 				/*
13250Sstevel@tonic-gate 				 * Signal performance counter overflow
13260Sstevel@tonic-gate 				 */
13270Sstevel@tonic-gate 				if (tudebug)
13280Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13290Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13300Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13310Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13320Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13330Sstevel@tonic-gate 				fault = FLTCPCOVF;
13340Sstevel@tonic-gate 			}
13350Sstevel@tonic-gate 		}
1336*5254Sgavinm 
13370Sstevel@tonic-gate 		break;
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * We can't get here from a system trap
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	ASSERT(type & USER);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (fault) {
13463506Saf 		/* We took a fault so abort single step. */
13473506Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13480Sstevel@tonic-gate 		/*
13490Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13500Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13510Sstevel@tonic-gate 		 */
13520Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13530Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 		/*
13580Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
13590Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
13600Sstevel@tonic-gate 		 * deliver the associated signal.
13610Sstevel@tonic-gate 		 */
13620Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
13630Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
13640Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
13650Sstevel@tonic-gate 			siginfo.si_signo = 0;
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	if (siginfo.si_signo)
13690Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
13720Sstevel@tonic-gate 		profil_tick(rp->r_pc);
13730Sstevel@tonic-gate 
13743446Smrj 	if (ct->t_astflag | ct->t_sig_check) {
13750Sstevel@tonic-gate 		/*
13760Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
13770Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
13780Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
13790Sstevel@tonic-gate 		 * syscall.
13800Sstevel@tonic-gate 		 */
13813446Smrj 		astoff(ct);
13822086Ssudheer 		/*
13832086Ssudheer 		 * If a single-step trap occurred on a syscall (see above)
13842086Ssudheer 		 * recognize it now.  Do this before checking for signals
13852086Ssudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
13862086Ssudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
13872086Ssudheer 		 */
13882086Ssudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
13892086Ssudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
13902086Ssudheer 
13913446Smrj 		ct->t_sig_check = 0;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
13940Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
13950Sstevel@tonic-gate 			timer_lwpbind();
13960Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
13970Sstevel@tonic-gate 		}
13980Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		/*
14010Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
14020Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
14030Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
14040Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
14050Sstevel@tonic-gate 		 * for the kaio request to complete.
14060Sstevel@tonic-gate 		 */
14070Sstevel@tonic-gate 		if (p->p_aio)
14080Sstevel@tonic-gate 			aio_cleanup(0);
14090Sstevel@tonic-gate 		/*
14100Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
14110Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
14120Sstevel@tonic-gate 		 * sets the AST flag.
14130Sstevel@tonic-gate 		 *
14140Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14150Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14160Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14170Sstevel@tonic-gate 		 */
14180Sstevel@tonic-gate 		if (ISHOLD(p))
14190Sstevel@tonic-gate 			holdlwp();
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 		/*
14220Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14230Sstevel@tonic-gate 		 * set t_astflag afterwards.
14240Sstevel@tonic-gate 		 */
14253446Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14260Sstevel@tonic-gate 			if (issig(FORREAL))
14270Sstevel@tonic-gate 				psig();
14283446Smrj 			ct->t_sig_check = 1;
14290Sstevel@tonic-gate 		}
14300Sstevel@tonic-gate 
14313446Smrj 		if (ct->t_rprof != NULL) {
14320Sstevel@tonic-gate 			realsigprof(0, 0);
14333446Smrj 			ct->t_sig_check = 1;
14340Sstevel@tonic-gate 		}
14351468Smarx 
14361468Smarx 		/*
14371468Smarx 		 * /proc can't enable/disable the trace bit itself
14381468Smarx 		 * because that could race with the call gate used by
14391468Smarx 		 * system calls via "lcall". If that happened, an
14401468Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
14411468Smarx 		 * therefore schedule an AST for the purpose.
14421468Smarx 		 */
14431468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
14441468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
14451468Smarx 			rp->r_ps |= PS_T;
14461468Smarx 		}
14471468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
14481468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
14491468Smarx 			rp->r_ps &= ~PS_T;
14501468Smarx 		}
14510Sstevel@tonic-gate 	}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14540Sstevel@tonic-gate 	ASSERT(type & USER);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if (ISHOLD(p))
14570Sstevel@tonic-gate 		holdlwp();
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	/*
14600Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
14610Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
14620Sstevel@tonic-gate 	 * restore the user-level priority.
14630Sstevel@tonic-gate 	 *
14640Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
14650Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
14660Sstevel@tonic-gate 	 * is set back to LWP_SYS).
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
14690Sstevel@tonic-gate 
14703446Smrj 	if (ct->t_trapret) {
14713446Smrj 		ct->t_trapret = 0;
14723446Smrj 		thread_lock(ct);
14733446Smrj 		CL_TRAPRET(ct);
14743446Smrj 		thread_unlock(ct);
14750Sstevel@tonic-gate 	}
14763792Sakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
14770Sstevel@tonic-gate 		preempt();
14783446Smrj 	(void) new_mstate(ct, mstate);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	/* Kernel probe */
14810Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
14820Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	return;
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate cleanup:	/* system traps end up here */
14870Sstevel@tonic-gate 	ASSERT(!(type & USER));
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
14920Sstevel@tonic-gate  */
14930Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
14960Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
14970Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
14980Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
14990Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
15000Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
15010Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
15020Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
15030Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
15040Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
15050Sstevel@tonic-gate } kpreempt_cnts;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate /*
15080Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
15090Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
15100Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
15110Sstevel@tonic-gate  */
15120Sstevel@tonic-gate void
15130Sstevel@tonic-gate kpreempt(int asyncspl)
15140Sstevel@tonic-gate {
15153446Smrj 	kthread_t *ct = curthread;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
15180Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
15190Sstevel@tonic-gate 		return;
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
15240Sstevel@tonic-gate 	 */
15250Sstevel@tonic-gate 	do {
15263446Smrj 		if (ct->t_preempt) {
15270Sstevel@tonic-gate 			/*
15280Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
15290Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
15300Sstevel@tonic-gate 			 */
15313446Smrj 			if (ct->t_pri < 0)
15320Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
15333446Smrj 			else if (ct->t_flag & T_INTR_THREAD) {
15340Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
15353446Smrj 				if (ct->t_pil == CLOCK_LEVEL)
15360Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
15370Sstevel@tonic-gate 			} else
15380Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
15390Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
15400Sstevel@tonic-gate 			return;
15410Sstevel@tonic-gate 		}
15423446Smrj 		if (ct->t_state != TS_ONPROC ||
15433446Smrj 		    ct->t_disp_queue != CPU->cpu_disp) {
15440Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
15450Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
15460Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
15470Sstevel@tonic-gate 				/* already in swtch(), force another */
15480Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
15490Sstevel@tonic-gate 				siron();
15500Sstevel@tonic-gate 			}
15510Sstevel@tonic-gate 			return;
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
15540Sstevel@tonic-gate 			/*
15550Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
15560Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
15570Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
15580Sstevel@tonic-gate 			 */
15590Sstevel@tonic-gate 			siron();	/* check back later */
15600Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
15610Sstevel@tonic-gate 			return;
15620Sstevel@tonic-gate 		}
15633446Smrj 		if (!interrupts_enabled()) {
15643446Smrj 			/*
15653446Smrj 			 * Can't preempt while running with ints disabled
15663446Smrj 			 */
15673446Smrj 			kpreempt_cnts.kpc_prilevel++;
15683446Smrj 			return;
15693446Smrj 		}
15700Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
15710Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
15720Sstevel@tonic-gate 		else
15730Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
15740Sstevel@tonic-gate 
15753446Smrj 		ct->t_preempt++;
15760Sstevel@tonic-gate 		preempt();
15773446Smrj 		ct->t_preempt--;
15780Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate /*
15820Sstevel@tonic-gate  * Print out debugging info.
15830Sstevel@tonic-gate  */
15840Sstevel@tonic-gate static void
15850Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
15860Sstevel@tonic-gate {
15870Sstevel@tonic-gate 	int s;
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	s = spl7();
15900Sstevel@tonic-gate 	type &= ~USER;
15913446Smrj 	if (PTOU(curproc)->u_comm[0])
15923446Smrj 		printf("%s: ", PTOU(curproc)->u_comm);
15930Sstevel@tonic-gate 	if (type < TRAP_TYPES)
15940Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
15950Sstevel@tonic-gate 	else
15960Sstevel@tonic-gate 		switch (type) {
15970Sstevel@tonic-gate 		case T_SYSCALL:
15980Sstevel@tonic-gate 			printf("Syscall Trap:\n");
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 		case T_AST:
16010Sstevel@tonic-gate 			printf("AST\n");
16020Sstevel@tonic-gate 			break;
16030Sstevel@tonic-gate 		default:
16040Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
16050Sstevel@tonic-gate 			break;
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 	if (type == T_PGFLT) {
16080Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
16090Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
16100Sstevel@tonic-gate 	} else if (addr) {
16110Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
16150Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
16160Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate #if defined(__lint)
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
16210Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
16240Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
16250Sstevel@tonic-gate #else
16260Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
16270Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
16283446Smrj #endif	/* __lint */
16290Sstevel@tonic-gate 
16305084Sjohnlev 	printf("cr2: %lx", getcr2());
16315084Sjohnlev #if !defined(__xpv)
16325084Sjohnlev 	printf("cr3: %lx", getcr3());
16330Sstevel@tonic-gate #if defined(__amd64)
16345084Sjohnlev 	printf("cr8: %lx\n", getcr8());
16350Sstevel@tonic-gate #endif
16365084Sjohnlev #endif
16375084Sjohnlev 	printf("\n");
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	dumpregs(rp);
16400Sstevel@tonic-gate 	splx(s);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate static void
16440Sstevel@tonic-gate dumpregs(struct regs *rp)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate #if defined(__amd64)
16470Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
16500Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
16510Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
16520Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
16530Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
16540Sstevel@tonic-gate 
16553446Smrj 	printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE),
16563446Smrj 	    " ds", rp->r_ds);
16570Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
16600Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate #elif defined(__i386)
16650Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
16680Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
16690Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
16700Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
16710Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
16720Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
16730Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
16740Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
16750Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
16760Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate #endif	/* __i386 */
16790Sstevel@tonic-gate }
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate /*
16825084Sjohnlev  * Test to see if the instruction is iret on i386 or iretq on amd64.
16835084Sjohnlev  *
16845084Sjohnlev  * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true
16855084Sjohnlev  * then we are in the context of hypervisor's failsafe handler because it
16865084Sjohnlev  * tried to iret and failed due to a bad selector. See xen_failsafe_callback.
16875084Sjohnlev  */
16885084Sjohnlev static int
16895084Sjohnlev instr_is_iret(caddr_t pc)
16905084Sjohnlev {
16915084Sjohnlev 
16925084Sjohnlev #if defined(__xpv)
16935084Sjohnlev 	extern void nopop_sys_rtt_syscall(void);
16945084Sjohnlev 	return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0);
16955084Sjohnlev 
16965084Sjohnlev #else
16975084Sjohnlev 
16985084Sjohnlev #if defined(__amd64)
16995084Sjohnlev 	static const uint8_t iret_insn[2] = { 0x48, 0xcf };	/* iretq */
17005084Sjohnlev 
17015084Sjohnlev #elif defined(__i386)
17025084Sjohnlev 	static const uint8_t iret_insn[1] = { 0xcf };		/* iret */
17035084Sjohnlev #endif	/* __i386 */
17045084Sjohnlev 	return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0);
17055084Sjohnlev 
17065084Sjohnlev #endif	/* __xpv */
17075084Sjohnlev }
17085084Sjohnlev 
17095084Sjohnlev #if defined(__i386)
17105084Sjohnlev 
17115084Sjohnlev /*
17125084Sjohnlev  * Test to see if the instruction is part of __SEGREGS_POP
17135084Sjohnlev  *
17145084Sjohnlev  * Note carefully the appallingly awful dependency between
17155084Sjohnlev  * the instruction sequence used in __SEGREGS_POP and these
17165084Sjohnlev  * instructions encoded here.
17175084Sjohnlev  */
17185084Sjohnlev static int
17195084Sjohnlev instr_is_segregs_pop(caddr_t pc)
17205084Sjohnlev {
17215084Sjohnlev 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
17225084Sjohnlev 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
17235084Sjohnlev 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
17245084Sjohnlev 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
17255084Sjohnlev 
17265084Sjohnlev 	if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
17275084Sjohnlev 	    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
17285084Sjohnlev 	    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
17295084Sjohnlev 	    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
17305084Sjohnlev 		return (1);
17315084Sjohnlev 
17325084Sjohnlev 	return (0);
17335084Sjohnlev }
17345084Sjohnlev 
17355084Sjohnlev #endif	/* __i386 */
17365084Sjohnlev 
17375084Sjohnlev /*
17385084Sjohnlev  * Test to see if the instruction is part of _sys_rtt.
17395084Sjohnlev  *
17405084Sjohnlev  * Again on the hypervisor if we try to IRET to user land with a bad code
17415084Sjohnlev  * or stack selector we will get vectored through xen_failsafe_callback.
17425084Sjohnlev  * In which case we assume we got here via _sys_rtt since we only allow
17435084Sjohnlev  * IRET to user land to take place in _sys_rtt.
17445084Sjohnlev  */
17455084Sjohnlev static int
17465084Sjohnlev instr_is_sys_rtt(caddr_t pc)
17475084Sjohnlev {
17485084Sjohnlev 	extern void _sys_rtt(), _sys_rtt_end();
17495084Sjohnlev 
17505084Sjohnlev 	if ((uintptr_t)pc < (uintptr_t)_sys_rtt ||
17515084Sjohnlev 	    (uintptr_t)pc > (uintptr_t)_sys_rtt_end)
17525084Sjohnlev 		return (0);
17535084Sjohnlev 
17545084Sjohnlev 	return (1);
17555084Sjohnlev }
17565084Sjohnlev 
17575084Sjohnlev /*
17580Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
17590Sstevel@tonic-gate  *
17600Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
17610Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
17620Sstevel@tonic-gate  *
17630Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
17640Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
17650Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
17660Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
17670Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
17680Sstevel@tonic-gate  *
17690Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
17700Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
17710Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
17720Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
17730Sstevel@tonic-gate  * what we were going to send the lwp.)
17740Sstevel@tonic-gate  *
17750Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
17760Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
17770Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
17780Sstevel@tonic-gate  */
17790Sstevel@tonic-gate static int
17800Sstevel@tonic-gate kern_gpfault(struct regs *rp)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	kthread_t *t = curthread;
17830Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
17840Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
17850Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
17860Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
17870Sstevel@tonic-gate 	int v;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	/*
17905084Sjohnlev 	 * if we're not an lwp, or in the case of running native the
17915084Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
17925084Sjohnlev 	 * be die()ing horribly.
17930Sstevel@tonic-gate 	 */
17945084Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
17950Sstevel@tonic-gate 		return (1);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	/*
17980Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
17990Sstevel@tonic-gate 	 *
18000Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
18010Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
18020Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
18030Sstevel@tonic-gate 	 * _sys_rtt. Ew.
18040Sstevel@tonic-gate 	 */
18055084Sjohnlev 	if (instr_is_iret(pc)) {
18060Sstevel@tonic-gate 		/*
18075084Sjohnlev 		 * We took the #gp while trying to perform the IRET.
18080Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
18090Sstevel@tonic-gate 		 * All we know for sure is that most of the general
18100Sstevel@tonic-gate 		 * registers have been restored, including the
18110Sstevel@tonic-gate 		 * segment registers, and all we have left on the
18120Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
18130Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
18140Sstevel@tonic-gate 		 *
18150Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
18160Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
18170Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
18180Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
18190Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
18200Sstevel@tonic-gate 		 */
18210Sstevel@tonic-gate 		trp = &tmpregs;
18220Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
18230Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
18240Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
18250Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
18260Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
18270Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 		/*
18300Sstevel@tonic-gate 		 * Validate simple math
18310Sstevel@tonic-gate 		 */
18320Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18330Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
18340Sstevel@tonic-gate 
18355084Sjohnlev 
18365084Sjohnlev 
18375084Sjohnlev 	}
18385084Sjohnlev 
18395084Sjohnlev #if defined(__amd64)
18405084Sjohnlev 	if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) {
18415084Sjohnlev 
18425084Sjohnlev 		/*
18435084Sjohnlev 		 * This is the common case -- we're trying to load
18445084Sjohnlev 		 * a bad segment register value in the only section
18455084Sjohnlev 		 * of kernel code that ever loads segment registers.
18465084Sjohnlev 		 *
18475084Sjohnlev 		 * We don't need to do anything at this point because
18485084Sjohnlev 		 * the pcb contains all the pending segment register
18495084Sjohnlev 		 * state, and the regs are still intact because we
18505084Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
18515084Sjohnlev 		 * of all this, we could conceivably send a signal
18525084Sjohnlev 		 * to the lwp, rather than core-ing.
18535084Sjohnlev 		 */
18545084Sjohnlev 		trp = lwptoregs(lwp);
18555084Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate #elif defined(__i386)
18590Sstevel@tonic-gate 
18605084Sjohnlev 	if (trp == NULL && instr_is_segregs_pop(pc))
18615084Sjohnlev 		trp = lwptoregs(lwp);
18620Sstevel@tonic-gate 
18635084Sjohnlev #endif	/* __i386 */
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	if (trp == NULL)
18660Sstevel@tonic-gate 		return (1);
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	/*
18690Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
18700Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
18710Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
18720Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
18730Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
18740Sstevel@tonic-gate 	 */
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	/*
18770Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
18780Sstevel@tonic-gate 	 */
18790Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
18800Sstevel@tonic-gate 	trp->r_err = rp->r_err;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
18830Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
18840Sstevel@tonic-gate 
18855084Sjohnlev 
18860Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
18870Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
18880Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	/*
18915084Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
18925084Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
18930Sstevel@tonic-gate 	 */
1894390Sraf 	proc_is_exiting(p);
18950Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
18960Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
18970Sstevel@tonic-gate 		lwp_exit();
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate #ifdef C2_AUDIT
19010Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19020Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
19030Sstevel@tonic-gate #endif
19040Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
19050Sstevel@tonic-gate #ifdef C2_AUDIT
19060Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19070Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
19080Sstevel@tonic-gate #endif
19090Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
19100Sstevel@tonic-gate 	return (0);
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate /*
19140Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
19150Sstevel@tonic-gate  */
19160Sstevel@tonic-gate 
19175084Sjohnlev #if !defined(__xpv)
19180Sstevel@tonic-gate #if defined(__amd64)
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate static void
19210Sstevel@tonic-gate dump_tss(void)
19220Sstevel@tonic-gate {
19230Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19240Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
19270Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
19280Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
19310Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
19320Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
19330Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
19340Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
19350Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
19360Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
19370Sstevel@tonic-gate }
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate #elif defined(__i386)
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate static void
19420Sstevel@tonic-gate dump_tss(void)
19430Sstevel@tonic-gate {
19440Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19450Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19460Sstevel@tonic-gate 
1947286Sdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
1948286Sdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
1949286Sdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
1950286Sdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
1951286Sdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
1952286Sdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
1953286Sdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
1954286Sdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
1955286Sdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
1956286Sdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
1957286Sdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
1958286Sdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
1959286Sdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
1960286Sdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
1961286Sdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate #endif	/* __amd64 */
19655084Sjohnlev #endif	/* !__xpv */
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate #if defined(TRAPTRACE)
19680Sstevel@tonic-gate 
19695084Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
19705084Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate /*
19730Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
19740Sstevel@tonic-gate  */
19750Sstevel@tonic-gate static void
19760Sstevel@tonic-gate dump_ttrace(void)
19770Sstevel@tonic-gate {
19780Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
19790Sstevel@tonic-gate 	trap_trace_rec_t *rec;
19800Sstevel@tonic-gate 	uintptr_t current;
19810Sstevel@tonic-gate 	int i, j, k;
19820Sstevel@tonic-gate 	int n = NCPU;
19830Sstevel@tonic-gate #if defined(__amd64)
19840Sstevel@tonic-gate 	const char banner[] =
19855084Sjohnlev 	    "\ncpu          address    timestamp "
19865084Sjohnlev 	    "type  vc  handler   pc\n";
19870Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
19880Sstevel@tonic-gate #elif defined(__i386)
19890Sstevel@tonic-gate 	const char banner[] =
19905084Sjohnlev 	    "\ncpu  address     timestamp type  vc  handler   pc\n";
19910Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
19920Sstevel@tonic-gate #endif
19930Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
19940Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	if (ttrace_nrec == 0)
19970Sstevel@tonic-gate 		return;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	printf(banner);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20020Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
20030Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
20040Sstevel@tonic-gate 			continue;
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
20070Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
20080Sstevel@tonic-gate 			struct sysent	*sys;
20090Sstevel@tonic-gate 			struct autovec	*vec;
20100Sstevel@tonic-gate 			extern struct av_head autovect[];
20110Sstevel@tonic-gate 			int type;
20120Sstevel@tonic-gate 			ulong_t	off;
20130Sstevel@tonic-gate 			char *sym, *stype;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 			if (current < ttc->ttc_first)
20160Sstevel@tonic-gate 				current =
20170Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 			if (current == NULL)
20200Sstevel@tonic-gate 				continue;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
20250Sstevel@tonic-gate 				break;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 			switch (rec->ttr_marker) {
20300Sstevel@tonic-gate 			case TT_SYSCALL:
20310Sstevel@tonic-gate 			case TT_SYSENTER:
20320Sstevel@tonic-gate 			case TT_SYSC:
20330Sstevel@tonic-gate 			case TT_SYSC64:
20340Sstevel@tonic-gate #if defined(__amd64)
20350Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
20360Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20370Sstevel@tonic-gate 				case TT_SYSC64:
20380Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
20390Sstevel@tonic-gate 					/*FALLTHROUGH*/
20400Sstevel@tonic-gate #elif defined(__i386)
20410Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
20420Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20430Sstevel@tonic-gate 				case TT_SYSC64:
20440Sstevel@tonic-gate #endif
20450Sstevel@tonic-gate 				case TT_SYSC:
20460Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
20470Sstevel@tonic-gate 					break;
20480Sstevel@tonic-gate 				case TT_SYSCALL:
20490Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
20500Sstevel@tonic-gate 					break;
20510Sstevel@tonic-gate 				case TT_SYSENTER:
20520Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
20530Sstevel@tonic-gate 					break;
20540Sstevel@tonic-gate 				default:
20550Sstevel@tonic-gate 					break;
20560Sstevel@tonic-gate 				}
20570Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
20580Sstevel@tonic-gate 				if (sys != NULL) {
20590Sstevel@tonic-gate 					sym = kobj_getsymname(
20600Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
20610Sstevel@tonic-gate 					    &off);
20620Sstevel@tonic-gate 					if (sym != NULL)
20633446Smrj 						printf(fmt3, sym);
20640Sstevel@tonic-gate 					else
20650Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
20660Sstevel@tonic-gate 				} else {
20673446Smrj 					printf(fmt3, "unknown");
20680Sstevel@tonic-gate 				}
20690Sstevel@tonic-gate 				break;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 			case TT_INTERRUPT:
20720Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
20730Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
20740Sstevel@tonic-gate 				if (vec != NULL) {
20750Sstevel@tonic-gate 					sym = kobj_getsymname(
20760Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
20770Sstevel@tonic-gate 					if (sym != NULL)
20783446Smrj 						printf(fmt3, sym);
20790Sstevel@tonic-gate 					else
20800Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
20810Sstevel@tonic-gate 				} else {
20823446Smrj 					printf(fmt3, "unknown ");
20830Sstevel@tonic-gate 				}
20840Sstevel@tonic-gate 				break;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 			case TT_TRAP:
20873446Smrj 			case TT_EVENT:
20880Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
20890Sstevel@tonic-gate 				printf(fmt2, "trap", type);
20903446Smrj 				if (type < TRAP_TYPES)
20913446Smrj 					printf("     #%s ",
20923446Smrj 					    trap_type_mnemonic[type]);
20933446Smrj 				else
20943446Smrj 					switch (type) {
20953446Smrj 					case T_AST:
20963446Smrj 						printf(fmt3, "ast");
20973446Smrj 						break;
20983446Smrj 					default:
20993446Smrj 						printf(fmt3, "");
21003446Smrj 						break;
21013446Smrj 					}
21023446Smrj 				break;
21033446Smrj 
21043446Smrj 			case TT_XCALL:
21053446Smrj 				printf(fmt2, "xcal",
21063446Smrj 				    rec->ttr_info.xc_entry.xce_marker);
21073446Smrj 				printf(fmt3, "");
21080Sstevel@tonic-gate 				break;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 			default:
21110Sstevel@tonic-gate 				break;
21120Sstevel@tonic-gate 			}
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
21150Sstevel@tonic-gate 			if (sym != NULL)
21160Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
21170Sstevel@tonic-gate 			else
21180Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
21210Sstevel@tonic-gate 				int s;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
21240Sstevel@tonic-gate 					printf(
21250Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
21260Sstevel@tonic-gate 					    rec->ttr_ipl,
21270Sstevel@tonic-gate 					    rec->ttr_spl,
21280Sstevel@tonic-gate 					    rec->ttr_pri);
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
21330Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 				/*
21360Sstevel@tonic-gate 				 * print out the pc stack that we recorded
21370Sstevel@tonic-gate 				 * at trap time (if any)
21380Sstevel@tonic-gate 				 */
21390Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
21400Sstevel@tonic-gate 					uintptr_t fullpc;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
21430Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
21440Sstevel@tonic-gate 						break;
21450Sstevel@tonic-gate 					}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
21500Sstevel@tonic-gate 					if (sym != NULL)
21510Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
21520Sstevel@tonic-gate 						    sym, off);
21530Sstevel@tonic-gate 					else
21540Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
21550Sstevel@tonic-gate 				}
21560Sstevel@tonic-gate 				printf("\n");
21570Sstevel@tonic-gate 			}
21580Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
21590Sstevel@tonic-gate 		}
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate }
21620Sstevel@tonic-gate 
21633446Smrj /*
21643446Smrj  * Help with constructing traptrace records in C
21653446Smrj  */
21663446Smrj trap_trace_rec_t *
21673446Smrj trap_trace_get_traceptr(uint8_t marker, ulong_t pc, ulong_t sp)
21683446Smrj {
21693446Smrj 	trap_trace_rec_t *ttr;
21703446Smrj 
21713446Smrj 	if (trap_trace_freeze)
21723446Smrj 		ttr = &trap_trace_postmort;
21733446Smrj 	else {
21743446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[CPU->cpu_id];
21753446Smrj 
21763446Smrj 		ttr = (void *)ttc->ttc_next;
21773446Smrj 
21783446Smrj 		if (ttc->ttc_next >= ttc->ttc_limit)
21793446Smrj 			ttc->ttc_next = ttc->ttc_first;
21803446Smrj 		else
21813446Smrj 			ttc->ttc_next += sizeof (trap_trace_rec_t);
21823446Smrj 	}
21833446Smrj 
21843446Smrj 	ttr->ttr_regs.r_sp = sp;
21853446Smrj 	ttr->ttr_regs.r_pc = pc;
21863446Smrj 	ttr->ttr_cr2 = getcr2();
21873446Smrj 	ttr->ttr_curthread = (uintptr_t)curthread;
21883446Smrj 	ttr->ttr_stamp = tsc_read();
21893446Smrj 	ttr->ttr_marker = marker;
21903446Smrj 	return (ttr);
21913446Smrj }
21923446Smrj 
21930Sstevel@tonic-gate #endif	/* TRAPTRACE */
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate void
21965084Sjohnlev panic_showtrap(struct panic_trap_info *tip)
21970Sstevel@tonic-gate {
21980Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate #if defined(TRAPTRACE)
22010Sstevel@tonic-gate 	dump_ttrace();
22025084Sjohnlev #endif
22030Sstevel@tonic-gate 
22045084Sjohnlev #if !defined(__xpv)
22050Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
22060Sstevel@tonic-gate 		dump_tss();
22075084Sjohnlev #endif
22080Sstevel@tonic-gate }
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate void
22115084Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
22120Sstevel@tonic-gate {
22130Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
22140Sstevel@tonic-gate }
2215