]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/dtrace_subr_x86.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / dev / i386 / dtrace_subr_x86.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <sys/dtrace.h>
28 #include <sys/dtrace_glue.h>
29 #include <sys/dtrace_impl.h>
30 #include <sys/fasttrap.h>
31 #include <sys/vm.h>
32 #include <sys/user.h>
33 #include <sys/kauth.h>
34 #include <kern/debug.h>
35
36 int (*dtrace_pid_probe_ptr)(x86_saved_state_t *);
37 int (*dtrace_return_probe_ptr)(x86_saved_state_t *);
38
39 /*
40 * HACK! There doesn't seem to be an easy way to include trap.h from
41 * here. FIXME!
42 */
43 #define T_INT3 3 /* int 3 instruction */
44 #define T_DTRACE_RET 0x7f /* DTrace pid return */
45
46 kern_return_t
47 dtrace_user_probe(x86_saved_state_t *);
48
49 kern_return_t
50 dtrace_user_probe(x86_saved_state_t *regs)
51 {
52 x86_saved_state64_t *regs64;
53 x86_saved_state32_t *regs32;
54 int trapno;
55
56 /*
57 * FIXME!
58 *
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.
61 */
62 boolean_t user_mode = TRUE;
63
64 if (is_saved_state64(regs) == TRUE) {
65 regs64 = saved_state64(regs);
66 regs32 = NULL;
67 trapno = regs64->isf.trapno;
68 user_mode = TRUE; // By default, because xnu is 32 bit only
69 } else {
70 regs64 = NULL;
71 regs32 = saved_state32(regs);
72 if (regs32->cs & 0x03) user_mode = TRUE;
73 trapno = regs32->trapno;
74 }
75
76 lck_rw_t *rwp;
77 struct proc *p = current_proc();
78
79 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
80 if (user_mode /*|| (rp->r_ps & PS_VM)*/) {
81 /*
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.
85 */
86 kauth_cred_uthread_update(uthread, p);
87 }
88
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;
93
94 if (uthread->t_dtrace_ast) {
95 printf("dtrace_user_probe() should be calling aston()\n");
96 // aston(uthread);
97 // uthread->t_sig_check = 1;
98 }
99
100 /*
101 * Clear all user tracing flags.
102 */
103 uthread->t_dtrace_ft = 0;
104
105 /*
106 * If we weren't expecting to take a return probe trap, kill
107 * the process as though it had just executed an unassigned
108 * trap instruction.
109 */
110 if (step == 0) {
111 /*
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.
115 */
116 return KERN_FAILURE;
117 }
118
119 /*
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
123 * copied out.
124 */
125 if (ret == 0) {
126 if (regs64) {
127 regs64->isf.rip = npc;
128 } else {
129 regs32->eip = (uint32_t)npc;
130 }
131 return KERN_SUCCESS;
132 }
133
134 /*
135 * We need to wait until after we've called the
136 * dtrace_return_probe_ptr function pointer to set %pc.
137 */
138 rwp = &CPU->cpu_ft_lock;
139 lck_rw_lock_shared(rwp);
140
141 if (dtrace_return_probe_ptr != NULL)
142 (void) (*dtrace_return_probe_ptr)(regs);
143 lck_rw_unlock_shared(rwp);
144
145 if (regs64) {
146 regs64->isf.rip = npc;
147 } else {
148 regs32->eip = (uint32_t)npc;
149 }
150
151 return KERN_SUCCESS;
152 } else if (trapno == T_INT3) {
153 uint8_t instr, instr2;
154 rwp = &CPU->cpu_ft_lock;
155
156 /*
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.
162 */
163
164 /*
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.
171 */
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);
176 return KERN_SUCCESS;
177 }
178 lck_rw_unlock_shared(rwp);
179
180
181 /*
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.
186 */
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)
190 if (regs64) {
191 regs64->isf.rip--;
192 } else {
193 regs32->eip--;
194 }
195 return KERN_SUCCESS;
196 }
197
198 }
199
200 return KERN_FAILURE;
201 }
202
203 void
204 dtrace_flush_caches(void)
205 {
206
207 }