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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
31 #include <sys/dtrace.h>
32 #include <sys/fasttrap.h>
33 #include <sys/x_call.h>
34 #include <sys/cmn_err.h>
37 #include <sys/privregs.h>
38 #include <sys/machsystm.h>
39 #include <vm/seg_kmem.h>
41 typedef struct dtrace_invop_hdlr
{
42 int (*dtih_func
)(uintptr_t, uintptr_t *, uintptr_t);
43 struct dtrace_invop_hdlr
*dtih_next
;
44 } dtrace_invop_hdlr_t
;
46 dtrace_invop_hdlr_t
*dtrace_invop_hdlr
;
49 dtrace_invop(uintptr_t addr
, uintptr_t *stack
, uintptr_t eax
)
51 dtrace_invop_hdlr_t
*hdlr
;
54 for (hdlr
= dtrace_invop_hdlr
; hdlr
!= NULL
; hdlr
= hdlr
->dtih_next
) {
55 if ((rval
= hdlr
->dtih_func(addr
, stack
, eax
)) != 0)
63 dtrace_invop_add(int (*func
)(uintptr_t, uintptr_t *, uintptr_t))
65 dtrace_invop_hdlr_t
*hdlr
;
67 hdlr
= kmem_alloc(sizeof (dtrace_invop_hdlr_t
), KM_SLEEP
);
68 hdlr
->dtih_func
= func
;
69 hdlr
->dtih_next
= dtrace_invop_hdlr
;
70 dtrace_invop_hdlr
= hdlr
;
74 dtrace_invop_remove(int (*func
)(uintptr_t, uintptr_t *, uintptr_t))
76 dtrace_invop_hdlr_t
*hdlr
= dtrace_invop_hdlr
, *prev
= NULL
;
80 panic("attempt to remove non-existent invop handler");
82 if (hdlr
->dtih_func
== func
)
86 hdlr
= hdlr
->dtih_next
;
90 ASSERT(dtrace_invop_hdlr
== hdlr
);
91 dtrace_invop_hdlr
= hdlr
->dtih_next
;
93 ASSERT(dtrace_invop_hdlr
!= hdlr
);
94 prev
->dtih_next
= hdlr
->dtih_next
;
97 kmem_free(hdlr
, sizeof (dtrace_invop_hdlr_t
));
103 return (CPU
->cpu_pri
);
108 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
111 extern uintptr_t toxic_addr
;
112 extern size_t toxic_size
;
114 (*func
)(0, _userlimit
);
116 if (hole_end
> hole_start
)
117 (*func
)(hole_start
, hole_end
);
118 (*func
)(toxic_addr
, toxic_addr
+ toxic_size
);
120 extern void *device_arena_contains(void *, size_t, size_t *);
124 for (vaddr
= (caddr_t
)kernelbase
; vaddr
< (caddr_t
)KERNEL_TEXT
;
126 len
= (caddr_t
)KERNEL_TEXT
- vaddr
;
127 vaddr
= device_arena_contains(vaddr
, len
, &len
);
130 (*func
)((uintptr_t)vaddr
, (uintptr_t)vaddr
+ len
);
133 (*func
)(0, _userlimit
);
137 dtrace_xcall_func(dtrace_xcall_t func
, void *arg
)
146 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t func
, void *arg
)
152 if (cpu
== DTRACE_CPUALL
) {
155 CPUSET_ADD(set
, cpu
);
159 xc_sync((xc_arg_t
)func
, (xc_arg_t
)arg
, 0, CPUSET2BV(set
),
160 (xc_func_t
)dtrace_xcall_func
);
165 dtrace_sync_func(void)
171 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)dtrace_sync_func
, NULL
);
174 int (*dtrace_pid_probe_ptr
)(struct regs
*);
175 int (*dtrace_return_probe_ptr
)(struct regs
*);
178 dtrace_user_probe(struct regs
*rp
, caddr_t addr
, processorid_t cpuid
)
182 extern void trap(struct regs
*, caddr_t
, processorid_t
);
184 if (USERMODE(rp
->r_cs
) || (rp
->r_ps
& PS_VM
)) {
185 if (curthread
->t_cred
!= p
->p_cred
) {
186 cred_t
*oldcred
= curthread
->t_cred
;
188 * DTrace accesses t_cred in probe context. t_cred
189 * must always be either NULL, or point to a valid,
190 * allocated cred structure.
192 curthread
->t_cred
= crgetcred();
197 if (rp
->r_trapno
== T_DTRACE_RET
) {
198 uint8_t step
= curthread
->t_dtrace_step
;
199 uint8_t ret
= curthread
->t_dtrace_ret
;
200 uintptr_t npc
= curthread
->t_dtrace_npc
;
202 if (curthread
->t_dtrace_ast
) {
204 curthread
->t_sig_check
= 1;
208 * Clear all user tracing flags.
210 curthread
->t_dtrace_ft
= 0;
213 * If we weren't expecting to take a return probe trap, kill
214 * the process as though it had just executed an unassigned
218 tsignal(curthread
, SIGILL
);
223 * If we hit this trap unrelated to a return probe, we're
224 * just here to reset the AST flag since we deferred a signal
225 * until after we logically single-stepped the instruction we
234 * We need to wait until after we've called the
235 * dtrace_return_probe_ptr function pointer to set %pc.
237 rwp
= &CPU
->cpu_ft_lock
;
238 rw_enter(rwp
, RW_READER
);
239 if (dtrace_return_probe_ptr
!= NULL
)
240 (void) (*dtrace_return_probe_ptr
)(rp
);
244 } else if (rp
->r_trapno
== T_BPTFLT
) {
245 uint8_t instr
, instr2
;
247 rwp
= &CPU
->cpu_ft_lock
;
250 * The DTrace fasttrap provider uses the breakpoint trap
251 * (int 3). We let DTrace take the first crack at handling
252 * this trap; if it's not a probe that DTrace knowns about,
253 * we call into the trap() routine to handle it like a
254 * breakpoint placed by a conventional debugger.
256 rw_enter(rwp
, RW_READER
);
257 if (dtrace_pid_probe_ptr
!= NULL
&&
258 (*dtrace_pid_probe_ptr
)(rp
) == 0) {
264 if (dtrace_linear_pc(rp
, p
, &linearpc
) != 0) {
265 trap(rp
, addr
, cpuid
);
270 * If the instruction that caused the breakpoint trap doesn't
271 * look like an int 3 anymore, it may be that this tracepoint
272 * was removed just after the user thread executed it. In
273 * that case, return to user land to retry the instuction.
274 * Note that we assume the length of the instruction to retry
275 * is 1 byte because that's the length of FASTTRAP_INSTR.
276 * We check for r_pc > 0 and > 2 so that we don't have to
277 * deal with segment wraparound.
279 if (rp
->r_pc
> 0 && fuword8(linearpc
- 1, &instr
) == 0 &&
280 instr
!= FASTTRAP_INSTR
&&
281 (instr
!= 3 || (rp
->r_pc
>= 2 &&
282 (fuword8(linearpc
- 2, &instr2
) != 0 || instr2
!= 0xCD)))) {
287 trap(rp
, addr
, cpuid
);
290 trap(rp
, addr
, cpuid
);
295 dtrace_safe_synchronous_signal(void)
297 kthread_t
*t
= curthread
;
298 struct regs
*rp
= lwptoregs(ttolwp(t
));
299 size_t isz
= t
->t_dtrace_npc
- t
->t_dtrace_pc
;
301 ASSERT(t
->t_dtrace_on
);
304 * If we're not in the range of scratch addresses, we're not actually
305 * tracing user instructions so turn off the flags. If the instruction
306 * we copied out caused a synchonous trap, reset the pc back to its
307 * original value and turn off the flags.
309 if (rp
->r_pc
< t
->t_dtrace_scrpc
||
310 rp
->r_pc
> t
->t_dtrace_astpc
+ isz
) {
312 } else if (rp
->r_pc
== t
->t_dtrace_scrpc
||
313 rp
->r_pc
== t
->t_dtrace_astpc
) {
314 rp
->r_pc
= t
->t_dtrace_pc
;
320 dtrace_safe_defer_signal(void)
322 kthread_t
*t
= curthread
;
323 struct regs
*rp
= lwptoregs(ttolwp(t
));
324 size_t isz
= t
->t_dtrace_npc
- t
->t_dtrace_pc
;
326 ASSERT(t
->t_dtrace_on
);
329 * If we're not in the range of scratch addresses, we're not actually
330 * tracing user instructions so turn off the flags.
332 if (rp
->r_pc
< t
->t_dtrace_scrpc
||
333 rp
->r_pc
> t
->t_dtrace_astpc
+ isz
) {
339 * If we have executed the original instruction, but we have performed
340 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
341 * registers used to emulate %rip-relative instructions in 64-bit mode,
342 * we'll save ourselves some effort by doing that here and taking the
343 * signal right away. We detect this condition by seeing if the program
344 * counter is the range [scrpc + isz, astpc).
346 if (rp
->r_pc
>= t
->t_dtrace_scrpc
+ isz
&&
347 rp
->r_pc
< t
->t_dtrace_astpc
) {
350 * If there is a scratch register and we're on the
351 * instruction immediately after the modified instruction,
352 * restore the value of that scratch register.
354 if (t
->t_dtrace_reg
!= 0 &&
355 rp
->r_pc
== t
->t_dtrace_scrpc
+ isz
) {
356 switch (t
->t_dtrace_reg
) {
358 rp
->r_rax
= t
->t_dtrace_regv
;
361 rp
->r_rcx
= t
->t_dtrace_regv
;
364 rp
->r_r8
= t
->t_dtrace_regv
;
367 rp
->r_r9
= t
->t_dtrace_regv
;
372 rp
->r_pc
= t
->t_dtrace_npc
;
378 * Otherwise, make sure we'll return to the kernel after executing
379 * the copied out instruction and defer the signal.
381 if (!t
->t_dtrace_step
) {
382 ASSERT(rp
->r_pc
< t
->t_dtrace_astpc
);
383 rp
->r_pc
+= t
->t_dtrace_astpc
- t
->t_dtrace_scrpc
;
384 t
->t_dtrace_step
= 1;
393 * Additional artificial frames for the machine type. For i86pc, we're already
394 * accounted for, so return 0. On the hypervisor, we have an additional frame
395 * (xen_callback_handler).
398 dtrace_mach_aframes(void)