xref: /freebsd/sys/cddl/dev/dtrace/riscv/dtrace_subr.c (revision 6419bb52)
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 
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 void
81 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
82 {
83 	dtrace_invop_hdlr_t *hdlr;
84 
85 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
86 	hdlr->dtih_func = func;
87 	hdlr->dtih_next = dtrace_invop_hdlr;
88 	dtrace_invop_hdlr = hdlr;
89 }
90 
91 void
92 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
93 {
94 	dtrace_invop_hdlr_t *hdlr, *prev;
95 
96 	hdlr = dtrace_invop_hdlr;
97 	prev = NULL;
98 
99 	for (;;) {
100 		if (hdlr == NULL)
101 			panic("attempt to remove non-existent invop handler");
102 
103 		if (hdlr->dtih_func == func)
104 			break;
105 
106 		prev = hdlr;
107 		hdlr = hdlr->dtih_next;
108 	}
109 
110 	if (prev == NULL) {
111 		ASSERT(dtrace_invop_hdlr == hdlr);
112 		dtrace_invop_hdlr = hdlr->dtih_next;
113 	} else {
114 		ASSERT(dtrace_invop_hdlr != hdlr);
115 		prev->dtih_next = hdlr->dtih_next;
116 	}
117 
118 	kmem_free(hdlr, 0);
119 }
120 
121 /*ARGSUSED*/
122 void
123 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
124 {
125 
126 	(*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
127 }
128 
129 void
130 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
131 {
132 	cpuset_t cpus;
133 
134 	if (cpu == DTRACE_CPUALL)
135 		cpus = all_cpus;
136 	else
137 		CPU_SETOF(cpu, &cpus);
138 
139 	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
140 	    smp_no_rendezvous_barrier, arg);
141 }
142 
143 static void
144 dtrace_sync_func(void)
145 {
146 
147 }
148 
149 void
150 dtrace_sync(void)
151 {
152 
153 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
154 }
155 
156 /*
157  * DTrace needs a high resolution time function which can
158  * be called from a probe context and guaranteed not to have
159  * instrumented with probes itself.
160  *
161  * Returns nanoseconds since boot.
162  */
163 uint64_t
164 dtrace_gethrtime()
165 {
166 	struct timespec curtime;
167 
168 	nanouptime(&curtime);
169 
170 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
171 
172 }
173 
174 uint64_t
175 dtrace_gethrestime(void)
176 {
177 	struct timespec current_time;
178 
179 	dtrace_getnanotime(&current_time);
180 
181 	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
182 }
183 
184 /* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */
185 int
186 dtrace_trap(struct trapframe *frame, u_int type)
187 {
188 	/*
189 	 * A trap can occur while DTrace executes a probe. Before
190 	 * executing the probe, DTrace blocks re-scheduling and sets
191 	 * a flag in its per-cpu flags to indicate that it doesn't
192 	 * want to fault. On returning from the probe, the no-fault
193 	 * flag is cleared and finally re-scheduling is enabled.
194 	 *
195 	 * Check if DTrace has enabled 'no-fault' mode:
196 	 *
197 	 */
198 
199 	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
200 		/*
201 		 * There are only a couple of trap types that are expected.
202 		 * All the rest will be handled in the usual way.
203 		 */
204 		switch (type) {
205 		case EXCP_FAULT_LOAD:
206 		case EXCP_FAULT_STORE:
207 		case EXCP_FAULT_FETCH:
208 			/* Flag a bad address. */
209 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
210 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
211 
212 			/*
213 			 * Offset the instruction pointer to the instruction
214 			 * following the one causing the fault.
215 			 */
216 			frame->tf_sepc += 4;
217 
218 			return (1);
219 		default:
220 			/* Handle all other traps in the usual way. */
221 			break;
222 		}
223 	}
224 
225 	/* Handle the trap in the usual way. */
226 	return (0);
227 }
228 
229 void
230 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
231     int fault, int fltoffs, uintptr_t illval)
232 {
233 
234 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
235 	    (uintptr_t)epid,
236 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
237 }
238 
239 static int
240 match_opcode(uint32_t insn, int match, int mask)
241 {
242 
243 	if (((insn ^ match) & mask) == 0)
244 		return (1);
245 
246 	return (0);
247 }
248 
249 static int
250 dtrace_invop_start(struct trapframe *frame)
251 {
252 	register_t *sp;
253 	uint32_t uimm;
254 	uint32_t imm;
255 	int invop;
256 
257 	invop = dtrace_invop(frame->tf_sepc, frame, frame->tf_sepc);
258 	if (invop == 0)
259 		return (-1);
260 
261 	if (match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
262 	    (MASK_SD | RS2_MASK | RS1_MASK))) {
263 		/* Non-compressed store of ra to sp */
264 		imm = (invop >> 7) & 0x1f;
265 		imm |= ((invop >> 25) & 0x7f) << 5;
266 		sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
267 		*sp = frame->tf_ra;
268 		frame->tf_sepc += INSN_SIZE;
269 		return (0);
270 	}
271 
272 	if (match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
273 	    (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
274 		/* Non-compressed ret */
275 		frame->tf_sepc = frame->tf_ra;
276 		return (0);
277 	}
278 
279 	if (match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
280 	    (MASK_C_SDSP | RS2_C_MASK))) {
281 		/* 'C'-compressed store of ra to sp */
282 		uimm = ((invop >> 10) & 0x7) << 3;
283 		uimm |= ((invop >> 7) & 0x7) << 6;
284 		sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);
285 		*sp = frame->tf_ra;
286 		frame->tf_sepc += INSN_C_SIZE;
287 		return (0);
288 	}
289 
290 	if (match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
291 	    (MASK_C_JR | RD_MASK))) {
292 		/* 'C'-compressed ret */
293 		frame->tf_sepc = frame->tf_ra;
294 		return (0);
295 	}
296 
297 #ifdef INVARIANTS
298 	panic("Instruction %x doesn't match any opcode.", invop);
299 #endif
300 
301 	return (-1);
302 }
303 
304 void
305 dtrace_invop_init(void)
306 {
307 
308 	dtrace_invop_jump_addr = dtrace_invop_start;
309 }
310 
311 void
312 dtrace_invop_uninit(void)
313 {
314 
315 	dtrace_invop_jump_addr = 0;
316 }
317