blob: 6f1bb0e8172b3c8045b4fa0867071c3118dd1e6c [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "pthread_impl.h"
2
Rich Felkerfd80cfa2011-04-03 02:33:50 -04003static void dummy_1(pthread_t self)
4{
5}
6weak_alias(dummy_1, __pthread_tsd_run_dtors);
7
Rich Felkerea343362011-03-25 22:13:57 -04008#ifdef __pthread_unwind_next
9#undef __pthread_unwind_next
10#define __pthread_unwind_next __pthread_unwind_next_3
11#endif
12
Rich Felker1a9a2ff2011-02-13 19:58:30 -050013void __pthread_unwind_next(struct __ptcb *cb)
14{
Rich Felker1a9a2ff2011-02-13 19:58:30 -050015 pthread_t self;
16
17 if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
18
19 self = pthread_self();
Rich Felker1a9a2ff2011-02-13 19:58:30 -050020
Rich Felker1a9a2ff2011-02-13 19:58:30 -050021 LOCK(&self->exitlock);
22
Rich Felkerfd80cfa2011-04-03 02:33:50 -040023 __pthread_tsd_run_dtors(self);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050024
Rich Felker5fcebcd2011-03-10 18:31:37 -050025 /* Mark this thread dead before decrementing count */
26 self->dead = 1;
Rich Felker19eb13b2011-02-19 11:04:36 -050027
Rich Felkerfb11b6b2011-02-19 10:38:57 -050028 if (!a_fetch_add(&libc.threads_minus_1, -1))
29 exit(0);
30
Rich Felker5fcebcd2011-03-10 18:31:37 -050031 if (self->detached && self->map_base) {
Rich Felker685e40b2011-03-19 21:36:10 -040032 syscall(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050033 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -050034 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050035
Rich Felkeraa398f52011-03-20 00:16:43 -040036 syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050037}
Rich Felker0b44a032011-02-12 00:22:29 -050038
39static void docancel(struct pthread *self)
40{
41 struct __ptcb cb = { .__next = self->cancelbuf };
Rich Felkerb4700302011-03-24 14:18:00 -040042 self->canceldisable = 1;
43 self->cancelasync = 0;
Rich Felker0b44a032011-02-12 00:22:29 -050044 __pthread_unwind_next(&cb);
45}
46
47static void cancel_handler(int sig, siginfo_t *si, void *ctx)
48{
Rich Felker1a9a2ff2011-02-13 19:58:30 -050049 struct pthread *self = __pthread_self();
Rich Felkerbf619d82011-03-29 12:58:22 -040050 if (!self->cancel) {
51 if (si->si_code == SI_TIMER && libc.sigtimer)
52 libc.sigtimer(sig, si, ctx);
53 return;
54 }
Rich Felkerb4700302011-03-24 14:18:00 -040055 if (self->canceldisable) return;
56 if (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx)))
57 docancel(self);
Rich Felker0b44a032011-02-12 00:22:29 -050058}
59
Rich Felker1a9a2ff2011-02-13 19:58:30 -050060static void cancelpt(int x)
61{
62 struct pthread *self = __pthread_self();
63 if (self->canceldisable) return;
Rich Felkerb4700302011-03-24 14:18:00 -040064 if ((self->cancelpoint+=x)==1 && x>=0 && self->cancel)
65 docancel(self);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050066}
67
Rich Felker0b44a032011-02-12 00:22:29 -050068/* "rsyscall" is a mechanism by which a thread can synchronously force all
69 * other threads to perform an arbitrary syscall. It is necessary to work
70 * around the non-conformant implementation of setuid() et al on Linux,
71 * which affect only the calling thread and not the whole process. This
72 * implementation performs some tricks with signal delivery to work around
73 * the fact that it does not keep any list of threads in userspace. */
74
75static struct {
76 volatile int lock, hold, blocks, cnt;
77 unsigned long arg[6];
78 int nr;
79 int err;
80} rs;
81
82static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
83{
Rich Felker5fcebcd2011-03-10 18:31:37 -050084 struct pthread *self = __pthread_self();
Rich Felker52213f72011-03-10 11:59:39 -050085
Rich Felker5fcebcd2011-03-10 18:31:37 -050086 if (si->si_code > 0 || si->si_pid != self->pid ||
87 rs.cnt == libc.threads_minus_1) return;
88
89 /* Threads which have already decremented themselves from the
90 * thread count must not increment rs.cnt or otherwise act. */
91 if (self->dead) {
92 __wait(&rs.hold, 0, 1, 1);
93 return;
94 }
Rich Felker0b44a032011-02-12 00:22:29 -050095
Rich Felker685e40b2011-03-19 21:36:10 -040096 if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
Rich Felker0b44a032011-02-12 00:22:29 -050097 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
98
99 a_inc(&rs.cnt);
100 __wake(&rs.cnt, 1, 1);
101 while(rs.hold)
102 __wait(&rs.hold, 0, 1, 1);
103 a_dec(&rs.cnt);
104 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
105}
106
107static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
108{
109 int i, ret;
110 sigset_t set = { 0 };
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500111 struct pthread *self = __pthread_self();
Rich Felker0b44a032011-02-12 00:22:29 -0500112 sigaddset(&set, SIGSYSCALL);
113
114 LOCK(&rs.lock);
115 while ((i=rs.blocks))
116 __wait(&rs.blocks, 0, i, 1);
117
118 __libc_sigprocmask(SIG_BLOCK, &set, 0);
119
120 rs.nr = nr;
121 rs.arg[0] = a; rs.arg[1] = b;
122 rs.arg[2] = c; rs.arg[3] = d;
123 rs.arg[4] = d; rs.arg[5] = f;
124 rs.hold = 1;
125 rs.err = 0;
126 rs.cnt = 0;
127
128 /* Dispatch signals until all threads respond */
129 for (i=libc.threads_minus_1; i; i--)
130 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
131 while ((i=rs.cnt) < libc.threads_minus_1) {
132 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
133 __wait(&rs.cnt, 0, i, 1);
134 }
135
136 /* Handle any lingering signals with no-op */
137 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
138
139 /* Resume other threads' signal handlers and wait for them */
140 rs.hold = 0;
141 __wake(&rs.hold, -1, 0);
142 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
143
144 if (rs.err) errno = rs.err, ret = -1;
Rich Felker685e40b2011-03-19 21:36:10 -0400145 else ret = syscall(nr, a, b, c, d, e, f);
Rich Felker0b44a032011-02-12 00:22:29 -0500146
147 UNLOCK(&rs.lock);
148 return ret;
149}
150
Rich Felker0b44a032011-02-12 00:22:29 -0500151static void init_threads()
152{
153 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
154 libc.lock = __lock;
Rich Felker5eb0d332011-03-12 21:55:45 -0500155 libc.lockfile = __lockfile;
Rich Felker0b44a032011-02-12 00:22:29 -0500156 libc.cancelpt = cancelpt;
157 libc.rsyscall = rsyscall;
158 sa.sa_sigaction = cancel_handler;
159 __libc_sigaction(SIGCANCEL, &sa, 0);
160 sigaddset(&sa.sa_mask, SIGSYSCALL);
161 sigaddset(&sa.sa_mask, SIGCANCEL);
162 sa.sa_sigaction = rsyscall_handler;
163 __libc_sigaction(SIGSYSCALL, &sa, 0);
164 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
165}
166
167static int start(void *p)
168{
169 struct pthread *self = p;
Rich Felkerbf619d82011-03-29 12:58:22 -0400170 if (self->unblock_cancel) {
171 sigset_t set;
172 sigemptyset(&set);
173 sigaddset(&set, SIGCANCEL);
174 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
175 }
Rich Felker0b44a032011-02-12 00:22:29 -0500176 pthread_exit(self->start(self->start_arg));
177 return 0;
178}
179
Rich Felker0b2006c2011-02-15 03:24:58 -0500180int __uniclone(void *, int (*)(), void *);
Rich Felker0b44a032011-02-12 00:22:29 -0500181
182#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
183
184/* pthread_key_create.c overrides this */
185static const size_t dummy = 0;
186weak_alias(dummy, __pthread_tsd_size);
187
188int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
189{
190 static int init;
191 int ret;
192 size_t size, guard;
193 struct pthread *self = pthread_self(), *new;
194 unsigned char *map, *stack, *tsd;
195 static const pthread_attr_t default_attr;
196
197 if (!self) return errno = ENOSYS;
198 if (!init && ++init) init_threads();
199
200 if (!attr) attr = &default_attr;
Rich Felkere8827562011-02-17 17:16:20 -0500201 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
202 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
Rich Felker0b44a032011-02-12 00:22:29 -0500203 size += __pthread_tsd_size;
204 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
205 if (!map) return EAGAIN;
Rich Felker29fae652011-03-16 11:36:21 -0400206 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -0500207
208 tsd = map + size - __pthread_tsd_size;
209 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
210 new->map_base = map;
211 new->map_size = size;
212 new->pid = self->pid;
213 new->errno_ptr = &new->errno_val;
214 new->start = entry;
215 new->start_arg = arg;
216 new->self = new;
217 new->tsd = (void *)tsd;
Rich Felkere8827562011-02-17 17:16:20 -0500218 new->detached = attr->_a_detach;
Rich Felker0b44a032011-02-12 00:22:29 -0500219 new->attr = *attr;
Rich Felkerbf619d82011-03-29 12:58:22 -0400220 new->unblock_cancel = self->cancel;
Rich Felker4ae5e812011-04-01 22:15:03 -0400221 new->result = PTHREAD_CANCELED;
Rich Felker0b44a032011-02-12 00:22:29 -0500222 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
223 new->tlsdesc[1] = (uintptr_t)new;
224 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
225
226 /* We must synchronize new thread creation with rsyscall
227 * delivery. This looks to be the least expensive way: */
228 a_inc(&rs.blocks);
229 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
230
231 a_inc(&libc.threads_minus_1);
Rich Felker0b2006c2011-02-15 03:24:58 -0500232 ret = __uniclone(stack, start, new);
Rich Felker0b44a032011-02-12 00:22:29 -0500233
234 a_dec(&rs.blocks);
235 if (rs.lock) __wake(&rs.blocks, 1, 1);
236
237 if (ret < 0) {
238 a_dec(&libc.threads_minus_1);
239 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500240 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500241 }
242 *res = new;
243 return 0;
244}
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500245
246void pthread_exit(void *result)
247{
248 struct pthread *self = pthread_self();
249 self->result = result;
250 docancel(self);
251}