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