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 extern uintptr_t dtrace_in_probe_addr; 51 extern int dtrace_in_probe; 52 extern dtrace_id_t dtrace_probeid_error; 53 extern int (*dtrace_invop_jump_addr)(struct trapframe *); 54 55 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t); 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, uintptr_t *, 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 int 67 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax) 68 { 69 dtrace_invop_hdlr_t *hdlr; 70 int rval; 71 72 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 73 if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) 74 return (rval); 75 76 return (0); 77 } 78 79 80 void 81 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 82 { 83 dtrace_invop_hdlr_t *hdlr; 84 85 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 86 hdlr->dtih_func = func; 87 hdlr->dtih_next = dtrace_invop_hdlr; 88 dtrace_invop_hdlr = hdlr; 89 } 90 91 void 92 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 93 { 94 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 95 96 for (;;) { 97 if (hdlr == NULL) 98 panic("attempt to remove non-existent invop handler"); 99 100 if (hdlr->dtih_func == func) 101 break; 102 103 prev = hdlr; 104 hdlr = hdlr->dtih_next; 105 } 106 107 if (prev == NULL) { 108 ASSERT(dtrace_invop_hdlr == hdlr); 109 dtrace_invop_hdlr = hdlr->dtih_next; 110 } else { 111 ASSERT(dtrace_invop_hdlr != hdlr); 112 prev->dtih_next = hdlr->dtih_next; 113 } 114 115 kmem_free(hdlr, 0); 116 } 117 118 119 /*ARGSUSED*/ 120 void 121 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 122 { 123 printf("IMPLEMENT ME: dtrace_toxic_ranges\n"); 124 } 125 126 void 127 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 128 { 129 cpuset_t cpus; 130 131 if (cpu == DTRACE_CPUALL) 132 cpus = all_cpus; 133 else 134 CPU_SETOF(cpu, &cpus); 135 136 smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func, 137 smp_no_rendevous_barrier, arg); 138 } 139 140 static void 141 dtrace_sync_func(void) 142 { 143 } 144 145 void 146 dtrace_sync(void) 147 { 148 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 149 } 150 151 /* 152 * DTrace needs a high resolution time function which can 153 * be called from a probe context and guaranteed not to have 154 * instrumented with probes itself. 155 * 156 * Returns nanoseconds since boot. 157 */ 158 uint64_t 159 dtrace_gethrtime() 160 { 161 struct timespec curtime; 162 163 nanouptime(&curtime); 164 165 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); 166 167 } 168 169 uint64_t 170 dtrace_gethrestime(void) 171 { 172 struct timespec curtime; 173 174 getnanotime(&curtime); 175 176 return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); 177 } 178 179 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ 180 int 181 dtrace_trap(struct trapframe *frame, u_int type) 182 { 183 /* 184 * A trap can occur while DTrace executes a probe. Before 185 * executing the probe, DTrace blocks re-scheduling and sets 186 * a flag in it's per-cpu flags to indicate that it doesn't 187 * want to fault. On returning from the probe, the no-fault 188 * flag is cleared and finally re-scheduling is enabled. 189 * 190 * Check if DTrace has enabled 'no-fault' mode: 191 * 192 */ 193 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 194 /* 195 * There are only a couple of trap types that are expected. 196 * All the rest will be handled in the usual way. 197 */ 198 switch (type) { 199 /* Page fault. */ 200 case FAULT_ALIGN: 201 /* Flag a bad address. */ 202 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 203 cpu_core[curcpu].cpuc_dtrace_illval = 0; 204 205 /* 206 * Offset the instruction pointer to the instruction 207 * following the one causing the fault. 208 */ 209 frame->tf_pc += sizeof(int); 210 return (1); 211 default: 212 /* Handle all other traps in the usual way. */ 213 break; 214 } 215 } 216 217 /* Handle the trap in the usual way. */ 218 return (0); 219 } 220 221 void 222 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which, 223 int fault, int fltoffs, uintptr_t illval) 224 { 225 226 dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state, 227 (uintptr_t)epid, 228 (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs); 229 } 230 231 static int 232 dtrace_invop_start(struct trapframe *frame) 233 { 234 printf("IMPLEMENT ME: %s\n", __func__); 235 switch (dtrace_invop(frame->tf_pc, (uintptr_t *)frame, frame->tf_pc)) { 236 case DTRACE_INVOP_PUSHM: 237 // TODO: 238 break; 239 case DTRACE_INVOP_POPM: 240 // TODO: 241 break; 242 case DTRACE_INVOP_B: 243 // TODO 244 break; 245 default: 246 return (-1); 247 break; 248 } 249 250 return (0); 251 } 252 253 void dtrace_invop_init(void) 254 { 255 dtrace_invop_jump_addr = dtrace_invop_start; 256 } 257 258 void dtrace_invop_uninit(void) 259 { 260 dtrace_invop_jump_addr = 0; 261 } 262