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.
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.
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]
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-2018 Ruslan Bukin <br@bsdpad.com>
30 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
31 * Use is subject to license terms.
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
37 #include <sys/dtrace.h>
39 #include <machine/riscvreg.h>
40 #include <machine/encoding.h>
44 #define FBT_C_PATCHVAL MATCH_C_EBREAK
45 #define FBT_PATCHVAL MATCH_EBREAK
46 #define FBT_ENTRY "entry"
47 #define FBT_RETURN "return"
50 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
55 cpu = &solaris_cpu[curcpu];
56 fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
58 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
59 if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
60 cpu->cpu_dtrace_caller = addr;
62 dtrace_probe(fbt->fbtp_id, frame->tf_a[0],
63 frame->tf_a[1], frame->tf_a[2],
64 frame->tf_a[3], frame->tf_a[4]);
66 cpu->cpu_dtrace_caller = 0;
67 return (fbt->fbtp_savedval);
75 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
78 switch(fbt->fbtp_patchval) {
80 *(uint16_t *)fbt->fbtp_patchpoint = (uint16_t)val;
84 *fbt->fbtp_patchpoint = val;
91 match_opcode(uint32_t insn, int match, int mask)
94 if (((insn ^ match) & mask) == 0)
101 check_c_ret(uint32_t **instr)
106 for (i = 0; i < 2; i++) {
107 instr1 = (uint16_t *)(*instr) + i;
108 if (match_opcode(*instr1, (MATCH_C_JR | (X_RA << RD_SHIFT)),
109 (MASK_C_JR | RD_MASK))) {
110 *instr = (uint32_t *)instr1;
119 check_c_sdsp(uint32_t **instr)
124 for (i = 0; i < 2; i++) {
125 instr1 = (uint16_t *)(*instr) + i;
126 if (match_opcode(*instr1, (MATCH_C_SDSP | RS2_C_RA),
127 (MASK_C_SDSP | RS2_C_MASK))) {
128 *instr = (uint32_t *)instr1;
137 fbt_provide_module_function(linker_file_t lf, int symindx,
138 linker_symval_t *symval, void *opaque)
140 fbt_probe_t *fbt, *retfbt;
141 uint32_t *instr, *limit;
150 /* Check if function is excluded from instrumentation */
151 if (fbt_excluded(name))
154 instr = (uint32_t *)(symval->value);
155 limit = (uint32_t *)(symval->value + symval->size);
157 /* Look for sd operation */
158 for (; instr < limit; instr++) {
159 /* Look for a non-compressed store of ra to sp */
160 if (match_opcode(*instr, (MATCH_SD | RS2_RA | RS1_SP),
161 (MASK_SD | RS2_MASK | RS1_MASK))) {
162 rval = DTRACE_INVOP_SD;
163 patchval = FBT_PATCHVAL;
167 /* Look for a 'C'-compressed store of ra to sp. */
168 if (check_c_sdsp(&instr)) {
169 rval = DTRACE_INVOP_C_SDSP;
170 patchval = FBT_C_PATCHVAL;
178 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
179 fbt->fbtp_name = name;
180 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
181 name, FBT_ENTRY, 3, fbt);
182 fbt->fbtp_patchpoint = instr;
184 fbt->fbtp_loadcnt = lf->loadcnt;
185 fbt->fbtp_savedval = *instr;
186 fbt->fbtp_patchval = patchval;
187 fbt->fbtp_rval = rval;
188 fbt->fbtp_symindx = symindx;
190 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
191 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
197 for (; instr < limit; instr++) {
198 /* Look for non-compressed return */
199 if (match_opcode(*instr, (MATCH_JALR | (X_RA << RS1_SHIFT)),
200 (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
201 rval = DTRACE_INVOP_RET;
202 patchval = FBT_PATCHVAL;
206 /* Look for 'C'-compressed return */
207 if (check_c_ret(&instr)) {
208 rval = DTRACE_INVOP_C_RET;
209 patchval = FBT_C_PATCHVAL;
220 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
221 fbt->fbtp_name = name;
222 if (retfbt == NULL) {
223 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
224 name, FBT_RETURN, 3, fbt);
226 retfbt->fbtp_probenext = fbt;
227 fbt->fbtp_id = retfbt->fbtp_id;
231 fbt->fbtp_patchpoint = instr;
233 fbt->fbtp_loadcnt = lf->loadcnt;
234 fbt->fbtp_symindx = symindx;
235 fbt->fbtp_rval = rval;
236 fbt->fbtp_savedval = *instr;
237 fbt->fbtp_patchval = patchval;
238 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
239 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;