52487001484ce8c1edfc08cd7b7027055c4a130d
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 static void dummy_1(pthread_t self)
4 {
5 }
6 weak_alias(dummy_1, __pthread_tsd_run_dtors);
7 weak_alias(dummy_1, __sigtimer_handler);
8
9 #ifdef __pthread_unwind_next
10 #undef __pthread_unwind_next
11 #define __pthread_unwind_next __pthread_unwind_next_3
12 #endif
13
14 void __pthread_unwind_next(struct __ptcb *cb)
15 {
16         pthread_t self;
17
18         if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
19
20         self = pthread_self();
21
22         LOCK(&self->exitlock);
23
24         __pthread_tsd_run_dtors(self);
25
26         /* Mark this thread dead before decrementing count */
27         self->dead = 1;
28
29         if (!a_fetch_add(&libc.threads_minus_1, -1))
30                 exit(0);
31
32         if (self->detached && self->map_base) {
33                 syscall(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
34                 __unmapself(self->map_base, self->map_size);
35         }
36
37         syscall(SYS_exit, 0);
38 }
39
40 static void docancel(struct pthread *self)
41 {
42         struct __ptcb cb = { .__next = self->cancelbuf };
43         self->canceldisable = 1;
44         self->cancelasync = 0;
45         __pthread_unwind_next(&cb);
46 }
47
48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
49 {
50         struct pthread *self = __pthread_self();
51         if (si->si_code == SI_TIMER) __sigtimer_handler(self);
52         if (self->cancel && !self->canceldisable &&
53             (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx))))
54                 docancel(self);
55 }
56
57 static void cancelpt(int x)
58 {
59         struct pthread *self = __pthread_self();
60         switch (x) {
61         case 1:
62                 self->cancelpoint++;
63         case 0:
64                 if (self->cancel && self->cancelpoint==1 && !self->canceldisable)
65                         docancel(self);
66                 break;
67         case -1:
68                 self->cancelpoint--;
69                 break;
70         default:
71                 self->canceldisable += x;
72         }
73 }
74
75 /* "rsyscall" is a mechanism by which a thread can synchronously force all
76  * other threads to perform an arbitrary syscall. It is necessary to work
77  * around the non-conformant implementation of setuid() et al on Linux,
78  * which affect only the calling thread and not the whole process. This
79  * implementation performs some tricks with signal delivery to work around
80  * the fact that it does not keep any list of threads in userspace. */
81
82 static struct {
83         volatile int lock, hold, blocks, cnt;
84         unsigned long arg[6];
85         int nr;
86         int err;
87 } rs;
88
89 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
90 {
91         struct pthread *self = __pthread_self();
92
93         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
94
95         /* Threads which have already decremented themselves from the
96          * thread count must not increment rs.cnt or otherwise act. */
97         if (self->dead) {
98                 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
99                 return;
100         }
101
102         if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
103                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
104
105         a_inc(&rs.cnt);
106         __wake(&rs.cnt, 1, 1);
107         while(rs.hold)
108                 __wait(&rs.hold, 0, 1, 1);
109         a_dec(&rs.cnt);
110         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
111 }
112
113 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
114 {
115         int i, ret;
116         sigset_t set = { 0 };
117         struct pthread *self = __pthread_self();
118         sigaddset(&set, SIGSYSCALL);
119
120         LOCK(&rs.lock);
121         while ((i=rs.blocks))
122                 __wait(&rs.blocks, 0, i, 1);
123
124         __libc_sigprocmask(SIG_BLOCK, &set, 0);
125
126         rs.nr = nr;
127         rs.arg[0] = a; rs.arg[1] = b;
128         rs.arg[2] = c; rs.arg[3] = d;
129         rs.arg[4] = d; rs.arg[5] = f;
130         rs.err = 0;
131         rs.cnt = 0;
132         rs.hold = 1;
133
134         /* Dispatch signals until all threads respond */
135         for (i=libc.threads_minus_1; i; i--)
136                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
137         while ((i=rs.cnt) < libc.threads_minus_1) {
138                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
139                 __wait(&rs.cnt, 0, i, 1);
140         }
141
142         /* Handle any lingering signals with no-op */
143         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
144
145         /* Resume other threads' signal handlers and wait for them */
146         rs.hold = 0;
147         __wake(&rs.hold, -1, 0);
148         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
149
150         if (rs.err) errno = rs.err, ret = -1;
151         else ret = syscall(nr, a, b, c, d, e, f);
152
153         UNLOCK(&rs.lock);
154         return ret;
155 }
156
157 static void init_threads()
158 {
159         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
160         libc.lock = __lock;
161         libc.lockfile = __lockfile;
162         libc.cancelpt = cancelpt;
163         libc.rsyscall = rsyscall;
164
165         sigfillset(&sa.sa_mask);
166         sa.sa_sigaction = rsyscall_handler;
167         __libc_sigaction(SIGSYSCALL, &sa, 0);
168
169         sigemptyset(&sa.sa_mask);
170         sa.sa_sigaction = cancel_handler;
171         __libc_sigaction(SIGCANCEL, &sa, 0);
172
173         sigaddset(&sa.sa_mask, SIGSYSCALL);
174         sigaddset(&sa.sa_mask, SIGCANCEL);
175         __libc_sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
176 }
177
178 static int start(void *p)
179 {
180         struct pthread *self = p;
181         if (self->unblock_cancel) {
182                 sigset_t set;
183                 sigemptyset(&set);
184                 sigaddset(&set, SIGCANCEL);
185                 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
186         }
187         pthread_exit(self->start(self->start_arg));
188         return 0;
189 }
190
191 int __uniclone(void *, int (*)(), void *);
192
193 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
194
195 /* pthread_key_create.c overrides this */
196 static const size_t dummy = 0;
197 weak_alias(dummy, __pthread_tsd_size);
198
199 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
200 {
201         static int init;
202         int ret;
203         size_t size, guard;
204         struct pthread *self = pthread_self(), *new;
205         unsigned char *map, *stack, *tsd;
206         static const pthread_attr_t default_attr;
207
208         if (!self) return ENOSYS;
209         if (!init && ++init) init_threads();
210
211         if (!attr) attr = &default_attr;
212         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
213         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
214         size += __pthread_tsd_size;
215         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
216         if (!map) return EAGAIN;
217         if (guard) mprotect(map, guard, PROT_NONE);
218
219         tsd = map + size - __pthread_tsd_size;
220         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
221         new->map_base = map;
222         new->map_size = size;
223         new->pid = self->pid;
224         new->errno_ptr = &new->errno_val;
225         new->start = entry;
226         new->start_arg = arg;
227         new->self = new;
228         new->tsd = (void *)tsd;
229         new->detached = attr->_a_detach;
230         new->attr = *attr;
231         new->unblock_cancel = self->cancel;
232         new->result = PTHREAD_CANCELED;
233         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
234         new->tlsdesc[1] = (uintptr_t)new;
235         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
236
237         /* We must synchronize new thread creation with rsyscall
238          * delivery. This looks to be the least expensive way: */
239         a_inc(&rs.blocks);
240         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
241
242         a_inc(&libc.threads_minus_1);
243         ret = __uniclone(stack, start, new);
244
245         a_dec(&rs.blocks);
246         if (rs.lock) __wake(&rs.blocks, 1, 1);
247
248         if (ret < 0) {
249                 a_dec(&libc.threads_minus_1);
250                 munmap(map, size);
251                 return EAGAIN;
252         }
253         *res = new;
254         return 0;
255 }
256
257 void pthread_exit(void *result)
258 {
259         struct pthread *self = pthread_self();
260         self->result = result;
261         docancel(self);
262 }