]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/arm64/dtrace_subr_arm.c
584317298d4f14f69f71d4d87e52fc55e82f01b9
[apple/xnu.git] / bsd / dev / arm64 / dtrace_subr_arm.c
1 /*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 */
4 /*
5 * CDDL HEADER START
6 *
7 * The contents of this file are subject to the terms of the
8 * Common Development and Distribution License, Version 1.0 only
9 * (the "License"). You may not use this file except in compliance
10 * with the License.
11 *
12 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
13 * or http://www.opensolaris.org/os/licensing.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 *
17 * When distributing Covered Code, include this CDDL HEADER in each
18 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
19 * If applicable, add the following below this CDDL HEADER, with the
20 * fields enclosed by brackets "[]" replaced with your own identifying
21 * information: Portions Copyright [yyyy] [name of copyright owner]
22 *
23 * CDDL HEADER END
24 */
25 /*
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 /*
31 * #pragma ident "@(#)dtrace_subr.c 1.12 05/06/08 SMI"
32 */
33
34 #include <sys/dtrace.h>
35 #include <sys/dtrace_glue.h>
36 #include <sys/dtrace_impl.h>
37 #include <sys/fasttrap.h>
38 #include <sys/vm.h>
39 #include <sys/user.h>
40 #include <sys/kauth.h>
41 #include <kern/debug.h>
42 #include <arm/proc_reg.h>
43
44 int (*dtrace_pid_probe_ptr)(arm_saved_state_t *);
45 int (*dtrace_return_probe_ptr) (arm_saved_state_t *);
46
47 kern_return_t
48 dtrace_user_probe(arm_saved_state_t *);
49
50 kern_return_t
51 dtrace_user_probe(arm_saved_state_t *regs)
52 {
53 /*
54 * FIXME
55 *
56 * The only call path into this method is always a user trap.
57 * We don't need to test for user trap, but should assert it.
58 */
59
60 lck_rw_t *rwp;
61 struct proc *p = current_proc();
62 int is_fasttrap = 0;
63
64 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
65
66 kauth_cred_uthread_update(uthread, p);
67
68 if (is_saved_state32(regs)) {
69 if (saved_state32(regs)->cpsr & PSR_TF) {
70 uint16_t pc;
71 if (copyin((user_addr_t)saved_state32(regs)->pc, &pc, sizeof(uint16_t))) {
72 return KERN_FAILURE;
73 }
74 is_fasttrap = (pc == FASTTRAP_THUMB32_RET_INSTR);
75 } else {
76 uint32_t pc;
77 if (copyin((user_addr_t)saved_state32(regs)->pc, &pc, sizeof(uint32_t))) {
78 return KERN_FAILURE;
79 }
80 is_fasttrap = (pc == FASTTRAP_ARM32_RET_INSTR);
81 }
82 } else {
83 uint32_t pc;
84 if (copyin((user_addr_t)saved_state64(regs)->pc, &pc, sizeof(uint32_t))) {
85 return KERN_FAILURE;
86 }
87 is_fasttrap = (pc == FASTTRAP_ARM64_RET_INSTR);
88 }
89
90 if (is_fasttrap) {
91 uint8_t step = uthread->t_dtrace_step;
92 uint8_t ret = uthread->t_dtrace_ret;
93 user_addr_t npc = uthread->t_dtrace_npc;
94
95 if (uthread->t_dtrace_ast) {
96 printf("dtrace_user_probe() should be calling aston()\n");
97 // aston(thread);
98 // uthread->t_sig_check = 1;
99 }
100
101 /*
102 * Clear all user tracing flags.
103 */
104 uthread->t_dtrace_ft = 0;
105
106 /*
107 * If we weren't expecting a quick return to the kernel, just kill
108 * the process as though it had just executed an unassigned
109 * trap instruction.
110 */
111 if (step == 0) {
112 /*
113 * APPLE NOTE: We're returning KERN_FAILURE, which causes
114 * the generic signal handling code to take over, which will effectively
115 * deliver a EXC_BAD_INSTRUCTION to the user process.
116 */
117 return KERN_FAILURE;
118 }
119
120 /*
121 * If we hit this trap unrelated to a return probe, we're
122 * here to either:
123 *
124 * 1. Reset the AST flag, since we deferred a signal
125 * until after we logically single-stepped the instruction we
126 * copied out.
127 *
128 * 2. Just return to normal execution (required for U64).
129 */
130 if (ret == 0) {
131 set_saved_state_pc(regs, npc);
132 return KERN_SUCCESS;
133 }
134
135 /*
136 * We need to wait until after we've called the
137 * dtrace_return_probe_ptr function pointer to step the pc.
138 */
139 rwp = &CPU->cpu_ft_lock;
140 lck_rw_lock_shared(rwp);
141
142 if (dtrace_return_probe_ptr != NULL) {
143 (void) (*dtrace_return_probe_ptr)(regs);
144 }
145 lck_rw_unlock_shared(rwp);
146
147 set_saved_state_pc(regs, npc);
148
149 return KERN_SUCCESS;
150 } else {
151 rwp = &CPU->cpu_ft_lock;
152
153 /*
154 * The DTrace fasttrap provider uses a trap,
155 * FASTTRAP_{ARM,THUMB}_INSTR. We let
156 * DTrace take the first crack at handling
157 * this trap; if it's not a probe that DTrace knows about,
158 * we call into the trap() routine to handle it like a
159 * breakpoint placed by a conventional debugger.
160 */
161
162 /*
163 * APPLE NOTE: I believe the purpose of the reader/writers lock
164 * is thus: There are times which dtrace needs to prevent calling
165 * dtrace_pid_probe_ptr(). Sun's original impl grabbed a plain
166 * mutex here. However, that serialized all probe calls, and
167 * destroyed MP behavior. So now they use a RW lock, with probes
168 * as readers, and the top level synchronization as a writer.
169 */
170 lck_rw_lock_shared(rwp);
171 if (dtrace_pid_probe_ptr != NULL &&
172 (*dtrace_pid_probe_ptr)(regs) == 0) {
173 lck_rw_unlock_shared(rwp);
174 return KERN_SUCCESS;
175 }
176 lck_rw_unlock_shared(rwp);
177
178 /*
179 * If the instruction that caused the breakpoint trap doesn't
180 * look like our trap anymore, it may be that this tracepoint
181 * was removed just after the user thread executed it. In
182 * that case, return to user land to retry the instuction.
183 *
184 * Note that the PC points to the instruction that caused the fault.
185 */
186 if (is_saved_state32(regs)) {
187 if (saved_state32(regs)->cpsr & PSR_TF) {
188 uint16_t instr;
189 if (fuword16(saved_state32(regs)->pc, &instr) == 0 && instr != FASTTRAP_THUMB32_INSTR) {
190 return KERN_SUCCESS;
191 }
192 } else {
193 uint32_t instr;
194 if (fuword32(saved_state32(regs)->pc, &instr) == 0 && instr != FASTTRAP_ARM32_INSTR) {
195 return KERN_SUCCESS;
196 }
197 }
198 } else {
199 uint32_t instr;
200 if (fuword32(saved_state64(regs)->pc, &instr) == 0 && instr != FASTTRAP_ARM64_INSTR) {
201 return KERN_SUCCESS;
202 }
203 }
204 }
205
206 return KERN_FAILURE;
207 }
208
209 void
210 dtrace_safe_synchronous_signal(void)
211 {
212 /* Not implemented */
213 }
214
215 int
216 dtrace_safe_defer_signal(void)
217 {
218 /* Not implemented */
219 return 0;
220 }