xref: /titanic_51/usr/src/uts/intel/dtrace/fasttrap_isa.c (revision 6009dbc6db3fe0e2890761d3d562bb4c4b9bf0be)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ac448965Sahl  * Common Development and Distribution License (the "License").
6ac448965Sahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ac448965Sahl 
227c478bd9Sstevel@tonic-gate /*
23*6009dbc6Sahl  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
307c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
317c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
327c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/regset.h>
357c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
367c478bd9Sstevel@tonic-gate #include <sys/segments.h>
37ae115bc7Smrj #include <sys/x86_archext.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/trap.h>
409acbbeafSnn35248 #include <sys/archsystm.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Lossless User-Land Tracing on x86
447c478bd9Sstevel@tonic-gate  * ---------------------------------
457c478bd9Sstevel@tonic-gate  *
46ac448965Sahl  * The execution of most instructions is not dependent on the address; for
47ac448965Sahl  * these instructions it is sufficient to copy them into the user process's
48ac448965Sahl  * address space and execute them. To effectively single-step an instruction
49ac448965Sahl  * in user-land, we copy out the following sequence of instructions to scratch
507c478bd9Sstevel@tonic-gate  * space in the user thread's ulwp_t structure.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * We then set the program counter (%eip or %rip) to point to this scratch
537c478bd9Sstevel@tonic-gate  * space. Once execution resumes, the original instruction is executed and
547c478bd9Sstevel@tonic-gate  * then control flow is redirected to what was originally the subsequent
557c478bd9Sstevel@tonic-gate  * instruction. If the kernel attemps to deliver a signal while single-
567c478bd9Sstevel@tonic-gate  * stepping, the signal is deferred and the program counter is moved into the
577c478bd9Sstevel@tonic-gate  * second sequence of instructions. The second sequence ends in a trap into
587c478bd9Sstevel@tonic-gate  * the kernel where the deferred signal is then properly handled and delivered.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * For instructions whose execute is position dependent, we perform simple
617c478bd9Sstevel@tonic-gate  * emulation. These instructions are limited to control transfer
627c478bd9Sstevel@tonic-gate  * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
637c478bd9Sstevel@tonic-gate  * of %rip-relative addressing that means that almost any instruction can be
647c478bd9Sstevel@tonic-gate  * position dependent. For all the details on how we emulate generic
657c478bd9Sstevel@tonic-gate  * instructions included %rip-relative instructions, see the code in
667c478bd9Sstevel@tonic-gate  * fasttrap_pid_probe() below where we handle instructions of type
677c478bd9Sstevel@tonic-gate  * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	FASTTRAP_MODRM_MOD(modrm)	(((modrm) >> 6) & 0x3)
717c478bd9Sstevel@tonic-gate #define	FASTTRAP_MODRM_REG(modrm)	(((modrm) >> 3) & 0x7)
727c478bd9Sstevel@tonic-gate #define	FASTTRAP_MODRM_RM(modrm)	((modrm) & 0x7)
737c478bd9Sstevel@tonic-gate #define	FASTTRAP_MODRM(mod, reg, rm)	(((mod) << 6) | ((reg) << 3) | (rm))
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	FASTTRAP_SIB_SCALE(sib)		(((sib) >> 6) & 0x3)
767c478bd9Sstevel@tonic-gate #define	FASTTRAP_SIB_INDEX(sib)		(((sib) >> 3) & 0x7)
777c478bd9Sstevel@tonic-gate #define	FASTTRAP_SIB_BASE(sib)		((sib) & 0x7)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	FASTTRAP_REX_W(rex)		(((rex) >> 3) & 1)
807c478bd9Sstevel@tonic-gate #define	FASTTRAP_REX_R(rex)		(((rex) >> 2) & 1)
817c478bd9Sstevel@tonic-gate #define	FASTTRAP_REX_X(rex)		(((rex) >> 1) & 1)
827c478bd9Sstevel@tonic-gate #define	FASTTRAP_REX_B(rex)		((rex) & 1)
837c478bd9Sstevel@tonic-gate #define	FASTTRAP_REX(w, r, x, b)	\
847c478bd9Sstevel@tonic-gate 	(0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Single-byte op-codes.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #define	FASTTRAP_PUSHL_EBP	0x55
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	FASTTRAP_JO		0x70
927c478bd9Sstevel@tonic-gate #define	FASTTRAP_JNO		0x71
937c478bd9Sstevel@tonic-gate #define	FASTTRAP_JB		0x72
947c478bd9Sstevel@tonic-gate #define	FASTTRAP_JAE		0x73
957c478bd9Sstevel@tonic-gate #define	FASTTRAP_JE		0x74
967c478bd9Sstevel@tonic-gate #define	FASTTRAP_JNE		0x75
977c478bd9Sstevel@tonic-gate #define	FASTTRAP_JBE		0x76
987c478bd9Sstevel@tonic-gate #define	FASTTRAP_JA		0x77
997c478bd9Sstevel@tonic-gate #define	FASTTRAP_JS		0x78
1007c478bd9Sstevel@tonic-gate #define	FASTTRAP_JNS		0x79
1017c478bd9Sstevel@tonic-gate #define	FASTTRAP_JP		0x7a
1027c478bd9Sstevel@tonic-gate #define	FASTTRAP_JNP		0x7b
1037c478bd9Sstevel@tonic-gate #define	FASTTRAP_JL		0x7c
1047c478bd9Sstevel@tonic-gate #define	FASTTRAP_JGE		0x7d
1057c478bd9Sstevel@tonic-gate #define	FASTTRAP_JLE		0x7e
1067c478bd9Sstevel@tonic-gate #define	FASTTRAP_JG		0x7f
1077c478bd9Sstevel@tonic-gate 
1082b6e762cSahl #define	FASTTRAP_NOP		0x90
1092b6e762cSahl 
1107c478bd9Sstevel@tonic-gate #define	FASTTRAP_MOV_EAX	0xb8
1117c478bd9Sstevel@tonic-gate #define	FASTTRAP_MOV_ECX	0xb9
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	FASTTRAP_RET16		0xc2
1147c478bd9Sstevel@tonic-gate #define	FASTTRAP_RET		0xc3
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #define	FASTTRAP_LOOPNZ		0xe0
1177c478bd9Sstevel@tonic-gate #define	FASTTRAP_LOOPZ		0xe1
1187c478bd9Sstevel@tonic-gate #define	FASTTRAP_LOOP		0xe2
1197c478bd9Sstevel@tonic-gate #define	FASTTRAP_JCXZ		0xe3
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	FASTTRAP_CALL		0xe8
1227c478bd9Sstevel@tonic-gate #define	FASTTRAP_JMP32		0xe9
1237c478bd9Sstevel@tonic-gate #define	FASTTRAP_JMP8		0xeb
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	FASTTRAP_INT3		0xcc
1267c478bd9Sstevel@tonic-gate #define	FASTTRAP_INT		0xcd
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	FASTTRAP_2_BYTE_OP	0x0f
1297c478bd9Sstevel@tonic-gate #define	FASTTRAP_GROUP5_OP	0xff
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Two-byte op-codes (second byte only).
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JO		0x80
1357c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JNO		0x81
1367c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JB		0x82
1377c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JAE		0x83
1387c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JE		0x84
1397c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JNE		0x85
1407c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JBE		0x86
1417c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JA		0x87
1427c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JS		0x88
1437c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JNS		0x89
1447c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JP		0x8a
1457c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JNP		0x8b
1467c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JL		0x8c
1477c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JGE		0x8d
1487c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JLE		0x8e
1497c478bd9Sstevel@tonic-gate #define	FASTTRAP_0F_JG		0x8f
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_OF	0x800
1527c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_DF	0x400
1537c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_SF	0x080
1547c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_ZF	0x040
1557c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_AF	0x010
1567c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_PF	0x004
1577c478bd9Sstevel@tonic-gate #define	FASTTRAP_EFLAGS_CF	0x001
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Instruction prefixes.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_OPERAND	0x66
1637c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_ADDRESS	0x67
1647c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_CS	0x2E
1657c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_DS	0x3E
1667c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_ES	0x26
1677c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_FS	0x64
1687c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_GS	0x65
1697c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_SS	0x36
1707c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_LOCK	0xF0
1717c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_REP	0xF3
1727c478bd9Sstevel@tonic-gate #define	FASTTRAP_PREFIX_REPNE	0xF2
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	FASTTRAP_NOREG	0xff
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Map between instruction register encodings and the kernel constants which
1787c478bd9Sstevel@tonic-gate  * correspond to indicies into struct regs.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate #ifdef __amd64
1817c478bd9Sstevel@tonic-gate static const uint8_t regmap[16] = {
1827c478bd9Sstevel@tonic-gate 	REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
1837c478bd9Sstevel@tonic-gate 	REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate #else
1867c478bd9Sstevel@tonic-gate static const uint8_t regmap[8] = {
1877c478bd9Sstevel@tonic-gate 	EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate #endif
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static uint64_t
1947c478bd9Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int function_entry, int argno)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	uint64_t value;
1977c478bd9Sstevel@tonic-gate 	int shift = function_entry ? 1 : 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #ifdef __amd64
2007c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_LP64) {
2017c478bd9Sstevel@tonic-gate 		uintptr_t *stack;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		/*
2047c478bd9Sstevel@tonic-gate 		 * In 64-bit mode, the first six arguments are stored in
2057c478bd9Sstevel@tonic-gate 		 * registers.
2067c478bd9Sstevel@tonic-gate 		 */
2077c478bd9Sstevel@tonic-gate 		if (argno < 6)
2087c478bd9Sstevel@tonic-gate 			return ((&rp->r_rdi)[argno]);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		stack = (uintptr_t *)rp->r_sp;
2117c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2127c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&stack[argno - 6 + shift]);
2137c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2147c478bd9Sstevel@tonic-gate 	} else {
2157c478bd9Sstevel@tonic-gate #endif
2167c478bd9Sstevel@tonic-gate 		uint32_t *stack = (uint32_t *)rp->r_sp;
2177c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2187c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&stack[argno + shift]);
2197c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2207c478bd9Sstevel@tonic-gate #ifdef __amd64
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate #endif
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (value);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2287c478bd9Sstevel@tonic-gate int
229ac448965Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
230ac448965Sahl     fasttrap_probe_type_t type)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
2337c478bd9Sstevel@tonic-gate 	size_t len = FASTTRAP_MAX_INSTR_SIZE;
2347c478bd9Sstevel@tonic-gate 	size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
2357c478bd9Sstevel@tonic-gate 	uint_t start = 0;
2362b6e762cSahl 	int rmindex, size;
2379acbbeafSnn35248 	uint8_t seg, rex = 0;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Read the instruction at the given address out of the process's
2417c478bd9Sstevel@tonic-gate 	 * address space. We don't have to worry about a debugger
2427c478bd9Sstevel@tonic-gate 	 * changing this instruction before we overwrite it with our trap
2437c478bd9Sstevel@tonic-gate 	 * instruction since P_PR_LOCK is set. Since instructions can span
2447c478bd9Sstevel@tonic-gate 	 * pages, we potentially read the instruction in two parts. If the
2457c478bd9Sstevel@tonic-gate 	 * second part fails, we just zero out that part of the instruction.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if (uread(p, &instr[0], first, pc) != 0)
2487c478bd9Sstevel@tonic-gate 		return (-1);
2497c478bd9Sstevel@tonic-gate 	if (len > first &&
2507c478bd9Sstevel@tonic-gate 	    uread(p, &instr[first], len - first, pc + first) != 0) {
2517c478bd9Sstevel@tonic-gate 		bzero(&instr[first], len - first);
2527c478bd9Sstevel@tonic-gate 		len = first;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * If the disassembly fails, then we have a malformed instruction.
2577c478bd9Sstevel@tonic-gate 	 */
2582b6e762cSahl 	if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0)
2597c478bd9Sstevel@tonic-gate 		return (-1);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Make sure the disassembler isn't completely broken.
2637c478bd9Sstevel@tonic-gate 	 */
2642b6e762cSahl 	ASSERT(-1 <= rmindex && rmindex < size);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * If the computed size is greater than the number of bytes read,
2687c478bd9Sstevel@tonic-gate 	 * then it was a malformed instruction possibly because it fell on a
2697c478bd9Sstevel@tonic-gate 	 * page boundary and the subsequent page was missing or because of
2707c478bd9Sstevel@tonic-gate 	 * some malicious user.
2717c478bd9Sstevel@tonic-gate 	 */
2722b6e762cSahl 	if (size > len)
2737c478bd9Sstevel@tonic-gate 		return (-1);
2747c478bd9Sstevel@tonic-gate 
2752b6e762cSahl 	tp->ftt_size = (uint8_t)size;
2769acbbeafSnn35248 	tp->ftt_segment = FASTTRAP_SEG_NONE;
2779acbbeafSnn35248 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Find the start of the instruction's opcode by processing any
2807c478bd9Sstevel@tonic-gate 	 * legacy prefixes.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	for (;;) {
2839acbbeafSnn35248 		seg = 0;
2847c478bd9Sstevel@tonic-gate 		switch (instr[start]) {
2859acbbeafSnn35248 		case FASTTRAP_PREFIX_SS:
2869acbbeafSnn35248 			seg++;
2879acbbeafSnn35248 			/*FALLTHRU*/
2889acbbeafSnn35248 		case FASTTRAP_PREFIX_GS:
2899acbbeafSnn35248 			seg++;
2909acbbeafSnn35248 			/*FALLTHRU*/
2919acbbeafSnn35248 		case FASTTRAP_PREFIX_FS:
2929acbbeafSnn35248 			seg++;
2939acbbeafSnn35248 			/*FALLTHRU*/
2949acbbeafSnn35248 		case FASTTRAP_PREFIX_ES:
2959acbbeafSnn35248 			seg++;
2969acbbeafSnn35248 			/*FALLTHRU*/
2979acbbeafSnn35248 		case FASTTRAP_PREFIX_DS:
2989acbbeafSnn35248 			seg++;
2999acbbeafSnn35248 			/*FALLTHRU*/
3009acbbeafSnn35248 		case FASTTRAP_PREFIX_CS:
3019acbbeafSnn35248 			seg++;
3029acbbeafSnn35248 			/*FALLTHRU*/
3037c478bd9Sstevel@tonic-gate 		case FASTTRAP_PREFIX_OPERAND:
3047c478bd9Sstevel@tonic-gate 		case FASTTRAP_PREFIX_ADDRESS:
3057c478bd9Sstevel@tonic-gate 		case FASTTRAP_PREFIX_LOCK:
3067c478bd9Sstevel@tonic-gate 		case FASTTRAP_PREFIX_REP:
3077c478bd9Sstevel@tonic-gate 		case FASTTRAP_PREFIX_REPNE:
3089acbbeafSnn35248 			if (seg != 0) {
3099acbbeafSnn35248 				/*
3109acbbeafSnn35248 				 * It's illegal for an instruction to specify
3119acbbeafSnn35248 				 * two segment prefixes -- give up on this
3129acbbeafSnn35248 				 * illegal instruction.
3139acbbeafSnn35248 				 */
3149acbbeafSnn35248 				if (tp->ftt_segment != FASTTRAP_SEG_NONE)
3159acbbeafSnn35248 					return (-1);
3169acbbeafSnn35248 
3179acbbeafSnn35248 				tp->ftt_segment = seg;
3189acbbeafSnn35248 			}
3197c478bd9Sstevel@tonic-gate 			start++;
3207c478bd9Sstevel@tonic-gate 			continue;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		break;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate #ifdef __amd64
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Identify the REX prefix on 64-bit processes.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
3307c478bd9Sstevel@tonic-gate 		rex = instr[start++];
3317c478bd9Sstevel@tonic-gate #endif
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Now that we're pretty sure that the instruction is okay, copy the
3357c478bd9Sstevel@tonic-gate 	 * valid part to the tracepoint.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	tp->ftt_type = FASTTRAP_T_COMMON;
3407c478bd9Sstevel@tonic-gate 	if (instr[start] == FASTTRAP_2_BYTE_OP) {
3417c478bd9Sstevel@tonic-gate 		switch (instr[start + 1]) {
3427c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JO:
3437c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JNO:
3447c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JB:
3457c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JAE:
3467c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JE:
3477c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JNE:
3487c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JBE:
3497c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JA:
3507c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JS:
3517c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JNS:
3527c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JP:
3537c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JNP:
3547c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JL:
3557c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JGE:
3567c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JLE:
3577c478bd9Sstevel@tonic-gate 		case FASTTRAP_0F_JG:
3587c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCC;
3597c478bd9Sstevel@tonic-gate 			tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
3607c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
36173427c57Sahl 			    /* LINTED - alignment */
3627c478bd9Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 2];
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 	} else if (instr[start] == FASTTRAP_GROUP5_OP) {
3667c478bd9Sstevel@tonic-gate 		uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
3677c478bd9Sstevel@tonic-gate 		uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
3687c478bd9Sstevel@tonic-gate 		uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		if (reg == 2 || reg == 4) {
3717c478bd9Sstevel@tonic-gate 			uint_t i, sz;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			if (reg == 2)
3747c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_CALL;
3757c478bd9Sstevel@tonic-gate 			else
3767c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_JMP;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			if (mod == 3)
3797c478bd9Sstevel@tonic-gate 				tp->ftt_code = 2;
3807c478bd9Sstevel@tonic-gate 			else
3817c478bd9Sstevel@tonic-gate 				tp->ftt_code = 1;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 			ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			/*
3867c478bd9Sstevel@tonic-gate 			 * See AMD x86-64 Architecture Programmer's Manual
3877c478bd9Sstevel@tonic-gate 			 * Volume 3, Section 1.2.7, Table 1-12, and
3887c478bd9Sstevel@tonic-gate 			 * Appendix A.3.1, Table A-15.
3897c478bd9Sstevel@tonic-gate 			 */
3907c478bd9Sstevel@tonic-gate 			if (mod != 3 && rm == 4) {
3917c478bd9Sstevel@tonic-gate 				uint8_t sib = instr[start + 2];
3927c478bd9Sstevel@tonic-gate 				uint_t index = FASTTRAP_SIB_INDEX(sib);
3937c478bd9Sstevel@tonic-gate 				uint_t base = FASTTRAP_SIB_BASE(sib);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 				tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 				tp->ftt_index = (index == 4) ?
3987c478bd9Sstevel@tonic-gate 				    FASTTRAP_NOREG :
3997c478bd9Sstevel@tonic-gate 				    regmap[index | (FASTTRAP_REX_X(rex) << 3)];
4007c478bd9Sstevel@tonic-gate 				tp->ftt_base = (mod == 0 && base == 5) ?
4017c478bd9Sstevel@tonic-gate 				    FASTTRAP_NOREG :
4027c478bd9Sstevel@tonic-gate 				    regmap[base | (FASTTRAP_REX_B(rex) << 3)];
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 				i = 3;
4057c478bd9Sstevel@tonic-gate 				sz = mod == 1 ? 1 : 4;
4067c478bd9Sstevel@tonic-gate 			} else {
4077c478bd9Sstevel@tonic-gate 				/*
4087c478bd9Sstevel@tonic-gate 				 * In 64-bit mode, mod == 0 and r/m == 5
4097c478bd9Sstevel@tonic-gate 				 * denotes %rip-relative addressing; in 32-bit
4107c478bd9Sstevel@tonic-gate 				 * mode, the base register isn't used. In both
4117c478bd9Sstevel@tonic-gate 				 * modes, there is a 32-bit operand.
4127c478bd9Sstevel@tonic-gate 				 */
4137c478bd9Sstevel@tonic-gate 				if (mod == 0 && rm == 5) {
4147c478bd9Sstevel@tonic-gate #ifdef __amd64
4157c478bd9Sstevel@tonic-gate 					if (p->p_model == DATAMODEL_LP64)
4167c478bd9Sstevel@tonic-gate 						tp->ftt_base = REG_RIP;
4177c478bd9Sstevel@tonic-gate 					else
4187c478bd9Sstevel@tonic-gate #endif
4197c478bd9Sstevel@tonic-gate 						tp->ftt_base = FASTTRAP_NOREG;
4207c478bd9Sstevel@tonic-gate 					sz = 4;
4217c478bd9Sstevel@tonic-gate 				} else  {
4227c478bd9Sstevel@tonic-gate 					uint8_t base = rm |
4237c478bd9Sstevel@tonic-gate 					    (FASTTRAP_REX_B(rex) << 3);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 					tp->ftt_base = regmap[base];
4267c478bd9Sstevel@tonic-gate 					sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
4277c478bd9Sstevel@tonic-gate 				}
4287c478bd9Sstevel@tonic-gate 				tp->ftt_index = FASTTRAP_NOREG;
4297c478bd9Sstevel@tonic-gate 				i = 2;
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 
43273427c57Sahl 			if (sz == 1) {
4337c478bd9Sstevel@tonic-gate 				tp->ftt_dest = *(int8_t *)&instr[start + i];
43473427c57Sahl 			} else if (sz == 4) {
43573427c57Sahl 				/* LINTED - alignment */
4367c478bd9Sstevel@tonic-gate 				tp->ftt_dest = *(int32_t *)&instr[start + i];
43773427c57Sahl 			} else {
4387c478bd9Sstevel@tonic-gate 				tp->ftt_dest = 0;
4397c478bd9Sstevel@tonic-gate 			}
44073427c57Sahl 		}
4417c478bd9Sstevel@tonic-gate 	} else {
4427c478bd9Sstevel@tonic-gate 		switch (instr[start]) {
4437c478bd9Sstevel@tonic-gate 		case FASTTRAP_RET:
4447c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RET;
4457c478bd9Sstevel@tonic-gate 			break;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		case FASTTRAP_RET16:
4487c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_RET16;
44973427c57Sahl 			/* LINTED - alignment */
4507c478bd9Sstevel@tonic-gate 			tp->ftt_dest = *(uint16_t *)&instr[start + 1];
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		case FASTTRAP_JO:
4547c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNO:
4557c478bd9Sstevel@tonic-gate 		case FASTTRAP_JB:
4567c478bd9Sstevel@tonic-gate 		case FASTTRAP_JAE:
4577c478bd9Sstevel@tonic-gate 		case FASTTRAP_JE:
4587c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNE:
4597c478bd9Sstevel@tonic-gate 		case FASTTRAP_JBE:
4607c478bd9Sstevel@tonic-gate 		case FASTTRAP_JA:
4617c478bd9Sstevel@tonic-gate 		case FASTTRAP_JS:
4627c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNS:
4637c478bd9Sstevel@tonic-gate 		case FASTTRAP_JP:
4647c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNP:
4657c478bd9Sstevel@tonic-gate 		case FASTTRAP_JL:
4667c478bd9Sstevel@tonic-gate 		case FASTTRAP_JGE:
4677c478bd9Sstevel@tonic-gate 		case FASTTRAP_JLE:
4687c478bd9Sstevel@tonic-gate 		case FASTTRAP_JG:
4697c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCC;
4707c478bd9Sstevel@tonic-gate 			tp->ftt_code = instr[start];
4717c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
4727c478bd9Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOPNZ:
4767c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOPZ:
4777c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOP:
4787c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_LOOP;
4797c478bd9Sstevel@tonic-gate 			tp->ftt_code = instr[start];
4807c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
4817c478bd9Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
4827c478bd9Sstevel@tonic-gate 			break;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		case FASTTRAP_JCXZ:
4857c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JCXZ;
4867c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
4877c478bd9Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
4887c478bd9Sstevel@tonic-gate 			break;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		case FASTTRAP_CALL:
4917c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_CALL;
4927c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
49373427c57Sahl 			    /* LINTED - alignment */
4947c478bd9Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 1];
4957c478bd9Sstevel@tonic-gate 			tp->ftt_code = 0;
4967c478bd9Sstevel@tonic-gate 			break;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		case FASTTRAP_JMP32:
4997c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMP;
5007c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
50173427c57Sahl 			    /* LINTED - alignment */
5027c478bd9Sstevel@tonic-gate 			    *(int32_t *)&instr[start + 1];
5037c478bd9Sstevel@tonic-gate 			break;
5047c478bd9Sstevel@tonic-gate 		case FASTTRAP_JMP8:
5057c478bd9Sstevel@tonic-gate 			tp->ftt_type = FASTTRAP_T_JMP;
5067c478bd9Sstevel@tonic-gate 			tp->ftt_dest = pc + tp->ftt_size +
5077c478bd9Sstevel@tonic-gate 			    (int8_t)instr[start + 1];
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		case FASTTRAP_PUSHL_EBP:
5117c478bd9Sstevel@tonic-gate 			if (start == 0)
5127c478bd9Sstevel@tonic-gate 				tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 
5152b6e762cSahl 		case FASTTRAP_NOP:
5162b6e762cSahl #ifdef __amd64
5172b6e762cSahl 			ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
5182b6e762cSahl 
5192b6e762cSahl 			/*
5202b6e762cSahl 			 * On amd64 we have to be careful not to confuse a nop
5212b6e762cSahl 			 * (actually xchgl %eax, %eax) with an instruction using
5222b6e762cSahl 			 * the same opcode, but that does something different
5232b6e762cSahl 			 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
5242b6e762cSahl 			 */
5252b6e762cSahl 			if (FASTTRAP_REX_B(rex) == 0)
5262b6e762cSahl #endif
5272b6e762cSahl 				tp->ftt_type = FASTTRAP_T_NOP;
5282b6e762cSahl 			break;
5292b6e762cSahl 
5307c478bd9Sstevel@tonic-gate 		case FASTTRAP_INT3:
5317c478bd9Sstevel@tonic-gate 			/*
5327c478bd9Sstevel@tonic-gate 			 * The pid provider shares the int3 trap with debugger
5337c478bd9Sstevel@tonic-gate 			 * breakpoints so we can't instrument them.
5347c478bd9Sstevel@tonic-gate 			 */
5357c478bd9Sstevel@tonic-gate 			ASSERT(instr[start] == FASTTRAP_INSTR);
5367c478bd9Sstevel@tonic-gate 			return (-1);
5379acbbeafSnn35248 
5389acbbeafSnn35248 		case FASTTRAP_INT:
5399acbbeafSnn35248 			/*
5409acbbeafSnn35248 			 * Interrupts seem like they could be traced with
5419acbbeafSnn35248 			 * no negative implications, but it's possible that
5429acbbeafSnn35248 			 * a thread could be redirected by the trap handling
5439acbbeafSnn35248 			 * code which would eventually return to the
5449acbbeafSnn35248 			 * instruction after the interrupt. If the interrupt
5459acbbeafSnn35248 			 * were in our scratch space, the subsequent
5469acbbeafSnn35248 			 * instruction might be overwritten before we return.
5479acbbeafSnn35248 			 * Accordingly we refuse to instrument any interrupt.
5489acbbeafSnn35248 			 */
5499acbbeafSnn35248 			return (-1);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate #ifdef __amd64
5547c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
5557c478bd9Sstevel@tonic-gate 		/*
5567c478bd9Sstevel@tonic-gate 		 * If the process is 64-bit and the instruction type is still
5577c478bd9Sstevel@tonic-gate 		 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
5587c478bd9Sstevel@tonic-gate 		 * execute it -- we need to watch for %rip-relative
5597c478bd9Sstevel@tonic-gate 		 * addressing mode. See the portion of fasttrap_pid_probe()
5607c478bd9Sstevel@tonic-gate 		 * below where we handle tracepoints with type
5617c478bd9Sstevel@tonic-gate 		 * FASTTRAP_T_COMMON for how we emulate instructions that
5627c478bd9Sstevel@tonic-gate 		 * employ %rip-relative addressing.
5637c478bd9Sstevel@tonic-gate 		 */
5647c478bd9Sstevel@tonic-gate 		if (rmindex != -1) {
5657c478bd9Sstevel@tonic-gate 			uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
5667c478bd9Sstevel@tonic-gate 			uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
5677c478bd9Sstevel@tonic-gate 			uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 			ASSERT(rmindex > start);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 			if (mod == 0 && rm == 5) {
5727c478bd9Sstevel@tonic-gate 				/*
5737c478bd9Sstevel@tonic-gate 				 * We need to be sure to avoid other
5747c478bd9Sstevel@tonic-gate 				 * registers used by this instruction. While
5757c478bd9Sstevel@tonic-gate 				 * the reg field may determine the op code
5767c478bd9Sstevel@tonic-gate 				 * rather than denoting a register, assuming
5777c478bd9Sstevel@tonic-gate 				 * that it denotes a register is always safe.
5787c478bd9Sstevel@tonic-gate 				 * We leave the REX field intact and use
5797c478bd9Sstevel@tonic-gate 				 * whatever value's there for simplicity.
5807c478bd9Sstevel@tonic-gate 				 */
5817c478bd9Sstevel@tonic-gate 				if (reg != 0) {
5827c478bd9Sstevel@tonic-gate 					tp->ftt_ripmode = FASTTRAP_RIP_1 |
5837c478bd9Sstevel@tonic-gate 					    (FASTTRAP_RIP_X *
5847c478bd9Sstevel@tonic-gate 					    FASTTRAP_REX_B(rex));
5857c478bd9Sstevel@tonic-gate 					rm = 0;
5867c478bd9Sstevel@tonic-gate 				} else {
5877c478bd9Sstevel@tonic-gate 					tp->ftt_ripmode = FASTTRAP_RIP_2 |
5887c478bd9Sstevel@tonic-gate 					    (FASTTRAP_RIP_X *
5897c478bd9Sstevel@tonic-gate 					    FASTTRAP_REX_B(rex));
5907c478bd9Sstevel@tonic-gate 					rm = 1;
5917c478bd9Sstevel@tonic-gate 				}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 				tp->ftt_modrm = tp->ftt_instr[rmindex];
5947c478bd9Sstevel@tonic-gate 				tp->ftt_instr[rmindex] =
5957c478bd9Sstevel@tonic-gate 				    FASTTRAP_MODRM(2, reg, rm);
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate #endif
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	return (0);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate int
6057c478bd9Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	fasttrap_instr_t instr = FASTTRAP_INSTR;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
6107c478bd9Sstevel@tonic-gate 		return (-1);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	return (0);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate int
6167c478bd9Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	uint8_t instr;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Distinguish between read or write failures and a changed
6227c478bd9Sstevel@tonic-gate 	 * instruction.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	if (uread(p, &instr, 1, tp->ftt_pc) != 0)
6257c478bd9Sstevel@tonic-gate 		return (0);
6267c478bd9Sstevel@tonic-gate 	if (instr != FASTTRAP_INSTR)
6277c478bd9Sstevel@tonic-gate 		return (0);
6287c478bd9Sstevel@tonic-gate 	if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
6297c478bd9Sstevel@tonic-gate 		return (-1);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	return (0);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
63473427c57Sahl #ifdef __amd64
6357c478bd9Sstevel@tonic-gate static uintptr_t
6367c478bd9Sstevel@tonic-gate fasttrap_fulword_noerr(const void *uaddr)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	uintptr_t ret;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (fasttrap_fulword(uaddr, &ret) == 0)
6417c478bd9Sstevel@tonic-gate 		return (ret);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	return (0);
6447c478bd9Sstevel@tonic-gate }
64573427c57Sahl #endif
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static uint32_t
6487c478bd9Sstevel@tonic-gate fasttrap_fuword32_noerr(const void *uaddr)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	uint32_t ret;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (fasttrap_fuword32(uaddr, &ret) == 0)
6537c478bd9Sstevel@tonic-gate 		return (ret);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	return (0);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate static void
6597c478bd9Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
6607c478bd9Sstevel@tonic-gate     uintptr_t new_pc)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
6637c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
6647c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
6657c478bd9Sstevel@tonic-gate 	kmutex_t *pid_mtx;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
6687c478bd9Sstevel@tonic-gate 	mutex_enter(pid_mtx);
6697c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6727c478bd9Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
673038dc6b3Sahl 		    tp->ftt_proc->ftpc_acount != 0)
6747c478bd9Sstevel@tonic-gate 			break;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/*
6787c478bd9Sstevel@tonic-gate 	 * Don't sweat it if we can't find the tracepoint again; unlike
6797c478bd9Sstevel@tonic-gate 	 * when we're in fasttrap_pid_probe(), finding the tracepoint here
6807c478bd9Sstevel@tonic-gate 	 * is not essential to the correct execution of the process.
6817c478bd9Sstevel@tonic-gate 	 */
6827c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
6837c478bd9Sstevel@tonic-gate 		mutex_exit(pid_mtx);
6847c478bd9Sstevel@tonic-gate 		return;
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
6887c478bd9Sstevel@tonic-gate 		/*
6897c478bd9Sstevel@tonic-gate 		 * If there's a branch that could act as a return site, we
6907c478bd9Sstevel@tonic-gate 		 * need to trace it, and check here if the program counter is
6917c478bd9Sstevel@tonic-gate 		 * external to the function.
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_RET &&
6947c478bd9Sstevel@tonic-gate 		    tp->ftt_type != FASTTRAP_T_RET16 &&
6957c478bd9Sstevel@tonic-gate 		    new_pc - id->fti_probe->ftp_faddr <
6967c478bd9Sstevel@tonic-gate 		    id->fti_probe->ftp_fsize)
6977c478bd9Sstevel@tonic-gate 			continue;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		dtrace_probe(id->fti_probe->ftp_id,
7007c478bd9Sstevel@tonic-gate 		    pc - id->fti_probe->ftp_faddr,
7017c478bd9Sstevel@tonic-gate 		    rp->r_r0, rp->r_r1, 0, 0);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	mutex_exit(pid_mtx);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate static void
7087c478bd9Sstevel@tonic-gate fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGSEGV;
7137c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_code = SEGV_MAPERR;
7147c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)addr;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7177c478bd9Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
7187c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (t != NULL)
7217c478bd9Sstevel@tonic-gate 		aston(t);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate #ifdef __amd64
7257c478bd9Sstevel@tonic-gate static void
7267c478bd9Sstevel@tonic-gate fasttrap_usdt_args64(fasttrap_probe_t *probe, struct regs *rp, int argc,
7277c478bd9Sstevel@tonic-gate     uintptr_t *argv)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
7307c478bd9Sstevel@tonic-gate 	uintptr_t *stack = (uintptr_t *)rp->r_sp;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	for (i = 0; i < cap; i++) {
7337c478bd9Sstevel@tonic-gate 		x = probe->ftp_argmap[i];
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		if (x < 6)
7367c478bd9Sstevel@tonic-gate 			argv[i] = (&rp->r_rdi)[x];
7377c478bd9Sstevel@tonic-gate 		else
7387c478bd9Sstevel@tonic-gate 			argv[i] = fasttrap_fulword_noerr(&stack[x]);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	for (; i < argc; i++) {
7427c478bd9Sstevel@tonic-gate 		argv[i] = 0;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate #endif
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate static void
7487c478bd9Sstevel@tonic-gate fasttrap_usdt_args32(fasttrap_probe_t *probe, struct regs *rp, int argc,
7497c478bd9Sstevel@tonic-gate     uint32_t *argv)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	int i, x, cap = MIN(argc, probe->ftp_nargs);
7527c478bd9Sstevel@tonic-gate 	uint32_t *stack = (uint32_t *)rp->r_sp;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	for (i = 0; i < cap; i++) {
7557c478bd9Sstevel@tonic-gate 		x = probe->ftp_argmap[i];
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		argv[i] = fasttrap_fuword32_noerr(&stack[x]);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	for (; i < argc; i++) {
7617c478bd9Sstevel@tonic-gate 		argv[i] = 0;
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7659acbbeafSnn35248 static int
7669acbbeafSnn35248 fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct regs *rp, uintptr_t *addr)
7679acbbeafSnn35248 {
7689acbbeafSnn35248 	proc_t *p = curproc;
7699acbbeafSnn35248 	user_desc_t *desc;
7709acbbeafSnn35248 	uint16_t sel, ndx, type;
7719acbbeafSnn35248 	uintptr_t limit;
7729acbbeafSnn35248 
7739acbbeafSnn35248 	switch (tp->ftt_segment) {
7749acbbeafSnn35248 	case FASTTRAP_SEG_CS:
7759acbbeafSnn35248 		sel = rp->r_cs;
7769acbbeafSnn35248 		break;
7779acbbeafSnn35248 	case FASTTRAP_SEG_DS:
7789acbbeafSnn35248 		sel = rp->r_ds;
7799acbbeafSnn35248 		break;
7809acbbeafSnn35248 	case FASTTRAP_SEG_ES:
7819acbbeafSnn35248 		sel = rp->r_es;
7829acbbeafSnn35248 		break;
7839acbbeafSnn35248 	case FASTTRAP_SEG_FS:
7849acbbeafSnn35248 		sel = rp->r_fs;
7859acbbeafSnn35248 		break;
7869acbbeafSnn35248 	case FASTTRAP_SEG_GS:
7879acbbeafSnn35248 		sel = rp->r_gs;
7889acbbeafSnn35248 		break;
7899acbbeafSnn35248 	case FASTTRAP_SEG_SS:
7909acbbeafSnn35248 		sel = rp->r_ss;
7919acbbeafSnn35248 		break;
7929acbbeafSnn35248 	}
7939acbbeafSnn35248 
7949acbbeafSnn35248 	/*
7959acbbeafSnn35248 	 * Make sure the given segment register specifies a user priority
7969acbbeafSnn35248 	 * selector rather than a kernel selector.
7979acbbeafSnn35248 	 */
7989acbbeafSnn35248 	if (!SELISUPL(sel))
7999acbbeafSnn35248 		return (-1);
8009acbbeafSnn35248 
8019acbbeafSnn35248 	ndx = SELTOIDX(sel);
8029acbbeafSnn35248 
8039acbbeafSnn35248 	/*
8049acbbeafSnn35248 	 * Check the bounds and grab the descriptor out of the specified
8059acbbeafSnn35248 	 * descriptor table.
8069acbbeafSnn35248 	 */
8079acbbeafSnn35248 	if (SELISLDT(sel)) {
8089acbbeafSnn35248 		if (ndx > p->p_ldtlimit)
8099acbbeafSnn35248 			return (-1);
8109acbbeafSnn35248 
8119acbbeafSnn35248 		desc = p->p_ldt + ndx;
8129acbbeafSnn35248 
8139acbbeafSnn35248 	} else {
8149acbbeafSnn35248 		if (ndx >= NGDT)
8159acbbeafSnn35248 			return (-1);
8169acbbeafSnn35248 
8179acbbeafSnn35248 		desc = cpu_get_gdt() + ndx;
8189acbbeafSnn35248 	}
8199acbbeafSnn35248 
8209acbbeafSnn35248 	/*
8219acbbeafSnn35248 	 * The descriptor must have user privilege level and it must be
8229acbbeafSnn35248 	 * present in memory.
8239acbbeafSnn35248 	 */
8249acbbeafSnn35248 	if (desc->usd_dpl != SEL_UPL || desc->usd_p != 1)
8259acbbeafSnn35248 		return (-1);
8269acbbeafSnn35248 
8279acbbeafSnn35248 	type = desc->usd_type;
8289acbbeafSnn35248 
8299acbbeafSnn35248 	/*
8309acbbeafSnn35248 	 * If the S bit in the type field is not set, this descriptor can
8319acbbeafSnn35248 	 * only be used in system context.
8329acbbeafSnn35248 	 */
8339acbbeafSnn35248 	if ((type & 0x10) != 0x10)
8349acbbeafSnn35248 		return (-1);
8359acbbeafSnn35248 
8369acbbeafSnn35248 	limit = USEGD_GETLIMIT(desc) * (desc->usd_gran ? PAGESIZE : 1);
8379acbbeafSnn35248 
8389acbbeafSnn35248 	if (tp->ftt_segment == FASTTRAP_SEG_CS) {
8399acbbeafSnn35248 		/*
8409acbbeafSnn35248 		 * The code/data bit and readable bit must both be set.
8419acbbeafSnn35248 		 */
8429acbbeafSnn35248 		if ((type & 0xa) != 0xa)
8439acbbeafSnn35248 			return (-1);
8449acbbeafSnn35248 
8459acbbeafSnn35248 		if (*addr > limit)
8469acbbeafSnn35248 			return (-1);
8479acbbeafSnn35248 	} else {
8489acbbeafSnn35248 		/*
8499acbbeafSnn35248 		 * The code/data bit must be clear.
8509acbbeafSnn35248 		 */
8519acbbeafSnn35248 		if ((type & 0x8) != 0)
8529acbbeafSnn35248 			return (-1);
8539acbbeafSnn35248 
8549acbbeafSnn35248 		/*
8559acbbeafSnn35248 		 * If the expand-down bit is clear, we just check the limit as
8569acbbeafSnn35248 		 * it would naturally be applied. Otherwise, we need to check
8579acbbeafSnn35248 		 * that the address is the range [limit + 1 .. 0xffff] or
8589acbbeafSnn35248 		 * [limit + 1 ... 0xffffffff] depending on if the default
8599acbbeafSnn35248 		 * operand size bit is set.
8609acbbeafSnn35248 		 */
8619acbbeafSnn35248 		if ((type & 0x4) == 0) {
8629acbbeafSnn35248 			if (*addr > limit)
8639acbbeafSnn35248 				return (-1);
8649acbbeafSnn35248 		} else if (desc->usd_def32) {
8659acbbeafSnn35248 			if (*addr < limit + 1 || 0xffff < *addr)
8669acbbeafSnn35248 				return (-1);
8679acbbeafSnn35248 		} else {
8689acbbeafSnn35248 			if (*addr < limit + 1 || 0xffffffff < *addr)
8699acbbeafSnn35248 				return (-1);
8709acbbeafSnn35248 		}
8719acbbeafSnn35248 	}
8729acbbeafSnn35248 
8739acbbeafSnn35248 	*addr += USEGD_GETBASE(desc);
8749acbbeafSnn35248 
8759acbbeafSnn35248 	return (0);
8769acbbeafSnn35248 }
8779acbbeafSnn35248 
8787c478bd9Sstevel@tonic-gate int
8797c478bd9Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8827c478bd9Sstevel@tonic-gate 	uintptr_t pc = rp->r_pc - 1, new_pc = 0;
8837c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
8847c478bd9Sstevel@tonic-gate 	kmutex_t *pid_mtx;
8857c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, tp_local;
8867c478bd9Sstevel@tonic-gate 	pid_t pid;
8877c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
888ac448965Sahl 	uint_t is_enabled = 0;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * It's possible that a user (in a veritable orgy of bad planning)
8927c478bd9Sstevel@tonic-gate 	 * could redirect this thread's flow of control before it reached the
8937c478bd9Sstevel@tonic-gate 	 * return probe fasttrap. In this case we need to kill the process
8947c478bd9Sstevel@tonic-gate 	 * since it's in a unrecoverable state.
8957c478bd9Sstevel@tonic-gate 	 */
8967c478bd9Sstevel@tonic-gate 	if (curthread->t_dtrace_step) {
8977c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_dtrace_on);
8987c478bd9Sstevel@tonic-gate 		fasttrap_sigtrap(p, curthread, pc);
8997c478bd9Sstevel@tonic-gate 		return (0);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * Clear all user tracing flags.
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_ft = 0;
9067c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
9077c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
9087c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
9097c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
9107c478bd9Sstevel@tonic-gate #ifdef __amd64
9117c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_regv = 0;
9127c478bd9Sstevel@tonic-gate #endif
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
9167c478bd9Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
9177c478bd9Sstevel@tonic-gate 	 * process: this one.
9187c478bd9Sstevel@tonic-gate 	 */
9197c478bd9Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
9207c478bd9Sstevel@tonic-gate 		p = p->p_parent;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	pid = p->p_pid;
9247c478bd9Sstevel@tonic-gate 	pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
9257c478bd9Sstevel@tonic-gate 	mutex_enter(pid_mtx);
9267c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Lookup the tracepoint that the process just hit.
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
9327c478bd9Sstevel@tonic-gate 		if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
933038dc6b3Sahl 		    tp->ftt_proc->ftpc_acount != 0)
9347c478bd9Sstevel@tonic-gate 			break;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * If we couldn't find a matching tracepoint, either a tracepoint has
9397c478bd9Sstevel@tonic-gate 	 * been inserted without using the pid<pid> ioctl interface (see
9407c478bd9Sstevel@tonic-gate 	 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
9437c478bd9Sstevel@tonic-gate 		mutex_exit(pid_mtx);
9447c478bd9Sstevel@tonic-gate 		return (-1);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * Set the program counter to the address of the traced instruction
9497c478bd9Sstevel@tonic-gate 	 * so that it looks right in ustack() output.
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	rp->r_pc = pc;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (tp->ftt_ids != NULL) {
9547c478bd9Sstevel@tonic-gate 		fasttrap_id_t *id;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate #ifdef __amd64
9577c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
9587c478bd9Sstevel@tonic-gate 			for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
9597c478bd9Sstevel@tonic-gate 				fasttrap_probe_t *probe = id->fti_probe;
9607c478bd9Sstevel@tonic-gate 
961ac448965Sahl 				if (id->fti_ptype == DTFTP_ENTRY) {
9627c478bd9Sstevel@tonic-gate 					/*
9637c478bd9Sstevel@tonic-gate 					 * We note that this was an entry
9647c478bd9Sstevel@tonic-gate 					 * probe to help ustack() find the
9657c478bd9Sstevel@tonic-gate 					 * first caller.
9667c478bd9Sstevel@tonic-gate 					 */
9677c478bd9Sstevel@tonic-gate 					cookie = dtrace_interrupt_disable();
9687c478bd9Sstevel@tonic-gate 					DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
9697c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, rp->r_rdi,
9707c478bd9Sstevel@tonic-gate 					    rp->r_rsi, rp->r_rdx, rp->r_rcx,
9717c478bd9Sstevel@tonic-gate 					    rp->r_r8);
9727c478bd9Sstevel@tonic-gate 					DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
9737c478bd9Sstevel@tonic-gate 					dtrace_interrupt_enable(cookie);
974ac448965Sahl 				} else if (id->fti_ptype == DTFTP_IS_ENABLED) {
975ac448965Sahl 					/*
976ac448965Sahl 					 * Note that in this case, we don't
977ac448965Sahl 					 * call dtrace_probe() since it's only
978ac448965Sahl 					 * an artificial probe meant to change
979ac448965Sahl 					 * the flow of control so that it
980ac448965Sahl 					 * encounters the true probe.
981ac448965Sahl 					 */
982ac448965Sahl 					is_enabled = 1;
9837c478bd9Sstevel@tonic-gate 				} else if (probe->ftp_argmap == NULL) {
9847c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, rp->r_rdi,
9857c478bd9Sstevel@tonic-gate 					    rp->r_rsi, rp->r_rdx, rp->r_rcx,
9867c478bd9Sstevel@tonic-gate 					    rp->r_r8);
9877c478bd9Sstevel@tonic-gate 				} else {
9887c478bd9Sstevel@tonic-gate 					uintptr_t t[5];
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 					fasttrap_usdt_args64(probe, rp,
9917c478bd9Sstevel@tonic-gate 					    sizeof (t) / sizeof (t[0]), t);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, t[0], t[1],
9947c478bd9Sstevel@tonic-gate 					    t[2], t[3], t[4]);
9957c478bd9Sstevel@tonic-gate 				}
9967c478bd9Sstevel@tonic-gate 			}
9977c478bd9Sstevel@tonic-gate 		} else {
9987c478bd9Sstevel@tonic-gate #endif
9997c478bd9Sstevel@tonic-gate 			uintptr_t s0, s1, s2, s3, s4, s5;
10007c478bd9Sstevel@tonic-gate 			uint32_t *stack = (uint32_t *)rp->r_sp;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 			/*
10037c478bd9Sstevel@tonic-gate 			 * In 32-bit mode, all arguments are passed on the
10047c478bd9Sstevel@tonic-gate 			 * stack. If this is a function entry probe, we need
10057c478bd9Sstevel@tonic-gate 			 * to skip the first entry on the stack as it
10067c478bd9Sstevel@tonic-gate 			 * represents the return address rather than a
10077c478bd9Sstevel@tonic-gate 			 * parameter to the function.
10087c478bd9Sstevel@tonic-gate 			 */
10097c478bd9Sstevel@tonic-gate 			s0 = fasttrap_fuword32_noerr(&stack[0]);
10107c478bd9Sstevel@tonic-gate 			s1 = fasttrap_fuword32_noerr(&stack[1]);
10117c478bd9Sstevel@tonic-gate 			s2 = fasttrap_fuword32_noerr(&stack[2]);
10127c478bd9Sstevel@tonic-gate 			s3 = fasttrap_fuword32_noerr(&stack[3]);
10137c478bd9Sstevel@tonic-gate 			s4 = fasttrap_fuword32_noerr(&stack[4]);
10147c478bd9Sstevel@tonic-gate 			s5 = fasttrap_fuword32_noerr(&stack[5]);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
10177c478bd9Sstevel@tonic-gate 				fasttrap_probe_t *probe = id->fti_probe;
10187c478bd9Sstevel@tonic-gate 
1019ac448965Sahl 				if (id->fti_ptype == DTFTP_ENTRY) {
10207c478bd9Sstevel@tonic-gate 					/*
10217c478bd9Sstevel@tonic-gate 					 * We note that this was an entry
10227c478bd9Sstevel@tonic-gate 					 * probe to help ustack() find the
10237c478bd9Sstevel@tonic-gate 					 * first caller.
10247c478bd9Sstevel@tonic-gate 					 */
10257c478bd9Sstevel@tonic-gate 					cookie = dtrace_interrupt_disable();
10267c478bd9Sstevel@tonic-gate 					DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
10277c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, s1, s2,
10287c478bd9Sstevel@tonic-gate 					    s3, s4, s5);
10297c478bd9Sstevel@tonic-gate 					DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
10307c478bd9Sstevel@tonic-gate 					dtrace_interrupt_enable(cookie);
1031ac448965Sahl 				} else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1032ac448965Sahl 					/*
1033ac448965Sahl 					 * Note that in this case, we don't
1034ac448965Sahl 					 * call dtrace_probe() since it's only
1035ac448965Sahl 					 * an artificial probe meant to change
1036ac448965Sahl 					 * the flow of control so that it
1037ac448965Sahl 					 * encounters the true probe.
1038ac448965Sahl 					 */
1039ac448965Sahl 					is_enabled = 1;
10407c478bd9Sstevel@tonic-gate 				} else if (probe->ftp_argmap == NULL) {
10417c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, s0, s1,
10427c478bd9Sstevel@tonic-gate 					    s2, s3, s4);
10437c478bd9Sstevel@tonic-gate 				} else {
10447c478bd9Sstevel@tonic-gate 					uint32_t t[5];
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 					fasttrap_usdt_args32(probe, rp,
10477c478bd9Sstevel@tonic-gate 					    sizeof (t) / sizeof (t[0]), t);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 					dtrace_probe(probe->ftp_id, t[0], t[1],
10507c478bd9Sstevel@tonic-gate 					    t[2], t[3], t[4]);
10517c478bd9Sstevel@tonic-gate 				}
10527c478bd9Sstevel@tonic-gate 			}
10537c478bd9Sstevel@tonic-gate #ifdef __amd64
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate #endif
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/*
10597c478bd9Sstevel@tonic-gate 	 * We're about to do a bunch of work so we cache a local copy of
10607c478bd9Sstevel@tonic-gate 	 * the tracepoint to emulate the instruction, and then find the
10617c478bd9Sstevel@tonic-gate 	 * tracepoint again later if we need to light up any return probes.
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	tp_local = *tp;
10647c478bd9Sstevel@tonic-gate 	mutex_exit(pid_mtx);
10657c478bd9Sstevel@tonic-gate 	tp = &tp_local;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	/*
10687c478bd9Sstevel@tonic-gate 	 * Set the program counter to appear as though the traced instruction
10697c478bd9Sstevel@tonic-gate 	 * had completely executed. This ensures that fasttrap_getreg() will
10707c478bd9Sstevel@tonic-gate 	 * report the expected value for REG_RIP.
10717c478bd9Sstevel@tonic-gate 	 */
10727c478bd9Sstevel@tonic-gate 	rp->r_pc = pc + tp->ftt_size;
10737c478bd9Sstevel@tonic-gate 
1074ac448965Sahl 	/*
1075ac448965Sahl 	 * If there's an is-enabled probe connected to this tracepoint it
1076ac448965Sahl 	 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1077ac448965Sahl 	 * instruction that was placed there by DTrace when the binary was
1078ac448965Sahl 	 * linked. As this probe is, in fact, enabled, we need to stuff 1
1079ac448965Sahl 	 * into %eax or %rax. Accordingly, we can bypass all the instruction
1080ac448965Sahl 	 * emulation logic since we know the inevitable result. It's possible
1081ac448965Sahl 	 * that a user could construct a scenario where the 'is-enabled'
1082ac448965Sahl 	 * probe was on some other instruction, but that would be a rather
1083ac448965Sahl 	 * exotic way to shoot oneself in the foot.
1084ac448965Sahl 	 */
1085ac448965Sahl 	if (is_enabled) {
1086ac448965Sahl 		rp->r_r0 = 1;
1087ac448965Sahl 		new_pc = rp->r_pc;
1088ac448965Sahl 		goto done;
1089ac448965Sahl 	}
1090ac448965Sahl 
1091ac448965Sahl 	/*
1092ac448965Sahl 	 * We emulate certain types of instructions to ensure correctness
1093ac448965Sahl 	 * (in the case of position dependent instructions) or optimize
1094ac448965Sahl 	 * common cases. The rest we have the thread execute back in user-
1095ac448965Sahl 	 * land.
1096ac448965Sahl 	 */
10977c478bd9Sstevel@tonic-gate 	switch (tp->ftt_type) {
10987c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_RET:
10997c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_RET16:
11007c478bd9Sstevel@tonic-gate 	{
11017c478bd9Sstevel@tonic-gate 		uintptr_t dst;
11027c478bd9Sstevel@tonic-gate 		uintptr_t addr;
11037c478bd9Sstevel@tonic-gate 		int ret;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		/*
11067c478bd9Sstevel@tonic-gate 		 * We have to emulate _every_ facet of the behavior of a ret
11077c478bd9Sstevel@tonic-gate 		 * instruction including what happens if the load from %esp
11087c478bd9Sstevel@tonic-gate 		 * fails; in that case, we send a SIGSEGV.
11097c478bd9Sstevel@tonic-gate 		 */
11107c478bd9Sstevel@tonic-gate #ifdef __amd64
11117c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE) {
11127c478bd9Sstevel@tonic-gate #endif
11137c478bd9Sstevel@tonic-gate 			ret = fasttrap_fulword((void *)rp->r_sp, &dst);
11147c478bd9Sstevel@tonic-gate 			addr = rp->r_sp + sizeof (uintptr_t);
11157c478bd9Sstevel@tonic-gate #ifdef __amd64
11167c478bd9Sstevel@tonic-gate 		} else {
11177c478bd9Sstevel@tonic-gate 			uint32_t dst32;
11187c478bd9Sstevel@tonic-gate 			ret = fasttrap_fuword32((void *)rp->r_sp, &dst32);
11197c478bd9Sstevel@tonic-gate 			dst = dst32;
11207c478bd9Sstevel@tonic-gate 			addr = rp->r_sp + sizeof (uint32_t);
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate #endif
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		if (ret == -1) {
11257c478bd9Sstevel@tonic-gate 			fasttrap_sigsegv(p, curthread, rp->r_sp);
11267c478bd9Sstevel@tonic-gate 			new_pc = pc;
11277c478bd9Sstevel@tonic-gate 			break;
11287c478bd9Sstevel@tonic-gate 		}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		if (tp->ftt_type == FASTTRAP_T_RET16)
11317c478bd9Sstevel@tonic-gate 			addr += tp->ftt_dest;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		rp->r_sp = addr;
11347c478bd9Sstevel@tonic-gate 		new_pc = dst;
11357c478bd9Sstevel@tonic-gate 		break;
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_JCC:
11397c478bd9Sstevel@tonic-gate 	{
11407c478bd9Sstevel@tonic-gate 		uint_t taken;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		switch (tp->ftt_code) {
11437c478bd9Sstevel@tonic-gate 		case FASTTRAP_JO:
11447c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) != 0;
11457c478bd9Sstevel@tonic-gate 			break;
11467c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNO:
11477c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) == 0;
11487c478bd9Sstevel@tonic-gate 			break;
11497c478bd9Sstevel@tonic-gate 		case FASTTRAP_JB:
11507c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0;
11517c478bd9Sstevel@tonic-gate 			break;
11527c478bd9Sstevel@tonic-gate 		case FASTTRAP_JAE:
11537c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0;
11547c478bd9Sstevel@tonic-gate 			break;
11557c478bd9Sstevel@tonic-gate 		case FASTTRAP_JE:
11567c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
11577c478bd9Sstevel@tonic-gate 			break;
11587c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNE:
11597c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
11607c478bd9Sstevel@tonic-gate 			break;
11617c478bd9Sstevel@tonic-gate 		case FASTTRAP_JBE:
11627c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0 ||
11637c478bd9Sstevel@tonic-gate 			    (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
11647c478bd9Sstevel@tonic-gate 			break;
11657c478bd9Sstevel@tonic-gate 		case FASTTRAP_JA:
11667c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0 &&
11677c478bd9Sstevel@tonic-gate 			    (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
11687c478bd9Sstevel@tonic-gate 			break;
11697c478bd9Sstevel@tonic-gate 		case FASTTRAP_JS:
11707c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) != 0;
11717c478bd9Sstevel@tonic-gate 			break;
11727c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNS:
11737c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) == 0;
11747c478bd9Sstevel@tonic-gate 			break;
11757c478bd9Sstevel@tonic-gate 		case FASTTRAP_JP:
11767c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) != 0;
11777c478bd9Sstevel@tonic-gate 			break;
11787c478bd9Sstevel@tonic-gate 		case FASTTRAP_JNP:
11797c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) == 0;
11807c478bd9Sstevel@tonic-gate 			break;
11817c478bd9Sstevel@tonic-gate 		case FASTTRAP_JL:
11827c478bd9Sstevel@tonic-gate 			taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
11837c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11847c478bd9Sstevel@tonic-gate 			break;
11857c478bd9Sstevel@tonic-gate 		case FASTTRAP_JGE:
11867c478bd9Sstevel@tonic-gate 			taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
11877c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11887c478bd9Sstevel@tonic-gate 			break;
11897c478bd9Sstevel@tonic-gate 		case FASTTRAP_JLE:
11907c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 ||
11917c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
11927c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11937c478bd9Sstevel@tonic-gate 			break;
11947c478bd9Sstevel@tonic-gate 		case FASTTRAP_JG:
11957c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
11967c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
11977c478bd9Sstevel@tonic-gate 			    ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11987c478bd9Sstevel@tonic-gate 			break;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		if (taken)
12037c478bd9Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
12047c478bd9Sstevel@tonic-gate 		else
12057c478bd9Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
12067c478bd9Sstevel@tonic-gate 		break;
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_LOOP:
12107c478bd9Sstevel@tonic-gate 	{
12117c478bd9Sstevel@tonic-gate 		uint_t taken;
12127c478bd9Sstevel@tonic-gate #ifdef __amd64
12137c478bd9Sstevel@tonic-gate 		greg_t cx = rp->r_rcx--;
12147c478bd9Sstevel@tonic-gate #else
12157c478bd9Sstevel@tonic-gate 		greg_t cx = rp->r_ecx--;
12167c478bd9Sstevel@tonic-gate #endif
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		switch (tp->ftt_code) {
12197c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOPNZ:
12207c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
12217c478bd9Sstevel@tonic-gate 			    cx != 0;
12227c478bd9Sstevel@tonic-gate 			break;
12237c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOPZ:
12247c478bd9Sstevel@tonic-gate 			taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 &&
12257c478bd9Sstevel@tonic-gate 			    cx != 0;
12267c478bd9Sstevel@tonic-gate 			break;
12277c478bd9Sstevel@tonic-gate 		case FASTTRAP_LOOP:
12287c478bd9Sstevel@tonic-gate 			taken = (cx != 0);
12297c478bd9Sstevel@tonic-gate 			break;
12307c478bd9Sstevel@tonic-gate 		}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		if (taken)
12337c478bd9Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
12347c478bd9Sstevel@tonic-gate 		else
12357c478bd9Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
12367c478bd9Sstevel@tonic-gate 		break;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_JCXZ:
12407c478bd9Sstevel@tonic-gate 	{
12417c478bd9Sstevel@tonic-gate #ifdef __amd64
12427c478bd9Sstevel@tonic-gate 		greg_t cx = rp->r_rcx;
12437c478bd9Sstevel@tonic-gate #else
12447c478bd9Sstevel@tonic-gate 		greg_t cx = rp->r_ecx;
12457c478bd9Sstevel@tonic-gate #endif
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		if (cx == 0)
12487c478bd9Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
12497c478bd9Sstevel@tonic-gate 		else
12507c478bd9Sstevel@tonic-gate 			new_pc = pc + tp->ftt_size;
12517c478bd9Sstevel@tonic-gate 		break;
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_PUSHL_EBP:
12557c478bd9Sstevel@tonic-gate 	{
12567c478bd9Sstevel@tonic-gate 		int ret;
12577c478bd9Sstevel@tonic-gate 		uintptr_t addr;
12587c478bd9Sstevel@tonic-gate #ifdef __amd64
12597c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE) {
12607c478bd9Sstevel@tonic-gate #endif
12617c478bd9Sstevel@tonic-gate 			addr = rp->r_sp - sizeof (uintptr_t);
12627c478bd9Sstevel@tonic-gate 			ret = fasttrap_sulword((void *)addr, rp->r_fp);
12637c478bd9Sstevel@tonic-gate #ifdef __amd64
12647c478bd9Sstevel@tonic-gate 		} else {
12657c478bd9Sstevel@tonic-gate 			addr = rp->r_sp - sizeof (uint32_t);
12667c478bd9Sstevel@tonic-gate 			ret = fasttrap_suword32((void *)addr,
12677c478bd9Sstevel@tonic-gate 			    (uint32_t)rp->r_fp);
12687c478bd9Sstevel@tonic-gate 		}
12697c478bd9Sstevel@tonic-gate #endif
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		if (ret == -1) {
12727c478bd9Sstevel@tonic-gate 			fasttrap_sigsegv(p, curthread, addr);
12737c478bd9Sstevel@tonic-gate 			new_pc = pc;
12747c478bd9Sstevel@tonic-gate 			break;
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		rp->r_sp = addr;
12787c478bd9Sstevel@tonic-gate 		new_pc = pc + tp->ftt_size;
12797c478bd9Sstevel@tonic-gate 		break;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12822b6e762cSahl 	case FASTTRAP_T_NOP:
12832b6e762cSahl 		new_pc = pc + tp->ftt_size;
12842b6e762cSahl 		break;
12852b6e762cSahl 
12867c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_JMP:
12877c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_CALL:
12887c478bd9Sstevel@tonic-gate 		if (tp->ftt_code == 0) {
12897c478bd9Sstevel@tonic-gate 			new_pc = tp->ftt_dest;
12907c478bd9Sstevel@tonic-gate 		} else {
12919acbbeafSnn35248 			uintptr_t value, addr = tp->ftt_dest;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 			if (tp->ftt_base != FASTTRAP_NOREG)
12947c478bd9Sstevel@tonic-gate 				addr += fasttrap_getreg(rp, tp->ftt_base);
12957c478bd9Sstevel@tonic-gate 			if (tp->ftt_index != FASTTRAP_NOREG)
12967c478bd9Sstevel@tonic-gate 				addr += fasttrap_getreg(rp, tp->ftt_index) <<
12977c478bd9Sstevel@tonic-gate 				    tp->ftt_scale;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 			if (tp->ftt_code == 1) {
13009acbbeafSnn35248 				/*
13019acbbeafSnn35248 				 * If there's a segment prefix for this
13029acbbeafSnn35248 				 * instruction, we'll need to check permissions
13039acbbeafSnn35248 				 * and bounds on the given selector, and adjust
13049acbbeafSnn35248 				 * the address accordingly.
13059acbbeafSnn35248 				 */
13069acbbeafSnn35248 				if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
13079acbbeafSnn35248 				    fasttrap_do_seg(tp, rp, &addr) != 0) {
13089acbbeafSnn35248 					fasttrap_sigsegv(p, curthread, addr);
13099acbbeafSnn35248 					new_pc = pc;
13109acbbeafSnn35248 					break;
13119acbbeafSnn35248 				}
13129acbbeafSnn35248 
13137c478bd9Sstevel@tonic-gate #ifdef __amd64
13147c478bd9Sstevel@tonic-gate 				if (p->p_model == DATAMODEL_NATIVE) {
13157c478bd9Sstevel@tonic-gate #endif
13167c478bd9Sstevel@tonic-gate 					if (fasttrap_fulword((void *)addr,
13177c478bd9Sstevel@tonic-gate 					    &value) == -1) {
13187c478bd9Sstevel@tonic-gate 						fasttrap_sigsegv(p, curthread,
13197c478bd9Sstevel@tonic-gate 						    addr);
13207c478bd9Sstevel@tonic-gate 						new_pc = pc;
13217c478bd9Sstevel@tonic-gate 						break;
13227c478bd9Sstevel@tonic-gate 					}
13237c478bd9Sstevel@tonic-gate 					new_pc = value;
13247c478bd9Sstevel@tonic-gate #ifdef __amd64
13257c478bd9Sstevel@tonic-gate 				} else {
13269acbbeafSnn35248 					uint32_t value32;
13279acbbeafSnn35248 					addr = (uintptr_t)(uint32_t)addr;
13287c478bd9Sstevel@tonic-gate 					if (fasttrap_fuword32((void *)addr,
13299acbbeafSnn35248 					    &value32) == -1) {
13307c478bd9Sstevel@tonic-gate 						fasttrap_sigsegv(p, curthread,
13317c478bd9Sstevel@tonic-gate 						    addr);
13327c478bd9Sstevel@tonic-gate 						new_pc = pc;
13337c478bd9Sstevel@tonic-gate 						break;
13347c478bd9Sstevel@tonic-gate 					}
13359acbbeafSnn35248 					new_pc = value32;
13367c478bd9Sstevel@tonic-gate 				}
13377c478bd9Sstevel@tonic-gate #endif
13387c478bd9Sstevel@tonic-gate 			} else {
13397c478bd9Sstevel@tonic-gate 				new_pc = addr;
13407c478bd9Sstevel@tonic-gate 			}
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		/*
13447c478bd9Sstevel@tonic-gate 		 * If this is a call instruction, we need to push the return
13457c478bd9Sstevel@tonic-gate 		 * address onto the stack. If this fails, we send the process
13467c478bd9Sstevel@tonic-gate 		 * a SIGSEGV and reset the pc to emulate what would happen if
13477c478bd9Sstevel@tonic-gate 		 * this instruction weren't traced.
13487c478bd9Sstevel@tonic-gate 		 */
13497c478bd9Sstevel@tonic-gate 		if (tp->ftt_type == FASTTRAP_T_CALL) {
13507c478bd9Sstevel@tonic-gate 			int ret;
13517c478bd9Sstevel@tonic-gate 			uintptr_t addr;
13527c478bd9Sstevel@tonic-gate #ifdef __amd64
13537c478bd9Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
13547c478bd9Sstevel@tonic-gate 				addr = rp->r_sp - sizeof (uintptr_t);
13557c478bd9Sstevel@tonic-gate 				ret = fasttrap_sulword((void *)addr,
13567c478bd9Sstevel@tonic-gate 				    pc + tp->ftt_size);
13577c478bd9Sstevel@tonic-gate 			} else {
13587c478bd9Sstevel@tonic-gate #endif
13597c478bd9Sstevel@tonic-gate 				addr = rp->r_sp - sizeof (uint32_t);
13607c478bd9Sstevel@tonic-gate 				ret = fasttrap_suword32((void *)addr,
13617c478bd9Sstevel@tonic-gate 				    (uint32_t)(pc + tp->ftt_size));
13627c478bd9Sstevel@tonic-gate #ifdef __amd64
13637c478bd9Sstevel@tonic-gate 			}
13647c478bd9Sstevel@tonic-gate #endif
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 			if (ret == -1) {
13677c478bd9Sstevel@tonic-gate 				fasttrap_sigsegv(p, curthread, addr);
13687c478bd9Sstevel@tonic-gate 				new_pc = pc;
13697c478bd9Sstevel@tonic-gate 				break;
13707c478bd9Sstevel@tonic-gate 			}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 			rp->r_sp = addr;
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		break;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	case FASTTRAP_T_COMMON:
13787c478bd9Sstevel@tonic-gate 	{
13797c478bd9Sstevel@tonic-gate 		uintptr_t addr;
1380*6009dbc6Sahl #if defined(__amd64)
1381*6009dbc6Sahl 		uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22];
1382*6009dbc6Sahl #else
1383*6009dbc6Sahl 		uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7];
1384*6009dbc6Sahl #endif
13857c478bd9Sstevel@tonic-gate 		uint_t i = 0;
13867c478bd9Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 		/*
13897c478bd9Sstevel@tonic-gate 		 * Compute the address of the ulwp_t and step over the
13907c478bd9Sstevel@tonic-gate 		 * ul_self pointer. The method used to store the user-land
13917c478bd9Sstevel@tonic-gate 		 * thread pointer is very different on 32- and 64-bit
13927c478bd9Sstevel@tonic-gate 		 * kernels.
13937c478bd9Sstevel@tonic-gate 		 */
13947c478bd9Sstevel@tonic-gate #if defined(__amd64)
13957c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
13967c478bd9Sstevel@tonic-gate 			addr = lwp->lwp_pcb.pcb_fsbase;
13977c478bd9Sstevel@tonic-gate 			addr += sizeof (void *);
13987c478bd9Sstevel@tonic-gate 		} else {
13997c478bd9Sstevel@tonic-gate 			addr = lwp->lwp_pcb.pcb_gsbase;
14007c478bd9Sstevel@tonic-gate 			addr += sizeof (caddr32_t);
14017c478bd9Sstevel@tonic-gate 		}
1402*6009dbc6Sahl #else
14037c478bd9Sstevel@tonic-gate 		addr = USEGD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
14047c478bd9Sstevel@tonic-gate 		addr += sizeof (void *);
14057c478bd9Sstevel@tonic-gate #endif
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		/*
14087c478bd9Sstevel@tonic-gate 		 * Generic Instruction Tracing
14097c478bd9Sstevel@tonic-gate 		 * ---------------------------
14107c478bd9Sstevel@tonic-gate 		 *
14117c478bd9Sstevel@tonic-gate 		 * This is the layout of the scratch space in the user-land
14127c478bd9Sstevel@tonic-gate 		 * thread structure for our generated instructions.
14137c478bd9Sstevel@tonic-gate 		 *
14147c478bd9Sstevel@tonic-gate 		 *	32-bit mode			bytes
14157c478bd9Sstevel@tonic-gate 		 *	------------------------	-----
14167c478bd9Sstevel@tonic-gate 		 * a:	<original instruction>		<= 15
14177c478bd9Sstevel@tonic-gate 		 *	jmp	<pc + tp->ftt_size>	    5
14187c478bd9Sstevel@tonic-gate 		 * b:	<original instrction>		<= 15
14197c478bd9Sstevel@tonic-gate 		 *	int	T_DTRACE_RET		    2
14207c478bd9Sstevel@tonic-gate 		 *					-----
14217c478bd9Sstevel@tonic-gate 		 *					<= 37
14227c478bd9Sstevel@tonic-gate 		 *
14237c478bd9Sstevel@tonic-gate 		 *	64-bit mode			bytes
14247c478bd9Sstevel@tonic-gate 		 *	------------------------	-----
14257c478bd9Sstevel@tonic-gate 		 * a:	<original instruction>		<= 15
14267c478bd9Sstevel@tonic-gate 		 *	jmp	0(%rip)			    6
14277c478bd9Sstevel@tonic-gate 		 *	<pc + tp->ftt_size>		    8
14287c478bd9Sstevel@tonic-gate 		 * b:	<original instruction>		<= 15
14297c478bd9Sstevel@tonic-gate 		 * 	int	T_DTRACE_RET		    2
14307c478bd9Sstevel@tonic-gate 		 * 					-----
14317c478bd9Sstevel@tonic-gate 		 * 					<= 46
14327c478bd9Sstevel@tonic-gate 		 *
14337c478bd9Sstevel@tonic-gate 		 * The %pc is set to a, and curthread->t_dtrace_astpc is set
14347c478bd9Sstevel@tonic-gate 		 * to b. If we encounter a signal on the way out of the
14357c478bd9Sstevel@tonic-gate 		 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
14367c478bd9Sstevel@tonic-gate 		 * so that we execute the original instruction and re-enter
14377c478bd9Sstevel@tonic-gate 		 * the kernel rather than redirecting to the next instruction.
14387c478bd9Sstevel@tonic-gate 		 *
14397c478bd9Sstevel@tonic-gate 		 * If there are return probes (so we know that we're going to
14407c478bd9Sstevel@tonic-gate 		 * need to reenter the kernel after executing the original
14417c478bd9Sstevel@tonic-gate 		 * instruction), the scratch space will just contain the
14427c478bd9Sstevel@tonic-gate 		 * original instruction followed by an interrupt -- the same
14437c478bd9Sstevel@tonic-gate 		 * data as at b.
14447c478bd9Sstevel@tonic-gate 		 *
14457c478bd9Sstevel@tonic-gate 		 * %rip-relative Addressing
14467c478bd9Sstevel@tonic-gate 		 * ------------------------
14477c478bd9Sstevel@tonic-gate 		 *
14487c478bd9Sstevel@tonic-gate 		 * There's a further complication in 64-bit mode due to %rip-
14497c478bd9Sstevel@tonic-gate 		 * relative addressing. While this is clearly a beneficial
14507c478bd9Sstevel@tonic-gate 		 * architectural decision for position independent code, it's
14517c478bd9Sstevel@tonic-gate 		 * hard not to see it as a personal attack against the pid
14527c478bd9Sstevel@tonic-gate 		 * provider since before there was a relatively small set of
14537c478bd9Sstevel@tonic-gate 		 * instructions to emulate; with %rip-relative addressing,
14547c478bd9Sstevel@tonic-gate 		 * almost every instruction can potentially depend on the
14557c478bd9Sstevel@tonic-gate 		 * address at which it's executed. Rather than emulating
14567c478bd9Sstevel@tonic-gate 		 * the broad spectrum of instructions that can now be
14577c478bd9Sstevel@tonic-gate 		 * position dependent, we emulate jumps and others as in
14587c478bd9Sstevel@tonic-gate 		 * 32-bit mode, and take a different tack for instructions
14597c478bd9Sstevel@tonic-gate 		 * using %rip-relative addressing.
14607c478bd9Sstevel@tonic-gate 		 *
14617c478bd9Sstevel@tonic-gate 		 * For every instruction that uses the ModRM byte, the
14627c478bd9Sstevel@tonic-gate 		 * in-kernel disassembler reports its location. We use the
14637c478bd9Sstevel@tonic-gate 		 * ModRM byte to identify that an instruction uses
14647c478bd9Sstevel@tonic-gate 		 * %rip-relative addressing and to see what other registers
14657c478bd9Sstevel@tonic-gate 		 * the instruction uses. To emulate those instructions,
14667c478bd9Sstevel@tonic-gate 		 * we modify the instruction to be %rax-relative rather than
14677c478bd9Sstevel@tonic-gate 		 * %rip-relative (or %rcx-relative if the instruction uses
14687c478bd9Sstevel@tonic-gate 		 * %rax; or %r8- or %r9-relative if the REX.B is present so
14697c478bd9Sstevel@tonic-gate 		 * we don't have to rewrite the REX prefix). We then load
14707c478bd9Sstevel@tonic-gate 		 * the value that %rip would have been into the scratch
14717c478bd9Sstevel@tonic-gate 		 * register and generate an instruction to reset the scratch
14727c478bd9Sstevel@tonic-gate 		 * register back to its original value. The instruction
14737c478bd9Sstevel@tonic-gate 		 * sequence looks like this:
14747c478bd9Sstevel@tonic-gate 		 *
14757c478bd9Sstevel@tonic-gate 		 *	64-mode %rip-relative		bytes
14767c478bd9Sstevel@tonic-gate 		 *	------------------------	-----
14777c478bd9Sstevel@tonic-gate 		 * a:	<modified instruction>		<= 15
14787c478bd9Sstevel@tonic-gate 		 *	movq	$<value>, %<scratch>	    6
14797c478bd9Sstevel@tonic-gate 		 *	jmp	0(%rip)			    6
14807c478bd9Sstevel@tonic-gate 		 *	<pc + tp->ftt_size>		    8
14817c478bd9Sstevel@tonic-gate 		 * b:	<modified instruction>  	<= 15
14827c478bd9Sstevel@tonic-gate 		 * 	int	T_DTRACE_RET		    2
14837c478bd9Sstevel@tonic-gate 		 * 					-----
14847c478bd9Sstevel@tonic-gate 		 *					   52
14857c478bd9Sstevel@tonic-gate 		 *
14867c478bd9Sstevel@tonic-gate 		 * We set curthread->t_dtrace_regv so that upon receiving
14877c478bd9Sstevel@tonic-gate 		 * a signal we can reset the value of the scratch register.
14887c478bd9Sstevel@tonic-gate 		 */
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_scrpc = addr;
14937c478bd9Sstevel@tonic-gate 		bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
14947c478bd9Sstevel@tonic-gate 		i += tp->ftt_size;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate #ifdef __amd64
14977c478bd9Sstevel@tonic-gate 		if (tp->ftt_ripmode != 0) {
14987c478bd9Sstevel@tonic-gate 			greg_t *reg;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 			ASSERT(p->p_model == DATAMODEL_LP64);
15017c478bd9Sstevel@tonic-gate 			ASSERT(tp->ftt_ripmode &
15027c478bd9Sstevel@tonic-gate 			    (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 			/*
15057c478bd9Sstevel@tonic-gate 			 * If this was a %rip-relative instruction, we change
15067c478bd9Sstevel@tonic-gate 			 * it to be either a %rax- or %rcx-relative
15077c478bd9Sstevel@tonic-gate 			 * instruction (depending on whether those registers
15087c478bd9Sstevel@tonic-gate 			 * are used as another operand; or %r8- or %r9-
15097c478bd9Sstevel@tonic-gate 			 * relative depending on the value of REX.B). We then
15107c478bd9Sstevel@tonic-gate 			 * set that register and generate a movq instruction
15117c478bd9Sstevel@tonic-gate 			 * to reset the value.
15127c478bd9Sstevel@tonic-gate 			 */
15137c478bd9Sstevel@tonic-gate 			if (tp->ftt_ripmode & FASTTRAP_RIP_X)
15147c478bd9Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
15157c478bd9Sstevel@tonic-gate 			else
15167c478bd9Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 			if (tp->ftt_ripmode & FASTTRAP_RIP_1)
15197c478bd9Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_MOV_EAX;
15207c478bd9Sstevel@tonic-gate 			else
15217c478bd9Sstevel@tonic-gate 				scratch[i++] = FASTTRAP_MOV_ECX;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 			switch (tp->ftt_ripmode) {
15247c478bd9Sstevel@tonic-gate 			case FASTTRAP_RIP_1:
15257c478bd9Sstevel@tonic-gate 				reg = &rp->r_rax;
15267c478bd9Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_RAX;
15277c478bd9Sstevel@tonic-gate 				break;
15287c478bd9Sstevel@tonic-gate 			case FASTTRAP_RIP_2:
15297c478bd9Sstevel@tonic-gate 				reg = &rp->r_rcx;
15307c478bd9Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_RCX;
15317c478bd9Sstevel@tonic-gate 				break;
15327c478bd9Sstevel@tonic-gate 			case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
15337c478bd9Sstevel@tonic-gate 				reg = &rp->r_r8;
15347c478bd9Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_R8;
15357c478bd9Sstevel@tonic-gate 				break;
15367c478bd9Sstevel@tonic-gate 			case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
15377c478bd9Sstevel@tonic-gate 				reg = &rp->r_r9;
15387c478bd9Sstevel@tonic-gate 				curthread->t_dtrace_reg = REG_R9;
15397c478bd9Sstevel@tonic-gate 				break;
15407c478bd9Sstevel@tonic-gate 			}
15417c478bd9Sstevel@tonic-gate 
154273427c57Sahl 			/* LINTED - alignment */
15437c478bd9Sstevel@tonic-gate 			*(uint64_t *)&scratch[i] = *reg;
15447c478bd9Sstevel@tonic-gate 			curthread->t_dtrace_regv = *reg;
15457c478bd9Sstevel@tonic-gate 			*reg = pc + tp->ftt_size;
15467c478bd9Sstevel@tonic-gate 			i += sizeof (uint64_t);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate #endif
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		/*
15517c478bd9Sstevel@tonic-gate 		 * Generate the branch instruction to what would have
15527c478bd9Sstevel@tonic-gate 		 * normally been the subsequent instruction. In 32-bit mode,
15537c478bd9Sstevel@tonic-gate 		 * this is just a relative branch; in 64-bit mode this is a
15547c478bd9Sstevel@tonic-gate 		 * %rip-relative branch that loads the 64-bit pc value
15557c478bd9Sstevel@tonic-gate 		 * immediately after the jmp instruction.
15567c478bd9Sstevel@tonic-gate 		 */
15577c478bd9Sstevel@tonic-gate #ifdef __amd64
15587c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_LP64) {
15597c478bd9Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_GROUP5_OP;
15607c478bd9Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
156173427c57Sahl 			/* LINTED - alignment */
15627c478bd9Sstevel@tonic-gate 			*(uint32_t *)&scratch[i] = 0;
15637c478bd9Sstevel@tonic-gate 			i += sizeof (uint32_t);
156473427c57Sahl 			/* LINTED - alignment */
15657c478bd9Sstevel@tonic-gate 			*(uint64_t *)&scratch[i] = pc + tp->ftt_size;
15667c478bd9Sstevel@tonic-gate 			i += sizeof (uint64_t);
15677c478bd9Sstevel@tonic-gate 		} else {
15687c478bd9Sstevel@tonic-gate #endif
15697c478bd9Sstevel@tonic-gate 			/*
15707c478bd9Sstevel@tonic-gate 			 * Set up the jmp to the next instruction; note that
15717c478bd9Sstevel@tonic-gate 			 * the size of the traced instruction cancels out.
15727c478bd9Sstevel@tonic-gate 			 */
15737c478bd9Sstevel@tonic-gate 			scratch[i++] = FASTTRAP_JMP32;
157473427c57Sahl 			/* LINTED - alignment */
15757c478bd9Sstevel@tonic-gate 			*(uint32_t *)&scratch[i] = pc - addr - 5;
15767c478bd9Sstevel@tonic-gate 			i += sizeof (uint32_t);
15777c478bd9Sstevel@tonic-gate #ifdef __amd64
15787c478bd9Sstevel@tonic-gate 		}
15797c478bd9Sstevel@tonic-gate #endif
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_astpc = addr + i;
15827c478bd9Sstevel@tonic-gate 		bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
15837c478bd9Sstevel@tonic-gate 		i += tp->ftt_size;
15847c478bd9Sstevel@tonic-gate 		scratch[i++] = FASTTRAP_INT;
15857c478bd9Sstevel@tonic-gate 		scratch[i++] = T_DTRACE_RET;
15867c478bd9Sstevel@tonic-gate 
1587*6009dbc6Sahl 		ASSERT(i <= sizeof (scratch));
1588*6009dbc6Sahl 
15897c478bd9Sstevel@tonic-gate 		if (fasttrap_copyout(scratch, (char *)addr, i)) {
15907c478bd9Sstevel@tonic-gate 			fasttrap_sigtrap(p, curthread, pc);
15917c478bd9Sstevel@tonic-gate 			new_pc = pc;
15927c478bd9Sstevel@tonic-gate 			break;
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		if (tp->ftt_retids != NULL) {
15967c478bd9Sstevel@tonic-gate 			curthread->t_dtrace_step = 1;
15977c478bd9Sstevel@tonic-gate 			curthread->t_dtrace_ret = 1;
15987c478bd9Sstevel@tonic-gate 			new_pc = curthread->t_dtrace_astpc;
15997c478bd9Sstevel@tonic-gate 		} else {
16007c478bd9Sstevel@tonic-gate 			new_pc = curthread->t_dtrace_scrpc;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_pc = pc;
16047c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_npc = pc + tp->ftt_size;
16057c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_on = 1;
16067c478bd9Sstevel@tonic-gate 		break;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	default:
16107c478bd9Sstevel@tonic-gate 		panic("fasttrap: mishandled an instruction");
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 
1613ac448965Sahl done:
16147c478bd9Sstevel@tonic-gate 	/*
16157c478bd9Sstevel@tonic-gate 	 * If there were no return probes when we first found the tracepoint,
16167c478bd9Sstevel@tonic-gate 	 * we should feel no obligation to honor any return probes that were
16177c478bd9Sstevel@tonic-gate 	 * subsequently enabled -- they'll just have to wait until the next
16187c478bd9Sstevel@tonic-gate 	 * time around.
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	if (tp->ftt_retids != NULL) {
16217c478bd9Sstevel@tonic-gate 		/*
16227c478bd9Sstevel@tonic-gate 		 * We need to wait until the results of the instruction are
16237c478bd9Sstevel@tonic-gate 		 * apparent before invoking any return probes. If this
16247c478bd9Sstevel@tonic-gate 		 * instruction was emulated we can just call
16257c478bd9Sstevel@tonic-gate 		 * fasttrap_return_common(); if it needs to be executed, we
16267c478bd9Sstevel@tonic-gate 		 * need to wait until the user thread returns to the kernel.
16277c478bd9Sstevel@tonic-gate 		 */
16287c478bd9Sstevel@tonic-gate 		if (tp->ftt_type != FASTTRAP_T_COMMON) {
16297c478bd9Sstevel@tonic-gate 			/*
16307c478bd9Sstevel@tonic-gate 			 * Set the program counter to the address of the traced
16317c478bd9Sstevel@tonic-gate 			 * instruction so that it looks right in ustack()
16327c478bd9Sstevel@tonic-gate 			 * output. We had previously set it to the end of the
16337c478bd9Sstevel@tonic-gate 			 * instruction to simplify %rip-relative addressing.
16347c478bd9Sstevel@tonic-gate 			 */
16357c478bd9Sstevel@tonic-gate 			rp->r_pc = pc;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 			fasttrap_return_common(rp, pc, pid, new_pc);
16387c478bd9Sstevel@tonic-gate 		} else {
16397c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_ret != 0);
16407c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_pc == pc);
16417c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_dtrace_scrpc != 0);
16427c478bd9Sstevel@tonic-gate 			ASSERT(new_pc == curthread->t_dtrace_astpc);
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	rp->r_pc = new_pc;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	return (0);
16497c478bd9Sstevel@tonic-gate }
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate int
16527c478bd9Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
16537c478bd9Sstevel@tonic-gate {
16547c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
16557c478bd9Sstevel@tonic-gate 	uintptr_t pc = curthread->t_dtrace_pc;
16567c478bd9Sstevel@tonic-gate 	uintptr_t npc = curthread->t_dtrace_npc;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_pc = 0;
16597c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_npc = 0;
16607c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_scrpc = 0;
16617c478bd9Sstevel@tonic-gate 	curthread->t_dtrace_astpc = 0;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	/*
16647c478bd9Sstevel@tonic-gate 	 * Treat a child created by a call to vfork(2) as if it were its
16657c478bd9Sstevel@tonic-gate 	 * parent. We know that there's only one thread of control in such a
16667c478bd9Sstevel@tonic-gate 	 * process: this one.
16677c478bd9Sstevel@tonic-gate 	 */
16687c478bd9Sstevel@tonic-gate 	while (p->p_flag & SVFORK) {
16697c478bd9Sstevel@tonic-gate 		p = p->p_parent;
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * We set rp->r_pc to the address of the traced instruction so
16747c478bd9Sstevel@tonic-gate 	 * that it appears to dtrace_probe() that we're on the original
16757c478bd9Sstevel@tonic-gate 	 * instruction, and so that the user can't easily detect our
16767c478bd9Sstevel@tonic-gate 	 * complex web of lies. dtrace_return_probe() (our caller)
16777c478bd9Sstevel@tonic-gate 	 * will correctly set %pc after we return.
16787c478bd9Sstevel@tonic-gate 	 */
16797c478bd9Sstevel@tonic-gate 	rp->r_pc = pc;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	fasttrap_return_common(rp, pc, p->p_pid, npc);
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	return (0);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16877c478bd9Sstevel@tonic-gate uint64_t
1688f498645aSahl fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1689f498645aSahl     int aframes)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 1, argno));
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16957c478bd9Sstevel@tonic-gate uint64_t
16967c478bd9Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
16977c478bd9Sstevel@tonic-gate     int aframes)
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate 	return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 0, argno));
17007c478bd9Sstevel@tonic-gate }
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate static ulong_t
17037c478bd9Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
17047c478bd9Sstevel@tonic-gate {
17057c478bd9Sstevel@tonic-gate #ifdef __amd64
17067c478bd9Sstevel@tonic-gate 	switch (reg) {
17077c478bd9Sstevel@tonic-gate 	case REG_R15:		return (rp->r_r15);
17087c478bd9Sstevel@tonic-gate 	case REG_R14:		return (rp->r_r14);
17097c478bd9Sstevel@tonic-gate 	case REG_R13:		return (rp->r_r13);
17107c478bd9Sstevel@tonic-gate 	case REG_R12:		return (rp->r_r12);
17117c478bd9Sstevel@tonic-gate 	case REG_R11:		return (rp->r_r11);
17127c478bd9Sstevel@tonic-gate 	case REG_R10:		return (rp->r_r10);
17137c478bd9Sstevel@tonic-gate 	case REG_R9:		return (rp->r_r9);
17147c478bd9Sstevel@tonic-gate 	case REG_R8:		return (rp->r_r8);
17157c478bd9Sstevel@tonic-gate 	case REG_RDI:		return (rp->r_rdi);
17167c478bd9Sstevel@tonic-gate 	case REG_RSI:		return (rp->r_rsi);
17177c478bd9Sstevel@tonic-gate 	case REG_RBP:		return (rp->r_rbp);
17187c478bd9Sstevel@tonic-gate 	case REG_RBX:		return (rp->r_rbx);
17197c478bd9Sstevel@tonic-gate 	case REG_RDX:		return (rp->r_rdx);
17207c478bd9Sstevel@tonic-gate 	case REG_RCX:		return (rp->r_rcx);
17217c478bd9Sstevel@tonic-gate 	case REG_RAX:		return (rp->r_rax);
17227c478bd9Sstevel@tonic-gate 	case REG_TRAPNO:	return (rp->r_trapno);
17237c478bd9Sstevel@tonic-gate 	case REG_ERR:		return (rp->r_err);
17247c478bd9Sstevel@tonic-gate 	case REG_RIP:		return (rp->r_rip);
17257c478bd9Sstevel@tonic-gate 	case REG_CS:		return (rp->r_cs);
17267c478bd9Sstevel@tonic-gate 	case REG_RFL:		return (rp->r_rfl);
17277c478bd9Sstevel@tonic-gate 	case REG_RSP:		return (rp->r_rsp);
17287c478bd9Sstevel@tonic-gate 	case REG_SS:		return (rp->r_ss);
17297c478bd9Sstevel@tonic-gate 	case REG_FS:		return (rp->r_fs);
17307c478bd9Sstevel@tonic-gate 	case REG_GS:		return (rp->r_gs);
17317c478bd9Sstevel@tonic-gate 	case REG_DS:		return (rp->r_ds);
17327c478bd9Sstevel@tonic-gate 	case REG_ES:		return (rp->r_es);
1733ae115bc7Smrj 	case REG_FSBASE:	return (rdmsr(MSR_AMD_FSBASE));
1734ae115bc7Smrj 	case REG_GSBASE:	return (rdmsr(MSR_AMD_GSBASE));
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	panic("dtrace: illegal register constant");
17387c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
17397c478bd9Sstevel@tonic-gate #else
17407c478bd9Sstevel@tonic-gate 	if (reg >= _NGREG)
17417c478bd9Sstevel@tonic-gate 		panic("dtrace: illegal register constant");
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	return (((greg_t *)&rp->r_gs)[reg]);
17447c478bd9Sstevel@tonic-gate #endif
17457c478bd9Sstevel@tonic-gate }
1746