xref: /freebsd/sys/cddl/dev/dtrace/powerpc/dtrace_subr.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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/clock.h>
43 #include <machine/frame.h>
44 #include <machine/trap.h>
45 #include <vm/pmap.h>
46 
47 #define	DELAYBRANCH(x)	((int)(x) < 0)
48 
49 extern dtrace_id_t	dtrace_probeid_error;
50 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
51 
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 arg0)
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, arg0)) != 0)
73 			return (rval);
74 
75 	return (0);
76 }
77 
78 void
79 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, 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, uintptr_t *, uintptr_t))
91 {
92 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
93 
94 	for (;;) {
95 		if (hdlr == NULL)
96 			panic("attempt to remove non-existent invop handler");
97 
98 		if (hdlr->dtih_func == func)
99 			break;
100 
101 		prev = hdlr;
102 		hdlr = hdlr->dtih_next;
103 	}
104 
105 	if (prev == NULL) {
106 		ASSERT(dtrace_invop_hdlr == hdlr);
107 		dtrace_invop_hdlr = hdlr->dtih_next;
108 	} else {
109 		ASSERT(dtrace_invop_hdlr != hdlr);
110 		prev->dtih_next = hdlr->dtih_next;
111 	}
112 
113 	kmem_free(hdlr, 0);
114 }
115 
116 
117 /*ARGSUSED*/
118 void
119 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
120 {
121 	/*
122 	 * No toxic regions?
123 	 */
124 }
125 
126 void
127 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
128 {
129 	cpuset_t cpus;
130 
131 	if (cpu == DTRACE_CPUALL)
132 		cpus = all_cpus;
133 	else
134 		CPU_SETOF(cpu, &cpus);
135 
136 	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
137 			smp_no_rendevous_barrier, arg);
138 }
139 
140 static void
141 dtrace_sync_func(void)
142 {
143 }
144 
145 void
146 dtrace_sync(void)
147 {
148 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
149 }
150 
151 static int64_t	tgt_cpu_tsc;
152 static int64_t	hst_cpu_tsc;
153 static int64_t	timebase_skew[MAXCPU];
154 static uint64_t	nsec_scale;
155 
156 /* See below for the explanation of this macro. */
157 /* This is taken from the amd64 dtrace_subr, to provide a synchronized timer
158  * between multiple processors in dtrace.  Since PowerPC Timebases can be much
159  * lower than x86, the scale shift is 26 instead of 28, allowing for a 15.63MHz
160  * timebase.
161  */
162 #define SCALE_SHIFT	26
163 
164 static void
165 dtrace_gethrtime_init_cpu(void *arg)
166 {
167 	uintptr_t cpu = (uintptr_t) arg;
168 
169 	if (cpu == curcpu)
170 		tgt_cpu_tsc = mftb();
171 	else
172 		hst_cpu_tsc = mftb();
173 }
174 
175 static void
176 dtrace_gethrtime_init(void *arg)
177 {
178 	struct pcpu *pc;
179 	uint64_t tb_f;
180 	cpuset_t map;
181 	int i;
182 
183 	tb_f = cpu_tickrate();
184 
185 	/*
186 	 * The following line checks that nsec_scale calculated below
187 	 * doesn't overflow 32-bit unsigned integer, so that it can multiply
188 	 * another 32-bit integer without overflowing 64-bit.
189 	 * Thus minimum supported Timebase frequency is 15.63MHz.
190 	 */
191 	KASSERT(tb_f > (NANOSEC >> (32 - SCALE_SHIFT)), ("Timebase frequency is too low"));
192 
193 	/*
194 	 * We scale up NANOSEC/tb_f ratio to preserve as much precision
195 	 * as possible.
196 	 * 2^26 factor was chosen quite arbitrarily from practical
197 	 * considerations:
198 	 * - it supports TSC frequencies as low as 15.63MHz (see above);
199 	 */
200 	nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tb_f;
201 
202 	/* The current CPU is the reference one. */
203 	sched_pin();
204 	timebase_skew[curcpu] = 0;
205 	CPU_FOREACH(i) {
206 		if (i == curcpu)
207 			continue;
208 
209 		pc = pcpu_find(i);
210 		CPU_SETOF(PCPU_GET(cpuid), &map);
211 		CPU_SET(pc->pc_cpuid, &map);
212 
213 		smp_rendezvous_cpus(map, NULL,
214 		    dtrace_gethrtime_init_cpu,
215 		    smp_no_rendevous_barrier, (void *)(uintptr_t) i);
216 
217 		timebase_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
218 	}
219 	sched_unpin();
220 }
221 
222 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL);
223 
224 /*
225  * DTrace needs a high resolution time function which can
226  * be called from a probe context and guaranteed not to have
227  * instrumented with probes itself.
228  *
229  * Returns nanoseconds since boot.
230  */
231 uint64_t
232 dtrace_gethrtime()
233 {
234 	uint64_t timebase;
235 	uint32_t lo;
236 	uint32_t hi;
237 
238 	/*
239 	 * We split timebase value into lower and higher 32-bit halves and separately
240 	 * scale them with nsec_scale, then we scale them down by 2^28
241 	 * (see nsec_scale calculations) taking into account 32-bit shift of
242 	 * the higher half and finally add.
243 	 */
244 	timebase = mftb() - timebase_skew[curcpu];
245 	lo = timebase;
246 	hi = timebase >> 32;
247 	return (((lo * nsec_scale) >> SCALE_SHIFT) +
248 	    ((hi * nsec_scale) << (32 - SCALE_SHIFT)));
249 }
250 
251 uint64_t
252 dtrace_gethrestime(void)
253 {
254 	struct      timespec curtime;
255 
256 	dtrace_getnanotime(&curtime);
257 
258 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
259 }
260 
261 /* Function to handle DTrace traps during probes. See powerpc/powerpc/trap.c */
262 int
263 dtrace_trap(struct trapframe *frame, u_int type)
264 {
265 
266 	/*
267 	 * A trap can occur while DTrace executes a probe. Before
268 	 * executing the probe, DTrace blocks re-scheduling and sets
269 	 * a flag in its per-cpu flags to indicate that it doesn't
270 	 * want to fault. On returning from the probe, the no-fault
271 	 * flag is cleared and finally re-scheduling is enabled.
272 	 *
273 	 * Check if DTrace has enabled 'no-fault' mode:
274 	 */
275 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
276 		/*
277 		 * There are only a couple of trap types that are expected.
278 		 * All the rest will be handled in the usual way.
279 		 */
280 		switch (type) {
281 		/* Page fault. */
282 		case EXC_DSI:
283 		case EXC_DSE:
284 			/* Flag a bad address. */
285 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
286 			cpu_core[curcpu].cpuc_dtrace_illval = frame->dar;
287 
288 			/*
289 			 * Offset the instruction pointer to the instruction
290 			 * following the one causing the fault.
291 			 */
292 			frame->srr0 += sizeof(int);
293 			return (1);
294 		case EXC_ISI:
295 		case EXC_ISE:
296 			/* Flag a bad address. */
297 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
298 			cpu_core[curcpu].cpuc_dtrace_illval = frame->srr0;
299 
300 			/*
301 			 * Offset the instruction pointer to the instruction
302 			 * following the one causing the fault.
303 			 */
304 			frame->srr0 += sizeof(int);
305 			return (1);
306 		default:
307 			/* Handle all other traps in the usual way. */
308 			break;
309 		}
310 	}
311 
312 	/* Handle the trap in the usual way. */
313 	return (0);
314 }
315 
316 void
317 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
318     int fault, int fltoffs, uintptr_t illval)
319 {
320 
321 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
322 	    (uintptr_t)epid,
323 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
324 }
325 
326 static int
327 dtrace_invop_start(struct trapframe *frame)
328 {
329 	switch (dtrace_invop(frame->srr0, (uintptr_t *)frame, frame->fixreg[3])) {
330 	case DTRACE_INVOP_JUMP:
331 		break;
332 	case DTRACE_INVOP_BCTR:
333 		frame->srr0 = frame->ctr;
334 		break;
335 	case DTRACE_INVOP_BLR:
336 		frame->srr0 = frame->lr;
337 		break;
338 	case DTRACE_INVOP_MFLR_R0:
339 		frame->fixreg[0] = frame->lr;
340 		frame->srr0 = frame->srr0 + 4;
341 		break;
342 	default:
343 		return (-1);
344 		break;
345 	}
346 
347 	return (0);
348 }
349 
350 void dtrace_invop_init(void)
351 {
352 	dtrace_invop_jump_addr = dtrace_invop_start;
353 }
354 
355 void dtrace_invop_uninit(void)
356 {
357 	dtrace_invop_jump_addr = 0;
358 }
359