xref: /illumos-gate/usr/src/uts/i86pc/os/trap.c (revision 2570281cf351044b6936651ce26dbe1f801dcbd8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d8aa0f5aSsudheer  * Common Development and Distribution License (the "License").
6d8aa0f5aSsudheer  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
235cd376e8SJimmy Vetayases  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
287c478bd9Sstevel@tonic-gate /*		All Rights Reserved				*/
297c478bd9Sstevel@tonic-gate /*								*/
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation		*/
317c478bd9Sstevel@tonic-gate /*		All Rights Reserved				*/
327c478bd9Sstevel@tonic-gate /*								*/
337c478bd9Sstevel@tonic-gate 
34f16a0f4cSRobert Mustacchi /*
3574ecdb51SJohn Levon  * Copyright 2018 Joyent, Inc.
36f16a0f4cSRobert Mustacchi  */
37f16a0f4cSRobert Mustacchi 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/disp.h>
467c478bd9Sstevel@tonic-gate #include <sys/class.h>
477c478bd9Sstevel@tonic-gate #include <sys/core.h>
487c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
497c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
507c478bd9Sstevel@tonic-gate #include <sys/vm.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
527c478bd9Sstevel@tonic-gate #include <sys/fault.h>
537c478bd9Sstevel@tonic-gate #include <sys/stack.h>
547c478bd9Sstevel@tonic-gate #include <sys/psw.h>
557c478bd9Sstevel@tonic-gate #include <sys/regset.h>
567c478bd9Sstevel@tonic-gate #include <sys/fp.h>
577c478bd9Sstevel@tonic-gate #include <sys/trap.h>
587c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
627c478bd9Sstevel@tonic-gate #include <sys/mutex_impl.h>
637c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
647c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
657c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
667c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
677c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <vm/hat.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
727c478bd9Sstevel@tonic-gate #include <vm/as.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg.h>
747c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
75ae115bc7Smrj #include <vm/hat_i86.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
807c478bd9Sstevel@tonic-gate #include <sys/debug.h>
817c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
827c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
837c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
847c478bd9Sstevel@tonic-gate #include <sys/cred.h>
857c478bd9Sstevel@tonic-gate #include <sys/mman.h>
867c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
877c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
887c478bd9Sstevel@tonic-gate #include <c2/audit.h>
897c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
907c478bd9Sstevel@tonic-gate #include <sys/panic.h>
917c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
927c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
937c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
94ae115bc7Smrj #include <sys/bootconf.h>
95ae115bc7Smrj #include <sys/bootinfo.h>
96ae115bc7Smrj #include <sys/promif.h>
97ae115bc7Smrj #include <sys/mach_mmu.h>
98843e1988Sjohnlev #if defined(__xpv)
99843e1988Sjohnlev #include <sys/hypervisor.h>
100843e1988Sjohnlev #endif
10120c794b3Sgavinm #include <sys/contract/process_impl.h>
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	USER	0x10000		/* user-mode flag added to trap type */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static const char *trap_type_mnemonic[] = {
1067c478bd9Sstevel@tonic-gate 	"de",	"db",	"2",	"bp",
1077c478bd9Sstevel@tonic-gate 	"of",	"br",	"ud",	"nm",
1087c478bd9Sstevel@tonic-gate 	"df",	"9",	"ts",	"np",
1097c478bd9Sstevel@tonic-gate 	"ss",	"gp",	"pf",	"15",
1107c478bd9Sstevel@tonic-gate 	"mf",	"ac",	"mc",	"xf"
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static const char *trap_type[] = {
1147c478bd9Sstevel@tonic-gate 	"Divide error",				/* trap id 0	*/
1157c478bd9Sstevel@tonic-gate 	"Debug",				/* trap id 1	*/
1167c478bd9Sstevel@tonic-gate 	"NMI interrupt",			/* trap id 2	*/
1177c478bd9Sstevel@tonic-gate 	"Breakpoint",				/* trap id 3	*/
1187c478bd9Sstevel@tonic-gate 	"Overflow",				/* trap id 4	*/
1197c478bd9Sstevel@tonic-gate 	"BOUND range exceeded",			/* trap id 5	*/
1207c478bd9Sstevel@tonic-gate 	"Invalid opcode",			/* trap id 6	*/
1217c478bd9Sstevel@tonic-gate 	"Device not available",			/* trap id 7	*/
1227c478bd9Sstevel@tonic-gate 	"Double fault",				/* trap id 8	*/
1237c478bd9Sstevel@tonic-gate 	"Coprocessor segment overrun",		/* trap id 9	*/
1247c478bd9Sstevel@tonic-gate 	"Invalid TSS",				/* trap id 10	*/
1257c478bd9Sstevel@tonic-gate 	"Segment not present",			/* trap id 11	*/
1267c478bd9Sstevel@tonic-gate 	"Stack segment fault",			/* trap id 12	*/
1277c478bd9Sstevel@tonic-gate 	"General protection",			/* trap id 13	*/
1287c478bd9Sstevel@tonic-gate 	"Page fault",				/* trap id 14	*/
1297c478bd9Sstevel@tonic-gate 	"Reserved",				/* trap id 15	*/
1307c478bd9Sstevel@tonic-gate 	"x87 floating point error",		/* trap id 16	*/
1317c478bd9Sstevel@tonic-gate 	"Alignment check",			/* trap id 17	*/
1327c478bd9Sstevel@tonic-gate 	"Machine check",			/* trap id 18	*/
1337c478bd9Sstevel@tonic-gate 	"SIMD floating point exception",	/* trap id 19	*/
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	TRAP_TYPES	(sizeof (trap_type) / sizeof (trap_type[0]))
1377c478bd9Sstevel@tonic-gate 
138ddece0baSsethg #define	SLOW_SCALL_SIZE	2
139ddece0baSsethg #define	FAST_SCALL_SIZE	2
140ddece0baSsethg 
1417c478bd9Sstevel@tonic-gate int tudebug = 0;
1427c478bd9Sstevel@tonic-gate int tudebugbpt = 0;
1437c478bd9Sstevel@tonic-gate int tudebugfpe = 0;
1447c478bd9Sstevel@tonic-gate int tudebugsse = 0;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #if defined(TRAPDEBUG) || defined(lint)
1477c478bd9Sstevel@tonic-gate int tdebug = 0;
1487c478bd9Sstevel@tonic-gate int lodebug = 0;
1497c478bd9Sstevel@tonic-gate int faultdebug = 0;
1507c478bd9Sstevel@tonic-gate #else
1517c478bd9Sstevel@tonic-gate #define	tdebug	0
1527c478bd9Sstevel@tonic-gate #define	lodebug	0
1537c478bd9Sstevel@tonic-gate #define	faultdebug	0
1547c478bd9Sstevel@tonic-gate #endif /* defined(TRAPDEBUG) || defined(lint) */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
157a563a037Sbholler /*
158a563a037Sbholler  * trap trace record for cpu0 is allocated here.
159a563a037Sbholler  * trap trace records for non-boot cpus are allocated in mp_startup_init().
160a563a037Sbholler  */
161a563a037Sbholler static trap_trace_rec_t trap_tr0[TRAPTR_NENT];
162a563a037Sbholler trap_trace_ctl_t trap_trace_ctl[NCPU] = {
163a563a037Sbholler 	{
164a563a037Sbholler 	    (uintptr_t)trap_tr0,			/* next record */
165a563a037Sbholler 	    (uintptr_t)trap_tr0,			/* first record */
166a563a037Sbholler 	    (uintptr_t)(trap_tr0 + TRAPTR_NENT),	/* limit */
167a563a037Sbholler 	    (uintptr_t)0				/* current */
168a563a037Sbholler 	},
169a563a037Sbholler };
170a563a037Sbholler 
171a563a037Sbholler /*
172a563a037Sbholler  * default trap buffer size
173a563a037Sbholler  */
174a563a037Sbholler size_t trap_trace_bufsize = TRAPTR_NENT * sizeof (trap_trace_rec_t);
175a563a037Sbholler int trap_trace_freeze = 0;
176a563a037Sbholler int trap_trace_off = 0;
177a563a037Sbholler 
178a563a037Sbholler /*
179a563a037Sbholler  * A dummy TRAPTRACE entry to use after death.
180a563a037Sbholler  */
181a563a037Sbholler trap_trace_rec_t trap_trace_postmort;
182a563a037Sbholler 
1837c478bd9Sstevel@tonic-gate static void dump_ttrace(void);
1847c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1857c478bd9Sstevel@tonic-gate static void dumpregs(struct regs *);
1867c478bd9Sstevel@tonic-gate static void showregs(uint_t, struct regs *, caddr_t);
1877c478bd9Sstevel@tonic-gate static int kern_gpfault(struct regs *);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1907c478bd9Sstevel@tonic-gate static int
die(uint_t type,struct regs * rp,caddr_t addr,processorid_t cpuid)1917c478bd9Sstevel@tonic-gate die(uint_t type, struct regs *rp, caddr_t addr, processorid_t cpuid)
1927c478bd9Sstevel@tonic-gate {
193843e1988Sjohnlev 	struct panic_trap_info ti;
1947c478bd9Sstevel@tonic-gate 	const char *trap_name, *trap_mnemonic;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES) {
1977c478bd9Sstevel@tonic-gate 		trap_name = trap_type[type];
1987c478bd9Sstevel@tonic-gate 		trap_mnemonic = trap_type_mnemonic[type];
1997c478bd9Sstevel@tonic-gate 	} else {
2007c478bd9Sstevel@tonic-gate 		trap_name = "trap";
2017c478bd9Sstevel@tonic-gate 		trap_mnemonic = "-";
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
2057c478bd9Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	ti.trap_regs = rp;
2097c478bd9Sstevel@tonic-gate 	ti.trap_type = type & ~USER;
2107c478bd9Sstevel@tonic-gate 	ti.trap_addr = addr;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	curthread->t_panic_trap = &ti;
2137c478bd9Sstevel@tonic-gate 
214086dcf7eSPatrick Mooney 	if (type == T_PGFLT && addr < (caddr_t)kernelbase) {
2157c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p "
2167c478bd9Sstevel@tonic-gate 		    "occurred in module \"%s\" due to %s",
2177c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr,
2187c478bd9Sstevel@tonic-gate 		    mod_containing_pc((caddr_t)rp->r_pc),
2197c478bd9Sstevel@tonic-gate 		    addr < (caddr_t)PAGESIZE ?
2207c478bd9Sstevel@tonic-gate 		    "a NULL pointer dereference" :
2217c478bd9Sstevel@tonic-gate 		    "an illegal access to a user address");
2227c478bd9Sstevel@tonic-gate 	} else
2237c478bd9Sstevel@tonic-gate 		panic("BAD TRAP: type=%x (#%s %s) rp=%p addr=%p",
2247c478bd9Sstevel@tonic-gate 		    type, trap_mnemonic, trap_name, (void *)rp, (void *)addr);
2257c478bd9Sstevel@tonic-gate 	return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Rewrite the instruction at pc to be an int $T_SYSCALLINT instruction.
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * int <vector> is two bytes: 0xCD <vector>
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static int
rewrite_syscall(caddr_t pc)2357c478bd9Sstevel@tonic-gate rewrite_syscall(caddr_t pc)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	uchar_t instr[SLOW_SCALL_SIZE] = { 0xCD, T_SYSCALLINT };
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (uwrite(curthread->t_procp, instr, SLOW_SCALL_SIZE,
2407c478bd9Sstevel@tonic-gate 	    (uintptr_t)pc) != 0)
2417c478bd9Sstevel@tonic-gate 		return (1);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is sysenter or syscall. The second
2487c478bd9Sstevel@tonic-gate  * argument should be the x86 feature flag corresponding to the expected
2497c478bd9Sstevel@tonic-gate  * instruction.
2507c478bd9Sstevel@tonic-gate  *
2517c478bd9Sstevel@tonic-gate  * sysenter is two bytes: 0x0F 0x34
2527c478bd9Sstevel@tonic-gate  * syscall is two bytes:  0x0F 0x05
253ddece0baSsethg  * int $T_SYSCALLINT is two bytes: 0xCD 0x91
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static int
instr_is_other_syscall(caddr_t pc,int which)257ddece0baSsethg instr_is_other_syscall(caddr_t pc, int which)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	uchar_t instr[FAST_SCALL_SIZE];
2607c478bd9Sstevel@tonic-gate 
2617417cfdeSKuriakose Kuruvilla 	ASSERT(which == X86FSET_SEP || which == X86FSET_ASYSC || which == 0xCD);
2627c478bd9Sstevel@tonic-gate 
263ddece0baSsethg 	if (copyin_nowatch(pc, (caddr_t)instr, FAST_SCALL_SIZE) != 0)
2647c478bd9Sstevel@tonic-gate 		return (0);
2657c478bd9Sstevel@tonic-gate 
266ddece0baSsethg 	switch (which) {
2677417cfdeSKuriakose Kuruvilla 	case X86FSET_SEP:
268ddece0baSsethg 		if (instr[0] == 0x0F && instr[1] == 0x34)
2697c478bd9Sstevel@tonic-gate 			return (1);
270ddece0baSsethg 		break;
2717417cfdeSKuriakose Kuruvilla 	case X86FSET_ASYSC:
272ddece0baSsethg 		if (instr[0] == 0x0F && instr[1] == 0x05)
273ddece0baSsethg 			return (1);
274ddece0baSsethg 		break;
275ddece0baSsethg 	case 0xCD:
276ddece0baSsethg 		if (instr[0] == 0xCD && instr[1] == T_SYSCALLINT)
277ddece0baSsethg 			return (1);
278ddece0baSsethg 		break;
279ddece0baSsethg 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (0);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
284*ae5a8bedSAndy Fiddaman #ifdef DEBUG
285ddece0baSsethg static const char *
syscall_insn_string(int syscall_insn)286ddece0baSsethg syscall_insn_string(int syscall_insn)
287ddece0baSsethg {
288ddece0baSsethg 	switch (syscall_insn) {
2897417cfdeSKuriakose Kuruvilla 	case X86FSET_SEP:
290ddece0baSsethg 		return ("sysenter");
2917417cfdeSKuriakose Kuruvilla 	case X86FSET_ASYSC:
292ddece0baSsethg 		return ("syscall");
293ddece0baSsethg 	case 0xCD:
294ddece0baSsethg 		return ("int");
295ddece0baSsethg 	default:
296ddece0baSsethg 		return ("Unknown");
297ddece0baSsethg 	}
298ddece0baSsethg }
299*ae5a8bedSAndy Fiddaman #endif /* DEBUG */
300ddece0baSsethg 
301ddece0baSsethg static int
ldt_rewrite_syscall(struct regs * rp,proc_t * p,int syscall_insn)302ddece0baSsethg ldt_rewrite_syscall(struct regs *rp, proc_t *p, int syscall_insn)
303ddece0baSsethg {
304ddece0baSsethg 	caddr_t	linearpc;
305ddece0baSsethg 	int return_code = 0;
306ddece0baSsethg 
307ddece0baSsethg 	mutex_enter(&p->p_ldtlock);	/* Must be held across linear_pc() */
308ddece0baSsethg 
309ddece0baSsethg 	if (linear_pc(rp, p, &linearpc) == 0) {
310ddece0baSsethg 
311ddece0baSsethg 		/*
312ddece0baSsethg 		 * If another thread beat us here, it already changed
313ddece0baSsethg 		 * this site to the slower (int) syscall instruction.
314ddece0baSsethg 		 */
315ddece0baSsethg 		if (instr_is_other_syscall(linearpc, 0xCD)) {
316ddece0baSsethg 			return_code = 1;
317ddece0baSsethg 		} else if (instr_is_other_syscall(linearpc, syscall_insn)) {
318ddece0baSsethg 
319ddece0baSsethg 			if (rewrite_syscall(linearpc) == 0) {
320ddece0baSsethg 				return_code = 1;
321ddece0baSsethg 			}
322ddece0baSsethg #ifdef DEBUG
323ddece0baSsethg 			else
324ddece0baSsethg 				cmn_err(CE_WARN, "failed to rewrite %s "
325ddece0baSsethg 				    "instruction in process %d",
326ddece0baSsethg 				    syscall_insn_string(syscall_insn),
327ddece0baSsethg 				    p->p_pid);
328ddece0baSsethg #endif /* DEBUG */
329ddece0baSsethg 		}
330ddece0baSsethg 	}
331ddece0baSsethg 
332ddece0baSsethg 	mutex_exit(&p->p_ldtlock);	/* Must be held across linear_pc() */
333ddece0baSsethg 
334ddece0baSsethg 	return (return_code);
335ddece0baSsethg }
336ddece0baSsethg 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a system call instruction.
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * The bytes of an lcall instruction used for the syscall trap.
3417c478bd9Sstevel@tonic-gate  * static uchar_t lcall[7] = { 0x9a, 0, 0, 0, 0, 0x7, 0 };
3427c478bd9Sstevel@tonic-gate  * static uchar_t lcallalt[7] = { 0x9a, 0, 0, 0, 0, 0x27, 0 };
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #define	LCALLSIZE	7
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static int
instr_is_lcall_syscall(caddr_t pc)348ddece0baSsethg instr_is_lcall_syscall(caddr_t pc)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	uchar_t instr[LCALLSIZE];
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (copyin_nowatch(pc, (caddr_t)instr, LCALLSIZE) == 0 &&
3537c478bd9Sstevel@tonic-gate 	    instr[0] == 0x9a &&
3547c478bd9Sstevel@tonic-gate 	    instr[1] == 0 &&
3557c478bd9Sstevel@tonic-gate 	    instr[2] == 0 &&
3567c478bd9Sstevel@tonic-gate 	    instr[3] == 0 &&
3577c478bd9Sstevel@tonic-gate 	    instr[4] == 0 &&
3587c478bd9Sstevel@tonic-gate 	    (instr[5] == 0x7 || instr[5] == 0x27) &&
3597c478bd9Sstevel@tonic-gate 	    instr[6] == 0)
3607c478bd9Sstevel@tonic-gate 		return (1);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
365130a12b8Sfvdl /*
366ae115bc7Smrj  * In the first revisions of amd64 CPUs produced by AMD, the LAHF and
367ae115bc7Smrj  * SAHF instructions were not implemented in 64-bit mode. Later revisions
368130a12b8Sfvdl  * did implement these instructions. An extension to the cpuid instruction
369130a12b8Sfvdl  * was added to check for the capability of executing these instructions
370ae115bc7Smrj  * in 64-bit mode.
371130a12b8Sfvdl  *
372130a12b8Sfvdl  * Intel originally did not implement these instructions in EM64T either,
373130a12b8Sfvdl  * but added them in later revisions.
374130a12b8Sfvdl  *
375130a12b8Sfvdl  * So, there are different chip revisions by both vendors out there that
376130a12b8Sfvdl  * may or may not implement these instructions. The easy solution is to
377130a12b8Sfvdl  * just always emulate these instructions on demand.
378130a12b8Sfvdl  *
379130a12b8Sfvdl  * SAHF == store %ah in the lower 8 bits of %rflags (opcode 0x9e)
380130a12b8Sfvdl  * LAHF == load the lower 8 bits of %rflags into %ah (opcode 0x9f)
381130a12b8Sfvdl  */
382130a12b8Sfvdl 
383130a12b8Sfvdl #define	LSAHFSIZE 1
384130a12b8Sfvdl 
385130a12b8Sfvdl static int
instr_is_lsahf(caddr_t pc,uchar_t * instr)386130a12b8Sfvdl instr_is_lsahf(caddr_t pc, uchar_t *instr)
387130a12b8Sfvdl {
388130a12b8Sfvdl 	if (copyin_nowatch(pc, (caddr_t)instr, LSAHFSIZE) == 0 &&
389130a12b8Sfvdl 	    (*instr == 0x9e || *instr == 0x9f))
390130a12b8Sfvdl 		return (1);
391130a12b8Sfvdl 	return (0);
392130a12b8Sfvdl }
393130a12b8Sfvdl 
394130a12b8Sfvdl /*
395130a12b8Sfvdl  * Emulate the LAHF and SAHF instructions. The reference manuals define
396130a12b8Sfvdl  * these instructions to always load/store bit 1 as a 1, and bits 3 and 5
397130a12b8Sfvdl  * as a 0. The other, defined, bits are copied (the PS_ICC bits and PS_P).
398130a12b8Sfvdl  *
399130a12b8Sfvdl  * Note that %ah is bits 8-15 of %rax.
400130a12b8Sfvdl  */
401130a12b8Sfvdl static void
emulate_lsahf(struct regs * rp,uchar_t instr)402130a12b8Sfvdl emulate_lsahf(struct regs *rp, uchar_t instr)
403130a12b8Sfvdl {
404130a12b8Sfvdl 	if (instr == 0x9e) {
405130a12b8Sfvdl 		/* sahf. Copy bits from %ah to flags. */
406130a12b8Sfvdl 		rp->r_ps = (rp->r_ps & ~0xff) |
407130a12b8Sfvdl 		    ((rp->r_rax >> 8) & PSL_LSAHFMASK) | PS_MB1;
408130a12b8Sfvdl 	} else {
409130a12b8Sfvdl 		/* lahf. Copy bits from flags to %ah. */
410130a12b8Sfvdl 		rp->r_rax = (rp->r_rax & ~0xff00) |
411130a12b8Sfvdl 		    (((rp->r_ps & PSL_LSAHFMASK) | PS_MB1) << 8);
412130a12b8Sfvdl 	}
413130a12b8Sfvdl 	rp->r_pc += LSAHFSIZE;
414130a12b8Sfvdl }
415130a12b8Sfvdl 
4167c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Test to see if the instruction at pc is a prefetch instruction.
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * The first byte of prefetch instructions is always 0x0F.
4227c478bd9Sstevel@tonic-gate  * The second byte is 0x18 for regular prefetch or 0x0D for AMD 3dnow prefetch.
423d32b5733SGeorge Wilson  * The third byte (ModRM) contains the register field bits (bits 3-5).
424d32b5733SGeorge Wilson  * These bits must be between 0 and 3 inclusive for regular prefetch and
425d32b5733SGeorge Wilson  * 0 and 1 inclusive for AMD 3dnow prefetch.
4268ad4d6ddSJeff Bonwick  *
4278ad4d6ddSJeff Bonwick  * In 64-bit mode, there may be a one-byte REX prefex (0x40-0x4F).
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int
cmp_to_prefetch(uchar_t * p)4317c478bd9Sstevel@tonic-gate cmp_to_prefetch(uchar_t *p)
4327c478bd9Sstevel@tonic-gate {
4338ad4d6ddSJeff Bonwick #ifdef _LP64
4348ad4d6ddSJeff Bonwick 	if ((p[0] & 0xF0) == 0x40)	/* 64-bit REX prefix */
4358ad4d6ddSJeff Bonwick 		p++;
4368ad4d6ddSJeff Bonwick #endif
437d32b5733SGeorge Wilson 	return ((p[0] == 0x0F && p[1] == 0x18 && ((p[2] >> 3) & 7) <= 3) ||
438d32b5733SGeorge Wilson 	    (p[0] == 0x0F && p[1] == 0x0D && ((p[2] >> 3) & 7) <= 1));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
instr_is_prefetch(caddr_t pc)4427c478bd9Sstevel@tonic-gate instr_is_prefetch(caddr_t pc)
4437c478bd9Sstevel@tonic-gate {
4448ad4d6ddSJeff Bonwick 	uchar_t instr[4];	/* optional REX prefix plus 3-byte opcode */
4457c478bd9Sstevel@tonic-gate 
4468ad4d6ddSJeff Bonwick 	return (copyin_nowatch(pc, instr, sizeof (instr)) == 0 &&
4478ad4d6ddSJeff Bonwick 	    cmp_to_prefetch(instr));
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Called from the trap handler when a processor trap occurs.
4547c478bd9Sstevel@tonic-gate  *
4557c478bd9Sstevel@tonic-gate  * Note: All user-level traps that might call stop() must exit
4567c478bd9Sstevel@tonic-gate  * trap() by 'goto out' or by falling through.
457ae115bc7Smrj  * Note Also: trap() is usually called with interrupts enabled, (PS_IE == 1)
458ae115bc7Smrj  * however, there are paths that arrive here with PS_IE == 0 so special care
459ae115bc7Smrj  * must be taken in those cases.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate void
trap(struct regs * rp,caddr_t addr,processorid_t cpuid)4627c478bd9Sstevel@tonic-gate trap(struct regs *rp, caddr_t addr, processorid_t cpuid)
4637c478bd9Sstevel@tonic-gate {
464ae115bc7Smrj 	kthread_t *ct = curthread;
4657c478bd9Sstevel@tonic-gate 	enum seg_rw rw;
4667c478bd9Sstevel@tonic-gate 	unsigned type;
467ae115bc7Smrj 	proc_t *p = ttoproc(ct);
468ae115bc7Smrj 	klwp_t *lwp = ttolwp(ct);
4697c478bd9Sstevel@tonic-gate 	uintptr_t lofault;
47069c92249SBryan Cantrill 	label_t *onfault;
4717c478bd9Sstevel@tonic-gate 	faultcode_t pagefault(), res, errcode;
4727c478bd9Sstevel@tonic-gate 	enum fault_type fault_type;
4737c478bd9Sstevel@tonic-gate 	k_siginfo_t siginfo;
4747c478bd9Sstevel@tonic-gate 	uint_t fault = 0;
4757c478bd9Sstevel@tonic-gate 	int mstate;
4767c478bd9Sstevel@tonic-gate 	int sicode = 0;
4777c478bd9Sstevel@tonic-gate 	int watchcode;
4787c478bd9Sstevel@tonic-gate 	int watchpage;
4797c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
4807c478bd9Sstevel@tonic-gate 	size_t sz;
4817c478bd9Sstevel@tonic-gate 	int ta;
482130a12b8Sfvdl 	uchar_t instr;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
4857c478bd9Sstevel@tonic-gate 
486584b574aSToomas Soome 	errcode = 0;
487584b574aSToomas Soome 	mstate = 0;
488584b574aSToomas Soome 	rw = S_OTHER;
4897c478bd9Sstevel@tonic-gate 	type = rp->r_trapno;
4907c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, trap, 1);
491ae115bc7Smrj 	ASSERT(ct->t_schedflag & TS_DONT_SWAP);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
4947c478bd9Sstevel@tonic-gate 		errcode = rp->r_err;
49586ef0a63SRichard Lowe 		if (errcode & PF_ERR_WRITE) {
4967c478bd9Sstevel@tonic-gate 			rw = S_WRITE;
49786ef0a63SRichard Lowe 		} else if ((caddr_t)rp->r_pc == addr ||
49886ef0a63SRichard Lowe 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
4997c478bd9Sstevel@tonic-gate 			rw = S_EXEC;
5007c478bd9Sstevel@tonic-gate 		} else {
50186ef0a63SRichard Lowe 			rw = S_READ;
5027c478bd9Sstevel@tonic-gate 		}
50386ef0a63SRichard Lowe 	} else if (type == T_SGLSTP && lwp != NULL) {
504ae115bc7Smrj 		lwp->lwp_pcb.pcb_drstat = (uintptr_t)addr;
50586ef0a63SRichard Lowe 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (tdebug)
5087c478bd9Sstevel@tonic-gate 		showregs(type, rp, addr);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (USERMODE(rp->r_cs)) {
5117c478bd9Sstevel@tonic-gate 		/*
5127c478bd9Sstevel@tonic-gate 		 * Set up the current cred to use during this trap. u_cred
5137c478bd9Sstevel@tonic-gate 		 * no longer exists.  t_cred is used instead.
5147c478bd9Sstevel@tonic-gate 		 * The current process credential applies to the thread for
5157c478bd9Sstevel@tonic-gate 		 * the entire trap.  If trapping from the kernel, this
5167c478bd9Sstevel@tonic-gate 		 * should already be set up.
5177c478bd9Sstevel@tonic-gate 		 */
518ae115bc7Smrj 		if (ct->t_cred != p->p_cred) {
519ae115bc7Smrj 			cred_t *oldcred = ct->t_cred;
5207c478bd9Sstevel@tonic-gate 			/*
5217c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
5227c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
5237c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
5247c478bd9Sstevel@tonic-gate 			 */
525ae115bc7Smrj 			ct->t_cred = crgetcred();
5267c478bd9Sstevel@tonic-gate 			crfree(oldcred);
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		ASSERT(lwp != NULL);
5297c478bd9Sstevel@tonic-gate 		type |= USER;
5307c478bd9Sstevel@tonic-gate 		ASSERT(lwptoregs(lwp) == rp);
5317c478bd9Sstevel@tonic-gate 		lwp->lwp_state = LWP_SYS;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		switch (type) {
5347c478bd9Sstevel@tonic-gate 		case T_PGFLT + USER:
5357c478bd9Sstevel@tonic-gate 			if ((caddr_t)rp->r_pc == addr)
5367c478bd9Sstevel@tonic-gate 				mstate = LMS_TFAULT;
5377c478bd9Sstevel@tonic-gate 			else
5387c478bd9Sstevel@tonic-gate 				mstate = LMS_DFAULT;
5397c478bd9Sstevel@tonic-gate 			break;
5407c478bd9Sstevel@tonic-gate 		default:
5417c478bd9Sstevel@tonic-gate 			mstate = LMS_TRAP;
5427c478bd9Sstevel@tonic-gate 			break;
5437c478bd9Sstevel@tonic-gate 		}
544ae115bc7Smrj 		mstate = new_mstate(ct, mstate);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	switch (type) {
5507c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:
5517c478bd9Sstevel@tonic-gate 	case T_SGLSTP:
5527c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER:
5537c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	default:
5577c478bd9Sstevel@tonic-gate 		FTRACE_2("trap(): type=0x%lx, regs=0x%lx",
5587c478bd9Sstevel@tonic-gate 		    (ulong_t)type, (ulong_t)rp);
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	switch (type) {
563eae0da43Ssethg 	case T_SIMDFPE:
564eae0da43Ssethg 		/* Make sure we enable interrupts before die()ing */
565eae0da43Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
566eae0da43Ssethg 		/*FALLTHROUGH*/
5677c478bd9Sstevel@tonic-gate 	default:
5687c478bd9Sstevel@tonic-gate 		if (type & USER) {
5697c478bd9Sstevel@tonic-gate 			if (tudebug)
5707c478bd9Sstevel@tonic-gate 				showregs(type, rp, (caddr_t)0);
5717c478bd9Sstevel@tonic-gate 			printf("trap: Unknown trap type %d in user mode\n",
5727c478bd9Sstevel@tonic-gate 			    type & ~USER);
5737c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
5747c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
5757c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
5767c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
5777c478bd9Sstevel@tonic-gate 			fault = FLTILL;
5787c478bd9Sstevel@tonic-gate 		} else {
5797c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
5807c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5817c478bd9Sstevel@tonic-gate 		}
582df23f1c1SToomas Soome 		break;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	case T_PGFLT:		/* system page fault */
5857c478bd9Sstevel@tonic-gate 		/*
5867c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
58720c794b3Sgavinm 		 * set ot_trap and bounce back to the on_trap() call site
58820c794b3Sgavinm 		 * via the installed trampoline.
5897c478bd9Sstevel@tonic-gate 		 */
590ae115bc7Smrj 		if ((ct->t_ontrap != NULL) &&
591ae115bc7Smrj 		    (ct->t_ontrap->ot_prot & OT_DATA_ACCESS)) {
592ae115bc7Smrj 			ct->t_ontrap->ot_trap |= OT_DATA_ACCESS;
59320c794b3Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
59420c794b3Sgavinm 			goto cleanup;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		/*
598799823bbSRobert Mustacchi 		 * If we have an Instruction fault in kernel mode, then that
599799823bbSRobert Mustacchi 		 * means we've tried to execute a user page (SMEP) or both of
600799823bbSRobert Mustacchi 		 * PAE and NXE are enabled. In either case, given that it's a
601799823bbSRobert Mustacchi 		 * kernel fault, we should panic immediately and not try to make
602799823bbSRobert Mustacchi 		 * any more forward progress. This indicates a bug in the
603799823bbSRobert Mustacchi 		 * kernel, which if execution continued, could be exploited to
604799823bbSRobert Mustacchi 		 * wreak havoc on the system.
605799823bbSRobert Mustacchi 		 */
606799823bbSRobert Mustacchi 		if (errcode & PF_ERR_EXEC) {
607799823bbSRobert Mustacchi 			(void) die(type, rp, addr, cpuid);
608799823bbSRobert Mustacchi 		}
609799823bbSRobert Mustacchi 
610799823bbSRobert Mustacchi 		/*
6113ce2fcdcSRobert Mustacchi 		 * We need to check if SMAP is in play. If SMAP is in play, then
6123ce2fcdcSRobert Mustacchi 		 * any access to a user page will show up as a protection
6133ce2fcdcSRobert Mustacchi 		 * violation. To see if SMAP is enabled we first check if it's a
6143ce2fcdcSRobert Mustacchi 		 * user address and whether we have the feature flag set. If we
6153ce2fcdcSRobert Mustacchi 		 * do and the interrupted registers do not allow for user
6163ce2fcdcSRobert Mustacchi 		 * accesses (PS_ACHK is not enabled), then we need to die
6173ce2fcdcSRobert Mustacchi 		 * immediately.
6183ce2fcdcSRobert Mustacchi 		 */
6193ce2fcdcSRobert Mustacchi 		if (addr < (caddr_t)kernelbase &&
6203ce2fcdcSRobert Mustacchi 		    is_x86_feature(x86_featureset, X86FSET_SMAP) == B_TRUE &&
6213ce2fcdcSRobert Mustacchi 		    (rp->r_ps & PS_ACHK) == 0) {
6223ce2fcdcSRobert Mustacchi 			(void) die(type, rp, addr, cpuid);
6233ce2fcdcSRobert Mustacchi 		}
6243ce2fcdcSRobert Mustacchi 
6253ce2fcdcSRobert Mustacchi 		/*
62669c92249SBryan Cantrill 		 * See if we can handle as pagefault. Save lofault and onfault
62769c92249SBryan Cantrill 		 * across this. Here we assume that an address less than
62869c92249SBryan Cantrill 		 * KERNELBASE is a user fault.  We can do this as copy.s
62969c92249SBryan Cantrill 		 * routines verify that the starting address is less than
63069c92249SBryan Cantrill 		 * KERNELBASE before starting and because we know that we
63169c92249SBryan Cantrill 		 * always have KERNELBASE mapped as invalid to serve as a
63269c92249SBryan Cantrill 		 * "barrier".
6337c478bd9Sstevel@tonic-gate 		 */
634ae115bc7Smrj 		lofault = ct->t_lofault;
63569c92249SBryan Cantrill 		onfault = ct->t_onfault;
636ae115bc7Smrj 		ct->t_lofault = 0;
6377c478bd9Sstevel@tonic-gate 
638ae115bc7Smrj 		mstate = new_mstate(ct, LMS_KFAULT);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		if (addr < (caddr_t)kernelbase) {
6417c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
6427c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 0);
6437c478bd9Sstevel@tonic-gate 			if (res == FC_NOMAP &&
6447c478bd9Sstevel@tonic-gate 			    addr < p->p_usrstack &&
6457c478bd9Sstevel@tonic-gate 			    grow(addr))
6467c478bd9Sstevel@tonic-gate 				res = 0;
6477c478bd9Sstevel@tonic-gate 		} else {
6487c478bd9Sstevel@tonic-gate 			res = pagefault(addr,
6497c478bd9Sstevel@tonic-gate 			    (errcode & PF_ERR_PROT)? F_PROT: F_INVAL, rw, 1);
6507c478bd9Sstevel@tonic-gate 		}
651ae115bc7Smrj 		(void) new_mstate(ct, mstate);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		/*
65469c92249SBryan Cantrill 		 * Restore lofault and onfault. If we resolved the fault, exit.
6557c478bd9Sstevel@tonic-gate 		 * If we didn't and lofault wasn't set, die.
6567c478bd9Sstevel@tonic-gate 		 */
657ae115bc7Smrj 		ct->t_lofault = lofault;
65869c92249SBryan Cantrill 		ct->t_onfault = onfault;
6597c478bd9Sstevel@tonic-gate 		if (res == 0)
6607c478bd9Sstevel@tonic-gate 			goto cleanup;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) && defined(_LP64)
6637c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_93) {
6647c478bd9Sstevel@tonic-gate 			/*
6657c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 93. On return from
6667c478bd9Sstevel@tonic-gate 			 * a System Managment Interrupt at a HLT instruction
6677c478bd9Sstevel@tonic-gate 			 * the %rip might be truncated to a 32 bit value.
6687c478bd9Sstevel@tonic-gate 			 * BIOS is supposed to fix this, but some don't.
6697c478bd9Sstevel@tonic-gate 			 * If this occurs we simply restore the high order bits.
6707c478bd9Sstevel@tonic-gate 			 * The HLT instruction is 1 byte of 0xf4.
6717c478bd9Sstevel@tonic-gate 			 */
6727c478bd9Sstevel@tonic-gate 			uintptr_t	rip = rp->r_pc;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 			if ((rip & 0xfffffffful) == rip) {
6757c478bd9Sstevel@tonic-gate 				rip |= 0xfffffffful << 32;
6767c478bd9Sstevel@tonic-gate 				if (hat_getpfnum(kas.a_hat, (caddr_t)rip) !=
6777c478bd9Sstevel@tonic-gate 				    PFN_INVALID &&
6787c478bd9Sstevel@tonic-gate 				    (*(uchar_t *)rip == 0xf4 ||
6797c478bd9Sstevel@tonic-gate 				    *(uchar_t *)(rip - 1) == 0xf4)) {
6807c478bd9Sstevel@tonic-gate 					rp->r_pc = rip;
6817c478bd9Sstevel@tonic-gate 					goto cleanup;
6827c478bd9Sstevel@tonic-gate 				}
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_93 && _LP64 */
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
6887c478bd9Sstevel@tonic-gate 		if (lofault == 0 && opteron_erratum_91) {
6897c478bd9Sstevel@tonic-gate 			/*
6907c478bd9Sstevel@tonic-gate 			 * Workaround for Opteron Erratum 91. Prefetches may
6917c478bd9Sstevel@tonic-gate 			 * generate a page fault (they're not supposed to do
6927c478bd9Sstevel@tonic-gate 			 * that!). If this occurs we simply return back to the
6937c478bd9Sstevel@tonic-gate 			 * instruction.
6947c478bd9Sstevel@tonic-gate 			 */
6957c478bd9Sstevel@tonic-gate 			caddr_t		pc = (caddr_t)rp->r_pc;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			/*
6987c478bd9Sstevel@tonic-gate 			 * If the faulting PC is not mapped, this is a
6997c478bd9Sstevel@tonic-gate 			 * legitimate kernel page fault that must result in a
7007c478bd9Sstevel@tonic-gate 			 * panic. If the faulting PC is mapped, it could contain
7017c478bd9Sstevel@tonic-gate 			 * a prefetch instruction. Check for that here.
7027c478bd9Sstevel@tonic-gate 			 */
7037c478bd9Sstevel@tonic-gate 			if (hat_getpfnum(kas.a_hat, pc) != PFN_INVALID) {
7047c478bd9Sstevel@tonic-gate 				if (cmp_to_prefetch((uchar_t *)pc)) {
7057c478bd9Sstevel@tonic-gate #ifdef DEBUG
7067c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Opteron erratum 91 "
7077c478bd9Sstevel@tonic-gate 					    "occurred: kernel prefetch"
7087c478bd9Sstevel@tonic-gate 					    " at %p generated a page fault!",
7097c478bd9Sstevel@tonic-gate 					    (void *)rp->r_pc);
7107c478bd9Sstevel@tonic-gate #endif /* DEBUG */
7117c478bd9Sstevel@tonic-gate 					goto cleanup;
7127c478bd9Sstevel@tonic-gate 				}
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		if (lofault == 0)
7197c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Cannot resolve fault.  Return to lofault.
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		if (lodebug) {
7257c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
7267c478bd9Sstevel@tonic-gate 			traceregs(rp);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		if (FC_CODE(res) == FC_OBJERR)
7297c478bd9Sstevel@tonic-gate 			res = FC_ERRNO(res);
7307c478bd9Sstevel@tonic-gate 		else
7317c478bd9Sstevel@tonic-gate 			res = EFAULT;
7327c478bd9Sstevel@tonic-gate 		rp->r_r0 = res;
733ae115bc7Smrj 		rp->r_pc = ct->t_lofault;
7347c478bd9Sstevel@tonic-gate 		goto cleanup;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	case T_PGFLT + USER:	/* user page fault */
7377c478bd9Sstevel@tonic-gate 		if (faultdebug) {
7387c478bd9Sstevel@tonic-gate 			char *fault_str;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 			switch (rw) {
7417c478bd9Sstevel@tonic-gate 			case S_READ:
7427c478bd9Sstevel@tonic-gate 				fault_str = "read";
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 			case S_WRITE:
7457c478bd9Sstevel@tonic-gate 				fault_str = "write";
7467c478bd9Sstevel@tonic-gate 				break;
7477c478bd9Sstevel@tonic-gate 			case S_EXEC:
7487c478bd9Sstevel@tonic-gate 				fault_str = "exec";
7497c478bd9Sstevel@tonic-gate 				break;
7507c478bd9Sstevel@tonic-gate 			default:
7517c478bd9Sstevel@tonic-gate 				fault_str = "";
7527c478bd9Sstevel@tonic-gate 				break;
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 			printf("user %s fault:  addr=0x%lx errcode=0x%x\n",
7557c478bd9Sstevel@tonic-gate 			    fault_str, (uintptr_t)addr, errcode);
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) && defined(_LP64)
7597c478bd9Sstevel@tonic-gate 		/*
7607c478bd9Sstevel@tonic-gate 		 * Workaround for AMD erratum 100
7617c478bd9Sstevel@tonic-gate 		 *
7627c478bd9Sstevel@tonic-gate 		 * A 32-bit process may receive a page fault on a non
7637c478bd9Sstevel@tonic-gate 		 * 32-bit address by mistake. The range of the faulting
7647c478bd9Sstevel@tonic-gate 		 * address will be
7657c478bd9Sstevel@tonic-gate 		 *
7667c478bd9Sstevel@tonic-gate 		 *	0xffffffff80000000 .. 0xffffffffffffffff or
7677c478bd9Sstevel@tonic-gate 		 *	0x0000000100000000 .. 0x000000017fffffff
7687c478bd9Sstevel@tonic-gate 		 *
7697c478bd9Sstevel@tonic-gate 		 * The fault is always due to an instruction fetch, however
7707c478bd9Sstevel@tonic-gate 		 * the value of r_pc should be correct (in 32 bit range),
7717c478bd9Sstevel@tonic-gate 		 * so we ignore the page fault on the bogus address.
7727c478bd9Sstevel@tonic-gate 		 */
7737c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_ILP32 &&
7747c478bd9Sstevel@tonic-gate 		    (0xffffffff80000000 <= (uintptr_t)addr ||
7757c478bd9Sstevel@tonic-gate 		    (0x100000000 <= (uintptr_t)addr &&
7767c478bd9Sstevel@tonic-gate 		    (uintptr_t)addr <= 0x17fffffff))) {
7777c478bd9Sstevel@tonic-gate 			if (!opteron_erratum_100)
7787c478bd9Sstevel@tonic-gate 				panic("unexpected erratum #100");
7797c478bd9Sstevel@tonic-gate 			if (rp->r_pc <= 0xffffffff)
7807c478bd9Sstevel@tonic-gate 				goto out;
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_100 && _LP64 */
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		ASSERT(!(curthread->t_flag & T_WATCHPT));
7857c478bd9Sstevel@tonic-gate 		watchpage = (pr_watch_active(p) && pr_is_watchpage(addr, rw));
7867c478bd9Sstevel@tonic-gate 		vaddr = addr;
7877c478bd9Sstevel@tonic-gate 		if (!watchpage || (sz = instr_size(rp, &vaddr, rw)) <= 0)
7887c478bd9Sstevel@tonic-gate 			fault_type = (errcode & PF_ERR_PROT)? F_PROT: F_INVAL;
7897c478bd9Sstevel@tonic-gate 		else if ((watchcode = pr_is_watchpoint(&vaddr, &ta,
7907c478bd9Sstevel@tonic-gate 		    sz, NULL, rw)) != 0) {
7917c478bd9Sstevel@tonic-gate 			if (ta) {
7927c478bd9Sstevel@tonic-gate 				do_watch_step(vaddr, sz, rw,
7937c478bd9Sstevel@tonic-gate 				    watchcode, rp->r_pc);
7947c478bd9Sstevel@tonic-gate 				fault_type = F_INVAL;
7957c478bd9Sstevel@tonic-gate 			} else {
7967c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
7977c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
7987c478bd9Sstevel@tonic-gate 				siginfo.si_code = watchcode;
7997c478bd9Sstevel@tonic-gate 				siginfo.si_addr = vaddr;
8007c478bd9Sstevel@tonic-gate 				siginfo.si_trapafter = 0;
8017c478bd9Sstevel@tonic-gate 				siginfo.si_pc = (caddr_t)rp->r_pc;
8027c478bd9Sstevel@tonic-gate 				fault = FLTWATCH;
8037c478bd9Sstevel@tonic-gate 				break;
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 		} else {
8067c478bd9Sstevel@tonic-gate 			/* XXX pr_watch_emul() never succeeds (for now) */
8077c478bd9Sstevel@tonic-gate 			if (rw != S_EXEC && pr_watch_emul(rp, vaddr, rw))
8087c478bd9Sstevel@tonic-gate 				goto out;
8097c478bd9Sstevel@tonic-gate 			do_watch_step(vaddr, sz, rw, 0, 0);
8107c478bd9Sstevel@tonic-gate 			fault_type = F_INVAL;
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		res = pagefault(addr, fault_type, rw, 0);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		/*
8167c478bd9Sstevel@tonic-gate 		 * If pagefault() succeeded, ok.
8177c478bd9Sstevel@tonic-gate 		 * Otherwise attempt to grow the stack.
8187c478bd9Sstevel@tonic-gate 		 */
8197c478bd9Sstevel@tonic-gate 		if (res == 0 ||
8207c478bd9Sstevel@tonic-gate 		    (res == FC_NOMAP &&
8217c478bd9Sstevel@tonic-gate 		    addr < p->p_usrstack &&
8227c478bd9Sstevel@tonic-gate 		    grow(addr))) {
8237c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfault = FLTPAGE;
8247c478bd9Sstevel@tonic-gate 			lwp->lwp_lastfaddr = addr;
8257c478bd9Sstevel@tonic-gate 			if (prismember(&p->p_fltmask, FLTPAGE)) {
8267c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
8277c478bd9Sstevel@tonic-gate 				siginfo.si_addr = addr;
8287c478bd9Sstevel@tonic-gate 				(void) stop_on_fault(FLTPAGE, &siginfo);
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			goto out;
8317c478bd9Sstevel@tonic-gate 		} else if (res == FC_PROT && addr < p->p_usrstack &&
8327c478bd9Sstevel@tonic-gate 		    (mmu.pt_nx != 0 && (errcode & PF_ERR_EXEC))) {
8337c478bd9Sstevel@tonic-gate 			report_stack_exec(p, addr);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate #ifdef OPTERON_ERRATUM_91
8377c478bd9Sstevel@tonic-gate 		/*
8387c478bd9Sstevel@tonic-gate 		 * Workaround for Opteron Erratum 91. Prefetches may generate a
8397c478bd9Sstevel@tonic-gate 		 * page fault (they're not supposed to do that!). If this
8407c478bd9Sstevel@tonic-gate 		 * occurs we simply return back to the instruction.
8417c478bd9Sstevel@tonic-gate 		 *
8427c478bd9Sstevel@tonic-gate 		 * We rely on copyin to properly fault in the page with r_pc.
8437c478bd9Sstevel@tonic-gate 		 */
8447c478bd9Sstevel@tonic-gate 		if (opteron_erratum_91 &&
8457c478bd9Sstevel@tonic-gate 		    addr != (caddr_t)rp->r_pc &&
8467c478bd9Sstevel@tonic-gate 		    instr_is_prefetch((caddr_t)rp->r_pc)) {
8477c478bd9Sstevel@tonic-gate #ifdef DEBUG
8487c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Opteron erratum 91 occurred: "
8497c478bd9Sstevel@tonic-gate 			    "prefetch at %p in pid %d generated a trap!",
8507c478bd9Sstevel@tonic-gate 			    (void *)rp->r_pc, p->p_pid);
8517c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8527c478bd9Sstevel@tonic-gate 			goto out;
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate #endif /* OPTERON_ERRATUM_91 */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		if (tudebug)
8577c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
8587c478bd9Sstevel@tonic-gate 		/*
8597c478bd9Sstevel@tonic-gate 		 * In the case where both pagefault and grow fail,
8607c478bd9Sstevel@tonic-gate 		 * set the code to the value provided by pagefault.
8617c478bd9Sstevel@tonic-gate 		 * We map all errors returned from pagefault() to SIGSEGV.
8627c478bd9Sstevel@tonic-gate 		 */
8637c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
8647c478bd9Sstevel@tonic-gate 		siginfo.si_addr = addr;
8657c478bd9Sstevel@tonic-gate 		switch (FC_CODE(res)) {
8667c478bd9Sstevel@tonic-gate 		case FC_HWERR:
8677c478bd9Sstevel@tonic-gate 		case FC_NOSUPPORT:
8687c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8697c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRERR;
8707c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
8717c478bd9Sstevel@tonic-gate 			break;
8727c478bd9Sstevel@tonic-gate 		case FC_ALIGN:
8737c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGBUS;
8747c478bd9Sstevel@tonic-gate 			siginfo.si_code = BUS_ADRALN;
8757c478bd9Sstevel@tonic-gate 			fault = FLTACCESS;
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 		case FC_OBJERR:
8787c478bd9Sstevel@tonic-gate 			if ((siginfo.si_errno = FC_ERRNO(res)) != EINTR) {
8797c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGBUS;
8807c478bd9Sstevel@tonic-gate 				siginfo.si_code = BUS_OBJERR;
8817c478bd9Sstevel@tonic-gate 				fault = FLTACCESS;
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 			break;
8847c478bd9Sstevel@tonic-gate 		default:	/* FC_NOMAP or FC_PROT */
8857c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGSEGV;
8867c478bd9Sstevel@tonic-gate 			siginfo.si_code =
8877c478bd9Sstevel@tonic-gate 			    (res == FC_NOMAP)? SEGV_MAPERR : SEGV_ACCERR;
8887c478bd9Sstevel@tonic-gate 			fault = FLTBOUNDS;
8897c478bd9Sstevel@tonic-gate 			break;
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	case T_ILLINST + USER:	/* invalid opcode fault */
8947c478bd9Sstevel@tonic-gate 		/*
8957c478bd9Sstevel@tonic-gate 		 * If the syscall instruction is disabled due to LDT usage, a
8967c478bd9Sstevel@tonic-gate 		 * user program that attempts to execute it will trigger a #ud
8977c478bd9Sstevel@tonic-gate 		 * trap. Check for that case here. If this occurs on a CPU which
8987c478bd9Sstevel@tonic-gate 		 * doesn't even support syscall, the result of all of this will
8997c478bd9Sstevel@tonic-gate 		 * be to emulate that particular instruction.
9007c478bd9Sstevel@tonic-gate 		 */
9017c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
9027417cfdeSKuriakose Kuruvilla 		    ldt_rewrite_syscall(rp, p, X86FSET_ASYSC))
9037c478bd9Sstevel@tonic-gate 			goto out;
904130a12b8Sfvdl 
905130a12b8Sfvdl 		/*
906130a12b8Sfvdl 		 * Emulate the LAHF and SAHF instructions if needed.
907130a12b8Sfvdl 		 * See the instr_is_lsahf function for details.
908130a12b8Sfvdl 		 */
909130a12b8Sfvdl 		if (p->p_model == DATAMODEL_LP64 &&
910130a12b8Sfvdl 		    instr_is_lsahf((caddr_t)rp->r_pc, &instr)) {
911130a12b8Sfvdl 			emulate_lsahf(rp, instr);
912130a12b8Sfvdl 			goto out;
913130a12b8Sfvdl 		}
914130a12b8Sfvdl 
9157c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		if (tudebug)
9187c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9197c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGILL;
9207c478bd9Sstevel@tonic-gate 		siginfo.si_code  = ILL_ILLOPC;
9217c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9227c478bd9Sstevel@tonic-gate 		fault = FLTILL;
9237c478bd9Sstevel@tonic-gate 		break;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	case T_ZERODIV + USER:		/* integer divide by zero */
9267c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9277c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9287c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9297c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTDIV;
9307c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9317c478bd9Sstevel@tonic-gate 		fault = FLTIZDIV;
9327c478bd9Sstevel@tonic-gate 		break;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	case T_OVFLW + USER:	/* integer overflow */
9357c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9367c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
9377c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGFPE;
9387c478bd9Sstevel@tonic-gate 		siginfo.si_code  = FPE_INTOVF;
9397c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
9407c478bd9Sstevel@tonic-gate 		fault = FLTIOVF;
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 
9434c28a617SRobert Mustacchi 	/*
9444c28a617SRobert Mustacchi 	 * When using an eager FPU on x86, the #NM trap is no longer meaningful.
9454c28a617SRobert Mustacchi 	 * Userland should not be able to trigger it. Anything that does
9464c28a617SRobert Mustacchi 	 * represents a fatal error in the kernel and likely in the register
9474c28a617SRobert Mustacchi 	 * state of the system. User FPU state should always be valid.
9484c28a617SRobert Mustacchi 	 */
9497c478bd9Sstevel@tonic-gate 	case T_NOEXTFLT + USER:	/* math coprocessor not available */
9504c28a617SRobert Mustacchi 	case T_NOEXTFLT:
9517c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
9527c478bd9Sstevel@tonic-gate 		break;
9537c478bd9Sstevel@tonic-gate 
9544c28a617SRobert Mustacchi 	/*
9554c28a617SRobert Mustacchi 	 * Kernel threads leveraging floating point need to mask the exceptions
9564c28a617SRobert Mustacchi 	 * or ensure that they cannot happen. There is no recovery from this.
9574c28a617SRobert Mustacchi 	 */
9587c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT:	/* x87 floating point exception pending */
959eae0da43Ssethg 		sti(); /* T_EXTERRFLT comes in via cmninttrap */
9607c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
9614c28a617SRobert Mustacchi 		break;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	case T_EXTERRFLT + USER: /* x87 floating point exception pending */
9647c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugfpe)
9657c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9663df2e8b2SRobert Mustacchi 		if ((sicode = fpexterrflt(rp)) != 0) {
9677c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9687c478bd9Sstevel@tonic-gate 			siginfo.si_code  = sicode;
9697c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9707c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	case T_SIMDFPE + USER:		/* SSE and SSE2 exceptions */
9757c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugsse)
9767c478bd9Sstevel@tonic-gate 			showregs(type, rp, addr);
9777417cfdeSKuriakose Kuruvilla 		if (!is_x86_feature(x86_featureset, X86FSET_SSE) &&
9787417cfdeSKuriakose Kuruvilla 		    !is_x86_feature(x86_featureset, X86FSET_SSE2)) {
9797c478bd9Sstevel@tonic-gate 			/*
9807c478bd9Sstevel@tonic-gate 			 * There are rumours that some user instructions
9817c478bd9Sstevel@tonic-gate 			 * on older CPUs can cause this trap to occur; in
9827c478bd9Sstevel@tonic-gate 			 * which case send a SIGILL instead of a SIGFPE.
9837c478bd9Sstevel@tonic-gate 			 */
9847c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGILL;
9857c478bd9Sstevel@tonic-gate 			siginfo.si_code  = ILL_ILLTRP;
9867c478bd9Sstevel@tonic-gate 			siginfo.si_addr  = (caddr_t)rp->r_pc;
9877c478bd9Sstevel@tonic-gate 			siginfo.si_trapno = type & ~USER;
9887c478bd9Sstevel@tonic-gate 			fault = FLTILL;
9897c478bd9Sstevel@tonic-gate 		} else if ((sicode = fpsimderrflt(rp)) != 0) {
9907c478bd9Sstevel@tonic-gate 			siginfo.si_signo = SIGFPE;
9917c478bd9Sstevel@tonic-gate 			siginfo.si_code = sicode;
9927c478bd9Sstevel@tonic-gate 			siginfo.si_addr = (caddr_t)rp->r_pc;
9937c478bd9Sstevel@tonic-gate 			fault = FLTFPE;
9947c478bd9Sstevel@tonic-gate 		}
995eae0da43Ssethg 
996eae0da43Ssethg 		sti();	/* The SIMD exception comes in via cmninttrap */
9977c478bd9Sstevel@tonic-gate 		break;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	case T_BPTFLT:	/* breakpoint trap */
10007c478bd9Sstevel@tonic-gate 		/*
10017c478bd9Sstevel@tonic-gate 		 * Kernel breakpoint traps should only happen when kmdb is
10027c478bd9Sstevel@tonic-gate 		 * active, and even then, it'll have interposed on the IDT, so
10037c478bd9Sstevel@tonic-gate 		 * control won't get here.  If it does, we've hit a breakpoint
10047c478bd9Sstevel@tonic-gate 		 * without the debugger, which is very strange, and very
10057c478bd9Sstevel@tonic-gate 		 * fatal.
10067c478bd9Sstevel@tonic-gate 		 */
10077c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
10087c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		(void) die(type, rp, addr, cpuid);
10117c478bd9Sstevel@tonic-gate 		break;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	case T_SGLSTP: /* single step/hw breakpoint exception */
10147c478bd9Sstevel@tonic-gate 
101574ecdb51SJohn Levon #if !defined(__xpv)
101674ecdb51SJohn Levon 		/*
101774ecdb51SJohn Levon 		 * We'd never normally get here, as kmdb handles its own single
101874ecdb51SJohn Levon 		 * step traps.  There is one nasty exception though, as
101974ecdb51SJohn Levon 		 * described in more detail in sys_sysenter().  Note that
102074ecdb51SJohn Levon 		 * checking for all four locations covers both the KPTI and the
102174ecdb51SJohn Levon 		 * non-KPTI cases correctly: the former will never be found at
102274ecdb51SJohn Levon 		 * (brand_)sys_sysenter, and vice versa.
102374ecdb51SJohn Levon 		 */
10247c478bd9Sstevel@tonic-gate 		if (lwp != NULL && (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP)) {
102574ecdb51SJohn Levon 			if (rp->r_pc == (greg_t)brand_sys_sysenter ||
102674ecdb51SJohn Levon 			    rp->r_pc == (greg_t)sys_sysenter ||
102774ecdb51SJohn Levon 			    rp->r_pc == (greg_t)tr_brand_sys_sysenter ||
102874ecdb51SJohn Levon 			    rp->r_pc == (greg_t)tr_sys_sysenter) {
102974ecdb51SJohn Levon 
103074ecdb51SJohn Levon 				rp->r_pc += 0x3; /* sizeof (swapgs) */
103174ecdb51SJohn Levon 
10327c478bd9Sstevel@tonic-gate 				rp->r_ps &= ~PS_T; /* turn off trace */
10337c478bd9Sstevel@tonic-gate 				lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
1034ae115bc7Smrj 				ct->t_post_sys = 1;
1035d8aa0f5aSsudheer 				aston(curthread);
10367c478bd9Sstevel@tonic-gate 				goto cleanup;
103774ecdb51SJohn Levon 			} else {
103874ecdb51SJohn Levon 				if (tudebug && tudebugbpt)
103974ecdb51SJohn Levon 					showregs(type, rp, (caddr_t)0);
10407c478bd9Sstevel@tonic-gate 			}
10417c478bd9Sstevel@tonic-gate 		}
104274ecdb51SJohn Levon #endif /* !__xpv */
104374ecdb51SJohn Levon 
10447c478bd9Sstevel@tonic-gate 		if (boothowto & RB_DEBUG)
10457c478bd9Sstevel@tonic-gate 			debug_enter((char *)NULL);
10467c478bd9Sstevel@tonic-gate 		else
10477c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
10487c478bd9Sstevel@tonic-gate 		break;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	case T_NMIFLT:	/* NMI interrupt */
10517c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in system mode\n");
10527c478bd9Sstevel@tonic-gate 		goto cleanup;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	case T_NMIFLT + USER:	/* NMI interrupt */
10557c478bd9Sstevel@tonic-gate 		printf("Unexpected NMI in user mode\n");
10567c478bd9Sstevel@tonic-gate 		break;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	case T_GPFLT:	/* general protection violation */
10597c478bd9Sstevel@tonic-gate 		/*
1060a4e4e13fSgavinm 		 * Any #GP that occurs during an on_trap .. no_trap bracket
1061a4e4e13fSgavinm 		 * with OT_DATA_ACCESS or OT_SEGMENT_ACCESS protection,
1062a4e4e13fSgavinm 		 * or in a on_fault .. no_fault bracket, is forgiven
1063a4e4e13fSgavinm 		 * and we trampoline.  This protection is given regardless
1064a4e4e13fSgavinm 		 * of whether we are 32/64 bit etc - if a distinction is
1065a4e4e13fSgavinm 		 * required then define new on_trap protection types.
1066a4e4e13fSgavinm 		 *
10677c478bd9Sstevel@tonic-gate 		 * On amd64, we can get a #gp from referencing addresses
1068843e1988Sjohnlev 		 * in the virtual address hole e.g. from a copyin or in
1069843e1988Sjohnlev 		 * update_sregs while updating user segment registers.
1070843e1988Sjohnlev 		 *
1071843e1988Sjohnlev 		 * On the 32-bit hypervisor we could also generate one in
1072843e1988Sjohnlev 		 * mfn_to_pfn by reaching around or into where the hypervisor
1073843e1988Sjohnlev 		 * lives which is protected by segmentation.
10747c478bd9Sstevel@tonic-gate 		 */
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		/*
10777c478bd9Sstevel@tonic-gate 		 * If we're under on_trap() protection (see <sys/ontrap.h>),
107820c794b3Sgavinm 		 * set ot_trap and trampoline back to the on_trap() call site
1079843e1988Sjohnlev 		 * for OT_DATA_ACCESS or OT_SEGMENT_ACCESS.
10807c478bd9Sstevel@tonic-gate 		 */
1081ae115bc7Smrj 		if (ct->t_ontrap != NULL) {
1082843e1988Sjohnlev 			int ttype =  ct->t_ontrap->ot_prot &
1083843e1988Sjohnlev 			    (OT_DATA_ACCESS | OT_SEGMENT_ACCESS);
1084ae115bc7Smrj 
1085843e1988Sjohnlev 			if (ttype != 0) {
1086843e1988Sjohnlev 				ct->t_ontrap->ot_trap |= ttype;
1087843e1988Sjohnlev 				if (tudebug)
1088843e1988Sjohnlev 					showregs(type, rp, (caddr_t)0);
108920c794b3Sgavinm 				rp->r_pc = ct->t_ontrap->ot_trampoline;
109020c794b3Sgavinm 				goto cleanup;
10917c478bd9Sstevel@tonic-gate 			}
1092843e1988Sjohnlev 		}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		/*
10957c478bd9Sstevel@tonic-gate 		 * If we're under lofault protection (copyin etc.),
10967c478bd9Sstevel@tonic-gate 		 * longjmp back to lofault with an EFAULT.
10977c478bd9Sstevel@tonic-gate 		 */
1098ae115bc7Smrj 		if (ct->t_lofault) {
10997c478bd9Sstevel@tonic-gate 			/*
11007c478bd9Sstevel@tonic-gate 			 * Fault is not resolvable, so just return to lofault
11017c478bd9Sstevel@tonic-gate 			 */
11027c478bd9Sstevel@tonic-gate 			if (lodebug) {
11037c478bd9Sstevel@tonic-gate 				showregs(type, rp, addr);
11047c478bd9Sstevel@tonic-gate 				traceregs(rp);
11057c478bd9Sstevel@tonic-gate 			}
11067c478bd9Sstevel@tonic-gate 			rp->r_r0 = EFAULT;
1107ae115bc7Smrj 			rp->r_pc = ct->t_lofault;
11087c478bd9Sstevel@tonic-gate 			goto cleanup;
11097c478bd9Sstevel@tonic-gate 		}
1110a4e4e13fSgavinm 
1111a4e4e13fSgavinm 		/*
1112a4e4e13fSgavinm 		 * We fall through to the next case, which repeats
1113a4e4e13fSgavinm 		 * the OT_SEGMENT_ACCESS check which we've already
1114a4e4e13fSgavinm 		 * done, so we'll always fall through to the
1115a4e4e13fSgavinm 		 * T_STKFLT case.
1116a4e4e13fSgavinm 		 */
11177c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1118ae115bc7Smrj 	case T_SEGFLT:	/* segment not present fault */
1119ae115bc7Smrj 		/*
1120ae115bc7Smrj 		 * One example of this is #NP in update_sregs while
1121ae115bc7Smrj 		 * attempting to update a user segment register
1122ae115bc7Smrj 		 * that points to a descriptor that is marked not
1123ae115bc7Smrj 		 * present.
1124ae115bc7Smrj 		 */
1125ae115bc7Smrj 		if (ct->t_ontrap != NULL &&
1126ae115bc7Smrj 		    ct->t_ontrap->ot_prot & OT_SEGMENT_ACCESS) {
1127ae115bc7Smrj 			ct->t_ontrap->ot_trap |= OT_SEGMENT_ACCESS;
1128843e1988Sjohnlev 			if (tudebug)
1129843e1988Sjohnlev 				showregs(type, rp, (caddr_t)0);
113020c794b3Sgavinm 			rp->r_pc = ct->t_ontrap->ot_trampoline;
113120c794b3Sgavinm 			goto cleanup;
1132ae115bc7Smrj 		}
1133ae115bc7Smrj 		/*FALLTHROUGH*/
11347c478bd9Sstevel@tonic-gate 	case T_STKFLT:	/* stack fault */
11357c478bd9Sstevel@tonic-gate 	case T_TSSFLT:	/* invalid TSS fault */
11367c478bd9Sstevel@tonic-gate 		if (tudebug)
11377c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
11387c478bd9Sstevel@tonic-gate 		if (kern_gpfault(rp))
11397c478bd9Sstevel@tonic-gate 			(void) die(type, rp, addr, cpuid);
11407c478bd9Sstevel@tonic-gate 		goto cleanup;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/*
1143ae115bc7Smrj 	 * ONLY 32-bit PROCESSES can USE a PRIVATE LDT! 64-bit apps
1144ae115bc7Smrj 	 * should have no need for them, so we put a stop to it here.
11457c478bd9Sstevel@tonic-gate 	 *
1146ae115bc7Smrj 	 * So: not-present fault is ONLY valid for 32-bit processes with
1147ae115bc7Smrj 	 * a private LDT trying to do a system call. Emulate it.
11480baeff3dSrab 	 *
1149ae115bc7Smrj 	 * #gp fault is ONLY valid for 32-bit processes also, which DO NOT
1150ae115bc7Smrj 	 * have a private LDT, and are trying to do a system call. Emulate it.
11510baeff3dSrab 	 */
1152ae115bc7Smrj 
11530baeff3dSrab 	case T_SEGFLT + USER:	/* segment not present fault */
11540baeff3dSrab 	case T_GPFLT + USER:	/* general protection violation */
11550baeff3dSrab #ifdef _SYSCALL32_IMPL
11560baeff3dSrab 		if (p->p_model != DATAMODEL_NATIVE) {
11570baeff3dSrab #endif /* _SYSCALL32_IMPL */
1158ddece0baSsethg 		if (instr_is_lcall_syscall((caddr_t)rp->r_pc)) {
11590baeff3dSrab 			if (type == T_SEGFLT + USER)
11600baeff3dSrab 				ASSERT(p->p_ldt != NULL);
11610baeff3dSrab 
11620baeff3dSrab 			if ((p->p_ldt == NULL && type == T_GPFLT + USER) ||
11630baeff3dSrab 			    type == T_SEGFLT + USER) {
11640baeff3dSrab 
11650baeff3dSrab 			/*
11660baeff3dSrab 			 * The user attempted a system call via the obsolete
11670baeff3dSrab 			 * call gate mechanism. Because the process doesn't have
11680baeff3dSrab 			 * an LDT (i.e. the ldtr contains 0), a #gp results.
11690baeff3dSrab 			 * Emulate the syscall here, just as we do above for a
11700baeff3dSrab 			 * #np trap.
11710baeff3dSrab 			 */
11720baeff3dSrab 
11730baeff3dSrab 			/*
11747c478bd9Sstevel@tonic-gate 			 * Since this is a not-present trap, rp->r_pc points to
11757c478bd9Sstevel@tonic-gate 			 * the trapping lcall instruction. We need to bump it
11767c478bd9Sstevel@tonic-gate 			 * to the next insn so the app can continue on.
11777c478bd9Sstevel@tonic-gate 			 */
11787c478bd9Sstevel@tonic-gate 			rp->r_pc += LCALLSIZE;
11797c478bd9Sstevel@tonic-gate 			lwp->lwp_regs = rp;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 			/*
11827c478bd9Sstevel@tonic-gate 			 * Normally the microstate of the LWP is forced back to
11837c478bd9Sstevel@tonic-gate 			 * LMS_USER by the syscall handlers. Emulate that
11847c478bd9Sstevel@tonic-gate 			 * behavior here.
11857c478bd9Sstevel@tonic-gate 			 */
11867c478bd9Sstevel@tonic-gate 			mstate = LMS_USER;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 			dosyscall();
11897c478bd9Sstevel@tonic-gate 			goto out;
11907c478bd9Sstevel@tonic-gate 			}
11910baeff3dSrab 		}
11920baeff3dSrab #ifdef _SYSCALL32_IMPL
11930baeff3dSrab 		}
11947c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
11957c478bd9Sstevel@tonic-gate 		/*
11967c478bd9Sstevel@tonic-gate 		 * If the current process is using a private LDT and the
11977c478bd9Sstevel@tonic-gate 		 * trapping instruction is sysenter, the sysenter instruction
11987c478bd9Sstevel@tonic-gate 		 * has been disabled on the CPU because it destroys segment
11997c478bd9Sstevel@tonic-gate 		 * registers. If this is the case, rewrite the instruction to
12007c478bd9Sstevel@tonic-gate 		 * be a safe system call and retry it. If this occurs on a CPU
12017c478bd9Sstevel@tonic-gate 		 * which doesn't even support sysenter, the result of all of
12027c478bd9Sstevel@tonic-gate 		 * this will be to emulate that particular instruction.
12037c478bd9Sstevel@tonic-gate 		 */
12047c478bd9Sstevel@tonic-gate 		if (p->p_ldt != NULL &&
12057417cfdeSKuriakose Kuruvilla 		    ldt_rewrite_syscall(rp, p, X86FSET_SEP))
12067c478bd9Sstevel@tonic-gate 			goto out;
1207ddece0baSsethg 
12087c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	case T_BOUNDFLT + USER:	/* bound fault */
12117c478bd9Sstevel@tonic-gate 	case T_STKFLT + USER:	/* stack fault */
12127c478bd9Sstevel@tonic-gate 	case T_TSSFLT + USER:	/* invalid TSS fault */
12137c478bd9Sstevel@tonic-gate 		if (tudebug)
12147c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12157c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGSEGV;
12167c478bd9Sstevel@tonic-gate 		siginfo.si_code  = SEGV_MAPERR;
12177c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12187c478bd9Sstevel@tonic-gate 		fault = FLTBOUNDS;
12197c478bd9Sstevel@tonic-gate 		break;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	case T_ALIGNMENT + USER:	/* user alignment error (486) */
12227c478bd9Sstevel@tonic-gate 		if (tudebug)
12237c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12247c478bd9Sstevel@tonic-gate 		bzero(&siginfo, sizeof (siginfo));
12257c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGBUS;
12267c478bd9Sstevel@tonic-gate 		siginfo.si_code = BUS_ADRALN;
12277c478bd9Sstevel@tonic-gate 		siginfo.si_addr = (caddr_t)rp->r_pc;
12287c478bd9Sstevel@tonic-gate 		fault = FLTACCESS;
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	case T_SGLSTP + USER: /* single step/hw breakpoint exception */
12327c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12337c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/* Was it single-stepping? */
12367c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_drstat & DR_SINGLESTEP) {
12377c478bd9Sstevel@tonic-gate 			pcb_t *pcb = &lwp->lwp_pcb;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 			rp->r_ps &= ~PS_T;
12407c478bd9Sstevel@tonic-gate 			/*
12417c478bd9Sstevel@tonic-gate 			 * If both NORMAL_STEP and WATCH_STEP are in effect,
12429acbbeafSnn35248 			 * give precedence to WATCH_STEP.  If neither is set,
12437c478bd9Sstevel@tonic-gate 			 * user must have set the PS_T bit in %efl; treat this
12447c478bd9Sstevel@tonic-gate 			 * as NORMAL_STEP.
12457c478bd9Sstevel@tonic-gate 			 */
12469acbbeafSnn35248 			if ((fault = undo_watch_step(&siginfo)) == 0 &&
12479acbbeafSnn35248 			    ((pcb->pcb_flags & NORMAL_STEP) ||
12489acbbeafSnn35248 			    !(pcb->pcb_flags & WATCH_STEP))) {
12497c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGTRAP;
12507c478bd9Sstevel@tonic-gate 				siginfo.si_code = TRAP_TRACE;
12517c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
12527c478bd9Sstevel@tonic-gate 				fault = FLTTRACE;
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 			pcb->pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 		break;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	case T_BPTFLT + USER:	/* breakpoint trap */
12597c478bd9Sstevel@tonic-gate 		if (tudebug && tudebugbpt)
12607c478bd9Sstevel@tonic-gate 			showregs(type, rp, (caddr_t)0);
12617c478bd9Sstevel@tonic-gate 		/*
12627c478bd9Sstevel@tonic-gate 		 * int 3 (the breakpoint instruction) leaves the pc referring
12637c478bd9Sstevel@tonic-gate 		 * to the address one byte after the breakpointed address.
12647c478bd9Sstevel@tonic-gate 		 * If the P_PR_BPTADJ flag has been set via /proc, We adjust
12657c478bd9Sstevel@tonic-gate 		 * it back so it refers to the breakpointed address.
12667c478bd9Sstevel@tonic-gate 		 */
12677c478bd9Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_BPTADJ)
12687c478bd9Sstevel@tonic-gate 			rp->r_pc--;
12697c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
12707c478bd9Sstevel@tonic-gate 		siginfo.si_code  = TRAP_BRKPT;
12717c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = (caddr_t)rp->r_pc;
12727c478bd9Sstevel@tonic-gate 		fault = FLTBPT;
12737c478bd9Sstevel@tonic-gate 		break;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	case T_AST:
12767c478bd9Sstevel@tonic-gate 		/*
12777c478bd9Sstevel@tonic-gate 		 * This occurs only after the cs register has been made to
12787c478bd9Sstevel@tonic-gate 		 * look like a kernel selector, either through debugging or
12797c478bd9Sstevel@tonic-gate 		 * possibly by functions like setcontext().  The thread is
12807c478bd9Sstevel@tonic-gate 		 * about to cause a general protection fault at common_iret()
12817c478bd9Sstevel@tonic-gate 		 * in locore.  We let that happen immediately instead of
12827c478bd9Sstevel@tonic-gate 		 * doing the T_AST processing.
12837c478bd9Sstevel@tonic-gate 		 */
12847c478bd9Sstevel@tonic-gate 		goto cleanup;
12857c478bd9Sstevel@tonic-gate 
128620c794b3Sgavinm 	case T_AST + USER:	/* profiling, resched, h/w error pseudo trap */
128720c794b3Sgavinm 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
128820c794b3Sgavinm 			proc_t *p = ttoproc(curthread);
1289d6c90996SAbhinandan Ekande 			extern void print_msg_hwerr(ctid_t ct_id, proc_t *p);
129020c794b3Sgavinm 
129120c794b3Sgavinm 			lwp->lwp_pcb.pcb_flags &= ~ASYNC_HWERR;
1292d6c90996SAbhinandan Ekande 			print_msg_hwerr(p->p_ct_process->conp_contract.ct_id,
1293d6c90996SAbhinandan Ekande 			    p);
129420c794b3Sgavinm 			contract_process_hwerr(p->p_ct_process, p);
129520c794b3Sgavinm 			siginfo.si_signo = SIGKILL;
129620c794b3Sgavinm 			siginfo.si_code = SI_NOINFO;
129720c794b3Sgavinm 		} else if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) {
12987c478bd9Sstevel@tonic-gate 			lwp->lwp_pcb.pcb_flags &= ~CPC_OVERFLOW;
12997c478bd9Sstevel@tonic-gate 			if (kcpc_overflow_ast()) {
13007c478bd9Sstevel@tonic-gate 				/*
13017c478bd9Sstevel@tonic-gate 				 * Signal performance counter overflow
13027c478bd9Sstevel@tonic-gate 				 */
13037c478bd9Sstevel@tonic-gate 				if (tudebug)
13047c478bd9Sstevel@tonic-gate 					showregs(type, rp, (caddr_t)0);
13057c478bd9Sstevel@tonic-gate 				bzero(&siginfo, sizeof (siginfo));
13067c478bd9Sstevel@tonic-gate 				siginfo.si_signo = SIGEMT;
13077c478bd9Sstevel@tonic-gate 				siginfo.si_code = EMT_CPCOVF;
13087c478bd9Sstevel@tonic-gate 				siginfo.si_addr = (caddr_t)rp->r_pc;
13097c478bd9Sstevel@tonic-gate 				fault = FLTCPCOVF;
13107c478bd9Sstevel@tonic-gate 			}
13117c478bd9Sstevel@tonic-gate 		}
131220c794b3Sgavinm 
13137c478bd9Sstevel@tonic-gate 		break;
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * We can't get here from a system trap
13187c478bd9Sstevel@tonic-gate 	 */
13197c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (fault) {
132210e812d4Saf 		/* We took a fault so abort single step. */
132310e812d4Saf 		lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP);
13247c478bd9Sstevel@tonic-gate 		/*
13257c478bd9Sstevel@tonic-gate 		 * Remember the fault and fault adddress
13267c478bd9Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
13277c478bd9Sstevel@tonic-gate 		 */
13287c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
13297c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(fault, int, fault, ksiginfo_t *, &siginfo);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		/*
13347c478bd9Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
13357c478bd9Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
13367c478bd9Sstevel@tonic-gate 		 * deliver the associated signal.
13377c478bd9Sstevel@tonic-gate 		 */
13387c478bd9Sstevel@tonic-gate 		if (siginfo.si_signo != SIGKILL &&
13397c478bd9Sstevel@tonic-gate 		    prismember(&p->p_fltmask, fault) &&
13407c478bd9Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
13417c478bd9Sstevel@tonic-gate 			siginfo.si_signo = 0;
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	if (siginfo.si_signo)
13456075fa3dSRoger A. Faulkner 		trapsig(&siginfo, (fault != FLTFPE && fault != FLTCPCOVF));
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if (lwp->lwp_oweupc)
13487c478bd9Sstevel@tonic-gate 		profil_tick(rp->r_pc);
13497c478bd9Sstevel@tonic-gate 
1350ae115bc7Smrj 	if (ct->t_astflag | ct->t_sig_check) {
13517c478bd9Sstevel@tonic-gate 		/*
13527c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
13537c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
13547c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
13557c478bd9Sstevel@tonic-gate 		 * syscall.
13567c478bd9Sstevel@tonic-gate 		 */
1357ae115bc7Smrj 		astoff(ct);
1358d8aa0f5aSsudheer 		/*
1359d8aa0f5aSsudheer 		 * If a single-step trap occurred on a syscall (see above)
1360d8aa0f5aSsudheer 		 * recognize it now.  Do this before checking for signals
1361d8aa0f5aSsudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
1362d8aa0f5aSsudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
1363d8aa0f5aSsudheer 		 */
1364d8aa0f5aSsudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
1365d8aa0f5aSsudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
1366d8aa0f5aSsudheer 
1367ae115bc7Smrj 		ct->t_sig_check = 0;
13687c478bd9Sstevel@tonic-gate 
13697429414eSBryan Cantrill 		/*
13707429414eSBryan Cantrill 		 * As in other code paths that check against TP_CHANGEBIND,
13717429414eSBryan Cantrill 		 * we perform the check first without p_lock held -- only
13727429414eSBryan Cantrill 		 * acquiring p_lock in the unlikely event that it is indeed
13737429414eSBryan Cantrill 		 * set.  This is safe because we are doing this after the
13747429414eSBryan Cantrill 		 * astoff(); if we are racing another thread setting
13757429414eSBryan Cantrill 		 * TP_CHANGEBIND on us, we will pick it up on a subsequent
13767429414eSBryan Cantrill 		 * lap through.
13777429414eSBryan Cantrill 		 */
13787429414eSBryan Cantrill 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
13797c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
13807c478bd9Sstevel@tonic-gate 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
13817c478bd9Sstevel@tonic-gate 				timer_lwpbind();
13827c478bd9Sstevel@tonic-gate 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
13837c478bd9Sstevel@tonic-gate 			}
13847c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
13857429414eSBryan Cantrill 		}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 		/*
13887c478bd9Sstevel@tonic-gate 		 * for kaio requests that are on the per-process poll queue,
13897c478bd9Sstevel@tonic-gate 		 * aiop->aio_pollq, they're AIO_POLL bit is set, the kernel
13907c478bd9Sstevel@tonic-gate 		 * should copyout their result_t to user memory. by copying
13917c478bd9Sstevel@tonic-gate 		 * out the result_t, the user can poll on memory waiting
13927c478bd9Sstevel@tonic-gate 		 * for the kaio request to complete.
13937c478bd9Sstevel@tonic-gate 		 */
13947c478bd9Sstevel@tonic-gate 		if (p->p_aio)
13957c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
13967c478bd9Sstevel@tonic-gate 		/*
13977c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
13987c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
13997c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
14007c478bd9Sstevel@tonic-gate 		 *
14017c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
14027c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
14037c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
14047c478bd9Sstevel@tonic-gate 		 */
14057c478bd9Sstevel@tonic-gate 		if (ISHOLD(p))
14067c478bd9Sstevel@tonic-gate 			holdlwp();
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		/*
14097c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG evaluate true must
14107c478bd9Sstevel@tonic-gate 		 * set t_astflag afterwards.
14117c478bd9Sstevel@tonic-gate 		 */
1412ae115bc7Smrj 		if (ISSIG_PENDING(ct, lwp, p)) {
14137c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
14147c478bd9Sstevel@tonic-gate 				psig();
1415ae115bc7Smrj 			ct->t_sig_check = 1;
14167c478bd9Sstevel@tonic-gate 		}
14177c478bd9Sstevel@tonic-gate 
1418ae115bc7Smrj 		if (ct->t_rprof != NULL) {
1419e0cf54a5SRoger A. Faulkner 			realsigprof(0, 0, 0);
1420ae115bc7Smrj 			ct->t_sig_check = 1;
14217c478bd9Sstevel@tonic-gate 		}
142265a89a64Smarx 
142365a89a64Smarx 		/*
142465a89a64Smarx 		 * /proc can't enable/disable the trace bit itself
142565a89a64Smarx 		 * because that could race with the call gate used by
142665a89a64Smarx 		 * system calls via "lcall". If that happened, an
142765a89a64Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
142865a89a64Smarx 		 * therefore schedule an AST for the purpose.
142965a89a64Smarx 		 */
143065a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
143165a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
143265a89a64Smarx 			rp->r_ps |= PS_T;
143365a89a64Smarx 		}
143465a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
143565a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
143665a89a64Smarx 			rp->r_ps &= ~PS_T;
143765a89a64Smarx 		}
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate out:	/* We can't get here from a system trap */
14417c478bd9Sstevel@tonic-gate 	ASSERT(type & USER);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if (ISHOLD(p))
14447c478bd9Sstevel@tonic-gate 		holdlwp();
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/*
14477c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
14487c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
14497c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
14507c478bd9Sstevel@tonic-gate 	 *
14517c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
14527c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
14537c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
14547c478bd9Sstevel@tonic-gate 	 */
14557c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
14567c478bd9Sstevel@tonic-gate 
1457ae115bc7Smrj 	if (ct->t_trapret) {
1458ae115bc7Smrj 		ct->t_trapret = 0;
1459ae115bc7Smrj 		thread_lock(ct);
1460ae115bc7Smrj 		CL_TRAPRET(ct);
1461ae115bc7Smrj 		thread_unlock(ct);
14627c478bd9Sstevel@tonic-gate 	}
1463c97ad5cdSakolb 	if (CPU->cpu_runrun || curthread->t_schedflag & TS_ANYWAITQ)
14647c478bd9Sstevel@tonic-gate 		preempt();
146507a48826SRoger A. Faulkner 	prunstop();
1466ae115bc7Smrj 	(void) new_mstate(ct, mstate);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	return;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate cleanup:	/* system traps end up here */
14717c478bd9Sstevel@tonic-gate 	ASSERT(!(type & USER));
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * Patch non-zero to disable preemption of threads in the kernel.
14767c478bd9Sstevel@tonic-gate  */
14777c478bd9Sstevel@tonic-gate int IGNORE_KERNEL_PREEMPTION = 0;	/* XXX - delete this someday */
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate struct kpreempt_cnts {		/* kernel preemption statistics */
14807c478bd9Sstevel@tonic-gate 	int	kpc_idle;	/* executing idle thread */
14817c478bd9Sstevel@tonic-gate 	int	kpc_intr;	/* executing interrupt thread */
14827c478bd9Sstevel@tonic-gate 	int	kpc_clock;	/* executing clock thread */
14837c478bd9Sstevel@tonic-gate 	int	kpc_blocked;	/* thread has blocked preemption (t_preempt) */
14847c478bd9Sstevel@tonic-gate 	int	kpc_notonproc;	/* thread is surrendering processor */
14857c478bd9Sstevel@tonic-gate 	int	kpc_inswtch;	/* thread has ratified scheduling decision */
14867c478bd9Sstevel@tonic-gate 	int	kpc_prilevel;	/* processor interrupt level is too high */
14877c478bd9Sstevel@tonic-gate 	int	kpc_apreempt;	/* asynchronous preemption */
14887c478bd9Sstevel@tonic-gate 	int	kpc_spreempt;	/* synchronous preemption */
14897c478bd9Sstevel@tonic-gate } kpreempt_cnts;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * kernel preemption: forced rescheduling, preempt the running kernel thread.
14937c478bd9Sstevel@tonic-gate  *	the argument is old PIL for an interrupt,
14947c478bd9Sstevel@tonic-gate  *	or the distingished value KPREEMPT_SYNC.
14957c478bd9Sstevel@tonic-gate  */
14967c478bd9Sstevel@tonic-gate void
kpreempt(int asyncspl)14977c478bd9Sstevel@tonic-gate kpreempt(int asyncspl)
14987c478bd9Sstevel@tonic-gate {
1499ae115bc7Smrj 	kthread_t *ct = curthread;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	if (IGNORE_KERNEL_PREEMPTION) {
15027c478bd9Sstevel@tonic-gate 		aston(CPU->cpu_dispthread);
15037c478bd9Sstevel@tonic-gate 		return;
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	/*
15077c478bd9Sstevel@tonic-gate 	 * Check that conditions are right for kernel preemption
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate 	do {
1510ae115bc7Smrj 		if (ct->t_preempt) {
15117c478bd9Sstevel@tonic-gate 			/*
15127c478bd9Sstevel@tonic-gate 			 * either a privileged thread (idle, panic, interrupt)
15137c478bd9Sstevel@tonic-gate 			 * or will check when t_preempt is lowered
1514b6d5e9b6SPramod Batni 			 * We need to specifically handle the case where
1515b6d5e9b6SPramod Batni 			 * the thread is in the middle of swtch (resume has
1516b6d5e9b6SPramod Batni 			 * been called) and has its t_preempt set
1517b6d5e9b6SPramod Batni 			 * [idle thread and a thread which is in kpreempt
1518b6d5e9b6SPramod Batni 			 * already] and then a high priority thread is
1519b6d5e9b6SPramod Batni 			 * available in the local dispatch queue.
1520b6d5e9b6SPramod Batni 			 * In this case the resumed thread needs to take a
1521b6d5e9b6SPramod Batni 			 * trap so that it can call kpreempt. We achieve
1522b6d5e9b6SPramod Batni 			 * this by using siron().
1523b6d5e9b6SPramod Batni 			 * How do we detect this condition:
1524b6d5e9b6SPramod Batni 			 * idle thread is running and is in the midst of
1525b6d5e9b6SPramod Batni 			 * resume: curthread->t_pri == -1 && CPU->dispthread
1526b6d5e9b6SPramod Batni 			 * != CPU->thread
1527b6d5e9b6SPramod Batni 			 * Need to ensure that this happens only at high pil
1528b6d5e9b6SPramod Batni 			 * resume is called at high pil
1529b6d5e9b6SPramod Batni 			 * Only in resume_from_idle is the pil changed.
15307c478bd9Sstevel@tonic-gate 			 */
1531b6d5e9b6SPramod Batni 			if (ct->t_pri < 0) {
15327c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_idle++;
1533b6d5e9b6SPramod Batni 				if (CPU->cpu_dispthread != CPU->cpu_thread)
1534b6d5e9b6SPramod Batni 					siron();
1535b6d5e9b6SPramod Batni 			} else if (ct->t_flag & T_INTR_THREAD) {
15367c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_intr++;
1537ae115bc7Smrj 				if (ct->t_pil == CLOCK_LEVEL)
15387c478bd9Sstevel@tonic-gate 					kpreempt_cnts.kpc_clock++;
1539b6d5e9b6SPramod Batni 			} else {
15407c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_blocked++;
1541b6d5e9b6SPramod Batni 				if (CPU->cpu_dispthread != CPU->cpu_thread)
1542b6d5e9b6SPramod Batni 					siron();
1543b6d5e9b6SPramod Batni 			}
15447c478bd9Sstevel@tonic-gate 			aston(CPU->cpu_dispthread);
15457c478bd9Sstevel@tonic-gate 			return;
15467c478bd9Sstevel@tonic-gate 		}
1547ae115bc7Smrj 		if (ct->t_state != TS_ONPROC ||
1548ae115bc7Smrj 		    ct->t_disp_queue != CPU->cpu_disp) {
15497c478bd9Sstevel@tonic-gate 			/* this thread will be calling swtch() shortly */
15507c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_notonproc++;
15517c478bd9Sstevel@tonic-gate 			if (CPU->cpu_thread != CPU->cpu_dispthread) {
15527c478bd9Sstevel@tonic-gate 				/* already in swtch(), force another */
15537c478bd9Sstevel@tonic-gate 				kpreempt_cnts.kpc_inswtch++;
15547c478bd9Sstevel@tonic-gate 				siron();
15557c478bd9Sstevel@tonic-gate 			}
15567c478bd9Sstevel@tonic-gate 			return;
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 		if (getpil() >= DISP_LEVEL) {
15597c478bd9Sstevel@tonic-gate 			/*
15607c478bd9Sstevel@tonic-gate 			 * We can't preempt this thread if it is at
15617c478bd9Sstevel@tonic-gate 			 * a PIL >= DISP_LEVEL since it may be holding
15627c478bd9Sstevel@tonic-gate 			 * a spin lock (like sched_lock).
15637c478bd9Sstevel@tonic-gate 			 */
15647c478bd9Sstevel@tonic-gate 			siron();	/* check back later */
15657c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_prilevel++;
15667c478bd9Sstevel@tonic-gate 			return;
15677c478bd9Sstevel@tonic-gate 		}
1568ae115bc7Smrj 		if (!interrupts_enabled()) {
1569ae115bc7Smrj 			/*
1570ae115bc7Smrj 			 * Can't preempt while running with ints disabled
1571ae115bc7Smrj 			 */
1572ae115bc7Smrj 			kpreempt_cnts.kpc_prilevel++;
1573ae115bc7Smrj 			return;
1574ae115bc7Smrj 		}
15757c478bd9Sstevel@tonic-gate 		if (asyncspl != KPREEMPT_SYNC)
15767c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_apreempt++;
15777c478bd9Sstevel@tonic-gate 		else
15787c478bd9Sstevel@tonic-gate 			kpreempt_cnts.kpc_spreempt++;
15797c478bd9Sstevel@tonic-gate 
1580ae115bc7Smrj 		ct->t_preempt++;
15817c478bd9Sstevel@tonic-gate 		preempt();
1582ae115bc7Smrj 		ct->t_preempt--;
15837c478bd9Sstevel@tonic-gate 	} while (CPU->cpu_kprunrun);
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate /*
15877c478bd9Sstevel@tonic-gate  * Print out debugging info.
15887c478bd9Sstevel@tonic-gate  */
15897c478bd9Sstevel@tonic-gate static void
showregs(uint_t type,struct regs * rp,caddr_t addr)15907c478bd9Sstevel@tonic-gate showregs(uint_t type, struct regs *rp, caddr_t addr)
15917c478bd9Sstevel@tonic-gate {
15927c478bd9Sstevel@tonic-gate 	int s;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	s = spl7();
15957c478bd9Sstevel@tonic-gate 	type &= ~USER;
1596ae115bc7Smrj 	if (PTOU(curproc)->u_comm[0])
1597ae115bc7Smrj 		printf("%s: ", PTOU(curproc)->u_comm);
15987c478bd9Sstevel@tonic-gate 	if (type < TRAP_TYPES)
15997c478bd9Sstevel@tonic-gate 		printf("#%s %s\n", trap_type_mnemonic[type], trap_type[type]);
16007c478bd9Sstevel@tonic-gate 	else
16017c478bd9Sstevel@tonic-gate 		switch (type) {
16027c478bd9Sstevel@tonic-gate 		case T_SYSCALL:
16037c478bd9Sstevel@tonic-gate 			printf("Syscall Trap:\n");
16047c478bd9Sstevel@tonic-gate 			break;
16057c478bd9Sstevel@tonic-gate 		case T_AST:
16067c478bd9Sstevel@tonic-gate 			printf("AST\n");
16077c478bd9Sstevel@tonic-gate 			break;
16087c478bd9Sstevel@tonic-gate 		default:
16097c478bd9Sstevel@tonic-gate 			printf("Bad Trap = %d\n", type);
16107c478bd9Sstevel@tonic-gate 			break;
16117c478bd9Sstevel@tonic-gate 		}
16127c478bd9Sstevel@tonic-gate 	if (type == T_PGFLT) {
16137c478bd9Sstevel@tonic-gate 		printf("Bad %s fault at addr=0x%lx\n",
16147c478bd9Sstevel@tonic-gate 		    USERMODE(rp->r_cs) ? "user": "kernel", (uintptr_t)addr);
16157c478bd9Sstevel@tonic-gate 	} else if (addr) {
16167c478bd9Sstevel@tonic-gate 		printf("addr=0x%lx\n", (uintptr_t)addr);
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	printf("pid=%d, pc=0x%lx, sp=0x%lx, eflags=0x%lx\n",
16207c478bd9Sstevel@tonic-gate 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
16217c478bd9Sstevel@tonic-gate 	    ttoproc(curthread)->p_pid : 0, rp->r_pc, rp->r_sp, rp->r_ps);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate #if defined(__lint)
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * this clause can be deleted when lint bug 4870403 is fixed
16267c478bd9Sstevel@tonic-gate 	 * (lint thinks that bit 32 is illegal in a %b format string)
16277c478bd9Sstevel@tonic-gate 	 */
16287c478bd9Sstevel@tonic-gate 	printf("cr0: %x  cr4: %b\n",
16297c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), (uint_t)getcr4(), FMT_CR4);
16307c478bd9Sstevel@tonic-gate #else
16317c478bd9Sstevel@tonic-gate 	printf("cr0: %b  cr4: %b\n",
16327c478bd9Sstevel@tonic-gate 	    (uint_t)getcr0(), FMT_CR0, (uint_t)getcr4(), FMT_CR4);
1633ae115bc7Smrj #endif	/* __lint */
16347c478bd9Sstevel@tonic-gate 
1635843e1988Sjohnlev 	printf("cr2: %lx  ", getcr2());
1636843e1988Sjohnlev #if !defined(__xpv)
1637843e1988Sjohnlev 	printf("cr3: %lx  ", getcr3());
1638843e1988Sjohnlev 	printf("cr8: %lx\n", getcr8());
16397c478bd9Sstevel@tonic-gate #endif
1640843e1988Sjohnlev 	printf("\n");
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	dumpregs(rp);
16437c478bd9Sstevel@tonic-gate 	splx(s);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate static void
dumpregs(struct regs * rp)16477c478bd9Sstevel@tonic-gate dumpregs(struct regs *rp)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	const char fmt[] = "\t%3s: %16lx %3s: %16lx %3s: %16lx\n";
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	printf(fmt, "rdi", rp->r_rdi, "rsi", rp->r_rsi, "rdx", rp->r_rdx);
16527c478bd9Sstevel@tonic-gate 	printf(fmt, "rcx", rp->r_rcx, " r8", rp->r_r8, " r9", rp->r_r9);
16537c478bd9Sstevel@tonic-gate 	printf(fmt, "rax", rp->r_rax, "rbx", rp->r_rbx, "rbp", rp->r_rbp);
16547c478bd9Sstevel@tonic-gate 	printf(fmt, "r10", rp->r_r10, "r11", rp->r_r11, "r12", rp->r_r12);
16557c478bd9Sstevel@tonic-gate 	printf(fmt, "r13", rp->r_r13, "r14", rp->r_r14, "r15", rp->r_r15);
16567c478bd9Sstevel@tonic-gate 
1657ae115bc7Smrj 	printf(fmt, "fsb", rdmsr(MSR_AMD_FSBASE), "gsb", rdmsr(MSR_AMD_GSBASE),
1658ae115bc7Smrj 	    " ds", rp->r_ds);
16597c478bd9Sstevel@tonic-gate 	printf(fmt, " es", rp->r_es, " fs", rp->r_fs, " gs", rp->r_gs);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	printf(fmt, "trp", rp->r_trapno, "err", rp->r_err, "rip", rp->r_rip);
16627c478bd9Sstevel@tonic-gate 	printf(fmt, " cs", rp->r_cs, "rfl", rp->r_rfl, "rsp", rp->r_rsp);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	printf("\t%3s: %16lx\n", " ss", rp->r_ss);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate /*
1669843e1988Sjohnlev  * Test to see if the instruction is iret on i386 or iretq on amd64.
1670843e1988Sjohnlev  *
1671843e1988Sjohnlev  * On the hypervisor we can only test for nopop_sys_rtt_syscall. If true
1672843e1988Sjohnlev  * then we are in the context of hypervisor's failsafe handler because it
1673843e1988Sjohnlev  * tried to iret and failed due to a bad selector. See xen_failsafe_callback.
1674843e1988Sjohnlev  */
1675843e1988Sjohnlev static int
instr_is_iret(caddr_t pc)1676843e1988Sjohnlev instr_is_iret(caddr_t pc)
1677843e1988Sjohnlev {
1678843e1988Sjohnlev 
1679843e1988Sjohnlev #if defined(__xpv)
1680843e1988Sjohnlev 	extern void nopop_sys_rtt_syscall(void);
1681843e1988Sjohnlev 	return ((pc == (caddr_t)nopop_sys_rtt_syscall) ? 1 : 0);
1682843e1988Sjohnlev 
1683843e1988Sjohnlev #else
1684843e1988Sjohnlev 
1685843e1988Sjohnlev 	static const uint8_t iret_insn[2] = { 0x48, 0xcf };	/* iretq */
1686843e1988Sjohnlev 
1687843e1988Sjohnlev 	return (bcmp(pc, iret_insn, sizeof (iret_insn)) == 0);
1688843e1988Sjohnlev 
1689843e1988Sjohnlev #endif	/* __xpv */
1690843e1988Sjohnlev }
1691843e1988Sjohnlev 
1692843e1988Sjohnlev 
1693843e1988Sjohnlev /*
169474ecdb51SJohn Levon  * Test to see if the instruction is part of _sys_rtt (or the KPTI trampolines
169574ecdb51SJohn Levon  * which are used by _sys_rtt).
1696843e1988Sjohnlev  *
1697843e1988Sjohnlev  * Again on the hypervisor if we try to IRET to user land with a bad code
1698843e1988Sjohnlev  * or stack selector we will get vectored through xen_failsafe_callback.
1699843e1988Sjohnlev  * In which case we assume we got here via _sys_rtt since we only allow
1700843e1988Sjohnlev  * IRET to user land to take place in _sys_rtt.
1701843e1988Sjohnlev  */
1702843e1988Sjohnlev static int
instr_is_sys_rtt(caddr_t pc)1703843e1988Sjohnlev instr_is_sys_rtt(caddr_t pc)
1704843e1988Sjohnlev {
1705843e1988Sjohnlev 	extern void _sys_rtt(), _sys_rtt_end();
1706843e1988Sjohnlev 
170774ecdb51SJohn Levon #if !defined(__xpv)
170874ecdb51SJohn Levon 	extern void tr_sysc_ret_start(), tr_sysc_ret_end();
170974ecdb51SJohn Levon 	extern void tr_intr_ret_start(), tr_intr_ret_end();
171074ecdb51SJohn Levon 
171174ecdb51SJohn Levon 	if ((uintptr_t)pc >= (uintptr_t)tr_sysc_ret_start &&
171274ecdb51SJohn Levon 	    (uintptr_t)pc <= (uintptr_t)tr_sysc_ret_end)
171374ecdb51SJohn Levon 		return (1);
171474ecdb51SJohn Levon 
171574ecdb51SJohn Levon 	if ((uintptr_t)pc >= (uintptr_t)tr_intr_ret_start &&
171674ecdb51SJohn Levon 	    (uintptr_t)pc <= (uintptr_t)tr_intr_ret_end)
171774ecdb51SJohn Levon 		return (1);
171874ecdb51SJohn Levon #endif
171974ecdb51SJohn Levon 
1720843e1988Sjohnlev 	if ((uintptr_t)pc < (uintptr_t)_sys_rtt ||
1721843e1988Sjohnlev 	    (uintptr_t)pc > (uintptr_t)_sys_rtt_end)
1722843e1988Sjohnlev 		return (0);
1723843e1988Sjohnlev 
1724843e1988Sjohnlev 	return (1);
1725843e1988Sjohnlev }
1726843e1988Sjohnlev 
1727843e1988Sjohnlev /*
17287c478bd9Sstevel@tonic-gate  * Handle #gp faults in kernel mode.
17297c478bd9Sstevel@tonic-gate  *
17307c478bd9Sstevel@tonic-gate  * One legitimate way this can happen is if we attempt to update segment
17317c478bd9Sstevel@tonic-gate  * registers to naughty values on the way out of the kernel.
17327c478bd9Sstevel@tonic-gate  *
17337c478bd9Sstevel@tonic-gate  * This can happen in a couple of ways: someone - either accidentally or
17347c478bd9Sstevel@tonic-gate  * on purpose - creates (setcontext(2), lwp_create(2)) or modifies
17357c478bd9Sstevel@tonic-gate  * (signal(2)) a ucontext that contains silly segment register values.
17367c478bd9Sstevel@tonic-gate  * Or someone - either accidentally or on purpose - modifies the prgregset_t
17377c478bd9Sstevel@tonic-gate  * of a subject process via /proc to contain silly segment register values.
17387c478bd9Sstevel@tonic-gate  *
17397c478bd9Sstevel@tonic-gate  * (The unfortunate part is that we can end up discovering the bad segment
17407c478bd9Sstevel@tonic-gate  * register value in the middle of an 'iret' after we've popped most of the
17417c478bd9Sstevel@tonic-gate  * stack.  So it becomes quite difficult to associate an accurate ucontext
17427c478bd9Sstevel@tonic-gate  * with the lwp, because the act of taking the #gp trap overwrites most of
17437c478bd9Sstevel@tonic-gate  * what we were going to send the lwp.)
17447c478bd9Sstevel@tonic-gate  *
17457c478bd9Sstevel@tonic-gate  * OTOH if it turns out that's -not- the problem, and we're -not- an lwp
17467c478bd9Sstevel@tonic-gate  * trying to return to user mode and we get a #gp fault, then we need
17477c478bd9Sstevel@tonic-gate  * to die() -- which will happen if we return non-zero from this routine.
17487c478bd9Sstevel@tonic-gate  */
17497c478bd9Sstevel@tonic-gate static int
kern_gpfault(struct regs * rp)17507c478bd9Sstevel@tonic-gate kern_gpfault(struct regs *rp)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
17537c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
17547c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
17557c478bd9Sstevel@tonic-gate 	struct regs tmpregs, *trp = NULL;
17567c478bd9Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
17577c478bd9Sstevel@tonic-gate 	int v;
1758005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/*
1761843e1988Sjohnlev 	 * if we're not an lwp, or in the case of running native the
1762843e1988Sjohnlev 	 * pc range is outside _sys_rtt, then we should immediately
1763843e1988Sjohnlev 	 * be die()ing horribly.
17647c478bd9Sstevel@tonic-gate 	 */
1765843e1988Sjohnlev 	if (lwp == NULL || !instr_is_sys_rtt(pc))
17667c478bd9Sstevel@tonic-gate 		return (1);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * So at least we're in the right part of the kernel.
17707c478bd9Sstevel@tonic-gate 	 *
17717c478bd9Sstevel@tonic-gate 	 * Disassemble the instruction at the faulting pc.
17727c478bd9Sstevel@tonic-gate 	 * Once we know what it is, we carefully reconstruct the stack
17737c478bd9Sstevel@tonic-gate 	 * based on the order in which the stack is deconstructed in
17747c478bd9Sstevel@tonic-gate 	 * _sys_rtt. Ew.
17757c478bd9Sstevel@tonic-gate 	 */
1776843e1988Sjohnlev 	if (instr_is_iret(pc)) {
17777c478bd9Sstevel@tonic-gate 		/*
1778843e1988Sjohnlev 		 * We took the #gp while trying to perform the IRET.
17797c478bd9Sstevel@tonic-gate 		 * This means that either %cs or %ss are bad.
17807c478bd9Sstevel@tonic-gate 		 * All we know for sure is that most of the general
17817c478bd9Sstevel@tonic-gate 		 * registers have been restored, including the
17827c478bd9Sstevel@tonic-gate 		 * segment registers, and all we have left on the
17837c478bd9Sstevel@tonic-gate 		 * topmost part of the lwp's stack are the
17847c478bd9Sstevel@tonic-gate 		 * registers that the iretq was unable to consume.
17857c478bd9Sstevel@tonic-gate 		 *
17867c478bd9Sstevel@tonic-gate 		 * All the rest of the state was crushed by the #gp
17877c478bd9Sstevel@tonic-gate 		 * which pushed -its- registers atop our old save area
17887c478bd9Sstevel@tonic-gate 		 * (because we had to decrement the stack pointer, sigh) so
17897c478bd9Sstevel@tonic-gate 		 * all that we can try and do is to reconstruct the
17907c478bd9Sstevel@tonic-gate 		 * crushed frame from the #gp trap frame itself.
17917c478bd9Sstevel@tonic-gate 		 */
17927c478bd9Sstevel@tonic-gate 		trp = &tmpregs;
17937c478bd9Sstevel@tonic-gate 		trp->r_ss = lwptoregs(lwp)->r_ss;
17947c478bd9Sstevel@tonic-gate 		trp->r_sp = lwptoregs(lwp)->r_sp;
17957c478bd9Sstevel@tonic-gate 		trp->r_ps = lwptoregs(lwp)->r_ps;
17967c478bd9Sstevel@tonic-gate 		trp->r_cs = lwptoregs(lwp)->r_cs;
17977c478bd9Sstevel@tonic-gate 		trp->r_pc = lwptoregs(lwp)->r_pc;
17987c478bd9Sstevel@tonic-gate 		bcopy(rp, trp, offsetof(struct regs, r_pc));
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		/*
18017c478bd9Sstevel@tonic-gate 		 * Validate simple math
18027c478bd9Sstevel@tonic-gate 		 */
18037c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_pc == lwptoregs(lwp)->r_pc);
18047c478bd9Sstevel@tonic-gate 		ASSERT(trp->r_err == rp->r_err);
18057c478bd9Sstevel@tonic-gate 
1806843e1988Sjohnlev 
1807843e1988Sjohnlev 
1808843e1988Sjohnlev 	}
1809843e1988Sjohnlev 
18104c28a617SRobert Mustacchi 	if (trp == NULL && PCB_NEED_UPDATE_SEGS(&lwp->lwp_pcb)) {
1811843e1988Sjohnlev 
1812843e1988Sjohnlev 		/*
1813843e1988Sjohnlev 		 * This is the common case -- we're trying to load
1814843e1988Sjohnlev 		 * a bad segment register value in the only section
1815843e1988Sjohnlev 		 * of kernel code that ever loads segment registers.
1816843e1988Sjohnlev 		 *
1817843e1988Sjohnlev 		 * We don't need to do anything at this point because
1818843e1988Sjohnlev 		 * the pcb contains all the pending segment register
1819843e1988Sjohnlev 		 * state, and the regs are still intact because we
1820843e1988Sjohnlev 		 * didn't adjust the stack pointer yet.  Given the fidelity
1821843e1988Sjohnlev 		 * of all this, we could conceivably send a signal
1822843e1988Sjohnlev 		 * to the lwp, rather than core-ing.
1823843e1988Sjohnlev 		 */
1824843e1988Sjohnlev 		trp = lwptoregs(lwp);
1825843e1988Sjohnlev 		ASSERT((caddr_t)trp == (caddr_t)rp->r_sp);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (trp == NULL)
18297c478bd9Sstevel@tonic-gate 		return (1);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * If we get to here, we're reasonably confident that we've
18337c478bd9Sstevel@tonic-gate 	 * correctly decoded what happened on the way out of the kernel.
18347c478bd9Sstevel@tonic-gate 	 * Rewrite the lwp's registers so that we can create a core dump
18357c478bd9Sstevel@tonic-gate 	 * the (at least vaguely) represents the mcontext we were
18367c478bd9Sstevel@tonic-gate 	 * being asked to restore when things went so terribly wrong.
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * Make sure that we have a meaningful %trapno and %err.
18417c478bd9Sstevel@tonic-gate 	 */
18427c478bd9Sstevel@tonic-gate 	trp->r_trapno = rp->r_trapno;
18437c478bd9Sstevel@tonic-gate 	trp->r_err = rp->r_err;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if ((caddr_t)trp != (caddr_t)lwptoregs(lwp))
18467c478bd9Sstevel@tonic-gate 		bcopy(trp, lwptoregs(lwp), sizeof (*trp));
18477c478bd9Sstevel@tonic-gate 
1848843e1988Sjohnlev 
18497c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
18507c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = SIGSEGV;
18517c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	/*
1854843e1988Sjohnlev 	 * Terminate all LWPs but don't discard them.  If another lwp beat
1855843e1988Sjohnlev 	 * us to the punch by calling exit(), evaporate now.
18567c478bd9Sstevel@tonic-gate 	 */
185797eda132Sraf 	proc_is_exiting(p);
18587c478bd9Sstevel@tonic-gate 	if (exitlwps(1) != 0) {
18597c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
18607c478bd9Sstevel@tonic-gate 		lwp_exit();
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 
1863005d3febSMarek Pospisil 	if (auditing)		/* audit core dump */
18647c478bd9Sstevel@tonic-gate 		audit_core_start(SIGSEGV);
18657c478bd9Sstevel@tonic-gate 	v = core(SIGSEGV, B_FALSE);
1866005d3febSMarek Pospisil 	if (auditing)		/* audit core dump */
18677c478bd9Sstevel@tonic-gate 		audit_core_finish(v ? CLD_KILLED : CLD_DUMPED);
18687c478bd9Sstevel@tonic-gate 	exit(v ? CLD_KILLED : CLD_DUMPED, SIGSEGV);
18697c478bd9Sstevel@tonic-gate 	return (0);
18707c478bd9Sstevel@tonic-gate }
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * dump_tss() - Display the TSS structure
18747c478bd9Sstevel@tonic-gate  */
18757c478bd9Sstevel@tonic-gate 
1876843e1988Sjohnlev #if !defined(__xpv)
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate static void
dump_tss(void)18797c478bd9Sstevel@tonic-gate dump_tss(void)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	const char tss_fmt[] = "tss.%s:\t0x%p\n";  /* Format string */
1882f16a0f4cSRobert Mustacchi 	tss_t *tss = CPU->cpu_tss;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp0", (void *)tss->tss_rsp0);
18857c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp1", (void *)tss->tss_rsp1);
18867c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_rsp2", (void *)tss->tss_rsp2);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist1", (void *)tss->tss_ist1);
18897c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist2", (void *)tss->tss_ist2);
18907c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist3", (void *)tss->tss_ist3);
18917c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist4", (void *)tss->tss_ist4);
18927c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist5", (void *)tss->tss_ist5);
18937c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist6", (void *)tss->tss_ist6);
18947c478bd9Sstevel@tonic-gate 	printf(tss_fmt, "tss_ist7", (void *)tss->tss_ist7);
18957c478bd9Sstevel@tonic-gate }
18967c478bd9Sstevel@tonic-gate 
1897843e1988Sjohnlev #endif	/* !__xpv */
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
19007c478bd9Sstevel@tonic-gate 
1901843e1988Sjohnlev int ttrace_nrec = 10;		/* number of records to dump out */
1902843e1988Sjohnlev int ttrace_dump_nregs = 0;	/* dump out this many records with regs too */
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate /*
19057c478bd9Sstevel@tonic-gate  * Dump out the last ttrace_nrec traptrace records on each CPU
19067c478bd9Sstevel@tonic-gate  */
19077c478bd9Sstevel@tonic-gate static void
dump_ttrace(void)19087c478bd9Sstevel@tonic-gate dump_ttrace(void)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc;
19117c478bd9Sstevel@tonic-gate 	trap_trace_rec_t *rec;
19127c478bd9Sstevel@tonic-gate 	uintptr_t current;
19133df2e8b2SRobert Mustacchi 	int i, j;
19147c478bd9Sstevel@tonic-gate 	int n = NCPU;
19157c478bd9Sstevel@tonic-gate 	const char banner[] =
1916905e0868SYuri Pankov 	    "CPU          ADDRESS    TIMESTAMP TYPE  VC HANDLER          PC\n";
1917905e0868SYuri Pankov 	/* Define format for the CPU, ADDRESS, and TIMESTAMP fields */
19187c478bd9Sstevel@tonic-gate 	const char fmt1[] = "%3d %016lx %12llx";
1919905e0868SYuri Pankov 	char data1[34];	/* length of string formatted by fmt1 + 1 */
1920905e0868SYuri Pankov 	/* Define format for the TYPE and VC fields */
19217c478bd9Sstevel@tonic-gate 	const char fmt2[] = "%4s %3x";
192262722731SHans Rosenfeld 	const char fmt2s[] = "%4s %3s";
1923905e0868SYuri Pankov 	char data2[9];	/* length of string formatted by fmt2 + 1 */
1924905e0868SYuri Pankov 	/*
1925905e0868SYuri Pankov 	 * Define format for the HANDLER field. Width is arbitrary, but should
1926905e0868SYuri Pankov 	 * be enough for common handler's names, and leave enough space for
1927905e0868SYuri Pankov 	 * the PC field, especially when we are in kmdb.
1928905e0868SYuri Pankov 	 */
1929905e0868SYuri Pankov 	const char fmt3h[] = "#%-15s";
1930905e0868SYuri Pankov 	const char fmt3p[] = "%-16p";
1931905e0868SYuri Pankov 	const char fmt3s[] = "%-16s";
1932905e0868SYuri Pankov 	char data3[17];	/* length of string formatted by fmt3* + 1 */
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (ttrace_nrec == 0)
19357c478bd9Sstevel@tonic-gate 		return;
19367c478bd9Sstevel@tonic-gate 
1937905e0868SYuri Pankov 	printf("\n");
19387c478bd9Sstevel@tonic-gate 	printf(banner);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19417c478bd9Sstevel@tonic-gate 		ttc = &trap_trace_ctl[i];
19428f22c1dfSToomas Soome 		if (ttc->ttc_first == (uintptr_t)NULL)
19437c478bd9Sstevel@tonic-gate 			continue;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 		current = ttc->ttc_next - sizeof (trap_trace_rec_t);
19467c478bd9Sstevel@tonic-gate 		for (j = 0; j < ttrace_nrec; j++) {
19477c478bd9Sstevel@tonic-gate 			struct sysent	*sys;
19487c478bd9Sstevel@tonic-gate 			struct autovec	*vec;
19497c478bd9Sstevel@tonic-gate 			extern struct av_head autovect[];
19507c478bd9Sstevel@tonic-gate 			int type;
19517c478bd9Sstevel@tonic-gate 			ulong_t	off;
19527c478bd9Sstevel@tonic-gate 			char *sym, *stype;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 			if (current < ttc->ttc_first)
19557c478bd9Sstevel@tonic-gate 				current =
19567c478bd9Sstevel@tonic-gate 				    ttc->ttc_limit - sizeof (trap_trace_rec_t);
19577c478bd9Sstevel@tonic-gate 
19588f22c1dfSToomas Soome 			if (current == (uintptr_t)NULL)
19597c478bd9Sstevel@tonic-gate 				continue;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 			rec = (trap_trace_rec_t *)current;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 			if (rec->ttr_stamp == 0)
19647c478bd9Sstevel@tonic-gate 				break;
19657c478bd9Sstevel@tonic-gate 
1966905e0868SYuri Pankov 			(void) snprintf(data1, sizeof (data1), fmt1, i,
1967905e0868SYuri Pankov 			    (uintptr_t)rec, rec->ttr_stamp);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 			switch (rec->ttr_marker) {
19707c478bd9Sstevel@tonic-gate 			case TT_SYSCALL:
19717c478bd9Sstevel@tonic-gate 			case TT_SYSENTER:
19727c478bd9Sstevel@tonic-gate 			case TT_SYSC:
19737c478bd9Sstevel@tonic-gate 			case TT_SYSC64:
19747c478bd9Sstevel@tonic-gate 				sys = &sysent32[rec->ttr_sysnum];
19757c478bd9Sstevel@tonic-gate 				switch (rec->ttr_marker) {
19767c478bd9Sstevel@tonic-gate 				case TT_SYSC64:
19777c478bd9Sstevel@tonic-gate 					sys = &sysent[rec->ttr_sysnum];
19787c478bd9Sstevel@tonic-gate 					/* FALLTHROUGH */
19797c478bd9Sstevel@tonic-gate 				case TT_SYSC:
19807c478bd9Sstevel@tonic-gate 					stype = "sysc";	/* syscall */
19817c478bd9Sstevel@tonic-gate 					break;
19827c478bd9Sstevel@tonic-gate 				case TT_SYSCALL:
19837c478bd9Sstevel@tonic-gate 					stype = "lcal";	/* lcall */
19847c478bd9Sstevel@tonic-gate 					break;
19857c478bd9Sstevel@tonic-gate 				case TT_SYSENTER:
19867c478bd9Sstevel@tonic-gate 					stype = "syse";	/* sysenter */
19877c478bd9Sstevel@tonic-gate 					break;
19887c478bd9Sstevel@tonic-gate 				default:
19893df2e8b2SRobert Mustacchi 					stype = "";
19907c478bd9Sstevel@tonic-gate 					break;
19917c478bd9Sstevel@tonic-gate 				}
1992905e0868SYuri Pankov 				(void) snprintf(data2, sizeof (data2), fmt2,
1993905e0868SYuri Pankov 				    stype, rec->ttr_sysnum);
19947c478bd9Sstevel@tonic-gate 				if (sys != NULL) {
19957c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
19967c478bd9Sstevel@tonic-gate 					    (uintptr_t)sys->sy_callc,
19977c478bd9Sstevel@tonic-gate 					    &off);
1998905e0868SYuri Pankov 					if (sym != NULL) {
1999905e0868SYuri Pankov 						(void) snprintf(data3,
2000905e0868SYuri Pankov 						    sizeof (data3), fmt3s, sym);
20017c478bd9Sstevel@tonic-gate 					} else {
2002905e0868SYuri Pankov 						(void) snprintf(data3,
2003905e0868SYuri Pankov 						    sizeof (data3), fmt3p,
2004905e0868SYuri Pankov 						    sys->sy_callc);
2005905e0868SYuri Pankov 					}
2006905e0868SYuri Pankov 				} else {
2007905e0868SYuri Pankov 					(void) snprintf(data3, sizeof (data3),
2008905e0868SYuri Pankov 					    fmt3s, "unknown");
20097c478bd9Sstevel@tonic-gate 				}
20107c478bd9Sstevel@tonic-gate 				break;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 			case TT_INTERRUPT:
201362722731SHans Rosenfeld 				if (rec->ttr_regs.r_trapno == T_SOFTINT) {
201462722731SHans Rosenfeld 					(void) snprintf(data2, sizeof (data2),
201562722731SHans Rosenfeld 					    fmt2s, "intr", "-");
201662722731SHans Rosenfeld 					(void) snprintf(data3, sizeof (data3),
201762722731SHans Rosenfeld 					    fmt3s, "(fakesoftint)");
201862722731SHans Rosenfeld 					break;
201962722731SHans Rosenfeld 				}
202062722731SHans Rosenfeld 
2021905e0868SYuri Pankov 				(void) snprintf(data2, sizeof (data2), fmt2,
2022905e0868SYuri Pankov 				    "intr", rec->ttr_vector);
20237ff178cdSJimmy Vetayases 				if (get_intr_handler != NULL)
20247ff178cdSJimmy Vetayases 					vec = (struct autovec *)
20257ff178cdSJimmy Vetayases 					    (*get_intr_handler)
20267ff178cdSJimmy Vetayases 					    (rec->ttr_cpuid, rec->ttr_vector);
20277ff178cdSJimmy Vetayases 				else
20287ff178cdSJimmy Vetayases 					vec =
20297ff178cdSJimmy Vetayases 					    autovect[rec->ttr_vector].avh_link;
20307ff178cdSJimmy Vetayases 
20317c478bd9Sstevel@tonic-gate 				if (vec != NULL) {
20327c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(
20337c478bd9Sstevel@tonic-gate 					    (uintptr_t)vec->av_vector, &off);
2034905e0868SYuri Pankov 					if (sym != NULL) {
2035905e0868SYuri Pankov 						(void) snprintf(data3,
2036905e0868SYuri Pankov 						    sizeof (data3), fmt3s, sym);
20377c478bd9Sstevel@tonic-gate 					} else {
2038905e0868SYuri Pankov 						(void) snprintf(data3,
2039905e0868SYuri Pankov 						    sizeof (data3), fmt3p,
2040905e0868SYuri Pankov 						    vec->av_vector);
2041905e0868SYuri Pankov 					}
2042905e0868SYuri Pankov 				} else {
2043905e0868SYuri Pankov 					(void) snprintf(data3, sizeof (data3),
2044905e0868SYuri Pankov 					    fmt3s, "unknown");
20457c478bd9Sstevel@tonic-gate 				}
20467c478bd9Sstevel@tonic-gate 				break;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 			case TT_TRAP:
2049ae115bc7Smrj 			case TT_EVENT:
20507c478bd9Sstevel@tonic-gate 				type = rec->ttr_regs.r_trapno;
2051905e0868SYuri Pankov 				(void) snprintf(data2, sizeof (data2), fmt2,
2052905e0868SYuri Pankov 				    "trap", type);
2053905e0868SYuri Pankov 				if (type < TRAP_TYPES) {
2054905e0868SYuri Pankov 					(void) snprintf(data3, sizeof (data3),
2055905e0868SYuri Pankov 					    fmt3h, trap_type_mnemonic[type]);
2056905e0868SYuri Pankov 				} else {
2057ae115bc7Smrj 					switch (type) {
2058ae115bc7Smrj 					case T_AST:
2059905e0868SYuri Pankov 						(void) snprintf(data3,
2060905e0868SYuri Pankov 						    sizeof (data3), fmt3s,
2061905e0868SYuri Pankov 						    "ast");
2062ae115bc7Smrj 						break;
2063ae115bc7Smrj 					default:
2064905e0868SYuri Pankov 						(void) snprintf(data3,
2065905e0868SYuri Pankov 						    sizeof (data3), fmt3s, "");
2066ae115bc7Smrj 						break;
2067ae115bc7Smrj 					}
2068905e0868SYuri Pankov 				}
2069ae115bc7Smrj 				break;
2070ae115bc7Smrj 
20717c478bd9Sstevel@tonic-gate 			default:
20727c478bd9Sstevel@tonic-gate 				break;
20737c478bd9Sstevel@tonic-gate 			}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(rec->ttr_regs.r_pc, &off);
2076905e0868SYuri Pankov 			if (sym != NULL) {
2077905e0868SYuri Pankov 				printf("%s %s %s %s+%lx\n", data1, data2, data3,
2078905e0868SYuri Pankov 				    sym, off);
2079905e0868SYuri Pankov 			} else {
2080905e0868SYuri Pankov 				printf("%s %s %s %lx\n", data1, data2, data3,
2081905e0868SYuri Pankov 				    rec->ttr_regs.r_pc);
2082905e0868SYuri Pankov 			}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 			if (ttrace_dump_nregs-- > 0) {
20857c478bd9Sstevel@tonic-gate 				int s;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 				if (rec->ttr_marker == TT_INTERRUPT)
20887c478bd9Sstevel@tonic-gate 					printf(
20897c478bd9Sstevel@tonic-gate 					    "\t\tipl %x spl %x pri %x\n",
20907c478bd9Sstevel@tonic-gate 					    rec->ttr_ipl,
20917c478bd9Sstevel@tonic-gate 					    rec->ttr_spl,
20927c478bd9Sstevel@tonic-gate 					    rec->ttr_pri);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 				dumpregs(&rec->ttr_regs);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 				printf("\t%3s: %p\n\n", " ct",
20977c478bd9Sstevel@tonic-gate 				    (void *)rec->ttr_curthread);
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 				/*
21007c478bd9Sstevel@tonic-gate 				 * print out the pc stack that we recorded
21017c478bd9Sstevel@tonic-gate 				 * at trap time (if any)
21027c478bd9Sstevel@tonic-gate 				 */
21037c478bd9Sstevel@tonic-gate 				for (s = 0; s < rec->ttr_sdepth; s++) {
21047c478bd9Sstevel@tonic-gate 					uintptr_t fullpc;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 					if (s >= TTR_STACK_DEPTH) {
21077c478bd9Sstevel@tonic-gate 						printf("ttr_sdepth corrupt\n");
21087c478bd9Sstevel@tonic-gate 						break;
21097c478bd9Sstevel@tonic-gate 					}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 					fullpc = (uintptr_t)rec->ttr_stack[s];
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 					sym = kobj_getsymname(fullpc, &off);
21147c478bd9Sstevel@tonic-gate 					if (sym != NULL)
21157c478bd9Sstevel@tonic-gate 						printf("-> %s+0x%lx()\n",
21167c478bd9Sstevel@tonic-gate 						    sym, off);
21177c478bd9Sstevel@tonic-gate 					else
21187c478bd9Sstevel@tonic-gate 						printf("-> 0x%lx()\n", fullpc);
21197c478bd9Sstevel@tonic-gate 				}
21207c478bd9Sstevel@tonic-gate 				printf("\n");
21217c478bd9Sstevel@tonic-gate 			}
21227c478bd9Sstevel@tonic-gate 			current -= sizeof (trap_trace_rec_t);
21237c478bd9Sstevel@tonic-gate 		}
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate void
panic_showtrap(struct panic_trap_info * tip)2130843e1988Sjohnlev panic_showtrap(struct panic_trap_info *tip)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr);
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate #if defined(TRAPTRACE)
21357c478bd9Sstevel@tonic-gate 	dump_ttrace();
2136843e1988Sjohnlev #endif
21377c478bd9Sstevel@tonic-gate 
2138843e1988Sjohnlev #if !defined(__xpv)
21397c478bd9Sstevel@tonic-gate 	if (tip->trap_type == T_DBLFLT)
21407c478bd9Sstevel@tonic-gate 		dump_tss();
2141843e1988Sjohnlev #endif
21427c478bd9Sstevel@tonic-gate }
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate void
panic_savetrap(panic_data_t * pdp,struct panic_trap_info * tip)2145843e1988Sjohnlev panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
21467c478bd9Sstevel@tonic-gate {
21477c478bd9Sstevel@tonic-gate 	panic_saveregs(pdp, tip->trap_regs);
21487c478bd9Sstevel@tonic-gate }
2149