1 /* $NetBSD: dtrace_subr.c,v 1.7 2012/06/16 17:31:47 chs 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: src/sys/cddl/dev/dtrace/amd64/dtrace_subr.c,v 1.3.2.1 2009/08/03 08:13:06 kensmith Exp $ 25 * 26 */ 27 /* 28 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 29 * Use is subject to license terms. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/types.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/kmem.h> 38 #include <sys/xcall.h> 39 #include <sys/cpu.h> 40 #include <sys/cpuvar.h> 41 //#include <sys/smp.h> 42 #include <sys/dtrace_impl.h> 43 #include <sys/dtrace_bsd.h> 44 #include <machine/frame.h> 45 #include <machine/cpu_counter.h> 46 #include <uvm/uvm_pglist.h> 47 #include <uvm/uvm_prot.h> 48 #include <uvm/uvm_pmap.h> 49 50 extern uintptr_t kernelbase; 51 extern uintptr_t dtrace_in_probe_addr; 52 extern int dtrace_in_probe; 53 54 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t); 55 56 typedef struct dtrace_invop_hdlr { 57 int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t); 58 struct dtrace_invop_hdlr *dtih_next; 59 } dtrace_invop_hdlr_t; 60 61 dtrace_invop_hdlr_t *dtrace_invop_hdlr; 62 void dtrace_gethrtime_init(void *); 63 64 65 int 66 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax) 67 { 68 dtrace_invop_hdlr_t *hdlr; 69 int rval; 70 71 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 72 if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) 73 return (rval); 74 75 return (0); 76 } 77 78 void 79 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, 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, uintptr_t *, uintptr_t)) 91 { 92 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 93 94 for (;;) { 95 if (hdlr == NULL) 96 panic("attempt to remove non-existent invop handler"); 97 98 if (hdlr->dtih_func == func) 99 break; 100 101 prev = hdlr; 102 hdlr = hdlr->dtih_next; 103 } 104 105 if (prev == NULL) { 106 ASSERT(dtrace_invop_hdlr == hdlr); 107 dtrace_invop_hdlr = hdlr->dtih_next; 108 } else { 109 ASSERT(dtrace_invop_hdlr != hdlr); 110 prev->dtih_next = hdlr->dtih_next; 111 } 112 113 kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t)); 114 } 115 116 /*ARGSUSED*/ 117 void 118 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 119 { 120 (*func)(0, kernelbase); 121 } 122 123 static void 124 xcall_func(void *arg0, void *arg1) 125 { 126 dtrace_xcall_t func = arg0; 127 128 (*func)(arg1); 129 } 130 131 void 132 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 133 { 134 uint64_t where; 135 136 if (cpu == DTRACE_CPUALL) { 137 where = xc_broadcast(0, xcall_func, func, arg); 138 } else { 139 struct cpu_info *cinfo = cpu_lookup(cpu); 140 141 KASSERT(cinfo != NULL); 142 where = xc_unicast(0, xcall_func, func, arg, cinfo); 143 } 144 xc_wait(where); 145 146 /* XXX Q. Do we really need the other cpus to wait also? 147 * (see solaris:xc_sync()) 148 */ 149 } 150 151 static void 152 dtrace_sync_func(void) 153 { 154 } 155 156 void 157 dtrace_sync(void) 158 { 159 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 160 } 161 162 #ifdef notyet 163 int (*dtrace_fasttrap_probe_ptr)(struct regs *); 164 int (*dtrace_pid_probe_ptr)(struct regs *); 165 int (*dtrace_return_probe_ptr)(struct regs *); 166 167 void 168 dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid) 169 { 170 krwlock_t *rwp; 171 proc_t *p = curproc; 172 extern void trap(struct regs *, caddr_t, processorid_t); 173 174 if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) { 175 if (curthread->t_cred != p->p_cred) { 176 cred_t *oldcred = curthread->t_cred; 177 /* 178 * DTrace accesses t_cred in probe context. t_cred 179 * must always be either NULL, or point to a valid, 180 * allocated cred structure. 181 */ 182 curthread->t_cred = crgetcred(); 183 crfree(oldcred); 184 } 185 } 186 187 if (rp->r_trapno == T_DTRACE_RET) { 188 uint8_t step = curthread->t_dtrace_step; 189 uint8_t ret = curthread->t_dtrace_ret; 190 uintptr_t npc = curthread->t_dtrace_npc; 191 192 if (curthread->t_dtrace_ast) { 193 aston(curthread); 194 curthread->t_sig_check = 1; 195 } 196 197 /* 198 * Clear all user tracing flags. 199 */ 200 curthread->t_dtrace_ft = 0; 201 202 /* 203 * If we weren't expecting to take a return probe trap, kill 204 * the process as though it had just executed an unassigned 205 * trap instruction. 206 */ 207 if (step == 0) { 208 tsignal(curthread, SIGILL); 209 return; 210 } 211 212 /* 213 * If we hit this trap unrelated to a return probe, we're 214 * just here to reset the AST flag since we deferred a signal 215 * until after we logically single-stepped the instruction we 216 * copied out. 217 */ 218 if (ret == 0) { 219 rp->r_pc = npc; 220 return; 221 } 222 223 /* 224 * We need to wait until after we've called the 225 * dtrace_return_probe_ptr function pointer to set %pc. 226 */ 227 rwp = &CPU->cpu_ft_lock; 228 rw_enter(rwp, RW_READER); 229 if (dtrace_return_probe_ptr != NULL) 230 (void) (*dtrace_return_probe_ptr)(rp); 231 rw_exit(rwp); 232 rp->r_pc = npc; 233 234 } else if (rp->r_trapno == T_DTRACE_PROBE) { 235 rwp = &CPU->cpu_ft_lock; 236 rw_enter(rwp, RW_READER); 237 if (dtrace_fasttrap_probe_ptr != NULL) 238 (void) (*dtrace_fasttrap_probe_ptr)(rp); 239 rw_exit(rwp); 240 241 } else if (rp->r_trapno == T_BPTFLT) { 242 uint8_t instr; 243 rwp = &CPU->cpu_ft_lock; 244 245 /* 246 * The DTrace fasttrap provider uses the breakpoint trap 247 * (int 3). We let DTrace take the first crack at handling 248 * this trap; if it's not a probe that DTrace knowns about, 249 * we call into the trap() routine to handle it like a 250 * breakpoint placed by a conventional debugger. 251 */ 252 rw_enter(rwp, RW_READER); 253 if (dtrace_pid_probe_ptr != NULL && 254 (*dtrace_pid_probe_ptr)(rp) == 0) { 255 rw_exit(rwp); 256 return; 257 } 258 rw_exit(rwp); 259 260 /* 261 * If the instruction that caused the breakpoint trap doesn't 262 * look like an int 3 anymore, it may be that this tracepoint 263 * was removed just after the user thread executed it. In 264 * that case, return to user land to retry the instuction. 265 */ 266 if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 && 267 instr != FASTTRAP_INSTR) { 268 rp->r_pc--; 269 return; 270 } 271 272 trap(rp, addr, cpuid); 273 274 } else { 275 trap(rp, addr, cpuid); 276 } 277 } 278 279 void 280 dtrace_safe_synchronous_signal(void) 281 { 282 kthread_t *t = curthread; 283 struct regs *rp = lwptoregs(ttolwp(t)); 284 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 285 286 ASSERT(t->t_dtrace_on); 287 288 /* 289 * If we're not in the range of scratch addresses, we're not actually 290 * tracing user instructions so turn off the flags. If the instruction 291 * we copied out caused a synchonous trap, reset the pc back to its 292 * original value and turn off the flags. 293 */ 294 if (rp->r_pc < t->t_dtrace_scrpc || 295 rp->r_pc > t->t_dtrace_astpc + isz) { 296 t->t_dtrace_ft = 0; 297 } else if (rp->r_pc == t->t_dtrace_scrpc || 298 rp->r_pc == t->t_dtrace_astpc) { 299 rp->r_pc = t->t_dtrace_pc; 300 t->t_dtrace_ft = 0; 301 } 302 } 303 304 int 305 dtrace_safe_defer_signal(void) 306 { 307 kthread_t *t = curthread; 308 struct regs *rp = lwptoregs(ttolwp(t)); 309 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 310 311 ASSERT(t->t_dtrace_on); 312 313 /* 314 * If we're not in the range of scratch addresses, we're not actually 315 * tracing user instructions so turn off the flags. 316 */ 317 if (rp->r_pc < t->t_dtrace_scrpc || 318 rp->r_pc > t->t_dtrace_astpc + isz) { 319 t->t_dtrace_ft = 0; 320 return (0); 321 } 322 323 /* 324 * If we've executed the original instruction, but haven't performed 325 * the jmp back to t->t_dtrace_npc or the clean up of any registers 326 * used to emulate %rip-relative instructions in 64-bit mode, do that 327 * here and take the signal right away. We detect this condition by 328 * seeing if the program counter is the range [scrpc + isz, astpc). 329 */ 330 if (t->t_dtrace_astpc - rp->r_pc < 331 t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) { 332 #ifdef __amd64 333 /* 334 * If there is a scratch register and we're on the 335 * instruction immediately after the modified instruction, 336 * restore the value of that scratch register. 337 */ 338 if (t->t_dtrace_reg != 0 && 339 rp->r_pc == t->t_dtrace_scrpc + isz) { 340 switch (t->t_dtrace_reg) { 341 case REG_RAX: 342 rp->r_rax = t->t_dtrace_regv; 343 break; 344 case REG_RCX: 345 rp->r_rcx = t->t_dtrace_regv; 346 break; 347 case REG_R8: 348 rp->r_r8 = t->t_dtrace_regv; 349 break; 350 case REG_R9: 351 rp->r_r9 = t->t_dtrace_regv; 352 break; 353 } 354 } 355 #endif 356 rp->r_pc = t->t_dtrace_npc; 357 t->t_dtrace_ft = 0; 358 return (0); 359 } 360 361 /* 362 * Otherwise, make sure we'll return to the kernel after executing 363 * the copied out instruction and defer the signal. 364 */ 365 if (!t->t_dtrace_step) { 366 ASSERT(rp->r_pc < t->t_dtrace_astpc); 367 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 368 t->t_dtrace_step = 1; 369 } 370 371 t->t_dtrace_ast = 1; 372 373 return (1); 374 } 375 #endif 376 377 #ifdef __NetBSD__ 378 static __inline uint64_t 379 dtrace_rdtsc(void) 380 { 381 uint32_t hi, lo; 382 383 __asm volatile("rdtsc" : "=d" (hi), "=a" (lo)); 384 return (((uint64_t)hi << 32) | (uint64_t) lo); 385 } 386 #define rdtsc dtrace_rdtsc 387 #endif 388 389 #ifdef notyet 390 static int64_t tgt_cpu_tsc; 391 static int64_t hst_cpu_tsc; 392 #endif 393 static int64_t tsc_skew[MAXCPUS]; 394 static uint64_t nsec_scale; 395 396 /* See below for the explanation of this macro. */ 397 #define SCALE_SHIFT 28 398 399 #ifdef notyet 400 static void 401 dtrace_gethrtime_init_sync(void *arg) 402 { 403 #ifdef CHECK_SYNC 404 /* 405 * Delay this function from returning on one 406 * of the CPUs to check that the synchronisation 407 * works. 408 */ 409 uintptr_t cpu = (uintptr_t) arg; 410 411 if (cpu == cpu_number()) { 412 int i; 413 for (i = 0; i < 1000000000; i++) 414 tgt_cpu_tsc = rdtsc(); 415 tgt_cpu_tsc = 0; 416 } 417 #endif 418 } 419 420 static void 421 dtrace_gethrtime_init_cpu(void *arg) 422 { 423 uintptr_t cpu = (uintptr_t) arg; 424 425 if (cpu == cpu_number()) 426 tgt_cpu_tsc = rdtsc(); 427 else 428 hst_cpu_tsc = rdtsc(); 429 } 430 #endif 431 432 void 433 dtrace_gethrtime_init(void *arg) 434 { 435 uint64_t tsc_f; 436 CPU_INFO_ITERATOR cpuind; 437 struct cpu_info *cinfo = curcpu(); 438 cpuid_t cur_cpuid = cpu_number(); /* current cpu id */ 439 440 /* 441 * Get TSC frequency known at this moment. 442 * This should be constant if TSC is invariant. 443 * Otherwise tick->time conversion will be inaccurate, but 444 * will preserve monotonic property of TSC. 445 */ 446 tsc_f = cpu_frequency(cinfo); 447 448 /* 449 * The following line checks that nsec_scale calculated below 450 * doesn't overflow 32-bit unsigned integer, so that it can multiply 451 * another 32-bit integer without overflowing 64-bit. 452 * Thus minimum supported TSC frequency is 62.5MHz. 453 */ 454 //KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), ("TSC frequency is too low")); 455 KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT))); 456 457 /* 458 * We scale up NANOSEC/tsc_f ratio to preserve as much precision 459 * as possible. 460 * 2^28 factor was chosen quite arbitrarily from practical 461 * considerations: 462 * - it supports TSC frequencies as low as 62.5MHz (see above); 463 * - it provides quite good precision (e < 0.01%) up to THz 464 * (terahertz) values; 465 */ 466 nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 467 468 /* The current CPU is the reference one. */ 469 tsc_skew[cur_cpuid] = 0; 470 471 for (CPU_INFO_FOREACH(cpuind, cinfo)) { 472 /* use skew relative to cpu 0 */ 473 tsc_skew[cpu_index(cinfo)] = cinfo->ci_data.cpu_cc_skew; 474 } 475 476 /* Already handled in x86/tsc.c for ci_data.cpu_cc_skew */ 477 #if 0 478 for (i = 0; i <= mp_maxid; i++) { 479 if (i == curcpu) 480 continue; 481 482 if (pcpu_find(i) == NULL) 483 continue; 484 485 map = 0; 486 map |= (1 << curcpu); 487 map |= (1 << i); 488 489 smp_rendezvous_cpus(map, dtrace_gethrtime_init_sync, 490 dtrace_gethrtime_init_cpu, 491 smp_no_rendevous_barrier, (void *)(uintptr_t) i); 492 493 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 494 } 495 #endif 496 } 497 498 /* 499 * DTrace needs a high resolution time function which can 500 * be called from a probe context and guaranteed not to have 501 * instrumented with probes itself. 502 * 503 * Returns nanoseconds since boot. 504 */ 505 uint64_t 506 dtrace_gethrtime() 507 { 508 uint64_t tsc; 509 uint32_t lo; 510 uint32_t hi; 511 512 /* 513 * We split TSC value into lower and higher 32-bit halves and separately 514 * scale them with nsec_scale, then we scale them down by 2^28 515 * (see nsec_scale calculations) taking into account 32-bit shift of 516 * the higher half and finally add. 517 */ 518 tsc = rdtsc() + tsc_skew[cpu_number()]; 519 lo = tsc; 520 hi = tsc >> 32; 521 return (((lo * nsec_scale) >> SCALE_SHIFT) + 522 ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 523 } 524 525 uint64_t 526 dtrace_gethrestime(void) 527 { 528 printf("%s(%d): XXX\n",__func__,__LINE__); 529 return (0); 530 } 531 532 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ 533 int 534 dtrace_trap(struct trapframe *frame, u_int type) 535 { 536 /* 537 * A trap can occur while DTrace executes a probe. Before 538 * executing the probe, DTrace blocks re-scheduling and sets 539 * a flag in it's per-cpu flags to indicate that it doesn't 540 * want to fault. On returning from the the probe, the no-fault 541 * flag is cleared and finally re-scheduling is enabled. 542 * 543 * Check if DTrace has enabled 'no-fault' mode: 544 * 545 */ 546 if ((cpu_core[cpu_number()].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 547 /* 548 * There are only a couple of trap types that are expected. 549 * All the rest will be handled in the usual way. 550 */ 551 switch (type) { 552 /* Privilieged instruction fault. */ 553 case T_PRIVINFLT: 554 break; 555 /* General protection fault. */ 556 case T_PROTFLT: 557 /* Flag an illegal operation. */ 558 cpu_core[cpu_number()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 559 560 /* 561 * Offset the instruction pointer to the instruction 562 * following the one causing the fault. 563 */ 564 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 565 return (1); 566 /* Page fault. */ 567 case T_PAGEFLT: 568 /* Flag a bad address. */ 569 cpu_core[cpu_number()].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 570 cpu_core[cpu_number()].cpuc_dtrace_illval = rcr2(); 571 572 /* 573 * Offset the instruction pointer to the instruction 574 * following the one causing the fault. 575 */ 576 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 577 return (1); 578 default: 579 /* Handle all other traps in the usual way. */ 580 break; 581 } 582 } 583 584 /* Handle the trap in the usual way. */ 585 return (0); 586 } 587