xref: /titanic_51/usr/src/uts/i86pc/os/trap.c (revision a4e4e13f4001644f2f960e3be0056c22b3a40fd1)
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
5d8aa0f5aSsudheer  * Common Development and Distribution License (the "License").
6d8aa0f5aSsudheer  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
297c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
307c478bd9Sstevel@tonic-gate /*								*/
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
327c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
337c478bd9Sstevel@tonic-gate /*								*/
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/signal.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/proc.h>
447c478bd9Sstevel@tonic-gate #include <sys/disp.h>
457c478bd9Sstevel@tonic-gate #include <sys/class.h>
467c478bd9Sstevel@tonic-gate #include <sys/core.h>
477c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/vm.h>
507c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
517c478bd9Sstevel@tonic-gate #include <sys/fault.h>
527c478bd9Sstevel@tonic-gate #include <sys/stack.h>
537c478bd9Sstevel@tonic-gate #include <sys/psw.h>
547c478bd9Sstevel@tonic-gate #include <sys/regset.h>
557c478bd9Sstevel@tonic-gate #include <sys/fp.h>
567c478bd9Sstevel@tonic-gate #include <sys/trap.h>
577c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
587c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
597c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
607c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/mutex_impl.h>
627c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
637c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
647c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
657c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
667c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <vm/hat.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
717c478bd9Sstevel@tonic-gate #include <vm/as.h>
727c478bd9Sstevel@tonic-gate #include <vm/seg.h>
737c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
74ae115bc7Smrj #include <vm/hat_i86.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
797c478bd9Sstevel@tonic-gate #include <sys/debug.h>
807c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
817c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
827c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
837c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
847c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
857c478bd9Sstevel@tonic-gate #include <sys/cred.h>
867c478bd9Sstevel@tonic-gate #include <sys/mman.h>
877c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
887c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
897c478bd9Sstevel@tonic-gate #include <c2/audit.h>
907c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
917c478bd9Sstevel@tonic-gate #include <sys/panic.h>
927c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
937c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
947c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
95ae115bc7Smrj #include <sys/bootconf.h>
96ae115bc7Smrj #include <sys/bootinfo.h>
97ae115bc7Smrj #include <sys/promif.h>
98ae115bc7Smrj #include <sys/mach_mmu.h>
99843e1988Sjohnlev #if defined(__xpv)
100843e1988Sjohnlev #include <sys/hypervisor.h>
101843e1988Sjohnlev #endif
10220c794b3Sgavinm #include <sys/contract/process_impl.h>
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1077c478bd9Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1087c478bd9Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1097c478bd9Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1107c478bd9Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1117c478bd9Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static const char *trap_type[] = {
1157c478bd9Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
1167c478bd9Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1177c478bd9Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1187c478bd9Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
1197c478bd9Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
1207c478bd9Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
1217c478bd9Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
1227c478bd9Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
1237c478bd9Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
1247c478bd9Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
1257c478bd9Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
1267c478bd9Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
1277c478bd9Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
1287c478bd9Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
1297c478bd9Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
1307c478bd9Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
1317c478bd9Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
1327c478bd9Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
1337c478bd9Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1347c478bd9Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1357c478bd9Sstevel@tonic-gate };
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1387c478bd9Sstevel@tonic-gate 
139ddece0baSsethg #define	SLOW_SCALL_SIZE	2
140ddece0baSsethg #define	FAST_SCALL_SIZE	2
141ddece0baSsethg 
1427c478bd9Sstevel@tonic-gate int tudebug = 0;
1437c478bd9Sstevel@tonic-gate int tudebugbpt = 0;
1447c478bd9Sstevel@tonic-gate int tudebugfpe = 0;
1457c478bd9Sstevel@tonic-gate int tudebugsse = 0;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1487c478bd9Sstevel@tonic-gate int tdebug = 0;
1497c478bd9Sstevel@tonic-gate int lodebug = 0;
1507c478bd9Sstevel@tonic-gate int faultdebug = 0;
1517c478bd9Sstevel@tonic-gate #else
1527c478bd9Sstevel@tonic-gate #define	tdebug	0
1537c478bd9Sstevel@tonic-gate #define	lodebug	0
1547c478bd9Sstevel@tonic-gate #define	faultdebug	0
1557c478bd9Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
1587c478bd9Sstevel@tonic-gate static void dump_ttrace(void);
1597c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1607c478bd9Sstevel@tonic-gate static void dumpregs(struct regs *);
1617c478bd9Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1627c478bd9Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1657c478bd9Sstevel@tonic-gate static int
1667c478bd9Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1677c478bd9Sstevel@tonic-gate {
168843e1988Sjohnlev 	struct panic_trap_info ti;
1697c478bd9Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1727c478bd9Sstevel@tonic-gate 		trap_name = trap_type[type];
1737c478bd9Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1747c478bd9Sstevel@tonic-gate 	} else {
1757c478bd9Sstevel@tonic-gate 		trap_name = "trap";
1767c478bd9Sstevel@tonic-gate 		trap_mnemonic = "-";
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
1807c478bd9Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	ti.trap_regs = rp;
1847c478bd9Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
1857c478bd9Sstevel@tonic-gate 	ti.trap_addr = addr;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
1907c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
1917c478bd9Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
1927c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
1937c478bd9Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
1947c478bd9Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
1957c478bd9Sstevel@tonic-gate 		    "a NULL pointer dereference" :
1967c478bd9Sstevel@tonic-gate 		    "an illegal access to a user address");
1977c478bd9Sstevel@tonic-gate 	} else
1987c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
1997c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
2007c478bd9Sstevel@tonic-gate 	return (0);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static int
2107c478bd9Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2157c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2167c478bd9Sstevel@tonic-gate 		return (1);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2237c478bd9Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2247c478bd9Sstevel@tonic-gate  * instruction.
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2277c478bd9Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
228ddece0baSsethg  * int $T_SYSCALLINT is two bytes: 0xCD 0x91
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static int
232ddece0baSsethg instr_is_other_syscall(caddr_t pc, int which)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2357c478bd9Sstevel@tonic-gate 
236ddece0baSsethg 	ASSERT(which == X86_SEP || which == X86_ASYSC || which == 0xCD);
2377c478bd9Sstevel@tonic-gate 
238ddece0baSsethg 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0)
2397c478bd9Sstevel@tonic-gate 		return (0);
2407c478bd9Sstevel@tonic-gate 
241ddece0baSsethg 	switch (which) {
242ddece0baSsethg 	case X86_SEP:
243ddece0baSsethg 		if (instr[0] == 0x0F && instr[1] == 0x34)
2447c478bd9Sstevel@tonic-gate 			return (1);
245ddece0baSsethg 		break;
246ddece0baSsethg 	case X86_ASYSC:
247ddece0baSsethg 		if (instr[0] == 0x0F && instr[1] == 0x05)
248ddece0baSsethg 			return (1);
249ddece0baSsethg 		break;
250ddece0baSsethg 	case 0xCD:
251ddece0baSsethg 		if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT)
252ddece0baSsethg 			return (1);
253ddece0baSsethg 		break;
254ddece0baSsethg 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
259ddece0baSsethg static const char *
260ddece0baSsethg syscall_insn_string(int syscall_insn)
261ddece0baSsethg {
262ddece0baSsethg 	switch (syscall_insn) {
263ddece0baSsethg 	case X86_SEP:
264ddece0baSsethg 		return ("sysenter");
265ddece0baSsethg 	case X86_ASYSC:
266ddece0baSsethg 		return ("syscall");
267ddece0baSsethg 	case 0xCD:
268ddece0baSsethg 		return ("int");
269ddece0baSsethg 	default:
270ddece0baSsethg 		return ("Unknown");
271ddece0baSsethg 	}
272ddece0baSsethg }
273ddece0baSsethg 
274ddece0baSsethg static int
275ddece0baSsethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn)
276ddece0baSsethg {
277ddece0baSsethg 	caddr_t	linearpc;
278ddece0baSsethg 	int return_code = 0;
279ddece0baSsethg 
280ddece0baSsethg 	mutex_enter(&p->p_ldtlock);	/* Must be held across linear_pc() */
281ddece0baSsethg 
282ddece0baSsethg 	if (linear_pc(rp, p, &linearpc) == 0) {
283ddece0baSsethg 
284ddece0baSsethg 		/*
285ddece0baSsethg 		 * If another thread beat us here, it already changed
286ddece0baSsethg 		 * this site to the slower (int) syscall instruction.
287ddece0baSsethg 		 */
288ddece0baSsethg 		if (instr_is_other_syscall(linearpc, 0xCD)) {
289ddece0baSsethg 			return_code = 1;
290ddece0baSsethg 		} else if (instr_is_other_syscall(linearpc, syscall_insn)) {
291ddece0baSsethg 
292ddece0baSsethg 			if (rewrite_syscall(linearpc) == 0) {
293ddece0baSsethg 				return_code = 1;
294ddece0baSsethg 			}
295ddece0baSsethg #ifdef DEBUG
296ddece0baSsethg 			else
297ddece0baSsethg 				cmn_err(CE_WARN, "failed to rewrite %s "
298ddece0baSsethg 				    "instruction in process %d",
299ddece0baSsethg 				    syscall_insn_string(syscall_insn),
300ddece0baSsethg 				    p->p_pid);
301ddece0baSsethg #endif /* DEBUG */
302ddece0baSsethg 		}
303ddece0baSsethg 	}
304ddece0baSsethg 
305ddece0baSsethg 	mutex_exit(&p->p_ldtlock);	/* Must be held across linear_pc() */
306ddece0baSsethg 
307ddece0baSsethg 	return (return_code);
308ddece0baSsethg }
309ddece0baSsethg 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
3127c478bd9Sstevel@tonic-gate  *
3137c478bd9Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
3147c478bd9Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
3157c478bd9Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #define	LCALLSIZE	7
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static int
321ddece0baSsethg instr_is_lcall_syscall(caddr_t pc)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
3267c478bd9Sstevel@tonic-gate 	    instr[0] == 0x9a &&
3277c478bd9Sstevel@tonic-gate 	    instr[1] == 0 &&
3287c478bd9Sstevel@tonic-gate 	    instr[2] == 0 &&
3297c478bd9Sstevel@tonic-gate 	    instr[3] == 0 &&
3307c478bd9Sstevel@tonic-gate 	    instr[4] == 0 &&
3317c478bd9Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
3327c478bd9Sstevel@tonic-gate 	    instr[6] == 0)
3337c478bd9Sstevel@tonic-gate 		return (1);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
338130a12b8Sfvdl #ifdef __amd64
339130a12b8Sfvdl 
340130a12b8Sfvdl /*
341ae115bc7Smrj  * In the first revisions of amd64 CPUs produced by AMD, the LAHF and
342ae115bc7Smrj  * SAHF instructions were not implemented in 64-bit mode. Later revisions
343130a12b8Sfvdl  * did implement these instructions. An extension to the cpuid instruction
344130a12b8Sfvdl  * was added to check for the capability of executing these instructions
345ae115bc7Smrj  * in 64-bit mode.
346130a12b8Sfvdl  *
347130a12b8Sfvdl  * Intel originally did not implement these instructions in EM64T either,
348130a12b8Sfvdl  * but added them in later revisions.
349130a12b8Sfvdl  *
350130a12b8Sfvdl  * So, there are different chip revisions by both vendors out there that
351130a12b8Sfvdl  * may or may not implement these instructions. The easy solution is to
352130a12b8Sfvdl  * just always emulate these instructions on demand.
353130a12b8Sfvdl  *
354130a12b8Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
355130a12b8Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
356130a12b8Sfvdl  */
357130a12b8Sfvdl 
358130a12b8Sfvdl #define	LSAHFSIZE 1
359130a12b8Sfvdl 
360130a12b8Sfvdl static int
361130a12b8Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
362130a12b8Sfvdl {
363130a12b8Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
364130a12b8Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
365130a12b8Sfvdl 		return (1);
366130a12b8Sfvdl 	return (0);
367130a12b8Sfvdl }
368130a12b8Sfvdl 
369130a12b8Sfvdl /*
370130a12b8Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
371130a12b8Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
372130a12b8Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
373130a12b8Sfvdl  *
374130a12b8Sfvdl  * Note that %ah is bits 8-15 of %rax.
375130a12b8Sfvdl  */
376130a12b8Sfvdl static void
377130a12b8Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
378130a12b8Sfvdl {
379130a12b8Sfvdl 	if (instr == 0x9e) {
380130a12b8Sfvdl 		/* sahf. Copy bits from %ah to flags. */
381130a12b8Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
382130a12b8Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
383130a12b8Sfvdl 	} else {
384130a12b8Sfvdl 		/* lahf. Copy bits from flags to %ah. */
385130a12b8Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
386130a12b8Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
387130a12b8Sfvdl 	}
388130a12b8Sfvdl 	rp->r_pc += LSAHFSIZE;
389130a12b8Sfvdl }
390130a12b8Sfvdl #endif /* __amd64 */
391130a12b8Sfvdl 
3927c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
3967c478bd9Sstevel@tonic-gate  *
3977c478bd9Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
3987c478bd9Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
3997c478bd9Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate #define	PREFETCHSIZE 3
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static int
4057c478bd9Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
4087c478bd9Sstevel@tonic-gate 		return (1);
4097c478bd9Sstevel@tonic-gate 	return (0);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate static int
4137c478bd9Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
4167c478bd9Sstevel@tonic-gate 	int	error;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
4217c478bd9Sstevel@tonic-gate 		return (1);
4227c478bd9Sstevel@tonic-gate 	return (0);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
4297c478bd9Sstevel@tonic-gate  *
4307c478bd9Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
4317c478bd9Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
432ae115bc7Smrj  * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1)
433ae115bc7Smrj  * however, there are paths that arrive here with PS_IE == 0 so special care
434ae115bc7Smrj  * must be taken in those cases.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate void
4377c478bd9Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
4387c478bd9Sstevel@tonic-gate {
439ae115bc7Smrj 	kthread_t *ct = curthread;
4407c478bd9Sstevel@tonic-gate 	enum seg_rw rw;
4417c478bd9Sstevel@tonic-gate 	unsigned type;
442ae115bc7Smrj 	proc_t *p = ttoproc(ct);
443ae115bc7Smrj 	klwp_t *lwp = ttolwp(ct);
4447c478bd9Sstevel@tonic-gate 	uintptr_t lofault;
4457c478bd9Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
4467c478bd9Sstevel@tonic-gate 	enum fault_type fault_type;
4477c478bd9Sstevel@tonic-gate 	k_siginfo_t siginfo;
4487c478bd9Sstevel@tonic-gate 	uint_t fault = 0;
4497c478bd9Sstevel@tonic-gate 	int mstate;
4507c478bd9Sstevel@tonic-gate 	int sicode = 0;
4517c478bd9Sstevel@tonic-gate 	int watchcode;
4527c478bd9Sstevel@tonic-gate 	int watchpage;
4537c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
4549acbbeafSnn35248 	int singlestep_twiddle;
4557c478bd9Sstevel@tonic-gate 	size_t sz;
4567c478bd9Sstevel@tonic-gate 	int ta;
457130a12b8Sfvdl #ifdef __amd64
458130a12b8Sfvdl 	uchar_t instr;
459130a12b8Sfvdl #endif
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	type = rp->r_trapno;
4647c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
465ae115bc7Smrj 	ASSERT(ct->t_schedflag & TS_DONT_SWAP);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		errcode = rp->r_err;
4707c478bd9Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
4717c478bd9Sstevel@tonic-gate 			rw = S_WRITE;
4727c478bd9Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
4737c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
4747c478bd9Sstevel@tonic-gate 			rw = S_EXEC;
4757c478bd9Sstevel@tonic-gate 		else
4767c478bd9Sstevel@tonic-gate 			rw = S_READ;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #if defined(__i386)
4797c478bd9Sstevel@tonic-gate 		/*
4807c478bd9Sstevel@tonic-gate 		 * Pentium Pro work-around
4817c478bd9Sstevel@tonic-gate 		 */
4827c478bd9Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
4837c478bd9Sstevel@tonic-gate 			uint_t	attr;
4847c478bd9Sstevel@tonic-gate 			uint_t	priv_violation;
4857c478bd9Sstevel@tonic-gate 			uint_t	access_violation;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
4887c478bd9Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
4897c478bd9Sstevel@tonic-gate 			    == -1) {
4907c478bd9Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
4917c478bd9Sstevel@tonic-gate 			} else {
4927c478bd9Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
4937c478bd9Sstevel@tonic-gate 				    !(attr & PROT_USER);
4947c478bd9Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
4957c478bd9Sstevel@tonic-gate 				    !(attr & PROT_WRITE);
4967c478bd9Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
4977c478bd9Sstevel@tonic-gate 					goto cleanup;
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate #endif /* __i386 */
5017c478bd9Sstevel@tonic-gate 
502ae115bc7Smrj 	} else if (type == T_SGLSTP && lwp != NULL)
503ae115bc7Smrj 		lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (tdebug)
5067c478bd9Sstevel@tonic-gate 		showregs(type, rp, addr);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
5117c478bd9Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
5127c478bd9Sstevel@tonic-gate 		 * The current process credential applies to the thread for
5137c478bd9Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
5147c478bd9Sstevel@tonic-gate 		 * should already be set up.
5157c478bd9Sstevel@tonic-gate 		 */
516ae115bc7Smrj 		if (ct->t_cred != p->p_cred) {
517ae115bc7Smrj 			cred_t *oldcred = ct->t_cred;
5187c478bd9Sstevel@tonic-gate 			/*
5197c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
5207c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
5217c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
5227c478bd9Sstevel@tonic-gate 			 */
523ae115bc7Smrj 			ct->t_cred = crgetcred();
5247c478bd9Sstevel@tonic-gate 			crfree(oldcred);
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		ASSERT(lwp != NULL);
5277c478bd9Sstevel@tonic-gate 		type |= USER;
5287c478bd9Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
5297c478bd9Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		switch (type) {
5327c478bd9Sstevel@tonic-gate 		case T_PGFLT + USER:
5337c478bd9Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
5347c478bd9Sstevel@tonic-gate 				mstate = LMS_TFAULT;
5357c478bd9Sstevel@tonic-gate 			else
5367c478bd9Sstevel@tonic-gate 				mstate = LMS_DFAULT;
5377c478bd9Sstevel@tonic-gate 			break;
5387c478bd9Sstevel@tonic-gate 		default:
5397c478bd9Sstevel@tonic-gate 			mstate = LMS_TRAP;
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 		/* Kernel probe */
5437c478bd9Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
5447c478bd9Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
545ae115bc7Smrj 		mstate = new_mstate(ct, mstate);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	switch (type) {
5517c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:
5527c478bd9Sstevel@tonic-gate 	case T_SGLSTP:
5537c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER:
5547c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:
5557c478bd9Sstevel@tonic-gate 		break;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	default:
5587c478bd9Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
5597c478bd9Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
5607c478bd9Sstevel@tonic-gate 		break;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	switch (type) {
5647c478bd9Sstevel@tonic-gate 	default:
5657c478bd9Sstevel@tonic-gate 		if (type & USER) {
5667c478bd9Sstevel@tonic-gate 			if (tudebug)
5677c478bd9Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5687c478bd9Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5697c478bd9Sstevel@tonic-gate 			    type & ~USER);
5707c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5717c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
5727c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
5737c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
5747c478bd9Sstevel@tonic-gate 			fault = FLTILL;
5757c478bd9Sstevel@tonic-gate 			break;
5767c478bd9Sstevel@tonic-gate 		} else {
5777c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5787c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
58420c794b3Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
58520c794b3Sgavinm 		 * via the installed trampoline.
5867c478bd9Sstevel@tonic-gate 		 */
587ae115bc7Smrj 		if ((ct->t_ontrap != NULL) &&
588ae115bc7Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
589ae115bc7Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
59020c794b3Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
59120c794b3Sgavinm 			goto cleanup;
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
5967c478bd9Sstevel@tonic-gate 		 * across this. Here we assume that an address
5977c478bd9Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
5987c478bd9Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
5997c478bd9Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
6007c478bd9Sstevel@tonic-gate 		 * starting and because we know that we always have
6017c478bd9Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
6027c478bd9Sstevel@tonic-gate 		 */
603ae115bc7Smrj 		lofault = ct->t_lofault;
604ae115bc7Smrj 		ct->t_lofault = 0;
6057c478bd9Sstevel@tonic-gate 
606ae115bc7Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6097c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
6107c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6117c478bd9Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6127c478bd9Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6137c478bd9Sstevel@tonic-gate 			    grow(addr))
6147c478bd9Sstevel@tonic-gate 				res = 0;
6157c478bd9Sstevel@tonic-gate 		} else {
6167c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
6177c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6187c478bd9Sstevel@tonic-gate 		}
619ae115bc7Smrj 		(void) new_mstate(ct, mstate);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
6237c478bd9Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6247c478bd9Sstevel@tonic-gate 		 */
625ae115bc7Smrj 		ct->t_lofault = lofault;
6267c478bd9Sstevel@tonic-gate 		if (res == 0)
6277c478bd9Sstevel@tonic-gate 			goto cleanup;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6307c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6317c478bd9Sstevel@tonic-gate 			/*
6327c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6337c478bd9Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6347c478bd9Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6357c478bd9Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6367c478bd9Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6377c478bd9Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6387c478bd9Sstevel@tonic-gate 			 */
6397c478bd9Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6427c478bd9Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6437c478bd9Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6447c478bd9Sstevel@tonic-gate 				    PFN_INVALID &&
6457c478bd9Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6467c478bd9Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6477c478bd9Sstevel@tonic-gate 					rp->r_pc = rip;
6487c478bd9Sstevel@tonic-gate 					goto cleanup;
6497c478bd9Sstevel@tonic-gate 				}
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6557c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6567c478bd9Sstevel@tonic-gate 			/*
6577c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6587c478bd9Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6597c478bd9Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6607c478bd9Sstevel@tonic-gate 			 * instruction.
6617c478bd9Sstevel@tonic-gate 			 */
6627c478bd9Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			/*
6657c478bd9Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6667c478bd9Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
6677c478bd9Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
6687c478bd9Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
6697c478bd9Sstevel@tonic-gate 			 */
6707c478bd9Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
6717c478bd9Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
6727c478bd9Sstevel@tonic-gate #ifdef DEBUG
6737c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
6747c478bd9Sstevel@tonic-gate 					    "occurred: kernel prefetch"
6757c478bd9Sstevel@tonic-gate 					    " at %p generated a page fault!",
6767c478bd9Sstevel@tonic-gate 					    (void *)rp->r_pc);
6777c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6787c478bd9Sstevel@tonic-gate 					goto cleanup;
6797c478bd9Sstevel@tonic-gate 				}
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		if (lofault == 0)
6867c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		/*
6897c478bd9Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
6907c478bd9Sstevel@tonic-gate 		 */
6917c478bd9Sstevel@tonic-gate 		if (lodebug) {
6927c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
6937c478bd9Sstevel@tonic-gate 			traceregs(rp);
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
6967c478bd9Sstevel@tonic-gate 			res = FC_ERRNO(res);
6977c478bd9Sstevel@tonic-gate 		else
6987c478bd9Sstevel@tonic-gate 			res = EFAULT;
6997c478bd9Sstevel@tonic-gate 		rp->r_r0 = res;
700ae115bc7Smrj 		rp->r_pc = ct->t_lofault;
7017c478bd9Sstevel@tonic-gate 		goto cleanup;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7047c478bd9Sstevel@tonic-gate 		if (faultdebug) {
7057c478bd9Sstevel@tonic-gate 			char *fault_str;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			switch (rw) {
7087c478bd9Sstevel@tonic-gate 			case S_READ:
7097c478bd9Sstevel@tonic-gate 				fault_str = "read";
7107c478bd9Sstevel@tonic-gate 				break;
7117c478bd9Sstevel@tonic-gate 			case S_WRITE:
7127c478bd9Sstevel@tonic-gate 				fault_str = "write";
7137c478bd9Sstevel@tonic-gate 				break;
7147c478bd9Sstevel@tonic-gate 			case S_EXEC:
7157c478bd9Sstevel@tonic-gate 				fault_str = "exec";
7167c478bd9Sstevel@tonic-gate 				break;
7177c478bd9Sstevel@tonic-gate 			default:
7187c478bd9Sstevel@tonic-gate 				fault_str = "";
7197c478bd9Sstevel@tonic-gate 				break;
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7227c478bd9Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7267c478bd9Sstevel@tonic-gate 		/*
7277c478bd9Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7287c478bd9Sstevel@tonic-gate 		 *
7297c478bd9Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7307c478bd9Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7317c478bd9Sstevel@tonic-gate 		 * address will be
7327c478bd9Sstevel@tonic-gate 		 *
7337c478bd9Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7347c478bd9Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7357c478bd9Sstevel@tonic-gate 		 *
7367c478bd9Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7377c478bd9Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7387c478bd9Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7417c478bd9Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7427c478bd9Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7437c478bd9Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7447c478bd9Sstevel@tonic-gate 			if (!opteron_erratum_100)
7457c478bd9Sstevel@tonic-gate 				panic("unexpected erratum #100");
7467c478bd9Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7477c478bd9Sstevel@tonic-gate 				goto out;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7527c478bd9Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7537c478bd9Sstevel@tonic-gate #ifdef __i386
7547c478bd9Sstevel@tonic-gate 		/*
7557c478bd9Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
7567c478bd9Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
7577c478bd9Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
7587c478bd9Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
7597c478bd9Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
7607c478bd9Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
7617c478bd9Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
7627c478bd9Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
7637c478bd9Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
7647c478bd9Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
767ddece0baSsethg 		    rw == S_READ && instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
7687c478bd9Sstevel@tonic-gate 			extern void watch_syscall(void);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
7717c478bd9Sstevel@tonic-gate 			watch_syscall();	/* never returns */
7727c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate #endif /* __i386 */
7757c478bd9Sstevel@tonic-gate 		vaddr = addr;
7767c478bd9Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
7777c478bd9Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
7787c478bd9Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
7797c478bd9Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
7807c478bd9Sstevel@tonic-gate 			if (ta) {
7817c478bd9Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
7827c478bd9Sstevel@tonic-gate 				    watchcode, rp->r_pc);
7837c478bd9Sstevel@tonic-gate 				fault_type = F_INVAL;
7847c478bd9Sstevel@tonic-gate 			} else {
7857c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7867c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
7877c478bd9Sstevel@tonic-gate 				siginfo.si_code = watchcode;
7887c478bd9Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
7897c478bd9Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
7907c478bd9Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
7917c478bd9Sstevel@tonic-gate 				fault = FLTWATCH;
7927c478bd9Sstevel@tonic-gate 				break;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 		} else {
7957c478bd9Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
7967c478bd9Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
7977c478bd9Sstevel@tonic-gate 				goto out;
7987c478bd9Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
7997c478bd9Sstevel@tonic-gate 			fault_type = F_INVAL;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		/*
8057c478bd9Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8067c478bd9Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8077c478bd9Sstevel@tonic-gate 		 */
8087c478bd9Sstevel@tonic-gate 		if (res == 0 ||
8097c478bd9Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8107c478bd9Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8117c478bd9Sstevel@tonic-gate 		    grow(addr))) {
8127c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8137c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8147c478bd9Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8157c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8167c478bd9Sstevel@tonic-gate 				siginfo.si_addr = addr;
8177c478bd9Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 			goto out;
8207c478bd9Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8217c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8227c478bd9Sstevel@tonic-gate 			report_stack_exec(p, addr);
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8267c478bd9Sstevel@tonic-gate 		/*
8277c478bd9Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8287c478bd9Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8297c478bd9Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8307c478bd9Sstevel@tonic-gate 		 *
8317c478bd9Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8347c478bd9Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8357c478bd9Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8367c478bd9Sstevel@tonic-gate #ifdef DEBUG
8377c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8387c478bd9Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8397c478bd9Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8407c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8417c478bd9Sstevel@tonic-gate 			goto out;
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		if (tudebug)
8467c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8477c478bd9Sstevel@tonic-gate 		/*
8487c478bd9Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8497c478bd9Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8507c478bd9Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8517c478bd9Sstevel@tonic-gate 		 */
8527c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8537c478bd9Sstevel@tonic-gate 		siginfo.si_addr = addr;
8547c478bd9Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8557c478bd9Sstevel@tonic-gate 		case FC_HWERR:
8567c478bd9Sstevel@tonic-gate 		case FC_NOSUPPORT:
8577c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8587c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8597c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		case FC_ALIGN:
8627c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8637c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8647c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
8657c478bd9Sstevel@tonic-gate 			break;
8667c478bd9Sstevel@tonic-gate 		case FC_OBJERR:
8677c478bd9Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8687c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8697c478bd9Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8707c478bd9Sstevel@tonic-gate 				fault = FLTACCESS;
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 			break;
8737c478bd9Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
8747c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8757c478bd9Sstevel@tonic-gate 			siginfo.si_code =
8767c478bd9Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
8777c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		break;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
8837c478bd9Sstevel@tonic-gate 		/*
8847c478bd9Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
8857c478bd9Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
8867c478bd9Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
8877c478bd9Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
8887c478bd9Sstevel@tonic-gate 		 * be to emulate that particular instruction.
8897c478bd9Sstevel@tonic-gate 		 */
8907c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
891ddece0baSsethg 		    ldt_rewrite_syscall(rp, p, X86_ASYSC))
8927c478bd9Sstevel@tonic-gate 			goto out;
893130a12b8Sfvdl 
894130a12b8Sfvdl #ifdef __amd64
895130a12b8Sfvdl 		/*
896130a12b8Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
897130a12b8Sfvdl 		 * See the instr_is_lsahf function for details.
898130a12b8Sfvdl 		 */
899130a12b8Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
900130a12b8Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
901130a12b8Sfvdl 			emulate_lsahf(rp, instr);
902130a12b8Sfvdl 			goto out;
903130a12b8Sfvdl 		}
904130a12b8Sfvdl #endif
905130a12b8Sfvdl 
9067c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		if (tudebug)
9097c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9107c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9117c478bd9Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9127c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9137c478bd9Sstevel@tonic-gate 		fault = FLTILL;
9147c478bd9Sstevel@tonic-gate 		break;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9177c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9187c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9197c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9207c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9217c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9227c478bd9Sstevel@tonic-gate 		fault = FLTIZDIV;
9237c478bd9Sstevel@tonic-gate 		break;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9267c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9277c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9287c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9297c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9307c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9317c478bd9Sstevel@tonic-gate 		fault = FLTIOVF;
9327c478bd9Sstevel@tonic-gate 		break;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9357c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9367c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9377c478bd9Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
9387c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9397c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
9407c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9417c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		break;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
9467c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9477c478bd9Sstevel@tonic-gate 		{
9487c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9497c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
9507c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9517c478bd9Sstevel@tonic-gate 			type |= USER;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
9547c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
9557c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9567c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9577c478bd9Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
9587c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
9597c478bd9Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
9607c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9617c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		break;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
9667c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
9677c478bd9Sstevel@tonic-gate 		{
9687c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
9697c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
9707c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
9717c478bd9Sstevel@tonic-gate 			type |= USER;
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
9767c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9777c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9787c478bd9Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
9797c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9807c478bd9Sstevel@tonic-gate 			siginfo.si_code  = sicode;
9817c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9827c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 		break;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
9877c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugsse)
9887c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9897c478bd9Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
9907c478bd9Sstevel@tonic-gate 			/*
9917c478bd9Sstevel@tonic-gate 			 * There are rumours that some user instructions
9927c478bd9Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
9937c478bd9Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
9947c478bd9Sstevel@tonic-gate 			 */
9957c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
9967c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
9977c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9987c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
9997c478bd9Sstevel@tonic-gate 			fault = FLTILL;
10007c478bd9Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
10017c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
10027c478bd9Sstevel@tonic-gate 			siginfo.si_code = sicode;
10037c478bd9Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
10047c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		break;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10097c478bd9Sstevel@tonic-gate 		/*
10107c478bd9Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10117c478bd9Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10127c478bd9Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10137c478bd9Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10147c478bd9Sstevel@tonic-gate 		 * fatal.
10157c478bd9Sstevel@tonic-gate 		 */
10167c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10177c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10207c478bd9Sstevel@tonic-gate 		break;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 		/* Now evaluate how we got here */
10257c478bd9Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
10267c478bd9Sstevel@tonic-gate 			/*
10277c478bd9Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
10287c478bd9Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
10297c478bd9Sstevel@tonic-gate 			 * the first instruction in privileged mode.
10307c478bd9Sstevel@tonic-gate 			 *
10317c478bd9Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
10327c478bd9Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
10337c478bd9Sstevel@tonic-gate 			 *
10347c478bd9Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
10357c478bd9Sstevel@tonic-gate 			 */
10369acbbeafSnn35248 			singlestep_twiddle = 0;
10379acbbeafSnn35248 			if (rp->r_pc == (uintptr_t)sys_sysenter ||
10389acbbeafSnn35248 			    rp->r_pc == (uintptr_t)brand_sys_sysenter) {
10399acbbeafSnn35248 				singlestep_twiddle = 1;
10407c478bd9Sstevel@tonic-gate #if defined(__amd64)
10417c478bd9Sstevel@tonic-gate 				/*
10429acbbeafSnn35248 				 * Since we are already on the kernel's
10439acbbeafSnn35248 				 * %gs, on 64-bit systems the sysenter case
10449acbbeafSnn35248 				 * needs to adjust the pc to avoid
10459acbbeafSnn35248 				 * executing the swapgs instruction at the
10469acbbeafSnn35248 				 * top of the handler.
10477c478bd9Sstevel@tonic-gate 				 */
10489acbbeafSnn35248 				if (rp->r_pc == (uintptr_t)sys_sysenter)
10497c478bd9Sstevel@tonic-gate 					rp->r_pc = (uintptr_t)
10507c478bd9Sstevel@tonic-gate 					    _sys_sysenter_post_swapgs;
10519acbbeafSnn35248 				else
10529acbbeafSnn35248 					rp->r_pc = (uintptr_t)
10539acbbeafSnn35248 					    _brand_sys_sysenter_post_swapgs;
10547c478bd9Sstevel@tonic-gate #endif
10559acbbeafSnn35248 			}
10569acbbeafSnn35248 #if defined(__i386)
10579acbbeafSnn35248 			else if (rp->r_pc == (uintptr_t)sys_call ||
10589acbbeafSnn35248 			    rp->r_pc == (uintptr_t)brand_sys_call) {
10599acbbeafSnn35248 				singlestep_twiddle = 1;
10609acbbeafSnn35248 			}
10619acbbeafSnn35248 #endif
10626db33255Sdmick 			else {
10636db33255Sdmick 				/* not on sysenter/syscall; uregs available */
10646db33255Sdmick 				if (tudebug && tudebugbpt)
10656db33255Sdmick 					showregs(type, rp, (caddr_t)0);
10666db33255Sdmick 			}
10679acbbeafSnn35248 			if (singlestep_twiddle) {
10687c478bd9Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
10697c478bd9Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
1070ae115bc7Smrj 				ct->t_post_sys = 1;
1071d8aa0f5aSsudheer 				aston(curthread);
10727c478bd9Sstevel@tonic-gate 				goto cleanup;
10737c478bd9Sstevel@tonic-gate 			}
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
10767c478bd9Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
10777c478bd9Sstevel@tonic-gate 			debug_enter((char *)NULL);
10787c478bd9Sstevel@tonic-gate 		else
10797c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10807c478bd9Sstevel@tonic-gate 		break;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
10837c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
10847c478bd9Sstevel@tonic-gate 		goto cleanup;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
10877c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
10887c478bd9Sstevel@tonic-gate 		break;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
10917c478bd9Sstevel@tonic-gate 		/*
1092*a4e4e13fSgavinm 		 * Any #GP that occurs during an on_trap .. no_trap bracket
1093*a4e4e13fSgavinm 		 * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection,
1094*a4e4e13fSgavinm 		 * or in a on_fault .. no_fault bracket, is forgiven
1095*a4e4e13fSgavinm 		 * and we trampoline.  This protection is given regardless
1096*a4e4e13fSgavinm 		 * of whether we are 32/64 bit etc - if a distinction is
1097*a4e4e13fSgavinm 		 * required then define new on_trap protection types.
1098*a4e4e13fSgavinm 		 *
10997c478bd9Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
1100843e1988Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
1101843e1988Sjohnlev 		 * update_sregs while updating user segment registers.
1102843e1988Sjohnlev 		 *
1103843e1988Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
1104843e1988Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
1105843e1988Sjohnlev 		 * lives which is protected by segmentation.
11067c478bd9Sstevel@tonic-gate 		 */
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		/*
11097c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
111020c794b3Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
1111843e1988Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
11127c478bd9Sstevel@tonic-gate 		 */
1113ae115bc7Smrj 		if (ct->t_ontrap != NULL) {
1114843e1988Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
1115843e1988Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
1116ae115bc7Smrj 
1117843e1988Sjohnlev 			if (ttype != 0) {
1118843e1988Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
1119843e1988Sjohnlev 				if (tudebug)
1120843e1988Sjohnlev 					showregs(type, rp, (caddr_t)0);
112120c794b3Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
112220c794b3Sgavinm 				goto cleanup;
11237c478bd9Sstevel@tonic-gate 			}
1124843e1988Sjohnlev 		}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		/*
11277c478bd9Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
11287c478bd9Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
11297c478bd9Sstevel@tonic-gate 		 */
1130ae115bc7Smrj 		if (ct->t_lofault) {
11317c478bd9Sstevel@tonic-gate 			/*
11327c478bd9Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11337c478bd9Sstevel@tonic-gate 			 */
11347c478bd9Sstevel@tonic-gate 			if (lodebug) {
11357c478bd9Sstevel@tonic-gate 				showregs(type, rp, addr);
11367c478bd9Sstevel@tonic-gate 				traceregs(rp);
11377c478bd9Sstevel@tonic-gate 			}
11387c478bd9Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
1139ae115bc7Smrj 			rp->r_pc = ct->t_lofault;
11407c478bd9Sstevel@tonic-gate 			goto cleanup;
11417c478bd9Sstevel@tonic-gate 		}
1142*a4e4e13fSgavinm 
1143*a4e4e13fSgavinm 		/*
1144*a4e4e13fSgavinm 		 * We fall through to the next case, which repeats
1145*a4e4e13fSgavinm 		 * the OT_SEGMENT_ACCESS check which we've already
1146*a4e4e13fSgavinm 		 * done, so we'll always fall through to the
1147*a4e4e13fSgavinm 		 * T_STKFLT case.
1148*a4e4e13fSgavinm 		 */
11497c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1150ae115bc7Smrj 	case T_SEGFLT:	/* segment not present fault */
1151ae115bc7Smrj 		/*
1152ae115bc7Smrj 		 * One example of this is #NP in update_sregs while
1153ae115bc7Smrj 		 * attempting to update a user segment register
1154ae115bc7Smrj 		 * that points to a descriptor that is marked not
1155ae115bc7Smrj 		 * present.
1156ae115bc7Smrj 		 */
1157ae115bc7Smrj 		if (ct->t_ontrap != NULL &&
1158ae115bc7Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
1159ae115bc7Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
1160843e1988Sjohnlev 			if (tudebug)
1161843e1988Sjohnlev 				showregs(type, rp, (caddr_t)0);
116220c794b3Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
116320c794b3Sgavinm 			goto cleanup;
1164ae115bc7Smrj 		}
1165ae115bc7Smrj 		/*FALLTHROUGH*/
11667c478bd9Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
11677c478bd9Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
11687c478bd9Sstevel@tonic-gate 		if (tudebug)
11697c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11707c478bd9Sstevel@tonic-gate 		if (kern_gpfault(rp))
11717c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11727c478bd9Sstevel@tonic-gate 		goto cleanup;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/*
1175ae115bc7Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
1176ae115bc7Smrj 	 * should have no need for them, so we put a stop to it here.
11777c478bd9Sstevel@tonic-gate 	 *
1178ae115bc7Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
1179ae115bc7Smrj 	 * a private LDT trying to do a system call. Emulate it.
11800baeff3dSrab 	 *
1181ae115bc7Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
1182ae115bc7Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
11830baeff3dSrab 	 */
1184ae115bc7Smrj 
11850baeff3dSrab 	case T_SEGFLT + USER:	/* segment not present fault */
11860baeff3dSrab 	case T_GPFLT + USER:	/* general protection violation */
11870baeff3dSrab #ifdef _SYSCALL32_IMPL
11880baeff3dSrab 		if (p->p_model != DATAMODEL_NATIVE) {
11890baeff3dSrab #endif /* _SYSCALL32_IMPL */
1190ddece0baSsethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
11910baeff3dSrab 			if (type == T_SEGFLT + USER)
11920baeff3dSrab 				ASSERT(p->p_ldt != NULL);
11930baeff3dSrab 
11940baeff3dSrab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
11950baeff3dSrab 			    type == T_SEGFLT + USER) {
11960baeff3dSrab 
11970baeff3dSrab 			/*
11980baeff3dSrab 			 * The user attempted a system call via the obsolete
11990baeff3dSrab 			 * call gate mechanism. Because the process doesn't have
12000baeff3dSrab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
12010baeff3dSrab 			 * Emulate the syscall here, just as we do above for a
12020baeff3dSrab 			 * #np trap.
12030baeff3dSrab 			 */
12040baeff3dSrab 
12050baeff3dSrab 			/*
12067c478bd9Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
12077c478bd9Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
12087c478bd9Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
12097c478bd9Sstevel@tonic-gate 			 */
12107c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
12117c478bd9Sstevel@tonic-gate 			lwp->lwp_regs = rp;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 			/*
12147c478bd9Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
12157c478bd9Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
12167c478bd9Sstevel@tonic-gate 			 * behavior here.
12177c478bd9Sstevel@tonic-gate 			 */
12187c478bd9Sstevel@tonic-gate 			mstate = LMS_USER;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			dosyscall();
12217c478bd9Sstevel@tonic-gate 			goto out;
12227c478bd9Sstevel@tonic-gate 			}
12230baeff3dSrab 		}
12240baeff3dSrab #ifdef _SYSCALL32_IMPL
12250baeff3dSrab 		}
12267c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
12277c478bd9Sstevel@tonic-gate 		/*
12287c478bd9Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
12297c478bd9Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
12307c478bd9Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
12317c478bd9Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12327c478bd9Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12337c478bd9Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12347c478bd9Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12357c478bd9Sstevel@tonic-gate 		 */
12367c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
1237ddece0baSsethg 		    ldt_rewrite_syscall(rp, p, X86_SEP))
12387c478bd9Sstevel@tonic-gate 			goto out;
1239ddece0baSsethg 
12407c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12437c478bd9Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12447c478bd9Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12457c478bd9Sstevel@tonic-gate 		if (tudebug)
12467c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12477c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12487c478bd9Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12497c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12507c478bd9Sstevel@tonic-gate 		fault = FLTBOUNDS;
12517c478bd9Sstevel@tonic-gate 		break;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12547c478bd9Sstevel@tonic-gate 		if (tudebug)
12557c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12567c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12577c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12587c478bd9Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12597c478bd9Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12607c478bd9Sstevel@tonic-gate 		fault = FLTACCESS;
12617c478bd9Sstevel@tonic-gate 		break;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
12647c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12657c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		/* Was it single-stepping? */
12687c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
12697c478bd9Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
12727c478bd9Sstevel@tonic-gate 			/*
12737c478bd9Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
12749acbbeafSnn35248 			 * give precedence to WATCH_STEP.  If neither is set,
12757c478bd9Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
12767c478bd9Sstevel@tonic-gate 			 * as NORMAL_STEP.
12777c478bd9Sstevel@tonic-gate 			 */
12789acbbeafSnn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
12799acbbeafSnn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
12809acbbeafSnn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
12817c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
12827c478bd9Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
12837c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
12847c478bd9Sstevel@tonic-gate 				fault = FLTTRACE;
12857c478bd9Sstevel@tonic-gate 			}
12867c478bd9Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
12877c478bd9Sstevel@tonic-gate 		} else {
12887c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
12897c478bd9Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
12907c478bd9Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 		break;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
12957c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12967c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12977c478bd9Sstevel@tonic-gate 		/*
12987c478bd9Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
12997c478bd9Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
13007c478bd9Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
13017c478bd9Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
13027c478bd9Sstevel@tonic-gate 		 */
13037c478bd9Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
13047c478bd9Sstevel@tonic-gate 			rp->r_pc--;
13057c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
13067c478bd9Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
13077c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
13087c478bd9Sstevel@tonic-gate 		fault = FLTBPT;
13097c478bd9Sstevel@tonic-gate 		break;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	case T_AST:
13127c478bd9Sstevel@tonic-gate 		/*
13137c478bd9Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
13147c478bd9Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
13157c478bd9Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
13167c478bd9Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
13177c478bd9Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
13187c478bd9Sstevel@tonic-gate 		 * doing the T_AST processing.
13197c478bd9Sstevel@tonic-gate 		 */
13207c478bd9Sstevel@tonic-gate 		goto cleanup;
13217c478bd9Sstevel@tonic-gate 
132220c794b3Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
132320c794b3Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
132420c794b3Sgavinm 			proc_t *p = ttoproc(curthread);
132520c794b3Sgavinm 
132620c794b3Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
132720c794b3Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
132820c794b3Sgavinm 			siginfo.si_signo = SIGKILL;
132920c794b3Sgavinm 			siginfo.si_code = SI_NOINFO;
133020c794b3Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
13317c478bd9Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
13327c478bd9Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13337c478bd9Sstevel@tonic-gate 				/*
13347c478bd9Sstevel@tonic-gate 				 * Signal performance counter overflow
13357c478bd9Sstevel@tonic-gate 				 */
13367c478bd9Sstevel@tonic-gate 				if (tudebug)
13377c478bd9Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13387c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13397c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13407c478bd9Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13417c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13427c478bd9Sstevel@tonic-gate 				fault = FLTCPCOVF;
13437c478bd9Sstevel@tonic-gate 			}
13447c478bd9Sstevel@tonic-gate 		}
134520c794b3Sgavinm 
13467c478bd9Sstevel@tonic-gate 		break;
13477c478bd9Sstevel@tonic-gate 	}
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/*
13507c478bd9Sstevel@tonic-gate 	 * We can't get here from a system trap
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	if (fault) {
135510e812d4Saf 		/* We took a fault so abort single step. */
135610e812d4Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13577c478bd9Sstevel@tonic-gate 		/*
13587c478bd9Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13597c478bd9Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13607c478bd9Sstevel@tonic-gate 		 */
13617c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13627c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 		/*
13677c478bd9Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
13687c478bd9Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
13697c478bd9Sstevel@tonic-gate 		 * deliver the associated signal.
13707c478bd9Sstevel@tonic-gate 		 */
13717c478bd9Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
13727c478bd9Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
13737c478bd9Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
13747c478bd9Sstevel@tonic-gate 			siginfo.si_signo = 0;
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	if (siginfo.si_signo)
13787c478bd9Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
13817c478bd9Sstevel@tonic-gate 		profil_tick(rp->r_pc);
13827c478bd9Sstevel@tonic-gate 
1383ae115bc7Smrj 	if (ct->t_astflag | ct->t_sig_check) {
13847c478bd9Sstevel@tonic-gate 		/*
13857c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
13867c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
13877c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
13887c478bd9Sstevel@tonic-gate 		 * syscall.
13897c478bd9Sstevel@tonic-gate 		 */
1390ae115bc7Smrj 		astoff(ct);
1391d8aa0f5aSsudheer 		/*
1392d8aa0f5aSsudheer 		 * If a single-step trap occurred on a syscall (see above)
1393d8aa0f5aSsudheer 		 * recognize it now.  Do this before checking for signals
1394d8aa0f5aSsudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
1395d8aa0f5aSsudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
1396d8aa0f5aSsudheer 		 */
1397d8aa0f5aSsudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
1398d8aa0f5aSsudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
1399d8aa0f5aSsudheer 
1400ae115bc7Smrj 		ct->t_sig_check = 0;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
14037c478bd9Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
14047c478bd9Sstevel@tonic-gate 			timer_lwpbind();
14057c478bd9Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
14067c478bd9Sstevel@tonic-gate 		}
14077c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		/*
14107c478bd9Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
14117c478bd9Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
14127c478bd9Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
14137c478bd9Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
14147c478bd9Sstevel@tonic-gate 		 * for the kaio request to complete.
14157c478bd9Sstevel@tonic-gate 		 */
14167c478bd9Sstevel@tonic-gate 		if (p->p_aio)
14177c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
14187c478bd9Sstevel@tonic-gate 		/*
14197c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
14207c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
14217c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
14227c478bd9Sstevel@tonic-gate 		 *
14237c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14247c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14257c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		if (ISHOLD(p))
14287c478bd9Sstevel@tonic-gate 			holdlwp();
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		/*
14317c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14327c478bd9Sstevel@tonic-gate 		 * set t_astflag afterwards.
14337c478bd9Sstevel@tonic-gate 		 */
1434ae115bc7Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14357c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
14367c478bd9Sstevel@tonic-gate 				psig();
1437ae115bc7Smrj 			ct->t_sig_check = 1;
14387c478bd9Sstevel@tonic-gate 		}
14397c478bd9Sstevel@tonic-gate 
1440ae115bc7Smrj 		if (ct->t_rprof != NULL) {
14417c478bd9Sstevel@tonic-gate 			realsigprof(0, 0);
1442ae115bc7Smrj 			ct->t_sig_check = 1;
14437c478bd9Sstevel@tonic-gate 		}
144465a89a64Smarx 
144565a89a64Smarx 		/*
144665a89a64Smarx 		 * /proc can't enable/disable the trace bit itself
144765a89a64Smarx 		 * because that could race with the call gate used by
144865a89a64Smarx 		 * system calls via "lcall". If that happened, an
144965a89a64Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
145065a89a64Smarx 		 * therefore schedule an AST for the purpose.
145165a89a64Smarx 		 */
145265a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
145365a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
145465a89a64Smarx 			rp->r_ps |= PS_T;
145565a89a64Smarx 		}
145665a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
145765a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
145865a89a64Smarx 			rp->r_ps &= ~PS_T;
145965a89a64Smarx 		}
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14637c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if (ISHOLD(p))
14667c478bd9Sstevel@tonic-gate 		holdlwp();
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
14707c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
14717c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
14727c478bd9Sstevel@tonic-gate 	 *
14737c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
14747c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
14757c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
14787c478bd9Sstevel@tonic-gate 
1479ae115bc7Smrj 	if (ct->t_trapret) {
1480ae115bc7Smrj 		ct->t_trapret = 0;
1481ae115bc7Smrj 		thread_lock(ct);
1482ae115bc7Smrj 		CL_TRAPRET(ct);
1483ae115bc7Smrj 		thread_unlock(ct);
14847c478bd9Sstevel@tonic-gate 	}
1485c97ad5cdSakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
14867c478bd9Sstevel@tonic-gate 		preempt();
1487ae115bc7Smrj 	(void) new_mstate(ct, mstate);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	/* Kernel probe */
14907c478bd9Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
14917c478bd9Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	return;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate cleanup:	/* system traps end up here */
14967c478bd9Sstevel@tonic-gate 	ASSERT(!(type & USER));
14977c478bd9Sstevel@tonic-gate }
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate /*
15007c478bd9Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
15017c478bd9Sstevel@tonic-gate  */
15027c478bd9Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
15057c478bd9Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
15067c478bd9Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
15077c478bd9Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
15087c478bd9Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
15097c478bd9Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
15107c478bd9Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
15117c478bd9Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
15127c478bd9Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
15137c478bd9Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
15147c478bd9Sstevel@tonic-gate } kpreempt_cnts;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate /*
15177c478bd9Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
15187c478bd9Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
15197c478bd9Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate void
15227c478bd9Sstevel@tonic-gate kpreempt(int asyncspl)
15237c478bd9Sstevel@tonic-gate {
1524ae115bc7Smrj 	kthread_t *ct = curthread;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
15277c478bd9Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
15287c478bd9Sstevel@tonic-gate 		return;
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	/*
15327c478bd9Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
15337c478bd9Sstevel@tonic-gate 	 */
15347c478bd9Sstevel@tonic-gate 	do {
1535ae115bc7Smrj 		if (ct->t_preempt) {
15367c478bd9Sstevel@tonic-gate 			/*
15377c478bd9Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
15387c478bd9Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
15397c478bd9Sstevel@tonic-gate 			 */
1540ae115bc7Smrj 			if (ct->t_pri < 0)
15417c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
1542ae115bc7Smrj 			else if (ct->t_flag & T_INTR_THREAD) {
15437c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
1544ae115bc7Smrj 				if (ct->t_pil == CLOCK_LEVEL)
15457c478bd9Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
15467c478bd9Sstevel@tonic-gate 			} else
15477c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
15487c478bd9Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
15497c478bd9Sstevel@tonic-gate 			return;
15507c478bd9Sstevel@tonic-gate 		}
1551ae115bc7Smrj 		if (ct->t_state != TS_ONPROC ||
1552ae115bc7Smrj 		    ct->t_disp_queue != CPU->cpu_disp) {
15537c478bd9Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
15547c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
15557c478bd9Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
15567c478bd9Sstevel@tonic-gate 				/* already in swtch(), force another */
15577c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
15587c478bd9Sstevel@tonic-gate 				siron();
15597c478bd9Sstevel@tonic-gate 			}
15607c478bd9Sstevel@tonic-gate 			return;
15617c478bd9Sstevel@tonic-gate 		}
15627c478bd9Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
15637c478bd9Sstevel@tonic-gate 			/*
15647c478bd9Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
15657c478bd9Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
15667c478bd9Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
15677c478bd9Sstevel@tonic-gate 			 */
15687c478bd9Sstevel@tonic-gate 			siron();	/* check back later */
15697c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
15707c478bd9Sstevel@tonic-gate 			return;
15717c478bd9Sstevel@tonic-gate 		}
1572ae115bc7Smrj 		if (!interrupts_enabled()) {
1573ae115bc7Smrj 			/*
1574ae115bc7Smrj 			 * Can't preempt while running with ints disabled
1575ae115bc7Smrj 			 */
1576ae115bc7Smrj 			kpreempt_cnts.kpc_prilevel++;
1577ae115bc7Smrj 			return;
1578ae115bc7Smrj 		}
15797c478bd9Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
15807c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
15817c478bd9Sstevel@tonic-gate 		else
15827c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
15837c478bd9Sstevel@tonic-gate 
1584ae115bc7Smrj 		ct->t_preempt++;
15857c478bd9Sstevel@tonic-gate 		preempt();
1586ae115bc7Smrj 		ct->t_preempt--;
15877c478bd9Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate /*
15917c478bd9Sstevel@tonic-gate  * Print out debugging info.
15927c478bd9Sstevel@tonic-gate  */
15937c478bd9Sstevel@tonic-gate static void
15947c478bd9Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate 	int s;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	s = spl7();
15997c478bd9Sstevel@tonic-gate 	type &= ~USER;
1600ae115bc7Smrj 	if (PTOU(curproc)->u_comm[0])
1601ae115bc7Smrj 		printf("%s: ", PTOU(curproc)->u_comm);
16027c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES)
16037c478bd9Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
16047c478bd9Sstevel@tonic-gate 	else
16057c478bd9Sstevel@tonic-gate 		switch (type) {
16067c478bd9Sstevel@tonic-gate 		case T_SYSCALL:
16077c478bd9Sstevel@tonic-gate 			printf("Syscall Trap:\n");
16087c478bd9Sstevel@tonic-gate 			break;
16097c478bd9Sstevel@tonic-gate 		case T_AST:
16107c478bd9Sstevel@tonic-gate 			printf("AST\n");
16117c478bd9Sstevel@tonic-gate 			break;
16127c478bd9Sstevel@tonic-gate 		default:
16137c478bd9Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
16147c478bd9Sstevel@tonic-gate 			break;
16157c478bd9Sstevel@tonic-gate 		}
16167c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
16177c478bd9Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
16187c478bd9Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
16197c478bd9Sstevel@tonic-gate 	} else if (addr) {
16207c478bd9Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
16247c478bd9Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
16257c478bd9Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate #if defined(__lint)
16287c478bd9Sstevel@tonic-gate 	/*
16297c478bd9Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
16307c478bd9Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
16317c478bd9Sstevel@tonic-gate 	 */
16327c478bd9Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
16337c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
16347c478bd9Sstevel@tonic-gate #else
16357c478bd9Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
16367c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
1637ae115bc7Smrj #endif	/* __lint */
16387c478bd9Sstevel@tonic-gate 
1639843e1988Sjohnlev 	printf("cr2: %lx", getcr2());
1640843e1988Sjohnlev #if !defined(__xpv)
1641843e1988Sjohnlev 	printf("cr3: %lx", getcr3());
16427c478bd9Sstevel@tonic-gate #if defined(__amd64)
1643843e1988Sjohnlev 	printf("cr8: %lx\n", getcr8());
16447c478bd9Sstevel@tonic-gate #endif
1645843e1988Sjohnlev #endif
1646843e1988Sjohnlev 	printf("\n");
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	dumpregs(rp);
16497c478bd9Sstevel@tonic-gate 	splx(s);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate static void
16537c478bd9Sstevel@tonic-gate dumpregs(struct regs *rp)
16547c478bd9Sstevel@tonic-gate {
16557c478bd9Sstevel@tonic-gate #if defined(__amd64)
16567c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
16597c478bd9Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
16607c478bd9Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
16617c478bd9Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
16627c478bd9Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
16637c478bd9Sstevel@tonic-gate 
1664ae115bc7Smrj 	printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE),
1665ae115bc7Smrj 	    " ds", rp->r_ds);
16667c478bd9Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
16697c478bd9Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate #elif defined(__i386)
16747c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
16777c478bd9Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
16787c478bd9Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
16797c478bd9Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
16807c478bd9Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
16817c478bd9Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
16827c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
16837c478bd9Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
16847c478bd9Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
16857c478bd9Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate #endif	/* __i386 */
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
1691843e1988Sjohnlev  * Test to see if the instruction is iret on i386 or iretq on amd64.
1692843e1988Sjohnlev  *
1693843e1988Sjohnlev  * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true
1694843e1988Sjohnlev  * then we are in the context of hypervisor's failsafe handler because it
1695843e1988Sjohnlev  * tried to iret and failed due to a bad selector. See xen_failsafe_callback.
1696843e1988Sjohnlev  */
1697843e1988Sjohnlev static int
1698843e1988Sjohnlev instr_is_iret(caddr_t pc)
1699843e1988Sjohnlev {
1700843e1988Sjohnlev 
1701843e1988Sjohnlev #if defined(__xpv)
1702843e1988Sjohnlev 	extern void nopop_sys_rtt_syscall(void);
1703843e1988Sjohnlev 	return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0);
1704843e1988Sjohnlev 
1705843e1988Sjohnlev #else
1706843e1988Sjohnlev 
1707843e1988Sjohnlev #if defined(__amd64)
1708843e1988Sjohnlev 	static const uint8_t iret_insn[2] = { 0x48, 0xcf };	/* iretq */
1709843e1988Sjohnlev 
1710843e1988Sjohnlev #elif defined(__i386)
1711843e1988Sjohnlev 	static const uint8_t iret_insn[1] = { 0xcf };		/* iret */
1712843e1988Sjohnlev #endif	/* __i386 */
1713843e1988Sjohnlev 	return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0);
1714843e1988Sjohnlev 
1715843e1988Sjohnlev #endif	/* __xpv */
1716843e1988Sjohnlev }
1717843e1988Sjohnlev 
1718843e1988Sjohnlev #if defined(__i386)
1719843e1988Sjohnlev 
1720843e1988Sjohnlev /*
1721843e1988Sjohnlev  * Test to see if the instruction is part of __SEGREGS_POP
1722843e1988Sjohnlev  *
1723843e1988Sjohnlev  * Note carefully the appallingly awful dependency between
1724843e1988Sjohnlev  * the instruction sequence used in __SEGREGS_POP and these
1725843e1988Sjohnlev  * instructions encoded here.
1726843e1988Sjohnlev  */
1727843e1988Sjohnlev static int
1728843e1988Sjohnlev instr_is_segregs_pop(caddr_t pc)
1729843e1988Sjohnlev {
1730843e1988Sjohnlev 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
1731843e1988Sjohnlev 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
1732843e1988Sjohnlev 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
1733843e1988Sjohnlev 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
1734843e1988Sjohnlev 
1735843e1988Sjohnlev 	if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
1736843e1988Sjohnlev 	    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
1737843e1988Sjohnlev 	    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
1738843e1988Sjohnlev 	    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
1739843e1988Sjohnlev 		return (1);
1740843e1988Sjohnlev 
1741843e1988Sjohnlev 	return (0);
1742843e1988Sjohnlev }
1743843e1988Sjohnlev 
1744843e1988Sjohnlev #endif	/* __i386 */
1745843e1988Sjohnlev 
1746843e1988Sjohnlev /*
1747843e1988Sjohnlev  * Test to see if the instruction is part of _sys_rtt.
1748843e1988Sjohnlev  *
1749843e1988Sjohnlev  * Again on the hypervisor if we try to IRET to user land with a bad code
1750843e1988Sjohnlev  * or stack selector we will get vectored through xen_failsafe_callback.
1751843e1988Sjohnlev  * In which case we assume we got here via _sys_rtt since we only allow
1752843e1988Sjohnlev  * IRET to user land to take place in _sys_rtt.
1753843e1988Sjohnlev  */
1754843e1988Sjohnlev static int
1755843e1988Sjohnlev instr_is_sys_rtt(caddr_t pc)
1756843e1988Sjohnlev {
1757843e1988Sjohnlev 	extern void _sys_rtt(), _sys_rtt_end();
1758843e1988Sjohnlev 
1759843e1988Sjohnlev 	if ((uintptr_t)pc < (uintptr_t)_sys_rtt ||
1760843e1988Sjohnlev 	    (uintptr_t)pc > (uintptr_t)_sys_rtt_end)
1761843e1988Sjohnlev 		return (0);
1762843e1988Sjohnlev 
1763843e1988Sjohnlev 	return (1);
1764843e1988Sjohnlev }
1765843e1988Sjohnlev 
1766843e1988Sjohnlev /*
17677c478bd9Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
17687c478bd9Sstevel@tonic-gate  *
17697c478bd9Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
17707c478bd9Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
17717c478bd9Sstevel@tonic-gate  *
17727c478bd9Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
17737c478bd9Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
17747c478bd9Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
17757c478bd9Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
17767c478bd9Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
17777c478bd9Sstevel@tonic-gate  *
17787c478bd9Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
17797c478bd9Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
17807c478bd9Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
17817c478bd9Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
17827c478bd9Sstevel@tonic-gate  * what we were going to send the lwp.)
17837c478bd9Sstevel@tonic-gate  *
17847c478bd9Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
17857c478bd9Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
17867c478bd9Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
17877c478bd9Sstevel@tonic-gate  */
17887c478bd9Sstevel@tonic-gate static int
17897c478bd9Sstevel@tonic-gate kern_gpfault(struct regs *rp)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
17927c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
17937c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
17947c478bd9Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
17957c478bd9Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
17967c478bd9Sstevel@tonic-gate 	int v;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
1799843e1988Sjohnlev 	 * if we're not an lwp, or in the case of running native the
1800843e1988Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
1801843e1988Sjohnlev 	 * be die()ing horribly.
18027c478bd9Sstevel@tonic-gate 	 */
1803843e1988Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
18047c478bd9Sstevel@tonic-gate 		return (1);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	/*
18077c478bd9Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
18087c478bd9Sstevel@tonic-gate 	 *
18097c478bd9Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
18107c478bd9Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
18117c478bd9Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
18127c478bd9Sstevel@tonic-gate 	 * _sys_rtt. Ew.
18137c478bd9Sstevel@tonic-gate 	 */
1814843e1988Sjohnlev 	if (instr_is_iret(pc)) {
18157c478bd9Sstevel@tonic-gate 		/*
1816843e1988Sjohnlev 		 * We took the #gp while trying to perform the IRET.
18177c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
18187c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
18197c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
18207c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
18217c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
18227c478bd9Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
18237c478bd9Sstevel@tonic-gate 		 *
18247c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
18257c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
18267c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
18277c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
18287c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
18297c478bd9Sstevel@tonic-gate 		 */
18307c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
18317c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
18327c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
18337c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
18347c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
18357c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
18367c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 		/*
18397c478bd9Sstevel@tonic-gate 		 * Validate simple math
18407c478bd9Sstevel@tonic-gate 		 */
18417c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18427c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
18437c478bd9Sstevel@tonic-gate 
1844843e1988Sjohnlev 
1845843e1988Sjohnlev 
1846843e1988Sjohnlev 	}
1847843e1988Sjohnlev 
1848843e1988Sjohnlev #if defined(__amd64)
1849843e1988Sjohnlev 	if (trp == NULL && lwp->lwp_pcb.pcb_rupdate != 0) {
1850843e1988Sjohnlev 
1851843e1988Sjohnlev 		/*
1852843e1988Sjohnlev 		 * This is the common case -- we're trying to load
1853843e1988Sjohnlev 		 * a bad segment register value in the only section
1854843e1988Sjohnlev 		 * of kernel code that ever loads segment registers.
1855843e1988Sjohnlev 		 *
1856843e1988Sjohnlev 		 * We don't need to do anything at this point because
1857843e1988Sjohnlev 		 * the pcb contains all the pending segment register
1858843e1988Sjohnlev 		 * state, and the regs are still intact because we
1859843e1988Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
1860843e1988Sjohnlev 		 * of all this, we could conceivably send a signal
1861843e1988Sjohnlev 		 * to the lwp, rather than core-ing.
1862843e1988Sjohnlev 		 */
1863843e1988Sjohnlev 		trp = lwptoregs(lwp);
1864843e1988Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate #elif defined(__i386)
18687c478bd9Sstevel@tonic-gate 
1869843e1988Sjohnlev 	if (trp == NULL && instr_is_segregs_pop(pc))
18707c478bd9Sstevel@tonic-gate 		trp = lwptoregs(lwp);
1871843e1988Sjohnlev 
1872843e1988Sjohnlev #endif	/* __i386 */
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	if (trp == NULL)
18757c478bd9Sstevel@tonic-gate 		return (1);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	/*
18787c478bd9Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
18797c478bd9Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
18807c478bd9Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
18817c478bd9Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
18827c478bd9Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	/*
18867c478bd9Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
18877c478bd9Sstevel@tonic-gate 	 */
18887c478bd9Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
18897c478bd9Sstevel@tonic-gate 	trp->r_err = rp->r_err;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
18927c478bd9Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
18937c478bd9Sstevel@tonic-gate 
1894843e1988Sjohnlev 
18957c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
18967c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
18977c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	/*
1900843e1988Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
1901843e1988Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
19027c478bd9Sstevel@tonic-gate 	 */
190397eda132Sraf 	proc_is_exiting(p);
19047c478bd9Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
19057c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19067c478bd9Sstevel@tonic-gate 		lwp_exit();
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
19107c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19117c478bd9Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
19127c478bd9Sstevel@tonic-gate #endif
19137c478bd9Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
19147c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
19157c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
19167c478bd9Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
19177c478bd9Sstevel@tonic-gate #endif
19187c478bd9Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
19197c478bd9Sstevel@tonic-gate 	return (0);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate /*
19237c478bd9Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
19247c478bd9Sstevel@tonic-gate  */
19257c478bd9Sstevel@tonic-gate 
1926843e1988Sjohnlev #if !defined(__xpv)
19277c478bd9Sstevel@tonic-gate #if defined(__amd64)
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate static void
19307c478bd9Sstevel@tonic-gate dump_tss(void)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19337c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
19367c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
19377c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
19407c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
19417c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
19427c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
19437c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
19447c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
19457c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
19467c478bd9Sstevel@tonic-gate }
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate #elif defined(__i386)
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate static void
19517c478bd9Sstevel@tonic-gate dump_tss(void)
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
19547c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
19557c478bd9Sstevel@tonic-gate 
19563dfcd6dcSdmick 	printf(tss_fmt, "tss_link", (void *)(uintptr_t)tss->tss_link);
19573dfcd6dcSdmick 	printf(tss_fmt, "tss_esp0", (void *)(uintptr_t)tss->tss_esp0);
19583dfcd6dcSdmick 	printf(tss_fmt, "tss_ss0", (void *)(uintptr_t)tss->tss_ss0);
19593dfcd6dcSdmick 	printf(tss_fmt, "tss_esp1", (void *)(uintptr_t)tss->tss_esp1);
19603dfcd6dcSdmick 	printf(tss_fmt, "tss_ss1", (void *)(uintptr_t)tss->tss_ss1);
19613dfcd6dcSdmick 	printf(tss_fmt, "tss_esp2", (void *)(uintptr_t)tss->tss_esp2);
19623dfcd6dcSdmick 	printf(tss_fmt, "tss_ss2", (void *)(uintptr_t)tss->tss_ss2);
19633dfcd6dcSdmick 	printf(tss_fmt, "tss_cr3", (void *)(uintptr_t)tss->tss_cr3);
19643dfcd6dcSdmick 	printf(tss_fmt, "tss_eip", (void *)(uintptr_t)tss->tss_eip);
19653dfcd6dcSdmick 	printf(tss_fmt, "tss_eflags", (void *)(uintptr_t)tss->tss_eflags);
19663dfcd6dcSdmick 	printf(tss_fmt, "tss_eax", (void *)(uintptr_t)tss->tss_eax);
19673dfcd6dcSdmick 	printf(tss_fmt, "tss_ebx", (void *)(uintptr_t)tss->tss_ebx);
19683dfcd6dcSdmick 	printf(tss_fmt, "tss_ecx", (void *)(uintptr_t)tss->tss_ecx);
19693dfcd6dcSdmick 	printf(tss_fmt, "tss_edx", (void *)(uintptr_t)tss->tss_edx);
19703dfcd6dcSdmick 	printf(tss_fmt, "tss_esp", (void *)(uintptr_t)tss->tss_esp);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
1974843e1988Sjohnlev #endif	/* !__xpv */
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
19777c478bd9Sstevel@tonic-gate 
1978843e1988Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
1979843e1988Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate /*
19827c478bd9Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
19837c478bd9Sstevel@tonic-gate  */
19847c478bd9Sstevel@tonic-gate static void
19857c478bd9Sstevel@tonic-gate dump_ttrace(void)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
19887c478bd9Sstevel@tonic-gate 	trap_trace_rec_t *rec;
19897c478bd9Sstevel@tonic-gate 	uintptr_t current;
19907c478bd9Sstevel@tonic-gate 	int i, j, k;
19917c478bd9Sstevel@tonic-gate 	int n = NCPU;
19927c478bd9Sstevel@tonic-gate #if defined(__amd64)
19937c478bd9Sstevel@tonic-gate 	const char banner[] =
19947c478bd9Sstevel@tonic-gate 	    "\ncpu          address    timestamp "
19957c478bd9Sstevel@tonic-gate 	    "type  vc  handler   pc\n";
19967c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
19977c478bd9Sstevel@tonic-gate #elif defined(__i386)
19987c478bd9Sstevel@tonic-gate 	const char banner[] =
19997c478bd9Sstevel@tonic-gate 	    "\ncpu  address     timestamp type  vc  handler   pc\n";
20007c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
20017c478bd9Sstevel@tonic-gate #endif
20027c478bd9Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
20037c478bd9Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	if (ttrace_nrec == 0)
20067c478bd9Sstevel@tonic-gate 		return;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	printf(banner);
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20117c478bd9Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
20127c478bd9Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
20137c478bd9Sstevel@tonic-gate 			continue;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
20167c478bd9Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
20177c478bd9Sstevel@tonic-gate 			struct sysent	*sys;
20187c478bd9Sstevel@tonic-gate 			struct autovec	*vec;
20197c478bd9Sstevel@tonic-gate 			extern struct av_head autovect[];
20207c478bd9Sstevel@tonic-gate 			int type;
20217c478bd9Sstevel@tonic-gate 			ulong_t	off;
20227c478bd9Sstevel@tonic-gate 			char *sym, *stype;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 			if (current < ttc->ttc_first)
20257c478bd9Sstevel@tonic-gate 				current =
20267c478bd9Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 			if (current == NULL)
20297c478bd9Sstevel@tonic-gate 				continue;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
20347c478bd9Sstevel@tonic-gate 				break;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 			switch (rec->ttr_marker) {
20397c478bd9Sstevel@tonic-gate 			case TT_SYSCALL:
20407c478bd9Sstevel@tonic-gate 			case TT_SYSENTER:
20417c478bd9Sstevel@tonic-gate 			case TT_SYSC:
20427c478bd9Sstevel@tonic-gate 			case TT_SYSC64:
20437c478bd9Sstevel@tonic-gate #if defined(__amd64)
20447c478bd9Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
20457c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20467c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
20477c478bd9Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
20487c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
20497c478bd9Sstevel@tonic-gate #elif defined(__i386)
20507c478bd9Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
20517c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
20527c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
20537c478bd9Sstevel@tonic-gate #endif
20547c478bd9Sstevel@tonic-gate 				case TT_SYSC:
20557c478bd9Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
20567c478bd9Sstevel@tonic-gate 					break;
20577c478bd9Sstevel@tonic-gate 				case TT_SYSCALL:
20587c478bd9Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
20597c478bd9Sstevel@tonic-gate 					break;
20607c478bd9Sstevel@tonic-gate 				case TT_SYSENTER:
20617c478bd9Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
20627c478bd9Sstevel@tonic-gate 					break;
20637c478bd9Sstevel@tonic-gate 				default:
20647c478bd9Sstevel@tonic-gate 					break;
20657c478bd9Sstevel@tonic-gate 				}
20667c478bd9Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
20677c478bd9Sstevel@tonic-gate 				if (sys != NULL) {
20687c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
20697c478bd9Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
20707c478bd9Sstevel@tonic-gate 					    &off);
20717c478bd9Sstevel@tonic-gate 					if (sym != NULL)
2072ae115bc7Smrj 						printf(fmt3, sym);
20737c478bd9Sstevel@tonic-gate 					else
20747c478bd9Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
20757c478bd9Sstevel@tonic-gate 				} else {
2076ae115bc7Smrj 					printf(fmt3, "unknown");
20777c478bd9Sstevel@tonic-gate 				}
20787c478bd9Sstevel@tonic-gate 				break;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 			case TT_INTERRUPT:
20817c478bd9Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
20827c478bd9Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
20837c478bd9Sstevel@tonic-gate 				if (vec != NULL) {
20847c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
20857c478bd9Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
20867c478bd9Sstevel@tonic-gate 					if (sym != NULL)
2087ae115bc7Smrj 						printf(fmt3, sym);
20887c478bd9Sstevel@tonic-gate 					else
20897c478bd9Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
20907c478bd9Sstevel@tonic-gate 				} else {
2091ae115bc7Smrj 					printf(fmt3, "unknown ");
20927c478bd9Sstevel@tonic-gate 				}
20937c478bd9Sstevel@tonic-gate 				break;
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 			case TT_TRAP:
2096ae115bc7Smrj 			case TT_EVENT:
20977c478bd9Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
20987c478bd9Sstevel@tonic-gate 				printf(fmt2, "trap", type);
2099ae115bc7Smrj 				if (type < TRAP_TYPES)
2100ae115bc7Smrj 					printf("     #%s ",
2101ae115bc7Smrj 					    trap_type_mnemonic[type]);
2102ae115bc7Smrj 				else
2103ae115bc7Smrj 					switch (type) {
2104ae115bc7Smrj 					case T_AST:
2105ae115bc7Smrj 						printf(fmt3, "ast");
2106ae115bc7Smrj 						break;
2107ae115bc7Smrj 					default:
2108ae115bc7Smrj 						printf(fmt3, "");
2109ae115bc7Smrj 						break;
2110ae115bc7Smrj 					}
2111ae115bc7Smrj 				break;
2112ae115bc7Smrj 
2113ae115bc7Smrj 			case TT_XCALL:
2114ae115bc7Smrj 				printf(fmt2, "xcal",
2115ae115bc7Smrj 				    rec->ttr_info.xc_entry.xce_marker);
2116ae115bc7Smrj 				printf(fmt3, "");
21177c478bd9Sstevel@tonic-gate 				break;
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 			default:
21207c478bd9Sstevel@tonic-gate 				break;
21217c478bd9Sstevel@tonic-gate 			}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
21247c478bd9Sstevel@tonic-gate 			if (sym != NULL)
21257c478bd9Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
21267c478bd9Sstevel@tonic-gate 			else
21277c478bd9Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
21307c478bd9Sstevel@tonic-gate 				int s;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
21337c478bd9Sstevel@tonic-gate 					printf(
21347c478bd9Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
21357c478bd9Sstevel@tonic-gate 					    rec->ttr_ipl,
21367c478bd9Sstevel@tonic-gate 					    rec->ttr_spl,
21377c478bd9Sstevel@tonic-gate 					    rec->ttr_pri);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
21427c478bd9Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 				/*
21457c478bd9Sstevel@tonic-gate 				 * print out the pc stack that we recorded
21467c478bd9Sstevel@tonic-gate 				 * at trap time (if any)
21477c478bd9Sstevel@tonic-gate 				 */
21487c478bd9Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
21497c478bd9Sstevel@tonic-gate 					uintptr_t fullpc;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
21527c478bd9Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
21537c478bd9Sstevel@tonic-gate 						break;
21547c478bd9Sstevel@tonic-gate 					}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
21597c478bd9Sstevel@tonic-gate 					if (sym != NULL)
21607c478bd9Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
21617c478bd9Sstevel@tonic-gate 						    sym, off);
21627c478bd9Sstevel@tonic-gate 					else
21637c478bd9Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
21647c478bd9Sstevel@tonic-gate 				}
21657c478bd9Sstevel@tonic-gate 				printf("\n");
21667c478bd9Sstevel@tonic-gate 			}
21677c478bd9Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
21687c478bd9Sstevel@tonic-gate 		}
21697c478bd9Sstevel@tonic-gate 	}
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate 
2172ae115bc7Smrj /*
2173ae115bc7Smrj  * Help with constructing traptrace records in C
2174ae115bc7Smrj  */
2175ae115bc7Smrj trap_trace_rec_t *
2176ae115bc7Smrj trap_trace_get_traceptr(uint8_t marker, ulong_t pc, ulong_t sp)
2177ae115bc7Smrj {
2178ae115bc7Smrj 	trap_trace_rec_t *ttr;
2179ae115bc7Smrj 
2180ae115bc7Smrj 	if (trap_trace_freeze)
2181ae115bc7Smrj 		ttr = &trap_trace_postmort;
2182ae115bc7Smrj 	else {
2183ae115bc7Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[CPU->cpu_id];
2184ae115bc7Smrj 
2185ae115bc7Smrj 		ttr = (void *)ttc->ttc_next;
2186ae115bc7Smrj 
2187ae115bc7Smrj 		if (ttc->ttc_next >= ttc->ttc_limit)
2188ae115bc7Smrj 			ttc->ttc_next = ttc->ttc_first;
2189ae115bc7Smrj 		else
2190ae115bc7Smrj 			ttc->ttc_next += sizeof (trap_trace_rec_t);
2191ae115bc7Smrj 	}
2192ae115bc7Smrj 
2193ae115bc7Smrj 	ttr->ttr_regs.r_sp = sp;
2194ae115bc7Smrj 	ttr->ttr_regs.r_pc = pc;
2195ae115bc7Smrj 	ttr->ttr_cr2 = getcr2();
2196ae115bc7Smrj 	ttr->ttr_curthread = (uintptr_t)curthread;
2197ae115bc7Smrj 	ttr->ttr_stamp = tsc_read();
2198ae115bc7Smrj 	ttr->ttr_marker = marker;
2199ae115bc7Smrj 	return (ttr);
2200ae115bc7Smrj }
2201ae115bc7Smrj 
22027c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate void
2205843e1988Sjohnlev panic_showtrap(struct panic_trap_info *tip)
22067c478bd9Sstevel@tonic-gate {
22077c478bd9Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
22107c478bd9Sstevel@tonic-gate 	dump_ttrace();
2211843e1988Sjohnlev #endif
22127c478bd9Sstevel@tonic-gate 
2213843e1988Sjohnlev #if !defined(__xpv)
22147c478bd9Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
22157c478bd9Sstevel@tonic-gate 		dump_tss();
2216843e1988Sjohnlev #endif
22177c478bd9Sstevel@tonic-gate }
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate void
2220843e1988Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
22217c478bd9Sstevel@tonic-gate {
22227c478bd9Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
22237c478bd9Sstevel@tonic-gate }
2224