dtrace_subr.c revision 276142
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 *
22 * $FreeBSD: head/sys/cddl/dev/dtrace/mips/dtrace_subr.c 276142 2014-12-23 15:38:19Z markj $
23 *
24 */
25/*
26 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27 * Use is subject to license terms.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/cddl/dev/dtrace/mips/dtrace_subr.c 276142 2014-12-23 15:38:19Z markj $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/types.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/kmem.h>
39#include <sys/smp.h>
40#include <sys/dtrace_impl.h>
41#include <sys/dtrace_bsd.h>
42#include <machine/clock.h>
43#include <machine/frame.h>
44#include <machine/trap.h>
45#include <vm/pmap.h>
46
47#define	DELAYBRANCH(x)	((int)(x) < 0)
48
49extern uintptr_t 	dtrace_in_probe_addr;
50extern int		dtrace_in_probe;
51extern dtrace_id_t	dtrace_probeid_error;
52
53int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
54
55typedef struct dtrace_invop_hdlr {
56	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
57	struct dtrace_invop_hdlr *dtih_next;
58} dtrace_invop_hdlr_t;
59
60dtrace_invop_hdlr_t *dtrace_invop_hdlr;
61
62int
63dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
64{
65	dtrace_invop_hdlr_t *hdlr;
66	int rval;
67
68	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
69		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
70			return (rval);
71
72	return (0);
73}
74
75
76/*ARGSUSED*/
77void
78dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
79{
80	/*
81	 * No toxic regions?
82	 */
83}
84
85void
86dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
87{
88	cpuset_t cpus;
89
90	if (cpu == DTRACE_CPUALL)
91		cpus = all_cpus;
92	else
93		CPU_SETOF(cpu, &cpus);
94
95	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
96	    smp_no_rendevous_barrier, arg);
97}
98
99static void
100dtrace_sync_func(void)
101{
102}
103
104void
105dtrace_sync(void)
106{
107        dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
108}
109
110/*
111 * DTrace needs a high resolution time function which can
112 * be called from a probe context and guaranteed not to have
113 * instrumented with probes itself.
114 *
115 * Returns nanoseconds since boot.
116 */
117uint64_t
118dtrace_gethrtime()
119{
120	struct      timespec curtime;
121
122	nanouptime(&curtime);
123
124	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
125
126}
127
128uint64_t
129dtrace_gethrestime(void)
130{
131	struct      timespec curtime;
132
133	getnanotime(&curtime);
134
135	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
136}
137
138/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
139int
140dtrace_trap(struct trapframe *frame, u_int type)
141{
142
143	/*
144	 * A trap can occur while DTrace executes a probe. Before
145	 * executing the probe, DTrace blocks re-scheduling and sets
146	 * a flag in its per-cpu flags to indicate that it doesn't
147	 * want to fault. On returning from the probe, the no-fault
148	 * flag is cleared and finally re-scheduling is enabled.
149	 *
150	 * Check if DTrace has enabled 'no-fault' mode:
151	 */
152	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
153		/*
154		 * There are only a couple of trap types that are expected.
155		 * All the rest will be handled in the usual way.
156		 */
157		switch (type) {
158		/* Page fault. */
159		case T_TLB_ST_MISS:
160		case T_ADDR_ERR_ST:
161		case T_TLB_LD_MISS:
162		case T_ADDR_ERR_LD:
163		case T_BUS_ERR_IFETCH:
164			/* Flag a bad address. */
165			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
166			cpu_core[curcpu].cpuc_dtrace_illval = frame->badvaddr;
167
168			/*
169			 * Offset the instruction pointer to the instruction
170			 * following the one causing the fault.
171			 */
172			if (DELAYBRANCH(frame->cause))	 /* Check BD bit */
173			{
174				/* XXX: check MipsEmulateBranch on MIPS64
175				frame->pc = MipsEmulateBranch(frame, frame->pc,
176				    0, 0);
177 				*/
178				panic("%s: delay slot at %jx, badvaddr = %jx\n",
179				    __func__,
180				    (intmax_t)frame->pc, (intmax_t)frame->badvaddr);
181			}
182			else
183				frame->pc += sizeof(int);
184			return (1);
185		default:
186			/* Handle all other traps in the usual way. */
187			break;
188		}
189	}
190
191	/* Handle the trap in the usual way. */
192	return (0);
193}
194
195void
196dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
197    int fault, int fltoffs, uintptr_t illval)
198{
199
200	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
201	    (uintptr_t)epid,
202	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
203}
204