191eaf3e1SJohn Birrell /* 291eaf3e1SJohn Birrell * CDDL HEADER START 391eaf3e1SJohn Birrell * 491eaf3e1SJohn Birrell * The contents of this file are subject to the terms of the 591eaf3e1SJohn Birrell * Common Development and Distribution License, Version 1.0 only 691eaf3e1SJohn Birrell * (the "License"). You may not use this file except in compliance 791eaf3e1SJohn Birrell * with the License. 891eaf3e1SJohn Birrell * 991eaf3e1SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 1091eaf3e1SJohn Birrell * or http://www.opensolaris.org/os/licensing. 1191eaf3e1SJohn Birrell * See the License for the specific language governing permissions 1291eaf3e1SJohn Birrell * and limitations under the License. 1391eaf3e1SJohn Birrell * 1491eaf3e1SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each 1591eaf3e1SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1691eaf3e1SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the 1791eaf3e1SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying 1891eaf3e1SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner] 1991eaf3e1SJohn Birrell * 2091eaf3e1SJohn Birrell * CDDL HEADER END 2191eaf3e1SJohn Birrell * 2291eaf3e1SJohn Birrell * $FreeBSD$ 2391eaf3e1SJohn Birrell * 2491eaf3e1SJohn Birrell */ 2591eaf3e1SJohn Birrell /* 2691eaf3e1SJohn Birrell * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 2791eaf3e1SJohn Birrell * Use is subject to license terms. 2891eaf3e1SJohn Birrell */ 2991eaf3e1SJohn Birrell 304737d389SGeorge V. Neville-Neil /* 314737d389SGeorge V. Neville-Neil * Copyright (c) 2011, Joyent, Inc. All rights reserved. 324737d389SGeorge V. Neville-Neil */ 334737d389SGeorge V. Neville-Neil 3491eaf3e1SJohn Birrell #include <sys/param.h> 3591eaf3e1SJohn Birrell #include <sys/systm.h> 3691eaf3e1SJohn Birrell #include <sys/types.h> 3791eaf3e1SJohn Birrell #include <sys/kernel.h> 3891eaf3e1SJohn Birrell #include <sys/malloc.h> 3991eaf3e1SJohn Birrell #include <sys/smp.h> 4091eaf3e1SJohn Birrell #include <sys/dtrace_impl.h> 4191eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h> 4291eaf3e1SJohn Birrell #include <machine/clock.h> 437174af79SMark Johnston #include <machine/cpufunc.h> 4491eaf3e1SJohn Birrell #include <machine/frame.h> 459ce875d9SKonstantin Belousov #include <machine/md_var.h> 467174af79SMark Johnston #include <machine/psl.h> 47d41e41f9SJohn Baldwin #include <machine/trap.h> 4891eaf3e1SJohn Birrell #include <vm/pmap.h> 4991eaf3e1SJohn Birrell 5057d025c3SGeorge V. Neville-Neil extern void dtrace_getnanotime(struct timespec *tsp); 519093dd9aSMark Johnston extern int (*dtrace_invop_jump_addr)(struct trapframe *); 5257d025c3SGeorge V. Neville-Neil 53*3ba8e9dcSMark Johnston int dtrace_invop(uintptr_t, struct trapframe *, void **); 549093dd9aSMark Johnston int dtrace_invop_start(struct trapframe *frame); 559093dd9aSMark Johnston void dtrace_invop_init(void); 569093dd9aSMark Johnston void dtrace_invop_uninit(void); 5791eaf3e1SJohn Birrell 5891eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr { 596c280659SMark Johnston int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); 6091eaf3e1SJohn Birrell struct dtrace_invop_hdlr *dtih_next; 6191eaf3e1SJohn Birrell } dtrace_invop_hdlr_t; 6291eaf3e1SJohn Birrell 6391eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr; 6491eaf3e1SJohn Birrell 6591eaf3e1SJohn Birrell int 66*3ba8e9dcSMark Johnston dtrace_invop(uintptr_t addr, struct trapframe *frame, void **scratch) 6791eaf3e1SJohn Birrell { 6891eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 6991eaf3e1SJohn Birrell int rval; 7091eaf3e1SJohn Birrell 71*3ba8e9dcSMark Johnston for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) { 72*3ba8e9dcSMark Johnston rval = hdlr->dtih_func(addr, frame, (uintptr_t)scratch); 73*3ba8e9dcSMark Johnston if (rval != 0) 7491eaf3e1SJohn Birrell return (rval); 75*3ba8e9dcSMark Johnston } 7691eaf3e1SJohn Birrell return (0); 7791eaf3e1SJohn Birrell } 7891eaf3e1SJohn Birrell 7991eaf3e1SJohn Birrell void 806c280659SMark Johnston dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 8191eaf3e1SJohn Birrell { 8291eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 8391eaf3e1SJohn Birrell 8491eaf3e1SJohn Birrell hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 8591eaf3e1SJohn Birrell hdlr->dtih_func = func; 8691eaf3e1SJohn Birrell hdlr->dtih_next = dtrace_invop_hdlr; 8791eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr; 8891eaf3e1SJohn Birrell } 8991eaf3e1SJohn Birrell 9091eaf3e1SJohn Birrell void 916c280659SMark Johnston dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 9291eaf3e1SJohn Birrell { 9391eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 9491eaf3e1SJohn Birrell 9591eaf3e1SJohn Birrell for (;;) { 9691eaf3e1SJohn Birrell if (hdlr == NULL) 9791eaf3e1SJohn Birrell panic("attempt to remove non-existent invop handler"); 9891eaf3e1SJohn Birrell 9991eaf3e1SJohn Birrell if (hdlr->dtih_func == func) 10091eaf3e1SJohn Birrell break; 10191eaf3e1SJohn Birrell 10291eaf3e1SJohn Birrell prev = hdlr; 10391eaf3e1SJohn Birrell hdlr = hdlr->dtih_next; 10491eaf3e1SJohn Birrell } 10591eaf3e1SJohn Birrell 10691eaf3e1SJohn Birrell if (prev == NULL) { 10791eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr == hdlr); 10891eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr->dtih_next; 10991eaf3e1SJohn Birrell } else { 11091eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr != hdlr); 11191eaf3e1SJohn Birrell prev->dtih_next = hdlr->dtih_next; 11291eaf3e1SJohn Birrell } 11391eaf3e1SJohn Birrell 11491eaf3e1SJohn Birrell kmem_free(hdlr, 0); 11591eaf3e1SJohn Birrell } 11691eaf3e1SJohn Birrell 1179093dd9aSMark Johnston void 1189093dd9aSMark Johnston dtrace_invop_init(void) 1199093dd9aSMark Johnston { 1209093dd9aSMark Johnston 1219093dd9aSMark Johnston dtrace_invop_jump_addr = dtrace_invop_start; 1229093dd9aSMark Johnston } 1239093dd9aSMark Johnston 1249093dd9aSMark Johnston void 1259093dd9aSMark Johnston dtrace_invop_uninit(void) 1269093dd9aSMark Johnston { 1279093dd9aSMark Johnston 1289093dd9aSMark Johnston dtrace_invop_jump_addr = NULL; 1299093dd9aSMark Johnston } 1309093dd9aSMark Johnston 13191eaf3e1SJohn Birrell /*ARGSUSED*/ 13291eaf3e1SJohn Birrell void 13391eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 13491eaf3e1SJohn Birrell { 1359ce875d9SKonstantin Belousov (*func)(0, la57 ? (uintptr_t)addr_P5Tmap : (uintptr_t)addr_P4Tmap); 13691eaf3e1SJohn Birrell } 13791eaf3e1SJohn Birrell 13891eaf3e1SJohn Birrell void 13991eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 14091eaf3e1SJohn Birrell { 14171a19bdcSAttilio Rao cpuset_t cpus; 14291eaf3e1SJohn Birrell 14391eaf3e1SJohn Birrell if (cpu == DTRACE_CPUALL) 14491eaf3e1SJohn Birrell cpus = all_cpus; 14591eaf3e1SJohn Birrell else 14671a19bdcSAttilio Rao CPU_SETOF(cpu, &cpus); 14791eaf3e1SJohn Birrell 14867d955aaSPatrick Kelsey smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func, 14967d955aaSPatrick Kelsey smp_no_rendezvous_barrier, arg); 15091eaf3e1SJohn Birrell } 15191eaf3e1SJohn Birrell 15291eaf3e1SJohn Birrell static void 15391eaf3e1SJohn Birrell dtrace_sync_func(void) 15491eaf3e1SJohn Birrell { 15591eaf3e1SJohn Birrell } 15691eaf3e1SJohn Birrell 15791eaf3e1SJohn Birrell void 15891eaf3e1SJohn Birrell dtrace_sync(void) 15991eaf3e1SJohn Birrell { 16091eaf3e1SJohn Birrell dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 16191eaf3e1SJohn Birrell } 16291eaf3e1SJohn Birrell 16391eaf3e1SJohn Birrell #ifdef notyet 16491eaf3e1SJohn Birrell void 16591eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void) 16691eaf3e1SJohn Birrell { 16791eaf3e1SJohn Birrell kthread_t *t = curthread; 16891eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 16991eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 17091eaf3e1SJohn Birrell 17191eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 17291eaf3e1SJohn Birrell 17391eaf3e1SJohn Birrell /* 17491eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 17591eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. If the instruction 17691eaf3e1SJohn Birrell * we copied out caused a synchonous trap, reset the pc back to its 17791eaf3e1SJohn Birrell * original value and turn off the flags. 17891eaf3e1SJohn Birrell */ 17991eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 18091eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 18191eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 18291eaf3e1SJohn Birrell } else if (rp->r_pc == t->t_dtrace_scrpc || 18391eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_astpc) { 18491eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_pc; 18591eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 18691eaf3e1SJohn Birrell } 18791eaf3e1SJohn Birrell } 18891eaf3e1SJohn Birrell 18991eaf3e1SJohn Birrell int 19091eaf3e1SJohn Birrell dtrace_safe_defer_signal(void) 19191eaf3e1SJohn Birrell { 19291eaf3e1SJohn Birrell kthread_t *t = curthread; 19391eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 19491eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 19591eaf3e1SJohn Birrell 19691eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 19791eaf3e1SJohn Birrell 19891eaf3e1SJohn Birrell /* 19991eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 20091eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. 20191eaf3e1SJohn Birrell */ 20291eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 20391eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 20491eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 20591eaf3e1SJohn Birrell return (0); 20691eaf3e1SJohn Birrell } 20791eaf3e1SJohn Birrell 20891eaf3e1SJohn Birrell /* 2094737d389SGeorge V. Neville-Neil * If we have executed the original instruction, but we have performed 2104737d389SGeorge V. Neville-Neil * neither the jmp back to t->t_dtrace_npc nor the clean up of any 2114737d389SGeorge V. Neville-Neil * registers used to emulate %rip-relative instructions in 64-bit mode, 2124737d389SGeorge V. Neville-Neil * we'll save ourselves some effort by doing that here and taking the 2134737d389SGeorge V. Neville-Neil * signal right away. We detect this condition by seeing if the program 2144737d389SGeorge V. Neville-Neil * counter is the range [scrpc + isz, astpc). 21591eaf3e1SJohn Birrell */ 2164737d389SGeorge V. Neville-Neil if (rp->r_pc >= t->t_dtrace_scrpc + isz && 2174737d389SGeorge V. Neville-Neil rp->r_pc < t->t_dtrace_astpc) { 21891eaf3e1SJohn Birrell #ifdef __amd64 21991eaf3e1SJohn Birrell /* 22091eaf3e1SJohn Birrell * If there is a scratch register and we're on the 22191eaf3e1SJohn Birrell * instruction immediately after the modified instruction, 22291eaf3e1SJohn Birrell * restore the value of that scratch register. 22391eaf3e1SJohn Birrell */ 22491eaf3e1SJohn Birrell if (t->t_dtrace_reg != 0 && 22591eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_scrpc + isz) { 22691eaf3e1SJohn Birrell switch (t->t_dtrace_reg) { 22791eaf3e1SJohn Birrell case REG_RAX: 22891eaf3e1SJohn Birrell rp->r_rax = t->t_dtrace_regv; 22991eaf3e1SJohn Birrell break; 23091eaf3e1SJohn Birrell case REG_RCX: 23191eaf3e1SJohn Birrell rp->r_rcx = t->t_dtrace_regv; 23291eaf3e1SJohn Birrell break; 23391eaf3e1SJohn Birrell case REG_R8: 23491eaf3e1SJohn Birrell rp->r_r8 = t->t_dtrace_regv; 23591eaf3e1SJohn Birrell break; 23691eaf3e1SJohn Birrell case REG_R9: 23791eaf3e1SJohn Birrell rp->r_r9 = t->t_dtrace_regv; 23891eaf3e1SJohn Birrell break; 23991eaf3e1SJohn Birrell } 24091eaf3e1SJohn Birrell } 24191eaf3e1SJohn Birrell #endif 24291eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_npc; 24391eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 24491eaf3e1SJohn Birrell return (0); 24591eaf3e1SJohn Birrell } 24691eaf3e1SJohn Birrell 24791eaf3e1SJohn Birrell /* 24891eaf3e1SJohn Birrell * Otherwise, make sure we'll return to the kernel after executing 24991eaf3e1SJohn Birrell * the copied out instruction and defer the signal. 25091eaf3e1SJohn Birrell */ 25191eaf3e1SJohn Birrell if (!t->t_dtrace_step) { 25291eaf3e1SJohn Birrell ASSERT(rp->r_pc < t->t_dtrace_astpc); 25391eaf3e1SJohn Birrell rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 25491eaf3e1SJohn Birrell t->t_dtrace_step = 1; 25591eaf3e1SJohn Birrell } 25691eaf3e1SJohn Birrell 25791eaf3e1SJohn Birrell t->t_dtrace_ast = 1; 25891eaf3e1SJohn Birrell 25991eaf3e1SJohn Birrell return (1); 26091eaf3e1SJohn Birrell } 26191eaf3e1SJohn Birrell #endif 26291eaf3e1SJohn Birrell 26391eaf3e1SJohn Birrell static int64_t tgt_cpu_tsc; 26491eaf3e1SJohn Birrell static int64_t hst_cpu_tsc; 26591eaf3e1SJohn Birrell static int64_t tsc_skew[MAXCPU]; 266b064b6d1SAndriy Gapon static uint64_t nsec_scale; 267b064b6d1SAndriy Gapon 268b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */ 269b064b6d1SAndriy Gapon #define SCALE_SHIFT 28 27091eaf3e1SJohn Birrell 271fdce57a0SJohn Baldwin static void 272fdce57a0SJohn Baldwin dtrace_gethrtime_init_cpu(void *arg) 273fdce57a0SJohn Baldwin { 274fdce57a0SJohn Baldwin uintptr_t cpu = (uintptr_t) arg; 275fdce57a0SJohn Baldwin 276fdce57a0SJohn Baldwin if (cpu == curcpu) 277fdce57a0SJohn Baldwin tgt_cpu_tsc = rdtsc(); 278fdce57a0SJohn Baldwin else 279fdce57a0SJohn Baldwin hst_cpu_tsc = rdtsc(); 280fdce57a0SJohn Baldwin } 281fdce57a0SJohn Baldwin 282fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 283fdce57a0SJohn Baldwin static void 284fdce57a0SJohn Baldwin dtrace_gethrtime_init(void *arg) 285fdce57a0SJohn Baldwin { 286fdce57a0SJohn Baldwin struct pcpu *pc; 287fdce57a0SJohn Baldwin uint64_t tsc_f; 288fdce57a0SJohn Baldwin cpuset_t map; 289fdce57a0SJohn Baldwin int i; 290fdce57a0SJohn Baldwin #else 291e1e33ff9SMark Johnston /* 292e1e33ff9SMark Johnston * Get the frequency and scale factor as early as possible so that they can be 293e1e33ff9SMark Johnston * used for boot-time tracing. 294e1e33ff9SMark Johnston */ 29591eaf3e1SJohn Birrell static void 296e1e33ff9SMark Johnston dtrace_gethrtime_init_early(void *arg) 29791eaf3e1SJohn Birrell { 298b064b6d1SAndriy Gapon uint64_t tsc_f; 299fdce57a0SJohn Baldwin #endif 300b064b6d1SAndriy Gapon 301b064b6d1SAndriy Gapon /* 302b064b6d1SAndriy Gapon * Get TSC frequency known at this moment. 303b064b6d1SAndriy Gapon * This should be constant if TSC is invariant. 304b064b6d1SAndriy Gapon * Otherwise tick->time conversion will be inaccurate, but 305b064b6d1SAndriy Gapon * will preserve monotonic property of TSC. 306b064b6d1SAndriy Gapon */ 3073453537fSJung-uk Kim tsc_f = atomic_load_acq_64(&tsc_freq); 308b064b6d1SAndriy Gapon 309b064b6d1SAndriy Gapon /* 310b064b6d1SAndriy Gapon * The following line checks that nsec_scale calculated below 311b064b6d1SAndriy Gapon * doesn't overflow 32-bit unsigned integer, so that it can multiply 312b064b6d1SAndriy Gapon * another 32-bit integer without overflowing 64-bit. 313b064b6d1SAndriy Gapon * Thus minimum supported TSC frequency is 62.5MHz. 314b064b6d1SAndriy Gapon */ 315e1e33ff9SMark Johnston KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), 316e1e33ff9SMark Johnston ("TSC frequency is too low")); 317b064b6d1SAndriy Gapon 318b064b6d1SAndriy Gapon /* 319b064b6d1SAndriy Gapon * We scale up NANOSEC/tsc_f ratio to preserve as much precision 320b064b6d1SAndriy Gapon * as possible. 321b064b6d1SAndriy Gapon * 2^28 factor was chosen quite arbitrarily from practical 322b064b6d1SAndriy Gapon * considerations: 323b064b6d1SAndriy Gapon * - it supports TSC frequencies as low as 62.5MHz (see above); 324b064b6d1SAndriy Gapon * - it provides quite good precision (e < 0.01%) up to THz 325b064b6d1SAndriy Gapon * (terahertz) values; 326b064b6d1SAndriy Gapon */ 327b064b6d1SAndriy Gapon nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 328fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP 329e1e33ff9SMark Johnston } 330e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY, 331e1e33ff9SMark Johnston dtrace_gethrtime_init_early, NULL); 332e1e33ff9SMark Johnston 333e1e33ff9SMark Johnston static void 334e1e33ff9SMark Johnston dtrace_gethrtime_init(void *arg) 335e1e33ff9SMark Johnston { 336e1e33ff9SMark Johnston struct pcpu *pc; 337e1e33ff9SMark Johnston cpuset_t map; 338e1e33ff9SMark Johnston int i; 339fdce57a0SJohn Baldwin #endif 34091eaf3e1SJohn Birrell 341a4b59d3dSMark Johnston if (vm_guest != VM_GUEST_NO) 342e362e590SMark Johnston return; 343e362e590SMark Johnston 34491eaf3e1SJohn Birrell /* The current CPU is the reference one. */ 3457becfa95SAndriy Gapon sched_pin(); 34691eaf3e1SJohn Birrell tsc_skew[curcpu] = 0; 3473aa6d94eSJohn Baldwin CPU_FOREACH(i) { 34891eaf3e1SJohn Birrell if (i == curcpu) 34991eaf3e1SJohn Birrell continue; 35091eaf3e1SJohn Birrell 3517becfa95SAndriy Gapon pc = pcpu_find(i); 352ada5b739SAttilio Rao CPU_SETOF(PCPU_GET(cpuid), &map); 353ada5b739SAttilio Rao CPU_SET(pc->pc_cpuid, &map); 35491eaf3e1SJohn Birrell 355d9b8935fSAndriy Gapon smp_rendezvous_cpus(map, NULL, 35691eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu, 35767d955aaSPatrick Kelsey smp_no_rendezvous_barrier, (void *)(uintptr_t) i); 35891eaf3e1SJohn Birrell 35991eaf3e1SJohn Birrell tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 36091eaf3e1SJohn Birrell } 3617becfa95SAndriy Gapon sched_unpin(); 36291eaf3e1SJohn Birrell } 363fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 364fdce57a0SJohn Baldwin SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, 365fdce57a0SJohn Baldwin dtrace_gethrtime_init, NULL); 366fdce57a0SJohn Baldwin #else 367e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, 368e1e33ff9SMark Johnston NULL); 369fdce57a0SJohn Baldwin #endif 37091eaf3e1SJohn Birrell 37191eaf3e1SJohn Birrell /* 37291eaf3e1SJohn Birrell * DTrace needs a high resolution time function which can 37391eaf3e1SJohn Birrell * be called from a probe context and guaranteed not to have 37491eaf3e1SJohn Birrell * instrumented with probes itself. 37591eaf3e1SJohn Birrell * 37691eaf3e1SJohn Birrell * Returns nanoseconds since boot. 37791eaf3e1SJohn Birrell */ 37891eaf3e1SJohn Birrell uint64_t 3796c7828a2SMark Johnston dtrace_gethrtime(void) 38091eaf3e1SJohn Birrell { 381b064b6d1SAndriy Gapon uint64_t tsc; 3826c7828a2SMark Johnston uint32_t lo, hi; 3836c7828a2SMark Johnston register_t rflags; 384b064b6d1SAndriy Gapon 385b064b6d1SAndriy Gapon /* 386b064b6d1SAndriy Gapon * We split TSC value into lower and higher 32-bit halves and separately 387b064b6d1SAndriy Gapon * scale them with nsec_scale, then we scale them down by 2^28 388b064b6d1SAndriy Gapon * (see nsec_scale calculations) taking into account 32-bit shift of 389b064b6d1SAndriy Gapon * the higher half and finally add. 390b064b6d1SAndriy Gapon */ 3916c7828a2SMark Johnston rflags = intr_disable(); 392db5c7d36SZachary Loafman tsc = rdtsc() - tsc_skew[curcpu]; 3936c7828a2SMark Johnston intr_restore(rflags); 3946c7828a2SMark Johnston 395b064b6d1SAndriy Gapon lo = tsc; 396b064b6d1SAndriy Gapon hi = tsc >> 32; 397b064b6d1SAndriy Gapon return (((lo * nsec_scale) >> SCALE_SHIFT) + 398b064b6d1SAndriy Gapon ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 39991eaf3e1SJohn Birrell } 40091eaf3e1SJohn Birrell 40191eaf3e1SJohn Birrell uint64_t 40291eaf3e1SJohn Birrell dtrace_gethrestime(void) 40391eaf3e1SJohn Birrell { 40457d025c3SGeorge V. Neville-Neil struct timespec current_time; 40557d025c3SGeorge V. Neville-Neil 40657d025c3SGeorge V. Neville-Neil dtrace_getnanotime(¤t_time); 40757d025c3SGeorge V. Neville-Neil 408d638e8dcSGeorge V. Neville-Neil return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 40991eaf3e1SJohn Birrell } 41091eaf3e1SJohn Birrell 4115a5f9d21SMark Johnston /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */ 41291eaf3e1SJohn Birrell int 413cafe8744SMark Johnston dtrace_trap(struct trapframe *frame, u_int type) 41491eaf3e1SJohn Birrell { 415a11ac730SMark Johnston uint16_t nofault; 416a11ac730SMark Johnston 41791eaf3e1SJohn Birrell /* 41891eaf3e1SJohn Birrell * A trap can occur while DTrace executes a probe. Before 41991eaf3e1SJohn Birrell * executing the probe, DTrace blocks re-scheduling and sets 420291624fdSMark Johnston * a flag in its per-cpu flags to indicate that it doesn't 4216bccea7cSRebecca Cran * want to fault. On returning from the probe, the no-fault 42291eaf3e1SJohn Birrell * flag is cleared and finally re-scheduling is enabled. 42391eaf3e1SJohn Birrell * 42491eaf3e1SJohn Birrell * Check if DTrace has enabled 'no-fault' mode: 42591eaf3e1SJohn Birrell */ 426a11ac730SMark Johnston sched_pin(); 427a11ac730SMark Johnston nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT; 428a11ac730SMark Johnston sched_unpin(); 429a11ac730SMark Johnston if (nofault) { 430a11ac730SMark Johnston KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled")); 431a11ac730SMark Johnston 43291eaf3e1SJohn Birrell /* 43391eaf3e1SJohn Birrell * There are only a couple of trap types that are expected. 43491eaf3e1SJohn Birrell * All the rest will be handled in the usual way. 43591eaf3e1SJohn Birrell */ 436cafe8744SMark Johnston switch (type) { 43791eaf3e1SJohn Birrell /* General protection fault. */ 43891eaf3e1SJohn Birrell case T_PROTFLT: 43991eaf3e1SJohn Birrell /* Flag an illegal operation. */ 44091eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 44191eaf3e1SJohn Birrell 44291eaf3e1SJohn Birrell /* 44391eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 44491eaf3e1SJohn Birrell * following the one causing the fault. 44591eaf3e1SJohn Birrell */ 44691eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 44791eaf3e1SJohn Birrell return (1); 44891eaf3e1SJohn Birrell /* Page fault. */ 44991eaf3e1SJohn Birrell case T_PAGEFLT: 45091eaf3e1SJohn Birrell /* Flag a bad address. */ 45191eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 45291eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr; 45391eaf3e1SJohn Birrell 45491eaf3e1SJohn Birrell /* 45591eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 45691eaf3e1SJohn Birrell * following the one causing the fault. 45791eaf3e1SJohn Birrell */ 45891eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 45991eaf3e1SJohn Birrell return (1); 46091eaf3e1SJohn Birrell default: 46191eaf3e1SJohn Birrell /* Handle all other traps in the usual way. */ 46291eaf3e1SJohn Birrell break; 46391eaf3e1SJohn Birrell } 46491eaf3e1SJohn Birrell } 46591eaf3e1SJohn Birrell 46691eaf3e1SJohn Birrell /* Handle the trap in the usual way. */ 46791eaf3e1SJohn Birrell return (0); 46891eaf3e1SJohn Birrell } 469