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
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.
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]
22 * Portions Copyright 2016-2018 Ruslan Bukin <br@bsdpad.com>
28 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/dtrace_impl.h>
43 #include <sys/dtrace_bsd.h>
44 #include <machine/vmparam.h>
45 #include <machine/encoding.h>
46 #include <machine/riscvreg.h>
47 #include <machine/clock.h>
48 #include <machine/frame.h>
49 #include <machine/trap.h>
52 extern dtrace_id_t dtrace_probeid_error;
53 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
54 extern void dtrace_getnanotime(struct timespec *tsp);
56 int dtrace_invop(uintptr_t, struct trapframe *);
57 void dtrace_invop_init(void);
58 void dtrace_invop_uninit(void);
60 typedef struct dtrace_invop_hdlr {
61 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
62 struct dtrace_invop_hdlr *dtih_next;
63 } dtrace_invop_hdlr_t;
65 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
68 dtrace_invop(uintptr_t addr, struct trapframe *frame)
70 dtrace_invop_hdlr_t *hdlr;
73 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
74 if ((rval = hdlr->dtih_func(addr, frame, 0)) != 0)
81 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
83 dtrace_invop_hdlr_t *hdlr;
85 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
86 hdlr->dtih_func = func;
87 hdlr->dtih_next = dtrace_invop_hdlr;
88 dtrace_invop_hdlr = hdlr;
92 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
94 dtrace_invop_hdlr_t *hdlr, *prev;
96 hdlr = dtrace_invop_hdlr;
101 panic("attempt to remove non-existent invop handler");
103 if (hdlr->dtih_func == func)
107 hdlr = hdlr->dtih_next;
111 ASSERT(dtrace_invop_hdlr == hdlr);
112 dtrace_invop_hdlr = hdlr->dtih_next;
114 ASSERT(dtrace_invop_hdlr != hdlr);
115 prev->dtih_next = hdlr->dtih_next;
123 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
126 (*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
130 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
134 if (cpu == DTRACE_CPUALL)
137 CPU_SETOF(cpu, &cpus);
139 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
140 smp_no_rendezvous_barrier, arg);
144 dtrace_sync_func(void)
153 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
157 * DTrace needs a high resolution time function which can
158 * be called from a probe context and guaranteed not to have
159 * instrumented with probes itself.
161 * Returns nanoseconds since boot.
166 struct timespec curtime;
168 nanouptime(&curtime);
170 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
175 dtrace_gethrestime(void)
177 struct timespec current_time;
179 dtrace_getnanotime(¤t_time);
181 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
184 /* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */
186 dtrace_trap(struct trapframe *frame, u_int type)
189 * A trap can occur while DTrace executes a probe. Before
190 * executing the probe, DTrace blocks re-scheduling and sets
191 * a flag in its per-cpu flags to indicate that it doesn't
192 * want to fault. On returning from the probe, the no-fault
193 * flag is cleared and finally re-scheduling is enabled.
195 * Check if DTrace has enabled 'no-fault' mode:
199 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
201 * There are only a couple of trap types that are expected.
202 * All the rest will be handled in the usual way.
205 case EXCP_FAULT_LOAD:
206 case EXCP_FAULT_STORE:
207 case EXCP_FAULT_FETCH:
208 /* Flag a bad address. */
209 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
210 cpu_core[curcpu].cpuc_dtrace_illval = 0;
213 * Offset the instruction pointer to the instruction
214 * following the one causing the fault.
220 /* Handle all other traps in the usual way. */
225 /* Handle the trap in the usual way. */
230 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
231 int fault, int fltoffs, uintptr_t illval)
234 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
236 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
240 match_opcode(uint32_t insn, int match, int mask)
243 if (((insn ^ match) & mask) == 0)
250 dtrace_invop_start(struct trapframe *frame)
257 invop = dtrace_invop(frame->tf_sepc, frame);
261 if (match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
262 (MASK_SD | RS2_MASK | RS1_MASK))) {
263 /* Non-compressed store of ra to sp */
264 imm = (invop >> 7) & 0x1f;
265 imm |= ((invop >> 25) & 0x7f) << 5;
266 sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
268 frame->tf_sepc += INSN_SIZE;
272 if (match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
273 (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
274 /* Non-compressed ret */
275 frame->tf_sepc = frame->tf_ra;
279 if (match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
280 (MASK_C_SDSP | RS2_C_MASK))) {
281 /* 'C'-compressed store of ra to sp */
282 uimm = ((invop >> 10) & 0x7) << 3;
283 uimm |= ((invop >> 7) & 0x7) << 6;
284 sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);
286 frame->tf_sepc += INSN_C_SIZE;
290 if (match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
291 (MASK_C_JR | RD_MASK))) {
292 /* 'C'-compressed ret */
293 frame->tf_sepc = frame->tf_ra;
298 panic("Instruction %x doesn't match any opcode.", invop);
305 dtrace_invop_init(void)
308 dtrace_invop_jump_addr = dtrace_invop_start;
312 dtrace_invop_uninit(void)
315 dtrace_invop_jump_addr = 0;