blob: 49f2f7296804be25efe16ce57f6280c66751eda9 [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 Felker1e21e782012-11-11 15:38:04 -0500134 if (self->startlock[0]) {
135 __wait(self->startlock, 0, 1, 1);
136 if (self->startlock[0]) {
137 self->detached = 2;
138 pthread_exit(0);
139 }
Rich Felker2c074b02013-04-26 19:48:01 -0400140 __restore_sigs(self->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500141 }
Rich Felker99b8a252011-05-07 23:23:58 -0400142 if (self->unblock_cancel)
Rich Felker2f437042012-08-09 22:52:13 -0400143 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
Rich Felkerccc7b4c2013-03-26 23:07:31 -0400144 SIGPT_SET, 0, _NSIG/8);
Rich Felker23614b02014-09-07 10:28:08 -0400145 __pthread_exit(self->start(self->start_arg));
146 return 0;
147}
148
149static int start_c11(void *p)
150{
151 pthread_t self = p;
152 int (*start)(void*) = (int(*)(void*)) self->start;
153 __pthread_exit((void *)(uintptr_t)start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -0400154 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500155}
156
Rich Felker0b44a032011-02-12 00:22:29 -0500157#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
158
159/* pthread_key_create.c overrides this */
Rich Felkera6adb2b2014-07-16 21:32:06 -0400160static volatile size_t dummy = 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500161weak_alias(dummy, __pthread_tsd_size);
Rich Felkera6adb2b2014-07-16 21:32:06 -0400162static void *dummy_tsd[1] = { 0 };
Rich Felkerdab441a2014-03-24 16:57:11 -0400163weak_alias(dummy_tsd, __pthread_tsd_main);
Rich Felker0b44a032011-02-12 00:22:29 -0500164
Rich Felker78a8ef42015-01-15 23:17:38 -0500165volatile int __block_new_threads = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500166size_t __default_stacksize = DEFAULT_STACK_SIZE;
167size_t __default_guardsize = DEFAULT_GUARD_SIZE;
Rich Felker78a8ef42015-01-15 23:17:38 -0500168
Rich Felkera6adb2b2014-07-16 21:32:06 -0400169static FILE *volatile dummy_file = 0;
Rich Felkerdba68bf2011-07-30 08:02:14 -0400170weak_alias(dummy_file, __stdin_used);
171weak_alias(dummy_file, __stdout_used);
172weak_alias(dummy_file, __stderr_used);
173
174static void init_file_lock(FILE *f)
175{
176 if (f && f->lock<0) f->lock = 0;
177}
178
Rich Felkerdcd60372012-10-05 11:51:50 -0400179void *__copy_tls(unsigned char *);
Rich Felker8431d792012-10-04 16:35:46 -0400180
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200181int __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 -0500182{
Rich Felker23614b02014-09-07 10:28:08 -0400183 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
Rich Felkerd5142642013-02-01 22:10:40 -0500184 size_t size, guard;
Rich Felkerdab441a2014-03-24 16:57:11 -0400185 struct pthread *self, *new;
Rich Felker14a835b2013-03-31 23:25:55 -0400186 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
Rich Felkerf68a3462013-09-16 10:54:31 -0400187 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
Rich Felker271c2112013-09-16 10:56:01 -0400188 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
Rich Felkerf68a3462013-09-16 10:54:31 -0400189 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
Rich Felker1e21e782012-11-11 15:38:04 -0500190 int do_sched = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500191 pthread_attr_t attr = { 0 };
Rich Felker0b44a032011-02-12 00:22:29 -0500192
Rich Felkerdab441a2014-03-24 16:57:11 -0400193 if (!libc.can_do_threads) return ENOSYS;
194 self = __pthread_self();
Rich Felker9080cc12011-04-17 16:53:54 -0400195 if (!libc.threaded) {
Rich Felker1b0cdc82015-06-16 07:11:19 +0000196 for (FILE *f=*__ofl_lock(); f; f=f->next)
Rich Felkerdba68bf2011-07-30 08:02:14 -0400197 init_file_lock(f);
Rich Felker1b0cdc82015-06-16 07:11:19 +0000198 __ofl_unlock();
Rich Felkerdba68bf2011-07-30 08:02:14 -0400199 init_file_lock(__stdin_used);
200 init_file_lock(__stdout_used);
201 init_file_lock(__stderr_used);
Rich Felkerdab441a2014-03-24 16:57:11 -0400202 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
Rich Felker689e0e62014-03-24 17:39:08 -0400203 self->tsd = (void **)__pthread_tsd_main;
Rich Felker9080cc12011-04-17 16:53:54 -0400204 libc.threaded = 1;
205 }
Rich Felker23614b02014-09-07 10:28:08 -0400206 if (attrp && !c11) attr = *attrp;
Rich Felker0b44a032011-02-12 00:22:29 -0500207
Rich Felkerdcd60372012-10-05 11:51:50 -0400208 __acquire_ptc();
Rich Felker31fb1742016-11-08 12:09:05 -0500209 if (!attrp || c11) {
210 attr._a_stacksize = __default_stacksize;
211 attr._a_guardsize = __default_guardsize;
212 }
213
Rich Felker78a8ef42015-01-15 23:17:38 -0500214 if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
Rich Felkerdcd60372012-10-05 11:51:50 -0400215
Rich Felkerd5142642013-02-01 22:10:40 -0500216 if (attr._a_stackaddr) {
217 size_t need = libc.tls_size + __pthread_tsd_size;
Rich Felker33ce9202016-11-07 20:47:24 -0500218 size = attr._a_stacksize;
Rich Felkerd5142642013-02-01 22:10:40 -0500219 stack = (void *)(attr._a_stackaddr & -16);
Rich Felkerced64992013-04-06 01:15:08 -0400220 stack_limit = (void *)(attr._a_stackaddr - size);
Rich Felkerd5142642013-02-01 22:10:40 -0500221 /* Use application-provided stack for TLS only when
222 * it does not take more than ~12% or 2k of the
223 * application's stack space. */
224 if (need < size/8 && need < 2048) {
225 tsd = stack - __pthread_tsd_size;
226 stack = tsd - libc.tls_size;
Rich Felkera6293282014-08-22 14:05:10 -0400227 memset(stack, 0, need);
Rich Felkerd5142642013-02-01 22:10:40 -0500228 } else {
229 size = ROUND(need);
230 guard = 0;
Rich Felker819006a2012-06-09 19:53:29 -0400231 }
Rich Felkerd5142642013-02-01 22:10:40 -0500232 } else {
Rich Felker33ce9202016-11-07 20:47:24 -0500233 guard = ROUND(attr._a_guardsize);
234 size = guard + ROUND(attr._a_stacksize
Rich Felkerd5142642013-02-01 22:10:40 -0500235 + libc.tls_size + __pthread_tsd_size);
236 }
237
238 if (!tsd) {
Rich Felker8431d792012-10-04 16:35:46 -0400239 if (guard) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200240 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500241 if (map == MAP_FAILED) goto fail;
Rich Felker75eceb32015-06-17 17:21:46 +0000242 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
243 && errno != ENOSYS) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200244 __munmap(map, size);
Rich Felker72768ea2013-02-01 22:25:19 -0500245 goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400246 }
247 } else {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200248 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500249 if (map == MAP_FAILED) goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400250 }
Rich Felker819006a2012-06-09 19:53:29 -0400251 tsd = map + size - __pthread_tsd_size;
Rich Felker14a835b2013-03-31 23:25:55 -0400252 if (!stack) {
253 stack = tsd - libc.tls_size;
254 stack_limit = map + guard;
255 }
Rich Felker11e4b922011-05-07 23:39:48 -0400256 }
Rich Felkerd5142642013-02-01 22:10:40 -0500257
258 new = __copy_tls(tsd - libc.tls_size);
Rich Felker0b44a032011-02-12 00:22:29 -0500259 new->map_base = map;
260 new->map_size = size;
Rich Felker14a835b2013-03-31 23:25:55 -0400261 new->stack = stack;
262 new->stack_size = stack - stack_limit;
Rich Felker0b44a032011-02-12 00:22:29 -0500263 new->start = entry;
264 new->start_arg = arg;
265 new->self = new;
266 new->tsd = (void *)tsd;
Rich Felker0bc03092014-07-02 19:33:19 -0400267 new->locale = &libc.global_locale;
Rich Felkerd5142642013-02-01 22:10:40 -0500268 if (attr._a_detach) {
Rich Felker92f83962012-07-11 23:36:46 -0400269 new->detached = 1;
Rich Felkerf68a3462013-09-16 10:54:31 -0400270 flags -= CLONE_CHILD_CLEARTID;
Rich Felker92f83962012-07-11 23:36:46 -0400271 }
Rich Felkerd5142642013-02-01 22:10:40 -0500272 if (attr._a_sched) {
Rich Felker1e21e782012-11-11 15:38:04 -0500273 do_sched = new->startlock[0] = 1;
Rich Felker2c074b02013-04-26 19:48:01 -0400274 __block_app_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500275 }
Rich Felker4e98cce2015-04-10 00:54:48 -0400276 new->robust_list.head = &new->robust_list.head;
Rich Felkerbf619d82011-03-29 12:58:22 -0400277 new->unblock_cancel = self->cancel;
Rich Felker484194d2015-05-06 18:37:19 -0400278 new->CANARY = self->CANARY;
Rich Felker0b44a032011-02-12 00:22:29 -0500279
Rich Felker0b44a032011-02-12 00:22:29 -0500280 a_inc(&libc.threads_minus_1);
Rich Felker23614b02014-09-07 10:28:08 -0400281 ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500282
Rich Felkerdcd60372012-10-05 11:51:50 -0400283 __release_ptc();
Rich Felker0b44a032011-02-12 00:22:29 -0500284
Rich Felker1e21e782012-11-11 15:38:04 -0500285 if (do_sched) {
Rich Felker2c074b02013-04-26 19:48:01 -0400286 __restore_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500287 }
288
Rich Felker0b44a032011-02-12 00:22:29 -0500289 if (ret < 0) {
290 a_dec(&libc.threads_minus_1);
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200291 if (map) __munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500292 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500293 }
Rich Felker1e21e782012-11-11 15:38:04 -0500294
295 if (do_sched) {
296 ret = __syscall(SYS_sched_setscheduler, new->tid,
Rich Felkerd5142642013-02-01 22:10:40 -0500297 attr._a_policy, &attr._a_prio);
Rich Felker1e21e782012-11-11 15:38:04 -0500298 a_store(new->startlock, ret<0 ? 2 : 0);
299 __wake(new->startlock, 1, 1);
300 if (ret < 0) return -ret;
301 }
302
Rich Felker0b44a032011-02-12 00:22:29 -0500303 *res = new;
304 return 0;
Rich Felker72768ea2013-02-01 22:25:19 -0500305fail:
306 __release_ptc();
307 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500308}
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200309
310weak_alias(__pthread_exit, pthread_exit);
311weak_alias(__pthread_create, pthread_create);