xref: /freebsd/sys/cddl/dev/dtrace/arm/dtrace_subr.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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  * $FreeBSD$
23  *
24  */
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/kmem.h>
39 #include <sys/smp.h>
40 #include <sys/dtrace_impl.h>
41 #include <sys/dtrace_bsd.h>
42 #include <machine/armreg.h>
43 #include <machine/clock.h>
44 #include <machine/frame.h>
45 #include <machine/trap.h>
46 #include <vm/pmap.h>
47 
48 #define	DELAYBRANCH(x)	((int)(x) < 0)
49 
50 #define	BIT_PC		15
51 #define	BIT_LR		14
52 #define	BIT_SP		13
53 
54 extern uintptr_t 	dtrace_in_probe_addr;
55 extern int		dtrace_in_probe;
56 extern dtrace_id_t	dtrace_probeid_error;
57 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
58 extern void dtrace_getnanotime(struct timespec *tsp);
59 
60 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
61 void dtrace_invop_init(void);
62 void dtrace_invop_uninit(void);
63 
64 typedef struct dtrace_invop_hdlr {
65 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
66 	struct dtrace_invop_hdlr *dtih_next;
67 } dtrace_invop_hdlr_t;
68 
69 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
70 
71 int
72 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
73 {
74 	dtrace_invop_hdlr_t *hdlr;
75 	int rval;
76 
77 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
78 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
79 			return (rval);
80 
81 	return (0);
82 }
83 
84 
85 void
86 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
87 {
88 	dtrace_invop_hdlr_t *hdlr;
89 
90 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
91 	hdlr->dtih_func = func;
92 	hdlr->dtih_next = dtrace_invop_hdlr;
93 	dtrace_invop_hdlr = hdlr;
94 }
95 
96 void
97 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
98 {
99 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
100 
101 	for (;;) {
102 		if (hdlr == NULL)
103 			panic("attempt to remove non-existent invop handler");
104 
105 		if (hdlr->dtih_func == func)
106 			break;
107 
108 		prev = hdlr;
109 		hdlr = hdlr->dtih_next;
110 	}
111 
112 	if (prev == NULL) {
113 		ASSERT(dtrace_invop_hdlr == hdlr);
114 		dtrace_invop_hdlr = hdlr->dtih_next;
115 	} else {
116 		ASSERT(dtrace_invop_hdlr != hdlr);
117 		prev->dtih_next = hdlr->dtih_next;
118 	}
119 
120 	kmem_free(hdlr, 0);
121 }
122 
123 
124 /*ARGSUSED*/
125 void
126 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
127 {
128 	printf("IMPLEMENT ME: dtrace_toxic_ranges\n");
129 }
130 
131 void
132 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
133 {
134 	cpuset_t cpus;
135 
136 	if (cpu == DTRACE_CPUALL)
137 		cpus = all_cpus;
138 	else
139 		CPU_SETOF(cpu, &cpus);
140 
141 	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
142 	    smp_no_rendevous_barrier, arg);
143 }
144 
145 static void
146 dtrace_sync_func(void)
147 {
148 }
149 
150 void
151 dtrace_sync(void)
152 {
153 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
154 }
155 
156 /*
157  * DTrace needs a high resolution time function which can
158  * be called from a probe context and guaranteed not to have
159  * instrumented with probes itself.
160  *
161  * Returns nanoseconds since boot.
162  */
163 uint64_t
164 dtrace_gethrtime()
165 {
166 	struct	timespec curtime;
167 
168 	nanouptime(&curtime);
169 
170 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
171 
172 }
173 
174 uint64_t
175 dtrace_gethrestime(void)
176 {
177 	struct timespec current_time;
178 
179 	dtrace_getnanotime(&current_time);
180 
181 	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
182 }
183 
184 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
185 int
186 dtrace_trap(struct trapframe *frame, u_int type)
187 {
188 	/*
189 	 * A trap can occur while DTrace executes a probe. Before
190 	 * executing the probe, DTrace blocks re-scheduling and sets
191 	 * a flag in it's per-cpu flags to indicate that it doesn't
192 	 * want to fault. On returning from the probe, the no-fault
193 	 * flag is cleared and finally re-scheduling is enabled.
194 	 *
195 	 * Check if DTrace has enabled 'no-fault' mode:
196 	 *
197 	 */
198 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
199 		/*
200 		 * There are only a couple of trap types that are expected.
201 		 * All the rest will be handled in the usual way.
202 		 */
203 		switch (type) {
204 		/* Page fault. */
205 		case FAULT_ALIGN:
206 			/* Flag a bad address. */
207 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
208 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
209 
210 			/*
211 			 * Offset the instruction pointer to the instruction
212 			 * following the one causing the fault.
213 			 */
214 			frame->tf_pc += sizeof(int);
215 			return (1);
216 		default:
217 			/* Handle all other traps in the usual way. */
218 			break;
219 		}
220 	}
221 
222 	/* Handle the trap in the usual way. */
223 	return (0);
224 }
225 
226 void
227 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
228     int fault, int fltoffs, uintptr_t illval)
229 {
230 
231 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
232 	    (uintptr_t)epid,
233 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
234 }
235 
236 static int
237 dtrace_invop_start(struct trapframe *frame)
238 {
239 	register_t *r0, *sp;
240 	int data, invop, reg, update_sp;
241 
242 	invop = dtrace_invop(frame->tf_pc, (uintptr_t *)frame, frame->tf_pc);
243 	switch (invop & DTRACE_INVOP_MASK) {
244 	case DTRACE_INVOP_PUSHM:
245 		sp = (register_t *)frame->tf_svc_sp;
246 		r0 = &frame->tf_r0;
247 		data = DTRACE_INVOP_DATA(invop);
248 
249 		/*
250 		 * Store the pc, lr, and sp. These have their own
251 		 * entries in the struct.
252 		 */
253 		if (data & (1 << BIT_PC)) {
254 			sp--;
255 			*sp = frame->tf_pc;
256 		}
257 		if (data & (1 << BIT_LR)) {
258 			sp--;
259 			*sp = frame->tf_svc_lr;
260 		}
261 		if (data & (1 << BIT_SP)) {
262 			sp--;
263 			*sp = frame->tf_svc_sp;
264 		}
265 
266 		/* Store the general registers */
267 		for (reg = 12; reg >= 0; reg--) {
268 			if (data & (1 << reg)) {
269 				sp--;
270 				*sp = r0[reg];
271 			}
272 		}
273 
274 		/* Update the stack pointer and program counter to continue */
275 		frame->tf_svc_sp = (register_t)sp;
276 		frame->tf_pc += 4;
277 		break;
278 	case DTRACE_INVOP_POPM:
279 		sp = (register_t *)frame->tf_svc_sp;
280 		r0 = &frame->tf_r0;
281 		data = DTRACE_INVOP_DATA(invop);
282 
283 		/* Read the general registers */
284 		for (reg = 0; reg <= 12; reg++) {
285 			if (data & (1 << reg)) {
286 				r0[reg] = *sp;
287 				sp++;
288 			}
289 		}
290 
291 		/*
292 		 * Set the stack pointer. If we don't update it here we will
293 		 * need to update it at the end as the instruction would do
294 		 */
295 		update_sp = 1;
296 		if (data & (1 << BIT_SP)) {
297 			frame->tf_svc_sp = *sp;
298 			*sp++;
299 			update_sp = 0;
300 		}
301 
302 		/* Update the link register, we need to use the correct copy */
303 		if (data & (1 << BIT_LR)) {
304 			frame->tf_svc_lr = *sp;
305 			*sp++;
306 		}
307 		/*
308 		 * And the program counter. If it's not in the list skip over
309 		 * it when we return so to not hit this again.
310 		 */
311 		if (data & (1 << BIT_PC)) {
312 			frame->tf_pc = *sp;
313 			*sp++;
314 		} else
315 			frame->tf_pc += 4;
316 
317 		/* Update the stack pointer if we haven't already done so */
318 		if (update_sp)
319 			frame->tf_svc_sp = (register_t)sp;
320 		break;
321 	case DTRACE_INVOP_B:
322 		data = DTRACE_INVOP_DATA(invop) & 0x00ffffff;
323 		/* Sign extend the data */
324 		if ((data & (1 << 23)) != 0)
325 			data |= 0xff000000;
326 		/* The data is the number of 4-byte words to change the pc */
327 		data *= 4;
328 		data += 8;
329 		frame->tf_pc += data;
330 		break;
331 	default:
332 		return (-1);
333 		break;
334 	}
335 
336 	return (0);
337 }
338 
339 void dtrace_invop_init(void)
340 {
341 	dtrace_invop_jump_addr = dtrace_invop_start;
342 }
343 
344 void dtrace_invop_uninit(void)
345 {
346 	dtrace_invop_jump_addr = 0;
347 }
348