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> 3771a19bdcSAttilio Rao #include <sys/cpuset.h> 3891eaf3e1SJohn Birrell #include <sys/kernel.h> 3991eaf3e1SJohn Birrell #include <sys/malloc.h> 4091eaf3e1SJohn Birrell #include <sys/kmem.h> 4191eaf3e1SJohn Birrell #include <sys/smp.h> 4291eaf3e1SJohn Birrell #include <sys/dtrace_impl.h> 4391eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h> 4491eaf3e1SJohn Birrell #include <machine/clock.h> 457174af79SMark Johnston #include <machine/cpufunc.h> 4691eaf3e1SJohn Birrell #include <machine/frame.h> 477174af79SMark Johnston #include <machine/psl.h> 48d41e41f9SJohn Baldwin #include <machine/trap.h> 4991eaf3e1SJohn Birrell #include <vm/pmap.h> 5091eaf3e1SJohn Birrell 5191eaf3e1SJohn Birrell extern uintptr_t kernelbase; 5291eaf3e1SJohn Birrell 5357d025c3SGeorge V. Neville-Neil extern void dtrace_getnanotime(struct timespec *tsp); 5457d025c3SGeorge V. Neville-Neil 556c280659SMark Johnston int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); 5691eaf3e1SJohn Birrell 5791eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr { 586c280659SMark Johnston int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); 5991eaf3e1SJohn Birrell struct dtrace_invop_hdlr *dtih_next; 6091eaf3e1SJohn Birrell } dtrace_invop_hdlr_t; 6191eaf3e1SJohn Birrell 6291eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr; 6391eaf3e1SJohn Birrell 6491eaf3e1SJohn Birrell int 656c280659SMark Johnston dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) 6691eaf3e1SJohn Birrell { 6791eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 6891eaf3e1SJohn Birrell int rval; 6991eaf3e1SJohn Birrell 7091eaf3e1SJohn Birrell for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 716c280659SMark Johnston if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) 7291eaf3e1SJohn Birrell return (rval); 7391eaf3e1SJohn Birrell 7491eaf3e1SJohn Birrell return (0); 7591eaf3e1SJohn Birrell } 7691eaf3e1SJohn Birrell 7791eaf3e1SJohn Birrell void 786c280659SMark Johnston dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 7991eaf3e1SJohn Birrell { 8091eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 8191eaf3e1SJohn Birrell 8291eaf3e1SJohn Birrell hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 8391eaf3e1SJohn Birrell hdlr->dtih_func = func; 8491eaf3e1SJohn Birrell hdlr->dtih_next = dtrace_invop_hdlr; 8591eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr; 8691eaf3e1SJohn Birrell } 8791eaf3e1SJohn Birrell 8891eaf3e1SJohn Birrell void 896c280659SMark Johnston dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) 9091eaf3e1SJohn Birrell { 9191eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 9291eaf3e1SJohn Birrell 9391eaf3e1SJohn Birrell for (;;) { 9491eaf3e1SJohn Birrell if (hdlr == NULL) 9591eaf3e1SJohn Birrell panic("attempt to remove non-existent invop handler"); 9691eaf3e1SJohn Birrell 9791eaf3e1SJohn Birrell if (hdlr->dtih_func == func) 9891eaf3e1SJohn Birrell break; 9991eaf3e1SJohn Birrell 10091eaf3e1SJohn Birrell prev = hdlr; 10191eaf3e1SJohn Birrell hdlr = hdlr->dtih_next; 10291eaf3e1SJohn Birrell } 10391eaf3e1SJohn Birrell 10491eaf3e1SJohn Birrell if (prev == NULL) { 10591eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr == hdlr); 10691eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr->dtih_next; 10791eaf3e1SJohn Birrell } else { 10891eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr != hdlr); 10991eaf3e1SJohn Birrell prev->dtih_next = hdlr->dtih_next; 11091eaf3e1SJohn Birrell } 11191eaf3e1SJohn Birrell 11291eaf3e1SJohn Birrell kmem_free(hdlr, 0); 11391eaf3e1SJohn Birrell } 11491eaf3e1SJohn Birrell 11591eaf3e1SJohn Birrell void 11691eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 11791eaf3e1SJohn Birrell { 11891eaf3e1SJohn Birrell (*func)(0, kernelbase); 11991eaf3e1SJohn Birrell } 12091eaf3e1SJohn Birrell 12191eaf3e1SJohn Birrell void 12291eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 12391eaf3e1SJohn Birrell { 12471a19bdcSAttilio Rao cpuset_t cpus; 12591eaf3e1SJohn Birrell 12691eaf3e1SJohn Birrell if (cpu == DTRACE_CPUALL) 12791eaf3e1SJohn Birrell cpus = all_cpus; 12891eaf3e1SJohn Birrell else 12971a19bdcSAttilio Rao CPU_SETOF(cpu, &cpus); 13091eaf3e1SJohn Birrell 13167d955aaSPatrick Kelsey smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func, 13267d955aaSPatrick Kelsey smp_no_rendezvous_barrier, arg); 13391eaf3e1SJohn Birrell } 13491eaf3e1SJohn Birrell 13591eaf3e1SJohn Birrell static void 13691eaf3e1SJohn Birrell dtrace_sync_func(void) 13791eaf3e1SJohn Birrell { 13891eaf3e1SJohn Birrell } 13991eaf3e1SJohn Birrell 14091eaf3e1SJohn Birrell void 14191eaf3e1SJohn Birrell dtrace_sync(void) 14291eaf3e1SJohn Birrell { 14391eaf3e1SJohn Birrell dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 14491eaf3e1SJohn Birrell } 14591eaf3e1SJohn Birrell 14691eaf3e1SJohn Birrell #ifdef notyet 14791eaf3e1SJohn Birrell void 14891eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void) 14991eaf3e1SJohn Birrell { 15091eaf3e1SJohn Birrell kthread_t *t = curthread; 15191eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 15291eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 15391eaf3e1SJohn Birrell 15491eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 15591eaf3e1SJohn Birrell 15691eaf3e1SJohn Birrell /* 15791eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 15891eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. If the instruction 15991eaf3e1SJohn Birrell * we copied out caused a synchonous trap, reset the pc back to its 16091eaf3e1SJohn Birrell * original value and turn off the flags. 16191eaf3e1SJohn Birrell */ 16291eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 16391eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 16491eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 16591eaf3e1SJohn Birrell } else if (rp->r_pc == t->t_dtrace_scrpc || 16691eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_astpc) { 16791eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_pc; 16891eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 16991eaf3e1SJohn Birrell } 17091eaf3e1SJohn Birrell } 17191eaf3e1SJohn Birrell 17291eaf3e1SJohn Birrell int 17391eaf3e1SJohn Birrell dtrace_safe_defer_signal(void) 17491eaf3e1SJohn Birrell { 17591eaf3e1SJohn Birrell kthread_t *t = curthread; 17691eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 17791eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 17891eaf3e1SJohn Birrell 17991eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 18091eaf3e1SJohn Birrell 18191eaf3e1SJohn Birrell /* 18291eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 18391eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. 18491eaf3e1SJohn Birrell */ 18591eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 18691eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 18791eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 18891eaf3e1SJohn Birrell return (0); 18991eaf3e1SJohn Birrell } 19091eaf3e1SJohn Birrell 19191eaf3e1SJohn Birrell /* 1924737d389SGeorge V. Neville-Neil * If we have executed the original instruction, but we have performed 1934737d389SGeorge V. Neville-Neil * neither the jmp back to t->t_dtrace_npc nor the clean up of any 1944737d389SGeorge V. Neville-Neil * registers used to emulate %rip-relative instructions in 64-bit mode, 1954737d389SGeorge V. Neville-Neil * we'll save ourselves some effort by doing that here and taking the 1964737d389SGeorge V. Neville-Neil * signal right away. We detect this condition by seeing if the program 1974737d389SGeorge V. Neville-Neil * counter is the range [scrpc + isz, astpc). 19891eaf3e1SJohn Birrell */ 1994737d389SGeorge V. Neville-Neil if (rp->r_pc >= t->t_dtrace_scrpc + isz && 2004737d389SGeorge V. Neville-Neil rp->r_pc < t->t_dtrace_astpc) { 20191eaf3e1SJohn Birrell #ifdef __amd64 20291eaf3e1SJohn Birrell /* 20391eaf3e1SJohn Birrell * If there is a scratch register and we're on the 20491eaf3e1SJohn Birrell * instruction immediately after the modified instruction, 20591eaf3e1SJohn Birrell * restore the value of that scratch register. 20691eaf3e1SJohn Birrell */ 20791eaf3e1SJohn Birrell if (t->t_dtrace_reg != 0 && 20891eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_scrpc + isz) { 20991eaf3e1SJohn Birrell switch (t->t_dtrace_reg) { 21091eaf3e1SJohn Birrell case REG_RAX: 21191eaf3e1SJohn Birrell rp->r_rax = t->t_dtrace_regv; 21291eaf3e1SJohn Birrell break; 21391eaf3e1SJohn Birrell case REG_RCX: 21491eaf3e1SJohn Birrell rp->r_rcx = t->t_dtrace_regv; 21591eaf3e1SJohn Birrell break; 21691eaf3e1SJohn Birrell case REG_R8: 21791eaf3e1SJohn Birrell rp->r_r8 = t->t_dtrace_regv; 21891eaf3e1SJohn Birrell break; 21991eaf3e1SJohn Birrell case REG_R9: 22091eaf3e1SJohn Birrell rp->r_r9 = t->t_dtrace_regv; 22191eaf3e1SJohn Birrell break; 22291eaf3e1SJohn Birrell } 22391eaf3e1SJohn Birrell } 22491eaf3e1SJohn Birrell #endif 22591eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_npc; 22691eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 22791eaf3e1SJohn Birrell return (0); 22891eaf3e1SJohn Birrell } 22991eaf3e1SJohn Birrell 23091eaf3e1SJohn Birrell /* 23191eaf3e1SJohn Birrell * Otherwise, make sure we'll return to the kernel after executing 23291eaf3e1SJohn Birrell * the copied out instruction and defer the signal. 23391eaf3e1SJohn Birrell */ 23491eaf3e1SJohn Birrell if (!t->t_dtrace_step) { 23591eaf3e1SJohn Birrell ASSERT(rp->r_pc < t->t_dtrace_astpc); 23691eaf3e1SJohn Birrell rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 23791eaf3e1SJohn Birrell t->t_dtrace_step = 1; 23891eaf3e1SJohn Birrell } 23991eaf3e1SJohn Birrell 24091eaf3e1SJohn Birrell t->t_dtrace_ast = 1; 24191eaf3e1SJohn Birrell 24291eaf3e1SJohn Birrell return (1); 24391eaf3e1SJohn Birrell } 24491eaf3e1SJohn Birrell #endif 24591eaf3e1SJohn Birrell 24691eaf3e1SJohn Birrell static int64_t tgt_cpu_tsc; 24791eaf3e1SJohn Birrell static int64_t hst_cpu_tsc; 24891eaf3e1SJohn Birrell static int64_t tsc_skew[MAXCPU]; 249b064b6d1SAndriy Gapon static uint64_t nsec_scale; 250b064b6d1SAndriy Gapon 251b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */ 252b064b6d1SAndriy Gapon #define SCALE_SHIFT 28 25391eaf3e1SJohn Birrell 254fdce57a0SJohn Baldwin static void 255fdce57a0SJohn Baldwin dtrace_gethrtime_init_cpu(void *arg) 256fdce57a0SJohn Baldwin { 257fdce57a0SJohn Baldwin uintptr_t cpu = (uintptr_t) arg; 258fdce57a0SJohn Baldwin 259fdce57a0SJohn Baldwin if (cpu == curcpu) 260fdce57a0SJohn Baldwin tgt_cpu_tsc = rdtsc(); 261fdce57a0SJohn Baldwin else 262fdce57a0SJohn Baldwin hst_cpu_tsc = rdtsc(); 263fdce57a0SJohn Baldwin } 264fdce57a0SJohn Baldwin 265fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 266fdce57a0SJohn Baldwin static void 267fdce57a0SJohn Baldwin dtrace_gethrtime_init(void *arg) 268fdce57a0SJohn Baldwin { 269fdce57a0SJohn Baldwin struct pcpu *pc; 270fdce57a0SJohn Baldwin uint64_t tsc_f; 271fdce57a0SJohn Baldwin cpuset_t map; 272fdce57a0SJohn Baldwin int i; 273fdce57a0SJohn Baldwin #else 274e1e33ff9SMark Johnston /* 275e1e33ff9SMark Johnston * Get the frequency and scale factor as early as possible so that they can be 276e1e33ff9SMark Johnston * used for boot-time tracing. 277e1e33ff9SMark Johnston */ 278e1e33ff9SMark Johnston static void 279e1e33ff9SMark Johnston dtrace_gethrtime_init_early(void *arg) 280e1e33ff9SMark Johnston { 281e1e33ff9SMark Johnston uint64_t tsc_f; 282fdce57a0SJohn Baldwin #endif 283e1e33ff9SMark Johnston 284e1e33ff9SMark Johnston /* 285e1e33ff9SMark Johnston * Get TSC frequency known at this moment. 286e1e33ff9SMark Johnston * This should be constant if TSC is invariant. 287e1e33ff9SMark Johnston * Otherwise tick->time conversion will be inaccurate, but 288e1e33ff9SMark Johnston * will preserve monotonic property of TSC. 289e1e33ff9SMark Johnston */ 290e1e33ff9SMark Johnston tsc_f = atomic_load_acq_64(&tsc_freq); 291e1e33ff9SMark Johnston 292e1e33ff9SMark Johnston /* 293e1e33ff9SMark Johnston * The following line checks that nsec_scale calculated below 294e1e33ff9SMark Johnston * doesn't overflow 32-bit unsigned integer, so that it can multiply 295e1e33ff9SMark Johnston * another 32-bit integer without overflowing 64-bit. 296e1e33ff9SMark Johnston * Thus minimum supported TSC frequency is 62.5MHz. 297e1e33ff9SMark Johnston */ 298e1e33ff9SMark Johnston KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), 299e1e33ff9SMark Johnston ("TSC frequency is too low")); 300e1e33ff9SMark Johnston 301e1e33ff9SMark Johnston /* 302e1e33ff9SMark Johnston * We scale up NANOSEC/tsc_f ratio to preserve as much precision 303e1e33ff9SMark Johnston * as possible. 304e1e33ff9SMark Johnston * 2^28 factor was chosen quite arbitrarily from practical 305e1e33ff9SMark Johnston * considerations: 306e1e33ff9SMark Johnston * - it supports TSC frequencies as low as 62.5MHz (see above); 307e1e33ff9SMark Johnston * - it provides quite good precision (e < 0.01%) up to THz 308e1e33ff9SMark Johnston * (terahertz) values; 309e1e33ff9SMark Johnston */ 310e1e33ff9SMark Johnston nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 311fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP 312e1e33ff9SMark Johnston } 313e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY, 314e1e33ff9SMark Johnston dtrace_gethrtime_init_early, NULL); 315e1e33ff9SMark Johnston 31691eaf3e1SJohn Birrell static void 31791eaf3e1SJohn Birrell dtrace_gethrtime_init(void *arg) 31891eaf3e1SJohn Birrell { 31971a19bdcSAttilio Rao cpuset_t map; 3207becfa95SAndriy Gapon struct pcpu *pc; 32191eaf3e1SJohn Birrell int i; 322fdce57a0SJohn Baldwin #endif 32391eaf3e1SJohn Birrell 324*e362e590SMark Johnston if (vm_guest) 325*e362e590SMark Johnston return; 326*e362e590SMark Johnston 32791eaf3e1SJohn Birrell /* The current CPU is the reference one. */ 3287becfa95SAndriy Gapon sched_pin(); 32991eaf3e1SJohn Birrell tsc_skew[curcpu] = 0; 3303aa6d94eSJohn Baldwin CPU_FOREACH(i) { 33191eaf3e1SJohn Birrell if (i == curcpu) 33291eaf3e1SJohn Birrell continue; 33391eaf3e1SJohn Birrell 3347becfa95SAndriy Gapon pc = pcpu_find(i); 335ada5b739SAttilio Rao CPU_SETOF(PCPU_GET(cpuid), &map); 336ada5b739SAttilio Rao CPU_SET(pc->pc_cpuid, &map); 33791eaf3e1SJohn Birrell 338d9b8935fSAndriy Gapon smp_rendezvous_cpus(map, NULL, 33991eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu, 34067d955aaSPatrick Kelsey smp_no_rendezvous_barrier, (void *)(uintptr_t) i); 34191eaf3e1SJohn Birrell 34291eaf3e1SJohn Birrell tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 34391eaf3e1SJohn Birrell } 3447becfa95SAndriy Gapon sched_unpin(); 34591eaf3e1SJohn Birrell } 346fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 347fdce57a0SJohn Baldwin SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, 348fdce57a0SJohn Baldwin dtrace_gethrtime_init, NULL); 349fdce57a0SJohn Baldwin #else 350e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, 351e1e33ff9SMark Johnston NULL); 352fdce57a0SJohn Baldwin #endif 35391eaf3e1SJohn Birrell 35491eaf3e1SJohn Birrell /* 35591eaf3e1SJohn Birrell * DTrace needs a high resolution time function which can 35691eaf3e1SJohn Birrell * be called from a probe context and guaranteed not to have 35791eaf3e1SJohn Birrell * instrumented with probes itself. 35891eaf3e1SJohn Birrell * 35991eaf3e1SJohn Birrell * Returns nanoseconds since boot. 36091eaf3e1SJohn Birrell */ 36191eaf3e1SJohn Birrell uint64_t 3626c7828a2SMark Johnston dtrace_gethrtime(void) 36391eaf3e1SJohn Birrell { 364b064b6d1SAndriy Gapon uint64_t tsc; 3656c7828a2SMark Johnston uint32_t lo, hi; 3666c7828a2SMark Johnston register_t eflags; 367b064b6d1SAndriy Gapon 368b064b6d1SAndriy Gapon /* 369b064b6d1SAndriy Gapon * We split TSC value into lower and higher 32-bit halves and separately 370b064b6d1SAndriy Gapon * scale them with nsec_scale, then we scale them down by 2^28 371b064b6d1SAndriy Gapon * (see nsec_scale calculations) taking into account 32-bit shift of 372b064b6d1SAndriy Gapon * the higher half and finally add. 373b064b6d1SAndriy Gapon */ 3746c7828a2SMark Johnston eflags = intr_disable(); 375db5c7d36SZachary Loafman tsc = rdtsc() - tsc_skew[curcpu]; 3766c7828a2SMark Johnston intr_restore(eflags); 3776c7828a2SMark Johnston 378b064b6d1SAndriy Gapon lo = tsc; 379b064b6d1SAndriy Gapon hi = tsc >> 32; 380b064b6d1SAndriy Gapon return (((lo * nsec_scale) >> SCALE_SHIFT) + 381b064b6d1SAndriy Gapon ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 38291eaf3e1SJohn Birrell } 38391eaf3e1SJohn Birrell 38491eaf3e1SJohn Birrell uint64_t 38591eaf3e1SJohn Birrell dtrace_gethrestime(void) 38691eaf3e1SJohn Birrell { 38757d025c3SGeorge V. Neville-Neil struct timespec current_time; 38857d025c3SGeorge V. Neville-Neil 38957d025c3SGeorge V. Neville-Neil dtrace_getnanotime(¤t_time); 39057d025c3SGeorge V. Neville-Neil 391d638e8dcSGeorge V. Neville-Neil return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 39291eaf3e1SJohn Birrell } 39391eaf3e1SJohn Birrell 39491eaf3e1SJohn Birrell /* Function to handle DTrace traps during probes. See i386/i386/trap.c */ 39591eaf3e1SJohn Birrell int 396cafe8744SMark Johnston dtrace_trap(struct trapframe *frame, u_int type) 39791eaf3e1SJohn Birrell { 398a11ac730SMark Johnston uint16_t nofault; 399a11ac730SMark Johnston 40091eaf3e1SJohn Birrell /* 40191eaf3e1SJohn Birrell * A trap can occur while DTrace executes a probe. Before 40291eaf3e1SJohn Birrell * executing the probe, DTrace blocks re-scheduling and sets 403291624fdSMark Johnston * a flag in its per-cpu flags to indicate that it doesn't 4046bccea7cSRebecca Cran * want to fault. On returning from the probe, the no-fault 40591eaf3e1SJohn Birrell * flag is cleared and finally re-scheduling is enabled. 40691eaf3e1SJohn Birrell * 40791eaf3e1SJohn Birrell * Check if DTrace has enabled 'no-fault' mode: 40891eaf3e1SJohn Birrell */ 409a11ac730SMark Johnston sched_pin(); 410a11ac730SMark Johnston nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT; 411a11ac730SMark Johnston sched_unpin(); 412a11ac730SMark Johnston if (nofault) { 413a11ac730SMark Johnston KASSERT((read_eflags() & PSL_I) == 0, ("interrupts enabled")); 414a11ac730SMark Johnston 41591eaf3e1SJohn Birrell /* 41691eaf3e1SJohn Birrell * There are only a couple of trap types that are expected. 41791eaf3e1SJohn Birrell * All the rest will be handled in the usual way. 41891eaf3e1SJohn Birrell */ 419cafe8744SMark Johnston switch (type) { 42091eaf3e1SJohn Birrell /* General protection fault. */ 42191eaf3e1SJohn Birrell case T_PROTFLT: 42291eaf3e1SJohn Birrell /* Flag an illegal operation. */ 42391eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 42491eaf3e1SJohn Birrell 42591eaf3e1SJohn Birrell /* 42691eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 42791eaf3e1SJohn Birrell * following the one causing the fault. 42891eaf3e1SJohn Birrell */ 42991eaf3e1SJohn Birrell frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip); 43091eaf3e1SJohn Birrell return (1); 43191eaf3e1SJohn Birrell /* Page fault. */ 43291eaf3e1SJohn Birrell case T_PAGEFLT: 43391eaf3e1SJohn Birrell /* Flag a bad address. */ 43491eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 43591eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = rcr2(); 43691eaf3e1SJohn Birrell 43791eaf3e1SJohn Birrell /* 43891eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 43991eaf3e1SJohn Birrell * following the one causing the fault. 44091eaf3e1SJohn Birrell */ 44191eaf3e1SJohn Birrell frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip); 44291eaf3e1SJohn Birrell return (1); 44391eaf3e1SJohn Birrell default: 44491eaf3e1SJohn Birrell /* Handle all other traps in the usual way. */ 44591eaf3e1SJohn Birrell break; 44691eaf3e1SJohn Birrell } 44791eaf3e1SJohn Birrell } 44891eaf3e1SJohn Birrell 44991eaf3e1SJohn Birrell /* Handle the trap in the usual way. */ 45091eaf3e1SJohn Birrell return (0); 45191eaf3e1SJohn Birrell } 452