dtrace_subr.c revision 298171
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 298171 2016-04-17 23:08:47Z 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 298171 2016-04-17 23:08:47Z 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 dtrace_id_t	dtrace_probeid_error;
50
51int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
52
53typedef struct dtrace_invop_hdlr {
54	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
55	struct dtrace_invop_hdlr *dtih_next;
56} dtrace_invop_hdlr_t;
57
58dtrace_invop_hdlr_t *dtrace_invop_hdlr;
59
60int
61dtrace_invop(uintptr_t addr, struct trapframe *stack, uintptr_t eax)
62{
63	dtrace_invop_hdlr_t *hdlr;
64	int rval;
65
66	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
67		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
68			return (rval);
69
70	return (0);
71}
72
73
74/*ARGSUSED*/
75void
76dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
77{
78	/*
79	 * No toxic regions?
80	 */
81}
82
83void
84dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
85{
86	cpuset_t cpus;
87
88	if (cpu == DTRACE_CPUALL)
89		cpus = all_cpus;
90	else
91		CPU_SETOF(cpu, &cpus);
92
93	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
94	    smp_no_rendevous_barrier, arg);
95}
96
97static void
98dtrace_sync_func(void)
99{
100}
101
102void
103dtrace_sync(void)
104{
105        dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
106}
107
108/*
109 * DTrace needs a high resolution time function which can
110 * be called from a probe context and guaranteed not to have
111 * instrumented with probes itself.
112 *
113 * Returns nanoseconds since boot.
114 */
115uint64_t
116dtrace_gethrtime()
117{
118	struct      timespec curtime;
119
120	nanouptime(&curtime);
121
122	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
123
124}
125
126uint64_t
127dtrace_gethrestime(void)
128{
129	struct      timespec curtime;
130
131	getnanotime(&curtime);
132
133	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
134}
135
136/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
137int
138dtrace_trap(struct trapframe *frame, u_int type)
139{
140
141	/*
142	 * A trap can occur while DTrace executes a probe. Before
143	 * executing the probe, DTrace blocks re-scheduling and sets
144	 * a flag in its per-cpu flags to indicate that it doesn't
145	 * want to fault. On returning from the probe, the no-fault
146	 * flag is cleared and finally re-scheduling is enabled.
147	 *
148	 * Check if DTrace has enabled 'no-fault' mode:
149	 */
150	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
151		/*
152		 * There are only a couple of trap types that are expected.
153		 * All the rest will be handled in the usual way.
154		 */
155		switch (type) {
156		/* Page fault. */
157		case T_TLB_ST_MISS:
158		case T_ADDR_ERR_ST:
159		case T_TLB_LD_MISS:
160		case T_ADDR_ERR_LD:
161		case T_BUS_ERR_IFETCH:
162			/* Flag a bad address. */
163			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
164			cpu_core[curcpu].cpuc_dtrace_illval = frame->badvaddr;
165
166			/*
167			 * Offset the instruction pointer to the instruction
168			 * following the one causing the fault.
169			 */
170			if (DELAYBRANCH(frame->cause))	 /* Check BD bit */
171			{
172				/* XXX: check MipsEmulateBranch on MIPS64
173				frame->pc = MipsEmulateBranch(frame, frame->pc,
174				    0, 0);
175 				*/
176				panic("%s: delay slot at %jx, badvaddr = %jx\n",
177				    __func__,
178				    (intmax_t)frame->pc, (intmax_t)frame->badvaddr);
179			}
180			else
181				frame->pc += sizeof(int);
182			return (1);
183		default:
184			/* Handle all other traps in the usual way. */
185			break;
186		}
187	}
188
189	/* Handle the trap in the usual way. */
190	return (0);
191}
192
193void
194dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
195    int fault, int fltoffs, uintptr_t illval)
196{
197
198	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
199	    (uintptr_t)epid,
200	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
201}
202