1 #include "pthread_impl.h"
3 #define pthread_self __pthread_self
5 static void docancel(struct pthread *self)
7 struct __ptcb cb = { .__next = self->cancelbuf };
8 __pthread_unwind_next(&cb);
11 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
13 struct pthread *self = pthread_self();
15 if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
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. */
28 volatile int lock, hold, blocks, cnt;
34 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
36 if (rs.cnt == libc.threads_minus_1) return;
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;
42 __wake(&rs.cnt, 1, 1);
44 __wait(&rs.hold, 0, 1, 1);
46 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
49 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
53 struct pthread *self = pthread_self();
54 sigaddset(&set, SIGSYSCALL);
58 __wait(&rs.blocks, 0, i, 1);
60 __libc_sigprocmask(SIG_BLOCK, &set, 0);
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;
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);
78 /* Handle any lingering signals with no-op */
79 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
81 /* Resume other threads' signal handlers and wait for them */
83 __wake(&rs.hold, -1, 0);
84 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
86 if (rs.err) errno = rs.err, ret = -1;
87 else ret = syscall6(nr, a, b, c, d, e, f);
93 static void cancelpt(int x)
95 struct pthread *self = pthread_self();
96 if (self->canceldisable) return;
97 self->cancelpoint = x;
98 if (self->cancel) docancel(self);
101 static void init_threads()
103 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
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);
116 static int start(void *p)
118 struct pthread *self = p;
119 pthread_exit(self->start(self->start_arg));
125 #define CLONE_MAGIC 0x7d0f00
126 int __clone(int (*)(void *), void *, int, void *, pid_t *, void *, pid_t *);
128 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
130 /* pthread_key_create.c overrides this */
131 static const size_t dummy = 0;
132 weak_alias(dummy, __pthread_tsd_size);
134 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
139 struct pthread *self = pthread_self(), *new;
140 unsigned char *map, *stack, *tsd;
141 static const pthread_attr_t default_attr;
143 if (!self) return errno = ENOSYS;
144 if (!init && ++init) init_threads();
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);
154 tsd = map + size - __pthread_tsd_size;
155 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
157 new->map_size = size;
158 new->pid = self->pid;
159 new->errno_ptr = &new->errno_val;
161 new->start_arg = arg;
163 new->tsd = (void *)tsd;
164 new->detached = attr->__detach;
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);
170 /* We must synchronize new thread creation with rsyscall
171 * delivery. This looks to be the least expensive way: */
173 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
175 a_inc(&libc.threads_minus_1);
176 ret = __clone(start, stack, CLONE_MAGIC, new,
177 &new->tid, &new->tlsdesc, &new->tid);
180 if (rs.lock) __wake(&rs.blocks, 1, 1);
183 a_dec(&libc.threads_minus_1);