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 5*a574db85Sraf * Common Development and Distribution License (the "License"). 6*a574db85Sraf * 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 */ 21*a574db85Sraf 227c478bd9Sstevel@tonic-gate /* 23*a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include "misc.h" 307c478bd9Sstevel@tonic-gate #include <ucontext.h> 317c478bd9Sstevel@tonic-gate #include <sys/frame.h> 327c478bd9Sstevel@tonic-gate #include <sys/stack.h> 337c478bd9Sstevel@tonic-gate #include <stdio.h> 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9) 367c478bd9Sstevel@tonic-gate extern void flush_windows(void); 377c478bd9Sstevel@tonic-gate #define UMEM_FRAMESIZE MINFRAME 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * On x86, MINFRAME is defined to be 0, but we want to be sure we can 427c478bd9Sstevel@tonic-gate * dereference the entire frame structure. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #define UMEM_FRAMESIZE (sizeof (struct frame)) 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #else 477c478bd9Sstevel@tonic-gate #error needs update for new architecture 487c478bd9Sstevel@tonic-gate #endif 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking. 527c478bd9Sstevel@tonic-gate * Returns MIN(current stack depth, pcstack_limit). 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 557c478bd9Sstevel@tonic-gate int 567c478bd9Sstevel@tonic-gate getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal) 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate struct frame *fp; 597c478bd9Sstevel@tonic-gate struct frame *nextfp, *minfp; 607c478bd9Sstevel@tonic-gate int depth = 0; 617c478bd9Sstevel@tonic-gate uintptr_t base = 0; 627c478bd9Sstevel@tonic-gate size_t size = 0; 637c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 647c478bd9Sstevel@tonic-gate int on_altstack = 0; 657c478bd9Sstevel@tonic-gate uintptr_t sigbase = 0; 667c478bd9Sstevel@tonic-gate size_t sigsize = 0; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate stack_t st; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (stack_getbounds(&st) != 0) { 717c478bd9Sstevel@tonic-gate if (thr_stksegment(&st) != 0 || 727c478bd9Sstevel@tonic-gate (uintptr_t)st.ss_sp < st.ss_size) { 737c478bd9Sstevel@tonic-gate return (0); /* unable to get stack bounds */ 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * thr_stksegment(3C) has a slightly different interface than 777c478bd9Sstevel@tonic-gate * stack_getbounds(3C) -- correct it 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size); 807c478bd9Sstevel@tonic-gate st.ss_flags = 0; /* can't be on-stack */ 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate on_altstack = (st.ss_flags & SS_ONSTACK); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate if (st.ss_size != 0) { 857c478bd9Sstevel@tonic-gate base = (uintptr_t)st.ss_sp; 867c478bd9Sstevel@tonic-gate size = st.ss_size; 877c478bd9Sstevel@tonic-gate } else { 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * If size == 0, then ss_sp is the *top* of the stack. 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * Since we only allow increasing frame pointers, and we 927c478bd9Sstevel@tonic-gate * know our caller set his up correctly, we can treat ss_sp 937c478bd9Sstevel@tonic-gate * as an upper bound safely. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate base = 0; 967c478bd9Sstevel@tonic-gate size = (uintptr_t)st.ss_sp; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate if (check_signal != 0) { 1007c478bd9Sstevel@tonic-gate void (*sigfunc)() = NULL; 1017c478bd9Sstevel@tonic-gate int sigfuncsize = 0; 1027c478bd9Sstevel@tonic-gate extern void thr_sighndlrinfo(void (**)(), int *); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate thr_sighndlrinfo(&sigfunc, &sigfuncsize); 1057c478bd9Sstevel@tonic-gate sigbase = (uintptr_t)sigfunc; 1067c478bd9Sstevel@tonic-gate sigsize = sigfuncsize; 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate #else /* UMEM_STANDALONE */ 1097c478bd9Sstevel@tonic-gate base = (uintptr_t)umem_min_stack; 1107c478bd9Sstevel@tonic-gate size = umem_max_stack - umem_min_stack; 1117c478bd9Sstevel@tonic-gate #endif 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 1147c478bd9Sstevel@tonic-gate * shorten size so that fr_savfp and fr_savpc will be within the stack 1157c478bd9Sstevel@tonic-gate * bounds. 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate if (size >= UMEM_FRAMESIZE - 1) 1187c478bd9Sstevel@tonic-gate size -= (UMEM_FRAMESIZE - 1); 1197c478bd9Sstevel@tonic-gate else 1207c478bd9Sstevel@tonic-gate size = 0; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9) 1237c478bd9Sstevel@tonic-gate flush_windows(); 1247c478bd9Sstevel@tonic-gate #endif 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* LINTED alignment */ 1277c478bd9Sstevel@tonic-gate fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate minfp = fp; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if (((uintptr_t)fp - base) >= size) 1327c478bd9Sstevel@tonic-gate return (0); /* the frame pointer isn't in our stack */ 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate while (depth < pcstack_limit) { 1357c478bd9Sstevel@tonic-gate uintptr_t tmp; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* LINTED alignment */ 1387c478bd9Sstevel@tonic-gate nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS); 1397c478bd9Sstevel@tonic-gate tmp = (uintptr_t)nextfp; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Check nextfp for validity. It must be properly aligned, 1437c478bd9Sstevel@tonic-gate * increasing compared to the last %fp (or the top of the 1447c478bd9Sstevel@tonic-gate * stack we just switched to), and it must be inside 1457c478bd9Sstevel@tonic-gate * [base, base + size). 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate if (tmp != SA(tmp)) 1487c478bd9Sstevel@tonic-gate break; 1497c478bd9Sstevel@tonic-gate else if (nextfp <= minfp || (tmp - base) >= size) { 1507c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 1517c478bd9Sstevel@tonic-gate if (tmp == NULL || !on_altstack) 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * If we're on an alternate signal stack, try jumping 1557c478bd9Sstevel@tonic-gate * to the main thread stack. 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * If the main thread stack has an unlimited size, we 1587c478bd9Sstevel@tonic-gate * punt, since we don't know where the frame pointer's 1597c478bd9Sstevel@tonic-gate * been. 1607c478bd9Sstevel@tonic-gate * 1617c478bd9Sstevel@tonic-gate * (thr_stksegment() returns the *top of stack* 1627c478bd9Sstevel@tonic-gate * in ss_sp, not the bottom) 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate if (thr_stksegment(&st) == 0) { 1657c478bd9Sstevel@tonic-gate if (st.ss_size >= (uintptr_t)st.ss_sp || 1667c478bd9Sstevel@tonic-gate st.ss_size < UMEM_FRAMESIZE - 1) 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate on_altstack = 0; 1707c478bd9Sstevel@tonic-gate base = (uintptr_t)st.ss_sp - st.ss_size; 1717c478bd9Sstevel@tonic-gate size = st.ss_size - (UMEM_FRAMESIZE - 1); 1727c478bd9Sstevel@tonic-gate minfp = (struct frame *)base; 1737c478bd9Sstevel@tonic-gate continue; /* try again */ 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate #endif 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 1807c478bd9Sstevel@tonic-gate if (check_signal && (fp->fr_savpc - sigbase) <= sigsize) 1817c478bd9Sstevel@tonic-gate umem_panic("called from signal handler"); 1827c478bd9Sstevel@tonic-gate #endif 1837c478bd9Sstevel@tonic-gate pcstack[depth++] = fp->fr_savpc; 1847c478bd9Sstevel@tonic-gate fp = nextfp; 1857c478bd9Sstevel@tonic-gate minfp = fp; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate return (depth); 1887c478bd9Sstevel@tonic-gate } 189