4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
31 #include <sys/dtrace_impl.h>
32 #include <sys/stack.h>
33 #include <sys/frame.h>
34 #include <sys/cmn_err.h>
35 #include <sys/privregs.h>
36 #include <sys/sysmacros.h>
38 extern uintptr_t kernelbase
;
40 int dtrace_ustackdepth_max
= 2048;
43 dtrace_getpcstack(pc_t
*pcstack
, int pcstack_limit
, int aframes
,
46 struct frame
*fp
= (struct frame
*)dtrace_getfp();
47 struct frame
*nextfp
, *minfp
, *stacktop
;
49 int on_intr
, last
= 0;
51 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
53 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
54 stacktop
= (struct frame
*)(CPU
->cpu_intr_stack
+ SA(MINFRAME
));
56 stacktop
= (struct frame
*)curthread
->t_stk
;
61 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
62 pcstack
[depth
++] = (pc_t
)intrpc
;
64 while (depth
< pcstack_limit
) {
65 nextfp
= (struct frame
*)fp
->fr_savfp
;
68 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
71 * Hop from interrupt stack to thread stack.
73 stacktop
= (struct frame
*)curthread
->t_stk
;
74 minfp
= (struct frame
*)curthread
->t_stkbase
;
80 * This is the last frame we can process; indicate
81 * that we should return after processing this frame.
87 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
89 * We've just run out of artificial frames,
90 * and we have a valid caller -- fill it in
93 ASSERT(depth
< pcstack_limit
);
94 pcstack
[depth
++] = (pc_t
)caller
;
95 caller
= (uintptr_t)NULL
;
98 if (depth
< pcstack_limit
)
99 pcstack
[depth
++] = (pc_t
)pc
;
103 while (depth
< pcstack_limit
)
104 pcstack
[depth
++] = (pc_t
)NULL
;
114 dtrace_getustack_common(uint64_t *pcstack
, int pcstack_limit
, uintptr_t pc
,
117 klwp_t
*lwp
= ttolwp(curthread
);
119 uintptr_t oldcontext
= lwp
->lwp_oldcontext
;
121 volatile uint16_t *flags
=
122 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
126 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
127 ASSERT(dtrace_ustackdepth_max
> 0);
129 if (p
->p_model
== DATAMODEL_NATIVE
) {
130 s1
= sizeof (struct frame
) + 2 * sizeof (long);
131 s2
= s1
+ sizeof (siginfo_t
);
133 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
134 s2
= s1
+ sizeof (siginfo32_t
);
139 * We limit the number of times we can go around this
140 * loop to account for a circular stack.
142 if (ret
++ >= dtrace_ustackdepth_max
) {
143 *flags
|= CPU_DTRACE_BADSTACK
;
144 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= sp
;
148 if (pcstack
!= NULL
) {
149 *pcstack
++ = (uint64_t)pc
;
151 if (pcstack_limit
<= 0)
160 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
161 if (p
->p_model
== DATAMODEL_NATIVE
) {
162 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
163 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
165 sp
= dtrace_fulword(&gregs
[REG_FP
]);
166 pc
= dtrace_fulword(&gregs
[REG_PC
]);
168 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
170 ucontext32_t
*ucp
= (ucontext32_t
*)oldcontext
;
171 greg32_t
*gregs
= ucp
->uc_mcontext
.gregs
;
173 sp
= dtrace_fuword32(&gregs
[EBP
]);
174 pc
= dtrace_fuword32(&gregs
[EIP
]);
176 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
179 if (p
->p_model
== DATAMODEL_NATIVE
) {
180 struct frame
*fr
= (struct frame
*)sp
;
182 pc
= dtrace_fulword(&fr
->fr_savpc
);
183 sp
= dtrace_fulword(&fr
->fr_savfp
);
185 struct frame32
*fr
= (struct frame32
*)sp
;
187 pc
= dtrace_fuword32(&fr
->fr_savpc
);
188 sp
= dtrace_fuword32(&fr
->fr_savfp
);
193 *flags
|= CPU_DTRACE_BADSTACK
;
194 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= sp
;
199 * This is totally bogus: if we faulted, we're going to clear
200 * the fault and break. This is to deal with the apparently
201 * broken Java stacks on x86.
203 if (*flags
& CPU_DTRACE_FAULT
) {
204 *flags
&= ~CPU_DTRACE_FAULT
;
213 dtrace_getupcstack(uint64_t *pcstack
, int pcstack_limit
)
215 klwp_t
*lwp
= ttolwp(curthread
);
221 ASSERT(DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
));
223 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
226 if (pcstack_limit
<= 0)
230 * If there's no user context we still need to zero the stack.
232 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
235 *pcstack
++ = (uint64_t)p
->p_pid
;
238 if (pcstack_limit
<= 0)
244 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
245 *pcstack
++ = (uint64_t)pc
;
247 if (pcstack_limit
<= 0)
250 if (p
->p_model
== DATAMODEL_NATIVE
)
251 pc
= dtrace_fulword((void *)rp
->r_sp
);
253 pc
= dtrace_fuword32((void *)rp
->r_sp
);
256 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, sp
);
258 ASSERT(n
<= pcstack_limit
);
264 while (pcstack_limit
-- > 0)
265 *pcstack
++ = (uintptr_t)NULL
;
269 dtrace_getustackdepth(void)
271 klwp_t
*lwp
= ttolwp(curthread
);
277 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
280 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
286 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
289 if (p
->p_model
== DATAMODEL_NATIVE
)
290 pc
= dtrace_fulword((void *)rp
->r_sp
);
292 pc
= dtrace_fuword32((void *)rp
->r_sp
);
295 n
+= dtrace_getustack_common(NULL
, 0, pc
, sp
);
301 dtrace_getufpstack(uint64_t *pcstack
, uint64_t *fpstack
, int pcstack_limit
)
303 klwp_t
*lwp
= ttolwp(curthread
);
306 uintptr_t pc
, sp
, oldcontext
;
307 volatile uint16_t *flags
=
308 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
311 if (*flags
& CPU_DTRACE_FAULT
)
314 if (pcstack_limit
<= 0)
318 * If there's no user context we still need to zero the stack.
320 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
323 *pcstack
++ = (uint64_t)p
->p_pid
;
326 if (pcstack_limit
<= 0)
331 oldcontext
= lwp
->lwp_oldcontext
;
333 if (p
->p_model
== DATAMODEL_NATIVE
) {
334 s1
= sizeof (struct frame
) + 2 * sizeof (long);
335 s2
= s1
+ sizeof (siginfo_t
);
337 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
338 s2
= s1
+ sizeof (siginfo32_t
);
341 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
342 *pcstack
++ = (uint64_t)pc
;
345 if (pcstack_limit
<= 0)
348 if (p
->p_model
== DATAMODEL_NATIVE
)
349 pc
= dtrace_fulword((void *)rp
->r_sp
);
351 pc
= dtrace_fuword32((void *)rp
->r_sp
);
355 *pcstack
++ = (uint64_t)pc
;
358 if (pcstack_limit
<= 0)
364 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
365 if (p
->p_model
== DATAMODEL_NATIVE
) {
366 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
367 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
369 sp
= dtrace_fulword(&gregs
[REG_FP
]);
370 pc
= dtrace_fulword(&gregs
[REG_PC
]);
372 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
374 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
375 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
377 sp
= dtrace_fuword32(&gregs
[EBP
]);
378 pc
= dtrace_fuword32(&gregs
[EIP
]);
380 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
383 if (p
->p_model
== DATAMODEL_NATIVE
) {
384 struct frame
*fr
= (struct frame
*)sp
;
386 pc
= dtrace_fulword(&fr
->fr_savpc
);
387 sp
= dtrace_fulword(&fr
->fr_savfp
);
389 struct frame32
*fr
= (struct frame32
*)sp
;
391 pc
= dtrace_fuword32(&fr
->fr_savpc
);
392 sp
= dtrace_fuword32(&fr
->fr_savfp
);
397 * This is totally bogus: if we faulted, we're going to clear
398 * the fault and break. This is to deal with the apparently
399 * broken Java stacks on x86.
401 if (*flags
& CPU_DTRACE_FAULT
) {
402 *flags
&= ~CPU_DTRACE_FAULT
;
408 while (pcstack_limit
-- > 0)
409 *pcstack
++ = (uintptr_t)NULL
;
414 dtrace_getarg(int arg
, int aframes
)
417 struct frame
*fp
= (struct frame
*)dtrace_getfp();
422 * A total of 6 arguments are passed via registers; any argument with
423 * index of 5 or lower is therefore in a register.
428 for (i
= 1; i
<= aframes
; i
++) {
429 fp
= (struct frame
*)(fp
->fr_savfp
);
431 if (fp
->fr_savpc
== (pc_t
)dtrace_invop_callsite
) {
432 #if !defined(__amd64)
434 * If we pass through the invalid op handler, we will
435 * use the pointer that it passed to the stack as the
436 * second argument to dtrace_invop() as the pointer to
437 * the stack. When using this stack, we must step
438 * beyond the EIP that was pushed when the trap was
439 * taken -- hence the "+ 1" below.
441 stack
= ((uintptr_t **)&fp
[1])[1] + 1;
444 * In the case of amd64, we will use the pointer to the
445 * regs structure that was pushed when we took the
446 * trap. To get this structure, we must increment
447 * beyond the frame structure, the calling RIP, and
448 * padding stored in dtrace_invop(). If the argument
449 * that we're seeking is passed on the stack, we'll
450 * pull the true stack pointer out of the saved
451 * registers and decrement our argument by the number
452 * of arguments passed in registers; if the argument
453 * we're seeking is passed in regsiters, we can just
456 struct regs
*rp
= (struct regs
*)((uintptr_t)&fp
[1] +
457 sizeof (uintptr_t) * 2);
460 stack
= (uintptr_t *)&rp
->r_rdi
;
462 stack
= (uintptr_t *)(rp
->r_rsp
);
472 * We know that we did not come through a trap to get into
473 * dtrace_probe() -- the provider simply called dtrace_probe()
474 * directly. As this is the case, we need to shift the argument
475 * that we're looking for: the probe ID is the first argument to
476 * dtrace_probe(), so the argument n will actually be found where
477 * one would expect to find argument (n + 1).
484 * This shouldn't happen. If the argument is passed in a
485 * register then it should have been, well, passed in a
488 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
494 stack
= (uintptr_t *)&fp
[1];
497 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
499 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
506 dtrace_getstackdepth(int aframes
)
508 struct frame
*fp
= (struct frame
*)dtrace_getfp();
509 struct frame
*nextfp
, *minfp
, *stacktop
;
513 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
514 stacktop
= (struct frame
*)(CPU
->cpu_intr_stack
+ SA(MINFRAME
));
516 stacktop
= (struct frame
*)curthread
->t_stk
;
524 nextfp
= (struct frame
*)fp
->fr_savfp
;
526 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
529 * Hop from interrupt stack to thread stack.
531 stacktop
= (struct frame
*)curthread
->t_stk
;
532 minfp
= (struct frame
*)curthread
->t_stkbase
;
543 if (depth
<= aframes
)
546 return (depth
- aframes
);
550 dtrace_getreg(struct regs
*rp
, uint_t reg
)
566 REG_TRAPNO
, /* TRAPNO */
576 if (reg
>= sizeof (regmap
) / sizeof (int)) {
577 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
626 return (rp
->r_trapno
);
640 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
646 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
650 return ((&rp
->r_gs
)[reg
]);
655 dtrace_copycheck(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
)
657 ASSERT(kaddr
>= kernelbase
&& kaddr
+ size
>= kaddr
);
659 if (uaddr
+ size
>= kernelbase
|| uaddr
+ size
< uaddr
) {
660 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
661 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
670 dtrace_copyin(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
,
671 volatile uint16_t *flags
)
673 if (dtrace_copycheck(uaddr
, kaddr
, size
))
674 dtrace_copy(uaddr
, kaddr
, size
);
679 dtrace_copyout(uintptr_t kaddr
, uintptr_t uaddr
, size_t size
,
680 volatile uint16_t *flags
)
682 if (dtrace_copycheck(uaddr
, kaddr
, size
))
683 dtrace_copy(kaddr
, uaddr
, size
);
687 dtrace_copyinstr(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
,
688 volatile uint16_t *flags
)
690 if (dtrace_copycheck(uaddr
, kaddr
, size
))
691 dtrace_copystr(uaddr
, kaddr
, size
, flags
);
695 dtrace_copyoutstr(uintptr_t kaddr
, uintptr_t uaddr
, size_t size
,
696 volatile uint16_t *flags
)
698 if (dtrace_copycheck(uaddr
, kaddr
, size
))
699 dtrace_copystr(kaddr
, uaddr
, size
, flags
);
703 dtrace_fuword8(void *uaddr
)
705 extern uint8_t dtrace_fuword8_nocheck(void *);
706 if ((uintptr_t)uaddr
>= _userlimit
) {
707 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
708 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
711 return (dtrace_fuword8_nocheck(uaddr
));
715 dtrace_fuword16(void *uaddr
)
717 extern uint16_t dtrace_fuword16_nocheck(void *);
718 if ((uintptr_t)uaddr
>= _userlimit
) {
719 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
720 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
723 return (dtrace_fuword16_nocheck(uaddr
));
727 dtrace_fuword32(void *uaddr
)
729 extern uint32_t dtrace_fuword32_nocheck(void *);
730 if ((uintptr_t)uaddr
>= _userlimit
) {
731 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
732 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
735 return (dtrace_fuword32_nocheck(uaddr
));
739 dtrace_fuword64(void *uaddr
)
741 extern uint64_t dtrace_fuword64_nocheck(void *);
742 if ((uintptr_t)uaddr
>= _userlimit
) {
743 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
744 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
747 return (dtrace_fuword64_nocheck(uaddr
));