fbt_isa.c revision 299118
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: head/sys/cddl/dev/fbt/mips/fbt_isa.c 299118 2016-05-05 13:54:50Z br $
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 *target, *start;
86	uint32_t *instr, *limit;
87	const char *name;
88	char *modname;
89
90	modname = opaque;
91	name = symval->name;
92
93	/* Check if function is excluded from instrumentation */
94	if (fbt_excluded(name))
95		return (0);
96
97	instr = (uint32_t *)(symval->value);
98	limit = (uint32_t *)(symval->value + symval->size);
99
100	/* Look for store double to ra register */
101	for (; instr < limit; instr++) {
102		if ((*instr & LDSD_RA_SP_MASK) == SD_RA_SP)
103			break;
104	}
105
106	if (instr >= limit)
107		return (0);
108
109	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
110	fbt->fbtp_name = name;
111	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
112	    name, FBT_ENTRY, 3, fbt);
113	fbt->fbtp_patchpoint = instr;
114	fbt->fbtp_ctl = lf;
115	fbt->fbtp_loadcnt = lf->loadcnt;
116	fbt->fbtp_savedval = *instr;
117	fbt->fbtp_patchval = FBT_PATCHVAL;
118	fbt->fbtp_rval = DTRACE_INVOP_SD;
119	fbt->fbtp_symindx = symindx;
120
121	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
122	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
123
124	lf->fbt_nentries++;
125
126	retfbt = NULL;
127again:
128	for (; instr < limit; instr++) {
129		if ((*instr & LDSD_RA_SP_MASK) == LD_RA_SP) {
130			break;
131		}
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_LD;
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