1 /*	$NetBSD: dtrace_subr.c,v 1.2 2014/03/10 03:04:57 ozaki-r Exp $	*/
2 
3 /*
4  * CDDL HEADER START
5  *
6  * The contents of this file are subject to the terms of the
7  * Common Development and Distribution License, Version 1.0 only
8  * (the "License").  You may not use this file except in compliance
9  * with the License.
10  *
11  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12  * or http://www.opensolaris.org/os/licensing.
13  * See the License for the specific language governing permissions
14  * and limitations under the License.
15  *
16  * When distributing Covered Code, include this CDDL HEADER in each
17  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18  * If applicable, add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your own identifying
20  * information: Portions Copyright [yyyy] [name of copyright owner]
21  *
22  * CDDL HEADER END
23  *
24  * $FreeBSD$
25  *
26  */
27 /*
28  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/kmem.h>
38 #include <sys/xcall.h>
39 #include <sys/cpu.h>
40 #include <sys/cpuvar.h>
41 #include <sys/dtrace_impl.h>
42 #include <sys/dtrace_bsd.h>
43 #include <machine/cpu.h>
44 #include <machine/frame.h>
45 #include <machine/vmparam.h>
46 #include <uvm/uvm_pglist.h>
47 #include <uvm/uvm_prot.h>
48 #include <uvm/uvm_pmap.h>
49 
50 extern uintptr_t 	kernelbase;
51 extern uintptr_t 	dtrace_in_probe_addr;
52 extern int		dtrace_in_probe;
53 extern dtrace_id_t	dtrace_probeid_error;
54 
55 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
56 
57 typedef struct dtrace_invop_hdlr {
58 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
59 	struct dtrace_invop_hdlr *dtih_next;
60 } dtrace_invop_hdlr_t;
61 
62 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
63 
64 void dtrace_gethrtime_init(void *arg);
65 
66 int
dtrace_invop(uintptr_t addr,uintptr_t * stack,uintptr_t eax)67 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
68 {
69 	dtrace_invop_hdlr_t *hdlr;
70 	int rval;
71 
72 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
73 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
74 			return (rval);
75 
76 	return (0);
77 }
78 
79 void
dtrace_invop_add(int (* func)(uintptr_t,uintptr_t *,uintptr_t))80 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
81 {
82 	dtrace_invop_hdlr_t *hdlr;
83 
84 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
85 	hdlr->dtih_func = func;
86 	hdlr->dtih_next = dtrace_invop_hdlr;
87 	dtrace_invop_hdlr = hdlr;
88 }
89 
90 void
dtrace_invop_remove(int (* func)(uintptr_t,uintptr_t *,uintptr_t))91 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
92 {
93 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
94 
95 	for (;;) {
96 		if (hdlr == NULL)
97 			panic("attempt to remove non-existent invop handler");
98 
99 		if (hdlr->dtih_func == func)
100 			break;
101 
102 		prev = hdlr;
103 		hdlr = hdlr->dtih_next;
104 	}
105 
106 	if (prev == NULL) {
107 		ASSERT(dtrace_invop_hdlr == hdlr);
108 		dtrace_invop_hdlr = hdlr->dtih_next;
109 	} else {
110 		ASSERT(dtrace_invop_hdlr != hdlr);
111 		prev->dtih_next = hdlr->dtih_next;
112 	}
113 
114 	kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
115 }
116 
117 void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))118 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
119 {
120 	(*func)(0, kernelbase);
121 }
122 
123 static void
xcall_func(void * arg0,void * arg1)124 xcall_func(void *arg0, void *arg1)
125 {
126     	dtrace_xcall_t func = arg0;
127 
128     	(*func)(arg1);
129 }
130 
131 void
dtrace_xcall(processorid_t cpu,dtrace_xcall_t func,void * arg)132 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
133 {
134 	uint64_t where;
135 
136 	if (cpu == DTRACE_CPUALL) {
137 		where = xc_broadcast(0, xcall_func, func, arg);
138 	} else {
139 		struct cpu_info *cinfo = cpu_lookup(cpu);
140 
141 		KASSERT(cinfo != NULL);
142 		where = xc_unicast(0, xcall_func, func, arg, cinfo);
143 	}
144 	xc_wait(where);
145 
146 	/* XXX Q. Do we really need the other cpus to wait also?
147 	 * (see solaris:xc_sync())
148 	 */
149 }
150 
151 static void
dtrace_sync_func(void)152 dtrace_sync_func(void)
153 {
154 }
155 
156 void
dtrace_sync(void)157 dtrace_sync(void)
158 {
159         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
160 }
161 
162 /*
163  * DTrace needs a high resolution time function which can
164  * be called from a probe context and guaranteed not to have
165  * instrumented with probes itself.
166  *
167  * Returns nanoseconds since boot.
168  */
169 uint64_t
dtrace_gethrtime()170 dtrace_gethrtime()
171 {
172 	struct      timespec curtime;
173 
174 	nanouptime(&curtime);
175 
176 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
177 
178 }
179 
180 uint64_t
dtrace_gethrestime(void)181 dtrace_gethrestime(void)
182 {
183 	struct      timespec curtime;
184 
185 	getnanotime(&curtime);
186 
187 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
188 }
189 
190 /* Function to handle DTrace traps during probes. Not used on ARM yet */
191 int
dtrace_trap(struct trapframe * frame,u_int type)192 dtrace_trap(struct trapframe *frame, u_int type)
193 {
194 	cpuid_t cpuid = cpu_number();	/* current cpu id */
195 
196 	/*
197 	 * A trap can occur while DTrace executes a probe. Before
198 	 * executing the probe, DTrace blocks re-scheduling and sets
199 	 * a flag in it's per-cpu flags to indicate that it doesn't
200 	 * want to fault. On returning from the probe, the no-fault
201 	 * flag is cleared and finally re-scheduling is enabled.
202 	 *
203 	 * Check if DTrace has enabled 'no-fault' mode:
204 	 *
205 	 */
206 
207 	if ((cpu_core[cpuid].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
208 		/*
209 		 * There are only a couple of trap types that are expected.
210 		 * All the rest will be handled in the usual way.
211 		 */
212 		switch (type) {
213 		/* Page fault. */
214 		case 0:
215 			/* Flag a bad address. */
216 			cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
217 			cpu_core[cpuid].cpuc_dtrace_illval = 0;
218 
219 			/*
220 			 * Offset the instruction pointer to the instruction
221 			 * following the one causing the fault.
222 			 */
223 			panic("%s", __func__);
224 			// frame->pc += sizeof(int);
225 			return (1);
226 		default:
227 			/* Handle all other traps in the usual way. */
228 			break;
229 		}
230 	}
231 
232 	/* Handle the trap in the usual way. */
233 	return (0);
234 }
235 
236 void
dtrace_probe_error(dtrace_state_t * state,dtrace_epid_t epid,int which,int fault,int fltoffs,uintptr_t illval)237 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
238     int fault, int fltoffs, uintptr_t illval)
239 {
240 
241 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
242 	    (uintptr_t)epid,
243 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
244 }
245 
246 void
dtrace_gethrtime_init(void * arg)247 dtrace_gethrtime_init(void *arg)
248 {
249 	/* FIXME */
250 }
251