xref: /freebsd/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 <machine/vmparam.h>
47 #include <vm/pmap.h>
48 
49 extern dtrace_id_t	dtrace_probeid_error;
50 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
51 extern void dtrace_getnanotime(struct timespec *tsp);
52 extern void dtrace_getnanouptime(struct timespec *tsp);
53 
54 int dtrace_invop(uintptr_t, struct trapframe *, 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, struct trapframe *, 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, struct trapframe *frame, 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, frame, eax)) != 0)
73 			return (rval);
74 
75 	return (0);
76 }
77 
78 
79 void
80 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, 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, struct trapframe *, 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 	(*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
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_rendezvous_barrier, func,
139 	    smp_no_rendezvous_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 	dtrace_getnanouptime(&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 its 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 void
236 dtrace_load64(uint64_t *addr, struct trapframe *frame, u_int reg)
237 {
238 
239 	KASSERT(reg <= 31, ("dtrace_load64: Invalid register %u", reg));
240 	if (reg < nitems(frame->tf_x))
241 		frame->tf_x[reg] = *addr;
242 	else if (reg == 30) /* lr */
243 		frame->tf_lr = *addr;
244 	/* Nothing to do for load to xzr */
245 }
246 
247 static void
248 dtrace_store64(uint64_t *addr, struct trapframe *frame, u_int reg)
249 {
250 
251 	KASSERT(reg <= 31, ("dtrace_store64: Invalid register %u", reg));
252 	if (reg < nitems(frame->tf_x))
253 		*addr = frame->tf_x[reg];
254 	else if (reg == 30) /* lr */
255 		*addr = frame->tf_lr;
256 	else if (reg == 31) /* xzr */
257 		*addr = 0;
258 }
259 
260 static int
261 dtrace_invop_start(struct trapframe *frame)
262 {
263 	int data, invop, reg, update_sp;
264 	register_t arg1, arg2;
265 	register_t *sp;
266 	int offs;
267 	int tmp;
268 	int i;
269 
270 	invop = dtrace_invop(frame->tf_elr, frame, frame->tf_elr);
271 
272 	tmp = (invop & LDP_STP_MASK);
273 	if (tmp == STP_64 || tmp == LDP_64) {
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 & B_MASK) == B_INSTR) {
315 		data = (invop & B_DATA_MASK);
316 		/* The data is the number of 4-byte words to change the pc */
317 		data *= 4;
318 		frame->tf_elr += data;
319 		return (0);
320 	}
321 
322 	if (invop == RET_INSTR) {
323 		frame->tf_elr = frame->tf_lr;
324 		return (0);
325 	}
326 
327 	return (-1);
328 }
329 
330 void
331 dtrace_invop_init(void)
332 {
333 
334 	dtrace_invop_jump_addr = dtrace_invop_start;
335 }
336 
337 void
338 dtrace_invop_uninit(void)
339 {
340 
341 	dtrace_invop_jump_addr = 0;
342 }
343