17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5ac448965Sahl * Common Development and Distribution License (the "License").
6ac448965Sahl * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21ac448965Sahl
227c478bd9Sstevel@tonic-gate /*
236009dbc6Sahl * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
287c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
297c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
307c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <sys/regset.h>
337c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
347c478bd9Sstevel@tonic-gate #include <sys/segments.h>
35ae115bc7Smrj #include <sys/x86_archext.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/trap.h>
389acbbeafSnn #include <sys/archsystm.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * Lossless User-Land Tracing on x86
427c478bd9Sstevel@tonic-gate * ---------------------------------
437c478bd9Sstevel@tonic-gate *
44ac448965Sahl * The execution of most instructions is not dependent on the address; for
45ac448965Sahl * these instructions it is sufficient to copy them into the user process's
46ac448965Sahl * address space and execute them. To effectively single-step an instruction
47ac448965Sahl * in user-land, we copy out the following sequence of instructions to scratch
487c478bd9Sstevel@tonic-gate * space in the user thread's ulwp_t structure.
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * We then set the program counter (%eip or %rip) to point to this scratch
517c478bd9Sstevel@tonic-gate * space. Once execution resumes, the original instruction is executed and
527c478bd9Sstevel@tonic-gate * then control flow is redirected to what was originally the subsequent
537c478bd9Sstevel@tonic-gate * instruction. If the kernel attemps to deliver a signal while single-
547c478bd9Sstevel@tonic-gate * stepping, the signal is deferred and the program counter is moved into the
557c478bd9Sstevel@tonic-gate * second sequence of instructions. The second sequence ends in a trap into
567c478bd9Sstevel@tonic-gate * the kernel where the deferred signal is then properly handled and delivered.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * For instructions whose execute is position dependent, we perform simple
597c478bd9Sstevel@tonic-gate * emulation. These instructions are limited to control transfer
607c478bd9Sstevel@tonic-gate * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
617c478bd9Sstevel@tonic-gate * of %rip-relative addressing that means that almost any instruction can be
627c478bd9Sstevel@tonic-gate * position dependent. For all the details on how we emulate generic
637c478bd9Sstevel@tonic-gate * instructions included %rip-relative instructions, see the code in
647c478bd9Sstevel@tonic-gate * fasttrap_pid_probe() below where we handle instructions of type
657c478bd9Sstevel@tonic-gate * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
697c478bd9Sstevel@tonic-gate #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
707c478bd9Sstevel@tonic-gate #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
717c478bd9Sstevel@tonic-gate #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
747c478bd9Sstevel@tonic-gate #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
757c478bd9Sstevel@tonic-gate #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
787c478bd9Sstevel@tonic-gate #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
797c478bd9Sstevel@tonic-gate #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
807c478bd9Sstevel@tonic-gate #define FASTTRAP_REX_B(rex) ((rex) & 1)
817c478bd9Sstevel@tonic-gate #define FASTTRAP_REX(w, r, x, b) \
827c478bd9Sstevel@tonic-gate (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Single-byte op-codes.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate #define FASTTRAP_PUSHL_EBP 0x55
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate #define FASTTRAP_JO 0x70
907c478bd9Sstevel@tonic-gate #define FASTTRAP_JNO 0x71
917c478bd9Sstevel@tonic-gate #define FASTTRAP_JB 0x72
927c478bd9Sstevel@tonic-gate #define FASTTRAP_JAE 0x73
937c478bd9Sstevel@tonic-gate #define FASTTRAP_JE 0x74
947c478bd9Sstevel@tonic-gate #define FASTTRAP_JNE 0x75
957c478bd9Sstevel@tonic-gate #define FASTTRAP_JBE 0x76
967c478bd9Sstevel@tonic-gate #define FASTTRAP_JA 0x77
977c478bd9Sstevel@tonic-gate #define FASTTRAP_JS 0x78
987c478bd9Sstevel@tonic-gate #define FASTTRAP_JNS 0x79
997c478bd9Sstevel@tonic-gate #define FASTTRAP_JP 0x7a
1007c478bd9Sstevel@tonic-gate #define FASTTRAP_JNP 0x7b
1017c478bd9Sstevel@tonic-gate #define FASTTRAP_JL 0x7c
1027c478bd9Sstevel@tonic-gate #define FASTTRAP_JGE 0x7d
1037c478bd9Sstevel@tonic-gate #define FASTTRAP_JLE 0x7e
1047c478bd9Sstevel@tonic-gate #define FASTTRAP_JG 0x7f
1057c478bd9Sstevel@tonic-gate
1062b6e762cSahl #define FASTTRAP_NOP 0x90
1072b6e762cSahl
1087c478bd9Sstevel@tonic-gate #define FASTTRAP_MOV_EAX 0xb8
1097c478bd9Sstevel@tonic-gate #define FASTTRAP_MOV_ECX 0xb9
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate #define FASTTRAP_RET16 0xc2
1127c478bd9Sstevel@tonic-gate #define FASTTRAP_RET 0xc3
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate #define FASTTRAP_LOOPNZ 0xe0
1157c478bd9Sstevel@tonic-gate #define FASTTRAP_LOOPZ 0xe1
1167c478bd9Sstevel@tonic-gate #define FASTTRAP_LOOP 0xe2
1177c478bd9Sstevel@tonic-gate #define FASTTRAP_JCXZ 0xe3
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate #define FASTTRAP_CALL 0xe8
1207c478bd9Sstevel@tonic-gate #define FASTTRAP_JMP32 0xe9
1217c478bd9Sstevel@tonic-gate #define FASTTRAP_JMP8 0xeb
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate #define FASTTRAP_INT3 0xcc
1247c478bd9Sstevel@tonic-gate #define FASTTRAP_INT 0xcd
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate #define FASTTRAP_2_BYTE_OP 0x0f
1277c478bd9Sstevel@tonic-gate #define FASTTRAP_GROUP5_OP 0xff
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * Two-byte op-codes (second byte only).
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JO 0x80
1337c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JNO 0x81
1347c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JB 0x82
1357c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JAE 0x83
1367c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JE 0x84
1377c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JNE 0x85
1387c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JBE 0x86
1397c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JA 0x87
1407c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JS 0x88
1417c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JNS 0x89
1427c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JP 0x8a
1437c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JNP 0x8b
1447c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JL 0x8c
1457c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JGE 0x8d
1467c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JLE 0x8e
1477c478bd9Sstevel@tonic-gate #define FASTTRAP_0F_JG 0x8f
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_OF 0x800
1507c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_DF 0x400
1517c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_SF 0x080
1527c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_ZF 0x040
1537c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_AF 0x010
1547c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_PF 0x004
1557c478bd9Sstevel@tonic-gate #define FASTTRAP_EFLAGS_CF 0x001
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * Instruction prefixes.
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_OPERAND 0x66
1617c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_ADDRESS 0x67
1627c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_CS 0x2E
1637c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_DS 0x3E
1647c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_ES 0x26
1657c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_FS 0x64
1667c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_GS 0x65
1677c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_SS 0x36
1687c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_LOCK 0xF0
1697c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_REP 0xF3
1707c478bd9Sstevel@tonic-gate #define FASTTRAP_PREFIX_REPNE 0xF2
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate #define FASTTRAP_NOREG 0xff
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * Map between instruction register encodings and the kernel constants which
1767c478bd9Sstevel@tonic-gate * correspond to indicies into struct regs.
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate static const uint8_t regmap[16] = {
1797c478bd9Sstevel@tonic-gate REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
1807c478bd9Sstevel@tonic-gate REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate static uint64_t
fasttrap_anarg(struct regs * rp,int function_entry,int argno)1867c478bd9Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int function_entry, int argno)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate uint64_t value;
1897c478bd9Sstevel@tonic-gate int shift = function_entry ? 1 : 0;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_LP64) {
1927c478bd9Sstevel@tonic-gate uintptr_t *stack;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * In 64-bit mode, the first six arguments are stored in
1967c478bd9Sstevel@tonic-gate * registers.
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate if (argno < 6)
1997c478bd9Sstevel@tonic-gate return ((&rp->r_rdi)[argno]);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate stack = (uintptr_t *)rp->r_sp;
2027c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2037c478bd9Sstevel@tonic-gate value = dtrace_fulword(&stack[argno - 6 + shift]);
2047c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2057c478bd9Sstevel@tonic-gate } else {
2067c478bd9Sstevel@tonic-gate uint32_t *stack = (uint32_t *)rp->r_sp;
2077c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2087c478bd9Sstevel@tonic-gate value = dtrace_fuword32(&stack[argno + shift]);
2097c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate return (value);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2167c478bd9Sstevel@tonic-gate int
fasttrap_tracepoint_init(proc_t * p,fasttrap_tracepoint_t * tp,uintptr_t pc,fasttrap_probe_type_t type)217ac448965Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
218ac448965Sahl fasttrap_probe_type_t type)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
2217c478bd9Sstevel@tonic-gate size_t len = FASTTRAP_MAX_INSTR_SIZE;
2227c478bd9Sstevel@tonic-gate size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
2237c478bd9Sstevel@tonic-gate uint_t start = 0;
2242b6e762cSahl int rmindex, size;
2259acbbeafSnn uint8_t seg, rex = 0;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * Read the instruction at the given address out of the process's
2297c478bd9Sstevel@tonic-gate * address space. We don't have to worry about a debugger
2307c478bd9Sstevel@tonic-gate * changing this instruction before we overwrite it with our trap
2317c478bd9Sstevel@tonic-gate * instruction since P_PR_LOCK is set. Since instructions can span
2327c478bd9Sstevel@tonic-gate * pages, we potentially read the instruction in two parts. If the
2337c478bd9Sstevel@tonic-gate * second part fails, we just zero out that part of the instruction.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate if (uread(p, &instr[0], first, pc) != 0)
2367c478bd9Sstevel@tonic-gate return (-1);
2377c478bd9Sstevel@tonic-gate if (len > first &&
2387c478bd9Sstevel@tonic-gate uread(p, &instr[first], len - first, pc + first) != 0) {
2397c478bd9Sstevel@tonic-gate bzero(&instr[first], len - first);
2407c478bd9Sstevel@tonic-gate len = first;
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * If the disassembly fails, then we have a malformed instruction.
2457c478bd9Sstevel@tonic-gate */
2462b6e762cSahl if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0)
2477c478bd9Sstevel@tonic-gate return (-1);
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * Make sure the disassembler isn't completely broken.
2517c478bd9Sstevel@tonic-gate */
2522b6e762cSahl ASSERT(-1 <= rmindex && rmindex < size);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * If the computed size is greater than the number of bytes read,
2567c478bd9Sstevel@tonic-gate * then it was a malformed instruction possibly because it fell on a
2577c478bd9Sstevel@tonic-gate * page boundary and the subsequent page was missing or because of
2587c478bd9Sstevel@tonic-gate * some malicious user.
2597c478bd9Sstevel@tonic-gate */
2602b6e762cSahl if (size > len)
2617c478bd9Sstevel@tonic-gate return (-1);
2627c478bd9Sstevel@tonic-gate
2632b6e762cSahl tp->ftt_size = (uint8_t)size;
2649acbbeafSnn tp->ftt_segment = FASTTRAP_SEG_NONE;
2659acbbeafSnn
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * Find the start of the instruction's opcode by processing any
2687c478bd9Sstevel@tonic-gate * legacy prefixes.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate for (;;) {
2719acbbeafSnn seg = 0;
2727c478bd9Sstevel@tonic-gate switch (instr[start]) {
2739acbbeafSnn case FASTTRAP_PREFIX_SS:
2749acbbeafSnn seg++;
2759acbbeafSnn /*FALLTHRU*/
2769acbbeafSnn case FASTTRAP_PREFIX_GS:
2779acbbeafSnn seg++;
2789acbbeafSnn /*FALLTHRU*/
2799acbbeafSnn case FASTTRAP_PREFIX_FS:
2809acbbeafSnn seg++;
2819acbbeafSnn /*FALLTHRU*/
2829acbbeafSnn case FASTTRAP_PREFIX_ES:
2839acbbeafSnn seg++;
2849acbbeafSnn /*FALLTHRU*/
2859acbbeafSnn case FASTTRAP_PREFIX_DS:
2869acbbeafSnn seg++;
2879acbbeafSnn /*FALLTHRU*/
2889acbbeafSnn case FASTTRAP_PREFIX_CS:
2899acbbeafSnn seg++;
2909acbbeafSnn /*FALLTHRU*/
2917c478bd9Sstevel@tonic-gate case FASTTRAP_PREFIX_OPERAND:
2927c478bd9Sstevel@tonic-gate case FASTTRAP_PREFIX_ADDRESS:
2937c478bd9Sstevel@tonic-gate case FASTTRAP_PREFIX_LOCK:
2947c478bd9Sstevel@tonic-gate case FASTTRAP_PREFIX_REP:
2957c478bd9Sstevel@tonic-gate case FASTTRAP_PREFIX_REPNE:
2969acbbeafSnn if (seg != 0) {
2979acbbeafSnn /*
2989acbbeafSnn * It's illegal for an instruction to specify
2999acbbeafSnn * two segment prefixes -- give up on this
3009acbbeafSnn * illegal instruction.
3019acbbeafSnn */
3029acbbeafSnn if (tp->ftt_segment != FASTTRAP_SEG_NONE)
3039acbbeafSnn return (-1);
3049acbbeafSnn
3059acbbeafSnn tp->ftt_segment = seg;
3069acbbeafSnn }
3077c478bd9Sstevel@tonic-gate start++;
3087c478bd9Sstevel@tonic-gate continue;
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate break;
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * Identify the REX prefix on 64-bit processes.
3157c478bd9Sstevel@tonic-gate */
3167c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
3177c478bd9Sstevel@tonic-gate rex = instr[start++];
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * Now that we're pretty sure that the instruction is okay, copy the
3217c478bd9Sstevel@tonic-gate * valid part to the tracepoint.
3227c478bd9Sstevel@tonic-gate */
3237c478bd9Sstevel@tonic-gate bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_COMMON;
3267c478bd9Sstevel@tonic-gate if (instr[start] == FASTTRAP_2_BYTE_OP) {
3277c478bd9Sstevel@tonic-gate switch (instr[start + 1]) {
3287c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JO:
3297c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JNO:
3307c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JB:
3317c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JAE:
3327c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JE:
3337c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JNE:
3347c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JBE:
3357c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JA:
3367c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JS:
3377c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JNS:
3387c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JP:
3397c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JNP:
3407c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JL:
3417c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JGE:
3427c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JLE:
3437c478bd9Sstevel@tonic-gate case FASTTRAP_0F_JG:
3447c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCC;
3457c478bd9Sstevel@tonic-gate tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
3467c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
34773427c57Sahl /* LINTED - alignment */
3487c478bd9Sstevel@tonic-gate *(int32_t *)&instr[start + 2];
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate } else if (instr[start] == FASTTRAP_GROUP5_OP) {
3527c478bd9Sstevel@tonic-gate uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
3537c478bd9Sstevel@tonic-gate uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
3547c478bd9Sstevel@tonic-gate uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate if (reg == 2 || reg == 4) {
3577c478bd9Sstevel@tonic-gate uint_t i, sz;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate if (reg == 2)
3607c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL;
3617c478bd9Sstevel@tonic-gate else
3627c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate if (mod == 3)
3657c478bd9Sstevel@tonic-gate tp->ftt_code = 2;
3667c478bd9Sstevel@tonic-gate else
3677c478bd9Sstevel@tonic-gate tp->ftt_code = 1;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate * See AMD x86-64 Architecture Programmer's Manual
3737c478bd9Sstevel@tonic-gate * Volume 3, Section 1.2.7, Table 1-12, and
3747c478bd9Sstevel@tonic-gate * Appendix A.3.1, Table A-15.
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate if (mod != 3 && rm == 4) {
3777c478bd9Sstevel@tonic-gate uint8_t sib = instr[start + 2];
3787c478bd9Sstevel@tonic-gate uint_t index = FASTTRAP_SIB_INDEX(sib);
3797c478bd9Sstevel@tonic-gate uint_t base = FASTTRAP_SIB_BASE(sib);
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate tp->ftt_index = (index == 4) ?
3847c478bd9Sstevel@tonic-gate FASTTRAP_NOREG :
3857c478bd9Sstevel@tonic-gate regmap[index | (FASTTRAP_REX_X(rex) << 3)];
3867c478bd9Sstevel@tonic-gate tp->ftt_base = (mod == 0 && base == 5) ?
3877c478bd9Sstevel@tonic-gate FASTTRAP_NOREG :
3887c478bd9Sstevel@tonic-gate regmap[base | (FASTTRAP_REX_B(rex) << 3)];
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate i = 3;
3917c478bd9Sstevel@tonic-gate sz = mod == 1 ? 1 : 4;
3927c478bd9Sstevel@tonic-gate } else {
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * In 64-bit mode, mod == 0 and r/m == 5
3957c478bd9Sstevel@tonic-gate * denotes %rip-relative addressing; in 32-bit
3967c478bd9Sstevel@tonic-gate * mode, the base register isn't used. In both
3977c478bd9Sstevel@tonic-gate * modes, there is a 32-bit operand.
3987c478bd9Sstevel@tonic-gate */
3997c478bd9Sstevel@tonic-gate if (mod == 0 && rm == 5) {
4007c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64)
4017c478bd9Sstevel@tonic-gate tp->ftt_base = REG_RIP;
4027c478bd9Sstevel@tonic-gate else
4037c478bd9Sstevel@tonic-gate tp->ftt_base = FASTTRAP_NOREG;
4047c478bd9Sstevel@tonic-gate sz = 4;
4057c478bd9Sstevel@tonic-gate } else {
4067c478bd9Sstevel@tonic-gate uint8_t base = rm |
4077c478bd9Sstevel@tonic-gate (FASTTRAP_REX_B(rex) << 3);
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate tp->ftt_base = regmap[base];
4107c478bd9Sstevel@tonic-gate sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate tp->ftt_index = FASTTRAP_NOREG;
4137c478bd9Sstevel@tonic-gate i = 2;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
41673427c57Sahl if (sz == 1) {
4177c478bd9Sstevel@tonic-gate tp->ftt_dest = *(int8_t *)&instr[start + i];
41873427c57Sahl } else if (sz == 4) {
41973427c57Sahl /* LINTED - alignment */
4207c478bd9Sstevel@tonic-gate tp->ftt_dest = *(int32_t *)&instr[start + i];
42173427c57Sahl } else {
4227c478bd9Sstevel@tonic-gate tp->ftt_dest = 0;
42373427c57Sahl }
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate } else {
4267c478bd9Sstevel@tonic-gate switch (instr[start]) {
4277c478bd9Sstevel@tonic-gate case FASTTRAP_RET:
4287c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RET;
4297c478bd9Sstevel@tonic-gate break;
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate case FASTTRAP_RET16:
4327c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RET16;
43373427c57Sahl /* LINTED - alignment */
4347c478bd9Sstevel@tonic-gate tp->ftt_dest = *(uint16_t *)&instr[start + 1];
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate case FASTTRAP_JO:
4387c478bd9Sstevel@tonic-gate case FASTTRAP_JNO:
4397c478bd9Sstevel@tonic-gate case FASTTRAP_JB:
4407c478bd9Sstevel@tonic-gate case FASTTRAP_JAE:
4417c478bd9Sstevel@tonic-gate case FASTTRAP_JE:
4427c478bd9Sstevel@tonic-gate case FASTTRAP_JNE:
4437c478bd9Sstevel@tonic-gate case FASTTRAP_JBE:
4447c478bd9Sstevel@tonic-gate case FASTTRAP_JA:
4457c478bd9Sstevel@tonic-gate case FASTTRAP_JS:
4467c478bd9Sstevel@tonic-gate case FASTTRAP_JNS:
4477c478bd9Sstevel@tonic-gate case FASTTRAP_JP:
4487c478bd9Sstevel@tonic-gate case FASTTRAP_JNP:
4497c478bd9Sstevel@tonic-gate case FASTTRAP_JL:
4507c478bd9Sstevel@tonic-gate case FASTTRAP_JGE:
4517c478bd9Sstevel@tonic-gate case FASTTRAP_JLE:
4527c478bd9Sstevel@tonic-gate case FASTTRAP_JG:
4537c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCC;
4547c478bd9Sstevel@tonic-gate tp->ftt_code = instr[start];
4557c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4567c478bd9Sstevel@tonic-gate (int8_t)instr[start + 1];
4577c478bd9Sstevel@tonic-gate break;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate case FASTTRAP_LOOPNZ:
4607c478bd9Sstevel@tonic-gate case FASTTRAP_LOOPZ:
4617c478bd9Sstevel@tonic-gate case FASTTRAP_LOOP:
4627c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_LOOP;
4637c478bd9Sstevel@tonic-gate tp->ftt_code = instr[start];
4647c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4657c478bd9Sstevel@tonic-gate (int8_t)instr[start + 1];
4667c478bd9Sstevel@tonic-gate break;
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate case FASTTRAP_JCXZ:
4697c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JCXZ;
4707c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4717c478bd9Sstevel@tonic-gate (int8_t)instr[start + 1];
4727c478bd9Sstevel@tonic-gate break;
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate case FASTTRAP_CALL:
4757c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL;
4767c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
47773427c57Sahl /* LINTED - alignment */
4787c478bd9Sstevel@tonic-gate *(int32_t *)&instr[start + 1];
4797c478bd9Sstevel@tonic-gate tp->ftt_code = 0;
4807c478bd9Sstevel@tonic-gate break;
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate case FASTTRAP_JMP32:
4837c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
4847c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
48573427c57Sahl /* LINTED - alignment */
4867c478bd9Sstevel@tonic-gate *(int32_t *)&instr[start + 1];
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate case FASTTRAP_JMP8:
4897c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMP;
4907c478bd9Sstevel@tonic-gate tp->ftt_dest = pc + tp->ftt_size +
4917c478bd9Sstevel@tonic-gate (int8_t)instr[start + 1];
4927c478bd9Sstevel@tonic-gate break;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate case FASTTRAP_PUSHL_EBP:
4957c478bd9Sstevel@tonic-gate if (start == 0)
4967c478bd9Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
4977c478bd9Sstevel@tonic-gate break;
4987c478bd9Sstevel@tonic-gate
4992b6e762cSahl case FASTTRAP_NOP:
5002b6e762cSahl ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
5012b6e762cSahl
5022b6e762cSahl /*
5032b6e762cSahl * On amd64 we have to be careful not to confuse a nop
5042b6e762cSahl * (actually xchgl %eax, %eax) with an instruction using
5052b6e762cSahl * the same opcode, but that does something different
5062b6e762cSahl * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
5072b6e762cSahl */
5082b6e762cSahl if (FASTTRAP_REX_B(rex) == 0)
5092b6e762cSahl tp->ftt_type = FASTTRAP_T_NOP;
5102b6e762cSahl break;
5112b6e762cSahl
5127c478bd9Sstevel@tonic-gate case FASTTRAP_INT3:
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate * The pid provider shares the int3 trap with debugger
5157c478bd9Sstevel@tonic-gate * breakpoints so we can't instrument them.
5167c478bd9Sstevel@tonic-gate */
5177c478bd9Sstevel@tonic-gate ASSERT(instr[start] == FASTTRAP_INSTR);
5187c478bd9Sstevel@tonic-gate return (-1);
5199acbbeafSnn
5209acbbeafSnn case FASTTRAP_INT:
5219acbbeafSnn /*
5229acbbeafSnn * Interrupts seem like they could be traced with
5239acbbeafSnn * no negative implications, but it's possible that
5249acbbeafSnn * a thread could be redirected by the trap handling
5259acbbeafSnn * code which would eventually return to the
5269acbbeafSnn * instruction after the interrupt. If the interrupt
5279acbbeafSnn * were in our scratch space, the subsequent
5289acbbeafSnn * instruction might be overwritten before we return.
5299acbbeafSnn * Accordingly we refuse to instrument any interrupt.
5309acbbeafSnn */
5319acbbeafSnn return (-1);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate * If the process is 64-bit and the instruction type is still
5387c478bd9Sstevel@tonic-gate * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
5397c478bd9Sstevel@tonic-gate * execute it -- we need to watch for %rip-relative
5407c478bd9Sstevel@tonic-gate * addressing mode. See the portion of fasttrap_pid_probe()
5417c478bd9Sstevel@tonic-gate * below where we handle tracepoints with type
5427c478bd9Sstevel@tonic-gate * FASTTRAP_T_COMMON for how we emulate instructions that
5437c478bd9Sstevel@tonic-gate * employ %rip-relative addressing.
5447c478bd9Sstevel@tonic-gate */
5457c478bd9Sstevel@tonic-gate if (rmindex != -1) {
5467c478bd9Sstevel@tonic-gate uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
5477c478bd9Sstevel@tonic-gate uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
5487c478bd9Sstevel@tonic-gate uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate ASSERT(rmindex > start);
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate if (mod == 0 && rm == 5) {
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate * We need to be sure to avoid other
5557c478bd9Sstevel@tonic-gate * registers used by this instruction. While
5567c478bd9Sstevel@tonic-gate * the reg field may determine the op code
5577c478bd9Sstevel@tonic-gate * rather than denoting a register, assuming
5587c478bd9Sstevel@tonic-gate * that it denotes a register is always safe.
5597c478bd9Sstevel@tonic-gate * We leave the REX field intact and use
5607c478bd9Sstevel@tonic-gate * whatever value's there for simplicity.
5617c478bd9Sstevel@tonic-gate */
5627c478bd9Sstevel@tonic-gate if (reg != 0) {
5637c478bd9Sstevel@tonic-gate tp->ftt_ripmode = FASTTRAP_RIP_1 |
5647c478bd9Sstevel@tonic-gate (FASTTRAP_RIP_X *
5657c478bd9Sstevel@tonic-gate FASTTRAP_REX_B(rex));
5667c478bd9Sstevel@tonic-gate rm = 0;
5677c478bd9Sstevel@tonic-gate } else {
5687c478bd9Sstevel@tonic-gate tp->ftt_ripmode = FASTTRAP_RIP_2 |
5697c478bd9Sstevel@tonic-gate (FASTTRAP_RIP_X *
5707c478bd9Sstevel@tonic-gate FASTTRAP_REX_B(rex));
5717c478bd9Sstevel@tonic-gate rm = 1;
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate tp->ftt_modrm = tp->ftt_instr[rmindex];
5757c478bd9Sstevel@tonic-gate tp->ftt_instr[rmindex] =
5767c478bd9Sstevel@tonic-gate FASTTRAP_MODRM(2, reg, rm);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate return (0);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate int
fasttrap_tracepoint_install(proc_t * p,fasttrap_tracepoint_t * tp)5857c478bd9Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate fasttrap_instr_t instr = FASTTRAP_INSTR;
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
5907c478bd9Sstevel@tonic-gate return (-1);
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate return (0);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate int
fasttrap_tracepoint_remove(proc_t * p,fasttrap_tracepoint_t * tp)5967c478bd9Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate uint8_t instr;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate * Distinguish between read or write failures and a changed
6027c478bd9Sstevel@tonic-gate * instruction.
6037c478bd9Sstevel@tonic-gate */
6047c478bd9Sstevel@tonic-gate if (uread(p, &instr, 1, tp->ftt_pc) != 0)
6057c478bd9Sstevel@tonic-gate return (0);
6067c478bd9Sstevel@tonic-gate if (instr != FASTTRAP_INSTR)
6077c478bd9Sstevel@tonic-gate return (0);
6087c478bd9Sstevel@tonic-gate if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
6097c478bd9Sstevel@tonic-gate return (-1);
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate return (0);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate static uintptr_t
fasttrap_fulword_noerr(const void * uaddr)6157c478bd9Sstevel@tonic-gate fasttrap_fulword_noerr(const void *uaddr)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate uintptr_t ret;
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate if (fasttrap_fulword(uaddr, &ret) == 0)
6207c478bd9Sstevel@tonic-gate return (ret);
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate return (0);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate static uint32_t
fasttrap_fuword32_noerr(const void * uaddr)6267c478bd9Sstevel@tonic-gate fasttrap_fuword32_noerr(const void *uaddr)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate uint32_t ret;
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate if (fasttrap_fuword32(uaddr, &ret) == 0)
6317c478bd9Sstevel@tonic-gate return (ret);
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate return (0);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate static void
fasttrap_return_common(struct regs * rp,uintptr_t pc,pid_t pid,uintptr_t new_pc)6377c478bd9Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
6387c478bd9Sstevel@tonic-gate uintptr_t new_pc)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
6417c478bd9Sstevel@tonic-gate fasttrap_bucket_t *bucket;
6427c478bd9Sstevel@tonic-gate fasttrap_id_t *id;
6437c478bd9Sstevel@tonic-gate kmutex_t *pid_mtx;
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
6467c478bd9Sstevel@tonic-gate mutex_enter(pid_mtx);
6477c478bd9Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6507c478bd9Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
651038dc6b3Sahl tp->ftt_proc->ftpc_acount != 0)
6527c478bd9Sstevel@tonic-gate break;
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate /*
6567c478bd9Sstevel@tonic-gate * Don't sweat it if we can't find the tracepoint again; unlike
6577c478bd9Sstevel@tonic-gate * when we're in fasttrap_pid_probe(), finding the tracepoint here
6587c478bd9Sstevel@tonic-gate * is not essential to the correct execution of the process.
6597c478bd9Sstevel@tonic-gate */
6607c478bd9Sstevel@tonic-gate if (tp == NULL) {
6617c478bd9Sstevel@tonic-gate mutex_exit(pid_mtx);
6627c478bd9Sstevel@tonic-gate return;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate * If there's a branch that could act as a return site, we
6687c478bd9Sstevel@tonic-gate * need to trace it, and check here if the program counter is
6697c478bd9Sstevel@tonic-gate * external to the function.
6707c478bd9Sstevel@tonic-gate */
6717c478bd9Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_RET &&
6727c478bd9Sstevel@tonic-gate tp->ftt_type != FASTTRAP_T_RET16 &&
6737c478bd9Sstevel@tonic-gate new_pc - id->fti_probe->ftp_faddr <
6747c478bd9Sstevel@tonic-gate id->fti_probe->ftp_fsize)
6757c478bd9Sstevel@tonic-gate continue;
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate dtrace_probe(id->fti_probe->ftp_id,
6787c478bd9Sstevel@tonic-gate pc - id->fti_probe->ftp_faddr,
6797c478bd9Sstevel@tonic-gate rp->r_r0, rp->r_r1, 0, 0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate
6827c478bd9Sstevel@tonic-gate mutex_exit(pid_mtx);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate
6857c478bd9Sstevel@tonic-gate static void
fasttrap_sigsegv(proc_t * p,kthread_t * t,uintptr_t addr)6867c478bd9Sstevel@tonic-gate fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate sqp->sq_info.si_signo = SIGSEGV;
6917c478bd9Sstevel@tonic-gate sqp->sq_info.si_code = SEGV_MAPERR;
6927c478bd9Sstevel@tonic-gate sqp->sq_info.si_addr = (caddr_t)addr;
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
6957c478bd9Sstevel@tonic-gate sigaddqa(p, t, sqp);
6967c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate if (t != NULL)
6997c478bd9Sstevel@tonic-gate aston(t);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate static void
fasttrap_usdt_args64(fasttrap_probe_t * probe,struct regs * rp,int argc,uintptr_t * argv)7037c478bd9Sstevel@tonic-gate fasttrap_usdt_args64(fasttrap_probe_t *probe, struct regs *rp, int argc,
7047c478bd9Sstevel@tonic-gate uintptr_t *argv)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs);
7077c478bd9Sstevel@tonic-gate uintptr_t *stack = (uintptr_t *)rp->r_sp;
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate for (i = 0; i < cap; i++) {
7107c478bd9Sstevel@tonic-gate x = probe->ftp_argmap[i];
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate if (x < 6)
7137c478bd9Sstevel@tonic-gate argv[i] = (&rp->r_rdi)[x];
7147c478bd9Sstevel@tonic-gate else
7157c478bd9Sstevel@tonic-gate argv[i] = fasttrap_fulword_noerr(&stack[x]);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate for (; i < argc; i++) {
7197c478bd9Sstevel@tonic-gate argv[i] = 0;
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate static void
fasttrap_usdt_args32(fasttrap_probe_t * probe,struct regs * rp,int argc,uint32_t * argv)7247c478bd9Sstevel@tonic-gate fasttrap_usdt_args32(fasttrap_probe_t *probe, struct regs *rp, int argc,
7257c478bd9Sstevel@tonic-gate uint32_t *argv)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs);
7287c478bd9Sstevel@tonic-gate uint32_t *stack = (uint32_t *)rp->r_sp;
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate for (i = 0; i < cap; i++) {
7317c478bd9Sstevel@tonic-gate x = probe->ftp_argmap[i];
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate argv[i] = fasttrap_fuword32_noerr(&stack[x]);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate
7367c478bd9Sstevel@tonic-gate for (; i < argc; i++) {
7377c478bd9Sstevel@tonic-gate argv[i] = 0;
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate
7419acbbeafSnn static int
fasttrap_do_seg(fasttrap_tracepoint_t * tp,struct regs * rp,uintptr_t * addr)7429acbbeafSnn fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct regs *rp, uintptr_t *addr)
7439acbbeafSnn {
7449acbbeafSnn proc_t *p = curproc;
7459acbbeafSnn user_desc_t *desc;
7469acbbeafSnn uint16_t sel, ndx, type;
7479acbbeafSnn uintptr_t limit;
7489acbbeafSnn
7499acbbeafSnn switch (tp->ftt_segment) {
7509acbbeafSnn case FASTTRAP_SEG_CS:
7519acbbeafSnn sel = rp->r_cs;
7529acbbeafSnn break;
7539acbbeafSnn case FASTTRAP_SEG_DS:
7549acbbeafSnn sel = rp->r_ds;
7559acbbeafSnn break;
7569acbbeafSnn case FASTTRAP_SEG_ES:
7579acbbeafSnn sel = rp->r_es;
7589acbbeafSnn break;
7599acbbeafSnn case FASTTRAP_SEG_FS:
7609acbbeafSnn sel = rp->r_fs;
7619acbbeafSnn break;
7629acbbeafSnn case FASTTRAP_SEG_GS:
7639acbbeafSnn sel = rp->r_gs;
7649acbbeafSnn break;
7659acbbeafSnn case FASTTRAP_SEG_SS:
7669acbbeafSnn sel = rp->r_ss;
7679acbbeafSnn break;
7689acbbeafSnn }
7699acbbeafSnn
7709acbbeafSnn /*
7719acbbeafSnn * Make sure the given segment register specifies a user priority
7729acbbeafSnn * selector rather than a kernel selector.
7739acbbeafSnn */
7749acbbeafSnn if (!SELISUPL(sel))
7759acbbeafSnn return (-1);
7769acbbeafSnn
7779acbbeafSnn ndx = SELTOIDX(sel);
7789acbbeafSnn
7799acbbeafSnn /*
7809acbbeafSnn * Check the bounds and grab the descriptor out of the specified
7819acbbeafSnn * descriptor table.
7829acbbeafSnn */
7839acbbeafSnn if (SELISLDT(sel)) {
7849acbbeafSnn if (ndx > p->p_ldtlimit)
7859acbbeafSnn return (-1);
7869acbbeafSnn
7879acbbeafSnn desc = p->p_ldt + ndx;
7889acbbeafSnn
7899acbbeafSnn } else {
7909acbbeafSnn if (ndx >= NGDT)
7919acbbeafSnn return (-1);
7929acbbeafSnn
7939acbbeafSnn desc = cpu_get_gdt() + ndx;
7949acbbeafSnn }
7959acbbeafSnn
7969acbbeafSnn /*
7979acbbeafSnn * The descriptor must have user privilege level and it must be
7989acbbeafSnn * present in memory.
7999acbbeafSnn */
8009acbbeafSnn if (desc->usd_dpl != SEL_UPL || desc->usd_p != 1)
8019acbbeafSnn return (-1);
8029acbbeafSnn
8039acbbeafSnn type = desc->usd_type;
8049acbbeafSnn
8059acbbeafSnn /*
8069acbbeafSnn * If the S bit in the type field is not set, this descriptor can
8079acbbeafSnn * only be used in system context.
8089acbbeafSnn */
8099acbbeafSnn if ((type & 0x10) != 0x10)
8109acbbeafSnn return (-1);
8119acbbeafSnn
8129acbbeafSnn limit = USEGD_GETLIMIT(desc) * (desc->usd_gran ? PAGESIZE : 1);
8139acbbeafSnn
8149acbbeafSnn if (tp->ftt_segment == FASTTRAP_SEG_CS) {
8159acbbeafSnn /*
8169acbbeafSnn * The code/data bit and readable bit must both be set.
8179acbbeafSnn */
8189acbbeafSnn if ((type & 0xa) != 0xa)
8199acbbeafSnn return (-1);
820