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
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.
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]
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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>
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>
48 #define DELAYBRANCH(x) ((int)(x) < 0)
54 extern dtrace_id_t dtrace_probeid_error;
55 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
56 extern void dtrace_getnanotime(struct timespec *tsp);
58 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
59 void dtrace_invop_init(void);
60 void dtrace_invop_uninit(void);
62 typedef struct dtrace_invop_hdlr {
63 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
64 struct dtrace_invop_hdlr *dtih_next;
65 } dtrace_invop_hdlr_t;
67 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
70 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
72 dtrace_invop_hdlr_t *hdlr;
75 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
76 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
84 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
86 dtrace_invop_hdlr_t *hdlr;
88 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
89 hdlr->dtih_func = func;
90 hdlr->dtih_next = dtrace_invop_hdlr;
91 dtrace_invop_hdlr = hdlr;
95 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
97 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
101 panic("attempt to remove non-existent invop handler");
103 if (hdlr->dtih_func == func)
107 hdlr = hdlr->dtih_next;
111 ASSERT(dtrace_invop_hdlr == hdlr);
112 dtrace_invop_hdlr = hdlr->dtih_next;
114 ASSERT(dtrace_invop_hdlr != hdlr);
115 prev->dtih_next = hdlr->dtih_next;
124 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
128 * There are no ranges to exclude that are common to all 32-bit arm
129 * platforms. This function only needs to exclude ranges "... in
130 * which it is impossible to recover from such a load after it has been
131 * attempted." -- i.e., accessing within the range causes some sort
132 * fault in the system which is not handled by the normal arm
133 * exception-handling mechanisms. If systems exist where that is the
134 * case, a method to handle this functionality would have to be added to
135 * the platform_if interface so that those systems could provide their
136 * specific toxic range(s).
141 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
145 if (cpu == DTRACE_CPUALL)
148 CPU_SETOF(cpu, &cpus);
150 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
151 smp_no_rendezvous_barrier, arg);
155 dtrace_sync_func(void)
162 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
166 * DTrace needs a high resolution time function which can
167 * be called from a probe context and guaranteed not to have
168 * instrumented with probes itself.
170 * Returns nanoseconds since boot.
175 struct timespec curtime;
177 nanouptime(&curtime);
179 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
184 dtrace_gethrestime(void)
186 struct timespec current_time;
188 dtrace_getnanotime(¤t_time);
190 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
193 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
195 dtrace_trap(struct trapframe *frame, u_int type)
198 * A trap can occur while DTrace executes a probe. Before
199 * executing the probe, DTrace blocks re-scheduling and sets
200 * a flag in its per-cpu flags to indicate that it doesn't
201 * want to fault. On returning from the probe, the no-fault
202 * flag is cleared and finally re-scheduling is enabled.
204 * Check if DTrace has enabled 'no-fault' mode:
207 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
209 * There are only a couple of trap types that are expected.
210 * All the rest will be handled in the usual way.
215 /* Flag a bad address. */
216 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
217 cpu_core[curcpu].cpuc_dtrace_illval = 0;
220 * Offset the instruction pointer to the instruction
221 * following the one causing the fault.
223 frame->tf_pc += sizeof(int);
226 /* Handle all other traps in the usual way. */
231 /* Handle the trap in the usual way. */
236 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
237 int fault, int fltoffs, uintptr_t illval)
240 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
242 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
246 dtrace_invop_start(struct trapframe *frame)
249 int data, invop, reg, update_sp;
251 invop = dtrace_invop(frame->tf_pc, frame, frame->tf_r0);
252 switch (invop & DTRACE_INVOP_MASK) {
253 case DTRACE_INVOP_PUSHM:
254 sp = (register_t *)frame->tf_svc_sp;
256 data = DTRACE_INVOP_DATA(invop);
259 * Store the pc, lr, and sp. These have their own
260 * entries in the struct.
262 if (data & (1 << BIT_PC)) {
266 if (data & (1 << BIT_LR)) {
268 *sp = frame->tf_svc_lr;
270 if (data & (1 << BIT_SP)) {
272 *sp = frame->tf_svc_sp;
275 /* Store the general registers */
276 for (reg = 12; reg >= 0; reg--) {
277 if (data & (1 << reg)) {
283 /* Update the stack pointer and program counter to continue */
284 frame->tf_svc_sp = (register_t)sp;
287 case DTRACE_INVOP_POPM:
288 sp = (register_t *)frame->tf_svc_sp;
290 data = DTRACE_INVOP_DATA(invop);
292 /* Read the general registers */
293 for (reg = 0; reg <= 12; reg++) {
294 if (data & (1 << reg)) {
301 * Set the stack pointer. If we don't update it here we will
302 * need to update it at the end as the instruction would do
305 if (data & (1 << BIT_SP)) {
306 frame->tf_svc_sp = *sp;
311 /* Update the link register, we need to use the correct copy */
312 if (data & (1 << BIT_LR)) {
313 frame->tf_svc_lr = *sp;
317 * And the program counter. If it's not in the list skip over
318 * it when we return so to not hit this again.
320 if (data & (1 << BIT_PC)) {
326 /* Update the stack pointer if we haven't already done so */
328 frame->tf_svc_sp = (register_t)sp;
331 data = DTRACE_INVOP_DATA(invop) & 0x00ffffff;
332 /* Sign extend the data */
333 if ((data & (1 << 23)) != 0)
335 /* The data is the number of 4-byte words to change the pc */
338 frame->tf_pc += data;
348 void dtrace_invop_init(void)
350 dtrace_invop_jump_addr = dtrace_invop_start;
353 void dtrace_invop_uninit(void)
355 dtrace_invop_jump_addr = 0;