xref: /freebsd/sys/cddl/dev/fbt/aarch64/fbt_isa.c (revision 119b75925c562202145d7bac7b676b98029c6cb9)
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 2015 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 "fbt.h"
40 
41 #define	AARCH64_BRK		0xd4200000
42 #define	AARCH64_BRK_IMM16_SHIFT	5
43 #define	AARCH64_BRK_IMM16_VAL	(0x40d << AARCH64_BRK_IMM16_SHIFT)
44 #define	FBT_PATCHVAL		(AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
45 #define	FBT_ENTRY	"entry"
46 #define	FBT_RETURN	"return"
47 
48 int
49 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
50 {
51 	struct trapframe *frame;
52 	solaris_cpu_t *cpu;
53 	fbt_probe_t *fbt;
54 
55 	frame = (struct trapframe *)stack;
56 	cpu = &solaris_cpu[curcpu];
57 	fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
58 
59 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
60 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
61 			fbt->fbtp_invop_cnt++;
62 			cpu->cpu_dtrace_caller = addr;
63 
64 			dtrace_probe(fbt->fbtp_id, frame->tf_x[0],
65 			    frame->tf_x[1], frame->tf_x[2],
66 			    frame->tf_x[3], frame->tf_x[4]);
67 
68 			cpu->cpu_dtrace_caller = 0;
69 			return (fbt->fbtp_savedval);
70 		}
71 	}
72 
73 	return (0);
74 }
75 
76 void
77 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
78 {
79 
80 	*fbt->fbtp_patchpoint = val;
81 	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
82 }
83 
84 int
85 fbt_provide_module_function(linker_file_t lf, int symindx,
86     linker_symval_t *symval, void *opaque)
87 {
88 	fbt_probe_t *fbt, *retfbt;
89 	uint32_t *target, *start;
90 	uint32_t *instr, *limit;
91 	const char *name;
92 	char *modname;
93 	int offs;
94 
95 	modname = opaque;
96 	name = symval->name;
97 
98 	/* Check if function is excluded from instrumentation */
99 	if (fbt_excluded(name))
100 		return (0);
101 
102 	instr = (uint32_t *)(symval->value);
103 	limit = (uint32_t *)(symval->value + symval->size);
104 
105 	/* Look for stp (pre-indexed) operation */
106 	for (; instr < limit; instr++) {
107 		if ((*instr & LDP_STP_MASK) == STP_64)
108 			break;
109 	}
110 
111 	if (instr >= limit)
112 		return (0);
113 
114 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
115 	fbt->fbtp_name = name;
116 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
117 	    name, FBT_ENTRY, 3, fbt);
118 	fbt->fbtp_patchpoint = instr;
119 	fbt->fbtp_ctl = lf;
120 	fbt->fbtp_loadcnt = lf->loadcnt;
121 	fbt->fbtp_savedval = *instr;
122 	fbt->fbtp_patchval = FBT_PATCHVAL;
123 	fbt->fbtp_rval = DTRACE_INVOP_PUSHM;
124 	fbt->fbtp_symindx = symindx;
125 
126 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
127 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
128 
129 	lf->fbt_nentries++;
130 
131 	retfbt = NULL;
132 again:
133 	for (; instr < limit; instr++) {
134 		if (*instr == RET_INSTR)
135 			break;
136 		else if ((*instr & B_MASK) == B_INSTR) {
137 			offs = (*instr & B_DATA_MASK);
138 			offs *= 4;
139 			target = (instr + offs);
140 			start = (uint32_t *)symval->value;
141 			if (target >= limit || target < start)
142 				break;
143 		}
144 	}
145 
146 	if (instr >= limit)
147 		return (0);
148 
149 	/*
150 	 * We have a winner!
151 	 */
152 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
153 	fbt->fbtp_name = name;
154 	if (retfbt == NULL) {
155 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
156 		    name, FBT_RETURN, 3, fbt);
157 	} else {
158 		retfbt->fbtp_next = fbt;
159 		fbt->fbtp_id = retfbt->fbtp_id;
160 	}
161 	retfbt = fbt;
162 
163 	fbt->fbtp_patchpoint = instr;
164 	fbt->fbtp_ctl = lf;
165 	fbt->fbtp_loadcnt = lf->loadcnt;
166 	fbt->fbtp_symindx = symindx;
167 	if ((*instr & B_MASK) == B_INSTR)
168 		fbt->fbtp_rval = DTRACE_INVOP_B;
169 	else
170 		fbt->fbtp_rval = DTRACE_INVOP_RET;
171 	fbt->fbtp_savedval = *instr;
172 	fbt->fbtp_patchval = FBT_PATCHVAL;
173 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
174 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
175 
176 	lf->fbt_nentries++;
177 
178 	instr++;
179 	goto again;
180 }
181