a9a08171c0c4d09c9ab497d9d8fd231fd6977562
[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         long r;
93
94         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
95
96         /* Threads which have already decremented themselves from the
97          * thread count must not increment rs.cnt or otherwise act. */
98         if (self->dead) {
99                 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
100                 return;
101         }
102
103         r = __syscall(rs.nr, rs.arg[0], rs.arg[1],
104                 rs.arg[2], rs.arg[3], rs.arg[4], rs.arg[5]);
105         if (r < 0) rs.err=-r;
106
107         a_inc(&rs.cnt);
108         __wake(&rs.cnt, 1, 1);
109         while(rs.hold)
110                 __wait(&rs.hold, 0, 1, 1);
111         a_dec(&rs.cnt);
112         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
113 }
114
115 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
116 {
117         int i, ret;
118         sigset_t set = { 0 };
119         struct pthread *self = __pthread_self();
120         sigaddset(&set, SIGSYSCALL);
121
122         LOCK(&rs.lock);
123         while ((i=rs.blocks))
124                 __wait(&rs.blocks, 0, i, 1);
125
126         __libc_sigprocmask(SIG_BLOCK, &set, 0);
127
128         rs.nr = nr;
129         rs.arg[0] = a; rs.arg[1] = b;
130         rs.arg[2] = c; rs.arg[3] = d;
131         rs.arg[4] = d; rs.arg[5] = f;
132         rs.err = 0;
133         rs.cnt = 0;
134         rs.hold = 1;
135
136         /* Dispatch signals until all threads respond */
137         for (i=libc.threads_minus_1; i; i--)
138                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
139         while ((i=rs.cnt) < libc.threads_minus_1) {
140                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
141                 __wait(&rs.cnt, 0, i, 1);
142         }
143
144         /* Handle any lingering signals with no-op */
145         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
146
147         /* Resume other threads' signal handlers and wait for them */
148         rs.hold = 0;
149         __wake(&rs.hold, -1, 0);
150         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
151
152         if (rs.err) errno = rs.err, ret = -1;
153         else ret = syscall(nr, a, b, c, d, e, f);
154
155         UNLOCK(&rs.lock);
156         return ret;
157 }
158
159 static void init_threads()
160 {
161         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
162         libc.lock = __lock;
163         libc.lockfile = __lockfile;
164         libc.cancelpt = cancelpt;
165         libc.rsyscall = rsyscall;
166
167         sigfillset(&sa.sa_mask);
168         sa.sa_sigaction = rsyscall_handler;
169         __libc_sigaction(SIGSYSCALL, &sa, 0);
170
171         sigemptyset(&sa.sa_mask);
172         sa.sa_sigaction = cancel_handler;
173         __libc_sigaction(SIGCANCEL, &sa, 0);
174
175         sigaddset(&sa.sa_mask, SIGSYSCALL);
176         sigaddset(&sa.sa_mask, SIGCANCEL);
177         __libc_sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
178 }
179
180 static int start(void *p)
181 {
182         struct pthread *self = p;
183         if (self->unblock_cancel) {
184                 sigset_t set;
185                 sigemptyset(&set);
186                 sigaddset(&set, SIGCANCEL);
187                 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
188         }
189         pthread_exit(self->start(self->start_arg));
190         return 0;
191 }
192
193 int __uniclone(void *, int (*)(), void *);
194
195 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
196
197 /* pthread_key_create.c overrides this */
198 static const size_t dummy = 0;
199 weak_alias(dummy, __pthread_tsd_size);
200
201 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
202 {
203         static int init;
204         int ret;
205         size_t size, guard;
206         struct pthread *self = pthread_self(), *new;
207         unsigned char *map, *stack, *tsd;
208         static const pthread_attr_t default_attr;
209
210         if (!self) return ENOSYS;
211         if (!init && ++init) init_threads();
212
213         if (!attr) attr = &default_attr;
214         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
215         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
216         size += __pthread_tsd_size;
217         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
218         if (!map) return EAGAIN;
219         if (guard) mprotect(map, guard, PROT_NONE);
220
221         tsd = map + size - __pthread_tsd_size;
222         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
223         new->map_base = map;
224         new->map_size = size;
225         new->pid = self->pid;
226         new->errno_ptr = &new->errno_val;
227         new->start = entry;
228         new->start_arg = arg;
229         new->self = new;
230         new->tsd = (void *)tsd;
231         new->detached = attr->_a_detach;
232         new->attr = *attr;
233         new->unblock_cancel = self->cancel;
234         new->result = PTHREAD_CANCELED;
235         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
236         new->tlsdesc[1] = (uintptr_t)new;
237         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
238
239         /* We must synchronize new thread creation with rsyscall
240          * delivery. This looks to be the least expensive way: */
241         a_inc(&rs.blocks);
242         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
243
244         a_inc(&libc.threads_minus_1);
245         ret = __uniclone(stack, start, new);
246
247         a_dec(&rs.blocks);
248         if (rs.lock) __wake(&rs.blocks, 1, 1);
249
250         if (ret < 0) {
251                 a_dec(&libc.threads_minus_1);
252                 munmap(map, size);
253                 return EAGAIN;
254         }
255         *res = new;
256         return 0;
257 }
258
259 void pthread_exit(void *result)
260 {
261         struct pthread *self = pthread_self();
262         self->result = result;
263         docancel(self);
264 }