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 3091eaf3e1SJohn Birrell #include <sys/param.h> 3191eaf3e1SJohn Birrell #include <sys/systm.h> 3291eaf3e1SJohn Birrell #include <sys/types.h> 3391eaf3e1SJohn Birrell #include <sys/kernel.h> 3491eaf3e1SJohn Birrell #include <sys/malloc.h> 3591eaf3e1SJohn Birrell #include <sys/kmem.h> 3691eaf3e1SJohn Birrell #include <sys/smp.h> 3791eaf3e1SJohn Birrell #include <sys/dtrace_impl.h> 3891eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h> 3991eaf3e1SJohn Birrell #include <machine/clock.h> 4091eaf3e1SJohn Birrell #include <machine/frame.h> 4191eaf3e1SJohn Birrell #include <vm/pmap.h> 4291eaf3e1SJohn Birrell 4391eaf3e1SJohn Birrell extern uintptr_t dtrace_in_probe_addr; 4491eaf3e1SJohn Birrell extern int dtrace_in_probe; 4591eaf3e1SJohn Birrell 4691eaf3e1SJohn Birrell int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t); 4791eaf3e1SJohn Birrell 4891eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr { 4991eaf3e1SJohn Birrell int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t); 5091eaf3e1SJohn Birrell struct dtrace_invop_hdlr *dtih_next; 5191eaf3e1SJohn Birrell } dtrace_invop_hdlr_t; 5291eaf3e1SJohn Birrell 5391eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr; 5491eaf3e1SJohn Birrell 5591eaf3e1SJohn Birrell int 5691eaf3e1SJohn Birrell dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax) 5791eaf3e1SJohn Birrell { 5891eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 5991eaf3e1SJohn Birrell int rval; 6091eaf3e1SJohn Birrell 6191eaf3e1SJohn Birrell for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) 6291eaf3e1SJohn Birrell if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) 6391eaf3e1SJohn Birrell return (rval); 6491eaf3e1SJohn Birrell 6591eaf3e1SJohn Birrell return (0); 6691eaf3e1SJohn Birrell } 6791eaf3e1SJohn Birrell 6891eaf3e1SJohn Birrell void 6991eaf3e1SJohn Birrell dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 7091eaf3e1SJohn Birrell { 7191eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr; 7291eaf3e1SJohn Birrell 7391eaf3e1SJohn Birrell hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); 7491eaf3e1SJohn Birrell hdlr->dtih_func = func; 7591eaf3e1SJohn Birrell hdlr->dtih_next = dtrace_invop_hdlr; 7691eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr; 7791eaf3e1SJohn Birrell } 7891eaf3e1SJohn Birrell 7991eaf3e1SJohn Birrell void 8091eaf3e1SJohn Birrell dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t)) 8191eaf3e1SJohn Birrell { 8291eaf3e1SJohn Birrell dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL; 8391eaf3e1SJohn Birrell 8491eaf3e1SJohn Birrell for (;;) { 8591eaf3e1SJohn Birrell if (hdlr == NULL) 8691eaf3e1SJohn Birrell panic("attempt to remove non-existent invop handler"); 8791eaf3e1SJohn Birrell 8891eaf3e1SJohn Birrell if (hdlr->dtih_func == func) 8991eaf3e1SJohn Birrell break; 9091eaf3e1SJohn Birrell 9191eaf3e1SJohn Birrell prev = hdlr; 9291eaf3e1SJohn Birrell hdlr = hdlr->dtih_next; 9391eaf3e1SJohn Birrell } 9491eaf3e1SJohn Birrell 9591eaf3e1SJohn Birrell if (prev == NULL) { 9691eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr == hdlr); 9791eaf3e1SJohn Birrell dtrace_invop_hdlr = hdlr->dtih_next; 9891eaf3e1SJohn Birrell } else { 9991eaf3e1SJohn Birrell ASSERT(dtrace_invop_hdlr != hdlr); 10091eaf3e1SJohn Birrell prev->dtih_next = hdlr->dtih_next; 10191eaf3e1SJohn Birrell } 10291eaf3e1SJohn Birrell 10391eaf3e1SJohn Birrell kmem_free(hdlr, 0); 10491eaf3e1SJohn Birrell } 10591eaf3e1SJohn Birrell 10691eaf3e1SJohn Birrell /*ARGSUSED*/ 10791eaf3e1SJohn Birrell void 10891eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 10991eaf3e1SJohn Birrell { 11091eaf3e1SJohn Birrell (*func)(0, (uintptr_t) addr_PTmap); 11191eaf3e1SJohn Birrell } 11291eaf3e1SJohn Birrell 11391eaf3e1SJohn Birrell void 11491eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 11591eaf3e1SJohn Birrell { 11691eaf3e1SJohn Birrell cpumask_t cpus; 11791eaf3e1SJohn Birrell 11891eaf3e1SJohn Birrell critical_enter(); 11991eaf3e1SJohn Birrell 12091eaf3e1SJohn Birrell if (cpu == DTRACE_CPUALL) 12191eaf3e1SJohn Birrell cpus = all_cpus; 12291eaf3e1SJohn Birrell else 12391eaf3e1SJohn Birrell cpus = (cpumask_t) (1 << cpu); 12491eaf3e1SJohn Birrell 12591eaf3e1SJohn Birrell /* If the current CPU is in the set, call the function directly: */ 12691eaf3e1SJohn Birrell if ((cpus & (1 << curcpu)) != 0) { 12791eaf3e1SJohn Birrell (*func)(arg); 12891eaf3e1SJohn Birrell 12991eaf3e1SJohn Birrell /* Mask the current CPU from the set */ 13091eaf3e1SJohn Birrell cpus &= ~(1 << curcpu); 13191eaf3e1SJohn Birrell } 13291eaf3e1SJohn Birrell 13391eaf3e1SJohn Birrell /* If there are any CPUs in the set, cross-call to those CPUs */ 13491eaf3e1SJohn Birrell if (cpus != 0) 13591eaf3e1SJohn Birrell smp_rendezvous_cpus(cpus, NULL, func, smp_no_rendevous_barrier, arg); 13691eaf3e1SJohn Birrell 13791eaf3e1SJohn Birrell critical_exit(); 13891eaf3e1SJohn Birrell } 13991eaf3e1SJohn Birrell 14091eaf3e1SJohn Birrell static void 14191eaf3e1SJohn Birrell dtrace_sync_func(void) 14291eaf3e1SJohn Birrell { 14391eaf3e1SJohn Birrell } 14491eaf3e1SJohn Birrell 14591eaf3e1SJohn Birrell void 14691eaf3e1SJohn Birrell dtrace_sync(void) 14791eaf3e1SJohn Birrell { 14891eaf3e1SJohn Birrell dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); 14991eaf3e1SJohn Birrell } 15091eaf3e1SJohn Birrell 15191eaf3e1SJohn Birrell #ifdef notyet 15291eaf3e1SJohn Birrell int (*dtrace_fasttrap_probe_ptr)(struct regs *); 15391eaf3e1SJohn Birrell int (*dtrace_pid_probe_ptr)(struct regs *); 15491eaf3e1SJohn Birrell int (*dtrace_return_probe_ptr)(struct regs *); 15591eaf3e1SJohn Birrell 15691eaf3e1SJohn Birrell void 15791eaf3e1SJohn Birrell dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid) 15891eaf3e1SJohn Birrell { 15991eaf3e1SJohn Birrell krwlock_t *rwp; 16091eaf3e1SJohn Birrell proc_t *p = curproc; 16191eaf3e1SJohn Birrell extern void trap(struct regs *, caddr_t, processorid_t); 16291eaf3e1SJohn Birrell 16391eaf3e1SJohn Birrell if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) { 16491eaf3e1SJohn Birrell if (curthread->t_cred != p->p_cred) { 16591eaf3e1SJohn Birrell cred_t *oldcred = curthread->t_cred; 16691eaf3e1SJohn Birrell /* 16791eaf3e1SJohn Birrell * DTrace accesses t_cred in probe context. t_cred 16891eaf3e1SJohn Birrell * must always be either NULL, or point to a valid, 16991eaf3e1SJohn Birrell * allocated cred structure. 17091eaf3e1SJohn Birrell */ 17191eaf3e1SJohn Birrell curthread->t_cred = crgetcred(); 17291eaf3e1SJohn Birrell crfree(oldcred); 17391eaf3e1SJohn Birrell } 17491eaf3e1SJohn Birrell } 17591eaf3e1SJohn Birrell 17691eaf3e1SJohn Birrell if (rp->r_trapno == T_DTRACE_RET) { 17791eaf3e1SJohn Birrell uint8_t step = curthread->t_dtrace_step; 17891eaf3e1SJohn Birrell uint8_t ret = curthread->t_dtrace_ret; 17991eaf3e1SJohn Birrell uintptr_t npc = curthread->t_dtrace_npc; 18091eaf3e1SJohn Birrell 18191eaf3e1SJohn Birrell if (curthread->t_dtrace_ast) { 18291eaf3e1SJohn Birrell aston(curthread); 18391eaf3e1SJohn Birrell curthread->t_sig_check = 1; 18491eaf3e1SJohn Birrell } 18591eaf3e1SJohn Birrell 18691eaf3e1SJohn Birrell /* 18791eaf3e1SJohn Birrell * Clear all user tracing flags. 18891eaf3e1SJohn Birrell */ 18991eaf3e1SJohn Birrell curthread->t_dtrace_ft = 0; 19091eaf3e1SJohn Birrell 19191eaf3e1SJohn Birrell /* 19291eaf3e1SJohn Birrell * If we weren't expecting to take a return probe trap, kill 19391eaf3e1SJohn Birrell * the process as though it had just executed an unassigned 19491eaf3e1SJohn Birrell * trap instruction. 19591eaf3e1SJohn Birrell */ 19691eaf3e1SJohn Birrell if (step == 0) { 19791eaf3e1SJohn Birrell tsignal(curthread, SIGILL); 19891eaf3e1SJohn Birrell return; 19991eaf3e1SJohn Birrell } 20091eaf3e1SJohn Birrell 20191eaf3e1SJohn Birrell /* 20291eaf3e1SJohn Birrell * If we hit this trap unrelated to a return probe, we're 20391eaf3e1SJohn Birrell * just here to reset the AST flag since we deferred a signal 20491eaf3e1SJohn Birrell * until after we logically single-stepped the instruction we 20591eaf3e1SJohn Birrell * copied out. 20691eaf3e1SJohn Birrell */ 20791eaf3e1SJohn Birrell if (ret == 0) { 20891eaf3e1SJohn Birrell rp->r_pc = npc; 20991eaf3e1SJohn Birrell return; 21091eaf3e1SJohn Birrell } 21191eaf3e1SJohn Birrell 21291eaf3e1SJohn Birrell /* 21391eaf3e1SJohn Birrell * We need to wait until after we've called the 21491eaf3e1SJohn Birrell * dtrace_return_probe_ptr function pointer to set %pc. 21591eaf3e1SJohn Birrell */ 21691eaf3e1SJohn Birrell rwp = &CPU->cpu_ft_lock; 21791eaf3e1SJohn Birrell rw_enter(rwp, RW_READER); 21891eaf3e1SJohn Birrell if (dtrace_return_probe_ptr != NULL) 21991eaf3e1SJohn Birrell (void) (*dtrace_return_probe_ptr)(rp); 22091eaf3e1SJohn Birrell rw_exit(rwp); 22191eaf3e1SJohn Birrell rp->r_pc = npc; 22291eaf3e1SJohn Birrell 22391eaf3e1SJohn Birrell } else if (rp->r_trapno == T_DTRACE_PROBE) { 22491eaf3e1SJohn Birrell rwp = &CPU->cpu_ft_lock; 22591eaf3e1SJohn Birrell rw_enter(rwp, RW_READER); 22691eaf3e1SJohn Birrell if (dtrace_fasttrap_probe_ptr != NULL) 22791eaf3e1SJohn Birrell (void) (*dtrace_fasttrap_probe_ptr)(rp); 22891eaf3e1SJohn Birrell rw_exit(rwp); 22991eaf3e1SJohn Birrell 23091eaf3e1SJohn Birrell } else if (rp->r_trapno == T_BPTFLT) { 23191eaf3e1SJohn Birrell uint8_t instr; 23291eaf3e1SJohn Birrell rwp = &CPU->cpu_ft_lock; 23391eaf3e1SJohn Birrell 23491eaf3e1SJohn Birrell /* 23591eaf3e1SJohn Birrell * The DTrace fasttrap provider uses the breakpoint trap 23691eaf3e1SJohn Birrell * (int 3). We let DTrace take the first crack at handling 23791eaf3e1SJohn Birrell * this trap; if it's not a probe that DTrace knowns about, 23891eaf3e1SJohn Birrell * we call into the trap() routine to handle it like a 23991eaf3e1SJohn Birrell * breakpoint placed by a conventional debugger. 24091eaf3e1SJohn Birrell */ 24191eaf3e1SJohn Birrell rw_enter(rwp, RW_READER); 24291eaf3e1SJohn Birrell if (dtrace_pid_probe_ptr != NULL && 24391eaf3e1SJohn Birrell (*dtrace_pid_probe_ptr)(rp) == 0) { 24491eaf3e1SJohn Birrell rw_exit(rwp); 24591eaf3e1SJohn Birrell return; 24691eaf3e1SJohn Birrell } 24791eaf3e1SJohn Birrell rw_exit(rwp); 24891eaf3e1SJohn Birrell 24991eaf3e1SJohn Birrell /* 25091eaf3e1SJohn Birrell * If the instruction that caused the breakpoint trap doesn't 25191eaf3e1SJohn Birrell * look like an int 3 anymore, it may be that this tracepoint 25291eaf3e1SJohn Birrell * was removed just after the user thread executed it. In 25391eaf3e1SJohn Birrell * that case, return to user land to retry the instuction. 25491eaf3e1SJohn Birrell */ 25591eaf3e1SJohn Birrell if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 && 25691eaf3e1SJohn Birrell instr != FASTTRAP_INSTR) { 25791eaf3e1SJohn Birrell rp->r_pc--; 25891eaf3e1SJohn Birrell return; 25991eaf3e1SJohn Birrell } 26091eaf3e1SJohn Birrell 26191eaf3e1SJohn Birrell trap(rp, addr, cpuid); 26291eaf3e1SJohn Birrell 26391eaf3e1SJohn Birrell } else { 26491eaf3e1SJohn Birrell trap(rp, addr, cpuid); 26591eaf3e1SJohn Birrell } 26691eaf3e1SJohn Birrell } 26791eaf3e1SJohn Birrell 26891eaf3e1SJohn Birrell void 26991eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void) 27091eaf3e1SJohn Birrell { 27191eaf3e1SJohn Birrell kthread_t *t = curthread; 27291eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 27391eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 27491eaf3e1SJohn Birrell 27591eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 27691eaf3e1SJohn Birrell 27791eaf3e1SJohn Birrell /* 27891eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 27991eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. If the instruction 28091eaf3e1SJohn Birrell * we copied out caused a synchonous trap, reset the pc back to its 28191eaf3e1SJohn Birrell * original value and turn off the flags. 28291eaf3e1SJohn Birrell */ 28391eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 28491eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 28591eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 28691eaf3e1SJohn Birrell } else if (rp->r_pc == t->t_dtrace_scrpc || 28791eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_astpc) { 28891eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_pc; 28991eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 29091eaf3e1SJohn Birrell } 29191eaf3e1SJohn Birrell } 29291eaf3e1SJohn Birrell 29391eaf3e1SJohn Birrell int 29491eaf3e1SJohn Birrell dtrace_safe_defer_signal(void) 29591eaf3e1SJohn Birrell { 29691eaf3e1SJohn Birrell kthread_t *t = curthread; 29791eaf3e1SJohn Birrell struct regs *rp = lwptoregs(ttolwp(t)); 29891eaf3e1SJohn Birrell size_t isz = t->t_dtrace_npc - t->t_dtrace_pc; 29991eaf3e1SJohn Birrell 30091eaf3e1SJohn Birrell ASSERT(t->t_dtrace_on); 30191eaf3e1SJohn Birrell 30291eaf3e1SJohn Birrell /* 30391eaf3e1SJohn Birrell * If we're not in the range of scratch addresses, we're not actually 30491eaf3e1SJohn Birrell * tracing user instructions so turn off the flags. 30591eaf3e1SJohn Birrell */ 30691eaf3e1SJohn Birrell if (rp->r_pc < t->t_dtrace_scrpc || 30791eaf3e1SJohn Birrell rp->r_pc > t->t_dtrace_astpc + isz) { 30891eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 30991eaf3e1SJohn Birrell return (0); 31091eaf3e1SJohn Birrell } 31191eaf3e1SJohn Birrell 31291eaf3e1SJohn Birrell /* 31391eaf3e1SJohn Birrell * If we've executed the original instruction, but haven't performed 31491eaf3e1SJohn Birrell * the jmp back to t->t_dtrace_npc or the clean up of any registers 31591eaf3e1SJohn Birrell * used to emulate %rip-relative instructions in 64-bit mode, do that 31691eaf3e1SJohn Birrell * here and take the signal right away. We detect this condition by 31791eaf3e1SJohn Birrell * seeing if the program counter is the range [scrpc + isz, astpc). 31891eaf3e1SJohn Birrell */ 31991eaf3e1SJohn Birrell if (t->t_dtrace_astpc - rp->r_pc < 32091eaf3e1SJohn Birrell t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) { 32191eaf3e1SJohn Birrell #ifdef __amd64 32291eaf3e1SJohn Birrell /* 32391eaf3e1SJohn Birrell * If there is a scratch register and we're on the 32491eaf3e1SJohn Birrell * instruction immediately after the modified instruction, 32591eaf3e1SJohn Birrell * restore the value of that scratch register. 32691eaf3e1SJohn Birrell */ 32791eaf3e1SJohn Birrell if (t->t_dtrace_reg != 0 && 32891eaf3e1SJohn Birrell rp->r_pc == t->t_dtrace_scrpc + isz) { 32991eaf3e1SJohn Birrell switch (t->t_dtrace_reg) { 33091eaf3e1SJohn Birrell case REG_RAX: 33191eaf3e1SJohn Birrell rp->r_rax = t->t_dtrace_regv; 33291eaf3e1SJohn Birrell break; 33391eaf3e1SJohn Birrell case REG_RCX: 33491eaf3e1SJohn Birrell rp->r_rcx = t->t_dtrace_regv; 33591eaf3e1SJohn Birrell break; 33691eaf3e1SJohn Birrell case REG_R8: 33791eaf3e1SJohn Birrell rp->r_r8 = t->t_dtrace_regv; 33891eaf3e1SJohn Birrell break; 33991eaf3e1SJohn Birrell case REG_R9: 34091eaf3e1SJohn Birrell rp->r_r9 = t->t_dtrace_regv; 34191eaf3e1SJohn Birrell break; 34291eaf3e1SJohn Birrell } 34391eaf3e1SJohn Birrell } 34491eaf3e1SJohn Birrell #endif 34591eaf3e1SJohn Birrell rp->r_pc = t->t_dtrace_npc; 34691eaf3e1SJohn Birrell t->t_dtrace_ft = 0; 34791eaf3e1SJohn Birrell return (0); 34891eaf3e1SJohn Birrell } 34991eaf3e1SJohn Birrell 35091eaf3e1SJohn Birrell /* 35191eaf3e1SJohn Birrell * Otherwise, make sure we'll return to the kernel after executing 35291eaf3e1SJohn Birrell * the copied out instruction and defer the signal. 35391eaf3e1SJohn Birrell */ 35491eaf3e1SJohn Birrell if (!t->t_dtrace_step) { 35591eaf3e1SJohn Birrell ASSERT(rp->r_pc < t->t_dtrace_astpc); 35691eaf3e1SJohn Birrell rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc; 35791eaf3e1SJohn Birrell t->t_dtrace_step = 1; 35891eaf3e1SJohn Birrell } 35991eaf3e1SJohn Birrell 36091eaf3e1SJohn Birrell t->t_dtrace_ast = 1; 36191eaf3e1SJohn Birrell 36291eaf3e1SJohn Birrell return (1); 36391eaf3e1SJohn Birrell } 36491eaf3e1SJohn Birrell #endif 36591eaf3e1SJohn Birrell 36691eaf3e1SJohn Birrell static int64_t tgt_cpu_tsc; 36791eaf3e1SJohn Birrell static int64_t hst_cpu_tsc; 36891eaf3e1SJohn Birrell static int64_t tsc_skew[MAXCPU]; 369b064b6d1SAndriy Gapon static uint64_t nsec_scale; 370b064b6d1SAndriy Gapon 371b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */ 372b064b6d1SAndriy Gapon #define SCALE_SHIFT 28 37391eaf3e1SJohn Birrell 37491eaf3e1SJohn Birrell static void 37591eaf3e1SJohn Birrell dtrace_gethrtime_init_sync(void *arg) 37691eaf3e1SJohn Birrell { 37791eaf3e1SJohn Birrell #ifdef CHECK_SYNC 37891eaf3e1SJohn Birrell /* 37991eaf3e1SJohn Birrell * Delay this function from returning on one 38091eaf3e1SJohn Birrell * of the CPUs to check that the synchronisation 38191eaf3e1SJohn Birrell * works. 38291eaf3e1SJohn Birrell */ 38391eaf3e1SJohn Birrell uintptr_t cpu = (uintptr_t) arg; 38491eaf3e1SJohn Birrell 38591eaf3e1SJohn Birrell if (cpu == curcpu) { 38691eaf3e1SJohn Birrell int i; 38791eaf3e1SJohn Birrell for (i = 0; i < 1000000000; i++) 38891eaf3e1SJohn Birrell tgt_cpu_tsc = rdtsc(); 38991eaf3e1SJohn Birrell tgt_cpu_tsc = 0; 39091eaf3e1SJohn Birrell } 39191eaf3e1SJohn Birrell #endif 39291eaf3e1SJohn Birrell } 39391eaf3e1SJohn Birrell 39491eaf3e1SJohn Birrell static void 39591eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu(void *arg) 39691eaf3e1SJohn Birrell { 39791eaf3e1SJohn Birrell uintptr_t cpu = (uintptr_t) arg; 39891eaf3e1SJohn Birrell 39991eaf3e1SJohn Birrell if (cpu == curcpu) 40091eaf3e1SJohn Birrell tgt_cpu_tsc = rdtsc(); 40191eaf3e1SJohn Birrell else 40291eaf3e1SJohn Birrell hst_cpu_tsc = rdtsc(); 40391eaf3e1SJohn Birrell } 40491eaf3e1SJohn Birrell 40591eaf3e1SJohn Birrell static void 40691eaf3e1SJohn Birrell dtrace_gethrtime_init(void *arg) 40791eaf3e1SJohn Birrell { 408*7becfa95SAndriy Gapon struct pcpu *pc; 409b064b6d1SAndriy Gapon uint64_t tsc_f; 41091eaf3e1SJohn Birrell cpumask_t map; 41191eaf3e1SJohn Birrell int i; 412b064b6d1SAndriy Gapon 413b064b6d1SAndriy Gapon /* 414b064b6d1SAndriy Gapon * Get TSC frequency known at this moment. 415b064b6d1SAndriy Gapon * This should be constant if TSC is invariant. 416b064b6d1SAndriy Gapon * Otherwise tick->time conversion will be inaccurate, but 417b064b6d1SAndriy Gapon * will preserve monotonic property of TSC. 418b064b6d1SAndriy Gapon */ 419b064b6d1SAndriy Gapon tsc_f = tsc_freq; 420b064b6d1SAndriy Gapon 421b064b6d1SAndriy Gapon /* 422b064b6d1SAndriy Gapon * The following line checks that nsec_scale calculated below 423b064b6d1SAndriy Gapon * doesn't overflow 32-bit unsigned integer, so that it can multiply 424b064b6d1SAndriy Gapon * another 32-bit integer without overflowing 64-bit. 425b064b6d1SAndriy Gapon * Thus minimum supported TSC frequency is 62.5MHz. 426b064b6d1SAndriy Gapon */ 427b064b6d1SAndriy Gapon KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)), ("TSC frequency is too low")); 428b064b6d1SAndriy Gapon 429b064b6d1SAndriy Gapon /* 430b064b6d1SAndriy Gapon * We scale up NANOSEC/tsc_f ratio to preserve as much precision 431b064b6d1SAndriy Gapon * as possible. 432b064b6d1SAndriy Gapon * 2^28 factor was chosen quite arbitrarily from practical 433b064b6d1SAndriy Gapon * considerations: 434b064b6d1SAndriy Gapon * - it supports TSC frequencies as low as 62.5MHz (see above); 435b064b6d1SAndriy Gapon * - it provides quite good precision (e < 0.01%) up to THz 436b064b6d1SAndriy Gapon * (terahertz) values; 437b064b6d1SAndriy Gapon */ 438b064b6d1SAndriy Gapon nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f; 43991eaf3e1SJohn Birrell 44091eaf3e1SJohn Birrell /* The current CPU is the reference one. */ 441*7becfa95SAndriy Gapon sched_pin(); 44291eaf3e1SJohn Birrell tsc_skew[curcpu] = 0; 4433aa6d94eSJohn Baldwin CPU_FOREACH(i) { 44491eaf3e1SJohn Birrell if (i == curcpu) 44591eaf3e1SJohn Birrell continue; 44691eaf3e1SJohn Birrell 447*7becfa95SAndriy Gapon pc = pcpu_find(i); 448*7becfa95SAndriy Gapon map = PCPU_GET(cpumask) | pc->pc_cpumask; 44991eaf3e1SJohn Birrell 45091eaf3e1SJohn Birrell smp_rendezvous_cpus(map, dtrace_gethrtime_init_sync, 45191eaf3e1SJohn Birrell dtrace_gethrtime_init_cpu, 45291eaf3e1SJohn Birrell smp_no_rendevous_barrier, (void *)(uintptr_t) i); 45391eaf3e1SJohn Birrell 45491eaf3e1SJohn Birrell tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; 45591eaf3e1SJohn Birrell } 456*7becfa95SAndriy Gapon sched_unpin(); 45791eaf3e1SJohn Birrell } 45891eaf3e1SJohn Birrell 45991eaf3e1SJohn Birrell SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL); 46091eaf3e1SJohn Birrell 46191eaf3e1SJohn Birrell /* 46291eaf3e1SJohn Birrell * DTrace needs a high resolution time function which can 46391eaf3e1SJohn Birrell * be called from a probe context and guaranteed not to have 46491eaf3e1SJohn Birrell * instrumented with probes itself. 46591eaf3e1SJohn Birrell * 46691eaf3e1SJohn Birrell * Returns nanoseconds since boot. 46791eaf3e1SJohn Birrell */ 46891eaf3e1SJohn Birrell uint64_t 46991eaf3e1SJohn Birrell dtrace_gethrtime() 47091eaf3e1SJohn Birrell { 471b064b6d1SAndriy Gapon uint64_t tsc; 472b064b6d1SAndriy Gapon uint32_t lo; 473b064b6d1SAndriy Gapon uint32_t hi; 474b064b6d1SAndriy Gapon 475b064b6d1SAndriy Gapon /* 476b064b6d1SAndriy Gapon * We split TSC value into lower and higher 32-bit halves and separately 477b064b6d1SAndriy Gapon * scale them with nsec_scale, then we scale them down by 2^28 478b064b6d1SAndriy Gapon * (see nsec_scale calculations) taking into account 32-bit shift of 479b064b6d1SAndriy Gapon * the higher half and finally add. 480b064b6d1SAndriy Gapon */ 481b064b6d1SAndriy Gapon tsc = rdtsc() + tsc_skew[curcpu]; 482b064b6d1SAndriy Gapon lo = tsc; 483b064b6d1SAndriy Gapon hi = tsc >> 32; 484b064b6d1SAndriy Gapon return (((lo * nsec_scale) >> SCALE_SHIFT) + 485b064b6d1SAndriy Gapon ((hi * nsec_scale) << (32 - SCALE_SHIFT))); 48691eaf3e1SJohn Birrell } 48791eaf3e1SJohn Birrell 48891eaf3e1SJohn Birrell uint64_t 48991eaf3e1SJohn Birrell dtrace_gethrestime(void) 49091eaf3e1SJohn Birrell { 49191eaf3e1SJohn Birrell printf("%s(%d): XXX\n",__func__,__LINE__); 49291eaf3e1SJohn Birrell return (0); 49391eaf3e1SJohn Birrell } 49491eaf3e1SJohn Birrell 49591eaf3e1SJohn Birrell /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ 49691eaf3e1SJohn Birrell int 49791eaf3e1SJohn Birrell dtrace_trap(struct trapframe *frame, u_int type) 49891eaf3e1SJohn Birrell { 49991eaf3e1SJohn Birrell /* 50091eaf3e1SJohn Birrell * A trap can occur while DTrace executes a probe. Before 50191eaf3e1SJohn Birrell * executing the probe, DTrace blocks re-scheduling and sets 50291eaf3e1SJohn Birrell * a flag in it's per-cpu flags to indicate that it doesn't 50391eaf3e1SJohn Birrell * want to fault. On returning from the the probe, the no-fault 50491eaf3e1SJohn Birrell * flag is cleared and finally re-scheduling is enabled. 50591eaf3e1SJohn Birrell * 50691eaf3e1SJohn Birrell * Check if DTrace has enabled 'no-fault' mode: 50791eaf3e1SJohn Birrell * 50891eaf3e1SJohn Birrell */ 50991eaf3e1SJohn Birrell if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { 51091eaf3e1SJohn Birrell /* 51191eaf3e1SJohn Birrell * There are only a couple of trap types that are expected. 51291eaf3e1SJohn Birrell * All the rest will be handled in the usual way. 51391eaf3e1SJohn Birrell */ 51491eaf3e1SJohn Birrell switch (type) { 51591eaf3e1SJohn Birrell /* Privilieged instruction fault. */ 51691eaf3e1SJohn Birrell case T_PRIVINFLT: 51791eaf3e1SJohn Birrell break; 51891eaf3e1SJohn Birrell /* General protection fault. */ 51991eaf3e1SJohn Birrell case T_PROTFLT: 52091eaf3e1SJohn Birrell /* Flag an illegal operation. */ 52191eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 52291eaf3e1SJohn Birrell 52391eaf3e1SJohn Birrell /* 52491eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 52591eaf3e1SJohn Birrell * following the one causing the fault. 52691eaf3e1SJohn Birrell */ 52791eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 52891eaf3e1SJohn Birrell return (1); 52991eaf3e1SJohn Birrell /* Page fault. */ 53091eaf3e1SJohn Birrell case T_PAGEFLT: 53191eaf3e1SJohn Birrell /* Flag a bad address. */ 53291eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; 53391eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr; 53491eaf3e1SJohn Birrell 53591eaf3e1SJohn Birrell /* 53691eaf3e1SJohn Birrell * Offset the instruction pointer to the instruction 53791eaf3e1SJohn Birrell * following the one causing the fault. 53891eaf3e1SJohn Birrell */ 53991eaf3e1SJohn Birrell frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip); 54091eaf3e1SJohn Birrell return (1); 54191eaf3e1SJohn Birrell default: 54291eaf3e1SJohn Birrell /* Handle all other traps in the usual way. */ 54391eaf3e1SJohn Birrell break; 54491eaf3e1SJohn Birrell } 54591eaf3e1SJohn Birrell } 54691eaf3e1SJohn Birrell 54791eaf3e1SJohn Birrell /* Handle the trap in the usual way. */ 54891eaf3e1SJohn Birrell return (0); 54991eaf3e1SJohn Birrell } 550