xref: /onnv-gate/usr/src/uts/i86pc/os/trap.c (revision 1468:02c8da23a4d8)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22390Sraf 
230Sstevel@tonic-gate /*
241217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
290Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
300Sstevel@tonic-gate /*		All Rights Reserved   				*/
310Sstevel@tonic-gate /*								*/
320Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
330Sstevel@tonic-gate /*		All Rights Reserved   				*/
340Sstevel@tonic-gate /*								*/
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/signal.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/disp.h>
460Sstevel@tonic-gate #include <sys/class.h>
470Sstevel@tonic-gate #include <sys/core.h>
480Sstevel@tonic-gate #include <sys/syscall.h>
490Sstevel@tonic-gate #include <sys/cpuvar.h>
500Sstevel@tonic-gate #include <sys/vm.h>
510Sstevel@tonic-gate #include <sys/sysinfo.h>
520Sstevel@tonic-gate #include <sys/fault.h>
530Sstevel@tonic-gate #include <sys/stack.h>
540Sstevel@tonic-gate #include <sys/mmu.h>
550Sstevel@tonic-gate #include <sys/psw.h>
560Sstevel@tonic-gate #include <sys/regset.h>
570Sstevel@tonic-gate #include <sys/fp.h>
580Sstevel@tonic-gate #include <sys/trap.h>
590Sstevel@tonic-gate #include <sys/kmem.h>
600Sstevel@tonic-gate #include <sys/vtrace.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/prsystm.h>
630Sstevel@tonic-gate #include <sys/mutex_impl.h>
640Sstevel@tonic-gate #include <sys/machsystm.h>
650Sstevel@tonic-gate #include <sys/archsystm.h>
660Sstevel@tonic-gate #include <sys/sdt.h>
670Sstevel@tonic-gate #include <sys/avintr.h>
680Sstevel@tonic-gate #include <sys/kobj.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <vm/hat.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <vm/seg_kmem.h>
730Sstevel@tonic-gate #include <vm/as.h>
740Sstevel@tonic-gate #include <vm/seg.h>
750Sstevel@tonic-gate #include <vm/hat_pte.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #include <sys/procfs.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include <sys/reboot.h>
800Sstevel@tonic-gate #include <sys/debug.h>
810Sstevel@tonic-gate #include <sys/debugreg.h>
820Sstevel@tonic-gate #include <sys/modctl.h>
830Sstevel@tonic-gate #include <sys/aio_impl.h>
840Sstevel@tonic-gate #include <sys/tnf.h>
850Sstevel@tonic-gate #include <sys/tnf_probe.h>
860Sstevel@tonic-gate #include <sys/cred.h>
870Sstevel@tonic-gate #include <sys/mman.h>
880Sstevel@tonic-gate #include <sys/x86_archext.h>
890Sstevel@tonic-gate #include <sys/copyops.h>
900Sstevel@tonic-gate #include <c2/audit.h>
910Sstevel@tonic-gate #include <sys/ftrace.h>
920Sstevel@tonic-gate #include <sys/panic.h>
930Sstevel@tonic-gate #include <sys/traptrace.h>
940Sstevel@tonic-gate #include <sys/ontrap.h>
950Sstevel@tonic-gate #include <sys/cpc_impl.h>
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1000Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1010Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1020Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1030Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1040Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static const char *trap_type[] = {
1080Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
1090Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1100Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1110Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
1120Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
1130Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
1140Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
1150Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
1160Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
1170Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
1180Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
1190Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
1200Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
1210Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
1220Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
1230Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
1240Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
1250Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
1260Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1270Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate int tudebug = 0;
1330Sstevel@tonic-gate int tudebugbpt = 0;
1340Sstevel@tonic-gate int tudebugfpe = 0;
1350Sstevel@tonic-gate int tudebugsse = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1380Sstevel@tonic-gate int tdebug = 0;
1390Sstevel@tonic-gate int lodebug = 0;
1400Sstevel@tonic-gate int faultdebug = 0;
1410Sstevel@tonic-gate #else
1420Sstevel@tonic-gate #define	tdebug	0
1430Sstevel@tonic-gate #define	lodebug	0
1440Sstevel@tonic-gate #define	faultdebug	0
1450Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #if defined(TRAPTRACE)
1480Sstevel@tonic-gate static void dump_ttrace(void);
1490Sstevel@tonic-gate #endif	/* TRAPTRACE */
1500Sstevel@tonic-gate static void dumpregs(struct regs *);
1510Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1520Sstevel@tonic-gate static void dump_tss(void);
1530Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate struct trap_info {
1560Sstevel@tonic-gate 	struct regs *trap_regs;
1570Sstevel@tonic-gate 	uint_t trap_type;
1580Sstevel@tonic-gate 	caddr_t trap_addr;
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*ARGSUSED*/
1620Sstevel@tonic-gate static int
1630Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	struct trap_info ti;
1660Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1690Sstevel@tonic-gate 		trap_name = trap_type[type];
1700Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1710Sstevel@tonic-gate 	} else {
1720Sstevel@tonic-gate 		trap_name = "trap";
1730Sstevel@tonic-gate 		trap_mnemonic = "-";
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate #ifdef TRAPTRACE
1770Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	ti.trap_regs = rp;
1810Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
1820Sstevel@tonic-gate 	ti.trap_addr = addr;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
1870Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
1880Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
1890Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
1900Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
1910Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
1920Sstevel@tonic-gate 		    "a NULL pointer dereference" :
1930Sstevel@tonic-gate 		    "an illegal access to a user address");
1940Sstevel@tonic-gate 	} else
1950Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
1960Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate #define	SLOW_SCALL_SIZE	2
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static int
2090Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2140Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2150Sstevel@tonic-gate 		return (1);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2220Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2230Sstevel@tonic-gate  * instruction.
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2260Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #define	FAST_SCALL_SIZE	2
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static int
2320Sstevel@tonic-gate instr_is_fast_syscall(caddr_t pc, int which)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	ASSERT(which == X86_SEP || which == X86_ASYSC);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0 ||
2390Sstevel@tonic-gate 	    instr[0] != 0x0F)
2400Sstevel@tonic-gate 		return (0);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if ((which == X86_SEP && instr[1] == 0x34) ||
2430Sstevel@tonic-gate 	    (which == X86_ASYSC && instr[1] == 0x05))
2440Sstevel@tonic-gate 		return (1);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	return (0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
2530Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
2540Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate #define	LCALLSIZE	7
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static int
2600Sstevel@tonic-gate instr_is_syscall(caddr_t pc)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
2650Sstevel@tonic-gate 	    instr[0] == 0x9a &&
2660Sstevel@tonic-gate 	    instr[1] == 0 &&
2670Sstevel@tonic-gate 	    instr[2] == 0 &&
2680Sstevel@tonic-gate 	    instr[3] == 0 &&
2690Sstevel@tonic-gate 	    instr[4] == 0 &&
2700Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
2710Sstevel@tonic-gate 	    instr[6] == 0)
2720Sstevel@tonic-gate 		return (1);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2771363Sfvdl #ifdef __amd64
2781363Sfvdl 
2791363Sfvdl /*
2801363Sfvdl  * In the first revisions of AMD64 CPUs produced by AMD, the LAHF and
2811363Sfvdl  * SAHF instructions were not implemented in 64bit mode. Later revisions
2821363Sfvdl  * did implement these instructions. An extension to the cpuid instruction
2831363Sfvdl  * was added to check for the capability of executing these instructions
2841363Sfvdl  * in 64bit mode.
2851363Sfvdl  *
2861363Sfvdl  * Intel originally did not implement these instructions in EM64T either,
2871363Sfvdl  * but added them in later revisions.
2881363Sfvdl  *
2891363Sfvdl  * So, there are different chip revisions by both vendors out there that
2901363Sfvdl  * may or may not implement these instructions. The easy solution is to
2911363Sfvdl  * just always emulate these instructions on demand.
2921363Sfvdl  *
2931363Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
2941363Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
2951363Sfvdl  */
2961363Sfvdl 
2971363Sfvdl #define	LSAHFSIZE 1
2981363Sfvdl 
2991363Sfvdl static int
3001363Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
3011363Sfvdl {
3021363Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
3031363Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
3041363Sfvdl 		return (1);
3051363Sfvdl 	return (0);
3061363Sfvdl }
3071363Sfvdl 
3081363Sfvdl /*
3091363Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
3101363Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
3111363Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
3121363Sfvdl  *
3131363Sfvdl  * Note that %ah is bits 8-15 of %rax.
3141363Sfvdl  */
3151363Sfvdl static void
3161363Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
3171363Sfvdl {
3181363Sfvdl 	if (instr == 0x9e) {
3191363Sfvdl 		/* sahf. Copy bits from %ah to flags. */
3201363Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
3211363Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
3221363Sfvdl 	} else {
3231363Sfvdl 		/* lahf. Copy bits from flags to %ah. */
3241363Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
3251363Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
3261363Sfvdl 	}
3271363Sfvdl 	rp->r_pc += LSAHFSIZE;
3281363Sfvdl }
3291363Sfvdl #endif /* __amd64 */
3301363Sfvdl 
3310Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
3370Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
3380Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate #define	PREFETCHSIZE 3
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static int
3440Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
3470Sstevel@tonic-gate 		return (1);
3480Sstevel@tonic-gate 	return (0);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate static int
3520Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
3550Sstevel@tonic-gate 	int	error;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
3600Sstevel@tonic-gate 		return (1);
3610Sstevel@tonic-gate 	return (0);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
3680Sstevel@tonic-gate  *
3690Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
3700Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate void
3730Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
3760Sstevel@tonic-gate 	enum seg_rw rw;
3770Sstevel@tonic-gate 	unsigned type;
3780Sstevel@tonic-gate 	extern int stop_on_fault(uint_t, k_siginfo_t *);
3790Sstevel@tonic-gate 	proc_t *p = ttoproc(cur_thread);
3800Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(cur_thread);
3810Sstevel@tonic-gate 	uintptr_t lofault;
3820Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
3830Sstevel@tonic-gate 	enum fault_type fault_type;
3840Sstevel@tonic-gate 	k_siginfo_t siginfo;
3850Sstevel@tonic-gate 	uint_t fault = 0;
3860Sstevel@tonic-gate 	int mstate;
3870Sstevel@tonic-gate 	int sicode = 0;
3880Sstevel@tonic-gate 	int watchcode;
3890Sstevel@tonic-gate 	int watchpage;
3900Sstevel@tonic-gate 	caddr_t vaddr;
3910Sstevel@tonic-gate 	size_t sz;
3920Sstevel@tonic-gate 	int ta;
3931363Sfvdl #ifdef __amd64
3941363Sfvdl 	uchar_t instr;
3951363Sfvdl #endif
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	type = rp->r_trapno;
4000Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	ASSERT(cur_thread->t_schedflag & TS_DONT_SWAP);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (type == T_PGFLT) {
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		errcode = rp->r_err;
4070Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
4080Sstevel@tonic-gate 			rw = S_WRITE;
4090Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
4100Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
4110Sstevel@tonic-gate 			rw = S_EXEC;
4120Sstevel@tonic-gate 		else
4130Sstevel@tonic-gate 			rw = S_READ;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate #if defined(__i386)
4160Sstevel@tonic-gate 		/*
4170Sstevel@tonic-gate 		 * Pentium Pro work-around
4180Sstevel@tonic-gate 		 */
4190Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
4200Sstevel@tonic-gate 			uint_t	attr;
4210Sstevel@tonic-gate 			uint_t	priv_violation;
4220Sstevel@tonic-gate 			uint_t	access_violation;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
4250Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
4260Sstevel@tonic-gate 			    == -1) {
4270Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
4280Sstevel@tonic-gate 			} else {
4290Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
4300Sstevel@tonic-gate 					!(attr & PROT_USER);
4310Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
4320Sstevel@tonic-gate 					!(attr & PROT_WRITE);
4330Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
4340Sstevel@tonic-gate 					goto cleanup;
4350Sstevel@tonic-gate 			}
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate #endif /* __i386 */
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (tdebug)
4420Sstevel@tonic-gate 		showregs(type, rp, addr);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
4450Sstevel@tonic-gate 		/*
4460Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
4470Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
4480Sstevel@tonic-gate 		 * The current process credential applies to the thread for
4490Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
4500Sstevel@tonic-gate 		 * should already be set up.
4510Sstevel@tonic-gate 		 */
4520Sstevel@tonic-gate 		if (cur_thread->t_cred != p->p_cred) {
4530Sstevel@tonic-gate 			cred_t *oldcred = cur_thread->t_cred;
4540Sstevel@tonic-gate 			/*
4550Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
4560Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
4570Sstevel@tonic-gate 			 * allocated cred structure.
4580Sstevel@tonic-gate 			 */
4590Sstevel@tonic-gate 			cur_thread->t_cred = crgetcred();
4600Sstevel@tonic-gate 			crfree(oldcred);
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 		ASSERT(lwp != NULL);
4630Sstevel@tonic-gate 		type |= USER;
4640Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
4650Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		switch (type) {
4680Sstevel@tonic-gate 		case T_PGFLT + USER:
4690Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
4700Sstevel@tonic-gate 				mstate = LMS_TFAULT;
4710Sstevel@tonic-gate 			else
4720Sstevel@tonic-gate 				mstate = LMS_DFAULT;
4730Sstevel@tonic-gate 			break;
4740Sstevel@tonic-gate 		default:
4750Sstevel@tonic-gate 			mstate = LMS_TRAP;
4760Sstevel@tonic-gate 			break;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 		/* Kernel probe */
4790Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
4800Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
4810Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, mstate);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	switch (type) {
4870Sstevel@tonic-gate 	case T_PGFLT + USER:
4880Sstevel@tonic-gate 	case T_SGLSTP:
4890Sstevel@tonic-gate 	case T_SGLSTP + USER:
4900Sstevel@tonic-gate 	case T_BPTFLT + USER:
4910Sstevel@tonic-gate 		break;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	default:
4940Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
4950Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
4960Sstevel@tonic-gate 		break;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	switch (type) {
5000Sstevel@tonic-gate 	default:
5010Sstevel@tonic-gate 		if (type & USER) {
5020Sstevel@tonic-gate 			if (tudebug)
5030Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5040Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5050Sstevel@tonic-gate 			    type & ~USER);
5060Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5070Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
5080Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
5090Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
5100Sstevel@tonic-gate 			fault = FLTILL;
5110Sstevel@tonic-gate 			break;
5120Sstevel@tonic-gate 		} else {
5130Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5140Sstevel@tonic-gate 			/*NOTREACHED*/
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
5180Sstevel@tonic-gate 		/*
5190Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
5200Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
5210Sstevel@tonic-gate 		 */
5220Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
5230Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
5240Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
5250Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 		/*
5290Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
5300Sstevel@tonic-gate 		 * across this. Here we assume that an address
5310Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
5320Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
5330Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
5340Sstevel@tonic-gate 		 * starting and because we know that we always have
5350Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
5360Sstevel@tonic-gate 		 */
5370Sstevel@tonic-gate 		lofault = cur_thread->t_lofault;
5380Sstevel@tonic-gate 		cur_thread->t_lofault = 0;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, LMS_KFAULT);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
5430Sstevel@tonic-gate 			res = pagefault(addr,
5440Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
5450Sstevel@tonic-gate 			if (res == FC_NOMAP &&
5460Sstevel@tonic-gate 			    addr < p->p_usrstack &&
5470Sstevel@tonic-gate 			    grow(addr))
5480Sstevel@tonic-gate 				res = 0;
5490Sstevel@tonic-gate 		} else {
5500Sstevel@tonic-gate 			res = pagefault(addr,
5510Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 		(void) new_mstate(cur_thread, mstate);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 		/*
5560Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
5570Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
5580Sstevel@tonic-gate 		 */
5590Sstevel@tonic-gate 		cur_thread->t_lofault = lofault;
5600Sstevel@tonic-gate 		if (res == 0)
5610Sstevel@tonic-gate 			goto cleanup;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
5640Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
5650Sstevel@tonic-gate 			/*
5660Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
5670Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
5680Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
5690Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
5700Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
5710Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
5720Sstevel@tonic-gate 			 */
5730Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
5760Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
5770Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
5780Sstevel@tonic-gate 				    PFN_INVALID &&
5790Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
5800Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
5810Sstevel@tonic-gate 					rp->r_pc = rip;
5820Sstevel@tonic-gate 					goto cleanup;
5830Sstevel@tonic-gate 				}
5840Sstevel@tonic-gate 			}
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
5890Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
5900Sstevel@tonic-gate 			/*
5910Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
5920Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
5930Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
5940Sstevel@tonic-gate 			 * instruction.
5950Sstevel@tonic-gate 			 */
5960Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 			/*
5990Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6000Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6010Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6020Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6030Sstevel@tonic-gate 			 */
6040Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
6050Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
6060Sstevel@tonic-gate #ifdef DEBUG
6070Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
6080Sstevel@tonic-gate 					    "occurred: kernel prefetch"
6090Sstevel@tonic-gate 					    " at %p generated a page fault!",
6100Sstevel@tonic-gate 					    (void *)rp->r_pc);
6110Sstevel@tonic-gate #endif /* DEBUG */
6120Sstevel@tonic-gate 					goto cleanup;
6130Sstevel@tonic-gate 				}
6140Sstevel@tonic-gate 			}
6150Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		if (lofault == 0)
6200Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		/*
6230Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
6240Sstevel@tonic-gate 		 */
6250Sstevel@tonic-gate 		if (lodebug) {
6260Sstevel@tonic-gate 			showregs(type, rp, addr);
6270Sstevel@tonic-gate 			traceregs(rp);
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
6300Sstevel@tonic-gate 			res = FC_ERRNO(res);
6310Sstevel@tonic-gate 		else
6320Sstevel@tonic-gate 			res = EFAULT;
6330Sstevel@tonic-gate 		rp->r_r0 = res;
6340Sstevel@tonic-gate 		rp->r_pc = cur_thread->t_lofault;
6350Sstevel@tonic-gate 		goto cleanup;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
6380Sstevel@tonic-gate 		if (faultdebug) {
6390Sstevel@tonic-gate 			char *fault_str;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 			switch (rw) {
6420Sstevel@tonic-gate 			case S_READ:
6430Sstevel@tonic-gate 				fault_str = "read";
6440Sstevel@tonic-gate 				break;
6450Sstevel@tonic-gate 			case S_WRITE:
6460Sstevel@tonic-gate 				fault_str = "write";
6470Sstevel@tonic-gate 				break;
6480Sstevel@tonic-gate 			case S_EXEC:
6490Sstevel@tonic-gate 				fault_str = "exec";
6500Sstevel@tonic-gate 				break;
6510Sstevel@tonic-gate 			default:
6520Sstevel@tonic-gate 				fault_str = "";
6530Sstevel@tonic-gate 				break;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
6560Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
6600Sstevel@tonic-gate 		/*
6610Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
6620Sstevel@tonic-gate 		 *
6630Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
6640Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
6650Sstevel@tonic-gate 		 * address will be
6660Sstevel@tonic-gate 		 *
6670Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
6680Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
6690Sstevel@tonic-gate 		 *
6700Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
6710Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
6720Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
6730Sstevel@tonic-gate 		 */
6740Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
6750Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
6760Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
6770Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
6780Sstevel@tonic-gate 			if (!opteron_erratum_100)
6790Sstevel@tonic-gate 				panic("unexpected erratum #100");
6800Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
6810Sstevel@tonic-gate 				goto out;
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
6860Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
6870Sstevel@tonic-gate #ifdef __i386
6880Sstevel@tonic-gate 		/*
6890Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
6900Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
6910Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
6920Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
6930Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
6940Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
6950Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
6960Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
6970Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
6980Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
7010Sstevel@tonic-gate 		    rw == S_READ && instr_is_syscall((caddr_t)rp->r_pc)) {
7020Sstevel@tonic-gate 			extern void watch_syscall(void);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
7050Sstevel@tonic-gate 			watch_syscall();	/* never returns */
7060Sstevel@tonic-gate 			/* NOTREACHED */
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate #endif /* __i386 */
7090Sstevel@tonic-gate 		vaddr = addr;
7100Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
7110Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
7120Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
7130Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
7140Sstevel@tonic-gate 			if (ta) {
7150Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
7160Sstevel@tonic-gate 					watchcode, rp->r_pc);
7170Sstevel@tonic-gate 				fault_type = F_INVAL;
7180Sstevel@tonic-gate 			} else {
7190Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7200Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
7210Sstevel@tonic-gate 				siginfo.si_code = watchcode;
7220Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
7230Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
7240Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
7250Sstevel@tonic-gate 				fault = FLTWATCH;
7260Sstevel@tonic-gate 				break;
7270Sstevel@tonic-gate 			}
7280Sstevel@tonic-gate 		} else {
7290Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
7300Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
7310Sstevel@tonic-gate 				goto out;
7320Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
7330Sstevel@tonic-gate 			fault_type = F_INVAL;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
7400Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
7410Sstevel@tonic-gate 		 */
7420Sstevel@tonic-gate 		if (res == 0 ||
7430Sstevel@tonic-gate 		    (res == FC_NOMAP &&
7440Sstevel@tonic-gate 		    addr < p->p_usrstack &&
7450Sstevel@tonic-gate 		    grow(addr))) {
7460Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
7470Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
7480Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
7490Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7500Sstevel@tonic-gate 				siginfo.si_addr = addr;
7510Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 			goto out;
7540Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
7550Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
7560Sstevel@tonic-gate 			report_stack_exec(p, addr);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
7600Sstevel@tonic-gate 		/*
7610Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
7620Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
7630Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
7640Sstevel@tonic-gate 		 *
7650Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
7660Sstevel@tonic-gate 		 */
7670Sstevel@tonic-gate 		if (opteron_erratum_91 &&
7680Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
7690Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
7700Sstevel@tonic-gate #ifdef DEBUG
7710Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
7720Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
7730Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
7740Sstevel@tonic-gate #endif /* DEBUG */
7750Sstevel@tonic-gate 			goto out;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		if (tudebug)
7800Sstevel@tonic-gate 			showregs(type, rp, addr);
7810Sstevel@tonic-gate 		/*
7820Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
7830Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
7840Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
7850Sstevel@tonic-gate 		 */
7860Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
7870Sstevel@tonic-gate 		siginfo.si_addr = addr;
7880Sstevel@tonic-gate 		switch (FC_CODE(res)) {
7890Sstevel@tonic-gate 		case FC_HWERR:
7900Sstevel@tonic-gate 		case FC_NOSUPPORT:
7910Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
7920Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
7930Sstevel@tonic-gate 			fault = FLTACCESS;
7940Sstevel@tonic-gate 			break;
7950Sstevel@tonic-gate 		case FC_ALIGN:
7960Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
7970Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
7980Sstevel@tonic-gate 			fault = FLTACCESS;
7990Sstevel@tonic-gate 			break;
8000Sstevel@tonic-gate 		case FC_OBJERR:
8010Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8020Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8030Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8040Sstevel@tonic-gate 				fault = FLTACCESS;
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 			break;
8070Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
8080Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8090Sstevel@tonic-gate 			siginfo.si_code =
8100Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
8110Sstevel@tonic-gate 			fault = FLTBOUNDS;
8120Sstevel@tonic-gate 			break;
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		break;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
8170Sstevel@tonic-gate 		/*
8180Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
8190Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
8200Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
8210Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
8220Sstevel@tonic-gate 		 * be to emulate that particular instruction.
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
8250Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_ASYSC)) {
8260Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
8270Sstevel@tonic-gate 				goto out;
8280Sstevel@tonic-gate #ifdef DEBUG
8290Sstevel@tonic-gate 			else
8300Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite syscall "
8310Sstevel@tonic-gate 				    "instruction in process %d",
8320Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
8330Sstevel@tonic-gate #endif /* DEBUG */
8340Sstevel@tonic-gate 		}
8351363Sfvdl 
8361363Sfvdl #ifdef __amd64
8371363Sfvdl 		/*
8381363Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
8391363Sfvdl 		 * See the instr_is_lsahf function for details.
8401363Sfvdl 		 */
8411363Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
8421363Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
8431363Sfvdl 			emulate_lsahf(rp, instr);
8441363Sfvdl 			goto out;
8451363Sfvdl 		}
8461363Sfvdl #endif
8471363Sfvdl 
8480Sstevel@tonic-gate 		/*FALLTHROUGH*/
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		if (tudebug)
8510Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8520Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
8530Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
8540Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
8550Sstevel@tonic-gate 		fault = FLTILL;
8560Sstevel@tonic-gate 		break;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
8590Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8600Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8610Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
8620Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
8630Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
8640Sstevel@tonic-gate 		fault = FLTIZDIV;
8650Sstevel@tonic-gate 		break;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
8680Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8690Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8700Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
8710Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
8720Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
8730Sstevel@tonic-gate 		fault = FLTIOVF;
8740Sstevel@tonic-gate 		break;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
8770Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8780Sstevel@tonic-gate 			showregs(type, rp, addr);
8790Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
8800Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
8810Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
8820Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
8830Sstevel@tonic-gate 			fault = FLTFPE;
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 		break;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
8880Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
8890Sstevel@tonic-gate 		{
8900Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
8910Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
8920Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
8930Sstevel@tonic-gate 			type |= USER;
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 		/*FALLTHROUGH*/
8960Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
8970Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8980Sstevel@tonic-gate 			showregs(type, rp, addr);
8990Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9000Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9010Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9020Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9030Sstevel@tonic-gate 			fault = FLTBOUNDS;
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 		break;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9080Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9090Sstevel@tonic-gate 		{
9100Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9110Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
9120Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9130Sstevel@tonic-gate 			type |= USER;
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 		/*FALLTHROUGH*/
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
9180Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9190Sstevel@tonic-gate 			showregs(type, rp, addr);
9200Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
9210Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9220Sstevel@tonic-gate 			siginfo.si_code  = sicode;
9230Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9240Sstevel@tonic-gate 			fault = FLTFPE;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 		break;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
9290Sstevel@tonic-gate 		if (tudebug && tudebugsse)
9300Sstevel@tonic-gate 			showregs(type, rp, addr);
9310Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
9320Sstevel@tonic-gate 			/*
9330Sstevel@tonic-gate 			 * There are rumours that some user instructions
9340Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
9350Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
9360Sstevel@tonic-gate 			 */
9370Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
9380Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
9390Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9400Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
9410Sstevel@tonic-gate 			fault = FLTILL;
9420Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
9430Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9440Sstevel@tonic-gate 			siginfo.si_code = sicode;
9450Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
9460Sstevel@tonic-gate 			fault = FLTFPE;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 		break;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
9510Sstevel@tonic-gate 		/*
9520Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
9530Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
9540Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
9550Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
9560Sstevel@tonic-gate 		 * fatal.
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
9590Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
9650Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
9660Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		/* Now evaluate how we got here */
9690Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
9700Sstevel@tonic-gate 			/*
9710Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
9720Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
9730Sstevel@tonic-gate 			 * the first instruction in privileged mode.
9740Sstevel@tonic-gate 			 *
9750Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
9760Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
9770Sstevel@tonic-gate 			 *
9780Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
9790Sstevel@tonic-gate 			 */
9800Sstevel@tonic-gate #if defined(__amd64)
9810Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_sysenter) {
9820Sstevel@tonic-gate 				/*
9830Sstevel@tonic-gate 				 * Adjust the pc so that we don't execute the
9840Sstevel@tonic-gate 				 * swapgs instruction at the head of the
9850Sstevel@tonic-gate 				 * handler and completely confuse things.
9860Sstevel@tonic-gate 				 */
9870Sstevel@tonic-gate 				rp->r_pc = (uintptr_t)
9880Sstevel@tonic-gate 				    _sys_sysenter_post_swapgs;
9890Sstevel@tonic-gate #elif defined(__i386)
9900Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_call ||
9910Sstevel@tonic-gate 			    rp->r_pc == (uintptr_t)sys_sysenter) {
9920Sstevel@tonic-gate #endif
9930Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
9940Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
9950Sstevel@tonic-gate 				cur_thread->t_post_sys = 1;
9960Sstevel@tonic-gate 				goto cleanup;
9970Sstevel@tonic-gate 			}
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
10000Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
10010Sstevel@tonic-gate 			debug_enter((char *)NULL);
10020Sstevel@tonic-gate 		else
10030Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10040Sstevel@tonic-gate 		break;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
10070Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
10080Sstevel@tonic-gate 		goto cleanup;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
10110Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
10120Sstevel@tonic-gate 		break;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
10150Sstevel@tonic-gate #if defined(__amd64)
10160Sstevel@tonic-gate 		/*
10170Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
10180Sstevel@tonic-gate 		 * in the virtual address hole e.g. from a copyin.
10190Sstevel@tonic-gate 		 */
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		/*
10220Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
10230Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
10260Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
10270Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
10280Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 		/*
10320Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
10330Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
10340Sstevel@tonic-gate 		 */
10350Sstevel@tonic-gate 		if (cur_thread->t_lofault) {
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
10380Sstevel@tonic-gate 			 */
10390Sstevel@tonic-gate 			if (lodebug) {
10400Sstevel@tonic-gate 				showregs(type, rp, addr);
10410Sstevel@tonic-gate 				traceregs(rp);
10420Sstevel@tonic-gate 			}
10430Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
10440Sstevel@tonic-gate 			rp->r_pc = cur_thread->t_lofault;
10450Sstevel@tonic-gate 			goto cleanup;
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 		/*FALLTHROUGH*/
10480Sstevel@tonic-gate #endif
10490Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
10500Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
10510Sstevel@tonic-gate 	case T_SEGFLT:	/* segment not present fault */
10520Sstevel@tonic-gate 		if (tudebug)
10530Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10540Sstevel@tonic-gate 		if (kern_gpfault(rp))
10550Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10560Sstevel@tonic-gate 		goto cleanup;
10571217Srab 		/*FALLTHROUGH*/
10580Sstevel@tonic-gate 
10591217Srab /*
10601217Srab  * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps should have
10611217Srab  * no legacy need for them, so we put a stop to it here.
10621217Srab  *
10631217Srab  * So: not-present fault is ONLY valid for 32-bit processes with a private LDT
10641217Srab  * trying to do a system call. Emulate it.
10651217Srab  *
10661217Srab  * #gp fault is ONLY valid for 32-bit processes also, which DO NOT have private
10671217Srab  * LDT, and are trying to do a system call. Emulate it.
10681217Srab  */
10690Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
10701217Srab 	case T_GPFLT + USER:	/* general protection violation */
10710Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
10721217Srab 		if (p->p_model != DATAMODEL_NATIVE) {
10731217Srab #endif /* _SYSCALL32_IMPL */
10740Sstevel@tonic-gate 		if (instr_is_syscall((caddr_t)rp->r_pc)) {
10751217Srab 			if (type == T_SEGFLT + USER)
10761217Srab 				ASSERT(p->p_ldt != NULL);
10771217Srab 
10781217Srab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
10791217Srab 			    type == T_SEGFLT + USER) {
10801217Srab 
10810Sstevel@tonic-gate 			/*
10821217Srab 			 * The user attempted a system call via the obsolete
10831217Srab 			 * call gate mechanism. Because the process doesn't have
10841217Srab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
10851217Srab 			 * Emulate the syscall here, just as we do above for a
10861217Srab 			 * #np trap.
10871217Srab 			 */
10881217Srab 
10891217Srab 			/*
10900Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
10910Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
10920Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
10930Sstevel@tonic-gate 			 */
10940Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
10950Sstevel@tonic-gate 			lwp->lwp_regs = rp;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 			/*
10980Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
10990Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
11000Sstevel@tonic-gate 			 * behavior here.
11010Sstevel@tonic-gate 			 */
11020Sstevel@tonic-gate 			mstate = LMS_USER;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 			dosyscall();
11050Sstevel@tonic-gate 			goto out;
11061217Srab 			}
11071217Srab 		}
11081217Srab #ifdef _SYSCALL32_IMPL
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
11110Sstevel@tonic-gate 		/*
11120Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
11130Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
11140Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
11150Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
11160Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
11170Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
11180Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
11190Sstevel@tonic-gate 		 */
11200Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
11210Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_SEP)) {
11220Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
11230Sstevel@tonic-gate 				goto out;
11240Sstevel@tonic-gate #ifdef DEBUG
11250Sstevel@tonic-gate 			else
11260Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite sysenter "
11270Sstevel@tonic-gate 				    "instruction in process %d",
11280Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
11290Sstevel@tonic-gate #endif /* DEBUG */
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		/*FALLTHROUGH*/
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
11340Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
11350Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
11360Sstevel@tonic-gate 		if (tudebug)
11370Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11380Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
11390Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
11400Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
11410Sstevel@tonic-gate 		fault = FLTBOUNDS;
11420Sstevel@tonic-gate 		break;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
11450Sstevel@tonic-gate 		if (tudebug)
11460Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11470Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
11480Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
11490Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
11500Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
11510Sstevel@tonic-gate 		fault = FLTACCESS;
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
11550Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
11560Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		/* Was it single-stepping? */
11590Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
11600Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
11630Sstevel@tonic-gate 			/*
11640Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
11650Sstevel@tonic-gate 			 * give precedence to NORMAL_STEP.  If neither is set,
11660Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
11670Sstevel@tonic-gate 			 * as NORMAL_STEP.
11680Sstevel@tonic-gate 			 */
11690Sstevel@tonic-gate 			if ((pcb->pcb_flags & NORMAL_STEP) ||
11700Sstevel@tonic-gate 			    !(pcb->pcb_flags & WATCH_STEP)) {
11710Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
11720Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
11730Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
11740Sstevel@tonic-gate 				fault = FLTTRACE;
11750Sstevel@tonic-gate 				if (pcb->pcb_flags & WATCH_STEP)
11760Sstevel@tonic-gate 					(void) undo_watch_step(NULL);
11770Sstevel@tonic-gate 			} else {
11780Sstevel@tonic-gate 				fault = undo_watch_step(&siginfo);
11790Sstevel@tonic-gate 			}
11800Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
11810Sstevel@tonic-gate 		} else {
11820Sstevel@tonic-gate 			cmn_err(CE_WARN,
11830Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
11840Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
11850Sstevel@tonic-gate 		}
11860Sstevel@tonic-gate 		break;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
11890Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
11900Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11910Sstevel@tonic-gate 		/*
11920Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
11930Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
11940Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
11950Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
11960Sstevel@tonic-gate 		 */
11970Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
11980Sstevel@tonic-gate 			rp->r_pc--;
11990Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
12000Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
12010Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12020Sstevel@tonic-gate 		fault = FLTBPT;
12030Sstevel@tonic-gate 		break;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	case T_AST:
12060Sstevel@tonic-gate 		/*
12070Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
12080Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
12090Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
12100Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
12110Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
12120Sstevel@tonic-gate 		 * doing the T_AST processing.
12130Sstevel@tonic-gate 		 */
12140Sstevel@tonic-gate 		goto cleanup;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	case T_AST + USER:		/* profiling or resched pseudo trap */
12170Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
12180Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
12190Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
12200Sstevel@tonic-gate 				/*
12210Sstevel@tonic-gate 				 * Signal performance counter overflow
12220Sstevel@tonic-gate 				 */
12230Sstevel@tonic-gate 				if (tudebug)
12240Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
12250Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
12260Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
12270Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
12280Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
12290Sstevel@tonic-gate 				fault = FLTCPCOVF;
12300Sstevel@tonic-gate 			}
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 		break;
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	/*
12360Sstevel@tonic-gate 	 * We can't get here from a system trap
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 	ASSERT(type & USER);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (fault) {
12410Sstevel@tonic-gate 		/*
12420Sstevel@tonic-gate 		 * Remember the fault and fault adddress
12430Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
12440Sstevel@tonic-gate 		 */
12450Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
12460Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 		/*
12510Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
12520Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
12530Sstevel@tonic-gate 		 * deliver the associated signal.
12540Sstevel@tonic-gate 		 */
12550Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
12560Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
12570Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
12580Sstevel@tonic-gate 			siginfo.si_signo = 0;
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	if (siginfo.si_signo)
12620Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
12650Sstevel@tonic-gate 		profil_tick(rp->r_pc);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (cur_thread->t_astflag | cur_thread->t_sig_check) {
12680Sstevel@tonic-gate 		/*
12690Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
12700Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
12710Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
12720Sstevel@tonic-gate 		 * syscall.
12730Sstevel@tonic-gate 		 */
12740Sstevel@tonic-gate 		astoff(cur_thread);
12750Sstevel@tonic-gate 		cur_thread->t_sig_check = 0;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
12780Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
12790Sstevel@tonic-gate 			timer_lwpbind();
12800Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
12810Sstevel@tonic-gate 		}
12820Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 		/*
12850Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
12860Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
12870Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
12880Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
12890Sstevel@tonic-gate 		 * for the kaio request to complete.
12900Sstevel@tonic-gate 		 */
12910Sstevel@tonic-gate 		if (p->p_aio)
12920Sstevel@tonic-gate 			aio_cleanup(0);
12930Sstevel@tonic-gate 		/*
12940Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
12950Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
12960Sstevel@tonic-gate 		 * sets the AST flag.
12970Sstevel@tonic-gate 		 *
12980Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
12990Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
13000Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
13010Sstevel@tonic-gate 		 */
13020Sstevel@tonic-gate 		if (ISHOLD(p))
13030Sstevel@tonic-gate 			holdlwp();
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 		/*
13060Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
13070Sstevel@tonic-gate 		 * set t_astflag afterwards.
13080Sstevel@tonic-gate 		 */
13090Sstevel@tonic-gate 		if (ISSIG_PENDING(cur_thread, lwp, p)) {
13100Sstevel@tonic-gate 			if (issig(FORREAL))
13110Sstevel@tonic-gate 				psig();
13120Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 		if (cur_thread->t_rprof != NULL) {
13160Sstevel@tonic-gate 			realsigprof(0, 0);
13170Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
13180Sstevel@tonic-gate 		}
1319*1468Smarx 
1320*1468Smarx 		/*
1321*1468Smarx 		 * /proc can't enable/disable the trace bit itself
1322*1468Smarx 		 * because that could race with the call gate used by
1323*1468Smarx 		 * system calls via "lcall". If that happened, an
1324*1468Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
1325*1468Smarx 		 * therefore schedule an AST for the purpose.
1326*1468Smarx 		 */
1327*1468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
1328*1468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
1329*1468Smarx 			rp->r_ps |= PS_T;
1330*1468Smarx 		}
1331*1468Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
1332*1468Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
1333*1468Smarx 			rp->r_ps &= ~PS_T;
1334*1468Smarx 		}
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate out:	/* We can't get here from a system trap */
13380Sstevel@tonic-gate 	ASSERT(type & USER);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (ISHOLD(p))
13410Sstevel@tonic-gate 		holdlwp();
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
13450Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
13460Sstevel@tonic-gate 	 * restore the user-level priority.
13470Sstevel@tonic-gate 	 *
13480Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
13490Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
13500Sstevel@tonic-gate 	 * is set back to LWP_SYS).
13510Sstevel@tonic-gate 	 */
13520Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (cur_thread->t_trapret) {
13550Sstevel@tonic-gate 		cur_thread->t_trapret = 0;
13560Sstevel@tonic-gate 		thread_lock(cur_thread);
13570Sstevel@tonic-gate 		CL_TRAPRET(cur_thread);
13580Sstevel@tonic-gate 		thread_unlock(cur_thread);
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 	if (CPU->cpu_runrun)
13610Sstevel@tonic-gate 		preempt();
13620Sstevel@tonic-gate 	(void) new_mstate(cur_thread, mstate);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	/* Kernel probe */
13650Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
13660Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	return;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate cleanup:	/* system traps end up here */
13710Sstevel@tonic-gate 	ASSERT(!(type & USER));
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate /*
13750Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
13760Sstevel@tonic-gate  */
13770Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
13800Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
13810Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
13820Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
13830Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
13840Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
13850Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
13860Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
13870Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
13880Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
13890Sstevel@tonic-gate } kpreempt_cnts;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate /*
13920Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
13930Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
13940Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
13950Sstevel@tonic-gate  */
13960Sstevel@tonic-gate void
13970Sstevel@tonic-gate kpreempt(int asyncspl)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
14020Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
14030Sstevel@tonic-gate 		return;
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	/*
14070Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	do {
14100Sstevel@tonic-gate 		if (cur_thread->t_preempt) {
14110Sstevel@tonic-gate 			/*
14120Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
14130Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
14140Sstevel@tonic-gate 			 */
14150Sstevel@tonic-gate 			if (cur_thread->t_pri < 0)
14160Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
14170Sstevel@tonic-gate 			else if (cur_thread->t_flag & T_INTR_THREAD) {
14180Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
14190Sstevel@tonic-gate 				if (cur_thread->t_pil == CLOCK_LEVEL)
14200Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
14210Sstevel@tonic-gate 			} else
14220Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
14230Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
14240Sstevel@tonic-gate 			return;
14250Sstevel@tonic-gate 		}
14260Sstevel@tonic-gate 		if (cur_thread->t_state != TS_ONPROC ||
14270Sstevel@tonic-gate 		    cur_thread->t_disp_queue != CPU->cpu_disp) {
14280Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
14290Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
14300Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
14310Sstevel@tonic-gate 				/* already in swtch(), force another */
14320Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
14330Sstevel@tonic-gate 				siron();
14340Sstevel@tonic-gate 			}
14350Sstevel@tonic-gate 			return;
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
14380Sstevel@tonic-gate 			/*
14390Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
14400Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
14410Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
14420Sstevel@tonic-gate 			 */
14430Sstevel@tonic-gate 			siron();	/* check back later */
14440Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
14450Sstevel@tonic-gate 			return;
14460Sstevel@tonic-gate 		}
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
14490Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
14500Sstevel@tonic-gate 		else
14510Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 		cur_thread->t_preempt++;
14540Sstevel@tonic-gate 		preempt();
14550Sstevel@tonic-gate 		cur_thread->t_preempt--;
14560Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate  * Print out debugging info.
14610Sstevel@tonic-gate  */
14620Sstevel@tonic-gate static void
14630Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate 	int s;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	s = spl7();
14680Sstevel@tonic-gate 	type &= ~USER;
14690Sstevel@tonic-gate 	if (u.u_comm[0])
14700Sstevel@tonic-gate 		printf("%s: ", u.u_comm);
14710Sstevel@tonic-gate 	if (type < TRAP_TYPES)
14720Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
14730Sstevel@tonic-gate 	else
14740Sstevel@tonic-gate 		switch (type) {
14750Sstevel@tonic-gate 		case T_SYSCALL:
14760Sstevel@tonic-gate 			printf("Syscall Trap:\n");
14770Sstevel@tonic-gate 			break;
14780Sstevel@tonic-gate 		case T_AST:
14790Sstevel@tonic-gate 			printf("AST\n");
14800Sstevel@tonic-gate 			break;
14810Sstevel@tonic-gate 		default:
14820Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
14830Sstevel@tonic-gate 			break;
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 	if (type == T_PGFLT) {
14860Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
14870Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
14880Sstevel@tonic-gate 	} else if (addr) {
14890Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
14900Sstevel@tonic-gate 	}
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
14930Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
14940Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate #if defined(__lint)
14970Sstevel@tonic-gate 	/*
14980Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
14990Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
15000Sstevel@tonic-gate 	 */
15010Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
15020Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
15030Sstevel@tonic-gate #else
15040Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
15050Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
15060Sstevel@tonic-gate #endif
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate #if defined(__amd64)
15090Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx cr8: %lx\n", getcr2(), getcr3(), getcr8());
15100Sstevel@tonic-gate #elif defined(__i386)
15110Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx\n", getcr2(), getcr3());
15120Sstevel@tonic-gate #endif
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	dumpregs(rp);
15150Sstevel@tonic-gate 	splx(s);
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate static void
15190Sstevel@tonic-gate dumpregs(struct regs *rp)
15200Sstevel@tonic-gate {
15210Sstevel@tonic-gate #if defined(__amd64)
15220Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
15250Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
15260Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
15270Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
15280Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	printf(fmt, "fsb", rp->r_fsbase, "gsb", rp->r_gsbase, " ds", rp->r_ds);
15310Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
15340Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate #elif defined(__i386)
15390Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
15420Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
15430Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
15440Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
15450Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
15460Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
15470Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
15480Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
15490Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
15500Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate #endif	/* __i386 */
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate /*
15560Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
15570Sstevel@tonic-gate  *
15580Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
15590Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
15600Sstevel@tonic-gate  *
15610Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
15620Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
15630Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
15640Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
15650Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
15660Sstevel@tonic-gate  *
15670Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
15680Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
15690Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
15700Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
15710Sstevel@tonic-gate  * what we were going to send the lwp.)
15720Sstevel@tonic-gate  *
15730Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
15740Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
15750Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
15760Sstevel@tonic-gate  */
15770Sstevel@tonic-gate static int
15780Sstevel@tonic-gate kern_gpfault(struct regs *rp)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	kthread_t *t = curthread;
15810Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
15820Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
15830Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
15840Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
15850Sstevel@tonic-gate 	int v;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	extern void _sys_rtt(), sr_sup();
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate #if defined(__amd64)
15900Sstevel@tonic-gate 	extern void _update_sregs(), _update_sregs_done();
15910Sstevel@tonic-gate 	static const uint8_t iretq_insn[2] = { 0x48, 0xcf };
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate #elif defined(__i386)
15940Sstevel@tonic-gate 	static const uint8_t iret_insn[1] = { 0xcf };
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	/*
15970Sstevel@tonic-gate 	 * Note carefully the appallingly awful dependency between
15980Sstevel@tonic-gate 	 * the instruction sequence used in __SEGREGS_POP and these
15990Sstevel@tonic-gate 	 * instructions encoded here.
16000Sstevel@tonic-gate 	 *
16010Sstevel@tonic-gate 	 * XX64	Add some commentary to locore.s/privregs.h to document this.
16020Sstevel@tonic-gate 	 */
16030Sstevel@tonic-gate 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
16040Sstevel@tonic-gate 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
16050Sstevel@tonic-gate 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
16060Sstevel@tonic-gate 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
16070Sstevel@tonic-gate #endif
16080Sstevel@tonic-gate 	/*
16090Sstevel@tonic-gate 	 * if we're not an lwp, or the pc range is outside _sys_rtt, then
16100Sstevel@tonic-gate 	 * we should immediately be die()ing horribly
16110Sstevel@tonic-gate 	 */
16120Sstevel@tonic-gate 	if (lwp == NULL ||
16130Sstevel@tonic-gate 	    (uintptr_t)pc < (uintptr_t)_sys_rtt ||
16140Sstevel@tonic-gate 	    (uintptr_t)pc > (uintptr_t)sr_sup)
16150Sstevel@tonic-gate 		return (1);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/*
16180Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
16190Sstevel@tonic-gate 	 *
16200Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
16210Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
16220Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
16230Sstevel@tonic-gate 	 * _sys_rtt. Ew.
16240Sstevel@tonic-gate 	 */
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate #if defined(__amd64)
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	if (bcmp(pc, iretq_insn, sizeof (iretq_insn)) == 0) {
16290Sstevel@tonic-gate 		/*
16300Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iretq.
16310Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
16320Sstevel@tonic-gate 		 * All we know for sure is that most of the general
16330Sstevel@tonic-gate 		 * registers have been restored, including the
16340Sstevel@tonic-gate 		 * segment registers, and all we have left on the
16350Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
16360Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
16370Sstevel@tonic-gate 		 *
16380Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
16390Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
16400Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
16410Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
16420Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
16430Sstevel@tonic-gate 		 */
16440Sstevel@tonic-gate 		trp = &tmpregs;
16450Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
16460Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
16470Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
16480Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
16490Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
16500Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 		/*
16530Sstevel@tonic-gate 		 * Validate simple math
16540Sstevel@tonic-gate 		 */
16550Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
16560Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	} else if ((lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) != 0 &&
16590Sstevel@tonic-gate 	    pc >= (caddr_t)_update_sregs &&
16600Sstevel@tonic-gate 	    pc < (caddr_t)_update_sregs_done) {
16610Sstevel@tonic-gate 		/*
16620Sstevel@tonic-gate 		 * This is the common case -- we're trying to load
16630Sstevel@tonic-gate 		 * a bad segment register value in the only section
16640Sstevel@tonic-gate 		 * of kernel code that ever loads segment registers.
16650Sstevel@tonic-gate 		 *
16660Sstevel@tonic-gate 		 * We don't need to do anything at this point because
16670Sstevel@tonic-gate 		 * the pcb contains all the pending segment register
16680Sstevel@tonic-gate 		 * state, and the regs are still intact because we
16690Sstevel@tonic-gate 		 * didn't adjust the stack pointer yet.  Given the fidelity
16700Sstevel@tonic-gate 		 * of all this, we could conceivably send a signal
16710Sstevel@tonic-gate 		 * to the lwp, rather than core-ing.
16720Sstevel@tonic-gate 		 */
16730Sstevel@tonic-gate 		trp = lwptoregs(lwp);
16740Sstevel@tonic-gate 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate #elif defined(__i386)
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	if (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0) {
16800Sstevel@tonic-gate 		/*
16810Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iret.
16820Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
16830Sstevel@tonic-gate 		 * All we know for sure is that most of the general
16840Sstevel@tonic-gate 		 * registers have been restored, including the
16850Sstevel@tonic-gate 		 * segment registers, and all we have left on the
16860Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the registers that
16870Sstevel@tonic-gate 		 * the iret was unable to consume.
16880Sstevel@tonic-gate 		 *
16890Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
16900Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
16910Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
16920Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
16930Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
16940Sstevel@tonic-gate 		 */
16950Sstevel@tonic-gate 		trp = &tmpregs;
16960Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
16970Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
16980Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
16990Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
17000Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
17010Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
17040Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	} else {
17070Sstevel@tonic-gate 		/*
17080Sstevel@tonic-gate 		 * Segment registers are reloaded in _sys_rtt
17090Sstevel@tonic-gate 		 * via the following sequence:
17100Sstevel@tonic-gate 		 *
17110Sstevel@tonic-gate 		 *	movw	0(%esp), %gs
17120Sstevel@tonic-gate 		 *	movw	4(%esp), %fs
17130Sstevel@tonic-gate 		 *	movw	8(%esp), %es
17140Sstevel@tonic-gate 		 *	movw	12(%esp), %ds
17150Sstevel@tonic-gate 		 *	addl	$16, %esp
17160Sstevel@tonic-gate 		 *
17170Sstevel@tonic-gate 		 * Thus if any of them fault, we know the user
17180Sstevel@tonic-gate 		 * registers are left unharmed on the stack.
17190Sstevel@tonic-gate 		 */
17200Sstevel@tonic-gate 		if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
17210Sstevel@tonic-gate 		    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
17220Sstevel@tonic-gate 		    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
17230Sstevel@tonic-gate 		    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
17240Sstevel@tonic-gate 			trp = lwptoregs(lwp);
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate #endif	/* __amd64 */
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	if (trp == NULL)
17290Sstevel@tonic-gate 		return (1);
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	/*
17320Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
17330Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
17340Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
17350Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
17360Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	/*
17400Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
17410Sstevel@tonic-gate 	 */
17420Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
17430Sstevel@tonic-gate 	trp->r_err = rp->r_err;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
17460Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17490Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
17500Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	/*
17530Sstevel@tonic-gate 	 * Terminate all LWPs but don't discard them.  If another lwp beat us to
17540Sstevel@tonic-gate 	 * the punch by calling exit(), evaporate now.
17550Sstevel@tonic-gate 	 */
1756390Sraf 	proc_is_exiting(p);
17570Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
17580Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
17590Sstevel@tonic-gate 		lwp_exit();
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate #ifdef C2_AUDIT
17630Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
17640Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
17650Sstevel@tonic-gate #endif
17660Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
17670Sstevel@tonic-gate #ifdef C2_AUDIT
17680Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
17690Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
17700Sstevel@tonic-gate #endif
17710Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
17720Sstevel@tonic-gate 	return (0);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate /*
17760Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
17770Sstevel@tonic-gate  */
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate #if defined(__amd64)
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate static void
17820Sstevel@tonic-gate dump_tss(void)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
17850Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
17880Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
17890Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
17920Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
17930Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
17940Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
17950Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
17960Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
17970Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
17980Sstevel@tonic-gate }
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate #elif defined(__i386)
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate static void
18030Sstevel@tonic-gate dump_tss(void)
18040Sstevel@tonic-gate {
18050Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
18060Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
18070Sstevel@tonic-gate 
1808286Sdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
1809286Sdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
1810286Sdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
1811286Sdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
1812286Sdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
1813286Sdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
1814286Sdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
1815286Sdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
1816286Sdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
1817286Sdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
1818286Sdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
1819286Sdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
1820286Sdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
1821286Sdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
1822286Sdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
18230Sstevel@tonic-gate }
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate #endif	/* __amd64 */
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate #if defined(TRAPTRACE)
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate int ttrace_nrec = 0;		/* number of records to dump out */
18300Sstevel@tonic-gate int ttrace_dump_nregs = 5;	/* dump out this many records with regs too */
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate /*
18330Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
18340Sstevel@tonic-gate  */
18350Sstevel@tonic-gate static void
18360Sstevel@tonic-gate dump_ttrace(void)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
18390Sstevel@tonic-gate 	trap_trace_rec_t *rec;
18400Sstevel@tonic-gate 	uintptr_t current;
18410Sstevel@tonic-gate 	int i, j, k;
18420Sstevel@tonic-gate 	int n = NCPU;
18430Sstevel@tonic-gate #if defined(__amd64)
18440Sstevel@tonic-gate 	const char banner[] =
18450Sstevel@tonic-gate 		"\ncpu          address    timestamp "
18460Sstevel@tonic-gate 		"type  vc  handler   pc\n";
18470Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
18480Sstevel@tonic-gate #elif defined(__i386)
18490Sstevel@tonic-gate 	const char banner[] =
18500Sstevel@tonic-gate 		"\ncpu address     timestamp type  vc  handler   pc\n";
18510Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
18520Sstevel@tonic-gate #endif
18530Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
18540Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	if (ttrace_nrec == 0)
18570Sstevel@tonic-gate 		return;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	printf(banner);
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
18620Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
18630Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
18640Sstevel@tonic-gate 			continue;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
18670Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
18680Sstevel@tonic-gate 			struct sysent	*sys;
18690Sstevel@tonic-gate 			struct autovec	*vec;
18700Sstevel@tonic-gate 			extern struct av_head autovect[];
18710Sstevel@tonic-gate 			int type;
18720Sstevel@tonic-gate 			ulong_t	off;
18730Sstevel@tonic-gate 			char *sym, *stype;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 			if (current < ttc->ttc_first)
18760Sstevel@tonic-gate 				current =
18770Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 			if (current == NULL)
18800Sstevel@tonic-gate 				continue;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
18850Sstevel@tonic-gate 				break;
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 			switch (rec->ttr_marker) {
18900Sstevel@tonic-gate 			case TT_SYSCALL:
18910Sstevel@tonic-gate 			case TT_SYSENTER:
18920Sstevel@tonic-gate 			case TT_SYSC:
18930Sstevel@tonic-gate 			case TT_SYSC64:
18940Sstevel@tonic-gate #if defined(__amd64)
18950Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
18960Sstevel@tonic-gate 				switch (rec->ttr_marker) {
18970Sstevel@tonic-gate 				case TT_SYSC64:
18980Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
18990Sstevel@tonic-gate 					/*FALLTHROUGH*/
19000Sstevel@tonic-gate #elif defined(__i386)
19010Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
19020Sstevel@tonic-gate 				switch (rec->ttr_marker) {
19030Sstevel@tonic-gate 				case TT_SYSC64:
19040Sstevel@tonic-gate #endif
19050Sstevel@tonic-gate 				case TT_SYSC:
19060Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
19070Sstevel@tonic-gate 					break;
19080Sstevel@tonic-gate 				case TT_SYSCALL:
19090Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
19100Sstevel@tonic-gate 					break;
19110Sstevel@tonic-gate 				case TT_SYSENTER:
19120Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
19130Sstevel@tonic-gate 					break;
19140Sstevel@tonic-gate 				default:
19150Sstevel@tonic-gate 					break;
19160Sstevel@tonic-gate 				}
19170Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
19180Sstevel@tonic-gate 				if (sys != NULL) {
19190Sstevel@tonic-gate 					sym = kobj_getsymname(
19200Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
19210Sstevel@tonic-gate 					    &off);
19220Sstevel@tonic-gate 					if (sym != NULL)
19230Sstevel@tonic-gate 						printf("%s ", sym);
19240Sstevel@tonic-gate 					else
19250Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
19260Sstevel@tonic-gate 				} else {
19270Sstevel@tonic-gate 					printf("unknown ");
19280Sstevel@tonic-gate 				}
19290Sstevel@tonic-gate 				break;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 			case TT_INTERRUPT:
19320Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
19330Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
19340Sstevel@tonic-gate 				if (vec != NULL) {
19350Sstevel@tonic-gate 					sym = kobj_getsymname(
19360Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
19370Sstevel@tonic-gate 					if (sym != NULL)
19380Sstevel@tonic-gate 						printf("%s ", sym);
19390Sstevel@tonic-gate 					else
19400Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
19410Sstevel@tonic-gate 				} else {
19420Sstevel@tonic-gate 					printf("unknown ");
19430Sstevel@tonic-gate 				}
19440Sstevel@tonic-gate 				break;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 			case TT_TRAP:
19470Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
19480Sstevel@tonic-gate 				printf(fmt2, "trap", type);
19490Sstevel@tonic-gate 				printf("#%s ", type < TRAP_TYPES ?
19500Sstevel@tonic-gate 				    trap_type_mnemonic[type] : "trap");
19510Sstevel@tonic-gate 				break;
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 			default:
19540Sstevel@tonic-gate 				break;
19550Sstevel@tonic-gate 			}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
19580Sstevel@tonic-gate 			if (sym != NULL)
19590Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
19600Sstevel@tonic-gate 			else
19610Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
19640Sstevel@tonic-gate 				int s;
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
19670Sstevel@tonic-gate 					printf(
19680Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
19690Sstevel@tonic-gate 					    rec->ttr_ipl,
19700Sstevel@tonic-gate 					    rec->ttr_spl,
19710Sstevel@tonic-gate 					    rec->ttr_pri);
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
19760Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 				/*
19790Sstevel@tonic-gate 				 * print out the pc stack that we recorded
19800Sstevel@tonic-gate 				 * at trap time (if any)
19810Sstevel@tonic-gate 				 */
19820Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
19830Sstevel@tonic-gate 					uintptr_t fullpc;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
19860Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
19870Sstevel@tonic-gate 						break;
19880Sstevel@tonic-gate 					}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
19930Sstevel@tonic-gate 					if (sym != NULL)
19940Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
19950Sstevel@tonic-gate 						    sym, off);
19960Sstevel@tonic-gate 					else
19970Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
19980Sstevel@tonic-gate 				}
19990Sstevel@tonic-gate 				printf("\n");
20000Sstevel@tonic-gate 			}
20010Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
20020Sstevel@tonic-gate 		}
20030Sstevel@tonic-gate 	}
20040Sstevel@tonic-gate }
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate #endif	/* TRAPTRACE */
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate void
20090Sstevel@tonic-gate panic_showtrap(struct trap_info *tip)
20100Sstevel@tonic-gate {
20110Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate #if defined(TRAPTRACE)
20140Sstevel@tonic-gate 	dump_ttrace();
20150Sstevel@tonic-gate #endif	/* TRAPTRACE */
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
20180Sstevel@tonic-gate 		dump_tss();
20190Sstevel@tonic-gate }
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate void
20220Sstevel@tonic-gate panic_savetrap(panic_data_t *pdp, struct trap_info *tip)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
20250Sstevel@tonic-gate }
2026