]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
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 *, unsigned int); | |
49 | ||
50 | kern_return_t | |
51 | dtrace_user_probe(arm_saved_state_t *regs, unsigned int instr) | |
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 | ||
63 | uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread()); | |
64 | ||
65 | kauth_cred_uthread_update(uthread, p); | |
66 | ||
67 | if (((regs->cpsr & PSR_TF) && ((uint16_t) instr) == FASTTRAP_THUMB_RET_INSTR) || | |
68 | ((uint32_t) instr == FASTTRAP_ARM_RET_INSTR)) { | |
69 | uint8_t step = uthread->t_dtrace_step; | |
70 | uint8_t ret = uthread->t_dtrace_ret; | |
71 | user_addr_t npc = uthread->t_dtrace_npc; | |
72 | ||
73 | if (uthread->t_dtrace_ast) { | |
74 | printf("dtrace_user_probe() should be calling aston()\n"); | |
75 | // aston(thread); | |
76 | // uthread->t_sig_check = 1; | |
77 | } | |
78 | ||
79 | /* | |
80 | * Clear all user tracing flags. | |
81 | */ | |
82 | uthread->t_dtrace_ft = 0; | |
83 | ||
84 | /* | |
85 | * If we weren't expecting to take a return probe trap, kill | |
86 | * the process as though it had just executed an unassigned | |
87 | * trap instruction. | |
88 | */ | |
89 | if (step == 0) { | |
90 | /* | |
91 | * APPLE NOTE: We're returning KERN_FAILURE, which causes | |
92 | * the generic signal handling code to take over, which will effectively | |
93 | * deliver a EXC_BAD_INSTRUCTION to the user process. | |
94 | */ | |
95 | return KERN_FAILURE; | |
96 | } | |
97 | ||
98 | /* | |
99 | * If we hit this trap unrelated to a return probe, we're | |
100 | * just here to reset the AST flag since we deferred a signal | |
101 | * until after we logically single-stepped the instruction we | |
102 | * copied out. | |
103 | */ | |
104 | if (ret == 0) { | |
105 | regs->pc = npc; | |
106 | return KERN_SUCCESS; | |
107 | } | |
108 | ||
109 | /* | |
110 | * We need to wait until after we've called the | |
111 | * dtrace_return_probe_ptr function pointer to step the pc. | |
112 | */ | |
113 | rwp = &CPU->cpu_ft_lock; | |
114 | lck_rw_lock_shared(rwp); | |
115 | ||
116 | if (dtrace_return_probe_ptr != NULL) | |
117 | (void) (*dtrace_return_probe_ptr)(regs); | |
118 | lck_rw_unlock_shared(rwp); | |
119 | ||
120 | regs->pc = npc; | |
121 | ||
122 | return KERN_SUCCESS; | |
123 | } else { | |
124 | rwp = &CPU->cpu_ft_lock; | |
125 | ||
126 | /* | |
127 | * The DTrace fasttrap provider uses a trap, | |
128 | * FASTTRAP_{ARM,THUMB}_INSTR. We let | |
129 | * DTrace take the first crack at handling | |
130 | * this trap; if it's not a probe that DTrace knows about, | |
131 | * we call into the trap() routine to handle it like a | |
132 | * breakpoint placed by a conventional debugger. | |
133 | */ | |
134 | ||
135 | /* | |
136 | * APPLE NOTE: I believe the purpose of the reader/writers lock | |
137 | * is thus: There are times which dtrace needs to prevent calling | |
138 | * dtrace_pid_probe_ptr(). Sun's original impl grabbed a plain | |
139 | * mutex here. However, that serialized all probe calls, and | |
140 | * destroyed MP behavior. So now they use a RW lock, with probes | |
141 | * as readers, and the top level synchronization as a writer. | |
142 | */ | |
143 | lck_rw_lock_shared(rwp); | |
144 | if (dtrace_pid_probe_ptr != NULL && | |
145 | (*dtrace_pid_probe_ptr)(regs) == 0) { | |
146 | lck_rw_unlock_shared(rwp); | |
147 | return KERN_SUCCESS; | |
148 | } | |
149 | lck_rw_unlock_shared(rwp); | |
150 | ||
151 | /* | |
152 | * If the instruction that caused the breakpoint trap doesn't | |
153 | * look like our trap anymore, it may be that this tracepoint | |
154 | * was removed just after the user thread executed it. In | |
155 | * that case, return to user land to retry the instuction. | |
156 | * | |
157 | * Note that the PC points to the instruction that caused the fault. | |
158 | */ | |
159 | if (regs->cpsr & PSR_TF) { | |
160 | uint16_t instr_check; | |
161 | if (fuword16(regs->pc, &instr_check) == 0 && instr_check != FASTTRAP_THUMB_INSTR) { | |
162 | return KERN_SUCCESS; | |
163 | } | |
164 | } else { | |
165 | uint32_t instr_check; | |
166 | if (fuword32(regs->pc, &instr_check) == 0 && instr_check != FASTTRAP_ARM_INSTR) { | |
167 | return KERN_SUCCESS; | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | return KERN_FAILURE; | |
173 | } | |
174 | ||
175 | void | |
176 | dtrace_safe_synchronous_signal(void) | |
177 | { | |
178 | /* Not implemented */ | |
179 | } | |
180 | ||
181 | int | |
182 | dtrace_safe_defer_signal(void) | |
183 | { | |
184 | /* Not implemented */ | |
185 | return 0; | |
186 | } |