1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/frame.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/trap.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <v9/sys/machpcb.h>
40*7c478bd9Sstevel@tonic-gate #include <v9/sys/privregs.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Lossless User-Land Tracing on SPARC
44*7c478bd9Sstevel@tonic-gate  * -----------------------------------
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * The Basic Idea
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  * The most important design constraint is, of course, correct execution of
49*7c478bd9Sstevel@tonic-gate  * the user thread above all else. The next most important goal is rapid
50*7c478bd9Sstevel@tonic-gate  * execution. We combine execution of instructions in user-land with
51*7c478bd9Sstevel@tonic-gate  * emulation of certain instructions in the kernel to aim for complete
52*7c478bd9Sstevel@tonic-gate  * correctness and maximal performance.
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  * We take advantage of the split PC/NPC architecture to speed up logical
55*7c478bd9Sstevel@tonic-gate  * single-stepping; when we copy an instruction out to the scratch space in
56*7c478bd9Sstevel@tonic-gate  * the ulwp_t structure (held in the %g7 register on SPARC), we can
57*7c478bd9Sstevel@tonic-gate  * effectively single step by setting the PC to our scratch space and leaving
58*7c478bd9Sstevel@tonic-gate  * the NPC alone. This executes the replaced instruction and then continues
59*7c478bd9Sstevel@tonic-gate  * on without having to reenter the kernel as with single- stepping. The
60*7c478bd9Sstevel@tonic-gate  * obvious caveat is for instructions whose execution is PC dependant --
61*7c478bd9Sstevel@tonic-gate  * branches, call and link instructions (call and jmpl), and the rdpc
62*7c478bd9Sstevel@tonic-gate  * instruction. These instructions cannot be executed in the manner described
63*7c478bd9Sstevel@tonic-gate  * so they must be emulated in the kernel.
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * Emulation for this small set of instructions if fairly simple; the most
66*7c478bd9Sstevel@tonic-gate  * difficult part being emulating branch conditions.
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * A Cache Heavy Portfolio
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * It's important to note at this time that copying an instruction out to the
72*7c478bd9Sstevel@tonic-gate  * ulwp_t scratch space in user-land is rather complicated. SPARC has
73*7c478bd9Sstevel@tonic-gate  * separate data and instruction caches so any writes to the D$ (using a
74*7c478bd9Sstevel@tonic-gate  * store instruction for example) aren't necessarily reflected in the I$.
75*7c478bd9Sstevel@tonic-gate  * The flush instruction can be used to synchronize the two and must be used
76*7c478bd9Sstevel@tonic-gate  * for any self-modifying code, but the flush instruction only applies to the
77*7c478bd9Sstevel@tonic-gate  * primary address space (the absence of a flusha analogue to the flush
78*7c478bd9Sstevel@tonic-gate  * instruction that accepts an ASI argument is an obvious omission from SPARC
79*7c478bd9Sstevel@tonic-gate  * v9 where the notion of the alternate address space was introduced on
80*7c478bd9Sstevel@tonic-gate  * SPARC). To correctly copy out the instruction we must use a block store
81*7c478bd9Sstevel@tonic-gate  * that doesn't allocate in the D$ and ensures synchronization with the I$;
82*7c478bd9Sstevel@tonic-gate  * see dtrace_blksuword32() for the implementation  (this function uses
83*7c478bd9Sstevel@tonic-gate  * ASI_BLK_COMMIT_S to write a block through the secondary ASI in the manner
84*7c478bd9Sstevel@tonic-gate  * described). Refer to the UltraSPARC I/II manual for details on the
85*7c478bd9Sstevel@tonic-gate  * ASI_BLK_COMMIT_S ASI.
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  * Return Subtleties
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  * When we're firing a return probe we need to expose the value returned by
91*7c478bd9Sstevel@tonic-gate  * the function being traced. Since the function can set the return value
92*7c478bd9Sstevel@tonic-gate  * in its last instruction, we need to fire the return probe only _after_
93*7c478bd9Sstevel@tonic-gate  * the effects of the instruction are apparent. For instructions that we
94*7c478bd9Sstevel@tonic-gate  * emulate, we can call dtrace_probe() after we've performed the emulation;
95*7c478bd9Sstevel@tonic-gate  * for instructions that we execute after we return to user-land, we set
96*7c478bd9Sstevel@tonic-gate  * %pc to the instruction we copied out (as described above) and set %npc
97*7c478bd9Sstevel@tonic-gate  * to a trap instruction stashed in the ulwp_t structure. After the traced
98*7c478bd9Sstevel@tonic-gate  * instruction is executed, the trap instruction returns control to the
99*7c478bd9Sstevel@tonic-gate  * kernel where we can fire the return probe.
100*7c478bd9Sstevel@tonic-gate  *
101*7c478bd9Sstevel@tonic-gate  * This need for a second trap in cases where we execute the traced
102*7c478bd9Sstevel@tonic-gate  * instruction makes it all the more important to emulate the most common
103*7c478bd9Sstevel@tonic-gate  * instructions to avoid the second trip in and out of the kernel.
104*7c478bd9Sstevel@tonic-gate  *
105*7c478bd9Sstevel@tonic-gate  *
106*7c478bd9Sstevel@tonic-gate  * Making it Fast
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * Since copying out an instruction is neither simple nor inexpensive for the
109*7c478bd9Sstevel@tonic-gate  * CPU, we should attempt to avoid doing it in as many cases as possible.
110*7c478bd9Sstevel@tonic-gate  * Since function entry and return are usually the most interesting probe
111*7c478bd9Sstevel@tonic-gate  * sites, we attempt to tune the performance of the fasttrap provider around
112*7c478bd9Sstevel@tonic-gate  * instructions typically in those places.
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  * Looking at a bunch of functions in libraries and executables reveals that
115*7c478bd9Sstevel@tonic-gate  * most functions begin with either a save or a sethi (to setup a larger
116*7c478bd9Sstevel@tonic-gate  * argument to the save) and end with a restore or an or (in the case of leaf
117*7c478bd9Sstevel@tonic-gate  * functions). To try to improve performance, we emulate all of these
118*7c478bd9Sstevel@tonic-gate  * instructions in the kernel.
119*7c478bd9Sstevel@tonic-gate  *
120*7c478bd9Sstevel@tonic-gate  * The save and restore instructions are a little tricky since they perform
121*7c478bd9Sstevel@tonic-gate  * register window maniplulation. Rather than trying to tinker with the
122*7c478bd9Sstevel@tonic-gate  * register windows from the kernel, we emulate the implicit add that takes
123*7c478bd9Sstevel@tonic-gate  * place as part of those instructions and set the %pc to point to a simple
124*7c478bd9Sstevel@tonic-gate  * save or restore we've hidden in the ulwp_t structure. If we're in a return
125*7c478bd9Sstevel@tonic-gate  * probe so want to make it seem as though the tracepoint has been completely
126*7c478bd9Sstevel@tonic-gate  * executed we need to remember that we've pulled this trick with restore and
127*7c478bd9Sstevel@tonic-gate  * pull registers from the previous window (the one that we'll switch to once
128*7c478bd9Sstevel@tonic-gate  * the simple store instruction is executed) rather than the current one. This
129*7c478bd9Sstevel@tonic-gate  * is why in the case of emulating a restore we set the DTrace CPU flag
130*7c478bd9Sstevel@tonic-gate  * CPU_DTRACE_FAKERESTORE before calling dtrace_probe() for the return probes
131*7c478bd9Sstevel@tonic-gate  * (see fasttrap_return_common()).
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate #define	OP(x)		((x) >> 30)
135*7c478bd9Sstevel@tonic-gate #define	OP2(x)		(((x) >> 22) & 0x07)
136*7c478bd9Sstevel@tonic-gate #define	OP3(x)		(((x) >> 19) & 0x3f)
137*7c478bd9Sstevel@tonic-gate #define	RCOND(x)	(((x) >> 25) & 0x07)
138*7c478bd9Sstevel@tonic-gate #define	COND(x)		(((x) >> 25) & 0x0f)
139*7c478bd9Sstevel@tonic-gate #define	A(x)		(((x) >> 29) & 0x01)
140*7c478bd9Sstevel@tonic-gate #define	I(x)		(((x) >> 13) & 0x01)
141*7c478bd9Sstevel@tonic-gate #define	RD(x)		(((x) >> 25) & 0x1f)
142*7c478bd9Sstevel@tonic-gate #define	RS1(x)		(((x) >> 14) & 0x1f)
143*7c478bd9Sstevel@tonic-gate #define	RS2(x)		(((x) >> 0) & 0x1f)
144*7c478bd9Sstevel@tonic-gate #define	CC(x)		(((x) >> 20) & 0x03)
145*7c478bd9Sstevel@tonic-gate #define	DISP16(x)	((((x) >> 6) & 0xc000) | ((x) & 0x3fff))
146*7c478bd9Sstevel@tonic-gate #define	DISP22(x)	((x) & 0x3fffff)
147*7c478bd9Sstevel@tonic-gate #define	DISP19(x)	((x) & 0x7ffff)
148*7c478bd9Sstevel@tonic-gate #define	DISP30(x)	((x) & 0x3fffffff)
149*7c478bd9Sstevel@tonic-gate #define	SW_TRAP(x)	((x) & 0x7f)
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate #define	OP3_OR		0x02
152*7c478bd9Sstevel@tonic-gate #define	OP3_RD		0x28
153*7c478bd9Sstevel@tonic-gate #define	OP3_JMPL	0x38
154*7c478bd9Sstevel@tonic-gate #define	OP3_RETURN	0x39
155*7c478bd9Sstevel@tonic-gate #define	OP3_TCC		0x3a
156*7c478bd9Sstevel@tonic-gate #define	OP3_SAVE	0x3c
157*7c478bd9Sstevel@tonic-gate #define	OP3_RESTORE	0x3d
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate #define	OP3_PREFETCH	0x2d
160*7c478bd9Sstevel@tonic-gate #define	OP3_CASA	0x3c
161*7c478bd9Sstevel@tonic-gate #define	OP3_PREFETCHA	0x3d
162*7c478bd9Sstevel@tonic-gate #define	OP3_CASXA	0x3e
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate #define	OP2_ILLTRAP	0x0
165*7c478bd9Sstevel@tonic-gate #define	OP2_BPcc	0x1
166*7c478bd9Sstevel@tonic-gate #define	OP2_Bicc	0x2
167*7c478bd9Sstevel@tonic-gate #define	OP2_BPr		0x3
168*7c478bd9Sstevel@tonic-gate #define	OP2_SETHI	0x4
169*7c478bd9Sstevel@tonic-gate #define	OP2_FBPfcc	0x5
170*7c478bd9Sstevel@tonic-gate #define	OP2_FBfcc	0x6
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate #define	R_G0		0
173*7c478bd9Sstevel@tonic-gate #define	R_O0		8
174*7c478bd9Sstevel@tonic-gate #define	R_SP		14
175*7c478bd9Sstevel@tonic-gate #define	R_I0		24
176*7c478bd9Sstevel@tonic-gate #define	R_I1		25
177*7c478bd9Sstevel@tonic-gate #define	R_I2		26
178*7c478bd9Sstevel@tonic-gate #define	R_I3		27
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate  * Check the comment in fasttrap.h when changing these offsets or adding
182*7c478bd9Sstevel@tonic-gate  * new instructions.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_OFF_SAVE	64
185*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_OFF_RESTORE	68
186*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_OFF_FTRET	72
187*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_OFF_RETURN	76
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate #define	BREAKPOINT_INSTR	0x91d02001	/* ta 1 */
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * Tunable to let users turn off the fancy save instruction optimization.
193*7c478bd9Sstevel@tonic-gate  * If a program is non-ABI compliant, there's a possibility that the save
194*7c478bd9Sstevel@tonic-gate  * instruction optimization could cause an error.
195*7c478bd9Sstevel@tonic-gate  */
196*7c478bd9Sstevel@tonic-gate int fasttrap_optimize_save = 1;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static uint64_t
199*7c478bd9Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int argno)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	uint64_t value;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	if (argno < 6)
204*7c478bd9Sstevel@tonic-gate 		return ((&rp->r_o0)[argno]);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
207*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
210*7c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_argd[argno]);
211*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
212*7c478bd9Sstevel@tonic-gate 		    CPU_DTRACE_BADALIGN);
213*7c478bd9Sstevel@tonic-gate 	} else {
214*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)rp->r_sp;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
217*7c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_argd[argno]);
218*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
219*7c478bd9Sstevel@tonic-gate 		    CPU_DTRACE_BADALIGN);
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	return (value);
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
226*7c478bd9Sstevel@tonic-gate static void fasttrap_putreg(struct regs *, uint_t, ulong_t);
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate int
229*7c478bd9Sstevel@tonic-gate fasttrap_probe(struct regs *rp)
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate 	dtrace_probe(fasttrap_probe_id,
232*7c478bd9Sstevel@tonic-gate 	    rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3, rp->r_o4);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	rp->r_pc = rp->r_npc;
235*7c478bd9Sstevel@tonic-gate 	rp->r_npc = rp->r_pc + 4;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	return (0);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate static void
241*7c478bd9Sstevel@tonic-gate fasttrap_usdt_args(fasttrap_probe_t *probe, struct regs *rp, int argc,
242*7c478bd9Sstevel@tonic-gate     uintptr_t *argv)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
247*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
248*7c478bd9Sstevel@tonic-gate 		uintptr_t v;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < cap; i++) {
251*7c478bd9Sstevel@tonic-gate 			x = probe->ftp_argmap[i];
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 			if (x < 6)
254*7c478bd9Sstevel@tonic-gate 				argv[i] = (&rp->r_o0)[x];
255*7c478bd9Sstevel@tonic-gate 			else if (fasttrap_fulword(&fr->fr_argd[x], &v) != 0)
256*7c478bd9Sstevel@tonic-gate 				argv[i] = 0;
257*7c478bd9Sstevel@tonic-gate 		}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	} else {
260*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)rp->r_sp;
261*7c478bd9Sstevel@tonic-gate 		uint32_t v;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < cap; i++) {
264*7c478bd9Sstevel@tonic-gate 			x = probe->ftp_argmap[i];
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 			if (x < 6)
267*7c478bd9Sstevel@tonic-gate 				argv[i] = (&rp->r_o0)[x];
268*7c478bd9Sstevel@tonic-gate 			else if (fasttrap_fuword32(&fr->fr_argd[x], &v) != 0)
269*7c478bd9Sstevel@tonic-gate 				argv[i] = 0;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	for (; i < argc; i++) {
274*7c478bd9Sstevel@tonic-gate 		argv[i] = 0;
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate static void
279*7c478bd9Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
280*7c478bd9Sstevel@tonic-gate     uint_t fake_restore)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
283*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
284*7c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
285*7c478bd9Sstevel@tonic-gate 	kmutex_t *pid_mtx;
286*7c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
289*7c478bd9Sstevel@tonic-gate 	mutex_enter(pid_mtx);
290*7c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
293*7c478bd9Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
294*7c478bd9Sstevel@tonic-gate 		    !tp->ftt_prov->ftp_defunct)
295*7c478bd9Sstevel@tonic-gate 			break;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/*
299*7c478bd9Sstevel@tonic-gate 	 * Don't sweat it if we can't find the tracepoint again; unlike
300*7c478bd9Sstevel@tonic-gate 	 * when we're in fasttrap_pid_probe(), finding the tracepoint here
301*7c478bd9Sstevel@tonic-gate 	 * is not essential to the correct execution of the process.
302*7c478bd9Sstevel@tonic-gate 	 */
303*7c478bd9Sstevel@tonic-gate 	if (tp == NULL || tp->ftt_retids == NULL) {
304*7c478bd9Sstevel@tonic-gate 		mutex_exit(pid_mtx);
305*7c478bd9Sstevel@tonic-gate 		return;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
309*7c478bd9Sstevel@tonic-gate 		fasttrap_probe_t *probe = id->fti_probe;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 		if (probe->ftp_type == DTFTP_POST_OFFSETS) {
312*7c478bd9Sstevel@tonic-gate 			if (probe->ftp_argmap == NULL) {
313*7c478bd9Sstevel@tonic-gate 				dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1,
314*7c478bd9Sstevel@tonic-gate 				    rp->r_o2, rp->r_o3, rp->r_o4);
315*7c478bd9Sstevel@tonic-gate 			} else {
316*7c478bd9Sstevel@tonic-gate 				uintptr_t t[5];
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 				fasttrap_usdt_args(probe, rp,
319*7c478bd9Sstevel@tonic-gate 				    sizeof (t) / sizeof (t[0]), t);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 				dtrace_probe(probe->ftp_id, t[0], t[1],
322*7c478bd9Sstevel@tonic-gate 				    t[2], t[3], t[4]);
323*7c478bd9Sstevel@tonic-gate 			}
324*7c478bd9Sstevel@tonic-gate 			continue;
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		/*
328*7c478bd9Sstevel@tonic-gate 		 * If this is only a possible return point, we must
329*7c478bd9Sstevel@tonic-gate 		 * be looking at a potential tail call in leaf context.
330*7c478bd9Sstevel@tonic-gate 		 * If the %npc is still within this function, then we
331*7c478bd9Sstevel@tonic-gate 		 * must have misidentified a jmpl as a tail-call when it
332*7c478bd9Sstevel@tonic-gate 		 * is, in fact, part of a jump table. It would be nice to
333*7c478bd9Sstevel@tonic-gate 		 * remove this tracepoint, but this is neither the time
334*7c478bd9Sstevel@tonic-gate 		 * nor the place.
335*7c478bd9Sstevel@tonic-gate 		 */
336*7c478bd9Sstevel@tonic-gate 		if ((tp->ftt_flags & FASTTRAP_F_RETMAYBE) &&
337*7c478bd9Sstevel@tonic-gate 		    rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
338*7c478bd9Sstevel@tonic-gate 			continue;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 		/*
341*7c478bd9Sstevel@tonic-gate 		 * It's possible for a function to branch to the delay slot
342*7c478bd9Sstevel@tonic-gate 		 * of an instruction that we've identified as a return site.
343*7c478bd9Sstevel@tonic-gate 		 * We can dectect this spurious return probe activation by
344*7c478bd9Sstevel@tonic-gate 		 * observing that in this case %npc will be %pc + 4 and %npc
345*7c478bd9Sstevel@tonic-gate 		 * will be inside the current function (unless the user is
346*7c478bd9Sstevel@tonic-gate 		 * doing _crazy_ instruction picking in which case there's
347*7c478bd9Sstevel@tonic-gate 		 * very little we can do). The second check is important
348*7c478bd9Sstevel@tonic-gate 		 * in case the last instructions of a function make a tail-
349*7c478bd9Sstevel@tonic-gate 		 * call to the function located immediately subsequent.
350*7c478bd9Sstevel@tonic-gate 		 */
351*7c478bd9Sstevel@tonic-gate 		if (rp->r_npc == rp->r_pc + 4 &&
352*7c478bd9Sstevel@tonic-gate 		    rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
353*7c478bd9Sstevel@tonic-gate 			continue;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 		/*
356*7c478bd9Sstevel@tonic-gate 		 * The first argument is the offset of return tracepoint
357*7c478bd9Sstevel@tonic-gate 		 * in the function; the remaining arguments are the return
358*7c478bd9Sstevel@tonic-gate 		 * values.
359*7c478bd9Sstevel@tonic-gate 		 *
360*7c478bd9Sstevel@tonic-gate 		 * If fake_restore is set, we need to pull the return values
361*7c478bd9Sstevel@tonic-gate 		 * out of the %i's rather than the %o's -- a little trickier.
362*7c478bd9Sstevel@tonic-gate 		 */
363*7c478bd9Sstevel@tonic-gate 		if (!fake_restore) {
364*7c478bd9Sstevel@tonic-gate 			dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
365*7c478bd9Sstevel@tonic-gate 			    rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3);
366*7c478bd9Sstevel@tonic-gate 		} else {
367*7c478bd9Sstevel@tonic-gate 			uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
368*7c478bd9Sstevel@tonic-gate 			uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
369*7c478bd9Sstevel@tonic-gate 			uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
370*7c478bd9Sstevel@tonic-gate 			uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 			cookie = dtrace_interrupt_disable();
373*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
374*7c478bd9Sstevel@tonic-gate 			dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
375*7c478bd9Sstevel@tonic-gate 			    arg0, arg1, arg2, arg3);
376*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
377*7c478bd9Sstevel@tonic-gate 			dtrace_interrupt_enable(cookie);
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	mutex_exit(pid_mtx);
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate int
385*7c478bd9Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
386*7c478bd9Sstevel@tonic-gate {
387*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
388*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, tp_local;
389*7c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
390*7c478bd9Sstevel@tonic-gate 	pid_t pid;
391*7c478bd9Sstevel@tonic-gate 	uintptr_t pc = rp->r_pc;
392*7c478bd9Sstevel@tonic-gate 	uintptr_t npc = rp->r_npc;
393*7c478bd9Sstevel@tonic-gate 	uintptr_t orig_pc = pc;
394*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
395*7c478bd9Sstevel@tonic-gate 	kmutex_t *pid_mtx;
396*7c478bd9Sstevel@tonic-gate 	uint_t fake_restore = 0;
397*7c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	/*
400*7c478bd9Sstevel@tonic-gate 	 * It's possible that a user (in a veritable orgy of bad planning)
401*7c478bd9Sstevel@tonic-gate 	 * could redirect this thread's flow of control before it reached the
402*7c478bd9Sstevel@tonic-gate 	 * return probe fasttrap. In this case we need to kill the process
403*7c478bd9Sstevel@tonic-gate 	 * since it's in a unrecoverable state.
404*7c478bd9Sstevel@tonic-gate 	 */
405*7c478bd9Sstevel@tonic-gate 	if (curthread->t_dtrace_step) {
406*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_dtrace_on);
407*7c478bd9Sstevel@tonic-gate 		fasttrap_sigtrap(p, curthread, pc);
408*7c478bd9Sstevel@tonic-gate 		return (0);
409*7c478bd9Sstevel@tonic-gate 	}
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	/*
412*7c478bd9Sstevel@tonic-gate 	 * Clear all user tracing flags.
413*7c478bd9Sstevel@tonic-gate 	 */
414*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_ft = 0;
415*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
416*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
417*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
418*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	/*
421*7c478bd9Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
422*7c478bd9Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
423*7c478bd9Sstevel@tonic-gate 	 * process: this one.
424*7c478bd9Sstevel@tonic-gate 	 */
425*7c478bd9Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
426*7c478bd9Sstevel@tonic-gate 		p = p->p_parent;
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	pid = p->p_pid;
430*7c478bd9Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
431*7c478bd9Sstevel@tonic-gate 	mutex_enter(pid_mtx);
432*7c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	/*
435*7c478bd9Sstevel@tonic-gate 	 * Lookup the tracepoint that the process just hit.
436*7c478bd9Sstevel@tonic-gate 	 */
437*7c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
438*7c478bd9Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
439*7c478bd9Sstevel@tonic-gate 		    !tp->ftt_prov->ftp_defunct)
440*7c478bd9Sstevel@tonic-gate 			break;
441*7c478bd9Sstevel@tonic-gate 	}
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	/*
444*7c478bd9Sstevel@tonic-gate 	 * If we couldn't find a matching tracepoint, either a tracepoint has
445*7c478bd9Sstevel@tonic-gate 	 * been inserted without using the pid<pid> ioctl interface (see
446*7c478bd9Sstevel@tonic-gate 	 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
447*7c478bd9Sstevel@tonic-gate 	 */
448*7c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
449*7c478bd9Sstevel@tonic-gate 		mutex_exit(pid_mtx);
450*7c478bd9Sstevel@tonic-gate 		return (-1);
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
454*7c478bd9Sstevel@tonic-gate 		fasttrap_probe_t *probe = id->fti_probe;
455*7c478bd9Sstevel@tonic-gate 		int isentry;
456*7c478bd9Sstevel@tonic-gate 		/*
457*7c478bd9Sstevel@tonic-gate 		 * We note that this was an entry probe to help ustack() find
458*7c478bd9Sstevel@tonic-gate 		 * the first caller.
459*7c478bd9Sstevel@tonic-gate 		 */
460*7c478bd9Sstevel@tonic-gate 		if ((isentry = (probe->ftp_type == DTFTP_ENTRY)) != 0) {
461*7c478bd9Sstevel@tonic-gate 			cookie = dtrace_interrupt_disable();
462*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
463*7c478bd9Sstevel@tonic-gate 		}
464*7c478bd9Sstevel@tonic-gate 		dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, rp->r_o2,
465*7c478bd9Sstevel@tonic-gate 		    rp->r_o3, rp->r_o4);
466*7c478bd9Sstevel@tonic-gate 		if (isentry) {
467*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
468*7c478bd9Sstevel@tonic-gate 			dtrace_interrupt_enable(cookie);
469*7c478bd9Sstevel@tonic-gate 		}
470*7c478bd9Sstevel@tonic-gate 	}
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	/*
473*7c478bd9Sstevel@tonic-gate 	 * We're about to do a bunch of work so we cache a local copy of
474*7c478bd9Sstevel@tonic-gate 	 * the tracepoint to emulate the instruction, and then find the
475*7c478bd9Sstevel@tonic-gate 	 * tracepoint again later if we need to light up any return probes.
476*7c478bd9Sstevel@tonic-gate 	 */
477*7c478bd9Sstevel@tonic-gate 	tp_local = *tp;
478*7c478bd9Sstevel@tonic-gate 	mutex_exit(pid_mtx);
479*7c478bd9Sstevel@tonic-gate 	tp = &tp_local;
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	/*
482*7c478bd9Sstevel@tonic-gate 	 * We emulate certain types of instructions do ensure correctness
483*7c478bd9Sstevel@tonic-gate 	 * (in the case of position dependent instructions) or optimize
484*7c478bd9Sstevel@tonic-gate 	 * common cases. The rest we have the thread execute back in user-
485*7c478bd9Sstevel@tonic-gate 	 * land.
486*7c478bd9Sstevel@tonic-gate 	 */
487*7c478bd9Sstevel@tonic-gate 	switch (tp->ftt_type) {
488*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_SAVE:
489*7c478bd9Sstevel@tonic-gate 	{
490*7c478bd9Sstevel@tonic-gate 		int32_t imm;
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 		/*
493*7c478bd9Sstevel@tonic-gate 		 * This an optimization to let us handle function entry
494*7c478bd9Sstevel@tonic-gate 		 * probes more efficiently. Many functions begin with a save
495*7c478bd9Sstevel@tonic-gate 		 * instruction that follows the pattern:
496*7c478bd9Sstevel@tonic-gate 		 *	save	%sp, <imm>, %sp
497*7c478bd9Sstevel@tonic-gate 		 *
498*7c478bd9Sstevel@tonic-gate 		 * Meanwhile, we've stashed the instruction:
499*7c478bd9Sstevel@tonic-gate 		 *	save	%g1, %g0, %sp
500*7c478bd9Sstevel@tonic-gate 		 *
501*7c478bd9Sstevel@tonic-gate 		 * off of %g7, so all we have to do is stick the right value
502*7c478bd9Sstevel@tonic-gate 		 * into %g1 and reset %pc to point to the instruction we've
503*7c478bd9Sstevel@tonic-gate 		 * cleverly hidden (%npc should not be touched).
504*7c478bd9Sstevel@tonic-gate 		 */
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 		imm = tp->ftt_instr << 19;
507*7c478bd9Sstevel@tonic-gate 		imm >>= 19;
508*7c478bd9Sstevel@tonic-gate 		rp->r_g1 = rp->r_sp + imm;
509*7c478bd9Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_SAVE;
510*7c478bd9Sstevel@tonic-gate 		break;
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_RESTORE:
514*7c478bd9Sstevel@tonic-gate 	{
515*7c478bd9Sstevel@tonic-gate 		ulong_t value;
516*7c478bd9Sstevel@tonic-gate 		uint_t rd;
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 		/*
519*7c478bd9Sstevel@tonic-gate 		 * This is an optimization to let us handle function
520*7c478bd9Sstevel@tonic-gate 		 * return probes more efficiently. Most non-leaf functions
521*7c478bd9Sstevel@tonic-gate 		 * end with the sequence:
522*7c478bd9Sstevel@tonic-gate 		 *	ret
523*7c478bd9Sstevel@tonic-gate 		 *	restore	<reg>, <reg_or_imm>, %oX
524*7c478bd9Sstevel@tonic-gate 		 *
525*7c478bd9Sstevel@tonic-gate 		 * We've stashed the instruction:
526*7c478bd9Sstevel@tonic-gate 		 *	restore	%g0, %g0, %g0
527*7c478bd9Sstevel@tonic-gate 		 *
528*7c478bd9Sstevel@tonic-gate 		 * off of %g7 so we just need to place the correct value
529*7c478bd9Sstevel@tonic-gate 		 * in the right %i register (since after our fake-o
530*7c478bd9Sstevel@tonic-gate 		 * restore, the %i's will become the %o's) and set the %pc
531*7c478bd9Sstevel@tonic-gate 		 * to point to our hidden restore. We also set fake_restore to
532*7c478bd9Sstevel@tonic-gate 		 * let fasttrap_return_common() know that it will find the
533*7c478bd9Sstevel@tonic-gate 		 * return values in the %i's rather than the %o's.
534*7c478bd9Sstevel@tonic-gate 		 */
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
537*7c478bd9Sstevel@tonic-gate 			int32_t imm;
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
540*7c478bd9Sstevel@tonic-gate 			imm >>= 19;
541*7c478bd9Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
542*7c478bd9Sstevel@tonic-gate 		} else {
543*7c478bd9Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
544*7c478bd9Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
545*7c478bd9Sstevel@tonic-gate 		}
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 		/*
548*7c478bd9Sstevel@tonic-gate 		 * Convert %o's to %i's; leave %g's as they are.
549*7c478bd9Sstevel@tonic-gate 		 */
550*7c478bd9Sstevel@tonic-gate 		rd = RD(tp->ftt_instr);
551*7c478bd9Sstevel@tonic-gate 		fasttrap_putreg(rp, ((rd & 0x18) == 0x8) ? rd + 16 : rd, value);
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_RESTORE;
554*7c478bd9Sstevel@tonic-gate 		fake_restore = 1;
555*7c478bd9Sstevel@tonic-gate 		break;
556*7c478bd9Sstevel@tonic-gate 	}
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_RETURN:
559*7c478bd9Sstevel@tonic-gate 	{
560*7c478bd9Sstevel@tonic-gate 		uintptr_t target;
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 		/*
563*7c478bd9Sstevel@tonic-gate 		 * A return instruction is like a jmpl (without the link
564*7c478bd9Sstevel@tonic-gate 		 * part) that executes an implicit restore. We've stashed
565*7c478bd9Sstevel@tonic-gate 		 * the instruction:
566*7c478bd9Sstevel@tonic-gate 		 *	return %o0
567*7c478bd9Sstevel@tonic-gate 		 *
568*7c478bd9Sstevel@tonic-gate 		 * off of %g7 so we just need to place the target in %o0
569*7c478bd9Sstevel@tonic-gate 		 * and set the %pc to point to the stashed return instruction.
570*7c478bd9Sstevel@tonic-gate 		 * We use %o0 since that register disappears after the return
571*7c478bd9Sstevel@tonic-gate 		 * executes, erasing any evidence of this tampering.
572*7c478bd9Sstevel@tonic-gate 		 */
573*7c478bd9Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
574*7c478bd9Sstevel@tonic-gate 			int32_t imm;
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
577*7c478bd9Sstevel@tonic-gate 			imm >>= 19;
578*7c478bd9Sstevel@tonic-gate 			target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
579*7c478bd9Sstevel@tonic-gate 		} else {
580*7c478bd9Sstevel@tonic-gate 			target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
581*7c478bd9Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
582*7c478bd9Sstevel@tonic-gate 		}
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 		fasttrap_putreg(rp, R_O0, target);
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 		pc = rp->r_g7 + FASTTRAP_OFF_RETURN;
587*7c478bd9Sstevel@tonic-gate 		fake_restore = 1;
588*7c478bd9Sstevel@tonic-gate 		break;
589*7c478bd9Sstevel@tonic-gate 	}
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_OR:
592*7c478bd9Sstevel@tonic-gate 	{
593*7c478bd9Sstevel@tonic-gate 		ulong_t value;
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
596*7c478bd9Sstevel@tonic-gate 			int32_t imm;
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
599*7c478bd9Sstevel@tonic-gate 			imm >>= 19;
600*7c478bd9Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | imm;
601*7c478bd9Sstevel@tonic-gate 		} else {
602*7c478bd9Sstevel@tonic-gate 			value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) |
603*7c478bd9Sstevel@tonic-gate 			    fasttrap_getreg(rp, RS2(tp->ftt_instr));
604*7c478bd9Sstevel@tonic-gate 		}
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), value);
607*7c478bd9Sstevel@tonic-gate 		pc = rp->r_npc;
608*7c478bd9Sstevel@tonic-gate 		npc = pc + 4;
609*7c478bd9Sstevel@tonic-gate 		break;
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_SETHI:
613*7c478bd9Sstevel@tonic-gate 		if (RD(tp->ftt_instr) != R_G0) {
614*7c478bd9Sstevel@tonic-gate 			uint32_t imm32 = tp->ftt_instr << 10;
615*7c478bd9Sstevel@tonic-gate 			fasttrap_putreg(rp, RD(tp->ftt_instr), (ulong_t)imm32);
616*7c478bd9Sstevel@tonic-gate 		}
617*7c478bd9Sstevel@tonic-gate 		pc = rp->r_npc;
618*7c478bd9Sstevel@tonic-gate 		npc = pc + 4;
619*7c478bd9Sstevel@tonic-gate 		break;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_CCR:
622*7c478bd9Sstevel@tonic-gate 	{
623*7c478bd9Sstevel@tonic-gate 		uint_t c, v, z, n, taken;
624*7c478bd9Sstevel@tonic-gate 		uint_t ccr = rp->r_tstate >> TSTATE_CCR_SHIFT;
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_cc != 0)
627*7c478bd9Sstevel@tonic-gate 			ccr >>= 4;
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 		c = (ccr >> 0) & 1;
630*7c478bd9Sstevel@tonic-gate 		v = (ccr >> 1) & 1;
631*7c478bd9Sstevel@tonic-gate 		z = (ccr >> 2) & 1;
632*7c478bd9Sstevel@tonic-gate 		n = (ccr >> 3) & 1;
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 		switch (tp->ftt_code) {
635*7c478bd9Sstevel@tonic-gate 		case 0x0:	/* BN */
636*7c478bd9Sstevel@tonic-gate 			taken = 0;		break;
637*7c478bd9Sstevel@tonic-gate 		case 0x1:	/* BE */
638*7c478bd9Sstevel@tonic-gate 			taken = z;		break;
639*7c478bd9Sstevel@tonic-gate 		case 0x2:	/* BLE */
640*7c478bd9Sstevel@tonic-gate 			taken = z | (n ^ v);	break;
641*7c478bd9Sstevel@tonic-gate 		case 0x3:	/* BL */
642*7c478bd9Sstevel@tonic-gate 			taken = n ^ v;		break;
643*7c478bd9Sstevel@tonic-gate 		case 0x4:	/* BLEU */
644*7c478bd9Sstevel@tonic-gate 			taken = c | z;		break;
645*7c478bd9Sstevel@tonic-gate 		case 0x5:	/* BCS (BLU) */
646*7c478bd9Sstevel@tonic-gate 			taken = c;		break;
647*7c478bd9Sstevel@tonic-gate 		case 0x6:	/* BNEG */
648*7c478bd9Sstevel@tonic-gate 			taken = n;		break;
649*7c478bd9Sstevel@tonic-gate 		case 0x7:	/* BVS */
650*7c478bd9Sstevel@tonic-gate 			taken = v;		break;
651*7c478bd9Sstevel@tonic-gate 		case 0x8:	/* BA */
652*7c478bd9Sstevel@tonic-gate 			/*
653*7c478bd9Sstevel@tonic-gate 			 * We handle the BA case differently since the annul
654*7c478bd9Sstevel@tonic-gate 			 * bit means something slightly different.
655*7c478bd9Sstevel@tonic-gate 			 */
656*7c478bd9Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
657*7c478bd9Sstevel@tonic-gate 			taken = 1;		break;
658*7c478bd9Sstevel@tonic-gate 		case 0x9:	/* BNE */
659*7c478bd9Sstevel@tonic-gate 			taken = ~z;		break;
660*7c478bd9Sstevel@tonic-gate 		case 0xa:	/* BG */
661*7c478bd9Sstevel@tonic-gate 			taken = ~(z | (n ^ v));	break;
662*7c478bd9Sstevel@tonic-gate 		case 0xb:	/* BGE */
663*7c478bd9Sstevel@tonic-gate 			taken = ~(n ^ v);	break;
664*7c478bd9Sstevel@tonic-gate 		case 0xc:	/* BGU */
665*7c478bd9Sstevel@tonic-gate 			taken = ~(c | z);	break;
666*7c478bd9Sstevel@tonic-gate 		case 0xd:	/* BCC (BGEU) */
667*7c478bd9Sstevel@tonic-gate 			taken = ~c;		break;
668*7c478bd9Sstevel@tonic-gate 		case 0xe:	/* BPOS */
669*7c478bd9Sstevel@tonic-gate 			taken = ~n;		break;
670*7c478bd9Sstevel@tonic-gate 		case 0xf:	/* BVC */
671*7c478bd9Sstevel@tonic-gate 			taken = ~v;		break;
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 		if (taken & 1) {
675*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
676*7c478bd9Sstevel@tonic-gate 			npc = tp->ftt_dest;
677*7c478bd9Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
678*7c478bd9Sstevel@tonic-gate 			/*
679*7c478bd9Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
680*7c478bd9Sstevel@tonic-gate 			 * instruction in the delay slot.
681*7c478bd9Sstevel@tonic-gate 			 */
682*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc + 4;
683*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
684*7c478bd9Sstevel@tonic-gate 		} else {
685*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
686*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 		break;
689*7c478bd9Sstevel@tonic-gate 	}
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_FCC:
692*7c478bd9Sstevel@tonic-gate 	{
693*7c478bd9Sstevel@tonic-gate 		uint_t fcc;
694*7c478bd9Sstevel@tonic-gate 		uint_t taken;
695*7c478bd9Sstevel@tonic-gate 		uint64_t fsr;
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 		dtrace_getfsr(&fsr);
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_cc == 0) {
700*7c478bd9Sstevel@tonic-gate 			fcc = (fsr >> 10) & 0x3;
701*7c478bd9Sstevel@tonic-gate 		} else {
702*7c478bd9Sstevel@tonic-gate 			uint_t shift;
703*7c478bd9Sstevel@tonic-gate 			ASSERT(tp->ftt_cc <= 3);
704*7c478bd9Sstevel@tonic-gate 			shift = 30 + tp->ftt_cc * 2;
705*7c478bd9Sstevel@tonic-gate 			fcc = (fsr >> shift) & 0x3;
706*7c478bd9Sstevel@tonic-gate 		}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 		switch (tp->ftt_code) {
709*7c478bd9Sstevel@tonic-gate 		case 0x0:	/* FBN */
710*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|0|0);	break;
711*7c478bd9Sstevel@tonic-gate 		case 0x1:	/* FBNE */
712*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|2|0);	break;
713*7c478bd9Sstevel@tonic-gate 		case 0x2:	/* FBLG */
714*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|2|0);	break;
715*7c478bd9Sstevel@tonic-gate 		case 0x3:	/* FBUL */
716*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|2|0);	break;
717*7c478bd9Sstevel@tonic-gate 		case 0x4:	/* FBL */
718*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|2|0);	break;
719*7c478bd9Sstevel@tonic-gate 		case 0x5:	/* FBUG */
720*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|0|0);	break;
721*7c478bd9Sstevel@tonic-gate 		case 0x6:	/* FBG */
722*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|0|0);	break;
723*7c478bd9Sstevel@tonic-gate 		case 0x7:	/* FBU */
724*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|0|0);	break;
725*7c478bd9Sstevel@tonic-gate 		case 0x8:	/* FBA */
726*7c478bd9Sstevel@tonic-gate 			/*
727*7c478bd9Sstevel@tonic-gate 			 * We handle the FBA case differently since the annul
728*7c478bd9Sstevel@tonic-gate 			 * bit means something slightly different.
729*7c478bd9Sstevel@tonic-gate 			 */
730*7c478bd9Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
731*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|2|1);	break;
732*7c478bd9Sstevel@tonic-gate 		case 0x9:	/* FBE */
733*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|0|1);	break;
734*7c478bd9Sstevel@tonic-gate 		case 0xa:	/* FBUE */
735*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|0|1);	break;
736*7c478bd9Sstevel@tonic-gate 		case 0xb:	/* FBGE */
737*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|0|1);	break;
738*7c478bd9Sstevel@tonic-gate 		case 0xc:	/* FBUGE */
739*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|4|0|1);	break;
740*7c478bd9Sstevel@tonic-gate 		case 0xd:	/* FBLE */
741*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|0|2|1);	break;
742*7c478bd9Sstevel@tonic-gate 		case 0xe:	/* FBULE */
743*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (8|0|2|1);	break;
744*7c478bd9Sstevel@tonic-gate 		case 0xf:	/* FBO */
745*7c478bd9Sstevel@tonic-gate 			taken = (1 << fcc) & (0|4|2|1);	break;
746*7c478bd9Sstevel@tonic-gate 		}
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 		if (taken) {
749*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
750*7c478bd9Sstevel@tonic-gate 			npc = tp->ftt_dest;
751*7c478bd9Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
752*7c478bd9Sstevel@tonic-gate 			/*
753*7c478bd9Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
754*7c478bd9Sstevel@tonic-gate 			 * instruction in the delay slot.
755*7c478bd9Sstevel@tonic-gate 			 */
756*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc + 4;
757*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
758*7c478bd9Sstevel@tonic-gate 		} else {
759*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
760*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
761*7c478bd9Sstevel@tonic-gate 		}
762*7c478bd9Sstevel@tonic-gate 		break;
763*7c478bd9Sstevel@tonic-gate 	}
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_REG:
766*7c478bd9Sstevel@tonic-gate 	{
767*7c478bd9Sstevel@tonic-gate 		uint64_t value;
768*7c478bd9Sstevel@tonic-gate 		uint_t taken;
769*7c478bd9Sstevel@tonic-gate 		uint_t reg = RS1(tp->ftt_instr);
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate 		/*
772*7c478bd9Sstevel@tonic-gate 		 * An ILP32 process shouldn't be using a branch predicated on
773*7c478bd9Sstevel@tonic-gate 		 * an %i or an %l since it would violate the ABI. It's a
774*7c478bd9Sstevel@tonic-gate 		 * violation of the ABI because we can't ensure deterministic
775*7c478bd9Sstevel@tonic-gate 		 * behavior. We should have identified this case when we
776*7c478bd9Sstevel@tonic-gate 		 * enabled the probe.
777*7c478bd9Sstevel@tonic-gate 		 */
778*7c478bd9Sstevel@tonic-gate 		ASSERT(p->p_model == DATAMODEL_LP64 || reg < 16);
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 		value = fasttrap_getreg(rp, reg);
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 		switch (tp->ftt_code) {
783*7c478bd9Sstevel@tonic-gate 		case 0x1:	/* BRZ */
784*7c478bd9Sstevel@tonic-gate 			taken = (value == 0);	break;
785*7c478bd9Sstevel@tonic-gate 		case 0x2:	/* BRLEZ */
786*7c478bd9Sstevel@tonic-gate 			taken = (value <= 0);	break;
787*7c478bd9Sstevel@tonic-gate 		case 0x3:	/* BRLZ */
788*7c478bd9Sstevel@tonic-gate 			taken = (value < 0);	break;
789*7c478bd9Sstevel@tonic-gate 		case 0x5:	/* BRNZ */
790*7c478bd9Sstevel@tonic-gate 			taken = (value != 0);	break;
791*7c478bd9Sstevel@tonic-gate 		case 0x6:	/* BRGZ */
792*7c478bd9Sstevel@tonic-gate 			taken = (value > 0);	break;
793*7c478bd9Sstevel@tonic-gate 		case 0x7:	/* BRGEZ */
794*7c478bd9Sstevel@tonic-gate 			taken = (value <= 0);	break;
795*7c478bd9Sstevel@tonic-gate 		default:
796*7c478bd9Sstevel@tonic-gate 		case 0x0:
797*7c478bd9Sstevel@tonic-gate 		case 0x4:
798*7c478bd9Sstevel@tonic-gate 			panic("fasttrap: mishandled a branch");
799*7c478bd9Sstevel@tonic-gate 		}
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate 		if (taken) {
802*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
803*7c478bd9Sstevel@tonic-gate 			npc = tp->ftt_dest;
804*7c478bd9Sstevel@tonic-gate 		} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
805*7c478bd9Sstevel@tonic-gate 			/*
806*7c478bd9Sstevel@tonic-gate 			 * Untaken annulled branches don't execute the
807*7c478bd9Sstevel@tonic-gate 			 * instruction in the delay slot.
808*7c478bd9Sstevel@tonic-gate 			 */
809*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc + 4;
810*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
811*7c478bd9Sstevel@tonic-gate 		} else {
812*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
813*7c478bd9Sstevel@tonic-gate 			npc = pc + 4;
814*7c478bd9Sstevel@tonic-gate 		}
815*7c478bd9Sstevel@tonic-gate 		break;
816*7c478bd9Sstevel@tonic-gate 	}
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_ALWAYS:
819*7c478bd9Sstevel@tonic-gate 		/*
820*7c478bd9Sstevel@tonic-gate 		 * BAs, BA,As...
821*7c478bd9Sstevel@tonic-gate 		 */
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
824*7c478bd9Sstevel@tonic-gate 			/*
825*7c478bd9Sstevel@tonic-gate 			 * Annulled branch always instructions never execute
826*7c478bd9Sstevel@tonic-gate 			 * the instruction in the delay slot.
827*7c478bd9Sstevel@tonic-gate 			 */
828*7c478bd9Sstevel@tonic-gate 			pc = tp->ftt_dest;
829*7c478bd9Sstevel@tonic-gate 			npc = tp->ftt_dest + 4;
830*7c478bd9Sstevel@tonic-gate 		} else {
831*7c478bd9Sstevel@tonic-gate 			pc = rp->r_npc;
832*7c478bd9Sstevel@tonic-gate 			npc = tp->ftt_dest;
833*7c478bd9Sstevel@tonic-gate 		}
834*7c478bd9Sstevel@tonic-gate 		break;
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_RDPC:
837*7c478bd9Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
838*7c478bd9Sstevel@tonic-gate 		pc = rp->r_npc;
839*7c478bd9Sstevel@tonic-gate 		npc = pc + 4;
840*7c478bd9Sstevel@tonic-gate 		break;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_CALL:
843*7c478bd9Sstevel@tonic-gate 		/*
844*7c478bd9Sstevel@tonic-gate 		 * It's a call _and_ link remember...
845*7c478bd9Sstevel@tonic-gate 		 */
846*7c478bd9Sstevel@tonic-gate 		rp->r_o7 = rp->r_pc;
847*7c478bd9Sstevel@tonic-gate 		pc = rp->r_npc;
848*7c478bd9Sstevel@tonic-gate 		npc = tp->ftt_dest;
849*7c478bd9Sstevel@tonic-gate 		break;
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_JMPL:
852*7c478bd9Sstevel@tonic-gate 		pc = rp->r_npc;
853*7c478bd9Sstevel@tonic-gate 
854*7c478bd9Sstevel@tonic-gate 		if (I(tp->ftt_instr)) {
855*7c478bd9Sstevel@tonic-gate 			uint_t rs1 = RS1(tp->ftt_instr);
856*7c478bd9Sstevel@tonic-gate 			int32_t imm;
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate 			imm = tp->ftt_instr << 19;
859*7c478bd9Sstevel@tonic-gate 			imm >>= 19;
860*7c478bd9Sstevel@tonic-gate 			npc = fasttrap_getreg(rp, rs1) + imm;
861*7c478bd9Sstevel@tonic-gate 		} else {
862*7c478bd9Sstevel@tonic-gate 			uint_t rs1 = RS1(tp->ftt_instr);
863*7c478bd9Sstevel@tonic-gate 			uint_t rs2 = RS2(tp->ftt_instr);
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate 			npc = fasttrap_getreg(rp, rs1) +
866*7c478bd9Sstevel@tonic-gate 			    fasttrap_getreg(rp, rs2);
867*7c478bd9Sstevel@tonic-gate 		}
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 		/*
870*7c478bd9Sstevel@tonic-gate 		 * Do the link part of the jump-and-link instruction.
871*7c478bd9Sstevel@tonic-gate 		 */
872*7c478bd9Sstevel@tonic-gate 		fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 		break;
875*7c478bd9Sstevel@tonic-gate 
876*7c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_COMMON:
877*7c478bd9Sstevel@tonic-gate 	{
878*7c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_scrpc = rp->r_g7;
879*7c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_astpc = rp->r_g7 + FASTTRAP_OFF_FTRET;
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 		/*
882*7c478bd9Sstevel@tonic-gate 		 * Copy the instruction to a reserved location in the
883*7c478bd9Sstevel@tonic-gate 		 * user-land thread structure, then set the PC to that
884*7c478bd9Sstevel@tonic-gate 		 * location and leave the NPC alone. We take pains to ensure
885*7c478bd9Sstevel@tonic-gate 		 * consistency in the instruction stream (See SPARC
886*7c478bd9Sstevel@tonic-gate 		 * Architecture Manual Version 9, sections 8.4.7, A.20, and
887*7c478bd9Sstevel@tonic-gate 		 * H.1.6; UltraSPARC I/II User's Manual, sections 3.1.1.1,
888*7c478bd9Sstevel@tonic-gate 		 * and 13.6.4) by using the ASI ASI_BLK_COMMIT_S to copy the
889*7c478bd9Sstevel@tonic-gate 		 * instruction into the user's address space without
890*7c478bd9Sstevel@tonic-gate 		 * bypassing the I$. There's no AS_USER version of this ASI
891*7c478bd9Sstevel@tonic-gate 		 * (as exist for other ASIs) so we use the lofault
892*7c478bd9Sstevel@tonic-gate 		 * mechanism to catch faults.
893*7c478bd9Sstevel@tonic-gate 		 */
894*7c478bd9Sstevel@tonic-gate 		if (dtrace_blksuword32(rp->r_g7, &tp->ftt_instr, 1) == -1) {
895*7c478bd9Sstevel@tonic-gate 			/*
896*7c478bd9Sstevel@tonic-gate 			 * If the copyout fails, then the process's state
897*7c478bd9Sstevel@tonic-gate 			 * is not consistent (the effects of the traced
898*7c478bd9Sstevel@tonic-gate 			 * instruction will never be seen). This process
899*7c478bd9Sstevel@tonic-gate 			 * cannot be allowed to continue execution.
900*7c478bd9Sstevel@tonic-gate 			 */
901*7c478bd9Sstevel@tonic-gate 			fasttrap_sigtrap(curproc, curthread, pc);
902*7c478bd9Sstevel@tonic-gate 			return (0);
903*7c478bd9Sstevel@tonic-gate 		}
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_pc = pc;
906*7c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_npc = npc;
907*7c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_on = 1;
908*7c478bd9Sstevel@tonic-gate 
909*7c478bd9Sstevel@tonic-gate 		pc = curthread->t_dtrace_scrpc;
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_retids != NULL) {
912*7c478bd9Sstevel@tonic-gate 			curthread->t_dtrace_step = 1;
913*7c478bd9Sstevel@tonic-gate 			curthread->t_dtrace_ret = 1;
914*7c478bd9Sstevel@tonic-gate 			npc = curthread->t_dtrace_astpc;
915*7c478bd9Sstevel@tonic-gate 		}
916*7c478bd9Sstevel@tonic-gate 		break;
917*7c478bd9Sstevel@tonic-gate 	}
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	default:
920*7c478bd9Sstevel@tonic-gate 		panic("fasttrap: mishandled an instruction");
921*7c478bd9Sstevel@tonic-gate 	}
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 	/*
924*7c478bd9Sstevel@tonic-gate 	 * This bit me in the ass a couple of times, so lets toss this
925*7c478bd9Sstevel@tonic-gate 	 * in as a cursory sanity check.
926*7c478bd9Sstevel@tonic-gate 	 */
927*7c478bd9Sstevel@tonic-gate 	ASSERT(pc != rp->r_g7 + 4);
928*7c478bd9Sstevel@tonic-gate 	ASSERT(pc != rp->r_g7 + 8);
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 	/*
931*7c478bd9Sstevel@tonic-gate 	 * If there were no return probes when we first found the tracepoint,
932*7c478bd9Sstevel@tonic-gate 	 * we should feel no obligation to honor any return probes that were
933*7c478bd9Sstevel@tonic-gate 	 * subsequently enabled -- they'll just have to wait until the next
934*7c478bd9Sstevel@tonic-gate 	 * time around.
935*7c478bd9Sstevel@tonic-gate 	 */
936*7c478bd9Sstevel@tonic-gate 	if (tp->ftt_retids != NULL) {
937*7c478bd9Sstevel@tonic-gate 		/*
938*7c478bd9Sstevel@tonic-gate 		 * We need to wait until the results of the instruction are
939*7c478bd9Sstevel@tonic-gate 		 * apparent before invoking any return probes. If this
940*7c478bd9Sstevel@tonic-gate 		 * instruction was emulated we can just call
941*7c478bd9Sstevel@tonic-gate 		 * fasttrap_return_common(); if it needs to be executed, we
942*7c478bd9Sstevel@tonic-gate 		 * need to wait until we return to the kernel.
943*7c478bd9Sstevel@tonic-gate 		 */
944*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_COMMON) {
945*7c478bd9Sstevel@tonic-gate 			fasttrap_return_common(rp, orig_pc, pid, fake_restore);
946*7c478bd9Sstevel@tonic-gate 		} else {
947*7c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_ret != 0);
948*7c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_pc == orig_pc);
949*7c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_scrpc == rp->r_g7);
950*7c478bd9Sstevel@tonic-gate 			ASSERT(npc == curthread->t_dtrace_astpc);
951*7c478bd9Sstevel@tonic-gate 		}
952*7c478bd9Sstevel@tonic-gate 	}
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate 	ASSERT(pc != 0);
955*7c478bd9Sstevel@tonic-gate 	rp->r_pc = pc;
956*7c478bd9Sstevel@tonic-gate 	rp->r_npc = npc;
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate 	return (0);
959*7c478bd9Sstevel@tonic-gate }
960*7c478bd9Sstevel@tonic-gate 
961*7c478bd9Sstevel@tonic-gate int
962*7c478bd9Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
963*7c478bd9Sstevel@tonic-gate {
964*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
965*7c478bd9Sstevel@tonic-gate 	pid_t pid;
966*7c478bd9Sstevel@tonic-gate 	uintptr_t pc = curthread->t_dtrace_pc;
967*7c478bd9Sstevel@tonic-gate 	uintptr_t npc = curthread->t_dtrace_npc;
968*7c478bd9Sstevel@tonic-gate 
969*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
970*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
971*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
972*7c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	/*
975*7c478bd9Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
976*7c478bd9Sstevel@tonic-gate 	 * parent. We know there's only one thread of control in such a
977*7c478bd9Sstevel@tonic-gate 	 * process: this one.
978*7c478bd9Sstevel@tonic-gate 	 */
979*7c478bd9Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
980*7c478bd9Sstevel@tonic-gate 		p = p->p_parent;
981*7c478bd9Sstevel@tonic-gate 	}
982*7c478bd9Sstevel@tonic-gate 
983*7c478bd9Sstevel@tonic-gate 	/*
984*7c478bd9Sstevel@tonic-gate 	 * We set the %pc and %npc to their values when the traced
985*7c478bd9Sstevel@tonic-gate 	 * instruction was initially executed so that it appears to
986*7c478bd9Sstevel@tonic-gate 	 * dtrace_probe() that we're on the original instruction, and so that
987*7c478bd9Sstevel@tonic-gate 	 * the user can't easily detect our complex web of lies.
988*7c478bd9Sstevel@tonic-gate 	 * dtrace_return_probe() (our caller) will correctly set %pc and %npc
989*7c478bd9Sstevel@tonic-gate 	 * after we return.
990*7c478bd9Sstevel@tonic-gate 	 */
991*7c478bd9Sstevel@tonic-gate 	rp->r_pc = pc;
992*7c478bd9Sstevel@tonic-gate 	rp->r_npc = npc;
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	pid = p->p_pid;
995*7c478bd9Sstevel@tonic-gate 	fasttrap_return_common(rp, pc, pid, 0);
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 	return (0);
998*7c478bd9Sstevel@tonic-gate }
999*7c478bd9Sstevel@tonic-gate 
1000*7c478bd9Sstevel@tonic-gate int
1001*7c478bd9Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
1002*7c478bd9Sstevel@tonic-gate {
1003*7c478bd9Sstevel@tonic-gate 	fasttrap_instr_t instr = FASTTRAP_INSTR;
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	if (uwrite(p, &instr, 4, tp->ftt_pc) != 0)
1006*7c478bd9Sstevel@tonic-gate 		return (-1);
1007*7c478bd9Sstevel@tonic-gate 
1008*7c478bd9Sstevel@tonic-gate 	return (0);
1009*7c478bd9Sstevel@tonic-gate }
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate int
1012*7c478bd9Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
1013*7c478bd9Sstevel@tonic-gate {
1014*7c478bd9Sstevel@tonic-gate 	fasttrap_instr_t instr;
1015*7c478bd9Sstevel@tonic-gate 
1016*7c478bd9Sstevel@tonic-gate 	/*
1017*7c478bd9Sstevel@tonic-gate 	 * Distinguish between read or write failures and a changed
1018*7c478bd9Sstevel@tonic-gate 	 * instruction.
1019*7c478bd9Sstevel@tonic-gate 	 */
1020*7c478bd9Sstevel@tonic-gate 	if (uread(p, &instr, 4, tp->ftt_pc) != 0)
1021*7c478bd9Sstevel@tonic-gate 		return (0);
1022*7c478bd9Sstevel@tonic-gate 	if (instr != FASTTRAP_INSTR && instr != BREAKPOINT_INSTR)
1023*7c478bd9Sstevel@tonic-gate 		return (0);
1024*7c478bd9Sstevel@tonic-gate 	if (uwrite(p, &tp->ftt_instr, 4, tp->ftt_pc) != 0)
1025*7c478bd9Sstevel@tonic-gate 		return (-1);
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate 	return (0);
1028*7c478bd9Sstevel@tonic-gate }
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate int
1031*7c478bd9Sstevel@tonic-gate fasttrap_tracepoint_init(proc_t *p, fasttrap_probe_t *probe,
1032*7c478bd9Sstevel@tonic-gate     fasttrap_tracepoint_t *tp, uintptr_t pc)
1033*7c478bd9Sstevel@tonic-gate {
1034*7c478bd9Sstevel@tonic-gate 	uint32_t instr;
1035*7c478bd9Sstevel@tonic-gate 	int32_t disp;
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate 	/*
1038*7c478bd9Sstevel@tonic-gate 	 * Read the instruction at the given address out of the process's
1039*7c478bd9Sstevel@tonic-gate 	 * address space. We don't have to worry about a debugger
1040*7c478bd9Sstevel@tonic-gate 	 * changing this instruction before we overwrite it with our trap
1041*7c478bd9Sstevel@tonic-gate 	 * instruction since P_PR_LOCK is set.
1042*7c478bd9Sstevel@tonic-gate 	 */
1043*7c478bd9Sstevel@tonic-gate 	if (uread(p, &instr, 4, pc) != 0)
1044*7c478bd9Sstevel@tonic-gate 		return (-1);
1045*7c478bd9Sstevel@tonic-gate 
1046*7c478bd9Sstevel@tonic-gate 	/*
1047*7c478bd9Sstevel@tonic-gate 	 * Decode the instruction to fill in the probe flags. We can have
1048*7c478bd9Sstevel@tonic-gate 	 * the process execute most instructions on its own using a pc/npc
1049*7c478bd9Sstevel@tonic-gate 	 * trick, but pc-relative control transfer present a problem since
1050*7c478bd9Sstevel@tonic-gate 	 * we're relocating the instruction. We emulate these instructions
1051*7c478bd9Sstevel@tonic-gate 	 * in the kernel. We assume a default type and over-write that as
1052*7c478bd9Sstevel@tonic-gate 	 * needed.
1053*7c478bd9Sstevel@tonic-gate 	 *
1054*7c478bd9Sstevel@tonic-gate 	 * pc-relative instructions must be emulated for correctness;
1055*7c478bd9Sstevel@tonic-gate 	 * other instructions (which represent a large set of commonly traced
1056*7c478bd9Sstevel@tonic-gate 	 * instructions) are emulated or otherwise optimized for performance.
1057*7c478bd9Sstevel@tonic-gate 	 */
1058*7c478bd9Sstevel@tonic-gate 	tp->ftt_type = FASTTRAP_T_COMMON;
1059*7c478bd9Sstevel@tonic-gate 	if (OP(instr) == 1) {
1060*7c478bd9Sstevel@tonic-gate 		/*
1061*7c478bd9Sstevel@tonic-gate 		 * Call instructions.
1062*7c478bd9Sstevel@tonic-gate 		 */
1063*7c478bd9Sstevel@tonic-gate 		tp->ftt_type = FASTTRAP_T_CALL;
1064*7c478bd9Sstevel@tonic-gate 		disp = DISP30(instr) << 2;
1065*7c478bd9Sstevel@tonic-gate 		tp->ftt_dest = pc + (intptr_t)disp;
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate 	} else if (OP(instr) == 0) {
1068*7c478bd9Sstevel@tonic-gate 		/*
1069*7c478bd9Sstevel@tonic-gate 		 * Branch instructions.
1070*7c478bd9Sstevel@tonic-gate 		 *
1071*7c478bd9Sstevel@tonic-gate 		 * Unconditional branches need careful attention when they're
1072*7c478bd9Sstevel@tonic-gate 		 * annulled: annulled unconditional branches never execute
1073*7c478bd9Sstevel@tonic-gate 		 * the instruction in the delay slot.
1074*7c478bd9Sstevel@tonic-gate 		 */
1075*7c478bd9Sstevel@tonic-gate 		switch (OP2(instr)) {
1076*7c478bd9Sstevel@tonic-gate 		case OP2_ILLTRAP:
1077*7c478bd9Sstevel@tonic-gate 		case 0x7:
1078*7c478bd9Sstevel@tonic-gate 			/*
1079*7c478bd9Sstevel@tonic-gate 			 * The compiler may place an illtrap after a call to
1080*7c478bd9Sstevel@tonic-gate 			 * a function that returns a structure. In the case of
1081*7c478bd9Sstevel@tonic-gate 			 * a returned structure, the compiler places an illtrap
1082*7c478bd9Sstevel@tonic-gate 			 * whose const22 field is the size of the returned
1083*7c478bd9Sstevel@tonic-gate 			 * structure immediately following the delay slot of
1084*7c478bd9Sstevel@tonic-gate 			 * the call. To stay out of the way, we refuse to
1085*7c478bd9Sstevel@tonic-gate 			 * place tracepoints on top of illtrap instructions.
1086*7c478bd9Sstevel@tonic-gate 			 *
1087*7c478bd9Sstevel@tonic-gate 			 * This is one of the dumbest architectural decisions
1088*7c478bd9Sstevel@tonic-gate 			 * I've ever had to work around.
1089*7c478bd9Sstevel@tonic-gate 			 *
1090*7c478bd9Sstevel@tonic-gate 			 * We also identify the only illegal op2 value (See
1091*7c478bd9Sstevel@tonic-gate 			 * SPARC Architecture Manual Version 9, E.2 table 31).
1092*7c478bd9Sstevel@tonic-gate 			 */
1093*7c478bd9Sstevel@tonic-gate 			return (-1);
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate 		case OP2_BPcc:
1096*7c478bd9Sstevel@tonic-gate 			if (COND(instr) == 8) {
1097*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
1098*7c478bd9Sstevel@tonic-gate 			} else {
1099*7c478bd9Sstevel@tonic-gate 				/*
1100*7c478bd9Sstevel@tonic-gate 				 * Check for an illegal instruction.
1101*7c478bd9Sstevel@tonic-gate 				 */
1102*7c478bd9Sstevel@tonic-gate 				if (CC(instr) & 1)
1103*7c478bd9Sstevel@tonic-gate 					return (-1);
1104*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CCR;
1105*7c478bd9Sstevel@tonic-gate 				tp->ftt_cc = CC(instr);
1106*7c478bd9Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
1107*7c478bd9Sstevel@tonic-gate 			}
1108*7c478bd9Sstevel@tonic-gate 
1109*7c478bd9Sstevel@tonic-gate 			if (A(instr) != 0)
1110*7c478bd9Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
1111*7c478bd9Sstevel@tonic-gate 
1112*7c478bd9Sstevel@tonic-gate 			disp = DISP19(instr);
1113*7c478bd9Sstevel@tonic-gate 			disp <<= 13;
1114*7c478bd9Sstevel@tonic-gate 			disp >>= 11;
1115*7c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
1116*7c478bd9Sstevel@tonic-gate 			break;
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate 		case OP2_Bicc:
1119*7c478bd9Sstevel@tonic-gate 			if (COND(instr) == 8) {
1120*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
1121*7c478bd9Sstevel@tonic-gate 			} else {
1122*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CCR;
1123*7c478bd9Sstevel@tonic-gate 				tp->ftt_cc = 0;
1124*7c478bd9Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
1125*7c478bd9Sstevel@tonic-gate 			}
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 			if (A(instr) != 0)
1128*7c478bd9Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
1129*7c478bd9Sstevel@tonic-gate 
1130*7c478bd9Sstevel@tonic-gate 			disp = DISP22(instr);
1131*7c478bd9Sstevel@tonic-gate 			disp <<= 10;
1132*7c478bd9Sstevel@tonic-gate 			disp >>= 8;
1133*7c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
1134*7c478bd9Sstevel@tonic-gate 			break;
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate 		case OP2_BPr:
1137*7c478bd9Sstevel@tonic-gate 			/*
1138*7c478bd9Sstevel@tonic-gate 			 * Check for an illegal instruction.
1139*7c478bd9Sstevel@tonic-gate 			 */
1140*7c478bd9Sstevel@tonic-gate 			if ((RCOND(instr) & 3) == 0)
1141*7c478bd9Sstevel@tonic-gate 				return (-1);
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 			/*
1144*7c478bd9Sstevel@tonic-gate 			 * It's a violation of the v8plus ABI to use a
1145*7c478bd9Sstevel@tonic-gate 			 * register-predicated branch in a 32-bit app if
1146*7c478bd9Sstevel@tonic-gate 			 * the register used is an %l or an %i (%gs and %os
1147*7c478bd9Sstevel@tonic-gate 			 * are legit because they're not saved to the stack
1148*7c478bd9Sstevel@tonic-gate 			 * in 32-bit words when we take a trap).
1149*7c478bd9Sstevel@tonic-gate 			 */
1150*7c478bd9Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_ILP32 && RS1(instr) >= 16)
1151*7c478bd9Sstevel@tonic-gate 				return (-1);
1152*7c478bd9Sstevel@tonic-gate 
1153*7c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_REG;
1154*7c478bd9Sstevel@tonic-gate 			if (A(instr) != 0)
1155*7c478bd9Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
1156*7c478bd9Sstevel@tonic-gate 			disp = DISP16(instr);
1157*7c478bd9Sstevel@tonic-gate 			disp <<= 16;
1158*7c478bd9Sstevel@tonic-gate 			disp >>= 14;
1159*7c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
1160*7c478bd9Sstevel@tonic-gate 			tp->ftt_code = RCOND(instr);
1161*7c478bd9Sstevel@tonic-gate 			break;
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 		case OP2_SETHI:
1164*7c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_SETHI;
1165*7c478bd9Sstevel@tonic-gate 			break;
1166*7c478bd9Sstevel@tonic-gate 
1167*7c478bd9Sstevel@tonic-gate 		case OP2_FBPfcc:
1168*7c478bd9Sstevel@tonic-gate 			if (COND(instr) == 8) {
1169*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
1170*7c478bd9Sstevel@tonic-gate 			} else {
1171*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_FCC;
1172*7c478bd9Sstevel@tonic-gate 				tp->ftt_cc = CC(instr);
1173*7c478bd9Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
1174*7c478bd9Sstevel@tonic-gate 			}
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate 			if (A(instr) != 0)
1177*7c478bd9Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
1178*7c478bd9Sstevel@tonic-gate 
1179*7c478bd9Sstevel@tonic-gate 			disp = DISP19(instr);
1180*7c478bd9Sstevel@tonic-gate 			disp <<= 13;
1181*7c478bd9Sstevel@tonic-gate 			disp >>= 11;
1182*7c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
1183*7c478bd9Sstevel@tonic-gate 			break;
1184*7c478bd9Sstevel@tonic-gate 
1185*7c478bd9Sstevel@tonic-gate 		case OP2_FBfcc:
1186*7c478bd9Sstevel@tonic-gate 			if (COND(instr) == 8) {
1187*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_ALWAYS;
1188*7c478bd9Sstevel@tonic-gate 			} else {
1189*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_FCC;
1190*7c478bd9Sstevel@tonic-gate 				tp->ftt_cc = 0;
1191*7c478bd9Sstevel@tonic-gate 				tp->ftt_code = COND(instr);
1192*7c478bd9Sstevel@tonic-gate 			}
1193*7c478bd9Sstevel@tonic-gate 
1194*7c478bd9Sstevel@tonic-gate 			if (A(instr) != 0)
1195*7c478bd9Sstevel@tonic-gate 				tp->ftt_flags |= FASTTRAP_F_ANNUL;
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 			disp = DISP22(instr);
1198*7c478bd9Sstevel@tonic-gate 			disp <<= 10;
1199*7c478bd9Sstevel@tonic-gate 			disp >>= 8;
1200*7c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + (intptr_t)disp;
1201*7c478bd9Sstevel@tonic-gate 			break;
1202*7c478bd9Sstevel@tonic-gate 		}
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate 	} else if (OP(instr) == 2) {
1205*7c478bd9Sstevel@tonic-gate 		switch (OP3(instr)) {
1206*7c478bd9Sstevel@tonic-gate 		case OP3_RETURN:
1207*7c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RETURN;
1208*7c478bd9Sstevel@tonic-gate 			break;
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 		case OP3_JMPL:
1211*7c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMPL;
1212*7c478bd9Sstevel@tonic-gate 			break;
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 		case OP3_RD:
1215*7c478bd9Sstevel@tonic-gate 			if (RS1(instr) == 5)
1216*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_RDPC;
1217*7c478bd9Sstevel@tonic-gate 			break;
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 		case OP3_SAVE:
1220*7c478bd9Sstevel@tonic-gate 			/*
1221*7c478bd9Sstevel@tonic-gate 			 * We optimize for save instructions at function
1222*7c478bd9Sstevel@tonic-gate 			 * entry; see the comment in fasttrap_pid_probe()
1223*7c478bd9Sstevel@tonic-gate 			 * (near FASTTRAP_T_SAVE) for details.
1224*7c478bd9Sstevel@tonic-gate 			 */
1225*7c478bd9Sstevel@tonic-gate 			if (fasttrap_optimize_save != 0 &&
1226*7c478bd9Sstevel@tonic-gate 			    probe->ftp_type == DTFTP_ENTRY &&
1227*7c478bd9Sstevel@tonic-gate 			    I(instr) == 1 && RD(instr) == R_SP)
1228*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_SAVE;
1229*7c478bd9Sstevel@tonic-gate 			break;
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 		case OP3_RESTORE:
1232*7c478bd9Sstevel@tonic-gate 			/*
1233*7c478bd9Sstevel@tonic-gate 			 * We optimize restore instructions at function
1234*7c478bd9Sstevel@tonic-gate 			 * return; see the comment in fasttrap_pid_probe()
1235*7c478bd9Sstevel@tonic-gate 			 * (near FASTTRAP_T_RESTORE) for details.
1236*7c478bd9Sstevel@tonic-gate 			 *
1237*7c478bd9Sstevel@tonic-gate 			 * rd must be an %o or %g register.
1238*7c478bd9Sstevel@tonic-gate 			 */
1239*7c478bd9Sstevel@tonic-gate 			if ((RD(instr) & 0x10) == 0)
1240*7c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_RESTORE;
1241*7c478bd9Sstevel@tonic-gate 			break;
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate 		case OP3_OR:
1244*7c478bd9Sstevel@tonic-gate 			/*
1245*7c478bd9Sstevel@tonic-gate 			 * A large proportion of instructions in the delay
1246*7c478bd9Sstevel@tonic-gate 			 * slot of retl instructions are or's so we emulate
1247*7c478bd9Sstevel@tonic-gate 			 * these downstairs as an optimization.
1248*7c478bd9Sstevel@tonic-gate 			 */
1249*7c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_OR;
1250*7c478bd9Sstevel@tonic-gate 			break;
1251*7c478bd9Sstevel@tonic-gate 
1252*7c478bd9Sstevel@tonic-gate 		case OP3_TCC:
1253*7c478bd9Sstevel@tonic-gate 			/*
1254*7c478bd9Sstevel@tonic-gate 			 * Breakpoint instructions are effectively position-
1255*7c478bd9Sstevel@tonic-gate 			 * dependent since the debugger uses the %pc value
1256*7c478bd9Sstevel@tonic-gate 			 * to lookup which breakpoint was executed. As a
1257*7c478bd9Sstevel@tonic-gate 			 * result, we can't actually instrument breakpoints.
1258*7c478bd9Sstevel@tonic-gate 			 */
1259*7c478bd9Sstevel@tonic-gate 			if (SW_TRAP(instr) == ST_BREAKPOINT)
1260*7c478bd9Sstevel@tonic-gate 				return (-1);
1261*7c478bd9Sstevel@tonic-gate 			break;
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate 		case 0x19:
1264*7c478bd9Sstevel@tonic-gate 		case 0x1d:
1265*7c478bd9Sstevel@tonic-gate 		case 0x29:
1266*7c478bd9Sstevel@tonic-gate 		case 0x33:
1267*7c478bd9Sstevel@tonic-gate 		case 0x3f:
1268*7c478bd9Sstevel@tonic-gate 			/*
1269*7c478bd9Sstevel@tonic-gate 			 * Identify illegal instructions (See SPARC
1270*7c478bd9Sstevel@tonic-gate 			 * Architecture Manual Version 9, E.2 table 32).
1271*7c478bd9Sstevel@tonic-gate 			 */
1272*7c478bd9Sstevel@tonic-gate 			return (-1);
1273*7c478bd9Sstevel@tonic-gate 		}
1274*7c478bd9Sstevel@tonic-gate 	} else if (OP(instr) == 3) {
1275*7c478bd9Sstevel@tonic-gate 		uint32_t op3 = OP3(instr);
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 		/*
1278*7c478bd9Sstevel@tonic-gate 		 * Identify illegal instructions (See SPARC Architecture
1279*7c478bd9Sstevel@tonic-gate 		 * Manual Version 9, E.2 table 33).
1280*7c478bd9Sstevel@tonic-gate 		 */
1281*7c478bd9Sstevel@tonic-gate 		if ((op3 & 0x28) == 0x28) {
1282*7c478bd9Sstevel@tonic-gate 			if (op3 != OP3_PREFETCH && op3 != OP3_CASA &&
1283*7c478bd9Sstevel@tonic-gate 			    op3 != OP3_PREFETCHA && op3 != OP3_CASXA)
1284*7c478bd9Sstevel@tonic-gate 				return (-1);
1285*7c478bd9Sstevel@tonic-gate 		} else {
1286*7c478bd9Sstevel@tonic-gate 			if ((op3 & 0x0f) == 0x0c || (op3 & 0x3b) == 0x31)
1287*7c478bd9Sstevel@tonic-gate 				return (-1);
1288*7c478bd9Sstevel@tonic-gate 		}
1289*7c478bd9Sstevel@tonic-gate 	}
1290*7c478bd9Sstevel@tonic-gate 
1291*7c478bd9Sstevel@tonic-gate 	tp->ftt_instr = instr;
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 	/*
1294*7c478bd9Sstevel@tonic-gate 	 * We don't know how this tracepoint is going to be used, but in case
1295*7c478bd9Sstevel@tonic-gate 	 * it's used as part of a function return probe, we need to indicate
1296*7c478bd9Sstevel@tonic-gate 	 * whether it's always a return site or only potentially a return
1297*7c478bd9Sstevel@tonic-gate 	 * site. If it's part of a return probe, it's always going to be a
1298*7c478bd9Sstevel@tonic-gate 	 * return from that function if it's a restore instruction or if
1299*7c478bd9Sstevel@tonic-gate 	 * the previous instruction was a return. If we could reliably
1300*7c478bd9Sstevel@tonic-gate 	 * distinguish jump tables from return sites, this wouldn't be
1301*7c478bd9Sstevel@tonic-gate 	 * necessary.
1302*7c478bd9Sstevel@tonic-gate 	 */
1303*7c478bd9Sstevel@tonic-gate 	if (tp->ftt_type != FASTTRAP_T_RESTORE &&
1304*7c478bd9Sstevel@tonic-gate 	    (uread(p, &instr, 4, pc - sizeof (instr)) != 0 ||
1305*7c478bd9Sstevel@tonic-gate 	    !(OP(instr) == 2 && OP3(instr) == OP3_RETURN)))
1306*7c478bd9Sstevel@tonic-gate 		tp->ftt_flags |= FASTTRAP_F_RETMAYBE;
1307*7c478bd9Sstevel@tonic-gate 
1308*7c478bd9Sstevel@tonic-gate 	return (0);
1309*7c478bd9Sstevel@tonic-gate }
1310*7c478bd9Sstevel@tonic-gate 
1311*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1312*7c478bd9Sstevel@tonic-gate uint64_t
1313*7c478bd9Sstevel@tonic-gate fasttrap_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
1314*7c478bd9Sstevel@tonic-gate {
1315*7c478bd9Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
1316*7c478bd9Sstevel@tonic-gate }
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1319*7c478bd9Sstevel@tonic-gate uint64_t
1320*7c478bd9Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1321*7c478bd9Sstevel@tonic-gate     int aframes)
1322*7c478bd9Sstevel@tonic-gate {
1323*7c478bd9Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
1324*7c478bd9Sstevel@tonic-gate }
1325*7c478bd9Sstevel@tonic-gate 
1326*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_getreg_fast_cnt;
1327*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_getreg_mpcb_cnt;
1328*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_getreg_slow_cnt;
1329*7c478bd9Sstevel@tonic-gate 
1330*7c478bd9Sstevel@tonic-gate static ulong_t
1331*7c478bd9Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
1332*7c478bd9Sstevel@tonic-gate {
1333*7c478bd9Sstevel@tonic-gate 	ulong_t value;
1334*7c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
1335*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
1336*7c478bd9Sstevel@tonic-gate 	extern ulong_t dtrace_getreg_win(uint_t, uint_t);
1337*7c478bd9Sstevel@tonic-gate 
1338*7c478bd9Sstevel@tonic-gate 	/*
1339*7c478bd9Sstevel@tonic-gate 	 * We have the %os and %gs in our struct regs, but if we need to
1340*7c478bd9Sstevel@tonic-gate 	 * snag a %l or %i we need to go scrounging around in the process's
1341*7c478bd9Sstevel@tonic-gate 	 * address space.
1342*7c478bd9Sstevel@tonic-gate 	 */
1343*7c478bd9Sstevel@tonic-gate 	if (reg == 0)
1344*7c478bd9Sstevel@tonic-gate 		return (0);
1345*7c478bd9Sstevel@tonic-gate 
1346*7c478bd9Sstevel@tonic-gate 	if (reg < 16)
1347*7c478bd9Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
1348*7c478bd9Sstevel@tonic-gate 
1349*7c478bd9Sstevel@tonic-gate 	/*
1350*7c478bd9Sstevel@tonic-gate 	 * Before we look at the user's stack, we'll check the register
1351*7c478bd9Sstevel@tonic-gate 	 * windows to see if the information we want is in there.
1352*7c478bd9Sstevel@tonic-gate 	 */
1353*7c478bd9Sstevel@tonic-gate 	cookie = dtrace_interrupt_disable();
1354*7c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
1355*7c478bd9Sstevel@tonic-gate 		value = dtrace_getreg_win(reg, 1);
1356*7c478bd9Sstevel@tonic-gate 		dtrace_interrupt_enable(cookie);
1357*7c478bd9Sstevel@tonic-gate 
1358*7c478bd9Sstevel@tonic-gate 		atomic_add_64(&fasttrap_getreg_fast_cnt, 1);
1359*7c478bd9Sstevel@tonic-gate 
1360*7c478bd9Sstevel@tonic-gate 		return (value);
1361*7c478bd9Sstevel@tonic-gate 	}
1362*7c478bd9Sstevel@tonic-gate 	dtrace_interrupt_enable(cookie);
1363*7c478bd9Sstevel@tonic-gate 
1364*7c478bd9Sstevel@tonic-gate 	/*
1365*7c478bd9Sstevel@tonic-gate 	 * First check the machpcb structure to see if we've already read
1366*7c478bd9Sstevel@tonic-gate 	 * in the register window we're looking for; if we haven't, (and
1367*7c478bd9Sstevel@tonic-gate 	 * we probably haven't) try to copy in the value of the register.
1368*7c478bd9Sstevel@tonic-gate 	 */
1369*7c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
1372*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
1375*7c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
1376*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
1377*7c478bd9Sstevel@tonic-gate 			do {
1378*7c478bd9Sstevel@tonic-gate 				i--;
1379*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
1380*7c478bd9Sstevel@tonic-gate 					continue;
1381*7c478bd9Sstevel@tonic-gate 
1382*7c478bd9Sstevel@tonic-gate 				atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
1383*7c478bd9Sstevel@tonic-gate 				return (rwin[i].rw_local[reg - 16]);
1384*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
1385*7c478bd9Sstevel@tonic-gate 		}
1386*7c478bd9Sstevel@tonic-gate 
1387*7c478bd9Sstevel@tonic-gate 		if (fasttrap_fulword(&fr->fr_local[reg - 16], &value) != 0)
1388*7c478bd9Sstevel@tonic-gate 			goto err;
1389*7c478bd9Sstevel@tonic-gate 	} else {
1390*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)(caddr32_t)rp->r_sp;
1391*7c478bd9Sstevel@tonic-gate 		uint32_t *v32 = (uint32_t *)&value;
1392*7c478bd9Sstevel@tonic-gate 
1393*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
1394*7c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
1395*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
1396*7c478bd9Sstevel@tonic-gate 			do {
1397*7c478bd9Sstevel@tonic-gate 				i--;
1398*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
1399*7c478bd9Sstevel@tonic-gate 					continue;
1400*7c478bd9Sstevel@tonic-gate 
1401*7c478bd9Sstevel@tonic-gate 				atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
1402*7c478bd9Sstevel@tonic-gate 				return (rwin[i].rw_local[reg - 16]);
1403*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
1404*7c478bd9Sstevel@tonic-gate 		}
1405*7c478bd9Sstevel@tonic-gate 
1406*7c478bd9Sstevel@tonic-gate 		if (fasttrap_fuword32(&fr->fr_local[reg - 16], &v32[1]) != 0)
1407*7c478bd9Sstevel@tonic-gate 			goto err;
1408*7c478bd9Sstevel@tonic-gate 
1409*7c478bd9Sstevel@tonic-gate 		v32[0] = 0;
1410*7c478bd9Sstevel@tonic-gate 	}
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 	atomic_add_64(&fasttrap_getreg_slow_cnt, 1);
1413*7c478bd9Sstevel@tonic-gate 	return (value);
1414*7c478bd9Sstevel@tonic-gate 
1415*7c478bd9Sstevel@tonic-gate err:
1416*7c478bd9Sstevel@tonic-gate 	/*
1417*7c478bd9Sstevel@tonic-gate 	 * If the copy in failed, the process will be in a irrecoverable
1418*7c478bd9Sstevel@tonic-gate 	 * state, and we have no choice but to kill it.
1419*7c478bd9Sstevel@tonic-gate 	 */
1420*7c478bd9Sstevel@tonic-gate 	psignal(ttoproc(curthread), SIGILL);
1421*7c478bd9Sstevel@tonic-gate 	return (0);
1422*7c478bd9Sstevel@tonic-gate }
1423*7c478bd9Sstevel@tonic-gate 
1424*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_putreg_fast_cnt;
1425*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_putreg_mpcb_cnt;
1426*7c478bd9Sstevel@tonic-gate static uint64_t fasttrap_putreg_slow_cnt;
1427*7c478bd9Sstevel@tonic-gate 
1428*7c478bd9Sstevel@tonic-gate static void
1429*7c478bd9Sstevel@tonic-gate fasttrap_putreg(struct regs *rp, uint_t reg, ulong_t value)
1430*7c478bd9Sstevel@tonic-gate {
1431*7c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
1432*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
1433*7c478bd9Sstevel@tonic-gate 	extern void dtrace_putreg_win(uint_t, ulong_t);
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate 	if (reg == 0)
1436*7c478bd9Sstevel@tonic-gate 		return;
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 	if (reg < 16) {
1439*7c478bd9Sstevel@tonic-gate 		(&rp->r_g1)[reg - 1] = value;
1440*7c478bd9Sstevel@tonic-gate 		return;
1441*7c478bd9Sstevel@tonic-gate 	}
1442*7c478bd9Sstevel@tonic-gate 
1443*7c478bd9Sstevel@tonic-gate 	/*
1444*7c478bd9Sstevel@tonic-gate 	 * If the user process is still using some register windows, we
1445*7c478bd9Sstevel@tonic-gate 	 * can just place the value in the correct window.
1446*7c478bd9Sstevel@tonic-gate 	 */
1447*7c478bd9Sstevel@tonic-gate 	cookie = dtrace_interrupt_disable();
1448*7c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
1449*7c478bd9Sstevel@tonic-gate 		dtrace_putreg_win(reg, value);
1450*7c478bd9Sstevel@tonic-gate 		dtrace_interrupt_enable(cookie);
1451*7c478bd9Sstevel@tonic-gate 		atomic_add_64(&fasttrap_putreg_fast_cnt, 1);
1452*7c478bd9Sstevel@tonic-gate 		return;
1453*7c478bd9Sstevel@tonic-gate 	}
1454*7c478bd9Sstevel@tonic-gate 	dtrace_interrupt_enable(cookie);
1455*7c478bd9Sstevel@tonic-gate 
1456*7c478bd9Sstevel@tonic-gate 	/*
1457*7c478bd9Sstevel@tonic-gate 	 * First see if there's a copy of the register window in the
1458*7c478bd9Sstevel@tonic-gate 	 * machpcb structure that we can modify; if there isn't try to
1459*7c478bd9Sstevel@tonic-gate 	 * copy out the value. If that fails, we try to create a new
1460*7c478bd9Sstevel@tonic-gate 	 * register window in the machpcb structure. While this isn't
1461*7c478bd9Sstevel@tonic-gate 	 * _precisely_ the intended use of the machpcb structure, it
1462*7c478bd9Sstevel@tonic-gate 	 * can't cause any problems since we know at this point in the
1463*7c478bd9Sstevel@tonic-gate 	 * code that all of the user's data have been flushed out of the
1464*7c478bd9Sstevel@tonic-gate 	 * register file (since %otherwin is 0).
1465*7c478bd9Sstevel@tonic-gate 	 */
1466*7c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
1467*7c478bd9Sstevel@tonic-gate 
1468*7c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
1469*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
1470*7c478bd9Sstevel@tonic-gate 		struct rwindow *rwin = (struct rwindow *)mpcb->mpcb_wbuf;
1471*7c478bd9Sstevel@tonic-gate 
1472*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
1473*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
1474*7c478bd9Sstevel@tonic-gate 			do {
1475*7c478bd9Sstevel@tonic-gate 				i--;
1476*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
1477*7c478bd9Sstevel@tonic-gate 					continue;
1478*7c478bd9Sstevel@tonic-gate 
1479*7c478bd9Sstevel@tonic-gate 				rwin[i].rw_local[reg - 16] = value;
1480*7c478bd9Sstevel@tonic-gate 				atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
1481*7c478bd9Sstevel@tonic-gate 				return;
1482*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
1483*7c478bd9Sstevel@tonic-gate 		}
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate 		if (fasttrap_sulword(&fr->fr_local[reg - 16], value) != 0) {
1486*7c478bd9Sstevel@tonic-gate 			if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
1487*7c478bd9Sstevel@tonic-gate 			    &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
1488*7c478bd9Sstevel@tonic-gate 				goto err;
1489*7c478bd9Sstevel@tonic-gate 
1490*7c478bd9Sstevel@tonic-gate 			rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = value;
1491*7c478bd9Sstevel@tonic-gate 			mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
1492*7c478bd9Sstevel@tonic-gate 			mpcb->mpcb_wbcnt++;
1493*7c478bd9Sstevel@tonic-gate 			atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
1494*7c478bd9Sstevel@tonic-gate 			return;
1495*7c478bd9Sstevel@tonic-gate 		}
1496*7c478bd9Sstevel@tonic-gate 	} else {
1497*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (struct frame32 *)(caddr32_t)rp->r_sp;
1498*7c478bd9Sstevel@tonic-gate 		struct rwindow32 *rwin = (struct rwindow32 *)mpcb->mpcb_wbuf;
1499*7c478bd9Sstevel@tonic-gate 		uint32_t v32 = (uint32_t)value;
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
1502*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
1503*7c478bd9Sstevel@tonic-gate 			do {
1504*7c478bd9Sstevel@tonic-gate 				i--;
1505*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
1506*7c478bd9Sstevel@tonic-gate 					continue;
1507*7c478bd9Sstevel@tonic-gate 
1508*7c478bd9Sstevel@tonic-gate 				rwin[i].rw_local[reg - 16] = v32;
1509*7c478bd9Sstevel@tonic-gate 				atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
1510*7c478bd9Sstevel@tonic-gate 				return;
1511*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
1512*7c478bd9Sstevel@tonic-gate 		}
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 		if (fasttrap_suword32(&fr->fr_local[reg - 16], v32) != 0) {
1515*7c478bd9Sstevel@tonic-gate 			if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
1516*7c478bd9Sstevel@tonic-gate 			    &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
1517*7c478bd9Sstevel@tonic-gate 				goto err;
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 			rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = v32;
1520*7c478bd9Sstevel@tonic-gate 			mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
1521*7c478bd9Sstevel@tonic-gate 			mpcb->mpcb_wbcnt++;
1522*7c478bd9Sstevel@tonic-gate 			atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
1523*7c478bd9Sstevel@tonic-gate 			return;
1524*7c478bd9Sstevel@tonic-gate 		}
1525*7c478bd9Sstevel@tonic-gate 	}
1526*7c478bd9Sstevel@tonic-gate 
1527*7c478bd9Sstevel@tonic-gate 	atomic_add_64(&fasttrap_putreg_slow_cnt, 1);
1528*7c478bd9Sstevel@tonic-gate 	return;
1529*7c478bd9Sstevel@tonic-gate 
1530*7c478bd9Sstevel@tonic-gate err:
1531*7c478bd9Sstevel@tonic-gate 	/*
1532*7c478bd9Sstevel@tonic-gate 	 * If we couldn't record this register's value, the process is in an
1533*7c478bd9Sstevel@tonic-gate 	 * irrecoverable state and we have no choice but to euthanize it.
1534*7c478bd9Sstevel@tonic-gate 	 */
1535*7c478bd9Sstevel@tonic-gate 	psignal(ttoproc(curthread), SIGILL);
1536*7c478bd9Sstevel@tonic-gate }
1537