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>
46 #include <machine/vmparam.h>
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);
53 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
54 void dtrace_invop_init(void);
55 void dtrace_invop_uninit(void);
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;
62 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
65 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
67 dtrace_invop_hdlr_t *hdlr;
70 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
71 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
79 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
81 dtrace_invop_hdlr_t *hdlr;
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;
90 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
92 dtrace_invop_hdlr_t *hdlr, *prev;
94 hdlr = dtrace_invop_hdlr;
99 panic("attempt to remove non-existent invop handler");
101 if (hdlr->dtih_func == func)
105 hdlr = hdlr->dtih_next;
109 ASSERT(dtrace_invop_hdlr == hdlr);
110 dtrace_invop_hdlr = hdlr->dtih_next;
112 ASSERT(dtrace_invop_hdlr != hdlr);
113 prev->dtih_next = hdlr->dtih_next;
121 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
124 (*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
128 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
132 if (cpu == DTRACE_CPUALL)
135 CPU_SETOF(cpu, &cpus);
137 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
138 smp_no_rendezvous_barrier, arg);
142 dtrace_sync_func(void)
151 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
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.
159 * Returns nanoseconds since boot.
164 struct timespec curtime;
166 nanouptime(&curtime);
168 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
173 dtrace_gethrestime(void)
175 struct timespec current_time;
177 dtrace_getnanotime(¤t_time);
179 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
182 /* Function to handle DTrace traps during probes. See arm64/arm64/trap.c */
184 dtrace_trap(struct trapframe *frame, u_int type)
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 its 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.
193 * Check if DTrace has enabled 'no-fault' mode:
197 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
199 * There are only a couple of trap types that are expected.
200 * All the rest will be handled in the usual way.
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;
209 * Offset the instruction pointer to the instruction
210 * following the one causing the fault.
215 /* Handle all other traps in the usual way. */
220 /* Handle the trap in the usual way. */
225 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
226 int fault, int fltoffs, uintptr_t illval)
229 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
231 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
235 dtrace_invop_start(struct trapframe *frame)
237 int data, invop, reg, update_sp;
238 register_t arg1, arg2;
244 invop = dtrace_invop(frame->tf_elr, frame, frame->tf_elr);
246 tmp = (invop & LDP_STP_MASK);
247 if (tmp == STP_64 || tmp == LDP_64) {
248 sp = (register_t *)frame->tf_sp;
250 arg1 = (data >> ARG1_SHIFT) & ARG1_MASK;
251 arg2 = (data >> ARG2_SHIFT) & ARG2_MASK;
253 offs = (data >> OFFSET_SHIFT) & OFFSET_MASK;
257 if (offs >> (OFFSET_SIZE - 1))
258 sp -= (~offs & OFFSET_MASK) + 1;
261 *(sp + 0) = frame->tf_x[arg1];
262 *(sp + 1) = frame->tf_x[arg2];
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;
276 /* Update the stack pointer and program counter to continue */
277 frame->tf_sp = (register_t)sp;
278 frame->tf_elr += INSN_SIZE;
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 */
286 frame->tf_elr += data;
290 if (invop == RET_INSTR) {
291 frame->tf_elr = frame->tf_lr;
299 dtrace_invop_init(void)
302 dtrace_invop_jump_addr = dtrace_invop_start;
306 dtrace_invop_uninit(void)
309 dtrace_invop_jump_addr = 0;