Path: blob/main/sys/cddl/dev/fbt/powerpc/fbt_isa.c
48378 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*20* Portions Copyright 2006-2008 John Birrell [email protected]21* Portions Copyright 2013 Justin Hibbits [email protected]22*23*/2425/*26* Copyright 2006 Sun Microsystems, Inc. All rights reserved.27* Use is subject to license terms.28*/2930#include <sys/param.h>31#include <sys/dtrace.h>32#include <machine/md_var.h>3334#include "fbt.h"3536#define FBT_PATCHVAL 0x7ffff80837#define FBT_MFLR_R0 0x7c0802a638#define FBT_MTLR_R0 0x7c0803a639#define FBT_BLR 0x4e80002040#define FBT_BCTR 0x4e80003041#define FBT_BRANCH 0x4800000042#define FBT_BR_MASK 0x03fffffc43#define FBT_IS_JUMP(instr) ((instr & ~FBT_BR_MASK) == FBT_BRANCH)4445#define FBT_AFRAMES 54647int48fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)49{50solaris_cpu_t *cpu = &solaris_cpu[curcpu];51fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];52uintptr_t tmp;5354for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {55if ((uintptr_t)fbt->fbtp_patchpoint == addr) {56if (fbt->fbtp_roffset == 0) {57cpu->cpu_dtrace_caller = addr;5859dtrace_probe(fbt->fbtp_id, frame->fixreg[3],60frame->fixreg[4], frame->fixreg[5],61frame->fixreg[6], frame->fixreg[7]);6263cpu->cpu_dtrace_caller = 0;64} else {6566dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,67rval, 0, 0, 0);68/*69* The caller doesn't have the fbt item, so70* fixup tail calls here.71*/72if (fbt->fbtp_rval == DTRACE_INVOP_JUMP) {73frame->srr0 = (uintptr_t)fbt->fbtp_patchpoint;74tmp = fbt->fbtp_savedval & FBT_BR_MASK;75/* Sign extend. */76if (tmp & 0x02000000)77#ifdef __powerpc64__78tmp |= 0xfffffffffc000000ULL;79#else80tmp |= 0xfc000000UL;81#endif82frame->srr0 += tmp;83}84cpu->cpu_dtrace_caller = 0;85}8687return (fbt->fbtp_rval);88}89}9091return (0);92}9394void95fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)96{9798*fbt->fbtp_patchpoint = val;99__syncicache(fbt->fbtp_patchpoint, 4);100}101102int103fbt_provide_module_function(linker_file_t lf, int symindx,104linker_symval_t *symval, void *opaque)105{106char *modname = opaque;107const char *name = symval->name;108fbt_probe_t *fbt, *retfbt;109int j;110uint32_t *instr, *limit;111112#ifdef __powerpc64__113#if !defined(_CALL_ELF) || _CALL_ELF == 1114/*115* PowerPC64 uses '.' prefixes on symbol names, ignore it, but only116* allow symbols with the '.' prefix, so that we don't get the function117* descriptor instead.118*/119if (name[0] == '.')120name++;121else122return (0);123#endif124#endif125126if (fbt_excluded(name))127return (0);128129instr = (uint32_t *) symval->value;130limit = (uint32_t *) (symval->value + symval->size);131132for (; instr < limit; instr++)133if (*instr == FBT_MFLR_R0)134break;135136if (*instr != FBT_MFLR_R0)137return (0);138139fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);140fbt->fbtp_name = name;141fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,142name, FBT_ENTRY, FBT_AFRAMES, fbt);143fbt->fbtp_patchpoint = instr;144fbt->fbtp_ctl = lf;145fbt->fbtp_loadcnt = lf->loadcnt;146fbt->fbtp_savedval = *instr;147fbt->fbtp_patchval = FBT_PATCHVAL;148fbt->fbtp_rval = DTRACE_INVOP_MFLR_R0;149fbt->fbtp_symindx = symindx;150151fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];152fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;153154lf->fbt_nentries++;155156retfbt = NULL;157again:158if (instr >= limit)159return (0);160161/*162* We (desperately) want to avoid erroneously instrumenting a163* jump table. To determine if we're looking at a true instruction164* sequence or an inline jump table that happens to contain the same165* byte sequences, we resort to some heuristic sleeze: we treat this166* instruction as being contained within a pointer, and see if that167* pointer points to within the body of the function. If it does, we168* refuse to instrument it.169*/170{171uint32_t *ptr;172173ptr = *(uint32_t **)instr;174175if (ptr >= (uint32_t *) symval->value && ptr < limit) {176instr++;177goto again;178}179}180181if (*instr != FBT_MTLR_R0) {182instr++;183goto again;184}185186instr++;187188for (j = 0; j < 12 && instr < limit; j++, instr++) {189if ((*instr == FBT_BCTR) || (*instr == FBT_BLR) ||190FBT_IS_JUMP(*instr))191break;192}193194if (!(*instr == FBT_BCTR || *instr == FBT_BLR || FBT_IS_JUMP(*instr)))195goto again;196197/*198* We have a winner!199*/200fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);201fbt->fbtp_name = name;202203if (retfbt == NULL) {204fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,205name, FBT_RETURN, FBT_AFRAMES, fbt);206} else {207retfbt->fbtp_probenext = fbt;208fbt->fbtp_id = retfbt->fbtp_id;209}210211retfbt = fbt;212fbt->fbtp_patchpoint = instr;213fbt->fbtp_ctl = lf;214fbt->fbtp_loadcnt = lf->loadcnt;215fbt->fbtp_symindx = symindx;216217if (*instr == FBT_BCTR)218fbt->fbtp_rval = DTRACE_INVOP_BCTR;219else if (*instr == FBT_BLR)220fbt->fbtp_rval = DTRACE_INVOP_BLR;221else222fbt->fbtp_rval = DTRACE_INVOP_JUMP;223224fbt->fbtp_roffset =225(uintptr_t)((uint8_t *)instr - (uint8_t *)symval->value);226227fbt->fbtp_savedval = *instr;228fbt->fbtp_patchval = FBT_PATCHVAL;229fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];230fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;231232lf->fbt_nentries++;233234instr += 4;235goto again;236}237238239