6fa484c72a38df8bc6d45fb06df7537443415a06
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 #define pthread_self __pthread_self
4
5 static void docancel(struct pthread *self)
6 {
7         struct __ptcb cb = { .__next = self->cancelbuf };
8         __pthread_unwind_next(&cb);
9 }
10
11 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
12 {
13         struct pthread *self = pthread_self();
14         self->cancel = 1;
15         if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
16                 return;
17         docancel(self);
18 }
19
20 /* "rsyscall" is a mechanism by which a thread can synchronously force all
21  * other threads to perform an arbitrary syscall. It is necessary to work
22  * around the non-conformant implementation of setuid() et al on Linux,
23  * which affect only the calling thread and not the whole process. This
24  * implementation performs some tricks with signal delivery to work around
25  * the fact that it does not keep any list of threads in userspace. */
26
27 static struct {
28         volatile int lock, hold, blocks, cnt;
29         unsigned long arg[6];
30         int nr;
31         int err;
32 } rs;
33
34 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
35 {
36         if (rs.cnt == libc.threads_minus_1) return;
37
38         if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
39                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
40
41         a_inc(&rs.cnt);
42         __wake(&rs.cnt, 1, 1);
43         while(rs.hold)
44                 __wait(&rs.hold, 0, 1, 1);
45         a_dec(&rs.cnt);
46         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
47 }
48
49 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
50 {
51         int i, ret;
52         sigset_t set = { 0 };
53         struct pthread *self = pthread_self();
54         sigaddset(&set, SIGSYSCALL);
55
56         LOCK(&rs.lock);
57         while ((i=rs.blocks))
58                 __wait(&rs.blocks, 0, i, 1);
59
60         __libc_sigprocmask(SIG_BLOCK, &set, 0);
61
62         rs.nr = nr;
63         rs.arg[0] = a; rs.arg[1] = b;
64         rs.arg[2] = c; rs.arg[3] = d;
65         rs.arg[4] = d; rs.arg[5] = f;
66         rs.hold = 1;
67         rs.err = 0;
68         rs.cnt = 0;
69
70         /* Dispatch signals until all threads respond */
71         for (i=libc.threads_minus_1; i; i--)
72                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
73         while ((i=rs.cnt) < libc.threads_minus_1) {
74                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
75                 __wait(&rs.cnt, 0, i, 1);
76         }
77
78         /* Handle any lingering signals with no-op */
79         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
80
81         /* Resume other threads' signal handlers and wait for them */
82         rs.hold = 0;
83         __wake(&rs.hold, -1, 0);
84         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
85
86         if (rs.err) errno = rs.err, ret = -1;
87         else ret = syscall6(nr, a, b, c, d, e, f);
88
89         UNLOCK(&rs.lock);
90         return ret;
91 }
92
93 static void cancelpt(int x)
94 {
95         struct pthread *self = pthread_self();
96         if (self->canceldisable) return;
97         self->cancelpoint = x;
98         if (self->cancel) docancel(self);
99 }
100
101 static void init_threads()
102 {
103         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
104         libc.lock = __lock;
105         libc.cancelpt = cancelpt;
106         libc.rsyscall = rsyscall;
107         sa.sa_sigaction = cancel_handler;
108         __libc_sigaction(SIGCANCEL, &sa, 0);
109         sigaddset(&sa.sa_mask, SIGSYSCALL);
110         sigaddset(&sa.sa_mask, SIGCANCEL);
111         sa.sa_sigaction = rsyscall_handler;
112         __libc_sigaction(SIGSYSCALL, &sa, 0);
113         sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
114 }
115
116 static int start(void *p)
117 {
118         struct pthread *self = p;
119         pthread_exit(self->start(self->start_arg));
120         return 0;
121 }
122
123 #undef pthread_self
124
125 #define CLONE_MAGIC 0x7d0f00
126 int __clone(int (*)(void *), void *, int, void *, pid_t *, void *, pid_t *);
127
128 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
129
130 /* pthread_key_create.c overrides this */
131 static const size_t dummy = 0;
132 weak_alias(dummy, __pthread_tsd_size);
133
134 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
135 {
136         static int init;
137         int ret;
138         size_t size, guard;
139         struct pthread *self = pthread_self(), *new;
140         unsigned char *map, *stack, *tsd;
141         static const pthread_attr_t default_attr;
142
143         if (!self) return errno = ENOSYS;
144         if (!init && ++init) init_threads();
145
146         if (!attr) attr = &default_attr;
147         guard = ROUND(attr->__guardsize + DEFAULT_GUARD_SIZE);
148         size = guard + ROUND(attr->__stacksize + DEFAULT_STACK_SIZE);
149         size += __pthread_tsd_size;
150         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
151         if (!map) return EAGAIN;
152         mprotect(map, guard, PROT_NONE);
153
154         tsd = map + size - __pthread_tsd_size;
155         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
156         new->map_base = map;
157         new->map_size = size;
158         new->pid = self->pid;
159         new->errno_ptr = &new->errno_val;
160         new->start = entry;
161         new->start_arg = arg;
162         new->self = new;
163         new->tsd = (void *)tsd;
164         new->detached = attr->__detach;
165         new->attr = *attr;
166         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
167         new->tlsdesc[1] = (uintptr_t)new;
168         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
169
170         /* We must synchronize new thread creation with rsyscall
171          * delivery. This looks to be the least expensive way: */
172         a_inc(&rs.blocks);
173         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
174
175         a_inc(&libc.threads_minus_1);
176         ret = __clone(start, stack, CLONE_MAGIC, new,
177                 &new->tid, &new->tlsdesc, &new->tid);
178
179         a_dec(&rs.blocks);
180         if (rs.lock) __wake(&rs.blocks, 1, 1);
181
182         if (ret < 0) {
183                 a_dec(&libc.threads_minus_1);
184                 munmap(map, size);
185                 return -ret;
186         }
187         *res = new;
188         return 0;
189 }