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 * Portions Copyright 2016-2018 Ruslan Bukin <br@bsdpad.com>
23 *
24 */
25/*
26 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27 * Use is subject to license terms.
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/kmem.h>
35#include <sys/proc.h>
36#include <sys/smp.h>
37#include <sys/dtrace_impl.h>
38#include <sys/dtrace_bsd.h>
39#include <cddl/dev/dtrace/dtrace_cddl.h>
40#include <machine/vmparam.h>
41#include <machine/encoding.h>
42#include <machine/riscvreg.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);
51extern void dtrace_getnanouptime(struct timespec *tsp);
52
53int dtrace_invop(uintptr_t, struct trapframe *);
54void dtrace_invop_init(void);
55void dtrace_invop_uninit(void);
56
57typedef struct dtrace_invop_hdlr {
58	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
59	struct dtrace_invop_hdlr *dtih_next;
60} dtrace_invop_hdlr_t;
61
62dtrace_invop_hdlr_t *dtrace_invop_hdlr;
63
64int
65dtrace_invop(uintptr_t addr, struct trapframe *frame)
66{
67	struct thread *td;
68	dtrace_invop_hdlr_t *hdlr;
69	int rval;
70
71	rval = 0;
72	td = curthread;
73	td->t_dtrace_trapframe = frame;
74	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
75		if ((rval = hdlr->dtih_func(addr, frame, 0)) != 0)
76			break;
77	td->t_dtrace_trapframe = NULL;
78	return (rval);
79}
80
81void
82dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
83{
84	dtrace_invop_hdlr_t *hdlr;
85
86	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
87	hdlr->dtih_func = func;
88	hdlr->dtih_next = dtrace_invop_hdlr;
89	dtrace_invop_hdlr = hdlr;
90}
91
92void
93dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
94{
95	dtrace_invop_hdlr_t *hdlr, *prev;
96
97	hdlr = dtrace_invop_hdlr;
98	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/*ARGSUSED*/
123void
124dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
125{
126
127	(*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
128}
129
130void
131dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
132{
133	cpuset_t cpus;
134
135	if (cpu == DTRACE_CPUALL)
136		cpus = all_cpus;
137	else
138		CPU_SETOF(cpu, &cpus);
139
140	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
141	    smp_no_rendezvous_barrier, arg);
142}
143
144static void
145dtrace_sync_func(void)
146{
147
148}
149
150void
151dtrace_sync(void)
152{
153
154	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
155}
156
157/*
158 * DTrace needs a high resolution time function which can
159 * be called from a probe context and guaranteed not to have
160 * instrumented with probes itself.
161 *
162 * Returns nanoseconds since boot.
163 */
164uint64_t
165dtrace_gethrtime(void)
166{
167	struct timespec curtime;
168
169	dtrace_getnanouptime(&curtime);
170
171	return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
172
173}
174
175uint64_t
176dtrace_gethrestime(void)
177{
178	struct timespec current_time;
179
180	dtrace_getnanotime(&current_time);
181
182	return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
183}
184
185/* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */
186int
187dtrace_trap(struct trapframe *frame, u_int type)
188{
189	/*
190	 * A trap can occur while DTrace executes a probe. Before
191	 * executing the probe, DTrace blocks re-scheduling and sets
192	 * a flag in its per-cpu flags to indicate that it doesn't
193	 * want to fault. On returning from the probe, the no-fault
194	 * flag is cleared and finally re-scheduling is enabled.
195	 *
196	 * Check if DTrace has enabled 'no-fault' mode:
197	 */
198	if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
199		/*
200		 * There are only a couple of trap types that are expected.
201		 * All the rest will be handled in the usual way.
202		 */
203		switch (type) {
204		case SCAUSE_LOAD_ACCESS_FAULT:
205		case SCAUSE_STORE_ACCESS_FAULT:
206		case SCAUSE_INST_ACCESS_FAULT:
207		case SCAUSE_INST_PAGE_FAULT:
208		case SCAUSE_LOAD_PAGE_FAULT:
209		case SCAUSE_STORE_PAGE_FAULT:
210			/* Flag a bad address. */
211			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
212			cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_stval;
213
214			/*
215			 * Offset the instruction pointer to the instruction
216			 * following the one causing the fault.
217			 */
218			frame->tf_sepc +=
219			    dtrace_instr_size((uint8_t *)frame->tf_sepc);
220
221			return (1);
222		default:
223			/* Handle all other traps in the usual way. */
224			break;
225		}
226	}
227
228	/* Handle the trap in the usual way. */
229	return (0);
230}
231
232void
233dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
234    int fault, int fltoffs, uintptr_t illval)
235{
236
237	dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
238	    (uintptr_t)epid,
239	    (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
240}
241
242static int
243dtrace_invop_start(struct trapframe *frame)
244{
245	register_t *sp;
246	uint32_t uimm;
247	uint32_t imm;
248	int invop;
249
250	invop = dtrace_invop(frame->tf_sepc, frame);
251	if (invop == 0)
252		return (-1);
253
254	if (dtrace_match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
255	    (MASK_SD | RS2_MASK | RS1_MASK))) {
256		/* Non-compressed store of ra to sp */
257		imm = (invop >> 7) & 0x1f;
258		imm |= ((invop >> 25) & 0x7f) << 5;
259		sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
260		*sp = frame->tf_ra;
261		frame->tf_sepc += INSN_SIZE;
262		return (0);
263	}
264
265	if (dtrace_match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
266	    (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
267		/* Non-compressed ret */
268		frame->tf_sepc = frame->tf_ra;
269		return (0);
270	}
271
272	if (dtrace_match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
273	    (MASK_C_SDSP | RS2_C_MASK))) {
274		/* 'C'-compressed store of ra to sp */
275		uimm = ((invop >> 10) & 0x7) << 3;
276		uimm |= ((invop >> 7) & 0x7) << 6;
277		sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);
278		*sp = frame->tf_ra;
279		frame->tf_sepc += INSN_C_SIZE;
280		return (0);
281	}
282
283	if (dtrace_match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
284	    (MASK_C_JR | RD_MASK))) {
285		/* 'C'-compressed ret */
286		frame->tf_sepc = frame->tf_ra;
287		return (0);
288	}
289
290	if (dtrace_match_opcode(invop, MATCH_C_NOP, MASK_C_NOP))
291		return (0);
292
293#ifdef INVARIANTS
294	panic("Instruction %x doesn't match any opcode.", invop);
295#endif
296
297	return (-1);
298}
299
300void
301dtrace_invop_init(void)
302{
303
304	dtrace_invop_jump_addr = dtrace_invop_start;
305}
306
307void
308dtrace_invop_uninit(void)
309{
310
311	dtrace_invop_jump_addr = 0;
312}
313