blob: 4eb8b8881d2871ed27153a506594924a9a8f90bb [file] [log] [blame]
Rich Felkerf68a3462013-09-16 10:54:31 -04001#define _GNU_SOURCE
Rich Felker0b44a032011-02-12 00:22:29 -05002#include "pthread_impl.h"
Rich Felkerdba68bf2011-07-30 08:02:14 -04003#include "stdio_impl.h"
Szabolcs Nagyb20760c2013-09-15 02:00:32 +00004#include "libc.h"
Rich Felkerefd4d872012-11-08 17:04:20 -05005#include <sys/mman.h>
Rich Felkera6293282014-08-22 14:05:10 -04006#include <string.h>
Rich Felker12e1e322015-04-10 00:26:34 -04007#include <stddef.h>
Rich Felker0b44a032011-02-12 00:22:29 -05008
Jens Gustedtdf7d0df2014-09-01 00:46:23 +02009void *__mmap(void *, size_t, int, int, int, off_t);
10int __munmap(void *, size_t);
11int __mprotect(void *, size_t, int);
12
Rich Felkerb2486a82011-04-06 20:27:07 -040013static void dummy_0()
14{
15}
Rich Felkerdcd60372012-10-05 11:51:50 -040016weak_alias(dummy_0, __acquire_ptc);
17weak_alias(dummy_0, __release_ptc);
Rich Felkera6054e32011-04-19 23:09:14 -040018weak_alias(dummy_0, __pthread_tsd_run_dtors);
Rich Felker5345c9b2014-08-23 23:35:10 -040019weak_alias(dummy_0, __do_orphaned_stdio_locks);
Rich Felker01d42742015-04-18 18:00:22 -040020weak_alias(dummy_0, __dl_thread_cleanup);
Rich Felkerfd80cfa2011-04-03 02:33:50 -040021
Jens Gustedtdf7d0df2014-09-01 00:46:23 +020022_Noreturn void __pthread_exit(void *result)
Rich Felker1a9a2ff2011-02-13 19:58:30 -050023{
Rich Felkerdf151682014-06-10 04:02:40 -040024 pthread_t self = __pthread_self();
Rich Felkerd0ba0982013-04-26 16:16:04 -040025 sigset_t set;
Rich Felker1a9a2ff2011-02-13 19:58:30 -050026
Rich Felker36d8e972015-02-16 22:25:50 -050027 self->canceldisable = 1;
28 self->cancelasync = 0;
Rich Felkerafc35d52012-02-09 02:33:08 -050029 self->result = result;
30
31 while (self->cancelbuf) {
32 void (*f)(void *) = self->cancelbuf->__f;
33 void *x = self->cancelbuf->__x;
34 self->cancelbuf = self->cancelbuf->__next;
35 f(x);
Rich Felker1ebde9c2011-04-17 17:06:05 -040036 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050037
Rich Felkera6054e32011-04-19 23:09:14 -040038 __pthread_tsd_run_dtors();
Rich Felker1a9a2ff2011-02-13 19:58:30 -050039
Rich Felkerbbbe87e2012-07-12 11:23:43 -040040 __lock(self->exitlock);
Rich Felkerf58c8a02011-06-14 01:25:17 -040041
Rich Felker5fcebcd2011-03-10 18:31:37 -050042 /* Mark this thread dead before decrementing count */
Rich Felkerbbbe87e2012-07-12 11:23:43 -040043 __lock(self->killlock);
Rich Felker5fcebcd2011-03-10 18:31:37 -050044 self->dead = 1;
Rich Felker19eb13b2011-02-19 11:04:36 -050045
Rich Felker6e531f92013-04-26 16:04:30 -040046 /* Block all signals before decrementing the live thread count.
47 * This is important to ensure that dynamically allocated TLS
48 * is not under-allocated/over-committed, and possibly for other
49 * reasons as well. */
Rich Felker2c074b02013-04-26 19:48:01 -040050 __block_all_sigs(&set);
Rich Felker23f21c32013-04-26 15:47:44 -040051
Rich Felkerd674f852013-04-26 17:51:22 -040052 /* Wait to unlock the kill lock, which governs functions like
53 * pthread_kill which target a thread id, until signals have
54 * been blocked. This precludes observation of the thread id
55 * as a live thread (with application code running in it) after
56 * the thread was reported dead by ESRCH being returned. */
57 __unlock(self->killlock);
58
Rich Felkerd0ba0982013-04-26 16:16:04 -040059 /* It's impossible to determine whether this is "the last thread"
60 * until performing the atomic decrement, since multiple threads
61 * could exit at the same time. For the last thread, revert the
62 * decrement and unblock signals to give the atexit handlers and
63 * stdio cleanup code a consistent state. */
64 if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
65 libc.threads_minus_1 = 0;
Rich Felker2c074b02013-04-26 19:48:01 -040066 __restore_sigs(&set);
Rich Felkerd0ba0982013-04-26 16:16:04 -040067 exit(0);
68 }
Rich Felkerfb11b6b2011-02-19 10:38:57 -050069
Rich Felker0bc03092014-07-02 19:33:19 -040070 if (self->locale != &libc.global_locale) {
71 a_dec(&libc.uselocale_cnt);
72 if (self->locale->ctype_utf8)
73 a_dec(&libc.bytelocale_cnt_minus_1);
74 }
75
Rich Felker12e1e322015-04-10 00:26:34 -040076 /* Process robust list in userspace to handle non-pshared mutexes
77 * and the detached thread case where the robust list head will
78 * be invalid when the kernel would process it. */
Rich Felkerf08ab9e2015-04-10 02:27:52 -040079 __vm_lock();
Rich Felker12e1e322015-04-10 00:26:34 -040080 volatile void *volatile *rp;
81 while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
82 pthread_mutex_t *m = (void *)((char *)rp
83 - offsetof(pthread_mutex_t, _m_next));
84 int waiters = m->_m_waiters;
85 int priv = (m->_m_type & 128) ^ 128;
86 self->robust_list.pending = rp;
87 self->robust_list.head = *rp;
88 int cont = a_swap(&m->_m_lock, self->tid|0x40000000);
89 self->robust_list.pending = 0;
90 if (cont < 0 || waiters)
91 __wake(&m->_m_lock, 1, priv);
92 }
Rich Felkerf08ab9e2015-04-10 02:27:52 -040093 __vm_unlock();
Rich Felker12e1e322015-04-10 00:26:34 -040094
Rich Felker5345c9b2014-08-23 23:35:10 -040095 __do_orphaned_stdio_locks();
Rich Felker01d42742015-04-18 18:00:22 -040096 __dl_thread_cleanup();
Rich Felkerb092f1c2014-08-16 02:28:34 -040097
Rich Felker5fcebcd2011-03-10 18:31:37 -050098 if (self->detached && self->map_base) {
Rich Felker6e531f92013-04-26 16:04:30 -040099 /* Detached threads must avoid the kernel clear_child_tid
100 * feature, since the virtual address will have been
101 * unmapped and possibly already reused by a new mapping
102 * at the time the kernel would perform the write. In
103 * the case of threads that started out detached, the
104 * initial clone flags are correct, but if the thread was
105 * detached later (== 2), we need to clear it here. */
106 if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
107
Rich Felker12e1e322015-04-10 00:26:34 -0400108 /* Robust list will no longer be valid, and was already
109 * processed above, so unregister it with the kernel. */
110 if (self->robust_list.off)
111 __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
112
Rich Felkera2d30532015-04-10 03:47:42 -0400113 /* Since __unmapself bypasses the normal munmap code path,
114 * explicitly wait for vmlock holders first. */
115 __vm_wait();
116
Rich Felker6e531f92013-04-26 16:04:30 -0400117 /* The following call unmaps the thread's stack mapping
118 * and then exits without touching the stack. */
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500119 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -0500120 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500121
Rich Felker0c05bd32012-09-06 23:34:10 -0400122 for (;;) __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500123}
Rich Felker0b44a032011-02-12 00:22:29 -0500124
Rich Felkercfd892f2012-05-23 14:13:54 -0400125void __do_cleanup_push(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400126{
Rich Felkerdf151682014-06-10 04:02:40 -0400127 struct pthread *self = __pthread_self();
Rich Felker5f37fc12011-08-03 19:57:46 -0400128 cb->__next = self->cancelbuf;
129 self->cancelbuf = cb;
130}
131
Rich Felkercfd892f2012-05-23 14:13:54 -0400132void __do_cleanup_pop(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400133{
Rich Felkerafc35d52012-02-09 02:33:08 -0500134 __pthread_self()->cancelbuf = cb->__next;
Rich Felker5f37fc12011-08-03 19:57:46 -0400135}
136
Rich Felker3f72cda2011-09-18 10:14:37 -0400137static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -0500138{
Rich Felker3f72cda2011-09-18 10:14:37 -0400139 pthread_t self = p;
Rich Felker1e21e782012-11-11 15:38:04 -0500140 if (self->startlock[0]) {
141 __wait(self->startlock, 0, 1, 1);
142 if (self->startlock[0]) {
143 self->detached = 2;
144 pthread_exit(0);
145 }
Rich Felker2c074b02013-04-26 19:48:01 -0400146 __restore_sigs(self->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500147 }
Rich Felker99b8a252011-05-07 23:23:58 -0400148 if (self->unblock_cancel)
Rich Felker2f437042012-08-09 22:52:13 -0400149 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
Rich Felkerccc7b4c2013-03-26 23:07:31 -0400150 SIGPT_SET, 0, _NSIG/8);
Rich Felker23614b02014-09-07 10:28:08 -0400151 __pthread_exit(self->start(self->start_arg));
152 return 0;
153}
154
155static int start_c11(void *p)
156{
157 pthread_t self = p;
158 int (*start)(void*) = (int(*)(void*)) self->start;
159 __pthread_exit((void *)(uintptr_t)start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -0400160 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500161}
162
Rich Felker0b44a032011-02-12 00:22:29 -0500163#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
164
165/* pthread_key_create.c overrides this */
Rich Felkera6adb2b2014-07-16 21:32:06 -0400166static volatile size_t dummy = 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500167weak_alias(dummy, __pthread_tsd_size);
Rich Felkera6adb2b2014-07-16 21:32:06 -0400168static void *dummy_tsd[1] = { 0 };
Rich Felkerdab441a2014-03-24 16:57:11 -0400169weak_alias(dummy_tsd, __pthread_tsd_main);
Rich Felker0b44a032011-02-12 00:22:29 -0500170
Rich Felker78a8ef42015-01-15 23:17:38 -0500171volatile int __block_new_threads = 0;
172
Rich Felkera6adb2b2014-07-16 21:32:06 -0400173static FILE *volatile dummy_file = 0;
Rich Felkerdba68bf2011-07-30 08:02:14 -0400174weak_alias(dummy_file, __stdin_used);
175weak_alias(dummy_file, __stdout_used);
176weak_alias(dummy_file, __stderr_used);
177
178static void init_file_lock(FILE *f)
179{
180 if (f && f->lock<0) f->lock = 0;
181}
182
Rich Felkerdcd60372012-10-05 11:51:50 -0400183void *__copy_tls(unsigned char *);
Rich Felker8431d792012-10-04 16:35:46 -0400184
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200185int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
Rich Felker0b44a032011-02-12 00:22:29 -0500186{
Rich Felker23614b02014-09-07 10:28:08 -0400187 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
Rich Felkerd5142642013-02-01 22:10:40 -0500188 size_t size, guard;
Rich Felkerdab441a2014-03-24 16:57:11 -0400189 struct pthread *self, *new;
Rich Felker14a835b2013-03-31 23:25:55 -0400190 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
Rich Felkerf68a3462013-09-16 10:54:31 -0400191 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
Rich Felker271c2112013-09-16 10:56:01 -0400192 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
Rich Felkerf68a3462013-09-16 10:54:31 -0400193 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
Rich Felker1e21e782012-11-11 15:38:04 -0500194 int do_sched = 0;
Rich Felkerd5142642013-02-01 22:10:40 -0500195 pthread_attr_t attr = {0};
Rich Felker0b44a032011-02-12 00:22:29 -0500196
Rich Felkerdab441a2014-03-24 16:57:11 -0400197 if (!libc.can_do_threads) return ENOSYS;
198 self = __pthread_self();
Rich Felker9080cc12011-04-17 16:53:54 -0400199 if (!libc.threaded) {
Rich Felkerdba68bf2011-07-30 08:02:14 -0400200 for (FILE *f=libc.ofl_head; f; f=f->next)
201 init_file_lock(f);
202 init_file_lock(__stdin_used);
203 init_file_lock(__stdout_used);
204 init_file_lock(__stderr_used);
Rich Felkerdab441a2014-03-24 16:57:11 -0400205 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
Rich Felker689e0e62014-03-24 17:39:08 -0400206 self->tsd = (void **)__pthread_tsd_main;
Rich Felker9080cc12011-04-17 16:53:54 -0400207 libc.threaded = 1;
208 }
Rich Felker23614b02014-09-07 10:28:08 -0400209 if (attrp && !c11) attr = *attrp;
Rich Felker0b44a032011-02-12 00:22:29 -0500210
Rich Felkerdcd60372012-10-05 11:51:50 -0400211 __acquire_ptc();
Rich Felker78a8ef42015-01-15 23:17:38 -0500212 if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
Rich Felkerdcd60372012-10-05 11:51:50 -0400213
Rich Felkerd5142642013-02-01 22:10:40 -0500214 if (attr._a_stackaddr) {
215 size_t need = libc.tls_size + __pthread_tsd_size;
216 size = attr._a_stacksize + DEFAULT_STACK_SIZE;
217 stack = (void *)(attr._a_stackaddr & -16);
Rich Felkerced64992013-04-06 01:15:08 -0400218 stack_limit = (void *)(attr._a_stackaddr - size);
Rich Felkerd5142642013-02-01 22:10:40 -0500219 /* Use application-provided stack for TLS only when
220 * it does not take more than ~12% or 2k of the
221 * application's stack space. */
222 if (need < size/8 && need < 2048) {
223 tsd = stack - __pthread_tsd_size;
224 stack = tsd - libc.tls_size;
Rich Felkera6293282014-08-22 14:05:10 -0400225 memset(stack, 0, need);
Rich Felkerd5142642013-02-01 22:10:40 -0500226 } else {
227 size = ROUND(need);
228 guard = 0;
Rich Felker819006a2012-06-09 19:53:29 -0400229 }
Rich Felkerd5142642013-02-01 22:10:40 -0500230 } else {
231 guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
232 size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
233 + libc.tls_size + __pthread_tsd_size);
234 }
235
236 if (!tsd) {
Rich Felker8431d792012-10-04 16:35:46 -0400237 if (guard) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200238 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500239 if (map == MAP_FAILED) goto fail;
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200240 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
241 __munmap(map, size);
Rich Felker72768ea2013-02-01 22:25:19 -0500242 goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400243 }
244 } else {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200245 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500246 if (map == MAP_FAILED) goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400247 }
Rich Felker819006a2012-06-09 19:53:29 -0400248 tsd = map + size - __pthread_tsd_size;
Rich Felker14a835b2013-03-31 23:25:55 -0400249 if (!stack) {
250 stack = tsd - libc.tls_size;
251 stack_limit = map + guard;
252 }
Rich Felker11e4b922011-05-07 23:39:48 -0400253 }
Rich Felkerd5142642013-02-01 22:10:40 -0500254
255 new = __copy_tls(tsd - libc.tls_size);
Rich Felker0b44a032011-02-12 00:22:29 -0500256 new->map_base = map;
257 new->map_size = size;
Rich Felker14a835b2013-03-31 23:25:55 -0400258 new->stack = stack;
259 new->stack_size = stack - stack_limit;
Rich Felker0b44a032011-02-12 00:22:29 -0500260 new->start = entry;
261 new->start_arg = arg;
262 new->self = new;
263 new->tsd = (void *)tsd;
Rich Felker0bc03092014-07-02 19:33:19 -0400264 new->locale = &libc.global_locale;
Rich Felkerd5142642013-02-01 22:10:40 -0500265 if (attr._a_detach) {
Rich Felker92f83962012-07-11 23:36:46 -0400266 new->detached = 1;
Rich Felkerf68a3462013-09-16 10:54:31 -0400267 flags -= CLONE_CHILD_CLEARTID;
Rich Felker92f83962012-07-11 23:36:46 -0400268 }
Rich Felkerd5142642013-02-01 22:10:40 -0500269 if (attr._a_sched) {
Rich Felker1e21e782012-11-11 15:38:04 -0500270 do_sched = new->startlock[0] = 1;
Rich Felker2c074b02013-04-26 19:48:01 -0400271 __block_app_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500272 }
Rich Felker4e98cce2015-04-10 00:54:48 -0400273 new->robust_list.head = &new->robust_list.head;
Rich Felkerbf619d82011-03-29 12:58:22 -0400274 new->unblock_cancel = self->cancel;
Rich Felker484194d2015-05-06 18:37:19 -0400275 new->CANARY = self->CANARY;
Rich Felker0b44a032011-02-12 00:22:29 -0500276
Rich Felker0b44a032011-02-12 00:22:29 -0500277 a_inc(&libc.threads_minus_1);
Rich Felker23614b02014-09-07 10:28:08 -0400278 ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500279
Rich Felkerdcd60372012-10-05 11:51:50 -0400280 __release_ptc();
Rich Felker0b44a032011-02-12 00:22:29 -0500281
Rich Felker1e21e782012-11-11 15:38:04 -0500282 if (do_sched) {
Rich Felker2c074b02013-04-26 19:48:01 -0400283 __restore_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500284 }
285
Rich Felker0b44a032011-02-12 00:22:29 -0500286 if (ret < 0) {
287 a_dec(&libc.threads_minus_1);
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200288 if (map) __munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500289 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500290 }
Rich Felker1e21e782012-11-11 15:38:04 -0500291
292 if (do_sched) {
293 ret = __syscall(SYS_sched_setscheduler, new->tid,
Rich Felkerd5142642013-02-01 22:10:40 -0500294 attr._a_policy, &attr._a_prio);
Rich Felker1e21e782012-11-11 15:38:04 -0500295 a_store(new->startlock, ret<0 ? 2 : 0);
296 __wake(new->startlock, 1, 1);
297 if (ret < 0) return -ret;
298 }
299
Rich Felker0b44a032011-02-12 00:22:29 -0500300 *res = new;
301 return 0;
Rich Felker72768ea2013-02-01 22:25:19 -0500302fail:
303 __release_ptc();
304 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500305}
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200306
307weak_alias(__pthread_exit, pthread_exit);
308weak_alias(__pthread_create, pthread_create);