10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51880Sahl * Common Development and Distribution License (the "License").
61880Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211880Sahl
220Sstevel@tonic-gate /*
233446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/dtrace_impl.h>
300Sstevel@tonic-gate #include <sys/stack.h>
310Sstevel@tonic-gate #include <sys/frame.h>
320Sstevel@tonic-gate #include <sys/cmn_err.h>
330Sstevel@tonic-gate #include <sys/privregs.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate extern uintptr_t kernelbase;
370Sstevel@tonic-gate
383682Sjhaslam int dtrace_ustackdepth_max = 2048;
393682Sjhaslam
400Sstevel@tonic-gate void
dtrace_getpcstack(pc_t * pcstack,int pcstack_limit,int aframes,uint32_t * intrpc)410Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
420Sstevel@tonic-gate uint32_t *intrpc)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate struct frame *fp = (struct frame *)dtrace_getfp();
450Sstevel@tonic-gate struct frame *nextfp, *minfp, *stacktop;
460Sstevel@tonic-gate int depth = 0;
470Sstevel@tonic-gate int on_intr, last = 0;
480Sstevel@tonic-gate uintptr_t pc;
490Sstevel@tonic-gate uintptr_t caller = CPU->cpu_dtrace_caller;
500Sstevel@tonic-gate
510Sstevel@tonic-gate if ((on_intr = CPU_ON_INTR(CPU)) != 0)
520Sstevel@tonic-gate stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
530Sstevel@tonic-gate else
540Sstevel@tonic-gate stacktop = (struct frame *)curthread->t_stk;
550Sstevel@tonic-gate minfp = fp;
560Sstevel@tonic-gate
570Sstevel@tonic-gate aframes++;
580Sstevel@tonic-gate
590Sstevel@tonic-gate if (intrpc != NULL && depth < pcstack_limit)
600Sstevel@tonic-gate pcstack[depth++] = (pc_t)intrpc;
610Sstevel@tonic-gate
620Sstevel@tonic-gate while (depth < pcstack_limit) {
630Sstevel@tonic-gate nextfp = (struct frame *)fp->fr_savfp;
640Sstevel@tonic-gate pc = fp->fr_savpc;
650Sstevel@tonic-gate
660Sstevel@tonic-gate if (nextfp <= minfp || nextfp >= stacktop) {
670Sstevel@tonic-gate if (on_intr) {
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * Hop from interrupt stack to thread stack.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate stacktop = (struct frame *)curthread->t_stk;
720Sstevel@tonic-gate minfp = (struct frame *)curthread->t_stkbase;
730Sstevel@tonic-gate on_intr = 0;
740Sstevel@tonic-gate continue;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * This is the last frame we can process; indicate
790Sstevel@tonic-gate * that we should return after processing this frame.
800Sstevel@tonic-gate */
810Sstevel@tonic-gate last = 1;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate if (aframes > 0) {
850Sstevel@tonic-gate if (--aframes == 0 && caller != NULL) {
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * We've just run out of artificial frames,
880Sstevel@tonic-gate * and we have a valid caller -- fill it in
890Sstevel@tonic-gate * now.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate ASSERT(depth < pcstack_limit);
920Sstevel@tonic-gate pcstack[depth++] = (pc_t)caller;
930Sstevel@tonic-gate caller = NULL;
940Sstevel@tonic-gate }
950Sstevel@tonic-gate } else {
960Sstevel@tonic-gate if (depth < pcstack_limit)
970Sstevel@tonic-gate pcstack[depth++] = (pc_t)pc;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (last) {
1010Sstevel@tonic-gate while (depth < pcstack_limit)
1020Sstevel@tonic-gate pcstack[depth++] = NULL;
1030Sstevel@tonic-gate return;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate fp = nextfp;
1070Sstevel@tonic-gate minfp = fp;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
111191Sahl static int
dtrace_getustack_common(uint64_t * pcstack,int pcstack_limit,uintptr_t pc,uintptr_t sp)112191Sahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
113191Sahl uintptr_t sp)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
116191Sahl proc_t *p = curproc;
117191Sahl uintptr_t oldcontext = lwp->lwp_oldcontext;
1183682Sjhaslam uintptr_t oldsp;
119191Sahl volatile uint16_t *flags =
120191Sahl (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1210Sstevel@tonic-gate size_t s1, s2;
122191Sahl int ret = 0;
1230Sstevel@tonic-gate
124191Sahl ASSERT(pcstack == NULL || pcstack_limit > 0);
1253682Sjhaslam ASSERT(dtrace_ustackdepth_max > 0);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
1280Sstevel@tonic-gate s1 = sizeof (struct frame) + 2 * sizeof (long);
1290Sstevel@tonic-gate s2 = s1 + sizeof (siginfo_t);
1300Sstevel@tonic-gate } else {
1310Sstevel@tonic-gate s1 = sizeof (struct frame32) + 3 * sizeof (int);
1320Sstevel@tonic-gate s2 = s1 + sizeof (siginfo32_t);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1351880Sahl while (pc != 0) {
1363682Sjhaslam /*
1373682Sjhaslam * We limit the number of times we can go around this
1383682Sjhaslam * loop to account for a circular stack.
1393682Sjhaslam */
1403682Sjhaslam if (ret++ >= dtrace_ustackdepth_max) {
1413682Sjhaslam *flags |= CPU_DTRACE_BADSTACK;
1423682Sjhaslam cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
1433682Sjhaslam break;
1443682Sjhaslam }
1453682Sjhaslam
146191Sahl if (pcstack != NULL) {
147191Sahl *pcstack++ = (uint64_t)pc;
148191Sahl pcstack_limit--;
149191Sahl if (pcstack_limit <= 0)
150191Sahl break;
151191Sahl }
1520Sstevel@tonic-gate
1531880Sahl if (sp == 0)
1541880Sahl break;
1551880Sahl
1563682Sjhaslam oldsp = sp;
1573682Sjhaslam
1580Sstevel@tonic-gate if (oldcontext == sp + s1 || oldcontext == sp + s2) {
1590Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
1600Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)oldcontext;
1610Sstevel@tonic-gate greg_t *gregs = ucp->uc_mcontext.gregs;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate sp = dtrace_fulword(&gregs[REG_FP]);
1640Sstevel@tonic-gate pc = dtrace_fulword(&gregs[REG_PC]);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate oldcontext = dtrace_fulword(&ucp->uc_link);
1670Sstevel@tonic-gate } else {
1680Sstevel@tonic-gate ucontext32_t *ucp = (ucontext32_t *)oldcontext;
1690Sstevel@tonic-gate greg32_t *gregs = ucp->uc_mcontext.gregs;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate sp = dtrace_fuword32(&gregs[EBP]);
1720Sstevel@tonic-gate pc = dtrace_fuword32(&gregs[EIP]);
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate oldcontext = dtrace_fuword32(&ucp->uc_link);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate } else {
1770Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
1780Sstevel@tonic-gate struct frame *fr = (struct frame *)sp;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate pc = dtrace_fulword(&fr->fr_savpc);
1810Sstevel@tonic-gate sp = dtrace_fulword(&fr->fr_savfp);
1820Sstevel@tonic-gate } else {
1830Sstevel@tonic-gate struct frame32 *fr = (struct frame32 *)sp;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate pc = dtrace_fuword32(&fr->fr_savpc);
1860Sstevel@tonic-gate sp = dtrace_fuword32(&fr->fr_savfp);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1903682Sjhaslam if (sp == oldsp) {
1913682Sjhaslam *flags |= CPU_DTRACE_BADSTACK;
1923682Sjhaslam cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
1933682Sjhaslam break;
1943682Sjhaslam }
1953682Sjhaslam
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * This is totally bogus: if we faulted, we're going to clear
1980Sstevel@tonic-gate * the fault and break. This is to deal with the apparently
1990Sstevel@tonic-gate * broken Java stacks on x86.
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate if (*flags & CPU_DTRACE_FAULT) {
2020Sstevel@tonic-gate *flags &= ~CPU_DTRACE_FAULT;
2030Sstevel@tonic-gate break;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
207191Sahl return (ret);
208191Sahl }
209191Sahl
210191Sahl void
dtrace_getupcstack(uint64_t * pcstack,int pcstack_limit)211191Sahl dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
212191Sahl {
213191Sahl klwp_t *lwp = ttolwp(curthread);
214191Sahl proc_t *p = curproc;
215191Sahl struct regs *rp;
216191Sahl uintptr_t pc, sp;
217191Sahl int n;
218191Sahl
219*5114Sahl ASSERT(DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT));
220*5114Sahl
221*5114Sahl if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
222191Sahl return;
223191Sahl
224191Sahl if (pcstack_limit <= 0)
225191Sahl return;
226191Sahl
227630Sahl /*
228630Sahl * If there's no user context we still need to zero the stack.
229630Sahl */
230630Sahl if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
231630Sahl goto zero;
232630Sahl
233191Sahl *pcstack++ = (uint64_t)p->p_pid;
234191Sahl pcstack_limit--;
235191Sahl
236191Sahl if (pcstack_limit <= 0)
237191Sahl return;
238191Sahl
239191Sahl pc = rp->r_pc;
240191Sahl sp = rp->r_fp;
241191Sahl
242191Sahl if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
243191Sahl *pcstack++ = (uint64_t)pc;
244191Sahl pcstack_limit--;
245191Sahl if (pcstack_limit <= 0)
246191Sahl return;
247191Sahl
248191Sahl if (p->p_model == DATAMODEL_NATIVE)
249191Sahl pc = dtrace_fulword((void *)rp->r_sp);
250191Sahl else
251191Sahl pc = dtrace_fuword32((void *)rp->r_sp);
252191Sahl }
253191Sahl
254191Sahl n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
255191Sahl ASSERT(n >= 0);
256191Sahl ASSERT(n <= pcstack_limit);
257191Sahl
258191Sahl pcstack += n;
259191Sahl pcstack_limit -= n;
260191Sahl
261630Sahl zero:
2620Sstevel@tonic-gate while (pcstack_limit-- > 0)
2630Sstevel@tonic-gate *pcstack++ = NULL;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
266191Sahl int
dtrace_getustackdepth(void)267191Sahl dtrace_getustackdepth(void)
268191Sahl {
269191Sahl klwp_t *lwp = ttolwp(curthread);
270191Sahl proc_t *p = curproc;
271191Sahl struct regs *rp;
272191Sahl uintptr_t pc, sp;
273191Sahl int n = 0;
274191Sahl
275191Sahl if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
276191Sahl return (0);
277191Sahl
278191Sahl if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
279191Sahl return (-1);
280191Sahl
281191Sahl pc = rp->r_pc;
282191Sahl sp = rp->r_fp;
283191Sahl
284191Sahl if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
285191Sahl n++;
286191Sahl
287191Sahl if (p->p_model == DATAMODEL_NATIVE)
288191Sahl pc = dtrace_fulword((void *)rp->r_sp);
289191Sahl else
290191Sahl pc = dtrace_fuword32((void *)rp->r_sp);
291191Sahl }
292191Sahl
293191Sahl n += dtrace_getustack_common(NULL, 0, pc, sp);
294191Sahl
295191Sahl return (n);
296191Sahl }
297191Sahl
2980Sstevel@tonic-gate void
dtrace_getufpstack(uint64_t * pcstack,uint64_t * fpstack,int pcstack_limit)2990Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
302191Sahl proc_t *p = curproc;
3030Sstevel@tonic-gate struct regs *rp;
3040Sstevel@tonic-gate uintptr_t pc, sp, oldcontext;
305191Sahl volatile uint16_t *flags =
306191Sahl (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
3070Sstevel@tonic-gate size_t s1, s2;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (*flags & CPU_DTRACE_FAULT)
3100Sstevel@tonic-gate return;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (pcstack_limit <= 0)
3130Sstevel@tonic-gate return;
3140Sstevel@tonic-gate
315630Sahl /*
316630Sahl * If there's no user context we still need to zero the stack.
317630Sahl */
318630Sahl if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
319630Sahl goto zero;
320630Sahl
3210Sstevel@tonic-gate *pcstack++ = (uint64_t)p->p_pid;
3220Sstevel@tonic-gate pcstack_limit--;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate if (pcstack_limit <= 0)
3250Sstevel@tonic-gate return;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate pc = rp->r_pc;
3280Sstevel@tonic-gate sp = rp->r_fp;
3290Sstevel@tonic-gate oldcontext = lwp->lwp_oldcontext;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
3320Sstevel@tonic-gate s1 = sizeof (struct frame) + 2 * sizeof (long);
3330Sstevel@tonic-gate s2 = s1 + sizeof (siginfo_t);
3340Sstevel@tonic-gate } else {
3350Sstevel@tonic-gate s1 = sizeof (struct frame32) + 3 * sizeof (int);
3360Sstevel@tonic-gate s2 = s1 + sizeof (siginfo32_t);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
3400Sstevel@tonic-gate *pcstack++ = (uint64_t)pc;
3410Sstevel@tonic-gate *fpstack++ = 0;
3420Sstevel@tonic-gate pcstack_limit--;
3430Sstevel@tonic-gate if (pcstack_limit <= 0)
3440Sstevel@tonic-gate return;
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE)
3470Sstevel@tonic-gate pc = dtrace_fulword((void *)rp->r_sp);
3480Sstevel@tonic-gate else
3490Sstevel@tonic-gate pc = dtrace_fuword32((void *)rp->r_sp);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3521880Sahl while (pc != 0) {
3530Sstevel@tonic-gate *pcstack++ = (uint64_t)pc;
3540Sstevel@tonic-gate *fpstack++ = sp;
3550Sstevel@tonic-gate pcstack_limit--;
3560Sstevel@tonic-gate if (pcstack_limit <= 0)
3570Sstevel@tonic-gate break;
3580Sstevel@tonic-gate
3591880Sahl if (sp == 0)
3601880Sahl break;
3611880Sahl
3620Sstevel@tonic-gate if (oldcontext == sp + s1 || oldcontext == sp + s2) {
3630Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
3640Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)oldcontext;
3650Sstevel@tonic-gate greg_t *gregs = ucp->uc_mcontext.gregs;
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate sp = dtrace_fulword(&gregs[REG_FP]);
3680Sstevel@tonic-gate pc = dtrace_fulword(&gregs[REG_PC]);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate oldcontext = dtrace_fulword(&ucp->uc_link);
3710Sstevel@tonic-gate } else {
3720Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)oldcontext;
3730Sstevel@tonic-gate greg_t *gregs = ucp->uc_mcontext.gregs;
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate sp = dtrace_fuword32(&gregs[EBP]);
3760Sstevel@tonic-gate pc = dtrace_fuword32(&gregs[EIP]);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate oldcontext = dtrace_fuword32(&ucp->uc_link);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate } else {
3810Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
3820Sstevel@tonic-gate struct frame *fr = (struct frame *)sp;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate pc = dtrace_fulword(&fr->fr_savpc);
3850Sstevel@tonic-gate sp = dtrace_fulword(&fr->fr_savfp);
3860Sstevel@tonic-gate } else {
3870Sstevel@tonic-gate struct frame32 *fr = (struct frame32 *)sp;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate pc = dtrace_fuword32(&fr->fr_savpc);
3900Sstevel@tonic-gate sp = dtrace_fuword32(&fr->fr_savfp);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * This is totally bogus: if we faulted, we're going to clear
3960Sstevel@tonic-gate * the fault and break. This is to deal with the apparently
3970Sstevel@tonic-gate * broken Java stacks on x86.
3980Sstevel@tonic-gate */
3990Sstevel@tonic-gate if (*flags & CPU_DTRACE_FAULT) {
4000Sstevel@tonic-gate *flags &= ~CPU_DTRACE_FAULT;
4010Sstevel@tonic-gate break;
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate
405630Sahl zero:
4060Sstevel@tonic-gate while (pcstack_limit-- > 0)
4070Sstevel@tonic-gate *pcstack++ = NULL;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate /*ARGSUSED*/
4110Sstevel@tonic-gate uint64_t
dtrace_getarg(int arg,int aframes)4120Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate uintptr_t val;
4150Sstevel@tonic-gate struct frame *fp = (struct frame *)dtrace_getfp();
4160Sstevel@tonic-gate uintptr_t *stack;
4170Sstevel@tonic-gate int i;
4180Sstevel@tonic-gate #if defined(__amd64)
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * A total of 6 arguments are passed via registers; any argument with
4210Sstevel@tonic-gate * index of 5 or lower is therefore in a register.
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate int inreg = 5;
4240Sstevel@tonic-gate #endif
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate for (i = 1; i <= aframes; i++) {
4270Sstevel@tonic-gate fp = (struct frame *)(fp->fr_savfp);
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate if (fp->fr_savpc == (pc_t)dtrace_invop_callsite) {
4300Sstevel@tonic-gate #if !defined(__amd64)
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate * If we pass through the invalid op handler, we will
4330Sstevel@tonic-gate * use the pointer that it passed to the stack as the
4340Sstevel@tonic-gate * second argument to dtrace_invop() as the pointer to
4350Sstevel@tonic-gate * the stack. When using this stack, we must step
4360Sstevel@tonic-gate * beyond the EIP/RIP that was pushed when the trap was
4370Sstevel@tonic-gate * taken -- hence the "+ 1" below.
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate stack = ((uintptr_t **)&fp[1])[1] + 1;
4400Sstevel@tonic-gate #else
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate * In the case of amd64, we will use the pointer to the
4430Sstevel@tonic-gate * regs structure that was pushed when we took the
4440Sstevel@tonic-gate * trap. To get this structure, we must increment
4450Sstevel@tonic-gate * beyond the frame structure, and then again beyond
4460Sstevel@tonic-gate * the calling RIP stored in dtrace_invop(). If the
4470Sstevel@tonic-gate * argument that we're seeking is passed on the stack,
4480Sstevel@tonic-gate * we'll pull the true stack pointer out of the saved
4490Sstevel@tonic-gate * registers and decrement our argument by the number
4500Sstevel@tonic-gate * of arguments passed in registers; if the argument
4510Sstevel@tonic-gate * we're seeking is passed in regsiters, we can just
4520Sstevel@tonic-gate * load it directly.
4530Sstevel@tonic-gate */
4540Sstevel@tonic-gate struct regs *rp = (struct regs *)((uintptr_t)&fp[1] +
4550Sstevel@tonic-gate sizeof (uintptr_t));
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate if (arg <= inreg) {
4580Sstevel@tonic-gate stack = (uintptr_t *)&rp->r_rdi;
4590Sstevel@tonic-gate } else {
4600Sstevel@tonic-gate stack = (uintptr_t *)(rp->r_rsp);
4610Sstevel@tonic-gate arg -= inreg;
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate #endif
4640Sstevel@tonic-gate goto load;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate * We know that we did not come through a trap to get into
4710Sstevel@tonic-gate * dtrace_probe() -- the provider simply called dtrace_probe()
4720Sstevel@tonic-gate * directly. As this is the case, we need to shift the argument
4730Sstevel@tonic-gate * that we're looking for: the probe ID is the first argument to
4740Sstevel@tonic-gate * dtrace_probe(), so the argument n will actually be found where
4750Sstevel@tonic-gate * one would expect to find argument (n + 1).
4760Sstevel@tonic-gate */
4770Sstevel@tonic-gate arg++;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate #if defined(__amd64)
4800Sstevel@tonic-gate if (arg <= inreg) {
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate * This shouldn't happen. If the argument is passed in a
4830Sstevel@tonic-gate * register then it should have been, well, passed in a
4840Sstevel@tonic-gate * register...
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4870Sstevel@tonic-gate return (0);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate arg -= (inreg + 1);
4910Sstevel@tonic-gate #endif
4920Sstevel@tonic-gate stack = (uintptr_t *)&fp[1];
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate load:
4950Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4960Sstevel@tonic-gate val = stack[arg];
4970Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate return (val);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate /*ARGSUSED*/
5030Sstevel@tonic-gate int
dtrace_getstackdepth(int aframes)5040Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate struct frame *fp = (struct frame *)dtrace_getfp();
5070Sstevel@tonic-gate struct frame *nextfp, *minfp, *stacktop;
5080Sstevel@tonic-gate int depth = 0;
5090Sstevel@tonic-gate int on_intr;
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate if ((on_intr = CPU_ON_INTR(CPU)) != 0)
5120Sstevel@tonic-gate stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
5130Sstevel@tonic-gate else
5140Sstevel@tonic-gate stacktop = (struct frame *)curthread->t_stk;
5150Sstevel@tonic-gate minfp = fp;
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate aframes++;
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate for (;;) {
5200Sstevel@tonic-gate depth++;
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate nextfp = (struct frame *)fp->fr_savfp;
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate if (nextfp <= minfp || nextfp >= stacktop) {
5250Sstevel@tonic-gate if (on_intr) {
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate * Hop from interrupt stack to thread stack.
5280Sstevel@tonic-gate */
5290Sstevel@tonic-gate stacktop = (struct frame *)curthread->t_stk;
5300Sstevel@tonic-gate minfp = (struct frame *)curthread->t_stkbase;
5310Sstevel@tonic-gate on_intr = 0;
5320Sstevel@tonic-gate continue;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate break;
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate fp = nextfp;
5380Sstevel@tonic-gate minfp = fp;
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate if (depth <= aframes)
5420Sstevel@tonic-gate return (0);
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate return (depth - aframes);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate ulong_t
dtrace_getreg(struct regs * rp,uint_t reg)5480Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate #if defined(__amd64)
5510Sstevel@tonic-gate int regmap[] = {
5520Sstevel@tonic-gate REG_GS, /* GS */
5530Sstevel@tonic-gate REG_FS, /* FS */
5540Sstevel@tonic-gate REG_ES, /* ES */
5550Sstevel@tonic-gate REG_DS, /* DS */
5560Sstevel@tonic-gate REG_RDI, /* EDI */
5570Sstevel@tonic-gate REG_RSI, /* ESI */
5580Sstevel@tonic-gate REG_RBP, /* EBP */
5590Sstevel@tonic-gate REG_RSP, /* ESP */
5600Sstevel@tonic-gate REG_RBX, /* EBX */
5610Sstevel@tonic-gate REG_RDX, /* EDX */
5620Sstevel@tonic-gate REG_RCX, /* ECX */
5630Sstevel@tonic-gate REG_RAX, /* EAX */
5640Sstevel@tonic-gate REG_TRAPNO, /* TRAPNO */
5650Sstevel@tonic-gate REG_ERR, /* ERR */
5660Sstevel@tonic-gate REG_RIP, /* EIP */
5670Sstevel@tonic-gate REG_CS, /* CS */
5680Sstevel@tonic-gate REG_RFL, /* EFL */
5690Sstevel@tonic-gate REG_RSP, /* UESP */
5700Sstevel@tonic-gate REG_SS /* SS */
5710Sstevel@tonic-gate };
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate if (reg <= SS) {
5740Sstevel@tonic-gate if (reg >= sizeof (regmap) / sizeof (int)) {
5750Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5760Sstevel@tonic-gate return (0);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate reg = regmap[reg];
5800Sstevel@tonic-gate } else {
5810Sstevel@tonic-gate reg -= SS + 1;
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate switch (reg) {
5850Sstevel@tonic-gate case REG_RDI:
5860Sstevel@tonic-gate return (rp->r_rdi);
5870Sstevel@tonic-gate case REG_RSI:
5880Sstevel@tonic-gate return (rp->r_rsi);
5890Sstevel@tonic-gate case REG_RDX:
5900Sstevel@tonic-gate return (rp->r_rdx);
5910Sstevel@tonic-gate case REG_RCX:
5920Sstevel@tonic-gate return (rp->r_rcx);
5930Sstevel@tonic-gate case REG_R8:
5940Sstevel@tonic-gate return (rp->r_r8);
5950Sstevel@tonic-gate case REG_R9:
5960Sstevel@tonic-gate return (rp->r_r9);
5970Sstevel@tonic-gate case REG_RAX:
5980Sstevel@tonic-gate return (rp->r_rax);
5990Sstevel@tonic-gate case REG_RBX:
6000Sstevel@tonic-gate return (rp->r_rbx);
6010Sstevel@tonic-gate case REG_RBP:
6020Sstevel@tonic-gate return (rp->r_rbp);
6030Sstevel@tonic-gate case REG_R10:
6040Sstevel@tonic-gate return (rp->r_r10);
6050Sstevel@tonic-gate case REG_R11:
6060Sstevel@tonic-gate return (rp->r_r11);
6070Sstevel@tonic-gate case REG_R12:
6080Sstevel@tonic-gate return (rp->r_r12);
6090Sstevel@tonic-gate case REG_R13:
6100Sstevel@tonic-gate return (rp->r_r13);
6110Sstevel@tonic-gate case REG_R14:
6120Sstevel@tonic-gate return (rp->r_r14);
6130Sstevel@tonic-gate case REG_R15:
6140Sstevel@tonic-gate return (rp->r_r15);
6150Sstevel@tonic-gate case REG_DS:
6160Sstevel@tonic-gate return (rp->r_ds);
6170Sstevel@tonic-gate case REG_ES:
6180Sstevel@tonic-gate return (rp->r_es);
6190Sstevel@tonic-gate case REG_FS:
6200Sstevel@tonic-gate return (rp->r_fs);
6210Sstevel@tonic-gate case REG_GS:
6220Sstevel@tonic-gate return (rp->r_gs);
6230Sstevel@tonic-gate case REG_TRAPNO:
6240Sstevel@tonic-gate return (rp->r_trapno);
6250Sstevel@tonic-gate case REG_ERR:
6260Sstevel@tonic-gate return (rp->r_err);
6270Sstevel@tonic-gate case REG_RIP:
6280Sstevel@tonic-gate return (rp->r_rip);
6290Sstevel@tonic-gate case REG_CS:
6300Sstevel@tonic-gate return (rp->r_cs);
6310Sstevel@tonic-gate case REG_SS:
6320Sstevel@tonic-gate return (rp->r_ss);
6330Sstevel@tonic-gate case REG_RFL:
6340Sstevel@tonic-gate return (rp->r_rfl);
6350Sstevel@tonic-gate case REG_RSP:
6360Sstevel@tonic-gate return (rp->r_rsp);
6370Sstevel@tonic-gate default:
6380Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6390Sstevel@tonic-gate return (0);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate #else
6430Sstevel@tonic-gate if (reg > SS) {
6440Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6450Sstevel@tonic-gate return (0);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate return ((&rp->r_gs)[reg]);
6490Sstevel@tonic-gate #endif
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate static int
dtrace_copycheck(uintptr_t uaddr,uintptr_t kaddr,size_t size)6530Sstevel@tonic-gate dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate ASSERT(kaddr >= kernelbase && kaddr + size >= kaddr);
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate if (uaddr + size >= kernelbase || uaddr + size < uaddr) {
6580Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
6590Sstevel@tonic-gate cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
6600Sstevel@tonic-gate return (0);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate return (1);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate
6663677Ssudheer /*ARGSUSED*/
6670Sstevel@tonic-gate void
dtrace_copyin(uintptr_t uaddr,uintptr_t kaddr,size_t size,volatile uint16_t * flags)6683677Ssudheer dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
6693677Ssudheer volatile uint16_t *flags)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate if (dtrace_copycheck(uaddr, kaddr, size))
6720Sstevel@tonic-gate dtrace_copy(uaddr, kaddr, size);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate
6753677Ssudheer /*ARGSUSED*/
6760Sstevel@tonic-gate void
dtrace_copyout(uintptr_t kaddr,uintptr_t uaddr,size_t size,volatile uint16_t * flags)6773677Ssudheer dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
6783677Ssudheer volatile uint16_t *flags)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate if (dtrace_copycheck(uaddr, kaddr, size))
6810Sstevel@tonic-gate dtrace_copy(kaddr, uaddr, size);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate void
dtrace_copyinstr(uintptr_t uaddr,uintptr_t kaddr,size_t size,volatile uint16_t * flags)6853677Ssudheer dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
6863677Ssudheer volatile uint16_t *flags)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate if (dtrace_copycheck(uaddr, kaddr, size))
6893677Ssudheer dtrace_copystr(uaddr, kaddr, size, flags);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate void
dtrace_copyoutstr(uintptr_t kaddr,uintptr_t uaddr,size_t size,volatile uint16_t * flags)6933677Ssudheer dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
6943677Ssudheer volatile uint16_t *flags)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate if (dtrace_copycheck(uaddr, kaddr, size))
6973677Ssudheer dtrace_copystr(kaddr, uaddr, size, flags);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate uint8_t
dtrace_fuword8(void * uaddr)7010Sstevel@tonic-gate dtrace_fuword8(void *uaddr)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate extern uint8_t dtrace_fuword8_nocheck(void *);
7040Sstevel@tonic-gate if ((uintptr_t)uaddr >= _userlimit) {
7050Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7060Sstevel@tonic-gate cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7070Sstevel@tonic-gate return (0);
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate return (dtrace_fuword8_nocheck(uaddr));
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate uint16_t
dtrace_fuword16(void * uaddr)7130Sstevel@tonic-gate dtrace_fuword16(void *uaddr)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate extern uint16_t dtrace_fuword16_nocheck(void *);
7160Sstevel@tonic-gate if ((uintptr_t)uaddr >= _userlimit) {
7170Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7180Sstevel@tonic-gate cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7190Sstevel@tonic-gate return (0);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate return (dtrace_fuword16_nocheck(uaddr));
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate uint32_t
dtrace_fuword32(void * uaddr)7250Sstevel@tonic-gate dtrace_fuword32(void *uaddr)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate extern uint32_t dtrace_fuword32_nocheck(void *);
7280Sstevel@tonic-gate if ((uintptr_t)uaddr >= _userlimit) {
7290Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7300Sstevel@tonic-gate cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7310Sstevel@tonic-gate return (0);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate return (dtrace_fuword32_nocheck(uaddr));
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate uint64_t
dtrace_fuword64(void * uaddr)7370Sstevel@tonic-gate dtrace_fuword64(void *uaddr)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate extern uint64_t dtrace_fuword64_nocheck(void *);
7400Sstevel@tonic-gate if ((uintptr_t)uaddr >= _userlimit) {
7410Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7420Sstevel@tonic-gate cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7430Sstevel@tonic-gate return (0);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate return (dtrace_fuword64_nocheck(uaddr));
7460Sstevel@tonic-gate }
747