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#define	FBT_AFRAMES	4
48
49int
50fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
51{
52	solaris_cpu_t *cpu;
53	fbt_probe_t *fbt;
54
55	cpu = &solaris_cpu[curcpu];
56	fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
57
58	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
59		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
60			cpu->cpu_dtrace_caller = addr;
61
62			dtrace_probe(fbt->fbtp_id, frame->tf_x[0],
63			    frame->tf_x[1], frame->tf_x[2],
64			    frame->tf_x[3], frame->tf_x[4]);
65
66			cpu->cpu_dtrace_caller = 0;
67			return (fbt->fbtp_savedval);
68		}
69	}
70
71	return (0);
72}
73
74void
75fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
76{
77
78	*fbt->fbtp_patchpoint = val;
79	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
80}
81
82int
83fbt_provide_module_function(linker_file_t lf, int symindx,
84    linker_symval_t *symval, void *opaque)
85{
86	fbt_probe_t *fbt, *retfbt;
87	uint32_t *target, *start;
88	uint32_t *instr, *limit;
89	const char *name;
90	char *modname;
91	bool found;
92	int offs;
93
94	modname = opaque;
95	name = symval->name;
96
97	/* Check if function is excluded from instrumentation */
98	if (fbt_excluded(name))
99		return (0);
100
101	/*
102	 * Instrumenting certain exception handling functions can lead to FBT
103	 * recursion, so exclude from instrumentation.
104	 */
105	 if (strcmp(name, "handle_el1h_sync") == 0 ||
106	    strcmp(name, "do_el1h_sync") == 0)
107		return (1);
108
109	instr = (uint32_t *)(symval->value);
110	limit = (uint32_t *)(symval->value + symval->size);
111
112	/* Look for stp (pre-indexed) operation */
113	found = false;
114	for (; instr < limit; instr++) {
115		/* Some functions start with "stp xt1, xt2, [xn, <const>]!" */
116		if ((*instr & LDP_STP_MASK) == STP_64) {
117			/*
118			 * Assume any other store of this type means we
119			 * are past the function prolog.
120			 */
121			if (((*instr >> ADDR_SHIFT) & ADDR_MASK) == 31)
122				found = true;
123			break;
124		}
125
126		/*
127		 * Some functions start with a "sub sp, sp, <const>"
128		 * Sometimes the compiler will have a sub instruction that
129		 * is not of the above type so don't stop if we see one.
130		 */
131		if ((*instr & SUB_MASK) == SUB_INSTR &&
132		    ((*instr >> SUB_RD_SHIFT) & SUB_R_MASK) == 31 &&
133		    ((*instr >> SUB_RN_SHIFT) & SUB_R_MASK) == 31) {
134			found = true;
135			break;
136		}
137	}
138
139	if (!found)
140		return (0);
141
142	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
143	fbt->fbtp_name = name;
144	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
145	    name, FBT_ENTRY, FBT_AFRAMES, fbt);
146	fbt->fbtp_patchpoint = instr;
147	fbt->fbtp_ctl = lf;
148	fbt->fbtp_loadcnt = lf->loadcnt;
149	fbt->fbtp_savedval = *instr;
150	fbt->fbtp_patchval = FBT_PATCHVAL;
151	if ((*instr & SUB_MASK) == SUB_INSTR)
152		fbt->fbtp_rval = DTRACE_INVOP_SUB;
153	else
154		fbt->fbtp_rval = DTRACE_INVOP_STP;
155	fbt->fbtp_symindx = symindx;
156
157	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
158	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
159
160	lf->fbt_nentries++;
161
162	retfbt = NULL;
163again:
164	for (; instr < limit; instr++) {
165		if (*instr == RET_INSTR)
166			break;
167		else if ((*instr & B_MASK) == B_INSTR) {
168			offs = (*instr & B_DATA_MASK);
169			offs *= 4;
170			target = (instr + offs);
171			start = (uint32_t *)symval->value;
172			if (target >= limit || target < start)
173				break;
174		}
175	}
176
177	if (instr >= limit)
178		return (0);
179
180	/*
181	 * We have a winner!
182	 */
183	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
184	fbt->fbtp_name = name;
185	if (retfbt == NULL) {
186		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
187		    name, FBT_RETURN, FBT_AFRAMES, fbt);
188	} else {
189		retfbt->fbtp_probenext = fbt;
190		fbt->fbtp_id = retfbt->fbtp_id;
191	}
192	retfbt = fbt;
193
194	fbt->fbtp_patchpoint = instr;
195	fbt->fbtp_ctl = lf;
196	fbt->fbtp_loadcnt = lf->loadcnt;
197	fbt->fbtp_symindx = symindx;
198	if ((*instr & B_MASK) == B_INSTR)
199		fbt->fbtp_rval = DTRACE_INVOP_B;
200	else
201		fbt->fbtp_rval = DTRACE_INVOP_RET;
202	fbt->fbtp_savedval = *instr;
203	fbt->fbtp_patchval = FBT_PATCHVAL;
204	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
205	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
206
207	lf->fbt_nentries++;
208
209	instr++;
210	goto again;
211}
212