xref: /freebsd/sys/cddl/dev/dtrace/riscv/dtrace_subr.c (revision 549198b1ab95139fb26470cf22ce894907c361b8)
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-2018 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/encoding.h>
46 #include <machine/riscvreg.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 extern void dtrace_getnanouptime(struct timespec *tsp);
56 
57 int dtrace_invop(uintptr_t, struct trapframe *);
58 void dtrace_invop_init(void);
59 void dtrace_invop_uninit(void);
60 
61 typedef struct dtrace_invop_hdlr {
62 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
63 	struct dtrace_invop_hdlr *dtih_next;
64 } dtrace_invop_hdlr_t;
65 
66 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
67 
68 int
69 dtrace_invop(uintptr_t addr, struct trapframe *frame)
70 {
71 	dtrace_invop_hdlr_t *hdlr;
72 	int rval;
73 
74 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
75 		if ((rval = hdlr->dtih_func(addr, frame, 0)) != 0)
76 			return (rval);
77 
78 	return (0);
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(void)
166 {
167 	struct timespec curtime;
168 
169 	dtrace_getnanouptime(&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 SCAUSE_LOAD_ACCESS_FAULT:
207 		case SCAUSE_STORE_ACCESS_FAULT:
208 		case SCAUSE_INST_ACCESS_FAULT:
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 match_opcode(uint32_t insn, int match, int mask)
242 {
243 
244 	if (((insn ^ match) & mask) == 0)
245 		return (1);
246 
247 	return (0);
248 }
249 
250 static int
251 dtrace_invop_start(struct trapframe *frame)
252 {
253 	register_t *sp;
254 	uint32_t uimm;
255 	uint32_t imm;
256 	int invop;
257 
258 	invop = dtrace_invop(frame->tf_sepc, frame);
259 	if (invop == 0)
260 		return (-1);
261 
262 	if (match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
263 	    (MASK_SD | RS2_MASK | RS1_MASK))) {
264 		/* Non-compressed store of ra to sp */
265 		imm = (invop >> 7) & 0x1f;
266 		imm |= ((invop >> 25) & 0x7f) << 5;
267 		sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
268 		*sp = frame->tf_ra;
269 		frame->tf_sepc += INSN_SIZE;
270 		return (0);
271 	}
272 
273 	if (match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
274 	    (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
275 		/* Non-compressed ret */
276 		frame->tf_sepc = frame->tf_ra;
277 		return (0);
278 	}
279 
280 	if (match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
281 	    (MASK_C_SDSP | RS2_C_MASK))) {
282 		/* 'C'-compressed store of ra to sp */
283 		uimm = ((invop >> 10) & 0x7) << 3;
284 		uimm |= ((invop >> 7) & 0x7) << 6;
285 		sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);
286 		*sp = frame->tf_ra;
287 		frame->tf_sepc += INSN_C_SIZE;
288 		return (0);
289 	}
290 
291 	if (match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
292 	    (MASK_C_JR | RD_MASK))) {
293 		/* 'C'-compressed ret */
294 		frame->tf_sepc = frame->tf_ra;
295 		return (0);
296 	}
297 
298 #ifdef INVARIANTS
299 	panic("Instruction %x doesn't match any opcode.", invop);
300 #endif
301 
302 	return (-1);
303 }
304 
305 void
306 dtrace_invop_init(void)
307 {
308 
309 	dtrace_invop_jump_addr = dtrace_invop_start;
310 }
311 
312 void
313 dtrace_invop_uninit(void)
314 {
315 
316 	dtrace_invop_jump_addr = 0;
317 }
318