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 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 2691eaf3e1SJohn Birrell * Use is subject to license terms. 2791eaf3e1SJohn Birrell */ 2891eaf3e1SJohn Birrell #include <sys/cdefs.h> 2991eaf3e1SJohn Birrell 3091eaf3e1SJohn Birrell #include <sys/param.h> 3191eaf3e1SJohn Birrell #include <sys/systm.h> 3291eaf3e1SJohn Birrell #include <sys/kernel.h> 3391eaf3e1SJohn Birrell #include <sys/stack.h> 3491eaf3e1SJohn Birrell #include <sys/pcpu.h> 3591eaf3e1SJohn Birrell 3691eaf3e1SJohn Birrell #include <machine/frame.h> 3791eaf3e1SJohn Birrell #include <machine/md_var.h> 3891eaf3e1SJohn Birrell #include <machine/reg.h> 3991eaf3e1SJohn Birrell #include <machine/stack.h> 4091eaf3e1SJohn Birrell 4191eaf3e1SJohn Birrell #include <vm/vm.h> 4291eaf3e1SJohn Birrell #include <vm/vm_param.h> 4391eaf3e1SJohn Birrell #include <vm/pmap.h> 4491eaf3e1SJohn Birrell 45c6f5742fSRui Paulo #include "regset.h" 4691eaf3e1SJohn Birrell 4791eaf3e1SJohn Birrell uint8_t dtrace_fuword8_nocheck(void *); 4891eaf3e1SJohn Birrell uint16_t dtrace_fuword16_nocheck(void *); 4991eaf3e1SJohn Birrell uint32_t dtrace_fuword32_nocheck(void *); 5091eaf3e1SJohn Birrell uint64_t dtrace_fuword64_nocheck(void *); 5191eaf3e1SJohn Birrell 5291eaf3e1SJohn Birrell void 5391eaf3e1SJohn Birrell dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, 5491eaf3e1SJohn Birrell uint32_t *intrpc) 5591eaf3e1SJohn Birrell { 5691eaf3e1SJohn Birrell int depth = 0; 5791eaf3e1SJohn Birrell register_t rbp; 5891eaf3e1SJohn Birrell struct amd64_frame *frame; 5991eaf3e1SJohn Birrell vm_offset_t callpc; 6091eaf3e1SJohn Birrell pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller; 6191eaf3e1SJohn Birrell 6291eaf3e1SJohn Birrell if (intrpc != 0) 6391eaf3e1SJohn Birrell pcstack[depth++] = (pc_t) intrpc; 6491eaf3e1SJohn Birrell 6591eaf3e1SJohn Birrell aframes++; 6691eaf3e1SJohn Birrell 6791eaf3e1SJohn Birrell __asm __volatile("movq %%rbp,%0" : "=r" (rbp)); 6891eaf3e1SJohn Birrell 6991eaf3e1SJohn Birrell frame = (struct amd64_frame *)rbp; 7091eaf3e1SJohn Birrell while (depth < pcstack_limit) { 7191eaf3e1SJohn Birrell if (!INKERNEL((long) frame)) 7291eaf3e1SJohn Birrell break; 7391eaf3e1SJohn Birrell 7491eaf3e1SJohn Birrell callpc = frame->f_retaddr; 7591eaf3e1SJohn Birrell 7691eaf3e1SJohn Birrell if (!INKERNEL(callpc)) 7791eaf3e1SJohn Birrell break; 7891eaf3e1SJohn Birrell 7991eaf3e1SJohn Birrell if (aframes > 0) { 8091eaf3e1SJohn Birrell aframes--; 8191eaf3e1SJohn Birrell if ((aframes == 0) && (caller != 0)) { 8291eaf3e1SJohn Birrell pcstack[depth++] = caller; 8391eaf3e1SJohn Birrell } 8491eaf3e1SJohn Birrell } 8591eaf3e1SJohn Birrell else { 8691eaf3e1SJohn Birrell pcstack[depth++] = callpc; 8791eaf3e1SJohn Birrell } 8891eaf3e1SJohn Birrell 8991eaf3e1SJohn Birrell if (frame->f_frame <= frame || 9091eaf3e1SJohn Birrell (vm_offset_t)frame->f_frame >= 9191eaf3e1SJohn Birrell (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE) 9291eaf3e1SJohn Birrell break; 9391eaf3e1SJohn Birrell frame = frame->f_frame; 9491eaf3e1SJohn Birrell } 9591eaf3e1SJohn Birrell 9691eaf3e1SJohn Birrell for (; depth < pcstack_limit; depth++) { 9791eaf3e1SJohn Birrell pcstack[depth] = 0; 9891eaf3e1SJohn Birrell } 9991eaf3e1SJohn Birrell } 10091eaf3e1SJohn Birrell 10191eaf3e1SJohn Birrell static int 10291eaf3e1SJohn Birrell dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc, 10391eaf3e1SJohn Birrell uintptr_t sp) 10491eaf3e1SJohn Birrell { 10591eaf3e1SJohn Birrell volatile uint16_t *flags = 10691eaf3e1SJohn Birrell (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 10791eaf3e1SJohn Birrell int ret = 0; 10891eaf3e1SJohn Birrell 10991eaf3e1SJohn Birrell ASSERT(pcstack == NULL || pcstack_limit > 0); 11091eaf3e1SJohn Birrell 111c6f5742fSRui Paulo while (pc != 0) { 11291eaf3e1SJohn Birrell ret++; 11391eaf3e1SJohn Birrell if (pcstack != NULL) { 11491eaf3e1SJohn Birrell *pcstack++ = (uint64_t)pc; 11591eaf3e1SJohn Birrell pcstack_limit--; 11691eaf3e1SJohn Birrell if (pcstack_limit <= 0) 11791eaf3e1SJohn Birrell break; 11891eaf3e1SJohn Birrell } 11991eaf3e1SJohn Birrell 120c6f5742fSRui Paulo if (sp == 0) 121c6f5742fSRui Paulo break; 12291eaf3e1SJohn Birrell 123c6f5742fSRui Paulo pc = dtrace_fuword64((void *)(sp + 124c6f5742fSRui Paulo offsetof(struct amd64_frame, f_retaddr))); 125c6f5742fSRui Paulo sp = dtrace_fuword64((void *)sp); 12691eaf3e1SJohn Birrell 12791eaf3e1SJohn Birrell /* 12891eaf3e1SJohn Birrell * This is totally bogus: if we faulted, we're going to clear 12991eaf3e1SJohn Birrell * the fault and break. This is to deal with the apparently 13091eaf3e1SJohn Birrell * broken Java stacks on x86. 13191eaf3e1SJohn Birrell */ 13291eaf3e1SJohn Birrell if (*flags & CPU_DTRACE_FAULT) { 13391eaf3e1SJohn Birrell *flags &= ~CPU_DTRACE_FAULT; 13491eaf3e1SJohn Birrell break; 13591eaf3e1SJohn Birrell } 13691eaf3e1SJohn Birrell } 13791eaf3e1SJohn Birrell 13891eaf3e1SJohn Birrell return (ret); 13991eaf3e1SJohn Birrell } 14091eaf3e1SJohn Birrell 14191eaf3e1SJohn Birrell void 14291eaf3e1SJohn Birrell dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit) 14391eaf3e1SJohn Birrell { 14491eaf3e1SJohn Birrell proc_t *p = curproc; 14591eaf3e1SJohn Birrell struct trapframe *tf; 146c6f5742fSRui Paulo uintptr_t pc, sp, fp; 14791eaf3e1SJohn Birrell volatile uint16_t *flags = 14891eaf3e1SJohn Birrell (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 14991eaf3e1SJohn Birrell int n; 15091eaf3e1SJohn Birrell 15191eaf3e1SJohn Birrell if (*flags & CPU_DTRACE_FAULT) 15291eaf3e1SJohn Birrell return; 15391eaf3e1SJohn Birrell 15491eaf3e1SJohn Birrell if (pcstack_limit <= 0) 15591eaf3e1SJohn Birrell return; 15691eaf3e1SJohn Birrell 15791eaf3e1SJohn Birrell /* 15891eaf3e1SJohn Birrell * If there's no user context we still need to zero the stack. 15991eaf3e1SJohn Birrell */ 16091eaf3e1SJohn Birrell if (p == NULL || (tf = curthread->td_frame) == NULL) 16191eaf3e1SJohn Birrell goto zero; 16291eaf3e1SJohn Birrell 16391eaf3e1SJohn Birrell *pcstack++ = (uint64_t)p->p_pid; 16491eaf3e1SJohn Birrell pcstack_limit--; 16591eaf3e1SJohn Birrell 16691eaf3e1SJohn Birrell if (pcstack_limit <= 0) 16791eaf3e1SJohn Birrell return; 16891eaf3e1SJohn Birrell 16991eaf3e1SJohn Birrell pc = tf->tf_rip; 170c6f5742fSRui Paulo fp = tf->tf_rbp; 17191eaf3e1SJohn Birrell sp = tf->tf_rsp; 17291eaf3e1SJohn Birrell 17391eaf3e1SJohn Birrell if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) { 174c6f5742fSRui Paulo /* 175c6f5742fSRui Paulo * In an entry probe. The frame pointer has not yet been 176c6f5742fSRui Paulo * pushed (that happens in the function prologue). The 177c6f5742fSRui Paulo * best approach is to add the current pc as a missing top 178c6f5742fSRui Paulo * of stack and back the pc up to the caller, which is stored 179c6f5742fSRui Paulo * at the current stack pointer address since the call 180c6f5742fSRui Paulo * instruction puts it there right before the branch. 181c6f5742fSRui Paulo */ 182c6f5742fSRui Paulo 18391eaf3e1SJohn Birrell *pcstack++ = (uint64_t)pc; 18491eaf3e1SJohn Birrell pcstack_limit--; 18591eaf3e1SJohn Birrell if (pcstack_limit <= 0) 18691eaf3e1SJohn Birrell return; 18791eaf3e1SJohn Birrell 188c6f5742fSRui Paulo pc = dtrace_fuword64((void *) sp); 18991eaf3e1SJohn Birrell } 19091eaf3e1SJohn Birrell 191c6f5742fSRui Paulo n = dtrace_getustack_common(pcstack, pcstack_limit, pc, fp); 19291eaf3e1SJohn Birrell ASSERT(n >= 0); 19391eaf3e1SJohn Birrell ASSERT(n <= pcstack_limit); 19491eaf3e1SJohn Birrell 19591eaf3e1SJohn Birrell pcstack += n; 19691eaf3e1SJohn Birrell pcstack_limit -= n; 19791eaf3e1SJohn Birrell 19891eaf3e1SJohn Birrell zero: 19991eaf3e1SJohn Birrell while (pcstack_limit-- > 0) 20091eaf3e1SJohn Birrell *pcstack++ = 0; 20191eaf3e1SJohn Birrell } 20291eaf3e1SJohn Birrell 20391eaf3e1SJohn Birrell int 20491eaf3e1SJohn Birrell dtrace_getustackdepth(void) 20591eaf3e1SJohn Birrell { 20691eaf3e1SJohn Birrell proc_t *p = curproc; 20791eaf3e1SJohn Birrell struct trapframe *tf; 208c6f5742fSRui Paulo uintptr_t pc, fp, sp; 20991eaf3e1SJohn Birrell int n = 0; 21091eaf3e1SJohn Birrell 21191eaf3e1SJohn Birrell if (p == NULL || (tf = curthread->td_frame) == NULL) 21291eaf3e1SJohn Birrell return (0); 21391eaf3e1SJohn Birrell 21491eaf3e1SJohn Birrell if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 21591eaf3e1SJohn Birrell return (-1); 21691eaf3e1SJohn Birrell 21791eaf3e1SJohn Birrell pc = tf->tf_rip; 218c6f5742fSRui Paulo fp = tf->tf_rbp; 21991eaf3e1SJohn Birrell sp = tf->tf_rsp; 22091eaf3e1SJohn Birrell 22191eaf3e1SJohn Birrell if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) { 222c6f5742fSRui Paulo /* 223c6f5742fSRui Paulo * In an entry probe. The frame pointer has not yet been 224c6f5742fSRui Paulo * pushed (that happens in the function prologue). The 225c6f5742fSRui Paulo * best approach is to add the current pc as a missing top 226c6f5742fSRui Paulo * of stack and back the pc up to the caller, which is stored 227c6f5742fSRui Paulo * at the current stack pointer address since the call 228c6f5742fSRui Paulo * instruction puts it there right before the branch. 229c6f5742fSRui Paulo */ 23091eaf3e1SJohn Birrell 231c6f5742fSRui Paulo pc = dtrace_fuword64((void *) sp); 232c6f5742fSRui Paulo n++; 23391eaf3e1SJohn Birrell } 23491eaf3e1SJohn Birrell 235c6f5742fSRui Paulo n += dtrace_getustack_common(NULL, 0, pc, fp); 23691eaf3e1SJohn Birrell 23791eaf3e1SJohn Birrell return (n); 23891eaf3e1SJohn Birrell } 23991eaf3e1SJohn Birrell 24091eaf3e1SJohn Birrell void 24191eaf3e1SJohn Birrell dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit) 24291eaf3e1SJohn Birrell { 24391eaf3e1SJohn Birrell proc_t *p = curproc; 244c6f5742fSRui Paulo struct trapframe *tf; 245c6f5742fSRui Paulo uintptr_t pc, sp, fp; 24691eaf3e1SJohn Birrell volatile uint16_t *flags = 24791eaf3e1SJohn Birrell (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 248c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */ 249c6f5742fSRui Paulo uintptr_t oldcontext; 25091eaf3e1SJohn Birrell size_t s1, s2; 251c6f5742fSRui Paulo #endif 25291eaf3e1SJohn Birrell 25391eaf3e1SJohn Birrell if (*flags & CPU_DTRACE_FAULT) 25491eaf3e1SJohn Birrell return; 25591eaf3e1SJohn Birrell 25691eaf3e1SJohn Birrell if (pcstack_limit <= 0) 25791eaf3e1SJohn Birrell return; 25891eaf3e1SJohn Birrell 25991eaf3e1SJohn Birrell /* 26091eaf3e1SJohn Birrell * If there's no user context we still need to zero the stack. 26191eaf3e1SJohn Birrell */ 262c6f5742fSRui Paulo if (p == NULL || (tf = curthread->td_frame) == NULL) 26391eaf3e1SJohn Birrell goto zero; 26491eaf3e1SJohn Birrell 26591eaf3e1SJohn Birrell *pcstack++ = (uint64_t)p->p_pid; 26691eaf3e1SJohn Birrell pcstack_limit--; 26791eaf3e1SJohn Birrell 26891eaf3e1SJohn Birrell if (pcstack_limit <= 0) 26991eaf3e1SJohn Birrell return; 27091eaf3e1SJohn Birrell 271c6f5742fSRui Paulo pc = tf->tf_rip; 272c6f5742fSRui Paulo sp = tf->tf_rsp; 273c6f5742fSRui Paulo fp = tf->tf_rbp; 27491eaf3e1SJohn Birrell 275c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */ 276c6f5742fSRui Paulo oldcontext = lwp->lwp_oldcontext; 27791eaf3e1SJohn Birrell s1 = sizeof (struct xframe) + 2 * sizeof (long); 27891eaf3e1SJohn Birrell s2 = s1 + sizeof (siginfo_t); 279c6f5742fSRui Paulo #endif 28091eaf3e1SJohn Birrell 28191eaf3e1SJohn Birrell if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) { 28291eaf3e1SJohn Birrell *pcstack++ = (uint64_t)pc; 28391eaf3e1SJohn Birrell *fpstack++ = 0; 28491eaf3e1SJohn Birrell pcstack_limit--; 28591eaf3e1SJohn Birrell if (pcstack_limit <= 0) 28691eaf3e1SJohn Birrell return; 28791eaf3e1SJohn Birrell 288c6f5742fSRui Paulo pc = dtrace_fuword64((void *)sp); 28991eaf3e1SJohn Birrell } 29091eaf3e1SJohn Birrell 291c6f5742fSRui Paulo while (pc != 0) { 29291eaf3e1SJohn Birrell *pcstack++ = (uint64_t)pc; 293c6f5742fSRui Paulo *fpstack++ = fp; 29491eaf3e1SJohn Birrell pcstack_limit--; 29591eaf3e1SJohn Birrell if (pcstack_limit <= 0) 29691eaf3e1SJohn Birrell break; 29791eaf3e1SJohn Birrell 298c6f5742fSRui Paulo if (fp == 0) 299c6f5742fSRui Paulo break; 300c6f5742fSRui Paulo 301c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */ 30291eaf3e1SJohn Birrell if (oldcontext == sp + s1 || oldcontext == sp + s2) { 30391eaf3e1SJohn Birrell ucontext_t *ucp = (ucontext_t *)oldcontext; 30491eaf3e1SJohn Birrell greg_t *gregs = ucp->uc_mcontext.gregs; 30591eaf3e1SJohn Birrell 30691eaf3e1SJohn Birrell sp = dtrace_fulword(&gregs[REG_FP]); 30791eaf3e1SJohn Birrell pc = dtrace_fulword(&gregs[REG_PC]); 30891eaf3e1SJohn Birrell 30991eaf3e1SJohn Birrell oldcontext = dtrace_fulword(&ucp->uc_link); 310c6f5742fSRui Paulo } else 311c6f5742fSRui Paulo #endif /* XXX */ 312c6f5742fSRui Paulo { 313c6f5742fSRui Paulo pc = dtrace_fuword64((void *)(fp + 314c6f5742fSRui Paulo offsetof(struct amd64_frame, f_retaddr))); 315c6f5742fSRui Paulo fp = dtrace_fuword64((void *)fp); 31691eaf3e1SJohn Birrell } 31791eaf3e1SJohn Birrell 31891eaf3e1SJohn Birrell /* 31991eaf3e1SJohn Birrell * This is totally bogus: if we faulted, we're going to clear 32091eaf3e1SJohn Birrell * the fault and break. This is to deal with the apparently 32191eaf3e1SJohn Birrell * broken Java stacks on x86. 32291eaf3e1SJohn Birrell */ 32391eaf3e1SJohn Birrell if (*flags & CPU_DTRACE_FAULT) { 32491eaf3e1SJohn Birrell *flags &= ~CPU_DTRACE_FAULT; 32591eaf3e1SJohn Birrell break; 32691eaf3e1SJohn Birrell } 32791eaf3e1SJohn Birrell } 32891eaf3e1SJohn Birrell 32991eaf3e1SJohn Birrell zero: 33091eaf3e1SJohn Birrell while (pcstack_limit-- > 0) 331c6f5742fSRui Paulo *pcstack++ = 0; 33291eaf3e1SJohn Birrell } 33391eaf3e1SJohn Birrell 33491eaf3e1SJohn Birrell /*ARGSUSED*/ 33591eaf3e1SJohn Birrell uint64_t 33691eaf3e1SJohn Birrell dtrace_getarg(int arg, int aframes) 33791eaf3e1SJohn Birrell { 33891eaf3e1SJohn Birrell uintptr_t val; 33991eaf3e1SJohn Birrell struct amd64_frame *fp = (struct amd64_frame *)dtrace_getfp(); 34091eaf3e1SJohn Birrell uintptr_t *stack; 34191eaf3e1SJohn Birrell int i; 34291eaf3e1SJohn Birrell 34391eaf3e1SJohn Birrell /* 34491eaf3e1SJohn Birrell * A total of 6 arguments are passed via registers; any argument with 34591eaf3e1SJohn Birrell * index of 5 or lower is therefore in a register. 34691eaf3e1SJohn Birrell */ 34791eaf3e1SJohn Birrell int inreg = 5; 34891eaf3e1SJohn Birrell 34991eaf3e1SJohn Birrell for (i = 1; i <= aframes; i++) { 35091eaf3e1SJohn Birrell fp = fp->f_frame; 35191eaf3e1SJohn Birrell 35291eaf3e1SJohn Birrell if (fp->f_retaddr == (long)dtrace_invop_callsite) { 35391eaf3e1SJohn Birrell /* 35491eaf3e1SJohn Birrell * In the case of amd64, we will use the pointer to the 35591eaf3e1SJohn Birrell * regs structure that was pushed when we took the 35691eaf3e1SJohn Birrell * trap. To get this structure, we must increment 35791eaf3e1SJohn Birrell * beyond the frame structure, and then again beyond 35891eaf3e1SJohn Birrell * the calling RIP stored in dtrace_invop(). If the 35991eaf3e1SJohn Birrell * argument that we're seeking is passed on the stack, 36091eaf3e1SJohn Birrell * we'll pull the true stack pointer out of the saved 36191eaf3e1SJohn Birrell * registers and decrement our argument by the number 36291eaf3e1SJohn Birrell * of arguments passed in registers; if the argument 36391eaf3e1SJohn Birrell * we're seeking is passed in regsiters, we can just 36491eaf3e1SJohn Birrell * load it directly. 36591eaf3e1SJohn Birrell */ 36691eaf3e1SJohn Birrell struct reg *rp = (struct reg *)((uintptr_t)&fp[1] + 36791eaf3e1SJohn Birrell sizeof (uintptr_t)); 36891eaf3e1SJohn Birrell 36991eaf3e1SJohn Birrell if (arg <= inreg) { 370*7e75d586SMark Johnston switch (arg) { 371*7e75d586SMark Johnston case 0: 37291eaf3e1SJohn Birrell stack = (uintptr_t *)&rp->r_rdi; 373*7e75d586SMark Johnston break; 374*7e75d586SMark Johnston case 1: 375*7e75d586SMark Johnston stack = (uintptr_t *)&rp->r_rsi; 376*7e75d586SMark Johnston break; 377*7e75d586SMark Johnston case 2: 378*7e75d586SMark Johnston stack = (uintptr_t *)&rp->r_rdx; 379*7e75d586SMark Johnston break; 380*7e75d586SMark Johnston case 3: 381*7e75d586SMark Johnston stack = (uintptr_t *)&rp->r_rcx; 382*7e75d586SMark Johnston break; 383*7e75d586SMark Johnston case 4: 384*7e75d586SMark Johnston stack = (uintptr_t *)&rp->r_r8; 385*7e75d586SMark Johnston break; 386*7e75d586SMark Johnston case 5: 387*7e75d586SMark Johnston stack = (uintptr_t *)&rp->r_r9; 388*7e75d586SMark Johnston break; 389*7e75d586SMark Johnston } 390*7e75d586SMark Johnston arg = 0; 39191eaf3e1SJohn Birrell } else { 39291eaf3e1SJohn Birrell stack = (uintptr_t *)(rp->r_rsp); 39391eaf3e1SJohn Birrell arg -= inreg; 39491eaf3e1SJohn Birrell } 39591eaf3e1SJohn Birrell goto load; 39691eaf3e1SJohn Birrell } 39791eaf3e1SJohn Birrell 39891eaf3e1SJohn Birrell } 39991eaf3e1SJohn Birrell 40091eaf3e1SJohn Birrell /* 40191eaf3e1SJohn Birrell * We know that we did not come through a trap to get into 40291eaf3e1SJohn Birrell * dtrace_probe() -- the provider simply called dtrace_probe() 40391eaf3e1SJohn Birrell * directly. As this is the case, we need to shift the argument 40491eaf3e1SJohn Birrell * that we're looking for: the probe ID is the first argument to 40591eaf3e1SJohn Birrell * dtrace_probe(), so the argument n will actually be found where 40691eaf3e1SJohn Birrell * one would expect to find argument (n + 1). 40791eaf3e1SJohn Birrell */ 40891eaf3e1SJohn Birrell arg++; 40991eaf3e1SJohn Birrell 41091eaf3e1SJohn Birrell if (arg <= inreg) { 41191eaf3e1SJohn Birrell /* 41291eaf3e1SJohn Birrell * This shouldn't happen. If the argument is passed in a 41391eaf3e1SJohn Birrell * register then it should have been, well, passed in a 41491eaf3e1SJohn Birrell * register... 41591eaf3e1SJohn Birrell */ 41691eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 41791eaf3e1SJohn Birrell return (0); 41891eaf3e1SJohn Birrell } 41991eaf3e1SJohn Birrell 42091eaf3e1SJohn Birrell arg -= (inreg + 1); 421f263e440SMark Johnston stack = (uintptr_t *)fp + 2; 42291eaf3e1SJohn Birrell 42391eaf3e1SJohn Birrell load: 42491eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 42591eaf3e1SJohn Birrell val = stack[arg]; 42691eaf3e1SJohn Birrell DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 42791eaf3e1SJohn Birrell 42891eaf3e1SJohn Birrell return (val); 42991eaf3e1SJohn Birrell return (0); 43091eaf3e1SJohn Birrell } 43191eaf3e1SJohn Birrell 43291eaf3e1SJohn Birrell int 43391eaf3e1SJohn Birrell dtrace_getstackdepth(int aframes) 43491eaf3e1SJohn Birrell { 43591eaf3e1SJohn Birrell int depth = 0; 43691eaf3e1SJohn Birrell struct amd64_frame *frame; 43791eaf3e1SJohn Birrell vm_offset_t rbp; 43891eaf3e1SJohn Birrell 43991eaf3e1SJohn Birrell aframes++; 44091eaf3e1SJohn Birrell rbp = dtrace_getfp(); 44191eaf3e1SJohn Birrell frame = (struct amd64_frame *)rbp; 44291eaf3e1SJohn Birrell depth++; 44391eaf3e1SJohn Birrell for(;;) { 44491eaf3e1SJohn Birrell if (!INKERNEL((long) frame)) 44591eaf3e1SJohn Birrell break; 44691eaf3e1SJohn Birrell if (!INKERNEL((long) frame->f_frame)) 44791eaf3e1SJohn Birrell break; 44891eaf3e1SJohn Birrell depth++; 44991eaf3e1SJohn Birrell if (frame->f_frame <= frame || 45091eaf3e1SJohn Birrell (vm_offset_t)frame->f_frame >= 45191eaf3e1SJohn Birrell (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE) 45291eaf3e1SJohn Birrell break; 45391eaf3e1SJohn Birrell frame = frame->f_frame; 45491eaf3e1SJohn Birrell } 45591eaf3e1SJohn Birrell if (depth < aframes) 45691eaf3e1SJohn Birrell return 0; 45791eaf3e1SJohn Birrell else 45891eaf3e1SJohn Birrell return depth - aframes; 45991eaf3e1SJohn Birrell } 46091eaf3e1SJohn Birrell 46191eaf3e1SJohn Birrell ulong_t 462c6f5742fSRui Paulo dtrace_getreg(struct trapframe *rp, uint_t reg) 46391eaf3e1SJohn Birrell { 464c6f5742fSRui Paulo /* This table is dependent on reg.d. */ 46591eaf3e1SJohn Birrell int regmap[] = { 466c6f5742fSRui Paulo REG_GS, /* 0 GS */ 467c6f5742fSRui Paulo REG_FS, /* 1 FS */ 468c6f5742fSRui Paulo REG_ES, /* 2 ES */ 469c6f5742fSRui Paulo REG_DS, /* 3 DS */ 470c6f5742fSRui Paulo REG_RDI, /* 4 EDI */ 471c6f5742fSRui Paulo REG_RSI, /* 5 ESI */ 472c6f5742fSRui Paulo REG_RBP, /* 6 EBP, REG_FP */ 473c6f5742fSRui Paulo REG_RSP, /* 7 ESP */ 474c6f5742fSRui Paulo REG_RBX, /* 8 EBX, REG_R1 */ 475c6f5742fSRui Paulo REG_RDX, /* 9 EDX */ 476c6f5742fSRui Paulo REG_RCX, /* 10 ECX */ 477c6f5742fSRui Paulo REG_RAX, /* 11 EAX, REG_R0 */ 478c6f5742fSRui Paulo REG_TRAPNO, /* 12 TRAPNO */ 479c6f5742fSRui Paulo REG_ERR, /* 13 ERR */ 480c6f5742fSRui Paulo REG_RIP, /* 14 EIP, REG_PC */ 481c6f5742fSRui Paulo REG_CS, /* 15 CS */ 482c6f5742fSRui Paulo REG_RFL, /* 16 EFL, REG_PS */ 483c6f5742fSRui Paulo REG_RSP, /* 17 UESP, REG_SP */ 484c6f5742fSRui Paulo REG_SS /* 18 SS */ 48591eaf3e1SJohn Birrell }; 48691eaf3e1SJohn Birrell 48791eaf3e1SJohn Birrell if (reg <= SS) { 48891eaf3e1SJohn Birrell if (reg >= sizeof (regmap) / sizeof (int)) { 48991eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 49091eaf3e1SJohn Birrell return (0); 49191eaf3e1SJohn Birrell } 49291eaf3e1SJohn Birrell 49391eaf3e1SJohn Birrell reg = regmap[reg]; 49491eaf3e1SJohn Birrell } else { 495c6f5742fSRui Paulo /* This is dependent on reg.d. */ 49691eaf3e1SJohn Birrell reg -= SS + 1; 49791eaf3e1SJohn Birrell } 49891eaf3e1SJohn Birrell 49991eaf3e1SJohn Birrell switch (reg) { 50091eaf3e1SJohn Birrell case REG_RDI: 501c6f5742fSRui Paulo return (rp->tf_rdi); 50291eaf3e1SJohn Birrell case REG_RSI: 503c6f5742fSRui Paulo return (rp->tf_rsi); 50491eaf3e1SJohn Birrell case REG_RDX: 505c6f5742fSRui Paulo return (rp->tf_rdx); 50691eaf3e1SJohn Birrell case REG_RCX: 507c6f5742fSRui Paulo return (rp->tf_rcx); 50891eaf3e1SJohn Birrell case REG_R8: 509c6f5742fSRui Paulo return (rp->tf_r8); 51091eaf3e1SJohn Birrell case REG_R9: 511c6f5742fSRui Paulo return (rp->tf_r9); 51291eaf3e1SJohn Birrell case REG_RAX: 513c6f5742fSRui Paulo return (rp->tf_rax); 51491eaf3e1SJohn Birrell case REG_RBX: 515c6f5742fSRui Paulo return (rp->tf_rbx); 51691eaf3e1SJohn Birrell case REG_RBP: 517c6f5742fSRui Paulo return (rp->tf_rbp); 51891eaf3e1SJohn Birrell case REG_R10: 519c6f5742fSRui Paulo return (rp->tf_r10); 52091eaf3e1SJohn Birrell case REG_R11: 521c6f5742fSRui Paulo return (rp->tf_r11); 52291eaf3e1SJohn Birrell case REG_R12: 523c6f5742fSRui Paulo return (rp->tf_r12); 52491eaf3e1SJohn Birrell case REG_R13: 525c6f5742fSRui Paulo return (rp->tf_r13); 52691eaf3e1SJohn Birrell case REG_R14: 527c6f5742fSRui Paulo return (rp->tf_r14); 52891eaf3e1SJohn Birrell case REG_R15: 529c6f5742fSRui Paulo return (rp->tf_r15); 53091eaf3e1SJohn Birrell case REG_DS: 531c6f5742fSRui Paulo return (rp->tf_ds); 53291eaf3e1SJohn Birrell case REG_ES: 533c6f5742fSRui Paulo return (rp->tf_es); 53491eaf3e1SJohn Birrell case REG_FS: 535c6f5742fSRui Paulo return (rp->tf_fs); 53691eaf3e1SJohn Birrell case REG_GS: 537c6f5742fSRui Paulo return (rp->tf_gs); 53891eaf3e1SJohn Birrell case REG_TRAPNO: 539c6f5742fSRui Paulo return (rp->tf_trapno); 54091eaf3e1SJohn Birrell case REG_ERR: 541c6f5742fSRui Paulo return (rp->tf_err); 54291eaf3e1SJohn Birrell case REG_RIP: 543c6f5742fSRui Paulo return (rp->tf_rip); 54491eaf3e1SJohn Birrell case REG_CS: 545c6f5742fSRui Paulo return (rp->tf_cs); 54691eaf3e1SJohn Birrell case REG_SS: 547c6f5742fSRui Paulo return (rp->tf_ss); 54891eaf3e1SJohn Birrell case REG_RFL: 549c6f5742fSRui Paulo return (rp->tf_rflags); 55091eaf3e1SJohn Birrell case REG_RSP: 551c6f5742fSRui Paulo return (rp->tf_rsp); 55291eaf3e1SJohn Birrell default: 55391eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 55491eaf3e1SJohn Birrell return (0); 55591eaf3e1SJohn Birrell } 55691eaf3e1SJohn Birrell } 55791eaf3e1SJohn Birrell 55891eaf3e1SJohn Birrell static int 55991eaf3e1SJohn Birrell dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size) 56091eaf3e1SJohn Birrell { 561f340e9feSAndriy Gapon ASSERT(INKERNEL(kaddr) && kaddr + size >= kaddr); 56291eaf3e1SJohn Birrell 563f340e9feSAndriy Gapon if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) { 56491eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 56591eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = uaddr; 56691eaf3e1SJohn Birrell return (0); 56791eaf3e1SJohn Birrell } 56891eaf3e1SJohn Birrell 56991eaf3e1SJohn Birrell return (1); 57091eaf3e1SJohn Birrell } 57191eaf3e1SJohn Birrell 57291eaf3e1SJohn Birrell void 57391eaf3e1SJohn Birrell dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size, 57491eaf3e1SJohn Birrell volatile uint16_t *flags) 57591eaf3e1SJohn Birrell { 57691eaf3e1SJohn Birrell if (dtrace_copycheck(uaddr, kaddr, size)) 57791eaf3e1SJohn Birrell dtrace_copy(uaddr, kaddr, size); 57891eaf3e1SJohn Birrell } 57991eaf3e1SJohn Birrell 58091eaf3e1SJohn Birrell void 58191eaf3e1SJohn Birrell dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size, 58291eaf3e1SJohn Birrell volatile uint16_t *flags) 58391eaf3e1SJohn Birrell { 58491eaf3e1SJohn Birrell if (dtrace_copycheck(uaddr, kaddr, size)) 58591eaf3e1SJohn Birrell dtrace_copy(kaddr, uaddr, size); 58691eaf3e1SJohn Birrell } 58791eaf3e1SJohn Birrell 58891eaf3e1SJohn Birrell void 58991eaf3e1SJohn Birrell dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size, 59091eaf3e1SJohn Birrell volatile uint16_t *flags) 59191eaf3e1SJohn Birrell { 59291eaf3e1SJohn Birrell if (dtrace_copycheck(uaddr, kaddr, size)) 59391eaf3e1SJohn Birrell dtrace_copystr(uaddr, kaddr, size, flags); 59491eaf3e1SJohn Birrell } 59591eaf3e1SJohn Birrell 59691eaf3e1SJohn Birrell void 59791eaf3e1SJohn Birrell dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size, 59891eaf3e1SJohn Birrell volatile uint16_t *flags) 59991eaf3e1SJohn Birrell { 60091eaf3e1SJohn Birrell if (dtrace_copycheck(uaddr, kaddr, size)) 60191eaf3e1SJohn Birrell dtrace_copystr(kaddr, uaddr, size, flags); 60291eaf3e1SJohn Birrell } 60391eaf3e1SJohn Birrell 60491eaf3e1SJohn Birrell uint8_t 60591eaf3e1SJohn Birrell dtrace_fuword8(void *uaddr) 60691eaf3e1SJohn Birrell { 607f340e9feSAndriy Gapon if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { 60891eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 60991eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; 61091eaf3e1SJohn Birrell return (0); 61191eaf3e1SJohn Birrell } 61291eaf3e1SJohn Birrell return (dtrace_fuword8_nocheck(uaddr)); 61391eaf3e1SJohn Birrell } 61491eaf3e1SJohn Birrell 61591eaf3e1SJohn Birrell uint16_t 61691eaf3e1SJohn Birrell dtrace_fuword16(void *uaddr) 61791eaf3e1SJohn Birrell { 618f340e9feSAndriy Gapon if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { 61991eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 62091eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; 62191eaf3e1SJohn Birrell return (0); 62291eaf3e1SJohn Birrell } 62391eaf3e1SJohn Birrell return (dtrace_fuword16_nocheck(uaddr)); 62491eaf3e1SJohn Birrell } 62591eaf3e1SJohn Birrell 62691eaf3e1SJohn Birrell uint32_t 62791eaf3e1SJohn Birrell dtrace_fuword32(void *uaddr) 62891eaf3e1SJohn Birrell { 629f340e9feSAndriy Gapon if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { 63091eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 63191eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; 63291eaf3e1SJohn Birrell return (0); 63391eaf3e1SJohn Birrell } 63491eaf3e1SJohn Birrell return (dtrace_fuword32_nocheck(uaddr)); 63591eaf3e1SJohn Birrell } 63691eaf3e1SJohn Birrell 63791eaf3e1SJohn Birrell uint64_t 63891eaf3e1SJohn Birrell dtrace_fuword64(void *uaddr) 63991eaf3e1SJohn Birrell { 640f340e9feSAndriy Gapon if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { 64191eaf3e1SJohn Birrell DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 64291eaf3e1SJohn Birrell cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; 64391eaf3e1SJohn Birrell return (0); 64491eaf3e1SJohn Birrell } 64591eaf3e1SJohn Birrell return (dtrace_fuword64_nocheck(uaddr)); 64691eaf3e1SJohn Birrell } 647