Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Aug 2014 21:42:55 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r270067 - in head/sys: cddl/dev/fbt cddl/dev/fbt/powerpc cddl/dev/fbt/x86 modules/dtrace/fbt
Message-ID:  <201408162142.s7GLgtPF081431@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Sat Aug 16 21:42:55 2014
New Revision: 270067
URL: http://svnweb.freebsd.org/changeset/base/270067

Log:
  Factor out the common code for function boundary tracing instead of
  duplicating the entire implementation for both x86 and powerpc. This makes
  it easier to add support for other architectures and has no functional
  impact.
  
  Phabric:	D613
  Reviewed by:	gnn, jhibbits, rpaulo
  Tested by:	jhibbits (powerpc)
  MFC after:	2 weeks

Added:
  head/sys/cddl/dev/fbt/fbt.h   (contents, props changed)
  head/sys/cddl/dev/fbt/powerpc/
  head/sys/cddl/dev/fbt/powerpc/fbt_isa.c   (contents, props changed)
  head/sys/cddl/dev/fbt/powerpc/fbt_isa.h   (contents, props changed)
  head/sys/cddl/dev/fbt/x86/
  head/sys/cddl/dev/fbt/x86/fbt_isa.c   (contents, props changed)
  head/sys/cddl/dev/fbt/x86/fbt_isa.h   (contents, props changed)
Deleted:
  head/sys/cddl/dev/fbt/fbt_powerpc.c
Modified:
  head/sys/cddl/dev/fbt/fbt.c
  head/sys/modules/dtrace/fbt/Makefile

Modified: head/sys/cddl/dev/fbt/fbt.c
==============================================================================
--- head/sys/cddl/dev/fbt/fbt.c	Sat Aug 16 21:36:22 2014	(r270066)
+++ head/sys/cddl/dev/fbt/fbt.c	Sat Aug 16 21:42:55 2014	(r270067)
@@ -61,25 +61,13 @@
 #include <sys/dtrace.h>
 #include <sys/dtrace_bsd.h>
 
-static MALLOC_DEFINE(M_FBT, "fbt", "Function Boundary Tracing");
+#include "fbt.h"
 
-#define	FBT_PUSHL_EBP		0x55
-#define	FBT_MOVL_ESP_EBP0_V0	0x8b
-#define	FBT_MOVL_ESP_EBP1_V0	0xec
-#define	FBT_MOVL_ESP_EBP0_V1	0x89
-#define	FBT_MOVL_ESP_EBP1_V1	0xe5
-#define	FBT_REX_RSP_RBP		0x48
-
-#define	FBT_POPL_EBP		0x5d
-#define	FBT_RET			0xc3
-#define	FBT_RET_IMM16		0xc2
-#define	FBT_LEAVE		0xc9
-
-#ifdef __amd64__
-#define	FBT_PATCHVAL		0xcc
-#else
-#define	FBT_PATCHVAL		0xf0
-#endif
+MALLOC_DEFINE(M_FBT, "fbt", "Function Boundary Tracing");
+
+dtrace_provider_id_t	fbt_id;
+fbt_probe_t		**fbt_probetab;
+int			fbt_probetab_mask;
 
 static d_open_t	fbt_open;
 static int	fbt_unload(void);
@@ -92,11 +80,6 @@ static void	fbt_load(void *);
 static void	fbt_suspend(void *, dtrace_id_t, void *);
 static void	fbt_resume(void *, dtrace_id_t, void *);
 
-#define	FBT_ENTRY	"entry"
-#define	FBT_RETURN	"return"
-#define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
-#define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
-
 static struct cdevsw fbt_cdevsw = {
 	.d_version	= D_VERSION,
 	.d_open		= fbt_open,
@@ -124,28 +107,8 @@ static dtrace_pops_t fbt_pops = {
 	fbt_destroy
 };
 
-typedef struct fbt_probe {
-	struct fbt_probe *fbtp_hashnext;
-	uint8_t		*fbtp_patchpoint;
-	int8_t		fbtp_rval;
-	uint8_t		fbtp_patchval;
-	uint8_t		fbtp_savedval;
-	uintptr_t	fbtp_roffset;
-	dtrace_id_t	fbtp_id;
-	const char	*fbtp_name;
-	modctl_t	*fbtp_ctl;
-	int		fbtp_loadcnt;
-	int		fbtp_primary;
-	int		fbtp_invop_cnt;
-	int		fbtp_symindx;
-	struct fbt_probe *fbtp_next;
-} fbt_probe_t;
-
 static struct cdev		*fbt_cdev;
-static dtrace_provider_id_t	fbt_id;
-static fbt_probe_t		**fbt_probetab;
 static int			fbt_probetab_size;
-static int			fbt_probetab_mask;
 static int			fbt_verbose = 0;
 
 static void
@@ -162,257 +125,6 @@ fbt_doubletrap(void)
 	}
 }
 
