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