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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/fasttrap_isa.h>
30 #include <sys/fasttrap_impl.h>
31 #include <sys/dtrace.h>
32 #include <sys/dtrace_impl.h>
33 #include <sys/cmn_err.h>
34 #include <sys/regset.h>
35 #include <sys/privregs.h>
36 #include <sys/segments.h>
37 #include <sys/x86_archext.h>
38 #include <sys/sysmacros.h>
40 #include <sys/archsystm.h>
43 * Lossless User-Land Tracing on x86
44 * ---------------------------------
46 * The execution of most instructions is not dependent on the address; for
47 * these instructions it is sufficient to copy them into the user process's
48 * address space and execute them. To effectively single-step an instruction
49 * in user-land, we copy out the following sequence of instructions to scratch
50 * space in the user thread's ulwp_t structure.
52 * We then set the program counter (%eip or %rip) to point to this scratch
53 * space. Once execution resumes, the original instruction is executed and
54 * then control flow is redirected to what was originally the subsequent
55 * instruction. If the kernel attemps to deliver a signal while single-
56 * stepping, the signal is deferred and the program counter is moved into the
57 * second sequence of instructions. The second sequence ends in a trap into
58 * the kernel where the deferred signal is then properly handled and delivered.
60 * For instructions whose execute is position dependent, we perform simple
61 * emulation. These instructions are limited to control transfer
62 * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
63 * of %rip-relative addressing that means that almost any instruction can be
64 * position dependent. For all the details on how we emulate generic
65 * instructions included %rip-relative instructions, see the code in
66 * fasttrap_pid_probe() below where we handle instructions of type
67 * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
70 #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
71 #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
72 #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
73 #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
75 #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
76 #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
77 #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
79 #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
80 #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
81 #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
82 #define FASTTRAP_REX_B(rex) ((rex) & 1)
83 #define FASTTRAP_REX(w, r, x, b) \
84 (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
87 * Single-byte op-codes.
89 #define FASTTRAP_PUSHL_EBP 0x55
91 #define FASTTRAP_JO 0x70
92 #define FASTTRAP_JNO 0x71
93 #define FASTTRAP_JB 0x72
94 #define FASTTRAP_JAE 0x73
95 #define FASTTRAP_JE 0x74
96 #define FASTTRAP_JNE 0x75
97 #define FASTTRAP_JBE 0x76
98 #define FASTTRAP_JA 0x77
99 #define FASTTRAP_JS 0x78
100 #define FASTTRAP_JNS 0x79
101 #define FASTTRAP_JP 0x7a
102 #define FASTTRAP_JNP 0x7b
103 #define FASTTRAP_JL 0x7c
104 #define FASTTRAP_JGE 0x7d
105 #define FASTTRAP_JLE 0x7e
106 #define FASTTRAP_JG 0x7f
108 #define FASTTRAP_NOP 0x90
110 #define FASTTRAP_MOV_EAX 0xb8
111 #define FASTTRAP_MOV_ECX 0xb9
113 #define FASTTRAP_RET16 0xc2
114 #define FASTTRAP_RET 0xc3
116 #define FASTTRAP_LOOPNZ 0xe0
117 #define FASTTRAP_LOOPZ 0xe1
118 #define FASTTRAP_LOOP 0xe2
119 #define FASTTRAP_JCXZ 0xe3
121 #define FASTTRAP_CALL 0xe8
122 #define FASTTRAP_JMP32 0xe9
123 #define FASTTRAP_JMP8 0xeb
125 #define FASTTRAP_INT3 0xcc
126 #define FASTTRAP_INT 0xcd
128 #define FASTTRAP_2_BYTE_OP 0x0f
129 #define FASTTRAP_GROUP5_OP 0xff
132 * Two-byte op-codes (second byte only).
134 #define FASTTRAP_0F_JO 0x80
135 #define FASTTRAP_0F_JNO 0x81
136 #define FASTTRAP_0F_JB 0x82
137 #define FASTTRAP_0F_JAE 0x83
138 #define FASTTRAP_0F_JE 0x84
139 #define FASTTRAP_0F_JNE 0x85
140 #define FASTTRAP_0F_JBE 0x86
141 #define FASTTRAP_0F_JA 0x87
142 #define FASTTRAP_0F_JS 0x88
143 #define FASTTRAP_0F_JNS 0x89
144 #define FASTTRAP_0F_JP 0x8a
145 #define FASTTRAP_0F_JNP 0x8b
146 #define FASTTRAP_0F_JL 0x8c
147 #define FASTTRAP_0F_JGE 0x8d
148 #define FASTTRAP_0F_JLE 0x8e
149 #define FASTTRAP_0F_JG 0x8f
151 #define FASTTRAP_EFLAGS_OF 0x800
152 #define FASTTRAP_EFLAGS_DF 0x400
153 #define FASTTRAP_EFLAGS_SF 0x080
154 #define FASTTRAP_EFLAGS_ZF 0x040
155 #define FASTTRAP_EFLAGS_AF 0x010
156 #define FASTTRAP_EFLAGS_PF 0x004
157 #define FASTTRAP_EFLAGS_CF 0x001
160 * Instruction prefixes.
162 #define FASTTRAP_PREFIX_OPERAND 0x66
163 #define FASTTRAP_PREFIX_ADDRESS 0x67
164 #define FASTTRAP_PREFIX_CS 0x2E
165 #define FASTTRAP_PREFIX_DS 0x3E
166 #define FASTTRAP_PREFIX_ES 0x26
167 #define FASTTRAP_PREFIX_FS 0x64
168 #define FASTTRAP_PREFIX_GS 0x65
169 #define FASTTRAP_PREFIX_SS 0x36
170 #define FASTTRAP_PREFIX_LOCK 0xF0
171 #define FASTTRAP_PREFIX_REP 0xF3
172 #define FASTTRAP_PREFIX_REPNE 0xF2
174 #define FASTTRAP_NOREG 0xff
177 * Map between instruction register encodings and the kernel constants which
178 * correspond to indicies into struct regs.
181 static const uint8_t regmap
[16] = {
182 REG_RAX
, REG_RCX
, REG_RDX
, REG_RBX
, REG_RSP
, REG_RBP
, REG_RSI
, REG_RDI
,
183 REG_R8
, REG_R9
, REG_R10
, REG_R11
, REG_R12
, REG_R13
, REG_R14
, REG_R15
,
186 static const uint8_t regmap
[8] = {
187 EAX
, ECX
, EDX
, EBX
, UESP
, EBP
, ESI
, EDI
191 static ulong_t
fasttrap_getreg(struct regs
*, uint_t
);
194 fasttrap_anarg(struct regs
*rp
, int function_entry
, int argno
)
197 int shift
= function_entry
? 1 : 0;
200 if (curproc
->p_model
== DATAMODEL_LP64
) {
204 * In 64-bit mode, the first six arguments are stored in
208 return ((&rp
->r_rdi
)[argno
]);
210 stack
= (uintptr_t *)rp
->r_sp
;
211 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
212 value
= dtrace_fulword(&stack
[argno
- 6 + shift
]);
213 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
| CPU_DTRACE_BADADDR
);
216 uint32_t *stack
= (uint32_t *)rp
->r_sp
;
217 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
218 value
= dtrace_fuword32(&stack
[argno
+ shift
]);
219 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
| CPU_DTRACE_BADADDR
);
229 fasttrap_tracepoint_init(proc_t
*p
, fasttrap_tracepoint_t
*tp
, uintptr_t pc
,
230 fasttrap_probe_type_t type
)
232 uint8_t instr
[FASTTRAP_MAX_INSTR_SIZE
+ 10];
233 size_t len
= FASTTRAP_MAX_INSTR_SIZE
;
234 size_t first
= MIN(len
, PAGESIZE
- (pc
& PAGEOFFSET
));
237 uint8_t seg
, rex
= 0;
240 * Read the instruction at the given address out of the process's
241 * address space. We don't have to worry about a debugger
242 * changing this instruction before we overwrite it with our trap
243 * instruction since P_PR_LOCK is set. Since instructions can span
244 * pages, we potentially read the instruction in two parts. If the
245 * second part fails, we just zero out that part of the instruction.
247 if (uread(p
, &instr
[0], first
, pc
) != 0)
250 uread(p
, &instr
[first
], len
- first
, pc
+ first
) != 0) {
251 bzero(&instr
[first
], len
- first
);
256 * If the disassembly fails, then we have a malformed instruction.
258 if ((size
= dtrace_instr_size_isa(instr
, p
->p_model
, &rmindex
)) <= 0)
262 * Make sure the disassembler isn't completely broken.
264 ASSERT(-1 <= rmindex
&& rmindex
< size
);
267 * If the computed size is greater than the number of bytes read,
268 * then it was a malformed instruction possibly because it fell on a
269 * page boundary and the subsequent page was missing or because of
270 * some malicious user.
275 tp
->ftt_size
= (uint8_t)size
;
276 tp
->ftt_segment
= FASTTRAP_SEG_NONE
;
279 * Find the start of the instruction's opcode by processing any
284 switch (instr
[start
]) {
285 case FASTTRAP_PREFIX_SS
:
288 case FASTTRAP_PREFIX_GS
:
291 case FASTTRAP_PREFIX_FS
:
294 case FASTTRAP_PREFIX_ES
:
297 case FASTTRAP_PREFIX_DS
:
300 case FASTTRAP_PREFIX_CS
:
303 case FASTTRAP_PREFIX_OPERAND
:
304 case FASTTRAP_PREFIX_ADDRESS
:
305 case FASTTRAP_PREFIX_LOCK
:
306 case FASTTRAP_PREFIX_REP
:
307 case FASTTRAP_PREFIX_REPNE
:
310 * It's illegal for an instruction to specify
311 * two segment prefixes -- give up on this
312 * illegal instruction.
314 if (tp
->ftt_segment
!= FASTTRAP_SEG_NONE
)
317 tp
->ftt_segment
= seg
;
327 * Identify the REX prefix on 64-bit processes.
329 if (p
->p_model
== DATAMODEL_LP64
&& (instr
[start
] & 0xf0) == 0x40)
330 rex
= instr
[start
++];
334 * Now that we're pretty sure that the instruction is okay, copy the
335 * valid part to the tracepoint.
337 bcopy(instr
, tp
->ftt_instr
, FASTTRAP_MAX_INSTR_SIZE
);
339 tp
->ftt_type
= FASTTRAP_T_COMMON
;
340 if (instr
[start
] == FASTTRAP_2_BYTE_OP
) {
341 switch (instr
[start
+ 1]) {
343 case FASTTRAP_0F_JNO
:
345 case FASTTRAP_0F_JAE
:
347 case FASTTRAP_0F_JNE
:
348 case FASTTRAP_0F_JBE
:
351 case FASTTRAP_0F_JNS
:
353 case FASTTRAP_0F_JNP
:
355 case FASTTRAP_0F_JGE
:
356 case FASTTRAP_0F_JLE
:
358 tp
->ftt_type
= FASTTRAP_T_JCC
;
359 tp
->ftt_code
= (instr
[start
+ 1] & 0x0f) | FASTTRAP_JO
;
360 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
361 /* LINTED - alignment */
362 *(int32_t *)&instr
[start
+ 2];
365 } else if (instr
[start
] == FASTTRAP_GROUP5_OP
) {
366 uint_t mod
= FASTTRAP_MODRM_MOD(instr
[start
+ 1]);
367 uint_t reg
= FASTTRAP_MODRM_REG(instr
[start
+ 1]);
368 uint_t rm
= FASTTRAP_MODRM_RM(instr
[start
+ 1]);
370 if (reg
== 2 || reg
== 4) {
374 tp
->ftt_type
= FASTTRAP_T_CALL
;
376 tp
->ftt_type
= FASTTRAP_T_JMP
;
383 ASSERT(p
->p_model
== DATAMODEL_LP64
|| rex
== 0);
386 * See AMD x86-64 Architecture Programmer's Manual
387 * Volume 3, Section 1.2.7, Table 1-12, and
388 * Appendix A.3.1, Table A-15.
390 if (mod
!= 3 && rm
== 4) {
391 uint8_t sib
= instr
[start
+ 2];
392 uint_t index
= FASTTRAP_SIB_INDEX(sib
);
393 uint_t base
= FASTTRAP_SIB_BASE(sib
);
395 tp
->ftt_scale
= FASTTRAP_SIB_SCALE(sib
);
397 tp
->ftt_index
= (index
== 4) ?
399 regmap
[index
| (FASTTRAP_REX_X(rex
) << 3)];
400 tp
->ftt_base
= (mod
== 0 && base
== 5) ?
402 regmap
[base
| (FASTTRAP_REX_B(rex
) << 3)];
405 sz
= mod
== 1 ? 1 : 4;
408 * In 64-bit mode, mod == 0 and r/m == 5
409 * denotes %rip-relative addressing; in 32-bit
410 * mode, the base register isn't used. In both
411 * modes, there is a 32-bit operand.
413 if (mod
== 0 && rm
== 5) {
415 if (p
->p_model
== DATAMODEL_LP64
)
416 tp
->ftt_base
= REG_RIP
;
419 tp
->ftt_base
= FASTTRAP_NOREG
;
423 (FASTTRAP_REX_B(rex
) << 3);
425 tp
->ftt_base
= regmap
[base
];
426 sz
= mod
== 1 ? 1 : mod
== 2 ? 4 : 0;
428 tp
->ftt_index
= FASTTRAP_NOREG
;
433 tp
->ftt_dest
= *(int8_t *)&instr
[start
+ i
];
434 } else if (sz
== 4) {
435 /* LINTED - alignment */
436 tp
->ftt_dest
= *(int32_t *)&instr
[start
+ i
];
442 switch (instr
[start
]) {
444 tp
->ftt_type
= FASTTRAP_T_RET
;
448 tp
->ftt_type
= FASTTRAP_T_RET16
;
449 /* LINTED - alignment */
450 tp
->ftt_dest
= *(uint16_t *)&instr
[start
+ 1];
469 tp
->ftt_type
= FASTTRAP_T_JCC
;
470 tp
->ftt_code
= instr
[start
];
471 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
472 (int8_t)instr
[start
+ 1];
475 case FASTTRAP_LOOPNZ
:
478 tp
->ftt_type
= FASTTRAP_T_LOOP
;
479 tp
->ftt_code
= instr
[start
];
480 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
481 (int8_t)instr
[start
+ 1];
485 tp
->ftt_type
= FASTTRAP_T_JCXZ
;
486 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
487 (int8_t)instr
[start
+ 1];
491 tp
->ftt_type
= FASTTRAP_T_CALL
;
492 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
493 /* LINTED - alignment */
494 *(int32_t *)&instr
[start
+ 1];
499 tp
->ftt_type
= FASTTRAP_T_JMP
;
500 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
501 /* LINTED - alignment */
502 *(int32_t *)&instr
[start
+ 1];
505 tp
->ftt_type
= FASTTRAP_T_JMP
;
506 tp
->ftt_dest
= pc
+ tp
->ftt_size
+
507 (int8_t)instr
[start
+ 1];
510 case FASTTRAP_PUSHL_EBP
:
512 tp
->ftt_type
= FASTTRAP_T_PUSHL_EBP
;
517 ASSERT(p
->p_model
== DATAMODEL_LP64
|| rex
== 0);
520 * On amd64 we have to be careful not to confuse a nop
521 * (actually xchgl %eax, %eax) with an instruction using
522 * the same opcode, but that does something different
523 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
525 if (FASTTRAP_REX_B(rex
) == 0)
527 tp
->ftt_type
= FASTTRAP_T_NOP
;
532 * The pid provider shares the int3 trap with debugger
533 * breakpoints so we can't instrument them.
535 ASSERT(instr
[start
] == FASTTRAP_INSTR
);
540 * Interrupts seem like they could be traced with
541 * no negative implications, but it's possible that
542 * a thread could be redirected by the trap handling
543 * code which would eventually return to the
544 * instruction after the interrupt. If the interrupt
545 * were in our scratch space, the subsequent
546 * instruction might be overwritten before we return.
547 * Accordingly we refuse to instrument any interrupt.
554 if (p
->p_model
== DATAMODEL_LP64
&& tp
->ftt_type
== FASTTRAP_T_COMMON
) {
556 * If the process is 64-bit and the instruction type is still
557 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
558 * execute it -- we need to watch for %rip-relative
559 * addressing mode. See the portion of fasttrap_pid_probe()
560 * below where we handle tracepoints with type
561 * FASTTRAP_T_COMMON for how we emulate instructions that
562 * employ %rip-relative addressing.
565 uint_t mod
= FASTTRAP_MODRM_MOD(instr
[rmindex
]);
566 uint_t reg
= FASTTRAP_MODRM_REG(instr
[rmindex
]);
567 uint_t rm
= FASTTRAP_MODRM_RM(instr
[rmindex
]);
569 ASSERT(rmindex
> start
);
571 if (mod
== 0 && rm
== 5) {
573 * We need to be sure to avoid other
574 * registers used by this instruction. While
575 * the reg field may determine the op code
576 * rather than denoting a register, assuming
577 * that it denotes a register is always safe.
578 * We leave the REX field intact and use
579 * whatever value's there for simplicity.
582 tp
->ftt_ripmode
= FASTTRAP_RIP_1
|
584 FASTTRAP_REX_B(rex
));
587 tp
->ftt_ripmode
= FASTTRAP_RIP_2
|
589 FASTTRAP_REX_B(rex
));
593 tp
->ftt_modrm
= tp
->ftt_instr
[rmindex
];
594 tp
->ftt_instr
[rmindex
] =
595 FASTTRAP_MODRM(2, reg
, rm
);
605 fasttrap_tracepoint_install(proc_t
*p
, fasttrap_tracepoint_t
*tp
)
607 fasttrap_instr_t instr
= FASTTRAP_INSTR
;
609 if (uwrite(p
, &instr
, 1, tp
->ftt_pc
) != 0)
616 fasttrap_tracepoint_remove(proc_t
*p
, fasttrap_tracepoint_t
*tp
)
621 * Distinguish between read or write failures and a changed
624 if (uread(p
, &instr
, 1, tp
->ftt_pc
) != 0)
626 if (instr
!= FASTTRAP_INSTR
)
628 if (uwrite(p
, &tp
->ftt_instr
[0], 1, tp
->ftt_pc
) != 0)
636 fasttrap_fulword_noerr(const void *uaddr
)
640 if (fasttrap_fulword(uaddr
, &ret
) == 0)
648 fasttrap_fuword32_noerr(const void *uaddr
)
652 if (fasttrap_fuword32(uaddr
, &ret
) == 0)
659 fasttrap_return_common(struct regs
*rp
, uintptr_t pc
, pid_t pid
,
662 fasttrap_tracepoint_t
*tp
;
663 fasttrap_bucket_t
*bucket
;
667 pid_mtx
= &cpu_core
[CPU
->cpu_id
].cpuc_pid_lock
;
668 mutex_enter(pid_mtx
);
669 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
671 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
672 if (pid
== tp
->ftt_pid
&& pc
== tp
->ftt_pc
&&
673 tp
->ftt_proc
->ftpc_acount
!= 0)
678 * Don't sweat it if we can't find the tracepoint again; unlike
679 * when we're in fasttrap_pid_probe(), finding the tracepoint here
680 * is not essential to the correct execution of the process.
687 for (id
= tp
->ftt_retids
; id
!= NULL
; id
= id
->fti_next
) {
689 * If there's a branch that could act as a return site, we
690 * need to trace it, and check here if the program counter is
691 * external to the function.
693 if (tp
->ftt_type
!= FASTTRAP_T_RET
&&
694 tp
->ftt_type
!= FASTTRAP_T_RET16
&&
695 new_pc
- id
->fti_probe
->ftp_faddr
<
696 id
->fti_probe
->ftp_fsize
)
699 dtrace_probe(id
->fti_probe
->ftp_id
,
700 pc
- id
->fti_probe
->ftp_faddr
,
701 rp
->r_r0
, rp
->r_r1
, 0, 0);
708 fasttrap_sigsegv(proc_t
*p
, kthread_t
*t
, uintptr_t addr
)
710 sigqueue_t
*sqp
= kmem_zalloc(sizeof (sigqueue_t
), KM_SLEEP
);
712 sqp
->sq_info
.si_signo
= SIGSEGV
;
713 sqp
->sq_info
.si_code
= SEGV_MAPERR
;
714 sqp
->sq_info
.si_addr
= (caddr_t
)addr
;
716 mutex_enter(&p
->p_lock
);
718 mutex_exit(&p
->p_lock
);
726 fasttrap_usdt_args64(fasttrap_probe_t
*probe
, struct regs
*rp
, int argc
,
729 int i
, x
, cap
= MIN(argc
, probe
->ftp_nargs
);
730 uintptr_t *stack
= (uintptr_t *)rp
->r_sp
;
732 for (i
= 0; i
< cap
; i
++) {
733 x
= probe
->ftp_argmap
[i
];
736 argv
[i
] = (&rp
->r_rdi
)[x
];
738 argv
[i
] = fasttrap_fulword_noerr(&stack
[x
]);
741 for (; i
< argc
; i
++) {
748 fasttrap_usdt_args32(fasttrap_probe_t
*probe
, struct regs
*rp
, int argc
,
751 int i
, x
, cap
= MIN(argc
, probe
->ftp_nargs
);
752 uint32_t *stack
= (uint32_t *)rp
->r_sp
;
754 for (i
= 0; i
< cap
; i
++) {
755 x
= probe
->ftp_argmap
[i
];
757 argv
[i
] = fasttrap_fuword32_noerr(&stack
[x
]);
760 for (; i
< argc
; i
++) {
766 fasttrap_do_seg(fasttrap_tracepoint_t
*tp
, struct regs
*rp
, uintptr_t *addr
)
770 uint16_t sel
, ndx
, type
;
773 switch (tp
->ftt_segment
) {
774 case FASTTRAP_SEG_CS
:
777 case FASTTRAP_SEG_DS
:
780 case FASTTRAP_SEG_ES
:
783 case FASTTRAP_SEG_FS
:
786 case FASTTRAP_SEG_GS
:
789 case FASTTRAP_SEG_SS
:
795 * Make sure the given segment register specifies a user priority
796 * selector rather than a kernel selector.
804 * Check the bounds and grab the descriptor out of the specified
808 if (ndx
> p
->p_ldtlimit
)
811 desc
= p
->p_ldt
+ ndx
;
817 desc
= cpu_get_gdt() + ndx
;
821 * The descriptor must have user privilege level and it must be
824 if (desc
->usd_dpl
!= SEL_UPL
|| desc
->usd_p
!= 1)
827 type
= desc
->usd_type
;
830 * If the S bit in the type field is not set, this descriptor can
831 * only be used in system context.
833 if ((type
& 0x10) != 0x10)
836 limit
= USEGD_GETLIMIT(desc
) * (desc
->usd_gran
? PAGESIZE
: 1);
838 if (tp
->ftt_segment
== FASTTRAP_SEG_CS
) {
840 * The code/data bit and readable bit must both be set.
842 if ((type
& 0xa) != 0xa)
849 * The code/data bit must be clear.
851 if ((type
& 0x8) != 0)
855 * If the expand-down bit is clear, we just check the limit as
856 * it would naturally be applied. Otherwise, we need to check
857 * that the address is the range [limit + 1 .. 0xffff] or
858 * [limit + 1 ... 0xffffffff] depending on if the default
859 * operand size bit is set.
861 if ((type
& 0x4) == 0) {
864 } else if (desc
->usd_def32
) {
865 if (*addr
< limit
+ 1 || 0xffff < *addr
)
868 if (*addr
< limit
+ 1 || 0xffffffff < *addr
)
873 *addr
+= USEGD_GETBASE(desc
);
879 fasttrap_pid_probe(struct regs
*rp
)
882 uintptr_t pc
= rp
->r_pc
- 1, new_pc
= 0;
883 fasttrap_bucket_t
*bucket
;
885 fasttrap_tracepoint_t
*tp
, tp_local
;
887 dtrace_icookie_t cookie
;
888 uint_t is_enabled
= 0;
891 * It's possible that a user (in a veritable orgy of bad planning)
892 * could redirect this thread's flow of control before it reached the
893 * return probe fasttrap. In this case we need to kill the process
894 * since it's in a unrecoverable state.
896 if (curthread
->t_dtrace_step
) {
897 ASSERT(curthread
->t_dtrace_on
);
898 fasttrap_sigtrap(p
, curthread
, pc
);
903 * Clear all user tracing flags.
905 curthread
->t_dtrace_ft
= 0;
906 curthread
->t_dtrace_pc
= 0;
907 curthread
->t_dtrace_npc
= 0;
908 curthread
->t_dtrace_scrpc
= 0;
909 curthread
->t_dtrace_astpc
= 0;
911 curthread
->t_dtrace_regv
= 0;
915 * Treat a child created by a call to vfork(2) as if it were its
916 * parent. We know that there's only one thread of control in such a
919 while (p
->p_flag
& SVFORK
) {
924 pid_mtx
= &cpu_core
[CPU
->cpu_id
].cpuc_pid_lock
;
925 mutex_enter(pid_mtx
);
926 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
929 * Lookup the tracepoint that the process just hit.
931 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
932 if (pid
== tp
->ftt_pid
&& pc
== tp
->ftt_pc
&&
933 tp
->ftt_proc
->ftpc_acount
!= 0)
938 * If we couldn't find a matching tracepoint, either a tracepoint has
939 * been inserted without using the pid<pid> ioctl interface (see
940 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
948 * Set the program counter to the address of the traced instruction
949 * so that it looks right in ustack() output.
953 if (tp
->ftt_ids
!= NULL
) {
957 if (p
->p_model
== DATAMODEL_LP64
) {
958 for (id
= tp
->ftt_ids
; id
!= NULL
; id
= id
->fti_next
) {
959 fasttrap_probe_t
*probe
= id
->fti_probe
;
961 if (id
->fti_ptype
== DTFTP_ENTRY
) {
963 * We note that this was an entry
964 * probe to help ustack() find the
967 cookie
= dtrace_interrupt_disable();
968 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY
);
969 dtrace_probe(probe
->ftp_id
, rp
->r_rdi
,
970 rp
->r_rsi
, rp
->r_rdx
, rp
->r_rcx
,
972 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY
);
973 dtrace_interrupt_enable(cookie
);
974 } else if (id
->fti_ptype
== DTFTP_IS_ENABLED
) {
976 * Note that in this case, we don't
977 * call dtrace_probe() since it's only
978 * an artificial probe meant to change
979 * the flow of control so that it
980 * encounters the true probe.
983 } else if (probe
->ftp_argmap
== NULL
) {
984 dtrace_probe(probe
->ftp_id
, rp
->r_rdi
,
985 rp
->r_rsi
, rp
->r_rdx
, rp
->r_rcx
,
990 fasttrap_usdt_args64(probe
, rp
,
991 sizeof (t
) / sizeof (t
[0]), t
);
993 dtrace_probe(probe
->ftp_id
, t
[0], t
[1],
999 uintptr_t s0
, s1
, s2
, s3
, s4
, s5
;
1000 uint32_t *stack
= (uint32_t *)rp
->r_sp
;
1003 * In 32-bit mode, all arguments are passed on the
1004 * stack. If this is a function entry probe, we need
1005 * to skip the first entry on the stack as it
1006 * represents the return address rather than a
1007 * parameter to the function.
1009 s0
= fasttrap_fuword32_noerr(&stack
[0]);
1010 s1
= fasttrap_fuword32_noerr(&stack
[1]);
1011 s2
= fasttrap_fuword32_noerr(&stack
[2]);
1012 s3
= fasttrap_fuword32_noerr(&stack
[3]);
1013 s4
= fasttrap_fuword32_noerr(&stack
[4]);
1014 s5
= fasttrap_fuword32_noerr(&stack
[5]);
1016 for (id
= tp
->ftt_ids
; id
!= NULL
; id
= id
->fti_next
) {
1017 fasttrap_probe_t
*probe
= id
->fti_probe
;
1019 if (id
->fti_ptype
== DTFTP_ENTRY
) {
1021 * We note that this was an entry
1022 * probe to help ustack() find the
1025 cookie
= dtrace_interrupt_disable();
1026 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY
);
1027 dtrace_probe(probe
->ftp_id
, s1
, s2
,
1029 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY
);
1030 dtrace_interrupt_enable(cookie
);
1031 } else if (id
->fti_ptype
== DTFTP_IS_ENABLED
) {
1033 * Note that in this case, we don't
1034 * call dtrace_probe() since it's only
1035 * an artificial probe meant to change
1036 * the flow of control so that it
1037 * encounters the true probe.
1040 } else if (probe
->ftp_argmap
== NULL
) {
1041 dtrace_probe(probe
->ftp_id
, s0
, s1
,
1046 fasttrap_usdt_args32(probe
, rp
,
1047 sizeof (t
) / sizeof (t
[0]), t
);
1049 dtrace_probe(probe
->ftp_id
, t
[0], t
[1],
1059 * We're about to do a bunch of work so we cache a local copy of
1060 * the tracepoint to emulate the instruction, and then find the
1061 * tracepoint again later if we need to light up any return probes.
1064 mutex_exit(pid_mtx
);
1068 * Set the program counter to appear as though the traced instruction
1069 * had completely executed. This ensures that fasttrap_getreg() will
1070 * report the expected value for REG_RIP.
1072 rp
->r_pc
= pc
+ tp
->ftt_size
;
1075 * If there's an is-enabled probe connected to this tracepoint it
1076 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1077 * instruction that was placed there by DTrace when the binary was
1078 * linked. As this probe is, in fact, enabled, we need to stuff 1
1079 * into %eax or %rax. Accordingly, we can bypass all the instruction
1080 * emulation logic since we know the inevitable result. It's possible
1081 * that a user could construct a scenario where the 'is-enabled'
1082 * probe was on some other instruction, but that would be a rather
1083 * exotic way to shoot oneself in the foot.
1092 * We emulate certain types of instructions to ensure correctness
1093 * (in the case of position dependent instructions) or optimize
1094 * common cases. The rest we have the thread execute back in user-
1097 switch (tp
->ftt_type
) {
1098 case FASTTRAP_T_RET
:
1099 case FASTTRAP_T_RET16
:
1106 * We have to emulate _every_ facet of the behavior of a ret
1107 * instruction including what happens if the load from %esp
1108 * fails; in that case, we send a SIGSEGV.
1111 if (p
->p_model
== DATAMODEL_NATIVE
) {
1113 ret
= fasttrap_fulword((void *)rp
->r_sp
, &dst
);
1114 addr
= rp
->r_sp
+ sizeof (uintptr_t);
1118 ret
= fasttrap_fuword32((void *)rp
->r_sp
, &dst32
);
1120 addr
= rp
->r_sp
+ sizeof (uint32_t);
1125 fasttrap_sigsegv(p
, curthread
, rp
->r_sp
);
1130 if (tp
->ftt_type
== FASTTRAP_T_RET16
)
1131 addr
+= tp
->ftt_dest
;
1138 case FASTTRAP_T_JCC
:
1142 switch (tp
->ftt_code
) {
1144 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_OF
) != 0;
1147 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_OF
) == 0;
1150 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_CF
) != 0;
1153 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_CF
) == 0;
1156 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) != 0;
1159 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) == 0;
1162 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_CF
) != 0 ||
1163 (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) != 0;
1166 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_CF
) == 0 &&
1167 (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) == 0;
1170 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_SF
) != 0;
1173 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_SF
) == 0;
1176 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_PF
) != 0;
1179 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_PF
) == 0;
1182 taken
= ((rp
->r_ps
& FASTTRAP_EFLAGS_SF
) == 0) !=
1183 ((rp
->r_ps
& FASTTRAP_EFLAGS_OF
) == 0);
1186 taken
= ((rp
->r_ps
& FASTTRAP_EFLAGS_SF
) == 0) ==
1187 ((rp
->r_ps
& FASTTRAP_EFLAGS_OF
) == 0);
1190 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) != 0 ||
1191 ((rp
->r_ps
& FASTTRAP_EFLAGS_SF
) == 0) !=
1192 ((rp
->r_ps
& FASTTRAP_EFLAGS_OF
) == 0);
1195 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) == 0 &&
1196 ((rp
->r_ps
& FASTTRAP_EFLAGS_SF
) == 0) ==
1197 ((rp
->r_ps
& FASTTRAP_EFLAGS_OF
) == 0);
1203 new_pc
= tp
->ftt_dest
;
1205 new_pc
= pc
+ tp
->ftt_size
;
1209 case FASTTRAP_T_LOOP
:
1213 greg_t cx
= rp
->r_rcx
--;
1215 greg_t cx
= rp
->r_ecx
--;
1218 switch (tp
->ftt_code
) {
1219 case FASTTRAP_LOOPNZ
:
1220 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) == 0 &&
1223 case FASTTRAP_LOOPZ
:
1224 taken
= (rp
->r_ps
& FASTTRAP_EFLAGS_ZF
) != 0 &&
1233 new_pc
= tp
->ftt_dest
;
1235 new_pc
= pc
+ tp
->ftt_size
;
1239 case FASTTRAP_T_JCXZ
:
1242 greg_t cx
= rp
->r_rcx
;
1244 greg_t cx
= rp
->r_ecx
;
1248 new_pc
= tp
->ftt_dest
;
1250 new_pc
= pc
+ tp
->ftt_size
;
1254 case FASTTRAP_T_PUSHL_EBP
:
1259 if (p
->p_model
== DATAMODEL_NATIVE
) {
1261 addr
= rp
->r_sp
- sizeof (uintptr_t);
1262 ret
= fasttrap_sulword((void *)addr
, rp
->r_fp
);
1265 addr
= rp
->r_sp
- sizeof (uint32_t);
1266 ret
= fasttrap_suword32((void *)addr
,
1267 (uint32_t)rp
->r_fp
);
1272 fasttrap_sigsegv(p
, curthread
, addr
);
1278 new_pc
= pc
+ tp
->ftt_size
;
1282 case FASTTRAP_T_NOP
:
1283 new_pc
= pc
+ tp
->ftt_size
;
1286 case FASTTRAP_T_JMP
:
1287 case FASTTRAP_T_CALL
:
1288 if (tp
->ftt_code
== 0) {
1289 new_pc
= tp
->ftt_dest
;
1291 uintptr_t value
, addr
= tp
->ftt_dest
;
1293 if (tp
->ftt_base
!= FASTTRAP_NOREG
)
1294 addr
+= fasttrap_getreg(rp
, tp
->ftt_base
);
1295 if (tp
->ftt_index
!= FASTTRAP_NOREG
)
1296 addr
+= fasttrap_getreg(rp
, tp
->ftt_index
) <<
1299 if (tp
->ftt_code
== 1) {
1301 * If there's a segment prefix for this
1302 * instruction, we'll need to check permissions
1303 * and bounds on the given selector, and adjust
1304 * the address accordingly.
1306 if (tp
->ftt_segment
!= FASTTRAP_SEG_NONE
&&
1307 fasttrap_do_seg(tp
, rp
, &addr
) != 0) {
1308 fasttrap_sigsegv(p
, curthread
, addr
);
1314 if (p
->p_model
== DATAMODEL_NATIVE
) {
1316 if (fasttrap_fulword((void *)addr
,
1318 fasttrap_sigsegv(p
, curthread
,
1327 addr
= (uintptr_t)(uint32_t)addr
;
1328 if (fasttrap_fuword32((void *)addr
,
1330 fasttrap_sigsegv(p
, curthread
,
1344 * If this is a call instruction, we need to push the return
1345 * address onto the stack. If this fails, we send the process
1346 * a SIGSEGV and reset the pc to emulate what would happen if
1347 * this instruction weren't traced.
1349 if (tp
->ftt_type
== FASTTRAP_T_CALL
) {
1353 if (p
->p_model
== DATAMODEL_NATIVE
) {
1354 addr
= rp
->r_sp
- sizeof (uintptr_t);
1355 ret
= fasttrap_sulword((void *)addr
,
1359 addr
= rp
->r_sp
- sizeof (uint32_t);
1360 ret
= fasttrap_suword32((void *)addr
,
1361 (uint32_t)(pc
+ tp
->ftt_size
));
1367 fasttrap_sigsegv(p
, curthread
, addr
);
1377 case FASTTRAP_T_COMMON
:
1380 #if defined(__amd64)
1381 uint8_t scratch
[2 * FASTTRAP_MAX_INSTR_SIZE
+ 22];
1383 uint8_t scratch
[2 * FASTTRAP_MAX_INSTR_SIZE
+ 7];
1386 klwp_t
*lwp
= ttolwp(curthread
);
1389 * Compute the address of the ulwp_t and step over the
1390 * ul_self pointer. The method used to store the user-land
1391 * thread pointer is very different on 32- and 64-bit
1394 #if defined(__amd64)
1395 if (p
->p_model
== DATAMODEL_LP64
) {
1396 addr
= lwp
->lwp_pcb
.pcb_fsbase
;
1397 addr
+= sizeof (void *);
1399 addr
= lwp
->lwp_pcb
.pcb_gsbase
;
1400 addr
+= sizeof (caddr32_t
);
1403 addr
= USEGD_GETBASE(&lwp
->lwp_pcb
.pcb_gsdesc
);
1404 addr
+= sizeof (void *);
1408 * Generic Instruction Tracing
1409 * ---------------------------
1411 * This is the layout of the scratch space in the user-land
1412 * thread structure for our generated instructions.
1415 * ------------------------ -----
1416 * a: <original instruction> <= 15
1417 * jmp <pc + tp->ftt_size> 5
1418 * b: <original instrction> <= 15
1419 * int T_DTRACE_RET 2
1424 * ------------------------ -----
1425 * a: <original instruction> <= 15
1427 * <pc + tp->ftt_size> 8
1428 * b: <original instruction> <= 15
1429 * int T_DTRACE_RET 2
1433 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1434 * to b. If we encounter a signal on the way out of the
1435 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1436 * so that we execute the original instruction and re-enter
1437 * the kernel rather than redirecting to the next instruction.
1439 * If there are return probes (so we know that we're going to
1440 * need to reenter the kernel after executing the original
1441 * instruction), the scratch space will just contain the
1442 * original instruction followed by an interrupt -- the same
1445 * %rip-relative Addressing
1446 * ------------------------
1448 * There's a further complication in 64-bit mode due to %rip-
1449 * relative addressing. While this is clearly a beneficial
1450 * architectural decision for position independent code, it's
1451 * hard not to see it as a personal attack against the pid
1452 * provider since before there was a relatively small set of
1453 * instructions to emulate; with %rip-relative addressing,
1454 * almost every instruction can potentially depend on the
1455 * address at which it's executed. Rather than emulating
1456 * the broad spectrum of instructions that can now be
1457 * position dependent, we emulate jumps and others as in
1458 * 32-bit mode, and take a different tack for instructions
1459 * using %rip-relative addressing.
1461 * For every instruction that uses the ModRM byte, the
1462 * in-kernel disassembler reports its location. We use the
1463 * ModRM byte to identify that an instruction uses
1464 * %rip-relative addressing and to see what other registers
1465 * the instruction uses. To emulate those instructions,
1466 * we modify the instruction to be %rax-relative rather than
1467 * %rip-relative (or %rcx-relative if the instruction uses
1468 * %rax; or %r8- or %r9-relative if the REX.B is present so
1469 * we don't have to rewrite the REX prefix). We then load
1470 * the value that %rip would have been into the scratch
1471 * register and generate an instruction to reset the scratch
1472 * register back to its original value. The instruction
1473 * sequence looks like this:
1475 * 64-mode %rip-relative bytes
1476 * ------------------------ -----
1477 * a: <modified instruction> <= 15
1478 * movq $<value>, %<scratch> 6
1480 * <pc + tp->ftt_size> 8
1481 * b: <modified instruction> <= 15
1482 * int T_DTRACE_RET 2
1486 * We set curthread->t_dtrace_regv so that upon receiving
1487 * a signal we can reset the value of the scratch register.
1490 ASSERT(tp
->ftt_size
< FASTTRAP_MAX_INSTR_SIZE
);
1492 curthread
->t_dtrace_scrpc
= addr
;
1493 bcopy(tp
->ftt_instr
, &scratch
[i
], tp
->ftt_size
);
1497 if (tp
->ftt_ripmode
!= 0) {
1500 ASSERT(p
->p_model
== DATAMODEL_LP64
);
1501 ASSERT(tp
->ftt_ripmode
&
1502 (FASTTRAP_RIP_1
| FASTTRAP_RIP_2
));
1505 * If this was a %rip-relative instruction, we change
1506 * it to be either a %rax- or %rcx-relative
1507 * instruction (depending on whether those registers
1508 * are used as another operand; or %r8- or %r9-
1509 * relative depending on the value of REX.B). We then
1510 * set that register and generate a movq instruction
1511 * to reset the value.
1513 if (tp
->ftt_ripmode
& FASTTRAP_RIP_X
)
1514 scratch
[i
++] = FASTTRAP_REX(1, 0, 0, 1);
1516 scratch
[i
++] = FASTTRAP_REX(1, 0, 0, 0);
1518 if (tp
->ftt_ripmode
& FASTTRAP_RIP_1
)
1519 scratch
[i
++] = FASTTRAP_MOV_EAX
;
1521 scratch
[i
++] = FASTTRAP_MOV_ECX
;
1523 switch (tp
->ftt_ripmode
) {
1524 case FASTTRAP_RIP_1
:
1526 curthread
->t_dtrace_reg
= REG_RAX
;
1528 case FASTTRAP_RIP_2
:
1530 curthread
->t_dtrace_reg
= REG_RCX
;
1532 case FASTTRAP_RIP_1
| FASTTRAP_RIP_X
:
1534 curthread
->t_dtrace_reg
= REG_R8
;
1536 case FASTTRAP_RIP_2
| FASTTRAP_RIP_X
:
1538 curthread
->t_dtrace_reg
= REG_R9
;
1542 /* LINTED - alignment */
1543 *(uint64_t *)&scratch
[i
] = *reg
;
1544 curthread
->t_dtrace_regv
= *reg
;
1545 *reg
= pc
+ tp
->ftt_size
;
1546 i
+= sizeof (uint64_t);
1551 * Generate the branch instruction to what would have
1552 * normally been the subsequent instruction. In 32-bit mode,
1553 * this is just a relative branch; in 64-bit mode this is a
1554 * %rip-relative branch that loads the 64-bit pc value
1555 * immediately after the jmp instruction.
1558 if (p
->p_model
== DATAMODEL_LP64
) {
1559 scratch
[i
++] = FASTTRAP_GROUP5_OP
;
1560 scratch
[i
++] = FASTTRAP_MODRM(0, 4, 5);
1561 /* LINTED - alignment */
1562 *(uint32_t *)&scratch
[i
] = 0;
1563 i
+= sizeof (uint32_t);
1564 /* LINTED - alignment */
1565 *(uint64_t *)&scratch
[i
] = pc
+ tp
->ftt_size
;
1566 i
+= sizeof (uint64_t);
1570 * Set up the jmp to the next instruction; note that
1571 * the size of the traced instruction cancels out.
1573 scratch
[i
++] = FASTTRAP_JMP32
;
1574 /* LINTED - alignment */
1575 *(uint32_t *)&scratch
[i
] = pc
- addr
- 5;
1576 i
+= sizeof (uint32_t);
1581 curthread
->t_dtrace_astpc
= addr
+ i
;
1582 bcopy(tp
->ftt_instr
, &scratch
[i
], tp
->ftt_size
);
1584 scratch
[i
++] = FASTTRAP_INT
;
1585 scratch
[i
++] = T_DTRACE_RET
;
1587 ASSERT(i
<= sizeof (scratch
));
1589 if (fasttrap_copyout(scratch
, (char *)addr
, i
)) {
1590 fasttrap_sigtrap(p
, curthread
, pc
);
1595 if (tp
->ftt_retids
!= NULL
) {
1596 curthread
->t_dtrace_step
= 1;
1597 curthread
->t_dtrace_ret
= 1;
1598 new_pc
= curthread
->t_dtrace_astpc
;
1600 new_pc
= curthread
->t_dtrace_scrpc
;
1603 curthread
->t_dtrace_pc
= pc
;
1604 curthread
->t_dtrace_npc
= pc
+ tp
->ftt_size
;
1605 curthread
->t_dtrace_on
= 1;
1610 panic("fasttrap: mishandled an instruction");
1615 * If there were no return probes when we first found the tracepoint,
1616 * we should feel no obligation to honor any return probes that were
1617 * subsequently enabled -- they'll just have to wait until the next
1620 if (tp
->ftt_retids
!= NULL
) {
1622 * We need to wait until the results of the instruction are
1623 * apparent before invoking any return probes. If this
1624 * instruction was emulated we can just call
1625 * fasttrap_return_common(); if it needs to be executed, we
1626 * need to wait until the user thread returns to the kernel.
1628 if (tp
->ftt_type
!= FASTTRAP_T_COMMON
) {
1630 * Set the program counter to the address of the traced
1631 * instruction so that it looks right in ustack()
1632 * output. We had previously set it to the end of the
1633 * instruction to simplify %rip-relative addressing.
1637 fasttrap_return_common(rp
, pc
, pid
, new_pc
);
1639 ASSERT(curthread
->t_dtrace_ret
!= 0);
1640 ASSERT(curthread
->t_dtrace_pc
== pc
);
1641 ASSERT(curthread
->t_dtrace_scrpc
!= 0);
1642 ASSERT(new_pc
== curthread
->t_dtrace_astpc
);
1652 fasttrap_return_probe(struct regs
*rp
)
1654 proc_t
*p
= curproc
;
1655 uintptr_t pc
= curthread
->t_dtrace_pc
;
1656 uintptr_t npc
= curthread
->t_dtrace_npc
;
1658 curthread
->t_dtrace_pc
= 0;
1659 curthread
->t_dtrace_npc
= 0;
1660 curthread
->t_dtrace_scrpc
= 0;
1661 curthread
->t_dtrace_astpc
= 0;
1664 * Treat a child created by a call to vfork(2) as if it were its
1665 * parent. We know that there's only one thread of control in such a
1666 * process: this one.
1668 while (p
->p_flag
& SVFORK
) {
1673 * We set rp->r_pc to the address of the traced instruction so
1674 * that it appears to dtrace_probe() that we're on the original
1675 * instruction, and so that the user can't easily detect our
1676 * complex web of lies. dtrace_return_probe() (our caller)
1677 * will correctly set %pc after we return.
1681 fasttrap_return_common(rp
, pc
, p
->p_pid
, npc
);
1688 fasttrap_pid_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
,
1691 return (fasttrap_anarg(ttolwp(curthread
)->lwp_regs
, 1, argno
));
1696 fasttrap_usdt_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
,
1699 return (fasttrap_anarg(ttolwp(curthread
)->lwp_regs
, 0, argno
));
1703 fasttrap_getreg(struct regs
*rp
, uint_t reg
)
1707 case REG_R15
: return (rp
->r_r15
);
1708 case REG_R14
: return (rp
->r_r14
);
1709 case REG_R13
: return (rp
->r_r13
);
1710 case REG_R12
: return (rp
->r_r12
);
1711 case REG_R11
: return (rp
->r_r11
);
1712 case REG_R10
: return (rp
->r_r10
);
1713 case REG_R9
: return (rp
->r_r9
);
1714 case REG_R8
: return (rp
->r_r8
);
1715 case REG_RDI
: return (rp
->r_rdi
);
1716 case REG_RSI
: return (rp
->r_rsi
);
1717 case REG_RBP
: return (rp
->r_rbp
);
1718 case REG_RBX
: return (rp
->r_rbx
);
1719 case REG_RDX
: return (rp
->r_rdx
);
1720 case REG_RCX
: return (rp
->r_rcx
);
1721 case REG_RAX
: return (rp
->r_rax
);
1722 case REG_TRAPNO
: return (rp
->r_trapno
);
1723 case REG_ERR
: return (rp
->r_err
);
1724 case REG_RIP
: return (rp
->r_rip
);
1725 case REG_CS
: return (rp
->r_cs
);
1726 case REG_RFL
: return (rp
->r_rfl
);
1727 case REG_RSP
: return (rp
->r_rsp
);
1728 case REG_SS
: return (rp
->r_ss
);
1729 case REG_FS
: return (rp
->r_fs
);
1730 case REG_GS
: return (rp
->r_gs
);
1731 case REG_DS
: return (rp
->r_ds
);
1732 case REG_ES
: return (rp
->r_es
);
1733 case REG_FSBASE
: return (rdmsr(MSR_AMD_FSBASE
));
1734 case REG_GSBASE
: return (rdmsr(MSR_AMD_GSBASE
));
1737 panic("dtrace: illegal register constant");
1741 panic("dtrace: illegal register constant");
1743 return (((greg_t
*)&rp
->r_gs
)[reg
]);