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/kmem.h> 4091eaf3e1SJohn Birrell #include <sys/smp.h> 4191eaf3e1SJohn Birrell #include <sys/dtrace_impl.h> 4291eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h> 4391eaf3e1SJohn Birrell #include <machine/clock.h> 4491eaf3e1SJohn Birrell #include <machine/frame.h> 4591eaf3e1SJohn Birrell #include <vm/pmap.h> 4691eaf3e1SJohn Birrell 4791eaf3e1SJohn Birrell extern uintptr_t dtrace_in_probe_addr; 4891eaf3e1SJohn Birrell extern int dtrace_in_probe; 4991eaf3e1SJohn Birrell 5057d025c3SGeorge V. Neville-Neil extern void dtrace_getnanotime(struct timespec *tsp); 5157d025c3SGeorge V. Neville-Neil 5291eaf3e1SJohn Birrell int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t); 5391eaf3e1SJohn Birrell 5491eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr { 5591eaf3e1SJohn Birrell int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t); 5691eaf3e1SJohn Birrell struct dtrace_invop_hdlr *dtih_next; 5791eaf3e1SJohn Birrell } dtrace_invop_hdlr_t; 5891eaf3e1SJohn Birrell 5991eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr; 6091eaf3e1SJohn Birrell 6191eaf3e1SJohn Birrell int 6291eaf3e1SJohn Birrell dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax) 6391eaf3e1SJohn Birrell { 6491eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 6591eaf3e1SJohn Birrell int rval; 6691eaf3e1SJohn Birrell 6791eaf3e1SJohn Birrell for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 6891eaf3e1SJohn Birrell if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) 6991eaf3e1SJohn Birrell return (rval); 7091eaf3e1SJohn Birrell 7191eaf3e1SJohn Birrell return (0); 7291eaf3e1SJohn Birrell } 7391eaf3e1SJohn Birrell 7491eaf3e1SJohn Birrell void 7591eaf3e1SJohn Birrell dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 7691eaf3e1SJohn Birrell { 7791eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 7891eaf3e1SJohn Birrell 7991eaf3e1SJohn Birrell hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 8091eaf3e1SJohn Birrell hdlr->dtih_func = func; 8191eaf3e1SJohn Birrell hdlr->dtih_next = dtrace_invop_hdlr; 8291eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr; 8391eaf3e1SJohn Birrell } 8491eaf3e1SJohn Birrell 8591eaf3e1SJohn Birrell void 8691eaf3e1SJohn Birrell dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 8791eaf3e1SJohn Birrell { 8891eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 8991eaf3e1SJohn Birrell 9091eaf3e1SJohn Birrell for (;;) { 9191eaf3e1SJohn Birrell if (hdlr == NULL) 9291eaf3e1SJohn Birrell panic("attempt to remove non-existent invop handler"); 9391eaf3e1SJohn Birrell 9491eaf3e1SJohn Birrell if (hdlr->dtih_func == func) 9591eaf3e1SJohn Birrell break; 9691eaf3e1SJohn Birrell 9791eaf3e1SJohn Birrell prev = hdlr; 9891eaf3e1SJohn Birrell hdlr = hdlr->dtih_next; 9991eaf3e1SJohn Birrell } 10091eaf3e1SJohn Birrell 10191eaf3e1SJohn Birrell if (prev == NULL) { 10291eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr == hdlr); 10391eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr->dtih_next; 10491eaf3e1SJohn Birrell } else { 10591eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr != hdlr); 10691eaf3e1SJohn Birrell prev->dtih_next = hdlr->dtih_next; 10791eaf3e1SJohn Birrell } 10891eaf3e1SJohn Birrell 10991eaf3e1SJohn Birrell kmem_free(hdlr, 0); 11091eaf3e1SJohn Birrell } 11191eaf3e1SJohn Birrell 11291eaf3e1SJohn Birrell /*ARGSUSED*/ 11391eaf3e1SJohn Birrell void 11491eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 11591eaf3e1SJohn Birrell { 11691eaf3e1SJohn Birrell (*func)(0, (uintptr_t) addr_PTmap); 11791eaf3e1SJohn Birrell } 11891eaf3e1SJohn Birrell 11991eaf3e1SJohn Birrell void 12091eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 12191eaf3e1SJohn Birrell { 12271a19bdcSAttilio Rao cpuset_t cpus; 12391eaf3e1SJohn Birrell 12491eaf3e1SJohn Birrell if (cpu == DTRACE_CPUALL) 12591eaf3e1SJohn Birrell cpus = all_cpus; 12691eaf3e1SJohn Birrell else 12771a19bdcSAttilio Rao CPU_SETOF(cpu, &cpus); 12891eaf3e1SJohn Birrell 129fe8c7b3dSAndriy Gapon smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func, 130fe8c7b3dSAndriy Gapon smp_no_rendevous_barrier, arg); 13191eaf3e1SJohn Birrell } 13291eaf3e1SJohn Birrell 13391eaf3e1SJohn Birrell static void 13491eaf3e1SJohn Birrell dtrace_sync_func(void) 13591eaf3e1SJohn Birrell { 13691eaf3e1SJohn Birrell } 13791eaf3e1SJohn Birrell 13891eaf3e1SJohn Birrell void 13991eaf3e1SJohn Birrell dtrace_sync(void) 14091eaf3e1SJohn Birrell { 14191eaf3e1SJohn Birrell dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 14291eaf3e1SJohn Birrell } 14391eaf3e1SJohn Birrell 14491eaf3e1SJohn Birrell #ifdef notyet 14591eaf3e1SJohn Birrell int (*dtrace_pid_probe_ptr)(struct regs *); 14691eaf3e1SJohn Birrell int (*dtrace_return_probe_ptr)(struct regs *); 14791eaf3e1SJohn Birrell 14891eaf3e1SJohn Birrell void 14991eaf3e1SJohn Birrell dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid) 15091eaf3e1SJohn Birrell { 15191eaf3e1SJohn Birrell krwlock_t *rwp; 15291eaf3e1SJohn Birrell proc_t *p = curproc; 15391eaf3e1SJohn Birrell extern void trap(struct regs *, caddr_t, processorid_t); 15491eaf3e1SJohn Birrell 15591eaf3e1SJohn Birrell if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) { 15691eaf3e1SJohn Birrell if (curthread->t_cred != p->p_cred) { 15791eaf3e1SJohn Birrell cred_t *oldcred = curthread->t_cred; 15891eaf3e1SJohn Birrell /* 15991eaf3e1SJohn Birrell * DTrace accesses t_cred in probe context. t_cred 16091eaf3e1SJohn Birrell * must always be either NULL, or point to a valid, 16191eaf3e1SJohn Birrell * allocated cred structure. 16291eaf3e1SJohn Birrell */ 16391eaf3e1SJohn Birrell curthread->t_cred = crgetcred(); 16491eaf3e1SJohn Birrell crfree(oldcred); 16591eaf3e1SJohn Birrell } 16691eaf3e1SJohn Birrell } 16791eaf3e1SJohn Birrell 16891eaf3e1SJohn Birrell if (rp->r_trapno == T_DTRACE_RET) { 16991eaf3e1SJohn Birrell uint8_t step = curthread->t_dtrace_step; 17091eaf3e1SJohn Birrell uint8_t ret = curthread->t_dtrace_ret; 17191eaf3e1SJohn Birrell uintptr_t npc = curthread->t_dtrace_npc; 17291eaf3e1SJohn Birrell 17391eaf3e1SJohn Birrell if (curthread->t_dtrace_ast) { 17491eaf3e1SJohn Birrell aston(curthread); 17591eaf3e1SJohn Birrell curthread->t_sig_check = 1; 17691eaf3e1SJohn Birrell } 17791eaf3e1SJohn Birrell 17891eaf3e1SJohn Birrell /* 17991eaf3e1SJohn Birrell * Clear all user tracing flags. 18091eaf3e1SJohn Birrell */ 18191eaf3e1SJohn Birrell curthread->t_dtrace_ft = 0; 18291eaf3e1SJohn Birrell 18391eaf3e1SJohn Birrell /* 18491eaf3e1SJohn Birrell * If we weren't expecting to take a return probe trap, kill 18591eaf3e1SJohn Birrell * the process as though it had just executed an unassigned 18691eaf3e1SJohn Birrell * trap instruction. 18791eaf3e1SJohn Birrell */ 18891eaf3e1SJohn Birrell if (step == 0) { 18991eaf3e1SJohn Birrell tsignal(curthread, SIGILL); 19091eaf3e1SJohn Birrell return; 19191eaf3e1SJohn Birrell } 19291eaf3e1SJohn Birrell 19391eaf3e1SJohn Birrell /* 19491eaf3e1SJohn Birrell * If we hit this trap unrelated to a return probe, we're 19591eaf3e1SJohn Birrell * just here to reset the AST flag since we deferred a signal 19691eaf3e1SJohn Birrell * until after we logically single-stepped the instruction we 19791eaf3e1SJohn Birrell * copied out. 19891eaf3e1SJohn Birrell */ 19991eaf3e1SJohn Birrell if (ret == 0) { 20091eaf3e1SJohn Birrell rp->r_pc = npc; 20191eaf3e1SJohn Birrell return; 20291eaf3e1SJohn Birrell } 20391eaf3e1SJohn Birrell 20491eaf3e1SJohn Birrell /* 20591eaf3e1SJohn Birrell * We need to wait until after we've called the 20691eaf3e1SJohn Birrell * dtrace_return_probe_ptr function pointer to set %pc. 20791eaf3e1SJohn Birrell */ 20891eaf3e1SJohn Birrell rwp = &CPU->cpu_ft_lock; 20991eaf3e1SJohn Birrell rw_enter(rwp, RW_READER); 21091eaf3e1SJohn Birrell if (dtrace_return_probe_ptr != NULL) 21191eaf3e1SJohn Birrell (void) (*dtrace_return_probe_ptr)(rp); 21291eaf3e1SJohn Birrell rw_exit(rwp); 21391eaf3e1SJohn Birrell rp->r_pc = npc; 21491eaf3e1SJohn Birrell 21591eaf3e1SJohn Birrell } else if (rp->r_trapno == T_BPTFLT) { 21691eaf3e1SJohn Birrell uint8_t instr; 21791eaf3e1SJohn Birrell rwp = &CPU->cpu_ft_lock; 21891eaf3e1SJohn Birrell 21991eaf3e1SJohn Birrell /* 22091eaf3e1SJohn Birrell * The DTrace fasttrap provider uses the breakpoint trap 22191eaf3e1SJohn Birrell * (int 3). We let DTrace take the first crack at handling 22291eaf3e1SJohn Birrell * this trap; if it's not a probe that DTrace knowns about, 22391eaf3e1SJohn Birrell * we call into the trap() routine to handle it like a 22491eaf3e1SJohn Birrell * breakpoint placed by a conventional debugger. 22591eaf3e1SJohn Birrell */ 22691eaf3e1SJohn Birrell rw_enter(rwp, RW_READER); 22791eaf3e1SJohn Birrell if (dtrace_pid_probe_ptr != NULL && 22891eaf3e1SJohn Birrell (*dtrace_pid_probe_ptr)(rp) == 0) { 22991eaf3e1SJohn Birrell rw_exit(rwp); 23091eaf3e1SJohn Birrell return; 23191eaf3e1SJohn Birrell } 23291eaf3e1SJohn Birrell rw_exit(rwp); 23391eaf3e1SJohn Birrell 23491eaf3e1SJohn Birrell /* 23591eaf3e1SJohn Birrell * If the instruction that caused the breakpoint trap doesn't 23691eaf3e1SJohn Birrell * look like an int 3 anymore, it may be that this tracepoint 23791eaf3e1SJohn Birrell * was removed just after the user thread executed it. In 23891eaf3e1SJohn Birrell * that case, return to user land to retry the instuction. 23991eaf3e1SJohn Birrell */ 24091eaf3e1SJohn Birrell if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 && 24191eaf3e1SJohn Birrell instr != FASTTRAP_INSTR) { 24291eaf3e1SJohn Birrell rp->r_pc--; 24391eaf3e1SJohn Birrell return; 24491eaf3e1SJohn Birrell } 24591eaf3e1SJohn Birrell 24691eaf3e1SJohn Birrell trap(rp, addr, cpuid); 24791eaf3e1SJohn Birrell 24891eaf3e1SJohn Birrell } else { 24991eaf3e1SJohn Birrell trap(rp, addr, cpuid); 25091eaf3e1SJohn Birrell } 25191eaf3e1SJohn Birrell } 25291eaf3e1SJohn Birrell 25391eaf3e1SJohn Birrell void 25491eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void) 25591eaf3e1SJohn Birrell { 25691eaf3e1SJohn Birrell kthread_t *t = curthread; 25791eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 25891eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 25991eaf3e1SJohn Birrell 26091eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 26191eaf3e1SJohn Birrell 26291eaf3e1SJohn Birrell /* 26391eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 26491eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. If the instruction 26591eaf3e1SJohn Birrell * we copied out caused a synchonous trap, reset the pc back to its 26691eaf3e1SJohn Birrell * original value and turn off the flags. 26791eaf3e1SJohn Birrell */ 26891eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 26991eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 27091eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 27191eaf3e1SJohn Birrell } else if (rp->r_pc == t->t_dtrace_scrpc || 27291eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_astpc) { 27391eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_pc; 27491eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 27591eaf3e1SJohn Birrell } 27691eaf3e1SJohn Birrell } 27791eaf3e1SJohn Birrell 27891eaf3e1SJohn Birrell int 27991eaf3e1SJohn Birrell dtrace_safe_defer_signal(void) 28091eaf3e1SJohn Birrell { 28191eaf3e1SJohn Birrell kthread_t *t = curthread; 28291eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 28391eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 28491eaf3e1SJohn Birrell 28591eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 28691eaf3e1SJohn Birrell 28791eaf3e1SJohn Birrell /* 28891eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 28991eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. 29091eaf3e1SJohn Birrell */ 29191eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 29291eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 29391eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 29491eaf3e1SJohn Birrell return (0); 29591eaf3e1SJohn Birrell } 29691eaf3e1SJohn Birrell 29791eaf3e1SJohn Birrell /* 2984737d389SGeorge V. Neville-Neil * If we have executed the original instruction, but we have performed 2994737d389SGeorge V. Neville-Neil * neither the jmp back to t->t_dtrace_npc nor the clean up of any 3004737d389SGeorge V. Neville-Neil * registers used to emulate %rip-relative instructions in 64-bit mode, 3014737d389SGeorge V. Neville-Neil * we'll save ourselves some effort by doing that here and taking the 3024737d389SGeorge V. Neville-Neil * signal right away. We detect this condition by seeing if the program 3034737d389SGeorge V. Neville-Neil * counter is the range [scrpc + isz, astpc). 30491eaf3e1SJohn Birrell */ 3054737d389SGeorge V. Neville-Neil if (rp->r_pc >= t->t_dtrace_scrpc + isz && 3064737d389SGeorge V. Neville-Neil rp->r_pc < t->t_dtrace_astpc) { 30791eaf3e1SJohn Birrell #ifdef __amd64 30891eaf3e1SJohn Birrell /* 30991eaf3e1SJohn Birrell * If there is a scratch register and we're on the 31091eaf3e1SJohn Birrell * instruction immediately after the modified instruction, 31191eaf3e1SJohn Birrell * restore the value of that scratch register. 31291eaf3e1SJohn Birrell */ 31391eaf3e1SJohn Birrell if (t->t_dtrace_reg != 0 && 31491eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_scrpc + isz) { 31591eaf3e1SJohn Birrell switch (t->t_dtrace_reg) { 31691eaf3e1SJohn Birrell case REG_RAX: 31791eaf3e1SJohn Birrell rp->r_rax = t->t_dtrace_regv; 31891eaf3e1SJohn Birrell break; 31991eaf3e1SJohn Birrell case REG_RCX: 32091eaf3e1SJohn Birrell rp->r_rcx = t->t_dtrace_regv; 32191eaf3e1SJohn Birrell break; 32291eaf3e1SJohn Birrell case REG_R8: 32391eaf3e1SJohn Birrell rp->r_r8 = t->t_dtrace_regv; 32491eaf3e1SJohn Birrell break; 32591eaf3e1SJohn Birrell case REG_R9: 32691eaf3e1SJohn Birrell rp->r_r9 = t->t_dtrace_regv; 32791eaf3e1SJohn Birrell break; 32891eaf3e1SJohn Birrell } 32991eaf3e1SJohn Birrell } 33091eaf3e1SJohn Birrell #endif 33191eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_npc; 33291eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 33391eaf3e1SJohn Birrell return (0); 33491eaf3e1SJohn Birrell } 33591eaf3e1SJohn Birrell 33691eaf3e1SJohn Birrell /* 33791eaf3e1SJohn Birrell * Otherwise, make sure we'll return to the kernel after executing 33891eaf3e1SJohn Birrell * the copied out instruction and defer the signal. 33991eaf3e1SJohn Birrell */ 34091eaf3e1SJohn Birrell if (!t->t_dtrace_step) { 34191eaf3e1SJohn Birrell ASSERT(rp->r_pc < t->t_dtrace_astpc); 34291eaf3e1SJohn Birrell rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 34391eaf3e1SJohn Birrell t->t_dtrace_step = 1; 34491eaf3e1SJohn Birrell } 34591eaf3e1SJohn Birrell 34691eaf3e1SJohn Birrell t->t_dtrace_ast = 1; 34791eaf3e1SJohn Birrell 34891eaf3e1SJohn Birrell return (1); 34991eaf3e1SJohn Birrell } 35091eaf3e1SJohn Birrell #endif 35191eaf3e1SJohn Birrell 35291eaf3e1SJohn Birrell static int64_t tgt_cpu_tsc; 35391eaf3e1SJohn Birrell static int64_t hst_cpu_tsc; 35491eaf3e1SJohn Birrell static int64_t tsc_skew[MAXCPU]; 355b064b6d1SAndriy Gapon static uint64_t nsec_scale; 356b064b6d1SAndriy Gapon 357b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */ 358b064b6d1SAndriy Gapon #define SCALE_SHIFT 28 35991eaf3e1SJohn Birrell 36091eaf3e1SJohn Birrell static void 36191eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu(void *arg) 36291eaf3e1SJohn Birrell { 36391eaf3e1SJohn Birrell uintptr_t cpu = (uintptr_t) arg; 36491eaf3e1SJohn Birrell 36591eaf3e1SJohn Birrell if (cpu == curcpu) 36691eaf3e1SJohn Birrell tgt_cpu_tsc = rdtsc(); 36791eaf3e1SJohn Birrell else 36891eaf3e1SJohn Birrell hst_cpu_tsc = rdtsc(); 36991eaf3e1SJohn Birrell } 37091eaf3e1SJohn Birrell 37191eaf3e1SJohn Birrell static void 37291eaf3e1SJohn Birrell dtrace_gethrtime_init(void *arg) 37391eaf3e1SJohn Birrell { 3747becfa95SAndriy Gapon struct pcpu *pc; 375b064b6d1SAndriy Gapon uint64_t tsc_f; 37671a19bdcSAttilio Rao cpuset_t map; 37791eaf3e1SJohn Birrell int i; 378b064b6d1SAndriy Gapon 379b064b6d1SAndriy Gapon /* 380b064b6d1SAndriy Gapon * Get TSC frequency known at this moment. 381b064b6d1SAndriy Gapon * This should be constant if TSC is invariant. 382b064b6d1SAndriy Gapon * Otherwise tick->time conversion will be inaccurate, but 383b064b6d1SAndriy Gapon * will preserve monotonic property of TSC. 384b064b6d1SAndriy Gapon */ 3853453537fSJung-uk Kim tsc_f = atomic_load_acq_64(&tsc_freq); 386b064b6d1SAndriy Gapon 387b064b6d1SAndriy Gapon /* 388b064b6d1SAndriy Gapon * The following line checks that nsec_scale calculated below 389b064b6d1SAndriy Gapon * doesn't overflow 32-bit unsigned integer, so that it can multiply 390b064b6d1SAndriy Gapon * another 32-bit integer without overflowing 64-bit. 391b064b6d1SAndriy Gapon * Thus minimum supported TSC frequency is 62.5MHz. 392b064b6d1SAndriy Gapon */ 393b064b6d1SAndriy Gapon KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), ("TSC frequency is too low")); 394b064b6d1SAndriy Gapon 395b064b6d1SAndriy Gapon /* 396b064b6d1SAndriy Gapon * We scale up NANOSEC/tsc_f ratio to preserve as much precision 397b064b6d1SAndriy Gapon * as possible. 398b064b6d1SAndriy Gapon * 2^28 factor was chosen quite arbitrarily from practical 399b064b6d1SAndriy Gapon * considerations: 400b064b6d1SAndriy Gapon * - it supports TSC frequencies as low as 62.5MHz (see above); 401b064b6d1SAndriy Gapon * - it provides quite good precision (e < 0.01%) up to THz 402b064b6d1SAndriy Gapon * (terahertz) values; 403b064b6d1SAndriy Gapon */ 404b064b6d1SAndriy Gapon nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 40591eaf3e1SJohn Birrell 40691eaf3e1SJohn Birrell /* The current CPU is the reference one. */ 4077becfa95SAndriy Gapon sched_pin(); 40891eaf3e1SJohn Birrell tsc_skew[curcpu] = 0; 4093aa6d94eSJohn Baldwin CPU_FOREACH(i) { 41091eaf3e1SJohn Birrell if (i == curcpu) 41191eaf3e1SJohn Birrell continue; 41291eaf3e1SJohn Birrell 4137becfa95SAndriy Gapon pc = pcpu_find(i); 414ada5b739SAttilio Rao CPU_SETOF(PCPU_GET(cpuid), &map); 415ada5b739SAttilio Rao CPU_SET(pc->pc_cpuid, &map); 41691eaf3e1SJohn Birrell 417d9b8935fSAndriy Gapon smp_rendezvous_cpus(map, NULL, 41891eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu, 41991eaf3e1SJohn Birrell smp_no_rendevous_barrier, (void *)(uintptr_t) i); 42091eaf3e1SJohn Birrell 42191eaf3e1SJohn Birrell tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 42291eaf3e1SJohn Birrell } 4237becfa95SAndriy Gapon sched_unpin(); 42491eaf3e1SJohn Birrell } 42591eaf3e1SJohn Birrell 42691eaf3e1SJohn Birrell SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL); 42791eaf3e1SJohn Birrell 42891eaf3e1SJohn Birrell /* 42991eaf3e1SJohn Birrell * DTrace needs a high resolution time function which can 43091eaf3e1SJohn Birrell * be called from a probe context and guaranteed not to have 43191eaf3e1SJohn Birrell * instrumented with probes itself. 43291eaf3e1SJohn Birrell * 43391eaf3e1SJohn Birrell * Returns nanoseconds since boot. 43491eaf3e1SJohn Birrell */ 43591eaf3e1SJohn Birrell uint64_t 43691eaf3e1SJohn Birrell dtrace_gethrtime() 43791eaf3e1SJohn Birrell { 438b064b6d1SAndriy Gapon uint64_t tsc; 439b064b6d1SAndriy Gapon uint32_t lo; 440b064b6d1SAndriy Gapon uint32_t hi; 441b064b6d1SAndriy Gapon 442b064b6d1SAndriy Gapon /* 443b064b6d1SAndriy Gapon * We split TSC value into lower and higher 32-bit halves and separately 444b064b6d1SAndriy Gapon * scale them with nsec_scale, then we scale them down by 2^28 445b064b6d1SAndriy Gapon * (see nsec_scale calculations) taking into account 32-bit shift of 446b064b6d1SAndriy Gapon * the higher half and finally add. 447b064b6d1SAndriy Gapon */ 448db5c7d36SZachary Loafman tsc = rdtsc() - tsc_skew[curcpu]; 449b064b6d1SAndriy Gapon lo = tsc; 450b064b6d1SAndriy Gapon hi = tsc >> 32; 451b064b6d1SAndriy Gapon return (((lo * nsec_scale) >> SCALE_SHIFT) + 452b064b6d1SAndriy Gapon ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 45391eaf3e1SJohn Birrell } 45491eaf3e1SJohn Birrell 45591eaf3e1SJohn Birrell uint64_t 45691eaf3e1SJohn Birrell dtrace_gethrestime(void) 45791eaf3e1SJohn Birrell { 45857d025c3SGeorge V. Neville-Neil struct timespec current_time; 45957d025c3SGeorge V. Neville-Neil 46057d025c3SGeorge V. Neville-Neil dtrace_getnanotime(¤t_time); 46157d025c3SGeorge V. Neville-Neil 462d638e8dcSGeorge V. Neville-Neil return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec); 46391eaf3e1SJohn Birrell } 46491eaf3e1SJohn Birrell 465*5a5f9d21SMark Johnston /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */ 46691eaf3e1SJohn Birrell int 467291624fdSMark Johnston dtrace_trap(struct trapframe *frame) 46891eaf3e1SJohn Birrell { 46991eaf3e1SJohn Birrell /* 47091eaf3e1SJohn Birrell * A trap can occur while DTrace executes a probe. Before 47191eaf3e1SJohn Birrell * executing the probe, DTrace blocks re-scheduling and sets 472291624fdSMark Johnston * a flag in its per-cpu flags to indicate that it doesn't 4736bccea7cSRebecca Cran * want to fault. On returning from the probe, the no-fault 47491eaf3e1SJohn Birrell * flag is cleared and finally re-scheduling is enabled. 47591eaf3e1SJohn Birrell * 47691eaf3e1SJohn Birrell * Check if DTrace has enabled 'no-fault' mode: 47791eaf3e1SJohn Birrell */ 47891eaf3e1SJohn Birrell if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 47991eaf3e1SJohn Birrell /* 48091eaf3e1SJohn Birrell * There are only a couple of trap types that are expected. 48191eaf3e1SJohn Birrell * All the rest will be handled in the usual way. 48291eaf3e1SJohn Birrell */ 483291624fdSMark Johnston switch (frame->tf_trapno) { 48491eaf3e1SJohn Birrell /* General protection fault. */ 48591eaf3e1SJohn Birrell case T_PROTFLT: 48691eaf3e1SJohn Birrell /* Flag an illegal operation. */ 48791eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 48891eaf3e1SJohn Birrell 48991eaf3e1SJohn Birrell /* 49091eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 49191eaf3e1SJohn Birrell * following the one causing the fault. 49291eaf3e1SJohn Birrell */ 49391eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 49491eaf3e1SJohn Birrell return (1); 49591eaf3e1SJohn Birrell /* Page fault. */ 49691eaf3e1SJohn Birrell case T_PAGEFLT: 49791eaf3e1SJohn Birrell /* Flag a bad address. */ 49891eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 49991eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr; 50091eaf3e1SJohn Birrell 50191eaf3e1SJohn Birrell /* 50291eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 50391eaf3e1SJohn Birrell * following the one causing the fault. 50491eaf3e1SJohn Birrell */ 50591eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 50691eaf3e1SJohn Birrell return (1); 50791eaf3e1SJohn Birrell default: 50891eaf3e1SJohn Birrell /* Handle all other traps in the usual way. */ 50991eaf3e1SJohn Birrell break; 51091eaf3e1SJohn Birrell } 51191eaf3e1SJohn Birrell } 51291eaf3e1SJohn Birrell 51391eaf3e1SJohn Birrell /* Handle the trap in the usual way. */ 51491eaf3e1SJohn Birrell return (0); 51591eaf3e1SJohn Birrell } 516