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/i386/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
110 * Lossless User-Land Tracing on x86
111 * ---------------------------------
113 * The execution of most instructions is not dependent on the address; for
114 * these instructions it is sufficient to copy them into the user process's
115 * address space and execute them. To effectively single-step an instruction
116 * in user-land, we copy out the following sequence of instructions to scratch
117 * space in the user thread's ulwp_t structure.
119 * We then set the program counter (%eip or %rip) to point to this scratch
120 * space. Once execution resumes, the original instruction is executed and
121 * then control flow is redirected to what was originally the subsequent
122 * instruction. If the kernel attemps to deliver a signal while single-
123 * stepping, the signal is deferred and the program counter is moved into the
124 * second sequence of instructions. The second sequence ends in a trap into
125 * the kernel where the deferred signal is then properly handled and delivered.
127 * For instructions whose execute is position dependent, we perform simple
128 * emulation. These instructions are limited to control transfer
129 * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
130 * of %rip-relative addressing that means that almost any instruction can be
131 * position dependent. For all the details on how we emulate generic
132 * instructions included %rip-relative instructions, see the code in
133 * fasttrap_pid_probe() below where we handle instructions of type
134 * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
137 #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
138 #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
139 #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
140 #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
142 #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
143 #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
144 #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
146 #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
147 #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
148 #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
149 #define FASTTRAP_REX_B(rex) ((rex) & 1)
150 #define FASTTRAP_REX(w, r, x, b) \
151 (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
154 * Single-byte op-codes.
156 #define FASTTRAP_PUSHL_EBP 0x55
158 #define FASTTRAP_JO 0x70
159 #define FASTTRAP_JNO 0x71
160 #define FASTTRAP_JB 0x72
161 #define FASTTRAP_JAE 0x73
162 #define FASTTRAP_JE 0x74
163 #define FASTTRAP_JNE 0x75
164 #define FASTTRAP_JBE 0x76
165 #define FASTTRAP_JA 0x77
166 #define FASTTRAP_JS 0x78
167 #define FASTTRAP_JNS 0x79
168 #define FASTTRAP_JP 0x7a
169 #define FASTTRAP_JNP 0x7b
170 #define FASTTRAP_JL 0x7c
171 #define FASTTRAP_JGE 0x7d
172 #define FASTTRAP_JLE 0x7e
173 #define FASTTRAP_JG 0x7f
175 #define FASTTRAP_NOP 0x90
177 #define FASTTRAP_MOV_EAX 0xb8
178 #define FASTTRAP_MOV_ECX 0xb9
180 #define FASTTRAP_RET16 0xc2
181 #define FASTTRAP_RET 0xc3
183 #define FASTTRAP_LOOPNZ 0xe0
184 #define FASTTRAP_LOOPZ 0xe1
185 #define FASTTRAP_LOOP 0xe2
186 #define FASTTRAP_JCXZ 0xe3
188 #define FASTTRAP_CALL 0xe8
189 #define FASTTRAP_JMP32 0xe9
190 #define FASTTRAP_JMP8 0xeb
192 #define FASTTRAP_INT3 0xcc
193 #define FASTTRAP_INT 0xcd
195 #define FASTTRAP_2_BYTE_OP 0x0f
196 #define FASTTRAP_GROUP5_OP 0xff
199 * Two-byte op-codes (second byte only).
201 #define FASTTRAP_0F_JO 0x80
202 #define FASTTRAP_0F_JNO 0x81
203 #define FASTTRAP_0F_JB 0x82
204 #define FASTTRAP_0F_JAE 0x83
205 #define FASTTRAP_0F_JE 0x84
206 #define FASTTRAP_0F_JNE 0x85
207 #define FASTTRAP_0F_JBE 0x86
208 #define FASTTRAP_0F_JA 0x87
209 #define FASTTRAP_0F_JS 0x88
210 #define FASTTRAP_0F_JNS 0x89
211 #define FASTTRAP_0F_JP 0x8a
212 #define FASTTRAP_0F_JNP 0x8b
213 #define FASTTRAP_0F_JL 0x8c
214 #define FASTTRAP_0F_JGE 0x8d
215 #define FASTTRAP_0F_JLE 0x8e
216 #define FASTTRAP_0F_JG 0x8f
218 #define FASTTRAP_EFLAGS_OF 0x800
219 #define FASTTRAP_EFLAGS_DF 0x400
220 #define FASTTRAP_EFLAGS_SF 0x080
221 #define FASTTRAP_EFLAGS_ZF 0x040
222 #define FASTTRAP_EFLAGS_AF 0x010
223 #define FASTTRAP_EFLAGS_PF 0x004
224 #define FASTTRAP_EFLAGS_CF 0x001
227 * Instruction prefixes.
229 #define FASTTRAP_PREFIX_OPERAND 0x66
230 #define FASTTRAP_PREFIX_ADDRESS 0x67
231 #define FASTTRAP_PREFIX_CS 0x2E
232 #define FASTTRAP_PREFIX_DS 0x3E
233 #define FASTTRAP_PREFIX_ES 0x26
234 #define FASTTRAP_PREFIX_FS 0x64
235 #define FASTTRAP_PREFIX_GS 0x65
236 #define FASTTRAP_PREFIX_SS 0x36
237 #define FASTTRAP_PREFIX_LOCK 0xF0
238 #define FASTTRAP_PREFIX_REP 0xF3
239 #define FASTTRAP_PREFIX_REPNE 0xF2
241 #define FASTTRAP_NOREG 0xff
244 * Map between instruction register encodings and the kernel constants which
245 * correspond to indicies into struct regs.
248 static const uint8_t regmap[16] = {
249 REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
250 REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
253 static const uint8_t regmap[8] = {
254 EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
258 static ulong_t fasttrap_getreg(struct reg *, uint_t);
261 fasttrap_anarg(struct reg *rp, int function_entry, int argno)
264 int shift = function_entry ? 1 : 0;
267 if (curproc->p_model == DATAMODEL_LP64) {
271 * In 64-bit mode, the first six arguments are stored in
275 return ((&rp->r_rdi)[argno]);
277 stack = (uintptr_t *)rp->r_rsp;
278 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
279 value = dtrace_fulword(&stack[argno - 6 + shift]);
280 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
284 uint32_t *stack = (uint32_t *)rp->r_esp;
285 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
286 value = dtrace_fuword32(&stack[argno + shift]);
287 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
298 fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
299 fasttrap_probe_type_t type)
301 uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
302 size_t len = FASTTRAP_MAX_INSTR_SIZE;
303 size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET));
306 uint8_t seg, rex = 0;
309 * Read the instruction at the given address out of the process's
310 * address space. We don't have to worry about a debugger
311 * changing this instruction before we overwrite it with our trap
312 * instruction since P_PR_LOCK is set. Since instructions can span
313 * pages, we potentially read the instruction in two parts. If the
314 * second part fails, we just zero out that part of the instruction.
316 if (uread(p, &instr[0], first, pc) != 0)
319 uread(p, &instr[first], len - first, pc + first) != 0) {
320 bzero(&instr[first], len - first);
325 * If the disassembly fails, then we have a malformed instruction.
327 if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0)
331 * Make sure the disassembler isn't completely broken.
333 ASSERT(-1 <= rmindex && rmindex < size);
336 * If the computed size is greater than the number of bytes read,
337 * then it was a malformed instruction possibly because it fell on a
338 * page boundary and the subsequent page was missing or because of
339 * some malicious user.
344 tp->ftt_size = (uint8_t)size;
345 tp->ftt_segment = FASTTRAP_SEG_NONE;
348 * Find the start of the instruction's opcode by processing any
353 switch (instr[start]) {
354 case FASTTRAP_PREFIX_SS:
357 case FASTTRAP_PREFIX_GS:
360 case FASTTRAP_PREFIX_FS:
363 case FASTTRAP_PREFIX_ES:
366 case FASTTRAP_PREFIX_DS:
369 case FASTTRAP_PREFIX_CS:
372 case FASTTRAP_PREFIX_OPERAND:
373 case FASTTRAP_PREFIX_ADDRESS:
374 case FASTTRAP_PREFIX_LOCK:
375 case FASTTRAP_PREFIX_REP:
376 case FASTTRAP_PREFIX_REPNE:
379 * It's illegal for an instruction to specify
380 * two segment prefixes -- give up on this
381 * illegal instruction.
383 if (tp->ftt_segment != FASTTRAP_SEG_NONE)
386 tp->ftt_segment = seg;
396 * Identify the REX prefix on 64-bit processes.
398 if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
399 rex = instr[start++];
403 * Now that we're pretty sure that the instruction is okay, copy the
404 * valid part to the tracepoint.
406 bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
408 tp->ftt_type = FASTTRAP_T_COMMON;
409 if (instr[start] == FASTTRAP_2_BYTE_OP) {
410 switch (instr[start + 1]) {
412 case FASTTRAP_0F_JNO:
414 case FASTTRAP_0F_JAE:
416 case FASTTRAP_0F_JNE:
417 case FASTTRAP_0F_JBE:
420 case FASTTRAP_0F_JNS:
422 case FASTTRAP_0F_JNP:
424 case FASTTRAP_0F_JGE:
425 case FASTTRAP_0F_JLE:
427 tp->ftt_type = FASTTRAP_T_JCC;
428 tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
429 tp->ftt_dest = pc + tp->ftt_size +
430 /* LINTED - alignment */
431 *(int32_t *)&instr[start + 2];
434 } else if (instr[start] == FASTTRAP_GROUP5_OP) {
435 uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
436 uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
437 uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
439 if (reg == 2 || reg == 4) {
443 tp->ftt_type = FASTTRAP_T_CALL;
445 tp->ftt_type = FASTTRAP_T_JMP;
452 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
455 * See AMD x86-64 Architecture Programmer's Manual
456 * Volume 3, Section 1.2.7, Table 1-12, and
457 * Appendix A.3.1, Table A-15.
459 if (mod != 3 && rm == 4) {
460 uint8_t sib = instr[start + 2];
461 uint_t index = FASTTRAP_SIB_INDEX(sib);
462 uint_t base = FASTTRAP_SIB_BASE(sib);
464 tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
466 tp->ftt_index = (index == 4) ?
468 regmap[index | (FASTTRAP_REX_X(rex) << 3)];
469 tp->ftt_base = (mod == 0 && base == 5) ?
471 regmap[base | (FASTTRAP_REX_B(rex) << 3)];
474 sz = mod == 1 ? 1 : 4;
477 * In 64-bit mode, mod == 0 and r/m == 5
478 * denotes %rip-relative addressing; in 32-bit
479 * mode, the base register isn't used. In both
480 * modes, there is a 32-bit operand.
482 if (mod == 0 && rm == 5) {
484 if (p->p_model == DATAMODEL_LP64)
485 tp->ftt_base = REG_RIP;
488 tp->ftt_base = FASTTRAP_NOREG;
492 (FASTTRAP_REX_B(rex) << 3);
494 tp->ftt_base = regmap[base];
495 sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
497 tp->ftt_index = FASTTRAP_NOREG;
502 tp->ftt_dest = *(int8_t *)&instr[start + i];
503 } else if (sz == 4) {
504 /* LINTED - alignment */
505 tp->ftt_dest = *(int32_t *)&instr[start + i];
511 switch (instr[start]) {
513 tp->ftt_type = FASTTRAP_T_RET;
517 tp->ftt_type = FASTTRAP_T_RET16;
518 /* LINTED - alignment */
519 tp->ftt_dest = *(uint16_t *)&instr[start + 1];
538 tp->ftt_type = FASTTRAP_T_JCC;
539 tp->ftt_code = instr[start];
540 tp->ftt_dest = pc + tp->ftt_size +
541 (int8_t)instr[start + 1];
544 case FASTTRAP_LOOPNZ:
547 tp->ftt_type = FASTTRAP_T_LOOP;
548 tp->ftt_code = instr[start];
549 tp->ftt_dest = pc + tp->ftt_size +
550 (int8_t)instr[start + 1];
554 tp->ftt_type = FASTTRAP_T_JCXZ;
555 tp->ftt_dest = pc + tp->ftt_size +
556 (int8_t)instr[start + 1];
560 tp->ftt_type = FASTTRAP_T_CALL;
561 tp->ftt_dest = pc + tp->ftt_size +
562 /* LINTED - alignment */
563 *(int32_t *)&instr[start + 1];
568 tp->ftt_type = FASTTRAP_T_JMP;
569 tp->ftt_dest = pc + tp->ftt_size +
570 /* LINTED - alignment */
571 *(int32_t *)&instr[start + 1];
574 tp->ftt_type = FASTTRAP_T_JMP;
575 tp->ftt_dest = pc + tp->ftt_size +
576 (int8_t)instr[start + 1];
579 case FASTTRAP_PUSHL_EBP:
581 tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
586 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0);
589 * On amd64 we have to be careful not to confuse a nop
590 * (actually xchgl %eax, %eax) with an instruction using
591 * the same opcode, but that does something different
592 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
594 if (FASTTRAP_REX_B(rex) == 0)
596 tp->ftt_type = FASTTRAP_T_NOP;
601 * The pid provider shares the int3 trap with debugger
602 * breakpoints so we can't instrument them.
604 ASSERT(instr[start] == FASTTRAP_INSTR);
609 * Interrupts seem like they could be traced with
610 * no negative implications, but it's possible that
611 * a thread could be redirected by the trap handling
612 * code which would eventually return to the
613 * instruction after the interrupt. If the interrupt
614 * were in our scratch space, the subsequent
615 * instruction might be overwritten before we return.
616 * Accordingly we refuse to instrument any interrupt.
623 if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
625 * If the process is 64-bit and the instruction type is still
626 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
627 * execute it -- we need to watch for %rip-relative
628 * addressing mode. See the portion of fasttrap_pid_probe()
629 * below where we handle tracepoints with type
630 * FASTTRAP_T_COMMON for how we emulate instructions that
631 * employ %rip-relative addressing.
634 uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
635 uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
636 uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
638 ASSERT(rmindex > start);
640 if (mod == 0 && rm == 5) {
642 * We need to be sure to avoid other
643 * registers used by this instruction. While
644 * the reg field may determine the op code
645 * rather than denoting a register, assuming
646 * that it denotes a register is always safe.
647 * We leave the REX field intact and use
648 * whatever value's there for simplicity.
651 tp->ftt_ripmode = FASTTRAP_RIP_1 |
653 FASTTRAP_REX_B(rex));
656 tp->ftt_ripmode = FASTTRAP_RIP_2 |
658 FASTTRAP_REX_B(rex));
662 tp->ftt_modrm = tp->ftt_instr[rmindex];
663 tp->ftt_instr[rmindex] =
664 FASTTRAP_MODRM(2, reg, rm);
674 fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
676 fasttrap_instr_t instr = FASTTRAP_INSTR;
678 if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
685 fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
690 * Distinguish between read or write failures and a changed
693 if (uread(p, &instr, 1, tp->ftt_pc) != 0)
695 if (instr != FASTTRAP_INSTR)
697 if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
705 fasttrap_fulword_noerr(const void *uaddr)
709 if ((ret = fasttrap_fulword(uaddr)) != -1)
718 fasttrap_fuword32_noerr(const void *uaddr)
722 if ((ret = fasttrap_fuword32(uaddr)) != -1)
730 fasttrap_return_common(struct reg *rp, uintptr_t pc, pid_t pid,
733 fasttrap_tracepoint_t *tp;
734 fasttrap_bucket_t *bucket;
741 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
742 mutex_enter(pid_mtx);
744 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
746 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
747 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
748 tp->ftt_proc->ftpc_acount != 0)
753 * Don't sweat it if we can't find the tracepoint again; unlike
754 * when we're in fasttrap_pid_probe(), finding the tracepoint here
755 * is not essential to the correct execution of the process.
764 for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
766 * If there's a branch that could act as a return site, we
767 * need to trace it, and check here if the program counter is
768 * external to the function.
770 if (tp->ftt_type != FASTTRAP_T_RET &&
771 tp->ftt_type != FASTTRAP_T_RET16 &&
772 new_pc - id->fti_probe->ftp_faddr <
773 id->fti_probe->ftp_fsize)
776 dtrace_probe(id->fti_probe->ftp_id,
777 pc - id->fti_probe->ftp_faddr,
778 rp->r_rax, rp->r_rbx, 0, 0);
787 fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr)
790 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
792 sqp->sq_info.si_signo = SIGSEGV;
793 sqp->sq_info.si_code = SEGV_MAPERR;
794 sqp->sq_info.si_addr = (caddr_t)addr;
796 mutex_enter(&p->p_lock);
798 mutex_exit(&p->p_lock);
803 ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP);
806 ksi->ksi_signo = SIGSEGV;
807 ksi->ksi_code = SEGV_MAPERR;
808 ksi->ksi_addr = (caddr_t)addr;
809 (void) tdksignal(t, SIGSEGV, ksi);
815 fasttrap_usdt_args64(fasttrap_probe_t *probe, struct reg *rp, int argc,
818 int i, x, cap = MIN(argc, probe->ftp_nargs);
819 uintptr_t *stack = (uintptr_t *)rp->r_rsp;
821 for (i = 0; i < cap; i++) {
822 x = probe->ftp_argmap[i];
825 argv[i] = (&rp->r_rdi)[x];
827 argv[i] = fasttrap_fulword_noerr(&stack[x]);
830 for (; i < argc; i++) {
838 fasttrap_usdt_args32(fasttrap_probe_t *probe, struct reg *rp, int argc,
841 int i, x, cap = MIN(argc, probe->ftp_nargs);
842 uint32_t *stack = (uint32_t *)rp->r_rsp;
844 for (i = 0; i < cap; i++) {
845 x = probe->ftp_argmap[i];
847 argv[i] = fasttrap_fuword32_noerr(&stack[x]);
850 for (; i < argc; i++) {
857 fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct reg *rp, uintptr_t *addr)
861 struct segment_descriptor *desc;
863 struct user_segment_descriptor *desc;
865 uint16_t sel = 0, ndx, type;
868 switch (tp->ftt_segment) {
869 case FASTTRAP_SEG_CS:
872 case FASTTRAP_SEG_DS:
875 case FASTTRAP_SEG_ES:
878 case FASTTRAP_SEG_FS:
881 case FASTTRAP_SEG_GS:
884 case FASTTRAP_SEG_SS:
890 * Make sure the given segment register specifies a user priority
891 * selector rather than a kernel selector.
893 if (ISPL(sel) != SEL_UPL)
899 * Check the bounds and grab the descriptor out of the specified
904 if (ndx > p->p_md.md_ldt->ldt_len)
907 desc = (struct segment_descriptor *)
908 p->p_md.md_ldt[ndx].ldt_base;
910 if (ndx > max_ldt_segment)
913 desc = (struct user_segment_descriptor *)
914 p->p_md.md_ldt[ndx].ldt_base;
929 * The descriptor must have user privilege level and it must be
932 if (desc->sd_dpl != SEL_UPL || desc->sd_p != 1)
935 type = desc->sd_type;
938 * If the S bit in the type field is not set, this descriptor can
939 * only be used in system context.
941 if ((type & 0x10) != 0x10)
944 limit = USD_GETLIMIT(desc) * (desc->sd_gran ? PAGESIZE : 1);
946 if (tp->ftt_segment == FASTTRAP_SEG_CS) {
948 * The code/data bit and readable bit must both be set.
950 if ((type & 0xa) != 0xa)
957 * The code/data bit must be clear.
959 if ((type & 0x8) != 0)
963 * If the expand-down bit is clear, we just check the limit as
964 * it would naturally be applied. Otherwise, we need to check
965 * that the address is the range [limit + 1 .. 0xffff] or
966 * [limit + 1 ... 0xffffffff] depending on if the default
967 * operand size bit is set.
969 if ((type & 0x4) == 0) {
972 } else if (desc->sd_def32) {
973 if (*addr < limit + 1 || 0xffff < *addr)
976 if (*addr < limit + 1 || 0xffffffff < *addr)
981 *addr += USD_GETBASE(desc);
987 fasttrap_pid_probe(struct reg *rp)
990 uintptr_t pc = rp->r_rip - 1;
991 uintptr_t new_pc = 0;
992 fasttrap_bucket_t *bucket;
996 fasttrap_tracepoint_t *tp, tp_local;
998 dtrace_icookie_t cookie;
999 uint_t is_enabled = 0;
1002 * It's possible that a user (in a veritable orgy of bad planning)
1003 * could redirect this thread's flow of control before it reached the
1004 * return probe fasttrap. In this case we need to kill the process
1005 * since it's in a unrecoverable state.
1007 if (curthread->t_dtrace_step) {
1008 ASSERT(curthread->t_dtrace_on);
1009 fasttrap_sigtrap(p, curthread, pc);
1014 * Clear all user tracing flags.
1016 curthread->t_dtrace_ft = 0;
1017 curthread->t_dtrace_pc = 0;
1018 curthread->t_dtrace_npc = 0;
1019 curthread->t_dtrace_scrpc = 0;
1020 curthread->t_dtrace_astpc = 0;
1022 curthread->t_dtrace_regv = 0;
1027 * Treat a child created by a call to vfork(2) as if it were its
1028 * parent. We know that there's only one thread of control in such a
1029 * process: this one.
1031 while (p->p_flag & SVFORK) {
1040 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
1041 mutex_enter(pid_mtx);
1043 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
1046 * Lookup the tracepoint that the process just hit.
1048 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
1049 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
1050 tp->ftt_proc->ftpc_acount != 0)
1055 * If we couldn't find a matching tracepoint, either a tracepoint has
1056 * been inserted without using the pid<pid> ioctl interface (see
1057 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
1061 mutex_exit(pid_mtx);
1069 * Set the program counter to the address of the traced instruction
1070 * so that it looks right in ustack() output.
1074 if (tp->ftt_ids != NULL) {
1078 if (p->p_model == DATAMODEL_LP64) {
1079 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1080 fasttrap_probe_t *probe = id->fti_probe;
1082 if (id->fti_ptype == DTFTP_ENTRY) {
1084 * We note that this was an entry
1085 * probe to help ustack() find the
1088 cookie = dtrace_interrupt_disable();
1089 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1090 dtrace_probe(probe->ftp_id, rp->r_rdi,
1091 rp->r_rsi, rp->r_rdx, rp->r_rcx,
1093 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1094 dtrace_interrupt_enable(cookie);
1095 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1097 * Note that in this case, we don't
1098 * call dtrace_probe() since it's only
1099 * an artificial probe meant to change
1100 * the flow of control so that it
1101 * encounters the true probe.
1104 } else if (probe->ftp_argmap == NULL) {
1105 dtrace_probe(probe->ftp_id, rp->r_rdi,
1106 rp->r_rsi, rp->r_rdx, rp->r_rcx,
1111 fasttrap_usdt_args64(probe, rp,
1112 sizeof (t) / sizeof (t[0]), t);
1114 dtrace_probe(probe->ftp_id, t[0], t[1],
1120 uintptr_t s0, s1, s2, s3, s4, s5;
1121 uint32_t *stack = (uint32_t *)rp->r_esp;
1124 * In 32-bit mode, all arguments are passed on the
1125 * stack. If this is a function entry probe, we need
1126 * to skip the first entry on the stack as it
1127 * represents the return address rather than a
1128 * parameter to the function.
1130 s0 = fasttrap_fuword32_noerr(&stack[0]);
1131 s1 = fasttrap_fuword32_noerr(&stack[1]);
1132 s2 = fasttrap_fuword32_noerr(&stack[2]);
1133 s3 = fasttrap_fuword32_noerr(&stack[3]);
1134 s4 = fasttrap_fuword32_noerr(&stack[4]);
1135 s5 = fasttrap_fuword32_noerr(&stack[5]);
1137 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1138 fasttrap_probe_t *probe = id->fti_probe;
1140 if (id->fti_ptype == DTFTP_ENTRY) {
1142 * We note that this was an entry
1143 * probe to help ustack() find the
1146 cookie = dtrace_interrupt_disable();
1147 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1148 dtrace_probe(probe->ftp_id, s1, s2,
1150 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1151 dtrace_interrupt_enable(cookie);
1152 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1154 * Note that in this case, we don't
1155 * call dtrace_probe() since it's only
1156 * an artificial probe meant to change
1157 * the flow of control so that it
1158 * encounters the true probe.
1161 } else if (probe->ftp_argmap == NULL) {
1162 dtrace_probe(probe->ftp_id, s0, s1,
1167 fasttrap_usdt_args32(probe, rp,
1168 sizeof (t) / sizeof (t[0]), t);
1170 dtrace_probe(probe->ftp_id, t[0], t[1],
1174 #endif /* __amd64 */
1181 * We're about to do a bunch of work so we cache a local copy of
1182 * the tracepoint to emulate the instruction, and then find the
1183 * tracepoint again later if we need to light up any return probes.
1188 mutex_exit(pid_mtx);
1193 * Set the program counter to appear as though the traced instruction
1194 * had completely executed. This ensures that fasttrap_getreg() will
1195 * report the expected value for REG_RIP.
1197 rp->r_rip = pc + tp->ftt_size;
1200 * If there's an is-enabled probe connected to this tracepoint it
1201 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1202 * instruction that was placed there by DTrace when the binary was
1203 * linked. As this probe is, in fact, enabled, we need to stuff 1
1204 * into %eax or %rax. Accordingly, we can bypass all the instruction
1205 * emulation logic since we know the inevitable result. It's possible
1206 * that a user could construct a scenario where the 'is-enabled'
1207 * probe was on some other instruction, but that would be a rather
1208 * exotic way to shoot oneself in the foot.
1217 * We emulate certain types of instructions to ensure correctness
1218 * (in the case of position dependent instructions) or optimize
1219 * common cases. The rest we have the thread execute back in user-
1222 switch (tp->ftt_type) {
1223 case FASTTRAP_T_RET:
1224 case FASTTRAP_T_RET16:
1231 * We have to emulate _every_ facet of the behavior of a ret
1232 * instruction including what happens if the load from %esp
1233 * fails; in that case, we send a SIGSEGV.
1236 if (p->p_model == DATAMODEL_NATIVE) {
1237 ret = dst = fasttrap_fulword((void *)rp->r_rsp);
1238 addr = rp->r_rsp + sizeof (uintptr_t);
1243 ret = dst32 = fasttrap_fuword32((void *)rp->r_esp);
1245 addr = rp->r_esp + sizeof (uint32_t);
1252 fasttrap_sigsegv(p, curthread, rp->r_rsp);
1257 if (tp->ftt_type == FASTTRAP_T_RET16)
1258 addr += tp->ftt_dest;
1265 case FASTTRAP_T_JCC:
1269 switch (tp->ftt_code) {
1271 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) != 0;
1274 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0;
1277 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0;
1280 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0;
1283 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0;
1286 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0;
1289 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0 ||
1290 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0;
1293 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0 &&
1294 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0;
1297 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) != 0;
1300 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0;
1303 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) != 0;
1306 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) == 0;
1309 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1310 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1313 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1314 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1317 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 ||
1318 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1319 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1322 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1323 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1324 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0);
1330 new_pc = tp->ftt_dest;
1332 new_pc = pc + tp->ftt_size;
1336 case FASTTRAP_T_LOOP:
1340 greg_t cx = rp->r_rcx--;
1342 greg_t cx = rp->r_ecx--;
1345 switch (tp->ftt_code) {
1346 case FASTTRAP_LOOPNZ:
1347 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1350 case FASTTRAP_LOOPZ:
1351 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 &&
1360 new_pc = tp->ftt_dest;
1362 new_pc = pc + tp->ftt_size;
1366 case FASTTRAP_T_JCXZ:
1369 greg_t cx = rp->r_rcx;
1371 greg_t cx = rp->r_ecx;
1375 new_pc = tp->ftt_dest;
1377 new_pc = pc + tp->ftt_size;
1381 case FASTTRAP_T_PUSHL_EBP:
1387 if (p->p_model == DATAMODEL_NATIVE) {
1388 addr = rp->r_rsp - sizeof (uintptr_t);
1389 ret = fasttrap_sulword((void *)addr, &rp->r_rsp);
1393 addr = rp->r_rsp - sizeof (uint32_t);
1394 ret = fasttrap_suword32((void *)addr, &rp->r_rsp);
1401 fasttrap_sigsegv(p, curthread, addr);
1407 new_pc = pc + tp->ftt_size;
1411 case FASTTRAP_T_NOP:
1412 new_pc = pc + tp->ftt_size;
1415 case FASTTRAP_T_JMP:
1416 case FASTTRAP_T_CALL:
1417 if (tp->ftt_code == 0) {
1418 new_pc = tp->ftt_dest;
1423 uintptr_t addr = tp->ftt_dest;
1425 if (tp->ftt_base != FASTTRAP_NOREG)
1426 addr += fasttrap_getreg(rp, tp->ftt_base);
1427 if (tp->ftt_index != FASTTRAP_NOREG)
1428 addr += fasttrap_getreg(rp, tp->ftt_index) <<
1431 if (tp->ftt_code == 1) {
1433 * If there's a segment prefix for this
1434 * instruction, we'll need to check permissions
1435 * and bounds on the given selector, and adjust
1436 * the address accordingly.
1438 if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
1439 fasttrap_do_seg(tp, rp, &addr) != 0) {
1440 fasttrap_sigsegv(p, curthread, addr);
1446 if (p->p_model == DATAMODEL_NATIVE) {
1447 if ((value = fasttrap_fulword((void *)addr))
1449 fasttrap_sigsegv(p, curthread,
1459 addr = (uintptr_t)(uint32_t)addr;
1460 if ((value32 = fasttrap_fuword32((void *)addr))
1462 fasttrap_sigsegv(p, curthread,
1478 * If this is a call instruction, we need to push the return
1479 * address onto the stack. If this fails, we send the process
1480 * a SIGSEGV and reset the pc to emulate what would happen if
1481 * this instruction weren't traced.
1483 if (tp->ftt_type == FASTTRAP_T_CALL) {
1485 uintptr_t addr = 0, pcps;
1487 if (p->p_model == DATAMODEL_NATIVE) {
1488 addr = rp->r_rsp - sizeof (uintptr_t);
1489 pcps = pc + tp->ftt_size;
1490 ret = fasttrap_sulword((void *)addr, &pcps);
1494 addr = rp->r_rsp - sizeof (uint32_t);
1495 pcps = (uint32_t)(pc + tp->ftt_size);
1496 ret = fasttrap_suword32((void *)addr, &pcps);
1503 fasttrap_sigsegv(p, curthread, addr);
1513 case FASTTRAP_T_COMMON:
1516 #if defined(__amd64)
1517 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22];
1519 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7];
1523 klwp_t *lwp = ttolwp(curthread);
1527 * Compute the address of the ulwp_t and step over the
1528 * ul_self pointer. The method used to store the user-land
1529 * thread pointer is very different on 32- and 64-bit
1533 #if defined(__amd64)
1534 if (p->p_model == DATAMODEL_LP64) {
1535 addr = lwp->lwp_pcb.pcb_fsbase;
1536 addr += sizeof (void *);
1538 addr = lwp->lwp_pcb.pcb_gsbase;
1539 addr += sizeof (caddr32_t);
1542 addr = USD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
1543 addr += sizeof (void *);
1547 addr = USD_GETBASE(&curthread->td_pcb->pcb_gsd);
1549 addr = curthread->td_pcb->pcb_gsbase;
1551 addr += sizeof (void *);
1554 * Generic Instruction Tracing
1555 * ---------------------------
1557 * This is the layout of the scratch space in the user-land
1558 * thread structure for our generated instructions.
1561 * ------------------------ -----
1562 * a: <original instruction> <= 15
1563 * jmp <pc + tp->ftt_size> 5
1564 * b: <original instruction> <= 15
1565 * int T_DTRACE_RET 2
1570 * ------------------------ -----
1571 * a: <original instruction> <= 15
1573 * <pc + tp->ftt_size> 8
1574 * b: <original instruction> <= 15
1575 * int T_DTRACE_RET 2
1579 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1580 * to b. If we encounter a signal on the way out of the
1581 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1582 * so that we execute the original instruction and re-enter
1583 * the kernel rather than redirecting to the next instruction.
1585 * If there are return probes (so we know that we're going to
1586 * need to reenter the kernel after executing the original
1587 * instruction), the scratch space will just contain the
1588 * original instruction followed by an interrupt -- the same
1591 * %rip-relative Addressing
1592 * ------------------------
1594 * There's a further complication in 64-bit mode due to %rip-
1595 * relative addressing. While this is clearly a beneficial
1596 * architectural decision for position independent code, it's
1597 * hard not to see it as a personal attack against the pid
1598 * provider since before there was a relatively small set of
1599 * instructions to emulate; with %rip-relative addressing,
1600 * almost every instruction can potentially depend on the
1601 * address at which it's executed. Rather than emulating
1602 * the broad spectrum of instructions that can now be
1603 * position dependent, we emulate jumps and others as in
1604 * 32-bit mode, and take a different tack for instructions
1605 * using %rip-relative addressing.
1607 * For every instruction that uses the ModRM byte, the
1608 * in-kernel disassembler reports its location. We use the
1609 * ModRM byte to identify that an instruction uses
1610 * %rip-relative addressing and to see what other registers
1611 * the instruction uses. To emulate those instructions,
1612 * we modify the instruction to be %rax-relative rather than
1613 * %rip-relative (or %rcx-relative if the instruction uses
1614 * %rax; or %r8- or %r9-relative if the REX.B is present so
1615 * we don't have to rewrite the REX prefix). We then load
1616 * the value that %rip would have been into the scratch
1617 * register and generate an instruction to reset the scratch
1618 * register back to its original value. The instruction
1619 * sequence looks like this:
1621 * 64-mode %rip-relative bytes
1622 * ------------------------ -----
1623 * a: <modified instruction> <= 15
1624 * movq $<value>, %<scratch> 6
1626 * <pc + tp->ftt_size> 8
1627 * b: <modified instruction> <= 15
1628 * int T_DTRACE_RET 2
1632 * We set curthread->t_dtrace_regv so that upon receiving
1633 * a signal we can reset the value of the scratch register.
1636 ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
1638 curthread->t_dtrace_scrpc = addr;
1639 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1643 if (tp->ftt_ripmode != 0) {
1646 ASSERT(p->p_model == DATAMODEL_LP64);
1647 ASSERT(tp->ftt_ripmode &
1648 (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
1651 * If this was a %rip-relative instruction, we change
1652 * it to be either a %rax- or %rcx-relative
1653 * instruction (depending on whether those registers
1654 * are used as another operand; or %r8- or %r9-
1655 * relative depending on the value of REX.B). We then
1656 * set that register and generate a movq instruction
1657 * to reset the value.
1659 if (tp->ftt_ripmode & FASTTRAP_RIP_X)
1660 scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
1662 scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
1664 if (tp->ftt_ripmode & FASTTRAP_RIP_1)
1665 scratch[i++] = FASTTRAP_MOV_EAX;
1667 scratch[i++] = FASTTRAP_MOV_ECX;
1669 switch (tp->ftt_ripmode) {
1670 case FASTTRAP_RIP_1:
1672 curthread->t_dtrace_reg = REG_RAX;
1674 case FASTTRAP_RIP_2:
1676 curthread->t_dtrace_reg = REG_RCX;
1678 case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
1680 curthread->t_dtrace_reg = REG_R8;
1682 case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
1684 curthread->t_dtrace_reg = REG_R9;
1688 /* LINTED - alignment */
1689 *(uint64_t *)&scratch[i] = *reg;
1690 curthread->t_dtrace_regv = *reg;
1691 *reg = pc + tp->ftt_size;
1692 i += sizeof (uint64_t);
1697 * Generate the branch instruction to what would have
1698 * normally been the subsequent instruction. In 32-bit mode,
1699 * this is just a relative branch; in 64-bit mode this is a
1700 * %rip-relative branch that loads the 64-bit pc value
1701 * immediately after the jmp instruction.
1704 if (p->p_model == DATAMODEL_LP64) {
1705 scratch[i++] = FASTTRAP_GROUP5_OP;
1706 scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
1707 /* LINTED - alignment */
1708 *(uint32_t *)&scratch[i] = 0;
1709 i += sizeof (uint32_t);
1710 /* LINTED - alignment */
1711 *(uint64_t *)&scratch[i] = pc + tp->ftt_size;
1712 i += sizeof (uint64_t);
1717 * Set up the jmp to the next instruction; note that
1718 * the size of the traced instruction cancels out.
1720 scratch[i++] = FASTTRAP_JMP32;
1721 /* LINTED - alignment */
1722 *(uint32_t *)&scratch[i] = pc - addr - 5;
1723 i += sizeof (uint32_t);
1729 curthread->t_dtrace_astpc = addr + i;
1730 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1732 scratch[i++] = FASTTRAP_INT;
1733 scratch[i++] = T_DTRACE_RET;
1735 ASSERT(i <= sizeof (scratch));
1738 if (fasttrap_copyout(scratch, (char *)addr, i)) {
1740 if (uwrite(curproc, scratch, i, addr)) {
1742 fasttrap_sigtrap(p, curthread, pc);
1746 if (tp->ftt_retids != NULL) {
1747 curthread->t_dtrace_step = 1;
1748 curthread->t_dtrace_ret = 1;
1749 new_pc = curthread->t_dtrace_astpc;
1751 new_pc = curthread->t_dtrace_scrpc;
1754 curthread->t_dtrace_pc = pc;
1755 curthread->t_dtrace_npc = pc + tp->ftt_size;
1756 curthread->t_dtrace_on = 1;
1761 panic("fasttrap: mishandled an instruction");
1766 * If there were no return probes when we first found the tracepoint,
1767 * we should feel no obligation to honor any return probes that were
1768 * subsequently enabled -- they'll just have to wait until the next
1771 if (tp->ftt_retids != NULL) {
1773 * We need to wait until the results of the instruction are
1774 * apparent before invoking any return probes. If this
1775 * instruction was emulated we can just call
1776 * fasttrap_return_common(); if it needs to be executed, we
1777 * need to wait until the user thread returns to the kernel.
1779 if (tp->ftt_type != FASTTRAP_T_COMMON) {
1781 * Set the program counter to the address of the traced
1782 * instruction so that it looks right in ustack()
1783 * output. We had previously set it to the end of the
1784 * instruction to simplify %rip-relative addressing.
1788 fasttrap_return_common(rp, pc, pid, new_pc);
1790 ASSERT(curthread->t_dtrace_ret != 0);
1791 ASSERT(curthread->t_dtrace_pc == pc);
1792 ASSERT(curthread->t_dtrace_scrpc != 0);
1793 ASSERT(new_pc == curthread->t_dtrace_astpc);
1800 proc_write_regs(curthread, rp);
1808 fasttrap_return_probe(struct reg *rp)
1810 proc_t *p = curproc;
1811 uintptr_t pc = curthread->t_dtrace_pc;
1812 uintptr_t npc = curthread->t_dtrace_npc;
1814 curthread->t_dtrace_pc = 0;
1815 curthread->t_dtrace_npc = 0;
1816 curthread->t_dtrace_scrpc = 0;
1817 curthread->t_dtrace_astpc = 0;
1821 * Treat a child created by a call to vfork(2) as if it were its
1822 * parent. We know that there's only one thread of control in such a
1823 * process: this one.
1825 while (p->p_flag & SVFORK) {
1831 * We set rp->r_rip to the address of the traced instruction so
1832 * that it appears to dtrace_probe() that we're on the original
1833 * instruction, and so that the user can't easily detect our
1834 * complex web of lies. dtrace_return_probe() (our caller)
1835 * will correctly set %pc after we return.
1839 fasttrap_return_common(rp, pc, p->p_pid, npc);
1846 fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1851 fill_regs(curthread, &r);
1853 return (fasttrap_anarg(&r, 1, argno));
1858 fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
1863 fill_regs(curthread, &r);
1865 return (fasttrap_anarg(&r, 0, argno));
1869 fasttrap_getreg(struct reg *rp, uint_t reg)
1873 case REG_R15: return (rp->r_r15);
1874 case REG_R14: return (rp->r_r14);
1875 case REG_R13: return (rp->r_r13);
1876 case REG_R12: return (rp->r_r12);
1877 case REG_R11: return (rp->r_r11);
1878 case REG_R10: return (rp->r_r10);
1879 case REG_R9: return (rp->r_r9);
1880 case REG_R8: return (rp->r_r8);
1881 case REG_RDI: return (rp->r_rdi);
1882 case REG_RSI: return (rp->r_rsi);
1883 case REG_RBP: return (rp->r_rbp);
1884 case REG_RBX: return (rp->r_rbx);
1885 case REG_RDX: return (rp->r_rdx);
1886 case REG_RCX: return (rp->r_rcx);
1887 case REG_RAX: return (rp->r_rax);
1888 case REG_TRAPNO: return (rp->r_trapno);
1889 case REG_ERR: return (rp->r_err);
1890 case REG_RIP: return (rp->r_rip);
1891 case REG_CS: return (rp->r_cs);
1893 case REG_RFL: return (rp->r_rfl);
1895 case REG_RSP: return (rp->r_rsp);
1896 case REG_SS: return (rp->r_ss);
1897 case REG_FS: return (rp->r_fs);
1898 case REG_GS: return (rp->r_gs);
1899 case REG_DS: return (rp->r_ds);
1900 case REG_ES: return (rp->r_es);
1901 case REG_FSBASE: return (rdmsr(MSR_FSBASE));
1902 case REG_GSBASE: return (rdmsr(MSR_GSBASE));
1905 panic("dtrace: illegal register constant");
1910 panic("dtrace: illegal register constant");
1912 return (((greg_t *)&rp->r_gs)[reg]);