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