-static int
-fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
-{
-	solaris_cpu_t *cpu = &solaris_cpu[curcpu];
-	uintptr_t stack0, stack1, stack2, stack3, stack4;
-	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
-
-	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
-		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
-			fbt->fbtp_invop_cnt++;
-			if (fbt->fbtp_roffset == 0) {
-				int i = 0;
-				/*
-				 * When accessing the arguments on the stack,
-				 * we must protect against accessing beyond
-				 * the stack.  We can safely set NOFAULT here
-				 * -- we know that interrupts are already
-				 * disabled.
-				 */
-				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
-				cpu->cpu_dtrace_caller = stack[i++];
-				stack0 = stack[i++];
-				stack1 = stack[i++];
-				stack2 = stack[i++];
-				stack3 = stack[i++];
-				stack4 = stack[i++];
-				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
-				    CPU_DTRACE_BADADDR);
-
-				dtrace_probe(fbt->fbtp_id, stack0, stack1,
-				    stack2, stack3, stack4);
-
-				cpu->cpu_dtrace_caller = 0;
-			} else {
-#ifdef __amd64__
-				/*
-				 * On amd64, we instrument the ret, not the
-				 * leave.  We therefore need to set the caller
-				 * to assure that the top frame of a stack()
-				 * action is correct.
-				 */
-				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
-				cpu->cpu_dtrace_caller = stack[0];
-				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
-				    CPU_DTRACE_BADADDR);
-#endif
-
-				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
-				    rval, 0, 0, 0);
-				cpu->cpu_dtrace_caller = 0;
-			}
-
-			return (fbt->fbtp_rval);
-		}
-	}
-
-	return (0);
-}
-
-static int
-fbt_provide_module_function(linker_file_t lf, int symindx,
-    linker_symval_t *symval, void *opaque)
-{
-	char *modname = opaque;
-	const char *name = symval->name;
-	fbt_probe_t *fbt, *retfbt;
-	int j;
-	int size;
-	u_int8_t *instr, *limit;
-
-	if ((strncmp(name, "dtrace_", 7) == 0 &&
-	    strncmp(name, "dtrace_safe_", 12) != 0) ||
-	    strcmp(name, "trap_check") == 0) {
-		/*
-		 * Anything beginning with "dtrace_" may be called
-		 * from probe context unless it explicitly indicates
-		 * that it won't be called from probe context by
-		 * using the prefix "dtrace_safe_".
-		 *
-		 * Additionally, we avoid instrumenting trap_check() to avoid
-		 * the possibility of generating a fault in probe context before
-		 * DTrace's fault handler is called.
-		 */
-		return (0);
-	}
-
-	if (name[0] == '_' && name[1] == '_')
-		return (0);
-
-	size = symval->size;
-
-	instr = (u_int8_t *) symval->value;
-	limit = (u_int8_t *) symval->value + symval->size;
-
-#ifdef __amd64__
-	while (instr < limit) {
-		if (*instr == FBT_PUSHL_EBP)
-			break;
-
-		if ((size = dtrace_instr_size(instr)) <= 0)
-			break;
-
-		instr += size;
-	}
-
-	if (instr >= limit || *instr != FBT_PUSHL_EBP) {
-		/*
-		 * We either don't save the frame pointer in this
-		 * function, or we ran into some disassembly
-		 * screw-up.  Either way, we bail.
-		 */
-		return (0);
-	}
-#else
-	if (instr[0] != FBT_PUSHL_EBP)
-		return (0);
-
-	if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
-	    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
-	    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
-	    instr[2] == FBT_MOVL_ESP_EBP1_V1))
-		return (0);
-#endif
-
-	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
-	fbt->fbtp_name = name;
-	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
-	    name, FBT_ENTRY, 3, fbt);
-	fbt->fbtp_patchpoint = instr;
-	fbt->fbtp_ctl = lf;
-	fbt->fbtp_loadcnt = lf->loadcnt;
-	fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
-	fbt->fbtp_savedval = *instr;
-	fbt->fbtp_patchval = FBT_PATCHVAL;
-	fbt->fbtp_symindx = symindx;
-
-	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
-	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
-
-	lf->fbt_nentries++;
-
-	retfbt = NULL;
-again:
-	if (instr >= limit)
-		return (0);
-
-	/*
-	 * If this disassembly fails, then we've likely walked off into
-	 * a jump table or some other unsuitable area.  Bail out of the
-	 * disassembly now.
-	 */
-	if ((size = dtrace_instr_size(instr)) <= 0)
-		return (0);
-
-#ifdef __amd64__
-	/*
-	 * We only instrument "ret" on amd64 -- we don't yet instrument
-	 * ret imm16, largely because the compiler doesn't seem to
-	 * (yet) emit them in the kernel...
-	 */
-	if (*instr != FBT_RET) {
-		instr += size;
-		goto again;
-	}
-#else
-	if (!(size == 1 &&
-	    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
-	    (*(instr + 1) == FBT_RET ||
-	    *(instr + 1) == FBT_RET_IMM16))) {
-		instr += size;
-		goto again;
-	}
-#endif
-
-	/*
-	 * We (desperately) want to avoid erroneously instrumenting a
-	 * jump table, especially given that our markers are pretty
-	 * short:  two bytes on x86, and just one byte on amd64.  To
-	 * determine if we're looking at a true instruction sequence
-	 * or an inline jump table that happens to contain the same
-	 * byte sequences, we resort to some heuristic sleeze:  we
-	 * treat this instruction as being contained within a pointer,
-	 * and see if that pointer points to within the body of the
-	 * function.  If it does, we refuse to instrument it.
-	 */
-	for (j = 0; j < sizeof (uintptr_t); j++) {
-		caddr_t check = (caddr_t) instr - j;
-		uint8_t *ptr;
-
-		if (check < symval->value)
-			break;
-
-		if (check + sizeof (caddr_t) > (caddr_t)limit)
-			continue;
-
-		ptr = *(uint8_t **)check;
-
-		if (ptr >= (uint8_t *) symval->value && ptr < limit) {
-			instr += size;
-			goto again;
-		}
-	}
-
-	/*
-	 * We have a winner!
-	 */
-	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
-	fbt->fbtp_name = name;
-
-	if (retfbt == NULL) {
-		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
-		    name, FBT_RETURN, 3, fbt);
-	} else {
-		retfbt->fbtp_next = fbt;
-		fbt->fbtp_id = retfbt->fbtp_id;
-	}
-
-	retfbt = fbt;
-	fbt->fbtp_patchpoint = instr;
-	fbt->fbtp_ctl = lf;
-	fbt->fbtp_loadcnt = lf->loadcnt;
-	fbt->fbtp_symindx = symindx;
-
-#ifndef __amd64__
-	if (*instr == FBT_POPL_EBP) {
-		fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
-	} else {
-		ASSERT(*instr == FBT_LEAVE);
-		fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
-	}
-	fbt->fbtp_roffset =
-	    (uintptr_t)(instr - (uint8_t *) symval->value) + 1;
-
-#else
-	ASSERT(*instr == FBT_RET);
-	fbt->fbtp_rval = DTRACE_INVOP_RET;
-	fbt->fbtp_roffset =
-	    (uintptr_t)(instr - (uint8_t *) symval->value);
-#endif
-
-	fbt->fbtp_savedval = *instr;
-	fbt->fbtp_patchval = FBT_PATCHVAL;
-	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
-	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
-
-	lf->fbt_nentries++;
-
-	instr += size;
-	goto again;
-}
-
 static void
 fbt_provide_module(void *arg, modctl_t *lf)
 {
@@ -524,9 +236,8 @@ fbt_enable(void *arg, dtrace_id_t id, vo
 		return;
 	}
 
-	for (; fbt != NULL; fbt = fbt->fbtp_next) {
-		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
-	}
+	for (; fbt != NULL; fbt = fbt->fbtp_next)
+		fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
 }
 
 static void
