pthread_create.c 6.4 KB

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