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 */
23/*
24 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25 * Use is subject to license terms.
26 */
27
28#include <sys/param.h>
29#include <sys/systm.h>
30#include <sys/kernel.h>
31#include <sys/malloc.h>
32#include <sys/kmem.h>
33#include <sys/proc.h>
34#include <sys/smp.h>
35#include <sys/dtrace_impl.h>
36#include <sys/dtrace_bsd.h>
37#include <cddl/dev/dtrace/dtrace_cddl.h>
38#include <machine/armreg.h>
39#include <machine/clock.h>
40#include <machine/frame.h>
41#include <machine/trap.h>
42#include <vm/pmap.h>
43
44#define	DELAYBRANCH(x)	((int)(x) < 0)
45
46#define	BIT_PC		15
47#define	BIT_LR		14
48#define	BIT_SP		13
49
50extern dtrace_id_t	dtrace_probeid_error;
51extern int (*dtrace_invop_jump_addr)(struct trapframe *);
52extern void dtrace_getnanotime(struct timespec *tsp);
53extern void dtrace_getnanouptime(struct timespec *tsp);
54
55int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
56void dtrace_invop_init(void);
57void dtrace_invop_uninit(void);
58
59typedef struct dtrace_invop_hdlr {
60	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
61	struct dtrace_invop_hdlr *dtih_next;
62} dtrace_invop_hdlr_t;
63
64dtrace_invop_hdlr_t *dtrace_invop_hdlr;
65
66int
67dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
68{
69	struct thread *td;
70	dtrace_invop_hdlr_t *hdlr;
71	int rval;
72
73	rval = 0;
74	td = curthread;
75	td->t_dtrace_trapframe = frame;
76	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
77		if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
78			break;
79	td->t_dtrace_trapframe = NULL;
80	return (rval);
81}
82
83
84void
85dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
86{
87	dtrace_invop_hdlr_t *hdlr;
88
89	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
90	hdlr->dtih_func = func;
91	hdlr->dtih_next = dtrace_invop_hdlr;
92	dtrace_invop_hdlr = hdlr;
93}
94
95void
96dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
97{
98	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
99
100	for (;;) {
101		if (hdlr == NULL)
102			panic("attempt to remove non-existent invop handler");
103
104		if (hdlr->dtih_func == func)
105			break;
106
107		prev = hdlr;
108		hdlr = hdlr->dtih_next;
109	}
110
111	if (prev == NULL) {
112		ASSERT(dtrace_invop_hdlr == hdlr);
113		dtrace_invop_hdlr = hdlr->dtih_next;
114	} else {
115		ASSERT(dtrace_invop_hdlr != hdlr);
116		prev->dtih_next = hdlr->dtih_next;
117	}
118
119	kmem_free(hdlr, 0);
120}
121
122
123/*ARGSUSED*/
124void
125dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
126{
127
128	/*
129	 * There are no ranges to exclude that are common to all 32-bit arm
130	 * platforms.  This function only needs to exclude ranges "... in
131	 * which it is impossible to recover from such a load after it has been
132	 * attempted." -- i.e., accessing within the range causes some sort
133	 * fault in the system which is not handled by the normal arm
134	 * exception-handling mechanisms.  If systems exist where that is the
135	 * case, a method to handle this functionality would have to be added to
136	 * the platform_if interface so that those systems could provide their
137	 * specific toxic range(s).
138	 */
139}
140
141void
142dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
143{
144	cpuset_t cpus;
145
146	if (cpu == DTRACE_CPUALL)
147		cpus = all_cpus;
148	else
149		CPU_SETOF(cpu, &cpus);
150
151	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
152	    smp_no_rendezvous_barrier, arg);
153}
154
155static void
156dtrace_sync_func(void)
157{
158}
159
160void
161dtrace_sync(void)
162{
163	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
164}
165
166/*
167 * DTrace needs a high resolution time function which can
168 * be called from a probe context and guaranteed not to have
169 * instrumented with probes itself.
170 *
171 * Returns nanoseconds since boot.
172 */
173uint64_t
174dtrace_gethrtime(void)
175{
176	struct	timespec curtime;
177
178	dtrace_getnanouptime(&curtime);
179
180	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
181
182}
183
184uint64_t
185dtrace_gethrestime(void)
186{
187	struct timespec current_time;
188
189	dtrace_getnanotime(&current_time);
190
191	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
192}
193
194/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */
195int
196dtrace_trap(struct trapframe *frame, u_int type)
197{
198	/*
199	 * A trap can occur while DTrace executes a probe. Before
200	 * executing the probe, DTrace blocks re-scheduling and sets
201	 * a flag in its per-cpu flags to indicate that it doesn't
202	 * want to fault. On returning from the probe, the no-fault
203	 * flag is cleared and finally re-scheduling is enabled.
204	 *
205	 * Check if DTrace has enabled 'no-fault' mode:
206	 *
207	 */
208	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
209		/*
210		 * There are only a couple of trap types that are expected.
211		 * All the rest will be handled in the usual way.
212		 */
213		switch (type) {
214		/* Page fault. */
215		case FAULT_ALIGN:
216			/* Flag a bad address. */
217			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
218			cpu_core[curcpu].cpuc_dtrace_illval = 0;
219
220			/*
221			 * Offset the instruction pointer to the instruction
222			 * following the one causing the fault.
223			 */
224			frame->tf_pc += sizeof(int);
225			return (1);
226		default:
227			/* Handle all other traps in the usual way. */
228			break;
229		}
230	}
231
232	/* Handle the trap in the usual way. */
233	return (0);
234}
235
236void
237dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
238    int fault, int fltoffs, uintptr_t illval)
239{
240
241	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
242	    (uintptr_t)epid,
243	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
244}
245
246static int
247dtrace_invop_start(struct trapframe *frame)
248{
249	register_t *r0, *sp;
250	int data, invop, reg, update_sp;
251
252	invop = dtrace_invop(frame->tf_pc, frame, frame->tf_r0);
253	switch (invop & DTRACE_INVOP_MASK) {
254	case DTRACE_INVOP_PUSHM:
255		sp = (register_t *)frame->tf_svc_sp;
256		r0 = &frame->tf_r0;
257		data = DTRACE_INVOP_DATA(invop);
258
259		/*
260		 * Store the pc, lr, and sp. These have their own
261		 * entries in the struct.
262		 */
263		if (data & (1 << BIT_PC)) {
264			sp--;
265			*sp = frame->tf_pc;
266		}
267		if (data & (1 << BIT_LR)) {
268			sp--;
269			*sp = frame->tf_svc_lr;
270		}
271		if (data & (1 << BIT_SP)) {
272			sp--;
273			*sp = frame->tf_svc_sp;
274		}
275
276		/* Store the general registers */
277		for (reg = 12; reg >= 0; reg--) {
278			if (data & (1 << reg)) {
279				sp--;
280				*sp = r0[reg];
281			}
282		}
283
284		/* Update the stack pointer and program counter to continue */
285		frame->tf_svc_sp = (register_t)sp;
286		frame->tf_pc += 4;
287		break;
288	case DTRACE_INVOP_POPM:
289		sp = (register_t *)frame->tf_svc_sp;
290		r0 = &frame->tf_r0;
291		data = DTRACE_INVOP_DATA(invop);
292
293		/* Read the general registers */
294		for (reg = 0; reg <= 12; reg++) {
295			if (data & (1 << reg)) {
296				r0[reg] = *sp;
297				sp++;
298			}
299		}
300
301		/*
302		 * Set the stack pointer. If we don't update it here we will
303		 * need to update it at the end as the instruction would do
304		 */
305		update_sp = 1;
306		if (data & (1 << BIT_SP)) {
307			frame->tf_svc_sp = *sp;
308			*sp++;
309			update_sp = 0;
310		}
311
312		/* Update the link register, we need to use the correct copy */
313		if (data & (1 << BIT_LR)) {
314			frame->tf_svc_lr = *sp;
315			*sp++;
316		}
317		/*
318		 * And the program counter. If it's not in the list skip over
319		 * it when we return so to not hit this again.
320		 */
321		if (data & (1 << BIT_PC)) {
322			frame->tf_pc = *sp;
323			*sp++;
324		} else
325			frame->tf_pc += 4;
326
327		/* Update the stack pointer if we haven't already done so */
328		if (update_sp)
329			frame->tf_svc_sp = (register_t)sp;
330		break;
331	case DTRACE_INVOP_B:
332		data = DTRACE_INVOP_DATA(invop) & 0x00ffffff;
333		/* Sign extend the data */
334		if ((data & (1 << 23)) != 0)
335			data |= 0xff000000;
336		/* The data is the number of 4-byte words to change the pc */
337		data *= 4;
338		data += 8;
339		frame->tf_pc += data;
340		break;
341	default:
342		return (-1);
343		break;
344	}
345
346	return (0);
347}
348
349void dtrace_invop_init(void)
350{
351	dtrace_invop_jump_addr = dtrace_invop_start;
352}
353
354void dtrace_invop_uninit(void)
355{
356	dtrace_invop_jump_addr = 0;
357}
358