17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a574db85Sraf  * Common Development and Distribution License (the "License").
6a574db85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "misc.h"
297c478bd9Sstevel@tonic-gate #include <ucontext.h>
307c478bd9Sstevel@tonic-gate #include <sys/frame.h>
317c478bd9Sstevel@tonic-gate #include <sys/stack.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
357c478bd9Sstevel@tonic-gate extern void flush_windows(void);
367c478bd9Sstevel@tonic-gate #define	UMEM_FRAMESIZE	MINFRAME
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * On x86, MINFRAME is defined to be 0, but we want to be sure we can
417c478bd9Sstevel@tonic-gate  * dereference the entire frame structure.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #define	UMEM_FRAMESIZE	(sizeof (struct frame))
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #else
467c478bd9Sstevel@tonic-gate #error needs update for new architecture
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
517c478bd9Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
547c478bd9Sstevel@tonic-gate int
getpcstack(uintptr_t * pcstack,int pcstack_limit,int check_signal)557c478bd9Sstevel@tonic-gate getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	struct frame *fp;
587c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp;
597c478bd9Sstevel@tonic-gate 	int depth = 0;
607c478bd9Sstevel@tonic-gate 	uintptr_t base = 0;
617c478bd9Sstevel@tonic-gate 	size_t size = 0;
627c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
637c478bd9Sstevel@tonic-gate 	int on_altstack = 0;
647c478bd9Sstevel@tonic-gate 	uintptr_t sigbase = 0;
657c478bd9Sstevel@tonic-gate 	size_t sigsize = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	stack_t st;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (stack_getbounds(&st) != 0) {
707c478bd9Sstevel@tonic-gate 		if (thr_stksegment(&st) != 0 ||
717c478bd9Sstevel@tonic-gate 		    (uintptr_t)st.ss_sp < st.ss_size) {
727c478bd9Sstevel@tonic-gate 			return (0);		/* unable to get stack bounds */
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		/*
757c478bd9Sstevel@tonic-gate 		 * thr_stksegment(3C) has a slightly different interface than
767c478bd9Sstevel@tonic-gate 		 * stack_getbounds(3C) -- correct it
777c478bd9Sstevel@tonic-gate 		 */
787c478bd9Sstevel@tonic-gate 		st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
797c478bd9Sstevel@tonic-gate 		st.ss_flags = 0;		/* can't be on-stack */
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	on_altstack = (st.ss_flags & SS_ONSTACK);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (st.ss_size != 0) {
847c478bd9Sstevel@tonic-gate 		base = (uintptr_t)st.ss_sp;
857c478bd9Sstevel@tonic-gate 		size = st.ss_size;
867c478bd9Sstevel@tonic-gate 	} else {
877c478bd9Sstevel@tonic-gate 		/*
887c478bd9Sstevel@tonic-gate 		 * If size == 0, then ss_sp is the *top* of the stack.
897c478bd9Sstevel@tonic-gate 		 *
907c478bd9Sstevel@tonic-gate 		 * Since we only allow increasing frame pointers, and we
9148bbca81SDaniel Hoffman 		 * know our caller set its up correctly, we can treat ss_sp
927c478bd9Sstevel@tonic-gate 		 * as an upper bound safely.
937c478bd9Sstevel@tonic-gate 		 */
947c478bd9Sstevel@tonic-gate 		base = 0;
957c478bd9Sstevel@tonic-gate 		size = (uintptr_t)st.ss_sp;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (check_signal != 0) {
997c478bd9Sstevel@tonic-gate 		void (*sigfunc)() = NULL;
1007c478bd9Sstevel@tonic-gate 		int sigfuncsize = 0;
1017c478bd9Sstevel@tonic-gate 		extern void thr_sighndlrinfo(void (**)(), int *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		thr_sighndlrinfo(&sigfunc, &sigfuncsize);
1047c478bd9Sstevel@tonic-gate 		sigbase = (uintptr_t)sigfunc;
1057c478bd9Sstevel@tonic-gate 		sigsize = sigfuncsize;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate #else /* UMEM_STANDALONE */
1087c478bd9Sstevel@tonic-gate 	base = (uintptr_t)umem_min_stack;
1097c478bd9Sstevel@tonic-gate 	size = umem_max_stack - umem_min_stack;
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * shorten size so that fr_savfp and fr_savpc will be within the stack
1147c478bd9Sstevel@tonic-gate 	 * bounds.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if (size >= UMEM_FRAMESIZE - 1)
1177c478bd9Sstevel@tonic-gate 		size -= (UMEM_FRAMESIZE - 1);
1187c478bd9Sstevel@tonic-gate 	else
1197c478bd9Sstevel@tonic-gate 		size = 0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
1227c478bd9Sstevel@tonic-gate 	flush_windows();
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
1267c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	minfp = fp;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (((uintptr_t)fp - base) >= size)
1317c478bd9Sstevel@tonic-gate 		return (0);	/* the frame pointer isn't in our stack */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
1347c478bd9Sstevel@tonic-gate 		uintptr_t tmp;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
1377c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
1387c478bd9Sstevel@tonic-gate 		tmp = (uintptr_t)nextfp;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		 * Check nextfp for validity.  It must be properly aligned,
1427c478bd9Sstevel@tonic-gate 		 * increasing compared to the last %fp (or the top of the
1437c478bd9Sstevel@tonic-gate 		 * stack we just switched to), and it must be inside
1447c478bd9Sstevel@tonic-gate 		 * [base, base + size).
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		if (tmp != SA(tmp))
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		else if (nextfp <= minfp || (tmp - base) >= size) {
1497c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
150*a6313a9dSToomas Soome 			if (tmp == (uintptr_t)NULL || !on_altstack)
1517c478bd9Sstevel@tonic-gate 				break;
1527c478bd9Sstevel@tonic-gate 			/*
1537c478bd9Sstevel@tonic-gate 			 * If we're on an alternate signal stack, try jumping
1547c478bd9Sstevel@tonic-gate 			 * to the main thread stack.
1557c478bd9Sstevel@tonic-gate 			 *
1567c478bd9Sstevel@tonic-gate 			 * If the main thread stack has an unlimited size, we
1577c478bd9Sstevel@tonic-gate 			 * punt, since we don't know where the frame pointer's
1587c478bd9Sstevel@tonic-gate 			 * been.
1597c478bd9Sstevel@tonic-gate 			 *
1607c478bd9Sstevel@tonic-gate 			 * (thr_stksegment() returns the *top of stack*
1617c478bd9Sstevel@tonic-gate 			 * in ss_sp, not the bottom)
1627c478bd9Sstevel@tonic-gate 			 */
1637c478bd9Sstevel@tonic-gate 			if (thr_stksegment(&st) == 0) {
1647c478bd9Sstevel@tonic-gate 				if (st.ss_size >= (uintptr_t)st.ss_sp ||
1657c478bd9Sstevel@tonic-gate 				    st.ss_size < UMEM_FRAMESIZE - 1)
1667c478bd9Sstevel@tonic-gate 					break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 				on_altstack = 0;
1697c478bd9Sstevel@tonic-gate 				base = (uintptr_t)st.ss_sp - st.ss_size;
1707c478bd9Sstevel@tonic-gate 				size = st.ss_size - (UMEM_FRAMESIZE - 1);
1717c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)base;
1727c478bd9Sstevel@tonic-gate 				continue;		/* try again */
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate #endif
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1797c478bd9Sstevel@tonic-gate 		if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
1807c478bd9Sstevel@tonic-gate 			umem_panic("called from signal handler");
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 		pcstack[depth++] = fp->fr_savpc;
1837c478bd9Sstevel@tonic-gate 		fp = nextfp;
1847c478bd9Sstevel@tonic-gate 		minfp = fp;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	return (depth);
1877c478bd9Sstevel@tonic-gate }
188