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
48 extern dtrace_id_t dtrace_probeid_error;
49 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
50 extern void dtrace_getnanotime(struct timespec *tsp);
51 extern void dtrace_getnanouptime(struct timespec *tsp);
52
53 int dtrace_invop(uintptr_t, struct trapframe *);
54 void dtrace_invop_init(void);
55 void dtrace_invop_uninit(void);
56
57 typedef 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
62 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
63
64 int
dtrace_invop(uintptr_t addr,struct trapframe * frame)65 dtrace_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
81 void
dtrace_invop_add(int (* func)(uintptr_t,struct trapframe *,uintptr_t))82 dtrace_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
92 void
dtrace_invop_remove(int (* func)(uintptr_t,struct trapframe *,uintptr_t))93 dtrace_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*/
123 void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))124 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
125 {
126
127 (*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);
128 }
129
130 /*
131 * DTrace needs a high resolution time function which can
132 * be called from a probe context and guaranteed not to have
133 * instrumented with probes itself.
134 *
135 * Returns nanoseconds since boot.
136 */
137 uint64_t
dtrace_gethrtime(void)138 dtrace_gethrtime(void)
139 {
140 struct timespec curtime;
141
142 dtrace_getnanouptime(&curtime);
143
144 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);
145
146 }
147
148 uint64_t
dtrace_gethrestime(void)149 dtrace_gethrestime(void)
150 {
151 struct timespec current_time;
152
153 dtrace_getnanotime(¤t_time);
154
155 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);
156 }
157
158 /* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */
159 int
dtrace_trap(struct trapframe * frame,u_int type)160 dtrace_trap(struct trapframe *frame, u_int type)
161 {
162 /*
163 * A trap can occur while DTrace executes a probe. Before
164 * executing the probe, DTrace blocks re-scheduling and sets
165 * a flag in its per-cpu flags to indicate that it doesn't
166 * want to fault. On returning from the probe, the no-fault
167 * flag is cleared and finally re-scheduling is enabled.
168 *
169 * Check if DTrace has enabled 'no-fault' mode:
170 */
171 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
172 /*
173 * There are only a couple of trap types that are expected.
174 * All the rest will be handled in the usual way.
175 */
176 switch (type) {
177 case SCAUSE_LOAD_ACCESS_FAULT:
178 case SCAUSE_STORE_ACCESS_FAULT:
179 case SCAUSE_INST_ACCESS_FAULT:
180 case SCAUSE_INST_PAGE_FAULT:
181 case SCAUSE_LOAD_PAGE_FAULT:
182 case SCAUSE_STORE_PAGE_FAULT:
183 /* Flag a bad address. */
184 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
185 cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_stval;
186
187 /*
188 * Offset the instruction pointer to the instruction
189 * following the one causing the fault.
190 */
191 frame->tf_sepc +=
192 dtrace_instr_size((uint8_t *)frame->tf_sepc);
193
194 return (1);
195 default:
196 /* Handle all other traps in the usual way. */
197 break;
198 }
199 }
200
201 /* Handle the trap in the usual way. */
202 return (0);
203 }
204
205 void
dtrace_probe_error(dtrace_state_t * state,dtrace_epid_t epid,int which,int fault,int fltoffs,uintptr_t illval)206 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
207 int fault, int fltoffs, uintptr_t illval)
208 {
209
210 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,
211 (uintptr_t)epid,
212 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);
213 }
214
215 static int
dtrace_invop_start(struct trapframe * frame)216 dtrace_invop_start(struct trapframe *frame)
217 {
218 register_t *sp;
219 uint32_t uimm;
220 uint32_t imm;
221 int invop;
222
223 invop = dtrace_invop(frame->tf_sepc, frame);
224 if (invop == 0)
225 return (-1);
226
227 if (dtrace_match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),
228 (MASK_SD | RS2_MASK | RS1_MASK))) {
229 /* Non-compressed store of ra to sp */
230 imm = (invop >> 7) & 0x1f;
231 imm |= ((invop >> 25) & 0x7f) << 5;
232 sp = (register_t *)((uint8_t *)frame->tf_sp + imm);
233 *sp = frame->tf_ra;
234 frame->tf_sepc += INSN_SIZE;
235 return (0);
236 }
237
238 if (dtrace_match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),
239 (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {
240 /* Non-compressed ret */
241 frame->tf_sepc = frame->tf_ra;
242 return (0);
243 }
244
245 if (dtrace_match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),
246 (MASK_C_SDSP | RS2_C_MASK))) {
247 /* 'C'-compressed store of ra to sp */
248 uimm = ((invop >> 10) & 0x7) << 3;
249 uimm |= ((invop >> 7) & 0x7) << 6;
250 sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);
251 *sp = frame->tf_ra;
252 frame->tf_sepc += INSN_C_SIZE;
253 return (0);
254 }
255
256 if (dtrace_match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),
257 (MASK_C_JR | RD_MASK))) {
258 /* 'C'-compressed ret */
259 frame->tf_sepc = frame->tf_ra;
260 return (0);
261 }
262
263 if (dtrace_match_opcode(invop, MATCH_C_NOP, MASK_C_NOP))
264 return (0);
265
266 #ifdef INVARIANTS
267 panic("Instruction %x doesn't match any opcode.", invop);
268 #endif
269
270 return (-1);
271 }
272
273 void
dtrace_invop_init(void)274 dtrace_invop_init(void)
275 {
276
277 dtrace_invop_jump_addr = dtrace_invop_start;
278 }
279
280 void
dtrace_invop_uninit(void)281 dtrace_invop_uninit(void)
282 {
283
284 dtrace_invop_jump_addr = 0;
285 }
286