xref: /freebsd/lib/libthr/thread/thr_create.c (revision 6b96e7a5731795e76fe33df5a23edfb136f2e508)
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 #define _WANT_P_OSREL
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/rtprio.h>
34 #include <sys/signalvar.h>
35 #include <sys/exterrvar.h>
36 #include <errno.h>
37 #include <link.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stddef.h>
41 #include <pthread.h>
42 #include <pthread_np.h>
43 #include "un-namespace.h"
44 
45 #include "libc_private.h"
46 #include "thr_private.h"
47 
48 int __getosreldate(void);
49 
50 static int  create_stack(struct pthread_attr *pattr);
51 static void thread_start(struct pthread *curthread);
52 
53 int __thr_new_flags = THR_C_RUNTIME;
54 
55 __weak_reference(_pthread_create, pthread_create);
56 
57 int
_pthread_create(pthread_t * __restrict thread,const pthread_attr_t * __restrict attr,void * (* start_routine)(void *),void * __restrict arg)58 _pthread_create(pthread_t * __restrict thread,
59     const pthread_attr_t * __restrict attr, void *(*start_routine) (void *),
60     void * __restrict arg)
61 {
62 	struct pthread *curthread, *new_thread;
63 	struct thr_param param;
64 	struct sched_param sched_param;
65 	struct rtprio rtp;
66 	sigset_t set, oset;
67 	cpuset_t *cpusetp;
68 	int i, cpusetsize, create_suspended, locked, old_stack_prot, ret;
69 
70 	cpusetp = NULL;
71 	ret = cpusetsize = 0;
72 	_thr_check_init();
73 
74 	/*
75 	 * Tell libc and others now they need lock to protect their data.
76 	 */
77 	if (_thr_isthreaded() == 0) {
78 		_malloc_first_thread();
79 		_thr_setthreaded(1);
80 	}
81 
82 	curthread = _get_curthread();
83 	if ((new_thread = _thr_alloc(curthread)) == NULL)
84 		return (EAGAIN);
85 
86 	memset(&param, 0, sizeof(param));
87 
88 	if (attr == NULL || *attr == NULL)
89 		/* Use the default thread attributes: */
90 		new_thread->attr = _pthread_attr_default;
91 	else {
92 		new_thread->attr = *(*attr);
93 		cpusetp = new_thread->attr.cpuset;
94 		cpusetsize = new_thread->attr.cpusetsize;
95 		new_thread->attr.cpuset = NULL;
96 		new_thread->attr.cpusetsize = 0;
97 	}
98 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
99 		/* inherit scheduling contention scope */
100 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
101 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
102 		else
103 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
104 
105 		new_thread->attr.prio = curthread->attr.prio;
106 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
107 	}
108 
109 	new_thread->tid = TID_TERMINATED;
110 
111 	old_stack_prot = _rtld_get_stack_prot();
112 	if (create_stack(&new_thread->attr) != 0) {
113 		/* Insufficient memory to create a stack: */
114 		_thr_free(curthread, new_thread);
115 		return (EAGAIN);
116 	}
117 	/*
118 	 * Write a magic value to the thread structure
119 	 * to help identify valid ones:
120 	 */
121 	new_thread->magic = THR_MAGIC;
122 	new_thread->start_routine = start_routine;
123 	new_thread->arg = arg;
124 	new_thread->cancel_enable = 1;
125 	new_thread->cancel_async = 0;
126 	/* Initialize the mutex queue: */
127 	for (i = 0; i < TMQ_NITEMS; i++)
128 		TAILQ_INIT(&new_thread->mq[i]);
129 
130 	/* Initialise hooks in the thread structure: */
131 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
132 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
133 		create_suspended = 1;
134 	} else {
135 		create_suspended = 0;
136 	}
137 
138 	new_thread->state = PS_RUNNING;
139 
140 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
141 		new_thread->flags |= THR_FLAGS_DETACHED;
142 
143 	/* Add the new thread. */
144 	new_thread->refcount = 1;
145 	_thr_link(curthread, new_thread);
146 
147 	/*
148 	 * Handle the race between __pthread_map_stacks_exec and
149 	 * thread linkage.
150 	 */
151 	if (old_stack_prot != _rtld_get_stack_prot())
152 		_thr_stack_fix_protection(new_thread);
153 
154 	/* Return thread pointer eariler so that new thread can use it. */
155 	*thread = new_thread;
156 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE) || cpusetp != NULL) {
157 		THR_THREAD_LOCK(curthread, new_thread);
158 		locked = 1;
159 	} else
160 		locked = 0;
161 	param.start_func = (void (*)(void *)) thread_start;
162 	param.arg = new_thread;
163 	param.stack_base = new_thread->attr.stackaddr_attr;
164 	param.stack_size = new_thread->attr.stacksize_attr;
165 	param.tls_base = (char *)new_thread->tcb;
166 	param.tls_size = sizeof(struct tcb);
167 	param.child_tid = &new_thread->tid;
168 	param.parent_tid = &new_thread->tid;
169 	param.flags = __thr_new_flags;
170 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
171 		param.flags |= THR_SYSTEM_SCOPE;
172 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
173 		param.rtp = NULL;
174 	else {
175 		sched_param.sched_priority = new_thread->attr.prio;
176 		_schedparam_to_rtp(new_thread->attr.sched_policy,
177 			&sched_param, &rtp);
178 		param.rtp = &rtp;
179 	}
180 
181 	/* Schedule the new thread. */
182 	if (create_suspended) {
183 		SIGFILLSET(set);
184 		SIGDELSET(set, SIGTRAP);
185 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
186 		new_thread->sigmask = oset;
187 		SIGDELSET(new_thread->sigmask, SIGCANCEL);
188 	}
189 
190 	ret = thr_new(&param, sizeof(param));
191 
192 	if (ret != 0) {
193 		ret = errno;
194 		/*
195 		 * Translate EPROCLIM into well-known POSIX code EAGAIN.
196 		 */
197 		if (ret == EPROCLIM)
198 			ret = EAGAIN;
199 	}
200 
201 	if (create_suspended)
202 		__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
203 
204 	if (ret != 0) {
205 		if (!locked)
206 			THR_THREAD_LOCK(curthread, new_thread);
207 		new_thread->state = PS_DEAD;
208 		new_thread->tid = TID_TERMINATED;
209 		new_thread->flags |= THR_FLAGS_DETACHED;
210 		new_thread->refcount--;
211 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
212 			new_thread->cycle++;
213 			_thr_umtx_wake(&new_thread->cycle, INT_MAX, 0);
214 		}
215 		_thr_try_gc(curthread, new_thread); /* thread lock released */
216 		atomic_add_int(&_thread_active_threads, -1);
217 	} else if (locked) {
218 		if (cpusetp != NULL) {
219 			if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
220 				TID(new_thread), cpusetsize, cpusetp)) {
221 				ret = errno;
222 				/* kill the new thread */
223 				new_thread->force_exit = 1;
224 				new_thread->flags |= THR_FLAGS_DETACHED;
225 				_thr_try_gc(curthread, new_thread);
226 				 /* thread lock released */
227 				goto out;
228 			}
229 		}
230 
231 		_thr_report_creation(curthread, new_thread);
232 		THR_THREAD_UNLOCK(curthread, new_thread);
233 	}
234 out:
235 	if (ret != 0)
236 		*thread = NULL;
237 	return (ret);
238 }
239 
240 static int
create_stack(struct pthread_attr * pattr)241 create_stack(struct pthread_attr *pattr)
242 {
243 	int ret;
244 
245 	/* Check if a stack was specified in the thread attributes: */
246 	if ((pattr->stackaddr_attr) != NULL) {
247 		pattr->guardsize_attr = 0;
248 		pattr->flags |= THR_STACK_USER;
249 		ret = 0;
250 	}
251 	else
252 		ret = _thr_stack_alloc(pattr);
253 	return (ret);
254 }
255 
256 static void
thread_start(struct pthread * curthread)257 thread_start(struct pthread *curthread)
258 {
259 	sigset_t set;
260 
261 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED)
262 		set = curthread->sigmask;
263 	_thr_signal_block_setup(curthread);
264 
265 	/*
266 	 * This is used as a serialization point to allow parent
267 	 * to report 'new thread' event to debugger or tweak new thread's
268 	 * attributes before the new thread does real-world work.
269 	 */
270 	THR_LOCK(curthread);
271 	THR_UNLOCK(curthread);
272 
273 	if (curthread->force_exit)
274 		_pthread_exit(PTHREAD_CANCELED);
275 
276 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
277 #if 0
278 		/* Done in THR_UNLOCK() */
279 		_thr_ast(curthread);
280 #endif
281 
282 		/*
283 		 * Parent thread have stored signal mask for us,
284 		 * we should restore it now.
285 		 */
286 		__sys_sigprocmask(SIG_SETMASK, &set, NULL);
287 	}
288 
289 #ifdef _PTHREAD_FORCED_UNWIND
290 	curthread->unwind_stackend = (char *)curthread->attr.stackaddr_attr +
291 		curthread->attr.stacksize_attr;
292 #endif
293 
294 	curthread->uexterr.ver = UEXTERROR_VER;
295 	if (__getosreldate() >= P_OSREL_EXTERRCTL)
296 		exterrctl(EXTERRCTL_ENABLE, 0, &curthread->uexterr);
297 
298 	/* Run the current thread's start routine with argument: */
299 	_pthread_exit(curthread->start_routine(curthread->arg));
300 
301 	/* This point should never be reached. */
302 	PANIC("Thread has resumed after exit");
303 }
304