xref: /freebsd/sys/cddl/dev/dtrace/riscv/dtrace_subr.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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  * Portions Copyright 2016 Ruslan Bukin <br@bsdpad.com>
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/kmem.h>
41 #include <sys/smp.h>
42 #include <sys/dtrace_impl.h>
43 #include <sys/dtrace_bsd.h>
44 #include <machine/vmparam.h>
45 #include <machine/riscvreg.h>
46 #include <machine/riscv_opcode.h>
47 #include <machine/clock.h>
48 #include <machine/frame.h>
49 #include <machine/trap.h>
50 #include <vm/pmap.h>
51 
52 extern dtrace_id_t	dtrace_probeid_error;
53 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
54 extern void dtrace_getnanotime(struct timespec *tsp);
55 
56 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
57 void dtrace_invop_init(void);
58 void dtrace_invop_uninit(void);
59 
60 typedef struct dtrace_invop_hdlr {
61 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
62 	struct dtrace_invop_hdlr *dtih_next;
63 } dtrace_invop_hdlr_t;
64 
65 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
66 
67 int
68 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
69 {
70 	dtrace_invop_hdlr_t *hdlr;
71 	int rval;
72 
73 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
74 		if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
75 			return (rval);
76 
77 	return (0);
78 }
79 
80 
81 void
82 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
83 {
84 	dtrace_invop_hdlr_t *hdlr;
85 
86 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
87 	hdlr->dtih_func = func;
88 	hdlr->dtih_next = dtrace_invop_hdlr;
89 	dtrace_invop_hdlr = hdlr;
90 }
91 
92 void
93 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
94 {
95 	dtrace_invop_hdlr_t *hdlr, *prev;
96 
97 	hdlr = dtrace_invop_hdlr;
98 	prev = NULL;
99 
100 	for (;;) {
101 		if (hdlr == NULL)
102 			panic("attempt to remove non-existent invop handler");
103 
104 		if (hdlr->dtih_func == func)
105 			break;
106 
107 		prev = hdlr;
108 		hdlr = hdlr->dtih_next;
109 	}
110 
111 	if (prev == NULL) {
112 		ASSERT(dtrace_invop_hdlr == hdlr);
113 		dtrace_invop_hdlr = hdlr->dtih_next;
114 	} else {
115 		ASSERT(dtrace_invop_hdlr != hdlr);
116 		prev->dtih_next = hdlr->dtih_next;
117 	}
118 
119 	kmem_free(hdlr, 0);
120 }
121 
122 /*ARGSUSED*/
123 void
124 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
125 {
126 
127 	(*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
128 }
129 
130 void
131 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
132 {
133 	cpuset_t cpus;
134 
135 	if (cpu == DTRACE_CPUALL)
136 		cpus = all_cpus;
137 	else
138 		CPU_SETOF(cpu, &cpus);
139 
140 	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
141 	    smp_no_rendezvous_barrier, arg);
142 }
143 
144 static void
145 dtrace_sync_func(void)
146 {
147 
148 }
149 
150 void
151 dtrace_sync(void)
152 {
153 
154 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
155 }
156 
157 /*
158  * DTrace needs a high resolution time function which can
159  * be called from a probe context and guaranteed not to have
160  * instrumented with probes itself.
161  *
162  * Returns nanoseconds since boot.
163  */
164 uint64_t
165 dtrace_gethrtime()
166 {
167 	struct timespec curtime;
168 
169 	nanouptime(&curtime);
170 
171 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
172 
173 }
174 
175 uint64_t
176 dtrace_gethrestime(void)
177 {
178 	struct timespec current_time;
179 
180 	dtrace_getnanotime(&current_time);
181 
182 	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
183 }
184 
185 /* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */
186 int
187 dtrace_trap(struct trapframe *frame, u_int type)
188 {
189 	/*
190 	 * A trap can occur while DTrace executes a probe. Before
191 	 * executing the probe, DTrace blocks re-scheduling and sets
192 	 * a flag in its per-cpu flags to indicate that it doesn't
193 	 * want to fault. On returning from the probe, the no-fault
194 	 * flag is cleared and finally re-scheduling is enabled.
195 	 *
196 	 * Check if DTrace has enabled 'no-fault' mode:
197 	 *
198 	 */
199 
200 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
201 		/*
202 		 * There are only a couple of trap types that are expected.
203 		 * All the rest will be handled in the usual way.
204 		 */
205 		switch (type) {
206 		case EXCP_FAULT_LOAD:
207 		case EXCP_FAULT_STORE:
208 		case EXCP_FAULT_FETCH:
209 			/* Flag a bad address. */
210 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
211 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
212 
213 			/*
214 			 * Offset the instruction pointer to the instruction
215 			 * following the one causing the fault.
216 			 */
217 			frame->tf_sepc += 4;
218 
219 			return (1);
220 		default:
221 			/* Handle all other traps in the usual way. */
222 			break;
223 		}
224 	}
225 
226 	/* Handle the trap in the usual way. */
227 	return (0);
228 }
229 
230 void
231 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
232     int fault, int fltoffs, uintptr_t illval)
233 {
234 
235 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
236 	    (uintptr_t)epid,
237 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
238 }
239 
240 static int
241 dtrace_invop_start(struct trapframe *frame)
242 {
243 	int data, invop, reg, update_sp;
244 	register_t arg1, arg2;
245 	register_t *sp;
246 	uint32_t imm;
247 	InstFmt i;
248 	int offs;
249 	int tmp;
250 
251 	invop = dtrace_invop(frame->tf_sepc, frame, frame->tf_sepc);
252 
253 	if (invop == RISCV_INSN_RET) {
254 		frame->tf_sepc = frame->tf_ra;
255 		return (0);
256 	}
257 
258 	if ((invop & SD_RA_SP_MASK) == SD_RA_SP) {
259 		i.word = invop;
260 		imm = i.SType.imm0_4 | (i.SType.imm5_11 << 5);
261 		sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
262 		*sp = frame->tf_ra;
263 		frame->tf_sepc += INSN_SIZE;
264 		return (0);
265 	}
266 
267 	return (-1);
268 }
269 
270 void
271 dtrace_invop_init(void)
272 {
273 
274 	dtrace_invop_jump_addr = dtrace_invop_start;
275 }
276 
277 void
278 dtrace_invop_uninit(void)
279 {
280 
281 	dtrace_invop_jump_addr = 0;
282 }
283