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