4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Portions Copyright 2010 The FreeBSD Foundation
27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <sys/fasttrap_isa.h>
36 #include <sys/fasttrap_impl.h>
37 #include <sys/dtrace.h>
38 #include <sys/dtrace_impl.h>
39 #include <sys/cmn_err.h>
41 #include <sys/regset.h>
42 #include <sys/privregs.h>
43 #include <sys/segments.h>
44 #include <sys/x86_archext.h>
46 #include <cddl/dev/dtrace/dtrace_cddl.h>
47 #include <sys/types.h>
49 #include <sys/dtrace_bsd.h>
50 #include <cddl/dev/dtrace/x86/regset.h>
51 #include <machine/segments.h>
52 #include <machine/reg.h>
53 #include <machine/pcb.h>
55 #include <sys/sysmacros.h>
58 #include <sys/archsystm.h>
60 #include <sys/ptrace.h>
63 proc_ops(int op, proc_t *p, void *kaddr, off_t uaddr, size_t len)
70 uio.uio_offset = uaddr;
74 uio.uio_segflg = UIO_SYSSPACE;
75 uio.uio_td = curthread;
78 if (proc_rwmem(p, &uio) < 0) {
88 uread(proc_t *p, void *kaddr, size_t len, uintptr_t uaddr)
91 return (proc_ops(UIO_READ, p, kaddr, uaddr, len));
95 uwrite(proc_t *p, void *kaddr, size_t len, uintptr_t uaddr)
98 return (proc_ops(UIO_WRITE, p, kaddr, uaddr, len));
105 #define r_rflags r_eflags
111 * Lossless User-Land Tracing on x86
112 * ---------------------------------
114 * The execution of most instructions is not dependent on the address; for
115 * these instructions it is sufficient to copy them into the user process's
116 * address space and execute them. To effectively single-step an instruction
117 * in user-land, we copy out the following sequence of instructions to scratch
118 * space in the user thread's ulwp_t structure.
120 * We then set the program counter (%eip or %rip) to point to this scratch
121 * space. Once execution resumes, the original instruction is executed and
122 * then control flow is redirected to what was originally the subsequent
123 * instruction. If the kernel attemps to deliver a signal while single-
124 * stepping, the signal is deferred and the program counter is moved into the
125 * second sequence of instructions. The second sequence ends in a trap into
126 * the kernel where the deferred signal is then properly handled and delivered.
128 * For instructions whose execute is position dependent, we perform simple
129 * emulation. These instructions are limited to control transfer
130 * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
131 * of %rip-relative addressing that means that almost any instruction can be
132 * position dependent. For all the details on how we emulate generic
133 * instructions included %rip-relative instructions, see the code in
134 * fasttrap_pid_probe() below where we handle instructions of type
135 * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
138 #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
139 #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
140 #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
141 #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
143 #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
144 #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
145 #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
147 #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
148 #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
149 #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
150 #define FASTTRAP_REX_B(rex) ((rex) & 1)
151 #define FASTTRAP_REX(w, r, x, b) \
152 (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
155 * Single-byte op-codes.
157 #define FASTTRAP_PUSHL_EBP 0x55
159 #define FASTTRAP_JO 0x70
160 #define FASTTRAP_JNO 0x71
161 #define FASTTRAP_JB 0x72
162 #define FASTTRAP_JAE 0x73
163 #define FASTTRAP_JE 0x74
164 #define FASTTRAP_JNE 0x75
165 #define FASTTRAP_JBE 0x76
166 #define FASTTRAP_JA 0x77
167 #define FASTTRAP_JS 0x78
168 #define FASTTRAP_JNS 0x79
169 #define FASTTRAP_JP 0x7a
170 #define FASTTRAP_JNP 0x7b
171 #define FASTTRAP_JL 0x7c
172 #define FASTTRAP_JGE 0x7d
173 #define FASTTRAP_JLE 0x7e
174 #define FASTTRAP_JG 0x7f
176 #define FASTTRAP_NOP 0x90
178 #define FASTTRAP_MOV_EAX 0xb8
179 #define FASTTRAP_MOV_ECX 0xb9
181 #define FASTTRAP_RET16 0xc2
182 #define FASTTRAP_RET 0xc3
184 #define FASTTRAP_LOOPNZ 0xe0
185 #define FASTTRAP_LOOPZ 0xe1
186 #define FASTTRAP_LOOP 0xe2
187 #define FASTTRAP_JCXZ 0xe3
189 #define FASTTRAP_CALL 0xe8
190 #define FASTTRAP_JMP32 0xe9
191 #define FASTTRAP_JMP8 0xeb
193 #define FASTTRAP_INT3 0xcc
194 #define FASTTRAP_INT 0xcd
196 #define FASTTRAP_2_BYTE_OP 0x0f
197 #define FASTTRAP_GROUP5_OP 0xff
200 * Two-byte op-codes (second byte only).
202 #define FASTTRAP_0F_JO 0x80
203 #define FASTTRAP_0F_JNO 0x81
204 #define FASTTRAP_0F_JB 0x82
205 #define FASTTRAP_0F_JAE 0x83
206 #define FASTTRAP_0F_JE 0x84
207 #define FASTTRAP_0F_JNE 0x85
208 #define FASTTRAP_0F_JBE 0x86
209 #define FASTTRAP_0F_JA 0x87
210 #define FASTTRAP_0F_JS 0x88
211 #define FASTTRAP_0F_JNS 0x89
212 #define FASTTRAP_0F_JP 0x8a
213 #define FASTTRAP_0F_JNP 0x8b
214 #define FASTTRAP_0F_JL 0x8c
215 #define FASTTRAP_0F_JGE 0x8d
216 #define FASTTRAP_0F_JLE 0x8e
217 #define FASTTRAP_0F_JG 0x8f
219 #define FASTTRAP_EFLAGS_OF 0x800
220 #define FASTTRAP_EFLAGS_DF 0x400
221 #define FASTTRAP_EFLAGS_SF 0x080
222 #define FASTTRAP_EFLAGS_ZF 0x040
223 #define FASTTRAP_EFLAGS_AF 0x010
224 #define FASTTRAP_EFLAGS_PF 0x004
225 #define FASTTRAP_EFLAGS_CF 0x001
228 * Instruction prefixes.
230 #define FASTTRAP_PREFIX_OPERAND 0x66
231 #define FASTTRAP_PREFIX_ADDRESS 0x67
232 #define FASTTRAP_PREFIX_CS 0x2E
233 #define FASTTRAP_PREFIX_DS 0x3E
234 #define FASTTRAP_PREFIX_ES 0x26
235 #define FASTTRAP_PREFIX_FS 0x64
236 #define FASTTRAP_PREFIX_GS 0x65
237 #define FASTTRAP_PREFIX_SS 0x36
238 #define FASTTRAP_PREFIX_LOCK 0xF0
239 #define FASTTRAP_PREFIX_REP 0xF3
240 #define FASTTRAP_PREFIX_REPNE 0xF2
242 #define FASTTRAP_NOREG 0xff
245 * Map between instruction register encodings and the kernel constants which
246 * correspond to indicies into struct regs.
249 static const uint8_t regmap[16] = {
250 REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
251 REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
254 static const uint8_t regmap[8] = {
255 EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
259 static ulong_t fasttrap_getreg(struct reg *, uint_t);
262 fasttrap_anarg(struct reg *rp, int function_entry, int argno)
265 int shift = function_entry ? 1 : 0;
268 if (curproc->p_model == DATAMODEL_LP64) {
272 * In 64-bit mode, the first six arguments are stored in
276 return ((&rp->r_rdi)[argno]);
278 stack = (uintptr_t *)rp->r_rsp;
279 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
280 value = dtrace_fulword(&stack[argno - 6 + shift]);
281 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
285 uint32_t *stack = (uint32_t *)rp->r_esp;
286 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
287 value = dtrace_fuword32(&stack[argno + shift]);
288 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
299 fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
300 fasttrap_probe_type_t type)
302 uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
303 size_t len = FASTTRAP_MAX_INSTR_SIZE;
304 size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
307 uint8_t seg, rex = 0;
310 * Read the instruction at the given address out of the process's
311 * address space. We don't have to worry about a debugger
312 * changing this instruction before we overwrite it with our trap
313 * instruction since P_PR_LOCK is set. Since instructions can span
314 * pages, we potentially read the instruction in two parts. If the
315 * second part fails, we just zero out that part of the instruction.
317 if (uread(p, &instr[0], first, pc) != 0)
320 uread(p, &instr[first], len - first, pc + first) != 0) {
321 bzero(&instr[first], len - first);
326 * If the disassembly fails, then we have a malformed instruction.
328 if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0)
332 * Make sure the disassembler isn't completely broken.
334 ASSERT(-1 <= rmindex && rmindex < size);
337 * If the computed size is greater than the number of bytes read,
338 * then it was a malformed instruction possibly because it fell on a
339 * page boundary and the subsequent page was missing or because of
340 * some malicious user.
345 tp->ftt_size = (uint8_t)size;
346 tp->ftt_segment = FASTTRAP_SEG_NONE;
349 * Find the start of the instruction's opcode by processing any
354 switch (instr[start]) {
355 case FASTTRAP_PREFIX_SS:
358 case FASTTRAP_PREFIX_GS:
361 case FASTTRAP_PREFIX_FS:
364 case FASTTRAP_PREFIX_ES:
367 case FASTTRAP_PREFIX_DS:
370 case FASTTRAP_PREFIX_CS:
373 case FASTTRAP_PREFIX_OPERAND:
374 case FASTTRAP_PREFIX_ADDRESS:
375 case FASTTRAP_PREFIX_LOCK:
376 case FASTTRAP_PREFIX_REP:
377 case FASTTRAP_PREFIX_REPNE:
380 * It's illegal for an instruction to specify
381 * two segment prefixes -- give up on this
382 * illegal instruction.
384 if (tp->ftt_segment != FASTTRAP_SEG_NONE)
387 tp->ftt_segment = seg;
397 * Identify the REX prefix on 64-bit processes.
399 if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
400 rex = instr[start++];
404 * Now that we're pretty sure that the instruction is okay, copy the
405 * valid part to the tracepoint.
407 bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
409 tp->ftt_type = FASTTRAP_T_COMMON;
410 if (instr[start] == FASTTRAP_2_BYTE_OP) {
411 switch (instr[start + 1]) {
413 case FASTTRAP_0F_JNO:
415 case FASTTRAP_0F_JAE:
417 case FASTTRAP_0F_JNE:
418 case FASTTRAP_0F_JBE:
421 case FASTTRAP_0F_JNS:
423 case FASTTRAP_0F_JNP:
425 case FASTTRAP_0F_JGE:
426 case FASTTRAP_0F_JLE:
428 tp->ftt_type = FASTTRAP_T_JCC;
429 tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
430 tp->ftt_dest = pc + tp->ftt_size +
431 /* LINTED - alignment */
432 *(int32_t *)&instr[start + 2];
435 } else if (instr[start] == FASTTRAP_GROUP5_OP) {
436 uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
437 uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
438 uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
440 if (reg == 2 || reg == 4) {
444 tp->ftt_type = FASTTRAP_T_CALL;
446 tp->ftt_type = FASTTRAP_T_JMP;
453 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
456 * See AMD x86-64 Architecture Programmer's Manual
457 * Volume 3, Section 1.2.7, Table 1-12, and
458 * Appendix A.3.1, Table A-15.
460 if (mod != 3 && rm == 4) {
461 uint8_t sib = instr[start + 2];
462 uint_t index = FASTTRAP_SIB_INDEX(sib);
463 uint_t base = FASTTRAP_SIB_BASE(sib);
465 tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
467 tp->ftt_index = (index == 4) ?
469 regmap[index | (FASTTRAP_REX_X(rex) << 3)];
470 tp->ftt_base = (mod == 0 && base == 5) ?
472 regmap[base | (FASTTRAP_REX_B(rex) << 3)];
475 sz = mod == 1 ? 1 : 4;
478 * In 64-bit mode, mod == 0 and r/m == 5
479 * denotes %rip-relative addressing; in 32-bit
480 * mode, the base register isn't used. In both
481 * modes, there is a 32-bit operand.
483 if (mod == 0 && rm == 5) {
485 if (p->p_model == DATAMODEL_LP64)
486 tp->ftt_base = REG_RIP;
489 tp->ftt_base = FASTTRAP_NOREG;
493 (FASTTRAP_REX_B(rex) << 3);
495 tp->ftt_base = regmap[base];
496 sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
498 tp->ftt_index = FASTTRAP_NOREG;
503 tp->ftt_dest = *(int8_t *)&instr[start + i];
504 } else if (sz == 4) {
505 /* LINTED - alignment */
506 tp->ftt_dest = *(int32_t *)&instr[start + i];
512 switch (instr[start]) {
514 tp->ftt_type = FASTTRAP_T_RET;
518 tp->ftt_type = FASTTRAP_T_RET16;
519 /* LINTED - alignment */
520 tp->ftt_dest = *(uint16_t *)&instr[start + 1];
539 tp->ftt_type = FASTTRAP_T_JCC;
540 tp->ftt_code = instr[start];
541 tp->ftt_dest = pc + tp->ftt_size +
542 (int8_t)instr[start + 1];
545 case FASTTRAP_LOOPNZ:
548 tp->ftt_type = FASTTRAP_T_LOOP;
549 tp->ftt_code = instr[start];
550 tp->ftt_dest = pc + tp->ftt_size +
551 (int8_t)instr[start + 1];
555 tp->ftt_type = FASTTRAP_T_JCXZ;
556 tp->ftt_dest = pc + tp->ftt_size +
557 (int8_t)instr[start + 1];
561 tp->ftt_type = FASTTRAP_T_CALL;
562 tp->ftt_dest = pc + tp->ftt_size +
563 /* LINTED - alignment */
564 *(int32_t *)&instr[start + 1];
569 tp->ftt_type = FASTTRAP_T_JMP;
570 tp->ftt_dest = pc + tp->ftt_size +
571 /* LINTED - alignment */
572 *(int32_t *)&instr[start + 1];
575 tp->ftt_type = FASTTRAP_T_JMP;
576 tp->ftt_dest = pc + tp->ftt_size +
577 (int8_t)instr[start + 1];
580 case FASTTRAP_PUSHL_EBP:
582 tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
587 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
590 * On amd64 we have to be careful not to confuse a nop
591 * (actually xchgl %eax, %eax) with an instruction using
592 * the same opcode, but that does something different
593 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
595 if (FASTTRAP_REX_B(rex) == 0)
597 tp->ftt_type = FASTTRAP_T_NOP;
602 * The pid provider shares the int3 trap with debugger
603 * breakpoints so we can't instrument them.
605 ASSERT(instr[start] == FASTTRAP_INSTR);
610 * Interrupts seem like they could be traced with
611 * no negative implications, but it's possible that
612 * a thread could be redirected by the trap handling
613 * code which would eventually return to the
614 * instruction after the interrupt. If the interrupt
615 * were in our scratch space, the subsequent
616 * instruction might be overwritten before we return.
617 * Accordingly we refuse to instrument any interrupt.
624 if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
626 * If the process is 64-bit and the instruction type is still
627 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
628 * execute it -- we need to watch for %rip-relative
629 * addressing mode. See the portion of fasttrap_pid_probe()
630 * below where we handle tracepoints with type
631 * FASTTRAP_T_COMMON for how we emulate instructions that
632 * employ %rip-relative addressing.
635 uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
636 uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
637 uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
639 ASSERT(rmindex > start);
641 if (mod == 0 && rm == 5) {
643 * We need to be sure to avoid other
644 * registers used by this instruction. While
645 * the reg field may determine the op code
646 * rather than denoting a register, assuming
647 * that it denotes a register is always safe.
648 * We leave the REX field intact and use
649 * whatever value's there for simplicity.
652 tp->ftt_ripmode = FASTTRAP_RIP_1 |
654 FASTTRAP_REX_B(rex));
657 tp->ftt_ripmode = FASTTRAP_RIP_2 |
659 FASTTRAP_REX_B(rex));
663 tp->ftt_modrm = tp->ftt_instr[rmindex];
664 tp->ftt_instr[rmindex] =
665 FASTTRAP_MODRM(2, reg, rm);
675 fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
677 fasttrap_instr_t instr = FASTTRAP_INSTR;
679 if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
686 fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
691 * Distinguish between read or write failures and a changed
694 if (uread(p, &instr, 1, tp->ftt_pc) != 0)
696 if (instr != FASTTRAP_INSTR)
698 if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
706 fasttrap_fulword_noerr(const void *uaddr)
710 if ((ret = fasttrap_fulword(uaddr)) != -1)
719 fasttrap_fuword32_noerr(const void *uaddr)
723 if ((ret = fasttrap_fuword32(uaddr)) != -1)
731 fasttrap_return_common(struct reg *rp, uintptr_t pc, pid_t pid,
734 fasttrap_tracepoint_t *tp;
735 fasttrap_bucket_t *bucket;
742 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
743 mutex_enter(pid_mtx);
745 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
747 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
748 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
749 tp->ftt_proc->ftpc_acount != 0)
754 * Don't sweat it if we can't find the tracepoint again; unlike
755 * when we're in fasttrap_pid_probe(), finding the tracepoint here
756 * is not essential to the correct execution of the process.
765 for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
767 * If there's a branch that could act as a return site, we
768 * need to trace it, and check here if the program counter is
769 * external to the function.
771 if (tp->ftt_type != FASTTRAP_T_RET &&
772 tp->ftt_type != FASTTRAP_T_RET16 &&
773 new_pc - id->fti_probe->ftp_faddr <
774 id->fti_probe->ftp_fsize)
777 dtrace_probe(id->fti_probe->ftp_id,
778 pc - id->fti_probe->ftp_faddr,
779 rp->r_rax, rp->r_rbx, 0, 0);
788 fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
791 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
793 sqp->sq_info.si_signo = SIGSEGV;
794 sqp->sq_info.si_code = SEGV_MAPERR;
795 sqp->sq_info.si_addr = (caddr_t)addr;
797 mutex_enter(&p->p_lock);
799 mutex_exit(&p->p_lock);
804 ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP);
807 ksi->ksi_signo = SIGSEGV;
808 ksi->ksi_code = SEGV_MAPERR;
809 ksi->ksi_addr = (caddr_t)addr;
810 (void) tdksignal(t, SIGSEGV, ksi);
816 fasttrap_usdt_args64(fasttrap_probe_t *probe, struct reg *rp, int argc,
819 int i, x, cap = MIN(argc, probe->ftp_nargs);
820 uintptr_t *stack = (uintptr_t *)rp->r_rsp;
822 for (i = 0; i < cap; i++) {
823 x = probe->ftp_argmap[i];
826 argv[i] = (&rp->r_rdi)[x];
828 argv[i] = fasttrap_fulword_noerr(&stack[x]);
831 for (; i < argc; i++) {
839 fasttrap_usdt_args32(fasttrap_probe_t *probe, struct reg *rp, int argc,
842 int i, x, cap = MIN(argc, probe->ftp_nargs);
843 uint32_t *stack = (uint32_t *)rp->r_rsp;
845 for (i = 0; i < cap; i++) {
846 x = probe->ftp_argmap[i];
848 argv[i] = fasttrap_fuword32_noerr(&stack[x]);
851 for (; i < argc; i++) {
858 fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct reg *rp, uintptr_t *addr)
862 struct segment_descriptor *desc;
864 struct user_segment_descriptor *desc;
866 uint16_t sel = 0, ndx, type;
869 switch (tp->ftt_segment) {
870 case FASTTRAP_SEG_CS:
873 case FASTTRAP_SEG_DS:
876 case FASTTRAP_SEG_ES:
879 case FASTTRAP_SEG_FS:
882 case FASTTRAP_SEG_GS:
885 case FASTTRAP_SEG_SS:
891 * Make sure the given segment register specifies a user priority
892 * selector rather than a kernel selector.
894 if (ISPL(sel) != SEL_UPL)
900 * Check the bounds and grab the descriptor out of the specified
905 if (ndx > p->p_md.md_ldt->ldt_len)
908 desc = (struct segment_descriptor *)
909 p->p_md.md_ldt[ndx].ldt_base;
911 if (ndx > max_ldt_segment)
914 desc = (struct user_segment_descriptor *)
915 p->p_md.md_ldt[ndx].ldt_base;
930 * The descriptor must have user privilege level and it must be
933 if (desc->sd_dpl != SEL_UPL || desc->sd_p != 1)
936 type = desc->sd_type;
939 * If the S bit in the type field is not set, this descriptor can
940 * only be used in system context.
942 if ((type & 0x10) != 0x10)
945 limit = USD_GETLIMIT(desc) * (desc->sd_gran ? PAGESIZE : 1);
947 if (tp->ftt_segment == FASTTRAP_SEG_CS) {
949 * The code/data bit and readable bit must both be set.
951 if ((type & 0xa) != 0xa)
958 * The code/data bit must be clear.
960 if ((type & 0x8) != 0)
964 * If the expand-down bit is clear, we just check the limit as
965 * it would naturally be applied. Otherwise, we need to check
966 * that the address is the range [limit + 1 .. 0xffff] or
967 * [limit + 1 ... 0xffffffff] depending on if the default
968 * operand size bit is set.
970 if ((type & 0x4) == 0) {
973 } else if (desc->sd_def32) {
974 if (*addr < limit + 1 || 0xffff < *addr)
977 if (*addr < limit + 1 || 0xffffffff < *addr)
982 *addr += USD_GETBASE(desc);
988 fasttrap_pid_probe(struct reg *rp)
994 uintptr_t pc = rp->r_rip - 1;
995 uintptr_t new_pc = 0;
996 fasttrap_bucket_t *bucket;
1000 fasttrap_tracepoint_t *tp, tp_local;
1002 dtrace_icookie_t cookie;
1003 uint_t is_enabled = 0;
1006 * It's possible that a user (in a veritable orgy of bad planning)
1007 * could redirect this thread's flow of control before it reached the
1008 * return probe fasttrap. In this case we need to kill the process
1009 * since it's in a unrecoverable state.
1011 if (curthread->t_dtrace_step) {
1012 ASSERT(curthread->t_dtrace_on);
1013 fasttrap_sigtrap(p, curthread, pc);
1018 * Clear all user tracing flags.
1020 curthread->t_dtrace_ft = 0;
1021 curthread->t_dtrace_pc = 0;
1022 curthread->t_dtrace_npc = 0;
1023 curthread->t_dtrace_scrpc = 0;
1024 curthread->t_dtrace_astpc = 0;
1026 curthread->t_dtrace_regv = 0;
1030 * Treat a child created by a call to vfork(2) as if it were its
1031 * parent. We know that there's only one thread of control in such a
1032 * process: this one.
1035 while (p->p_flag & SVFORK) {
1040 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
1041 mutex_enter(pid_mtx);
1044 sx_slock(&proctree_lock);
1045 while (pp->p_vmspace == pp->p_pptr->p_vmspace)
1048 sx_sunlock(&proctree_lock);
1055 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
1058 * Lookup the tracepoint that the process just hit.
1060 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
1061 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
1062 tp->ftt_proc->ftpc_acount != 0)
1067 * If we couldn't find a matching tracepoint, either a tracepoint has
1068 * been inserted without using the pid<pid> ioctl interface (see
1069 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
1073 mutex_exit(pid_mtx);
1082 * Set the program counter to the address of the traced instruction
1083 * so that it looks right in ustack() output.
1087 if (tp->ftt_ids != NULL) {
1091 if (p->p_model == DATAMODEL_LP64) {
1092 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1093 fasttrap_probe_t *probe = id->fti_probe;
1095 if (id->fti_ptype == DTFTP_ENTRY) {
1097 * We note that this was an entry
1098 * probe to help ustack() find the
1101 cookie = dtrace_interrupt_disable();
1102 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1103 dtrace_probe(probe->ftp_id, rp->r_rdi,
1104 rp->r_rsi, rp->r_rdx, rp->r_rcx,
1106 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1107 dtrace_interrupt_enable(cookie);
1108 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1110 * Note that in this case, we don't
1111 * call dtrace_probe() since it's only
1112 * an artificial probe meant to change
1113 * the flow of control so that it
1114 * encounters the true probe.
1117 } else if (probe->ftp_argmap == NULL) {
1118 dtrace_probe(probe->ftp_id, rp->r_rdi,
1119 rp->r_rsi, rp->r_rdx, rp->r_rcx,
1124 fasttrap_usdt_args64(probe, rp,
1125 sizeof (t) / sizeof (t[0]), t);
1127 dtrace_probe(probe->ftp_id, t[0], t[1],
1133 uintptr_t s0, s1, s2, s3, s4, s5;
1134 uint32_t *stack = (uint32_t *)rp->r_esp;
1137 * In 32-bit mode, all arguments are passed on the
1138 * stack. If this is a function entry probe, we need
1139 * to skip the first entry on the stack as it
1140 * represents the return address rather than a
1141 * parameter to the function.
1143 s0 = fasttrap_fuword32_noerr(&stack[0]);
1144 s1 = fasttrap_fuword32_noerr(&stack[1]);
1145 s2 = fasttrap_fuword32_noerr(&stack[2]);
1146 s3 = fasttrap_fuword32_noerr(&stack[3]);
1147 s4 = fasttrap_fuword32_noerr(&stack[4]);
1148 s5 = fasttrap_fuword32_noerr(&stack[5]);
1150 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1151 fasttrap_probe_t *probe = id->fti_probe;
1153 if (id->fti_ptype == DTFTP_ENTRY) {
1155 * We note that this was an entry
1156 * probe to help ustack() find the
1159 cookie = dtrace_interrupt_disable();
1160 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1161 dtrace_probe(probe->ftp_id, s1, s2,
1163 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1164 dtrace_interrupt_enable(cookie);
1165 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1167 * Note that in this case, we don't
1168 * call dtrace_probe() since it's only
1169 * an artificial probe meant to change
1170 * the flow of control so that it
1171 * encounters the true probe.
1174 } else if (probe->ftp_argmap == NULL) {
1175 dtrace_probe(probe->ftp_id, s0, s1,
1180 fasttrap_usdt_args32(probe, rp,
1181 sizeof (t) / sizeof (t[0]), t);
1183 dtrace_probe(probe->ftp_id, t[0], t[1],
1187 #endif /* __amd64 */
1194 * We're about to do a bunch of work so we cache a local copy of
1195 * the tracepoint to emulate the instruction, and then find the
1196 * tracepoint again later if we need to light up any return probes.
1200 mutex_exit(pid_mtx);
1207 * Set the program counter to appear as though the traced instruction
1208 * had completely executed. This ensures that fasttrap_getreg() will
1209 * report the expected value for REG_RIP.
1211 rp->r_rip = pc + tp->ftt_size;
1214 * If there's an is-enabled probe connected to this tracepoint it
1215 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1216 * instruction that was placed there by DTrace when the binary was
1217 * linked. As this probe is, in fact, enabled, we need to stuff 1
1218 * into %eax or %rax. Accordingly, we can bypass all the instruction
1219 * emulation logic since we know the inevitable result. It's possible
1220 * that a user could construct a scenario where the 'is-enabled'
1221 * probe was on some other instruction, but that would be a rather
1222 * exotic way to shoot oneself in the foot.
1231 * We emulate certain types of instructions to ensure correctness
1232 * (in the case of position dependent instructions) or optimize
1233 * common cases. The rest we have the thread execute back in user-
1236 switch (tp->ftt_type) {
1237 case FASTTRAP_T_RET:
1238 case FASTTRAP_T_RET16:
1245 * We have to emulate _every_ facet of the behavior of a ret
1246 * instruction including what happens if the load from %esp
1247 * fails; in that case, we send a SIGSEGV.
1250 if (p->p_model == DATAMODEL_NATIVE) {
1251 ret = dst = fasttrap_fulword((void *)rp->r_rsp);
1252 addr = rp->r_rsp + sizeof (uintptr_t);
1257 ret = dst32 = fasttrap_fuword32((void *)rp->r_esp);
1259 addr = rp->r_esp + sizeof (uint32_t);
1266 fasttrap_sigsegv(p, curthread, rp->r_rsp);
1271 if (tp->ftt_type == FASTTRAP_T_RET16)
1272 addr += tp->ftt_dest;
1279 case FASTTRAP_T_JCC:
1283 switch (tp->ftt_code) {
1285 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) != 0;
1288 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0;
1291 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0;
1294 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0;
1297 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0;
1300 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0;
1303 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0 ||
1304 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0;
1307 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0 &&
1308 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0;
1311 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) != 0;
1314 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0;
1317 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) != 0;
1320 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) == 0;
1323 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1324 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1327 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1328 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1331 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 ||
1332 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1333 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1336 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1337 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1338 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1344 new_pc = tp->ftt_dest;
1346 new_pc = pc + tp->ftt_size;
1350 case FASTTRAP_T_LOOP:
1354 greg_t cx = rp->r_rcx--;
1356 greg_t cx = rp->r_ecx--;
1359 switch (tp->ftt_code) {
1360 case FASTTRAP_LOOPNZ:
1361 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1364 case FASTTRAP_LOOPZ:
1365 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 &&
1374 new_pc = tp->ftt_dest;
1376 new_pc = pc + tp->ftt_size;
1380 case FASTTRAP_T_JCXZ:
1383 greg_t cx = rp->r_rcx;
1385 greg_t cx = rp->r_ecx;
1389 new_pc = tp->ftt_dest;
1391 new_pc = pc + tp->ftt_size;
1395 case FASTTRAP_T_PUSHL_EBP:
1400 if (p->p_model == DATAMODEL_NATIVE) {
1401 rp->r_rsp -= sizeof (uintptr_t);
1402 ret = fasttrap_sulword((void *)rp->r_rsp, rp->r_rbp);
1406 rp->r_rsp -= sizeof (uint32_t);
1407 ret = fasttrap_suword32((void *)rp->r_rsp, rp->r_rbp);
1414 fasttrap_sigsegv(p, curthread, rp->r_rsp);
1419 new_pc = pc + tp->ftt_size;
1423 case FASTTRAP_T_NOP:
1424 new_pc = pc + tp->ftt_size;
1427 case FASTTRAP_T_JMP:
1428 case FASTTRAP_T_CALL:
1429 if (tp->ftt_code == 0) {
1430 new_pc = tp->ftt_dest;
1435 uintptr_t addr = tp->ftt_dest;
1437 if (tp->ftt_base != FASTTRAP_NOREG)
1438 addr += fasttrap_getreg(rp, tp->ftt_base);
1439 if (tp->ftt_index != FASTTRAP_NOREG)
1440 addr += fasttrap_getreg(rp, tp->ftt_index) <<
1443 if (tp->ftt_code == 1) {
1445 * If there's a segment prefix for this
1446 * instruction, we'll need to check permissions
1447 * and bounds on the given selector, and adjust
1448 * the address accordingly.
1450 if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
1451 fasttrap_do_seg(tp, rp, &addr) != 0) {
1452 fasttrap_sigsegv(p, curthread, addr);
1458 if (p->p_model == DATAMODEL_NATIVE) {
1459 if ((value = fasttrap_fulword((void *)addr))
1461 fasttrap_sigsegv(p, curthread,
1471 addr = (uintptr_t)(uint32_t)addr;
1472 if ((value32 = fasttrap_fuword32((void *)addr))
1474 fasttrap_sigsegv(p, curthread,
1490 * If this is a call instruction, we need to push the return
1491 * address onto the stack. If this fails, we send the process
1492 * a SIGSEGV and reset the pc to emulate what would happen if
1493 * this instruction weren't traced.
1495 if (tp->ftt_type == FASTTRAP_T_CALL) {
1497 uintptr_t addr = 0, pcps;
1499 if (p->p_model == DATAMODEL_NATIVE) {
1500 addr = rp->r_rsp - sizeof (uintptr_t);
1501 pcps = pc + tp->ftt_size;
1502 ret = fasttrap_sulword((void *)addr, pcps);
1506 addr = rp->r_rsp - sizeof (uint32_t);
1507 pcps = (uint32_t)(pc + tp->ftt_size);
1508 ret = fasttrap_suword32((void *)addr, pcps);
1515 fasttrap_sigsegv(p, curthread, addr);
1525 case FASTTRAP_T_COMMON:
1528 #if defined(__amd64)
1529 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22];
1531 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7];
1535 klwp_t *lwp = ttolwp(curthread);
1539 * Compute the address of the ulwp_t and step over the
1540 * ul_self pointer. The method used to store the user-land
1541 * thread pointer is very different on 32- and 64-bit
1545 #if defined(__amd64)
1546 if (p->p_model == DATAMODEL_LP64) {
1547 addr = lwp->lwp_pcb.pcb_fsbase;
1548 addr += sizeof (void *);
1550 addr = lwp->lwp_pcb.pcb_gsbase;
1551 addr += sizeof (caddr32_t);
1554 addr = USD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
1555 addr += sizeof (void *);
1559 addr = USD_GETBASE(&curthread->td_pcb->pcb_gsd);
1561 addr = curthread->td_pcb->pcb_gsbase;
1563 addr += sizeof (void *);
1566 * Generic Instruction Tracing
1567 * ---------------------------
1569 * This is the layout of the scratch space in the user-land
1570 * thread structure for our generated instructions.
1573 * ------------------------ -----
1574 * a: <original instruction> <= 15
1575 * jmp <pc + tp->ftt_size> 5
1576 * b: <original instruction> <= 15
1577 * int T_DTRACE_RET 2
1582 * ------------------------ -----
1583 * a: <original instruction> <= 15
1585 * <pc + tp->ftt_size> 8
1586 * b: <original instruction> <= 15
1587 * int T_DTRACE_RET 2
1591 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1592 * to b. If we encounter a signal on the way out of the
1593 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1594 * so that we execute the original instruction and re-enter
1595 * the kernel rather than redirecting to the next instruction.
1597 * If there are return probes (so we know that we're going to
1598 * need to reenter the kernel after executing the original
1599 * instruction), the scratch space will just contain the
1600 * original instruction followed by an interrupt -- the same
1603 * %rip-relative Addressing
1604 * ------------------------
1606 * There's a further complication in 64-bit mode due to %rip-
1607 * relative addressing. While this is clearly a beneficial
1608 * architectural decision for position independent code, it's
1609 * hard not to see it as a personal attack against the pid
1610 * provider since before there was a relatively small set of
1611 * instructions to emulate; with %rip-relative addressing,
1612 * almost every instruction can potentially depend on the
1613 * address at which it's executed. Rather than emulating
1614 * the broad spectrum of instructions that can now be
1615 * position dependent, we emulate jumps and others as in
1616 * 32-bit mode, and take a different tack for instructions
1617 * using %rip-relative addressing.
1619 * For every instruction that uses the ModRM byte, the
1620 * in-kernel disassembler reports its location. We use the
1621 * ModRM byte to identify that an instruction uses
1622 * %rip-relative addressing and to see what other registers
1623 * the instruction uses. To emulate those instructions,
1624 * we modify the instruction to be %rax-relative rather than
1625 * %rip-relative (or %rcx-relative if the instruction uses
1626 * %rax; or %r8- or %r9-relative if the REX.B is present so
1627 * we don't have to rewrite the REX prefix). We then load
1628 * the value that %rip would have been into the scratch
1629 * register and generate an instruction to reset the scratch
1630 * register back to its original value. The instruction
1631 * sequence looks like this:
1633 * 64-mode %rip-relative bytes
1634 * ------------------------ -----
1635 * a: <modified instruction> <= 15
1636 * movq $<value>, %<scratch> 6
1638 * <pc + tp->ftt_size> 8
1639 * b: <modified instruction> <= 15
1640 * int T_DTRACE_RET 2
1644 * We set curthread->t_dtrace_regv so that upon receiving
1645 * a signal we can reset the value of the scratch register.
1648 ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
1650 curthread->t_dtrace_scrpc = addr;
1651 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1655 if (tp->ftt_ripmode != 0) {
1658 ASSERT(p->p_model == DATAMODEL_LP64);
1659 ASSERT(tp->ftt_ripmode &
1660 (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
1663 * If this was a %rip-relative instruction, we change
1664 * it to be either a %rax- or %rcx-relative
1665 * instruction (depending on whether those registers
1666 * are used as another operand; or %r8- or %r9-
1667 * relative depending on the value of REX.B). We then
1668 * set that register and generate a movq instruction
1669 * to reset the value.
1671 if (tp->ftt_ripmode & FASTTRAP_RIP_X)
1672 scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
1674 scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
1676 if (tp->ftt_ripmode & FASTTRAP_RIP_1)
1677 scratch[i++] = FASTTRAP_MOV_EAX;
1679 scratch[i++] = FASTTRAP_MOV_ECX;
1681 switch (tp->ftt_ripmode) {
1682 case FASTTRAP_RIP_1:
1684 curthread->t_dtrace_reg = REG_RAX;
1686 case FASTTRAP_RIP_2:
1688 curthread->t_dtrace_reg = REG_RCX;
1690 case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
1692 curthread->t_dtrace_reg = REG_R8;
1694 case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
1696 curthread->t_dtrace_reg = REG_R9;
1700 /* LINTED - alignment */
1701 *(uint64_t *)&scratch[i] = *reg;
1702 curthread->t_dtrace_regv = *reg;
1703 *reg = pc + tp->ftt_size;
1704 i += sizeof (uint64_t);
1709 * Generate the branch instruction to what would have
1710 * normally been the subsequent instruction. In 32-bit mode,
1711 * this is just a relative branch; in 64-bit mode this is a
1712 * %rip-relative branch that loads the 64-bit pc value
1713 * immediately after the jmp instruction.
1716 if (p->p_model == DATAMODEL_LP64) {
1717 scratch[i++] = FASTTRAP_GROUP5_OP;
1718 scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
1719 /* LINTED - alignment */
1720 *(uint32_t *)&scratch[i] = 0;
1721 i += sizeof (uint32_t);
1722 /* LINTED - alignment */
1723 *(uint64_t *)&scratch[i] = pc + tp->ftt_size;
1724 i += sizeof (uint64_t);
1729 * Set up the jmp to the next instruction; note that
1730 * the size of the traced instruction cancels out.
1732 scratch[i++] = FASTTRAP_JMP32;
1733 /* LINTED - alignment */
1734 *(uint32_t *)&scratch[i] = pc - addr - 5;
1735 i += sizeof (uint32_t);
1741 curthread->t_dtrace_astpc = addr + i;
1742 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1744 scratch[i++] = FASTTRAP_INT;
1745 scratch[i++] = T_DTRACE_RET;
1747 ASSERT(i <= sizeof (scratch));
1750 if (fasttrap_copyout(scratch, (char *)addr, i)) {
1752 if (uwrite(p, scratch, i, addr)) {
1754 fasttrap_sigtrap(p, curthread, pc);
1758 if (tp->ftt_retids != NULL) {
1759 curthread->t_dtrace_step = 1;
1760 curthread->t_dtrace_ret = 1;
1761 new_pc = curthread->t_dtrace_astpc;
1763 new_pc = curthread->t_dtrace_scrpc;
1766 curthread->t_dtrace_pc = pc;
1767 curthread->t_dtrace_npc = pc + tp->ftt_size;
1768 curthread->t_dtrace_on = 1;
1773 panic("fasttrap: mishandled an instruction");
1778 * If there were no return probes when we first found the tracepoint,
1779 * we should feel no obligation to honor any return probes that were
1780 * subsequently enabled -- they'll just have to wait until the next
1783 if (tp->ftt_retids != NULL) {
1785 * We need to wait until the results of the instruction are
1786 * apparent before invoking any return probes. If this
1787 * instruction was emulated we can just call
1788 * fasttrap_return_common(); if it needs to be executed, we
1789 * need to wait until the user thread returns to the kernel.
1791 if (tp->ftt_type != FASTTRAP_T_COMMON) {
1793 * Set the program counter to the address of the traced
1794 * instruction so that it looks right in ustack()
1795 * output. We had previously set it to the end of the
1796 * instruction to simplify %rip-relative addressing.
1800 fasttrap_return_common(rp, pc, pid, new_pc);
1802 ASSERT(curthread->t_dtrace_ret != 0);
1803 ASSERT(curthread->t_dtrace_pc == pc);
1804 ASSERT(curthread->t_dtrace_scrpc != 0);
1805 ASSERT(new_pc == curthread->t_dtrace_astpc);
1813 proc_write_regs(curthread, rp);
1822 fasttrap_return_probe(struct reg *rp)
1824 proc_t *p = curproc;
1825 uintptr_t pc = curthread->t_dtrace_pc;
1826 uintptr_t npc = curthread->t_dtrace_npc;
1828 curthread->t_dtrace_pc = 0;
1829 curthread->t_dtrace_npc = 0;
1830 curthread->t_dtrace_scrpc = 0;
1831 curthread->t_dtrace_astpc = 0;
1835 * Treat a child created by a call to vfork(2) as if it were its
1836 * parent. We know that there's only one thread of control in such a
1837 * process: this one.
1839 while (p->p_flag & SVFORK) {
1845 * We set rp->r_rip to the address of the traced instruction so
1846 * that it appears to dtrace_probe() that we're on the original
1847 * instruction, and so that the user can't easily detect our
1848 * complex web of lies. dtrace_return_probe() (our caller)
1849 * will correctly set %pc after we return.
1853 fasttrap_return_common(rp, pc, p->p_pid, npc);
1860 fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1865 fill_regs(curthread, &r);
1867 return (fasttrap_anarg(&r, 1, argno));
1872 fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1877 fill_regs(curthread, &r);
1879 return (fasttrap_anarg(&r, 0, argno));
1883 fasttrap_getreg(struct reg *rp, uint_t reg)
1887 case REG_R15: return (rp->r_r15);
1888 case REG_R14: return (rp->r_r14);
1889 case REG_R13: return (rp->r_r13);
1890 case REG_R12: return (rp->r_r12);
1891 case REG_R11: return (rp->r_r11);
1892 case REG_R10: return (rp->r_r10);
1893 case REG_R9: return (rp->r_r9);
1894 case REG_R8: return (rp->r_r8);
1895 case REG_RDI: return (rp->r_rdi);
1896 case REG_RSI: return (rp->r_rsi);
1897 case REG_RBP: return (rp->r_rbp);
1898 case REG_RBX: return (rp->r_rbx);
1899 case REG_RDX: return (rp->r_rdx);
1900 case REG_RCX: return (rp->r_rcx);
1901 case REG_RAX: return (rp->r_rax);
1902 case REG_TRAPNO: return (rp->r_trapno);
1903 case REG_ERR: return (rp->r_err);
1904 case REG_RIP: return (rp->r_rip);
1905 case REG_CS: return (rp->r_cs);
1907 case REG_RFL: return (rp->r_rfl);
1909 case REG_RSP: return (rp->r_rsp);
1910 case REG_SS: return (rp->r_ss);
1911 case REG_FS: return (rp->r_fs);
1912 case REG_GS: return (rp->r_gs);
1913 case REG_DS: return (rp->r_ds);
1914 case REG_ES: return (rp->r_es);
1915 case REG_FSBASE: return (rdmsr(MSR_FSBASE));
1916 case REG_GSBASE: return (rdmsr(MSR_GSBASE));
1919 panic("dtrace: illegal register constant");
1924 panic("dtrace: illegal register constant");
1926 return (((greg_t *)&rp->r_gs)[reg]);