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, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 *
22 */
23 /*
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/kmem.h>
33 #include <sys/proc.h>
34 #include <sys/smp.h>
35 #include <sys/dtrace_impl.h>
36 #include <sys/dtrace_bsd.h>
37 #include <cddl/dev/dtrace/dtrace_cddl.h>
38 #include <machine/armreg.h>
39 #include <machine/clock.h>
40 #include <machine/frame.h>
41 #include <machine/trap.h>
42 #include <machine/vmparam.h>
43 #include <vm/pmap.h>
44
45 extern dtrace_id_t dtrace_probeid_error;
46 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
47 extern void dtrace_getnanotime(struct timespec *tsp);
48 extern void dtrace_getnanouptime(struct timespec *tsp);
49
50 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
51 void dtrace_invop_init(void);
52 void dtrace_invop_uninit(void);
53
54 typedef struct dtrace_invop_hdlr {
55 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
56 struct dtrace_invop_hdlr *dtih_next;
57 } dtrace_invop_hdlr_t;
58
59 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
60
61 int
dtrace_invop(uintptr_t addr,struct trapframe * frame,uintptr_t eax)62 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
63 {
64 struct thread *td;
65 dtrace_invop_hdlr_t *hdlr;
66 int rval;
67
68 rval = 0;
69 td = curthread;
70 td->t_dtrace_trapframe = frame;
71 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
72 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
73 break;
74 td->t_dtrace_trapframe = NULL;
75 return (rval);
76 }
77
78 void
dtrace_invop_add(int (* func)(uintptr_t,struct trapframe *,uintptr_t))79 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
80 {
81 dtrace_invop_hdlr_t *hdlr;
82
83 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
84 hdlr->dtih_func = func;
85 hdlr->dtih_next = dtrace_invop_hdlr;
86 dtrace_invop_hdlr = hdlr;
87 }
88
89 void
dtrace_invop_remove(int (* func)(uintptr_t,struct trapframe *,uintptr_t))90 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
91 {
92 dtrace_invop_hdlr_t *hdlr, *prev;
93
94 hdlr = dtrace_invop_hdlr;
95 prev = NULL;
96
97 for (;;) {
98 if (hdlr == NULL)
99 panic("attempt to remove non-existent invop handler");
100
101 if (hdlr->dtih_func == func)
102 break;
103
104 prev = hdlr;
105 hdlr = hdlr->dtih_next;
106 }
107
108 if (prev == NULL) {
109 ASSERT(dtrace_invop_hdlr == hdlr);
110 dtrace_invop_hdlr = hdlr->dtih_next;
111 } else {
112 ASSERT(dtrace_invop_hdlr != hdlr);
113 prev->dtih_next = hdlr->dtih_next;
114 }
115
116 kmem_free(hdlr, 0);
117 }
118
119 /*ARGSUSED*/
120 void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))121 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
122 {
123
124 (*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
125 }
126
127 void
dtrace_xcall(processorid_t cpu,dtrace_xcall_t func,void * arg)128 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
129 {
130 cpuset_t cpus;
131
132 if (cpu == DTRACE_CPUALL)
133 cpus = all_cpus;
134 else
135 CPU_SETOF(cpu, &cpus);
136
137 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
138 smp_no_rendezvous_barrier, arg);
139 }
140
141 static void
dtrace_sync_func(void)142 dtrace_sync_func(void)
143 {
144
145 }
146
147 void
dtrace_sync(void)148 dtrace_sync(void)
149 {
150
151 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
152 }
153
154 /*
155 * DTrace needs a high resolution time function which can be called from a
156 * probe context and guaranteed not to have instrumented with probes itself.
157 *
158 * Returns nanoseconds since some arbitrary point in time (likely SoC reset?).
159 */
160 uint64_t
dtrace_gethrtime(void)161 dtrace_gethrtime(void)
162 {
163 uint64_t count, freq;
164
165 count = READ_SPECIALREG(cntvct_el0);
166 freq = READ_SPECIALREG(cntfrq_el0);
167 return ((1000000000UL * count) / freq);
168 }
169
170 /*
171 * Return a much lower resolution wallclock time based on the system clock
172 * updated by the timer. If needed, we could add a version interpolated from
173 * the system clock as is the case with dtrace_gethrtime().
174 */
175 uint64_t
dtrace_gethrestime(void)176 dtrace_gethrestime(void)
177 {
178 struct timespec current_time;
179
180 dtrace_getnanotime(¤t_time);
181
182 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
183 }
184
185 /* Function to handle DTrace traps during probes. See arm64/arm64/trap.c */
186 int
dtrace_trap(struct trapframe * frame,u_int type)187 dtrace_trap(struct trapframe *frame, u_int type)
188 {
189 /*
190 * A trap can occur while DTrace executes a probe. Before
191 * executing the probe, DTrace blocks re-scheduling and sets
192 * a flag in its per-cpu flags to indicate that it doesn't
193 * want to fault. On returning from the probe, the no-fault
194 * flag is cleared and finally re-scheduling is enabled.
195 *
196 * Check if DTrace has enabled 'no-fault' mode:
197 *
198 */
199
200 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
201 /*
202 * There are only a couple of trap types that are expected.
203 * All the rest will be handled in the usual way.
204 */
205 switch (type) {
206 case EXCP_DATA_ABORT:
207 /* Flag a bad address. */
208 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
209 cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_far;
210
211 /*
212 * Offset the instruction pointer to the instruction
213 * following the one causing the fault.
214 */
215 frame->tf_elr += 4;
216 return (1);
217 default:
218 /* Handle all other traps in the usual way. */
219 break;
220 }
221 }
222
223 /* Handle the trap in the usual way. */
224 return (0);
225 }
226
227 void
dtrace_probe_error(dtrace_state_t * state,dtrace_epid_t epid,int which,int fault,int fltoffs,uintptr_t illval)228 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
229 int fault, int fltoffs, uintptr_t illval)
230 {
231
232 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
233 (uintptr_t)epid,
234 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
235 }
236
237 static void
dtrace_load64(uint64_t * addr,struct trapframe * frame,u_int reg)238 dtrace_load64(uint64_t *addr, struct trapframe *frame, u_int reg)
239 {
240
241 KASSERT(reg <= 31, ("dtrace_load64: Invalid register %u", reg));
242 if (reg < nitems(frame->tf_x))
243 frame->tf_x[reg] = *addr;
244 else if (reg == 30) /* lr */
245 frame->tf_lr = *addr;
246 /* Nothing to do for load to xzr */
247 }
248
249 static void
dtrace_store64(uint64_t * addr,struct trapframe * frame,u_int reg)250 dtrace_store64(uint64_t *addr, struct trapframe *frame, u_int reg)
251 {
252
253 KASSERT(reg <= 31, ("dtrace_store64: Invalid register %u", reg));
254 if (reg < nitems(frame->tf_x))
255 *addr = frame->tf_x[reg];
256 else if (reg == 30) /* lr */
257 *addr = frame->tf_lr;
258 else if (reg == 31) /* xzr */
259 *addr = 0;
260 }
261
262 static int
dtrace_invop_start(struct trapframe * frame)263 dtrace_invop_start(struct trapframe *frame)
264 {
265 int data, invop, tmp;
266
267 invop = dtrace_invop(frame->tf_elr, frame, frame->tf_x[0]);
268
269 tmp = (invop & LDP_STP_MASK);
270 if (tmp == STP_64 || tmp == LDP_64) {
271 register_t arg1, arg2, *sp;
272 int offs;
273
274 sp = (register_t *)frame->tf_sp;
275 data = invop;
276 arg1 = (data >> ARG1_SHIFT) & ARG1_MASK;
277 arg2 = (data >> ARG2_SHIFT) & ARG2_MASK;
278
279 offs = (data >> OFFSET_SHIFT) & OFFSET_MASK;
280
281 switch (tmp) {
282 case STP_64:
283 if (offs >> (OFFSET_SIZE - 1))
284 sp -= (~offs & OFFSET_MASK) + 1;
285 else
286 sp += (offs);
287 dtrace_store64(sp + 0, frame, arg1);
288 dtrace_store64(sp + 1, frame, arg2);
289 break;
290 case LDP_64:
291 dtrace_load64(sp + 0, frame, arg1);
292 dtrace_load64(sp + 1, frame, arg2);
293 if (offs >> (OFFSET_SIZE - 1))
294 sp -= (~offs & OFFSET_MASK) + 1;
295 else
296 sp += (offs);
297 break;
298 default:
299 break;
300 }
301
302 /* Update the stack pointer and program counter to continue */
303 frame->tf_sp = (register_t)sp;
304 frame->tf_elr += INSN_SIZE;
305 return (0);
306 }
307
308 if ((invop & SUB_MASK) == SUB_INSTR) {
309 frame->tf_sp -= (invop >> SUB_IMM_SHIFT) & SUB_IMM_MASK;
310 frame->tf_elr += INSN_SIZE;
311 return (0);
312 }
313
314 if (invop == NOP_INSTR) {
315 frame->tf_elr += INSN_SIZE;
316 return (0);
317 }
318
319 if ((invop & B_MASK) == B_INSTR) {
320 data = (invop & B_DATA_MASK);
321 /* The data is the number of 4-byte words to change the pc */
322 data *= 4;
323 frame->tf_elr += data;
324 return (0);
325 }
326
327 if (invop == RET_INSTR) {
328 frame->tf_elr = frame->tf_lr;
329 return (0);
330 }
331
332 return (-1);
333 }
334
335 void
dtrace_invop_init(void)336 dtrace_invop_init(void)
337 {
338
339 dtrace_invop_jump_addr = dtrace_invop_start;
340 }
341
342 void
dtrace_invop_uninit(void)343 dtrace_invop_uninit(void)
344 {
345
346 dtrace_invop_jump_addr = 0;
347 }
348