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 * $FreeBSD$ 23 * 24 */ 25 /* 26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/types.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/kmem.h> 39 #include <sys/smp.h> 40 #include <sys/dtrace_impl.h> 41 #include <sys/dtrace_bsd.h> 42 #include <machine/armreg.h> 43 #include <machine/clock.h> 44 #include <machine/frame.h> 45 #include <machine/trap.h> 46 #include <machine/vmparam.h> 47 #include <vm/pmap.h> 48 49 extern dtrace_id_t dtrace_probeid_error; 50 extern int (*dtrace_invop_jump_addr)(struct trapframe *); 51 extern void dtrace_getnanotime(struct timespec *tsp); 52 53 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 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 65 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 66 { 67 dtrace_invop_hdlr_t *hdlr; 68 int rval; 69 70 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 71 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 72 return (rval); 73 74 return (0); 75 } 76 77 78 void 79 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 80 { 81 dtrace_invop_hdlr_t *hdlr; 82 83 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 84 hdlr->dtih_func = func; 85 hdlr->dtih_next = dtrace_invop_hdlr; 86 dtrace_invop_hdlr = hdlr; 87 } 88 89 void 90 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 91 { 92 dtrace_invop_hdlr_t *hdlr, *prev; 93 94 hdlr = dtrace_invop_hdlr; 95 prev = NULL; 96 97 for (;;) { 98 if (hdlr == NULL) 99 panic("attempt to remove non-existent invop handler"); 100 101 if (hdlr->dtih_func == func) 102 break; 103 104 prev = hdlr; 105 hdlr = hdlr->dtih_next; 106 } 107 108 if (prev == NULL) { 109 ASSERT(dtrace_invop_hdlr == hdlr); 110 dtrace_invop_hdlr = hdlr->dtih_next; 111 } else { 112 ASSERT(dtrace_invop_hdlr != hdlr); 113 prev->dtih_next = hdlr->dtih_next; 114 } 115 116 kmem_free(hdlr, 0); 117 } 118 119 /*ARGSUSED*/ 120 void 121 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 122 { 123 124 (*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS); 125 } 126 127 void 128 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 129 { 130 cpuset_t cpus; 131 132 if (cpu == DTRACE_CPUALL) 133 cpus = all_cpus; 134 else 135 CPU_SETOF(cpu, &cpus); 136 137 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func, 138 smp_no_rendezvous_barrier, arg); 139 } 140 141 static void 142 dtrace_sync_func(void) 143 { 144 145 } 146 147 void 148 dtrace_sync(void) 149 { 150 151 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 152 } 153 154 /* 155 * DTrace needs a high resolution time function which can 156 * be called from a probe context and guaranteed not to have 157 * instrumented with probes itself. 158 * 159 * Returns nanoseconds since boot. 160 */ 161 uint64_t 162 dtrace_gethrtime() 163 { 164 struct timespec curtime; 165 166 nanouptime(&curtime); 167 168 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); 169 170 } 171 172 uint64_t 173 dtrace_gethrestime(void) 174 { 175 struct timespec current_time; 176 177 dtrace_getnanotime(¤t_time); 178 179 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec); 180 } 181 182 /* Function to handle DTrace traps during probes. See arm64/arm64/trap.c */ 183 int 184 dtrace_trap(struct trapframe *frame, u_int type) 185 { 186 /* 187 * A trap can occur while DTrace executes a probe. Before 188 * executing the probe, DTrace blocks re-scheduling and sets 189 * a flag in its per-cpu flags to indicate that it doesn't 190 * want to fault. On returning from the probe, the no-fault 191 * flag is cleared and finally re-scheduling is enabled. 192 * 193 * Check if DTrace has enabled 'no-fault' mode: 194 * 195 */ 196 197 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 198 /* 199 * There are only a couple of trap types that are expected. 200 * All the rest will be handled in the usual way. 201 */ 202 switch (type) { 203 case EXCP_DATA_ABORT: 204 /* Flag a bad address. */ 205 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 206 cpu_core[curcpu].cpuc_dtrace_illval = 0; 207 208 /* 209 * Offset the instruction pointer to the instruction 210 * following the one causing the fault. 211 */ 212 frame->tf_elr += 4; 213 return (1); 214 default: 215 /* Handle all other traps in the usual way. */ 216 break; 217 } 218 } 219 220 /* Handle the trap in the usual way. */ 221 return (0); 222 } 223 224 void 225 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which, 226 int fault, int fltoffs, uintptr_t illval) 227 { 228 229 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state, 230 (uintptr_t)epid, 231 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs); 232 } 233 234 static void 235 dtrace_load64(uint64_t *addr, struct trapframe *frame, u_int reg) 236 { 237 238 KASSERT(reg <= 31, ("dtrace_load64: Invalid register %u", reg)); 239 if (reg < nitems(frame->tf_x)) 240 frame->tf_x[reg] = *addr; 241 else if (reg == 30) /* lr */ 242 frame->tf_lr = *addr; 243 /* Nothing to do for load to xzr */ 244 } 245 246 static void 247 dtrace_store64(uint64_t *addr, struct trapframe *frame, u_int reg) 248 { 249 250 KASSERT(reg <= 31, ("dtrace_store64: Invalid register %u", reg)); 251 if (reg < nitems(frame->tf_x)) 252 *addr = frame->tf_x[reg]; 253 else if (reg == 30) /* lr */ 254 *addr = frame->tf_lr; 255 else if (reg == 31) /* xzr */ 256 *addr = 0; 257 } 258 259 static int 260 dtrace_invop_start(struct trapframe *frame) 261 { 262 int data, invop, reg, update_sp; 263 register_t arg1, arg2; 264 register_t *sp; 265 int offs; 266 int tmp; 267 int i; 268 269 invop = dtrace_invop(frame->tf_elr, frame, frame->tf_elr); 270 271 tmp = (invop & LDP_STP_MASK); 272 if (tmp == STP_64 || tmp == LDP_64) { 273 sp = (register_t *)frame->tf_sp; 274 data = invop; 275 arg1 = (data >> ARG1_SHIFT) & ARG1_MASK; 276 arg2 = (data >> ARG2_SHIFT) & ARG2_MASK; 277 278 offs = (data >> OFFSET_SHIFT) & OFFSET_MASK; 279 280 switch (tmp) { 281 case STP_64: 282 if (offs >> (OFFSET_SIZE - 1)) 283 sp -= (~offs & OFFSET_MASK) + 1; 284 else 285 sp += (offs); 286 dtrace_store64(sp + 0, frame, arg1); 287 dtrace_store64(sp + 1, frame, arg2); 288 break; 289 case LDP_64: 290 dtrace_load64(sp + 0, frame, arg1); 291 dtrace_load64(sp + 1, frame, arg2); 292 if (offs >> (OFFSET_SIZE - 1)) 293 sp -= (~offs & OFFSET_MASK) + 1; 294 else 295 sp += (offs); 296 break; 297 default: 298 break; 299 } 300 301 /* Update the stack pointer and program counter to continue */ 302 frame->tf_sp = (register_t)sp; 303 frame->tf_elr += INSN_SIZE; 304 return (0); 305 } 306 307 if ((invop & B_MASK) == B_INSTR) { 308 data = (invop & B_DATA_MASK); 309 /* The data is the number of 4-byte words to change the pc */ 310 data *= 4; 311 frame->tf_elr += data; 312 return (0); 313 } 314 315 if (invop == RET_INSTR) { 316 frame->tf_elr = frame->tf_lr; 317 return (0); 318 } 319 320 return (-1); 321 } 322 323 void 324 dtrace_invop_init(void) 325 { 326 327 dtrace_invop_jump_addr = dtrace_invop_start; 328 } 329 330 void 331 dtrace_invop_uninit(void) 332 { 333 334 dtrace_invop_jump_addr = 0; 335 } 336