1 /* $NetBSD: dtrace_subr.c,v 1.11 2018/06/04 21:35:29 christos Exp $ */ 2 3 /* 4 * CDDL HEADER START 5 * 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License, Version 1.0 only 8 * (the "License"). You may not use this file except in compliance 9 * with the License. 10 * 11 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 12 * or http://www.opensolaris.org/os/licensing. 13 * See the License for the specific language governing permissions 14 * and limitations under the License. 15 * 16 * When distributing Covered Code, include this CDDL HEADER in each 17 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 18 * If applicable, add the following below this CDDL HEADER, with the 19 * fields enclosed by brackets "[]" replaced with your own identifying 20 * information: Portions Copyright [yyyy] [name of copyright owner] 21 * 22 * CDDL HEADER END 23 * 24 * $FreeBSD: head/sys/cddl/dev/dtrace/i386/dtrace_subr.c 313850 2017-02-17 03:27:20Z markj $ 25 * 26 */ 27 /* 28 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 29 * Use is subject to license terms. 30 */ 31 32 /* 33 * Copyright (c) 2011, Joyent, Inc. All rights reserved. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/types.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/kmem.h> 42 #include <sys/xcall.h> 43 #include <sys/cpu.h> 44 #include <sys/cpuvar.h> 45 #include <sys/dtrace_impl.h> 46 #include <sys/dtrace_bsd.h> 47 #include <machine/cpu.h> 48 #include <machine/clock.h> 49 #include <machine/frame.h> 50 #include <uvm/uvm_pglist.h> 51 #include <uvm/uvm_prot.h> 52 #include <uvm/uvm_pmap.h> 53 54 #include <x86/include/cpu_counter.h> 55 56 extern uintptr_t kernelbase; 57 58 extern void dtrace_getnanotime(struct timespec *tsp); 59 60 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 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 void dtrace_gethrtime_init(void *arg); 70 71 int 72 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 73 { 74 dtrace_invop_hdlr_t *hdlr; 75 int rval; 76 77 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 78 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 79 return (rval); 80 81 return (0); 82 } 83 84 void 85 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 86 { 87 dtrace_invop_hdlr_t *hdlr; 88 89 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 90 hdlr->dtih_func = func; 91 hdlr->dtih_next = dtrace_invop_hdlr; 92 dtrace_invop_hdlr = hdlr; 93 } 94 95 void 96 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 97 { 98 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 99 100 for (;;) { 101 if (hdlr == NULL) 102 panic("attempt to remove non-existent invop handler"); 103 104 if (hdlr->dtih_func == func) 105 break; 106 107 prev = hdlr; 108 hdlr = hdlr->dtih_next; 109 } 110 111 if (prev == NULL) { 112 ASSERT(dtrace_invop_hdlr == hdlr); 113 dtrace_invop_hdlr = hdlr->dtih_next; 114 } else { 115 ASSERT(dtrace_invop_hdlr != hdlr); 116 prev->dtih_next = hdlr->dtih_next; 117 } 118 119 kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t)); 120 } 121 122 void 123 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 124 { 125 (*func)(0, kernelbase); 126 } 127 128 static void 129 xcall_func(void *arg0, void *arg1) 130 { 131 dtrace_xcall_t func = arg0; 132 133 (*func)(arg1); 134 } 135 136 void 137 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 138 { 139 uint64_t where; 140 141 if (cpu == DTRACE_CPUALL) { 142 where = xc_broadcast(0, xcall_func, func, arg); 143 } else { 144 struct cpu_info *cinfo = cpu_lookup(cpu); 145 146 KASSERT(cinfo != NULL); 147 where = xc_unicast(0, xcall_func, func, arg, cinfo); 148 } 149 xc_wait(where); 150 151 /* XXX Q. Do we really need the other cpus to wait also? 152 * (see solaris:xc_sync()) 153 */ 154 } 155 156 static void 157 dtrace_sync_func(void) 158 { 159 } 160 161 void 162 dtrace_sync(void) 163 { 164 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 165 } 166 167 #ifdef notyet 168 void 169 dtrace_safe_synchronous_signal(void) 170 { 171 kthread_t *t = curthread; 172 struct regs *rp = lwptoregs(ttolwp(t)); 173 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 174 175 ASSERT(t->t_dtrace_on); 176 177 /* 178 * If we're not in the range of scratch addresses, we're not actually 179 * tracing user instructions so turn off the flags. If the instruction 180 * we copied out caused a synchonous trap, reset the pc back to its 181 * original value and turn off the flags. 182 */ 183 if (rp->r_pc < t->t_dtrace_scrpc || 184 rp->r_pc > t->t_dtrace_astpc + isz) { 185 t->t_dtrace_ft = 0; 186 } else if (rp->r_pc == t->t_dtrace_scrpc || 187 rp->r_pc == t->t_dtrace_astpc) { 188 rp->r_pc = t->t_dtrace_pc; 189 t->t_dtrace_ft = 0; 190 } 191 } 192 193 int 194 dtrace_safe_defer_signal(void) 195 { 196 kthread_t *t = curthread; 197 struct regs *rp = lwptoregs(ttolwp(t)); 198 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 199 200 ASSERT(t->t_dtrace_on); 201 202 /* 203 * If we're not in the range of scratch addresses, we're not actually 204 * tracing user instructions so turn off the flags. 205 */ 206 if (rp->r_pc < t->t_dtrace_scrpc || 207 rp->r_pc > t->t_dtrace_astpc + isz) { 208 t->t_dtrace_ft = 0; 209 return (0); 210 } 211 212 /* 213 * If we have executed the original instruction, but we have performed 214 * neither the jmp back to t->t_dtrace_npc nor the clean up of any 215 * registers used to emulate %rip-relative instructions in 64-bit mode, 216 * we'll save ourselves some effort by doing that here and taking the 217 * signal right away. We detect this condition by seeing if the program 218 * counter is the range [scrpc + isz, astpc). 219 */ 220 if (rp->r_pc >= t->t_dtrace_scrpc + isz && 221 rp->r_pc < t->t_dtrace_astpc) { 222 #ifdef __amd64 223 /* 224 * If there is a scratch register and we're on the 225 * instruction immediately after the modified instruction, 226 * restore the value of that scratch register. 227 */ 228 if (t->t_dtrace_reg != 0 && 229 rp->r_pc == t->t_dtrace_scrpc + isz) { 230 switch (t->t_dtrace_reg) { 231 case REG_RAX: 232 rp->r_rax = t->t_dtrace_regv; 233 break; 234 case REG_RCX: 235 rp->r_rcx = t->t_dtrace_regv; 236 break; 237 case REG_R8: 238 rp->r_r8 = t->t_dtrace_regv; 239 break; 240 case REG_R9: 241 rp->r_r9 = t->t_dtrace_regv; 242 break; 243 } 244 } 245 #endif 246 rp->r_pc = t->t_dtrace_npc; 247 t->t_dtrace_ft = 0; 248 return (0); 249 } 250 251 /* 252 * Otherwise, make sure we'll return to the kernel after executing 253 * the copied out instruction and defer the signal. 254 */ 255 if (!t->t_dtrace_step) { 256 ASSERT(rp->r_pc < t->t_dtrace_astpc); 257 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 258 t->t_dtrace_step = 1; 259 } 260 261 t->t_dtrace_ast = 1; 262 263 return (1); 264 } 265 #endif 266 267 static int64_t tgt_cpu_tsc; 268 static int64_t hst_cpu_tsc; 269 static int64_t tsc_skew[MAXCPUS]; 270 static uint64_t nsec_scale; 271 272 /* See below for the explanation of this macro. */ 273 #define SCALE_SHIFT 28 274 275 static __inline uint64_t 276 dtrace_rdtsc(void) 277 { 278 uint64_t rv; 279 280 __asm __volatile("rdtsc" : "=A" (rv)); 281 return (rv); 282 } 283 284 static void 285 dtrace_gethrtime_init_cpu(void *arg) 286 { 287 uintptr_t cpu = (uintptr_t) arg; 288 289 if (cpu == cpu_number()) 290 tgt_cpu_tsc = dtrace_rdtsc(); 291 else 292 hst_cpu_tsc = dtrace_rdtsc(); 293 } 294 295 void 296 dtrace_gethrtime_init(void *arg) 297 { 298 uint64_t tsc_f; 299 CPU_INFO_ITERATOR cpuind; 300 struct cpu_info *cinfo = curcpu(); 301 cpuid_t cur_cpuid = cpu_number(); /* current cpu id */ 302 303 /* 304 * Get TSC frequency known at this moment. 305 * This should be constant if TSC is invariant. 306 * Otherwise tick->time conversion will be inaccurate, but 307 * will preserve monotonic property of TSC. 308 */ 309 tsc_f = cpu_frequency(cinfo); 310 311 /* 312 * The following line checks that nsec_scale calculated below 313 * doesn't overflow 32-bit unsigned integer, so that it can multiply 314 * another 32-bit integer without overflowing 64-bit. 315 * Thus minimum supported TSC frequency is 62.5MHz. 316 */ 317 KASSERTMSG(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), 318 "TSC frequency is too low"); 319 320 /* 321 * We scale up NANOSEC/tsc_f ratio to preserve as much precision 322 * as possible. 323 * 2^28 factor was chosen quite arbitrarily from practical 324 * considerations: 325 * - it supports TSC frequencies as low as 62.5MHz (see above); 326 * - it provides quite good precision (e < 0.01%) up to THz 327 * (terahertz) values; 328 */ 329 nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 330 331 /* The current CPU is the reference one. */ 332 tsc_skew[cur_cpuid] = 0; 333 334 for (CPU_INFO_FOREACH(cpuind, cinfo)) { 335 /* use skew relative to cpu 0 */ 336 tsc_skew[cpu_index(cinfo)] = cinfo->ci_data.cpu_cc_skew; 337 } 338 339 /* Already handled in x86/tsc.c for ci_data.cpu_cc_skew */ 340 #if 0 341 /* The current CPU is the reference one. */ 342 sched_pin(); 343 tsc_skew[curcpu] = 0; 344 CPU_FOREACH(i) { 345 if (i == curcpu) 346 continue; 347 348 pc = pcpu_find(i); 349 CPU_SETOF(PCPU_GET(cpuid), &map); 350 CPU_SET(pc->pc_cpuid, &map); 351 352 smp_rendezvous_cpus(map, NULL, 353 dtrace_gethrtime_init_cpu, 354 smp_no_rendevous_barrier, (void *)(uintptr_t) i); 355 356 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 357 } 358 sched_unpin(); 359 #endif 360 } 361 362 #ifdef __FreeBSD__ 363 #ifdef EARLY_AP_STARTUP 364 SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, 365 dtrace_gethrtime_init, NULL); 366 #else 367 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, 368 NULL); 369 #endif 370 #endif 371 372 /* 373 * DTrace needs a high resolution time function which can 374 * be called from a probe context and guaranteed not to have 375 * instrumented with probes itself. 376 * 377 * Returns nanoseconds since boot. 378 */ 379 uint64_t 380 dtrace_gethrtime() 381 { 382 uint64_t tsc; 383 uint32_t lo; 384 uint32_t hi; 385 386 /* 387 * We split TSC value into lower and higher 32-bit halves and separately 388 * scale them with nsec_scale, then we scale them down by 2^28 389 * (see nsec_scale calculations) taking into account 32-bit shift of 390 * the higher half and finally add. 391 */ 392 tsc = dtrace_rdtsc() + tsc_skew[cpu_number()]; 393 lo = tsc; 394 hi = tsc >> 32; 395 return (((lo * nsec_scale) >> SCALE_SHIFT) + 396 ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 397 } 398 399 uint64_t 400 dtrace_gethrestime(void) 401 { 402 struct timespec current_time; 403 404 dtrace_getnanotime(¤t_time); 405 406 return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 407 } 408 409 /* Function to handle DTrace traps during probes. See i386/i386/trap.c */ 410 int 411 dtrace_trap(struct trapframe *frame, u_int type) 412 { 413 bool nofault; 414 cpuid_t cpuid = cpu_number(); /* current cpu id */ 415 416 /* 417 * A trap can occur while DTrace executes a probe. Before 418 * executing the probe, DTrace blocks re-scheduling and sets 419 * a flag in its per-cpu flags to indicate that it doesn't 420 * want to fault. On returning from the probe, the no-fault 421 * flag is cleared and finally re-scheduling is enabled. 422 * 423 * Check if DTrace has enabled 'no-fault' mode: 424 */ 425 nofault = (cpu_core[cpuid].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0; 426 if (nofault) { 427 KASSERTMSG((read_eflags() & PSL_I) == 0, "interrupts enabled"); 428 429 /* 430 * There are only a couple of trap types that are expected. 431 * All the rest will be handled in the usual way. 432 */ 433 switch (type) { 434 /* General protection fault. */ 435 case T_PROTFLT: 436 /* Flag an illegal operation. */ 437 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 438 439 /* 440 * Offset the instruction pointer to the instruction 441 * following the one causing the fault. 442 */ 443 frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip); 444 return (1); 445 /* Page fault. */ 446 case T_PAGEFLT: 447 /* Flag a bad address. */ 448 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 449 cpu_core[cpuid].cpuc_dtrace_illval = rcr2(); 450 451 /* 452 * Offset the instruction pointer to the instruction 453 * following the one causing the fault. 454 */ 455 frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip); 456 return (1); 457 default: 458 /* Handle all other traps in the usual way. */ 459 break; 460 } 461 } 462 463 /* Handle the trap in the usual way. */ 464 return (0); 465 } 466