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