]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/dtrace_subr_x86.c
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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/dtrace.h>
28 #include <sys/dtrace_glue.h>
29 #include <sys/dtrace_impl.h>
30 #include <sys/fasttrap.h>
33 #include <sys/kauth.h>
34 #include <kern/debug.h>
36 int (*dtrace_pid_probe_ptr
)(x86_saved_state_t
*);
37 int (*dtrace_return_probe_ptr
)(x86_saved_state_t
*);
40 * HACK! There doesn't seem to be an easy way to include trap.h from
43 #define T_INT3 3 /* int 3 instruction */
44 #define T_DTRACE_RET 0x7f /* DTrace pid return */
47 dtrace_user_probe(x86_saved_state_t
*);
50 dtrace_user_probe(x86_saved_state_t
*regs
)
52 x86_saved_state64_t
*regs64
;
53 x86_saved_state32_t
*regs32
;
59 * The only call path into this method is always a user trap.
60 * We don't need to test for user trap, but should assert it.
62 boolean_t user_mode
= TRUE
;
64 if (is_saved_state64(regs
) == TRUE
) {
65 regs64
= saved_state64(regs
);
67 trapno
= regs64
->isf
.trapno
;
68 user_mode
= TRUE
; // By default, because xnu is 32 bit only
71 regs32
= saved_state32(regs
);
72 if (regs32
->cs
& 0x03) user_mode
= TRUE
;
73 trapno
= regs32
->trapno
;
77 struct proc
*p
= current_proc();
79 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
80 if (user_mode
/*|| (rp->r_ps & PS_VM)*/) {
82 * DTrace accesses t_cred in probe context. t_cred
83 * must always be either NULL, or point to a valid,
84 * allocated cred structure.
86 kauth_cred_uthread_update(uthread
, p
);
89 if (trapno
== T_DTRACE_RET
) {
90 uint8_t step
= uthread
->t_dtrace_step
;
91 uint8_t ret
= uthread
->t_dtrace_ret
;
92 user_addr_t npc
= uthread
->t_dtrace_npc
;
94 if (uthread
->t_dtrace_ast
) {
95 printf("dtrace_user_probe() should be calling aston()\n");
97 // uthread->t_sig_check = 1;
101 * Clear all user tracing flags.
103 uthread
->t_dtrace_ft
= 0;
106 * If we weren't expecting to take a return probe trap, kill
107 * the process as though it had just executed an unassigned
112 * APPLE NOTE: We're returning KERN_FAILURE, which causes
113 * the generic signal handling code to take over, which will effectively
114 * deliver a EXC_BAD_INSTRUCTION to the user process.
120 * If we hit this trap unrelated to a return probe, we're
121 * just here to reset the AST flag since we deferred a signal
122 * until after we logically single-stepped the instruction we
127 regs64
->isf
.rip
= npc
;
129 regs32
->eip
= (uint32_t)npc
;
135 * We need to wait until after we've called the
136 * dtrace_return_probe_ptr function pointer to set %pc.
138 rwp
= &CPU
->cpu_ft_lock
;
139 lck_rw_lock_shared(rwp
);
141 if (dtrace_return_probe_ptr
!= NULL
)
142 (void) (*dtrace_return_probe_ptr
)(regs
);
143 lck_rw_unlock_shared(rwp
);
146 regs64
->isf
.rip
= npc
;
148 regs32
->eip
= (uint32_t)npc
;
152 } else if (trapno
== T_INT3
) {
153 uint8_t instr
, instr2
;
154 rwp
= &CPU
->cpu_ft_lock
;
157 * The DTrace fasttrap provider uses the breakpoint trap
158 * (int 3). We let DTrace take the first crack at handling
159 * this trap; if it's not a probe that DTrace knowns about,
160 * we call into the trap() routine to handle it like a
161 * breakpoint placed by a conventional debugger.
165 * APPLE NOTE: I believe the purpose of the reader/writers lock
166 * is thus: There are times which dtrace needs to prevent calling
167 * dtrace_pid_probe_ptr(). Sun's original impl grabbed a plain
168 * mutex here. However, that serialized all probe calls, and
169 * destroyed MP behavior. So now they use a RW lock, with probes
170 * as readers, and the top level synchronization as a writer.
172 lck_rw_lock_shared(rwp
);
173 if (dtrace_pid_probe_ptr
!= NULL
&&
174 (*dtrace_pid_probe_ptr
)(regs
) == 0) {
175 lck_rw_unlock_shared(rwp
);
178 lck_rw_unlock_shared(rwp
);
182 * If the instruction that caused the breakpoint trap doesn't
183 * look like an int 3 anymore, it may be that this tracepoint
184 * was removed just after the user thread executed it. In
185 * that case, return to user land to retry the instuction.
187 user_addr_t pc
= (regs64
) ? regs64
->isf
.rip
: (user_addr_t
)regs32
->eip
;
188 if (fuword8(pc
- 1, &instr
) == 0 && instr
!= FASTTRAP_INSTR
&& // neither single-byte INT3 (0xCC)
189 !(instr
== 3 && fuword8(pc
- 2, &instr2
) == 0 && instr2
== 0xCD)) { // nor two-byte INT 3 (0xCD03)
204 dtrace_flush_caches(void)