@@ -542,7 +253,7 @@ fbt_disable(void *arg, dtrace_id_t id, v
 		return;
 
 	for (; fbt != NULL; fbt = fbt->fbtp_next)
-		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
+		fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
 }
 
 static void
@@ -557,7 +268,7 @@ fbt_suspend(void *arg, dtrace_id_t id, v
 		return;
 
 	for (; fbt != NULL; fbt = fbt->fbtp_next)
-		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
+		fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
 }
 
 static void
@@ -572,7 +283,7 @@ fbt_resume(void *arg, dtrace_id_t id, vo
 		return;
 
 	for (; fbt != NULL; fbt = fbt->fbtp_next)
-		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
+		fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
 }
 
 static int

Added: head/sys/cddl/dev/fbt/fbt.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/cddl/dev/fbt/fbt.h	Sat Aug 16 21:42:55 2014	(r270067)
@@ -0,0 +1,73 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
+ *
+ * $FreeBSD$
+ *
+ */
+
+/*
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef _FBT_H_
+#define _FBT_H_
+
+#include "fbt_isa.h"
+
+typedef struct fbt_probe {
+	struct fbt_probe *fbtp_hashnext;
+	fbt_patchval_t	*fbtp_patchpoint;
+	int8_t		fbtp_rval;
+	fbt_patchval_t	fbtp_patchval;
+	fbt_patchval_t	fbtp_savedval;
+	uintptr_t	fbtp_roffset;
+	dtrace_id_t	fbtp_id;
+	const char	*fbtp_name;
+	modctl_t	*fbtp_ctl;
+	int		fbtp_loadcnt;
+	int		fbtp_primary;
+	int		fbtp_invop_cnt;
+	int		fbtp_symindx;
+	struct fbt_probe *fbtp_next;
+} fbt_probe_t;
+
+struct linker_file;
+struct linker_symval;
+
+int	fbt_invop(uintptr_t, uintptr_t *, uintptr_t);
+void	fbt_patch_tracepoint(fbt_probe_t *, fbt_patchval_t);
+int	fbt_provide_module_function(struct linker_file *, int,
+	    struct linker_symval *, void *);
+
+extern dtrace_provider_id_t	fbt_id;
+extern fbt_probe_t		**fbt_probetab;
+extern int			fbt_probetab_mask;
+
+#define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
+#define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
+
+#ifdef MALLOC_DECLARE
+MALLOC_DECLARE(M_FBT);
+#endif
+
+#endif

Added: head/sys/cddl/dev/fbt/powerpc/fbt_isa.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/cddl/dev/fbt/powerpc/fbt_isa.c	Sat Aug 16 21:42:55 2014	(r270067)
@@ -0,0 +1,241 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
+ * Portions Copyright 2013 Justin Hibbits jhibbits@freebsd.org
+ *
+ * $FreeBSD$
+ *
+ */
+
+/*
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/dtrace.h>
+#include <machine/md_var.h>
+
+#include "fbt.h"
+
+#define FBT_PATCHVAL		0x7c810808
+#define FBT_MFLR_R0		0x7c0802a6
+#define FBT_MTLR_R0		0x7c0803a6
+#define FBT_BLR			0x4e800020
+#define FBT_BCTR		0x4e800030
+#define FBT_BRANCH		0x48000000
+#define FBT_BR_MASK		0x03fffffc
+#define FBT_IS_JUMP(instr)	((instr & ~FBT_BR_MASK) == FBT_BRANCH)
+
+#define	FBT_ENTRY	"entry"
+#define	FBT_RETURN	"return"
+
+int
+fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
+{
+	struct trapframe *frame = (struct trapframe *)stack;
+	solaris_cpu_t *cpu = &solaris_cpu[curcpu];
+	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
+	uintptr_t tmp;
+
+	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
+		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
+			fbt->fbtp_invop_cnt++;
+			if (fbt->fbtp_roffset == 0) {
+				cpu->cpu_dtrace_caller = addr;
+
+				dtrace_probe(fbt->fbtp_id, frame->fixreg[3],
+				    frame->fixreg[4], frame->fixreg[5],
+				    frame->fixreg[6], frame->fixreg[7]);
+
+				cpu->cpu_dtrace_caller = 0;
+			} else {
+
+				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
+				    rval, 0, 0, 0);
+				/*
+				 * The caller doesn't have the fbt item, so
+				 * fixup tail calls here.
+				 */
+				if (fbt->fbtp_rval == DTRACE_INVOP_JUMP) {
+					frame->srr0 = (uintptr_t)fbt->fbtp_patchpoint;
+					tmp = fbt->fbtp_savedval & FBT_BR_MASK;
+					/* Sign extend. */
+					if (tmp & 0x02000000)
+#ifdef __powerpc64__
+						tmp |= 0xfffffffffc000000ULL;
+#else
+						tmp |= 0xfc000000UL;
+#endif
+					frame->srr0 += tmp;
+				}
+				cpu->cpu_dtrace_caller = 0;
+			}
+
+			return (fbt->fbtp_rval);
+		}
+	}
+
+	return (0);
+}
+
+void
+fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
+{
+
+	*fbt->fbtp_patchpoint = val;
+	__syncicache(fbt->fbtp_patchpoint, 4);
+}
+
+int
+fbt_provide_module_function(linker_file_t lf, int symindx,
+    linker_symval_t *symval, void *opaque)
+{
+	char *modname = opaque;
+	const char *name = symval->name;
+	fbt_probe_t *fbt, *retfbt;
+	int j;
+	uint32_t *instr, *limit;
+
+	/* PowerPC64 uses '.' prefixes on symbol names, ignore it. */
+	if (name[0] == '.')
+		name++;
+
+	if (strncmp(name, "dtrace_", 7) == 0 &&
+	    strncmp(name, "dtrace_safe_", 12) != 0) {
+		/*
+		 * Anything beginning with "dtrace_" may be called
+		 * from probe context unless it explicitly indicates
+		 * that it won't be called from probe context by
+		 * using the prefix "dtrace_safe_".
+		 */
+		return (0);
+	}
+
+	if (name[0] == '_' && name[1] == '_')
+		return (0);
+
+	instr = (uint32_t *) symval->value;
+	limit = (uint32_t *) (symval->value + symval->size);
+
+	for (; instr < limit; instr++)
+		if (*instr == FBT_MFLR_R0)
+			break;
+
+	if (*instr != FBT_MFLR_R0)
+		return (0);
+
+	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
+	fbt->fbtp_name = name;
+	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
+	    name, FBT_ENTRY, 3, fbt);
+	fbt->fbtp_patchpoint = instr;
+	fbt->fbtp_ctl = lf;
+	fbt->fbtp_loadcnt = lf->loadcnt;
+	fbt->fbtp_savedval = *instr;
+	fbt->fbtp_patchval = FBT_PATCHVAL;
+	fbt->fbtp_rval = DTRACE_INVOP_MFLR_R0;
+	fbt->fbtp_symindx = symindx;
+
+	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
+	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
+
+	lf->fbt_nentries++;
+
+	retfbt = NULL;
+again:
+	if (instr >= limit)
+		return (0);
+
+	/*
+	 * We (desperately) want to avoid erroneously instrumenting a
+	 * jump table. To determine if we're looking at a true instruction
+	 * sequence or an inline jump table that happens to contain the same
+	 * byte sequences, we resort to some heuristic sleeze:  we treat this
+	 * instruction as being contained within a pointer, and see if that
+	 * pointer points to within the body of the function.  If it does, we
+	 * refuse to instrument it.
+	 */
+	{
+		uint32_t *ptr;
+
+		ptr = *(uint32_t **)instr;
+
+		if (ptr >= (uint32_t *) symval->value && ptr < limit) {
+			instr++;
+			goto again;
+		}
+	}
+
+	if (*instr != FBT_MTLR_R0) {
+		instr++;
+		goto again;
+	}
+
+	instr++;
+
+	for (j = 0; j < 12 && instr < limit; j++, instr++) {
+		if ((*instr == FBT_BCTR) || (*instr == FBT_BLR) ||
+		    FBT_IS_JUMP(*instr))
+			break;
+	}
+
+	if (!(*instr == FBT_BCTR || *instr == FBT_BLR || FBT_IS_JUMP(*instr)))
+		goto again;
+
+	/*
+	 * We have a winner!
+	 */
+	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
+	fbt->fbtp_name = name;
+
+	if (retfbt == NULL) {
+		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
+		    name, FBT_RETURN, 5, fbt);
+	} else {
+		retfbt->fbtp_next = fbt;
+		fbt->fbtp_id = retfbt->fbtp_id;
+	}
+
+	retfbt = fbt;
+	fbt->fbtp_patchpoint = instr;
+	fbt->fbtp_ctl = lf;
+	fbt->fbtp_loadcnt = lf->loadcnt;
+	fbt->fbtp_symindx = symindx;
+
+	if (*instr == FBT_BCTR)
+		fbt->fbtp_rval = DTRACE_INVOP_BCTR;
+	else if (*instr == FBT_BLR)
+		fbt->fbtp_rval = DTRACE_INVOP_RET;
+	else
+		fbt->fbtp_rval = DTRACE_INVOP_JUMP;
+
+	fbt->fbtp_savedval = *instr;
+	fbt->fbtp_patchval = FBT_PATCHVAL;
+	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
+	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
+
+	lf->fbt_nentries++;
+
+	instr += 4;
+	goto again;
+}

