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/aarch64/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/aarch64/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/armreg.h>
43#include <machine/clock.h>
44#include <machine/frame.h>
45#include <machine/trap.h>
46#include <vm/pmap.h>
47
48extern dtrace_id_t	dtrace_probeid_error;
49extern int (*dtrace_invop_jump_addr)(struct trapframe *);
50extern void dtrace_getnanotime(struct timespec *tsp);
51
52int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
53void dtrace_invop_init(void);
54void dtrace_invop_uninit(void);
55
56typedef struct dtrace_invop_hdlr {
57	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
58	struct dtrace_invop_hdlr *dtih_next;
59} dtrace_invop_hdlr_t;
60
61dtrace_invop_hdlr_t *dtrace_invop_hdlr;
62
63int
64dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
65{
66	dtrace_invop_hdlr_t *hdlr;
67	int rval;
68
69	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
70		if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
71			return (rval);
72
73	return (0);
74}
75
76
77void
78dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
79{
80	dtrace_invop_hdlr_t *hdlr;
81
82	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
83	hdlr->dtih_func = func;
84	hdlr->dtih_next = dtrace_invop_hdlr;
85	dtrace_invop_hdlr = hdlr;
86}
87
88void
89dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
90{
91	dtrace_invop_hdlr_t *hdlr, *prev;
92
93	hdlr = dtrace_invop_hdlr;
94	prev = NULL;
95
96	for (;;) {
97		if (hdlr == NULL)
98			panic("attempt to remove non-existent invop handler");
99
100		if (hdlr->dtih_func == func)
101			break;
102
103		prev = hdlr;
104		hdlr = hdlr->dtih_next;
105	}
106
107	if (prev == NULL) {
108		ASSERT(dtrace_invop_hdlr == hdlr);
109		dtrace_invop_hdlr = hdlr->dtih_next;
110	} else {
111		ASSERT(dtrace_invop_hdlr != hdlr);
112		prev->dtih_next = hdlr->dtih_next;
113	}
114
115	kmem_free(hdlr, 0);
116}
117
118/*ARGSUSED*/
119void
120dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
121{
122
123	printf("IMPLEMENT ME: dtrace_toxic_ranges\n");
124}
125
126void
127dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
128{
129	cpuset_t cpus;
130
131	if (cpu == DTRACE_CPUALL)
132		cpus = all_cpus;
133	else
134		CPU_SETOF(cpu, &cpus);
135
136	smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func,
137	    smp_no_rendevous_barrier, arg);
138}
139
140static void
141dtrace_sync_func(void)
142{
143
144}
145
146void
147dtrace_sync(void)
148{
149
150	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
151}
152
153/*
154 * DTrace needs a high resolution time function which can
155 * be called from a probe context and guaranteed not to have
156 * instrumented with probes itself.
157 *
158 * Returns nanoseconds since boot.
159 */
160uint64_t
161dtrace_gethrtime()
162{
163	struct timespec curtime;
164
165	nanouptime(&curtime);
166
167	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
168
169}
170
171uint64_t
172dtrace_gethrestime(void)
173{
174	struct timespec current_time;
175
176	dtrace_getnanotime(&current_time);
177
178	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
179}
180
181/* Function to handle DTrace traps during probes. See arm64/arm64/trap.c */
182int
183dtrace_trap(struct trapframe *frame, u_int type)
184{
185	/*
186	 * A trap can occur while DTrace executes a probe. Before
187	 * executing the probe, DTrace blocks re-scheduling and sets
188	 * a flag in it's per-cpu flags to indicate that it doesn't
189	 * want to fault. On returning from the probe, the no-fault
190	 * flag is cleared and finally re-scheduling is enabled.
191	 *
192	 * Check if DTrace has enabled 'no-fault' mode:
193	 *
194	 */
195
196	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
197		/*
198		 * There are only a couple of trap types that are expected.
199		 * All the rest will be handled in the usual way.
200		 */
201		switch (type) {
202		case EXCP_DATA_ABORT:
203			/* Flag a bad address. */
204			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
205			cpu_core[curcpu].cpuc_dtrace_illval = 0;
206
207			/*
208			 * Offset the instruction pointer to the instruction
209			 * following the one causing the fault.
210			 */
211			frame->tf_elr += 4;
212			return (1);
213		default:
214			/* Handle all other traps in the usual way. */
215			break;
216		}
217	}
218
219	/* Handle the trap in the usual way. */
220	return (0);
221}
222
223void
224dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
225    int fault, int fltoffs, uintptr_t illval)
226{
227
228	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
229	    (uintptr_t)epid,
230	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
231}
232
233static int
234dtrace_invop_start(struct trapframe *frame)
235{
236	int data, invop, reg, update_sp;
237	register_t arg1, arg2;
238	register_t *sp;
239	int offs;
240	int tmp;
241	int i;
242
243	invop = dtrace_invop(frame->tf_elr, frame, frame->tf_elr);
244
245	tmp = (invop & LDP_STP_MASK);
246	if (tmp == STP_64 || tmp == LDP_64) {
247		sp = (register_t *)frame->tf_sp;
248		data = invop;
249		arg1 = (data >> ARG1_SHIFT) & ARG1_MASK;
250		arg2 = (data >> ARG2_SHIFT) & ARG2_MASK;
251
252		offs = (data >> OFFSET_SHIFT) & OFFSET_MASK;
253
254		switch (tmp) {
255		case STP_64:
256			if (offs >> (OFFSET_SIZE - 1))
257				sp -= (~offs & OFFSET_MASK) + 1;
258			else
259				sp += (offs);
260			*(sp + 0) = frame->tf_x[arg1];
261			*(sp + 1) = frame->tf_x[arg2];
262			break;
263		case LDP_64:
264			frame->tf_x[arg1] = *(sp + 0);
265			frame->tf_x[arg2] = *(sp + 1);
266			if (offs >> (OFFSET_SIZE - 1))
267				sp -= (~offs & OFFSET_MASK) + 1;
268			else
269				sp += (offs);
270			break;
271		default:
272			break;
273		}
274
275		/* Update the stack pointer and program counter to continue */
276		frame->tf_sp = (register_t)sp;
277		frame->tf_elr += INSN_SIZE;
278		return (0);
279	}
280
281	if ((invop & B_MASK) == B_INSTR) {
282		data = (invop & B_DATA_MASK);
283		/* The data is the number of 4-byte words to change the pc */
284		data *= 4;
285		frame->tf_elr += data;
286		return (0);
287	}
288
289	if (invop == RET_INSTR) {
290		frame->tf_elr = frame->tf_lr;
291		return (0);
292	}
293
294	return (-1);
295}
296
297void
298dtrace_invop_init(void)
299{
300
301	dtrace_invop_jump_addr = dtrace_invop_start;
302}
303
304void
305dtrace_invop_uninit(void)
306{
307
308	dtrace_invop_jump_addr = 0;
309}
310