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 /* 31 * Copyright (c) 2011, Joyent, Inc. All rights reserved. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/types.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/kmem.h> 40 #include <sys/smp.h> 41 #include <sys/dtrace_impl.h> 42 #include <sys/dtrace_bsd.h> 43 #include <machine/clock.h> 44 #include <machine/frame.h> 45 #include <vm/pmap.h> 46 47 extern void dtrace_getnanotime(struct timespec *tsp); 48 49 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 50 51 typedef struct dtrace_invop_hdlr { 52 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); 53 struct dtrace_invop_hdlr *dtih_next; 54 } dtrace_invop_hdlr_t; 55 56 dtrace_invop_hdlr_t *dtrace_invop_hdlr; 57 58 int 59 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 60 { 61 dtrace_invop_hdlr_t *hdlr; 62 int rval; 63 64 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 65 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 66 return (rval); 67 68 return (0); 69 } 70 71 void 72 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 73 { 74 dtrace_invop_hdlr_t *hdlr; 75 76 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 77 hdlr->dtih_func = func; 78 hdlr->dtih_next = dtrace_invop_hdlr; 79 dtrace_invop_hdlr = hdlr; 80 } 81 82 void 83 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 84 { 85 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 86 87 for (;;) { 88 if (hdlr == NULL) 89 panic("attempt to remove non-existent invop handler"); 90 91 if (hdlr->dtih_func == func) 92 break; 93 94 prev = hdlr; 95 hdlr = hdlr->dtih_next; 96 } 97 98 if (prev == NULL) { 99 ASSERT(dtrace_invop_hdlr == hdlr); 100 dtrace_invop_hdlr = hdlr->dtih_next; 101 } else { 102 ASSERT(dtrace_invop_hdlr != hdlr); 103 prev->dtih_next = hdlr->dtih_next; 104 } 105 106 kmem_free(hdlr, 0); 107 } 108 109 /*ARGSUSED*/ 110 void 111 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 112 { 113 (*func)(0, (uintptr_t) addr_PTmap); 114 } 115 116 void 117 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 118 { 119 cpuset_t cpus; 120 121 if (cpu == DTRACE_CPUALL) 122 cpus = all_cpus; 123 else 124 CPU_SETOF(cpu, &cpus); 125 126 smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func, 127 smp_no_rendevous_barrier, arg); 128 } 129 130 static void 131 dtrace_sync_func(void) 132 { 133 } 134 135 void 136 dtrace_sync(void) 137 { 138 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 139 } 140 141 #ifdef notyet 142 void 143 dtrace_safe_synchronous_signal(void) 144 { 145 kthread_t *t = curthread; 146 struct regs *rp = lwptoregs(ttolwp(t)); 147 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 148 149 ASSERT(t->t_dtrace_on); 150 151 /* 152 * If we're not in the range of scratch addresses, we're not actually 153 * tracing user instructions so turn off the flags. If the instruction 154 * we copied out caused a synchonous trap, reset the pc back to its 155 * original value and turn off the flags. 156 */ 157 if (rp->r_pc < t->t_dtrace_scrpc || 158 rp->r_pc > t->t_dtrace_astpc + isz) { 159 t->t_dtrace_ft = 0; 160 } else if (rp->r_pc == t->t_dtrace_scrpc || 161 rp->r_pc == t->t_dtrace_astpc) { 162 rp->r_pc = t->t_dtrace_pc; 163 t->t_dtrace_ft = 0; 164 } 165 } 166 167 int 168 dtrace_safe_defer_signal(void) 169 { 170 kthread_t *t = curthread; 171 struct regs *rp = lwptoregs(ttolwp(t)); 172 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 173 174 ASSERT(t->t_dtrace_on); 175 176 /* 177 * If we're not in the range of scratch addresses, we're not actually 178 * tracing user instructions so turn off the flags. 179 */ 180 if (rp->r_pc < t->t_dtrace_scrpc || 181 rp->r_pc > t->t_dtrace_astpc + isz) { 182 t->t_dtrace_ft = 0; 183 return (0); 184 } 185 186 /* 187 * If we have executed the original instruction, but we have performed 188 * neither the jmp back to t->t_dtrace_npc nor the clean up of any 189 * registers used to emulate %rip-relative instructions in 64-bit mode, 190 * we'll save ourselves some effort by doing that here and taking the 191 * signal right away. We detect this condition by seeing if the program 192 * counter is the range [scrpc + isz, astpc). 193 */ 194 if (rp->r_pc >= t->t_dtrace_scrpc + isz && 195 rp->r_pc < t->t_dtrace_astpc) { 196 #ifdef __amd64 197 /* 198 * If there is a scratch register and we're on the 199 * instruction immediately after the modified instruction, 200 * restore the value of that scratch register. 201 */ 202 if (t->t_dtrace_reg != 0 && 203 rp->r_pc == t->t_dtrace_scrpc + isz) { 204 switch (t->t_dtrace_reg) { 205 case REG_RAX: 206 rp->r_rax = t->t_dtrace_regv; 207 break; 208 case REG_RCX: 209 rp->r_rcx = t->t_dtrace_regv; 210 break; 211 case REG_R8: 212 rp->r_r8 = t->t_dtrace_regv; 213 break; 214 case REG_R9: 215 rp->r_r9 = t->t_dtrace_regv; 216 break; 217 } 218 } 219 #endif 220 rp->r_pc = t->t_dtrace_npc; 221 t->t_dtrace_ft = 0; 222 return (0); 223 } 224 225 /* 226 * Otherwise, make sure we'll return to the kernel after executing 227 * the copied out instruction and defer the signal. 228 */ 229 if (!t->t_dtrace_step) { 230 ASSERT(rp->r_pc < t->t_dtrace_astpc); 231 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 232 t->t_dtrace_step = 1; 233 } 234 235 t->t_dtrace_ast = 1; 236 237 return (1); 238 } 239 #endif 240 241 static int64_t tgt_cpu_tsc; 242 static int64_t hst_cpu_tsc; 243 static int64_t tsc_skew[MAXCPU]; 244 static uint64_t nsec_scale; 245 246 /* See below for the explanation of this macro. */ 247 #define SCALE_SHIFT 28 248 249 static void 250 dtrace_gethrtime_init_cpu(void *arg) 251 { 252 uintptr_t cpu = (uintptr_t) arg; 253 254 if (cpu == curcpu) 255 tgt_cpu_tsc = rdtsc(); 256 else 257 hst_cpu_tsc = rdtsc(); 258 } 259 260 #ifdef EARLY_AP_STARTUP 261 static void 262 dtrace_gethrtime_init(void *arg) 263 { 264 struct pcpu *pc; 265 uint64_t tsc_f; 266 cpuset_t map; 267 int i; 268 #else 269 /* 270 * Get the frequency and scale factor as early as possible so that they can be 271 * used for boot-time tracing. 272 */ 273 static void 274 dtrace_gethrtime_init_early(void *arg) 275 { 276 uint64_t tsc_f; 277 #endif 278 279 /* 280 * Get TSC frequency known at this moment. 281 * This should be constant if TSC is invariant. 282 * Otherwise tick->time conversion will be inaccurate, but 283 * will preserve monotonic property of TSC. 284 */ 285 tsc_f = atomic_load_acq_64(&tsc_freq); 286 287 /* 288 * The following line checks that nsec_scale calculated below 289 * doesn't overflow 32-bit unsigned integer, so that it can multiply 290 * another 32-bit integer without overflowing 64-bit. 291 * Thus minimum supported TSC frequency is 62.5MHz. 292 */ 293 KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), 294 ("TSC frequency is too low")); 295 296 /* 297 * We scale up NANOSEC/tsc_f ratio to preserve as much precision 298 * as possible. 299 * 2^28 factor was chosen quite arbitrarily from practical 300 * considerations: 301 * - it supports TSC frequencies as low as 62.5MHz (see above); 302 * - it provides quite good precision (e < 0.01%) up to THz 303 * (terahertz) values; 304 */ 305 nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 306 #ifndef EARLY_AP_STARTUP 307 } 308 SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY, 309 dtrace_gethrtime_init_early, NULL); 310 311 static void 312 dtrace_gethrtime_init(void *arg) 313 { 314 struct pcpu *pc; 315 cpuset_t map; 316 int i; 317 #endif 318 319 /* The current CPU is the reference one. */ 320 sched_pin(); 321 tsc_skew[curcpu] = 0; 322 CPU_FOREACH(i) { 323 if (i == curcpu) 324 continue; 325 326 pc = pcpu_find(i); 327 CPU_SETOF(PCPU_GET(cpuid), &map); 328 CPU_SET(pc->pc_cpuid, &map); 329 330 smp_rendezvous_cpus(map, NULL, 331 dtrace_gethrtime_init_cpu, 332 smp_no_rendevous_barrier, (void *)(uintptr_t) i); 333 334 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 335 } 336 sched_unpin(); 337 } 338 #ifdef EARLY_AP_STARTUP 339 SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, 340 dtrace_gethrtime_init, NULL); 341 #else 342 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, 343 NULL); 344 #endif 345 346 /* 347 * DTrace needs a high resolution time function which can 348 * be called from a probe context and guaranteed not to have 349 * instrumented with probes itself. 350 * 351 * Returns nanoseconds since boot. 352 */ 353 uint64_t 354 dtrace_gethrtime() 355 { 356 uint64_t tsc; 357 uint32_t lo; 358 uint32_t hi; 359 360 /* 361 * We split TSC value into lower and higher 32-bit halves and separately 362 * scale them with nsec_scale, then we scale them down by 2^28 363 * (see nsec_scale calculations) taking into account 32-bit shift of 364 * the higher half and finally add. 365 */ 366 tsc = rdtsc() - tsc_skew[curcpu]; 367 lo = tsc; 368 hi = tsc >> 32; 369 return (((lo * nsec_scale) >> SCALE_SHIFT) + 370 ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 371 } 372 373 uint64_t 374 dtrace_gethrestime(void) 375 { 376 struct timespec current_time; 377 378 dtrace_getnanotime(¤t_time); 379 380 return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 381 } 382 383 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */ 384 int 385 dtrace_trap(struct trapframe *frame, u_int type) 386 { 387 /* 388 * A trap can occur while DTrace executes a probe. Before 389 * executing the probe, DTrace blocks re-scheduling and sets 390 * a flag in its per-cpu flags to indicate that it doesn't 391 * want to fault. On returning from the probe, the no-fault 392 * flag is cleared and finally re-scheduling is enabled. 393 * 394 * Check if DTrace has enabled 'no-fault' mode: 395 */ 396 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 397 /* 398 * There are only a couple of trap types that are expected. 399 * All the rest will be handled in the usual way. 400 */ 401 switch (type) { 402 /* General protection fault. */ 403 case T_PROTFLT: 404 /* Flag an illegal operation. */ 405 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 406 407 /* 408 * Offset the instruction pointer to the instruction 409 * following the one causing the fault. 410 */ 411 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 412 return (1); 413 /* Page fault. */ 414 case T_PAGEFLT: 415 /* Flag a bad address. */ 416 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 417 cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr; 418 419 /* 420 * Offset the instruction pointer to the instruction 421 * following the one causing the fault. 422 */ 423 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 424 return (1); 425 default: 426 /* Handle all other traps in the usual way. */ 427 break; 428 } 429 } 430 431 /* Handle the trap in the usual way. */ 432 return (0); 433 } 434