simplify calling of timer signal handler
[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         if (self->canceldisable) return;
61         if ((self->cancelpoint+=x)==1 && x>=0 && self->cancel)
62                 docancel(self);
63 }
64
65 /* "rsyscall" is a mechanism by which a thread can synchronously force all
66  * other threads to perform an arbitrary syscall. It is necessary to work
67  * around the non-conformant implementation of setuid() et al on Linux,
68  * which affect only the calling thread and not the whole process. This
69  * implementation performs some tricks with signal delivery to work around
70  * the fact that it does not keep any list of threads in userspace. */
71
72 static struct {
73         volatile int lock, hold, blocks, cnt;
74         unsigned long arg[6];
75         int nr;
76         int err;
77 } rs;
78
79 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
80 {
81         struct pthread *self = __pthread_self();
82
83         if (si->si_code > 0 || si->si_pid != self->pid ||
84                 rs.cnt == libc.threads_minus_1) return;
85
86         /* Threads which have already decremented themselves from the
87          * thread count must not increment rs.cnt or otherwise act. */
88         if (self->dead) {
89                 __wait(&rs.hold, 0, 1, 1);
90                 return;
91         }
92
93         if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
94                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
95
96         a_inc(&rs.cnt);
97         __wake(&rs.cnt, 1, 1);
98         while(rs.hold)
99                 __wait(&rs.hold, 0, 1, 1);
100         a_dec(&rs.cnt);
101         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
102 }
103
104 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
105 {
106         int i, ret;
107         sigset_t set = { 0 };
108         struct pthread *self = __pthread_self();
109         sigaddset(&set, SIGSYSCALL);
110
111         LOCK(&rs.lock);
112         while ((i=rs.blocks))
113                 __wait(&rs.blocks, 0, i, 1);
114
115         __libc_sigprocmask(SIG_BLOCK, &set, 0);
116
117         rs.nr = nr;
118         rs.arg[0] = a; rs.arg[1] = b;
119         rs.arg[2] = c; rs.arg[3] = d;
120         rs.arg[4] = d; rs.arg[5] = f;
121         rs.hold = 1;
122         rs.err = 0;
123         rs.cnt = 0;
124
125         /* Dispatch signals until all threads respond */
126         for (i=libc.threads_minus_1; i; i--)
127                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
128         while ((i=rs.cnt) < libc.threads_minus_1) {
129                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
130                 __wait(&rs.cnt, 0, i, 1);
131         }
132
133         /* Handle any lingering signals with no-op */
134         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
135
136         /* Resume other threads' signal handlers and wait for them */
137         rs.hold = 0;
138         __wake(&rs.hold, -1, 0);
139         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
140
141         if (rs.err) errno = rs.err, ret = -1;
142         else ret = syscall(nr, a, b, c, d, e, f);
143
144         UNLOCK(&rs.lock);
145         return ret;
146 }
147
148 static void init_threads()
149 {
150         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
151         libc.lock = __lock;
152         libc.lockfile = __lockfile;
153         libc.cancelpt = cancelpt;
154         libc.rsyscall = rsyscall;
155         sa.sa_sigaction = cancel_handler;
156         __libc_sigaction(SIGCANCEL, &sa, 0);
157         sigaddset(&sa.sa_mask, SIGSYSCALL);
158         sigaddset(&sa.sa_mask, SIGCANCEL);
159         sa.sa_sigaction = rsyscall_handler;
160         __libc_sigaction(SIGSYSCALL, &sa, 0);
161         sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
162 }
163
164 static int start(void *p)
165 {
166         struct pthread *self = p;
167         if (self->unblock_cancel) {
168                 sigset_t set;
169                 sigemptyset(&set);
170                 sigaddset(&set, SIGCANCEL);
171                 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
172         }
173         pthread_exit(self->start(self->start_arg));
174         return 0;
175 }
176
177 int __uniclone(void *, int (*)(), void *);
178
179 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
180
181 /* pthread_key_create.c overrides this */
182 static const size_t dummy = 0;
183 weak_alias(dummy, __pthread_tsd_size);
184
185 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
186 {
187         static int init;
188         int ret;
189         size_t size, guard;
190         struct pthread *self = pthread_self(), *new;
191         unsigned char *map, *stack, *tsd;
192         static const pthread_attr_t default_attr;
193
194         if (!self) return errno = ENOSYS;
195         if (!init && ++init) init_threads();
196
197         if (!attr) attr = &default_attr;
198         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
199         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
200         size += __pthread_tsd_size;
201         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
202         if (!map) return EAGAIN;
203         if (guard) mprotect(map, guard, PROT_NONE);
204
205         tsd = map + size - __pthread_tsd_size;
206         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
207         new->map_base = map;
208         new->map_size = size;
209         new->pid = self->pid;
210         new->errno_ptr = &new->errno_val;
211         new->start = entry;
212         new->start_arg = arg;
213         new->self = new;
214         new->tsd = (void *)tsd;
215         new->detached = attr->_a_detach;
216         new->attr = *attr;
217         new->unblock_cancel = self->cancel;
218         new->result = PTHREAD_CANCELED;
219         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
220         new->tlsdesc[1] = (uintptr_t)new;
221         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
222
223         /* We must synchronize new thread creation with rsyscall
224          * delivery. This looks to be the least expensive way: */
225         a_inc(&rs.blocks);
226         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
227
228         a_inc(&libc.threads_minus_1);
229         ret = __uniclone(stack, start, new);
230
231         a_dec(&rs.blocks);
232         if (rs.lock) __wake(&rs.blocks, 1, 1);
233
234         if (ret < 0) {
235                 a_dec(&libc.threads_minus_1);
236                 munmap(map, size);
237                 return EAGAIN;
238         }
239         *res = new;
240         return 0;
241 }
242
243 void pthread_exit(void *result)
244 {
245         struct pthread *self = pthread_self();
246         self->result = result;
247         docancel(self);
248 }