1 /* $OpenBSD: uthread_stackseg_np.c,v 1.4 2004/05/01 22:15:10 marc Exp $ */
2 
3 /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
4 
5 #include <sys/param.h>
6 #include <sys/lock.h>
7 #include <sys/queue.h>
8 
9 #include <errno.h>
10 #include <pthread.h>
11 #include <pthread_np.h>
12 #include <unistd.h>
13 
14 #include <uvm/uvm_extern.h>
15 
16 #include "pthread_private.h"
17 
18 /*
19  * Return stack info from the given thread.  Based upon the solaris
20  * thr_stksegment function.
21  */
22 
23 int
pthread_stackseg_np(pthread_t thread,stack_t * sinfo)24 pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
25 {
26 	char *base;
27 	size_t pgsz;
28 	int ret;
29 
30 	if (thread->stack) {
31 		base = thread->stack->base;
32 #if !defined(MACHINE_STACK_GROWS_UP)
33 		base += thread->stack->size;
34 #endif
35 		sinfo->ss_sp = base;
36 		sinfo->ss_size = thread->stack->size;
37 		sinfo->ss_flags = 0;
38 		ret = 0;
39 	} else if (thread == _thread_initial) {
40 		pgsz = sysconf(_SC_PAGESIZE);
41 		if (pgsz == (size_t)-1)
42 			ret = EAGAIN;
43 		else {
44 #if defined(MACHINE_STACK_GROWS_UP)
45 			base = (caddr_t) USRSTACK;
46 #else
47 			base = (caddr_t) ((USRSTACK - DFLSSIZ) & ~(pgsz - 1));
48 			base += DFLSSIZ;
49 #endif
50 			sinfo->ss_sp = base;
51 			sinfo->ss_size = DFLSSIZ;
52 			sinfo->ss_flags = 0;
53 			ret = 0;
54 		}
55 
56 	} else
57 		ret = EAGAIN;
58 
59 	return ret;
60 }
61