Home | History | Annotate | Line # | Download | only in mips
      1  1.1    simonb /*
      2  1.1    simonb  * CDDL HEADER START
      3  1.1    simonb  *
      4  1.1    simonb  * The contents of this file are subject to the terms of the
      5  1.1    simonb  * Common Development and Distribution License, Version 1.0 only
      6  1.1    simonb  * (the "License").  You may not use this file except in compliance
      7  1.1    simonb  * with the License.
      8  1.1    simonb  *
      9  1.1    simonb  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     10  1.1    simonb  * or http://www.opensolaris.org/os/licensing.
     11  1.1    simonb  * See the License for the specific language governing permissions
     12  1.1    simonb  * and limitations under the License.
     13  1.1    simonb  *
     14  1.1    simonb  * When distributing Covered Code, include this CDDL HEADER in each
     15  1.1    simonb  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     16  1.1    simonb  * If applicable, add the following below this CDDL HEADER, with the
     17  1.1    simonb  * fields enclosed by brackets "[]" replaced with your own identifying
     18  1.1    simonb  * information: Portions Copyright [yyyy] [name of copyright owner]
     19  1.1    simonb  *
     20  1.1    simonb  * CDDL HEADER END
     21  1.1    simonb  *
     22  1.1    simonb  * $FreeBSD$
     23  1.1    simonb  *
     24  1.1    simonb  */
     25  1.1    simonb /*
     26  1.1    simonb  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
     27  1.1    simonb  * Use is subject to license terms.
     28  1.1    simonb  */
     29  1.1    simonb 
     30  1.1    simonb #include <sys/cdefs.h>
     31  1.1    simonb __FBSDID("$FreeBSD$");
     32  1.1    simonb 
     33  1.1    simonb #include <sys/param.h>
     34  1.1    simonb #include <sys/systm.h>
     35  1.1    simonb #include <sys/types.h>
     36  1.1    simonb #include <sys/kernel.h>
     37  1.1    simonb #include <sys/malloc.h>
     38  1.1    simonb #include <sys/kmem.h>
     39  1.1    simonb #include <sys/xcall.h>
     40  1.1    simonb #include <sys/cpu.h>
     41  1.1    simonb #include <sys/dtrace_impl.h>
     42  1.1    simonb #include <sys/dtrace_bsd.h>
     43  1.1    simonb #include <machine/regnum.h>
     44  1.1    simonb #include <machine/locore.h>
     45  1.1    simonb #include <machine/trap.h>
     46  1.1    simonb 
     47  1.1    simonb #define	DELAYBRANCH(x)	((int)(x) < 0)
     48  1.1    simonb 
     49  1.1    simonb #ifdef __FreeBSD__
     50  1.1    simonb #define	CURRENT_CPU		curcpu
     51  1.1    simonb #endif
     52  1.1    simonb #ifdef __NetBSD__
     53  1.1    simonb #define	CURRENT_CPU		cpu_index(curcpu())
     54  1.1    simonb #endif
     55  1.1    simonb 
     56  1.1    simonb extern dtrace_id_t	dtrace_probeid_error;
     57  1.1    simonb extern int (*dtrace_invop_jump_addr)(struct trapframe *);
     58  1.1    simonb extern void dtrace_getnanotime(struct timespec *tsp);
     59  1.1    simonb 
     60  1.1    simonb int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
     61  1.1    simonb void dtrace_invop_init(void);
     62  1.1    simonb void dtrace_invop_uninit(void);
     63  1.1    simonb 
     64  1.1    simonb void dtrace_gethrtime_init(void);
     65  1.1    simonb 
     66  1.1    simonb typedef struct dtrace_invop_hdlr {
     67  1.1    simonb 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
     68  1.1    simonb 	struct dtrace_invop_hdlr *dtih_next;
     69  1.1    simonb } dtrace_invop_hdlr_t;
     70  1.1    simonb 
     71  1.1    simonb dtrace_invop_hdlr_t *dtrace_invop_hdlr;
     72  1.1    simonb 
     73  1.1    simonb int
     74  1.1    simonb dtrace_invop(uintptr_t addr, struct trapframe *stack, uintptr_t eax)
     75  1.1    simonb {
     76  1.1    simonb 	dtrace_invop_hdlr_t *hdlr;
     77  1.1    simonb 	int rval;
     78  1.1    simonb 
     79  1.1    simonb 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
     80  1.1    simonb 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
     81  1.1    simonb 			return (rval);
     82  1.1    simonb 
     83  1.1    simonb 	return (0);
     84  1.1    simonb }
     85  1.1    simonb 
     86  1.1    simonb void
     87  1.1    simonb dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
     88  1.1    simonb {
     89  1.1    simonb 	dtrace_invop_hdlr_t *hdlr;
     90  1.1    simonb 
     91  1.3  christos 	hdlr = kmem_alloc(sizeof(*hdlr), KM_SLEEP);
     92  1.1    simonb 	hdlr->dtih_func = func;
     93  1.1    simonb 	hdlr->dtih_next = dtrace_invop_hdlr;
     94  1.1    simonb 	dtrace_invop_hdlr = hdlr;
     95  1.1    simonb }
     96  1.1    simonb 
     97  1.1    simonb void
     98  1.1    simonb dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
     99  1.1    simonb {
    100  1.1    simonb 	dtrace_invop_hdlr_t *hdlr, *prev;
    101  1.1    simonb 
    102  1.1    simonb 	hdlr = dtrace_invop_hdlr;
    103  1.1    simonb 	prev = NULL;
    104  1.1    simonb 
    105  1.1    simonb 	for (;;) {
    106  1.1    simonb 		if (hdlr == NULL)
    107  1.1    simonb 			panic("attempt to remove non-existent invop handler");
    108  1.1    simonb 
    109  1.1    simonb 		if (hdlr->dtih_func == func)
    110  1.1    simonb 			break;
    111  1.1    simonb 
    112  1.1    simonb 		prev = hdlr;
    113  1.1    simonb 		hdlr = hdlr->dtih_next;
    114  1.1    simonb 	}
    115  1.1    simonb 
    116  1.1    simonb 	if (prev == NULL) {
    117  1.1    simonb 		ASSERT(dtrace_invop_hdlr == hdlr);
    118  1.1    simonb 		dtrace_invop_hdlr = hdlr->dtih_next;
    119  1.1    simonb 	} else {
    120  1.1    simonb 		ASSERT(dtrace_invop_hdlr != hdlr);
    121  1.1    simonb 		prev->dtih_next = hdlr->dtih_next;
    122  1.1    simonb 	}
    123  1.1    simonb 
    124  1.3  christos 	kmem_free(hdlr, sizeof(*hdlr));
    125  1.1    simonb }
    126  1.1    simonb 
    127  1.1    simonb /*ARGSUSED*/
    128  1.1    simonb void
    129  1.1    simonb dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
    130  1.1    simonb {
    131  1.1    simonb 	/* XXXXXXsimonb what is a "toxic range"? */
    132  1.1    simonb }
    133  1.1    simonb 
    134  1.1    simonb static void
    135  1.1    simonb xcall_func(void *arg0, void *arg1)
    136  1.1    simonb {
    137  1.1    simonb 	dtrace_xcall_t func = arg0;
    138  1.1    simonb 
    139  1.1    simonb 	(*func)(arg1);
    140  1.1    simonb }
    141  1.1    simonb 
    142  1.1    simonb void
    143  1.1    simonb dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
    144  1.1    simonb {
    145  1.1    simonb 	uint64_t where;
    146  1.1    simonb 
    147  1.1    simonb 	if (cpu == DTRACE_CPUALL) {
    148  1.1    simonb 		where = xc_broadcast(0, xcall_func, func, arg);
    149  1.1    simonb 	} else {
    150  1.1    simonb 		struct cpu_info *ci = cpu_lookup(cpu);
    151  1.1    simonb 
    152  1.1    simonb 		KASSERT(ci != NULL);
    153  1.1    simonb 		where = xc_unicast(0, xcall_func, func, arg, ci);
    154  1.1    simonb 	}
    155  1.1    simonb 	xc_wait(where);
    156  1.1    simonb }
    157  1.1    simonb 
    158  1.1    simonb static void
    159  1.1    simonb dtrace_sync_func(void)
    160  1.1    simonb {
    161  1.1    simonb 
    162  1.1    simonb }
    163  1.1    simonb 
    164  1.1    simonb void
    165  1.1    simonb dtrace_sync(void)
    166  1.1    simonb {
    167  1.1    simonb 
    168  1.1    simonb         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
    169  1.1    simonb }
    170  1.1    simonb 
    171  1.1    simonb /*
    172  1.1    simonb  * DTrace needs a high resolution time function which can
    173  1.1    simonb  * be called from a probe context and guaranteed not to have
    174  1.1    simonb  * instrumented with probes itself.
    175  1.1    simonb  *
    176  1.1    simonb  * Returns nanoseconds since boot.
    177  1.1    simonb  */
    178  1.1    simonb uint64_t
    179  1.1    simonb dtrace_gethrtime()
    180  1.1    simonb {
    181  1.1    simonb 	struct timespec curtime;
    182  1.1    simonb 
    183  1.1    simonb 	nanouptime(&curtime);
    184  1.1    simonb 
    185  1.1    simonb 	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
    186  1.1    simonb 
    187  1.1    simonb }
    188  1.1    simonb 
    189  1.1    simonb void
    190  1.1    simonb dtrace_gethrtime_init(void)
    191  1.1    simonb {
    192  1.1    simonb }
    193  1.1    simonb 
    194  1.1    simonb uint64_t
    195  1.1    simonb dtrace_gethrestime(void)
    196  1.1    simonb {
    197  1.1    simonb 	struct timespec current_time;
    198  1.1    simonb 
    199  1.1    simonb 	dtrace_getnanotime(&current_time);
    200  1.1    simonb 
    201  1.1    simonb 	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
    202  1.1    simonb }
    203  1.1    simonb 
    204  1.1    simonb /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
    205  1.1    simonb int
    206  1.1    simonb dtrace_trap(struct trapframe *frame, u_int type)
    207  1.1    simonb {
    208  1.1    simonb 	/*
    209  1.1    simonb 	 * A trap can occur while DTrace executes a probe. Before
    210  1.1    simonb 	 * executing the probe, DTrace blocks re-scheduling and sets
    211  1.1    simonb 	 * a flag in its per-cpu flags to indicate that it doesn't
    212  1.1    simonb 	 * want to fault. On returning from the probe, the no-fault
    213  1.1    simonb 	 * flag is cleared and finally re-scheduling is enabled.
    214  1.1    simonb 	 *
    215  1.1    simonb 	 * Check if DTrace has enabled 'no-fault' mode:
    216  1.1    simonb 	 */
    217  1.1    simonb 
    218  1.1    simonb 	if ((cpu_core[CURRENT_CPU].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
    219  1.1    simonb 		/*
    220  1.1    simonb 		 * There are only a couple of trap types that are expected.
    221  1.1    simonb 		 * All the rest will be handled in the usual way.
    222  1.1    simonb 		 */
    223  1.1    simonb 		switch (type) {
    224  1.1    simonb 		/* Page fault. */
    225  1.1    simonb 		case T_TLB_ST_MISS:
    226  1.1    simonb 		case T_ADDR_ERR_ST:
    227  1.1    simonb 		case T_TLB_LD_MISS:
    228  1.1    simonb 		case T_ADDR_ERR_LD:
    229  1.1    simonb 		case T_BUS_ERR_IFETCH:
    230  1.1    simonb 			/* Flag a bad address. */
    231  1.1    simonb 			cpu_core[CURRENT_CPU].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
    232  1.1    simonb 			cpu_core[CURRENT_CPU].cpuc_dtrace_illval = frame->tf_regs[_R_BADVADDR];
    233  1.1    simonb 
    234  1.1    simonb 			/*
    235  1.1    simonb 			 * Offset the instruction pointer to the instruction
    236  1.1    simonb 			 * following the one causing the fault.
    237  1.1    simonb 			 */
    238  1.1    simonb 			if (DELAYBRANCH(frame->tf_regs[_R_CAUSE]))	 /* Check BD bit */
    239  1.1    simonb 			{
    240  1.1    simonb 				/* XXX: check MipsEmulateBranch on MIPS64
    241  1.1    simonb 				frame->tf_regs[_R_PC] = MipsEmulateBranch(frame,
    242  1.1    simonb 				    frame->tf_regs[_R_PC], 0, 0);
    243  1.1    simonb  				*/
    244  1.1    simonb 				panic("%s: delay slot at %jx, badvaddr = %jx\n",
    245  1.1    simonb 				    __func__, (intmax_t)frame->tf_regs[_R_PC],
    246  1.1    simonb 				    (intmax_t)frame->tf_regs[_R_BADVADDR]);
    247  1.1    simonb 			}
    248  1.1    simonb 			else
    249  1.1    simonb 				frame->tf_regs[_R_PC] += sizeof(int);
    250  1.1    simonb 			return (1);
    251  1.1    simonb 		default:
    252  1.1    simonb 			/* Handle all other traps in the usual way. */
    253  1.1    simonb 			break;
    254  1.1    simonb 		}
    255  1.1    simonb 	}
    256  1.1    simonb 
    257  1.1    simonb 	/* Handle the trap in the usual way. */
    258  1.1    simonb 	return (0);
    259  1.1    simonb }
    260  1.1    simonb 
    261  1.1    simonb void
    262  1.1    simonb dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
    263  1.1    simonb     int fault, int fltoffs, uintptr_t illval)
    264  1.1    simonb {
    265  1.1    simonb 
    266  1.1    simonb 	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
    267  1.1    simonb 	    (uintptr_t)epid,
    268  1.1    simonb 	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
    269  1.1    simonb }
    270  1.1    simonb 
    271  1.1    simonb static int
    272  1.1    simonb dtrace_invop_start(struct trapframe *frame)
    273  1.1    simonb {
    274  1.1    simonb 	register_t *sp;
    275  1.1    simonb 	int16_t offs;
    276  1.1    simonb 	int invop;
    277  1.1    simonb 
    278  1.1    simonb 	invop = dtrace_invop(frame->tf_regs[_R_PC], frame, frame->tf_regs[_R_PC]);
    279  1.1    simonb 	if (invop == 0)
    280  1.1    simonb 		return (-1);
    281  1.1    simonb 
    282  1.1    simonb 	offs = (invop & LDSD_DATA_MASK);
    283  1.1    simonb 	sp = (register_t *)(intptr_t)(frame->tf_regs[_R_SP] + offs);
    284  1.1    simonb 
    285  1.1    simonb 	switch (invop & LDSD_RA_SP_MASK) {
    286  1.1    simonb 	case LD_RA_SP:
    287  1.1    simonb 		frame->tf_regs[_R_RA] = *sp;
    288  1.1    simonb 		frame->tf_regs[_R_PC] += INSN_SIZE;
    289  1.1    simonb 		break;
    290  1.1    simonb 	case SD_RA_SP:
    291  1.1    simonb 		*(sp) = frame->tf_regs[_R_RA];
    292  1.1    simonb 		frame->tf_regs[_R_PC] += INSN_SIZE;
    293  1.1    simonb 		break;
    294  1.1    simonb 	default:
    295  1.1    simonb 		printf("%s: 0x%x undefined\n", __func__, invop);
    296  1.1    simonb 		return (-1);
    297  1.1    simonb 	};
    298  1.1    simonb 
    299  1.1    simonb 	return (0);
    300  1.1    simonb }
    301  1.1    simonb 
    302  1.1    simonb void
    303  1.1    simonb dtrace_invop_init(void)
    304  1.1    simonb {
    305  1.1    simonb 
    306  1.1    simonb 	dtrace_invop_jump_addr = dtrace_invop_start;
    307  1.1    simonb }
    308  1.1    simonb 
    309  1.1    simonb void
    310  1.1    simonb dtrace_invop_uninit(void)
    311  1.1    simonb {
    312  1.1    simonb 
    313  1.1    simonb 	dtrace_invop_jump_addr = 0;
    314  1.1    simonb }
    315