xref: /openbsd/lib/librthread/rthread_np.c (revision a6445c1d)
1 /*	$OpenBSD: rthread_np.c,v 1.14 2014/08/09 03:29:35 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 #include <sys/sysctl.h>
26 
27 #include <stdint.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include <pthread_np.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <machine/spinlock.h>
36 
37 #include "rthread.h"
38 
39 void
40 pthread_set_name_np(pthread_t thread, const char *name)
41 {
42 	strlcpy(thread->name, name, sizeof(thread->name));
43 }
44 
45 int
46 pthread_main_np(void)
47 {
48 	return (!_threads_ready || (pthread_self()->flags & THREAD_ORIGINAL)
49 	    ? 1 : 0);
50 }
51 
52 
53 /*
54  * Return stack info from the given thread.  Based upon the solaris
55  * thr_stksegment function.  Note that the returned ss_sp member is the
56  * *top* of the allocated stack area, unlike in sigaltstack() where
57  * it's the bottom.  You'll have to ask Sun what they were thinking...
58  *
59  * This function taken from the uthread library, with the following
60  * license:
61  * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
62 int
63 pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
64 {
65 	if (thread->stack) {
66 #ifdef MACHINE_STACK_GROWS_UP
67 		sinfo->ss_sp = thread->stack->base;
68 #else
69 		sinfo->ss_sp = thread->stack->base + thread->stack->len;
70 #endif
71 		sinfo->ss_size = thread->stack->len;
72 		if (thread->stack->guardsize != 1)
73 			sinfo->ss_size -= thread->stack->guardsize;
74 		sinfo->ss_flags = 0;
75 		return (0);
76 	} else if (thread->flags & THREAD_INITIAL_STACK) {
77 		static struct _ps_strings _ps;
78 		static struct rlimit rl;
79 		static int gotself;
80 
81 		if (gotself == 0) {
82 			int mib[2];
83 			size_t len;
84 
85 			if (getrlimit(RLIMIT_STACK, &rl) != 0)
86 				return (EAGAIN);
87 
88 			mib[0] = CTL_VM;
89 			mib[1] = VM_PSSTRINGS;
90 			len = sizeof(_ps);
91 			if (sysctl(mib, 2, &_ps, &len, NULL, 0) != 0)
92 				return (EAGAIN);
93 			gotself = 1;
94 		}
95 
96 		/*
97 		 * Provides a rough estimation of stack bounds.   Caller
98 		 * likely wants to know for the purpose of inspecting call
99 		 * frames, but VM_PSSTRINGS points to process arguments...
100 		 */
101 #ifdef MACHINE_STACK_GROWS_UP
102 		sinfo->ss_sp = _ps.val;
103 #else
104 		sinfo->ss_sp = (void *)ROUND_TO_PAGE((uintptr_t)_ps.val);
105 #endif
106 		sinfo->ss_size = (size_t)rl.rlim_cur;
107 		sinfo->ss_flags = 0;
108 		return (0);
109 	}
110 	return (EAGAIN);
111 }
112