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