Added: head/sys/cddl/dev/fbt/powerpc/fbt_isa.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/cddl/dev/fbt/powerpc/fbt_isa.h	Sat Aug 16 21:42:55 2014	(r270067)
@@ -0,0 +1,30 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ * $FreeBSD$
+ *
+ */
+
+#ifndef _FBT_ISA_H_
+#define _FBT_ISA_H_
+
+typedef uint32_t fbt_patchval_t;
+
+#endif

Added: head/sys/cddl/dev/fbt/x86/fbt_isa.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/cddl/dev/fbt/x86/fbt_isa.c	Sat Aug 16 21:42:55 2014	(r270067)
@@ -0,0 +1,316 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
+ *
+ * $FreeBSD$
+ *
+ */
+
+/*
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+
+#include <sys/dtrace.h>
+
+#include "fbt.h"
+
+#define	FBT_PUSHL_EBP		0x55
+#define	FBT_MOVL_ESP_EBP0_V0	0x8b
+#define	FBT_MOVL_ESP_EBP1_V0	0xec
+#define	FBT_MOVL_ESP_EBP0_V1	0x89
+#define	FBT_MOVL_ESP_EBP1_V1	0xe5
+#define	FBT_REX_RSP_RBP		0x48
+
+#define	FBT_POPL_EBP		0x5d
+#define	FBT_RET			0xc3
+#define	FBT_RET_IMM16		0xc2
+#define	FBT_LEAVE		0xc9
+
+#ifdef __amd64__
+#define	FBT_PATCHVAL		0xcc
+#else
+#define	FBT_PATCHVAL		0xf0
+#endif
+
+#define	FBT_ENTRY	"entry"
+#define	FBT_RETURN	"return"
+
+int
+fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
+{
+	solaris_cpu_t *cpu = &solaris_cpu[curcpu];
+	uintptr_t stack0, stack1, stack2, stack3, stack4;
+	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
+
+	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
+		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
+			fbt->fbtp_invop_cnt++;
+			if (fbt->fbtp_roffset == 0) {
+				int i = 0;
+				/*
+				 * When accessing the arguments on the stack,
+				 * we must protect against accessing beyond
+				 * the stack.  We can safely set NOFAULT here
+				 * -- we know that interrupts are already
+				 * disabled.
+				 */
+				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
+				cpu->cpu_dtrace_caller = stack[i++];
+				stack0 = stack[i++];
+				stack1 = stack[i++];
+				stack2 = stack[i++];
+				stack3 = stack[i++];
+				stack4 = stack[i++];
+				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
+				    CPU_DTRACE_BADADDR);
+
+				dtrace_probe(fbt->fbtp_id, stack0, stack1,
+				    stack2, stack3, stack4);
+
+				cpu->cpu_dtrace_caller = 0;
+			} else {
+#ifdef __amd64__
+				/*
+				 * On amd64, we instrument the ret, not the
+				 * leave.  We therefore need to set the caller
+				 * to assure that the top frame of a stack()
+				 * action is correct.
+				 */
+				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
+				cpu->cpu_dtrace_caller = stack[0];
+				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
+				    CPU_DTRACE_BADADDR);
+#endif
+
+				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
+				    rval, 0, 0, 0);
+				cpu->cpu_dtrace_caller = 0;
+			}
+
+			return (fbt->fbtp_rval);
+		}
+	}
+
+	return (0);
+}
+
+void
+fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
+{
+
+	*fbt->fbtp_patchpoint = val;
+}
+
+int
+fbt_provide_module_function(linker_file_t lf, int symindx,
+    linker_symval_t *symval, void *opaque)
+{
+	char *modname = opaque;
+	const char *name = symval->name;
+	fbt_probe_t *fbt, *retfbt;
+	int j;
+	int size;
+	uint8_t *instr, *limit;
+
+	if ((strncmp(name, "dtrace_", 7) == 0 &&
+	    strncmp(name, "dtrace_safe_", 12) != 0) ||
+	    strcmp(name, "trap_check") == 0) {
+		/*
+		 * Anything beginning with "dtrace_" may be called
+		 * from probe context unless it explicitly indicates
+		 * that it won't be called from probe context by
+		 * using the prefix "dtrace_safe_".
+		 *
+		 * Additionally, we avoid instrumenting trap_check() to avoid
+		 * the possibility of generating a fault in probe context before
+		 * DTrace's fault handler is called.
+		 */
+		return (0);
+	}
+
+	if (name[0] == '_' && name[1] == '_')
+		return (0);
+
+	size = symval->size;
+
+	instr = (uint8_t *) symval->value;
+	limit = (uint8_t *) symval->value + symval->size;
+
+#ifdef __amd64__
+	while (instr < limit) {
+		if (*instr == FBT_PUSHL_EBP)
+			break;
+
+		if ((size = dtrace_instr_size(instr)) <= 0)
+			break;
+
+		instr += size;
+	}
+
+	if (instr >= limit || *instr != FBT_PUSHL_EBP) {
+		/*
+		 * We either don't save the frame pointer in this
+		 * function, or we ran into some disassembly
+		 * screw-up.  Either way, we bail.
+		 */
+		return (0);
+	}
+#else
+	if (instr[0] != FBT_PUSHL_EBP)
+		return (0);
+
+	if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
+	    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
+	    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
+	    instr[2] == FBT_MOVL_ESP_EBP1_V1))
+		return (0);
+#endif
+
+	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
+	fbt->fbtp_name = name;
+	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
+	    name, FBT_ENTRY, 3, fbt);
+	fbt->fbtp_patchpoint = instr;
+	fbt->fbtp_ctl = lf;
+	fbt->fbtp_loadcnt = lf->loadcnt;
+	fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
+	fbt->fbtp_savedval = *instr;
+	fbt->fbtp_patchval = FBT_PATCHVAL;
+	fbt->fbtp_symindx = symindx;
+
+	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
+	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
+
+	lf->fbt_nentries++;
+
+	retfbt = NULL;
+again:
+	if (instr >= limit)
+		return (0);
+
+	/*
+	 * If this disassembly fails, then we've likely walked off into
+	 * a jump table or some other unsuitable area.  Bail out of the
+	 * disassembly now.
+	 */
+	if ((size = dtrace_instr_size(instr)) <= 0)
+		return (0);
+
+#ifdef __amd64__
+	/*
+	 * We only instrument "ret" on amd64 -- we don't yet instrument
+	 * ret imm16, largely because the compiler doesn't seem to
+	 * (yet) emit them in the kernel...
+	 */
+	if (*instr != FBT_RET) {
+		instr += size;
+		goto again;
+	}
+#else
+	if (!(size == 1 &&
+	    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
+	    (*(instr + 1) == FBT_RET ||
+	    *(instr + 1) == FBT_RET_IMM16))) {
+		instr += size;
+		goto again;
+	}
+#endif
+
+	/*
+	 * We (desperately) want to avoid erroneously instrumenting a
+	 * jump table, especially given that our markers are pretty
+	 * short:  two bytes on x86, and just one byte on amd64.  To
+	 * determine if we're looking at a true instruction sequence
+	 * or an inline jump table that happens to contain the same
+	 * byte sequences, we resort to some heuristic sleeze:  we
+	 * treat this instruction as being contained within a pointer,
+	 * and see if that pointer points to within the body of the
+	 * function.  If it does, we refuse to instrument it.
+	 */
+	for (j = 0; j < sizeof (uintptr_t); j++) {
+		caddr_t check = (caddr_t) instr - j;
+		uint8_t *ptr;
+
+		if (check < symval->value)
+			break;
+
+		if (check + sizeof (caddr_t) > (caddr_t)limit)
+			continue;
+
+		ptr = *(uint8_t **)check;
+
+		if (ptr >= (uint8_t *) symval->value && ptr < limit) {
+			instr += size;
+			goto again;
+		}
+	}
+
+	/*
+	 * We have a winner!
+	 */

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201408162142.s7GLgtPF081431>