xref: /onnv-gate/usr/src/uts/intel/dtrace/fasttrap_isa.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
30*0Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
31*0Sstevel@tonic-gate #include <sys/dtrace.h>
32*0Sstevel@tonic-gate #include <sys/dtrace_impl.h>
33*0Sstevel@tonic-gate #include <sys/cmn_err.h>
34*0Sstevel@tonic-gate #include <sys/regset.h>
35*0Sstevel@tonic-gate #include <sys/privregs.h>
36*0Sstevel@tonic-gate #include <sys/segments.h>
37*0Sstevel@tonic-gate #include <sys/sysmacros.h>
38*0Sstevel@tonic-gate #include <sys/trap.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * Lossless User-Land Tracing on x86
42*0Sstevel@tonic-gate  * ---------------------------------
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * Most instructions' execution is not dependent on their address; for these
45*0Sstevel@tonic-gate  * instrutions it is sufficient to copy them into the user process's address
46*0Sstevel@tonic-gate  * space and execute them. To effectively single-step an instruction in
47*0Sstevel@tonic-gate  * user-land, we copy out the following sequence of instructions to scratch
48*0Sstevel@tonic-gate  * space in the user thread's ulwp_t structure.
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * We then set the program counter (%eip or %rip) to point to this scratch
51*0Sstevel@tonic-gate  * space. Once execution resumes, the original instruction is executed and
52*0Sstevel@tonic-gate  * then control flow is redirected to what was originally the subsequent
53*0Sstevel@tonic-gate  * instruction. If the kernel attemps to deliver a signal while single-
54*0Sstevel@tonic-gate  * stepping, the signal is deferred and the program counter is moved into the
55*0Sstevel@tonic-gate  * second sequence of instructions. The second sequence ends in a trap into
56*0Sstevel@tonic-gate  * the kernel where the deferred signal is then properly handled and delivered.
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  * For instructions whose execute is position dependent, we perform simple
59*0Sstevel@tonic-gate  * emulation. These instructions are limited to control transfer
60*0Sstevel@tonic-gate  * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
61*0Sstevel@tonic-gate  * of %rip-relative addressing that means that almost any instruction can be
62*0Sstevel@tonic-gate  * position dependent. For all the details on how we emulate generic
63*0Sstevel@tonic-gate  * instructions included %rip-relative instructions, see the code in
64*0Sstevel@tonic-gate  * fasttrap_pid_probe() below where we handle instructions of type
65*0Sstevel@tonic-gate  * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #define	FASTTRAP_MODRM_MOD(modrm)	(((modrm) >> 6) & 0x3)
69*0Sstevel@tonic-gate #define	FASTTRAP_MODRM_REG(modrm)	(((modrm) >> 3) & 0x7)
70*0Sstevel@tonic-gate #define	FASTTRAP_MODRM_RM(modrm)	((modrm) & 0x7)
71*0Sstevel@tonic-gate #define	FASTTRAP_MODRM(mod, reg, rm)	(((mod) << 6) | ((reg) << 3) | (rm))
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #define	FASTTRAP_SIB_SCALE(sib)		(((sib) >> 6) & 0x3)
74*0Sstevel@tonic-gate #define	FASTTRAP_SIB_INDEX(sib)		(((sib) >> 3) & 0x7)
75*0Sstevel@tonic-gate #define	FASTTRAP_SIB_BASE(sib)		((sib) & 0x7)
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #define	FASTTRAP_REX_W(rex)		(((rex) >> 3) & 1)
78*0Sstevel@tonic-gate #define	FASTTRAP_REX_R(rex)		(((rex) >> 2) & 1)
79*0Sstevel@tonic-gate #define	FASTTRAP_REX_X(rex)		(((rex) >> 1) & 1)
80*0Sstevel@tonic-gate #define	FASTTRAP_REX_B(rex)		((rex) & 1)
81*0Sstevel@tonic-gate #define	FASTTRAP_REX(w, r, x, b)	\
82*0Sstevel@tonic-gate 	(0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * Single-byte op-codes.
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate #define	FASTTRAP_PUSHL_EBP	0x55
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #define	FASTTRAP_JO		0x70
90*0Sstevel@tonic-gate #define	FASTTRAP_JNO		0x71
91*0Sstevel@tonic-gate #define	FASTTRAP_JB		0x72
92*0Sstevel@tonic-gate #define	FASTTRAP_JAE		0x73
93*0Sstevel@tonic-gate #define	FASTTRAP_JE		0x74
94*0Sstevel@tonic-gate #define	FASTTRAP_JNE		0x75
95*0Sstevel@tonic-gate #define	FASTTRAP_JBE		0x76
96*0Sstevel@tonic-gate #define	FASTTRAP_JA		0x77
97*0Sstevel@tonic-gate #define	FASTTRAP_JS		0x78
98*0Sstevel@tonic-gate #define	FASTTRAP_JNS		0x79
99*0Sstevel@tonic-gate #define	FASTTRAP_JP		0x7a
100*0Sstevel@tonic-gate #define	FASTTRAP_JNP		0x7b
101*0Sstevel@tonic-gate #define	FASTTRAP_JL		0x7c
102*0Sstevel@tonic-gate #define	FASTTRAP_JGE		0x7d
103*0Sstevel@tonic-gate #define	FASTTRAP_JLE		0x7e
104*0Sstevel@tonic-gate #define	FASTTRAP_JG		0x7f
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate #define	FASTTRAP_MOV_EAX	0xb8
107*0Sstevel@tonic-gate #define	FASTTRAP_MOV_ECX	0xb9
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #define	FASTTRAP_RET16		0xc2
110*0Sstevel@tonic-gate #define	FASTTRAP_RET		0xc3
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #define	FASTTRAP_LOOPNZ		0xe0
113*0Sstevel@tonic-gate #define	FASTTRAP_LOOPZ		0xe1
114*0Sstevel@tonic-gate #define	FASTTRAP_LOOP		0xe2
115*0Sstevel@tonic-gate #define	FASTTRAP_JCXZ		0xe3
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate #define	FASTTRAP_CALL		0xe8
118*0Sstevel@tonic-gate #define	FASTTRAP_JMP32		0xe9
119*0Sstevel@tonic-gate #define	FASTTRAP_JMP8		0xeb
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate #define	FASTTRAP_INT3		0xcc
122*0Sstevel@tonic-gate #define	FASTTRAP_INT		0xcd
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate #define	FASTTRAP_2_BYTE_OP	0x0f
125*0Sstevel@tonic-gate #define	FASTTRAP_GROUP5_OP	0xff
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * Two-byte op-codes (second byte only).
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate #define	FASTTRAP_0F_JO		0x80
131*0Sstevel@tonic-gate #define	FASTTRAP_0F_JNO		0x81
132*0Sstevel@tonic-gate #define	FASTTRAP_0F_JB		0x82
133*0Sstevel@tonic-gate #define	FASTTRAP_0F_JAE		0x83
134*0Sstevel@tonic-gate #define	FASTTRAP_0F_JE		0x84
135*0Sstevel@tonic-gate #define	FASTTRAP_0F_JNE		0x85
136*0Sstevel@tonic-gate #define	FASTTRAP_0F_JBE		0x86
137*0Sstevel@tonic-gate #define	FASTTRAP_0F_JA		0x87
138*0Sstevel@tonic-gate #define	FASTTRAP_0F_JS		0x88
139*0Sstevel@tonic-gate #define	FASTTRAP_0F_JNS		0x89
140*0Sstevel@tonic-gate #define	FASTTRAP_0F_JP		0x8a
141*0Sstevel@tonic-gate #define	FASTTRAP_0F_JNP		0x8b
142*0Sstevel@tonic-gate #define	FASTTRAP_0F_JL		0x8c
143*0Sstevel@tonic-gate #define	FASTTRAP_0F_JGE		0x8d
144*0Sstevel@tonic-gate #define	FASTTRAP_0F_JLE		0x8e
145*0Sstevel@tonic-gate #define	FASTTRAP_0F_JG		0x8f
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_OF	0x800
148*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_DF	0x400
149*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_SF	0x080
150*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_ZF	0x040
151*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_AF	0x010
152*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_PF	0x004
153*0Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_CF	0x001
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * Instruction prefixes.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_OPERAND	0x66
159*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_ADDRESS	0x67
160*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_CS	0x2E
161*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_DS	0x3E
162*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_ES	0x26
163*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_FS	0x64
164*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_GS	0x65
165*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_SS	0x36
166*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_LOCK	0xF0
167*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_REP	0xF3
168*0Sstevel@tonic-gate #define	FASTTRAP_PREFIX_REPNE	0xF2
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate #define	FASTTRAP_NOREG	0xff
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * Map between instruction register encodings and the kernel constants which
174*0Sstevel@tonic-gate  * correspond to indicies into struct regs.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate #ifdef __amd64
177*0Sstevel@tonic-gate static const uint8_t regmap[16] = {
178*0Sstevel@tonic-gate 	REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
179*0Sstevel@tonic-gate 	REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
180*0Sstevel@tonic-gate };
181*0Sstevel@tonic-gate #else
182*0Sstevel@tonic-gate static const uint8_t regmap[8] = {
183*0Sstevel@tonic-gate 	EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
184*0Sstevel@tonic-gate };
185*0Sstevel@tonic-gate #endif
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static uint64_t
190*0Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int function_entry, int argno)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	uint64_t value;
193*0Sstevel@tonic-gate 	int shift = function_entry ? 1 : 0;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate #ifdef __amd64
196*0Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_LP64) {
197*0Sstevel@tonic-gate 		uintptr_t *stack;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		/*
200*0Sstevel@tonic-gate 		 * In 64-bit mode, the first six arguments are stored in
201*0Sstevel@tonic-gate 		 * registers.
202*0Sstevel@tonic-gate 		 */
203*0Sstevel@tonic-gate 		if (argno < 6)
204*0Sstevel@tonic-gate 			return ((&rp->r_rdi)[argno]);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		stack = (uintptr_t *)rp->r_sp;
207*0Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
208*0Sstevel@tonic-gate 		value = dtrace_fulword(&stack[argno - 6 + shift]);
209*0Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
210*0Sstevel@tonic-gate 	} else {
211*0Sstevel@tonic-gate #endif
212*0Sstevel@tonic-gate 		uint32_t *stack = (uint32_t *)rp->r_sp;
213*0Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
214*0Sstevel@tonic-gate 		value = dtrace_fuword32(&stack[argno + shift]);
215*0Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
216*0Sstevel@tonic-gate #ifdef __amd64
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate #endif
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	return (value);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /*ARGSUSED*/
224*0Sstevel@tonic-gate int
225*0Sstevel@tonic-gate fasttrap_tracepoint_init(proc_t *p, fasttrap_probe_t *probe,
226*0Sstevel@tonic-gate     fasttrap_tracepoint_t *tp, uintptr_t pc)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
229*0Sstevel@tonic-gate 	size_t len = FASTTRAP_MAX_INSTR_SIZE;
230*0Sstevel@tonic-gate 	size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
231*0Sstevel@tonic-gate 	uint_t start = 0;
232*0Sstevel@tonic-gate 	int rmindex;
233*0Sstevel@tonic-gate 	uint8_t rex = 0;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * Read the instruction at the given address out of the process's
237*0Sstevel@tonic-gate 	 * address space. We don't have to worry about a debugger
238*0Sstevel@tonic-gate 	 * changing this instruction before we overwrite it with our trap
239*0Sstevel@tonic-gate 	 * instruction since P_PR_LOCK is set. Since instructions can span
240*0Sstevel@tonic-gate 	 * pages, we potentially read the instruction in two parts. If the
241*0Sstevel@tonic-gate 	 * second part fails, we just zero out that part of the instruction.
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	if (uread(p, &instr[0], first, pc) != 0)
244*0Sstevel@tonic-gate 		return (-1);
245*0Sstevel@tonic-gate 	if (len > first &&
246*0Sstevel@tonic-gate 	    uread(p, &instr[first], len - first, pc + first) != 0) {
247*0Sstevel@tonic-gate 		bzero(&instr[first], len - first);
248*0Sstevel@tonic-gate 		len = first;
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	/*
252*0Sstevel@tonic-gate 	 * If the disassembly fails, then we have a malformed instruction.
253*0Sstevel@tonic-gate 	 */
254*0Sstevel@tonic-gate 	if ((tp->ftt_size = dtrace_instr_size_isa(instr, p->p_model,
255*0Sstevel@tonic-gate 	    &rmindex)) <= 0)
256*0Sstevel@tonic-gate 		return (-1);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/*
259*0Sstevel@tonic-gate 	 * Make sure the disassembler isn't completely broken.
260*0Sstevel@tonic-gate 	 */
261*0Sstevel@tonic-gate 	ASSERT(-1 <= rmindex && rmindex < tp->ftt_size);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	/*
264*0Sstevel@tonic-gate 	 * If the computed size is greater than the number of bytes read,
265*0Sstevel@tonic-gate 	 * then it was a malformed instruction possibly because it fell on a
266*0Sstevel@tonic-gate 	 * page boundary and the subsequent page was missing or because of
267*0Sstevel@tonic-gate 	 * some malicious user.
268*0Sstevel@tonic-gate 	 */
269*0Sstevel@tonic-gate 	if (tp->ftt_size > len)
270*0Sstevel@tonic-gate 		return (-1);
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	/*
273*0Sstevel@tonic-gate 	 * Find the start of the instruction's opcode by processing any
274*0Sstevel@tonic-gate 	 * legacy prefixes.
275*0Sstevel@tonic-gate 	 */
276*0Sstevel@tonic-gate 	for (;;) {
277*0Sstevel@tonic-gate 		switch (instr[start]) {
278*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_OPERAND:
279*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_ADDRESS:
280*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_CS:
281*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_DS:
282*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_ES:
283*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_FS:
284*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_GS:
285*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_SS:
286*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_LOCK:
287*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_REP:
288*0Sstevel@tonic-gate 		case FASTTRAP_PREFIX_REPNE:
289*0Sstevel@tonic-gate 			start++;
290*0Sstevel@tonic-gate 			continue;
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 		break;
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate #ifdef __amd64
296*0Sstevel@tonic-gate 	/*
297*0Sstevel@tonic-gate 	 * Identify the REX prefix on 64-bit processes.
298*0Sstevel@tonic-gate 	 */
299*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
300*0Sstevel@tonic-gate 		rex = instr[start++];
301*0Sstevel@tonic-gate #endif
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	/*
304*0Sstevel@tonic-gate 	 * Now that we're pretty sure that the instruction is okay, copy the
305*0Sstevel@tonic-gate 	 * valid part to the tracepoint.
306*0Sstevel@tonic-gate 	 */
307*0Sstevel@tonic-gate 	bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	tp->ftt_type = FASTTRAP_T_COMMON;
310*0Sstevel@tonic-gate 	if (instr[start] == FASTTRAP_2_BYTE_OP) {
311*0Sstevel@tonic-gate 		switch (instr[start + 1]) {
312*0Sstevel@tonic-gate 		case FASTTRAP_0F_JO:
313*0Sstevel@tonic-gate 		case FASTTRAP_0F_JNO:
314*0Sstevel@tonic-gate 		case FASTTRAP_0F_JB:
315*0Sstevel@tonic-gate 		case FASTTRAP_0F_JAE:
316*0Sstevel@tonic-gate 		case FASTTRAP_0F_JE:
317*0Sstevel@tonic-gate 		case FASTTRAP_0F_JNE:
318*0Sstevel@tonic-gate 		case FASTTRAP_0F_JBE:
319*0Sstevel@tonic-gate 		case FASTTRAP_0F_JA:
320*0Sstevel@tonic-gate 		case FASTTRAP_0F_JS:
321*0Sstevel@tonic-gate 		case FASTTRAP_0F_JNS:
322*0Sstevel@tonic-gate 		case FASTTRAP_0F_JP:
323*0Sstevel@tonic-gate 		case FASTTRAP_0F_JNP:
324*0Sstevel@tonic-gate 		case FASTTRAP_0F_JL:
325*0Sstevel@tonic-gate 		case FASTTRAP_0F_JGE:
326*0Sstevel@tonic-gate 		case FASTTRAP_0F_JLE:
327*0Sstevel@tonic-gate 		case FASTTRAP_0F_JG:
328*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCC;
329*0Sstevel@tonic-gate 			tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
330*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
331*0Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 2];
332*0Sstevel@tonic-gate 			break;
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 	} else if (instr[start] == FASTTRAP_GROUP5_OP) {
335*0Sstevel@tonic-gate 		uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
336*0Sstevel@tonic-gate 		uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
337*0Sstevel@tonic-gate 		uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 		if (reg == 2 || reg == 4) {
340*0Sstevel@tonic-gate 			uint_t i, sz;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 			if (reg == 2)
343*0Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CALL;
344*0Sstevel@tonic-gate 			else
345*0Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_JMP;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 			if (mod == 3)
348*0Sstevel@tonic-gate 				tp->ftt_code = 2;
349*0Sstevel@tonic-gate 			else
350*0Sstevel@tonic-gate 				tp->ftt_code = 1;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 			ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 			/*
355*0Sstevel@tonic-gate 			 * See AMD x86-64 Architecture Programmer's Manual
356*0Sstevel@tonic-gate 			 * Volume 3, Section 1.2.7, Table 1-12, and
357*0Sstevel@tonic-gate 			 * Appendix A.3.1, Table A-15.
358*0Sstevel@tonic-gate 			 */
359*0Sstevel@tonic-gate 			if (mod != 3 && rm == 4) {
360*0Sstevel@tonic-gate 				uint8_t sib = instr[start + 2];
361*0Sstevel@tonic-gate 				uint_t index = FASTTRAP_SIB_INDEX(sib);
362*0Sstevel@tonic-gate 				uint_t base = FASTTRAP_SIB_BASE(sib);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 				tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 				tp->ftt_index = (index == 4) ?
367*0Sstevel@tonic-gate 				    FASTTRAP_NOREG :
368*0Sstevel@tonic-gate 				    regmap[index | (FASTTRAP_REX_X(rex) << 3)];
369*0Sstevel@tonic-gate 				tp->ftt_base = (mod == 0 && base == 5) ?
370*0Sstevel@tonic-gate 				    FASTTRAP_NOREG :
371*0Sstevel@tonic-gate 				    regmap[base | (FASTTRAP_REX_B(rex) << 3)];
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 				i = 3;
374*0Sstevel@tonic-gate 				sz = mod == 1 ? 1 : 4;
375*0Sstevel@tonic-gate 			} else {
376*0Sstevel@tonic-gate 				/*
377*0Sstevel@tonic-gate 				 * In 64-bit mode, mod == 0 and r/m == 5
378*0Sstevel@tonic-gate 				 * denotes %rip-relative addressing; in 32-bit
379*0Sstevel@tonic-gate 				 * mode, the base register isn't used. In both
380*0Sstevel@tonic-gate 				 * modes, there is a 32-bit operand.
381*0Sstevel@tonic-gate 				 */
382*0Sstevel@tonic-gate 				if (mod == 0 && rm == 5) {
383*0Sstevel@tonic-gate #ifdef __amd64
384*0Sstevel@tonic-gate 					if (p->p_model == DATAMODEL_LP64)
385*0Sstevel@tonic-gate 						tp->ftt_base = REG_RIP;
386*0Sstevel@tonic-gate 					else
387*0Sstevel@tonic-gate #endif
388*0Sstevel@tonic-gate 						tp->ftt_base = FASTTRAP_NOREG;
389*0Sstevel@tonic-gate 					sz = 4;
390*0Sstevel@tonic-gate 				} else  {
391*0Sstevel@tonic-gate 					uint8_t base = rm |
392*0Sstevel@tonic-gate 					    (FASTTRAP_REX_B(rex) << 3);
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 					tp->ftt_base = regmap[base];
395*0Sstevel@tonic-gate 					sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
396*0Sstevel@tonic-gate 				}
397*0Sstevel@tonic-gate 				tp->ftt_index = FASTTRAP_NOREG;
398*0Sstevel@tonic-gate 				i = 2;
399*0Sstevel@tonic-gate 			}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 			if (sz == 1)
402*0Sstevel@tonic-gate 				tp->ftt_dest = *(int8_t *)&instr[start + i];
403*0Sstevel@tonic-gate 			else if (sz == 4)
404*0Sstevel@tonic-gate 				tp->ftt_dest = *(int32_t *)&instr[start + i];
405*0Sstevel@tonic-gate 			else
406*0Sstevel@tonic-gate 				tp->ftt_dest = 0;
407*0Sstevel@tonic-gate 		}
408*0Sstevel@tonic-gate 	} else {
409*0Sstevel@tonic-gate 		switch (instr[start]) {
410*0Sstevel@tonic-gate 		case FASTTRAP_RET:
411*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RET;
412*0Sstevel@tonic-gate 			break;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 		case FASTTRAP_RET16:
415*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RET16;
416*0Sstevel@tonic-gate 			tp->ftt_dest = *(uint16_t *)&instr[start + 1];
417*0Sstevel@tonic-gate 			break;
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 		case FASTTRAP_JO:
420*0Sstevel@tonic-gate 		case FASTTRAP_JNO:
421*0Sstevel@tonic-gate 		case FASTTRAP_JB:
422*0Sstevel@tonic-gate 		case FASTTRAP_JAE:
423*0Sstevel@tonic-gate 		case FASTTRAP_JE:
424*0Sstevel@tonic-gate 		case FASTTRAP_JNE:
425*0Sstevel@tonic-gate 		case FASTTRAP_JBE:
426*0Sstevel@tonic-gate 		case FASTTRAP_JA:
427*0Sstevel@tonic-gate 		case FASTTRAP_JS:
428*0Sstevel@tonic-gate 		case FASTTRAP_JNS:
429*0Sstevel@tonic-gate 		case FASTTRAP_JP:
430*0Sstevel@tonic-gate 		case FASTTRAP_JNP:
431*0Sstevel@tonic-gate 		case FASTTRAP_JL:
432*0Sstevel@tonic-gate 		case FASTTRAP_JGE:
433*0Sstevel@tonic-gate 		case FASTTRAP_JLE:
434*0Sstevel@tonic-gate 		case FASTTRAP_JG:
435*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCC;
436*0Sstevel@tonic-gate 			tp->ftt_code = instr[start];
437*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
438*0Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
439*0Sstevel@tonic-gate 			break;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 		case FASTTRAP_LOOPNZ:
442*0Sstevel@tonic-gate 		case FASTTRAP_LOOPZ:
443*0Sstevel@tonic-gate 		case FASTTRAP_LOOP:
444*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_LOOP;
445*0Sstevel@tonic-gate 			tp->ftt_code = instr[start];
446*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
447*0Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
448*0Sstevel@tonic-gate 			break;
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 		case FASTTRAP_JCXZ:
451*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCXZ;
452*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
453*0Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
454*0Sstevel@tonic-gate 			break;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 		case FASTTRAP_CALL:
457*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_CALL;
458*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
459*0Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 1];
460*0Sstevel@tonic-gate 			tp->ftt_code = 0;
461*0Sstevel@tonic-gate 			break;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 		case FASTTRAP_JMP32:
464*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMP;
465*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
466*0Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 1];
467*0Sstevel@tonic-gate 			break;
468*0Sstevel@tonic-gate 		case FASTTRAP_JMP8:
469*0Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMP;
470*0Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
471*0Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
472*0Sstevel@tonic-gate 			break;
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 		case FASTTRAP_PUSHL_EBP:
475*0Sstevel@tonic-gate 			if (start == 0)
476*0Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
477*0Sstevel@tonic-gate 			break;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 		case FASTTRAP_INT3:
480*0Sstevel@tonic-gate 			/*
481*0Sstevel@tonic-gate 			 * The pid provider shares the int3 trap with debugger
482*0Sstevel@tonic-gate 			 * breakpoints so we can't instrument them.
483*0Sstevel@tonic-gate 			 */
484*0Sstevel@tonic-gate 			ASSERT(instr[start] == FASTTRAP_INSTR);
485*0Sstevel@tonic-gate 			return (-1);
486*0Sstevel@tonic-gate 		}
487*0Sstevel@tonic-gate 	}
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate #ifdef __amd64
490*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
491*0Sstevel@tonic-gate 		/*
492*0Sstevel@tonic-gate 		 * If the process is 64-bit and the instruction type is still
493*0Sstevel@tonic-gate 		 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
494*0Sstevel@tonic-gate 		 * execute it -- we need to watch for %rip-relative
495*0Sstevel@tonic-gate 		 * addressing mode. See the portion of fasttrap_pid_probe()
496*0Sstevel@tonic-gate 		 * below where we handle tracepoints with type
497*0Sstevel@tonic-gate 		 * FASTTRAP_T_COMMON for how we emulate instructions that
498*0Sstevel@tonic-gate 		 * employ %rip-relative addressing.
499*0Sstevel@tonic-gate 		 */
500*0Sstevel@tonic-gate 		if (rmindex != -1) {
501*0Sstevel@tonic-gate 			uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
502*0Sstevel@tonic-gate 			uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
503*0Sstevel@tonic-gate 			uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 			ASSERT(rmindex > start);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 			if (mod == 0 && rm == 5) {
508*0Sstevel@tonic-gate 				/*
509*0Sstevel@tonic-gate 				 * We need to be sure to avoid other
510*0Sstevel@tonic-gate 				 * registers used by this instruction. While
511*0Sstevel@tonic-gate 				 * the reg field may determine the op code
512*0Sstevel@tonic-gate 				 * rather than denoting a register, assuming
513*0Sstevel@tonic-gate 				 * that it denotes a register is always safe.
514*0Sstevel@tonic-gate 				 * We leave the REX field intact and use
515*0Sstevel@tonic-gate 				 * whatever value's there for simplicity.
516*0Sstevel@tonic-gate 				 */
517*0Sstevel@tonic-gate 				if (reg != 0) {
518*0Sstevel@tonic-gate 					tp->ftt_ripmode = FASTTRAP_RIP_1 |
519*0Sstevel@tonic-gate 					    (FASTTRAP_RIP_X *
520*0Sstevel@tonic-gate 					    FASTTRAP_REX_B(rex));
521*0Sstevel@tonic-gate 					rm = 0;
522*0Sstevel@tonic-gate 				} else {
523*0Sstevel@tonic-gate 					tp->ftt_ripmode = FASTTRAP_RIP_2 |
524*0Sstevel@tonic-gate 					    (FASTTRAP_RIP_X *
525*0Sstevel@tonic-gate 					    FASTTRAP_REX_B(rex));
526*0Sstevel@tonic-gate 					rm = 1;
527*0Sstevel@tonic-gate 				}
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 				tp->ftt_modrm = tp->ftt_instr[rmindex];
530*0Sstevel@tonic-gate 				tp->ftt_instr[rmindex] =
531*0Sstevel@tonic-gate 				    FASTTRAP_MODRM(2, reg, rm);
532*0Sstevel@tonic-gate 			}
533*0Sstevel@tonic-gate 		}
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate #endif
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	return (0);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate int
541*0Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
542*0Sstevel@tonic-gate {
543*0Sstevel@tonic-gate 	fasttrap_instr_t instr = FASTTRAP_INSTR;
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 	if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
546*0Sstevel@tonic-gate 		return (-1);
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	return (0);
549*0Sstevel@tonic-gate }
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate int
552*0Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
553*0Sstevel@tonic-gate {
554*0Sstevel@tonic-gate 	uint8_t instr;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/*
557*0Sstevel@tonic-gate 	 * Distinguish between read or write failures and a changed
558*0Sstevel@tonic-gate 	 * instruction.
559*0Sstevel@tonic-gate 	 */
560*0Sstevel@tonic-gate 	if (uread(p, &instr, 1, tp->ftt_pc) != 0)
561*0Sstevel@tonic-gate 		return (0);
562*0Sstevel@tonic-gate 	if (instr != FASTTRAP_INSTR)
563*0Sstevel@tonic-gate 		return (0);
564*0Sstevel@tonic-gate 	if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
565*0Sstevel@tonic-gate 		return (-1);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	return (0);
568*0Sstevel@tonic-gate }
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate static uintptr_t
571*0Sstevel@tonic-gate fasttrap_fulword_noerr(const void *uaddr)
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	uintptr_t ret;
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	if (fasttrap_fulword(uaddr, &ret) == 0)
576*0Sstevel@tonic-gate 		return (ret);
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	return (0);
579*0Sstevel@tonic-gate }
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate static uint32_t
582*0Sstevel@tonic-gate fasttrap_fuword32_noerr(const void *uaddr)
583*0Sstevel@tonic-gate {
584*0Sstevel@tonic-gate 	uint32_t ret;
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	if (fasttrap_fuword32(uaddr, &ret) == 0)
587*0Sstevel@tonic-gate 		return (ret);
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	return (0);
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /*ARGSUSED*/
593*0Sstevel@tonic-gate int
594*0Sstevel@tonic-gate fasttrap_probe(struct regs *rp)
595*0Sstevel@tonic-gate {
596*0Sstevel@tonic-gate #ifdef __amd64
597*0Sstevel@tonic-gate 	proc_t *p = curproc;
598*0Sstevel@tonic-gate #endif
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate #ifdef __amd64
601*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64) {
602*0Sstevel@tonic-gate 		dtrace_probe(fasttrap_probe_id, rp->r_rdi, rp->r_rsi,
603*0Sstevel@tonic-gate 		    rp->r_rdx, rp->r_rcx, rp->r_r8);
604*0Sstevel@tonic-gate 	} else {
605*0Sstevel@tonic-gate #endif
606*0Sstevel@tonic-gate 		uint32_t *stack = (uint32_t *)rp->r_sp;
607*0Sstevel@tonic-gate 		uintptr_t s0, s1, s2, s3, s4;
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 		s0 = fasttrap_fuword32_noerr(&stack[1]);
610*0Sstevel@tonic-gate 		s1 = fasttrap_fuword32_noerr(&stack[2]);
611*0Sstevel@tonic-gate 		s2 = fasttrap_fuword32_noerr(&stack[3]);
612*0Sstevel@tonic-gate 		s3 = fasttrap_fuword32_noerr(&stack[4]);
613*0Sstevel@tonic-gate 		s4 = fasttrap_fuword32_noerr(&stack[5]);
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		dtrace_probe(fasttrap_probe_id, s0, s1, s2, s3, s4);
616*0Sstevel@tonic-gate #ifdef __amd64
617*0Sstevel@tonic-gate 	}
618*0Sstevel@tonic-gate #endif
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	return (0);
621*0Sstevel@tonic-gate }
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate static void
624*0Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
625*0Sstevel@tonic-gate     uintptr_t new_pc)
626*0Sstevel@tonic-gate {
627*0Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
628*0Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
629*0Sstevel@tonic-gate 	fasttrap_id_t *id;
630*0Sstevel@tonic-gate 	kmutex_t *pid_mtx;
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
633*0Sstevel@tonic-gate 	mutex_enter(pid_mtx);
634*0Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
637*0Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
638*0Sstevel@tonic-gate 		    !tp->ftt_prov->ftp_defunct)
639*0Sstevel@tonic-gate 			break;
640*0Sstevel@tonic-gate 	}
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	/*
643*0Sstevel@tonic-gate 	 * Don't sweat it if we can't find the tracepoint again; unlike
644*0Sstevel@tonic-gate 	 * when we're in fasttrap_pid_probe(), finding the tracepoint here
645*0Sstevel@tonic-gate 	 * is not essential to the correct execution of the process.
646*0Sstevel@tonic-gate 	 */
647*0Sstevel@tonic-gate 	if (tp == NULL) {
648*0Sstevel@tonic-gate 		mutex_exit(pid_mtx);
649*0Sstevel@tonic-gate 		return;
650*0Sstevel@tonic-gate 	}
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
653*0Sstevel@tonic-gate 		/*
654*0Sstevel@tonic-gate 		 * If there's a branch that could act as a return site, we
655*0Sstevel@tonic-gate 		 * need to trace it, and check here if the program counter is
656*0Sstevel@tonic-gate 		 * external to the function.
657*0Sstevel@tonic-gate 		 */
658*0Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_RET &&
659*0Sstevel@tonic-gate 		    tp->ftt_type != FASTTRAP_T_RET16 &&
660*0Sstevel@tonic-gate 		    new_pc - id->fti_probe->ftp_faddr <
661*0Sstevel@tonic-gate 		    id->fti_probe->ftp_fsize)
662*0Sstevel@tonic-gate 			continue;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 		dtrace_probe(id->fti_probe->ftp_id,
665*0Sstevel@tonic-gate 		    pc - id->fti_probe->ftp_faddr,
666*0Sstevel@tonic-gate 		    rp->r_r0, rp->r_r1, 0, 0);
667*0Sstevel@tonic-gate 	}
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	mutex_exit(pid_mtx);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate static void
673*0Sstevel@tonic-gate fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
674*0Sstevel@tonic-gate {
675*0Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGSEGV;
678*0Sstevel@tonic-gate 	sqp->sq_info.si_code = SEGV_MAPERR;
679*0Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)addr;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
682*0Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
683*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	if (t != NULL)
686*0Sstevel@tonic-gate 		aston(t);
687*0Sstevel@tonic-gate }
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate #ifdef __amd64
690*0Sstevel@tonic-gate static void
691*0Sstevel@tonic-gate fasttrap_usdt_args64(fasttrap_probe_t *probe, struct regs *rp, int argc,
692*0Sstevel@tonic-gate     uintptr_t *argv)
693*0Sstevel@tonic-gate {
694*0Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
695*0Sstevel@tonic-gate 	uintptr_t *stack = (uintptr_t *)rp->r_sp;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	for (i = 0; i < cap; i++) {
698*0Sstevel@tonic-gate 		x = probe->ftp_argmap[i];
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 		if (x < 6)
701*0Sstevel@tonic-gate 			argv[i] = (&rp->r_rdi)[x];
702*0Sstevel@tonic-gate 		else
703*0Sstevel@tonic-gate 			argv[i] = fasttrap_fulword_noerr(&stack[x]);
704*0Sstevel@tonic-gate 	}
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	for (; i < argc; i++) {
707*0Sstevel@tonic-gate 		argv[i] = 0;
708*0Sstevel@tonic-gate 	}
709*0Sstevel@tonic-gate }
710*0Sstevel@tonic-gate #endif
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate static void
713*0Sstevel@tonic-gate fasttrap_usdt_args32(fasttrap_probe_t *probe, struct regs *rp, int argc,
714*0Sstevel@tonic-gate     uint32_t *argv)
715*0Sstevel@tonic-gate {
716*0Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
717*0Sstevel@tonic-gate 	uint32_t *stack = (uint32_t *)rp->r_sp;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	for (i = 0; i < cap; i++) {
720*0Sstevel@tonic-gate 		x = probe->ftp_argmap[i];
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		argv[i] = fasttrap_fuword32_noerr(&stack[x]);
723*0Sstevel@tonic-gate 	}
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	for (; i < argc; i++) {
726*0Sstevel@tonic-gate 		argv[i] = 0;
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate }
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate int
731*0Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	proc_t *p = curproc;
734*0Sstevel@tonic-gate 	uintptr_t pc = rp->r_pc - 1, new_pc = 0;
735*0Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
736*0Sstevel@tonic-gate 	kmutex_t *pid_mtx;
737*0Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, tp_local;
738*0Sstevel@tonic-gate 	pid_t pid;
739*0Sstevel@tonic-gate 	dtrace_icookie_t cookie;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	/*
742*0Sstevel@tonic-gate 	 * It's possible that a user (in a veritable orgy of bad planning)
743*0Sstevel@tonic-gate 	 * could redirect this thread's flow of control before it reached the
744*0Sstevel@tonic-gate 	 * return probe fasttrap. In this case we need to kill the process
745*0Sstevel@tonic-gate 	 * since it's in a unrecoverable state.
746*0Sstevel@tonic-gate 	 */
747*0Sstevel@tonic-gate 	if (curthread->t_dtrace_step) {
748*0Sstevel@tonic-gate 		ASSERT(curthread->t_dtrace_on);
749*0Sstevel@tonic-gate 		fasttrap_sigtrap(p, curthread, pc);
750*0Sstevel@tonic-gate 		return (0);
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	/*
754*0Sstevel@tonic-gate 	 * Clear all user tracing flags.
755*0Sstevel@tonic-gate 	 */
756*0Sstevel@tonic-gate 	curthread->t_dtrace_ft = 0;
757*0Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
758*0Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
759*0Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
760*0Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
761*0Sstevel@tonic-gate #ifdef __amd64
762*0Sstevel@tonic-gate 	curthread->t_dtrace_regv = 0;
763*0Sstevel@tonic-gate #endif
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	/*
766*0Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
767*0Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
768*0Sstevel@tonic-gate 	 * process: this one.
769*0Sstevel@tonic-gate 	 */
770*0Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
771*0Sstevel@tonic-gate 		p = p->p_parent;
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	pid = p->p_pid;
775*0Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
776*0Sstevel@tonic-gate 	mutex_enter(pid_mtx);
777*0Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 	/*
780*0Sstevel@tonic-gate 	 * Lookup the tracepoint that the process just hit.
781*0Sstevel@tonic-gate 	 */
782*0Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
783*0Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
784*0Sstevel@tonic-gate 		    !tp->ftt_prov->ftp_defunct)
785*0Sstevel@tonic-gate 			break;
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/*
789*0Sstevel@tonic-gate 	 * If we couldn't find a matching tracepoint, either a tracepoint has
790*0Sstevel@tonic-gate 	 * been inserted without using the pid<pid> ioctl interface (see
791*0Sstevel@tonic-gate 	 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
792*0Sstevel@tonic-gate 	 */
793*0Sstevel@tonic-gate 	if (tp == NULL) {
794*0Sstevel@tonic-gate 		mutex_exit(pid_mtx);
795*0Sstevel@tonic-gate 		return (-1);
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	/*
799*0Sstevel@tonic-gate 	 * Set the program counter to the address of the traced instruction
800*0Sstevel@tonic-gate 	 * so that it looks right in ustack() output.
801*0Sstevel@tonic-gate 	 */
802*0Sstevel@tonic-gate 	rp->r_pc = pc;
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	if (tp->ftt_ids != NULL) {
805*0Sstevel@tonic-gate 		fasttrap_id_t *id;
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate #ifdef __amd64
808*0Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
809*0Sstevel@tonic-gate 			for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
810*0Sstevel@tonic-gate 				fasttrap_probe_t *probe = id->fti_probe;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 				if (probe->ftp_type == DTFTP_ENTRY) {
813*0Sstevel@tonic-gate 					/*
814*0Sstevel@tonic-gate 					 * We note that this was an entry
815*0Sstevel@tonic-gate 					 * probe to help ustack() find the
816*0Sstevel@tonic-gate 					 * first caller.
817*0Sstevel@tonic-gate 					 */
818*0Sstevel@tonic-gate 					cookie = dtrace_interrupt_disable();
819*0Sstevel@tonic-gate 					DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
820*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, rp->r_rdi,
821*0Sstevel@tonic-gate 					    rp->r_rsi, rp->r_rdx, rp->r_rcx,
822*0Sstevel@tonic-gate 					    rp->r_r8);
823*0Sstevel@tonic-gate 					DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
824*0Sstevel@tonic-gate 					dtrace_interrupt_enable(cookie);
825*0Sstevel@tonic-gate 				} else if (probe->ftp_argmap == NULL) {
826*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, rp->r_rdi,
827*0Sstevel@tonic-gate 					    rp->r_rsi, rp->r_rdx, rp->r_rcx,
828*0Sstevel@tonic-gate 					    rp->r_r8);
829*0Sstevel@tonic-gate 				} else {
830*0Sstevel@tonic-gate 					uintptr_t t[5];
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 					fasttrap_usdt_args64(probe, rp,
833*0Sstevel@tonic-gate 					    sizeof (t) / sizeof (t[0]), t);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, t[0], t[1],
836*0Sstevel@tonic-gate 					    t[2], t[3], t[4]);
837*0Sstevel@tonic-gate 				}
838*0Sstevel@tonic-gate 			}
839*0Sstevel@tonic-gate 		} else {
840*0Sstevel@tonic-gate #endif
841*0Sstevel@tonic-gate 			uintptr_t s0, s1, s2, s3, s4, s5;
842*0Sstevel@tonic-gate 			uint32_t *stack = (uint32_t *)rp->r_sp;
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 			/*
845*0Sstevel@tonic-gate 			 * In 32-bit mode, all arguments are passed on the
846*0Sstevel@tonic-gate 			 * stack. If this is a function entry probe, we need
847*0Sstevel@tonic-gate 			 * to skip the first entry on the stack as it
848*0Sstevel@tonic-gate 			 * represents the return address rather than a
849*0Sstevel@tonic-gate 			 * parameter to the function.
850*0Sstevel@tonic-gate 			 */
851*0Sstevel@tonic-gate 			s0 = fasttrap_fuword32_noerr(&stack[0]);
852*0Sstevel@tonic-gate 			s1 = fasttrap_fuword32_noerr(&stack[1]);
853*0Sstevel@tonic-gate 			s2 = fasttrap_fuword32_noerr(&stack[2]);
854*0Sstevel@tonic-gate 			s3 = fasttrap_fuword32_noerr(&stack[3]);
855*0Sstevel@tonic-gate 			s4 = fasttrap_fuword32_noerr(&stack[4]);
856*0Sstevel@tonic-gate 			s5 = fasttrap_fuword32_noerr(&stack[5]);
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 			for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
859*0Sstevel@tonic-gate 				fasttrap_probe_t *probe = id->fti_probe;
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 				if (probe->ftp_type == DTFTP_ENTRY) {
862*0Sstevel@tonic-gate 					/*
863*0Sstevel@tonic-gate 					 * We note that this was an entry
864*0Sstevel@tonic-gate 					 * probe to help ustack() find the
865*0Sstevel@tonic-gate 					 * first caller.
866*0Sstevel@tonic-gate 					 */
867*0Sstevel@tonic-gate 					cookie = dtrace_interrupt_disable();
868*0Sstevel@tonic-gate 					DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
869*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, s1, s2,
870*0Sstevel@tonic-gate 					    s3, s4, s5);
871*0Sstevel@tonic-gate 					DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
872*0Sstevel@tonic-gate 					dtrace_interrupt_enable(cookie);
873*0Sstevel@tonic-gate 				} else if (probe->ftp_argmap == NULL) {
874*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, s0, s1,
875*0Sstevel@tonic-gate 					    s2, s3, s4);
876*0Sstevel@tonic-gate 				} else {
877*0Sstevel@tonic-gate 					uint32_t t[5];
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 					fasttrap_usdt_args32(probe, rp,
880*0Sstevel@tonic-gate 					    sizeof (t) / sizeof (t[0]), t);
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, t[0], t[1],
883*0Sstevel@tonic-gate 					    t[2], t[3], t[4]);
884*0Sstevel@tonic-gate 				}
885*0Sstevel@tonic-gate 			}
886*0Sstevel@tonic-gate #ifdef __amd64
887*0Sstevel@tonic-gate 		}
888*0Sstevel@tonic-gate #endif
889*0Sstevel@tonic-gate 	}
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	/*
892*0Sstevel@tonic-gate 	 * We're about to do a bunch of work so we cache a local copy of
893*0Sstevel@tonic-gate 	 * the tracepoint to emulate the instruction, and then find the
894*0Sstevel@tonic-gate 	 * tracepoint again later if we need to light up any return probes.
895*0Sstevel@tonic-gate 	 */
896*0Sstevel@tonic-gate 	tp_local = *tp;
897*0Sstevel@tonic-gate 	mutex_exit(pid_mtx);
898*0Sstevel@tonic-gate 	tp = &tp_local;
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	/*
901*0Sstevel@tonic-gate 	 * Set the program counter to appear as though the traced instruction
902*0Sstevel@tonic-gate 	 * had completely executed. This ensures that fasttrap_getreg() will
903*0Sstevel@tonic-gate 	 * report the expected value for REG_RIP.
904*0Sstevel@tonic-gate 	 */
905*0Sstevel@tonic-gate 	rp->r_pc = pc + tp->ftt_size;
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 	switch (tp->ftt_type) {
908*0Sstevel@tonic-gate 	case FASTTRAP_T_RET:
909*0Sstevel@tonic-gate 	case FASTTRAP_T_RET16:
910*0Sstevel@tonic-gate 	{
911*0Sstevel@tonic-gate 		uintptr_t dst;
912*0Sstevel@tonic-gate 		uintptr_t addr;
913*0Sstevel@tonic-gate 		int ret;
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 		/*
916*0Sstevel@tonic-gate 		 * We have to emulate _every_ facet of the behavior of a ret
917*0Sstevel@tonic-gate 		 * instruction including what happens if the load from %esp
918*0Sstevel@tonic-gate 		 * fails; in that case, we send a SIGSEGV.
919*0Sstevel@tonic-gate 		 */
920*0Sstevel@tonic-gate #ifdef __amd64
921*0Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE) {
922*0Sstevel@tonic-gate #endif
923*0Sstevel@tonic-gate 			ret = fasttrap_fulword((void *)rp->r_sp, &dst);
924*0Sstevel@tonic-gate 			addr = rp->r_sp + sizeof (uintptr_t);
925*0Sstevel@tonic-gate #ifdef __amd64
926*0Sstevel@tonic-gate 		} else {
927*0Sstevel@tonic-gate 			uint32_t dst32;
928*0Sstevel@tonic-gate 			ret = fasttrap_fuword32((void *)rp->r_sp, &dst32);
929*0Sstevel@tonic-gate 			dst = dst32;
930*0Sstevel@tonic-gate 			addr = rp->r_sp + sizeof (uint32_t);
931*0Sstevel@tonic-gate 		}
932*0Sstevel@tonic-gate #endif
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate 		if (ret == -1) {
935*0Sstevel@tonic-gate 			fasttrap_sigsegv(p, curthread, rp->r_sp);
936*0Sstevel@tonic-gate 			new_pc = pc;
937*0Sstevel@tonic-gate 			break;
938*0Sstevel@tonic-gate 		}
939*0Sstevel@tonic-gate 
940*0Sstevel@tonic-gate 		if (tp->ftt_type == FASTTRAP_T_RET16)
941*0Sstevel@tonic-gate 			addr += tp->ftt_dest;
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 		rp->r_sp = addr;
944*0Sstevel@tonic-gate 		new_pc = dst;
945*0Sstevel@tonic-gate 		break;
946*0Sstevel@tonic-gate 	}
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 	case FASTTRAP_T_JCC:
949*0Sstevel@tonic-gate 	{
950*0Sstevel@tonic-gate 		uint_t taken;
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 		switch (tp->ftt_code) {
953*0Sstevel@tonic-gate 		case FASTTRAP_JO:
954*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) != 0;
955*0Sstevel@tonic-gate 			break;
956*0Sstevel@tonic-gate 		case FASTTRAP_JNO:
957*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) == 0;
958*0Sstevel@tonic-gate 			break;
959*0Sstevel@tonic-gate 		case FASTTRAP_JB:
960*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0;
961*0Sstevel@tonic-gate 			break;
962*0Sstevel@tonic-gate 		case FASTTRAP_JAE:
963*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0;
964*0Sstevel@tonic-gate 			break;
965*0Sstevel@tonic-gate 		case FASTTRAP_JE:
966*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
967*0Sstevel@tonic-gate 			break;
968*0Sstevel@tonic-gate 		case FASTTRAP_JNE:
969*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
970*0Sstevel@tonic-gate 			break;
971*0Sstevel@tonic-gate 		case FASTTRAP_JBE:
972*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0 ||
973*0Sstevel@tonic-gate 			    (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
974*0Sstevel@tonic-gate 			break;
975*0Sstevel@tonic-gate 		case FASTTRAP_JA:
976*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0 &&
977*0Sstevel@tonic-gate 			    (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
978*0Sstevel@tonic-gate 			break;
979*0Sstevel@tonic-gate 		case FASTTRAP_JS:
980*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) != 0;
981*0Sstevel@tonic-gate 			break;
982*0Sstevel@tonic-gate 		case FASTTRAP_JNS:
983*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) == 0;
984*0Sstevel@tonic-gate 			break;
985*0Sstevel@tonic-gate 		case FASTTRAP_JP:
986*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) != 0;
987*0Sstevel@tonic-gate 			break;
988*0Sstevel@tonic-gate 		case FASTTRAP_JNP:
989*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) == 0;
990*0Sstevel@tonic-gate 			break;
991*0Sstevel@tonic-gate 		case FASTTRAP_JL:
992*0Sstevel@tonic-gate 			taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
993*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
994*0Sstevel@tonic-gate 			break;
995*0Sstevel@tonic-gate 		case FASTTRAP_JGE:
996*0Sstevel@tonic-gate 			taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
997*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
998*0Sstevel@tonic-gate 			break;
999*0Sstevel@tonic-gate 		case FASTTRAP_JLE:
1000*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 ||
1001*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
1002*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
1003*0Sstevel@tonic-gate 			break;
1004*0Sstevel@tonic-gate 		case FASTTRAP_JG:
1005*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
1006*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
1007*0Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
1008*0Sstevel@tonic-gate 			break;
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 		}
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 		if (taken)
1013*0Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
1014*0Sstevel@tonic-gate 		else
1015*0Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
1016*0Sstevel@tonic-gate 		break;
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 	case FASTTRAP_T_LOOP:
1020*0Sstevel@tonic-gate 	{
1021*0Sstevel@tonic-gate 		uint_t taken;
1022*0Sstevel@tonic-gate #ifdef __amd64
1023*0Sstevel@tonic-gate 		greg_t cx = rp->r_rcx--;
1024*0Sstevel@tonic-gate #else
1025*0Sstevel@tonic-gate 		greg_t cx = rp->r_ecx--;
1026*0Sstevel@tonic-gate #endif
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate 		switch (tp->ftt_code) {
1029*0Sstevel@tonic-gate 		case FASTTRAP_LOOPNZ:
1030*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
1031*0Sstevel@tonic-gate 			    cx != 0;
1032*0Sstevel@tonic-gate 			break;
1033*0Sstevel@tonic-gate 		case FASTTRAP_LOOPZ:
1034*0Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 &&
1035*0Sstevel@tonic-gate 			    cx != 0;
1036*0Sstevel@tonic-gate 			break;
1037*0Sstevel@tonic-gate 		case FASTTRAP_LOOP:
1038*0Sstevel@tonic-gate 			taken = (cx != 0);
1039*0Sstevel@tonic-gate 			break;
1040*0Sstevel@tonic-gate 		}
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 		if (taken)
1043*0Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
1044*0Sstevel@tonic-gate 		else
1045*0Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
1046*0Sstevel@tonic-gate 		break;
1047*0Sstevel@tonic-gate 	}
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 	case FASTTRAP_T_JCXZ:
1050*0Sstevel@tonic-gate 	{
1051*0Sstevel@tonic-gate #ifdef __amd64
1052*0Sstevel@tonic-gate 		greg_t cx = rp->r_rcx;
1053*0Sstevel@tonic-gate #else
1054*0Sstevel@tonic-gate 		greg_t cx = rp->r_ecx;
1055*0Sstevel@tonic-gate #endif
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 		if (cx == 0)
1058*0Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
1059*0Sstevel@tonic-gate 		else
1060*0Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
1061*0Sstevel@tonic-gate 		break;
1062*0Sstevel@tonic-gate 	}
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	case FASTTRAP_T_PUSHL_EBP:
1065*0Sstevel@tonic-gate 	{
1066*0Sstevel@tonic-gate 		int ret;
1067*0Sstevel@tonic-gate 		uintptr_t addr;
1068*0Sstevel@tonic-gate #ifdef __amd64
1069*0Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE) {
1070*0Sstevel@tonic-gate #endif
1071*0Sstevel@tonic-gate 			addr = rp->r_sp - sizeof (uintptr_t);
1072*0Sstevel@tonic-gate 			ret = fasttrap_sulword((void *)addr, rp->r_fp);
1073*0Sstevel@tonic-gate #ifdef __amd64
1074*0Sstevel@tonic-gate 		} else {
1075*0Sstevel@tonic-gate 			addr = rp->r_sp - sizeof (uint32_t);
1076*0Sstevel@tonic-gate 			ret = fasttrap_suword32((void *)addr,
1077*0Sstevel@tonic-gate 			    (uint32_t)rp->r_fp);
1078*0Sstevel@tonic-gate 		}
1079*0Sstevel@tonic-gate #endif
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 		if (ret == -1) {
1082*0Sstevel@tonic-gate 			fasttrap_sigsegv(p, curthread, addr);
1083*0Sstevel@tonic-gate 			new_pc = pc;
1084*0Sstevel@tonic-gate 			break;
1085*0Sstevel@tonic-gate 		}
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 		rp->r_sp = addr;
1088*0Sstevel@tonic-gate 		new_pc = pc + tp->ftt_size;
1089*0Sstevel@tonic-gate 		break;
1090*0Sstevel@tonic-gate 	}
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	case FASTTRAP_T_JMP:
1093*0Sstevel@tonic-gate 	case FASTTRAP_T_CALL:
1094*0Sstevel@tonic-gate 		if (tp->ftt_code == 0) {
1095*0Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
1096*0Sstevel@tonic-gate 		} else {
1097*0Sstevel@tonic-gate 			uintptr_t addr = tp->ftt_dest;
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 			if (tp->ftt_base != FASTTRAP_NOREG)
1100*0Sstevel@tonic-gate 				addr += fasttrap_getreg(rp, tp->ftt_base);
1101*0Sstevel@tonic-gate 			if (tp->ftt_index != FASTTRAP_NOREG)
1102*0Sstevel@tonic-gate 				addr += fasttrap_getreg(rp, tp->ftt_index) <<
1103*0Sstevel@tonic-gate 				    tp->ftt_scale;
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate 			if (tp->ftt_code == 1) {
1106*0Sstevel@tonic-gate #ifdef __amd64
1107*0Sstevel@tonic-gate 				if (p->p_model == DATAMODEL_NATIVE) {
1108*0Sstevel@tonic-gate #endif
1109*0Sstevel@tonic-gate 					uintptr_t value;
1110*0Sstevel@tonic-gate 					if (fasttrap_fulword((void *)addr,
1111*0Sstevel@tonic-gate 					    &value) == -1) {
1112*0Sstevel@tonic-gate 						fasttrap_sigsegv(p, curthread,
1113*0Sstevel@tonic-gate 						    addr);
1114*0Sstevel@tonic-gate 						new_pc = pc;
1115*0Sstevel@tonic-gate 						break;
1116*0Sstevel@tonic-gate 					}
1117*0Sstevel@tonic-gate 					new_pc = value;
1118*0Sstevel@tonic-gate #ifdef __amd64
1119*0Sstevel@tonic-gate 				} else {
1120*0Sstevel@tonic-gate 					uint32_t value;
1121*0Sstevel@tonic-gate 					if (fasttrap_fuword32((void *)addr,
1122*0Sstevel@tonic-gate 					    &value) == -1) {
1123*0Sstevel@tonic-gate 						fasttrap_sigsegv(p, curthread,
1124*0Sstevel@tonic-gate 						    addr);
1125*0Sstevel@tonic-gate 						new_pc = pc;
1126*0Sstevel@tonic-gate 						break;
1127*0Sstevel@tonic-gate 					}
1128*0Sstevel@tonic-gate 					new_pc = value;
1129*0Sstevel@tonic-gate 				}
1130*0Sstevel@tonic-gate #endif
1131*0Sstevel@tonic-gate 			} else {
1132*0Sstevel@tonic-gate 				new_pc = addr;
1133*0Sstevel@tonic-gate 			}
1134*0Sstevel@tonic-gate 		}
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 		/*
1137*0Sstevel@tonic-gate 		 * If this is a call instruction, we need to push the return
1138*0Sstevel@tonic-gate 		 * address onto the stack. If this fails, we send the process
1139*0Sstevel@tonic-gate 		 * a SIGSEGV and reset the pc to emulate what would happen if
1140*0Sstevel@tonic-gate 		 * this instruction weren't traced.
1141*0Sstevel@tonic-gate 		 */
1142*0Sstevel@tonic-gate 		if (tp->ftt_type == FASTTRAP_T_CALL) {
1143*0Sstevel@tonic-gate 			int ret;
1144*0Sstevel@tonic-gate 			uintptr_t addr;
1145*0Sstevel@tonic-gate #ifdef __amd64
1146*0Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
1147*0Sstevel@tonic-gate 				addr = rp->r_sp - sizeof (uintptr_t);
1148*0Sstevel@tonic-gate 				ret = fasttrap_sulword((void *)addr,
1149*0Sstevel@tonic-gate 				    pc + tp->ftt_size);
1150*0Sstevel@tonic-gate 			} else {
1151*0Sstevel@tonic-gate #endif
1152*0Sstevel@tonic-gate 				addr = rp->r_sp - sizeof (uint32_t);
1153*0Sstevel@tonic-gate 				ret = fasttrap_suword32((void *)addr,
1154*0Sstevel@tonic-gate 				    (uint32_t)(pc + tp->ftt_size));
1155*0Sstevel@tonic-gate #ifdef __amd64
1156*0Sstevel@tonic-gate 			}
1157*0Sstevel@tonic-gate #endif
1158*0Sstevel@tonic-gate 
1159*0Sstevel@tonic-gate 			if (ret == -1) {
1160*0Sstevel@tonic-gate 				fasttrap_sigsegv(p, curthread, addr);
1161*0Sstevel@tonic-gate 				new_pc = pc;
1162*0Sstevel@tonic-gate 				break;
1163*0Sstevel@tonic-gate 			}
1164*0Sstevel@tonic-gate 
1165*0Sstevel@tonic-gate 			rp->r_sp = addr;
1166*0Sstevel@tonic-gate 		}
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 		break;
1169*0Sstevel@tonic-gate 
1170*0Sstevel@tonic-gate 	case FASTTRAP_T_COMMON:
1171*0Sstevel@tonic-gate 	{
1172*0Sstevel@tonic-gate 		uintptr_t addr;
1173*0Sstevel@tonic-gate 		uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 5 + 2];
1174*0Sstevel@tonic-gate 		uint_t i = 0;
1175*0Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 		/*
1178*0Sstevel@tonic-gate 		 * Compute the address of the ulwp_t and step over the
1179*0Sstevel@tonic-gate 		 * ul_self pointer. The method used to store the user-land
1180*0Sstevel@tonic-gate 		 * thread pointer is very different on 32- and 64-bit
1181*0Sstevel@tonic-gate 		 * kernels.
1182*0Sstevel@tonic-gate 		 */
1183*0Sstevel@tonic-gate #if defined(__amd64)
1184*0Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
1185*0Sstevel@tonic-gate 			addr = lwp->lwp_pcb.pcb_fsbase;
1186*0Sstevel@tonic-gate 			addr += sizeof (void *);
1187*0Sstevel@tonic-gate 		} else {
1188*0Sstevel@tonic-gate 			addr = lwp->lwp_pcb.pcb_gsbase;
1189*0Sstevel@tonic-gate 			addr += sizeof (caddr32_t);
1190*0Sstevel@tonic-gate 		}
1191*0Sstevel@tonic-gate #elif defined(__i386)
1192*0Sstevel@tonic-gate 		addr = USEGD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
1193*0Sstevel@tonic-gate 		addr += sizeof (void *);
1194*0Sstevel@tonic-gate #endif
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate 		/*
1197*0Sstevel@tonic-gate 		 * Generic Instruction Tracing
1198*0Sstevel@tonic-gate 		 * ---------------------------
1199*0Sstevel@tonic-gate 		 *
1200*0Sstevel@tonic-gate 		 * This is the layout of the scratch space in the user-land
1201*0Sstevel@tonic-gate 		 * thread structure for our generated instructions.
1202*0Sstevel@tonic-gate 		 *
1203*0Sstevel@tonic-gate 		 *	32-bit mode			bytes
1204*0Sstevel@tonic-gate 		 *	------------------------	-----
1205*0Sstevel@tonic-gate 		 * a:	<original instruction>		<= 15
1206*0Sstevel@tonic-gate 		 *	jmp	<pc + tp->ftt_size>	    5
1207*0Sstevel@tonic-gate 		 * b:	<original instrction>		<= 15
1208*0Sstevel@tonic-gate 		 *	int	T_DTRACE_RET		    2
1209*0Sstevel@tonic-gate 		 *					-----
1210*0Sstevel@tonic-gate 		 *					<= 37
1211*0Sstevel@tonic-gate 		 *
1212*0Sstevel@tonic-gate 		 *	64-bit mode			bytes
1213*0Sstevel@tonic-gate 		 *	------------------------	-----
1214*0Sstevel@tonic-gate 		 * a:	<original instruction>		<= 15
1215*0Sstevel@tonic-gate 		 *	jmp	0(%rip)			    6
1216*0Sstevel@tonic-gate 		 *	<pc + tp->ftt_size>		    8
1217*0Sstevel@tonic-gate 		 * b:	<original instruction>		<= 15
1218*0Sstevel@tonic-gate 		 * 	int	T_DTRACE_RET		    2
1219*0Sstevel@tonic-gate 		 * 					-----
1220*0Sstevel@tonic-gate 		 * 					<= 46
1221*0Sstevel@tonic-gate 		 *
1222*0Sstevel@tonic-gate 		 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1223*0Sstevel@tonic-gate 		 * to b. If we encounter a signal on the way out of the
1224*0Sstevel@tonic-gate 		 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1225*0Sstevel@tonic-gate 		 * so that we execute the original instruction and re-enter
1226*0Sstevel@tonic-gate 		 * the kernel rather than redirecting to the next instruction.
1227*0Sstevel@tonic-gate 		 *
1228*0Sstevel@tonic-gate 		 * If there are return probes (so we know that we're going to
1229*0Sstevel@tonic-gate 		 * need to reenter the kernel after executing the original
1230*0Sstevel@tonic-gate 		 * instruction), the scratch space will just contain the
1231*0Sstevel@tonic-gate 		 * original instruction followed by an interrupt -- the same
1232*0Sstevel@tonic-gate 		 * data as at b.
1233*0Sstevel@tonic-gate 		 *
1234*0Sstevel@tonic-gate 		 * %rip-relative Addressing
1235*0Sstevel@tonic-gate 		 * ------------------------
1236*0Sstevel@tonic-gate 		 *
1237*0Sstevel@tonic-gate 		 * There's a further complication in 64-bit mode due to %rip-
1238*0Sstevel@tonic-gate 		 * relative addressing. While this is clearly a beneficial
1239*0Sstevel@tonic-gate 		 * architectural decision for position independent code, it's
1240*0Sstevel@tonic-gate 		 * hard not to see it as a personal attack against the pid
1241*0Sstevel@tonic-gate 		 * provider since before there was a relatively small set of
1242*0Sstevel@tonic-gate 		 * instructions to emulate; with %rip-relative addressing,
1243*0Sstevel@tonic-gate 		 * almost every instruction can potentially depend on the
1244*0Sstevel@tonic-gate 		 * address at which it's executed. Rather than emulating
1245*0Sstevel@tonic-gate 		 * the broad spectrum of instructions that can now be
1246*0Sstevel@tonic-gate 		 * position dependent, we emulate jumps and others as in
1247*0Sstevel@tonic-gate 		 * 32-bit mode, and take a different tack for instructions
1248*0Sstevel@tonic-gate 		 * using %rip-relative addressing.
1249*0Sstevel@tonic-gate 		 *
1250*0Sstevel@tonic-gate 		 * For every instruction that uses the ModRM byte, the
1251*0Sstevel@tonic-gate 		 * in-kernel disassembler reports its location. We use the
1252*0Sstevel@tonic-gate 		 * ModRM byte to identify that an instruction uses
1253*0Sstevel@tonic-gate 		 * %rip-relative addressing and to see what other registers
1254*0Sstevel@tonic-gate 		 * the instruction uses. To emulate those instructions,
1255*0Sstevel@tonic-gate 		 * we modify the instruction to be %rax-relative rather than
1256*0Sstevel@tonic-gate 		 * %rip-relative (or %rcx-relative if the instruction uses
1257*0Sstevel@tonic-gate 		 * %rax; or %r8- or %r9-relative if the REX.B is present so
1258*0Sstevel@tonic-gate 		 * we don't have to rewrite the REX prefix). We then load
1259*0Sstevel@tonic-gate 		 * the value that %rip would have been into the scratch
1260*0Sstevel@tonic-gate 		 * register and generate an instruction to reset the scratch
1261*0Sstevel@tonic-gate 		 * register back to its original value. The instruction
1262*0Sstevel@tonic-gate 		 * sequence looks like this:
1263*0Sstevel@tonic-gate 		 *
1264*0Sstevel@tonic-gate 		 *	64-mode %rip-relative		bytes
1265*0Sstevel@tonic-gate 		 *	------------------------	-----
1266*0Sstevel@tonic-gate 		 * a:	<modified instruction>		<= 15
1267*0Sstevel@tonic-gate 		 *	movq	$<value>, %<scratch>	    6
1268*0Sstevel@tonic-gate 		 *	jmp	0(%rip)			    6
1269*0Sstevel@tonic-gate 		 *	<pc + tp->ftt_size>		    8
1270*0Sstevel@tonic-gate 		 * b:	<modified instruction>  	<= 15
1271*0Sstevel@tonic-gate 		 * 	int	T_DTRACE_RET		    2
1272*0Sstevel@tonic-gate 		 * 					-----
1273*0Sstevel@tonic-gate 		 *					   52
1274*0Sstevel@tonic-gate 		 *
1275*0Sstevel@tonic-gate 		 * We set curthread->t_dtrace_regv so that upon receiving
1276*0Sstevel@tonic-gate 		 * a signal we can reset the value of the scratch register.
1277*0Sstevel@tonic-gate 		 */
1278*0Sstevel@tonic-gate 
1279*0Sstevel@tonic-gate 		ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
1280*0Sstevel@tonic-gate 
1281*0Sstevel@tonic-gate 		curthread->t_dtrace_scrpc = addr;
1282*0Sstevel@tonic-gate 		bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1283*0Sstevel@tonic-gate 		i += tp->ftt_size;
1284*0Sstevel@tonic-gate 
1285*0Sstevel@tonic-gate #ifdef __amd64
1286*0Sstevel@tonic-gate 		if (tp->ftt_ripmode != 0) {
1287*0Sstevel@tonic-gate 			greg_t *reg;
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 			ASSERT(p->p_model == DATAMODEL_LP64);
1290*0Sstevel@tonic-gate 			ASSERT(tp->ftt_ripmode &
1291*0Sstevel@tonic-gate 			    (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 			/*
1294*0Sstevel@tonic-gate 			 * If this was a %rip-relative instruction, we change
1295*0Sstevel@tonic-gate 			 * it to be either a %rax- or %rcx-relative
1296*0Sstevel@tonic-gate 			 * instruction (depending on whether those registers
1297*0Sstevel@tonic-gate 			 * are used as another operand; or %r8- or %r9-
1298*0Sstevel@tonic-gate 			 * relative depending on the value of REX.B). We then
1299*0Sstevel@tonic-gate 			 * set that register and generate a movq instruction
1300*0Sstevel@tonic-gate 			 * to reset the value.
1301*0Sstevel@tonic-gate 			 */
1302*0Sstevel@tonic-gate 			if (tp->ftt_ripmode & FASTTRAP_RIP_X)
1303*0Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
1304*0Sstevel@tonic-gate 			else
1305*0Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
1306*0Sstevel@tonic-gate 
1307*0Sstevel@tonic-gate 			if (tp->ftt_ripmode & FASTTRAP_RIP_1)
1308*0Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_MOV_EAX;
1309*0Sstevel@tonic-gate 			else
1310*0Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_MOV_ECX;
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate 			switch (tp->ftt_ripmode) {
1313*0Sstevel@tonic-gate 			case FASTTRAP_RIP_1:
1314*0Sstevel@tonic-gate 				reg = &rp->r_rax;
1315*0Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_RAX;
1316*0Sstevel@tonic-gate 				break;
1317*0Sstevel@tonic-gate 			case FASTTRAP_RIP_2:
1318*0Sstevel@tonic-gate 				reg = &rp->r_rcx;
1319*0Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_RCX;
1320*0Sstevel@tonic-gate 				break;
1321*0Sstevel@tonic-gate 			case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
1322*0Sstevel@tonic-gate 				reg = &rp->r_r8;
1323*0Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_R8;
1324*0Sstevel@tonic-gate 				break;
1325*0Sstevel@tonic-gate 			case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
1326*0Sstevel@tonic-gate 				reg = &rp->r_r9;
1327*0Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_R9;
1328*0Sstevel@tonic-gate 				break;
1329*0Sstevel@tonic-gate 			}
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 			*(uint64_t *)&scratch[i] = *reg;
1332*0Sstevel@tonic-gate 			curthread->t_dtrace_regv = *reg;
1333*0Sstevel@tonic-gate 			*reg = pc + tp->ftt_size;
1334*0Sstevel@tonic-gate 			i += sizeof (uint64_t);
1335*0Sstevel@tonic-gate 		}
1336*0Sstevel@tonic-gate #endif
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 		/*
1339*0Sstevel@tonic-gate 		 * Generate the branch instruction to what would have
1340*0Sstevel@tonic-gate 		 * normally been the subsequent instruction. In 32-bit mode,
1341*0Sstevel@tonic-gate 		 * this is just a relative branch; in 64-bit mode this is a
1342*0Sstevel@tonic-gate 		 * %rip-relative branch that loads the 64-bit pc value
1343*0Sstevel@tonic-gate 		 * immediately after the jmp instruction.
1344*0Sstevel@tonic-gate 		 */
1345*0Sstevel@tonic-gate #ifdef __amd64
1346*0Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
1347*0Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_GROUP5_OP;
1348*0Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
1349*0Sstevel@tonic-gate 			*(uint32_t *)&scratch[i] = 0;
1350*0Sstevel@tonic-gate 			i += sizeof (uint32_t);
1351*0Sstevel@tonic-gate 			*(uint64_t *)&scratch[i] = pc + tp->ftt_size;
1352*0Sstevel@tonic-gate 			i += sizeof (uint64_t);
1353*0Sstevel@tonic-gate 		} else {
1354*0Sstevel@tonic-gate #endif
1355*0Sstevel@tonic-gate 			/*
1356*0Sstevel@tonic-gate 			 * Set up the jmp to the next instruction; note that
1357*0Sstevel@tonic-gate 			 * the size of the traced instruction cancels out.
1358*0Sstevel@tonic-gate 			 */
1359*0Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_JMP32;
1360*0Sstevel@tonic-gate 			*(uint32_t *)&scratch[i] = pc - addr - 5;
1361*0Sstevel@tonic-gate 			i += sizeof (uint32_t);
1362*0Sstevel@tonic-gate #ifdef __amd64
1363*0Sstevel@tonic-gate 		}
1364*0Sstevel@tonic-gate #endif
1365*0Sstevel@tonic-gate 
1366*0Sstevel@tonic-gate 		curthread->t_dtrace_astpc = addr + i;
1367*0Sstevel@tonic-gate 		bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1368*0Sstevel@tonic-gate 		i += tp->ftt_size;
1369*0Sstevel@tonic-gate 		scratch[i++] = FASTTRAP_INT;
1370*0Sstevel@tonic-gate 		scratch[i++] = T_DTRACE_RET;
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 		if (fasttrap_copyout(scratch, (char *)addr, i)) {
1373*0Sstevel@tonic-gate 			fasttrap_sigtrap(p, curthread, pc);
1374*0Sstevel@tonic-gate 			new_pc = pc;
1375*0Sstevel@tonic-gate 			break;
1376*0Sstevel@tonic-gate 		}
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate 		if (tp->ftt_retids != NULL) {
1379*0Sstevel@tonic-gate 			curthread->t_dtrace_step = 1;
1380*0Sstevel@tonic-gate 			curthread->t_dtrace_ret = 1;
1381*0Sstevel@tonic-gate 			new_pc = curthread->t_dtrace_astpc;
1382*0Sstevel@tonic-gate 		} else {
1383*0Sstevel@tonic-gate 			new_pc = curthread->t_dtrace_scrpc;
1384*0Sstevel@tonic-gate 		}
1385*0Sstevel@tonic-gate 
1386*0Sstevel@tonic-gate 		curthread->t_dtrace_pc = pc;
1387*0Sstevel@tonic-gate 		curthread->t_dtrace_npc = pc + tp->ftt_size;
1388*0Sstevel@tonic-gate 		curthread->t_dtrace_on = 1;
1389*0Sstevel@tonic-gate 		break;
1390*0Sstevel@tonic-gate 	}
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate 	default:
1393*0Sstevel@tonic-gate 		panic("fasttrap: mishandled an instruction");
1394*0Sstevel@tonic-gate 	}
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 	/*
1397*0Sstevel@tonic-gate 	 * If there were no return probes when we first found the tracepoint,
1398*0Sstevel@tonic-gate 	 * we should feel no obligation to honor any return probes that were
1399*0Sstevel@tonic-gate 	 * subsequently enabled -- they'll just have to wait until the next
1400*0Sstevel@tonic-gate 	 * time around.
1401*0Sstevel@tonic-gate 	 */
1402*0Sstevel@tonic-gate 	if (tp->ftt_retids != NULL) {
1403*0Sstevel@tonic-gate 		/*
1404*0Sstevel@tonic-gate 		 * We need to wait until the results of the instruction are
1405*0Sstevel@tonic-gate 		 * apparent before invoking any return probes. If this
1406*0Sstevel@tonic-gate 		 * instruction was emulated we can just call
1407*0Sstevel@tonic-gate 		 * fasttrap_return_common(); if it needs to be executed, we
1408*0Sstevel@tonic-gate 		 * need to wait until the user thread returns to the kernel.
1409*0Sstevel@tonic-gate 		 */
1410*0Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_COMMON) {
1411*0Sstevel@tonic-gate 			/*
1412*0Sstevel@tonic-gate 			 * Set the program counter to the address of the traced
1413*0Sstevel@tonic-gate 			 * instruction so that it looks right in ustack()
1414*0Sstevel@tonic-gate 			 * output. We had previously set it to the end of the
1415*0Sstevel@tonic-gate 			 * instruction to simplify %rip-relative addressing.
1416*0Sstevel@tonic-gate 			 */
1417*0Sstevel@tonic-gate 			rp->r_pc = pc;
1418*0Sstevel@tonic-gate 
1419*0Sstevel@tonic-gate 			fasttrap_return_common(rp, pc, pid, new_pc);
1420*0Sstevel@tonic-gate 		} else {
1421*0Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_ret != 0);
1422*0Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_pc == pc);
1423*0Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_scrpc != 0);
1424*0Sstevel@tonic-gate 			ASSERT(new_pc == curthread->t_dtrace_astpc);
1425*0Sstevel@tonic-gate 		}
1426*0Sstevel@tonic-gate 	}
1427*0Sstevel@tonic-gate 
1428*0Sstevel@tonic-gate 	ASSERT(new_pc != 0);
1429*0Sstevel@tonic-gate 	rp->r_pc = new_pc;
1430*0Sstevel@tonic-gate 
1431*0Sstevel@tonic-gate 	return (0);
1432*0Sstevel@tonic-gate }
1433*0Sstevel@tonic-gate 
1434*0Sstevel@tonic-gate int
1435*0Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
1436*0Sstevel@tonic-gate {
1437*0Sstevel@tonic-gate 	proc_t *p = curproc;
1438*0Sstevel@tonic-gate 	uintptr_t pc = curthread->t_dtrace_pc;
1439*0Sstevel@tonic-gate 	uintptr_t npc = curthread->t_dtrace_npc;
1440*0Sstevel@tonic-gate 
1441*0Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
1442*0Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
1443*0Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
1444*0Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 	/*
1447*0Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
1448*0Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
1449*0Sstevel@tonic-gate 	 * process: this one.
1450*0Sstevel@tonic-gate 	 */
1451*0Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
1452*0Sstevel@tonic-gate 		p = p->p_parent;
1453*0Sstevel@tonic-gate 	}
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 	/*
1456*0Sstevel@tonic-gate 	 * We set rp->r_pc to the address of the traced instruction so
1457*0Sstevel@tonic-gate 	 * that it appears to dtrace_probe() that we're on the original
1458*0Sstevel@tonic-gate 	 * instruction, and so that the user can't easily detect our
1459*0Sstevel@tonic-gate 	 * complex web of lies. dtrace_return_probe() (our caller)
1460*0Sstevel@tonic-gate 	 * will correctly set %pc after we return.
1461*0Sstevel@tonic-gate 	 */
1462*0Sstevel@tonic-gate 	rp->r_pc = pc;
1463*0Sstevel@tonic-gate 
1464*0Sstevel@tonic-gate 	fasttrap_return_common(rp, pc, p->p_pid, npc);
1465*0Sstevel@tonic-gate 
1466*0Sstevel@tonic-gate 	return (0);
1467*0Sstevel@tonic-gate }
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate /*ARGSUSED*/
1470*0Sstevel@tonic-gate uint64_t
1471*0Sstevel@tonic-gate fasttrap_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
1472*0Sstevel@tonic-gate {
1473*0Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 1, argno));
1474*0Sstevel@tonic-gate }
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate /*ARGSUSED*/
1477*0Sstevel@tonic-gate uint64_t
1478*0Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1479*0Sstevel@tonic-gate     int aframes)
1480*0Sstevel@tonic-gate {
1481*0Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 0, argno));
1482*0Sstevel@tonic-gate }
1483*0Sstevel@tonic-gate 
1484*0Sstevel@tonic-gate static ulong_t
1485*0Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
1486*0Sstevel@tonic-gate {
1487*0Sstevel@tonic-gate #ifdef __amd64
1488*0Sstevel@tonic-gate 	switch (reg) {
1489*0Sstevel@tonic-gate 	case REG_R15:		return (rp->r_r15);
1490*0Sstevel@tonic-gate 	case REG_R14:		return (rp->r_r14);
1491*0Sstevel@tonic-gate 	case REG_R13:		return (rp->r_r13);
1492*0Sstevel@tonic-gate 	case REG_R12:		return (rp->r_r12);
1493*0Sstevel@tonic-gate 	case REG_R11:		return (rp->r_r11);
1494*0Sstevel@tonic-gate 	case REG_R10:		return (rp->r_r10);
1495*0Sstevel@tonic-gate 	case REG_R9:		return (rp->r_r9);
1496*0Sstevel@tonic-gate 	case REG_R8:		return (rp->r_r8);
1497*0Sstevel@tonic-gate 	case REG_RDI:		return (rp->r_rdi);
1498*0Sstevel@tonic-gate 	case REG_RSI:		return (rp->r_rsi);
1499*0Sstevel@tonic-gate 	case REG_RBP:		return (rp->r_rbp);
1500*0Sstevel@tonic-gate 	case REG_RBX:		return (rp->r_rbx);
1501*0Sstevel@tonic-gate 	case REG_RDX:		return (rp->r_rdx);
1502*0Sstevel@tonic-gate 	case REG_RCX:		return (rp->r_rcx);
1503*0Sstevel@tonic-gate 	case REG_RAX:		return (rp->r_rax);
1504*0Sstevel@tonic-gate 	case REG_TRAPNO:	return (rp->r_trapno);
1505*0Sstevel@tonic-gate 	case REG_ERR:		return (rp->r_err);
1506*0Sstevel@tonic-gate 	case REG_RIP:		return (rp->r_rip);
1507*0Sstevel@tonic-gate 	case REG_CS:		return (rp->r_cs);
1508*0Sstevel@tonic-gate 	case REG_RFL:		return (rp->r_rfl);
1509*0Sstevel@tonic-gate 	case REG_RSP:		return (rp->r_rsp);
1510*0Sstevel@tonic-gate 	case REG_SS:		return (rp->r_ss);
1511*0Sstevel@tonic-gate 	case REG_FS:		return (rp->r_fs);
1512*0Sstevel@tonic-gate 	case REG_GS:		return (rp->r_gs);
1513*0Sstevel@tonic-gate 	case REG_DS:		return (rp->r_ds);
1514*0Sstevel@tonic-gate 	case REG_ES:		return (rp->r_es);
1515*0Sstevel@tonic-gate 	case REG_FSBASE:	return (rp->r_fsbase);
1516*0Sstevel@tonic-gate 	case REG_GSBASE:	return (rp->r_gsbase);
1517*0Sstevel@tonic-gate 	}
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate 	panic("dtrace: illegal register constant");
1520*0Sstevel@tonic-gate 	/*NOTREACHED*/
1521*0Sstevel@tonic-gate #else
1522*0Sstevel@tonic-gate 	if (reg >= _NGREG)
1523*0Sstevel@tonic-gate 		panic("dtrace: illegal register constant");
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate 	return (((greg_t *)&rp->r_gs)[reg]);
1526*0Sstevel@tonic-gate #endif
1527*0Sstevel@tonic-gate }
1528