10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51710Sahl * Common Development and Distribution License (the "License").
61710Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211710Sahl
220Sstevel@tonic-gate /*
23*6390Sahl * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
300Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
310Sstevel@tonic-gate #include <sys/dtrace.h>
320Sstevel@tonic-gate #include <sys/dtrace_impl.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/regset.h>
350Sstevel@tonic-gate #include <sys/privregs.h>
360Sstevel@tonic-gate #include <sys/segments.h>
373446Smrj #include <sys/x86_archext.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/trap.h>
402712Snn35248 #include <sys/archsystm.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * Lossless User-Land Tracing on x86
440Sstevel@tonic-gate * ---------------------------------
450Sstevel@tonic-gate *
461710Sahl * The execution of most instructions is not dependent on the address; for
471710Sahl * these instructions it is sufficient to copy them into the user process's
481710Sahl * address space and execute them. To effectively single-step an instruction
491710Sahl * in user-land, we copy out the following sequence of instructions to scratch
500Sstevel@tonic-gate * space in the user thread's ulwp_t structure.
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * We then set the program counter (%eip or %rip) to point to this scratch
530Sstevel@tonic-gate * space. Once execution resumes, the original instruction is executed and
540Sstevel@tonic-gate * then control flow is redirected to what was originally the subsequent
550Sstevel@tonic-gate * instruction. If the kernel attemps to deliver a signal while single-
560Sstevel@tonic-gate * stepping, the signal is deferred and the program counter is moved into the
570Sstevel@tonic-gate * second sequence of instructions. The second sequence ends in a trap into
580Sstevel@tonic-gate * the kernel where the deferred signal is then properly handled and delivered.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * For instructions whose execute is position dependent, we perform simple
610Sstevel@tonic-gate * emulation. These instructions are limited to control transfer
620Sstevel@tonic-gate * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
630Sstevel@tonic-gate * of %rip-relative addressing that means that almost any instruction can be
640Sstevel@tonic-gate * position dependent. For all the details on how we emulate generic
650Sstevel@tonic-gate * instructions included %rip-relative instructions, see the code in
660Sstevel@tonic-gate * fasttrap_pid_probe() below where we handle instructions of type
670Sstevel@tonic-gate * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
680Sstevel@tonic-gate */
690Sstevel@tonic-gate
700Sstevel@tonic-gate #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
710Sstevel@tonic-gate #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
720Sstevel@tonic-gate #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
730Sstevel@tonic-gate #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
740Sstevel@tonic-gate
750Sstevel@tonic-gate #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
760Sstevel@tonic-gate #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
770Sstevel@tonic-gate #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
780Sstevel@tonic-gate
790Sstevel@tonic-gate #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
800Sstevel@tonic-gate #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
810Sstevel@tonic-gate #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
820Sstevel@tonic-gate #define FASTTRAP_REX_B(rex) ((rex) & 1)
830Sstevel@tonic-gate #define FASTTRAP_REX(w, r, x, b) \
840Sstevel@tonic-gate (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * Single-byte op-codes.
880Sstevel@tonic-gate */
890Sstevel@tonic-gate #define FASTTRAP_PUSHL_EBP 0x55
900Sstevel@tonic-gate
910Sstevel@tonic-gate #define FASTTRAP_JO 0x70
920Sstevel@tonic-gate #define FASTTRAP_JNO 0x71
930Sstevel@tonic-gate #define FASTTRAP_JB 0x72
940Sstevel@tonic-gate #define FASTTRAP_JAE 0x73
950Sstevel@tonic-gate #define FASTTRAP_JE 0x74
960Sstevel@tonic-gate #define FASTTRAP_JNE 0x75
970Sstevel@tonic-gate #define FASTTRAP_JBE 0x76
980Sstevel@tonic-gate #define FASTTRAP_JA 0x77
990Sstevel@tonic-gate #define FASTTRAP_JS 0x78
1000Sstevel@tonic-gate #define FASTTRAP_JNS 0x79
1010Sstevel@tonic-gate #define FASTTRAP_JP 0x7a
1020Sstevel@tonic-gate #define FASTTRAP_JNP 0x7b
1030Sstevel@tonic-gate #define FASTTRAP_JL 0x7c
1040Sstevel@tonic-gate #define FASTTRAP_JGE 0x7d
1050Sstevel@tonic-gate #define FASTTRAP_JLE 0x7e
1060Sstevel@tonic-gate #define FASTTRAP_JG 0x7f
1070Sstevel@tonic-gate
1082769Sahl #define FASTTRAP_NOP 0x90
1092769Sahl
1100Sstevel@tonic-gate #define FASTTRAP_MOV_EAX 0xb8
1110Sstevel@tonic-gate #define FASTTRAP_MOV_ECX 0xb9
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate #define FASTTRAP_RET16 0xc2
1140Sstevel@tonic-gate #define FASTTRAP_RET 0xc3
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #define FASTTRAP_LOOPNZ 0xe0
1170Sstevel@tonic-gate #define FASTTRAP_LOOPZ 0xe1
1180Sstevel@tonic-gate #define FASTTRAP_LOOP 0xe2
1190Sstevel@tonic-gate #define FASTTRAP_JCXZ 0xe3
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #define FASTTRAP_CALL 0xe8
1220Sstevel@tonic-gate #define FASTTRAP_JMP32 0xe9
1230Sstevel@tonic-gate #define FASTTRAP_JMP8 0xeb
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define FASTTRAP_INT3 0xcc
1260Sstevel@tonic-gate #define FASTTRAP_INT 0xcd
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate #define FASTTRAP_2_BYTE_OP 0x0f
1290Sstevel@tonic-gate #define FASTTRAP_GROUP5_OP 0xff
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * Two-byte op-codes (second byte only).
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate #define FASTTRAP_0F_JO 0x80
1350Sstevel@tonic-gate #define FASTTRAP_0F_JNO 0x81
1360Sstevel@tonic-gate #define FASTTRAP_0F_JB 0x82
1370Sstevel@tonic-gate #define FASTTRAP_0F_JAE 0x83
1380Sstevel@tonic-gate #define FASTTRAP_0F_JE 0x84
1390Sstevel@tonic-gate #define FASTTRAP_0F_JNE 0x85
1400Sstevel@tonic-gate #define FASTTRAP_0F_JBE 0x86
1410Sstevel@tonic-gate #define FASTTRAP_0F_JA 0x87
1420Sstevel@tonic-gate #define FASTTRAP_0F_JS 0x88
1430Sstevel@tonic-gate #define FASTTRAP_0F_JNS 0x89
1440Sstevel@tonic-gate #define FASTTRAP_0F_JP 0x8a
1450Sstevel@tonic-gate #define FASTTRAP_0F_JNP 0x8b
1460Sstevel@tonic-gate #define FASTTRAP_0F_JL 0x8c
1470Sstevel@tonic-gate #define FASTTRAP_0F_JGE 0x8d
1480Sstevel@tonic-gate #define FASTTRAP_0F_JLE 0x8e
1490Sstevel@tonic-gate #define FASTTRAP_0F_JG 0x8f
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #define FASTTRAP_EFLAGS_OF 0x800
1520Sstevel@tonic-gate #define FASTTRAP_EFLAGS_DF 0x400
1530Sstevel@tonic-gate #define FASTTRAP_EFLAGS_SF 0x080
1540Sstevel@tonic-gate #define FASTTRAP_EFLAGS_ZF 0x040
1550Sstevel@tonic-gate #define FASTTRAP_EFLAGS_AF 0x010
1560Sstevel@tonic-gate #define FASTTRAP_EFLAGS_PF 0x004
1570Sstevel@tonic-gate #define FASTTRAP_EFLAGS_CF 0x001
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * Instruction prefixes.
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate #define FASTTRAP_PREFIX_OPERAND 0x66
1630Sstevel@tonic-gate #define FASTTRAP_PREFIX_ADDRESS 0x67
1640Sstevel@tonic-gate #define FASTTRAP_PREFIX_CS 0x2E
1650Sstevel@tonic-gate #define FASTTRAP_PREFIX_DS 0x3E
1660Sstevel@tonic-gate #define FASTTRAP_PREFIX_ES 0x26
1670Sstevel@tonic-gate #define FASTTRAP_PREFIX_FS 0x64
1680Sstevel@tonic-gate #define FASTTRAP_PREFIX_GS 0x65
1690Sstevel@tonic-gate #define FASTTRAP_PREFIX_SS 0x36
1700Sstevel@tonic-gate #define FASTTRAP_PREFIX_LOCK 0xF0
1710Sstevel@tonic-gate #define FASTTRAP_PREFIX_REP 0xF3
1720Sstevel@tonic-gate #define FASTTRAP_PREFIX_REPNE 0xF2
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate #define FASTTRAP_NOREG 0xff
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Map between instruction register encodings and the kernel constants which
1780Sstevel@tonic-gate * correspond to indicies into struct regs.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate #ifdef __amd64
1810Sstevel@tonic-gate static const uint8_t regmap[16] = {
1820Sstevel@tonic-gate REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
1830Sstevel@tonic-gate REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate #else
1860Sstevel@tonic-gate static const uint8_t regmap[8] = {
1870Sstevel@tonic-gate EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
1880Sstevel@tonic-gate };
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate static uint64_t
fasttrap_anarg(struct regs * rp,int function_entry,int argno)1940Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int function_entry, int argno)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate uint64_t value;
1970Sstevel@tonic-gate int shift = function_entry ? 1 : 0;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate #ifdef __amd64
2000Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_LP64) {
2010Sstevel@tonic-gate uintptr_t *stack;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * In 64-bit mode, the first six arguments are stored in
2050Sstevel@tonic-gate * registers.
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate if (argno < 6)
2080Sstevel@tonic-gate return ((&rp->r_rdi)[argno]);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate stack = (uintptr_t *)rp->r_sp;
2110Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2120Sstevel@tonic-gate value = dtrace_fulword(&stack[argno - 6 + shift]);
2130Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2140Sstevel@tonic-gate } else {
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate uint32_t *stack = (uint32_t *)rp->r_sp;
2170Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2180Sstevel@tonic-gate value = dtrace_fuword32(&stack[argno + shift]);
2190Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2200Sstevel@tonic-gate #ifdef __amd64
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate #endif
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate return (value);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /*ARGSUSED*/
2280Sstevel@tonic-gate int
fasttrap_tracepoint_init(proc_t * p,fasttrap_tracepoint_t * tp,uintptr_t pc,fasttrap_probe_type_t type)2291710Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
2301710Sahl fasttrap_probe_type_t type)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
2330Sstevel@tonic-gate size_t len = FASTTRAP_MAX_INSTR_SIZE;
2340Sstevel@tonic-gate size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
2350Sstevel@tonic-gate uint_t start = 0;
2362769Sahl int rmindex, size;
2372712Snn35248 uint8_t seg, rex = 0;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate * Read the instruction at the given address out of the process's
2410Sstevel@tonic-gate * address space. We don't have to worry about a debugger
2420Sstevel@tonic-gate * changing this instruction before we overwrite it with our trap
2430Sstevel@tonic-gate * instruction since P_PR_LOCK is set. Since instructions can span
2440Sstevel@tonic-gate * pages, we potentially read the instruction in two parts. If the
2450Sstevel@tonic-gate * second part fails, we just zero out that part of the instruction.
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate if (uread(p, &instr[0], first, pc) != 0)
2480Sstevel@tonic-gate return (-1);
2490Sstevel@tonic-gate if (len > first &&
2500Sstevel@tonic-gate uread(p, &instr[first], len - first, pc + first) != 0) {
2510Sstevel@tonic-gate bzero(&instr[first], len - first);
2520Sstevel@tonic-gate len = first;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * If the disassembly fails, then we have a malformed instruction.
2570Sstevel@tonic-gate */
2582769Sahl if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0)
2590Sstevel@tonic-gate return (-1);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * Make sure the disassembler isn't completely broken.
2630Sstevel@tonic-gate */
2642769Sahl ASSERT(-1 <= rmindex && rmindex < size);
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * If the computed size is greater than the number of bytes read,
2680Sstevel@tonic-gate * then it was a malformed instruction possibly because it fell on a
2690Sstevel@tonic-gate * page boundary and the subsequent page was missing or because of
2700Sstevel@tonic-gate * some malicious user.
2710Sstevel@tonic-gate */
2722769Sahl if (size > len)
2730Sstevel@tonic-gate return (-1);
2740Sstevel@tonic-gate
2752769Sahl tp->ftt_size = (uint8_t)size;
2762712Snn35248 tp->ftt_segment = FASTTRAP_SEG_NONE;
2772712Snn35248
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate * Find the start of the instruction's opcode by processing any
2800Sstevel@tonic-gate * legacy prefixes.
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate for (;;) {
2832712Snn35248 seg = 0;
2840Sstevel@tonic-gate switch (instr[start]) {
2852712Snn35248 case FASTTRAP_PREFIX_SS:
2862712Snn35248 seg++;
2872712Snn35248 /*FALLTHRU*/
2882712Snn35248 case FASTTRAP_PREFIX_GS:
2892712Snn35248 seg++;
2902712Snn35248 /*FALLTHRU*/
2912712Snn35248 case FASTTRAP_PREFIX_FS:
2922712Snn35248 seg++;
2932712Snn35248 /*FALLTHRU*/
2942712Snn35248 case FASTTRAP_PREFIX_ES:
2952712Snn35248 seg++;
2962712Snn35248 /*FALLTHRU*/
2972712Snn35248 case FASTTRAP_PREFIX_DS:
2982712Snn35248 seg++;
2992712Snn35248 /*FALLTHRU*/
3002712Snn35248 case FASTTRAP_PREFIX_CS:
3012712Snn35248 seg++;
3022712Snn35248 /*FALLTHRU*/
3030Sstevel@tonic-gate case FASTTRAP_PREFIX_OPERAND:
3040Sstevel@tonic-gate case FASTTRAP_PREFIX_ADDRESS:
3050Sstevel@tonic-gate case FASTTRAP_PREFIX_LOCK:
3060Sstevel@tonic-gate case FASTTRAP_PREFIX_REP:
3070Sstevel@tonic-gate case FASTTRAP_PREFIX_REPNE:
3082712Snn35248 if (seg != 0) {
3092712Snn35248 /*
3102712Snn35248 * It's illegal for an instruction to specify
3112712Snn35248 * two segment prefixes -- give up on this
3122712Snn35248 * illegal instruction.
3132712Snn35248 */
3142712Snn35248 if (tp->ftt_segment != FASTTRAP_SEG_NONE)
3152712Snn35248 return (-1);
3162712Snn35248
3172712Snn35248 tp->ftt_segment = seg;
3182712Snn35248 }
3190Sstevel@tonic-gate start++;
3200Sstevel@tonic-gate continue;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate break;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate #ifdef __amd64
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * Identify the REX prefix on 64-bit processes.
3280Sstevel@tonic-gate */
3290Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
3300Sstevel@tonic-gate rex = instr[start++];
3310Sstevel@tonic-gate #endif
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * Now that we're pretty sure that the instruction is okay, copy the
3350Sstevel@tonic-gate * valid part to the tracepoint.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_COMMON;
3400Sstevel@tonic-gate if (instr[start] == FASTTRAP_2_BYTE_OP) {
3410Sstevel@tonic-gate switch (instr[start + 1]) {
3420Sstevel@tonic-gate case FASTTRAP_0F_JO:
3430Sstevel@tonic-gate case FASTTRAP_0F_JNO:
3440Sstevel@tonic-gate case FASTTRAP_0F_JB:
3450Sstevel@tonic-gate case FASTTRAP_0F_JAE:
3460Sstevel@tonic-gate case FASTTRAP_0F_JE:
3470Sstevel@tonic-gate case FASTTRAP_0F_JNE:
3480Sstevel@tonic-gate case FASTTRAP_0F_JBE:
3490Sstevel@tonic-gate case FASTTRAP_0F_JA:
3500Sstevel@tonic-gate case FASTTRAP_0F_JS:
3510Sstevel@tonic-gate case FASTTRAP_0F_JNS:
3520Sstevel@tonic-gate case FASTTRAP_0F_JP:
3530Sstevel@tonic-gate case FASTTRAP_0F_JNP:
3540Sstevel@tonic-gate case FASTTRAP_0F_JL:
3550Sstevel@tonic-gate case FASTTRAP_0F_JGE:
3560Sstevel@tonic-gate case FASTTRAP_0F_JLE:
3570Sstevel@tonic-gate case FASTTRAP_0F_JG:
3580Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCC;
3590Sstevel@tonic-gate tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
3600Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
3613944Sahl /* LINTED - alignment */
3620Sstevel@tonic-gate *(int32_t *)&instr[start + 2];
3630Sstevel@tonic-gate break;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate } else if (instr[start] == FASTTRAP_GROUP5_OP) {
3660Sstevel@tonic-gate uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
3670Sstevel@tonic-gate uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
3680Sstevel@tonic-gate uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (reg == 2 || reg == 4) {
3710Sstevel@tonic-gate uint_t i, sz;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (reg == 2)
3740Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL;
3750Sstevel@tonic-gate else
3760Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (mod == 3)
3790Sstevel@tonic-gate tp->ftt_code = 2;
3800Sstevel@tonic-gate else
3810Sstevel@tonic-gate tp->ftt_code = 1;
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate * See AMD x86-64 Architecture Programmer's Manual
3870Sstevel@tonic-gate * Volume 3, Section 1.2.7, Table 1-12, and
3880Sstevel@tonic-gate * Appendix A.3.1, Table A-15.
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate if (mod != 3 && rm == 4) {
3910Sstevel@tonic-gate uint8_t sib = instr[start + 2];
3920Sstevel@tonic-gate uint_t index = FASTTRAP_SIB_INDEX(sib);
3930Sstevel@tonic-gate uint_t base = FASTTRAP_SIB_BASE(sib);
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate tp->ftt_index = (index == 4) ?
3980Sstevel@tonic-gate FASTTRAP_NOREG :
3990Sstevel@tonic-gate regmap[index | (FASTTRAP_REX_X(rex) << 3)];
4000Sstevel@tonic-gate tp->ftt_base = (mod == 0 && base == 5) ?
4010Sstevel@tonic-gate FASTTRAP_NOREG :
4020Sstevel@tonic-gate regmap[base | (FASTTRAP_REX_B(rex) << 3)];
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate i = 3;
4050Sstevel@tonic-gate sz = mod == 1 ? 1 : 4;
4060Sstevel@tonic-gate } else {
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate * In 64-bit mode, mod == 0 and r/m == 5
4090Sstevel@tonic-gate * denotes %rip-relative addressing; in 32-bit
4100Sstevel@tonic-gate * mode, the base register isn't used. In both
4110Sstevel@tonic-gate * modes, there is a 32-bit operand.
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate if (mod == 0 && rm == 5) {
4140Sstevel@tonic-gate #ifdef __amd64
4150Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64)
4160Sstevel@tonic-gate tp->ftt_base = REG_RIP;
4170Sstevel@tonic-gate else
4180Sstevel@tonic-gate #endif
4190Sstevel@tonic-gate tp->ftt_base = FASTTRAP_NOREG;
4200Sstevel@tonic-gate sz = 4;
4210Sstevel@tonic-gate } else {
4220Sstevel@tonic-gate uint8_t base = rm |
4230Sstevel@tonic-gate (FASTTRAP_REX_B(rex) << 3);
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate tp->ftt_base = regmap[base];
4260Sstevel@tonic-gate sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate tp->ftt_index = FASTTRAP_NOREG;
4290Sstevel@tonic-gate i = 2;
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4323944Sahl if (sz == 1) {
4330Sstevel@tonic-gate tp->ftt_dest = *(int8_t *)&instr[start + i];
4343944Sahl } else if (sz == 4) {
4353944Sahl /* LINTED - alignment */
4360Sstevel@tonic-gate tp->ftt_dest = *(int32_t *)&instr[start + i];
4373944Sahl } else {
4380Sstevel@tonic-gate tp->ftt_dest = 0;
4393944Sahl }
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate } else {
4420Sstevel@tonic-gate switch (instr[start]) {
4430Sstevel@tonic-gate case FASTTRAP_RET:
4440Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RET;
4450Sstevel@tonic-gate break;
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate case FASTTRAP_RET16:
4480Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RET16;
4493944Sahl /* LINTED - alignment */
4500Sstevel@tonic-gate tp->ftt_dest = *(uint16_t *)&instr[start + 1];
4510Sstevel@tonic-gate break;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate case FASTTRAP_JO:
4540Sstevel@tonic-gate case FASTTRAP_JNO:
4550Sstevel@tonic-gate case FASTTRAP_JB:
4560Sstevel@tonic-gate case FASTTRAP_JAE:
4570Sstevel@tonic-gate case FASTTRAP_JE:
4580Sstevel@tonic-gate case FASTTRAP_JNE:
4590Sstevel@tonic-gate case FASTTRAP_JBE:
4600Sstevel@tonic-gate case FASTTRAP_JA:
4610Sstevel@tonic-gate case FASTTRAP_JS:
4620Sstevel@tonic-gate case FASTTRAP_JNS:
4630Sstevel@tonic-gate case FASTTRAP_JP:
4640Sstevel@tonic-gate case FASTTRAP_JNP:
4650Sstevel@tonic-gate case FASTTRAP_JL:
4660Sstevel@tonic-gate case FASTTRAP_JGE:
4670Sstevel@tonic-gate case FASTTRAP_JLE:
4680Sstevel@tonic-gate case FASTTRAP_JG:
4690Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCC;
4700Sstevel@tonic-gate tp->ftt_code = instr[start];
4710Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4720Sstevel@tonic-gate (int8_t)instr[start + 1];
4730Sstevel@tonic-gate break;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate case FASTTRAP_LOOPNZ:
4760Sstevel@tonic-gate case FASTTRAP_LOOPZ:
4770Sstevel@tonic-gate case FASTTRAP_LOOP:
4780Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_LOOP;
4790Sstevel@tonic-gate tp->ftt_code = instr[start];
4800Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4810Sstevel@tonic-gate (int8_t)instr[start + 1];
4820Sstevel@tonic-gate break;
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate case FASTTRAP_JCXZ:
4850Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCXZ;
4860Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4870Sstevel@tonic-gate (int8_t)instr[start + 1];
4880Sstevel@tonic-gate break;
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate case FASTTRAP_CALL:
4910Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL;
4920Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4933944Sahl /* LINTED - alignment */
4940Sstevel@tonic-gate *(int32_t *)&instr[start + 1];
4950Sstevel@tonic-gate tp->ftt_code = 0;
4960Sstevel@tonic-gate break;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate case FASTTRAP_JMP32:
4990Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
5000Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
5013944Sahl /* LINTED - alignment */
5020Sstevel@tonic-gate *(int32_t *)&instr[start + 1];
5030Sstevel@tonic-gate break;
5040Sstevel@tonic-gate case FASTTRAP_JMP8:
5050Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
5060Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
5070Sstevel@tonic-gate (int8_t)instr[start + 1];
5080Sstevel@tonic-gate break;
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate case FASTTRAP_PUSHL_EBP:
5110Sstevel@tonic-gate if (start == 0)
5120Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
5130Sstevel@tonic-gate break;
5140Sstevel@tonic-gate
5152769Sahl case FASTTRAP_NOP:
5162769Sahl #ifdef __amd64
5172769Sahl ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
5182769Sahl
5192769Sahl /*
5202769Sahl * On amd64 we have to be careful not to confuse a nop
5212769Sahl * (actually xchgl %eax, %eax) with an instruction using
5222769Sahl * the same opcode, but that does something different
5232769Sahl * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
5242769Sahl */
5252769Sahl if (FASTTRAP_REX_B(rex) == 0)
5262769Sahl #endif
5272769Sahl tp->ftt_type = FASTTRAP_T_NOP;
5282769Sahl break;
5292769Sahl
5300Sstevel@tonic-gate case FASTTRAP_INT3:
5310Sstevel@tonic-gate /*
5320Sstevel@tonic-gate * The pid provider shares the int3 trap with debugger
5330Sstevel@tonic-gate * breakpoints so we can't instrument them.
5340Sstevel@tonic-gate */
5350Sstevel@tonic-gate ASSERT(instr[start] == FASTTRAP_INSTR);
5360Sstevel@tonic-gate return (-1);
5372712Snn35248
5382712Snn35248 case FASTTRAP_INT:
5392712Snn35248 /*
5402712Snn35248 * Interrupts seem like they could be traced with
5412712Snn35248 * no negative implications, but it's possible that
5422712Snn35248 * a thread could be redirected by the trap handling
5432712Snn35248 * code which would eventually return to the
5442712Snn35248 * instruction after the interrupt. If the interrupt
5452712Snn35248 * were in our scratch space, the subsequent
5462712Snn35248 * instruction might be overwritten before we return.
5472712Snn35248 * Accordingly we refuse to instrument any interrupt.
5482712Snn35248 */
5492712Snn35248 return (-1);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate #ifdef __amd64
5540Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate * If the process is 64-bit and the instruction type is still
5570Sstevel@tonic-gate * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
5580Sstevel@tonic-gate * execute it -- we need to watch for %rip-relative
5590Sstevel@tonic-gate * addressing mode. See the portion of fasttrap_pid_probe()
5600Sstevel@tonic-gate * below where we handle tracepoints with type
5610Sstevel@tonic-gate * FASTTRAP_T_COMMON for how we emulate instructions that
5620Sstevel@tonic-gate * employ %rip-relative addressing.
5630Sstevel@tonic-gate */
5640Sstevel@tonic-gate if (rmindex != -1) {
5650Sstevel@tonic-gate uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
5660Sstevel@tonic-gate uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
5670Sstevel@tonic-gate uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate ASSERT(rmindex > start);
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate if (mod == 0 && rm == 5) {
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate * We need to be sure to avoid other
5740Sstevel@tonic-gate * registers used by this instruction. While
5750Sstevel@tonic-gate * the reg field may determine the op code
5760Sstevel@tonic-gate * rather than denoting a register, assuming
5770Sstevel@tonic-gate * that it denotes a register is always safe.
5780Sstevel@tonic-gate * We leave the REX field intact and use
5790Sstevel@tonic-gate * whatever value's there for simplicity.
5800Sstevel@tonic-gate */
5810Sstevel@tonic-gate if (reg != 0) {
5820Sstevel@tonic-gate tp->ftt_ripmode = FASTTRAP_RIP_1 |
5830Sstevel@tonic-gate (FASTTRAP_RIP_X *
5840Sstevel@tonic-gate FASTTRAP_REX_B(rex));
5850Sstevel@tonic-gate rm = 0;
5860Sstevel@tonic-gate } else {
5870Sstevel@tonic-gate tp->ftt_ripmode = FASTTRAP_RIP_2 |
5880Sstevel@tonic-gate (FASTTRAP_RIP_X *
5890Sstevel@tonic-gate FASTTRAP_REX_B(rex));
5900Sstevel@tonic-gate rm = 1;
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate tp->ftt_modrm = tp->ftt_instr[rmindex];
5940Sstevel@tonic-gate tp->ftt_instr[rmindex] =
5950Sstevel@tonic-gate FASTTRAP_MODRM(2, reg, rm);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate #endif
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate return (0);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate int
fasttrap_tracepoint_install(proc_t * p,fasttrap_tracepoint_t * tp)6050Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate fasttrap_instr_t instr = FASTTRAP_INSTR;
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
6100Sstevel@tonic-gate return (-1);
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate return (0);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate int
fasttrap_tracepoint_remove(proc_t * p,fasttrap_tracepoint_t * tp)6160Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate uint8_t instr;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate * Distinguish between read or write failures and a changed
6220Sstevel@tonic-gate * instruction.
6230Sstevel@tonic-gate */
6240Sstevel@tonic-gate if (uread(p, &instr, 1, tp->ftt_pc) != 0)
6250Sstevel@tonic-gate return (0);
6260Sstevel@tonic-gate if (instr != FASTTRAP_INSTR)
6270Sstevel@tonic-gate return (0);
6280Sstevel@tonic-gate if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
6290Sstevel@tonic-gate return (-1);
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate return (0);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate
6343944Sahl #ifdef __amd64
6350Sstevel@tonic-gate static uintptr_t
fasttrap_fulword_noerr(const void * uaddr)6360Sstevel@tonic-gate fasttrap_fulword_noerr(const void *uaddr)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate uintptr_t ret;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate if (fasttrap_fulword(uaddr, &ret) == 0)
6410Sstevel@tonic-gate return (ret);
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate return (0);
6440Sstevel@tonic-gate }
6453944Sahl #endif
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate static uint32_t
fasttrap_fuword32_noerr(const void * uaddr)6480Sstevel@tonic-gate fasttrap_fuword32_noerr(const void *uaddr)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate uint32_t ret;
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate if (fasttrap_fuword32(uaddr, &ret) == 0)
6530Sstevel@tonic-gate return (ret);
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate return (0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate static void
fasttrap_return_common(struct regs * rp,uintptr_t pc,pid_t pid,uintptr_t new_pc)6590Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
6600Sstevel@tonic-gate uintptr_t new_pc)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
6630Sstevel@tonic-gate fasttrap_bucket_t *bucket;
6640Sstevel@tonic-gate fasttrap_id_t *id;
6650Sstevel@tonic-gate kmutex_t *pid_mtx;
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
6680Sstevel@tonic-gate mutex_enter(pid_mtx);
6690Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6720Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
6734821Sahl tp->ftt_proc->ftpc_acount != 0)
6740Sstevel@tonic-gate break;
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate * Don't sweat it if we can't find the tracepoint again; unlike
6790Sstevel@tonic-gate * when we're in fasttrap_pid_probe(), finding the tracepoint here
6800Sstevel@tonic-gate * is not essential to the correct execution of the process.
6810Sstevel@tonic-gate */
6820Sstevel@tonic-gate if (tp == NULL) {
6830Sstevel@tonic-gate mutex_exit(pid_mtx);
6840Sstevel@tonic-gate return;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate * If there's a branch that could act as a return site, we
6900Sstevel@tonic-gate * need to trace it, and check here if the program counter is
6910Sstevel@tonic-gate * external to the function.
6920Sstevel@tonic-gate */
6930Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_RET &&
6940Sstevel@tonic-gate tp->ftt_type != FASTTRAP_T_RET16 &&
6950Sstevel@tonic-gate new_pc - id->fti_probe->ftp_faddr <
6960Sstevel@tonic-gate id->fti_probe->ftp_fsize)
6970Sstevel@tonic-gate continue;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate dtrace_probe(id->fti_probe->ftp_id,
7000Sstevel@tonic-gate pc - id->fti_probe->ftp_faddr,
7010Sstevel@tonic-gate rp->r_r0, rp->r_r1, 0, 0);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate mutex_exit(pid_mtx);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate static void
fasttrap_sigsegv(proc_t * p,kthread_t * t,uintptr_t addr)7080Sstevel@tonic-gate fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate sqp->sq_info.si_signo = SIGSEGV;
7130Sstevel@tonic-gate sqp->sq_info.si_code = SEGV_MAPERR;
7140Sstevel@tonic-gate sqp->sq_info.si_addr = (caddr_t)addr;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate mutex_enter(&p->p_lock);
7170Sstevel@tonic-gate sigaddqa(p, t, sqp);
7180Sstevel@tonic-gate mutex_exit(&p->p_lock);
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate if (t != NULL)
7210Sstevel@tonic-gate aston(t);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate #ifdef __amd64
7250Sstevel@tonic-gate static void
fasttrap_usdt_args64(fasttrap_probe_t * probe,struct regs * rp,int argc,uintptr_t * argv)7260Sstevel@tonic-gate fasttrap_usdt_args64(fasttrap_probe_t *probe, struct regs *rp, int argc,
7270Sstevel@tonic-gate uintptr_t *argv)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs);
7300Sstevel@tonic-gate uintptr_t *stack = (uintptr_t *)rp->r_sp;
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate for (i = 0; i < cap; i++) {
7330Sstevel@tonic-gate x = probe->ftp_argmap[i];
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate if (x < 6)
7360Sstevel@tonic-gate argv[i] = (&rp->r_rdi)[x];
7370Sstevel@tonic-gate else
7380Sstevel@tonic-gate argv[i] = fasttrap_fulword_noerr(&stack[x]);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate for (; i < argc; i++) {
7420Sstevel@tonic-gate argv[i] = 0;
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate #endif
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate static void
fasttrap_usdt_args32(fasttrap_probe_t * probe,struct regs * rp,int argc,uint32_t * argv)7480Sstevel@tonic-gate fasttrap_usdt_args32(fasttrap_probe_t *probe, struct regs *rp, int argc,
7490Sstevel@tonic-gate uint32_t *argv)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs);
7520Sstevel@tonic-gate uint32_t *stack = (uint32_t *)rp->r_sp;
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate for (i = 0; i < cap; i++) {
7550Sstevel@tonic-gate x = probe->ftp_argmap[i];
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate argv[i] = fasttrap_fuword32_noerr(&stack[x]);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate for (; i < argc; i++) {
7610Sstevel@tonic-gate argv[i] = 0;
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7652712Snn35248 static int
fasttrap_do_seg(fasttrap_tracepoint_t * tp,struct regs * rp,uintptr_t * addr)7662712Snn35248 fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct regs *rp, uintptr_t *addr)
7672712Snn35248 {
7682712Snn35248 proc_t *p = curproc;
7692712Snn35248 user_desc_t *desc;
7702712Snn35248 uint16_t sel, ndx, type;
7712712Snn35248 uintptr_t limit;
7722712Snn35248
7732712Snn35248 switch (tp->ftt_segment) {
7742712Snn35248 case FASTTRAP_SEG_CS:
7752712Snn35248 sel = rp->r_cs;
7762712Snn35248 break;
7772712Snn35248 case FASTTRAP_SEG_DS:
7782712Snn35248 sel = rp->r_ds;
7792712Snn35248 break;
7802712Snn35248 case FASTTRAP_SEG_ES:
7812712Snn35248 sel = rp->r_es;
7822712Snn35248 break;
7832712Snn35248 case FASTTRAP_SEG_FS:
7842712Snn35248 sel = rp->r_fs;
7852712Snn35248 break;
7862712Snn35248 case FASTTRAP_SEG_GS:
7872712Snn35248 sel = rp->r_gs;
7882712Snn35248 break;
7892712Snn35248 case FASTTRAP_SEG_SS:
7902712Snn35248 sel = rp->r_ss;
7912712Snn35248 break;
7922712Snn35248 }
7932712Snn35248
7942712Snn35248 /*
7952712Snn35248 * Make sure the given segment register specifies a user priority
7962712Snn35248 * selector rather than a kernel selector.
7972712Snn35248 */
7982712Snn35248 if (!SELISUPL(sel))
7992712Snn35248 return (-1);
8002712Snn35248
8012712Snn35248 ndx = SELTOIDX(sel);
8022712Snn35248
8032712Snn35248 /*
8042712Snn35248 * Check the bounds and grab the descriptor out of the specified
8052712Snn35248 * descriptor table.
8062712Snn35248 */
8072712Snn35248 if (SELISLDT(sel)) {
8082712Snn35248 if (ndx > p->p_ldtlimit)
8092712Snn35248 return (-1);
8102712Snn35248
8112712Snn35248 desc = p->p_ldt + ndx;
8122712Snn35248
8132712Snn35248 } else {
8142712Snn35248 if (ndx >= NGDT)
8152712Snn35248 return (-1);
8162712Snn35248
8172712Snn35248 desc = cpu_get_gdt() + ndx;
8182712Snn35248 }
8192712Snn35248
8202712Snn35248 /*
8212712Snn35248 * The descriptor must have user privilege level and it must be
8222712Snn35248 * present in memory.
8232712Snn35248 */
8242712Snn35248 if (desc->usd_dpl != SEL_UPL || desc->usd_p != 1)
8252712Snn35248 return (-1);
8262712Snn35248
8272712Snn35248 type = desc->usd_type;
8282712Snn35248
8292712Snn35248 /*
8302712Snn35248 * If the S bit in the type field is not set, this descriptor can
8312712Snn35248 * only be used in system context.
8322712Snn35248 */
8332712Snn35248 if ((type & 0x10) != 0x10)
8342712Snn35248 return (-1);
8352712Snn35248
8362712Snn35248 limit = USEGD_GETLIMIT(desc) * (desc->usd_gran ? PAGESIZE : 1);
8372712Snn35248
8382712Snn35248 if (tp->ftt_segment == FASTTRAP_SEG_CS) {
8392712Snn35248 /*
8402712Snn35248 * The code/data bit and readable bit must both be set.
8412712Snn35248 */
8422712Snn35248 if ((type & 0xa) != 0xa)
8432712Snn35248 return (-1);
8442712Snn35248
8452712Snn35248 if (*addr > limit)
8462712Snn35248 return (-1);
8472712Snn35248 } else {
8482712Snn35248 /*
8492712Snn35248 * The code/data bit must be clear.
8502712Snn35248 */
8512712Snn35248 if ((type & 0x8) != 0)
8522712Snn35248 return (-1);
8532712Snn35248
8542712Snn35248 /*
8552712Snn35248 * If the expand-down bit is clear, we just check the limit as
8562712Snn35248 * it would naturally be applied. Otherwise, we need to check
8572712Snn35248 * that the address is the range [limit + 1 .. 0xffff] or
8582712Snn35248 * [limit + 1 ... 0xffffffff] depending on if the default
8592712Snn35248 * operand size bit is set.
8602712Snn35248 */
8612712Snn35248 if ((type & 0x4) == 0) {
8622712Snn35248 if (*addr > limit)
8632712Snn35248 return (-1);
8642712Snn35248 } else if (desc->usd_def32) {
8652712Snn35248 if (*addr < limit + 1 || 0xffff < *addr)
8662712Snn35248 return (-1);
8672712Snn35248 } else {
8682712Snn35248 if (*addr < limit + 1 || 0xffffffff < *addr)
8692712Snn35248 return (-1);
8702712Snn35248 }
8712712Snn35248 }
8722712Snn35248
8732712Snn35248 *addr += USEGD_GETBASE(desc);
8742712Snn35248
8752712Snn35248 return (0);
8762712Snn35248 }
8772712Snn35248
8780Sstevel@tonic-gate int
fasttrap_pid_probe(struct regs * rp)8790Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate proc_t *p = curproc;
8820Sstevel@tonic-gate uintptr_t pc = rp->r_pc - 1, new_pc = 0;
8830Sstevel@tonic-gate fasttrap_bucket_t *bucket;
8840Sstevel@tonic-gate kmutex_t *pid_mtx;
8850Sstevel@tonic-gate fasttrap_tracepoint_t *tp, tp_local;
8860Sstevel@tonic-gate pid_t pid;
8870Sstevel@tonic-gate dtrace_icookie_t cookie;
8881710Sahl uint_t is_enabled = 0;
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate * It's possible that a user (in a veritable orgy of bad planning)
8920Sstevel@tonic-gate * could redirect this thread's flow of control before it reached the
8930Sstevel@tonic-gate * return probe fasttrap. In this case we need to kill the process
8940Sstevel@tonic-gate * since it's in a unrecoverable state.
8950Sstevel@tonic-gate */
8960Sstevel@tonic-gate if (curthread->t_dtrace_step) {
8970Sstevel@tonic-gate ASSERT(curthread->t_dtrace_on);
8980Sstevel@tonic-gate fasttrap_sigtrap(p, curthread, pc);
8990Sstevel@tonic-gate return (0);
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate /*
9030Sstevel@tonic-gate * Clear all user tracing flags.
9040Sstevel@tonic-gate */
9050Sstevel@tonic-gate curthread->t_dtrace_ft = 0;
9060Sstevel@tonic-gate curthread->t_dtrace_pc = 0;
9070Sstevel@tonic-gate curthread->t_dtrace_npc = 0;
9080Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0;
9090Sstevel@tonic-gate curthread->t_dtrace_astpc = 0;
9100Sstevel@tonic-gate #ifdef __amd64
9110Sstevel@tonic-gate curthread->t_dtrace_regv = 0;
9120Sstevel@tonic-gate #endif
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its
9160Sstevel@tonic-gate * parent. We know that there's only one thread of control in such a
9170Sstevel@tonic-gate * process: this one.
9180Sstevel@tonic-gate */
9190Sstevel@tonic-gate while (p->p_flag & SVFORK) {
9200Sstevel@tonic-gate p = p->p_parent;
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate pid = p->p_pid;
9240Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
9250Sstevel@tonic-gate mutex_enter(pid_mtx);
9260Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate * Lookup the tracepoint that the process just hit.
9300Sstevel@tonic-gate */
9310Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
9320Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
9334821Sahl tp->ftt_proc->ftpc_acount != 0)
9340Sstevel@tonic-gate break;
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate * If we couldn't find a matching tracepoint, either a tracepoint has
9390Sstevel@tonic-gate * been inserted without using the pid<pid> ioctl interface (see
9400Sstevel@tonic-gate * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
9410Sstevel@tonic-gate */
9420Sstevel@tonic-gate if (tp == NULL) {
9430Sstevel@tonic-gate mutex_exit(pid_mtx);
9440Sstevel@tonic-gate return (-1);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate * Set the program counter to the address of the traced instruction
9490Sstevel@tonic-gate * so that it looks right in ustack() output.
9500Sstevel@tonic-gate */
9510Sstevel@tonic-gate rp->r_pc = pc;
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate if (tp->ftt_ids != NULL) {
9540Sstevel@tonic-gate fasttrap_id_t *id;
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate #ifdef __amd64
9570Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64) {
9580Sstevel@tonic-gate for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
9590Sstevel@tonic-gate fasttrap_probe_t *probe = id->fti_probe;
9600Sstevel@tonic-gate
9611710Sahl if (id->fti_ptype == DTFTP_ENTRY) {
9620Sstevel@tonic-gate /*
9630Sstevel@tonic-gate * We note that this was an entry
9640Sstevel@tonic-gate * probe to help ustack() find the
9650Sstevel@tonic-gate * first caller.
9660Sstevel@tonic-gate */
9670Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
9680Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
9690Sstevel@tonic-gate dtrace_probe(probe->ftp_id, rp->r_rdi,
9700Sstevel@tonic-gate rp->r_rsi, rp->r_rdx, rp->r_rcx,
9710Sstevel@tonic-gate rp->r_r8);
9720Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
9730Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
9741710Sahl } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
9751710Sahl /*
9761710Sahl * Note that in this case, we don't
9771710Sahl * call dtrace_probe() since it's only
9781710Sahl * an artificial probe meant to change
9791710Sahl * the flow of control so that it
9801710Sahl * encounters the true probe.
9811710Sahl */
9821710Sahl is_enabled = 1;
9830Sstevel@tonic-gate } else if (probe->ftp_argmap == NULL) {
9840Sstevel@tonic-gate dtrace_probe(probe->ftp_id, rp->r_rdi,
9850Sstevel@tonic-gate rp->r_rsi, rp->r_rdx, rp->r_rcx,
9860Sstevel@tonic-gate rp->r_r8);
9870Sstevel@tonic-gate } else {
9880Sstevel@tonic-gate uintptr_t t[5];
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate fasttrap_usdt_args64(probe, rp,
9910Sstevel@tonic-gate sizeof (t) / sizeof (t[0]), t);
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate dtrace_probe(probe->ftp_id, t[0], t[1],
9940Sstevel@tonic-gate t[2], t[3], t[4]);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate } else {
9980Sstevel@tonic-gate #endif
9990Sstevel@tonic-gate uintptr_t s0, s1, s2, s3, s4, s5;
10000Sstevel@tonic-gate uint32_t *stack = (uint32_t *)rp->r_sp;
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate /*
10030Sstevel@tonic-gate * In 32-bit mode, all arguments are passed on the
10040Sstevel@tonic-gate * stack. If this is a function entry probe, we need
10050Sstevel@tonic-gate * to skip the first entry on the stack as it
10060Sstevel@tonic-gate * represents the return address rather than a
10070Sstevel@tonic-gate * parameter to the function.
10080Sstevel@tonic-gate */
10090Sstevel@tonic-gate s0 = fasttrap_fuword32_noerr(&stack[0]);
10100Sstevel@tonic-gate s1 = fasttrap_fuword32_noerr(&stack[1]);
10110Sstevel@tonic-gate s2 = fasttrap_fuword32_noerr(&stack[2]);
10120Sstevel@tonic-gate s3 = fasttrap_fuword32_noerr(&stack[3]);
10130Sstevel@tonic-gate s4 = fasttrap_fuword32_noerr(&stack[4]);
10140Sstevel@tonic-gate s5 = fasttrap_fuword32_noerr(&stack[5]);
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
10170Sstevel@tonic-gate fasttrap_probe_t *probe = id->fti_probe;
10180Sstevel@tonic-gate
10191710Sahl if (id->fti_ptype == DTFTP_ENTRY) {
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate * We note that this was an entry
10220Sstevel@tonic-gate * probe to help ustack() find the
10230Sstevel@tonic-gate * first caller.
10240Sstevel@tonic-gate */
10250Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
10260Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
10270Sstevel@tonic-gate dtrace_probe(probe->ftp_id, s1, s2,
10280Sstevel@tonic-gate s3, s4, s5);
10290Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
10300Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
10311710Sahl } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
10321710Sahl /*
10331710Sahl * Note that in this case, we don't
10341710Sahl * call dtrace_probe() since it's only
10351710Sahl * an artificial probe meant to change
10361710Sahl * the flow of control so that it
10371710Sahl * encounters the true probe.
10381710Sahl */
10391710Sahl is_enabled = 1;
10400Sstevel@tonic-gate } else if (probe->ftp_argmap == NULL) {
10410Sstevel@tonic-gate dtrace_probe(probe->ftp_id, s0, s1,
10420Sstevel@tonic-gate s2, s3, s4);
10430Sstevel@tonic-gate } else {
10440Sstevel@tonic-gate uint32_t t[5];
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate fasttrap_usdt_args32(probe, rp,
10470Sstevel@tonic-gate sizeof (t) / sizeof (t[0]), t);
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate dtrace_probe(probe->ftp_id, t[0], t[1],
10500Sstevel@tonic-gate t[2], t[3], t[4]);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate #ifdef __amd64
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate #endif
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate * We're about to do a bunch of work so we cache a local copy of
10600Sstevel@tonic-gate * the tracepoint to emulate the instruction, and then find the
10610Sstevel@tonic-gate * tracepoint again later if we need to light up any return probes.
10620Sstevel@tonic-gate */
10630Sstevel@tonic-gate tp_local = *tp;
10640Sstevel@tonic-gate mutex_exit(pid_mtx);
10650Sstevel@tonic-gate tp = &tp_local;
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate /*
10680Sstevel@tonic-gate * Set the program counter to appear as though the traced instruction
10690Sstevel@tonic-gate * had completely executed. This ensures that fasttrap_getreg() will
10700Sstevel@tonic-gate * report the expected value for REG_RIP.
10710Sstevel@tonic-gate */
10720Sstevel@tonic-gate rp->r_pc = pc + tp->ftt_size;
10730Sstevel@tonic-gate
10741710Sahl /*
10751710Sahl * If there's an is-enabled probe connected to this tracepoint it
10761710Sahl * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
10771710Sahl * instruction that was placed there by DTrace when the binary was
10781710Sahl * linked. As this probe is, in fact, enabled, we need to stuff 1
10791710Sahl * into %eax or %rax. Accordingly, we can bypass all the instruction
10801710Sahl * emulation logic since we know the inevitable result. It's possible
10811710Sahl * that a user could construct a scenario where the 'is-enabled'
10821710Sahl * probe was on some other instruction, but that would be a rather
10831710Sahl * exotic way to shoot oneself in the foot.
10841710Sahl */
10851710Sahl if (is_enabled) {
10861710Sahl rp->r_r0 = 1;
10871710Sahl new_pc = rp->r_pc;
10881710Sahl goto done;
10891710Sahl }
10901710Sahl
10911710Sahl /*
10921710Sahl * We emulate certain types of instructions to ensure correctness
10931710Sahl * (in the case of position dependent instructions) or optimize
10941710Sahl * common cases. The rest we have the thread execute back in user-
10951710Sahl * land.
10961710Sahl */
10970Sstevel@tonic-gate switch (tp->ftt_type) {
10980Sstevel@tonic-gate case FASTTRAP_T_RET:
10990Sstevel@tonic-gate case FASTTRAP_T_RET16:
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate uintptr_t dst;
11020Sstevel@tonic-gate uintptr_t addr;
11030Sstevel@tonic-gate int ret;
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate * We have to emulate _every_ facet of the behavior of a ret
11070Sstevel@tonic-gate * instruction including what happens if the load from %esp
11080Sstevel@tonic-gate * fails; in that case, we send a SIGSEGV.
11090Sstevel@tonic-gate */
11100Sstevel@tonic-gate #ifdef __amd64
11110Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
11120Sstevel@tonic-gate #endif
11130Sstevel@tonic-gate ret = fasttrap_fulword((void *)rp->r_sp, &dst);
11140Sstevel@tonic-gate addr = rp->r_sp + sizeof (uintptr_t);
11150Sstevel@tonic-gate #ifdef __amd64
11160Sstevel@tonic-gate } else {
11170Sstevel@tonic-gate uint32_t dst32;
11180Sstevel@tonic-gate ret = fasttrap_fuword32((void *)rp->r_sp, &dst32);
11190Sstevel@tonic-gate dst = dst32;
11200Sstevel@tonic-gate addr = rp->r_sp + sizeof (uint32_t);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate #endif
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate if (ret == -1) {
11250Sstevel@tonic-gate fasttrap_sigsegv(p, curthread, rp->r_sp);
11260Sstevel@tonic-gate new_pc = pc;
11270Sstevel@tonic-gate break;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate if (tp->ftt_type == FASTTRAP_T_RET16)
11310Sstevel@tonic-gate addr += tp->ftt_dest;
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate rp->r_sp = addr;
11340Sstevel@tonic-gate new_pc = dst;
11350Sstevel@tonic-gate break;
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate case FASTTRAP_T_JCC:
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate uint_t taken;
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate switch (tp->ftt_code) {
11430Sstevel@tonic-gate case FASTTRAP_JO:
11440Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) != 0;
11450Sstevel@tonic-gate break;
11460Sstevel@tonic-gate case FASTTRAP_JNO:
11470Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_OF) == 0;
11480Sstevel@tonic-gate break;
11490Sstevel@tonic-gate case FASTTRAP_JB:
11500Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0;
11510Sstevel@tonic-gate break;
11520Sstevel@tonic-gate case FASTTRAP_JAE:
11530Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0;
11540Sstevel@tonic-gate break;
11550Sstevel@tonic-gate case FASTTRAP_JE:
11560Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
11570Sstevel@tonic-gate break;
11580Sstevel@tonic-gate case FASTTRAP_JNE:
11590Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
11600Sstevel@tonic-gate break;
11610Sstevel@tonic-gate case FASTTRAP_JBE:
11620Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) != 0 ||
11630Sstevel@tonic-gate (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0;
11640Sstevel@tonic-gate break;
11650Sstevel@tonic-gate case FASTTRAP_JA:
11660Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_CF) == 0 &&
11670Sstevel@tonic-gate (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0;
11680Sstevel@tonic-gate break;
11690Sstevel@tonic-gate case FASTTRAP_JS:
11700Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) != 0;
11710Sstevel@tonic-gate break;
11720Sstevel@tonic-gate case FASTTRAP_JNS:
11730Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_SF) == 0;
11740Sstevel@tonic-gate break;
11750Sstevel@tonic-gate case FASTTRAP_JP:
11760Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) != 0;
11770Sstevel@tonic-gate break;
11780Sstevel@tonic-gate case FASTTRAP_JNP:
11790Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_PF) == 0;
11800Sstevel@tonic-gate break;
11810Sstevel@tonic-gate case FASTTRAP_JL:
11820Sstevel@tonic-gate taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
11830Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11840Sstevel@tonic-gate break;
11850Sstevel@tonic-gate case FASTTRAP_JGE:
11860Sstevel@tonic-gate taken = ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
11870Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11880Sstevel@tonic-gate break;
11890Sstevel@tonic-gate case FASTTRAP_JLE:
11900Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 ||
11910Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) !=
11920Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11930Sstevel@tonic-gate break;
11940Sstevel@tonic-gate case FASTTRAP_JG:
11950Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
11960Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_SF) == 0) ==
11970Sstevel@tonic-gate ((rp->r_ps & FASTTRAP_EFLAGS_OF) == 0);
11980Sstevel@tonic-gate break;
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate if (taken)
12030Sstevel@tonic-gate new_pc = tp->ftt_dest;
12040Sstevel@tonic-gate else
12050Sstevel@tonic-gate new_pc = pc + tp->ftt_size;
12060Sstevel@tonic-gate break;
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate case FASTTRAP_T_LOOP:
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate uint_t taken;
12120Sstevel@tonic-gate #ifdef __amd64
12130Sstevel@tonic-gate greg_t cx = rp->r_rcx--;
12140Sstevel@tonic-gate #else
12150Sstevel@tonic-gate greg_t cx = rp->r_ecx--;
12160Sstevel@tonic-gate #endif
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate switch (tp->ftt_code) {
12190Sstevel@tonic-gate case FASTTRAP_LOOPNZ:
12200Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) == 0 &&
12210Sstevel@tonic-gate cx != 0;
12220Sstevel@tonic-gate break;
12230Sstevel@tonic-gate case FASTTRAP_LOOPZ:
12240Sstevel@tonic-gate taken = (rp->r_ps & FASTTRAP_EFLAGS_ZF) != 0 &&
12250Sstevel@tonic-gate cx != 0;
12260Sstevel@tonic-gate break;
12270Sstevel@tonic-gate case FASTTRAP_LOOP:
12280Sstevel@tonic-gate taken = (cx != 0);
12290Sstevel@tonic-gate break;
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate
12320Sstevel@tonic-gate if (taken)
12330Sstevel@tonic-gate new_pc = tp->ftt_dest;
12340Sstevel@tonic-gate else
12350Sstevel@tonic-gate new_pc = pc + tp->ftt_size;
12360Sstevel@tonic-gate break;
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate
12390Sstevel@tonic-gate case FASTTRAP_T_JCXZ:
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate #ifdef __amd64
12420Sstevel@tonic-gate greg_t cx = rp->r_rcx;
12430Sstevel@tonic-gate #else
12440Sstevel@tonic-gate greg_t cx = rp->r_ecx;
12450Sstevel@tonic-gate #endif
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate if (cx == 0)
12480Sstevel@tonic-gate new_pc = tp->ftt_dest;
12490Sstevel@tonic-gate else
12500Sstevel@tonic-gate new_pc = pc + tp->ftt_size;
12510Sstevel@tonic-gate break;
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate case FASTTRAP_T_PUSHL_EBP:
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate int ret;
12570Sstevel@tonic-gate uintptr_t addr;
12580Sstevel@tonic-gate #ifdef __amd64
12590Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
12600Sstevel@tonic-gate #endif
12610Sstevel@tonic-gate addr = rp->r_sp - sizeof (uintptr_t);
12620Sstevel@tonic-gate ret = fasttrap_sulword((void *)addr, rp->r_fp);
12630Sstevel@tonic-gate #ifdef __amd64
12640Sstevel@tonic-gate } else {
12650Sstevel@tonic-gate addr = rp->r_sp - sizeof (uint32_t);
12660Sstevel@tonic-gate ret = fasttrap_suword32((void *)addr,
12670Sstevel@tonic-gate (uint32_t)rp->r_fp);
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate #endif
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate if (ret == -1) {
12720Sstevel@tonic-gate fasttrap_sigsegv(p, curthread, addr);
12730Sstevel@tonic-gate new_pc = pc;
12740Sstevel@tonic-gate break;
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate rp->r_sp = addr;
12780Sstevel@tonic-gate new_pc = pc + tp->ftt_size;
12790Sstevel@tonic-gate break;
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate
12822769Sahl case FASTTRAP_T_NOP:
12832769Sahl new_pc = pc + tp->ftt_size;
12842769Sahl break;
12852769Sahl
12860Sstevel@tonic-gate case FASTTRAP_T_JMP:
12870Sstevel@tonic-gate case FASTTRAP_T_CALL:
12880Sstevel@tonic-gate if (tp->ftt_code == 0) {
12890Sstevel@tonic-gate new_pc = tp->ftt_dest;
12900Sstevel@tonic-gate } else {
12912712Snn35248 uintptr_t value, addr = tp->ftt_dest;
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate if (tp->ftt_base != FASTTRAP_NOREG)
12940Sstevel@tonic-gate addr += fasttrap_getreg(rp, tp->ftt_base);
12950Sstevel@tonic-gate if (tp->ftt_index != FASTTRAP_NOREG)
12960Sstevel@tonic-gate addr += fasttrap_getreg(rp, tp->ftt_index) <<
12970Sstevel@tonic-gate tp->ftt_scale;
12980Sstevel@tonic-gate
12990Sstevel@tonic-gate if (tp->ftt_code == 1) {
13002712Snn35248 /*
13012712Snn35248 * If there's a segment prefix for this
13022712Snn35248 * instruction, we'll need to check permissions
13032712Snn35248 * and bounds on the given selector, and adjust
13042712Snn35248 * the address accordingly.
13052712Snn35248 */
13062712Snn35248 if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
13072712Snn35248 fasttrap_do_seg(tp, rp, &addr) != 0) {
13082712Snn35248 fasttrap_sigsegv(p, curthread, addr);
13092712Snn35248 new_pc = pc;
13102712Snn35248 break;
13112712Snn35248 }
13122712Snn35248
13130Sstevel@tonic-gate #ifdef __amd64
13140Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
13150Sstevel@tonic-gate #endif
13160Sstevel@tonic-gate if (fasttrap_fulword((void *)addr,
13170Sstevel@tonic-gate &value) == -1) {
13180Sstevel@tonic-gate fasttrap_sigsegv(p, curthread,
13190Sstevel@tonic-gate addr);
13200Sstevel@tonic-gate new_pc = pc;
13210Sstevel@tonic-gate break;
13220Sstevel@tonic-gate }
13230Sstevel@tonic-gate new_pc = value;
13240Sstevel@tonic-gate #ifdef __amd64
13250Sstevel@tonic-gate } else {
13262712Snn35248 uint32_t value32;
13272712Snn35248 addr = (uintptr_t)(uint32_t)addr;
13280Sstevel@tonic-gate if (fasttrap_fuword32((void *)addr,
13292712Snn35248 &value32) == -1) {
13300Sstevel@tonic-gate fasttrap_sigsegv(p, curthread,
13310Sstevel@tonic-gate addr);
13320Sstevel@tonic-gate new_pc = pc;
13330Sstevel@tonic-gate break;
13340Sstevel@tonic-gate }
13352712Snn35248 new_pc = value32;
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate #endif
13380Sstevel@tonic-gate } else {
13390Sstevel@tonic-gate new_pc = addr;
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate /*
13440Sstevel@tonic-gate * If this is a call instruction, we need to push the return
13450Sstevel@tonic-gate * address onto the stack. If this fails, we send the process
13460Sstevel@tonic-gate * a SIGSEGV and reset the pc to emulate what would happen if
13470Sstevel@tonic-gate * this instruction weren't traced.
13480Sstevel@tonic-gate */
13490Sstevel@tonic-gate if (tp->ftt_type == FASTTRAP_T_CALL) {
13500Sstevel@tonic-gate int ret;
13510Sstevel@tonic-gate uintptr_t addr;
13520Sstevel@tonic-gate #ifdef __amd64
13530Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
13540Sstevel@tonic-gate addr = rp->r_sp - sizeof (uintptr_t);
13550Sstevel@tonic-gate ret = fasttrap_sulword((void *)addr,
13560Sstevel@tonic-gate pc + tp->ftt_size);
13570Sstevel@tonic-gate } else {
13580Sstevel@tonic-gate #endif
13590Sstevel@tonic-gate addr = rp->r_sp - sizeof (uint32_t);
13600Sstevel@tonic-gate ret = fasttrap_suword32((void *)addr,
13610Sstevel@tonic-gate (uint32_t)(pc + tp->ftt_size));
13620Sstevel@tonic-gate #ifdef __amd64
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate #endif
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate if (ret == -1) {
13670Sstevel@tonic-gate fasttrap_sigsegv(p, curthread, addr);
13680Sstevel@tonic-gate new_pc = pc;
13690Sstevel@tonic-gate break;
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate rp->r_sp = addr;
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate break;
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate case FASTTRAP_T_COMMON:
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate uintptr_t addr;
1380*6390Sahl #if defined(__amd64)
1381*6390Sahl uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22];
1382*6390Sahl #else
1383*6390Sahl uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7];
1384*6390Sahl #endif
13850Sstevel@tonic-gate uint_t i = 0;
13860Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate /*
13890Sstevel@tonic-gate * Compute the address of the ulwp_t and step over the
13900Sstevel@tonic-gate * ul_self pointer. The method used to store the user-land
13910Sstevel@tonic-gate * thread pointer is very different on 32- and 64-bit
13920Sstevel@tonic-gate * kernels.
13930Sstevel@tonic-gate */
13940Sstevel@tonic-gate #if defined(__amd64)
13950Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64) {
13960Sstevel@tonic-gate addr = lwp->lwp_pcb.pcb_fsbase;
13970Sstevel@tonic-gate addr += sizeof (void *);
13980Sstevel@tonic-gate } else {
13990Sstevel@tonic-gate addr = lwp->lwp_pcb.pcb_gsbase;
14000Sstevel@tonic-gate addr += sizeof (caddr32_t);
14010Sstevel@tonic-gate }
1402*6390Sahl #else
14030Sstevel@tonic-gate addr = USEGD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
14040Sstevel@tonic-gate addr += sizeof (void *);
14050Sstevel@tonic-gate #endif
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate /*
14080Sstevel@tonic-gate * Generic Instruction Tracing
14090Sstevel@tonic-gate * ---------------------------
14100Sstevel@tonic-gate *
14110Sstevel@tonic-gate * This is the layout of the scratch space in the user-land
14120Sstevel@tonic-gate * thread structure for our generated instructions.
14130Sstevel@tonic-gate *
14140Sstevel@tonic-gate * 32-bit mode bytes
14150Sstevel@tonic-gate * ------------------------ -----
14160Sstevel@tonic-gate * a: <original instruction> <= 15
14170Sstevel@tonic-gate * jmp <pc + tp->ftt_size> 5
14180Sstevel@tonic-gate * b: <original instrction> <= 15
14190Sstevel@tonic-gate * int T_DTRACE_RET 2
14200Sstevel@tonic-gate * -----
14210Sstevel@tonic-gate * <= 37
14220Sstevel@tonic-gate *
14230Sstevel@tonic-gate * 64-bit mode bytes
14240Sstevel@tonic-gate * ------------------------ -----
14250Sstevel@tonic-gate * a: <original instruction> <= 15
14260Sstevel@tonic-gate * jmp 0(%rip) 6
14270Sstevel@tonic-gate * <pc + tp->ftt_size> 8
14280Sstevel@tonic-gate * b: <original instruction> <= 15
14290Sstevel@tonic-gate * int T_DTRACE_RET 2
14300Sstevel@tonic-gate * -----
14310Sstevel@tonic-gate * <= 46
14320Sstevel@tonic-gate *
14330Sstevel@tonic-gate * The %pc is set to a, and curthread->t_dtrace_astpc is set
14340Sstevel@tonic-gate * to b. If we encounter a signal on the way out of the
14350Sstevel@tonic-gate * kernel, trap() will set %pc to curthread->t_dtrace_astpc
14360Sstevel@tonic-gate * so that we execute the original instruction and re-enter
14370Sstevel@tonic-gate * the kernel rather than redirecting to the next instruction.
14380Sstevel@tonic-gate *
14390Sstevel@tonic-gate * If there are return probes (so we know that we're going to
14400Sstevel@tonic-gate * need to reenter the kernel after executing the original
14410Sstevel@tonic-gate * instruction), the scratch space will just contain the
14420Sstevel@tonic-gate * original instruction followed by an interrupt -- the same
14430Sstevel@tonic-gate * data as at b.
14440Sstevel@tonic-gate *
14450Sstevel@tonic-gate * %rip-relative Addressing
14460Sstevel@tonic-gate * ------------------------
14470Sstevel@tonic-gate *
14480Sstevel@tonic-gate * There's a further complication in 64-bit mode due to %rip-
14490Sstevel@tonic-gate * relative addressing. While this is clearly a beneficial
14500Sstevel@tonic-gate * architectural decision for position independent code, it's
14510Sstevel@tonic-gate * hard not to see it as a personal attack against the pid
14520Sstevel@tonic-gate * provider since before there was a relatively small set of
14530Sstevel@tonic-gate * instructions to emulate; with %rip-relative addressing,
14540Sstevel@tonic-gate * almost every instruction can potentially depend on the
14550Sstevel@tonic-gate * address at which it's executed. Rather than emulating
14560Sstevel@tonic-gate * the broad spectrum of instructions that can now be
14570Sstevel@tonic-gate * position dependent, we emulate jumps and others as in
14580Sstevel@tonic-gate * 32-bit mode, and take a different tack for instructions
14590Sstevel@tonic-gate * using %rip-relative addressing.
14600Sstevel@tonic-gate *
14610Sstevel@tonic-gate * For every instruction that uses the ModRM byte, the
14620Sstevel@tonic-gate * in-kernel disassembler reports its location. We use the
14630Sstevel@tonic-gate * ModRM byte to identify that an instruction uses
14640Sstevel@tonic-gate * %rip-relative addressing and to see what other registers
14650Sstevel@tonic-gate * the instruction uses. To emulate those instructions,
14660Sstevel@tonic-gate * we modify the instruction to be %rax-relative rather than
14670Sstevel@tonic-gate * %rip-relative (or %rcx-relative if the instruction uses
14680Sstevel@tonic-gate * %rax; or %r8- or %r9-relative if the REX.B is present so
14690Sstevel@tonic-gate * we don't have to rewrite the REX prefix). We then load
14700Sstevel@tonic-gate * the value that %rip would have been into the scratch
14710Sstevel@tonic-gate * register and generate an instruction to reset the scratch
14720Sstevel@tonic-gate * register back to its original value. The instruction
14730Sstevel@tonic-gate * sequence looks like this:
14740Sstevel@tonic-gate *
14750Sstevel@tonic-gate * 64-mode %rip-relative bytes
14760Sstevel@tonic-gate * ------------------------ -----
14770Sstevel@tonic-gate * a: <modified instruction> <= 15
14780Sstevel@tonic-gate * movq $<value>, %<scratch> 6
14790Sstevel@tonic-gate * jmp 0(%rip) 6
14800Sstevel@tonic-gate * <pc + tp->ftt_size> 8
14810Sstevel@tonic-gate * b: <modified instruction> <= 15
14820Sstevel@tonic-gate * int T_DTRACE_RET 2
14830Sstevel@tonic-gate * -----
14840Sstevel@tonic-gate * 52
14850Sstevel@tonic-gate *
14860Sstevel@tonic-gate * We set curthread->t_dtrace_regv so that upon receiving
14870Sstevel@tonic-gate * a signal we can reset the value of the scratch register.
14880Sstevel@tonic-gate */
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate curthread->t_dtrace_scrpc = addr;
14930Sstevel@tonic-gate bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
14940Sstevel@tonic-gate i += tp->ftt_size;
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate #ifdef __amd64
14970Sstevel@tonic-gate if (tp->ftt_ripmode != 0) {
14980Sstevel@tonic-gate greg_t *reg;
14990Sstevel@tonic-gate
15000Sstevel@tonic-gate ASSERT(p->p_model == DATAMODEL_LP64);
15010Sstevel@tonic-gate ASSERT(tp->ftt_ripmode &
15020Sstevel@tonic-gate (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate /*
15050Sstevel@tonic-gate * If this was a %rip-relative instruction, we change
15060Sstevel@tonic-gate * it to be either a %rax- or %rcx-relative
15070Sstevel@tonic-gate * instruction (depending on whether those registers
15080Sstevel@tonic-gate * are used as another operand; or %r8- or %r9-
15090Sstevel@tonic-gate * relative depending on the value of REX.B). We then
15100Sstevel@tonic-gate * set that register and generate a movq instruction
15110Sstevel@tonic-gate * to reset the value.
15120Sstevel@tonic-gate */
15130Sstevel@tonic-gate if (tp->ftt_ripmode & FASTTRAP_RIP_X)
15140Sstevel@tonic-gate scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
15150Sstevel@tonic-gate else
15160Sstevel@tonic-gate scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate if (tp->ftt_ripmode & FASTTRAP_RIP_1)
15190Sstevel@tonic-gate scratch[i++] = FASTTRAP_MOV_EAX;
15200Sstevel@tonic-gate else
15210Sstevel@tonic-gate scratch[i++] = FASTTRAP_MOV_ECX;
15220Sstevel@tonic-gate
15230Sstevel@tonic-gate switch (tp->ftt_ripmode) {
15240Sstevel@tonic-gate case FASTTRAP_RIP_1:
15250Sstevel@tonic-gate reg = &rp->r_rax;
15260Sstevel@tonic-gate curthread->t_dtrace_reg = REG_RAX;
15270Sstevel@tonic-gate break;
15280Sstevel@tonic-gate case FASTTRAP_RIP_2:
15290Sstevel@tonic-gate reg = &rp->r_rcx;
15300Sstevel@tonic-gate curthread->t_dtrace_reg = REG_RCX;
15310Sstevel@tonic-gate break;
15320Sstevel@tonic-gate case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
15330Sstevel@tonic-gate reg = &rp->r_r8;
15340Sstevel@tonic-gate curthread->t_dtrace_reg = REG_R8;
15350Sstevel@tonic-gate break;
15360Sstevel@tonic-gate case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
15370Sstevel@tonic-gate reg = &rp->r_r9;
15380Sstevel@tonic-gate curthread->t_dtrace_reg = REG_R9;
15390Sstevel@tonic-gate break;
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate
15423944Sahl /* LINTED - alignment */
15430Sstevel@tonic-gate *(uint64_t *)&scratch[i] = *reg;
15440Sstevel@tonic-gate curthread->t_dtrace_regv = *reg;
15450Sstevel@tonic-gate *reg = pc + tp->ftt_size;
15460Sstevel@tonic-gate i += sizeof (uint64_t);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate #endif
15490Sstevel@tonic-gate
15500Sstevel@tonic-gate /*
15510Sstevel@tonic-gate * Generate the branch instruction to what would have
15520Sstevel@tonic-gate * normally been the subsequent instruction. In 32-bit mode,
15530Sstevel@tonic-gate * this is just a relative branch; in 64-bit mode this is a
15540Sstevel@tonic-gate * %rip-relative branch that loads the 64-bit pc value
15550Sstevel@tonic-gate * immediately after the jmp instruction.
15560Sstevel@tonic-gate */
15570Sstevel@tonic-gate #ifdef __amd64
15580Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64) {
15590Sstevel@tonic-gate scratch[i++] = FASTTRAP_GROUP5_OP;
15600Sstevel@tonic-gate scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
15613944Sahl /* LINTED - alignment */
15620Sstevel@tonic-gate *(uint32_t *)&scratch[i] = 0;
15630Sstevel@tonic-gate i += sizeof (uint32_t);
15643944Sahl /* LINTED - alignment */
15650Sstevel@tonic-gate *(uint64_t *)&scratch[i] = pc + tp->ftt_size;
15660Sstevel@tonic-gate i += sizeof (uint64_t);
15670Sstevel@tonic-gate } else {
15680Sstevel@tonic-gate #endif
15690Sstevel@tonic-gate /*
15700Sstevel@tonic-gate * Set up the jmp to the next instruction; note that
15710Sstevel@tonic-gate * the size of the traced instruction cancels out.
15720Sstevel@tonic-gate */
15730Sstevel@tonic-gate scratch[i++] = FASTTRAP_JMP32;
15743944Sahl /* LINTED - alignment */
15750Sstevel@tonic-gate *(uint32_t *)&scratch[i] = pc - addr - 5;
15760Sstevel@tonic-gate i += sizeof (uint32_t);
15770Sstevel@tonic-gate #ifdef __amd64
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate #endif
15800Sstevel@tonic-gate
15810Sstevel@tonic-gate curthread->t_dtrace_astpc = addr + i;
15820Sstevel@tonic-gate bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
15830Sstevel@tonic-gate i += tp->ftt_size;
15840Sstevel@tonic-gate scratch[i++] = FASTTRAP_INT;
15850Sstevel@tonic-gate scratch[i++] = T_DTRACE_RET;
15860Sstevel@tonic-gate
1587*6390Sahl ASSERT(i <= sizeof (scratch));
1588*6390Sahl
15890Sstevel@tonic-gate if (fasttrap_copyout(scratch, (char *)addr, i)) {
15900Sstevel@tonic-gate fasttrap_sigtrap(p, curthread, pc);
15910Sstevel@tonic-gate new_pc = pc;
15920Sstevel@tonic-gate break;
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate if (tp->ftt_retids != NULL) {
15960Sstevel@tonic-gate curthread->t_dtrace_step = 1;
15970Sstevel@tonic-gate curthread->t_dtrace_ret = 1;
15980Sstevel@tonic-gate new_pc = curthread->t_dtrace_astpc;
15990Sstevel@tonic-gate } else {
16000Sstevel@tonic-gate new_pc = curthread->t_dtrace_scrpc;
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate curthread->t_dtrace_pc = pc;
16040Sstevel@tonic-gate curthread->t_dtrace_npc = pc + tp->ftt_size;
16050Sstevel@tonic-gate curthread->t_dtrace_on = 1;
16060Sstevel@tonic-gate break;
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate default:
16100Sstevel@tonic-gate panic("fasttrap: mishandled an instruction");
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate
16131710Sahl done:
16140Sstevel@tonic-gate /*
16150Sstevel@tonic-gate * If there were no return probes when we first found the tracepoint,
16160Sstevel@tonic-gate * we should feel no obligation to honor any return probes that were
16170Sstevel@tonic-gate * subsequently enabled -- they'll just have to wait until the next
16180Sstevel@tonic-gate * time around.
16190Sstevel@tonic-gate */
16200Sstevel@tonic-gate if (tp->ftt_retids != NULL) {
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate * We need to wait until the results of the instruction are
16230Sstevel@tonic-gate * apparent before invoking any return probes. If this
16240Sstevel@tonic-gate * instruction was emulated we can just call
16250Sstevel@tonic-gate * fasttrap_return_common(); if it needs to be executed, we
16260Sstevel@tonic-gate * need to wait until the user thread returns to the kernel.
16270Sstevel@tonic-gate */
16280Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_COMMON) {
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate * Set the program counter to the address of the traced
16310Sstevel@tonic-gate * instruction so that it looks right in ustack()
16320Sstevel@tonic-gate * output. We had previously set it to the end of the
16330Sstevel@tonic-gate * instruction to simplify %rip-relative addressing.
16340Sstevel@tonic-gate */
16350Sstevel@tonic-gate rp->r_pc = pc;
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate fasttrap_return_common(rp, pc, pid, new_pc);
16380Sstevel@tonic-gate } else {
16390Sstevel@tonic-gate ASSERT(curthread->t_dtrace_ret != 0);
16400Sstevel@tonic-gate ASSERT(curthread->t_dtrace_pc == pc);
16410Sstevel@tonic-gate ASSERT(curthread->t_dtrace_scrpc != 0);
16420Sstevel@tonic-gate ASSERT(new_pc == curthread->t_dtrace_astpc);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate
16460Sstevel@tonic-gate rp->r_pc = new_pc;
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate return (0);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate
16510Sstevel@tonic-gate int
fasttrap_return_probe(struct regs * rp)16520Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate proc_t *p = curproc;
16550Sstevel@tonic-gate uintptr_t pc = curthread->t_dtrace_pc;
16560Sstevel@tonic-gate uintptr_t npc = curthread->t_dtrace_npc;
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate curthread->t_dtrace_pc = 0;
16590Sstevel@tonic-gate curthread->t_dtrace_npc = 0;
16600Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0;
16610Sstevel@tonic-gate curthread->t_dtrace_astpc = 0;
16620Sstevel@tonic-gate
16630Sstevel@tonic-gate /*
16640Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its
16650Sstevel@tonic-gate * parent. We know that there's only one thread of control in such a
16660Sstevel@tonic-gate * process: this one.
16670Sstevel@tonic-gate */
16680Sstevel@tonic-gate while (p->p_flag & SVFORK) {
16690Sstevel@tonic-gate p = p->p_parent;
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate * We set rp->r_pc to the address of the traced instruction so
16740Sstevel@tonic-gate * that it appears to dtrace_probe() that we're on the original
16750Sstevel@tonic-gate * instruction, and so that the user can't easily detect our
16760Sstevel@tonic-gate * complex web of lies. dtrace_return_probe() (our caller)
16770Sstevel@tonic-gate * will correctly set %pc after we return.
16780Sstevel@tonic-gate */
16790Sstevel@tonic-gate rp->r_pc = pc;
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate fasttrap_return_common(rp, pc, p->p_pid, npc);
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate return (0);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate
16860Sstevel@tonic-gate /*ARGSUSED*/
16870Sstevel@tonic-gate uint64_t
fasttrap_pid_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)16882179Sahl fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
16892179Sahl int aframes)
16900Sstevel@tonic-gate {
16910Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 1, argno));
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate
16940Sstevel@tonic-gate /*ARGSUSED*/
16950Sstevel@tonic-gate uint64_t
fasttrap_usdt_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)16960Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
16970Sstevel@tonic-gate int aframes)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, 0, argno));
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate static ulong_t
fasttrap_getreg(struct regs * rp,uint_t reg)17030Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate #ifdef __amd64
17060Sstevel@tonic-gate switch (reg) {
17070Sstevel@tonic-gate case REG_R15: return (rp->r_r15);
17080Sstevel@tonic-gate case REG_R14: return (rp->r_r14);
17090Sstevel@tonic-gate case REG_R13: return (rp->r_r13);
17100Sstevel@tonic-gate case REG_R12: return (rp->r_r12);
17110Sstevel@tonic-gate case REG_R11: return (rp->r_r11);
17120Sstevel@tonic-gate case REG_R10: return (rp->r_r10);
17130Sstevel@tonic-gate case REG_R9: return (rp->r_r9);
17140Sstevel@tonic-gate case REG_R8: return (rp->r_r8);
17150Sstevel@tonic-gate case REG_RDI: return (rp->r_rdi);
17160Sstevel@tonic-gate case REG_RSI: return (rp->r_rsi);
17170Sstevel@tonic-gate case REG_RBP: return (rp->r_rbp);
17180Sstevel@tonic-gate case REG_RBX: return (rp->r_rbx);
17190Sstevel@tonic-gate case REG_RDX: return (rp->r_rdx);
17200Sstevel@tonic-gate case REG_RCX: return (rp->r_rcx);
17210Sstevel@tonic-gate case REG_RAX: return (rp->r_rax);
17220Sstevel@tonic-gate case REG_TRAPNO: return (rp->r_trapno);
17230Sstevel@tonic-gate case REG_ERR: return (rp->r_err);
17240Sstevel@tonic-gate case REG_RIP: return (rp->r_rip);
17250Sstevel@tonic-gate case REG_CS: return (rp->r_cs);
17260Sstevel@tonic-gate case REG_RFL: return (rp->r_rfl);
17270Sstevel@tonic-gate case REG_RSP: return (rp->r_rsp);
17280Sstevel@tonic-gate case REG_SS: return (rp->r_ss);
17290Sstevel@tonic-gate case REG_FS: return (rp->r_fs);
17300Sstevel@tonic-gate case REG_GS: return (rp->r_gs);
17310Sstevel@tonic-gate case REG_DS: return (rp->r_ds);
17320Sstevel@tonic-gate case REG_ES: return (rp->r_es);
17333446Smrj case REG_FSBASE: return (rdmsr(MSR_AMD_FSBASE));
17343446Smrj case REG_GSBASE: return (rdmsr(MSR_AMD_GSBASE));
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate
17370Sstevel@tonic-gate panic("dtrace: illegal register constant");
17380Sstevel@tonic-gate /*NOTREACHED*/
17390Sstevel@tonic-gate #else
17400Sstevel@tonic-gate if (reg >= _NGREG)
17410Sstevel@tonic-gate panic("dtrace: illegal register constant");
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate return (((greg_t *)&rp->r_gs)[reg]);
17440Sstevel@tonic-gate #endif
17450Sstevel@tonic-gate }
1746