xref: /freebsd/lib/libthr/thread/thr_create.c (revision 6b96e7a5731795e76fe33df5a23edfb136f2e508)
15e53a4f9SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4a091d823SDavid Xu  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
5df2cf821SDavid Xu  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
6bb535300SJeff Roberson  * All rights reserved.
7bb535300SJeff Roberson  *
8bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
9bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
10bb535300SJeff Roberson  * are met:
11bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12df2cf821SDavid Xu  *    notice unmodified, this list of conditions, and the following
13df2cf821SDavid Xu  *    disclaimer.
14bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
15bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
16bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
17bb535300SJeff Roberson  *
18df2cf821SDavid Xu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19df2cf821SDavid Xu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20df2cf821SDavid Xu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21df2cf821SDavid Xu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22df2cf821SDavid Xu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23df2cf821SDavid Xu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24df2cf821SDavid Xu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25df2cf821SDavid Xu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26df2cf821SDavid Xu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27df2cf821SDavid Xu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28bb535300SJeff Roberson  */
29a091d823SDavid Xu 
30a5b0b2a7SBrooks Davis #define _WANT_P_OSREL
3137a6356bSDavid Xu #include "namespace.h"
327e4cbc3aSStefan Farfeleder #include <sys/types.h>
3370e79fbbSDavid Xu #include <sys/rtprio.h>
347e4cbc3aSStefan Farfeleder #include <sys/signalvar.h>
35b9c8a07dSKonstantin Belousov #include <sys/exterrvar.h>
36bb535300SJeff Roberson #include <errno.h>
37da2fcff7SKonstantin Belousov #include <link.h>
38bb535300SJeff Roberson #include <stdlib.h>
39bb535300SJeff Roberson #include <string.h>
40bb535300SJeff Roberson #include <stddef.h>
41bb535300SJeff Roberson #include <pthread.h>
42a759db94SDavid Xu #include <pthread_np.h>
4337a6356bSDavid Xu #include "un-namespace.h"
44a091d823SDavid Xu 
458495e8b1SKonstantin Belousov #include "libc_private.h"
46bb535300SJeff Roberson #include "thr_private.h"
47bb535300SJeff Roberson 
48a5b0b2a7SBrooks Davis int __getosreldate(void);
49a5b0b2a7SBrooks Davis 
50a091d823SDavid Xu static int  create_stack(struct pthread_attr *pattr);
51a091d823SDavid Xu static void thread_start(struct pthread *curthread);
52bb535300SJeff Roberson 
53*6b96e7a5SKonstantin Belousov int __thr_new_flags = THR_C_RUNTIME;
54*6b96e7a5SKonstantin Belousov 
55bb535300SJeff Roberson __weak_reference(_pthread_create, pthread_create);
56bb535300SJeff Roberson 
57bb535300SJeff Roberson int
_pthread_create(pthread_t * __restrict thread,const pthread_attr_t * __restrict attr,void * (* start_routine)(void *),void * __restrict arg)58b6413b6dSPedro F. Giffuni _pthread_create(pthread_t * __restrict thread,
59b6413b6dSPedro F. Giffuni     const pthread_attr_t * __restrict attr, void *(*start_routine) (void *),
60b6413b6dSPedro F. Giffuni     void * __restrict arg)
61bb535300SJeff Roberson {
62a091d823SDavid Xu 	struct pthread *curthread, *new_thread;
6380c9676eSDavid Xu 	struct thr_param param;
64e6747c7cSDavid Xu 	struct sched_param sched_param;
6570e79fbbSDavid Xu 	struct rtprio rtp;
66bc414752SDavid Xu 	sigset_t set, oset;
671bdbd705SKonstantin Belousov 	cpuset_t *cpusetp;
681bdbd705SKonstantin Belousov 	int i, cpusetsize, create_suspended, locked, old_stack_prot, ret;
69a091d823SDavid Xu 
701bdbd705SKonstantin Belousov 	cpusetp = NULL;
711bdbd705SKonstantin Belousov 	ret = cpusetsize = 0;
72a091d823SDavid Xu 	_thr_check_init();
73bb535300SJeff Roberson 
74bb535300SJeff Roberson 	/*
75a091d823SDavid Xu 	 * Tell libc and others now they need lock to protect their data.
76bb535300SJeff Roberson 	 */
778495e8b1SKonstantin Belousov 	if (_thr_isthreaded() == 0) {
788495e8b1SKonstantin Belousov 		_malloc_first_thread();
79ad8c236bSEric van Gyzen 		_thr_setthreaded(1);
808495e8b1SKonstantin Belousov 	}
81bb535300SJeff Roberson 
82a091d823SDavid Xu 	curthread = _get_curthread();
83a091d823SDavid Xu 	if ((new_thread = _thr_alloc(curthread)) == NULL)
84a091d823SDavid Xu 		return (EAGAIN);
85a091d823SDavid Xu 
8680c9676eSDavid Xu 	memset(&param, 0, sizeof(param));
8780c9676eSDavid Xu 
88bb535300SJeff Roberson 	if (attr == NULL || *attr == NULL)
89a091d823SDavid Xu 		/* Use the default thread attributes: */
90a091d823SDavid Xu 		new_thread->attr = _pthread_attr_default;
91a759db94SDavid Xu 	else {
92a091d823SDavid Xu 		new_thread->attr = *(*attr);
93d0aa4fd3SXin LI 		cpusetp = new_thread->attr.cpuset;
9454dff16bSDavid Xu 		cpusetsize = new_thread->attr.cpusetsize;
95a759db94SDavid Xu 		new_thread->attr.cpuset = NULL;
96a759db94SDavid Xu 		new_thread->attr.cpusetsize = 0;
97a759db94SDavid Xu 	}
98a091d823SDavid Xu 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
99a091d823SDavid Xu 		/* inherit scheduling contention scope */
100a091d823SDavid Xu 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
101a091d823SDavid Xu 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
102a091d823SDavid Xu 		else
103a091d823SDavid Xu 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
104e2dc286cSDavid Xu 
105e2dc286cSDavid Xu 		new_thread->attr.prio = curthread->attr.prio;
106e2dc286cSDavid Xu 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
107a091d823SDavid Xu 	}
108bb535300SJeff Roberson 
109d245d9e1SDavid Xu 	new_thread->tid = TID_TERMINATED;
110d245d9e1SDavid Xu 
111da2fcff7SKonstantin Belousov 	old_stack_prot = _rtld_get_stack_prot();
112a091d823SDavid Xu 	if (create_stack(&new_thread->attr) != 0) {
113a091d823SDavid Xu 		/* Insufficient memory to create a stack: */
114a091d823SDavid Xu 		_thr_free(curthread, new_thread);
115bb535300SJeff Roberson 		return (EAGAIN);
116bb535300SJeff Roberson 	}
117a091d823SDavid Xu 	/*
118a091d823SDavid Xu 	 * Write a magic value to the thread structure
119a091d823SDavid Xu 	 * to help identify valid ones:
120a091d823SDavid Xu 	 */
121a091d823SDavid Xu 	new_thread->magic = THR_MAGIC;
122bb535300SJeff Roberson 	new_thread->start_routine = start_routine;
123bb535300SJeff Roberson 	new_thread->arg = arg;
124f08e1bf6SDavid Xu 	new_thread->cancel_enable = 1;
125f08e1bf6SDavid Xu 	new_thread->cancel_async = 0;
126a091d823SDavid Xu 	/* Initialize the mutex queue: */
1271bdbd705SKonstantin Belousov 	for (i = 0; i < TMQ_NITEMS; i++)
1281bdbd705SKonstantin Belousov 		TAILQ_INIT(&new_thread->mq[i]);
1294393f2c4SMike Makonnen 
130a091d823SDavid Xu 	/* Initialise hooks in the thread structure: */
131bc414752SDavid Xu 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
13288676cbcSDavid Xu 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
133bc414752SDavid Xu 		create_suspended = 1;
134bc414752SDavid Xu 	} else {
135bc414752SDavid Xu 		create_suspended = 0;
136bc414752SDavid Xu 	}
137bc414752SDavid Xu 
138a091d823SDavid Xu 	new_thread->state = PS_RUNNING;
13980c9676eSDavid Xu 
140bc414752SDavid Xu 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
141a9b764e2SDavid Xu 		new_thread->flags |= THR_FLAGS_DETACHED;
142bc414752SDavid Xu 
143a091d823SDavid Xu 	/* Add the new thread. */
144bc414752SDavid Xu 	new_thread->refcount = 1;
145a091d823SDavid Xu 	_thr_link(curthread, new_thread);
146da2fcff7SKonstantin Belousov 
147da2fcff7SKonstantin Belousov 	/*
148da2fcff7SKonstantin Belousov 	 * Handle the race between __pthread_map_stacks_exec and
149da2fcff7SKonstantin Belousov 	 * thread linkage.
150da2fcff7SKonstantin Belousov 	 */
151da2fcff7SKonstantin Belousov 	if (old_stack_prot != _rtld_get_stack_prot())
152da2fcff7SKonstantin Belousov 		_thr_stack_fix_protection(new_thread);
153da2fcff7SKonstantin Belousov 
154a091d823SDavid Xu 	/* Return thread pointer eariler so that new thread can use it. */
15502703de8SKonstantin Belousov 	*thread = new_thread;
156d0aa4fd3SXin LI 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE) || cpusetp != NULL) {
157d245d9e1SDavid Xu 		THR_THREAD_LOCK(curthread, new_thread);
158d245d9e1SDavid Xu 		locked = 1;
159d245d9e1SDavid Xu 	} else
160d245d9e1SDavid Xu 		locked = 0;
16180c9676eSDavid Xu 	param.start_func = (void (*)(void *)) thread_start;
16280c9676eSDavid Xu 	param.arg = new_thread;
16380c9676eSDavid Xu 	param.stack_base = new_thread->attr.stackaddr_attr;
16480c9676eSDavid Xu 	param.stack_size = new_thread->attr.stacksize_attr;
16580c9676eSDavid Xu 	param.tls_base = (char *)new_thread->tcb;
16680c9676eSDavid Xu 	param.tls_size = sizeof(struct tcb);
16780c9676eSDavid Xu 	param.child_tid = &new_thread->tid;
16880c9676eSDavid Xu 	param.parent_tid = &new_thread->tid;
169*6b96e7a5SKonstantin Belousov 	param.flags = __thr_new_flags;
17080c9676eSDavid Xu 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
17180c9676eSDavid Xu 		param.flags |= THR_SYSTEM_SCOPE;
17270e79fbbSDavid Xu 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
173e6747c7cSDavid Xu 		param.rtp = NULL;
17470e79fbbSDavid Xu 	else {
17570e79fbbSDavid Xu 		sched_param.sched_priority = new_thread->attr.prio;
17670e79fbbSDavid Xu 		_schedparam_to_rtp(new_thread->attr.sched_policy,
17770e79fbbSDavid Xu 			&sched_param, &rtp);
17870e79fbbSDavid Xu 		param.rtp = &rtp;
17970e79fbbSDavid Xu 	}
180bc414752SDavid Xu 
181a091d823SDavid Xu 	/* Schedule the new thread. */
182bc414752SDavid Xu 	if (create_suspended) {
183bc414752SDavid Xu 		SIGFILLSET(set);
184bc414752SDavid Xu 		SIGDELSET(set, SIGTRAP);
185bc414752SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
186bc414752SDavid Xu 		new_thread->sigmask = oset;
187a9a11568SDavid Xu 		SIGDELSET(new_thread->sigmask, SIGCANCEL);
188bc414752SDavid Xu 	}
189bc414752SDavid Xu 
19080c9676eSDavid Xu 	ret = thr_new(&param, sizeof(param));
191bc414752SDavid Xu 
1927b4f8f03SDavid Xu 	if (ret != 0) {
1937b4f8f03SDavid Xu 		ret = errno;
1947b4f8f03SDavid Xu 		/*
1957b4f8f03SDavid Xu 		 * Translate EPROCLIM into well-known POSIX code EAGAIN.
1967b4f8f03SDavid Xu 		 */
1977b4f8f03SDavid Xu 		if (ret == EPROCLIM)
1987b4f8f03SDavid Xu 			ret = EAGAIN;
1997b4f8f03SDavid Xu 	}
2007b4f8f03SDavid Xu 
201a9a11568SDavid Xu 	if (create_suspended)
202bc414752SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
203bc414752SDavid Xu 
204a091d823SDavid Xu 	if (ret != 0) {
205bc414752SDavid Xu 		if (!locked)
206bc414752SDavid Xu 			THR_THREAD_LOCK(curthread, new_thread);
207bc414752SDavid Xu 		new_thread->state = PS_DEAD;
208bc414752SDavid Xu 		new_thread->tid = TID_TERMINATED;
209a9b764e2SDavid Xu 		new_thread->flags |= THR_FLAGS_DETACHED;
210a9b764e2SDavid Xu 		new_thread->refcount--;
211bc414752SDavid Xu 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
212bc414752SDavid Xu 			new_thread->cycle++;
2138d6a11a0SDavid Xu 			_thr_umtx_wake(&new_thread->cycle, INT_MAX, 0);
214bc414752SDavid Xu 		}
215a9b764e2SDavid Xu 		_thr_try_gc(curthread, new_thread); /* thread lock released */
216a9b764e2SDavid Xu 		atomic_add_int(&_thread_active_threads, -1);
217d245d9e1SDavid Xu 	} else if (locked) {
218d0aa4fd3SXin LI 		if (cpusetp != NULL) {
21954dff16bSDavid Xu 			if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
220d0aa4fd3SXin LI 				TID(new_thread), cpusetsize, cpusetp)) {
22154dff16bSDavid Xu 				ret = errno;
22254dff16bSDavid Xu 				/* kill the new thread */
22354dff16bSDavid Xu 				new_thread->force_exit = 1;
224a9b764e2SDavid Xu 				new_thread->flags |= THR_FLAGS_DETACHED;
225a9b764e2SDavid Xu 				_thr_try_gc(curthread, new_thread);
226a9b764e2SDavid Xu 				 /* thread lock released */
22754dff16bSDavid Xu 				goto out;
22854dff16bSDavid Xu 			}
22954dff16bSDavid Xu 		}
23054dff16bSDavid Xu 
231d245d9e1SDavid Xu 		_thr_report_creation(curthread, new_thread);
232d245d9e1SDavid Xu 		THR_THREAD_UNLOCK(curthread, new_thread);
233a9b764e2SDavid Xu 	}
23454dff16bSDavid Xu out:
23502703de8SKonstantin Belousov 	if (ret != 0)
23602703de8SKonstantin Belousov 		*thread = NULL;
237a091d823SDavid Xu 	return (ret);
238bb535300SJeff Roberson }
239bb535300SJeff Roberson 
240a091d823SDavid Xu static int
create_stack(struct pthread_attr * pattr)241a091d823SDavid Xu create_stack(struct pthread_attr *pattr)
242bb535300SJeff Roberson {
2439027ac47SDavid Xu 	int ret;
2449027ac47SDavid Xu 
245a091d823SDavid Xu 	/* Check if a stack was specified in the thread attributes: */
246a091d823SDavid Xu 	if ((pattr->stackaddr_attr) != NULL) {
247a091d823SDavid Xu 		pattr->guardsize_attr = 0;
248a091d823SDavid Xu 		pattr->flags |= THR_STACK_USER;
249a091d823SDavid Xu 		ret = 0;
250a091d823SDavid Xu 	}
251a091d823SDavid Xu 	else
252a091d823SDavid Xu 		ret = _thr_stack_alloc(pattr);
253a091d823SDavid Xu 	return (ret);
254a091d823SDavid Xu }
2559027ac47SDavid Xu 
256a091d823SDavid Xu static void
thread_start(struct pthread * curthread)257a091d823SDavid Xu thread_start(struct pthread *curthread)
258a091d823SDavid Xu {
2592ea1f90aSDavid Xu 	sigset_t set;
2602ea1f90aSDavid Xu 
2612ea1f90aSDavid Xu 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED)
2622ea1f90aSDavid Xu 		set = curthread->sigmask;
263fc908e50SKonstantin Belousov 	_thr_signal_block_setup(curthread);
2642ea1f90aSDavid Xu 
26554dff16bSDavid Xu 	/*
26654dff16bSDavid Xu 	 * This is used as a serialization point to allow parent
26754dff16bSDavid Xu 	 * to report 'new thread' event to debugger or tweak new thread's
26854dff16bSDavid Xu 	 * attributes before the new thread does real-world work.
26954dff16bSDavid Xu 	 */
27054dff16bSDavid Xu 	THR_LOCK(curthread);
27154dff16bSDavid Xu 	THR_UNLOCK(curthread);
27254dff16bSDavid Xu 
27354dff16bSDavid Xu 	if (curthread->force_exit)
27454dff16bSDavid Xu 		_pthread_exit(PTHREAD_CANCELED);
27554dff16bSDavid Xu 
276bc414752SDavid Xu 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
2772ea1f90aSDavid Xu #if 0
2782ea1f90aSDavid Xu 		/* Done in THR_UNLOCK() */
279bc414752SDavid Xu 		_thr_ast(curthread);
2802ea1f90aSDavid Xu #endif
281bc414752SDavid Xu 
282bc414752SDavid Xu 		/*
283bc414752SDavid Xu 		 * Parent thread have stored signal mask for us,
284bc414752SDavid Xu 		 * we should restore it now.
285bc414752SDavid Xu 		 */
2862ea1f90aSDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &set, NULL);
287bc414752SDavid Xu 	}
288bc414752SDavid Xu 
2893832fd24SDavid Xu #ifdef _PTHREAD_FORCED_UNWIND
2903832fd24SDavid Xu 	curthread->unwind_stackend = (char *)curthread->attr.stackaddr_attr +
2913832fd24SDavid Xu 		curthread->attr.stacksize_attr;
2923832fd24SDavid Xu #endif
2933832fd24SDavid Xu 
294b9c8a07dSKonstantin Belousov 	curthread->uexterr.ver = UEXTERROR_VER;
295a5b0b2a7SBrooks Davis 	if (__getosreldate() >= P_OSREL_EXTERRCTL)
296b9c8a07dSKonstantin Belousov 		exterrctl(EXTERRCTL_ENABLE, 0, &curthread->uexterr);
297b9c8a07dSKonstantin Belousov 
298a091d823SDavid Xu 	/* Run the current thread's start routine with argument: */
29937a6356bSDavid Xu 	_pthread_exit(curthread->start_routine(curthread->arg));
300a091d823SDavid Xu 
301bb535300SJeff Roberson 	/* This point should never be reached. */
302bb535300SJeff Roberson 	PANIC("Thread has resumed after exit");
303bb535300SJeff Roberson }
304