xref: /freebsd/lib/libthr/thread/thr_create.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
5  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/rtprio.h>
33 #include <sys/signalvar.h>
34 #include <errno.h>
35 #include <link.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stddef.h>
39 #include <pthread.h>
40 #include <pthread_np.h>
41 #include "un-namespace.h"
42 
43 #include "libc_private.h"
44 #include "thr_private.h"
45 
46 static int  create_stack(struct pthread_attr *pattr);
47 static void thread_start(struct pthread *curthread);
48 
49 __weak_reference(_pthread_create, pthread_create);
50 
51 int
_pthread_create(pthread_t * __restrict thread,const pthread_attr_t * __restrict attr,void * (* start_routine)(void *),void * __restrict arg)52 _pthread_create(pthread_t * __restrict thread,
53     const pthread_attr_t * __restrict attr, void *(*start_routine) (void *),
54     void * __restrict arg)
55 {
56 	struct pthread *curthread, *new_thread;
57 	struct thr_param param;
58 	struct sched_param sched_param;
59 	struct rtprio rtp;
60 	sigset_t set, oset;
61 	cpuset_t *cpusetp;
62 	int i, cpusetsize, create_suspended, locked, old_stack_prot, ret;
63 
64 	cpusetp = NULL;
65 	ret = cpusetsize = 0;
66 	_thr_check_init();
67 
68 	/*
69 	 * Tell libc and others now they need lock to protect their data.
70 	 */
71 	if (_thr_isthreaded() == 0) {
72 		_malloc_first_thread();
73 		_thr_setthreaded(1);
74 	}
75 
76 	curthread = _get_curthread();
77 	if ((new_thread = _thr_alloc(curthread)) == NULL)
78 		return (EAGAIN);
79 
80 	memset(&param, 0, sizeof(param));
81 
82 	if (attr == NULL || *attr == NULL)
83 		/* Use the default thread attributes: */
84 		new_thread->attr = _pthread_attr_default;
85 	else {
86 		new_thread->attr = *(*attr);
87 		cpusetp = new_thread->attr.cpuset;
88 		cpusetsize = new_thread->attr.cpusetsize;
89 		new_thread->attr.cpuset = NULL;
90 		new_thread->attr.cpusetsize = 0;
91 	}
92 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
93 		/* inherit scheduling contention scope */
94 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
95 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
96 		else
97 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
98 
99 		new_thread->attr.prio = curthread->attr.prio;
100 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
101 	}
102 
103 	new_thread->tid = TID_TERMINATED;
104 
105 	old_stack_prot = _rtld_get_stack_prot();
106 	if (create_stack(&new_thread->attr) != 0) {
107 		/* Insufficient memory to create a stack: */
108 		_thr_free(curthread, new_thread);
109 		return (EAGAIN);
110 	}
111 	/*
112 	 * Write a magic value to the thread structure
113 	 * to help identify valid ones:
114 	 */
115 	new_thread->magic = THR_MAGIC;
116 	new_thread->start_routine = start_routine;
117 	new_thread->arg = arg;
118 	new_thread->cancel_enable = 1;
119 	new_thread->cancel_async = 0;
120 	/* Initialize the mutex queue: */
121 	for (i = 0; i < TMQ_NITEMS; i++)
122 		TAILQ_INIT(&new_thread->mq[i]);
123 
124 	/* Initialise hooks in the thread structure: */
125 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
126 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
127 		create_suspended = 1;
128 	} else {
129 		create_suspended = 0;
130 	}
131 
132 	new_thread->state = PS_RUNNING;
133 
134 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
135 		new_thread->flags |= THR_FLAGS_DETACHED;
136 
137 	/* Add the new thread. */
138 	new_thread->refcount = 1;
139 	_thr_link(curthread, new_thread);
140 
141 	/*
142 	 * Handle the race between __pthread_map_stacks_exec and
143 	 * thread linkage.
144 	 */
145 	if (old_stack_prot != _rtld_get_stack_prot())
146 		_thr_stack_fix_protection(new_thread);
147 
148 	/* Return thread pointer eariler so that new thread can use it. */
149 	(*thread) = new_thread;
150 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE) || cpusetp != NULL) {
151 		THR_THREAD_LOCK(curthread, new_thread);
152 		locked = 1;
153 	} else
154 		locked = 0;
155 	param.start_func = (void (*)(void *)) thread_start;
156 	param.arg = new_thread;
157 	param.stack_base = new_thread->attr.stackaddr_attr;
158 	param.stack_size = new_thread->attr.stacksize_attr;
159 	param.tls_base = (char *)new_thread->tcb;
160 	param.tls_size = sizeof(struct tcb);
161 	param.child_tid = &new_thread->tid;
162 	param.parent_tid = &new_thread->tid;
163 	param.flags = 0;
164 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
165 		param.flags |= THR_SYSTEM_SCOPE;
166 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
167 		param.rtp = NULL;
168 	else {
169 		sched_param.sched_priority = new_thread->attr.prio;
170 		_schedparam_to_rtp(new_thread->attr.sched_policy,
171 			&sched_param, &rtp);
172 		param.rtp = &rtp;
173 	}
174 
175 	/* Schedule the new thread. */
176 	if (create_suspended) {
177 		SIGFILLSET(set);
178 		SIGDELSET(set, SIGTRAP);
179 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
180 		new_thread->sigmask = oset;
181 		SIGDELSET(new_thread->sigmask, SIGCANCEL);
182 	}
183 
184 	ret = thr_new(&param, sizeof(param));
185 
186 	if (ret != 0) {
187 		ret = errno;
188 		/*
189 		 * Translate EPROCLIM into well-known POSIX code EAGAIN.
190 		 */
191 		if (ret == EPROCLIM)
192 			ret = EAGAIN;
193 	}
194 
195 	if (create_suspended)
196 		__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
197 
198 	if (ret != 0) {
199 		if (!locked)
200 			THR_THREAD_LOCK(curthread, new_thread);
201 		new_thread->state = PS_DEAD;
202 		new_thread->tid = TID_TERMINATED;
203 		new_thread->flags |= THR_FLAGS_DETACHED;
204 		new_thread->refcount--;
205 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
206 			new_thread->cycle++;
207 			_thr_umtx_wake(&new_thread->cycle, INT_MAX, 0);
208 		}
209 		_thr_try_gc(curthread, new_thread); /* thread lock released */
210 		atomic_add_int(&_thread_active_threads, -1);
211 	} else if (locked) {
212 		if (cpusetp != NULL) {
213 			if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
214 				TID(new_thread), cpusetsize, cpusetp)) {
215 				ret = errno;
216 				/* kill the new thread */
217 				new_thread->force_exit = 1;
218 				new_thread->flags |= THR_FLAGS_DETACHED;
219 				_thr_try_gc(curthread, new_thread);
220 				 /* thread lock released */
221 				goto out;
222 			}
223 		}
224 
225 		_thr_report_creation(curthread, new_thread);
226 		THR_THREAD_UNLOCK(curthread, new_thread);
227 	}
228 out:
229 	if (ret)
230 		(*thread) = 0;
231 	return (ret);
232 }
233 
234 static int
create_stack(struct pthread_attr * pattr)235 create_stack(struct pthread_attr *pattr)
236 {
237 	int ret;
238 
239 	/* Check if a stack was specified in the thread attributes: */
240 	if ((pattr->stackaddr_attr) != NULL) {
241 		pattr->guardsize_attr = 0;
242 		pattr->flags |= THR_STACK_USER;
243 		ret = 0;
244 	}
245 	else
246 		ret = _thr_stack_alloc(pattr);
247 	return (ret);
248 }
249 
250 static void
thread_start(struct pthread * curthread)251 thread_start(struct pthread *curthread)
252 {
253 	sigset_t set;
254 
255 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED)
256 		set = curthread->sigmask;
257 	_thr_signal_block_setup(curthread);
258 
259 	/*
260 	 * This is used as a serialization point to allow parent
261 	 * to report 'new thread' event to debugger or tweak new thread's
262 	 * attributes before the new thread does real-world work.
263 	 */
264 	THR_LOCK(curthread);
265 	THR_UNLOCK(curthread);
266 
267 	if (curthread->force_exit)
268 		_pthread_exit(PTHREAD_CANCELED);
269 
270 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
271 #if 0
272 		/* Done in THR_UNLOCK() */
273 		_thr_ast(curthread);
274 #endif
275 
276 		/*
277 		 * Parent thread have stored signal mask for us,
278 		 * we should restore it now.
279 		 */
280 		__sys_sigprocmask(SIG_SETMASK, &set, NULL);
281 	}
282 
283 #ifdef _PTHREAD_FORCED_UNWIND
284 	curthread->unwind_stackend = (char *)curthread->attr.stackaddr_attr +
285 		curthread->attr.stacksize_attr;
286 #endif
287 
288 	/* Run the current thread's start routine with argument: */
289 	_pthread_exit(curthread->start_routine(curthread->arg));
290 
291 	/* This point should never be reached. */
292 	PANIC("Thread has resumed after exit");
293 }
294