blob: a86b5e1ba3f02ab104ea6348d986ad5b140c4ff0 [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
Jens Gustedtc4bc0b12018-01-03 14:17:12 +010040 LOCK(self->exitlock);
Rich Felkerf58c8a02011-06-14 01:25:17 -040041
Rich Felker526e64f2018-05-04 14:26:31 -040042 /* Access to target the exiting thread with syscalls that use
43 * its kernel tid is controlled by killlock. For detached threads,
44 * any use past this point would have undefined behavior, but for
45 * joinable threads it's a valid usage that must be handled. */
Jens Gustedtc4bc0b12018-01-03 14:17:12 +010046 LOCK(self->killlock);
Rich Felker19eb13b2011-02-19 11:04:36 -050047
Rich Felker6e531f92013-04-26 16:04:30 -040048 /* Block all signals before decrementing the live thread count.
49 * This is important to ensure that dynamically allocated TLS
50 * is not under-allocated/over-committed, and possibly for other
51 * reasons as well. */
Rich Felker2c074b02013-04-26 19:48:01 -040052 __block_all_sigs(&set);
Rich Felker23f21c32013-04-26 15:47:44 -040053
Rich Felkerd0ba0982013-04-26 16:16:04 -040054 /* It's impossible to determine whether this is "the last thread"
55 * until performing the atomic decrement, since multiple threads
56 * could exit at the same time. For the last thread, revert the
Rich Felker526e64f2018-05-04 14:26:31 -040057 * decrement, restore the tid, and unblock signals to give the
58 * atexit handlers and stdio cleanup code a consistent state. */
Rich Felkerd0ba0982013-04-26 16:16:04 -040059 if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
60 libc.threads_minus_1 = 0;
Rich Felker526e64f2018-05-04 14:26:31 -040061 UNLOCK(self->killlock);
Rich Felker2c074b02013-04-26 19:48:01 -040062 __restore_sigs(&set);
Rich Felkerd0ba0982013-04-26 16:16:04 -040063 exit(0);
64 }
Rich Felkerfb11b6b2011-02-19 10:38:57 -050065
Rich Felker12e1e322015-04-10 00:26:34 -040066 /* Process robust list in userspace to handle non-pshared mutexes
67 * and the detached thread case where the robust list head will
68 * be invalid when the kernel would process it. */
Rich Felkerf08ab9e2015-04-10 02:27:52 -040069 __vm_lock();
Rich Felker12e1e322015-04-10 00:26:34 -040070 volatile void *volatile *rp;
71 while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
72 pthread_mutex_t *m = (void *)((char *)rp
73 - offsetof(pthread_mutex_t, _m_next));
74 int waiters = m->_m_waiters;
75 int priv = (m->_m_type & 128) ^ 128;
76 self->robust_list.pending = rp;
77 self->robust_list.head = *rp;
Rich Felker384d1032016-06-27 15:18:13 -040078 int cont = a_swap(&m->_m_lock, 0x40000000);
Rich Felker12e1e322015-04-10 00:26:34 -040079 self->robust_list.pending = 0;
80 if (cont < 0 || waiters)
81 __wake(&m->_m_lock, 1, priv);
82 }
Rich Felkerf08ab9e2015-04-10 02:27:52 -040083 __vm_unlock();
Rich Felker12e1e322015-04-10 00:26:34 -040084
Rich Felker5345c9b2014-08-23 23:35:10 -040085 __do_orphaned_stdio_locks();
Rich Felker01d42742015-04-18 18:00:22 -040086 __dl_thread_cleanup();
Rich Felkerb092f1c2014-08-16 02:28:34 -040087
Rich Felker5fcebcd2011-03-10 18:31:37 -050088 if (self->detached && self->map_base) {
Rich Felker6e531f92013-04-26 16:04:30 -040089 /* Detached threads must avoid the kernel clear_child_tid
90 * feature, since the virtual address will have been
91 * unmapped and possibly already reused by a new mapping
92 * at the time the kernel would perform the write. In
93 * the case of threads that started out detached, the
94 * initial clone flags are correct, but if the thread was
95 * detached later (== 2), we need to clear it here. */
96 if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
97
Rich Felker12e1e322015-04-10 00:26:34 -040098 /* Robust list will no longer be valid, and was already
99 * processed above, so unregister it with the kernel. */
100 if (self->robust_list.off)
101 __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
102
Rich Felkera2d30532015-04-10 03:47:42 -0400103 /* Since __unmapself bypasses the normal munmap code path,
104 * explicitly wait for vmlock holders first. */
105 __vm_wait();
106
Rich Felker6e531f92013-04-26 16:04:30 -0400107 /* The following call unmaps the thread's stack mapping
108 * and then exits without touching the stack. */
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500109 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -0500110 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500111
Rich Felker526e64f2018-05-04 14:26:31 -0400112 /* After the kernel thread exits, its tid may be reused. Clear it
113 * to prevent inadvertent use and inform functions that would use
114 * it that it's no longer available. */
115 self->tid = 0;
116 UNLOCK(self->killlock);
117
Rich Felker0c05bd32012-09-06 23:34:10 -0400118 for (;;) __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500119}
Rich Felker0b44a032011-02-12 00:22:29 -0500120
Rich Felkercfd892f2012-05-23 14:13:54 -0400121void __do_cleanup_push(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400122{
Rich Felkerdf151682014-06-10 04:02:40 -0400123 struct pthread *self = __pthread_self();
Rich Felker5f37fc12011-08-03 19:57:46 -0400124 cb->__next = self->cancelbuf;
125 self->cancelbuf = cb;
126}
127
Rich Felkercfd892f2012-05-23 14:13:54 -0400128void __do_cleanup_pop(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -0400129{
Rich Felkerafc35d52012-02-09 02:33:08 -0500130 __pthread_self()->cancelbuf = cb->__next;
Rich Felker5f37fc12011-08-03 19:57:46 -0400131}
132
Rich Felker3f72cda2011-09-18 10:14:37 -0400133static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -0500134{
Rich Felker3f72cda2011-09-18 10:14:37 -0400135 pthread_t self = p;
Rich Felker9e01be62017-09-06 20:37:19 -0400136 /* States for startlock:
137 * 0 = no need for start sync
138 * 1 = waiting for parent to do work
139 * 2 = failure in parent, child must abort
140 * 3 = success in parent, child must restore sigmask */
Rich Felker1e21e782012-11-11 15:38:04 -0500141 if (self->startlock[0]) {
142 __wait(self->startlock, 0, 1, 1);
Rich Felker9e01be62017-09-06 20:37:19 -0400143 if (self->startlock[0] == 2) {
Rich Felker1e21e782012-11-11 15:38:04 -0500144 self->detached = 2;
145 pthread_exit(0);
146 }
Rich Felker2c074b02013-04-26 19:48:01 -0400147 __restore_sigs(self->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500148 }
Rich Felker99b8a252011-05-07 23:23:58 -0400149 if (self->unblock_cancel)
Rich Felker2f437042012-08-09 22:52:13 -0400150 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
Rich Felkerccc7b4c2013-03-26 23:07:31 -0400151 SIGPT_SET, 0, _NSIG/8);
Rich Felker23614b02014-09-07 10:28:08 -0400152 __pthread_exit(self->start(self->start_arg));
153 return 0;
154}
155
156static int start_c11(void *p)
157{
158 pthread_t self = p;
159 int (*start)(void*) = (int(*)(void*)) self->start;
160 __pthread_exit((void *)(uintptr_t)start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -0400161 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500162}
163
Rich Felker0b44a032011-02-12 00:22:29 -0500164#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
165
166/* pthread_key_create.c overrides this */
Rich Felkera6adb2b2014-07-16 21:32:06 -0400167static volatile size_t dummy = 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500168weak_alias(dummy, __pthread_tsd_size);
Rich Felkera6adb2b2014-07-16 21:32:06 -0400169static void *dummy_tsd[1] = { 0 };
Rich Felkerdab441a2014-03-24 16:57:11 -0400170weak_alias(dummy_tsd, __pthread_tsd_main);
Rich Felker0b44a032011-02-12 00:22:29 -0500171
Rich Felker78a8ef42015-01-15 23:17:38 -0500172volatile int __block_new_threads = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500173size_t __default_stacksize = DEFAULT_STACK_SIZE;
174size_t __default_guardsize = DEFAULT_GUARD_SIZE;
Rich Felker78a8ef42015-01-15 23:17:38 -0500175
Rich Felkera6adb2b2014-07-16 21:32:06 -0400176static FILE *volatile dummy_file = 0;
Rich Felkerdba68bf2011-07-30 08:02:14 -0400177weak_alias(dummy_file, __stdin_used);
178weak_alias(dummy_file, __stdout_used);
179weak_alias(dummy_file, __stderr_used);
180
181static void init_file_lock(FILE *f)
182{
183 if (f && f->lock<0) f->lock = 0;
184}
185
Rich Felkerdcd60372012-10-05 11:51:50 -0400186void *__copy_tls(unsigned char *);
Rich Felker8431d792012-10-04 16:35:46 -0400187
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200188int __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 -0500189{
Rich Felker23614b02014-09-07 10:28:08 -0400190 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
Rich Felkerd5142642013-02-01 22:10:40 -0500191 size_t size, guard;
Rich Felkerdab441a2014-03-24 16:57:11 -0400192 struct pthread *self, *new;
Rich Felker14a835b2013-03-31 23:25:55 -0400193 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
Rich Felkerf68a3462013-09-16 10:54:31 -0400194 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
Rich Felker271c2112013-09-16 10:56:01 -0400195 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
Rich Felkerf68a3462013-09-16 10:54:31 -0400196 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
Rich Felker1e21e782012-11-11 15:38:04 -0500197 int do_sched = 0;
Rich Felker31fb1742016-11-08 12:09:05 -0500198 pthread_attr_t attr = { 0 };
Rich Felker0b44a032011-02-12 00:22:29 -0500199
Rich Felkerdab441a2014-03-24 16:57:11 -0400200 if (!libc.can_do_threads) return ENOSYS;
201 self = __pthread_self();
Rich Felker9080cc12011-04-17 16:53:54 -0400202 if (!libc.threaded) {
Rich Felker1b0cdc82015-06-16 07:11:19 +0000203 for (FILE *f=*__ofl_lock(); f; f=f->next)
Rich Felkerdba68bf2011-07-30 08:02:14 -0400204 init_file_lock(f);
Rich Felker1b0cdc82015-06-16 07:11:19 +0000205 __ofl_unlock();
Rich Felkerdba68bf2011-07-30 08:02:14 -0400206 init_file_lock(__stdin_used);
207 init_file_lock(__stdout_used);
208 init_file_lock(__stderr_used);
Rich Felkerdab441a2014-03-24 16:57:11 -0400209 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
Rich Felker689e0e62014-03-24 17:39:08 -0400210 self->tsd = (void **)__pthread_tsd_main;
Rich Felker9080cc12011-04-17 16:53:54 -0400211 libc.threaded = 1;
212 }
Rich Felker23614b02014-09-07 10:28:08 -0400213 if (attrp && !c11) attr = *attrp;
Rich Felker0b44a032011-02-12 00:22:29 -0500214
Rich Felkerdcd60372012-10-05 11:51:50 -0400215 __acquire_ptc();
Rich Felker31fb1742016-11-08 12:09:05 -0500216 if (!attrp || c11) {
217 attr._a_stacksize = __default_stacksize;
218 attr._a_guardsize = __default_guardsize;
219 }
220
Rich Felker78a8ef42015-01-15 23:17:38 -0500221 if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
Rich Felkerdcd60372012-10-05 11:51:50 -0400222
Rich Felkerd5142642013-02-01 22:10:40 -0500223 if (attr._a_stackaddr) {
224 size_t need = libc.tls_size + __pthread_tsd_size;
Rich Felker33ce9202016-11-07 20:47:24 -0500225 size = attr._a_stacksize;
Rich Felkerd5142642013-02-01 22:10:40 -0500226 stack = (void *)(attr._a_stackaddr & -16);
Rich Felkerced64992013-04-06 01:15:08 -0400227 stack_limit = (void *)(attr._a_stackaddr - size);
Rich Felkerd5142642013-02-01 22:10:40 -0500228 /* Use application-provided stack for TLS only when
229 * it does not take more than ~12% or 2k of the
230 * application's stack space. */
231 if (need < size/8 && need < 2048) {
232 tsd = stack - __pthread_tsd_size;
233 stack = tsd - libc.tls_size;
Rich Felkera6293282014-08-22 14:05:10 -0400234 memset(stack, 0, need);
Rich Felkerd5142642013-02-01 22:10:40 -0500235 } else {
236 size = ROUND(need);
Rich Felker819006a2012-06-09 19:53:29 -0400237 }
William Pitcock7c709f22018-02-02 20:08:55 +0000238 guard = 0;
Rich Felkerd5142642013-02-01 22:10:40 -0500239 } else {
Rich Felker33ce9202016-11-07 20:47:24 -0500240 guard = ROUND(attr._a_guardsize);
241 size = guard + ROUND(attr._a_stacksize
Rich Felkerd5142642013-02-01 22:10:40 -0500242 + libc.tls_size + __pthread_tsd_size);
243 }
244
245 if (!tsd) {
Rich Felker8431d792012-10-04 16:35:46 -0400246 if (guard) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200247 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500248 if (map == MAP_FAILED) goto fail;
Rich Felker75eceb32015-06-17 17:21:46 +0000249 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
250 && errno != ENOSYS) {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200251 __munmap(map, size);
Rich Felker72768ea2013-02-01 22:25:19 -0500252 goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400253 }
254 } else {
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200255 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker72768ea2013-02-01 22:25:19 -0500256 if (map == MAP_FAILED) goto fail;
Rich Felker8431d792012-10-04 16:35:46 -0400257 }
Rich Felker819006a2012-06-09 19:53:29 -0400258 tsd = map + size - __pthread_tsd_size;
Rich Felker14a835b2013-03-31 23:25:55 -0400259 if (!stack) {
260 stack = tsd - libc.tls_size;
261 stack_limit = map + guard;
262 }
Rich Felker11e4b922011-05-07 23:39:48 -0400263 }
Rich Felkerd5142642013-02-01 22:10:40 -0500264
265 new = __copy_tls(tsd - libc.tls_size);
Rich Felker0b44a032011-02-12 00:22:29 -0500266 new->map_base = map;
267 new->map_size = size;
Rich Felker14a835b2013-03-31 23:25:55 -0400268 new->stack = stack;
269 new->stack_size = stack - stack_limit;
William Pitcock7c709f22018-02-02 20:08:55 +0000270 new->guard_size = guard;
Rich Felker0b44a032011-02-12 00:22:29 -0500271 new->start = entry;
272 new->start_arg = arg;
273 new->self = new;
274 new->tsd = (void *)tsd;
Rich Felker0bc03092014-07-02 19:33:19 -0400275 new->locale = &libc.global_locale;
Rich Felkerd5142642013-02-01 22:10:40 -0500276 if (attr._a_detach) {
Rich Felker92f83962012-07-11 23:36:46 -0400277 new->detached = 1;
Rich Felkerf68a3462013-09-16 10:54:31 -0400278 flags -= CLONE_CHILD_CLEARTID;
Rich Felker92f83962012-07-11 23:36:46 -0400279 }
Rich Felkerd5142642013-02-01 22:10:40 -0500280 if (attr._a_sched) {
Rich Felker1e21e782012-11-11 15:38:04 -0500281 do_sched = new->startlock[0] = 1;
Rich Felker2c074b02013-04-26 19:48:01 -0400282 __block_app_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500283 }
Rich Felker4e98cce2015-04-10 00:54:48 -0400284 new->robust_list.head = &new->robust_list.head;
Rich Felkerbf619d82011-03-29 12:58:22 -0400285 new->unblock_cancel = self->cancel;
Rich Felker484194d2015-05-06 18:37:19 -0400286 new->CANARY = self->CANARY;
Rich Felker9e2d8202018-05-02 12:13:43 -0400287 new->join_futex = -1;
Rich Felker0b44a032011-02-12 00:22:29 -0500288
Rich Felker0b44a032011-02-12 00:22:29 -0500289 a_inc(&libc.threads_minus_1);
Rich Felker9e2d8202018-05-02 12:13:43 -0400290 ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->join_futex);
Rich Felker0b44a032011-02-12 00:22:29 -0500291
Rich Felkerdcd60372012-10-05 11:51:50 -0400292 __release_ptc();
Rich Felker0b44a032011-02-12 00:22:29 -0500293
Rich Felker1e21e782012-11-11 15:38:04 -0500294 if (do_sched) {
Rich Felker2c074b02013-04-26 19:48:01 -0400295 __restore_sigs(new->sigmask);
Rich Felker1e21e782012-11-11 15:38:04 -0500296 }
297
Rich Felker0b44a032011-02-12 00:22:29 -0500298 if (ret < 0) {
299 a_dec(&libc.threads_minus_1);
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200300 if (map) __munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500301 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500302 }
Rich Felker1e21e782012-11-11 15:38:04 -0500303
304 if (do_sched) {
305 ret = __syscall(SYS_sched_setscheduler, new->tid,
Rich Felkerd5142642013-02-01 22:10:40 -0500306 attr._a_policy, &attr._a_prio);
Rich Felker9e01be62017-09-06 20:37:19 -0400307 a_store(new->startlock, ret<0 ? 2 : 3);
Rich Felker1e21e782012-11-11 15:38:04 -0500308 __wake(new->startlock, 1, 1);
309 if (ret < 0) return -ret;
310 }
311
Rich Felker0b44a032011-02-12 00:22:29 -0500312 *res = new;
313 return 0;
Rich Felker72768ea2013-02-01 22:25:19 -0500314fail:
315 __release_ptc();
316 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500317}
Jens Gustedtdf7d0df2014-09-01 00:46:23 +0200318
319weak_alias(__pthread_exit, pthread_exit);
320weak_alias(__pthread_create, pthread_create);