xref: /illumos-gate/usr/src/lib/libc/port/gen/stack.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "thr_uberdata.h"
28 #include <sys/stack.h>
29 
30 /*
31  * Initialization of the main stack is performed in libc_init().
32  * Initialization of thread stacks is performed in _thrp_setup().
33  */
34 
35 #pragma weak _stack_getbounds = stack_getbounds
36 int
37 stack_getbounds(stack_t *sp)
38 {
39 	*sp = curthread->ul_ustack;
40 	return (0);
41 }
42 
43 #pragma weak _stack_setbounds = stack_setbounds
44 int
45 stack_setbounds(const stack_t *sp)
46 {
47 	ulwp_t *self = curthread;
48 
49 	if (sp == NULL || sp->ss_sp == NULL ||
50 	    (uintptr_t)sp->ss_sp != SA((uintptr_t)sp->ss_sp) ||
51 	    sp->ss_flags != 0 || sp->ss_size < MINSIGSTKSZ ||
52 	    (uintptr_t)sp->ss_size != SA((uintptr_t)sp->ss_size)) {
53 		errno = EINVAL;
54 		return (-1);
55 	}
56 
57 	sigoff(self);
58 	self->ul_ustack = *sp;
59 	sigon(self);
60 
61 	return (0);
62 }
63 
64 /*
65  * Returns a boolean value:
66  *	1 addr is within the bounds of the current stack
67  *	0 addr is outside of the bounds of the current stack
68  * Note that addr is an unbiased value.
69  */
70 #pragma weak _stack_inbounds = stack_inbounds
71 int
72 stack_inbounds(void *addr)
73 {
74 	stack_t *ustackp = &curthread->ul_ustack;
75 	uintptr_t base = (uintptr_t)ustackp->ss_sp;
76 	size_t size = ustackp->ss_size;
77 
78 	return ((uintptr_t)addr >= base && (uintptr_t)addr < base + size);
79 }
80 
81 #pragma weak _stack_violation = stack_violation
82 int
83 stack_violation(int sig, const siginfo_t *sip, const ucontext_t *ucp)
84 {
85 	uintptr_t addr;
86 	uintptr_t base;
87 	size_t size;
88 
89 	if ((sig != SIGSEGV && sig != SIGBUS) ||
90 	    sip == NULL || ucp == NULL || SI_FROMUSER(sip))
91 		return (0);
92 
93 	/*
94 	 * ucp has the correct view of the stack when the signal was raised.
95 	 */
96 	base = (uintptr_t)ucp->uc_stack.ss_sp;
97 	size = ucp->uc_stack.ss_size;
98 #if defined(__sparc)
99 	addr = ucp->uc_mcontext.gregs[REG_SP] + STACK_BIAS;
100 #elif defined(__amd64) || defined(__i386)
101 	addr = ucp->uc_mcontext.gregs[REG_SP];
102 	/*
103 	 * If the faulted address is just below the stack pointer we
104 	 * might be looking at a push instruction that caused the fault
105 	 * (the largest amount a push instruction can decrement the
106 	 * stack pointer by is 32).  In that case, use the faulted
107 	 * address in our computation rather than the stack pointer.
108 	 */
109 	if (addr - (uintptr_t)sip->si_addr < 32)
110 		addr = (uintptr_t)sip->si_addr;
111 #else
112 #error "none of __sparc, __amd64, __i386 is defined"
113 #endif
114 	return (!(addr >= base && addr < base + size));
115 }
116