xref: /freebsd/sys/cddl/dev/fbt/riscv/fbt_isa.c (revision 6780e684d49034610f82bea5d3bfb04d42e91628)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22  * Portions Copyright 2013 Justin Hibbits jhibbits@freebsd.org
23  * Portions Copyright 2013 Howard Su howardsu@freebsd.org
24  * Portions Copyright 2016 Ruslan Bukin <br@bsdpad.com>
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
31  * Use is subject to license terms.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 
37 #include <sys/dtrace.h>
38 
39 #include <machine/riscvreg.h>
40 
41 #include "fbt.h"
42 
43 #define	FBT_PATCHVAL		(RISCV_INSN_BREAK)
44 #define	FBT_ENTRY		"entry"
45 #define	FBT_RETURN		"return"
46 
47 int
48 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
49 {
50 	solaris_cpu_t *cpu;
51 	fbt_probe_t *fbt;
52 
53 	cpu = &solaris_cpu[curcpu];
54 	fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
55 
56 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
57 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
58 			cpu->cpu_dtrace_caller = addr;
59 
60 			dtrace_probe(fbt->fbtp_id, frame->tf_a[0],
61 			    frame->tf_a[1], frame->tf_a[2],
62 			    frame->tf_a[3], frame->tf_a[4]);
63 
64 			cpu->cpu_dtrace_caller = 0;
65 			return (fbt->fbtp_savedval);
66 		}
67 	}
68 
69 	return (0);
70 }
71 
72 void
73 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
74 {
75 
76 	*fbt->fbtp_patchpoint = val;
77 	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
78 }
79 
80 int
81 fbt_provide_module_function(linker_file_t lf, int symindx,
82     linker_symval_t *symval, void *opaque)
83 {
84 	fbt_probe_t *fbt, *retfbt;
85 	uint32_t *instr, *limit;
86 	const char *name;
87 	char *modname;
88 
89 	modname = opaque;
90 	name = symval->name;
91 
92 	/* Check if function is excluded from instrumentation */
93 	if (fbt_excluded(name))
94 		return (0);
95 
96 	instr = (uint32_t *)(symval->value);
97 	limit = (uint32_t *)(symval->value + symval->size);
98 
99 	/* Look for sd operation */
100 	for (; instr < limit; instr++) {
101 		if ((*instr & SD_RA_SP_MASK) == SD_RA_SP)
102 			break;
103 	}
104 
105 	if (instr >= limit)
106 		return (0);
107 
108 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
109 	fbt->fbtp_name = name;
110 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
111 	    name, FBT_ENTRY, 3, fbt);
112 	fbt->fbtp_patchpoint = instr;
113 	fbt->fbtp_ctl = lf;
114 	fbt->fbtp_loadcnt = lf->loadcnt;
115 	fbt->fbtp_savedval = *instr;
116 	fbt->fbtp_patchval = FBT_PATCHVAL;
117 	fbt->fbtp_rval = DTRACE_INVOP_SD;
118 	fbt->fbtp_symindx = symindx;
119 
120 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
121 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
122 
123 	lf->fbt_nentries++;
124 
125 	retfbt = NULL;
126 again:
127 	for (; instr < limit; instr++) {
128 		if (*instr == RISCV_INSN_RET)
129 			break;
130 	}
131 
132 	if (instr >= limit)
133 		return (0);
134 
135 	/*
136 	 * We have a winner!
137 	 */
138 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
139 	fbt->fbtp_name = name;
140 	if (retfbt == NULL) {
141 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
142 		    name, FBT_RETURN, 3, fbt);
143 	} else {
144 		retfbt->fbtp_next = fbt;
145 		fbt->fbtp_id = retfbt->fbtp_id;
146 	}
147 	retfbt = fbt;
148 
149 	fbt->fbtp_patchpoint = instr;
150 	fbt->fbtp_ctl = lf;
151 	fbt->fbtp_loadcnt = lf->loadcnt;
152 	fbt->fbtp_symindx = symindx;
153 	fbt->fbtp_rval = DTRACE_INVOP_RET;
154 	fbt->fbtp_savedval = *instr;
155 	fbt->fbtp_patchval = FBT_PATCHVAL;
156 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
157 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
158 
159 	lf->fbt_nentries++;
160 
161 	instr++;
162 	goto again;
163 }
164