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 <vm/pmap.h> 47 48 #define DELAYBRANCH(x) ((int)(x) < 0) 49 50 #define BIT_PC 15 51 #define BIT_LR 14 52 #define BIT_SP 13 53 54 extern dtrace_id_t dtrace_probeid_error; 55 extern int (*dtrace_invop_jump_addr)(struct trapframe *); 56 extern void dtrace_getnanotime(struct timespec *tsp); 57 58 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 59 void dtrace_invop_init(void); 60 void dtrace_invop_uninit(void); 61 62 typedef struct dtrace_invop_hdlr { 63 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); 64 struct dtrace_invop_hdlr *dtih_next; 65 } dtrace_invop_hdlr_t; 66 67 dtrace_invop_hdlr_t *dtrace_invop_hdlr; 68 69 int 70 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 71 { 72 dtrace_invop_hdlr_t *hdlr; 73 int rval; 74 75 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 76 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 77 return (rval); 78 79 return (0); 80 } 81 82 83 void 84 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 85 { 86 dtrace_invop_hdlr_t *hdlr; 87 88 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 89 hdlr->dtih_func = func; 90 hdlr->dtih_next = dtrace_invop_hdlr; 91 dtrace_invop_hdlr = hdlr; 92 } 93 94 void 95 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 96 { 97 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 98 99 for (;;) { 100 if (hdlr == NULL) 101 panic("attempt to remove non-existent invop handler"); 102 103 if (hdlr->dtih_func == func) 104 break; 105 106 prev = hdlr; 107 hdlr = hdlr->dtih_next; 108 } 109 110 if (prev == NULL) { 111 ASSERT(dtrace_invop_hdlr == hdlr); 112 dtrace_invop_hdlr = hdlr->dtih_next; 113 } else { 114 ASSERT(dtrace_invop_hdlr != hdlr); 115 prev->dtih_next = hdlr->dtih_next; 116 } 117 118 kmem_free(hdlr, 0); 119 } 120 121 122 /*ARGSUSED*/ 123 void 124 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 125 { 126 127 /* 128 * There are no ranges to exclude that are common to all 32-bit arm 129 * platforms. This function only needs to exclude ranges "... in 130 * which it is impossible to recover from such a load after it has been 131 * attempted." -- i.e., accessing within the range causes some sort 132 * fault in the system which is not handled by the normal arm 133 * exception-handling mechanisms. If systems exist where that is the 134 * case, a method to handle this functionality would have to be added to 135 * the platform_if interface so that those systems could provide their 136 * specific toxic range(s). 137 */ 138 } 139 140 void 141 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 142 { 143 cpuset_t cpus; 144 145 if (cpu == DTRACE_CPUALL) 146 cpus = all_cpus; 147 else 148 CPU_SETOF(cpu, &cpus); 149 150 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func, 151 smp_no_rendezvous_barrier, arg); 152 } 153 154 static void 155 dtrace_sync_func(void) 156 { 157 } 158 159 void 160 dtrace_sync(void) 161 { 162 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 163 } 164 165 /* 166 * DTrace needs a high resolution time function which can 167 * be called from a probe context and guaranteed not to have 168 * instrumented with probes itself. 169 * 170 * Returns nanoseconds since boot. 171 */ 172 uint64_t 173 dtrace_gethrtime() 174 { 175 struct timespec curtime; 176 177 nanouptime(&curtime); 178 179 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); 180 181 } 182 183 uint64_t 184 dtrace_gethrestime(void) 185 { 186 struct timespec current_time; 187 188 dtrace_getnanotime(¤t_time); 189 190 return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec); 191 } 192 193 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ 194 int 195 dtrace_trap(struct trapframe *frame, u_int type) 196 { 197 /* 198 * A trap can occur while DTrace executes a probe. Before 199 * executing the probe, DTrace blocks re-scheduling and sets 200 * a flag in its per-cpu flags to indicate that it doesn't 201 * want to fault. On returning from the probe, the no-fault 202 * flag is cleared and finally re-scheduling is enabled. 203 * 204 * Check if DTrace has enabled 'no-fault' mode: 205 * 206 */ 207 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 208 /* 209 * There are only a couple of trap types that are expected. 210 * All the rest will be handled in the usual way. 211 */ 212 switch (type) { 213 /* Page fault. */ 214 case FAULT_ALIGN: 215 /* Flag a bad address. */ 216 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 217 cpu_core[curcpu].cpuc_dtrace_illval = 0; 218 219 /* 220 * Offset the instruction pointer to the instruction 221 * following the one causing the fault. 222 */ 223 frame->tf_pc += sizeof(int); 224 return (1); 225 default: 226 /* Handle all other traps in the usual way. */ 227 break; 228 } 229 } 230 231 /* Handle the trap in the usual way. */ 232 return (0); 233 } 234 235 void 236 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which, 237 int fault, int fltoffs, uintptr_t illval) 238 { 239 240 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state, 241 (uintptr_t)epid, 242 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs); 243 } 244 245 static int 246 dtrace_invop_start(struct trapframe *frame) 247 { 248 register_t *r0, *sp; 249 int data, invop, reg, update_sp; 250 251 invop = dtrace_invop(frame->tf_pc, frame, frame->tf_r0); 252 switch (invop & DTRACE_INVOP_MASK) { 253 case DTRACE_INVOP_PUSHM: 254 sp = (register_t *)frame->tf_svc_sp; 255 r0 = &frame->tf_r0; 256 data = DTRACE_INVOP_DATA(invop); 257 258 /* 259 * Store the pc, lr, and sp. These have their own 260 * entries in the struct. 261 */ 262 if (data & (1 << BIT_PC)) { 263 sp--; 264 *sp = frame->tf_pc; 265 } 266 if (data & (1 << BIT_LR)) { 267 sp--; 268 *sp = frame->tf_svc_lr; 269 } 270 if (data & (1 << BIT_SP)) { 271 sp--; 272 *sp = frame->tf_svc_sp; 273 } 274 275 /* Store the general registers */ 276 for (reg = 12; reg >= 0; reg--) { 277 if (data & (1 << reg)) { 278 sp--; 279 *sp = r0[reg]; 280 } 281 } 282 283 /* Update the stack pointer and program counter to continue */ 284 frame->tf_svc_sp = (register_t)sp; 285 frame->tf_pc += 4; 286 break; 287 case DTRACE_INVOP_POPM: 288 sp = (register_t *)frame->tf_svc_sp; 289 r0 = &frame->tf_r0; 290 data = DTRACE_INVOP_DATA(invop); 291 292 /* Read the general registers */ 293 for (reg = 0; reg <= 12; reg++) { 294 if (data & (1 << reg)) { 295 r0[reg] = *sp; 296 sp++; 297 } 298 } 299 300 /* 301 * Set the stack pointer. If we don't update it here we will 302 * need to update it at the end as the instruction would do 303 */ 304 update_sp = 1; 305 if (data & (1 << BIT_SP)) { 306 frame->tf_svc_sp = *sp; 307 *sp++; 308 update_sp = 0; 309 } 310 311 /* Update the link register, we need to use the correct copy */ 312 if (data & (1 << BIT_LR)) { 313 frame->tf_svc_lr = *sp; 314 *sp++; 315 } 316 /* 317 * And the program counter. If it's not in the list skip over 318 * it when we return so to not hit this again. 319 */ 320 if (data & (1 << BIT_PC)) { 321 frame->tf_pc = *sp; 322 *sp++; 323 } else 324 frame->tf_pc += 4; 325 326 /* Update the stack pointer if we haven't already done so */ 327 if (update_sp) 328 frame->tf_svc_sp = (register_t)sp; 329 break; 330 case DTRACE_INVOP_B: 331 data = DTRACE_INVOP_DATA(invop) & 0x00ffffff; 332 /* Sign extend the data */ 333 if ((data & (1 << 23)) != 0) 334 data |= 0xff000000; 335 /* The data is the number of 4-byte words to change the pc */ 336 data *= 4; 337 data += 8; 338 frame->tf_pc += data; 339 break; 340 default: 341 return (-1); 342 break; 343 } 344 345 return (0); 346 } 347 348 void dtrace_invop_init(void) 349 { 350 dtrace_invop_jump_addr = dtrace_invop_start; 351 } 352 353 void dtrace_invop_uninit(void) 354 { 355 dtrace_invop_jump_addr = 0; 356 } 357