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>
42 #include <vm/vm_param.h>
47 uint8_t dtrace_fuword8_nocheck(void *);
48 uint16_t dtrace_fuword16_nocheck(void *);
49 uint32_t dtrace_fuword32_nocheck(void *);
50 uint64_t dtrace_fuword64_nocheck(void *);
52 int dtrace_ustackdepth_max = 2048;
55 dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
60 struct amd64_frame *frame;
62 pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller;
65 pcstack[depth++] = (pc_t) intrpc;
69 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
71 frame = (struct amd64_frame *)rbp;
72 while (depth < pcstack_limit) {
73 if (!INKERNEL((long) frame))
76 callpc = frame->f_retaddr;
78 if (!INKERNEL(callpc))
83 if ((aframes == 0) && (caller != 0)) {
84 pcstack[depth++] = caller;
88 pcstack[depth++] = callpc;
91 if (frame->f_frame <= frame ||
92 (vm_offset_t)frame->f_frame >=
93 (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE)
95 frame = frame->f_frame;
98 for (; depth < pcstack_limit; depth++) {
104 dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
108 volatile uint16_t *flags =
109 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
112 ASSERT(pcstack == NULL || pcstack_limit > 0);
113 ASSERT(dtrace_ustackdepth_max > 0);
117 * We limit the number of times we can go around this
118 * loop to account for a circular stack.
120 if (ret++ >= dtrace_ustackdepth_max) {
121 *flags |= CPU_DTRACE_BADSTACK;
122 cpu_core[curcpu].cpuc_dtrace_illval = sp;
126 if (pcstack != NULL) {
127 *pcstack++ = (uint64_t)pc;
129 if (pcstack_limit <= 0)
138 pc = dtrace_fuword64((void *)(sp +
139 offsetof(struct amd64_frame, f_retaddr)));
140 sp = dtrace_fuword64((void *)sp);
143 *flags |= CPU_DTRACE_BADSTACK;
144 cpu_core[curcpu].cpuc_dtrace_illval = sp;
149 * This is totally bogus: if we faulted, we're going to clear
150 * the fault and break. This is to deal with the apparently
151 * broken Java stacks on x86.
153 if (*flags & CPU_DTRACE_FAULT) {
154 *flags &= ~CPU_DTRACE_FAULT;
163 dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
166 struct trapframe *tf;
167 uintptr_t pc, sp, fp;
168 volatile uint16_t *flags =
169 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
172 if (*flags & CPU_DTRACE_FAULT)
175 if (pcstack_limit <= 0)
179 * If there's no user context we still need to zero the stack.
181 if (p == NULL || (tf = curthread->td_frame) == NULL)
184 *pcstack++ = (uint64_t)p->p_pid;
187 if (pcstack_limit <= 0)
194 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
196 * In an entry probe. The frame pointer has not yet been
197 * pushed (that happens in the function prologue). The
198 * best approach is to add the current pc as a missing top
199 * of stack and back the pc up to the caller, which is stored
200 * at the current stack pointer address since the call
201 * instruction puts it there right before the branch.
204 *pcstack++ = (uint64_t)pc;
206 if (pcstack_limit <= 0)
209 pc = dtrace_fuword64((void *) sp);
212 n = dtrace_getustack_common(pcstack, pcstack_limit, pc, fp);
214 ASSERT(n <= pcstack_limit);
220 while (pcstack_limit-- > 0)
225 dtrace_getustackdepth(void)
228 struct trapframe *tf;
229 uintptr_t pc, fp, sp;
232 if (p == NULL || (tf = curthread->td_frame) == NULL)
235 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
242 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
244 * In an entry probe. The frame pointer has not yet been
245 * pushed (that happens in the function prologue). The
246 * best approach is to add the current pc as a missing top
247 * of stack and back the pc up to the caller, which is stored
248 * at the current stack pointer address since the call
249 * instruction puts it there right before the branch.
252 pc = dtrace_fuword64((void *) sp);
256 n += dtrace_getustack_common(NULL, 0, pc, fp);
262 dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
265 struct trapframe *tf;
266 uintptr_t pc, sp, fp;
267 volatile uint16_t *flags =
268 (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
269 #ifdef notyet /* XXX signal stack */
270 uintptr_t oldcontext;
274 if (*flags & CPU_DTRACE_FAULT)
277 if (pcstack_limit <= 0)
281 * If there's no user context we still need to zero the stack.
283 if (p == NULL || (tf = curthread->td_frame) == NULL)
286 *pcstack++ = (uint64_t)p->p_pid;
289 if (pcstack_limit <= 0)
296 #ifdef notyet /* XXX signal stack */
297 oldcontext = lwp->lwp_oldcontext;
298 s1 = sizeof (struct xframe) + 2 * sizeof (long);
299 s2 = s1 + sizeof (siginfo_t);
302 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
303 *pcstack++ = (uint64_t)pc;
306 if (pcstack_limit <= 0)
309 pc = dtrace_fuword64((void *)sp);
313 *pcstack++ = (uint64_t)pc;
316 if (pcstack_limit <= 0)
322 #ifdef notyet /* XXX signal stack */
323 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
324 ucontext_t *ucp = (ucontext_t *)oldcontext;
325 greg_t *gregs = ucp->uc_mcontext.gregs;
327 sp = dtrace_fulword(&gregs[REG_FP]);
328 pc = dtrace_fulword(&gregs[REG_PC]);
330 oldcontext = dtrace_fulword(&ucp->uc_link);
334 pc = dtrace_fuword64((void *)(fp +
335 offsetof(struct amd64_frame, f_retaddr)));
336 fp = dtrace_fuword64((void *)fp);
340 * This is totally bogus: if we faulted, we're going to clear
341 * the fault and break. This is to deal with the apparently
342 * broken Java stacks on x86.
344 if (*flags & CPU_DTRACE_FAULT) {
345 *flags &= ~CPU_DTRACE_FAULT;
351 while (pcstack_limit-- > 0)
357 dtrace_getarg(int arg, int aframes)
360 struct amd64_frame *fp = (struct amd64_frame *)dtrace_getfp();
365 * A total of 6 arguments are passed via registers; any argument with
366 * index of 5 or lower is therefore in a register.
370 for (i = 1; i <= aframes; i++) {
373 if (P2ROUNDUP(fp->f_retaddr, 16) ==
374 (long)dtrace_invop_callsite) {
376 * In the case of amd64, we will use the pointer to the
377 * regs structure that was pushed when we took the
378 * trap. To get this structure, we must increment
379 * beyond the frame structure, and then again beyond
380 * the calling RIP stored in dtrace_invop(). If the
381 * argument that we're seeking is passed on the stack,
382 * we'll pull the true stack pointer out of the saved
383 * registers and decrement our argument by the number
384 * of arguments passed in registers; if the argument
385 * we're seeking is passed in regsiters, we can just
388 struct trapframe *tf =
389 (struct trapframe *)((uintptr_t)&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 + 2;
446 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
448 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
455 dtrace_getstackdepth(int aframes)
458 struct amd64_frame *frame;
462 rbp = dtrace_getfp();
463 frame = (struct amd64_frame *)rbp;
466 if (!INKERNEL((long) frame))
468 if (!INKERNEL((long) frame->f_frame))
471 if (frame->f_frame <= frame ||
472 (vm_offset_t)frame->f_frame >=
473 (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE)
475 frame = frame->f_frame;
480 return depth - aframes;
484 dtrace_getreg(struct trapframe *rp, uint_t reg)
486 /* This table is dependent on reg.d. */
494 REG_RBP, /* 6 EBP, REG_FP */
496 REG_RBX, /* 8 EBX, REG_R1 */
498 REG_RCX, /* 10 ECX */
499 REG_RAX, /* 11 EAX, REG_R0 */
500 REG_TRAPNO, /* 12 TRAPNO */
501 REG_ERR, /* 13 ERR */
502 REG_RIP, /* 14 EIP, REG_PC */
504 REG_RFL, /* 16 EFL, REG_PS */
505 REG_RSP, /* 17 UESP, REG_SP */
510 if (reg >= sizeof (regmap) / sizeof (int)) {
511 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
517 /* This is dependent on reg.d. */
561 return (rp->tf_trapno);
571 return (rp->tf_rflags);
575 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
581 dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
583 ASSERT(INKERNEL(kaddr) && kaddr + size >= kaddr);
585 if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) {
586 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
587 cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
595 dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
596 volatile uint16_t *flags)
598 if (dtrace_copycheck(uaddr, kaddr, size))
599 dtrace_copy(uaddr, kaddr, size);
603 dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
604 volatile uint16_t *flags)
606 if (dtrace_copycheck(uaddr, kaddr, size))
607 dtrace_copy(kaddr, uaddr, size);
611 dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
612 volatile uint16_t *flags)
614 if (dtrace_copycheck(uaddr, kaddr, size))
615 dtrace_copystr(uaddr, kaddr, size, flags);
619 dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
620 volatile uint16_t *flags)
622 if (dtrace_copycheck(uaddr, kaddr, size))
623 dtrace_copystr(kaddr, uaddr, size, flags);
627 dtrace_fuword8(void *uaddr)
629 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
630 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
631 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
634 return (dtrace_fuword8_nocheck(uaddr));
638 dtrace_fuword16(void *uaddr)
640 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
641 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
642 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
645 return (dtrace_fuword16_nocheck(uaddr));
649 dtrace_fuword32(void *uaddr)
651 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
652 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
653 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
656 return (dtrace_fuword32_nocheck(uaddr));
660 dtrace_fuword64(void *uaddr)
662 if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
663 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
664 cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
667 return (dtrace_fuword64_nocheck(uaddr));