xref: /freebsd/sys/cddl/dev/fbt/riscv/fbt_isa.c (revision 3f68b24e10aeb1a1cd85f2d349da44138d52c501)
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 *target, *start;
86 	uint32_t *instr, *limit;
87 	const char *name;
88 	char *modname;
89 	int offs;
90 
91 	modname = opaque;
92 	name = symval->name;
93 
94 	/* Check if function is excluded from instrumentation */
95 	if (fbt_excluded(name))
96 		return (0);
97 
98 	instr = (uint32_t *)(symval->value);
99 	limit = (uint32_t *)(symval->value + symval->size);
100 
101 	/* Look for sd operation */
102 	for (; instr < limit; instr++) {
103 		if ((*instr & SD_RA_SP_MASK) == SD_RA_SP)
104 			break;
105 	}
106 
107 	if (instr >= limit)
108 		return (0);
109 
110 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
111 	fbt->fbtp_name = name;
112 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
113 	    name, FBT_ENTRY, 3, fbt);
114 	fbt->fbtp_patchpoint = instr;
115 	fbt->fbtp_ctl = lf;
116 	fbt->fbtp_loadcnt = lf->loadcnt;
117 	fbt->fbtp_savedval = *instr;
118 	fbt->fbtp_patchval = FBT_PATCHVAL;
119 	fbt->fbtp_rval = DTRACE_INVOP_SD;
120 	fbt->fbtp_symindx = symindx;
121 
122 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
123 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
124 
125 	lf->fbt_nentries++;
126 
127 	retfbt = NULL;
128 again:
129 	for (; instr < limit; instr++) {
130 		if (*instr == RISCV_INSN_RET)
131 			break;
132 	}
133 
134 	if (instr >= limit)
135 		return (0);
136 
137 	/*
138 	 * We have a winner!
139 	 */
140 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
141 	fbt->fbtp_name = name;
142 	if (retfbt == NULL) {
143 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
144 		    name, FBT_RETURN, 3, fbt);
145 	} else {
146 		retfbt->fbtp_next = fbt;
147 		fbt->fbtp_id = retfbt->fbtp_id;
148 	}
149 	retfbt = fbt;
150 
151 	fbt->fbtp_patchpoint = instr;
152 	fbt->fbtp_ctl = lf;
153 	fbt->fbtp_loadcnt = lf->loadcnt;
154 	fbt->fbtp_symindx = symindx;
155 	fbt->fbtp_rval = DTRACE_INVOP_RET;
156 	fbt->fbtp_savedval = *instr;
157 	fbt->fbtp_patchval = FBT_PATCHVAL;
158 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
159 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
160 
161 	lf->fbt_nentries++;
162 
163 	instr++;
164 	goto again;
165 }
166