blob: 6cbf85b32e052342032a09fc2e0f43530a9b7f25 [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 Felker12e1e322015-04-10 00:26:34 -040070 /* Process robust list in userspace to handle non-pshared mutexes
71 * and the detached thread case where the robust list head will
72 * be invalid when the kernel would process it. */
Rich Felkerf08ab9e2015-04-10 02:27:52 -040073 __vm_lock();
Rich Felker12e1e322015-04-10 00:26:34 -040074 volatile void *volatile *rp;
75 while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
76 pthread_mutex_t *m = (void *)((char *)rp
77 - offsetof(pthread_mutex_t, _m_next));
78 int waiters = m->_m_waiters;
79 int priv = (m->_m_type & 128) ^ 128;
80 self->robust_list.pending = rp;
81 self->robust_list.head = *rp;
Rich Felker384d1032016-06-27 15:18:13 -040082 int cont = a_swap(&m->_m_lock, 0x40000000);
Rich Felker12e1e322015-04-10 00:26:34 -040083 self->robust_list.pending = 0;
84 if (cont < 0 || waiters)
85 __wake(&m->_m_lock, 1, priv);
86 }
Rich Felkerf08ab9e2015-04-10 02:27:52 -040087 __vm_unlock();
Rich Felker12e1e322015-04-10 00:26:34 -040088
Rich Felker5345c9b2014-08-23 23:35:10 -040089 __do_orphaned_stdio_locks();
Rich Felker01d42742015-04-18 18:00:22 -040090 __dl_thread_cleanup();
Rich Felkerb092f1c2014-08-16 02:28:34 -040091
Rich Felker5fcebcd2011-03-10 18:31:37 -050092 if (self->detached && self->map_base) {
Rich Felker6e531f92013-04-26 16:04:30 -040093 /* Detached threads must avoid the kernel clear_child_tid
94 * feature, since the virtual address will have been
95 * unmapped and possibly already reused by a new mapping
96 * at the time the kernel would perform the write. In
97 * the case of threads that started out detached, the
98 * initial clone flags are correct, but if the thread was
99 * detached later (== 2), we need to clear it here. */
100 if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
101
Rich Felker12e1e322015-04-10 00:26:34 -0400102 /* Robust list will no longer be valid, and was already
103 * processed above, so unregister it with the kernel. */
104 if (self->robust_list.off)
105 __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
106
Rich Felkera2d30532015-04-10 03:47:42 -0400107 /* Since __unmapself bypasses the normal munmap code path,
108 * explicitly wait for vmlock holders first. */
109 __vm_wait();
110
Rich Felker6e531f92013-04-26 16:04:30 -0400111 /* The following call unmaps the thread's stack mapping
112 * and then exits without touching the stack. */
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500113 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -0500114 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500115
Rich Felker0c05bd32012-09-06 23:34:10 -0400116 for (;;) __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500117}
Rich Felker0b44a032011-02-12 00:22:29 -0500118
Rich Felkercfd892f2012-05-23 14:13:54 -0400119void __do_cleanup_push(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400120{
Rich Felkerdf151682014-06-10 04:02:40 -0400121 struct pthread *self = __pthread_self();
Rich Felker5f37fc12011-08-03 19:57:46 -0400122 cb->__next = self->cancelbuf;
123 self->cancelbuf = cb;
124}
125
Rich Felkercfd892f2012-05-23 14:13:54 -0400126void __do_cleanup_pop(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400127{
Rich Felkerafc35d52012-02-09 02:33:08 -0500128 __pthread_self()->cancelbuf = cb->__next;
Rich Felker5f37fc12011-08-03 19:57:46 -0400129}
130
Rich Felker3f72cda2011-09-18 10:14:37 -0400131static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -0500132{
Rich Felker3f72cda2011-09-18 10:14:37 -0400133 pthread_t self = p;
Rich Felker9e01be62017-09-06 20:37:19 -0400134 /* States for startlock:
135 * 0 = no need for start sync
136 * 1 = waiting for parent to do work
137 * 2 = failure in parent, child must abort
138 * 3 = success in parent, child must restore sigmask */
Rich Felker1e21e782012-11-11 15:38:04 -0500139 if (self->startlock[0]) {
140 __wait(self->startlock, 0, 1, 1);
Rich Felker9e01be62017-09-06 20:37:19 -0400141 if (self->startlock[0] == 2) {
Rich Felker1e21e782012-11-11 15:38:04 -0500142 self->detached = 2;
143 pthread_exit(0);
144 }
Rich Felker2c074b02013-04-26 19:48:01 -0400145 __restore_sigs(self->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500146 }
Rich Felker99b8a252011-05-07 23:23:58 -0400147 if (self->unblock_cancel)
Rich Felker2f437042012-08-09 22:52:13 -0400148 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
Rich Felkerccc7b4c2013-03-26 23:07:31 -0400149 SIGPT_SET, 0, _NSIG/8);
Rich Felker23614b02014-09-07 10:28:08 -0400150 __pthread_exit(self->start(self->start_arg));
151 return 0;
152}
153
154static int start_c11(void *p)
155{
156 pthread_t self = p;
157 int (*start)(void*) = (int(*)(void*)) self->start;
158 __pthread_exit((void *)(uintptr_t)start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -0400159 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500160}
161
Rich Felker0b44a032011-02-12 00:22:29 -0500162#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
163
164/* pthread_key_create.c overrides this */
Rich Felkera6adb2b2014-07-16 21:32:06 -0400165static volatile size_t dummy = 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500166weak_alias(dummy, __pthread_tsd_size);
Rich Felkera6adb2b2014-07-16 21:32:06 -0400167static void *dummy_tsd[1] = { 0 };
Rich Felkerdab441a2014-03-24 16:57:11 -0400168weak_alias(dummy_tsd, __pthread_tsd_main);
Rich Felker0b44a032011-02-12 00:22:29 -0500169
Rich Felker78a8ef42015-01-15 23:17:38 -0500170volatile int __block_new_threads = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500171size_t __default_stacksize = DEFAULT_STACK_SIZE;
172size_t __default_guardsize = DEFAULT_GUARD_SIZE;
Rich Felker78a8ef42015-01-15 23:17:38 -0500173
Rich Felkera6adb2b2014-07-16 21:32:06 -0400174static FILE *volatile dummy_file = 0;
Rich Felkerdba68bf2011-07-30 08:02:14 -0400175weak_alias(dummy_file, __stdin_used);
176weak_alias(dummy_file, __stdout_used);
177weak_alias(dummy_file, __stderr_used);
178
179static void init_file_lock(FILE *f)
180{
181 if (f && f->lock<0) f->lock = 0;
182}
183
Rich Felkerdcd60372012-10-05 11:51:50 -0400184void *__copy_tls(unsigned char *);
Rich Felker8431d792012-10-04 16:35:46 -0400185
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200186int __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 -0500187{
Rich Felker23614b02014-09-07 10:28:08 -0400188 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
Rich Felkerd5142642013-02-01 22:10:40 -0500189 size_t size, guard;
Rich Felkerdab441a2014-03-24 16:57:11 -0400190 struct pthread *self, *new;
Rich Felker14a835b2013-03-31 23:25:55 -0400191 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
Rich Felkerf68a3462013-09-16 10:54:31 -0400192 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
Rich Felker271c2112013-09-16 10:56:01 -0400193 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
Rich Felkerf68a3462013-09-16 10:54:31 -0400194 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
Rich Felker1e21e782012-11-11 15:38:04 -0500195 int do_sched = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500196 pthread_attr_t attr = { 0 };
Rich Felker0b44a032011-02-12 00:22:29 -0500197
Rich Felkerdab441a2014-03-24 16:57:11 -0400198 if (!libc.can_do_threads) return ENOSYS;
199 self = __pthread_self();
Rich Felker9080cc12011-04-17 16:53:54 -0400200 if (!libc.threaded) {
Rich Felker1b0cdc82015-06-16 07:11:19 +0000201 for (FILE *f=*__ofl_lock(); f; f=f->next)
Rich Felkerdba68bf2011-07-30 08:02:14 -0400202 init_file_lock(f);
Rich Felker1b0cdc82015-06-16 07:11:19 +0000203 __ofl_unlock();
Rich Felkerdba68bf2011-07-30 08:02:14 -0400204 init_file_lock(__stdin_used);
205 init_file_lock(__stdout_used);
206 init_file_lock(__stderr_used);
Rich Felkerdab441a2014-03-24 16:57:11 -0400207 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
Rich Felker689e0e62014-03-24 17:39:08 -0400208 self->tsd = (void **)__pthread_tsd_main;
Rich Felker9080cc12011-04-17 16:53:54 -0400209 libc.threaded = 1;
210 }
Rich Felker23614b02014-09-07 10:28:08 -0400211 if (attrp && !c11) attr = *attrp;
Rich Felker0b44a032011-02-12 00:22:29 -0500212
Rich Felkerdcd60372012-10-05 11:51:50 -0400213 __acquire_ptc();
Rich Felker31fb1742016-11-08 12:09:05 -0500214 if (!attrp || c11) {
215 attr._a_stacksize = __default_stacksize;
216 attr._a_guardsize = __default_guardsize;
217 }
218
Rich Felker78a8ef42015-01-15 23:17:38 -0500219 if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
Rich Felkerdcd60372012-10-05 11:51:50 -0400220
Rich Felkerd5142642013-02-01 22:10:40 -0500221 if (attr._a_stackaddr) {
222 size_t need = libc.tls_size + __pthread_tsd_size;
Rich Felker33ce9202016-11-07 20:47:24 -0500223 size = attr._a_stacksize;
Rich Felkerd5142642013-02-01 22:10:40 -0500224 stack = (void *)(attr._a_stackaddr & -16);
Rich Felkerced64992013-04-06 01:15:08 -0400225 stack_limit = (void *)(attr._a_stackaddr - size);
Rich Felkerd5142642013-02-01 22:10:40 -0500226 /* Use application-provided stack for TLS only when
227 * it does not take more than ~12% or 2k of the
228 * application's stack space. */
229 if (need < size/8 && need < 2048) {
230 tsd = stack - __pthread_tsd_size;
231 stack = tsd - libc.tls_size;
Rich Felkera6293282014-08-22 14:05:10 -0400232 memset(stack, 0, need);
Rich Felkerd5142642013-02-01 22:10:40 -0500233 } else {
234 size = ROUND(need);
235 guard = 0;
Rich Felker819006a2012-06-09 19:53:29 -0400236 }
Rich Felkerd5142642013-02-01 22:10:40 -0500237 } else {
Rich Felker33ce9202016-11-07 20:47:24 -0500238 guard = ROUND(attr._a_guardsize);
239 size = guard + ROUND(attr._a_stacksize
Rich Felkerd5142642013-02-01 22:10:40 -0500240 + libc.tls_size + __pthread_tsd_size);
241 }
242
243 if (!tsd) {
Rich Felker8431d792012-10-04 16:35:46 -0400244 if (guard) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200245 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500246 if (map == MAP_FAILED) goto fail;
Rich Felker75eceb32015-06-17 17:21:46 +0000247 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
248 && errno != ENOSYS) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200249 __munmap(map, size);
Rich Felker72768ea2013-02-01 22:25:19 -0500250 goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400251 }
252 } else {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200253 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500254 if (map == MAP_FAILED) goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400255 }
Rich Felker819006a2012-06-09 19:53:29 -0400256 tsd = map + size - __pthread_tsd_size;
Rich Felker14a835b2013-03-31 23:25:55 -0400257 if (!stack) {
258 stack = tsd - libc.tls_size;
259 stack_limit = map + guard;
260 }
Rich Felker11e4b922011-05-07 23:39:48 -0400261 }
Rich Felkerd5142642013-02-01 22:10:40 -0500262
263 new = __copy_tls(tsd - libc.tls_size);
Rich Felker0b44a032011-02-12 00:22:29 -0500264 new->map_base = map;
265 new->map_size = size;
Rich Felker14a835b2013-03-31 23:25:55 -0400266 new->stack = stack;
267 new->stack_size = stack - stack_limit;
Rich Felker0b44a032011-02-12 00:22:29 -0500268 new->start = entry;
269 new->start_arg = arg;
270 new->self = new;
271 new->tsd = (void *)tsd;
Rich Felker0bc03092014-07-02 19:33:19 -0400272 new->locale = &libc.global_locale;
Rich Felkerd5142642013-02-01 22:10:40 -0500273 if (attr._a_detach) {
Rich Felker92f83962012-07-11 23:36:46 -0400274 new->detached = 1;
Rich Felkerf68a3462013-09-16 10:54:31 -0400275 flags -= CLONE_CHILD_CLEARTID;
Rich Felker92f83962012-07-11 23:36:46 -0400276 }
Rich Felkerd5142642013-02-01 22:10:40 -0500277 if (attr._a_sched) {
Rich Felker1e21e782012-11-11 15:38:04 -0500278 do_sched = new->startlock[0] = 1;
Rich Felker2c074b02013-04-26 19:48:01 -0400279 __block_app_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500280 }
Rich Felker4e98cce2015-04-10 00:54:48 -0400281 new->robust_list.head = &new->robust_list.head;
Rich Felkerbf619d82011-03-29 12:58:22 -0400282 new->unblock_cancel = self->cancel;
Rich Felker484194d2015-05-06 18:37:19 -0400283 new->CANARY = self->CANARY;
Rich Felker0b44a032011-02-12 00:22:29 -0500284
Rich Felker0b44a032011-02-12 00:22:29 -0500285 a_inc(&libc.threads_minus_1);
Rich Felker23614b02014-09-07 10:28:08 -0400286 ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500287
Rich Felkerdcd60372012-10-05 11:51:50 -0400288 __release_ptc();
Rich Felker0b44a032011-02-12 00:22:29 -0500289
Rich Felker1e21e782012-11-11 15:38:04 -0500290 if (do_sched) {
Rich Felker2c074b02013-04-26 19:48:01 -0400291 __restore_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500292 }
293
Rich Felker0b44a032011-02-12 00:22:29 -0500294 if (ret < 0) {
295 a_dec(&libc.threads_minus_1);
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200296 if (map) __munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500297 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500298 }
Rich Felker1e21e782012-11-11 15:38:04 -0500299
300 if (do_sched) {
301 ret = __syscall(SYS_sched_setscheduler, new->tid,
Rich Felkerd5142642013-02-01 22:10:40 -0500302 attr._a_policy, &attr._a_prio);
Rich Felker9e01be62017-09-06 20:37:19 -0400303 a_store(new->startlock, ret<0 ? 2 : 3);
Rich Felker1e21e782012-11-11 15:38:04 -0500304 __wake(new->startlock, 1, 1);
305 if (ret < 0) return -ret;
306 }
307
Rich Felker0b44a032011-02-12 00:22:29 -0500308 *res = new;
309 return 0;
Rich Felker72768ea2013-02-01 22:25:19 -0500310fail:
311 __release_ptc();
312 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500313}
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200314
315weak_alias(__pthread_exit, pthread_exit);
316weak_alias(__pthread_create, pthread_create);