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