xref: /illumos-gate/usr/src/uts/i86pc/os/trap.c (revision 97eda132fb49582e04504d6a221b98750a14f5b3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*97eda132Sraf 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
307c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
317c478bd9Sstevel@tonic-gate /*								*/
327c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
337c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
347c478bd9Sstevel@tonic-gate /*								*/
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/disp.h>
467c478bd9Sstevel@tonic-gate #include <sys/class.h>
477c478bd9Sstevel@tonic-gate #include <sys/core.h>
487c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
497c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
507c478bd9Sstevel@tonic-gate #include <sys/vm.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
527c478bd9Sstevel@tonic-gate #include <sys/fault.h>
537c478bd9Sstevel@tonic-gate #include <sys/stack.h>
547c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
557c478bd9Sstevel@tonic-gate #include <sys/psw.h>
567c478bd9Sstevel@tonic-gate #include <sys/regset.h>
577c478bd9Sstevel@tonic-gate #include <sys/fp.h>
587c478bd9Sstevel@tonic-gate #include <sys/trap.h>
597c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
607c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
637c478bd9Sstevel@tonic-gate #include <sys/mutex_impl.h>
647c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
657c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
667c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
677c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
687c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <vm/hat.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
737c478bd9Sstevel@tonic-gate #include <vm/as.h>
747c478bd9Sstevel@tonic-gate #include <vm/seg.h>
757c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
807c478bd9Sstevel@tonic-gate #include <sys/debug.h>
817c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
827c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
837c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
847c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
857c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
867c478bd9Sstevel@tonic-gate #include <sys/cred.h>
877c478bd9Sstevel@tonic-gate #include <sys/mman.h>
887c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
897c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
907c478bd9Sstevel@tonic-gate #include <c2/audit.h>
917c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
927c478bd9Sstevel@tonic-gate #include <sys/panic.h>
937c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
947c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
957c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1007c478bd9Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1017c478bd9Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1027c478bd9Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1037c478bd9Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1047c478bd9Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static const char *trap_type[] = {
1087c478bd9Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
1097c478bd9Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1107c478bd9Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1117c478bd9Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
1127c478bd9Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
1137c478bd9Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
1147c478bd9Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
1157c478bd9Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
1167c478bd9Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
1177c478bd9Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
1187c478bd9Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
1197c478bd9Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
1207c478bd9Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
1217c478bd9Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
1227c478bd9Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
1237c478bd9Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
1247c478bd9Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
1257c478bd9Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
1267c478bd9Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1277c478bd9Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate int tudebug = 0;
1337c478bd9Sstevel@tonic-gate int tudebugbpt = 0;
1347c478bd9Sstevel@tonic-gate int tudebugfpe = 0;
1357c478bd9Sstevel@tonic-gate int tudebugsse = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1387c478bd9Sstevel@tonic-gate int tdebug = 0;
1397c478bd9Sstevel@tonic-gate int lodebug = 0;
1407c478bd9Sstevel@tonic-gate int faultdebug = 0;
1417c478bd9Sstevel@tonic-gate #else
1427c478bd9Sstevel@tonic-gate #define	tdebug	0
1437c478bd9Sstevel@tonic-gate #define	lodebug	0
1447c478bd9Sstevel@tonic-gate #define	faultdebug	0
1457c478bd9Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
1487c478bd9Sstevel@tonic-gate static void dump_ttrace(void);
1497c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1507c478bd9Sstevel@tonic-gate static void dumpregs(struct regs *);
1517c478bd9Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1527c478bd9Sstevel@tonic-gate static void dump_tss(void);
1537c478bd9Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate struct trap_info {
1567c478bd9Sstevel@tonic-gate 	struct regs *trap_regs;
1577c478bd9Sstevel@tonic-gate 	uint_t trap_type;
1587c478bd9Sstevel@tonic-gate 	caddr_t trap_addr;
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1627c478bd9Sstevel@tonic-gate static int
1637c478bd9Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct trap_info ti;
1667c478bd9Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1697c478bd9Sstevel@tonic-gate 		trap_name = trap_type[type];
1707c478bd9Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1717c478bd9Sstevel@tonic-gate 	} else {
1727c478bd9Sstevel@tonic-gate 		trap_name = "trap";
1737c478bd9Sstevel@tonic-gate 		trap_mnemonic = "-";
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
1777c478bd9Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	ti.trap_regs = rp;
1817c478bd9Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
1827c478bd9Sstevel@tonic-gate 	ti.trap_addr = addr;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
1877c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
1887c478bd9Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
1897c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
1907c478bd9Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
1917c478bd9Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
1927c478bd9Sstevel@tonic-gate 		    "a NULL pointer dereference" :
1937c478bd9Sstevel@tonic-gate 		    "an illegal access to a user address");
1947c478bd9Sstevel@tonic-gate 	} else
1957c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
1967c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2027c478bd9Sstevel@tonic-gate  *
2037c478bd9Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #define	SLOW_SCALL_SIZE	2
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static int
2097c478bd9Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2147c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2157c478bd9Sstevel@tonic-gate 		return (1);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2227c478bd9Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2237c478bd9Sstevel@tonic-gate  * instruction.
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2267c478bd9Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #define	FAST_SCALL_SIZE	2
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static int
2327c478bd9Sstevel@tonic-gate instr_is_fast_syscall(caddr_t pc, int which)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	ASSERT(which == X86_SEP || which == X86_ASYSC);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0 ||
2397c478bd9Sstevel@tonic-gate 	    instr[0] != 0x0F)
2407c478bd9Sstevel@tonic-gate 		return (0);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if ((which == X86_SEP && instr[1] == 0x34) ||
2437c478bd9Sstevel@tonic-gate 	    (which == X86_ASYSC && instr[1] == 0x05))
2447c478bd9Sstevel@tonic-gate 		return (1);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
2537c478bd9Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
2547c478bd9Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate #define	LCALLSIZE	7
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate instr_is_syscall(caddr_t pc)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
2657c478bd9Sstevel@tonic-gate 	    instr[0] == 0x9a &&
2667c478bd9Sstevel@tonic-gate 	    instr[1] == 0 &&
2677c478bd9Sstevel@tonic-gate 	    instr[2] == 0 &&
2687c478bd9Sstevel@tonic-gate 	    instr[3] == 0 &&
2697c478bd9Sstevel@tonic-gate 	    instr[4] == 0 &&
2707c478bd9Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
2717c478bd9Sstevel@tonic-gate 	    instr[6] == 0)
2727c478bd9Sstevel@tonic-gate 		return (1);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
2837c478bd9Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
2847c478bd9Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate #define	PREFETCHSIZE 3
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static int
2907c478bd9Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
2937c478bd9Sstevel@tonic-gate 		return (1);
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static int
2987c478bd9Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
3017c478bd9Sstevel@tonic-gate 	int	error;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
3067c478bd9Sstevel@tonic-gate 		return (1);
3077c478bd9Sstevel@tonic-gate 	return (0);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
3147c478bd9Sstevel@tonic-gate  *
3157c478bd9Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
3167c478bd9Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate void
3197c478bd9Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
3227c478bd9Sstevel@tonic-gate 	enum seg_rw rw;
3237c478bd9Sstevel@tonic-gate 	unsigned type;
3247c478bd9Sstevel@tonic-gate 	extern int stop_on_fault(uint_t, k_siginfo_t *);
3257c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(cur_thread);
3267c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(cur_thread);
3277c478bd9Sstevel@tonic-gate 	uintptr_t lofault;
3287c478bd9Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
3297c478bd9Sstevel@tonic-gate 	enum fault_type fault_type;
3307c478bd9Sstevel@tonic-gate 	k_siginfo_t siginfo;
3317c478bd9Sstevel@tonic-gate 	uint_t fault = 0;
3327c478bd9Sstevel@tonic-gate 	int mstate;
3337c478bd9Sstevel@tonic-gate 	int sicode = 0;
3347c478bd9Sstevel@tonic-gate 	int watchcode;
3357c478bd9Sstevel@tonic-gate 	int watchpage;
3367c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
3377c478bd9Sstevel@tonic-gate 	size_t sz;
3387c478bd9Sstevel@tonic-gate 	int ta;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	type = rp->r_trapno;
3437c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	ASSERT(cur_thread->t_schedflag & TS_DONT_SWAP);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		errcode = rp->r_err;
3507c478bd9Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
3517c478bd9Sstevel@tonic-gate 			rw = S_WRITE;
3527c478bd9Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
3537c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
3547c478bd9Sstevel@tonic-gate 			rw = S_EXEC;
3557c478bd9Sstevel@tonic-gate 		else
3567c478bd9Sstevel@tonic-gate 			rw = S_READ;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate #if defined(__i386)
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * Pentium Pro work-around
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
3637c478bd9Sstevel@tonic-gate 			uint_t	attr;
3647c478bd9Sstevel@tonic-gate 			uint_t	priv_violation;
3657c478bd9Sstevel@tonic-gate 			uint_t	access_violation;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
3687c478bd9Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
3697c478bd9Sstevel@tonic-gate 			    == -1) {
3707c478bd9Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
3717c478bd9Sstevel@tonic-gate 			} else {
3727c478bd9Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
3737c478bd9Sstevel@tonic-gate 					!(attr & PROT_USER);
3747c478bd9Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
3757c478bd9Sstevel@tonic-gate 					!(attr & PROT_WRITE);
3767c478bd9Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
3777c478bd9Sstevel@tonic-gate 					goto cleanup;
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate #endif /* __i386 */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (tdebug)
3857c478bd9Sstevel@tonic-gate 		showregs(type, rp, addr);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
3887c478bd9Sstevel@tonic-gate 		/*
3897c478bd9Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
3907c478bd9Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
3917c478bd9Sstevel@tonic-gate 		 * The current process credential applies to the thread for
3927c478bd9Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
3937c478bd9Sstevel@tonic-gate 		 * should already be set up.
3947c478bd9Sstevel@tonic-gate 		 */
3957c478bd9Sstevel@tonic-gate 		if (cur_thread->t_cred != p->p_cred) {
3967c478bd9Sstevel@tonic-gate 			cred_t *oldcred = cur_thread->t_cred;
3977c478bd9Sstevel@tonic-gate 			/*
3987c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
3997c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
4007c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
4017c478bd9Sstevel@tonic-gate 			 */
4027c478bd9Sstevel@tonic-gate 			cur_thread->t_cred = crgetcred();
4037c478bd9Sstevel@tonic-gate 			crfree(oldcred);
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 		ASSERT(lwp != NULL);
4067c478bd9Sstevel@tonic-gate 		type |= USER;
4077c478bd9Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
4087c478bd9Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		switch (type) {
4117c478bd9Sstevel@tonic-gate 		case T_PGFLT + USER:
4127c478bd9Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
4137c478bd9Sstevel@tonic-gate 				mstate = LMS_TFAULT;
4147c478bd9Sstevel@tonic-gate 			else
4157c478bd9Sstevel@tonic-gate 				mstate = LMS_DFAULT;
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 		default:
4187c478bd9Sstevel@tonic-gate 			mstate = LMS_TRAP;
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		/* Kernel probe */
4227c478bd9Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
4237c478bd9Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
4247c478bd9Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, mstate);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	switch (type) {
4307c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:
4317c478bd9Sstevel@tonic-gate 	case T_SGLSTP:
4327c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER:
4337c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	default:
4377c478bd9Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
4387c478bd9Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
4397c478bd9Sstevel@tonic-gate 		break;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	switch (type) {
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	case T_MCE:	/* Machine check exception */
4457c478bd9Sstevel@tonic-gate 	case T_MCE + USER:
4467c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_MCA) {
4477c478bd9Sstevel@tonic-gate 			if (mca_exception(rp))
4487c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
4497c478bd9Sstevel@tonic-gate 			type &= ~USER;
4507c478bd9Sstevel@tonic-gate 			goto cleanup;
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	default:
4537c478bd9Sstevel@tonic-gate 		if (type & USER) {
4547c478bd9Sstevel@tonic-gate 			if (tudebug)
4557c478bd9Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
4567c478bd9Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
4577c478bd9Sstevel@tonic-gate 			    type & ~USER);
4587c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
4597c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
4607c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
4617c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
4627c478bd9Sstevel@tonic-gate 			fault = FLTILL;
4637c478bd9Sstevel@tonic-gate 			break;
4647c478bd9Sstevel@tonic-gate 		} else {
4657c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
4667c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
4727c478bd9Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
4757c478bd9Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
4767c478bd9Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
4777c478bd9Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		/*
4817c478bd9Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
4827c478bd9Sstevel@tonic-gate 		 * across this. Here we assume that an address
4837c478bd9Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
4847c478bd9Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
4857c478bd9Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
4867c478bd9Sstevel@tonic-gate 		 * starting and because we know that we always have
4877c478bd9Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
4887c478bd9Sstevel@tonic-gate 		 */
4897c478bd9Sstevel@tonic-gate 		lofault = cur_thread->t_lofault;
4907c478bd9Sstevel@tonic-gate 		cur_thread->t_lofault = 0;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, LMS_KFAULT);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
4957c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
4967c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
4977c478bd9Sstevel@tonic-gate 			if (res == FC_NOMAP &&
4987c478bd9Sstevel@tonic-gate 			    addr < p->p_usrstack &&
4997c478bd9Sstevel@tonic-gate 			    grow(addr))
5007c478bd9Sstevel@tonic-gate 				res = 0;
5017c478bd9Sstevel@tonic-gate 		} else {
5027c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
5037c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 		(void) new_mstate(cur_thread, mstate);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		/*
5087c478bd9Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
5097c478bd9Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		cur_thread->t_lofault = lofault;
5127c478bd9Sstevel@tonic-gate 		if (res == 0)
5137c478bd9Sstevel@tonic-gate 			goto cleanup;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
5167c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
5177c478bd9Sstevel@tonic-gate 			/*
5187c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
5197c478bd9Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
5207c478bd9Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
5217c478bd9Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
5227c478bd9Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
5237c478bd9Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
5247c478bd9Sstevel@tonic-gate 			 */
5257c478bd9Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
5287c478bd9Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
5297c478bd9Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
5307c478bd9Sstevel@tonic-gate 				    PFN_INVALID &&
5317c478bd9Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
5327c478bd9Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
5337c478bd9Sstevel@tonic-gate 					rp->r_pc = rip;
5347c478bd9Sstevel@tonic-gate 					goto cleanup;
5357c478bd9Sstevel@tonic-gate 				}
5367c478bd9Sstevel@tonic-gate 			}
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
5417c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
5427c478bd9Sstevel@tonic-gate 			/*
5437c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
5447c478bd9Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
5457c478bd9Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
5467c478bd9Sstevel@tonic-gate 			 * instruction.
5477c478bd9Sstevel@tonic-gate 			 */
5487c478bd9Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 			/*
5517c478bd9Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
5527c478bd9Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
5537c478bd9Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
5547c478bd9Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
5557c478bd9Sstevel@tonic-gate 			 */
5567c478bd9Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
5577c478bd9Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
5587c478bd9Sstevel@tonic-gate #ifdef DEBUG
5597c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
5607c478bd9Sstevel@tonic-gate 					    "occurred: kernel prefetch"
5617c478bd9Sstevel@tonic-gate 					    " at %p generated a page fault!",
5627c478bd9Sstevel@tonic-gate 					    (void *)rp->r_pc);
5637c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5647c478bd9Sstevel@tonic-gate 					goto cleanup;
5657c478bd9Sstevel@tonic-gate 				}
5667c478bd9Sstevel@tonic-gate 			}
5677c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		if (lofault == 0)
5727c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		/*
5757c478bd9Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
5767c478bd9Sstevel@tonic-gate 		 */
5777c478bd9Sstevel@tonic-gate 		if (lodebug) {
5787c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
5797c478bd9Sstevel@tonic-gate 			traceregs(rp);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
5827c478bd9Sstevel@tonic-gate 			res = FC_ERRNO(res);
5837c478bd9Sstevel@tonic-gate 		else
5847c478bd9Sstevel@tonic-gate 			res = EFAULT;
5857c478bd9Sstevel@tonic-gate 		rp->r_r0 = res;
5867c478bd9Sstevel@tonic-gate 		rp->r_pc = cur_thread->t_lofault;
5877c478bd9Sstevel@tonic-gate 		goto cleanup;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
5907c478bd9Sstevel@tonic-gate 		if (faultdebug) {
5917c478bd9Sstevel@tonic-gate 			char *fault_str;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 			switch (rw) {
5947c478bd9Sstevel@tonic-gate 			case S_READ:
5957c478bd9Sstevel@tonic-gate 				fault_str = "read";
5967c478bd9Sstevel@tonic-gate 				break;
5977c478bd9Sstevel@tonic-gate 			case S_WRITE:
5987c478bd9Sstevel@tonic-gate 				fault_str = "write";
5997c478bd9Sstevel@tonic-gate 				break;
6007c478bd9Sstevel@tonic-gate 			case S_EXEC:
6017c478bd9Sstevel@tonic-gate 				fault_str = "exec";
6027c478bd9Sstevel@tonic-gate 				break;
6037c478bd9Sstevel@tonic-gate 			default:
6047c478bd9Sstevel@tonic-gate 				fault_str = "";
6057c478bd9Sstevel@tonic-gate 				break;
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
6087c478bd9Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
6127c478bd9Sstevel@tonic-gate 		/*
6137c478bd9Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
6147c478bd9Sstevel@tonic-gate 		 *
6157c478bd9Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
6167c478bd9Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
6177c478bd9Sstevel@tonic-gate 		 * address will be
6187c478bd9Sstevel@tonic-gate 		 *
6197c478bd9Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
6207c478bd9Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
6217c478bd9Sstevel@tonic-gate 		 *
6227c478bd9Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
6237c478bd9Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
6247c478bd9Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
6257c478bd9Sstevel@tonic-gate 		 */
6267c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
6277c478bd9Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
6287c478bd9Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
6297c478bd9Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
6307c478bd9Sstevel@tonic-gate 			if (!opteron_erratum_100)
6317c478bd9Sstevel@tonic-gate 				panic("unexpected erratum #100");
6327c478bd9Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
6337c478bd9Sstevel@tonic-gate 				goto out;
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
6387c478bd9Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
6397c478bd9Sstevel@tonic-gate #ifdef __i386
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
6427c478bd9Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
6437c478bd9Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
6447c478bd9Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
6457c478bd9Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
6467c478bd9Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
6477c478bd9Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
6487c478bd9Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
6497c478bd9Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
6507c478bd9Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
6537c478bd9Sstevel@tonic-gate 		    rw == S_READ && instr_is_syscall((caddr_t)rp->r_pc)) {
6547c478bd9Sstevel@tonic-gate 			extern void watch_syscall(void);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
6577c478bd9Sstevel@tonic-gate 			watch_syscall();	/* never returns */
6587c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate #endif /* __i386 */
6617c478bd9Sstevel@tonic-gate 		vaddr = addr;
6627c478bd9Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
6637c478bd9Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
6647c478bd9Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
6657c478bd9Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
6667c478bd9Sstevel@tonic-gate 			if (ta) {
6677c478bd9Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
6687c478bd9Sstevel@tonic-gate 					watchcode, rp->r_pc);
6697c478bd9Sstevel@tonic-gate 				fault_type = F_INVAL;
6707c478bd9Sstevel@tonic-gate 			} else {
6717c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
6727c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
6737c478bd9Sstevel@tonic-gate 				siginfo.si_code = watchcode;
6747c478bd9Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
6757c478bd9Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
6767c478bd9Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
6777c478bd9Sstevel@tonic-gate 				fault = FLTWATCH;
6787c478bd9Sstevel@tonic-gate 				break;
6797c478bd9Sstevel@tonic-gate 			}
6807c478bd9Sstevel@tonic-gate 		} else {
6817c478bd9Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
6827c478bd9Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
6837c478bd9Sstevel@tonic-gate 				goto out;
6847c478bd9Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
6857c478bd9Sstevel@tonic-gate 			fault_type = F_INVAL;
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		/*
6917c478bd9Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
6927c478bd9Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		if (res == 0 ||
6957c478bd9Sstevel@tonic-gate 		    (res == FC_NOMAP &&
6967c478bd9Sstevel@tonic-gate 		    addr < p->p_usrstack &&
6977c478bd9Sstevel@tonic-gate 		    grow(addr))) {
6987c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
6997c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
7007c478bd9Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
7017c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7027c478bd9Sstevel@tonic-gate 				siginfo.si_addr = addr;
7037c478bd9Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 			goto out;
7067c478bd9Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
7077c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
7087c478bd9Sstevel@tonic-gate 			report_stack_exec(p, addr);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
7127c478bd9Sstevel@tonic-gate 		/*
7137c478bd9Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
7147c478bd9Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
7157c478bd9Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
7167c478bd9Sstevel@tonic-gate 		 *
7177c478bd9Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
7187c478bd9Sstevel@tonic-gate 		 */
7197c478bd9Sstevel@tonic-gate 		if (opteron_erratum_91 &&
7207c478bd9Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
7217c478bd9Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
7227c478bd9Sstevel@tonic-gate #ifdef DEBUG
7237c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
7247c478bd9Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
7257c478bd9Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
7267c478bd9Sstevel@tonic-gate #endif /* DEBUG */
7277c478bd9Sstevel@tonic-gate 			goto out;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		if (tudebug)
7327c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
7357c478bd9Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
7367c478bd9Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
7377c478bd9Sstevel@tonic-gate 		 */
7387c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
7397c478bd9Sstevel@tonic-gate 		siginfo.si_addr = addr;
7407c478bd9Sstevel@tonic-gate 		switch (FC_CODE(res)) {
7417c478bd9Sstevel@tonic-gate 		case FC_HWERR:
7427c478bd9Sstevel@tonic-gate 		case FC_NOSUPPORT:
7437c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
7447c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
7457c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
7467c478bd9Sstevel@tonic-gate 			break;
7477c478bd9Sstevel@tonic-gate 		case FC_ALIGN:
7487c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
7497c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
7507c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 		case FC_OBJERR:
7537c478bd9Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
7547c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
7557c478bd9Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
7567c478bd9Sstevel@tonic-gate 				fault = FLTACCESS;
7577c478bd9Sstevel@tonic-gate 			}
7587c478bd9Sstevel@tonic-gate 			break;
7597c478bd9Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
7607c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
7617c478bd9Sstevel@tonic-gate 			siginfo.si_code =
7627c478bd9Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
7637c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
7647c478bd9Sstevel@tonic-gate 			break;
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 		break;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
7697c478bd9Sstevel@tonic-gate 		/*
7707c478bd9Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
7717c478bd9Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
7727c478bd9Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
7737c478bd9Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
7747c478bd9Sstevel@tonic-gate 		 * be to emulate that particular instruction.
7757c478bd9Sstevel@tonic-gate 		 */
7767c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
7777c478bd9Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_ASYSC)) {
7787c478bd9Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
7797c478bd9Sstevel@tonic-gate 				goto out;
7807c478bd9Sstevel@tonic-gate #ifdef DEBUG
7817c478bd9Sstevel@tonic-gate 			else
7827c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite syscall "
7837c478bd9Sstevel@tonic-gate 				    "instruction in process %d",
7847c478bd9Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
7857c478bd9Sstevel@tonic-gate #endif /* DEBUG */
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		if (tudebug)
7907c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
7917c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
7927c478bd9Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
7937c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
7947c478bd9Sstevel@tonic-gate 		fault = FLTILL;
7957c478bd9Sstevel@tonic-gate 		break;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
7987c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
7997c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8007c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
8017c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
8027c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
8037c478bd9Sstevel@tonic-gate 		fault = FLTIZDIV;
8047c478bd9Sstevel@tonic-gate 		break;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
8077c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8087c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8097c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
8107c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
8117c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
8127c478bd9Sstevel@tonic-gate 		fault = FLTIOVF;
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
8167c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8177c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8187c478bd9Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
8197c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
8207c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
8217c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
8227c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 		break;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
8277c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
8287c478bd9Sstevel@tonic-gate 		{
8297c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
8307c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
8317c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
8327c478bd9Sstevel@tonic-gate 			type |= USER;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
8357c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
8367c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8377c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8387c478bd9Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
8397c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8407c478bd9Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
8417c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
8427c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 		break;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
8477c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
8487c478bd9Sstevel@tonic-gate 		{
8497c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
8507c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
8517c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
8527c478bd9Sstevel@tonic-gate 			type |= USER;
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
8577c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
8587c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8597c478bd9Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
8607c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
8617c478bd9Sstevel@tonic-gate 			siginfo.si_code  = sicode;
8627c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
8637c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 		break;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
8687c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugsse)
8697c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8707c478bd9Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
8717c478bd9Sstevel@tonic-gate 			/*
8727c478bd9Sstevel@tonic-gate 			 * There are rumours that some user instructions
8737c478bd9Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
8747c478bd9Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
8757c478bd9Sstevel@tonic-gate 			 */
8767c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
8777c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
8787c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
8797c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
8807c478bd9Sstevel@tonic-gate 			fault = FLTILL;
8817c478bd9Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
8827c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
8837c478bd9Sstevel@tonic-gate 			siginfo.si_code = sicode;
8847c478bd9Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
8857c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		break;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
8907c478bd9Sstevel@tonic-gate 		/*
8917c478bd9Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
8927c478bd9Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
8937c478bd9Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
8947c478bd9Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
8957c478bd9Sstevel@tonic-gate 		 * fatal.
8967c478bd9Sstevel@tonic-gate 		 */
8977c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
8987c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
9017c478bd9Sstevel@tonic-gate 		break;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
9047c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
9057c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 		/* Now evaluate how we got here */
9087c478bd9Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
9097c478bd9Sstevel@tonic-gate 			/*
9107c478bd9Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
9117c478bd9Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
9127c478bd9Sstevel@tonic-gate 			 * the first instruction in privileged mode.
9137c478bd9Sstevel@tonic-gate 			 *
9147c478bd9Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
9157c478bd9Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
9167c478bd9Sstevel@tonic-gate 			 *
9177c478bd9Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
9187c478bd9Sstevel@tonic-gate 			 */
9197c478bd9Sstevel@tonic-gate #if defined(__amd64)
9207c478bd9Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_sysenter) {
9217c478bd9Sstevel@tonic-gate 				/*
9227c478bd9Sstevel@tonic-gate 				 * Adjust the pc so that we don't execute the
9237c478bd9Sstevel@tonic-gate 				 * swapgs instruction at the head of the
9247c478bd9Sstevel@tonic-gate 				 * handler and completely confuse things.
9257c478bd9Sstevel@tonic-gate 				 */
9267c478bd9Sstevel@tonic-gate 				rp->r_pc = (uintptr_t)
9277c478bd9Sstevel@tonic-gate 				    _sys_sysenter_post_swapgs;
9287c478bd9Sstevel@tonic-gate #elif defined(__i386)
9297c478bd9Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_call ||
9307c478bd9Sstevel@tonic-gate 			    rp->r_pc == (uintptr_t)sys_sysenter) {
9317c478bd9Sstevel@tonic-gate #endif
9327c478bd9Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
9337c478bd9Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
9347c478bd9Sstevel@tonic-gate 				cur_thread->t_post_sys = 1;
9357c478bd9Sstevel@tonic-gate 				goto cleanup;
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
9397c478bd9Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
9407c478bd9Sstevel@tonic-gate 			debug_enter((char *)NULL);
9417c478bd9Sstevel@tonic-gate 		else
9427c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
9437c478bd9Sstevel@tonic-gate 		break;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
9467c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
9477c478bd9Sstevel@tonic-gate 		goto cleanup;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
9507c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
9517c478bd9Sstevel@tonic-gate 		break;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
9547c478bd9Sstevel@tonic-gate #if defined(__amd64)
9557c478bd9Sstevel@tonic-gate 		/*
9567c478bd9Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
9577c478bd9Sstevel@tonic-gate 		 * in the virtual address hole e.g. from a copyin.
9587c478bd9Sstevel@tonic-gate 		 */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		/*
9617c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
9627c478bd9Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
9657c478bd9Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
9667c478bd9Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
9677c478bd9Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/*
9717c478bd9Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
9727c478bd9Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
9737c478bd9Sstevel@tonic-gate 		 */
9747c478bd9Sstevel@tonic-gate 		if (cur_thread->t_lofault) {
9757c478bd9Sstevel@tonic-gate 			/*
9767c478bd9Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
9777c478bd9Sstevel@tonic-gate 			 */
9787c478bd9Sstevel@tonic-gate 			if (lodebug) {
9797c478bd9Sstevel@tonic-gate 				showregs(type, rp, addr);
9807c478bd9Sstevel@tonic-gate 				traceregs(rp);
9817c478bd9Sstevel@tonic-gate 			}
9827c478bd9Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
9837c478bd9Sstevel@tonic-gate 			rp->r_pc = cur_thread->t_lofault;
9847c478bd9Sstevel@tonic-gate 			goto cleanup;
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
9877c478bd9Sstevel@tonic-gate #endif
9887c478bd9Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
9897c478bd9Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
9907c478bd9Sstevel@tonic-gate 	case T_SEGFLT:	/* segment not present fault */
9917c478bd9Sstevel@tonic-gate 		if (tudebug)
9927c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9937c478bd9Sstevel@tonic-gate 		if (kern_gpfault(rp))
9947c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
9957c478bd9Sstevel@tonic-gate 		goto cleanup;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
9987c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
9997c478bd9Sstevel@tonic-gate 		if (instr_is_syscall((caddr_t)rp->r_pc)) {
10007c478bd9Sstevel@tonic-gate 			/*
10017c478bd9Sstevel@tonic-gate 			 * System calls via the call gate come in through
10027c478bd9Sstevel@tonic-gate 			 * not-present traps.
10037c478bd9Sstevel@tonic-gate 			 *
10047c478bd9Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
10057c478bd9Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
10067c478bd9Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
10077c478bd9Sstevel@tonic-gate 			 */
10087c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
10097c478bd9Sstevel@tonic-gate 			lwp->lwp_regs = rp;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 			/*
10127c478bd9Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
10137c478bd9Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
10147c478bd9Sstevel@tonic-gate 			 * behavior here.
10157c478bd9Sstevel@tonic-gate 			 */
10167c478bd9Sstevel@tonic-gate 			mstate = LMS_USER;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 			dosyscall();
10197c478bd9Sstevel@tonic-gate 			goto out;
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
10227c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	case T_GPFLT + USER:	/* general protection violation */
10257c478bd9Sstevel@tonic-gate 		/*
10267c478bd9Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
10277c478bd9Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
10287c478bd9Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
10297c478bd9Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
10307c478bd9Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
10317c478bd9Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
10327c478bd9Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
10357c478bd9Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_SEP)) {
10367c478bd9Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
10377c478bd9Sstevel@tonic-gate 				goto out;
10387c478bd9Sstevel@tonic-gate #ifdef DEBUG
10397c478bd9Sstevel@tonic-gate 			else
10407c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite sysenter "
10417c478bd9Sstevel@tonic-gate 				    "instruction in process %d",
10427c478bd9Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
10437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
10487c478bd9Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
10497c478bd9Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
10507c478bd9Sstevel@tonic-gate 		if (tudebug)
10517c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10527c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
10537c478bd9Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
10547c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
10557c478bd9Sstevel@tonic-gate 		fault = FLTBOUNDS;
10567c478bd9Sstevel@tonic-gate 		break;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
10597c478bd9Sstevel@tonic-gate 		if (tudebug)
10607c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10617c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
10627c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
10637c478bd9Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
10647c478bd9Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
10657c478bd9Sstevel@tonic-gate 		fault = FLTACCESS;
10667c478bd9Sstevel@tonic-gate 		break;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
10697c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10707c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		/* Was it single-stepping? */
10737c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
10747c478bd9Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
10777c478bd9Sstevel@tonic-gate 			/*
10787c478bd9Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
10797c478bd9Sstevel@tonic-gate 			 * give precedence to NORMAL_STEP.  If neither is set,
10807c478bd9Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
10817c478bd9Sstevel@tonic-gate 			 * as NORMAL_STEP.
10827c478bd9Sstevel@tonic-gate 			 */
10837c478bd9Sstevel@tonic-gate 			if ((pcb->pcb_flags & NORMAL_STEP) ||
10847c478bd9Sstevel@tonic-gate 			    !(pcb->pcb_flags & WATCH_STEP)) {
10857c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
10867c478bd9Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
10877c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
10887c478bd9Sstevel@tonic-gate 				fault = FLTTRACE;
10897c478bd9Sstevel@tonic-gate 				if (pcb->pcb_flags & WATCH_STEP)
10907c478bd9Sstevel@tonic-gate 					(void) undo_watch_step(NULL);
10917c478bd9Sstevel@tonic-gate 			} else {
10927c478bd9Sstevel@tonic-gate 				fault = undo_watch_step(&siginfo);
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
10957c478bd9Sstevel@tonic-gate 		} else {
10967c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
10977c478bd9Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
10987c478bd9Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 		break;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
11037c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
11047c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11057c478bd9Sstevel@tonic-gate 		/*
11067c478bd9Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
11077c478bd9Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
11087c478bd9Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
11097c478bd9Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
11107c478bd9Sstevel@tonic-gate 		 */
11117c478bd9Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
11127c478bd9Sstevel@tonic-gate 			rp->r_pc--;
11137c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
11147c478bd9Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
11157c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
11167c478bd9Sstevel@tonic-gate 		fault = FLTBPT;
11177c478bd9Sstevel@tonic-gate 		break;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	case T_AST:
11207c478bd9Sstevel@tonic-gate 		/*
11217c478bd9Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
11227c478bd9Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
11237c478bd9Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
11247c478bd9Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
11257c478bd9Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
11267c478bd9Sstevel@tonic-gate 		 * doing the T_AST processing.
11277c478bd9Sstevel@tonic-gate 		 */
11287c478bd9Sstevel@tonic-gate 		goto cleanup;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	case T_AST + USER:		/* profiling or resched pseudo trap */
11317c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
11327c478bd9Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
11337c478bd9Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
11347c478bd9Sstevel@tonic-gate 				/*
11357c478bd9Sstevel@tonic-gate 				 * Signal performance counter overflow
11367c478bd9Sstevel@tonic-gate 				 */
11377c478bd9Sstevel@tonic-gate 				if (tudebug)
11387c478bd9Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
11397c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
11407c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
11417c478bd9Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
11427c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
11437c478bd9Sstevel@tonic-gate 				fault = FLTCPCOVF;
11447c478bd9Sstevel@tonic-gate 			}
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 		break;
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * We can't get here from a system trap
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (fault) {
11557c478bd9Sstevel@tonic-gate 		/*
11567c478bd9Sstevel@tonic-gate 		 * Remember the fault and fault adddress
11577c478bd9Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
11587c478bd9Sstevel@tonic-gate 		 */
11597c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
11607c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
11667c478bd9Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
11677c478bd9Sstevel@tonic-gate 		 * deliver the associated signal.
11687c478bd9Sstevel@tonic-gate 		 */
11697c478bd9Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
11707c478bd9Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
11717c478bd9Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
11727c478bd9Sstevel@tonic-gate 			siginfo.si_signo = 0;
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (siginfo.si_signo)
11767c478bd9Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
11797c478bd9Sstevel@tonic-gate 		profil_tick(rp->r_pc);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if (cur_thread->t_astflag | cur_thread->t_sig_check) {
11827c478bd9Sstevel@tonic-gate 		/*
11837c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
11847c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
11857c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
11867c478bd9Sstevel@tonic-gate 		 * syscall.
11877c478bd9Sstevel@tonic-gate 		 */
11887c478bd9Sstevel@tonic-gate 		astoff(cur_thread);
11897c478bd9Sstevel@tonic-gate 		cur_thread->t_sig_check = 0;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
11927c478bd9Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
11937c478bd9Sstevel@tonic-gate 			timer_lwpbind();
11947c478bd9Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/*
11997c478bd9Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
12007c478bd9Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
12017c478bd9Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
12027c478bd9Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
12037c478bd9Sstevel@tonic-gate 		 * for the kaio request to complete.
12047c478bd9Sstevel@tonic-gate 		 */
12057c478bd9Sstevel@tonic-gate 		if (p->p_aio)
12067c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
12077c478bd9Sstevel@tonic-gate 		/*
12087c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
12097c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
12107c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
12117c478bd9Sstevel@tonic-gate 		 *
12127c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
12137c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
12147c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
12157c478bd9Sstevel@tonic-gate 		 */
12167c478bd9Sstevel@tonic-gate 		if (ISHOLD(p))
12177c478bd9Sstevel@tonic-gate 			holdlwp();
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		/*
12207c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
12217c478bd9Sstevel@tonic-gate 		 * set t_astflag afterwards.
12227c478bd9Sstevel@tonic-gate 		 */
12237c478bd9Sstevel@tonic-gate 		if (ISSIG_PENDING(cur_thread, lwp, p)) {
12247c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
12257c478bd9Sstevel@tonic-gate 				psig();
12267c478bd9Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		if (cur_thread->t_rprof != NULL) {
12307c478bd9Sstevel@tonic-gate 			realsigprof(0, 0);
12317c478bd9Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
12327c478bd9Sstevel@tonic-gate 		}
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate out:	/* We can't get here from a system trap */
12367c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (ISHOLD(p))
12397c478bd9Sstevel@tonic-gate 		holdlwp();
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	/*
12427c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
12437c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
12447c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
12457c478bd9Sstevel@tonic-gate 	 *
12467c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
12477c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
12487c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (cur_thread->t_trapret) {
12537c478bd9Sstevel@tonic-gate 		cur_thread->t_trapret = 0;
12547c478bd9Sstevel@tonic-gate 		thread_lock(cur_thread);
12557c478bd9Sstevel@tonic-gate 		CL_TRAPRET(cur_thread);
12567c478bd9Sstevel@tonic-gate 		thread_unlock(cur_thread);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	if (CPU->cpu_runrun)
12597c478bd9Sstevel@tonic-gate 		preempt();
12607c478bd9Sstevel@tonic-gate 	(void) new_mstate(cur_thread, mstate);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/* Kernel probe */
12637c478bd9Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
12647c478bd9Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	return;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate cleanup:	/* system traps end up here */
12697c478bd9Sstevel@tonic-gate 	ASSERT(!(type & USER));
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate /*
12737c478bd9Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
12747c478bd9Sstevel@tonic-gate  */
12757c478bd9Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
12787c478bd9Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
12797c478bd9Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
12807c478bd9Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
12817c478bd9Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
12827c478bd9Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
12837c478bd9Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
12847c478bd9Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
12857c478bd9Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
12867c478bd9Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
12877c478bd9Sstevel@tonic-gate } kpreempt_cnts;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
12917c478bd9Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
12927c478bd9Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
12937c478bd9Sstevel@tonic-gate  */
12947c478bd9Sstevel@tonic-gate void
12957c478bd9Sstevel@tonic-gate kpreempt(int asyncspl)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
13007c478bd9Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
13017c478bd9Sstevel@tonic-gate 		return;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/*
13057c478bd9Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	do {
13087c478bd9Sstevel@tonic-gate 		if (cur_thread->t_preempt) {
13097c478bd9Sstevel@tonic-gate 			/*
13107c478bd9Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
13117c478bd9Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
13127c478bd9Sstevel@tonic-gate 			 */
13137c478bd9Sstevel@tonic-gate 			if (cur_thread->t_pri < 0)
13147c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
13157c478bd9Sstevel@tonic-gate 			else if (cur_thread->t_flag & T_INTR_THREAD) {
13167c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
13177c478bd9Sstevel@tonic-gate 				if (cur_thread->t_pil == CLOCK_LEVEL)
13187c478bd9Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
13197c478bd9Sstevel@tonic-gate 			} else
13207c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
13217c478bd9Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
13227c478bd9Sstevel@tonic-gate 			return;
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 		if (cur_thread->t_state != TS_ONPROC ||
13257c478bd9Sstevel@tonic-gate 		    cur_thread->t_disp_queue != CPU->cpu_disp) {
13267c478bd9Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
13277c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
13287c478bd9Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
13297c478bd9Sstevel@tonic-gate 				/* already in swtch(), force another */
13307c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
13317c478bd9Sstevel@tonic-gate 				siron();
13327c478bd9Sstevel@tonic-gate 			}
13337c478bd9Sstevel@tonic-gate 			return;
13347c478bd9Sstevel@tonic-gate 		}
13357c478bd9Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
13367c478bd9Sstevel@tonic-gate 			/*
13377c478bd9Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
13387c478bd9Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
13397c478bd9Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
13407c478bd9Sstevel@tonic-gate 			 */
13417c478bd9Sstevel@tonic-gate 			siron();	/* check back later */
13427c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
13437c478bd9Sstevel@tonic-gate 			return;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
13477c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
13487c478bd9Sstevel@tonic-gate 		else
13497c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		cur_thread->t_preempt++;
13527c478bd9Sstevel@tonic-gate 		preempt();
13537c478bd9Sstevel@tonic-gate 		cur_thread->t_preempt--;
13547c478bd9Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate /*
13587c478bd9Sstevel@tonic-gate  * Print out debugging info.
13597c478bd9Sstevel@tonic-gate  */
13607c478bd9Sstevel@tonic-gate static void
13617c478bd9Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	int s;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	s = spl7();
13667c478bd9Sstevel@tonic-gate 	type &= ~USER;
13677c478bd9Sstevel@tonic-gate 	if (u.u_comm[0])
13687c478bd9Sstevel@tonic-gate 		printf("%s: ", u.u_comm);
13697c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES)
13707c478bd9Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
13717c478bd9Sstevel@tonic-gate 	else
13727c478bd9Sstevel@tonic-gate 		switch (type) {
13737c478bd9Sstevel@tonic-gate 		case T_SYSCALL:
13747c478bd9Sstevel@tonic-gate 			printf("Syscall Trap:\n");
13757c478bd9Sstevel@tonic-gate 			break;
13767c478bd9Sstevel@tonic-gate 		case T_AST:
13777c478bd9Sstevel@tonic-gate 			printf("AST\n");
13787c478bd9Sstevel@tonic-gate 			break;
13797c478bd9Sstevel@tonic-gate 		default:
13807c478bd9Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
13817c478bd9Sstevel@tonic-gate 			break;
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
13847c478bd9Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
13857c478bd9Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
13867c478bd9Sstevel@tonic-gate 	} else if (addr) {
13877c478bd9Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
13917c478bd9Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
13927c478bd9Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate #if defined(__lint)
13957c478bd9Sstevel@tonic-gate 	/*
13967c478bd9Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
13977c478bd9Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
13987c478bd9Sstevel@tonic-gate 	 */
13997c478bd9Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
14007c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
14017c478bd9Sstevel@tonic-gate #else
14027c478bd9Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
14037c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
14047c478bd9Sstevel@tonic-gate #endif
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate #if defined(__amd64)
14077c478bd9Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx cr8: %lx\n", getcr2(), getcr3(), getcr8());
14087c478bd9Sstevel@tonic-gate #elif defined(__i386)
14097c478bd9Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx\n", getcr2(), getcr3());
14107c478bd9Sstevel@tonic-gate #endif
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	dumpregs(rp);
14137c478bd9Sstevel@tonic-gate 	splx(s);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static void
14177c478bd9Sstevel@tonic-gate dumpregs(struct regs *rp)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate #if defined(__amd64)
14207c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
14237c478bd9Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
14247c478bd9Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
14257c478bd9Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
14267c478bd9Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	printf(fmt, "fsb", rp->r_fsbase, "gsb", rp->r_gsbase, " ds", rp->r_ds);
14297c478bd9Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
14327c478bd9Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate #elif defined(__i386)
14377c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
14407c478bd9Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
14417c478bd9Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
14427c478bd9Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
14437c478bd9Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
14447c478bd9Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
14457c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
14467c478bd9Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
14477c478bd9Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
14487c478bd9Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate #endif	/* __i386 */
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate /*
14547c478bd9Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
14557c478bd9Sstevel@tonic-gate  *
14567c478bd9Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
14577c478bd9Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
14587c478bd9Sstevel@tonic-gate  *
14597c478bd9Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
14607c478bd9Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
14617c478bd9Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
14627c478bd9Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
14637c478bd9Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
14647c478bd9Sstevel@tonic-gate  *
14657c478bd9Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
14667c478bd9Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
14677c478bd9Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
14687c478bd9Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
14697c478bd9Sstevel@tonic-gate  * what we were going to send the lwp.)
14707c478bd9Sstevel@tonic-gate  *
14717c478bd9Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
14727c478bd9Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
14737c478bd9Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
14747c478bd9Sstevel@tonic-gate  */
14757c478bd9Sstevel@tonic-gate static int
14767c478bd9Sstevel@tonic-gate kern_gpfault(struct regs *rp)
14777c478bd9Sstevel@tonic-gate {
14787c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
14797c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
14807c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
14817c478bd9Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
14827c478bd9Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
14837c478bd9Sstevel@tonic-gate 	int v;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	extern void _sys_rtt(), sr_sup();
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate #if defined(__amd64)
14887c478bd9Sstevel@tonic-gate 	extern void _update_sregs(), _update_sregs_done();
14897c478bd9Sstevel@tonic-gate 	static const uint8_t iretq_insn[2] = { 0x48, 0xcf };
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate #elif defined(__i386)
14927c478bd9Sstevel@tonic-gate 	static const uint8_t iret_insn[1] = { 0xcf };
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	/*
14957c478bd9Sstevel@tonic-gate 	 * Note carefully the appallingly awful dependency between
14967c478bd9Sstevel@tonic-gate 	 * the instruction sequence used in __SEGREGS_POP and these
14977c478bd9Sstevel@tonic-gate 	 * instructions encoded here.
14987c478bd9Sstevel@tonic-gate 	 *
14997c478bd9Sstevel@tonic-gate 	 * XX64	Add some commentary to locore.s/privregs.h to document this.
15007c478bd9Sstevel@tonic-gate 	 */
15017c478bd9Sstevel@tonic-gate 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
15027c478bd9Sstevel@tonic-gate 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
15037c478bd9Sstevel@tonic-gate 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
15047c478bd9Sstevel@tonic-gate 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
15057c478bd9Sstevel@tonic-gate #endif
15067c478bd9Sstevel@tonic-gate 	/*
15077c478bd9Sstevel@tonic-gate 	 * if we're not an lwp, or the pc range is outside _sys_rtt, then
15087c478bd9Sstevel@tonic-gate 	 * we should immediately be die()ing horribly
15097c478bd9Sstevel@tonic-gate 	 */
15107c478bd9Sstevel@tonic-gate 	if (lwp == NULL ||
15117c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc < (uintptr_t)_sys_rtt ||
15127c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc > (uintptr_t)sr_sup)
15137c478bd9Sstevel@tonic-gate 		return (1);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	/*
15167c478bd9Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
15177c478bd9Sstevel@tonic-gate 	 *
15187c478bd9Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
15197c478bd9Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
15207c478bd9Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
15217c478bd9Sstevel@tonic-gate 	 * _sys_rtt. Ew.
15227c478bd9Sstevel@tonic-gate 	 */
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate #if defined(__amd64)
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	if (bcmp(pc, iretq_insn, sizeof (iretq_insn)) == 0) {
15277c478bd9Sstevel@tonic-gate 		/*
15287c478bd9Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iretq.
15297c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
15307c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
15317c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
15327c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
15337c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
15347c478bd9Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
15357c478bd9Sstevel@tonic-gate 		 *
15367c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
15377c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
15387c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
15397c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
15407c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
15417c478bd9Sstevel@tonic-gate 		 */
15427c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
15437c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
15447c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
15457c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
15467c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
15477c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
15487c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		/*
15517c478bd9Sstevel@tonic-gate 		 * Validate simple math
15527c478bd9Sstevel@tonic-gate 		 */
15537c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
15547c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	} else if ((lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) != 0 &&
15577c478bd9Sstevel@tonic-gate 	    pc >= (caddr_t)_update_sregs &&
15587c478bd9Sstevel@tonic-gate 	    pc < (caddr_t)_update_sregs_done) {
15597c478bd9Sstevel@tonic-gate 		/*
15607c478bd9Sstevel@tonic-gate 		 * This is the common case -- we're trying to load
15617c478bd9Sstevel@tonic-gate 		 * a bad segment register value in the only section
15627c478bd9Sstevel@tonic-gate 		 * of kernel code that ever loads segment registers.
15637c478bd9Sstevel@tonic-gate 		 *
15647c478bd9Sstevel@tonic-gate 		 * We don't need to do anything at this point because
15657c478bd9Sstevel@tonic-gate 		 * the pcb contains all the pending segment register
15667c478bd9Sstevel@tonic-gate 		 * state, and the regs are still intact because we
15677c478bd9Sstevel@tonic-gate 		 * didn't adjust the stack pointer yet.  Given the fidelity
15687c478bd9Sstevel@tonic-gate 		 * of all this, we could conceivably send a signal
15697c478bd9Sstevel@tonic-gate 		 * to the lwp, rather than core-ing.
15707c478bd9Sstevel@tonic-gate 		 */
15717c478bd9Sstevel@tonic-gate 		trp = lwptoregs(lwp);
15727c478bd9Sstevel@tonic-gate 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate #elif defined(__i386)
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0) {
15787c478bd9Sstevel@tonic-gate 		/*
15797c478bd9Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iret.
15807c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
15817c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
15827c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
15837c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
15847c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the registers that
15857c478bd9Sstevel@tonic-gate 		 * the iret was unable to consume.
15867c478bd9Sstevel@tonic-gate 		 *
15877c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
15887c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
15897c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
15907c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
15917c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
15927c478bd9Sstevel@tonic-gate 		 */
15937c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
15947c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
15957c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
15967c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
15977c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
15987c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
15997c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
16027c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	} else {
16057c478bd9Sstevel@tonic-gate 		/*
16067c478bd9Sstevel@tonic-gate 		 * Segment registers are reloaded in _sys_rtt
16077c478bd9Sstevel@tonic-gate 		 * via the following sequence:
16087c478bd9Sstevel@tonic-gate 		 *
16097c478bd9Sstevel@tonic-gate 		 *	movw	0(%esp), %gs
16107c478bd9Sstevel@tonic-gate 		 *	movw	4(%esp), %fs
16117c478bd9Sstevel@tonic-gate 		 *	movw	8(%esp), %es
16127c478bd9Sstevel@tonic-gate 		 *	movw	12(%esp), %ds
16137c478bd9Sstevel@tonic-gate 		 *	addl	$16, %esp
16147c478bd9Sstevel@tonic-gate 		 *
16157c478bd9Sstevel@tonic-gate 		 * Thus if any of them fault, we know the user
16167c478bd9Sstevel@tonic-gate 		 * registers are left unharmed on the stack.
16177c478bd9Sstevel@tonic-gate 		 */
16187c478bd9Sstevel@tonic-gate 		if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
16197c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
16207c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
16217c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
16227c478bd9Sstevel@tonic-gate 			trp = lwptoregs(lwp);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (trp == NULL)
16277c478bd9Sstevel@tonic-gate 		return (1);
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	/*
16307c478bd9Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
16317c478bd9Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
16327c478bd9Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
16337c478bd9Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
16347c478bd9Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	/*
16387c478bd9Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
16397c478bd9Sstevel@tonic-gate 	 */
16407c478bd9Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
16417c478bd9Sstevel@tonic-gate 	trp->r_err = rp->r_err;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
16447c478bd9Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16477c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
16487c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	/*
16517c478bd9Sstevel@tonic-gate 	 * Terminate all LWPs but don't discard them.  If another lwp beat us to
16527c478bd9Sstevel@tonic-gate 	 * the punch by calling exit(), evaporate now.
16537c478bd9Sstevel@tonic-gate 	 */
1654*97eda132Sraf 	proc_is_exiting(p);
16557c478bd9Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
16567c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
16577c478bd9Sstevel@tonic-gate 		lwp_exit();
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
16617c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
16627c478bd9Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
16637c478bd9Sstevel@tonic-gate #endif
16647c478bd9Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
16657c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
16667c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
16677c478bd9Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
16687c478bd9Sstevel@tonic-gate #endif
16697c478bd9Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
16707c478bd9Sstevel@tonic-gate 	return (0);
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate /*
16747c478bd9Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
16757c478bd9Sstevel@tonic-gate  */
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate #if defined(__amd64)
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate static void
16807c478bd9Sstevel@tonic-gate dump_tss(void)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
16837c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
16867c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
16877c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
16907c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
16917c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
16927c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
16937c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
16947c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
16957c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate #elif defined(__i386)
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate static void
17017c478bd9Sstevel@tonic-gate dump_tss(void)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
17047c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
17057c478bd9Sstevel@tonic-gate 
17063dfcd6dcSdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
17073dfcd6dcSdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
17083dfcd6dcSdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
17093dfcd6dcSdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
17103dfcd6dcSdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
17113dfcd6dcSdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
17123dfcd6dcSdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
17133dfcd6dcSdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
17143dfcd6dcSdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
17153dfcd6dcSdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
17163dfcd6dcSdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
17173dfcd6dcSdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
17183dfcd6dcSdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
17193dfcd6dcSdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
17203dfcd6dcSdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate int ttrace_nrec = 0;		/* number of records to dump out */
17287c478bd9Sstevel@tonic-gate int ttrace_dump_nregs = 5;	/* dump out this many records with regs too */
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate /*
17317c478bd9Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
17327c478bd9Sstevel@tonic-gate  */
17337c478bd9Sstevel@tonic-gate static void
17347c478bd9Sstevel@tonic-gate dump_ttrace(void)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
17377c478bd9Sstevel@tonic-gate 	trap_trace_rec_t *rec;
17387c478bd9Sstevel@tonic-gate 	uintptr_t current;
17397c478bd9Sstevel@tonic-gate 	int i, j, k;
17407c478bd9Sstevel@tonic-gate 	int n = NCPU;
17417c478bd9Sstevel@tonic-gate #if defined(__amd64)
17427c478bd9Sstevel@tonic-gate 	const char banner[] =
17437c478bd9Sstevel@tonic-gate 		"\ncpu          address    timestamp "
17447c478bd9Sstevel@tonic-gate 		"type  vc  handler   pc\n";
17457c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
17467c478bd9Sstevel@tonic-gate #elif defined(__i386)
17477c478bd9Sstevel@tonic-gate 	const char banner[] =
17487c478bd9Sstevel@tonic-gate 		"\ncpu address     timestamp type  vc  handler   pc\n";
17497c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
17507c478bd9Sstevel@tonic-gate #endif
17517c478bd9Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
17527c478bd9Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if (ttrace_nrec == 0)
17557c478bd9Sstevel@tonic-gate 		return;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	printf(banner);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
17607c478bd9Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
17617c478bd9Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
17627c478bd9Sstevel@tonic-gate 			continue;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
17657c478bd9Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
17667c478bd9Sstevel@tonic-gate 			struct sysent	*sys;
17677c478bd9Sstevel@tonic-gate 			struct autovec	*vec;
17687c478bd9Sstevel@tonic-gate 			extern struct av_head autovect[];
17697c478bd9Sstevel@tonic-gate 			int type;
17707c478bd9Sstevel@tonic-gate 			ulong_t	off;
17717c478bd9Sstevel@tonic-gate 			char *sym, *stype;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 			if (current < ttc->ttc_first)
17747c478bd9Sstevel@tonic-gate 				current =
17757c478bd9Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			if (current == NULL)
17787c478bd9Sstevel@tonic-gate 				continue;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
17837c478bd9Sstevel@tonic-gate 				break;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 			switch (rec->ttr_marker) {
17887c478bd9Sstevel@tonic-gate 			case TT_SYSCALL:
17897c478bd9Sstevel@tonic-gate 			case TT_SYSENTER:
17907c478bd9Sstevel@tonic-gate 			case TT_SYSC:
17917c478bd9Sstevel@tonic-gate 			case TT_SYSC64:
17927c478bd9Sstevel@tonic-gate #if defined(__amd64)
17937c478bd9Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
17947c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
17957c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
17967c478bd9Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
17977c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
17987c478bd9Sstevel@tonic-gate #elif defined(__i386)
17997c478bd9Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
18007c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
18017c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
18027c478bd9Sstevel@tonic-gate #endif
18037c478bd9Sstevel@tonic-gate 				case TT_SYSC:
18047c478bd9Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
18057c478bd9Sstevel@tonic-gate 					break;
18067c478bd9Sstevel@tonic-gate 				case TT_SYSCALL:
18077c478bd9Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
18087c478bd9Sstevel@tonic-gate 					break;
18097c478bd9Sstevel@tonic-gate 				case TT_SYSENTER:
18107c478bd9Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
18117c478bd9Sstevel@tonic-gate 					break;
18127c478bd9Sstevel@tonic-gate 				default:
18137c478bd9Sstevel@tonic-gate 					break;
18147c478bd9Sstevel@tonic-gate 				}
18157c478bd9Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
18167c478bd9Sstevel@tonic-gate 				if (sys != NULL) {
18177c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
18187c478bd9Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
18197c478bd9Sstevel@tonic-gate 					    &off);
18207c478bd9Sstevel@tonic-gate 					if (sym != NULL)
18217c478bd9Sstevel@tonic-gate 						printf("%s ", sym);
18227c478bd9Sstevel@tonic-gate 					else
18237c478bd9Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
18247c478bd9Sstevel@tonic-gate 				} else {
18257c478bd9Sstevel@tonic-gate 					printf("unknown ");
18267c478bd9Sstevel@tonic-gate 				}
18277c478bd9Sstevel@tonic-gate 				break;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 			case TT_INTERRUPT:
18307c478bd9Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
18317c478bd9Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
18327c478bd9Sstevel@tonic-gate 				if (vec != NULL) {
18337c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
18347c478bd9Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
18357c478bd9Sstevel@tonic-gate 					if (sym != NULL)
18367c478bd9Sstevel@tonic-gate 						printf("%s ", sym);
18377c478bd9Sstevel@tonic-gate 					else
18387c478bd9Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
18397c478bd9Sstevel@tonic-gate 				} else {
18407c478bd9Sstevel@tonic-gate 					printf("unknown ");
18417c478bd9Sstevel@tonic-gate 				}
18427c478bd9Sstevel@tonic-gate 				break;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 			case TT_TRAP:
18457c478bd9Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
18467c478bd9Sstevel@tonic-gate 				printf(fmt2, "trap", type);
18477c478bd9Sstevel@tonic-gate 				printf("#%s ", type < TRAP_TYPES ?
18487c478bd9Sstevel@tonic-gate 				    trap_type_mnemonic[type] : "trap");
18497c478bd9Sstevel@tonic-gate 				break;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 			default:
18527c478bd9Sstevel@tonic-gate 				break;
18537c478bd9Sstevel@tonic-gate 			}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
18567c478bd9Sstevel@tonic-gate 			if (sym != NULL)
18577c478bd9Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
18587c478bd9Sstevel@tonic-gate 			else
18597c478bd9Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
18627c478bd9Sstevel@tonic-gate 				int s;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
18657c478bd9Sstevel@tonic-gate 					printf(
18667c478bd9Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
18677c478bd9Sstevel@tonic-gate 					    rec->ttr_ipl,
18687c478bd9Sstevel@tonic-gate 					    rec->ttr_spl,
18697c478bd9Sstevel@tonic-gate 					    rec->ttr_pri);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
18747c478bd9Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 				/*
18777c478bd9Sstevel@tonic-gate 				 * print out the pc stack that we recorded
18787c478bd9Sstevel@tonic-gate 				 * at trap time (if any)
18797c478bd9Sstevel@tonic-gate 				 */
18807c478bd9Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
18817c478bd9Sstevel@tonic-gate 					uintptr_t fullpc;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
18847c478bd9Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
18857c478bd9Sstevel@tonic-gate 						break;
18867c478bd9Sstevel@tonic-gate 					}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
18917c478bd9Sstevel@tonic-gate 					if (sym != NULL)
18927c478bd9Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
18937c478bd9Sstevel@tonic-gate 						    sym, off);
18947c478bd9Sstevel@tonic-gate 					else
18957c478bd9Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
18967c478bd9Sstevel@tonic-gate 				}
18977c478bd9Sstevel@tonic-gate 				printf("\n");
18987c478bd9Sstevel@tonic-gate 			}
18997c478bd9Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate void
19077c478bd9Sstevel@tonic-gate panic_showtrap(struct trap_info *tip)
19087c478bd9Sstevel@tonic-gate {
19097c478bd9Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
19127c478bd9Sstevel@tonic-gate 	dump_ttrace();
19137c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
19167c478bd9Sstevel@tonic-gate 		dump_tss();
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate void
19207c478bd9Sstevel@tonic-gate panic_savetrap(panic_data_t *pdp, struct trap_info *tip)
19217c478bd9Sstevel@tonic-gate {
19227c478bd9Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
19237c478bd9Sstevel@tonic-gate }
1924