xref: /onnv-gate/usr/src/lib/libc/port/gen/stack.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "thr_uberdata.h"
30*0Sstevel@tonic-gate #include <sys/stack.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * Initialization of the main stack is performed in libc_init().
34*0Sstevel@tonic-gate  * Initialization of thread stacks is performed in _thr_setup().
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #pragma weak stack_getbounds = _stack_getbounds
38*0Sstevel@tonic-gate int
39*0Sstevel@tonic-gate _stack_getbounds(stack_t *sp)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	*sp = curthread->ul_ustack;
42*0Sstevel@tonic-gate 	return (0);
43*0Sstevel@tonic-gate }
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #pragma weak stack_setbounds = _stack_setbounds
46*0Sstevel@tonic-gate int
47*0Sstevel@tonic-gate _stack_setbounds(const stack_t *sp)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	ulwp_t *self = curthread;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	if (sp == NULL || sp->ss_sp == NULL ||
52*0Sstevel@tonic-gate 	    (uintptr_t)sp->ss_sp != SA((uintptr_t)sp->ss_sp) ||
53*0Sstevel@tonic-gate 	    sp->ss_flags != 0 || sp->ss_size < MINSIGSTKSZ ||
54*0Sstevel@tonic-gate 	    (uintptr_t)sp->ss_size != SA((uintptr_t)sp->ss_size)) {
55*0Sstevel@tonic-gate 		errno = EINVAL;
56*0Sstevel@tonic-gate 		return (-1);
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	sigoff(self);
60*0Sstevel@tonic-gate 	self->ul_ustack = *sp;
61*0Sstevel@tonic-gate 	sigon(self);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	return (0);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * Returns a boolean value:
68*0Sstevel@tonic-gate  *	1 addr is within the bounds of the current stack
69*0Sstevel@tonic-gate  *	0 addr is outside of the bounds of the current stack
70*0Sstevel@tonic-gate  * Note that addr is an unbiased value.
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate #pragma weak stack_inbounds = _stack_inbounds
73*0Sstevel@tonic-gate int
74*0Sstevel@tonic-gate _stack_inbounds(void *addr)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	stack_t *ustackp = &curthread->ul_ustack;
77*0Sstevel@tonic-gate 	uintptr_t base = (uintptr_t)ustackp->ss_sp;
78*0Sstevel@tonic-gate 	size_t size = ustackp->ss_size;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	return ((uintptr_t)addr >= base && (uintptr_t)addr < base + size);
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #pragma weak stack_violation = _stack_violation
84*0Sstevel@tonic-gate int
85*0Sstevel@tonic-gate _stack_violation(int sig, const siginfo_t *sip, const ucontext_t *ucp)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	uintptr_t addr;
88*0Sstevel@tonic-gate 	uintptr_t base;
89*0Sstevel@tonic-gate 	size_t size;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if ((sig != SIGSEGV && sig != SIGBUS) ||
92*0Sstevel@tonic-gate 	    sip == NULL || ucp == NULL || SI_FROMUSER(sip))
93*0Sstevel@tonic-gate 		return (0);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	/*
96*0Sstevel@tonic-gate 	 * ucp has the correct view of the stack when the signal was raised.
97*0Sstevel@tonic-gate 	 */
98*0Sstevel@tonic-gate 	base = (uintptr_t)ucp->uc_stack.ss_sp;
99*0Sstevel@tonic-gate 	size = ucp->uc_stack.ss_size;
100*0Sstevel@tonic-gate #if defined(__sparc)
101*0Sstevel@tonic-gate 	addr = ucp->uc_mcontext.gregs[REG_SP] + STACK_BIAS;
102*0Sstevel@tonic-gate #elif defined(__amd64) || defined(__i386)
103*0Sstevel@tonic-gate 	addr = ucp->uc_mcontext.gregs[REG_SP];
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * If the faulted address is just below the stack pointer we
106*0Sstevel@tonic-gate 	 * might be looking at a push instruction that caused the fault
107*0Sstevel@tonic-gate 	 * (the largest amount a push instruction can decrement the
108*0Sstevel@tonic-gate 	 * stack pointer by is 32).  In that case, use the faulted
109*0Sstevel@tonic-gate 	 * address in our computation rather than the stack pointer.
110*0Sstevel@tonic-gate 	 */
111*0Sstevel@tonic-gate 	if (addr - (uintptr_t)sip->si_addr < 32)
112*0Sstevel@tonic-gate 		addr = (uintptr_t)sip->si_addr;
113*0Sstevel@tonic-gate #else
114*0Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 is defined"
115*0Sstevel@tonic-gate #endif
116*0Sstevel@tonic-gate 	return (!(addr >= base && addr < base + size));
117*0Sstevel@tonic-gate }
118