1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang  * CDDL HEADER START
3*22ce4affSfengbojiang  *
4*22ce4affSfengbojiang  * The contents of this file are subject to the terms of the
5*22ce4affSfengbojiang  * Common Development and Distribution License (the "License").
6*22ce4affSfengbojiang  * You may not use this file except in compliance with the License.
7*22ce4affSfengbojiang  *
8*22ce4affSfengbojiang  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*22ce4affSfengbojiang  * or http://www.opensolaris.org/os/licensing.
10*22ce4affSfengbojiang  * See the License for the specific language governing permissions
11*22ce4affSfengbojiang  * and limitations under the License.
12*22ce4affSfengbojiang  *
13*22ce4affSfengbojiang  * When distributing Covered Code, include this CDDL HEADER in each
14*22ce4affSfengbojiang  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*22ce4affSfengbojiang  * If applicable, add the following below this CDDL HEADER, with the
16*22ce4affSfengbojiang  * fields enclosed by brackets "[]" replaced with your own identifying
17*22ce4affSfengbojiang  * information: Portions Copyright [yyyy] [name of copyright owner]
18*22ce4affSfengbojiang  *
19*22ce4affSfengbojiang  * CDDL HEADER END
20*22ce4affSfengbojiang  */
21*22ce4affSfengbojiang /*
22*22ce4affSfengbojiang  * This header file distributed under the terms of the CDDL.
23*22ce4affSfengbojiang  * Portions Copyright 2008 Sun Microsystems, Inc. All Rights reserved.
24*22ce4affSfengbojiang  */
25*22ce4affSfengbojiang #ifndef _SYS_STACK_H
26*22ce4affSfengbojiang #define	_SYS_STACK_H
27*22ce4affSfengbojiang 
28*22ce4affSfengbojiang #include <pthread.h>
29*22ce4affSfengbojiang 
30*22ce4affSfengbojiang #define	STACK_BIAS	0
31*22ce4affSfengbojiang 
32*22ce4affSfengbojiang #ifdef __USE_GNU
33*22ce4affSfengbojiang 
34*22ce4affSfengbojiang static inline int
stack_getbounds(stack_t * sp)35*22ce4affSfengbojiang stack_getbounds(stack_t *sp)
36*22ce4affSfengbojiang {
37*22ce4affSfengbojiang 	pthread_attr_t attr;
38*22ce4affSfengbojiang 	int rc;
39*22ce4affSfengbojiang 
40*22ce4affSfengbojiang 	rc = pthread_getattr_np(pthread_self(), &attr);
41*22ce4affSfengbojiang 	if (rc)
42*22ce4affSfengbojiang 		return (rc);
43*22ce4affSfengbojiang 
44*22ce4affSfengbojiang 	rc = pthread_attr_getstack(&attr, &sp->ss_sp, &sp->ss_size);
45*22ce4affSfengbojiang 	if (rc == 0)
46*22ce4affSfengbojiang 		sp->ss_flags = 0;
47*22ce4affSfengbojiang 
48*22ce4affSfengbojiang 	pthread_attr_destroy(&attr);
49*22ce4affSfengbojiang 
50*22ce4affSfengbojiang 	return (rc);
51*22ce4affSfengbojiang }
52*22ce4affSfengbojiang 
53*22ce4affSfengbojiang static inline int
thr_stksegment(stack_t * sp)54*22ce4affSfengbojiang thr_stksegment(stack_t *sp)
55*22ce4affSfengbojiang {
56*22ce4affSfengbojiang 	int rc;
57*22ce4affSfengbojiang 
58*22ce4affSfengbojiang 	rc = stack_getbounds(sp);
59*22ce4affSfengbojiang 	if (rc)
60*22ce4affSfengbojiang 		return (rc);
61*22ce4affSfengbojiang 
62*22ce4affSfengbojiang 	/*
63*22ce4affSfengbojiang 	 * thr_stksegment() is expected to set sp.ss_sp to the high stack
64*22ce4affSfengbojiang 	 * address, but the stack_getbounds() interface is expected to
65*22ce4affSfengbojiang 	 * set sp.ss_sp to the low address.  Adjust accordingly.
66*22ce4affSfengbojiang 	 */
67*22ce4affSfengbojiang 	sp->ss_sp = (void *)(((uintptr_t)sp->ss_sp) + sp->ss_size);
68*22ce4affSfengbojiang 	sp->ss_flags = 0;
69*22ce4affSfengbojiang 
70*22ce4affSfengbojiang 	return (rc);
71*22ce4affSfengbojiang }
72*22ce4affSfengbojiang 
73*22ce4affSfengbojiang #endif /* __USE_GNU */
74*22ce4affSfengbojiang #endif /* _SYS_STACK_H */
75