4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
28 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/stack.h>
36 #include <machine/frame.h>
37 #include <machine/md_var.h>
38 #include <machine/reg.h>
39 #include <machine/stack.h>
40 #include <x86/ifunc.h>
43 #include <vm/vm_param.h>
48 uint8_t dtrace_fuword8_nocheck(void *);
49 uint16_t dtrace_fuword16_nocheck(void *);
50 uint32_t dtrace_fuword32_nocheck(void *);
51 uint64_t dtrace_fuword64_nocheck(void *);
53 int dtrace_ustackdepth_max = 2048;
56 dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
61 struct amd64_frame *frame;
63 pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller;
66 pcstack[depth++] = (pc_t) intrpc;
70 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
72 frame = (struct amd64_frame *)rbp;
73 while (depth < pcstack_limit) {
74 if (!INKERNEL((long) frame))
77 callpc = frame->f_retaddr;
79 if (!INKERNEL(callpc))
84 if ((aframes == 0) && (caller != 0)) {
85 pcstack[depth++] = caller;
89 pcstack[depth++] = callpc;
92 if (frame->f_frame <= frame ||
93 (vm_offset_t)frame->f_frame >= curthread->td_kstack +
94 curthread->td_kstack_pages * PAGE_SIZE)
96 frame = frame->f_frame;
99 for (; depth < pcstack_limit; depth++) {
105 dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
109 volatile uint16_t *flags =
110 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
113 ASSERT(pcstack == NULL || pcstack_limit > 0);
114 ASSERT(dtrace_ustackdepth_max > 0);
118 * We limit the number of times we can go around this
119 * loop to account for a circular stack.
121 if (ret++ >= dtrace_ustackdepth_max) {
122 *flags |= CPU_DTRACE_BADSTACK;
123 cpu_core[curcpu].cpuc_dtrace_illval = sp;
127 if (pcstack != NULL) {
128 *pcstack++ = (uint64_t)pc;
130 if (pcstack_limit <= 0)
139 pc = dtrace_fuword64((void *)(sp +
140 offsetof(struct amd64_frame, f_retaddr)));
141 sp = dtrace_fuword64((void *)sp);
144 *flags |= CPU_DTRACE_BADSTACK;
145 cpu_core[curcpu].cpuc_dtrace_illval = sp;
150 * This is totally bogus: if we faulted, we're going to clear
151 * the fault and break. This is to deal with the apparently
152 * broken Java stacks on x86.
154 if (*flags & CPU_DTRACE_FAULT) {
155 *flags &= ~CPU_DTRACE_FAULT;
164 dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
167 struct trapframe *tf;
168 uintptr_t pc, sp, fp;
169 volatile uint16_t *flags =
170 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
173 if (*flags & CPU_DTRACE_FAULT)
176 if (pcstack_limit <= 0)
180 * If there's no user context we still need to zero the stack.
182 if (p == NULL || (tf = curthread->td_frame) == NULL)
185 *pcstack++ = (uint64_t)p->p_pid;
188 if (pcstack_limit <= 0)
195 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
197 * In an entry probe. The frame pointer has not yet been
198 * pushed (that happens in the function prologue). The
199 * best approach is to add the current pc as a missing top
200 * of stack and back the pc up to the caller, which is stored
201 * at the current stack pointer address since the call
202 * instruction puts it there right before the branch.
205 *pcstack++ = (uint64_t)pc;
207 if (pcstack_limit <= 0)
210 pc = dtrace_fuword64((void *) sp);
213 n = dtrace_getustack_common(pcstack, pcstack_limit, pc, fp);
215 ASSERT(n <= pcstack_limit);
221 while (pcstack_limit-- > 0)
226 dtrace_getustackdepth(void)
229 struct trapframe *tf;
230 uintptr_t pc, fp, sp;
233 if (p == NULL || (tf = curthread->td_frame) == NULL)
236 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
243 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
245 * In an entry probe. The frame pointer has not yet been
246 * pushed (that happens in the function prologue). The
247 * best approach is to add the current pc as a missing top
248 * of stack and back the pc up to the caller, which is stored
249 * at the current stack pointer address since the call
250 * instruction puts it there right before the branch.
253 pc = dtrace_fuword64((void *) sp);
257 n += dtrace_getustack_common(NULL, 0, pc, fp);
263 dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
266 struct trapframe *tf;
267 uintptr_t pc, sp, fp;
268 volatile uint16_t *flags =
269 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
270 #ifdef notyet /* XXX signal stack */
271 uintptr_t oldcontext;
275 if (*flags & CPU_DTRACE_FAULT)
278 if (pcstack_limit <= 0)
282 * If there's no user context we still need to zero the stack.
284 if (p == NULL || (tf = curthread->td_frame) == NULL)
287 *pcstack++ = (uint64_t)p->p_pid;
290 if (pcstack_limit <= 0)
297 #ifdef notyet /* XXX signal stack */
298 oldcontext = lwp->lwp_oldcontext;
299 s1 = sizeof (struct xframe) + 2 * sizeof (long);
300 s2 = s1 + sizeof (siginfo_t);
303 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
304 *pcstack++ = (uint64_t)pc;
307 if (pcstack_limit <= 0)
310 pc = dtrace_fuword64((void *)sp);
314 *pcstack++ = (uint64_t)pc;
317 if (pcstack_limit <= 0)
323 #ifdef notyet /* XXX signal stack */
324 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
325 ucontext_t *ucp = (ucontext_t *)oldcontext;
326 greg_t *gregs = ucp->uc_mcontext.gregs;
328 sp = dtrace_fulword(&gregs[REG_FP]);
329 pc = dtrace_fulword(&gregs[REG_PC]);
331 oldcontext = dtrace_fulword(&ucp->uc_link);
335 pc = dtrace_fuword64((void *)(fp +
336 offsetof(struct amd64_frame, f_retaddr)));
337 fp = dtrace_fuword64((void *)fp);
341 * This is totally bogus: if we faulted, we're going to clear
342 * the fault and break. This is to deal with the apparently
343 * broken Java stacks on x86.
345 if (*flags & CPU_DTRACE_FAULT) {
346 *flags &= ~CPU_DTRACE_FAULT;
352 while (pcstack_limit-- > 0)
358 dtrace_getarg(int arg, int aframes)
361 struct amd64_frame *fp = (struct amd64_frame *)dtrace_getfp();
366 * A total of 6 arguments are passed via registers; any argument with
367 * index of 5 or lower is therefore in a register.
371 for (i = 1; i <= aframes; i++) {
374 if (P2ROUNDUP(fp->f_retaddr, 16) ==
375 (long)dtrace_invop_callsite) {
377 * In the case of amd64, we will use the pointer to the
378 * regs structure that was pushed when we took the
379 * trap. To get this structure, we must increment
380 * beyond the frame structure, and then again beyond
381 * the calling RIP stored in dtrace_invop(). If the
382 * argument that we're seeking is passed on the stack,
383 * we'll pull the true stack pointer out of the saved
384 * registers and decrement our argument by the number
385 * of arguments passed in registers; if the argument
386 * we're seeking is passed in registers, we can just
389 struct trapframe *tf = (struct trapframe *)&fp[1];
394 stack = (uintptr_t *)&tf->tf_rdi;
397 stack = (uintptr_t *)&tf->tf_rsi;
400 stack = (uintptr_t *)&tf->tf_rdx;
403 stack = (uintptr_t *)&tf->tf_rcx;
406 stack = (uintptr_t *)&tf->tf_r8;
409 stack = (uintptr_t *)&tf->tf_r9;
414 stack = (uintptr_t *)(tf->tf_rsp);
423 * We know that we did not come through a trap to get into
424 * dtrace_probe() -- the provider simply called dtrace_probe()
425 * directly. As this is the case, we need to shift the argument
426 * that we're looking for: the probe ID is the first argument to
427 * dtrace_probe(), so the argument n will actually be found where
428 * one would expect to find argument (n + 1).
434 * This shouldn't happen. If the argument is passed in a
435 * register then it should have been, well, passed in a
438 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
443 stack = (uintptr_t *)&fp[1];
446 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
448 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
454 dtrace_getstackdepth(int aframes)
457 struct amd64_frame *frame;
461 rbp = dtrace_getfp();
462 frame = (struct amd64_frame *)rbp;
465 if (!INKERNEL((long) frame))
467 if (!INKERNEL((long) frame->f_frame))
470 if (frame->f_frame <= frame ||
471 (vm_offset_t)frame->f_frame >= curthread->td_kstack +
472 curthread->td_kstack_pages * PAGE_SIZE)
474 frame = frame->f_frame;
479 return depth - aframes;
483 dtrace_getreg(struct trapframe *rp, uint_t reg)
485 /* This table is dependent on reg.d. */
493 REG_RBP, /* 6 EBP, REG_FP */
495 REG_RBX, /* 8 EBX, REG_R1 */
497 REG_RCX, /* 10 ECX */
498 REG_RAX, /* 11 EAX, REG_R0 */
499 REG_TRAPNO, /* 12 TRAPNO */
500 REG_ERR, /* 13 ERR */
501 REG_RIP, /* 14 EIP, REG_PC */
503 REG_RFL, /* 16 EFL, REG_PS */
504 REG_RSP, /* 17 UESP, REG_SP */
513 if (reg >= sizeof (regmap) / sizeof (int)) {
514 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
520 /* This is dependent on reg.d. */
568 return (rp->tf_trapno);
578 return (rp->tf_rflags);
582 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
588 dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
590 ASSERT(INKERNEL(kaddr) && kaddr + size >= kaddr);
592 if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) {
593 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
594 cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
602 dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
603 volatile uint16_t *flags)
605 if (dtrace_copycheck(uaddr, kaddr, size))
606 dtrace_copy(uaddr, kaddr, size);
610 dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
611 volatile uint16_t *flags)
613 if (dtrace_copycheck(uaddr, kaddr, size))
614 dtrace_copy(kaddr, uaddr, size);
618 dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
619 volatile uint16_t *flags)
621 if (dtrace_copycheck(uaddr, kaddr, size))
622 dtrace_copystr(uaddr, kaddr, size, flags);
626 dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
627 volatile uint16_t *flags)
629 if (dtrace_copycheck(uaddr, kaddr, size))
630 dtrace_copystr(kaddr, uaddr, size, flags);
634 dtrace_fuword8(void *uaddr)
636 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
637 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
638 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
641 return (dtrace_fuword8_nocheck(uaddr));
645 dtrace_fuword16(void *uaddr)
647 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
648 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
649 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
652 return (dtrace_fuword16_nocheck(uaddr));
656 dtrace_fuword32(void *uaddr)
658 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
659 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
660 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
663 return (dtrace_fuword32_nocheck(uaddr));
667 dtrace_fuword64(void *uaddr)
669 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
670 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
671 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
674 return (dtrace_fuword64_nocheck(uaddr));
678 * ifunc resolvers for SMAP support
680 void dtrace_copy_nosmap(uintptr_t, uintptr_t, size_t);
681 void dtrace_copy_smap(uintptr_t, uintptr_t, size_t);
682 DEFINE_IFUNC(, void, dtrace_copy, (uintptr_t, uintptr_t, size_t))
685 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
686 dtrace_copy_smap : dtrace_copy_nosmap);
689 void dtrace_copystr_nosmap(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
690 void dtrace_copystr_smap(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
691 DEFINE_IFUNC(, void, dtrace_copystr, (uintptr_t, uintptr_t, size_t,
692 volatile uint16_t *))
695 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
696 dtrace_copystr_smap : dtrace_copystr_nosmap);
699 uintptr_t dtrace_fulword_nosmap(void *);
700 uintptr_t dtrace_fulword_smap(void *);
701 DEFINE_IFUNC(, uintptr_t, dtrace_fulword, (void *))
704 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
705 dtrace_fulword_smap : dtrace_fulword_nosmap);
708 uint8_t dtrace_fuword8_nocheck_nosmap(void *);
709 uint8_t dtrace_fuword8_nocheck_smap(void *);
710 DEFINE_IFUNC(, uint8_t, dtrace_fuword8_nocheck, (void *))
713 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
714 dtrace_fuword8_nocheck_smap : dtrace_fuword8_nocheck_nosmap);
717 uint16_t dtrace_fuword16_nocheck_nosmap(void *);
718 uint16_t dtrace_fuword16_nocheck_smap(void *);
719 DEFINE_IFUNC(, uint16_t, dtrace_fuword16_nocheck, (void *))
722 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
723 dtrace_fuword16_nocheck_smap : dtrace_fuword16_nocheck_nosmap);
726 uint32_t dtrace_fuword32_nocheck_nosmap(void *);
727 uint32_t dtrace_fuword32_nocheck_smap(void *);
728 DEFINE_IFUNC(, uint32_t, dtrace_fuword32_nocheck, (void *))
731 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
732 dtrace_fuword32_nocheck_smap : dtrace_fuword32_nocheck_nosmap);
735 uint64_t dtrace_fuword64_nocheck_nosmap(void *);
736 uint64_t dtrace_fuword64_nocheck_smap(void *);
737 DEFINE_IFUNC(, uint64_t, dtrace_fuword64_nocheck, (void *))
740 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
741 dtrace_fuword64_nocheck_smap : dtrace_fuword64_nocheck_nosmap);