xref: /freebsd/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
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 
53 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
54 void dtrace_invop_init(void);
55 void dtrace_invop_uninit(void);
56 
57 typedef struct dtrace_invop_hdlr {
58 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
59 	struct dtrace_invop_hdlr *dtih_next;
60 } dtrace_invop_hdlr_t;
61 
62 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
63 
64 int
65 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
66 {
67 	dtrace_invop_hdlr_t *hdlr;
68 	int rval;
69 
70 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
71 		if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
72 			return (rval);
73 
74 	return (0);
75 }
76 
77 
78 void
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
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
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
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_rendevous_barrier, func,
138 	    smp_no_rendevous_barrier, arg);
139 }
140 
141 static void
142 dtrace_sync_func(void)
143 {
144 
145 }
146 
147 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
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 arm64/arm64/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 it's 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 
197 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
198 		/*
199 		 * There are only a couple of trap types that are expected.
200 		 * All the rest will be handled in the usual way.
201 		 */
202 		switch (type) {
203 		case EXCP_DATA_ABORT:
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_elr += 4;
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 	int data, invop, reg, update_sp;
238 	register_t arg1, arg2;
239 	register_t *sp;
240 	int offs;
241 	int tmp;
242 	int i;
243 
244 	invop = dtrace_invop(frame->tf_elr, frame, frame->tf_elr);
245 
246 	tmp = (invop & LDP_STP_MASK);
247 	if (tmp == STP_64 || tmp == LDP_64) {
248 		sp = (register_t *)frame->tf_sp;
249 		data = invop;
250 		arg1 = (data >> ARG1_SHIFT) & ARG1_MASK;
251 		arg2 = (data >> ARG2_SHIFT) & ARG2_MASK;
252 
253 		offs = (data >> OFFSET_SHIFT) & OFFSET_MASK;
254 
255 		switch (tmp) {
256 		case STP_64:
257 			if (offs >> (OFFSET_SIZE - 1))
258 				sp -= (~offs & OFFSET_MASK) + 1;
259 			else
260 				sp += (offs);
261 			*(sp + 0) = frame->tf_x[arg1];
262 			*(sp + 1) = frame->tf_x[arg2];
263 			break;
264 		case LDP_64:
265 			frame->tf_x[arg1] = *(sp + 0);
266 			frame->tf_x[arg2] = *(sp + 1);
267 			if (offs >> (OFFSET_SIZE - 1))
268 				sp -= (~offs & OFFSET_MASK) + 1;
269 			else
270 				sp += (offs);
271 			break;
272 		default:
273 			break;
274 		}
275 
276 		/* Update the stack pointer and program counter to continue */
277 		frame->tf_sp = (register_t)sp;
278 		frame->tf_elr += INSN_SIZE;
279 		return (0);
280 	}
281 
282 	if ((invop & B_MASK) == B_INSTR) {
283 		data = (invop & B_DATA_MASK);
284 		/* The data is the number of 4-byte words to change the pc */
285 		data *= 4;
286 		frame->tf_elr += data;
287 		return (0);
288 	}
289 
290 	if (invop == RET_INSTR) {
291 		frame->tf_elr = frame->tf_lr;
292 		return (0);
293 	}
294 
295 	return (-1);
296 }
297 
298 void
299 dtrace_invop_init(void)
300 {
301 
302 	dtrace_invop_jump_addr = dtrace_invop_start;
303 }
304 
305 void
306 dtrace_invop_uninit(void)
307 {
308 
309 	dtrace_invop_jump_addr = 0;
310 }
311