xref: /openbsd/lib/librthread/rthread_np.c (revision 898184e3)
1 /*	$OpenBSD: rthread_np.c,v 1.7 2012/02/18 21:12:09 guenther Exp $	*/
2 /*
3  * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
4  * Copyright (c) 2005 Otto Moerbeek <otto@openbsd.org>
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/time.h>
22 #include <sys/lock.h>
23 #include <sys/resource.h>
24 #include <sys/queue.h>
25 
26 #include <errno.h>
27 #include <pthread.h>
28 #include <pthread_np.h>
29 #include <stddef.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include <uvm/uvm_extern.h>
34 #include <machine/spinlock.h>
35 
36 #include "rthread.h"
37 
38 void
39 pthread_set_name_np(pthread_t thread, const char *name)
40 {
41 	strlcpy(thread->name, name, sizeof(thread->name));
42 }
43 
44 int
45 pthread_main_np(void)
46 {
47 	return (!_threads_ready || pthread_self() == &_initial_thread ? 1 : 0);
48 }
49 
50 
51 /*
52  * Return stack info from the given thread.  Based upon the solaris
53  * thr_stksegment function.  Note that the returned ss_sp member is the
54  * *top* of the allocated stack area, unlike in sigaltstack() where
55  * it's the bottom.  You'll have to ask Sun what they were thinking...
56  *
57  * This function taken from the uthread library, with the following
58  * license:
59  * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
60 int
61 pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
62 {
63 	char *base;
64 	int ret;
65 	struct rlimit rl;
66 
67 	if (thread->stack) {
68 		base = thread->stack->base;
69 #if !defined(MACHINE_STACK_GROWS_UP)
70 		base += (ptrdiff_t)thread->stack->len;
71 #endif
72 		sinfo->ss_sp = base;
73 		sinfo->ss_size = thread->stack->len;
74 		sinfo->ss_flags = 0;
75 		ret = 0;
76 	} else if (thread == &_initial_thread) {
77 		if (getrlimit(RLIMIT_STACK, &rl) != 0)
78 			return (EAGAIN);
79 
80 		/*
81 		 * round_page() stack rlim_cur and
82 		 * trunc_page() USRSTACK to be consistent with
83 		 * the way the kernel sets up the stack.
84 		 */
85 		sinfo->ss_size = ROUND_TO_PAGE((size_t)rl.rlim_cur);
86 		sinfo->ss_sp = (caddr_t) (USRSTACK & ~(_thread_pagesize - 1));
87 		sinfo->ss_flags = 0;
88 		ret = 0;
89 
90 	} else
91 		ret = EAGAIN;
92 
93 	return ret;
94 }
95