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.
29 * Copyright (c) 2017 Joyent, Inc.
32 #include <sys/dtrace_impl.h>
33 #include <sys/stack.h>
34 #include <sys/frame.h>
35 #include <sys/cmn_err.h>
36 #include <sys/privregs.h>
37 #include <sys/sysmacros.h>
39 extern uintptr_t kernelbase
;
41 int dtrace_ustackdepth_max
= 2048;
44 dtrace_getpcstack(pc_t
*pcstack
, int pcstack_limit
, int aframes
,
47 struct frame
*fp
= (struct frame
*)dtrace_getfp();
48 struct frame
*nextfp
, *minfp
, *stacktop
;
50 int on_intr
, last
= 0;
52 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
54 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
55 stacktop
= (struct frame
*)(CPU
->cpu_intr_stack
+ SA(MINFRAME
));
57 stacktop
= (struct frame
*)curthread
->t_stk
;
62 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
63 pcstack
[depth
++] = (pc_t
)intrpc
;
65 while (depth
< pcstack_limit
) {
66 nextfp
= (struct frame
*)fp
->fr_savfp
;
69 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
72 * Hop from interrupt stack to thread stack.
74 stacktop
= (struct frame
*)curthread
->t_stk
;
75 minfp
= (struct frame
*)curthread
->t_stkbase
;
81 * This is the last frame we can process; indicate
82 * that we should return after processing this frame.
88 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
90 * We've just run out of artificial frames,
91 * and we have a valid caller -- fill it in
94 ASSERT(depth
< pcstack_limit
);
95 pcstack
[depth
++] = (pc_t
)caller
;
96 caller
= (uintptr_t)NULL
;
99 if (depth
< pcstack_limit
)
100 pcstack
[depth
++] = (pc_t
)pc
;
104 while (depth
< pcstack_limit
)
105 pcstack
[depth
++] = (pc_t
)NULL
;
115 dtrace_getustack_common(uint64_t *pcstack
, int pcstack_limit
, uintptr_t pc
,
118 klwp_t
*lwp
= ttolwp(curthread
);
120 uintptr_t oldcontext
= lwp
->lwp_oldcontext
;
122 volatile uint16_t *flags
=
123 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
127 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
128 ASSERT(dtrace_ustackdepth_max
> 0);
130 if (p
->p_model
== DATAMODEL_NATIVE
) {
131 s1
= sizeof (struct frame
) + 2 * sizeof (long);
132 s2
= s1
+ sizeof (siginfo_t
);
134 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
135 s2
= s1
+ sizeof (siginfo32_t
);
140 * We limit the number of times we can go around this
141 * loop to account for a circular stack.
143 if (ret
++ >= dtrace_ustackdepth_max
) {
144 *flags
|= CPU_DTRACE_BADSTACK
;
145 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= sp
;
149 if (pcstack
!= NULL
) {
150 *pcstack
++ = (uint64_t)pc
;
152 if (pcstack_limit
<= 0)
161 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
162 if (p
->p_model
== DATAMODEL_NATIVE
) {
163 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
164 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
166 sp
= dtrace_fulword(&gregs
[REG_FP
]);
167 pc
= dtrace_fulword(&gregs
[REG_PC
]);
169 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
171 ucontext32_t
*ucp
= (ucontext32_t
*)oldcontext
;
172 greg32_t
*gregs
= ucp
->uc_mcontext
.gregs
;
174 sp
= dtrace_fuword32(&gregs
[EBP
]);
175 pc
= dtrace_fuword32(&gregs
[EIP
]);
177 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
180 if (p
->p_model
== DATAMODEL_NATIVE
) {
181 struct frame
*fr
= (struct frame
*)sp
;
183 pc
= dtrace_fulword(&fr
->fr_savpc
);
184 sp
= dtrace_fulword(&fr
->fr_savfp
);
186 struct frame32
*fr
= (struct frame32
*)sp
;
188 pc
= dtrace_fuword32(&fr
->fr_savpc
);
189 sp
= dtrace_fuword32(&fr
->fr_savfp
);
194 *flags
|= CPU_DTRACE_BADSTACK
;
195 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= sp
;
200 * This is totally bogus: if we faulted, we're going to clear
201 * the fault and break. This is to deal with the apparently
202 * broken Java stacks on x86.
204 if (*flags
& CPU_DTRACE_FAULT
) {
205 *flags
&= ~CPU_DTRACE_FAULT
;
214 dtrace_getupcstack(uint64_t *pcstack
, int pcstack_limit
)
216 klwp_t
*lwp
= ttolwp(curthread
);
222 ASSERT(DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
));
224 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
227 if (pcstack_limit
<= 0)
231 * If there's no user context we still need to zero the stack.
233 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
236 *pcstack
++ = (uint64_t)p
->p_pid
;
239 if (pcstack_limit
<= 0)
245 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
246 *pcstack
++ = (uint64_t)pc
;
248 if (pcstack_limit
<= 0)
251 if (p
->p_model
== DATAMODEL_NATIVE
)
252 pc
= dtrace_fulword((void *)rp
->r_sp
);
254 pc
= dtrace_fuword32((void *)rp
->r_sp
);
257 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, sp
);
259 ASSERT(n
<= pcstack_limit
);
265 while (pcstack_limit
-- > 0)
266 *pcstack
++ = (uintptr_t)NULL
;
270 dtrace_getustackdepth(void)
272 klwp_t
*lwp
= ttolwp(curthread
);
278 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
281 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
287 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
290 if (p
->p_model
== DATAMODEL_NATIVE
)
291 pc
= dtrace_fulword((void *)rp
->r_sp
);
293 pc
= dtrace_fuword32((void *)rp
->r_sp
);
296 n
+= dtrace_getustack_common(NULL
, 0, pc
, sp
);
302 dtrace_getufpstack(uint64_t *pcstack
, uint64_t *fpstack
, int pcstack_limit
)
304 klwp_t
*lwp
= ttolwp(curthread
);
307 uintptr_t pc
, sp
, oldcontext
;
308 volatile uint16_t *flags
=
309 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
312 if (*flags
& CPU_DTRACE_FAULT
)
315 if (pcstack_limit
<= 0)
319 * If there's no user context we still need to zero the stack.
321 if (lwp
== NULL
|| p
== NULL
|| (rp
= lwp
->lwp_regs
) == NULL
)
324 *pcstack
++ = (uint64_t)p
->p_pid
;
327 if (pcstack_limit
<= 0)
332 oldcontext
= lwp
->lwp_oldcontext
;
334 if (p
->p_model
== DATAMODEL_NATIVE
) {
335 s1
= sizeof (struct frame
) + 2 * sizeof (long);
336 s2
= s1
+ sizeof (siginfo_t
);
338 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
339 s2
= s1
+ sizeof (siginfo32_t
);
342 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
343 *pcstack
++ = (uint64_t)pc
;
346 if (pcstack_limit
<= 0)
349 if (p
->p_model
== DATAMODEL_NATIVE
)
350 pc
= dtrace_fulword((void *)rp
->r_sp
);
352 pc
= dtrace_fuword32((void *)rp
->r_sp
);
356 *pcstack
++ = (uint64_t)pc
;
359 if (pcstack_limit
<= 0)
365 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
366 if (p
->p_model
== DATAMODEL_NATIVE
) {
367 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
368 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
370 sp
= dtrace_fulword(&gregs
[REG_FP
]);
371 pc
= dtrace_fulword(&gregs
[REG_PC
]);
373 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
375 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
376 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
378 sp
= dtrace_fuword32(&gregs
[EBP
]);
379 pc
= dtrace_fuword32(&gregs
[EIP
]);
381 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
384 if (p
->p_model
== DATAMODEL_NATIVE
) {
385 struct frame
*fr
= (struct frame
*)sp
;
387 pc
= dtrace_fulword(&fr
->fr_savpc
);
388 sp
= dtrace_fulword(&fr
->fr_savfp
);
390 struct frame32
*fr
= (struct frame32
*)sp
;
392 pc
= dtrace_fuword32(&fr
->fr_savpc
);
393 sp
= dtrace_fuword32(&fr
->fr_savfp
);
398 * This is totally bogus: if we faulted, we're going to clear
399 * the fault and break. This is to deal with the apparently
400 * broken Java stacks on x86.
402 if (*flags
& CPU_DTRACE_FAULT
) {
403 *flags
&= ~CPU_DTRACE_FAULT
;
409 while (pcstack_limit
-- > 0)
410 *pcstack
++ = (uintptr_t)NULL
;
415 dtrace_getarg(int arg
, int aframes
)
418 struct frame
*fp
= (struct frame
*)dtrace_getfp();
423 * A total of 6 arguments are passed via registers; any argument with
424 * index of 5 or lower is therefore in a register.
429 for (i
= 1; i
<= aframes
; i
++) {
430 fp
= (struct frame
*)(fp
->fr_savfp
);
432 if (fp
->fr_savpc
== (pc_t
)dtrace_invop_callsite
) {
433 #if !defined(__amd64)
435 * If we pass through the invalid op handler, we will
436 * use the pointer that it passed to the stack as the
437 * second argument to dtrace_invop() as the pointer to
438 * the stack. When using this stack, we must step
439 * beyond the EIP that was pushed when the trap was
440 * taken -- hence the "+ 1" below.
442 stack
= ((uintptr_t **)&fp
[1])[1] + 1;
445 * In the case of amd64, we will use the pointer to the
446 * regs structure that was pushed when we took the
447 * trap. To get this structure, we must increment
448 * beyond the frame structure, the calling RIP, and
449 * padding stored in dtrace_invop(). If the argument
450 * that we're seeking is passed on the stack, we'll
451 * pull the true stack pointer out of the saved
452 * registers and decrement our argument by the number
453 * of arguments passed in registers; if the argument
454 * we're seeking is passed in regsiters, we can just
457 struct regs
*rp
= (struct regs
*)((uintptr_t)&fp
[1] +
458 sizeof (uintptr_t) * 2);
461 stack
= (uintptr_t *)&rp
->r_rdi
;
463 stack
= (uintptr_t *)(rp
->r_rsp
);
473 * We know that we did not come through a trap to get into
474 * dtrace_probe() -- the provider simply called dtrace_probe()
475 * directly. As this is the case, we need to shift the argument
476 * that we're looking for: the probe ID is the first argument to
477 * dtrace_probe(), so the argument n will actually be found where
478 * one would expect to find argument (n + 1).
485 * This shouldn't happen. If the argument is passed in a
486 * register then it should have been, well, passed in a
489 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
495 stack
= (uintptr_t *)&fp
[1];
498 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
500 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
507 dtrace_getstackdepth(int aframes
)
509 struct frame
*fp
= (struct frame
*)dtrace_getfp();
510 struct frame
*nextfp
, *minfp
, *stacktop
;
514 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
515 stacktop
= (struct frame
*)(CPU
->cpu_intr_stack
+ SA(MINFRAME
));
517 stacktop
= (struct frame
*)curthread
->t_stk
;
525 nextfp
= (struct frame
*)fp
->fr_savfp
;
527 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
530 * Hop from interrupt stack to thread stack.
532 stacktop
= (struct frame
*)curthread
->t_stk
;
533 minfp
= (struct frame
*)curthread
->t_stkbase
;
544 if (depth
<= aframes
)
547 return (depth
- aframes
);
551 static const int dtrace_regmap
[] = {
564 REG_TRAPNO
, /* TRAPNO */
576 dtrace_getreg(struct regs
*rp
, uint_t reg
)
580 if (reg
>= sizeof (dtrace_regmap
) / sizeof (int)) {
581 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
585 reg
= dtrace_regmap
[reg
];
630 return (rp
->r_trapno
);
644 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
650 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
654 return ((&rp
->r_gs
)[reg
]);
659 dtrace_setreg(struct regs
*rp
, uint_t reg
, ulong_t val
)
663 ASSERT(reg
< (sizeof (dtrace_regmap
) / sizeof (int)));
665 reg
= dtrace_regmap
[reg
];
720 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
724 #else /* defined(__amd64) */
751 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
754 #endif /* defined(__amd64) */
758 dtrace_copycheck(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
)
760 ASSERT(kaddr
>= kernelbase
&& kaddr
+ size
>= kaddr
);
762 if (uaddr
+ size
>= kernelbase
|| uaddr
+ size
< uaddr
) {
763 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
764 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
773 dtrace_copyin(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
,
774 volatile uint16_t *flags
)
776 if (dtrace_copycheck(uaddr
, kaddr
, size
))
777 dtrace_copy(uaddr
, kaddr
, size
);
782 dtrace_copyout(uintptr_t kaddr
, uintptr_t uaddr
, size_t size
,
783 volatile uint16_t *flags
)
785 if (dtrace_copycheck(uaddr
, kaddr
, size
))
786 dtrace_copy(kaddr
, uaddr
, size
);
790 dtrace_copyinstr(uintptr_t uaddr
, uintptr_t kaddr
, size_t size
,
791 volatile uint16_t *flags
)
793 if (dtrace_copycheck(uaddr
, kaddr
, size
))
794 dtrace_copystr(uaddr
, kaddr
, size
, flags
);
798 dtrace_copyoutstr(uintptr_t kaddr
, uintptr_t uaddr
, size_t size
,
799 volatile uint16_t *flags
)
801 if (dtrace_copycheck(uaddr
, kaddr
, size
))
802 dtrace_copystr(kaddr
, uaddr
, size
, flags
);
806 dtrace_fuword8(void *uaddr
)
808 extern uint8_t dtrace_fuword8_nocheck(void *);
809 if ((uintptr_t)uaddr
>= _userlimit
) {
810 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
811 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
814 return (dtrace_fuword8_nocheck(uaddr
));
818 dtrace_fuword16(void *uaddr
)
820 extern uint16_t dtrace_fuword16_nocheck(void *);
821 if ((uintptr_t)uaddr
>= _userlimit
) {
822 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
823 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
826 return (dtrace_fuword16_nocheck(uaddr
));
830 dtrace_fuword32(void *uaddr
)
832 extern uint32_t dtrace_fuword32_nocheck(void *);
833 if ((uintptr_t)uaddr
>= _userlimit
) {
834 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
835 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
838 return (dtrace_fuword32_nocheck(uaddr
));
842 dtrace_fuword64(void *uaddr
)
844 extern uint64_t dtrace_fuword64_nocheck(void *);
845 if ((uintptr_t)uaddr
>= _userlimit
) {
846 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
847 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= (uintptr_t)uaddr
;
850 return (dtrace_fuword64_nocheck(uaddr
));