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-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#include <sys/dtrace.h>
37
38#include <machine/cpuregs.h>
39#include <machine/cache.h>
40
41#include "fbt.h"
42
43#define	FBT_PATCHVAL		(MIPS_BREAK_INSTR)
44#define	FBT_ENTRY		"entry"
45#define	FBT_RETURN		"return"
46
47int
48fbt_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->a0,
61			    frame->a1, frame->a2,
62			    frame->a3, frame->a4);
63
64			cpu->cpu_dtrace_caller = 0;
65			return (fbt->fbtp_savedval);
66		}
67	}
68
69	return (0);
70}
71
72void
73fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
74{
75
76	*fbt->fbtp_patchpoint = val;
77	mips_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
78}
79
80int
81fbt_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 store double to ra register */
100	for (; instr < limit; instr++) {
101		if ((*instr & LDSD_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;
126again:
127	for (; instr < limit; instr++) {
128		if ((*instr & LDSD_RA_SP_MASK) == LD_RA_SP) {
129			break;
130		}
131	}
132
133	if (instr >= limit)
134		return (0);
135
136	/*
137	 * We have a winner!
138	 */
139	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
140	fbt->fbtp_name = name;
141	if (retfbt == NULL) {
142		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
143		    name, FBT_RETURN, 3, fbt);
144	} else {
145		retfbt->fbtp_probenext = fbt;
146		fbt->fbtp_id = retfbt->fbtp_id;
147	}
148	retfbt = fbt;
149
150	fbt->fbtp_patchpoint = instr;
151	fbt->fbtp_ctl = lf;
152	fbt->fbtp_loadcnt = lf->loadcnt;
153	fbt->fbtp_symindx = symindx;
154	fbt->fbtp_rval = DTRACE_INVOP_LD;
155	fbt->fbtp_savedval = *instr;
156	fbt->fbtp_patchval = FBT_PATCHVAL;
157	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
158	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
159
160	lf->fbt_nentries++;
161
162	instr++;
163	goto again;
164}
165