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 * $FreeBSD$ 26fcb56067SGeorge V. Neville-Neil * 27fcb56067SGeorge V. Neville-Neil */ 28fcb56067SGeorge V. Neville-Neil 29fcb56067SGeorge V. Neville-Neil /* 30fcb56067SGeorge V. Neville-Neil * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 31fcb56067SGeorge V. Neville-Neil * Use is subject to license terms. 32fcb56067SGeorge V. Neville-Neil */ 33fcb56067SGeorge V. Neville-Neil 34fcb56067SGeorge V. Neville-Neil #include <sys/cdefs.h> 35fcb56067SGeorge V. Neville-Neil #include <sys/param.h> 36fcb56067SGeorge V. Neville-Neil 37fcb56067SGeorge V. Neville-Neil #include <sys/dtrace.h> 3806ef4878SAndrew Turner #include <machine/stack.h> 39c2188153SAndrew Turner #include <machine/trap.h> 40fcb56067SGeorge V. Neville-Neil 41a57dc98eSGeorge V. Neville-Neil #include "fbt.h" 42fcb56067SGeorge V. Neville-Neil 43fcb56067SGeorge V. Neville-Neil #define FBT_PUSHM 0xe92d0000 44fcb56067SGeorge V. Neville-Neil #define FBT_POPM 0xe8bd0000 45fcb56067SGeorge V. Neville-Neil #define FBT_JUMP 0xea000000 4606ef4878SAndrew Turner #define FBT_SUBSP 0xe24dd000 47fcb56067SGeorge V. Neville-Neil 48fcb56067SGeorge V. Neville-Neil #define FBT_ENTRY "entry" 49fcb56067SGeorge V. Neville-Neil #define FBT_RETURN "return" 50fcb56067SGeorge V. Neville-Neil 51a57dc98eSGeorge V. Neville-Neil int 526c280659SMark Johnston fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval) 53fcb56067SGeorge V. Neville-Neil { 54fcb56067SGeorge V. Neville-Neil solaris_cpu_t *cpu = &solaris_cpu[curcpu]; 55fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 56c2188153SAndrew Turner register_t fifthparam; 57fcb56067SGeorge V. Neville-Neil 58fcb56067SGeorge V. Neville-Neil for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 59fcb56067SGeorge V. Neville-Neil if ((uintptr_t)fbt->fbtp_patchpoint == addr) { 60fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = addr; 61fcb56067SGeorge V. Neville-Neil 62c2188153SAndrew Turner /* Get 5th parameter from stack */ 63c2188153SAndrew Turner DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 64ae691723SAndrew Turner fifthparam = *(register_t *)frame->tf_svc_sp; 65c2188153SAndrew Turner DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR); 66c2188153SAndrew Turner 67fcb56067SGeorge V. Neville-Neil dtrace_probe(fbt->fbtp_id, frame->tf_r0, 68fcb56067SGeorge V. Neville-Neil frame->tf_r1, frame->tf_r2, 69c2188153SAndrew Turner frame->tf_r3, fifthparam); 70fcb56067SGeorge V. Neville-Neil 71fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = 0; 72fcb56067SGeorge V. Neville-Neil 734a8169d9SAndrew Turner return (fbt->fbtp_rval | (fbt->fbtp_savedval << DTRACE_INVOP_SHIFT)); 74fcb56067SGeorge V. Neville-Neil } 75fcb56067SGeorge V. Neville-Neil } 76fcb56067SGeorge V. Neville-Neil 77fcb56067SGeorge V. Neville-Neil return (0); 78fcb56067SGeorge V. Neville-Neil } 79fcb56067SGeorge V. Neville-Neil 80a57dc98eSGeorge V. Neville-Neil void 81a57dc98eSGeorge V. Neville-Neil fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 82a57dc98eSGeorge V. Neville-Neil { 83a57dc98eSGeorge V. Neville-Neil 84a57dc98eSGeorge V. Neville-Neil *fbt->fbtp_patchpoint = val; 857a308c64SMichal Meloun icache_sync((vm_offset_t)fbt->fbtp_patchpoint, sizeof(val)); 86a57dc98eSGeorge V. Neville-Neil } 87a57dc98eSGeorge V. Neville-Neil 88a57dc98eSGeorge V. Neville-Neil int 89fcb56067SGeorge V. Neville-Neil fbt_provide_module_function(linker_file_t lf, int symindx, 90fcb56067SGeorge V. Neville-Neil linker_symval_t *symval, void *opaque) 91fcb56067SGeorge V. Neville-Neil { 92fcb56067SGeorge V. Neville-Neil char *modname = opaque; 93fcb56067SGeorge V. Neville-Neil const char *name = symval->name; 94fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt, *retfbt; 95a57dc98eSGeorge V. Neville-Neil uint32_t *instr, *limit; 96fcb56067SGeorge V. Neville-Neil int popm; 97fcb56067SGeorge V. Neville-Neil 988bb9b7f1SMark Johnston if (fbt_excluded(name)) 99fcb56067SGeorge V. Neville-Neil return (0); 100fcb56067SGeorge V. Neville-Neil 101a57dc98eSGeorge V. Neville-Neil instr = (uint32_t *)symval->value; 102a57dc98eSGeorge V. Neville-Neil limit = (uint32_t *)(symval->value + symval->size); 103fcb56067SGeorge V. Neville-Neil 10406ef4878SAndrew Turner /* 10506ef4878SAndrew Turner * va_arg functions has first instruction of 10606ef4878SAndrew Turner * sub sp, sp, #? 10706ef4878SAndrew Turner */ 10806ef4878SAndrew Turner if ((*instr & 0xfffff000) == FBT_SUBSP) 10906ef4878SAndrew Turner instr++; 110fcb56067SGeorge V. Neville-Neil 11106ef4878SAndrew Turner /* 11206ef4878SAndrew Turner * check if insn is a pushm with LR 11306ef4878SAndrew Turner */ 11406ef4878SAndrew Turner if ((*instr & 0xffff0000) != FBT_PUSHM || 11506ef4878SAndrew Turner (*instr & (1 << LR)) == 0) 116fcb56067SGeorge V. Neville-Neil return (0); 117fcb56067SGeorge V. Neville-Neil 118fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 119fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name; 120fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 121c2188153SAndrew Turner name, FBT_ENTRY, 2, fbt); 122fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr; 123fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf; 124fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt; 125fcb56067SGeorge V. Neville-Neil fbt->fbtp_savedval = *instr; 126c2188153SAndrew Turner fbt->fbtp_patchval = FBT_BREAKPOINT; 127fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_PUSHM; 128fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx; 129fcb56067SGeorge V. Neville-Neil 130fcb56067SGeorge V. Neville-Neil fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 131fcb56067SGeorge V. Neville-Neil fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 132fcb56067SGeorge V. Neville-Neil 133fcb56067SGeorge V. Neville-Neil lf->fbt_nentries++; 134fcb56067SGeorge V. Neville-Neil 135fcb56067SGeorge V. Neville-Neil popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000; 136fcb56067SGeorge V. Neville-Neil 137fcb56067SGeorge V. Neville-Neil retfbt = NULL; 138fcb56067SGeorge V. Neville-Neil again: 139a57dc98eSGeorge V. Neville-Neil for (; instr < limit; instr++) { 140fcb56067SGeorge V. Neville-Neil if (*instr == popm) 141fcb56067SGeorge V. Neville-Neil break; 142a57dc98eSGeorge V. Neville-Neil else if ((*instr & 0xff000000) == FBT_JUMP) { 143a57dc98eSGeorge V. Neville-Neil uint32_t *target, *start; 144fcb56067SGeorge V. Neville-Neil int offset; 145a57dc98eSGeorge V. Neville-Neil 146fcb56067SGeorge V. Neville-Neil offset = (*instr & 0xffffff); 147fcb56067SGeorge V. Neville-Neil offset <<= 8; 148fcb56067SGeorge V. Neville-Neil offset /= 64; 149fcb56067SGeorge V. Neville-Neil target = instr + (2 + offset); 150a57dc98eSGeorge V. Neville-Neil start = (uint32_t *)symval->value; 151fcb56067SGeorge V. Neville-Neil if (target >= limit || target < start) 152fcb56067SGeorge V. Neville-Neil break; 153fcb56067SGeorge V. Neville-Neil } 154fcb56067SGeorge V. Neville-Neil } 155fcb56067SGeorge V. Neville-Neil 156fcb56067SGeorge V. Neville-Neil if (instr >= limit) 157fcb56067SGeorge V. Neville-Neil return (0); 158fcb56067SGeorge V. Neville-Neil 159fcb56067SGeorge V. Neville-Neil /* 160fcb56067SGeorge V. Neville-Neil * We have a winner! 161fcb56067SGeorge V. Neville-Neil */ 162fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 163fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name; 164fcb56067SGeorge V. Neville-Neil if (retfbt == NULL) { 165fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 166c2188153SAndrew Turner name, FBT_RETURN, 2, fbt); 167fcb56067SGeorge V. Neville-Neil } else { 168*c208cb99SMark Johnston retfbt->fbtp_probenext = fbt; 169fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = retfbt->fbtp_id; 170fcb56067SGeorge V. Neville-Neil } 171fcb56067SGeorge V. Neville-Neil retfbt = fbt; 172fcb56067SGeorge V. Neville-Neil 173fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr; 174fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf; 175fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt; 176fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx; 177fcb56067SGeorge V. Neville-Neil if ((*instr & 0xff000000) == FBT_JUMP) 178fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_B; 179fcb56067SGeorge V. Neville-Neil else 180fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_POPM; 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