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> 38*06ef4878SAndrew 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 46*06ef4878SAndrew 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 52fcb56067SGeorge V. Neville-Neil fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval) 53fcb56067SGeorge V. Neville-Neil { 54fcb56067SGeorge V. Neville-Neil struct trapframe *frame = (struct trapframe *)stack; 55fcb56067SGeorge V. Neville-Neil solaris_cpu_t *cpu = &solaris_cpu[curcpu]; 56fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 57c2188153SAndrew Turner register_t fifthparam; 58fcb56067SGeorge V. Neville-Neil 59fcb56067SGeorge V. Neville-Neil for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 60fcb56067SGeorge V. Neville-Neil if ((uintptr_t)fbt->fbtp_patchpoint == addr) { 61fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = addr; 62fcb56067SGeorge V. Neville-Neil 63c2188153SAndrew Turner /* Get 5th parameter from stack */ 64c2188153SAndrew Turner DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 65c2188153SAndrew Turner fifthparam = *(register_t *)frame->tf_usr_sp; 66c2188153SAndrew Turner DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR); 67c2188153SAndrew Turner 68fcb56067SGeorge V. Neville-Neil dtrace_probe(fbt->fbtp_id, frame->tf_r0, 69fcb56067SGeorge V. Neville-Neil frame->tf_r1, frame->tf_r2, 70c2188153SAndrew Turner frame->tf_r3, fifthparam); 71fcb56067SGeorge V. Neville-Neil 72fcb56067SGeorge V. Neville-Neil cpu->cpu_dtrace_caller = 0; 73fcb56067SGeorge V. Neville-Neil 744a8169d9SAndrew Turner return (fbt->fbtp_rval | (fbt->fbtp_savedval << DTRACE_INVOP_SHIFT)); 75fcb56067SGeorge V. Neville-Neil } 76fcb56067SGeorge V. Neville-Neil } 77fcb56067SGeorge V. Neville-Neil 78fcb56067SGeorge V. Neville-Neil return (0); 79fcb56067SGeorge V. Neville-Neil } 80fcb56067SGeorge V. Neville-Neil 81a57dc98eSGeorge V. Neville-Neil void 82a57dc98eSGeorge V. Neville-Neil fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 83a57dc98eSGeorge V. Neville-Neil { 84a57dc98eSGeorge V. Neville-Neil 85a57dc98eSGeorge V. Neville-Neil *fbt->fbtp_patchpoint = val; 86c2188153SAndrew Turner cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, sizeof(val)); 87a57dc98eSGeorge V. Neville-Neil } 88a57dc98eSGeorge V. Neville-Neil 89a57dc98eSGeorge V. Neville-Neil int 90fcb56067SGeorge V. Neville-Neil fbt_provide_module_function(linker_file_t lf, int symindx, 91fcb56067SGeorge V. Neville-Neil linker_symval_t *symval, void *opaque) 92fcb56067SGeorge V. Neville-Neil { 93fcb56067SGeorge V. Neville-Neil char *modname = opaque; 94fcb56067SGeorge V. Neville-Neil const char *name = symval->name; 95fcb56067SGeorge V. Neville-Neil fbt_probe_t *fbt, *retfbt; 96a57dc98eSGeorge V. Neville-Neil uint32_t *instr, *limit; 97fcb56067SGeorge V. Neville-Neil int popm; 98fcb56067SGeorge V. Neville-Neil 99fcb56067SGeorge V. Neville-Neil if (strncmp(name, "dtrace_", 7) == 0 && 100fcb56067SGeorge V. Neville-Neil strncmp(name, "dtrace_safe_", 12) != 0) { 101fcb56067SGeorge V. Neville-Neil /* 102fcb56067SGeorge V. Neville-Neil * Anything beginning with "dtrace_" may be called 103fcb56067SGeorge V. Neville-Neil * from probe context unless it explicitly indicates 104fcb56067SGeorge V. Neville-Neil * that it won't be called from probe context by 105fcb56067SGeorge V. Neville-Neil * using the prefix "dtrace_safe_". 106fcb56067SGeorge V. Neville-Neil */ 107fcb56067SGeorge V. Neville-Neil return (0); 108fcb56067SGeorge V. Neville-Neil } 109fcb56067SGeorge V. Neville-Neil 110fcb56067SGeorge V. Neville-Neil if (name[0] == '_' && name[1] == '_') 111fcb56067SGeorge V. Neville-Neil return (0); 112fcb56067SGeorge V. Neville-Neil 113a57dc98eSGeorge V. Neville-Neil instr = (uint32_t *)symval->value; 114a57dc98eSGeorge V. Neville-Neil limit = (uint32_t *)(symval->value + symval->size); 115fcb56067SGeorge V. Neville-Neil 116*06ef4878SAndrew Turner /* 117*06ef4878SAndrew Turner * va_arg functions has first instruction of 118*06ef4878SAndrew Turner * sub sp, sp, #? 119*06ef4878SAndrew Turner */ 120*06ef4878SAndrew Turner if ((*instr & 0xfffff000) == FBT_SUBSP) 121*06ef4878SAndrew Turner instr++; 122fcb56067SGeorge V. Neville-Neil 123*06ef4878SAndrew Turner /* 124*06ef4878SAndrew Turner * check if insn is a pushm with LR 125*06ef4878SAndrew Turner */ 126*06ef4878SAndrew Turner if ((*instr & 0xffff0000) != FBT_PUSHM || 127*06ef4878SAndrew Turner (*instr & (1 << LR)) == 0) 128fcb56067SGeorge V. Neville-Neil return (0); 129fcb56067SGeorge V. Neville-Neil 130fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 131fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name; 132fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 133c2188153SAndrew Turner name, FBT_ENTRY, 2, fbt); 134fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr; 135fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf; 136fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt; 137fcb56067SGeorge V. Neville-Neil fbt->fbtp_savedval = *instr; 138c2188153SAndrew Turner fbt->fbtp_patchval = FBT_BREAKPOINT; 139fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_PUSHM; 140fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx; 141fcb56067SGeorge V. Neville-Neil 142fcb56067SGeorge V. Neville-Neil fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 143fcb56067SGeorge V. Neville-Neil fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 144fcb56067SGeorge V. Neville-Neil 145fcb56067SGeorge V. Neville-Neil lf->fbt_nentries++; 146fcb56067SGeorge V. Neville-Neil 147fcb56067SGeorge V. Neville-Neil popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000; 148fcb56067SGeorge V. Neville-Neil 149fcb56067SGeorge V. Neville-Neil retfbt = NULL; 150fcb56067SGeorge V. Neville-Neil again: 151a57dc98eSGeorge V. Neville-Neil for (; instr < limit; instr++) { 152fcb56067SGeorge V. Neville-Neil if (*instr == popm) 153fcb56067SGeorge V. Neville-Neil break; 154a57dc98eSGeorge V. Neville-Neil else if ((*instr & 0xff000000) == FBT_JUMP) { 155a57dc98eSGeorge V. Neville-Neil uint32_t *target, *start; 156fcb56067SGeorge V. Neville-Neil int offset; 157a57dc98eSGeorge V. Neville-Neil 158fcb56067SGeorge V. Neville-Neil offset = (*instr & 0xffffff); 159fcb56067SGeorge V. Neville-Neil offset <<= 8; 160fcb56067SGeorge V. Neville-Neil offset /= 64; 161fcb56067SGeorge V. Neville-Neil target = instr + (2 + offset); 162a57dc98eSGeorge V. Neville-Neil start = (uint32_t *)symval->value; 163fcb56067SGeorge V. Neville-Neil if (target >= limit || target < start) 164fcb56067SGeorge V. Neville-Neil break; 165fcb56067SGeorge V. Neville-Neil } 166fcb56067SGeorge V. Neville-Neil } 167fcb56067SGeorge V. Neville-Neil 168fcb56067SGeorge V. Neville-Neil if (instr >= limit) 169fcb56067SGeorge V. Neville-Neil return (0); 170fcb56067SGeorge V. Neville-Neil 171fcb56067SGeorge V. Neville-Neil /* 172fcb56067SGeorge V. Neville-Neil * We have a winner! 173fcb56067SGeorge V. Neville-Neil */ 174fcb56067SGeorge V. Neville-Neil fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 175fcb56067SGeorge V. Neville-Neil fbt->fbtp_name = name; 176fcb56067SGeorge V. Neville-Neil if (retfbt == NULL) { 177fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 178c2188153SAndrew Turner name, FBT_RETURN, 2, fbt); 179fcb56067SGeorge V. Neville-Neil } else { 180fcb56067SGeorge V. Neville-Neil retfbt->fbtp_next = fbt; 181fcb56067SGeorge V. Neville-Neil fbt->fbtp_id = retfbt->fbtp_id; 182fcb56067SGeorge V. Neville-Neil } 183fcb56067SGeorge V. Neville-Neil retfbt = fbt; 184fcb56067SGeorge V. Neville-Neil 185fcb56067SGeorge V. Neville-Neil fbt->fbtp_patchpoint = instr; 186fcb56067SGeorge V. Neville-Neil fbt->fbtp_ctl = lf; 187fcb56067SGeorge V. Neville-Neil fbt->fbtp_loadcnt = lf->loadcnt; 188fcb56067SGeorge V. Neville-Neil fbt->fbtp_symindx = symindx; 189fcb56067SGeorge V. Neville-Neil if ((*instr & 0xff000000) == FBT_JUMP) 190fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_B; 191fcb56067SGeorge V. Neville-Neil else 192fcb56067SGeorge V. Neville-Neil fbt->fbtp_rval = DTRACE_INVOP_POPM; 193fcb56067SGeorge V. Neville-Neil fbt->fbtp_savedval = *instr; 194c2188153SAndrew Turner fbt->fbtp_patchval = FBT_BREAKPOINT; 195fcb56067SGeorge V. Neville-Neil fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 196fcb56067SGeorge V. Neville-Neil fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 197fcb56067SGeorge V. Neville-Neil 198fcb56067SGeorge V. Neville-Neil lf->fbt_nentries++; 199fcb56067SGeorge V. Neville-Neil 200fcb56067SGeorge V. Neville-Neil instr++; 201fcb56067SGeorge V. Neville-Neil goto again; 202fcb56067SGeorge V. Neville-Neil } 203