xref: /illumos-gate/usr/src/uts/i86pc/os/trap.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
28*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
29*7c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
30*7c478bd9Sstevel@tonic-gate /*								*/
31*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation  	*/
32*7c478bd9Sstevel@tonic-gate /*		All Rights Reserved   				*/
33*7c478bd9Sstevel@tonic-gate /*								*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/core.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/vm.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/fault.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/psw.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/regset.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/fp.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/trap.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/mutex_impl.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate #include <vm/hat.h>
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
72*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
73*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
74*7c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
79*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
80*7c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
81*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
82*7c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
83*7c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
84*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
85*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
86*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
87*7c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
88*7c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
89*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
90*7c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
91*7c478bd9Sstevel@tonic-gate #include <sys/panic.h>
92*7c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
93*7c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
94*7c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
99*7c478bd9Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
100*7c478bd9Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
101*7c478bd9Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
102*7c478bd9Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
103*7c478bd9Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
104*7c478bd9Sstevel@tonic-gate };
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate static const char *trap_type[] = {
107*7c478bd9Sstevel@tonic-gate 	"Divide error",				/* trap id 0 	*/
108*7c478bd9Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
109*7c478bd9Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
110*7c478bd9Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3 	*/
111*7c478bd9Sstevel@tonic-gate 	"Overflow",				/* trap id 4 	*/
112*7c478bd9Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5 	*/
113*7c478bd9Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6 	*/
114*7c478bd9Sstevel@tonic-gate 	"Device not available",			/* trap id 7 	*/
115*7c478bd9Sstevel@tonic-gate 	"Double fault",				/* trap id 8 	*/
116*7c478bd9Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9 	*/
117*7c478bd9Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10 	*/
118*7c478bd9Sstevel@tonic-gate 	"Segment not present",			/* trap id 11 	*/
119*7c478bd9Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12 	*/
120*7c478bd9Sstevel@tonic-gate 	"General protection",			/* trap id 13 	*/
121*7c478bd9Sstevel@tonic-gate 	"Page fault",				/* trap id 14 	*/
122*7c478bd9Sstevel@tonic-gate 	"Reserved",				/* trap id 15 	*/
123*7c478bd9Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16 	*/
124*7c478bd9Sstevel@tonic-gate 	"Alignment check",			/* trap id 17 	*/
125*7c478bd9Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
126*7c478bd9Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
127*7c478bd9Sstevel@tonic-gate };
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate int tudebug = 0;
132*7c478bd9Sstevel@tonic-gate int tudebugbpt = 0;
133*7c478bd9Sstevel@tonic-gate int tudebugfpe = 0;
134*7c478bd9Sstevel@tonic-gate int tudebugsse = 0;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
137*7c478bd9Sstevel@tonic-gate int tdebug = 0;
138*7c478bd9Sstevel@tonic-gate int lodebug = 0;
139*7c478bd9Sstevel@tonic-gate int faultdebug = 0;
140*7c478bd9Sstevel@tonic-gate #else
141*7c478bd9Sstevel@tonic-gate #define	tdebug	0
142*7c478bd9Sstevel@tonic-gate #define	lodebug	0
143*7c478bd9Sstevel@tonic-gate #define	faultdebug	0
144*7c478bd9Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
147*7c478bd9Sstevel@tonic-gate static void dump_ttrace(void);
148*7c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
149*7c478bd9Sstevel@tonic-gate static void dumpregs(struct regs *);
150*7c478bd9Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
151*7c478bd9Sstevel@tonic-gate static void dump_tss(void);
152*7c478bd9Sstevel@tonic-gate static int kern_gpfault(struct regs *);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate struct trap_info {
155*7c478bd9Sstevel@tonic-gate 	struct regs *trap_regs;
156*7c478bd9Sstevel@tonic-gate 	uint_t trap_type;
157*7c478bd9Sstevel@tonic-gate 	caddr_t trap_addr;
158*7c478bd9Sstevel@tonic-gate };
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
161*7c478bd9Sstevel@tonic-gate static int
162*7c478bd9Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	struct trap_info ti;
165*7c478bd9Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
168*7c478bd9Sstevel@tonic-gate 		trap_name = trap_type[type];
169*7c478bd9Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
170*7c478bd9Sstevel@tonic-gate 	} else {
171*7c478bd9Sstevel@tonic-gate 		trap_name = "trap";
172*7c478bd9Sstevel@tonic-gate 		trap_mnemonic = "-";
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
176*7c478bd9Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
177*7c478bd9Sstevel@tonic-gate #endif
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	ti.trap_regs = rp;
180*7c478bd9Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
181*7c478bd9Sstevel@tonic-gate 	ti.trap_addr = addr;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT && addr < (caddr_t)KERNELBASE) {
186*7c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
187*7c478bd9Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
188*7c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
189*7c478bd9Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
190*7c478bd9Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
191*7c478bd9Sstevel@tonic-gate 		    "a NULL pointer dereference" :
192*7c478bd9Sstevel@tonic-gate 		    "an illegal access to a user address");
193*7c478bd9Sstevel@tonic-gate 	} else
194*7c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
195*7c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
196*7c478bd9Sstevel@tonic-gate 	return (0);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
201*7c478bd9Sstevel@tonic-gate  *
202*7c478bd9Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
203*7c478bd9Sstevel@tonic-gate  */
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate #define	SLOW_SCALL_SIZE	2
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate static int
208*7c478bd9Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
213*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
214*7c478bd9Sstevel@tonic-gate 		return (1);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	return (0);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate /*
220*7c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
221*7c478bd9Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
222*7c478bd9Sstevel@tonic-gate  * instruction.
223*7c478bd9Sstevel@tonic-gate  *
224*7c478bd9Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
225*7c478bd9Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
226*7c478bd9Sstevel@tonic-gate  */
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate #define	FAST_SCALL_SIZE	2
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate static int
231*7c478bd9Sstevel@tonic-gate instr_is_fast_syscall(caddr_t pc, int which)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	ASSERT(which == X86_SEP || which == X86_ASYSC);
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0 ||
238*7c478bd9Sstevel@tonic-gate 	    instr[0] != 0x0F)
239*7c478bd9Sstevel@tonic-gate 		return (0);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	if ((which == X86_SEP && instr[1] == 0x34) ||
242*7c478bd9Sstevel@tonic-gate 	    (which == X86_ASYSC && instr[1] == 0x05))
243*7c478bd9Sstevel@tonic-gate 		return (1);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	return (0);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
250*7c478bd9Sstevel@tonic-gate  *
251*7c478bd9Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
252*7c478bd9Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
253*7c478bd9Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
254*7c478bd9Sstevel@tonic-gate  */
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate #define	LCALLSIZE	7
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate static int
259*7c478bd9Sstevel@tonic-gate instr_is_syscall(caddr_t pc)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
264*7c478bd9Sstevel@tonic-gate 	    instr[0] == 0x9a &&
265*7c478bd9Sstevel@tonic-gate 	    instr[1] == 0 &&
266*7c478bd9Sstevel@tonic-gate 	    instr[2] == 0 &&
267*7c478bd9Sstevel@tonic-gate 	    instr[3] == 0 &&
268*7c478bd9Sstevel@tonic-gate 	    instr[4] == 0 &&
269*7c478bd9Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
270*7c478bd9Sstevel@tonic-gate 	    instr[6] == 0)
271*7c478bd9Sstevel@tonic-gate 		return (1);
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	return (0);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
280*7c478bd9Sstevel@tonic-gate  *
281*7c478bd9Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
282*7c478bd9Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
283*7c478bd9Sstevel@tonic-gate  * The third byte is between 0 and 3 inclusive.
284*7c478bd9Sstevel@tonic-gate  */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate #define	PREFETCHSIZE 3
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate static int
289*7c478bd9Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	if (*p == 0x0F && (*(p+1) == 0x18 || *(p+1) == 0x0D) && *(p+2) <= 3)
292*7c478bd9Sstevel@tonic-gate 		return (1);
293*7c478bd9Sstevel@tonic-gate 	return (0);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate static int
297*7c478bd9Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	uchar_t instr[PREFETCHSIZE];
300*7c478bd9Sstevel@tonic-gate 	int	error;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	error = copyin_nowatch(pc, (caddr_t)instr, PREFETCHSIZE);
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	if (error == 0 && cmp_to_prefetch(instr))
305*7c478bd9Sstevel@tonic-gate 		return (1);
306*7c478bd9Sstevel@tonic-gate 	return (0);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate /*
312*7c478bd9Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
313*7c478bd9Sstevel@tonic-gate  *
314*7c478bd9Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
315*7c478bd9Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
316*7c478bd9Sstevel@tonic-gate  */
317*7c478bd9Sstevel@tonic-gate void
318*7c478bd9Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
319*7c478bd9Sstevel@tonic-gate {
320*7c478bd9Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
321*7c478bd9Sstevel@tonic-gate 	enum seg_rw rw;
322*7c478bd9Sstevel@tonic-gate 	unsigned type;
323*7c478bd9Sstevel@tonic-gate 	extern int stop_on_fault(uint_t, k_siginfo_t *);
324*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(cur_thread);
325*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(cur_thread);
326*7c478bd9Sstevel@tonic-gate 	uintptr_t lofault;
327*7c478bd9Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
328*7c478bd9Sstevel@tonic-gate 	enum fault_type fault_type;
329*7c478bd9Sstevel@tonic-gate 	k_siginfo_t siginfo;
330*7c478bd9Sstevel@tonic-gate 	uint_t fault = 0;
331*7c478bd9Sstevel@tonic-gate 	int mstate;
332*7c478bd9Sstevel@tonic-gate 	int sicode = 0;
333*7c478bd9Sstevel@tonic-gate 	int watchcode;
334*7c478bd9Sstevel@tonic-gate 	int watchpage;
335*7c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
336*7c478bd9Sstevel@tonic-gate 	size_t sz;
337*7c478bd9Sstevel@tonic-gate 	int ta;
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	type = rp->r_trapno;
342*7c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	ASSERT(cur_thread->t_schedflag & TS_DONT_SWAP);
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 		errcode = rp->r_err;
349*7c478bd9Sstevel@tonic-gate 		if (errcode & PF_ERR_WRITE)
350*7c478bd9Sstevel@tonic-gate 			rw = S_WRITE;
351*7c478bd9Sstevel@tonic-gate 		else if ((caddr_t)rp->r_pc == addr ||
352*7c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC)))
353*7c478bd9Sstevel@tonic-gate 			rw = S_EXEC;
354*7c478bd9Sstevel@tonic-gate 		else
355*7c478bd9Sstevel@tonic-gate 			rw = S_READ;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate #if defined(__i386)
358*7c478bd9Sstevel@tonic-gate 		/*
359*7c478bd9Sstevel@tonic-gate 		 * Pentium Pro work-around
360*7c478bd9Sstevel@tonic-gate 		 */
361*7c478bd9Sstevel@tonic-gate 		if ((errcode & PF_ERR_PROT) && pentiumpro_bug4046376) {
362*7c478bd9Sstevel@tonic-gate 			uint_t	attr;
363*7c478bd9Sstevel@tonic-gate 			uint_t	priv_violation;
364*7c478bd9Sstevel@tonic-gate 			uint_t	access_violation;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 			if (hat_getattr(addr < (caddr_t)kernelbase ?
367*7c478bd9Sstevel@tonic-gate 			    curproc->p_as->a_hat : kas.a_hat, addr, &attr)
368*7c478bd9Sstevel@tonic-gate 			    == -1) {
369*7c478bd9Sstevel@tonic-gate 				errcode &= ~PF_ERR_PROT;
370*7c478bd9Sstevel@tonic-gate 			} else {
371*7c478bd9Sstevel@tonic-gate 				priv_violation = (errcode & PF_ERR_USER) &&
372*7c478bd9Sstevel@tonic-gate 					!(attr & PROT_USER);
373*7c478bd9Sstevel@tonic-gate 				access_violation = (errcode & PF_ERR_WRITE) &&
374*7c478bd9Sstevel@tonic-gate 					!(attr & PROT_WRITE);
375*7c478bd9Sstevel@tonic-gate 				if (!priv_violation && !access_violation)
376*7c478bd9Sstevel@tonic-gate 					goto cleanup;
377*7c478bd9Sstevel@tonic-gate 			}
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate #endif /* __i386 */
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	if (tdebug)
384*7c478bd9Sstevel@tonic-gate 		showregs(type, rp, addr);
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
387*7c478bd9Sstevel@tonic-gate 		/*
388*7c478bd9Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
389*7c478bd9Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
390*7c478bd9Sstevel@tonic-gate 		 * The current process credential applies to the thread for
391*7c478bd9Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
392*7c478bd9Sstevel@tonic-gate 		 * should already be set up.
393*7c478bd9Sstevel@tonic-gate 		 */
394*7c478bd9Sstevel@tonic-gate 		if (cur_thread->t_cred != p->p_cred) {
395*7c478bd9Sstevel@tonic-gate 			cred_t *oldcred = cur_thread->t_cred;
396*7c478bd9Sstevel@tonic-gate 			/*
397*7c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
398*7c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
399*7c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
400*7c478bd9Sstevel@tonic-gate 			 */
401*7c478bd9Sstevel@tonic-gate 			cur_thread->t_cred = crgetcred();
402*7c478bd9Sstevel@tonic-gate 			crfree(oldcred);
403*7c478bd9Sstevel@tonic-gate 		}
404*7c478bd9Sstevel@tonic-gate 		ASSERT(lwp != NULL);
405*7c478bd9Sstevel@tonic-gate 		type |= USER;
406*7c478bd9Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
407*7c478bd9Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		switch (type) {
410*7c478bd9Sstevel@tonic-gate 		case T_PGFLT + USER:
411*7c478bd9Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
412*7c478bd9Sstevel@tonic-gate 				mstate = LMS_TFAULT;
413*7c478bd9Sstevel@tonic-gate 			else
414*7c478bd9Sstevel@tonic-gate 				mstate = LMS_DFAULT;
415*7c478bd9Sstevel@tonic-gate 			break;
416*7c478bd9Sstevel@tonic-gate 		default:
417*7c478bd9Sstevel@tonic-gate 			mstate = LMS_TRAP;
418*7c478bd9Sstevel@tonic-gate 			break;
419*7c478bd9Sstevel@tonic-gate 		}
420*7c478bd9Sstevel@tonic-gate 		/* Kernel probe */
421*7c478bd9Sstevel@tonic-gate 		TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
422*7c478bd9Sstevel@tonic-gate 		    tnf_microstate, state, mstate);
423*7c478bd9Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, mstate);
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	switch (type) {
429*7c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:
430*7c478bd9Sstevel@tonic-gate 	case T_SGLSTP:
431*7c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER:
432*7c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:
433*7c478bd9Sstevel@tonic-gate 		break;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	default:
436*7c478bd9Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
437*7c478bd9Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
438*7c478bd9Sstevel@tonic-gate 		break;
439*7c478bd9Sstevel@tonic-gate 	}
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	switch (type) {
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	case T_MCE:	/* Machine check exception */
444*7c478bd9Sstevel@tonic-gate 	case T_MCE + USER:
445*7c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_MCA) {
446*7c478bd9Sstevel@tonic-gate 			if (mca_exception(rp))
447*7c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
448*7c478bd9Sstevel@tonic-gate 			type &= ~USER;
449*7c478bd9Sstevel@tonic-gate 			goto cleanup;
450*7c478bd9Sstevel@tonic-gate 		}
451*7c478bd9Sstevel@tonic-gate 	default:
452*7c478bd9Sstevel@tonic-gate 		if (type & USER) {
453*7c478bd9Sstevel@tonic-gate 			if (tudebug)
454*7c478bd9Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
455*7c478bd9Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
456*7c478bd9Sstevel@tonic-gate 			    type & ~USER);
457*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
458*7c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
459*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
460*7c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
461*7c478bd9Sstevel@tonic-gate 			fault = FLTILL;
462*7c478bd9Sstevel@tonic-gate 			break;
463*7c478bd9Sstevel@tonic-gate 		} else {
464*7c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
465*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
466*7c478bd9Sstevel@tonic-gate 		}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
469*7c478bd9Sstevel@tonic-gate 		/*
470*7c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
471*7c478bd9Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
472*7c478bd9Sstevel@tonic-gate 		 */
473*7c478bd9Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
474*7c478bd9Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
475*7c478bd9Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
476*7c478bd9Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 		/*
480*7c478bd9Sstevel@tonic-gate 		 * See if we can handle as pagefault. Save lofault
481*7c478bd9Sstevel@tonic-gate 		 * across this. Here we assume that an address
482*7c478bd9Sstevel@tonic-gate 		 * less than KERNELBASE is a user fault.
483*7c478bd9Sstevel@tonic-gate 		 * We can do this as copy.s routines verify that the
484*7c478bd9Sstevel@tonic-gate 		 * starting address is less than KERNELBASE before
485*7c478bd9Sstevel@tonic-gate 		 * starting and because we know that we always have
486*7c478bd9Sstevel@tonic-gate 		 * KERNELBASE mapped as invalid to serve as a "barrier".
487*7c478bd9Sstevel@tonic-gate 		 */
488*7c478bd9Sstevel@tonic-gate 		lofault = cur_thread->t_lofault;
489*7c478bd9Sstevel@tonic-gate 		cur_thread->t_lofault = 0;
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 		mstate = new_mstate(cur_thread, LMS_KFAULT);
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
494*7c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
495*7c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
496*7c478bd9Sstevel@tonic-gate 			if (res == FC_NOMAP &&
497*7c478bd9Sstevel@tonic-gate 			    addr < p->p_usrstack &&
498*7c478bd9Sstevel@tonic-gate 			    grow(addr))
499*7c478bd9Sstevel@tonic-gate 				res = 0;
500*7c478bd9Sstevel@tonic-gate 		} else {
501*7c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
502*7c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
503*7c478bd9Sstevel@tonic-gate 		}
504*7c478bd9Sstevel@tonic-gate 		(void) new_mstate(cur_thread, mstate);
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 		/*
507*7c478bd9Sstevel@tonic-gate 		 * Restore lofault. If we resolved the fault, exit.
508*7c478bd9Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
509*7c478bd9Sstevel@tonic-gate 		 */
510*7c478bd9Sstevel@tonic-gate 		cur_thread->t_lofault = lofault;
511*7c478bd9Sstevel@tonic-gate 		if (res == 0)
512*7c478bd9Sstevel@tonic-gate 			goto cleanup;
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
515*7c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
516*7c478bd9Sstevel@tonic-gate 			/*
517*7c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
518*7c478bd9Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
519*7c478bd9Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
520*7c478bd9Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
521*7c478bd9Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
522*7c478bd9Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
523*7c478bd9Sstevel@tonic-gate 			 */
524*7c478bd9Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
527*7c478bd9Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
528*7c478bd9Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
529*7c478bd9Sstevel@tonic-gate 				    PFN_INVALID &&
530*7c478bd9Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
531*7c478bd9Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
532*7c478bd9Sstevel@tonic-gate 					rp->r_pc = rip;
533*7c478bd9Sstevel@tonic-gate 					goto cleanup;
534*7c478bd9Sstevel@tonic-gate 				}
535*7c478bd9Sstevel@tonic-gate 			}
536*7c478bd9Sstevel@tonic-gate 		}
537*7c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
540*7c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
541*7c478bd9Sstevel@tonic-gate 			/*
542*7c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
543*7c478bd9Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
544*7c478bd9Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
545*7c478bd9Sstevel@tonic-gate 			 * instruction.
546*7c478bd9Sstevel@tonic-gate 			 */
547*7c478bd9Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 			/*
550*7c478bd9Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
551*7c478bd9Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
552*7c478bd9Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
553*7c478bd9Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
554*7c478bd9Sstevel@tonic-gate 			 */
555*7c478bd9Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
556*7c478bd9Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
557*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
558*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
559*7c478bd9Sstevel@tonic-gate 					    "occurred: kernel prefetch"
560*7c478bd9Sstevel@tonic-gate 					    " at %p generated a page fault!",
561*7c478bd9Sstevel@tonic-gate 					    (void *)rp->r_pc);
562*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
563*7c478bd9Sstevel@tonic-gate 					goto cleanup;
564*7c478bd9Sstevel@tonic-gate 				}
565*7c478bd9Sstevel@tonic-gate 			}
566*7c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
567*7c478bd9Sstevel@tonic-gate 		}
568*7c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 		if (lofault == 0)
571*7c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 		/*
574*7c478bd9Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
575*7c478bd9Sstevel@tonic-gate 		 */
576*7c478bd9Sstevel@tonic-gate 		if (lodebug) {
577*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
578*7c478bd9Sstevel@tonic-gate 			traceregs(rp);
579*7c478bd9Sstevel@tonic-gate 		}
580*7c478bd9Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
581*7c478bd9Sstevel@tonic-gate 			res = FC_ERRNO(res);
582*7c478bd9Sstevel@tonic-gate 		else
583*7c478bd9Sstevel@tonic-gate 			res = EFAULT;
584*7c478bd9Sstevel@tonic-gate 		rp->r_r0 = res;
585*7c478bd9Sstevel@tonic-gate 		rp->r_pc = cur_thread->t_lofault;
586*7c478bd9Sstevel@tonic-gate 		goto cleanup;
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
589*7c478bd9Sstevel@tonic-gate 		if (faultdebug) {
590*7c478bd9Sstevel@tonic-gate 			char *fault_str;
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 			switch (rw) {
593*7c478bd9Sstevel@tonic-gate 			case S_READ:
594*7c478bd9Sstevel@tonic-gate 				fault_str = "read";
595*7c478bd9Sstevel@tonic-gate 				break;
596*7c478bd9Sstevel@tonic-gate 			case S_WRITE:
597*7c478bd9Sstevel@tonic-gate 				fault_str = "write";
598*7c478bd9Sstevel@tonic-gate 				break;
599*7c478bd9Sstevel@tonic-gate 			case S_EXEC:
600*7c478bd9Sstevel@tonic-gate 				fault_str = "exec";
601*7c478bd9Sstevel@tonic-gate 				break;
602*7c478bd9Sstevel@tonic-gate 			default:
603*7c478bd9Sstevel@tonic-gate 				fault_str = "";
604*7c478bd9Sstevel@tonic-gate 				break;
605*7c478bd9Sstevel@tonic-gate 			}
606*7c478bd9Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
607*7c478bd9Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
611*7c478bd9Sstevel@tonic-gate 		/*
612*7c478bd9Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
613*7c478bd9Sstevel@tonic-gate 		 *
614*7c478bd9Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
615*7c478bd9Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
616*7c478bd9Sstevel@tonic-gate 		 * address will be
617*7c478bd9Sstevel@tonic-gate 		 *
618*7c478bd9Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
619*7c478bd9Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
620*7c478bd9Sstevel@tonic-gate 		 *
621*7c478bd9Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
622*7c478bd9Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
623*7c478bd9Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
624*7c478bd9Sstevel@tonic-gate 		 */
625*7c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
626*7c478bd9Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
627*7c478bd9Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
628*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
629*7c478bd9Sstevel@tonic-gate 			if (!opteron_erratum_100)
630*7c478bd9Sstevel@tonic-gate 				panic("unexpected erratum #100");
631*7c478bd9Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
632*7c478bd9Sstevel@tonic-gate 				goto out;
633*7c478bd9Sstevel@tonic-gate 		}
634*7c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
637*7c478bd9Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
638*7c478bd9Sstevel@tonic-gate #ifdef __i386
639*7c478bd9Sstevel@tonic-gate 		/*
640*7c478bd9Sstevel@tonic-gate 		 * In 32-bit mode, the lcall (system call) instruction fetches
641*7c478bd9Sstevel@tonic-gate 		 * one word from the stack, at the stack pointer, because of the
642*7c478bd9Sstevel@tonic-gate 		 * way the call gate is constructed.  This is a bogus
643*7c478bd9Sstevel@tonic-gate 		 * read and should not be counted as a read watchpoint.
644*7c478bd9Sstevel@tonic-gate 		 * We work around the problem here by testing to see if
645*7c478bd9Sstevel@tonic-gate 		 * this situation applies and, if so, simply jumping to
646*7c478bd9Sstevel@tonic-gate 		 * the code in locore.s that fields the system call trap.
647*7c478bd9Sstevel@tonic-gate 		 * The registers on the stack are already set up properly
648*7c478bd9Sstevel@tonic-gate 		 * due to the match between the call gate sequence and the
649*7c478bd9Sstevel@tonic-gate 		 * trap gate sequence.  We just have to adjust the pc.
650*7c478bd9Sstevel@tonic-gate 		 */
651*7c478bd9Sstevel@tonic-gate 		if (watchpage && addr == (caddr_t)rp->r_sp &&
652*7c478bd9Sstevel@tonic-gate 		    rw == S_READ && instr_is_syscall((caddr_t)rp->r_pc)) {
653*7c478bd9Sstevel@tonic-gate 			extern void watch_syscall(void);
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
656*7c478bd9Sstevel@tonic-gate 			watch_syscall();	/* never returns */
657*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
658*7c478bd9Sstevel@tonic-gate 		}
659*7c478bd9Sstevel@tonic-gate #endif /* __i386 */
660*7c478bd9Sstevel@tonic-gate 		vaddr = addr;
661*7c478bd9Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
662*7c478bd9Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
663*7c478bd9Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
664*7c478bd9Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
665*7c478bd9Sstevel@tonic-gate 			if (ta) {
666*7c478bd9Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
667*7c478bd9Sstevel@tonic-gate 					watchcode, rp->r_pc);
668*7c478bd9Sstevel@tonic-gate 				fault_type = F_INVAL;
669*7c478bd9Sstevel@tonic-gate 			} else {
670*7c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
671*7c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
672*7c478bd9Sstevel@tonic-gate 				siginfo.si_code = watchcode;
673*7c478bd9Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
674*7c478bd9Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
675*7c478bd9Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
676*7c478bd9Sstevel@tonic-gate 				fault = FLTWATCH;
677*7c478bd9Sstevel@tonic-gate 				break;
678*7c478bd9Sstevel@tonic-gate 			}
679*7c478bd9Sstevel@tonic-gate 		} else {
680*7c478bd9Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
681*7c478bd9Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
682*7c478bd9Sstevel@tonic-gate 				goto out;
683*7c478bd9Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
684*7c478bd9Sstevel@tonic-gate 			fault_type = F_INVAL;
685*7c478bd9Sstevel@tonic-gate 		}
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 		/*
690*7c478bd9Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
691*7c478bd9Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
692*7c478bd9Sstevel@tonic-gate 		 */
693*7c478bd9Sstevel@tonic-gate 		if (res == 0 ||
694*7c478bd9Sstevel@tonic-gate 		    (res == FC_NOMAP &&
695*7c478bd9Sstevel@tonic-gate 		    addr < p->p_usrstack &&
696*7c478bd9Sstevel@tonic-gate 		    grow(addr))) {
697*7c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
698*7c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
699*7c478bd9Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
700*7c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
701*7c478bd9Sstevel@tonic-gate 				siginfo.si_addr = addr;
702*7c478bd9Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
703*7c478bd9Sstevel@tonic-gate 			}
704*7c478bd9Sstevel@tonic-gate 			goto out;
705*7c478bd9Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
706*7c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
707*7c478bd9Sstevel@tonic-gate 			report_stack_exec(p, addr);
708*7c478bd9Sstevel@tonic-gate 		}
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
711*7c478bd9Sstevel@tonic-gate 		/*
712*7c478bd9Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
713*7c478bd9Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
714*7c478bd9Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
715*7c478bd9Sstevel@tonic-gate 		 *
716*7c478bd9Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
717*7c478bd9Sstevel@tonic-gate 		 */
718*7c478bd9Sstevel@tonic-gate 		if (opteron_erratum_91 &&
719*7c478bd9Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
720*7c478bd9Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
721*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
722*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
723*7c478bd9Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
724*7c478bd9Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
725*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
726*7c478bd9Sstevel@tonic-gate 			goto out;
727*7c478bd9Sstevel@tonic-gate 		}
728*7c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 		if (tudebug)
731*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
732*7c478bd9Sstevel@tonic-gate 		/*
733*7c478bd9Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
734*7c478bd9Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
735*7c478bd9Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
736*7c478bd9Sstevel@tonic-gate 		 */
737*7c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
738*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr = addr;
739*7c478bd9Sstevel@tonic-gate 		switch (FC_CODE(res)) {
740*7c478bd9Sstevel@tonic-gate 		case FC_HWERR:
741*7c478bd9Sstevel@tonic-gate 		case FC_NOSUPPORT:
742*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
743*7c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
744*7c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
745*7c478bd9Sstevel@tonic-gate 			break;
746*7c478bd9Sstevel@tonic-gate 		case FC_ALIGN:
747*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
748*7c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
749*7c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
750*7c478bd9Sstevel@tonic-gate 			break;
751*7c478bd9Sstevel@tonic-gate 		case FC_OBJERR:
752*7c478bd9Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
753*7c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
754*7c478bd9Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
755*7c478bd9Sstevel@tonic-gate 				fault = FLTACCESS;
756*7c478bd9Sstevel@tonic-gate 			}
757*7c478bd9Sstevel@tonic-gate 			break;
758*7c478bd9Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
759*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
760*7c478bd9Sstevel@tonic-gate 			siginfo.si_code =
761*7c478bd9Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
762*7c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
763*7c478bd9Sstevel@tonic-gate 			break;
764*7c478bd9Sstevel@tonic-gate 		}
765*7c478bd9Sstevel@tonic-gate 		break;
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
768*7c478bd9Sstevel@tonic-gate 		/*
769*7c478bd9Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
770*7c478bd9Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
771*7c478bd9Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
772*7c478bd9Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
773*7c478bd9Sstevel@tonic-gate 		 * be to emulate that particular instruction.
774*7c478bd9Sstevel@tonic-gate 		 */
775*7c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
776*7c478bd9Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_ASYSC)) {
777*7c478bd9Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
778*7c478bd9Sstevel@tonic-gate 				goto out;
779*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
780*7c478bd9Sstevel@tonic-gate 			else
781*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite syscall "
782*7c478bd9Sstevel@tonic-gate 				    "instruction in process %d",
783*7c478bd9Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
784*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
785*7c478bd9Sstevel@tonic-gate 		}
786*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 		if (tudebug)
789*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
790*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
791*7c478bd9Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
792*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
793*7c478bd9Sstevel@tonic-gate 		fault = FLTILL;
794*7c478bd9Sstevel@tonic-gate 		break;
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
797*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
798*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
799*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
800*7c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
801*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
802*7c478bd9Sstevel@tonic-gate 		fault = FLTIZDIV;
803*7c478bd9Sstevel@tonic-gate 		break;
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
806*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
807*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
808*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
809*7c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
810*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
811*7c478bd9Sstevel@tonic-gate 		fault = FLTIOVF;
812*7c478bd9Sstevel@tonic-gate 		break;
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
815*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
816*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
817*7c478bd9Sstevel@tonic-gate 		if (fpnoextflt(rp)) {
818*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
819*7c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLOPC;
820*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
821*7c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
822*7c478bd9Sstevel@tonic-gate 		}
823*7c478bd9Sstevel@tonic-gate 		break;
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT:	/* extension overrun fault */
826*7c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
827*7c478bd9Sstevel@tonic-gate 		{
828*7c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
829*7c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
830*7c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
831*7c478bd9Sstevel@tonic-gate 			type |= USER;
832*7c478bd9Sstevel@tonic-gate 		}
833*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
834*7c478bd9Sstevel@tonic-gate 	case T_EXTOVRFLT + USER:	/* extension overrun fault */
835*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
836*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
837*7c478bd9Sstevel@tonic-gate 		if (fpextovrflt(rp)) {
838*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
839*7c478bd9Sstevel@tonic-gate 			siginfo.si_code  = SEGV_MAPERR;
840*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
841*7c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
842*7c478bd9Sstevel@tonic-gate 		}
843*7c478bd9Sstevel@tonic-gate 		break;
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
846*7c478bd9Sstevel@tonic-gate 		/* check if we took a kernel trap on behalf of user */
847*7c478bd9Sstevel@tonic-gate 		{
848*7c478bd9Sstevel@tonic-gate 			extern  void ndptrap_frstor(void);
849*7c478bd9Sstevel@tonic-gate 			if (rp->r_pc != (uintptr_t)ndptrap_frstor)
850*7c478bd9Sstevel@tonic-gate 				(void) die(type, rp, addr, cpuid);
851*7c478bd9Sstevel@tonic-gate 			type |= USER;
852*7c478bd9Sstevel@tonic-gate 		}
853*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
856*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
857*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
858*7c478bd9Sstevel@tonic-gate 		if (sicode = fpexterrflt(rp)) {
859*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
860*7c478bd9Sstevel@tonic-gate 			siginfo.si_code  = sicode;
861*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
862*7c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
863*7c478bd9Sstevel@tonic-gate 		}
864*7c478bd9Sstevel@tonic-gate 		break;
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
867*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugsse)
868*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
869*7c478bd9Sstevel@tonic-gate 		if ((x86_feature & (X86_SSE|X86_SSE2)) == 0) {
870*7c478bd9Sstevel@tonic-gate 			/*
871*7c478bd9Sstevel@tonic-gate 			 * There are rumours that some user instructions
872*7c478bd9Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
873*7c478bd9Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
874*7c478bd9Sstevel@tonic-gate 			 */
875*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
876*7c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
877*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
878*7c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
879*7c478bd9Sstevel@tonic-gate 			fault = FLTILL;
880*7c478bd9Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
881*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
882*7c478bd9Sstevel@tonic-gate 			siginfo.si_code = sicode;
883*7c478bd9Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
884*7c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
885*7c478bd9Sstevel@tonic-gate 		}
886*7c478bd9Sstevel@tonic-gate 		break;
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
889*7c478bd9Sstevel@tonic-gate 		/*
890*7c478bd9Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
891*7c478bd9Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
892*7c478bd9Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
893*7c478bd9Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
894*7c478bd9Sstevel@tonic-gate 		 * fatal.
895*7c478bd9Sstevel@tonic-gate 		 */
896*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
897*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
900*7c478bd9Sstevel@tonic-gate 		break;
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
903*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
904*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 		/* Now evaluate how we got here */
907*7c478bd9Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
908*7c478bd9Sstevel@tonic-gate 			/*
909*7c478bd9Sstevel@tonic-gate 			 * i386 single-steps even through lcalls which
910*7c478bd9Sstevel@tonic-gate 			 * change the privilege level. So we take a trap at
911*7c478bd9Sstevel@tonic-gate 			 * the first instruction in privileged mode.
912*7c478bd9Sstevel@tonic-gate 			 *
913*7c478bd9Sstevel@tonic-gate 			 * Set a flag to indicate that upon completion of
914*7c478bd9Sstevel@tonic-gate 			 * the system call, deal with the single-step trap.
915*7c478bd9Sstevel@tonic-gate 			 *
916*7c478bd9Sstevel@tonic-gate 			 * The same thing happens for sysenter, too.
917*7c478bd9Sstevel@tonic-gate 			 */
918*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
919*7c478bd9Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_sysenter) {
920*7c478bd9Sstevel@tonic-gate 				/*
921*7c478bd9Sstevel@tonic-gate 				 * Adjust the pc so that we don't execute the
922*7c478bd9Sstevel@tonic-gate 				 * swapgs instruction at the head of the
923*7c478bd9Sstevel@tonic-gate 				 * handler and completely confuse things.
924*7c478bd9Sstevel@tonic-gate 				 */
925*7c478bd9Sstevel@tonic-gate 				rp->r_pc = (uintptr_t)
926*7c478bd9Sstevel@tonic-gate 				    _sys_sysenter_post_swapgs;
927*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
928*7c478bd9Sstevel@tonic-gate 			if (rp->r_pc == (uintptr_t)sys_call ||
929*7c478bd9Sstevel@tonic-gate 			    rp->r_pc == (uintptr_t)sys_sysenter) {
930*7c478bd9Sstevel@tonic-gate #endif
931*7c478bd9Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
932*7c478bd9Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
933*7c478bd9Sstevel@tonic-gate 				cur_thread->t_post_sys = 1;
934*7c478bd9Sstevel@tonic-gate 				goto cleanup;
935*7c478bd9Sstevel@tonic-gate 			}
936*7c478bd9Sstevel@tonic-gate 		}
937*7c478bd9Sstevel@tonic-gate 		/* XXX - needs review on debugger interface? */
938*7c478bd9Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
939*7c478bd9Sstevel@tonic-gate 			debug_enter((char *)NULL);
940*7c478bd9Sstevel@tonic-gate 		else
941*7c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
942*7c478bd9Sstevel@tonic-gate 		break;
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
945*7c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
946*7c478bd9Sstevel@tonic-gate 		goto cleanup;
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
949*7c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
950*7c478bd9Sstevel@tonic-gate 		break;
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
953*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
954*7c478bd9Sstevel@tonic-gate 		/*
955*7c478bd9Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
956*7c478bd9Sstevel@tonic-gate 		 * in the virtual address hole e.g. from a copyin.
957*7c478bd9Sstevel@tonic-gate 		 */
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate 		/*
960*7c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
961*7c478bd9Sstevel@tonic-gate 		 * set ot_trap and longjmp back to the on_trap() call site.
962*7c478bd9Sstevel@tonic-gate 		 */
963*7c478bd9Sstevel@tonic-gate 		if ((cur_thread->t_ontrap != NULL) &&
964*7c478bd9Sstevel@tonic-gate 		    (cur_thread->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
965*7c478bd9Sstevel@tonic-gate 			curthread->t_ontrap->ot_trap |= OT_DATA_ACCESS;
966*7c478bd9Sstevel@tonic-gate 			longjmp(&curthread->t_ontrap->ot_jmpbuf);
967*7c478bd9Sstevel@tonic-gate 		}
968*7c478bd9Sstevel@tonic-gate 
969*7c478bd9Sstevel@tonic-gate 		/*
970*7c478bd9Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
971*7c478bd9Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
972*7c478bd9Sstevel@tonic-gate 		 */
973*7c478bd9Sstevel@tonic-gate 		if (cur_thread->t_lofault) {
974*7c478bd9Sstevel@tonic-gate 			/*
975*7c478bd9Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
976*7c478bd9Sstevel@tonic-gate 			 */
977*7c478bd9Sstevel@tonic-gate 			if (lodebug) {
978*7c478bd9Sstevel@tonic-gate 				showregs(type, rp, addr);
979*7c478bd9Sstevel@tonic-gate 				traceregs(rp);
980*7c478bd9Sstevel@tonic-gate 			}
981*7c478bd9Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
982*7c478bd9Sstevel@tonic-gate 			rp->r_pc = cur_thread->t_lofault;
983*7c478bd9Sstevel@tonic-gate 			goto cleanup;
984*7c478bd9Sstevel@tonic-gate 		}
985*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
986*7c478bd9Sstevel@tonic-gate #endif
987*7c478bd9Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
988*7c478bd9Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
989*7c478bd9Sstevel@tonic-gate 	case T_SEGFLT:	/* segment not present fault */
990*7c478bd9Sstevel@tonic-gate 		if (tudebug)
991*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
992*7c478bd9Sstevel@tonic-gate 		if (kern_gpfault(rp))
993*7c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
994*7c478bd9Sstevel@tonic-gate 		goto cleanup;
995*7c478bd9Sstevel@tonic-gate 
996*7c478bd9Sstevel@tonic-gate 	case T_SEGFLT + USER:	/* segment not present fault */
997*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
998*7c478bd9Sstevel@tonic-gate 		if (instr_is_syscall((caddr_t)rp->r_pc)) {
999*7c478bd9Sstevel@tonic-gate 			/*
1000*7c478bd9Sstevel@tonic-gate 			 * System calls via the call gate come in through
1001*7c478bd9Sstevel@tonic-gate 			 * not-present traps.
1002*7c478bd9Sstevel@tonic-gate 			 *
1003*7c478bd9Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
1004*7c478bd9Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
1005*7c478bd9Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
1006*7c478bd9Sstevel@tonic-gate 			 */
1007*7c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
1008*7c478bd9Sstevel@tonic-gate 			lwp->lwp_regs = rp;
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 			/*
1011*7c478bd9Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
1012*7c478bd9Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
1013*7c478bd9Sstevel@tonic-gate 			 * behavior here.
1014*7c478bd9Sstevel@tonic-gate 			 */
1015*7c478bd9Sstevel@tonic-gate 			mstate = LMS_USER;
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 			dosyscall();
1018*7c478bd9Sstevel@tonic-gate 			goto out;
1019*7c478bd9Sstevel@tonic-gate 		}
1020*7c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1021*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	case T_GPFLT + USER:	/* general protection violation */
1024*7c478bd9Sstevel@tonic-gate 		/*
1025*7c478bd9Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
1026*7c478bd9Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
1027*7c478bd9Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
1028*7c478bd9Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
1029*7c478bd9Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
1030*7c478bd9Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
1031*7c478bd9Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
1032*7c478bd9Sstevel@tonic-gate 		 */
1033*7c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
1034*7c478bd9Sstevel@tonic-gate 		    instr_is_fast_syscall((caddr_t)rp->r_pc, X86_SEP)) {
1035*7c478bd9Sstevel@tonic-gate 			if (rewrite_syscall((caddr_t)rp->r_pc) == 0)
1036*7c478bd9Sstevel@tonic-gate 				goto out;
1037*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
1038*7c478bd9Sstevel@tonic-gate 			else
1039*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "failed to rewrite sysenter "
1040*7c478bd9Sstevel@tonic-gate 				    "instruction in process %d",
1041*7c478bd9Sstevel@tonic-gate 				    curthread->t_procp->p_pid);
1042*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1043*7c478bd9Sstevel@tonic-gate 		}
1044*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1045*7c478bd9Sstevel@tonic-gate 
1046*7c478bd9Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
1047*7c478bd9Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
1048*7c478bd9Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
1049*7c478bd9Sstevel@tonic-gate 		if (tudebug)
1050*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
1051*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
1052*7c478bd9Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
1053*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
1054*7c478bd9Sstevel@tonic-gate 		fault = FLTBOUNDS;
1055*7c478bd9Sstevel@tonic-gate 		break;
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
1058*7c478bd9Sstevel@tonic-gate 		if (tudebug)
1059*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
1060*7c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
1061*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
1062*7c478bd9Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
1063*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
1064*7c478bd9Sstevel@tonic-gate 		fault = FLTACCESS;
1065*7c478bd9Sstevel@tonic-gate 		break;
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
1068*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
1069*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 		/* Was it single-stepping? */
1072*7c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
1073*7c478bd9Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
1074*7c478bd9Sstevel@tonic-gate 
1075*7c478bd9Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
1076*7c478bd9Sstevel@tonic-gate 			/*
1077*7c478bd9Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
1078*7c478bd9Sstevel@tonic-gate 			 * give precedence to NORMAL_STEP.  If neither is set,
1079*7c478bd9Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
1080*7c478bd9Sstevel@tonic-gate 			 * as NORMAL_STEP.
1081*7c478bd9Sstevel@tonic-gate 			 */
1082*7c478bd9Sstevel@tonic-gate 			if ((pcb->pcb_flags & NORMAL_STEP) ||
1083*7c478bd9Sstevel@tonic-gate 			    !(pcb->pcb_flags & WATCH_STEP)) {
1084*7c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
1085*7c478bd9Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
1086*7c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
1087*7c478bd9Sstevel@tonic-gate 				fault = FLTTRACE;
1088*7c478bd9Sstevel@tonic-gate 				if (pcb->pcb_flags & WATCH_STEP)
1089*7c478bd9Sstevel@tonic-gate 					(void) undo_watch_step(NULL);
1090*7c478bd9Sstevel@tonic-gate 			} else {
1091*7c478bd9Sstevel@tonic-gate 				fault = undo_watch_step(&siginfo);
1092*7c478bd9Sstevel@tonic-gate 			}
1093*7c478bd9Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
1094*7c478bd9Sstevel@tonic-gate 		} else {
1095*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
1096*7c478bd9Sstevel@tonic-gate 			    "Unexpected INT 1 in user mode, dr6=%lx",
1097*7c478bd9Sstevel@tonic-gate 			    lwp->lwp_pcb.pcb_drstat);
1098*7c478bd9Sstevel@tonic-gate 		}
1099*7c478bd9Sstevel@tonic-gate 		break;
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
1102*7c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
1103*7c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
1104*7c478bd9Sstevel@tonic-gate 		/*
1105*7c478bd9Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
1106*7c478bd9Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
1107*7c478bd9Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
1108*7c478bd9Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
1109*7c478bd9Sstevel@tonic-gate 		 */
1110*7c478bd9Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
1111*7c478bd9Sstevel@tonic-gate 			rp->r_pc--;
1112*7c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
1113*7c478bd9Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
1114*7c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
1115*7c478bd9Sstevel@tonic-gate 		fault = FLTBPT;
1116*7c478bd9Sstevel@tonic-gate 		break;
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate 	case T_AST:
1119*7c478bd9Sstevel@tonic-gate 		/*
1120*7c478bd9Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
1121*7c478bd9Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
1122*7c478bd9Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
1123*7c478bd9Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
1124*7c478bd9Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
1125*7c478bd9Sstevel@tonic-gate 		 * doing the T_AST processing.
1126*7c478bd9Sstevel@tonic-gate 		 */
1127*7c478bd9Sstevel@tonic-gate 		goto cleanup;
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate 	case T_AST + USER:		/* profiling or resched pseudo trap */
1130*7c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
1131*7c478bd9Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
1132*7c478bd9Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
1133*7c478bd9Sstevel@tonic-gate 				/*
1134*7c478bd9Sstevel@tonic-gate 				 * Signal performance counter overflow
1135*7c478bd9Sstevel@tonic-gate 				 */
1136*7c478bd9Sstevel@tonic-gate 				if (tudebug)
1137*7c478bd9Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
1138*7c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
1139*7c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
1140*7c478bd9Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
1141*7c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
1142*7c478bd9Sstevel@tonic-gate 				fault = FLTCPCOVF;
1143*7c478bd9Sstevel@tonic-gate 			}
1144*7c478bd9Sstevel@tonic-gate 		}
1145*7c478bd9Sstevel@tonic-gate 		break;
1146*7c478bd9Sstevel@tonic-gate 	}
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	/*
1149*7c478bd9Sstevel@tonic-gate 	 * We can't get here from a system trap
1150*7c478bd9Sstevel@tonic-gate 	 */
1151*7c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
1152*7c478bd9Sstevel@tonic-gate 
1153*7c478bd9Sstevel@tonic-gate 	if (fault) {
1154*7c478bd9Sstevel@tonic-gate 		/*
1155*7c478bd9Sstevel@tonic-gate 		 * Remember the fault and fault adddress
1156*7c478bd9Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
1157*7c478bd9Sstevel@tonic-gate 		 */
1158*7c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
1159*7c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
1160*7c478bd9Sstevel@tonic-gate 
1161*7c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 		/*
1164*7c478bd9Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
1165*7c478bd9Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
1166*7c478bd9Sstevel@tonic-gate 		 * deliver the associated signal.
1167*7c478bd9Sstevel@tonic-gate 		 */
1168*7c478bd9Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
1169*7c478bd9Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
1170*7c478bd9Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
1171*7c478bd9Sstevel@tonic-gate 			siginfo.si_signo = 0;
1172*7c478bd9Sstevel@tonic-gate 	}
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	if (siginfo.si_signo)
1175*7c478bd9Sstevel@tonic-gate 		trapsig(&siginfo, (fault == FLTCPCOVF)? 0 : 1);
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
1178*7c478bd9Sstevel@tonic-gate 		profil_tick(rp->r_pc);
1179*7c478bd9Sstevel@tonic-gate 
1180*7c478bd9Sstevel@tonic-gate 	if (cur_thread->t_astflag | cur_thread->t_sig_check) {
1181*7c478bd9Sstevel@tonic-gate 		/*
1182*7c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
1183*7c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
1184*7c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
1185*7c478bd9Sstevel@tonic-gate 		 * syscall.
1186*7c478bd9Sstevel@tonic-gate 		 */
1187*7c478bd9Sstevel@tonic-gate 		astoff(cur_thread);
1188*7c478bd9Sstevel@tonic-gate 		cur_thread->t_sig_check = 0;
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1191*7c478bd9Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
1192*7c478bd9Sstevel@tonic-gate 			timer_lwpbind();
1193*7c478bd9Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
1194*7c478bd9Sstevel@tonic-gate 		}
1195*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 		/*
1198*7c478bd9Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
1199*7c478bd9Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
1200*7c478bd9Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
1201*7c478bd9Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
1202*7c478bd9Sstevel@tonic-gate 		 * for the kaio request to complete.
1203*7c478bd9Sstevel@tonic-gate 		 */
1204*7c478bd9Sstevel@tonic-gate 		if (p->p_aio)
1205*7c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
1206*7c478bd9Sstevel@tonic-gate 		/*
1207*7c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
1208*7c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
1209*7c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
1210*7c478bd9Sstevel@tonic-gate 		 *
1211*7c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
1212*7c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
1213*7c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
1214*7c478bd9Sstevel@tonic-gate 		 */
1215*7c478bd9Sstevel@tonic-gate 		if (ISHOLD(p))
1216*7c478bd9Sstevel@tonic-gate 			holdlwp();
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 		/*
1219*7c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
1220*7c478bd9Sstevel@tonic-gate 		 * set t_astflag afterwards.
1221*7c478bd9Sstevel@tonic-gate 		 */
1222*7c478bd9Sstevel@tonic-gate 		if (ISSIG_PENDING(cur_thread, lwp, p)) {
1223*7c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
1224*7c478bd9Sstevel@tonic-gate 				psig();
1225*7c478bd9Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
1226*7c478bd9Sstevel@tonic-gate 		}
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate 		if (cur_thread->t_rprof != NULL) {
1229*7c478bd9Sstevel@tonic-gate 			realsigprof(0, 0);
1230*7c478bd9Sstevel@tonic-gate 			cur_thread->t_sig_check = 1;
1231*7c478bd9Sstevel@tonic-gate 		}
1232*7c478bd9Sstevel@tonic-gate 	}
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate out:	/* We can't get here from a system trap */
1235*7c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
1236*7c478bd9Sstevel@tonic-gate 
1237*7c478bd9Sstevel@tonic-gate 	if (ISHOLD(p))
1238*7c478bd9Sstevel@tonic-gate 		holdlwp();
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate 	/*
1241*7c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
1242*7c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
1243*7c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
1244*7c478bd9Sstevel@tonic-gate 	 *
1245*7c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
1246*7c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
1247*7c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
1248*7c478bd9Sstevel@tonic-gate 	 */
1249*7c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 	if (cur_thread->t_trapret) {
1252*7c478bd9Sstevel@tonic-gate 		cur_thread->t_trapret = 0;
1253*7c478bd9Sstevel@tonic-gate 		thread_lock(cur_thread);
1254*7c478bd9Sstevel@tonic-gate 		CL_TRAPRET(cur_thread);
1255*7c478bd9Sstevel@tonic-gate 		thread_unlock(cur_thread);
1256*7c478bd9Sstevel@tonic-gate 	}
1257*7c478bd9Sstevel@tonic-gate 	if (CPU->cpu_runrun)
1258*7c478bd9Sstevel@tonic-gate 		preempt();
1259*7c478bd9Sstevel@tonic-gate 	(void) new_mstate(cur_thread, mstate);
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate 	/* Kernel probe */
1262*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
1263*7c478bd9Sstevel@tonic-gate 	    tnf_microstate, state, LMS_USER);
1264*7c478bd9Sstevel@tonic-gate 
1265*7c478bd9Sstevel@tonic-gate 	return;
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate cleanup:	/* system traps end up here */
1268*7c478bd9Sstevel@tonic-gate 	ASSERT(!(type & USER));
1269*7c478bd9Sstevel@tonic-gate }
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate /*
1272*7c478bd9Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
1273*7c478bd9Sstevel@tonic-gate  */
1274*7c478bd9Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
1277*7c478bd9Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
1278*7c478bd9Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
1279*7c478bd9Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
1280*7c478bd9Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
1281*7c478bd9Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
1282*7c478bd9Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
1283*7c478bd9Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
1284*7c478bd9Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
1285*7c478bd9Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
1286*7c478bd9Sstevel@tonic-gate } kpreempt_cnts;
1287*7c478bd9Sstevel@tonic-gate 
1288*7c478bd9Sstevel@tonic-gate /*
1289*7c478bd9Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
1290*7c478bd9Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
1291*7c478bd9Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
1292*7c478bd9Sstevel@tonic-gate  */
1293*7c478bd9Sstevel@tonic-gate void
1294*7c478bd9Sstevel@tonic-gate kpreempt(int asyncspl)
1295*7c478bd9Sstevel@tonic-gate {
1296*7c478bd9Sstevel@tonic-gate 	kthread_t *cur_thread = curthread;
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
1299*7c478bd9Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
1300*7c478bd9Sstevel@tonic-gate 		return;
1301*7c478bd9Sstevel@tonic-gate 	}
1302*7c478bd9Sstevel@tonic-gate 
1303*7c478bd9Sstevel@tonic-gate 	/*
1304*7c478bd9Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
1305*7c478bd9Sstevel@tonic-gate 	 */
1306*7c478bd9Sstevel@tonic-gate 	do {
1307*7c478bd9Sstevel@tonic-gate 		if (cur_thread->t_preempt) {
1308*7c478bd9Sstevel@tonic-gate 			/*
1309*7c478bd9Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
1310*7c478bd9Sstevel@tonic-gate 			 *	or will check when t_preempt is lowered
1311*7c478bd9Sstevel@tonic-gate 			 */
1312*7c478bd9Sstevel@tonic-gate 			if (cur_thread->t_pri < 0)
1313*7c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
1314*7c478bd9Sstevel@tonic-gate 			else if (cur_thread->t_flag & T_INTR_THREAD) {
1315*7c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
1316*7c478bd9Sstevel@tonic-gate 				if (cur_thread->t_pil == CLOCK_LEVEL)
1317*7c478bd9Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
1318*7c478bd9Sstevel@tonic-gate 			} else
1319*7c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
1320*7c478bd9Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
1321*7c478bd9Sstevel@tonic-gate 			return;
1322*7c478bd9Sstevel@tonic-gate 		}
1323*7c478bd9Sstevel@tonic-gate 		if (cur_thread->t_state != TS_ONPROC ||
1324*7c478bd9Sstevel@tonic-gate 		    cur_thread->t_disp_queue != CPU->cpu_disp) {
1325*7c478bd9Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
1326*7c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
1327*7c478bd9Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
1328*7c478bd9Sstevel@tonic-gate 				/* already in swtch(), force another */
1329*7c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
1330*7c478bd9Sstevel@tonic-gate 				siron();
1331*7c478bd9Sstevel@tonic-gate 			}
1332*7c478bd9Sstevel@tonic-gate 			return;
1333*7c478bd9Sstevel@tonic-gate 		}
1334*7c478bd9Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
1335*7c478bd9Sstevel@tonic-gate 			/*
1336*7c478bd9Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
1337*7c478bd9Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
1338*7c478bd9Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
1339*7c478bd9Sstevel@tonic-gate 			 */
1340*7c478bd9Sstevel@tonic-gate 			siron();	/* check back later */
1341*7c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
1342*7c478bd9Sstevel@tonic-gate 			return;
1343*7c478bd9Sstevel@tonic-gate 		}
1344*7c478bd9Sstevel@tonic-gate 
1345*7c478bd9Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
1346*7c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
1347*7c478bd9Sstevel@tonic-gate 		else
1348*7c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
1349*7c478bd9Sstevel@tonic-gate 
1350*7c478bd9Sstevel@tonic-gate 		cur_thread->t_preempt++;
1351*7c478bd9Sstevel@tonic-gate 		preempt();
1352*7c478bd9Sstevel@tonic-gate 		cur_thread->t_preempt--;
1353*7c478bd9Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
1354*7c478bd9Sstevel@tonic-gate }
1355*7c478bd9Sstevel@tonic-gate 
1356*7c478bd9Sstevel@tonic-gate /*
1357*7c478bd9Sstevel@tonic-gate  * Print out debugging info.
1358*7c478bd9Sstevel@tonic-gate  */
1359*7c478bd9Sstevel@tonic-gate static void
1360*7c478bd9Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
1361*7c478bd9Sstevel@tonic-gate {
1362*7c478bd9Sstevel@tonic-gate 	int s;
1363*7c478bd9Sstevel@tonic-gate 
1364*7c478bd9Sstevel@tonic-gate 	s = spl7();
1365*7c478bd9Sstevel@tonic-gate 	type &= ~USER;
1366*7c478bd9Sstevel@tonic-gate 	if (u.u_comm[0])
1367*7c478bd9Sstevel@tonic-gate 		printf("%s: ", u.u_comm);
1368*7c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES)
1369*7c478bd9Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
1370*7c478bd9Sstevel@tonic-gate 	else
1371*7c478bd9Sstevel@tonic-gate 		switch (type) {
1372*7c478bd9Sstevel@tonic-gate 		case T_SYSCALL:
1373*7c478bd9Sstevel@tonic-gate 			printf("Syscall Trap:\n");
1374*7c478bd9Sstevel@tonic-gate 			break;
1375*7c478bd9Sstevel@tonic-gate 		case T_AST:
1376*7c478bd9Sstevel@tonic-gate 			printf("AST\n");
1377*7c478bd9Sstevel@tonic-gate 			break;
1378*7c478bd9Sstevel@tonic-gate 		default:
1379*7c478bd9Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
1380*7c478bd9Sstevel@tonic-gate 			break;
1381*7c478bd9Sstevel@tonic-gate 		}
1382*7c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
1383*7c478bd9Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
1384*7c478bd9Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
1385*7c478bd9Sstevel@tonic-gate 	} else if (addr) {
1386*7c478bd9Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
1387*7c478bd9Sstevel@tonic-gate 	}
1388*7c478bd9Sstevel@tonic-gate 
1389*7c478bd9Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
1390*7c478bd9Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
1391*7c478bd9Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
1392*7c478bd9Sstevel@tonic-gate 
1393*7c478bd9Sstevel@tonic-gate #if defined(__lint)
1394*7c478bd9Sstevel@tonic-gate 	/*
1395*7c478bd9Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
1396*7c478bd9Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
1397*7c478bd9Sstevel@tonic-gate 	 */
1398*7c478bd9Sstevel@tonic-gate 	printf("cr0: %x cr4: %b\n",
1399*7c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
1400*7c478bd9Sstevel@tonic-gate #else
1401*7c478bd9Sstevel@tonic-gate 	printf("cr0: %b cr4: %b\n",
1402*7c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
1403*7c478bd9Sstevel@tonic-gate #endif
1404*7c478bd9Sstevel@tonic-gate 
1405*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1406*7c478bd9Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx cr8: %lx\n", getcr2(), getcr3(), getcr8());
1407*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1408*7c478bd9Sstevel@tonic-gate 	printf("cr2: %lx cr3: %lx\n", getcr2(), getcr3());
1409*7c478bd9Sstevel@tonic-gate #endif
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate 	dumpregs(rp);
1412*7c478bd9Sstevel@tonic-gate 	splx(s);
1413*7c478bd9Sstevel@tonic-gate }
1414*7c478bd9Sstevel@tonic-gate 
1415*7c478bd9Sstevel@tonic-gate static void
1416*7c478bd9Sstevel@tonic-gate dumpregs(struct regs *rp)
1417*7c478bd9Sstevel@tonic-gate {
1418*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1419*7c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
1422*7c478bd9Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
1423*7c478bd9Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
1424*7c478bd9Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
1425*7c478bd9Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
1426*7c478bd9Sstevel@tonic-gate 
1427*7c478bd9Sstevel@tonic-gate 	printf(fmt, "fsb", rp->r_fsbase, "gsb", rp->r_gsbase, " ds", rp->r_ds);
1428*7c478bd9Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
1429*7c478bd9Sstevel@tonic-gate 
1430*7c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
1431*7c478bd9Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
1432*7c478bd9Sstevel@tonic-gate 
1433*7c478bd9Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1436*7c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %8lx %3s: %8lx %3s: %8lx %3s: %8lx\n";
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 	printf(fmt, " gs", rp->r_gs, " fs", rp->r_fs,
1439*7c478bd9Sstevel@tonic-gate 	    " es", rp->r_es, " ds", rp->r_ds);
1440*7c478bd9Sstevel@tonic-gate 	printf(fmt, "edi", rp->r_edi, "esi", rp->r_esi,
1441*7c478bd9Sstevel@tonic-gate 	    "ebp", rp->r_ebp, "esp", rp->r_esp);
1442*7c478bd9Sstevel@tonic-gate 	printf(fmt, "ebx", rp->r_ebx, "edx", rp->r_edx,
1443*7c478bd9Sstevel@tonic-gate 	    "ecx", rp->r_ecx, "eax", rp->r_eax);
1444*7c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err,
1445*7c478bd9Sstevel@tonic-gate 	    "eip", rp->r_eip, " cs", rp->r_cs);
1446*7c478bd9Sstevel@tonic-gate 	printf("\t%3s: %8lx %3s: %8lx %3s: %8lx\n",
1447*7c478bd9Sstevel@tonic-gate 	    "efl", rp->r_efl, "usp", rp->r_uesp, " ss", rp->r_ss);
1448*7c478bd9Sstevel@tonic-gate 
1449*7c478bd9Sstevel@tonic-gate #endif	/* __i386 */
1450*7c478bd9Sstevel@tonic-gate }
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate /*
1453*7c478bd9Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
1454*7c478bd9Sstevel@tonic-gate  *
1455*7c478bd9Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
1456*7c478bd9Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
1457*7c478bd9Sstevel@tonic-gate  *
1458*7c478bd9Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
1459*7c478bd9Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
1460*7c478bd9Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
1461*7c478bd9Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
1462*7c478bd9Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
1463*7c478bd9Sstevel@tonic-gate  *
1464*7c478bd9Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
1465*7c478bd9Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
1466*7c478bd9Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
1467*7c478bd9Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
1468*7c478bd9Sstevel@tonic-gate  * what we were going to send the lwp.)
1469*7c478bd9Sstevel@tonic-gate  *
1470*7c478bd9Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
1471*7c478bd9Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
1472*7c478bd9Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
1473*7c478bd9Sstevel@tonic-gate  */
1474*7c478bd9Sstevel@tonic-gate static int
1475*7c478bd9Sstevel@tonic-gate kern_gpfault(struct regs *rp)
1476*7c478bd9Sstevel@tonic-gate {
1477*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1478*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1479*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1480*7c478bd9Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
1481*7c478bd9Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
1482*7c478bd9Sstevel@tonic-gate 	int v;
1483*7c478bd9Sstevel@tonic-gate 
1484*7c478bd9Sstevel@tonic-gate 	extern void _sys_rtt(), sr_sup();
1485*7c478bd9Sstevel@tonic-gate 
1486*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1487*7c478bd9Sstevel@tonic-gate 	extern void _update_sregs(), _update_sregs_done();
1488*7c478bd9Sstevel@tonic-gate 	static const uint8_t iretq_insn[2] = { 0x48, 0xcf };
1489*7c478bd9Sstevel@tonic-gate 
1490*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1491*7c478bd9Sstevel@tonic-gate 	static const uint8_t iret_insn[1] = { 0xcf };
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 	/*
1494*7c478bd9Sstevel@tonic-gate 	 * Note carefully the appallingly awful dependency between
1495*7c478bd9Sstevel@tonic-gate 	 * the instruction sequence used in __SEGREGS_POP and these
1496*7c478bd9Sstevel@tonic-gate 	 * instructions encoded here.
1497*7c478bd9Sstevel@tonic-gate 	 *
1498*7c478bd9Sstevel@tonic-gate 	 * XX64	Add some commentary to locore.s/privregs.h to document this.
1499*7c478bd9Sstevel@tonic-gate 	 */
1500*7c478bd9Sstevel@tonic-gate 	static const uint8_t movw_0_esp_gs[4] = { 0x8e, 0x6c, 0x24, 0x0 };
1501*7c478bd9Sstevel@tonic-gate 	static const uint8_t movw_4_esp_fs[4] = { 0x8e, 0x64, 0x24, 0x4 };
1502*7c478bd9Sstevel@tonic-gate 	static const uint8_t movw_8_esp_es[4] = { 0x8e, 0x44, 0x24, 0x8 };
1503*7c478bd9Sstevel@tonic-gate 	static const uint8_t movw_c_esp_ds[4] = { 0x8e, 0x5c, 0x24, 0xc };
1504*7c478bd9Sstevel@tonic-gate #endif
1505*7c478bd9Sstevel@tonic-gate 	/*
1506*7c478bd9Sstevel@tonic-gate 	 * if we're not an lwp, or the pc range is outside _sys_rtt, then
1507*7c478bd9Sstevel@tonic-gate 	 * we should immediately be die()ing horribly
1508*7c478bd9Sstevel@tonic-gate 	 */
1509*7c478bd9Sstevel@tonic-gate 	if (lwp == NULL ||
1510*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc < (uintptr_t)_sys_rtt ||
1511*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc > (uintptr_t)sr_sup)
1512*7c478bd9Sstevel@tonic-gate 		return (1);
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 	/*
1515*7c478bd9Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
1516*7c478bd9Sstevel@tonic-gate 	 *
1517*7c478bd9Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
1518*7c478bd9Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
1519*7c478bd9Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
1520*7c478bd9Sstevel@tonic-gate 	 * _sys_rtt. Ew.
1521*7c478bd9Sstevel@tonic-gate 	 */
1522*7c478bd9Sstevel@tonic-gate 
1523*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1524*7c478bd9Sstevel@tonic-gate 
1525*7c478bd9Sstevel@tonic-gate 	if (bcmp(pc, iretq_insn, sizeof (iretq_insn)) == 0) {
1526*7c478bd9Sstevel@tonic-gate 		/*
1527*7c478bd9Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iretq.
1528*7c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
1529*7c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
1530*7c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
1531*7c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
1532*7c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
1533*7c478bd9Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
1534*7c478bd9Sstevel@tonic-gate 		 *
1535*7c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
1536*7c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
1537*7c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
1538*7c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
1539*7c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
1540*7c478bd9Sstevel@tonic-gate 		 */
1541*7c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
1542*7c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
1543*7c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
1544*7c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
1545*7c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
1546*7c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
1547*7c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
1548*7c478bd9Sstevel@tonic-gate 
1549*7c478bd9Sstevel@tonic-gate 		/*
1550*7c478bd9Sstevel@tonic-gate 		 * Validate simple math
1551*7c478bd9Sstevel@tonic-gate 		 */
1552*7c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
1553*7c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
1554*7c478bd9Sstevel@tonic-gate 
1555*7c478bd9Sstevel@tonic-gate 	} else if ((lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) != 0 &&
1556*7c478bd9Sstevel@tonic-gate 	    pc >= (caddr_t)_update_sregs &&
1557*7c478bd9Sstevel@tonic-gate 	    pc < (caddr_t)_update_sregs_done) {
1558*7c478bd9Sstevel@tonic-gate 		/*
1559*7c478bd9Sstevel@tonic-gate 		 * This is the common case -- we're trying to load
1560*7c478bd9Sstevel@tonic-gate 		 * a bad segment register value in the only section
1561*7c478bd9Sstevel@tonic-gate 		 * of kernel code that ever loads segment registers.
1562*7c478bd9Sstevel@tonic-gate 		 *
1563*7c478bd9Sstevel@tonic-gate 		 * We don't need to do anything at this point because
1564*7c478bd9Sstevel@tonic-gate 		 * the pcb contains all the pending segment register
1565*7c478bd9Sstevel@tonic-gate 		 * state, and the regs are still intact because we
1566*7c478bd9Sstevel@tonic-gate 		 * didn't adjust the stack pointer yet.  Given the fidelity
1567*7c478bd9Sstevel@tonic-gate 		 * of all this, we could conceivably send a signal
1568*7c478bd9Sstevel@tonic-gate 		 * to the lwp, rather than core-ing.
1569*7c478bd9Sstevel@tonic-gate 		 */
1570*7c478bd9Sstevel@tonic-gate 		trp = lwptoregs(lwp);
1571*7c478bd9Sstevel@tonic-gate 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
1572*7c478bd9Sstevel@tonic-gate 	}
1573*7c478bd9Sstevel@tonic-gate 
1574*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1575*7c478bd9Sstevel@tonic-gate 
1576*7c478bd9Sstevel@tonic-gate 	if (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0) {
1577*7c478bd9Sstevel@tonic-gate 		/*
1578*7c478bd9Sstevel@tonic-gate 		 * We took the #gp while trying to perform the iret.
1579*7c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
1580*7c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
1581*7c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
1582*7c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
1583*7c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the registers that
1584*7c478bd9Sstevel@tonic-gate 		 * the iret was unable to consume.
1585*7c478bd9Sstevel@tonic-gate 		 *
1586*7c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
1587*7c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
1588*7c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
1589*7c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
1590*7c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
1591*7c478bd9Sstevel@tonic-gate 		 */
1592*7c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
1593*7c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
1594*7c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
1595*7c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
1596*7c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
1597*7c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
1598*7c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
1599*7c478bd9Sstevel@tonic-gate 
1600*7c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
1601*7c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
1602*7c478bd9Sstevel@tonic-gate 
1603*7c478bd9Sstevel@tonic-gate 	} else {
1604*7c478bd9Sstevel@tonic-gate 		/*
1605*7c478bd9Sstevel@tonic-gate 		 * Segment registers are reloaded in _sys_rtt
1606*7c478bd9Sstevel@tonic-gate 		 * via the following sequence:
1607*7c478bd9Sstevel@tonic-gate 		 *
1608*7c478bd9Sstevel@tonic-gate 		 *	movw	0(%esp), %gs
1609*7c478bd9Sstevel@tonic-gate 		 *	movw	4(%esp), %fs
1610*7c478bd9Sstevel@tonic-gate 		 *	movw	8(%esp), %es
1611*7c478bd9Sstevel@tonic-gate 		 *	movw	12(%esp), %ds
1612*7c478bd9Sstevel@tonic-gate 		 *	addl	$16, %esp
1613*7c478bd9Sstevel@tonic-gate 		 *
1614*7c478bd9Sstevel@tonic-gate 		 * Thus if any of them fault, we know the user
1615*7c478bd9Sstevel@tonic-gate 		 * registers are left unharmed on the stack.
1616*7c478bd9Sstevel@tonic-gate 		 */
1617*7c478bd9Sstevel@tonic-gate 		if (bcmp(pc, movw_0_esp_gs, sizeof (movw_0_esp_gs)) == 0 ||
1618*7c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_4_esp_fs, sizeof (movw_4_esp_fs)) == 0 ||
1619*7c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_8_esp_es, sizeof (movw_8_esp_es)) == 0 ||
1620*7c478bd9Sstevel@tonic-gate 		    bcmp(pc, movw_c_esp_ds, sizeof (movw_c_esp_ds)) == 0)
1621*7c478bd9Sstevel@tonic-gate 			trp = lwptoregs(lwp);
1622*7c478bd9Sstevel@tonic-gate 	}
1623*7c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
1624*7c478bd9Sstevel@tonic-gate 
1625*7c478bd9Sstevel@tonic-gate 	if (trp == NULL)
1626*7c478bd9Sstevel@tonic-gate 		return (1);
1627*7c478bd9Sstevel@tonic-gate 
1628*7c478bd9Sstevel@tonic-gate 	/*
1629*7c478bd9Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
1630*7c478bd9Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
1631*7c478bd9Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
1632*7c478bd9Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
1633*7c478bd9Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
1634*7c478bd9Sstevel@tonic-gate 	 */
1635*7c478bd9Sstevel@tonic-gate 
1636*7c478bd9Sstevel@tonic-gate 	/*
1637*7c478bd9Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
1638*7c478bd9Sstevel@tonic-gate 	 */
1639*7c478bd9Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
1640*7c478bd9Sstevel@tonic-gate 	trp->r_err = rp->r_err;
1641*7c478bd9Sstevel@tonic-gate 
1642*7c478bd9Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
1643*7c478bd9Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
1644*7c478bd9Sstevel@tonic-gate 
1645*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1646*7c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
1647*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1648*7c478bd9Sstevel@tonic-gate 
1649*7c478bd9Sstevel@tonic-gate 	/*
1650*7c478bd9Sstevel@tonic-gate 	 * Terminate all LWPs but don't discard them.  If another lwp beat us to
1651*7c478bd9Sstevel@tonic-gate 	 * the punch by calling exit(), evaporate now.
1652*7c478bd9Sstevel@tonic-gate 	 */
1653*7c478bd9Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
1654*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1655*7c478bd9Sstevel@tonic-gate 		lwp_exit();
1656*7c478bd9Sstevel@tonic-gate 	}
1657*7c478bd9Sstevel@tonic-gate 
1658*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1659*7c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
1660*7c478bd9Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
1661*7c478bd9Sstevel@tonic-gate #endif
1662*7c478bd9Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
1663*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1664*7c478bd9Sstevel@tonic-gate 	if (audit_active)		/* audit core dump */
1665*7c478bd9Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
1666*7c478bd9Sstevel@tonic-gate #endif
1667*7c478bd9Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
1668*7c478bd9Sstevel@tonic-gate 	return (0);
1669*7c478bd9Sstevel@tonic-gate }
1670*7c478bd9Sstevel@tonic-gate 
1671*7c478bd9Sstevel@tonic-gate /*
1672*7c478bd9Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
1673*7c478bd9Sstevel@tonic-gate  */
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1676*7c478bd9Sstevel@tonic-gate 
1677*7c478bd9Sstevel@tonic-gate static void
1678*7c478bd9Sstevel@tonic-gate dump_tss(void)
1679*7c478bd9Sstevel@tonic-gate {
1680*7c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
1681*7c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
1682*7c478bd9Sstevel@tonic-gate 
1683*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
1684*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
1685*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
1686*7c478bd9Sstevel@tonic-gate 
1687*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
1688*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
1689*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
1690*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
1691*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
1692*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
1693*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
1694*7c478bd9Sstevel@tonic-gate }
1695*7c478bd9Sstevel@tonic-gate 
1696*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1697*7c478bd9Sstevel@tonic-gate 
1698*7c478bd9Sstevel@tonic-gate static void
1699*7c478bd9Sstevel@tonic-gate dump_tss(void)
1700*7c478bd9Sstevel@tonic-gate {
1701*7c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
1702*7c478bd9Sstevel@tonic-gate 	struct tss *tss = CPU->cpu_tss;
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_link", (void *)tss->tss_link);
1705*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_esp0", (void *)tss->tss_esp0);
1706*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ss0", (void *)tss->tss_ss0);
1707*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_esp1", (void *)tss->tss_esp1);
1708*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ss1", (void *)tss->tss_ss1);
1709*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_esp2", (void *)tss->tss_esp2);
1710*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ss2", (void *)tss->tss_ss2);
1711*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_cr3", (void *)tss->tss_cr3);
1712*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_eip", (void *)tss->tss_eip);
1713*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_eflags", (void *)tss->tss_eflags);
1714*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_eax", (void *)tss->tss_eax);
1715*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ebx", (void *)tss->tss_ebx);
1716*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ecx", (void *)tss->tss_ecx);
1717*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_edx", (void *)tss->tss_edx);
1718*7c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_esp", (void *)tss->tss_esp);
1719*7c478bd9Sstevel@tonic-gate }
1720*7c478bd9Sstevel@tonic-gate 
1721*7c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
1724*7c478bd9Sstevel@tonic-gate 
1725*7c478bd9Sstevel@tonic-gate int ttrace_nrec = 0;		/* number of records to dump out */
1726*7c478bd9Sstevel@tonic-gate int ttrace_dump_nregs = 5;	/* dump out this many records with regs too */
1727*7c478bd9Sstevel@tonic-gate 
1728*7c478bd9Sstevel@tonic-gate /*
1729*7c478bd9Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
1730*7c478bd9Sstevel@tonic-gate  */
1731*7c478bd9Sstevel@tonic-gate static void
1732*7c478bd9Sstevel@tonic-gate dump_ttrace(void)
1733*7c478bd9Sstevel@tonic-gate {
1734*7c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
1735*7c478bd9Sstevel@tonic-gate 	trap_trace_rec_t *rec;
1736*7c478bd9Sstevel@tonic-gate 	uintptr_t current;
1737*7c478bd9Sstevel@tonic-gate 	int i, j, k;
1738*7c478bd9Sstevel@tonic-gate 	int n = NCPU;
1739*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1740*7c478bd9Sstevel@tonic-gate 	const char banner[] =
1741*7c478bd9Sstevel@tonic-gate 		"\ncpu          address    timestamp "
1742*7c478bd9Sstevel@tonic-gate 		"type  vc  handler   pc\n";
1743*7c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx ";
1744*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1745*7c478bd9Sstevel@tonic-gate 	const char banner[] =
1746*7c478bd9Sstevel@tonic-gate 		"\ncpu address     timestamp type  vc  handler   pc\n";
1747*7c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %08lx %12llx ";
1748*7c478bd9Sstevel@tonic-gate #endif
1749*7c478bd9Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x ";
1750*7c478bd9Sstevel@tonic-gate 	const char fmt3[] = "%8s ";
1751*7c478bd9Sstevel@tonic-gate 
1752*7c478bd9Sstevel@tonic-gate 	if (ttrace_nrec == 0)
1753*7c478bd9Sstevel@tonic-gate 		return;
1754*7c478bd9Sstevel@tonic-gate 
1755*7c478bd9Sstevel@tonic-gate 	printf(banner);
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
1758*7c478bd9Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
1759*7c478bd9Sstevel@tonic-gate 		if (ttc->ttc_first == NULL)
1760*7c478bd9Sstevel@tonic-gate 			continue;
1761*7c478bd9Sstevel@tonic-gate 
1762*7c478bd9Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
1763*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
1764*7c478bd9Sstevel@tonic-gate 			struct sysent	*sys;
1765*7c478bd9Sstevel@tonic-gate 			struct autovec	*vec;
1766*7c478bd9Sstevel@tonic-gate 			extern struct av_head autovect[];
1767*7c478bd9Sstevel@tonic-gate 			int type;
1768*7c478bd9Sstevel@tonic-gate 			ulong_t	off;
1769*7c478bd9Sstevel@tonic-gate 			char *sym, *stype;
1770*7c478bd9Sstevel@tonic-gate 
1771*7c478bd9Sstevel@tonic-gate 			if (current < ttc->ttc_first)
1772*7c478bd9Sstevel@tonic-gate 				current =
1773*7c478bd9Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
1774*7c478bd9Sstevel@tonic-gate 
1775*7c478bd9Sstevel@tonic-gate 			if (current == NULL)
1776*7c478bd9Sstevel@tonic-gate 				continue;
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
1781*7c478bd9Sstevel@tonic-gate 				break;
1782*7c478bd9Sstevel@tonic-gate 
1783*7c478bd9Sstevel@tonic-gate 			printf(fmt1, i, (uintptr_t)rec, rec->ttr_stamp);
1784*7c478bd9Sstevel@tonic-gate 
1785*7c478bd9Sstevel@tonic-gate 			switch (rec->ttr_marker) {
1786*7c478bd9Sstevel@tonic-gate 			case TT_SYSCALL:
1787*7c478bd9Sstevel@tonic-gate 			case TT_SYSENTER:
1788*7c478bd9Sstevel@tonic-gate 			case TT_SYSC:
1789*7c478bd9Sstevel@tonic-gate 			case TT_SYSC64:
1790*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
1791*7c478bd9Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
1792*7c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
1793*7c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
1794*7c478bd9Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
1795*7c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
1796*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
1797*7c478bd9Sstevel@tonic-gate 				sys = &sysent[rec->ttr_sysnum];
1798*7c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
1799*7c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
1800*7c478bd9Sstevel@tonic-gate #endif
1801*7c478bd9Sstevel@tonic-gate 				case TT_SYSC:
1802*7c478bd9Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
1803*7c478bd9Sstevel@tonic-gate 					break;
1804*7c478bd9Sstevel@tonic-gate 				case TT_SYSCALL:
1805*7c478bd9Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
1806*7c478bd9Sstevel@tonic-gate 					break;
1807*7c478bd9Sstevel@tonic-gate 				case TT_SYSENTER:
1808*7c478bd9Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
1809*7c478bd9Sstevel@tonic-gate 					break;
1810*7c478bd9Sstevel@tonic-gate 				default:
1811*7c478bd9Sstevel@tonic-gate 					break;
1812*7c478bd9Sstevel@tonic-gate 				}
1813*7c478bd9Sstevel@tonic-gate 				printf(fmt2, "sysc", rec->ttr_sysnum);
1814*7c478bd9Sstevel@tonic-gate 				if (sys != NULL) {
1815*7c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
1816*7c478bd9Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
1817*7c478bd9Sstevel@tonic-gate 					    &off);
1818*7c478bd9Sstevel@tonic-gate 					if (sym != NULL)
1819*7c478bd9Sstevel@tonic-gate 						printf("%s ", sym);
1820*7c478bd9Sstevel@tonic-gate 					else
1821*7c478bd9Sstevel@tonic-gate 						printf("%p ", sys->sy_callc);
1822*7c478bd9Sstevel@tonic-gate 				} else {
1823*7c478bd9Sstevel@tonic-gate 					printf("unknown ");
1824*7c478bd9Sstevel@tonic-gate 				}
1825*7c478bd9Sstevel@tonic-gate 				break;
1826*7c478bd9Sstevel@tonic-gate 
1827*7c478bd9Sstevel@tonic-gate 			case TT_INTERRUPT:
1828*7c478bd9Sstevel@tonic-gate 				printf(fmt2, "intr", rec->ttr_vector);
1829*7c478bd9Sstevel@tonic-gate 				vec = (&autovect[rec->ttr_vector])->avh_link;
1830*7c478bd9Sstevel@tonic-gate 				if (vec != NULL) {
1831*7c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
1832*7c478bd9Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
1833*7c478bd9Sstevel@tonic-gate 					if (sym != NULL)
1834*7c478bd9Sstevel@tonic-gate 						printf("%s ", sym);
1835*7c478bd9Sstevel@tonic-gate 					else
1836*7c478bd9Sstevel@tonic-gate 						printf("%p ", vec->av_vector);
1837*7c478bd9Sstevel@tonic-gate 				} else {
1838*7c478bd9Sstevel@tonic-gate 					printf("unknown ");
1839*7c478bd9Sstevel@tonic-gate 				}
1840*7c478bd9Sstevel@tonic-gate 				break;
1841*7c478bd9Sstevel@tonic-gate 
1842*7c478bd9Sstevel@tonic-gate 			case TT_TRAP:
1843*7c478bd9Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
1844*7c478bd9Sstevel@tonic-gate 				printf(fmt2, "trap", type);
1845*7c478bd9Sstevel@tonic-gate 				printf("#%s ", type < TRAP_TYPES ?
1846*7c478bd9Sstevel@tonic-gate 				    trap_type_mnemonic[type] : "trap");
1847*7c478bd9Sstevel@tonic-gate 				break;
1848*7c478bd9Sstevel@tonic-gate 
1849*7c478bd9Sstevel@tonic-gate 			default:
1850*7c478bd9Sstevel@tonic-gate 				break;
1851*7c478bd9Sstevel@tonic-gate 			}
1852*7c478bd9Sstevel@tonic-gate 
1853*7c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
1854*7c478bd9Sstevel@tonic-gate 			if (sym != NULL)
1855*7c478bd9Sstevel@tonic-gate 				printf("%s+%lx\n", sym, off);
1856*7c478bd9Sstevel@tonic-gate 			else
1857*7c478bd9Sstevel@tonic-gate 				printf("%lx\n", rec->ttr_regs.r_pc);
1858*7c478bd9Sstevel@tonic-gate 
1859*7c478bd9Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
1860*7c478bd9Sstevel@tonic-gate 				int s;
1861*7c478bd9Sstevel@tonic-gate 
1862*7c478bd9Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
1863*7c478bd9Sstevel@tonic-gate 					printf(
1864*7c478bd9Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
1865*7c478bd9Sstevel@tonic-gate 					    rec->ttr_ipl,
1866*7c478bd9Sstevel@tonic-gate 					    rec->ttr_spl,
1867*7c478bd9Sstevel@tonic-gate 					    rec->ttr_pri);
1868*7c478bd9Sstevel@tonic-gate 
1869*7c478bd9Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
1870*7c478bd9Sstevel@tonic-gate 
1871*7c478bd9Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
1872*7c478bd9Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
1873*7c478bd9Sstevel@tonic-gate 
1874*7c478bd9Sstevel@tonic-gate 				/*
1875*7c478bd9Sstevel@tonic-gate 				 * print out the pc stack that we recorded
1876*7c478bd9Sstevel@tonic-gate 				 * at trap time (if any)
1877*7c478bd9Sstevel@tonic-gate 				 */
1878*7c478bd9Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
1879*7c478bd9Sstevel@tonic-gate 					uintptr_t fullpc;
1880*7c478bd9Sstevel@tonic-gate 
1881*7c478bd9Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
1882*7c478bd9Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
1883*7c478bd9Sstevel@tonic-gate 						break;
1884*7c478bd9Sstevel@tonic-gate 					}
1885*7c478bd9Sstevel@tonic-gate 
1886*7c478bd9Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
1887*7c478bd9Sstevel@tonic-gate 
1888*7c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
1889*7c478bd9Sstevel@tonic-gate 					if (sym != NULL)
1890*7c478bd9Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
1891*7c478bd9Sstevel@tonic-gate 						    sym, off);
1892*7c478bd9Sstevel@tonic-gate 					else
1893*7c478bd9Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
1894*7c478bd9Sstevel@tonic-gate 				}
1895*7c478bd9Sstevel@tonic-gate 				printf("\n");
1896*7c478bd9Sstevel@tonic-gate 			}
1897*7c478bd9Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
1898*7c478bd9Sstevel@tonic-gate 		}
1899*7c478bd9Sstevel@tonic-gate 	}
1900*7c478bd9Sstevel@tonic-gate }
1901*7c478bd9Sstevel@tonic-gate 
1902*7c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1903*7c478bd9Sstevel@tonic-gate 
1904*7c478bd9Sstevel@tonic-gate void
1905*7c478bd9Sstevel@tonic-gate panic_showtrap(struct trap_info *tip)
1906*7c478bd9Sstevel@tonic-gate {
1907*7c478bd9Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
1908*7c478bd9Sstevel@tonic-gate 
1909*7c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
1910*7c478bd9Sstevel@tonic-gate 	dump_ttrace();
1911*7c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1912*7c478bd9Sstevel@tonic-gate 
1913*7c478bd9Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
1914*7c478bd9Sstevel@tonic-gate 		dump_tss();
1915*7c478bd9Sstevel@tonic-gate }
1916*7c478bd9Sstevel@tonic-gate 
1917*7c478bd9Sstevel@tonic-gate void
1918*7c478bd9Sstevel@tonic-gate panic_savetrap(panic_data_t *pdp, struct trap_info *tip)
1919*7c478bd9Sstevel@tonic-gate {
1920*7c478bd9Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
1921*7c478bd9Sstevel@tonic-gate }
1922