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 /* 194 * A trap can occur while DTrace executes a probe. Before 195 * executing the probe, DTrace blocks re-scheduling and sets 196 * a flag in its per-cpu flags to indicate that it doesn't 197 * want to fault. On returning from the probe, the no-fault 198 * flag is cleared and finally re-scheduling is enabled. 199 * 200 * Check if DTrace has enabled 'no-fault' mode: 201 */ 202 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 203 /* 204 * There are only a couple of trap types that are expected. 205 * All the rest will be handled in the usual way. 206 */ 207 switch (type) { 208 case SCAUSE_LOAD_ACCESS_FAULT: 209 case SCAUSE_STORE_ACCESS_FAULT: 210 case SCAUSE_INST_ACCESS_FAULT: 211 case SCAUSE_INST_PAGE_FAULT: 212 case SCAUSE_LOAD_PAGE_FAULT: 213 case SCAUSE_STORE_PAGE_FAULT: 214 /* Flag a bad address. */ 215 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 216 cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_stval; 217 218 /* 219 * Offset the instruction pointer to the instruction 220 * following the one causing the fault. 221 */ 222 frame->tf_sepc += 223 dtrace_instr_size((uint8_t *)frame->tf_sepc); 224 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 236 void 237 dtrace_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 246 static int 247 match_opcode(uint32_t insn, int match, int mask) 248 { 249 250 if (((insn ^ match) & mask) == 0) 251 return (1); 252 253 return (0); 254 } 255 256 static int 257 dtrace_invop_start(struct trapframe *frame) 258 { 259 register_t *sp; 260 uint32_t uimm; 261 uint32_t imm; 262 int invop; 263 264 invop = dtrace_invop(frame->tf_sepc, frame); 265 if (invop == 0) 266 return (-1); 267 268 if (match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP), 269 (MASK_SD | RS2_MASK | RS1_MASK))) { 270 /* Non-compressed store of ra to sp */ 271 imm = (invop >> 7) & 0x1f; 272 imm |= ((invop >> 25) & 0x7f) << 5; 273 sp = (register_t *)((uint8_t *)frame->tf_sp + imm); 274 *sp = frame->tf_ra; 275 frame->tf_sepc += INSN_SIZE; 276 return (0); 277 } 278 279 if (match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)), 280 (MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) { 281 /* Non-compressed ret */ 282 frame->tf_sepc = frame->tf_ra; 283 return (0); 284 } 285 286 if (match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA), 287 (MASK_C_SDSP | RS2_C_MASK))) { 288 /* 'C'-compressed store of ra to sp */ 289 uimm = ((invop >> 10) & 0x7) << 3; 290 uimm |= ((invop >> 7) & 0x7) << 6; 291 sp = (register_t *)((uint8_t *)frame->tf_sp + uimm); 292 *sp = frame->tf_ra; 293 frame->tf_sepc += INSN_C_SIZE; 294 return (0); 295 } 296 297 if (match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)), 298 (MASK_C_JR | RD_MASK))) { 299 /* 'C'-compressed ret */ 300 frame->tf_sepc = frame->tf_ra; 301 return (0); 302 } 303 304 if (match_opcode(invop, MATCH_C_NOP, MASK_C_NOP)) 305 return (0); 306 307 #ifdef INVARIANTS 308 panic("Instruction %x doesn't match any opcode.", invop); 309 #endif 310 311 return (-1); 312 } 313 314 void 315 dtrace_invop_init(void) 316 { 317 318 dtrace_invop_jump_addr = dtrace_invop_start; 319 } 320 321 void 322 dtrace_invop_uninit(void) 323 { 324 325 dtrace_invop_jump_addr = 0; 326 } 327