xref: /freebsd/sys/cddl/dev/dtrace/aarch64/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 extern uintptr_t 	dtrace_in_probe_addr;
49 extern int		dtrace_in_probe;
50 extern dtrace_id_t	dtrace_probeid_error;
51 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
52 extern void dtrace_getnanotime(struct timespec *tsp);
53 
54 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
55 void dtrace_invop_init(void);
56 void dtrace_invop_uninit(void);
57 
58 typedef struct dtrace_invop_hdlr {
59 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
60 	struct dtrace_invop_hdlr *dtih_next;
61 } dtrace_invop_hdlr_t;
62 
63 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
64 
65 int
66 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
67 {
68 	dtrace_invop_hdlr_t *hdlr;
69 	int rval;
70 
71 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
72 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
73 			return (rval);
74 
75 	return (0);
76 }
77 
78 
79 void
80 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
81 {
82 	dtrace_invop_hdlr_t *hdlr;
83 
84 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
85 	hdlr->dtih_func = func;
86 	hdlr->dtih_next = dtrace_invop_hdlr;
87 	dtrace_invop_hdlr = hdlr;
88 }
89 
90 void
91 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
92 {
93 	dtrace_invop_hdlr_t *hdlr, *prev;
94 
95 	hdlr = dtrace_invop_hdlr;
96 	prev = NULL;
97 
98 	for (;;) {
99 		if (hdlr == NULL)
100 			panic("attempt to remove non-existent invop handler");
101 
102 		if (hdlr->dtih_func == func)
103 			break;
104 
105 		prev = hdlr;
106 		hdlr = hdlr->dtih_next;
107 	}
108 
109 	if (prev == NULL) {
110 		ASSERT(dtrace_invop_hdlr == hdlr);
111 		dtrace_invop_hdlr = hdlr->dtih_next;
112 	} else {
113 		ASSERT(dtrace_invop_hdlr != hdlr);
114 		prev->dtih_next = hdlr->dtih_next;
115 	}
116 
117 	kmem_free(hdlr, 0);
118 }
119 
120 /*ARGSUSED*/
121 void
122 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
123 {
124 
125 	printf("IMPLEMENT ME: dtrace_toxic_ranges\n");
126 }
127 
128 void
129 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
130 {
131 	cpuset_t cpus;
132 
133 	if (cpu == DTRACE_CPUALL)
134 		cpus = all_cpus;
135 	else
136 		CPU_SETOF(cpu, &cpus);
137 
138 	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
139 	    smp_no_rendevous_barrier, arg);
140 }
141 
142 static void
143 dtrace_sync_func(void)
144 {
145 
146 }
147 
148 void
149 dtrace_sync(void)
150 {
151 
152 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
153 }
154 
155 /*
156  * DTrace needs a high resolution time function which can
157  * be called from a probe context and guaranteed not to have
158  * instrumented with probes itself.
159  *
160  * Returns nanoseconds since boot.
161  */
162 uint64_t
163 dtrace_gethrtime()
164 {
165 	struct timespec curtime;
166 
167 	nanouptime(&curtime);
168 
169 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
170 
171 }
172 
173 uint64_t
174 dtrace_gethrestime(void)
175 {
176 	struct timespec current_time;
177 
178 	dtrace_getnanotime(&current_time);
179 
180 	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
181 }
182 
183 /* Function to handle DTrace traps during probes. See arm64/arm64/trap.c */
184 int
185 dtrace_trap(struct trapframe *frame, u_int type)
186 {
187 	/*
188 	 * A trap can occur while DTrace executes a probe. Before
189 	 * executing the probe, DTrace blocks re-scheduling and sets
190 	 * a flag in it's per-cpu flags to indicate that it doesn't
191 	 * want to fault. On returning from the probe, the no-fault
192 	 * flag is cleared and finally re-scheduling is enabled.
193 	 *
194 	 * Check if DTrace has enabled 'no-fault' mode:
195 	 *
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 		case EXCP_DATA_ABORT:
205 			/* Flag a bad address. */
206 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
207 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
208 
209 			/*
210 			 * Offset the instruction pointer to the instruction
211 			 * following the one causing the fault.
212 			 */
213 			frame->tf_elr += 4;
214 			return (1);
215 		default:
216 			/* Handle all other traps in the usual way. */
217 			break;
218 		}
219 	}
220 
221 	/* Handle the trap in the usual way. */
222 	return (0);
223 }
224 
225 void
226 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
227     int fault, int fltoffs, uintptr_t illval)
228 {
229 
230 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
231 	    (uintptr_t)epid,
232 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
233 }
234 
235 static int
236 dtrace_invop_start(struct trapframe *frame)
237 {
238 	int data, invop, reg, update_sp;
239 	register_t arg1, arg2;
240 	register_t *sp;
241 	int offs;
242 	int tmp;
243 	int i;
244 
245 	invop = dtrace_invop(frame->tf_elr, (uintptr_t *)frame, frame->tf_elr);
246 
247 	tmp = (invop & LDP_STP_MASK);
248 	if (tmp == STP_64 || tmp == LDP_64) {
249 		sp = (register_t *)frame->tf_sp;
250 		data = invop;
251 		arg1 = (data >> ARG1_SHIFT) & ARG1_MASK;
252 		arg2 = (data >> ARG2_SHIFT) & ARG2_MASK;
253 
254 		offs = (data >> OFFSET_SHIFT) & OFFSET_MASK;
255 
256 		switch (tmp) {
257 		case STP_64:
258 			if (offs >> (OFFSET_SIZE - 1))
259 				sp -= (~offs & OFFSET_MASK) + 1;
260 			else
261 				sp += (offs);
262 			*(sp + 0) = frame->tf_x[arg1];
263 			*(sp + 1) = frame->tf_x[arg2];
264 			break;
265 		case LDP_64:
266 			frame->tf_x[arg1] = *(sp + 0);
267 			frame->tf_x[arg2] = *(sp + 1);
268 			if (offs >> (OFFSET_SIZE - 1))
269 				sp -= (~offs & OFFSET_MASK) + 1;
270 			else
271 				sp += (offs);
272 			break;
273 		default:
274 			break;
275 		}
276 
277 		/* Update the stack pointer and program counter to continue */
278 		frame->tf_sp = (register_t)sp;
279 		frame->tf_elr += INSN_SIZE;
280 		return (0);
281 	}
282 
283 	if ((invop & B_MASK) == B_INSTR) {
284 		data = (invop & B_DATA_MASK);
285 		/* The data is the number of 4-byte words to change the pc */
286 		data *= 4;
287 		frame->tf_elr += data;
288 		return (0);
289 	}
290 
291 	if (invop == RET_INSTR) {
292 		frame->tf_elr = frame->tf_lr;
293 		return (0);
294 	}
295 
296 	return (-1);
297 }
298 
299 void
300 dtrace_invop_init(void)
301 {
302 
303 	dtrace_invop_jump_addr = dtrace_invop_start;
304 }
305 
306 void
307 dtrace_invop_uninit(void)
308 {
309 
310 	dtrace_invop_jump_addr = 0;
311 }
312