Path: blob/main/sys/cddl/dev/fbt/aarch64/fbt_isa.c
48375 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* Portions Copyright 2013 Howard Su [email protected]23* Portions Copyright 2015 Ruslan Bukin <[email protected]>24*/2526/*27* Copyright 2006 Sun Microsystems, Inc. All rights reserved.28* Use is subject to license terms.29*/3031#include <sys/param.h>3233#include <sys/dtrace.h>3435#include "fbt.h"3637#define FBT_PATCHVAL DTRACE_PATCHVAL38#define FBT_AFRAMES 43940int41fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)42{43solaris_cpu_t *cpu;44fbt_probe_t *fbt;4546cpu = &solaris_cpu[curcpu];47fbt = fbt_probetab[FBT_ADDR2NDX(addr)];4849for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {50if ((uintptr_t)fbt->fbtp_patchpoint != addr)51continue;5253cpu->cpu_dtrace_caller = addr;5455if (fbt->fbtp_roffset == 0) {56dtrace_probe(fbt->fbtp_id, frame->tf_x[0],57frame->tf_x[1], frame->tf_x[2],58frame->tf_x[3], frame->tf_x[4]);59} else {60dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval,610, 0, 0);62}63cpu->cpu_dtrace_caller = 0;64return (fbt->fbtp_savedval);65}6667return (0);68}6970void71fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)72{73void *addr;7475if (!arm64_get_writable_addr(fbt->fbtp_patchpoint, &addr))76panic("%s: Unable to write new instruction", __func__);7778*(fbt_patchval_t *)addr = val;79cpu_icache_sync_range(fbt->fbtp_patchpoint, 4);80}8182int83fbt_provide_module_function(linker_file_t lf, int symindx,84linker_symval_t *symval, void *opaque)85{86fbt_probe_t *fbt, *retfbt;87uint32_t *target, *start;88uint32_t *instr, *limit;89const char *name;90char *modname;91int offs;9293modname = opaque;94name = symval->name;9596/* Check if function is excluded from instrumentation */97if (fbt_excluded(name))98return (0);99100/*101* Instrumenting certain exception handling functions can lead to FBT102* recursion, so exclude from instrumentation.103*/104if (strcmp(name, "handle_el1h_sync") == 0 ||105strcmp(name, "do_el1h_sync") == 0)106return (1);107108instr = (uint32_t *)(symval->value);109limit = (uint32_t *)(symval->value + symval->size);110111/*112* Ignore any bti instruction at the start of the function113* we need to keep it there for any indirect branches calling114* the function on Armv8.5+115*/116if ((*instr & BTI_MASK) == BTI_INSTR)117instr++;118119/*120* If the first instruction is a nop it's a specially marked121* asm function. We only support a nop first as it's not a normal122* part of the function prologue.123*/124if (*instr == NOP_INSTR)125goto found;126127/* Look for stp (pre-indexed) or sub operation */128for (; instr < limit; instr++) {129/*130* Functions start with "stp xt1, xt2, [xn, <const>]!" or131* "sub sp, sp, <const>".132*133* Sometimes the compiler will have a sub instruction that is134* not of the above type so don't stop if we see one.135*/136if ((*instr & LDP_STP_MASK) == STP_64) {137/*138* Assume any other store of this type means we are139* past the function prologue.140*/141if (((*instr >> ADDR_SHIFT) & ADDR_MASK) == 31)142break;143} else if ((*instr & SUB_MASK) == SUB_INSTR &&144((*instr >> SUB_RD_SHIFT) & SUB_R_MASK) == 31 &&145((*instr >> SUB_RN_SHIFT) & SUB_R_MASK) == 31)146break;147}148found:149if (instr >= limit)150return (0);151152fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);153fbt->fbtp_name = name;154fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,155name, FBT_ENTRY, FBT_AFRAMES, fbt);156fbt->fbtp_patchpoint = instr;157fbt->fbtp_ctl = lf;158fbt->fbtp_loadcnt = lf->loadcnt;159fbt->fbtp_savedval = *instr;160fbt->fbtp_patchval = FBT_PATCHVAL;161if ((*instr & SUB_MASK) == SUB_INSTR)162fbt->fbtp_rval = DTRACE_INVOP_SUB;163else164fbt->fbtp_rval = DTRACE_INVOP_STP;165fbt->fbtp_symindx = symindx;166167fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];168fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;169170lf->fbt_nentries++;171172retfbt = NULL;173again:174for (; instr < limit; instr++) {175if (*instr == RET_INSTR)176break;177else if ((*instr & B_MASK) == B_INSTR) {178offs = (*instr & B_DATA_MASK);179target = instr + offs;180start = (uint32_t *)symval->value;181if (target >= limit || target < start)182break;183}184}185186if (instr >= limit)187return (0);188189/*190* We have a winner!191*/192fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);193fbt->fbtp_name = name;194if (retfbt == NULL) {195fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,196name, FBT_RETURN, FBT_AFRAMES, fbt);197} else {198retfbt->fbtp_probenext = fbt;199fbt->fbtp_id = retfbt->fbtp_id;200}201retfbt = fbt;202203fbt->fbtp_patchpoint = instr;204fbt->fbtp_ctl = lf;205fbt->fbtp_loadcnt = lf->loadcnt;206fbt->fbtp_symindx = symindx;207if ((*instr & B_MASK) == B_INSTR)208fbt->fbtp_rval = DTRACE_INVOP_B;209else210fbt->fbtp_rval = DTRACE_INVOP_RET;211fbt->fbtp_roffset = (uintptr_t)instr - (uintptr_t)symval->value;212fbt->fbtp_savedval = *instr;213fbt->fbtp_patchval = FBT_PATCHVAL;214fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];215fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;216217lf->fbt_nentries++;218219instr++;220goto again;221}222223224