1fcb56067SGeorge V. Neville-Neil /*
2fcb56067SGeorge V. Neville-Neil * CDDL HEADER START
3fcb56067SGeorge V. Neville-Neil *
4fcb56067SGeorge V. Neville-Neil * The contents of this file are subject to the terms of the
5fcb56067SGeorge V. Neville-Neil * Common Development and Distribution License (the "License").
6fcb56067SGeorge V. Neville-Neil * You may not use this file except in compliance with the License.
7fcb56067SGeorge V. Neville-Neil *
8fcb56067SGeorge V. Neville-Neil * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcb56067SGeorge V. Neville-Neil * or http://www.opensolaris.org/os/licensing.
10fcb56067SGeorge V. Neville-Neil * See the License for the specific language governing permissions
11fcb56067SGeorge V. Neville-Neil * and limitations under the License.
12fcb56067SGeorge V. Neville-Neil *
13fcb56067SGeorge V. Neville-Neil * When distributing Covered Code, include this CDDL HEADER in each
14fcb56067SGeorge V. Neville-Neil * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcb56067SGeorge V. Neville-Neil * If applicable, add the following below this CDDL HEADER, with the
16fcb56067SGeorge V. Neville-Neil * fields enclosed by brackets "[]" replaced with your own identifying
17fcb56067SGeorge V. Neville-Neil * information: Portions Copyright [yyyy] [name of copyright owner]
18fcb56067SGeorge V. Neville-Neil *
19fcb56067SGeorge V. Neville-Neil * CDDL HEADER END
20fcb56067SGeorge V. Neville-Neil *
21fcb56067SGeorge V. Neville-Neil * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22fcb56067SGeorge V. Neville-Neil * Portions Copyright 2013 Justin Hibbits jhibbits@freebsd.org
23fcb56067SGeorge V. Neville-Neil * Portions Copyright 2013 Howard Su howardsu@freebsd.org
24fcb56067SGeorge V. Neville-Neil *
25fcb56067SGeorge V. Neville-Neil */
26fcb56067SGeorge V. Neville-Neil
27fcb56067SGeorge V. Neville-Neil /*
28fcb56067SGeorge V. Neville-Neil * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
29fcb56067SGeorge V. Neville-Neil * Use is subject to license terms.
30fcb56067SGeorge V. Neville-Neil */
31fcb56067SGeorge V. Neville-Neil
32fcb56067SGeorge V. Neville-Neil #include <sys/param.h>
33fcb56067SGeorge V. Neville-Neil
34fcb56067SGeorge V. Neville-Neil #include <sys/dtrace.h>
3506ef4878SAndrew Turner #include <machine/stack.h>
36c2188153SAndrew Turner #include <machine/trap.h>
37fcb56067SGeorge V. Neville-Neil
38a57dc98eSGeorge V. Neville-Neil #include "fbt.h"
39fcb56067SGeorge V. Neville-Neil
40fcb56067SGeorge V. Neville-Neil #define FBT_PUSHM 0xe92d0000
41fcb56067SGeorge V. Neville-Neil #define FBT_POPM 0xe8bd0000
42fcb56067SGeorge V. Neville-Neil #define FBT_JUMP 0xea000000
4306ef4878SAndrew Turner #define FBT_SUBSP 0xe24dd000
44fcb56067SGeorge V. Neville-Neil
45a57dc98eSGeorge V. Neville-Neil int
fbt_invop(uintptr_t addr,struct trapframe * frame,uintptr_t rval)466c280659SMark Johnston fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
47fcb56067SGeorge V. Neville-Neil {
48fcb56067SGeorge V. Neville-Neil solaris_cpu_t *cpu = &solaris_cpu[curcpu];
49fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
50c2188153SAndrew Turner register_t fifthparam;
51fcb56067SGeorge V. Neville-Neil
52fcb56067SGeorge V. Neville-Neil for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
53*2032c532SAndriy Gapon if ((uintptr_t)fbt->fbtp_patchpoint != addr)
54*2032c532SAndriy Gapon continue;
55*2032c532SAndriy Gapon
56fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = addr;
57fcb56067SGeorge V. Neville-Neil
58*2032c532SAndriy Gapon if (fbt->fbtp_roffset == 0) {
59c2188153SAndrew Turner /* Get 5th parameter from stack */
60c2188153SAndrew Turner DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
61ae691723SAndrew Turner fifthparam = *(register_t *)frame->tf_svc_sp;
62c2188153SAndrew Turner DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
63c2188153SAndrew Turner
64fcb56067SGeorge V. Neville-Neil dtrace_probe(fbt->fbtp_id, frame->tf_r0,
65fcb56067SGeorge V. Neville-Neil frame->tf_r1, frame->tf_r2,
66c2188153SAndrew Turner frame->tf_r3, fifthparam);
67*2032c532SAndriy Gapon } else {
68*2032c532SAndriy Gapon dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval,
69*2032c532SAndriy Gapon 0, 0, 0);
70*2032c532SAndriy Gapon }
71fcb56067SGeorge V. Neville-Neil
72fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = 0;
734a8169d9SAndrew Turner return (fbt->fbtp_rval | (fbt->fbtp_savedval << DTRACE_INVOP_SHIFT));
74fcb56067SGeorge V. Neville-Neil }
75fcb56067SGeorge V. Neville-Neil
76fcb56067SGeorge V. Neville-Neil return (0);
77fcb56067SGeorge V. Neville-Neil }
78fcb56067SGeorge V. Neville-Neil
79a57dc98eSGeorge V. Neville-Neil void
fbt_patch_tracepoint(fbt_probe_t * fbt,fbt_patchval_t val)80a57dc98eSGeorge V. Neville-Neil fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
81a57dc98eSGeorge V. Neville-Neil {
82a57dc98eSGeorge V. Neville-Neil
83a57dc98eSGeorge V. Neville-Neil *fbt->fbtp_patchpoint = val;
847a308c64SMichal Meloun icache_sync((vm_offset_t)fbt->fbtp_patchpoint, sizeof(val));
85a57dc98eSGeorge V. Neville-Neil }
86a57dc98eSGeorge V. Neville-Neil
87a57dc98eSGeorge V. Neville-Neil int
fbt_provide_module_function(linker_file_t lf,int symindx,linker_symval_t * symval,void * opaque)88fcb56067SGeorge V. Neville-Neil fbt_provide_module_function(linker_file_t lf, int symindx,
89fcb56067SGeorge V. Neville-Neil linker_symval_t *symval, void *opaque)
90fcb56067SGeorge V. Neville-Neil {
91fcb56067SGeorge V. Neville-Neil char *modname = opaque;
92fcb56067SGeorge V. Neville-Neil const char *name = symval->name;
93fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt, *retfbt;
94a57dc98eSGeorge V. Neville-Neil uint32_t *instr, *limit;
95fcb56067SGeorge V. Neville-Neil int popm;
96fcb56067SGeorge V. Neville-Neil
978bb9b7f1SMark Johnston if (fbt_excluded(name))
98fcb56067SGeorge V. Neville-Neil return (0);
99fcb56067SGeorge V. Neville-Neil
100a57dc98eSGeorge V. Neville-Neil instr = (uint32_t *)symval->value;
101a57dc98eSGeorge V. Neville-Neil limit = (uint32_t *)(symval->value + symval->size);
102fcb56067SGeorge V. Neville-Neil
10306ef4878SAndrew Turner /*
10406ef4878SAndrew Turner * va_arg functions has first instruction of
10506ef4878SAndrew Turner * sub sp, sp, #?
10606ef4878SAndrew Turner */
10706ef4878SAndrew Turner if ((*instr & 0xfffff000) == FBT_SUBSP)
10806ef4878SAndrew Turner instr++;
109fcb56067SGeorge V. Neville-Neil
11006ef4878SAndrew Turner /*
11106ef4878SAndrew Turner * check if insn is a pushm with LR
11206ef4878SAndrew Turner */
11306ef4878SAndrew Turner if ((*instr & 0xffff0000) != FBT_PUSHM ||
11406ef4878SAndrew Turner (*instr & (1 << LR)) == 0)
115fcb56067SGeorge V. Neville-Neil return (0);
116fcb56067SGeorge V. Neville-Neil
117fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
118fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name;
119fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
120c2188153SAndrew Turner name, FBT_ENTRY, 2, fbt);
121fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr;
122fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf;
123fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt;
124fcb56067SGeorge V. Neville-Neil fbt->fbtp_savedval = *instr;
125c2188153SAndrew Turner fbt->fbtp_patchval = FBT_BREAKPOINT;
126fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_PUSHM;
127fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx;
128fcb56067SGeorge V. Neville-Neil
129fcb56067SGeorge V. Neville-Neil fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
130fcb56067SGeorge V. Neville-Neil fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
131fcb56067SGeorge V. Neville-Neil
132fcb56067SGeorge V. Neville-Neil lf->fbt_nentries++;
133fcb56067SGeorge V. Neville-Neil
134fcb56067SGeorge V. Neville-Neil popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000;
135fcb56067SGeorge V. Neville-Neil
136fcb56067SGeorge V. Neville-Neil retfbt = NULL;
137fcb56067SGeorge V. Neville-Neil again:
138a57dc98eSGeorge V. Neville-Neil for (; instr < limit; instr++) {
139fcb56067SGeorge V. Neville-Neil if (*instr == popm)
140fcb56067SGeorge V. Neville-Neil break;
141a57dc98eSGeorge V. Neville-Neil else if ((*instr & 0xff000000) == FBT_JUMP) {
142a57dc98eSGeorge V. Neville-Neil uint32_t *target, *start;
143fcb56067SGeorge V. Neville-Neil int offset;
144a57dc98eSGeorge V. Neville-Neil
145fcb56067SGeorge V. Neville-Neil offset = (*instr & 0xffffff);
146fcb56067SGeorge V. Neville-Neil offset <<= 8;
147fcb56067SGeorge V. Neville-Neil offset /= 64;
148fcb56067SGeorge V. Neville-Neil target = instr + (2 + offset);
149a57dc98eSGeorge V. Neville-Neil start = (uint32_t *)symval->value;
150fcb56067SGeorge V. Neville-Neil if (target >= limit || target < start)
151fcb56067SGeorge V. Neville-Neil break;
152fcb56067SGeorge V. Neville-Neil }
153fcb56067SGeorge V. Neville-Neil }
154fcb56067SGeorge V. Neville-Neil
155fcb56067SGeorge V. Neville-Neil if (instr >= limit)
156fcb56067SGeorge V. Neville-Neil return (0);
157fcb56067SGeorge V. Neville-Neil
158fcb56067SGeorge V. Neville-Neil /*
159fcb56067SGeorge V. Neville-Neil * We have a winner!
160fcb56067SGeorge V. Neville-Neil */
161fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
162fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name;
163fcb56067SGeorge V. Neville-Neil if (retfbt == NULL) {
164fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
165c2188153SAndrew Turner name, FBT_RETURN, 2, fbt);
166fcb56067SGeorge V. Neville-Neil } else {
167c208cb99SMark Johnston retfbt->fbtp_probenext = fbt;
168fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = retfbt->fbtp_id;
169fcb56067SGeorge V. Neville-Neil }
170fcb56067SGeorge V. Neville-Neil retfbt = fbt;
171fcb56067SGeorge V. Neville-Neil
172fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr;
173fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf;
174fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt;
175fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx;
176fcb56067SGeorge V. Neville-Neil if ((*instr & 0xff000000) == FBT_JUMP)
177fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_B;
178fcb56067SGeorge V. Neville-Neil else
179fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_POPM;
180*2032c532SAndriy Gapon fbt->fbtp_roffset = (uintptr_t)instr - (uintptr_t)symval->value;
181fcb56067SGeorge V. Neville-Neil fbt->fbtp_savedval = *instr;
182c2188153SAndrew Turner fbt->fbtp_patchval = FBT_BREAKPOINT;
183fcb56067SGeorge V. Neville-Neil fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
184fcb56067SGeorge V. Neville-Neil fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
185fcb56067SGeorge V. Neville-Neil
186fcb56067SGeorge V. Neville-Neil lf->fbt_nentries++;
187fcb56067SGeorge V. Neville-Neil
188fcb56067SGeorge V. Neville-Neil instr++;
189fcb56067SGeorge V. Neville-Neil goto again;
190fcb56067SGeorge V. Neville-Neil }
191