xref: /freebsd/sys/cddl/dev/dtrace/powerpc/dtrace_subr.c (revision 955c8cbb4960e6cf3602de144b1b9154a5092968)
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 	/*
81 	 * No toxic regions?
82 	 */
83 }
84 
85 void
86 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
87 {
88 	cpuset_t cpus;
89 
90 	if (cpu == DTRACE_CPUALL)
91 		cpus = all_cpus;
92 	else
93 		CPU_SETOF(cpu, &cpus);
94 
95 	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
96 	    smp_no_rendevous_barrier, arg);
97 }
98 
99 static void
100 dtrace_sync_func(void)
101 {
102 }
103 
104 void
105 dtrace_sync(void)
106 {
107         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
108 }
109 
110 /*
111  * DTrace needs a high resolution time function which can
112  * be called from a probe context and guaranteed not to have
113  * instrumented with probes itself.
114  *
115  * Returns nanoseconds since boot.
116  */
117 uint64_t
118 dtrace_gethrtime()
119 {
120 	struct      timespec curtime;
121 
122 	nanouptime(&curtime);
123 
124 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
125 
126 }
127 
128 uint64_t
129 dtrace_gethrestime(void)
130 {
131 	struct      timespec curtime;
132 
133 	getnanotime(&curtime);
134 
135 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
136 }
137 
138 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
139 int
140 dtrace_trap(struct trapframe *frame, u_int type)
141 {
142 	/*
143 	 * A trap can occur while DTrace executes a probe. Before
144 	 * executing the probe, DTrace blocks re-scheduling and sets
145 	 * a flag in it's per-cpu flags to indicate that it doesn't
146 	 * want to fault. On returning from the probe, the no-fault
147 	 * flag is cleared and finally re-scheduling is enabled.
148 	 *
149 	 * Check if DTrace has enabled 'no-fault' mode:
150 	 *
151 	 */
152 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
153 		/*
154 		 * There are only a couple of trap types that are expected.
155 		 * All the rest will be handled in the usual way.
156 		 */
157 		switch (type) {
158 		/* Page fault. */
159 		case EXC_DSI:
160 		case EXC_DSE:
161 			/* Flag a bad address. */
162 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
163 			cpu_core[curcpu].cpuc_dtrace_illval = frame->cpu.aim.dar;
164 
165 			/*
166 			 * Offset the instruction pointer to the instruction
167 			 * following the one causing the fault.
168 			 */
169 			frame->srr0 += sizeof(int);
170 			return (1);
171 		case EXC_ISI:
172 		case EXC_ISE:
173 			/* Flag a bad address. */
174 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
175 			cpu_core[curcpu].cpuc_dtrace_illval = frame->srr0;
176 
177 			/*
178 			 * Offset the instruction pointer to the instruction
179 			 * following the one causing the fault.
180 			 */
181 			frame->srr0 += sizeof(int);
182 			return (1);
183 		default:
184 			/* Handle all other traps in the usual way. */
185 			break;
186 		}
187 	}
188 
189 	/* Handle the trap in the usual way. */
190 	return (0);
191 }
192 
193 void
194 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
195     int fault, int fltoffs, uintptr_t illval)
196 {
197 
198 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
199 	    (uintptr_t)epid,
200 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
201 }
202