blob: 6e2e48161753e03f0296e97383b7e081b711fb32 [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;
82 int cont = a_swap(&m->_m_lock, self->tid|0x40000000);
83 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;
166
Rich Felkera6adb2b2014-07-16 21:32:06 -0400167static FILE *volatile dummy_file = 0;
Rich Felkerdba68bf2011-07-30 08:02:14 -0400168weak_alias(dummy_file, __stdin_used);
169weak_alias(dummy_file, __stdout_used);
170weak_alias(dummy_file, __stderr_used);
171
172static void init_file_lock(FILE *f)
173{
174 if (f && f->lock<0) f->lock = 0;
175}
176
Rich Felkerdcd60372012-10-05 11:51:50 -0400177void *__copy_tls(unsigned char *);
Rich Felker8431d792012-10-04 16:35:46 -0400178
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200179int __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 -0500180{
Rich Felker23614b02014-09-07 10:28:08 -0400181 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
Rich Felkerd5142642013-02-01 22:10:40 -0500182 size_t size, guard;
Rich Felkerdab441a2014-03-24 16:57:11 -0400183 struct pthread *self, *new;
Rich Felker14a835b2013-03-31 23:25:55 -0400184 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
Rich Felkerf68a3462013-09-16 10:54:31 -0400185 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
Rich Felker271c2112013-09-16 10:56:01 -0400186 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
Rich Felkerf68a3462013-09-16 10:54:31 -0400187 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
Rich Felker1e21e782012-11-11 15:38:04 -0500188 int do_sched = 0;
Rich Felkerd5142642013-02-01 22:10:40 -0500189 pthread_attr_t attr = {0};
Rich Felker0b44a032011-02-12 00:22:29 -0500190
Rich Felkerdab441a2014-03-24 16:57:11 -0400191 if (!libc.can_do_threads) return ENOSYS;
192 self = __pthread_self();
Rich Felker9080cc12011-04-17 16:53:54 -0400193 if (!libc.threaded) {
Rich Felker1b0cdc82015-06-16 07:11:19 +0000194 for (FILE *f=*__ofl_lock(); f; f=f->next)
Rich Felkerdba68bf2011-07-30 08:02:14 -0400195 init_file_lock(f);
Rich Felker1b0cdc82015-06-16 07:11:19 +0000196 __ofl_unlock();
Rich Felkerdba68bf2011-07-30 08:02:14 -0400197 init_file_lock(__stdin_used);
198 init_file_lock(__stdout_used);
199 init_file_lock(__stderr_used);
Rich Felkerdab441a2014-03-24 16:57:11 -0400200 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
Rich Felker689e0e62014-03-24 17:39:08 -0400201 self->tsd = (void **)__pthread_tsd_main;
Rich Felker9080cc12011-04-17 16:53:54 -0400202 libc.threaded = 1;
203 }
Rich Felker23614b02014-09-07 10:28:08 -0400204 if (attrp && !c11) attr = *attrp;
Rich Felker0b44a032011-02-12 00:22:29 -0500205
Rich Felkerdcd60372012-10-05 11:51:50 -0400206 __acquire_ptc();
Rich Felker78a8ef42015-01-15 23:17:38 -0500207 if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
Rich Felkerdcd60372012-10-05 11:51:50 -0400208
Rich Felkerd5142642013-02-01 22:10:40 -0500209 if (attr._a_stackaddr) {
210 size_t need = libc.tls_size + __pthread_tsd_size;
211 size = attr._a_stacksize + DEFAULT_STACK_SIZE;
212 stack = (void *)(attr._a_stackaddr & -16);
Rich Felkerced64992013-04-06 01:15:08 -0400213 stack_limit = (void *)(attr._a_stackaddr - size);
Rich Felkerd5142642013-02-01 22:10:40 -0500214 /* Use application-provided stack for TLS only when
215 * it does not take more than ~12% or 2k of the
216 * application's stack space. */
217 if (need < size/8 && need < 2048) {
218 tsd = stack - __pthread_tsd_size;
219 stack = tsd - libc.tls_size;
Rich Felkera6293282014-08-22 14:05:10 -0400220 memset(stack, 0, need);
Rich Felkerd5142642013-02-01 22:10:40 -0500221 } else {
222 size = ROUND(need);
223 guard = 0;
Rich Felker819006a2012-06-09 19:53:29 -0400224 }
Rich Felkerd5142642013-02-01 22:10:40 -0500225 } else {
226 guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
227 size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
228 + libc.tls_size + __pthread_tsd_size);
229 }
230
231 if (!tsd) {
Rich Felker8431d792012-10-04 16:35:46 -0400232 if (guard) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200233 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500234 if (map == MAP_FAILED) goto fail;
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200235 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
236 __munmap(map, size);
Rich Felker72768ea2013-02-01 22:25:19 -0500237 goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400238 }
239 } else {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200240 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500241 if (map == MAP_FAILED) goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400242 }
Rich Felker819006a2012-06-09 19:53:29 -0400243 tsd = map + size - __pthread_tsd_size;
Rich Felker14a835b2013-03-31 23:25:55 -0400244 if (!stack) {
245 stack = tsd - libc.tls_size;
246 stack_limit = map + guard;
247 }
Rich Felker11e4b922011-05-07 23:39:48 -0400248 }
Rich Felkerd5142642013-02-01 22:10:40 -0500249
250 new = __copy_tls(tsd - libc.tls_size);
Rich Felker0b44a032011-02-12 00:22:29 -0500251 new->map_base = map;
252 new->map_size = size;
Rich Felker14a835b2013-03-31 23:25:55 -0400253 new->stack = stack;
254 new->stack_size = stack - stack_limit;
Rich Felker0b44a032011-02-12 00:22:29 -0500255 new->start = entry;
256 new->start_arg = arg;
257 new->self = new;
258 new->tsd = (void *)tsd;
Rich Felker0bc03092014-07-02 19:33:19 -0400259 new->locale = &libc.global_locale;
Rich Felkerd5142642013-02-01 22:10:40 -0500260 if (attr._a_detach) {
Rich Felker92f83962012-07-11 23:36:46 -0400261 new->detached = 1;
Rich Felkerf68a3462013-09-16 10:54:31 -0400262 flags -= CLONE_CHILD_CLEARTID;
Rich Felker92f83962012-07-11 23:36:46 -0400263 }
Rich Felkerd5142642013-02-01 22:10:40 -0500264 if (attr._a_sched) {
Rich Felker1e21e782012-11-11 15:38:04 -0500265 do_sched = new->startlock[0] = 1;
Rich Felker2c074b02013-04-26 19:48:01 -0400266 __block_app_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500267 }
Rich Felker4e98cce2015-04-10 00:54:48 -0400268 new->robust_list.head = &new->robust_list.head;
Rich Felkerbf619d82011-03-29 12:58:22 -0400269 new->unblock_cancel = self->cancel;
Rich Felker484194d2015-05-06 18:37:19 -0400270 new->CANARY = self->CANARY;
Rich Felker0b44a032011-02-12 00:22:29 -0500271
Rich Felker0b44a032011-02-12 00:22:29 -0500272 a_inc(&libc.threads_minus_1);
Rich Felker23614b02014-09-07 10:28:08 -0400273 ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500274
Rich Felkerdcd60372012-10-05 11:51:50 -0400275 __release_ptc();
Rich Felker0b44a032011-02-12 00:22:29 -0500276
Rich Felker1e21e782012-11-11 15:38:04 -0500277 if (do_sched) {
Rich Felker2c074b02013-04-26 19:48:01 -0400278 __restore_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500279 }
280
Rich Felker0b44a032011-02-12 00:22:29 -0500281 if (ret < 0) {
282 a_dec(&libc.threads_minus_1);
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200283 if (map) __munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500284 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500285 }
Rich Felker1e21e782012-11-11 15:38:04 -0500286
287 if (do_sched) {
288 ret = __syscall(SYS_sched_setscheduler, new->tid,
Rich Felkerd5142642013-02-01 22:10:40 -0500289 attr._a_policy, &attr._a_prio);
Rich Felker1e21e782012-11-11 15:38:04 -0500290 a_store(new->startlock, ret<0 ? 2 : 0);
291 __wake(new->startlock, 1, 1);
292 if (ret < 0) return -ret;
293 }
294
Rich Felker0b44a032011-02-12 00:22:29 -0500295 *res = new;
296 return 0;
Rich Felker72768ea2013-02-01 22:25:19 -0500297fail:
298 __release_ptc();
299 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500300}
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200301
302weak_alias(__pthread_exit, pthread_exit);
303weak_alias(__pthread_create, pthread_create);