xref: /onnv-gate/usr/src/uts/sparc/dtrace/fasttrap_isa.c (revision 2179:a857f0fbafd0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51710Sahl  * Common Development and Distribution License (the "License").
61710Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211048Sraf 
220Sstevel@tonic-gate /*
231710Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
300Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
310Sstevel@tonic-gate #include <sys/dtrace.h>
320Sstevel@tonic-gate #include <sys/dtrace_impl.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/frame.h>
350Sstevel@tonic-gate #include <sys/stack.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/trap.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <v9/sys/machpcb.h>
400Sstevel@tonic-gate #include <v9/sys/privregs.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Lossless User-Land Tracing on SPARC
440Sstevel@tonic-gate  * -----------------------------------
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * The Basic Idea
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * The most important design constraint is, of course, correct execution of
490Sstevel@tonic-gate  * the user thread above all else. The next most important goal is rapid
500Sstevel@tonic-gate  * execution. We combine execution of instructions in user-land with
510Sstevel@tonic-gate  * emulation of certain instructions in the kernel to aim for complete
520Sstevel@tonic-gate  * correctness and maximal performance.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * We take advantage of the split PC/NPC architecture to speed up logical
550Sstevel@tonic-gate  * single-stepping; when we copy an instruction out to the scratch space in
560Sstevel@tonic-gate  * the ulwp_t structure (held in the %g7 register on SPARC), we can
570Sstevel@tonic-gate  * effectively single step by setting the PC to our scratch space and leaving
580Sstevel@tonic-gate  * the NPC alone. This executes the replaced instruction and then continues
590Sstevel@tonic-gate  * on without having to reenter the kernel as with single- stepping. The
600Sstevel@tonic-gate  * obvious caveat is for instructions whose execution is PC dependant --
610Sstevel@tonic-gate  * branches, call and link instructions (call and jmpl), and the rdpc
620Sstevel@tonic-gate  * instruction. These instructions cannot be executed in the manner described
630Sstevel@tonic-gate  * so they must be emulated in the kernel.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * Emulation for this small set of instructions if fairly simple; the most
660Sstevel@tonic-gate  * difficult part being emulating branch conditions.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * A Cache Heavy Portfolio
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * It's important to note at this time that copying an instruction out to the
720Sstevel@tonic-gate  * ulwp_t scratch space in user-land is rather complicated. SPARC has
730Sstevel@tonic-gate  * separate data and instruction caches so any writes to the D$ (using a
740Sstevel@tonic-gate  * store instruction for example) aren't necessarily reflected in the I$.
750Sstevel@tonic-gate  * The flush instruction can be used to synchronize the two and must be used
760Sstevel@tonic-gate  * for any self-modifying code, but the flush instruction only applies to the
770Sstevel@tonic-gate  * primary address space (the absence of a flusha analogue to the flush
780Sstevel@tonic-gate  * instruction that accepts an ASI argument is an obvious omission from SPARC
790Sstevel@tonic-gate  * v9 where the notion of the alternate address space was introduced on
800Sstevel@tonic-gate  * SPARC). To correctly copy out the instruction we must use a block store
810Sstevel@tonic-gate  * that doesn't allocate in the D$ and ensures synchronization with the I$;
820Sstevel@tonic-gate  * see dtrace_blksuword32() for the implementation  (this function uses
830Sstevel@tonic-gate  * ASI_BLK_COMMIT_S to write a block through the secondary ASI in the manner
840Sstevel@tonic-gate  * described). Refer to the UltraSPARC I/II manual for details on the
850Sstevel@tonic-gate  * ASI_BLK_COMMIT_S ASI.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * Return Subtleties
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  * When we're firing a return probe we need to expose the value returned by
910Sstevel@tonic-gate  * the function being traced. Since the function can set the return value
920Sstevel@tonic-gate  * in its last instruction, we need to fire the return probe only _after_
930Sstevel@tonic-gate  * the effects of the instruction are apparent. For instructions that we
940Sstevel@tonic-gate  * emulate, we can call dtrace_probe() after we've performed the emulation;
950Sstevel@tonic-gate  * for instructions that we execute after we return to user-land, we set
960Sstevel@tonic-gate  * %pc to the instruction we copied out (as described above) and set %npc
970Sstevel@tonic-gate  * to a trap instruction stashed in the ulwp_t structure. After the traced
980Sstevel@tonic-gate  * instruction is executed, the trap instruction returns control to the
990Sstevel@tonic-gate  * kernel where we can fire the return probe.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * This need for a second trap in cases where we execute the traced
1020Sstevel@tonic-gate  * instruction makes it all the more important to emulate the most common
1030Sstevel@tonic-gate  * instructions to avoid the second trip in and out of the kernel.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * Making it Fast
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * Since copying out an instruction is neither simple nor inexpensive for the
1090Sstevel@tonic-gate  * CPU, we should attempt to avoid doing it in as many cases as possible.
1100Sstevel@tonic-gate  * Since function entry and return are usually the most interesting probe
1110Sstevel@tonic-gate  * sites, we attempt to tune the performance of the fasttrap provider around
1120Sstevel@tonic-gate  * instructions typically in those places.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * Looking at a bunch of functions in libraries and executables reveals that
1150Sstevel@tonic-gate  * most functions begin with either a save or a sethi (to setup a larger
1160Sstevel@tonic-gate  * argument to the save) and end with a restore or an or (in the case of leaf
1170Sstevel@tonic-gate  * functions). To try to improve performance, we emulate all of these
1180Sstevel@tonic-gate  * instructions in the kernel.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * The save and restore instructions are a little tricky since they perform
1210Sstevel@tonic-gate  * register window maniplulation. Rather than trying to tinker with the
1220Sstevel@tonic-gate  * register windows from the kernel, we emulate the implicit add that takes
1230Sstevel@tonic-gate  * place as part of those instructions and set the %pc to point to a simple
1240Sstevel@tonic-gate  * save or restore we've hidden in the ulwp_t structure. If we're in a return
1250Sstevel@tonic-gate  * probe so want to make it seem as though the tracepoint has been completely
1260Sstevel@tonic-gate  * executed we need to remember that we've pulled this trick with restore and
1270Sstevel@tonic-gate  * pull registers from the previous window (the one that we'll switch to once
1280Sstevel@tonic-gate  * the simple store instruction is executed) rather than the current one. This
1290Sstevel@tonic-gate  * is why in the case of emulating a restore we set the DTrace CPU flag
1300Sstevel@tonic-gate  * CPU_DTRACE_FAKERESTORE before calling dtrace_probe() for the return probes
1310Sstevel@tonic-gate  * (see fasttrap_return_common()).
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #define	OP(x)		((x) >> 30)
1350Sstevel@tonic-gate #define	OP2(x)		(((x) >> 22) & 0x07)
1360Sstevel@tonic-gate #define	OP3(x)		(((x) >> 19) & 0x3f)
1370Sstevel@tonic-gate #define	RCOND(x)	(((x) >> 25) & 0x07)
1380Sstevel@tonic-gate #define	COND(x)		(((x) >> 25) & 0x0f)
1390Sstevel@tonic-gate #define	A(x)		(((x) >> 29) & 0x01)
1400Sstevel@tonic-gate #define	I(x)		(((x) >> 13) & 0x01)
1410Sstevel@tonic-gate #define	RD(x)		(((x) >> 25) & 0x1f)
1420Sstevel@tonic-gate #define	RS1(x)		(((x) >> 14) & 0x1f)
1430Sstevel@tonic-gate #define	RS2(x)		(((x) >> 0) & 0x1f)
1440Sstevel@tonic-gate #define	CC(x)		(((x) >> 20) & 0x03)
1450Sstevel@tonic-gate #define	DISP16(x)	((((x) >> 6) & 0xc000) | ((x) & 0x3fff))
1460Sstevel@tonic-gate #define	DISP22(x)	((x) & 0x3fffff)
1470Sstevel@tonic-gate #define	DISP19(x)	((x) & 0x7ffff)
1480Sstevel@tonic-gate #define	DISP30(x)	((x) & 0x3fffffff)
1490Sstevel@tonic-gate #define	SW_TRAP(x)	((x) & 0x7f)
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #define	OP3_OR		0x02
1520Sstevel@tonic-gate #define	OP3_RD		0x28
1530Sstevel@tonic-gate #define	OP3_JMPL	0x38
1540Sstevel@tonic-gate #define	OP3_RETURN	0x39
1550Sstevel@tonic-gate #define	OP3_TCC		0x3a
1560Sstevel@tonic-gate #define	OP3_SAVE	0x3c
1570Sstevel@tonic-gate #define	OP3_RESTORE	0x3d
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	OP3_PREFETCH	0x2d
1600Sstevel@tonic-gate #define	OP3_CASA	0x3c
1610Sstevel@tonic-gate #define	OP3_PREFETCHA	0x3d
1620Sstevel@tonic-gate #define	OP3_CASXA	0x3e
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate #define	OP2_ILLTRAP	0x0
1650Sstevel@tonic-gate #define	OP2_BPcc	0x1
1660Sstevel@tonic-gate #define	OP2_Bicc	0x2
1670Sstevel@tonic-gate #define	OP2_BPr		0x3
1680Sstevel@tonic-gate #define	OP2_SETHI	0x4
1690Sstevel@tonic-gate #define	OP2_FBPfcc	0x5
1700Sstevel@tonic-gate #define	OP2_FBfcc	0x6
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate #define	R_G0		0
1730Sstevel@tonic-gate #define	R_O0		8
1740Sstevel@tonic-gate #define	R_SP		14
1750Sstevel@tonic-gate #define	R_I0		24
1760Sstevel@tonic-gate #define	R_I1		25
1770Sstevel@tonic-gate #define	R_I2		26
1780Sstevel@tonic-gate #define	R_I3		27
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Check the comment in fasttrap.h when changing these offsets or adding
1820Sstevel@tonic-gate  * new instructions.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate #define	FASTTRAP_OFF_SAVE	64
1850Sstevel@tonic-gate #define	FASTTRAP_OFF_RESTORE	68
1860Sstevel@tonic-gate #define	FASTTRAP_OFF_FTRET	72
1870Sstevel@tonic-gate #define	FASTTRAP_OFF_RETURN	76
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #define	BREAKPOINT_INSTR	0x91d02001	/* ta 1 */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * Tunable to let users turn off the fancy save instruction optimization.
1930Sstevel@tonic-gate  * If a program is non-ABI compliant, there's a possibility that the save
1940Sstevel@tonic-gate  * instruction optimization could cause an error.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate int fasttrap_optimize_save = 1;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate static uint64_t
1990Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int argno)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	uint64_t value;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (argno < 6)
2040Sstevel@tonic-gate 		return ((&rp->r_o0)[argno]);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
2070Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2100Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_argd[argno]);
2110Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
2120Sstevel@tonic-gate 		    CPU_DTRACE_BADALIGN);
2130Sstevel@tonic-gate 	} else {
2140Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)rp->r_sp;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2170Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_argd[argno]);
2180Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
2190Sstevel@tonic-gate 		    CPU_DTRACE_BADALIGN);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (value);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
2260Sstevel@tonic-gate static void fasttrap_putreg(struct regs *, uint_t, ulong_t);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static void
2290Sstevel@tonic-gate fasttrap_usdt_args(fasttrap_probe_t *probe, struct regs *rp, int argc,
2300Sstevel@tonic-gate     uintptr_t *argv)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
2350Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
2360Sstevel@tonic-gate 		uintptr_t v;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		for (i = 0; i < cap; i++) {
2390Sstevel@tonic-gate 			x = probe->ftp_argmap[i];
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 			if (x < 6)
2420Sstevel@tonic-gate 				argv[i] = (&rp->r_o0)[x];
2430Sstevel@tonic-gate 			else if (fasttrap_fulword(&fr->fr_argd[x], &v) != 0)
2440Sstevel@tonic-gate 				argv[i] = 0;
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	} else {
2480Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)rp->r_sp;
2490Sstevel@tonic-gate 		uint32_t v;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 		for (i = 0; i < cap; i++) {
2520Sstevel@tonic-gate 			x = probe->ftp_argmap[i];
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 			if (x < 6)
2550Sstevel@tonic-gate 				argv[i] = (&rp->r_o0)[x];
2560Sstevel@tonic-gate 			else if (fasttrap_fuword32(&fr->fr_argd[x], &v) != 0)
2570Sstevel@tonic-gate 				argv[i] = 0;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	for (; i < argc; i++) {
2620Sstevel@tonic-gate 		argv[i] = 0;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static void
2670Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
2680Sstevel@tonic-gate     uint_t fake_restore)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
2710Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
2720Sstevel@tonic-gate 	fasttrap_id_t *id;
2730Sstevel@tonic-gate 	kmutex_t *pid_mtx;
2740Sstevel@tonic-gate 	dtrace_icookie_t cookie;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
2770Sstevel@tonic-gate 	mutex_enter(pid_mtx);
2780Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
2810Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
282532Sahl 		    !tp->ftt_proc->ftpc_defunct)
2830Sstevel@tonic-gate 			break;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Don't sweat it if we can't find the tracepoint again; unlike
2880Sstevel@tonic-gate 	 * when we're in fasttrap_pid_probe(), finding the tracepoint here
2890Sstevel@tonic-gate 	 * is not essential to the correct execution of the process.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	if (tp == NULL || tp->ftt_retids == NULL) {
2920Sstevel@tonic-gate 		mutex_exit(pid_mtx);
2930Sstevel@tonic-gate 		return;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
2970Sstevel@tonic-gate 		fasttrap_probe_t *probe = id->fti_probe;
2980Sstevel@tonic-gate 
2991710Sahl 		if (id->fti_ptype == DTFTP_POST_OFFSETS) {
3000Sstevel@tonic-gate 			if (probe->ftp_argmap == NULL) {
3010Sstevel@tonic-gate 				dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1,
3020Sstevel@tonic-gate 				    rp->r_o2, rp->r_o3, rp->r_o4);
3030Sstevel@tonic-gate 			} else {
3040Sstevel@tonic-gate 				uintptr_t t[5];
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 				fasttrap_usdt_args(probe, rp,
3070Sstevel@tonic-gate 				    sizeof (t) / sizeof (t[0]), t);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 				dtrace_probe(probe->ftp_id, t[0], t[1],
3100Sstevel@tonic-gate 				    t[2], t[3], t[4]);
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 			continue;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * If this is only a possible return point, we must
3170Sstevel@tonic-gate 		 * be looking at a potential tail call in leaf context.
3180Sstevel@tonic-gate 		 * If the %npc is still within this function, then we
3190Sstevel@tonic-gate 		 * must have misidentified a jmpl as a tail-call when it
3200Sstevel@tonic-gate 		 * is, in fact, part of a jump table. It would be nice to
3210Sstevel@tonic-gate 		 * remove this tracepoint, but this is neither the time
3220Sstevel@tonic-gate 		 * nor the place.
3230Sstevel@tonic-gate 		 */
3240Sstevel@tonic-gate 		if ((tp->ftt_flags & FASTTRAP_F_RETMAYBE) &&
3250Sstevel@tonic-gate 		    rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
3260Sstevel@tonic-gate 			continue;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		/*
3290Sstevel@tonic-gate 		 * It's possible for a function to branch to the delay slot
3300Sstevel@tonic-gate 		 * of an instruction that we've identified as a return site.
3310Sstevel@tonic-gate 		 * We can dectect this spurious return probe activation by
3320Sstevel@tonic-gate 		 * observing that in this case %npc will be %pc + 4 and %npc
3330Sstevel@tonic-gate 		 * will be inside the current function (unless the user is
3340Sstevel@tonic-gate 		 * doing _crazy_ instruction picking in which case there's
3350Sstevel@tonic-gate 		 * very little we can do). The second check is important
3360Sstevel@tonic-gate 		 * in case the last instructions of a function make a tail-
3370Sstevel@tonic-gate 		 * call to the function located immediately subsequent.
3380Sstevel@tonic-gate 		 */
3390Sstevel@tonic-gate 		if (rp->r_npc == rp->r_pc + 4 &&
3400Sstevel@tonic-gate 		    rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
3410Sstevel@tonic-gate 			continue;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * The first argument is the offset of return tracepoint
3450Sstevel@tonic-gate 		 * in the function; the remaining arguments are the return
3460Sstevel@tonic-gate 		 * values.
3470Sstevel@tonic-gate 		 *
3480Sstevel@tonic-gate 		 * If fake_restore is set, we need to pull the return values
3490Sstevel@tonic-gate 		 * out of the %i's rather than the %o's -- a little trickier.
3500Sstevel@tonic-gate 		 */
3510Sstevel@tonic-gate 		if (!fake_restore) {
3520Sstevel@tonic-gate 			dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
3530Sstevel@tonic-gate 			    rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3);
3540Sstevel@tonic-gate 		} else {
3550Sstevel@tonic-gate 			uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
3560Sstevel@tonic-gate 			uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
3570Sstevel@tonic-gate 			uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
3580Sstevel@tonic-gate 			uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 			cookie = dtrace_interrupt_disable();
3610Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
3620Sstevel@tonic-gate 			dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
3630Sstevel@tonic-gate 			    arg0, arg1, arg2, arg3);
3640Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
3650Sstevel@tonic-gate 			dtrace_interrupt_enable(cookie);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	mutex_exit(pid_mtx);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate int
3730Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	proc_t *p = curproc;
3760Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, tp_local;
3770Sstevel@tonic-gate 	fasttrap_id_t *id;
3780Sstevel@tonic-gate 	pid_t pid;
3790Sstevel@tonic-gate 	uintptr_t pc = rp->r_pc;
3800Sstevel@tonic-gate 	uintptr_t npc = rp->r_npc;
3810Sstevel@tonic-gate 	uintptr_t orig_pc = pc;
3820Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
3830Sstevel@tonic-gate 	kmutex_t *pid_mtx;
3841710Sahl 	uint_t fake_restore = 0, is_enabled = 0;
3850Sstevel@tonic-gate 	dtrace_icookie_t cookie;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * It's possible that a user (in a veritable orgy of bad planning)
3890Sstevel@tonic-gate 	 * could redirect this thread's flow of control before it reached the
3900Sstevel@tonic-gate 	 * return probe fasttrap. In this case we need to kill the process
3910Sstevel@tonic-gate 	 * since it's in a unrecoverable state.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	if (curthread->t_dtrace_step) {
3940Sstevel@tonic-gate 		ASSERT(curthread->t_dtrace_on);
3950Sstevel@tonic-gate 		fasttrap_sigtrap(p, curthread, pc);
3960Sstevel@tonic-gate 		return (0);
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/*
4000Sstevel@tonic-gate 	 * Clear all user tracing flags.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	curthread->t_dtrace_ft = 0;
4030Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
4040Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
4050Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
4060Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
4100Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
4110Sstevel@tonic-gate 	 * process: this one.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
4140Sstevel@tonic-gate 		p = p->p_parent;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	pid = p->p_pid;
4180Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
4190Sstevel@tonic-gate 	mutex_enter(pid_mtx);
4200Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * Lookup the tracepoint that the process just hit.
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
4260Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
427532Sahl 		    !tp->ftt_proc->ftpc_defunct)
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * If we couldn't find a matching tracepoint, either a tracepoint has
4330Sstevel@tonic-gate 	 * been inserted without using the pid<pid> ioctl interface (see
4340Sstevel@tonic-gate 	 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	if (tp == NULL) {
4370Sstevel@tonic-gate 		mutex_exit(pid_mtx);
4380Sstevel@tonic-gate 		return (-1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
4420Sstevel@tonic-gate 		fasttrap_probe_t *probe = id->fti_probe;
4431710Sahl 		int isentry = (id->fti_ptype == DTFTP_ENTRY);
4441710Sahl 
4451710Sahl 		if (id->fti_ptype == DTFTP_IS_ENABLED) {
4461710Sahl 			is_enabled = 1;
4471710Sahl 			continue;
4481710Sahl 		}
4491710Sahl 
4500Sstevel@tonic-gate 		/*
4510Sstevel@tonic-gate 		 * We note that this was an entry probe to help ustack() find
4520Sstevel@tonic-gate 		 * the first caller.
4530Sstevel@tonic-gate 		 */
4541710Sahl 		if (isentry) {
4550Sstevel@tonic-gate 			cookie = dtrace_interrupt_disable();
4560Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 		dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, rp->r_o2,
4590Sstevel@tonic-gate 		    rp->r_o3, rp->r_o4);
4600Sstevel@tonic-gate 		if (isentry) {
4610Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
4620Sstevel@tonic-gate 			dtrace_interrupt_enable(cookie);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * We're about to do a bunch of work so we cache a local copy of
4680Sstevel@tonic-gate 	 * the tracepoint to emulate the instruction, and then find the
4690Sstevel@tonic-gate 	 * tracepoint again later if we need to light up any return probes.
4700Sstevel@tonic-gate 	 */
4710Sstevel@tonic-gate 	tp_local = *tp;
4720Sstevel@tonic-gate 	mutex_exit(pid_mtx);
4730Sstevel@tonic-gate 	tp = &tp_local;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4761710Sahl 	 * If there's an is-enabled probe conntected to this tracepoint it
4771710Sahl 	 * means that there was a 'mov %g0, %o0' instruction that was placed
4781710Sahl 	 * there by DTrace when the binary was linked. As this probe is, in
4791710Sahl 	 * fact, enabled, we need to stuff 1 into %o0. Accordingly, we can
4801710Sahl 	 * bypass all the instruction emulation logic since we know the
4811710Sahl 	 * inevitable result. It's possible that a user could construct a
4821710Sahl 	 * scenario where the 'is-enabled' probe was on some other
4831710Sahl 	 * instruction, but that would be a rather exotic way to shoot oneself
4841710Sahl 	 * in the foot.
4851710Sahl 	 */
4861710Sahl 	if (is_enabled) {
4871710Sahl 		rp->r_o0 = 1;
4881710Sahl 		pc = rp->r_npc;
4891710Sahl 		npc = pc + 4;
4901710Sahl 		goto done;
4911710Sahl 	}
4921710Sahl 
4931710Sahl 	/*
4941710Sahl 	 * We emulate certain types of instructions to ensure correctness
4950Sstevel@tonic-gate 	 * (in the case of position dependent instructions) or optimize
4960Sstevel@tonic-gate 	 * common cases. The rest we have the thread execute back in user-
4970Sstevel@tonic-gate 	 * land.
4980Sstevel@tonic-gate 	 */
4990Sstevel@tonic-gate 	switch (tp->ftt_type) {
5000Sstevel@tonic-gate 	case FASTTRAP_T_SAVE:
5010Sstevel@tonic-gate 	{
5020Sstevel@tonic-gate 		int32_t imm;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 		/*
5050Sstevel@tonic-gate 		 * This an optimization to let us handle function entry
5060Sstevel@tonic-gate 		 * probes more efficiently. Many functions begin with a save
5070Sstevel@tonic-gate 		 * instruction that follows the pattern:
5080Sstevel@tonic-gate 		 *	save	%sp, <imm>, %sp
5090Sstevel@tonic-gate 		 *
5100Sstevel@tonic-gate 		 * Meanwhile, we've stashed the instruction:
5110Sstevel@tonic-gate 		 *	save	%g1, %g0, %sp
5120Sstevel@tonic-gate 		 *
5130Sstevel@tonic-gate 		 * off of %g7, so all we have to do is stick the right value
5140Sstevel@tonic-gate 		 * into %g1 and reset %pc to point to the instruction we've
5150Sstevel@tonic-gate 		 * cleverly hidden (%npc should not be touched).
5160Sstevel@tonic-gate 		 */
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		imm = tp->ftt_instr << 19;
5190Sstevel@tonic-gate 		imm >>= 19;
5200Sstevel@tonic-gate 		rp->r_g1 = rp->r_sp + imm;
5210Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_SAVE;
5220Sstevel@tonic-gate 		break;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	case FASTTRAP_T_RESTORE:
5260Sstevel@tonic-gate 	{
5270Sstevel@tonic-gate 		ulong_t value;
5280Sstevel@tonic-gate 		uint_t rd;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		/*
5310Sstevel@tonic-gate 		 * This is an optimization to let us handle function
5320Sstevel@tonic-gate 		 * return probes more efficiently. Most non-leaf functions
5330Sstevel@tonic-gate 		 * end with the sequence:
5340Sstevel@tonic-gate 		 *	ret
5350Sstevel@tonic-gate 		 *	restore	<reg>, <reg_or_imm>, %oX
5360Sstevel@tonic-gate 		 *
5370Sstevel@tonic-gate 		 * We've stashed the instruction:
5380Sstevel@tonic-gate 		 *	restore	%g0, %g0, %g0
5390Sstevel@tonic-gate 		 *
5400Sstevel@tonic-gate 		 * off of %g7 so we just need to place the correct value
5410Sstevel@tonic-gate 		 * in the right %i register (since after our fake-o
5420Sstevel@tonic-gate 		 * restore, the %i's will become the %o's) and set the %pc
5430Sstevel@tonic-gate 		 * to point to our hidden restore. We also set fake_restore to
5440Sstevel@tonic-gate 		 * let fasttrap_return_common() know that it will find the
5450Sstevel@tonic-gate 		 * return values in the %i's rather than the %o's.
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
5490Sstevel@tonic-gate 			int32_t imm;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
5520Sstevel@tonic-gate 			imm >>= 19;
5530Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
5540Sstevel@tonic-gate 		} else {
5550Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
5560Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		/*
5600Sstevel@tonic-gate 		 * Convert %o's to %i's; leave %g's as they are.
5610Sstevel@tonic-gate 		 */
5620Sstevel@tonic-gate 		rd = RD(tp->ftt_instr);
5630Sstevel@tonic-gate 		fasttrap_putreg(rp, ((rd & 0x18) == 0x8) ? rd + 16 : rd, value);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_RESTORE;
5660Sstevel@tonic-gate 		fake_restore = 1;
5670Sstevel@tonic-gate 		break;
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	case FASTTRAP_T_RETURN:
5710Sstevel@tonic-gate 	{
5720Sstevel@tonic-gate 		uintptr_t target;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 		/*
5750Sstevel@tonic-gate 		 * A return instruction is like a jmpl (without the link
5760Sstevel@tonic-gate 		 * part) that executes an implicit restore. We've stashed
5770Sstevel@tonic-gate 		 * the instruction:
5780Sstevel@tonic-gate 		 *	return %o0
5790Sstevel@tonic-gate 		 *
5800Sstevel@tonic-gate 		 * off of %g7 so we just need to place the target in %o0
5810Sstevel@tonic-gate 		 * and set the %pc to point to the stashed return instruction.
5820Sstevel@tonic-gate 		 * We use %o0 since that register disappears after the return
5830Sstevel@tonic-gate 		 * executes, erasing any evidence of this tampering.
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
5860Sstevel@tonic-gate 			int32_t imm;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
5890Sstevel@tonic-gate 			imm >>= 19;
5900Sstevel@tonic-gate 			target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
5910Sstevel@tonic-gate 		} else {
5920Sstevel@tonic-gate 			target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
5930Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		fasttrap_putreg(rp, R_O0, target);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_RETURN;
5990Sstevel@tonic-gate 		fake_restore = 1;
6000Sstevel@tonic-gate 		break;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	case FASTTRAP_T_OR:
6040Sstevel@tonic-gate 	{
6050Sstevel@tonic-gate 		ulong_t value;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
6080Sstevel@tonic-gate 			int32_t imm;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
6110Sstevel@tonic-gate 			imm >>= 19;
6120Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | imm;
6130Sstevel@tonic-gate 		} else {
6140Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) |
6150Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), value);
6190Sstevel@tonic-gate 		pc = rp->r_npc;
6200Sstevel@tonic-gate 		npc = pc + 4;
6210Sstevel@tonic-gate 		break;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	case FASTTRAP_T_SETHI:
6250Sstevel@tonic-gate 		if (RD(tp->ftt_instr) != R_G0) {
6260Sstevel@tonic-gate 			uint32_t imm32 = tp->ftt_instr << 10;
6270Sstevel@tonic-gate 			fasttrap_putreg(rp, RD(tp->ftt_instr), (ulong_t)imm32);
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 		pc = rp->r_npc;
6300Sstevel@tonic-gate 		npc = pc + 4;
6310Sstevel@tonic-gate 		break;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	case FASTTRAP_T_CCR:
6340Sstevel@tonic-gate 	{
6350Sstevel@tonic-gate 		uint_t c, v, z, n, taken;
6360Sstevel@tonic-gate 		uint_t ccr = rp->r_tstate >> TSTATE_CCR_SHIFT;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		if (tp->ftt_cc != 0)
6390Sstevel@tonic-gate 			ccr >>= 4;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		c = (ccr >> 0) & 1;
6420Sstevel@tonic-gate 		v = (ccr >> 1) & 1;
6430Sstevel@tonic-gate 		z = (ccr >> 2) & 1;
6440Sstevel@tonic-gate 		n = (ccr >> 3) & 1;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		switch (tp->ftt_code) {
6470Sstevel@tonic-gate 		case 0x0:	/* BN */
6480Sstevel@tonic-gate 			taken = 0;		break;
6490Sstevel@tonic-gate 		case 0x1:	/* BE */
6500Sstevel@tonic-gate 			taken = z;		break;
6510Sstevel@tonic-gate 		case 0x2:	/* BLE */
6520Sstevel@tonic-gate 			taken = z | (n ^ v);	break;
6530Sstevel@tonic-gate 		case 0x3:	/* BL */
6540Sstevel@tonic-gate 			taken = n ^ v;		break;
6550Sstevel@tonic-gate 		case 0x4:	/* BLEU */
6560Sstevel@tonic-gate 			taken = c | z;		break;
6570Sstevel@tonic-gate 		case 0x5:	/* BCS (BLU) */
6580Sstevel@tonic-gate 			taken = c;		break;
6590Sstevel@tonic-gate 		case 0x6:	/* BNEG */
6600Sstevel@tonic-gate 			taken = n;		break;
6610Sstevel@tonic-gate 		case 0x7:	/* BVS */
6620Sstevel@tonic-gate 			taken = v;		break;
6630Sstevel@tonic-gate 		case 0x8:	/* BA */
6640Sstevel@tonic-gate 			/*
6650Sstevel@tonic-gate 			 * We handle the BA case differently since the annul
6660Sstevel@tonic-gate 			 * bit means something slightly different.
6670Sstevel@tonic-gate 			 */
6680Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
6690Sstevel@tonic-gate 			taken = 1;		break;
6700Sstevel@tonic-gate 		case 0x9:	/* BNE */
6710Sstevel@tonic-gate 			taken = ~z;		break;
6720Sstevel@tonic-gate 		case 0xa:	/* BG */
6730Sstevel@tonic-gate 			taken = ~(z | (n ^ v));	break;
6740Sstevel@tonic-gate 		case 0xb:	/* BGE */
6750Sstevel@tonic-gate 			taken = ~(n ^ v);	break;
6760Sstevel@tonic-gate 		case 0xc:	/* BGU */
6770Sstevel@tonic-gate 			taken = ~(c | z);	break;
6780Sstevel@tonic-gate 		case 0xd:	/* BCC (BGEU) */
6790Sstevel@tonic-gate 			taken = ~c;		break;
6800Sstevel@tonic-gate 		case 0xe:	/* BPOS */
6810Sstevel@tonic-gate 			taken = ~n;		break;
6820Sstevel@tonic-gate 		case 0xf:	/* BVC */
6830Sstevel@tonic-gate 			taken = ~v;		break;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (taken & 1) {
6870Sstevel@tonic-gate 			pc = rp->r_npc;
6880Sstevel@tonic-gate 			npc = tp->ftt_dest;
6890Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
6900Sstevel@tonic-gate 			/*
6910Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
6920Sstevel@tonic-gate 			 * instruction in the delay slot.
6930Sstevel@tonic-gate 			 */
6940Sstevel@tonic-gate 			pc = rp->r_npc + 4;
6950Sstevel@tonic-gate 			npc = pc + 4;
6960Sstevel@tonic-gate 		} else {
6970Sstevel@tonic-gate 			pc = rp->r_npc;
6980Sstevel@tonic-gate 			npc = pc + 4;
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 		break;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	case FASTTRAP_T_FCC:
7040Sstevel@tonic-gate 	{
7050Sstevel@tonic-gate 		uint_t fcc;
7060Sstevel@tonic-gate 		uint_t taken;
7070Sstevel@tonic-gate 		uint64_t fsr;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		dtrace_getfsr(&fsr);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		if (tp->ftt_cc == 0) {
7120Sstevel@tonic-gate 			fcc = (fsr >> 10) & 0x3;
7130Sstevel@tonic-gate 		} else {
7140Sstevel@tonic-gate 			uint_t shift;
7150Sstevel@tonic-gate 			ASSERT(tp->ftt_cc <= 3);
7160Sstevel@tonic-gate 			shift = 30 + tp->ftt_cc * 2;
7170Sstevel@tonic-gate 			fcc = (fsr >> shift) & 0x3;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		switch (tp->ftt_code) {
7210Sstevel@tonic-gate 		case 0x0:	/* FBN */
7220Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|0|0);	break;
7230Sstevel@tonic-gate 		case 0x1:	/* FBNE */
7240Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|2|0);	break;
7250Sstevel@tonic-gate 		case 0x2:	/* FBLG */
7260Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|2|0);	break;
7270Sstevel@tonic-gate 		case 0x3:	/* FBUL */
7280Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|2|0);	break;
7290Sstevel@tonic-gate 		case 0x4:	/* FBL */
7300Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|2|0);	break;
7310Sstevel@tonic-gate 		case 0x5:	/* FBUG */
7320Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|0|0);	break;
7330Sstevel@tonic-gate 		case 0x6:	/* FBG */
7340Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|0|0);	break;
7350Sstevel@tonic-gate 		case 0x7:	/* FBU */
7360Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|0|0);	break;
7370Sstevel@tonic-gate 		case 0x8:	/* FBA */
7380Sstevel@tonic-gate 			/*
7390Sstevel@tonic-gate 			 * We handle the FBA case differently since the annul
7400Sstevel@tonic-gate 			 * bit means something slightly different.
7410Sstevel@tonic-gate 			 */
7420Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
7430Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|2|1);	break;
7440Sstevel@tonic-gate 		case 0x9:	/* FBE */
7450Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|0|1);	break;
7460Sstevel@tonic-gate 		case 0xa:	/* FBUE */
7470Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|0|1);	break;
7480Sstevel@tonic-gate 		case 0xb:	/* FBGE */
7490Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|0|1);	break;
7500Sstevel@tonic-gate 		case 0xc:	/* FBUGE */
7510Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|0|1);	break;
7520Sstevel@tonic-gate 		case 0xd:	/* FBLE */
7530Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|2|1);	break;
7540Sstevel@tonic-gate 		case 0xe:	/* FBULE */
7550Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|2|1);	break;
7560Sstevel@tonic-gate 		case 0xf:	/* FBO */
7570Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|2|1);	break;
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (taken) {
7610Sstevel@tonic-gate 			pc = rp->r_npc;
7620Sstevel@tonic-gate 			npc = tp->ftt_dest;
7630Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
7640Sstevel@tonic-gate 			/*
7650Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
7660Sstevel@tonic-gate 			 * instruction in the delay slot.
7670Sstevel@tonic-gate 			 */
7680Sstevel@tonic-gate 			pc = rp->r_npc + 4;
7690Sstevel@tonic-gate 			npc = pc + 4;
7700Sstevel@tonic-gate 		} else {
7710Sstevel@tonic-gate 			pc = rp->r_npc;
7720Sstevel@tonic-gate 			npc = pc + 4;
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 		break;
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	case FASTTRAP_T_REG:
7780Sstevel@tonic-gate 	{
7790Sstevel@tonic-gate 		uint64_t value;
7800Sstevel@tonic-gate 		uint_t taken;
7810Sstevel@tonic-gate 		uint_t reg = RS1(tp->ftt_instr);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		/*
7840Sstevel@tonic-gate 		 * An ILP32 process shouldn't be using a branch predicated on
7850Sstevel@tonic-gate 		 * an %i or an %l since it would violate the ABI. It's a
7860Sstevel@tonic-gate 		 * violation of the ABI because we can't ensure deterministic
7870Sstevel@tonic-gate 		 * behavior. We should have identified this case when we
7880Sstevel@tonic-gate 		 * enabled the probe.
7890Sstevel@tonic-gate 		 */
7900Sstevel@tonic-gate 		ASSERT(p->p_model == DATAMODEL_LP64 || reg < 16);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		value = fasttrap_getreg(rp, reg);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		switch (tp->ftt_code) {
7950Sstevel@tonic-gate 		case 0x1:	/* BRZ */
7960Sstevel@tonic-gate 			taken = (value == 0);	break;
7970Sstevel@tonic-gate 		case 0x2:	/* BRLEZ */
7980Sstevel@tonic-gate 			taken = (value <= 0);	break;
7990Sstevel@tonic-gate 		case 0x3:	/* BRLZ */
8000Sstevel@tonic-gate 			taken = (value < 0);	break;
8010Sstevel@tonic-gate 		case 0x5:	/* BRNZ */
8020Sstevel@tonic-gate 			taken = (value != 0);	break;
8030Sstevel@tonic-gate 		case 0x6:	/* BRGZ */
8040Sstevel@tonic-gate 			taken = (value > 0);	break;
8050Sstevel@tonic-gate 		case 0x7:	/* BRGEZ */
8060Sstevel@tonic-gate 			taken = (value <= 0);	break;
8070Sstevel@tonic-gate 		default:
8080Sstevel@tonic-gate 		case 0x0:
8090Sstevel@tonic-gate 		case 0x4:
8100Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		if (taken) {
8140Sstevel@tonic-gate 			pc = rp->r_npc;
8150Sstevel@tonic-gate 			npc = tp->ftt_dest;
8160Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
8170Sstevel@tonic-gate 			/*
8180Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
8190Sstevel@tonic-gate 			 * instruction in the delay slot.
8200Sstevel@tonic-gate 			 */
8210Sstevel@tonic-gate 			pc = rp->r_npc + 4;
8220Sstevel@tonic-gate 			npc = pc + 4;
8230Sstevel@tonic-gate 		} else {
8240Sstevel@tonic-gate 			pc = rp->r_npc;
8250Sstevel@tonic-gate 			npc = pc + 4;
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 		break;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	case FASTTRAP_T_ALWAYS:
8310Sstevel@tonic-gate 		/*
8320Sstevel@tonic-gate 		 * BAs, BA,As...
8330Sstevel@tonic-gate 		 */
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
8360Sstevel@tonic-gate 			/*
8370Sstevel@tonic-gate 			 * Annulled branch always instructions never execute
8380Sstevel@tonic-gate 			 * the instruction in the delay slot.
8390Sstevel@tonic-gate 			 */
8400Sstevel@tonic-gate 			pc = tp->ftt_dest;
8410Sstevel@tonic-gate 			npc = tp->ftt_dest + 4;
8420Sstevel@tonic-gate 		} else {
8430Sstevel@tonic-gate 			pc = rp->r_npc;
8440Sstevel@tonic-gate 			npc = tp->ftt_dest;
8450Sstevel@tonic-gate 		}
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	case FASTTRAP_T_RDPC:
8490Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
8500Sstevel@tonic-gate 		pc = rp->r_npc;
8510Sstevel@tonic-gate 		npc = pc + 4;
8520Sstevel@tonic-gate 		break;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	case FASTTRAP_T_CALL:
8550Sstevel@tonic-gate 		/*
8560Sstevel@tonic-gate 		 * It's a call _and_ link remember...
8570Sstevel@tonic-gate 		 */
8580Sstevel@tonic-gate 		rp->r_o7 = rp->r_pc;
8590Sstevel@tonic-gate 		pc = rp->r_npc;
8600Sstevel@tonic-gate 		npc = tp->ftt_dest;
8610Sstevel@tonic-gate 		break;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	case FASTTRAP_T_JMPL:
8640Sstevel@tonic-gate 		pc = rp->r_npc;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
8670Sstevel@tonic-gate 			uint_t rs1 = RS1(tp->ftt_instr);
8680Sstevel@tonic-gate 			int32_t imm;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
8710Sstevel@tonic-gate 			imm >>= 19;
8720Sstevel@tonic-gate 			npc = fasttrap_getreg(rp, rs1) + imm;
8730Sstevel@tonic-gate 		} else {
8740Sstevel@tonic-gate 			uint_t rs1 = RS1(tp->ftt_instr);
8750Sstevel@tonic-gate 			uint_t rs2 = RS2(tp->ftt_instr);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 			npc = fasttrap_getreg(rp, rs1) +
8780Sstevel@tonic-gate 			    fasttrap_getreg(rp, rs2);
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		/*
8820Sstevel@tonic-gate 		 * Do the link part of the jump-and-link instruction.
8830Sstevel@tonic-gate 		 */
8840Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		break;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	case FASTTRAP_T_COMMON:
8890Sstevel@tonic-gate 	{
8900Sstevel@tonic-gate 		curthread->t_dtrace_scrpc = rp->r_g7;
8910Sstevel@tonic-gate 		curthread->t_dtrace_astpc = rp->r_g7 + FASTTRAP_OFF_FTRET;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		/*
8940Sstevel@tonic-gate 		 * Copy the instruction to a reserved location in the
8950Sstevel@tonic-gate 		 * user-land thread structure, then set the PC to that
8960Sstevel@tonic-gate 		 * location and leave the NPC alone. We take pains to ensure
8970Sstevel@tonic-gate 		 * consistency in the instruction stream (See SPARC
8980Sstevel@tonic-gate 		 * Architecture Manual Version 9, sections 8.4.7, A.20, and
8990Sstevel@tonic-gate 		 * H.1.6; UltraSPARC I/II User's Manual, sections 3.1.1.1,
9000Sstevel@tonic-gate 		 * and 13.6.4) by using the ASI ASI_BLK_COMMIT_S to copy the
9010Sstevel@tonic-gate 		 * instruction into the user's address space without
9020Sstevel@tonic-gate 		 * bypassing the I$. There's no AS_USER version of this ASI
9030Sstevel@tonic-gate 		 * (as exist for other ASIs) so we use the lofault
9040Sstevel@tonic-gate 		 * mechanism to catch faults.
9050Sstevel@tonic-gate 		 */
9060Sstevel@tonic-gate 		if (dtrace_blksuword32(rp->r_g7, &tp->ftt_instr, 1) == -1) {
9070Sstevel@tonic-gate 			/*
9080Sstevel@tonic-gate 			 * If the copyout fails, then the process's state
9090Sstevel@tonic-gate 			 * is not consistent (the effects of the traced
9100Sstevel@tonic-gate 			 * instruction will never be seen). This process
9110Sstevel@tonic-gate 			 * cannot be allowed to continue execution.
9120Sstevel@tonic-gate 			 */
9130Sstevel@tonic-gate 			fasttrap_sigtrap(curproc, curthread, pc);
9140Sstevel@tonic-gate 			return (0);
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		curthread->t_dtrace_pc = pc;
9180Sstevel@tonic-gate 		curthread->t_dtrace_npc = npc;
9190Sstevel@tonic-gate 		curthread->t_dtrace_on = 1;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		pc = curthread->t_dtrace_scrpc;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		if (tp->ftt_retids != NULL) {
9240Sstevel@tonic-gate 			curthread->t_dtrace_step = 1;
9250Sstevel@tonic-gate 			curthread->t_dtrace_ret = 1;
9260Sstevel@tonic-gate 			npc = curthread->t_dtrace_astpc;
9270Sstevel@tonic-gate 		}
9280Sstevel@tonic-gate 		break;
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	default:
9320Sstevel@tonic-gate 		panic("fasttrap: mishandled an instruction");
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9360Sstevel@tonic-gate 	 * This bit me in the ass a couple of times, so lets toss this
9370Sstevel@tonic-gate 	 * in as a cursory sanity check.
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 	ASSERT(pc != rp->r_g7 + 4);
9400Sstevel@tonic-gate 	ASSERT(pc != rp->r_g7 + 8);
9410Sstevel@tonic-gate 
9421710Sahl done:
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * If there were no return probes when we first found the tracepoint,
9450Sstevel@tonic-gate 	 * we should feel no obligation to honor any return probes that were
9460Sstevel@tonic-gate 	 * subsequently enabled -- they'll just have to wait until the next
9470Sstevel@tonic-gate 	 * time around.
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	if (tp->ftt_retids != NULL) {
9500Sstevel@tonic-gate 		/*
9510Sstevel@tonic-gate 		 * We need to wait until the results of the instruction are
9520Sstevel@tonic-gate 		 * apparent before invoking any return probes. If this
9530Sstevel@tonic-gate 		 * instruction was emulated we can just call
9540Sstevel@tonic-gate 		 * fasttrap_return_common(); if it needs to be executed, we
9550Sstevel@tonic-gate 		 * need to wait until we return to the kernel.
9560Sstevel@tonic-gate 		 */
9570Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_COMMON) {
9580Sstevel@tonic-gate 			fasttrap_return_common(rp, orig_pc, pid, fake_restore);
9590Sstevel@tonic-gate 		} else {
9600Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_ret != 0);
9610Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_pc == orig_pc);
9620Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_scrpc == rp->r_g7);
9630Sstevel@tonic-gate 			ASSERT(npc == curthread->t_dtrace_astpc);
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	ASSERT(pc != 0);
9680Sstevel@tonic-gate 	rp->r_pc = pc;
9690Sstevel@tonic-gate 	rp->r_npc = npc;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	return (0);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate int
9750Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
9780Sstevel@tonic-gate 	pid_t pid;
9790Sstevel@tonic-gate 	uintptr_t pc = curthread->t_dtrace_pc;
9800Sstevel@tonic-gate 	uintptr_t npc = curthread->t_dtrace_npc;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
9830Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
9840Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
9850Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
9890Sstevel@tonic-gate 	 * parent. We know there's only one thread of control in such a
9900Sstevel@tonic-gate 	 * process: this one.
9910Sstevel@tonic-gate 	 */
9920Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
9930Sstevel@tonic-gate 		p = p->p_parent;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * We set the %pc and %npc to their values when the traced
9980Sstevel@tonic-gate 	 * instruction was initially executed so that it appears to
9990Sstevel@tonic-gate 	 * dtrace_probe() that we're on the original instruction, and so that
10000Sstevel@tonic-gate 	 * the user can't easily detect our complex web of lies.
10010Sstevel@tonic-gate 	 * dtrace_return_probe() (our caller) will correctly set %pc and %npc
10020Sstevel@tonic-gate 	 * after we return.
10030Sstevel@tonic-gate 	 */
10040Sstevel@tonic-gate 	rp->r_pc = pc;
10050Sstevel@tonic-gate 	rp->r_npc = npc;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	pid = p->p_pid;
10080Sstevel@tonic-gate 	fasttrap_return_common(rp, pc, pid, 0);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	return (0);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate int
10140Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
10150Sstevel@tonic-gate {
10160Sstevel@tonic-gate 	fasttrap_instr_t instr = FASTTRAP_INSTR;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (uwrite(p, &instr, 4, tp->ftt_pc) != 0)
10190Sstevel@tonic-gate 		return (-1);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	return (0);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate int
10250Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	fasttrap_instr_t instr;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * Distinguish between read or write failures and a changed
10310Sstevel@tonic-gate 	 * instruction.
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	if (uread(p, &instr, 4, tp->ftt_pc) != 0)
10340Sstevel@tonic-gate 		return (0);
10350Sstevel@tonic-gate 	if (instr != FASTTRAP_INSTR && instr != BREAKPOINT_INSTR)
10360Sstevel@tonic-gate 		return (0);
10370Sstevel@tonic-gate 	if (uwrite(p, &tp->ftt_instr, 4, tp->ftt_pc) != 0)
10380Sstevel@tonic-gate 		return (-1);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	return (0);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate int
10441710Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
10451710Sahl     fasttrap_probe_type_t type)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	uint32_t instr;
10480Sstevel@tonic-gate 	int32_t disp;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/*
10510Sstevel@tonic-gate 	 * Read the instruction at the given address out of the process's
10520Sstevel@tonic-gate 	 * address space. We don't have to worry about a debugger
10530Sstevel@tonic-gate 	 * changing this instruction before we overwrite it with our trap
10540Sstevel@tonic-gate 	 * instruction since P_PR_LOCK is set.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	if (uread(p, &instr, 4, pc) != 0)
10570Sstevel@tonic-gate 		return (-1);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * Decode the instruction to fill in the probe flags. We can have
10610Sstevel@tonic-gate 	 * the process execute most instructions on its own using a pc/npc
10620Sstevel@tonic-gate 	 * trick, but pc-relative control transfer present a problem since
10630Sstevel@tonic-gate 	 * we're relocating the instruction. We emulate these instructions
10640Sstevel@tonic-gate 	 * in the kernel. We assume a default type and over-write that as
10650Sstevel@tonic-gate 	 * needed.
10660Sstevel@tonic-gate 	 *
10670Sstevel@tonic-gate 	 * pc-relative instructions must be emulated for correctness;
10680Sstevel@tonic-gate 	 * other instructions (which represent a large set of commonly traced
10690Sstevel@tonic-gate 	 * instructions) are emulated or otherwise optimized for performance.
10700Sstevel@tonic-gate 	 */
10710Sstevel@tonic-gate 	tp->ftt_type = FASTTRAP_T_COMMON;
10720Sstevel@tonic-gate 	if (OP(instr) == 1) {
10730Sstevel@tonic-gate 		/*
10740Sstevel@tonic-gate 		 * Call instructions.
10750Sstevel@tonic-gate 		 */
10760Sstevel@tonic-gate 		tp->ftt_type = FASTTRAP_T_CALL;
10770Sstevel@tonic-gate 		disp = DISP30(instr) << 2;
10780Sstevel@tonic-gate 		tp->ftt_dest = pc + (intptr_t)disp;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	} else if (OP(instr) == 0) {
10810Sstevel@tonic-gate 		/*
10820Sstevel@tonic-gate 		 * Branch instructions.
10830Sstevel@tonic-gate 		 *
10840Sstevel@tonic-gate 		 * Unconditional branches need careful attention when they're
10850Sstevel@tonic-gate 		 * annulled: annulled unconditional branches never execute
10860Sstevel@tonic-gate 		 * the instruction in the delay slot.
10870Sstevel@tonic-gate 		 */
10880Sstevel@tonic-gate 		switch (OP2(instr)) {
10890Sstevel@tonic-gate 		case OP2_ILLTRAP:
10900Sstevel@tonic-gate 		case 0x7:
10910Sstevel@tonic-gate 			/*
10920Sstevel@tonic-gate 			 * The compiler may place an illtrap after a call to
10930Sstevel@tonic-gate 			 * a function that returns a structure. In the case of
10940Sstevel@tonic-gate 			 * a returned structure, the compiler places an illtrap
10950Sstevel@tonic-gate 			 * whose const22 field is the size of the returned
10960Sstevel@tonic-gate 			 * structure immediately following the delay slot of
10970Sstevel@tonic-gate 			 * the call. To stay out of the way, we refuse to
10980Sstevel@tonic-gate 			 * place tracepoints on top of illtrap instructions.
10990Sstevel@tonic-gate 			 *
11000Sstevel@tonic-gate 			 * This is one of the dumbest architectural decisions
11010Sstevel@tonic-gate 			 * I've ever had to work around.
11020Sstevel@tonic-gate 			 *
11030Sstevel@tonic-gate 			 * We also identify the only illegal op2 value (See
11040Sstevel@tonic-gate 			 * SPARC Architecture Manual Version 9, E.2 table 31).
11050Sstevel@tonic-gate 			 */
11060Sstevel@tonic-gate 			return (-1);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		case OP2_BPcc:
11090Sstevel@tonic-gate 			if (COND(instr) == 8) {
11100Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
11110Sstevel@tonic-gate 			} else {
11120Sstevel@tonic-gate 				/*
11130Sstevel@tonic-gate 				 * Check for an illegal instruction.
11140Sstevel@tonic-gate 				 */
11150Sstevel@tonic-gate 				if (CC(instr) & 1)
11160Sstevel@tonic-gate 					return (-1);
11170Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CCR;
11180Sstevel@tonic-gate 				tp->ftt_cc = CC(instr);
11190Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
11200Sstevel@tonic-gate 			}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 			if (A(instr) != 0)
11230Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 			disp = DISP19(instr);
11260Sstevel@tonic-gate 			disp <<= 13;
11270Sstevel@tonic-gate 			disp >>= 11;
11280Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
11290Sstevel@tonic-gate 			break;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		case OP2_Bicc:
11320Sstevel@tonic-gate 			if (COND(instr) == 8) {
11330Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
11340Sstevel@tonic-gate 			} else {
11350Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CCR;
11360Sstevel@tonic-gate 				tp->ftt_cc = 0;
11370Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
11380Sstevel@tonic-gate 			}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 			if (A(instr) != 0)
11410Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			disp = DISP22(instr);
11440Sstevel@tonic-gate 			disp <<= 10;
11450Sstevel@tonic-gate 			disp >>= 8;
11460Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
11470Sstevel@tonic-gate 			break;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		case OP2_BPr:
11500Sstevel@tonic-gate 			/*
11510Sstevel@tonic-gate 			 * Check for an illegal instruction.
11520Sstevel@tonic-gate 			 */
11530Sstevel@tonic-gate 			if ((RCOND(instr) & 3) == 0)
11540Sstevel@tonic-gate 				return (-1);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 			/*
11570Sstevel@tonic-gate 			 * It's a violation of the v8plus ABI to use a
11580Sstevel@tonic-gate 			 * register-predicated branch in a 32-bit app if
11590Sstevel@tonic-gate 			 * the register used is an %l or an %i (%gs and %os
11600Sstevel@tonic-gate 			 * are legit because they're not saved to the stack
11610Sstevel@tonic-gate 			 * in 32-bit words when we take a trap).
11620Sstevel@tonic-gate 			 */
11630Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_ILP32 && RS1(instr) >= 16)
11640Sstevel@tonic-gate 				return (-1);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_REG;
11670Sstevel@tonic-gate 			if (A(instr) != 0)
11680Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
11690Sstevel@tonic-gate 			disp = DISP16(instr);
11700Sstevel@tonic-gate 			disp <<= 16;
11710Sstevel@tonic-gate 			disp >>= 14;
11720Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
11730Sstevel@tonic-gate 			tp->ftt_code = RCOND(instr);
11740Sstevel@tonic-gate 			break;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		case OP2_SETHI:
11770Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_SETHI;
11780Sstevel@tonic-gate 			break;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 		case OP2_FBPfcc:
11810Sstevel@tonic-gate 			if (COND(instr) == 8) {
11820Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
11830Sstevel@tonic-gate 			} else {
11840Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_FCC;
11850Sstevel@tonic-gate 				tp->ftt_cc = CC(instr);
11860Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
11870Sstevel@tonic-gate 			}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 			if (A(instr) != 0)
11900Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			disp = DISP19(instr);
11930Sstevel@tonic-gate 			disp <<= 13;
11940Sstevel@tonic-gate 			disp >>= 11;
11950Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
11960Sstevel@tonic-gate 			break;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		case OP2_FBfcc:
11990Sstevel@tonic-gate 			if (COND(instr) == 8) {
12000Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
12010Sstevel@tonic-gate 			} else {
12020Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_FCC;
12030Sstevel@tonic-gate 				tp->ftt_cc = 0;
12040Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
12050Sstevel@tonic-gate 			}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 			if (A(instr) != 0)
12080Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 			disp = DISP22(instr);
12110Sstevel@tonic-gate 			disp <<= 10;
12120Sstevel@tonic-gate 			disp >>= 8;
12130Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
12140Sstevel@tonic-gate 			break;
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	} else if (OP(instr) == 2) {
12180Sstevel@tonic-gate 		switch (OP3(instr)) {
12190Sstevel@tonic-gate 		case OP3_RETURN:
12200Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RETURN;
12210Sstevel@tonic-gate 			break;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		case OP3_JMPL:
12240Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMPL;
12250Sstevel@tonic-gate 			break;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		case OP3_RD:
12280Sstevel@tonic-gate 			if (RS1(instr) == 5)
12290Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_RDPC;
12300Sstevel@tonic-gate 			break;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		case OP3_SAVE:
12330Sstevel@tonic-gate 			/*
12340Sstevel@tonic-gate 			 * We optimize for save instructions at function
12350Sstevel@tonic-gate 			 * entry; see the comment in fasttrap_pid_probe()
12360Sstevel@tonic-gate 			 * (near FASTTRAP_T_SAVE) for details.
12370Sstevel@tonic-gate 			 */
12380Sstevel@tonic-gate 			if (fasttrap_optimize_save != 0 &&
12391710Sahl 			    type == DTFTP_ENTRY &&
12400Sstevel@tonic-gate 			    I(instr) == 1 && RD(instr) == R_SP)
12410Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_SAVE;
12420Sstevel@tonic-gate 			break;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 		case OP3_RESTORE:
12450Sstevel@tonic-gate 			/*
12460Sstevel@tonic-gate 			 * We optimize restore instructions at function
12470Sstevel@tonic-gate 			 * return; see the comment in fasttrap_pid_probe()
12480Sstevel@tonic-gate 			 * (near FASTTRAP_T_RESTORE) for details.
12490Sstevel@tonic-gate 			 *
12500Sstevel@tonic-gate 			 * rd must be an %o or %g register.
12510Sstevel@tonic-gate 			 */
12520Sstevel@tonic-gate 			if ((RD(instr) & 0x10) == 0)
12530Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_RESTORE;
12540Sstevel@tonic-gate 			break;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		case OP3_OR:
12570Sstevel@tonic-gate 			/*
12580Sstevel@tonic-gate 			 * A large proportion of instructions in the delay
12590Sstevel@tonic-gate 			 * slot of retl instructions are or's so we emulate
12600Sstevel@tonic-gate 			 * these downstairs as an optimization.
12610Sstevel@tonic-gate 			 */
12620Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_OR;
12630Sstevel@tonic-gate 			break;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 		case OP3_TCC:
12660Sstevel@tonic-gate 			/*
12670Sstevel@tonic-gate 			 * Breakpoint instructions are effectively position-
12680Sstevel@tonic-gate 			 * dependent since the debugger uses the %pc value
12690Sstevel@tonic-gate 			 * to lookup which breakpoint was executed. As a
12700Sstevel@tonic-gate 			 * result, we can't actually instrument breakpoints.
12710Sstevel@tonic-gate 			 */
12720Sstevel@tonic-gate 			if (SW_TRAP(instr) == ST_BREAKPOINT)
12730Sstevel@tonic-gate 				return (-1);
12740Sstevel@tonic-gate 			break;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		case 0x19:
12770Sstevel@tonic-gate 		case 0x1d:
12780Sstevel@tonic-gate 		case 0x29:
12790Sstevel@tonic-gate 		case 0x33:
12800Sstevel@tonic-gate 		case 0x3f:
12810Sstevel@tonic-gate 			/*
12820Sstevel@tonic-gate 			 * Identify illegal instructions (See SPARC
12830Sstevel@tonic-gate 			 * Architecture Manual Version 9, E.2 table 32).
12840Sstevel@tonic-gate 			 */
12850Sstevel@tonic-gate 			return (-1);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 	} else if (OP(instr) == 3) {
12880Sstevel@tonic-gate 		uint32_t op3 = OP3(instr);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 		/*
12910Sstevel@tonic-gate 		 * Identify illegal instructions (See SPARC Architecture
12920Sstevel@tonic-gate 		 * Manual Version 9, E.2 table 33).
12930Sstevel@tonic-gate 		 */
12940Sstevel@tonic-gate 		if ((op3 & 0x28) == 0x28) {
12950Sstevel@tonic-gate 			if (op3 != OP3_PREFETCH && op3 != OP3_CASA &&
12960Sstevel@tonic-gate 			    op3 != OP3_PREFETCHA && op3 != OP3_CASXA)
12970Sstevel@tonic-gate 				return (-1);
12980Sstevel@tonic-gate 		} else {
12990Sstevel@tonic-gate 			if ((op3 & 0x0f) == 0x0c || (op3 & 0x3b) == 0x31)
13000Sstevel@tonic-gate 				return (-1);
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	tp->ftt_instr = instr;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * We don't know how this tracepoint is going to be used, but in case
13080Sstevel@tonic-gate 	 * it's used as part of a function return probe, we need to indicate
13090Sstevel@tonic-gate 	 * whether it's always a return site or only potentially a return
13100Sstevel@tonic-gate 	 * site. If it's part of a return probe, it's always going to be a
13110Sstevel@tonic-gate 	 * return from that function if it's a restore instruction or if
13120Sstevel@tonic-gate 	 * the previous instruction was a return. If we could reliably
13130Sstevel@tonic-gate 	 * distinguish jump tables from return sites, this wouldn't be
13140Sstevel@tonic-gate 	 * necessary.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	if (tp->ftt_type != FASTTRAP_T_RESTORE &&
13170Sstevel@tonic-gate 	    (uread(p, &instr, 4, pc - sizeof (instr)) != 0 ||
13180Sstevel@tonic-gate 	    !(OP(instr) == 2 && OP3(instr) == OP3_RETURN)))
13190Sstevel@tonic-gate 		tp->ftt_flags |= FASTTRAP_F_RETMAYBE;
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	return (0);
13220Sstevel@tonic-gate }
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate /*ARGSUSED*/
13250Sstevel@tonic-gate uint64_t
1326*2179Sahl fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1327*2179Sahl     int aframes)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate /*ARGSUSED*/
13330Sstevel@tonic-gate uint64_t
13340Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
13350Sstevel@tonic-gate     int aframes)
13360Sstevel@tonic-gate {
13370Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate static uint64_t fasttrap_getreg_fast_cnt;
13410Sstevel@tonic-gate static uint64_t fasttrap_getreg_mpcb_cnt;
13420Sstevel@tonic-gate static uint64_t fasttrap_getreg_slow_cnt;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate static ulong_t
13450Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
13460Sstevel@tonic-gate {
13470Sstevel@tonic-gate 	ulong_t value;
13480Sstevel@tonic-gate 	dtrace_icookie_t cookie;
13490Sstevel@tonic-gate 	struct machpcb *mpcb;
13500Sstevel@tonic-gate 	extern ulong_t dtrace_getreg_win(uint_t, uint_t);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * We have the %os and %gs in our struct regs, but if we need to
13540Sstevel@tonic-gate 	 * snag a %l or %i we need to go scrounging around in the process's
13550Sstevel@tonic-gate 	 * address space.
13560Sstevel@tonic-gate 	 */
13570Sstevel@tonic-gate 	if (reg == 0)
13580Sstevel@tonic-gate 		return (0);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	if (reg < 16)
13610Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/*
13640Sstevel@tonic-gate 	 * Before we look at the user's stack, we'll check the register
13650Sstevel@tonic-gate 	 * windows to see if the information we want is in there.
13660Sstevel@tonic-gate 	 */
13670Sstevel@tonic-gate 	cookie = dtrace_interrupt_disable();
13680Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
13690Sstevel@tonic-gate 		value = dtrace_getreg_win(reg, 1);
13700Sstevel@tonic-gate 		dtrace_interrupt_enable(cookie);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 		atomic_add_64(&fasttrap_getreg_fast_cnt, 1);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		return (value);
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 	dtrace_interrupt_enable(cookie);
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	/*
13790Sstevel@tonic-gate 	 * First check the machpcb structure to see if we've already read
13800Sstevel@tonic-gate 	 * in the register window we're looking for; if we haven't, (and
13810Sstevel@tonic-gate 	 * we probably haven't) try to copy in the value of the register.
13820Sstevel@tonic-gate 	 */
13830Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
13860Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
13890Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
13900Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
13910Sstevel@tonic-gate 			do {
13920Sstevel@tonic-gate 				i--;
13930Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
13940Sstevel@tonic-gate 					continue;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 				atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
13970Sstevel@tonic-gate 				return (rwin[i].rw_local[reg - 16]);
13980Sstevel@tonic-gate 			} while (i > 0);
13990Sstevel@tonic-gate 		}
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 		if (fasttrap_fulword(&fr->fr_local[reg - 16], &value) != 0)
14020Sstevel@tonic-gate 			goto err;
14030Sstevel@tonic-gate 	} else {
14041048Sraf 		struct frame32 *fr =
14051048Sraf 		    (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
14060Sstevel@tonic-gate 		uint32_t *v32 = (uint32_t *)&value;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
14090Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
14100Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
14110Sstevel@tonic-gate 			do {
14120Sstevel@tonic-gate 				i--;
14130Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
14140Sstevel@tonic-gate 					continue;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 				atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
14170Sstevel@tonic-gate 				return (rwin[i].rw_local[reg - 16]);
14180Sstevel@tonic-gate 			} while (i > 0);
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 		if (fasttrap_fuword32(&fr->fr_local[reg - 16], &v32[1]) != 0)
14220Sstevel@tonic-gate 			goto err;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 		v32[0] = 0;
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	atomic_add_64(&fasttrap_getreg_slow_cnt, 1);
14280Sstevel@tonic-gate 	return (value);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate err:
14310Sstevel@tonic-gate 	/*
14320Sstevel@tonic-gate 	 * If the copy in failed, the process will be in a irrecoverable
14330Sstevel@tonic-gate 	 * state, and we have no choice but to kill it.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	psignal(ttoproc(curthread), SIGILL);
14360Sstevel@tonic-gate 	return (0);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate static uint64_t fasttrap_putreg_fast_cnt;
14400Sstevel@tonic-gate static uint64_t fasttrap_putreg_mpcb_cnt;
14410Sstevel@tonic-gate static uint64_t fasttrap_putreg_slow_cnt;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate static void
14440Sstevel@tonic-gate fasttrap_putreg(struct regs *rp, uint_t reg, ulong_t value)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate 	dtrace_icookie_t cookie;
14470Sstevel@tonic-gate 	struct machpcb *mpcb;
14480Sstevel@tonic-gate 	extern void dtrace_putreg_win(uint_t, ulong_t);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	if (reg == 0)
14510Sstevel@tonic-gate 		return;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if (reg < 16) {
14540Sstevel@tonic-gate 		(&rp->r_g1)[reg - 1] = value;
14550Sstevel@tonic-gate 		return;
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	/*
14590Sstevel@tonic-gate 	 * If the user process is still using some register windows, we
14600Sstevel@tonic-gate 	 * can just place the value in the correct window.
14610Sstevel@tonic-gate 	 */
14620Sstevel@tonic-gate 	cookie = dtrace_interrupt_disable();
14630Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
14640Sstevel@tonic-gate 		dtrace_putreg_win(reg, value);
14650Sstevel@tonic-gate 		dtrace_interrupt_enable(cookie);
14660Sstevel@tonic-gate 		atomic_add_64(&fasttrap_putreg_fast_cnt, 1);
14670Sstevel@tonic-gate 		return;
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	dtrace_interrupt_enable(cookie);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	/*
14720Sstevel@tonic-gate 	 * First see if there's a copy of the register window in the
14730Sstevel@tonic-gate 	 * machpcb structure that we can modify; if there isn't try to
14740Sstevel@tonic-gate 	 * copy out the value. If that fails, we try to create a new
14750Sstevel@tonic-gate 	 * register window in the machpcb structure. While this isn't
14760Sstevel@tonic-gate 	 * _precisely_ the intended use of the machpcb structure, it
14770Sstevel@tonic-gate 	 * can't cause any problems since we know at this point in the
14780Sstevel@tonic-gate 	 * code that all of the user's data have been flushed out of the
14790Sstevel@tonic-gate 	 * register file (since %otherwin is 0).
14800Sstevel@tonic-gate 	 */
14810Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
14840Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
14850Sstevel@tonic-gate 		struct rwindow *rwin = (struct rwindow *)mpcb->mpcb_wbuf;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
14880Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
14890Sstevel@tonic-gate 			do {
14900Sstevel@tonic-gate 				i--;
14910Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
14920Sstevel@tonic-gate 					continue;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 				rwin[i].rw_local[reg - 16] = value;
14950Sstevel@tonic-gate 				atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
14960Sstevel@tonic-gate 				return;
14970Sstevel@tonic-gate 			} while (i > 0);
14980Sstevel@tonic-gate 		}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 		if (fasttrap_sulword(&fr->fr_local[reg - 16], value) != 0) {
15010Sstevel@tonic-gate 			if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
15020Sstevel@tonic-gate 			    &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
15030Sstevel@tonic-gate 				goto err;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 			rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = value;
15060Sstevel@tonic-gate 			mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
15070Sstevel@tonic-gate 			mpcb->mpcb_wbcnt++;
15080Sstevel@tonic-gate 			atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15090Sstevel@tonic-gate 			return;
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 	} else {
15121048Sraf 		struct frame32 *fr =
15131048Sraf 		    (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
15140Sstevel@tonic-gate 		struct rwindow32 *rwin = (struct rwindow32 *)mpcb->mpcb_wbuf;
15150Sstevel@tonic-gate 		uint32_t v32 = (uint32_t)value;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
15180Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
15190Sstevel@tonic-gate 			do {
15200Sstevel@tonic-gate 				i--;
15210Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
15220Sstevel@tonic-gate 					continue;
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 				rwin[i].rw_local[reg - 16] = v32;
15250Sstevel@tonic-gate 				atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15260Sstevel@tonic-gate 				return;
15270Sstevel@tonic-gate 			} while (i > 0);
15280Sstevel@tonic-gate 		}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 		if (fasttrap_suword32(&fr->fr_local[reg - 16], v32) != 0) {
15310Sstevel@tonic-gate 			if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
15320Sstevel@tonic-gate 			    &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
15330Sstevel@tonic-gate 				goto err;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 			rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = v32;
15360Sstevel@tonic-gate 			mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
15370Sstevel@tonic-gate 			mpcb->mpcb_wbcnt++;
15380Sstevel@tonic-gate 			atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15390Sstevel@tonic-gate 			return;
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 	}
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	atomic_add_64(&fasttrap_putreg_slow_cnt, 1);
15440Sstevel@tonic-gate 	return;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate err:
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * If we couldn't record this register's value, the process is in an
15490Sstevel@tonic-gate 	 * irrecoverable state and we have no choice but to euthanize it.
15500Sstevel@tonic-gate 	 */
15510Sstevel@tonic-gate 	psignal(ttoproc(curthread), SIGILL);
15520Sstevel@tonic-gate }
1553