xref: /netbsd-src/external/cddl/osnet/dev/dtrace/arm/dtrace_subr.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
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 uintptr_t 	dtrace_in_probe_addr;
50 extern int		dtrace_in_probe;
51 extern dtrace_id_t	dtrace_probeid_error;
52 
53 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
54 
55 typedef struct dtrace_invop_hdlr {
56 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
57 	struct dtrace_invop_hdlr *dtih_next;
58 } dtrace_invop_hdlr_t;
59 
60 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
61 
62 int
63 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
64 {
65 	dtrace_invop_hdlr_t *hdlr;
66 	int rval;
67 
68 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
69 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
70 			return (rval);
71 
72 	return (0);
73 }
74 
75 
76 /*ARGSUSED*/
77 void
78 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
79 {
80 	printf("IMPLEMENT ME: dtrace_toxic_ranges\n");
81 }
82 
83 void
84 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
85 {
86 	cpuset_t cpus;
87 
88 	if (cpu == DTRACE_CPUALL)
89 		cpus = all_cpus;
90 	else
91 		CPU_SETOF(cpu, &cpus);
92 
93 	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
94 	    smp_no_rendevous_barrier, arg);
95 }
96 
97 static void
98 dtrace_sync_func(void)
99 {
100 }
101 
102 void
103 dtrace_sync(void)
104 {
105         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
106 }
107 
108 /*
109  * DTrace needs a high resolution time function which can
110  * be called from a probe context and guaranteed not to have
111  * instrumented with probes itself.
112  *
113  * Returns nanoseconds since boot.
114  */
115 uint64_t
116 dtrace_gethrtime()
117 {
118 	struct      timespec curtime;
119 
120 	nanouptime(&curtime);
121 
122 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
123 
124 }
125 
126 uint64_t
127 dtrace_gethrestime(void)
128 {
129 	struct      timespec curtime;
130 
131 	getnanotime(&curtime);
132 
133 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
134 }
135 
136 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
137 int
138 dtrace_trap(struct trapframe *frame, u_int type)
139 {
140 	/*
141 	 * A trap can occur while DTrace executes a probe. Before
142 	 * executing the probe, DTrace blocks re-scheduling and sets
143 	 * a flag in it's per-cpu flags to indicate that it doesn't
144 	 * want to fault. On returning from the probe, the no-fault
145 	 * flag is cleared and finally re-scheduling is enabled.
146 	 *
147 	 * Check if DTrace has enabled 'no-fault' mode:
148 	 *
149 	 */
150 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
151 		/*
152 		 * There are only a couple of trap types that are expected.
153 		 * All the rest will be handled in the usual way.
154 		 */
155 		switch (type) {
156 		/* Page fault. */
157 		case 0:
158 			/* Flag a bad address. */
159 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
160 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
161 
162 			/*
163 			 * Offset the instruction pointer to the instruction
164 			 * following the one causing the fault.
165 			 */
166 			panic("%s", __func__);
167 			// frame->pc += sizeof(int);
168 			return (1);
169 		default:
170 			/* Handle all other traps in the usual way. */
171 			break;
172 		}
173 	}
174 
175 	/* Handle the trap in the usual way. */
176 	return (0);
177 }
178 
179 void
180 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
181     int fault, int fltoffs, uintptr_t illval)
182 {
183 
184 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
185 	    (uintptr_t)epid,
186 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
187 }
188