1179237Sjb/* 2179237Sjb * CDDL HEADER START 3179237Sjb * 4179237Sjb * The contents of this file are subject to the terms of the 5179237Sjb * Common Development and Distribution License, Version 1.0 only 6179237Sjb * (the "License"). You may not use this file except in compliance 7179237Sjb * with the License. 8179237Sjb * 9179237Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10179237Sjb * or http://www.opensolaris.org/os/licensing. 11179237Sjb * See the License for the specific language governing permissions 12179237Sjb * and limitations under the License. 13179237Sjb * 14179237Sjb * When distributing Covered Code, include this CDDL HEADER in each 15179237Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16179237Sjb * If applicable, add the following below this CDDL HEADER, with the 17179237Sjb * fields enclosed by brackets "[]" replaced with your own identifying 18179237Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 19179237Sjb * 20179237Sjb * CDDL HEADER END 21179237Sjb * 22179237Sjb * $FreeBSD: releng/11.0/sys/cddl/dev/dtrace/amd64/dtrace_subr.c 299746 2016-05-14 18:22:52Z jhb $ 23179237Sjb * 24179237Sjb */ 25179237Sjb/* 26179237Sjb * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 27179237Sjb * Use is subject to license terms. 28179237Sjb */ 29179237Sjb 30236567Sgnn/* 31236567Sgnn * Copyright (c) 2011, Joyent, Inc. All rights reserved. 32236567Sgnn */ 33236567Sgnn 34179237Sjb#include <sys/param.h> 35179237Sjb#include <sys/systm.h> 36179237Sjb#include <sys/types.h> 37179237Sjb#include <sys/kernel.h> 38179237Sjb#include <sys/malloc.h> 39179237Sjb#include <sys/kmem.h> 40179237Sjb#include <sys/smp.h> 41179237Sjb#include <sys/dtrace_impl.h> 42179237Sjb#include <sys/dtrace_bsd.h> 43179237Sjb#include <machine/clock.h> 44179237Sjb#include <machine/frame.h> 45179237Sjb#include <vm/pmap.h> 46179237Sjb 47238537Sgnnextern void dtrace_getnanotime(struct timespec *tsp); 48238537Sgnn 49298171Smarkjint dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 50179237Sjb 51179237Sjbtypedef struct dtrace_invop_hdlr { 52298171Smarkj int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); 53179237Sjb struct dtrace_invop_hdlr *dtih_next; 54179237Sjb} dtrace_invop_hdlr_t; 55179237Sjb 56179237Sjbdtrace_invop_hdlr_t *dtrace_invop_hdlr; 57179237Sjb 58179237Sjbint 59298171Smarkjdtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 60179237Sjb{ 61179237Sjb dtrace_invop_hdlr_t *hdlr; 62179237Sjb int rval; 63179237Sjb 64179237Sjb for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 65298171Smarkj if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 66179237Sjb return (rval); 67179237Sjb 68179237Sjb return (0); 69179237Sjb} 70179237Sjb 71179237Sjbvoid 72298171Smarkjdtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 73179237Sjb{ 74179237Sjb dtrace_invop_hdlr_t *hdlr; 75179237Sjb 76179237Sjb hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 77179237Sjb hdlr->dtih_func = func; 78179237Sjb hdlr->dtih_next = dtrace_invop_hdlr; 79179237Sjb dtrace_invop_hdlr = hdlr; 80179237Sjb} 81179237Sjb 82179237Sjbvoid 83298171Smarkjdtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 84179237Sjb{ 85179237Sjb dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 86179237Sjb 87179237Sjb for (;;) { 88179237Sjb if (hdlr == NULL) 89179237Sjb panic("attempt to remove non-existent invop handler"); 90179237Sjb 91179237Sjb if (hdlr->dtih_func == func) 92179237Sjb break; 93179237Sjb 94179237Sjb prev = hdlr; 95179237Sjb hdlr = hdlr->dtih_next; 96179237Sjb } 97179237Sjb 98179237Sjb if (prev == NULL) { 99179237Sjb ASSERT(dtrace_invop_hdlr == hdlr); 100179237Sjb dtrace_invop_hdlr = hdlr->dtih_next; 101179237Sjb } else { 102179237Sjb ASSERT(dtrace_invop_hdlr != hdlr); 103179237Sjb prev->dtih_next = hdlr->dtih_next; 104179237Sjb } 105179237Sjb 106179237Sjb kmem_free(hdlr, 0); 107179237Sjb} 108179237Sjb 109179237Sjb/*ARGSUSED*/ 110179237Sjbvoid 111179237Sjbdtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 112179237Sjb{ 113179237Sjb (*func)(0, (uintptr_t) addr_PTmap); 114179237Sjb} 115179237Sjb 116179237Sjbvoid 117179237Sjbdtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 118179237Sjb{ 119222813Sattilio cpuset_t cpus; 120179237Sjb 121179237Sjb if (cpu == DTRACE_CPUALL) 122179237Sjb cpus = all_cpus; 123179237Sjb else 124222813Sattilio CPU_SETOF(cpu, &cpus); 125179237Sjb 126216251Savg smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func, 127216251Savg smp_no_rendevous_barrier, arg); 128179237Sjb} 129179237Sjb 130179237Sjbstatic void 131179237Sjbdtrace_sync_func(void) 132179237Sjb{ 133179237Sjb} 134179237Sjb 135179237Sjbvoid 136179237Sjbdtrace_sync(void) 137179237Sjb{ 138179237Sjb dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 139179237Sjb} 140179237Sjb 141179237Sjb#ifdef notyet 142179237Sjbvoid 143179237Sjbdtrace_safe_synchronous_signal(void) 144179237Sjb{ 145179237Sjb kthread_t *t = curthread; 146179237Sjb struct regs *rp = lwptoregs(ttolwp(t)); 147179237Sjb size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 148179237Sjb 149179237Sjb ASSERT(t->t_dtrace_on); 150179237Sjb 151179237Sjb /* 152179237Sjb * If we're not in the range of scratch addresses, we're not actually 153179237Sjb * tracing user instructions so turn off the flags. If the instruction 154179237Sjb * we copied out caused a synchonous trap, reset the pc back to its 155179237Sjb * original value and turn off the flags. 156179237Sjb */ 157179237Sjb if (rp->r_pc < t->t_dtrace_scrpc || 158179237Sjb rp->r_pc > t->t_dtrace_astpc + isz) { 159179237Sjb t->t_dtrace_ft = 0; 160179237Sjb } else if (rp->r_pc == t->t_dtrace_scrpc || 161179237Sjb rp->r_pc == t->t_dtrace_astpc) { 162179237Sjb rp->r_pc = t->t_dtrace_pc; 163179237Sjb t->t_dtrace_ft = 0; 164179237Sjb } 165179237Sjb} 166179237Sjb 167179237Sjbint 168179237Sjbdtrace_safe_defer_signal(void) 169179237Sjb{ 170179237Sjb kthread_t *t = curthread; 171179237Sjb struct regs *rp = lwptoregs(ttolwp(t)); 172179237Sjb size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 173179237Sjb 174179237Sjb ASSERT(t->t_dtrace_on); 175179237Sjb 176179237Sjb /* 177179237Sjb * If we're not in the range of scratch addresses, we're not actually 178179237Sjb * tracing user instructions so turn off the flags. 179179237Sjb */ 180179237Sjb if (rp->r_pc < t->t_dtrace_scrpc || 181179237Sjb rp->r_pc > t->t_dtrace_astpc + isz) { 182179237Sjb t->t_dtrace_ft = 0; 183179237Sjb return (0); 184179237Sjb } 185179237Sjb 186179237Sjb /* 187236567Sgnn * If we have executed the original instruction, but we have performed 188236567Sgnn * neither the jmp back to t->t_dtrace_npc nor the clean up of any 189236567Sgnn * registers used to emulate %rip-relative instructions in 64-bit mode, 190236567Sgnn * we'll save ourselves some effort by doing that here and taking the 191236567Sgnn * signal right away. We detect this condition by seeing if the program 192236567Sgnn * counter is the range [scrpc + isz, astpc). 193179237Sjb */ 194236567Sgnn if (rp->r_pc >= t->t_dtrace_scrpc + isz && 195236567Sgnn rp->r_pc < t->t_dtrace_astpc) { 196179237Sjb#ifdef __amd64 197179237Sjb /* 198179237Sjb * If there is a scratch register and we're on the 199179237Sjb * instruction immediately after the modified instruction, 200179237Sjb * restore the value of that scratch register. 201179237Sjb */ 202179237Sjb if (t->t_dtrace_reg != 0 && 203179237Sjb rp->r_pc == t->t_dtrace_scrpc + isz) { 204179237Sjb switch (t->t_dtrace_reg) { 205179237Sjb case REG_RAX: 206179237Sjb rp->r_rax = t->t_dtrace_regv; 207179237Sjb break; 208179237Sjb case REG_RCX: 209179237Sjb rp->r_rcx = t->t_dtrace_regv; 210179237Sjb break; 211179237Sjb case REG_R8: 212179237Sjb rp->r_r8 = t->t_dtrace_regv; 213179237Sjb break; 214179237Sjb case REG_R9: 215179237Sjb rp->r_r9 = t->t_dtrace_regv; 216179237Sjb break; 217179237Sjb } 218179237Sjb } 219179237Sjb#endif 220179237Sjb rp->r_pc = t->t_dtrace_npc; 221179237Sjb t->t_dtrace_ft = 0; 222179237Sjb return (0); 223179237Sjb } 224179237Sjb 225179237Sjb /* 226179237Sjb * Otherwise, make sure we'll return to the kernel after executing 227179237Sjb * the copied out instruction and defer the signal. 228179237Sjb */ 229179237Sjb if (!t->t_dtrace_step) { 230179237Sjb ASSERT(rp->r_pc < t->t_dtrace_astpc); 231179237Sjb rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 232179237Sjb t->t_dtrace_step = 1; 233179237Sjb } 234179237Sjb 235179237Sjb t->t_dtrace_ast = 1; 236179237Sjb 237179237Sjb return (1); 238179237Sjb} 239179237Sjb#endif 240179237Sjb 241179237Sjbstatic int64_t tgt_cpu_tsc; 242179237Sjbstatic int64_t hst_cpu_tsc; 243179237Sjbstatic int64_t tsc_skew[MAXCPU]; 244195710Savgstatic uint64_t nsec_scale; 245179237Sjb 246195710Savg/* See below for the explanation of this macro. */ 247195710Savg#define SCALE_SHIFT 28 248195710Savg 249299746Sjhbstatic void 250299746Sjhbdtrace_gethrtime_init_cpu(void *arg) 251299746Sjhb{ 252299746Sjhb uintptr_t cpu = (uintptr_t) arg; 253299746Sjhb 254299746Sjhb if (cpu == curcpu) 255299746Sjhb tgt_cpu_tsc = rdtsc(); 256299746Sjhb else 257299746Sjhb hst_cpu_tsc = rdtsc(); 258299746Sjhb} 259299746Sjhb 260299746Sjhb#ifdef EARLY_AP_STARTUP 261299746Sjhbstatic void 262299746Sjhbdtrace_gethrtime_init(void *arg) 263299746Sjhb{ 264299746Sjhb struct pcpu *pc; 265299746Sjhb uint64_t tsc_f; 266299746Sjhb cpuset_t map; 267299746Sjhb int i; 268299746Sjhb#else 269297770Smarkj/* 270297770Smarkj * Get the frequency and scale factor as early as possible so that they can be 271297770Smarkj * used for boot-time tracing. 272297770Smarkj */ 273179237Sjbstatic void 274297770Smarkjdtrace_gethrtime_init_early(void *arg) 275179237Sjb{ 276195710Savg uint64_t tsc_f; 277299746Sjhb#endif 278179237Sjb 279195710Savg /* 280195710Savg * Get TSC frequency known at this moment. 281195710Savg * This should be constant if TSC is invariant. 282195710Savg * Otherwise tick->time conversion will be inaccurate, but 283195710Savg * will preserve monotonic property of TSC. 284195710Savg */ 285220433Sjkim tsc_f = atomic_load_acq_64(&tsc_freq); 286195710Savg 287195710Savg /* 288195710Savg * The following line checks that nsec_scale calculated below 289195710Savg * doesn't overflow 32-bit unsigned integer, so that it can multiply 290195710Savg * another 32-bit integer without overflowing 64-bit. 291195710Savg * Thus minimum supported TSC frequency is 62.5MHz. 292195710Savg */ 293297770Smarkj KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), 294297770Smarkj ("TSC frequency is too low")); 295195710Savg 296195710Savg /* 297195710Savg * We scale up NANOSEC/tsc_f ratio to preserve as much precision 298195710Savg * as possible. 299195710Savg * 2^28 factor was chosen quite arbitrarily from practical 300195710Savg * considerations: 301195710Savg * - it supports TSC frequencies as low as 62.5MHz (see above); 302195710Savg * - it provides quite good precision (e < 0.01%) up to THz 303195710Savg * (terahertz) values; 304195710Savg */ 305195710Savg nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 306299746Sjhb#ifndef EARLY_AP_STARTUP 307297770Smarkj} 308297770SmarkjSYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY, 309297770Smarkj dtrace_gethrtime_init_early, NULL); 310195710Savg 311297770Smarkjstatic void 312297770Smarkjdtrace_gethrtime_init(void *arg) 313297770Smarkj{ 314297770Smarkj struct pcpu *pc; 315297770Smarkj cpuset_t map; 316297770Smarkj int i; 317299746Sjhb#endif 318297770Smarkj 319179237Sjb /* The current CPU is the reference one. */ 320216250Savg sched_pin(); 321179237Sjb tsc_skew[curcpu] = 0; 322209059Sjhb CPU_FOREACH(i) { 323179237Sjb if (i == curcpu) 324179237Sjb continue; 325179237Sjb 326216250Savg pc = pcpu_find(i); 327223758Sattilio CPU_SETOF(PCPU_GET(cpuid), &map); 328223758Sattilio CPU_SET(pc->pc_cpuid, &map); 329179237Sjb 330221740Savg smp_rendezvous_cpus(map, NULL, 331179237Sjb dtrace_gethrtime_init_cpu, 332179237Sjb smp_no_rendevous_barrier, (void *)(uintptr_t) i); 333179237Sjb 334179237Sjb tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 335179237Sjb } 336216250Savg sched_unpin(); 337179237Sjb} 338299746Sjhb#ifdef EARLY_AP_STARTUP 339299746SjhbSYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, 340299746Sjhb dtrace_gethrtime_init, NULL); 341299746Sjhb#else 342297770SmarkjSYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, 343297770Smarkj NULL); 344299746Sjhb#endif 345179237Sjb 346179237Sjb/* 347179237Sjb * DTrace needs a high resolution time function which can 348179237Sjb * be called from a probe context and guaranteed not to have 349179237Sjb * instrumented with probes itself. 350179237Sjb * 351179237Sjb * Returns nanoseconds since boot. 352179237Sjb */ 353179237Sjbuint64_t 354179237Sjbdtrace_gethrtime() 355179237Sjb{ 356195710Savg uint64_t tsc; 357195710Savg uint32_t lo; 358195710Savg uint32_t hi; 359195710Savg 360195710Savg /* 361195710Savg * We split TSC value into lower and higher 32-bit halves and separately 362195710Savg * scale them with nsec_scale, then we scale them down by 2^28 363195710Savg * (see nsec_scale calculations) taking into account 32-bit shift of 364195710Savg * the higher half and finally add. 365195710Savg */ 366236566Szml tsc = rdtsc() - tsc_skew[curcpu]; 367195710Savg lo = tsc; 368195710Savg hi = tsc >> 32; 369195710Savg return (((lo * nsec_scale) >> SCALE_SHIFT) + 370195710Savg ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 371179237Sjb} 372179237Sjb 373179237Sjbuint64_t 374179237Sjbdtrace_gethrestime(void) 375179237Sjb{ 376238537Sgnn struct timespec current_time; 377238537Sgnn 378238537Sgnn dtrace_getnanotime(¤t_time); 379238537Sgnn 380238552Sgnn return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 381179237Sjb} 382179237Sjb 383268869Smarkj/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */ 384179237Sjbint 385276142Smarkjdtrace_trap(struct trapframe *frame, u_int type) 386179237Sjb{ 387179237Sjb /* 388179237Sjb * A trap can occur while DTrace executes a probe. Before 389179237Sjb * executing the probe, DTrace blocks re-scheduling and sets 390268600Smarkj * a flag in its per-cpu flags to indicate that it doesn't 391218909Sbrucec * want to fault. On returning from the probe, the no-fault 392179237Sjb * flag is cleared and finally re-scheduling is enabled. 393179237Sjb * 394179237Sjb * Check if DTrace has enabled 'no-fault' mode: 395179237Sjb */ 396179237Sjb if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 397179237Sjb /* 398179237Sjb * There are only a couple of trap types that are expected. 399179237Sjb * All the rest will be handled in the usual way. 400179237Sjb */ 401276142Smarkj switch (type) { 402179237Sjb /* General protection fault. */ 403179237Sjb case T_PROTFLT: 404179237Sjb /* Flag an illegal operation. */ 405179237Sjb cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 406179237Sjb 407179237Sjb /* 408179237Sjb * Offset the instruction pointer to the instruction 409179237Sjb * following the one causing the fault. 410179237Sjb */ 411179237Sjb frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 412179237Sjb return (1); 413179237Sjb /* Page fault. */ 414179237Sjb case T_PAGEFLT: 415179237Sjb /* Flag a bad address. */ 416179237Sjb cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 417179237Sjb cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr; 418179237Sjb 419179237Sjb /* 420179237Sjb * Offset the instruction pointer to the instruction 421179237Sjb * following the one causing the fault. 422179237Sjb */ 423179237Sjb frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 424179237Sjb return (1); 425179237Sjb default: 426179237Sjb /* Handle all other traps in the usual way. */ 427179237Sjb break; 428179237Sjb } 429179237Sjb } 430179237Sjb 431179237Sjb /* Handle the trap in the usual way. */ 432179237Sjb return (0); 433179237Sjb} 434